Compare commits

...

4 Commits

Author SHA1 Message Date
645ebbdd4d 3.0.6 2024-05-21 18:47:00 +02:00
168148b2c9 fix(core): update 2024-05-21 18:46:59 +02:00
1293fc4ca6 3.0.5 2024-05-21 18:42:55 +02:00
b040120813 fix(core): update 2024-05-21 18:42:55 +02:00
7 changed files with 88 additions and 56 deletions

View File

@ -8,7 +8,7 @@
"githost": "code.foss.global", "githost": "code.foss.global",
"gitscope": "push.rocks", "gitscope": "push.rocks",
"gitrepo": "smartbucket", "gitrepo": "smartbucket",
"description": "A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.", "description": "A TypeScript library for cloud-independent object storage, providing features like bucket creation, file and directory management, and data streaming.",
"npmPackagename": "@push.rocks/smartbucket", "npmPackagename": "@push.rocks/smartbucket",
"license": "MIT", "license": "MIT",
"keywords": [ "keywords": [
@ -26,7 +26,12 @@
"unified storage", "unified storage",
"buffer handling", "buffer handling",
"access key", "access key",
"secret key" "secret key",
"metadata",
"file locking",
"file streaming",
"directory listing",
"cloud agnostic"
] ]
} }
}, },

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@push.rocks/smartbucket", "name": "@push.rocks/smartbucket",
"version": "3.0.4", "version": "3.0.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@push.rocks/smartbucket", "name": "@push.rocks/smartbucket",
"version": "3.0.4", "version": "3.0.6",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"@push.rocks/smartpath": "^5.0.18", "@push.rocks/smartpath": "^5.0.18",

View File

@ -1,7 +1,7 @@
{ {
"name": "@push.rocks/smartbucket", "name": "@push.rocks/smartbucket",
"version": "3.0.4", "version": "3.0.6",
"description": "A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.", "description": "A TypeScript library for cloud-independent object storage, providing features like bucket creation, file and directory management, and data streaming.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts", "typings": "dist_ts/index.d.ts",
"type": "module", "type": "module",
@ -58,6 +58,11 @@
"unified storage", "unified storage",
"buffer handling", "buffer handling",
"access key", "access key",
"secret key" "secret key",
"metadata",
"file locking",
"file streaming",
"directory listing",
"cloud agnostic"
] ]
} }

View File

@ -1,5 +1,6 @@
# @push.rocks/smartbucket # @push.rocks/smartbucket
A TypeScript library for simple cloud independent object storage with support for buckets, directories, and files.
A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.
## Install ## Install
@ -199,12 +200,15 @@ writeFileStream("exampleBucket", "path/to/streamedObject.txt", readable);
`@push.rocks/smartbucket` abstracts directories within buckets for easier object management. You can create, list, and delete directories using the `Directory` class. `@push.rocks/smartbucket` abstracts directories within buckets for easier object management. You can create, list, and delete directories using the `Directory` class.
Here's how to list the contents of a directory:
```typescript ```typescript
async function listDirectoryContents(bucketName: string, directoryPath: string) { async function listDirectoryContents(bucketName: string, directoryPath: string) {
const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName); const myBucket: Bucket = await mySmartBucket.getBucketByName(bucketName);
if (myBucket) { if (myBucket) {
const baseDirectory: Directory = await myBucket.getBaseDirectory(); const baseDirectory: Directory = await myBucket.getBaseDirectory();
const targetDirectory: Directory = await baseDirectory.getSubDirectoryByName(directoryPath); const targetDirectory: Directory = await baseDirectory.getSubDirectoryByName(directoryPath);
console.log('Listing directories:'); console.log('Listing directories:');
const directories = await targetDirectory.listDirectories(); const directories = await targetDirectory.listDirectories();
directories.forEach(dir => { directories.forEach(dir => {
@ -304,6 +308,8 @@ Remember, each cloud provider has specific features and limitations. `@push.rock
This guide covers the basic to advanced scenarios of using `@push.rocks/smartbucket`. For further details, refer to the API documentation and examples. This guide covers the basic to advanced scenarios of using `@push.rocks/smartbucket`. For further details, refer to the API documentation and examples.
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartbucket', name: '@push.rocks/smartbucket',
version: '3.0.4', version: '3.0.6',
description: 'A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.' description: 'A TypeScript library for cloud-independent object storage, providing features like bucket creation, file and directory management, and data streaming.'
} }

View File

@ -102,7 +102,7 @@ export class File {
const metadata = await this.getMetaData(); const metadata = await this.getMetaData();
await metadata.setLock({ await metadata.setLock({
lock: 'locked', lock: 'locked',
expires: new Date(Date.now() + (optionsArg?.timeoutMillis || 1000)), expires: Date.now() + (optionsArg?.timeoutMillis || 1000),
}); });
} }
@ -116,7 +116,10 @@ export class File {
*/ */
force?: boolean; force?: boolean;
}) { }) {
const metadata = await this.getMetaData();
await metadata.removeLock({
force: optionsArg?.force,
});
} }
public async updateWithContents(optionsArg: { public async updateWithContents(optionsArg: {
@ -146,9 +149,27 @@ export class File {
* @param updatedMetadata * @param updatedMetadata
*/ */
public async getMetaData() { public async getMetaData() {
if (this.name.endsWith('.metadata')) {
throw new Error('metadata files cannot have metadata');
}
const metadata = await MetaData.createForFile({ const metadata = await MetaData.createForFile({
file: this, file: this,
}); });
return metadata; return metadata;
} }
/**
* gets the contents as json
*/
public async getJsonData() {
const json = await this.getContentsAsString();
const parsed = await JSON.parse(json);
return parsed;
}
public async writeJsonData(dataArg: any) {
await this.updateWithContents({
contents: JSON.stringify(dataArg),
});
}
} }

View File

@ -4,9 +4,7 @@ import { File } from './classes.file.js';
export class MetaData { export class MetaData {
// static // static
public static async createForFile(optionsArg: { public static async createForFile(optionsArg: { file: File }) {
file: File;
}) {
const metaData = new MetaData(); const metaData = new MetaData();
metaData.fileRef = optionsArg.file; metaData.fileRef = optionsArg.file;
@ -29,16 +27,16 @@ export class MetaData {
* the file that the metadata is for * the file that the metadata is for
*/ */
fileRef: File; fileRef: File;
public async getFileType(optionsArg?: { public async getFileType(optionsArg?: {
useFileExtension?: boolean; useFileExtension?: boolean;
useMagicBytes?: boolean; useMagicBytes?: boolean;
}): Promise<string> { }): Promise<string> {
if (optionsArg && optionsArg.useFileExtension || optionsArg.useFileExtension === undefined) { if ((optionsArg && optionsArg.useFileExtension) || optionsArg.useFileExtension === undefined) {
return plugins.path.extname(this.fileRef.name); return plugins.path.extname(this.fileRef.name);
} }
}; }
/** /**
* gets the size of the fileRef * gets the size of the fileRef
*/ */
@ -47,59 +45,56 @@ export class MetaData {
path: this.fileRef.getBasePath(), path: this.fileRef.getBasePath(),
}); });
return stat.size; return stat.size;
}; }
private prefixCustomMetaData = 'custom_'; private prefixCustomMetaData = 'custom_';
public async storeCustomMetaData<T = any>(optionsArg: { public async storeCustomMetaData<T = any>(optionsArg: { key: string; value: T }) {
key: string; const data = await this.metadataFile.getContentsAsString();
value: T; data[this.prefixCustomMetaData + optionsArg.key] = optionsArg.value;
}) { await this.metadataFile.writeJsonData(data);
const json = await this.metadataFile.getContentsAsString();
const parsed = await JSON.parse(json);
parsed[this.prefixCustomMetaData + optionsArg.key] = optionsArg.value;
await this.metadataFile.updateWithContents({
contents: JSON.stringify(parsed),
});
} }
public async getCustomMetaData<T = any>(optionsArg: { public async getCustomMetaData<T = any>(optionsArg: { key: string }): Promise<T> {
key: string; const data = await this.metadataFile.getJsonData();
}): Promise<T> { return data[this.prefixCustomMetaData + optionsArg.key];
const json = await this.metadataFile.getContentsAsString();
const parsed = await JSON.parse(json);
return parsed[this.prefixCustomMetaData + optionsArg.key];
} }
public async deleteCustomMetaData(optionsArg: { public async deleteCustomMetaData(optionsArg: { key: string }) {
key: string; const data = await this.metadataFile.getJsonData();
}) { delete data[this.prefixCustomMetaData + optionsArg.key];
const json = await this.metadataFile.getContentsAsString(); await this.metadataFile.writeJsonData(data);
const parsed = await JSON.parse(json);
delete parsed[this.prefixCustomMetaData + optionsArg.key];
await this.metadataFile.updateWithContents({
contents: JSON.stringify(parsed),
});
} }
/** /**
* set a lock on the ref file * set a lock on the ref file
* @param optionsArg * @param optionsArg
*/ */
public async setLock(optionsArg: { public async setLock(optionsArg: { lock: string; expires: number }) {
lock: string; const data = await this.metadataFile.getJsonData();
expires: Date; data.lock = optionsArg.lock;
}) { data.lockExpires = optionsArg.expires;
await this.metadataFile.writeJsonData(data);
} }
/** /**
* remove the lock on the ref file * remove the lock on the ref file
* @param optionsArg * @param optionsArg
*/ */
public async removeLock(optionsArg: { public async removeLock(optionsArg: { force: boolean }) {
force: boolean; const data = await this.metadataFile.getJsonData();
}) { delete data.lock;
delete data.lockExpires;
await this.metadataFile.writeJsonData(data);
} }
}
public async checkLocked(): Promise<boolean> {
const data = await this.metadataFile.getJsonData();
return data.lock && data.lockExpires > Date.now();
}
public async getLockInfo(): Promise<{ lock: string; expires: number }> {
const data = await this.metadataFile.getJsonData();
return { lock: data.lock, expires: data.lockExpires };
}
}