smarts3/ts/index.ts

65 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

2022-04-14 08:36:24 +00:00
import * as plugins from './smarts3.plugins.js';
import * as paths from './paths.js';
2021-12-18 00:41:50 +00:00
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,
2022-04-14 08:38:14 +00:00
...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,
2022-04-14 08:38:14 +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,
2022-04-14 08:38:14 +00:00
};
2021-12-20 15:33:13 +00:00
}
2021-12-20 18:36:54 +00:00
public async createBucket(bucketNameArg: string) {
const smartbucketInstance = new plugins.smartbucket.SmartBucket(await this.getS3Descriptor());
const bucket = await smartbucketInstance.createBucket(bucketNameArg);
return bucket;
}
2021-12-18 00:41:50 +00:00
public async stop() {
await this.s3Instance.close();
}
2022-04-14 08:38:14 +00:00
}