diff --git a/changelog.md b/changelog.md index 23ca537..2cc4d38 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2024-11-24 - 3.2.2 - fix(core) +Refactor Bucket class for improved error handling + +- Ensured safe access using non-null assertions when finding a bucket. +- Enhanced fastPut method by adding fastPutStrict for safer operations. +- Added explicit error handling and type checking in fastExists method. + ## 2024-11-24 - 3.2.1 - fix(metadata) Fix metadata handling for deleted files diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 9e6bfd0..aaaeaa3 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbucket', - version: '3.2.1', + version: '3.2.2', description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.' } diff --git a/ts/classes.bucket.ts b/ts/classes.bucket.ts index 97f30cd..2c0e4cf 100644 --- a/ts/classes.bucket.ts +++ b/ts/classes.bucket.ts @@ -17,7 +17,7 @@ export class Bucket { public static async getBucketByName(smartbucketRef: SmartBucket, bucketNameArg: string) { const command = new plugins.s3.ListBucketsCommand({}); const buckets = await smartbucketRef.s3Client.send(command); - const foundBucket = buckets.Buckets.find((bucket) => bucket.Name === bucketNameArg); + const foundBucket = buckets.Buckets!.find((bucket) => bucket.Name === bucketNameArg); if (foundBucket) { console.log(`bucket with name ${bucketNameArg} exists.`); @@ -88,14 +88,15 @@ export class Bucket { contents: string | Buffer; overwrite?: boolean; } - ): Promise { + ): Promise { try { const reducedPath = await helpers.reducePathDescriptorToPath(optionsArg); const exists = await this.fastExists({ path: reducedPath }); if (exists && !optionsArg.overwrite) { - console.error(`Object already exists at path '${reducedPath}' in bucket '${this.name}'.`); - return; + const errorText = `Object already exists at path '${reducedPath}' in bucket '${this.name}'.`; + console.error(errorText); + return null; } else if (exists && optionsArg.overwrite) { console.log( `Overwriting existing object at path '${reducedPath}' in bucket '${this.name}'.` @@ -128,6 +129,14 @@ export class Bucket { } } + public async fastPutStrict(...args: Parameters) { + const file = await this.fastPut(...args); + if (!file) { + throw new Error(`File not stored at path '${args[0].path}'`); + } + return file; + } + /** * get file */ @@ -152,7 +161,7 @@ export class Bucket { }, }); await done.promise; - return completeFile; + return completeFile!; } /** @@ -220,7 +229,7 @@ export class Bucket { return chunk; }, finalFunction: async (cb) => { - return null; + return null!; }, }); @@ -392,8 +401,8 @@ export class Bucket { await this.smartbucketRef.s3Client.send(command); console.log(`Object '${optionsArg.path}' exists in bucket '${this.name}'.`); return true; - } catch (error) { - if (error.name === 'NotFound') { + } catch (error: any) { + if (error?.name === 'NotFound') { console.log(`Object '${optionsArg.path}' does not exist in bucket '${this.name}'.`); return false; } else {