smarts3/ts/index.ts

58 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2021-12-18 00:41:50 +00:00
import * as plugins from './smarts3.plugins';
import * as paths from './paths';
export interface ISmarts3ContructorOptions {
port?: number;
cleanSlate?: boolean;
}
export class Smarts3 {
2021-12-20 16:06:42 +00:00
// STATIC
public static async createAndStart(optionsArg: ConstructorParameters<typeof Smarts3>[0]) {
const smartS3Instance = new Smarts3(optionsArg);
await smartS3Instance.start();
return smartS3Instance;
}
// INSTANCE
2021-12-18 00:41:50 +00:00
public options: ISmarts3ContructorOptions;
public s3Instance: plugins.s3rver;
constructor(optionsArg: ISmarts3ContructorOptions) {
this.options = optionsArg;
2021-12-18 01:17:42 +00:00
this.options = {
...this.options,
...optionsArg
}
2021-12-18 00:41:50 +00:00
}
public async start() {
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,
2021-12-18 00:48:03 +00:00
directory: paths.bucketsDir
2021-12-18 00:41:50 +00:00
})
await this.s3Instance.run();
console.log('s3 server is running');
}
2021-12-20 15:38:51 +00:00
public async getS3Descriptor(): Promise<plugins.smartbucket.ISmartBucketConfig> {
2021-12-20 15:33:13 +00:00
return {
2021-12-20 17:25:55 +00:00
accessKey: 'S3RVER',
accessSecret: 'S3RVER',
2021-12-20 15:33:13 +00:00
endpoint: 'localhost',
2021-12-20 17:25:55 +00:00
port: this.options.port,
useSsl: false,
2021-12-20 15:33:13 +00:00
}
}
2021-12-18 00:41:50 +00:00
public async stop() {
await this.s3Instance.close();
}
}