Files
smarts3/ts/index.ts

105 lines
3.0 KiB
TypeScript

import * as plugins from './plugins.js';
import * as paths from './paths.js';
import { Smarts3Server } from './classes/smarts3-server.js';
export interface ISmarts3ContructorOptions {
port?: number;
cleanSlate?: boolean;
useCustomServer?: boolean; // Feature flag for custom server
}
export class Smarts3 {
// STATIC
public static async createAndStart(
optionsArg: ConstructorParameters<typeof Smarts3>[0],
) {
const smartS3Instance = new Smarts3(optionsArg);
await smartS3Instance.start();
return smartS3Instance;
}
// INSTANCE
public options: ISmarts3ContructorOptions;
public s3Instance: plugins.s3rver | Smarts3Server;
constructor(optionsArg: ISmarts3ContructorOptions) {
this.options = {
useCustomServer: true, // Default to custom server
...optionsArg,
};
}
public async start() {
if (this.options.useCustomServer) {
// Use new custom server
this.s3Instance = new Smarts3Server({
port: this.options.port || 3000,
address: '0.0.0.0',
directory: paths.bucketsDir,
cleanSlate: this.options.cleanSlate || false,
silent: false,
});
await this.s3Instance.start();
console.log('s3 server is running (custom implementation)');
} else {
// Use legacy s3rver
if (this.options.cleanSlate) {
await plugins.smartfile.fs.ensureEmptyDir(paths.bucketsDir);
} else {
await plugins.smartfile.fs.ensureDir(paths.bucketsDir);
}
this.s3Instance = new plugins.s3rver({
port: this.options.port || 3000,
address: '0.0.0.0',
silent: false,
directory: paths.bucketsDir,
});
await (this.s3Instance as plugins.s3rver).run();
console.log('s3 server is running (legacy s3rver)');
}
}
public async getS3Descriptor(
optionsArg?: Partial<plugins.tsclass.storage.IS3Descriptor>,
): Promise<plugins.tsclass.storage.IS3Descriptor> {
if (this.options.useCustomServer && this.s3Instance instanceof Smarts3Server) {
const descriptor = this.s3Instance.getS3Descriptor();
return {
...descriptor,
...(optionsArg ? optionsArg : {}),
};
}
// Legacy s3rver descriptor
return {
...{
accessKey: 'S3RVER',
accessSecret: 'S3RVER',
endpoint: '127.0.0.1',
port: this.options.port || 3000,
useSsl: false,
},
...(optionsArg ? optionsArg : {}),
};
}
public async createBucket(bucketNameArg: string) {
const smartbucketInstance = new plugins.smartbucket.SmartBucket(
await this.getS3Descriptor(),
);
const bucket = await smartbucketInstance.createBucket(bucketNameArg);
return bucket;
}
public async stop() {
if (this.s3Instance instanceof Smarts3Server) {
await this.s3Instance.stop();
} else {
await (this.s3Instance as plugins.s3rver).close();
}
}
}
// Export the custom server class for direct use
export { Smarts3Server } from './classes/smarts3-server.js';