Files
smarts3/ts/index.ts

105 lines
3.0 KiB
TypeScript
Raw Normal View History

import * as plugins from './plugins.js';
2022-04-14 10:36:24 +02:00
import * as paths from './paths.js';
import { Smarts3Server } from './classes/smarts3-server.js';
2021-12-18 01:41:50 +01:00
export interface ISmarts3ContructorOptions {
port?: number;
cleanSlate?: boolean;
useCustomServer?: boolean; // Feature flag for custom server
2021-12-18 01:41:50 +01:00
}
export class Smarts3 {
2021-12-20 17:06:42 +01:00
// STATIC
public static async createAndStart(
optionsArg: ConstructorParameters<typeof Smarts3>[0],
) {
2021-12-20 17:06:42 +01:00
const smartS3Instance = new Smarts3(optionsArg);
await smartS3Instance.start();
return smartS3Instance;
}
// INSTANCE
2021-12-18 01:41:50 +01:00
public options: ISmarts3ContructorOptions;
public s3Instance: plugins.s3rver | Smarts3Server;
2021-12-18 01:41:50 +01:00
constructor(optionsArg: ISmarts3ContructorOptions) {
2021-12-18 02:17:42 +01:00
this.options = {
useCustomServer: true, // Default to custom server
2022-04-14 10:38:14 +02:00
...optionsArg,
};
2021-12-18 01:41:50 +01:00
}
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)');
2021-12-18 01:41:50 +01:00
} 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)');
2021-12-18 01:41:50 +01:00
}
}
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
2021-12-20 16:33:13 +01:00
return {
...{
accessKey: 'S3RVER',
accessSecret: 'S3RVER',
endpoint: '127.0.0.1',
port: this.options.port || 3000,
useSsl: false,
},
...(optionsArg ? optionsArg : {}),
2022-04-14 10:38:14 +02:00
};
2021-12-20 16:33:13 +01:00
}
2021-12-20 19:36:54 +01:00
public async createBucket(bucketNameArg: string) {
const smartbucketInstance = new plugins.smartbucket.SmartBucket(
await this.getS3Descriptor(),
);
2021-12-20 19:36:54 +01:00
const bucket = await smartbucketInstance.createBucket(bucketNameArg);
return bucket;
}
2021-12-18 01:41:50 +01:00
public async stop() {
if (this.s3Instance instanceof Smarts3Server) {
await this.s3Instance.stop();
} else {
await (this.s3Instance as plugins.s3rver).close();
}
2021-12-18 01:41:50 +01:00
}
2022-04-14 10:38:14 +02:00
}
// Export the custom server class for direct use
export { Smarts3Server } from './classes/smarts3-server.js';