smarts3/ts/index.ts

65 lines
1.7 KiB
TypeScript

import * as plugins from './smarts3.plugins.js';
import * as paths from './paths.js';
export interface ISmarts3ContructorOptions {
port?: number;
cleanSlate?: boolean;
}
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;
constructor(optionsArg: ISmarts3ContructorOptions) {
this.options = optionsArg;
this.options = {
...this.options,
...optionsArg,
};
}
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,
directory: paths.bucketsDir,
});
await this.s3Instance.run();
console.log('s3 server is running');
}
public async getS3Descriptor(): Promise<plugins.smartbucket.ISmartBucketConfig> {
return {
accessKey: 'S3RVER',
accessSecret: 'S3RVER',
endpoint: 'localhost',
port: this.options.port,
useSsl: false,
};
}
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() {
await this.s3Instance.close();
}
}