Files
smarts3/ts/index.ts

65 lines
1.7 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;
}
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: Smarts3Server;
2021-12-18 01:41:50 +01:00
constructor(optionsArg: ISmarts3ContructorOptions) {
this.options = optionsArg;
2021-12-18 01:41:50 +01:00
}
public async start() {
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');
2021-12-18 01:41:50 +01:00
}
public async getS3Descriptor(
optionsArg?: Partial<plugins.tsclass.storage.IS3Descriptor>,
): Promise<plugins.tsclass.storage.IS3Descriptor> {
const descriptor = this.s3Instance.getS3Descriptor();
2021-12-20 16:33:13 +01:00
return {
...descriptor,
...(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() {
await this.s3Instance.stop();
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';