@ducktors/storagebus-local
Advanced tools
Comparing version 0.10.0 to 0.11.0
/// <reference types="node" /> | ||
import { Readable } from 'node:stream'; | ||
import { AbstractStorageOptions, Storage as AbstractStorage } from '@ducktors/storagebus-abstract'; | ||
type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = { | ||
[Key in KeysType]: Required<Pick<ObjectType, Key>> & Partial<Record<Exclude<KeysType, Key>, never>>; | ||
}[KeysType] & Omit<ObjectType, KeysType>; | ||
export type StorageOptions = RequireExactlyOne<{ | ||
bucket: string; | ||
rootFolder: string; | ||
}, 'bucket' | 'rootFolder'> & AbstractStorageOptions; | ||
export declare class Storage extends AbstractStorage { | ||
constructor(opts?: AbstractStorageOptions); | ||
protected bucket: string; | ||
constructor(opts: StorageOptions); | ||
write(filePath: string, fileReadable: Readable): Promise<string>; | ||
@@ -12,4 +20,4 @@ exists(filePath: string): Promise<boolean>; | ||
move(filePath: string, destFilePath: string): Promise<string>; | ||
saveToTmpFolder(filePath: string, fileReadable: Readable, subFolder?: string): Promise<string>; | ||
getTmpFolder(subFolder?: string): Promise<string>; | ||
static getTmpSubFolder(subFolder?: string): Promise<string>; | ||
} | ||
export {}; |
@@ -10,12 +10,18 @@ "use strict"; | ||
const node_path_1 = require("node:path"); | ||
const node_stream_1 = require("node:stream"); | ||
const node_util_1 = require("node:util"); | ||
const promises_2 = require("node:stream/promises"); | ||
const storagebus_abstract_1 = require("@ducktors/storagebus-abstract"); | ||
const pipeline = (0, node_util_1.promisify)(node_stream_1.pipeline); | ||
class Storage extends storagebus_abstract_1.Storage { | ||
constructor(opts = {}) { | ||
constructor(opts) { | ||
super(opts); | ||
Object.defineProperty(this, "bucket", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
this.bucket = opts.bucket ?? opts.rootFolder; | ||
} | ||
async write(filePath, fileReadable) { | ||
await pipeline(fileReadable, (0, node_fs_1.createWriteStream)(filePath)); | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
await (0, promises_2.pipeline)(fileReadable, (0, node_fs_1.createWriteStream)(path)); | ||
return filePath; | ||
@@ -25,3 +31,4 @@ } | ||
try { | ||
await promises_1.default.access(filePath, node_fs_1.constants.F_OK); | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
await promises_1.default.access(path, node_fs_1.constants.F_OK); | ||
return true; | ||
@@ -37,14 +44,20 @@ } | ||
async read(filePath) { | ||
return (0, node_fs_1.createReadStream)(filePath); | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
return (0, node_fs_1.createReadStream)(path); | ||
} | ||
async remove(filePath) { | ||
return promises_1.default.unlink(filePath); | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
return promises_1.default.unlink(path); | ||
} | ||
async copy(filePath, destFilePath) { | ||
await promises_1.default.copyFile(filePath, destFilePath); | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
const destPath = (0, node_path_1.join)(this.bucket, destFilePath); | ||
await promises_1.default.copyFile(path, destPath); | ||
return destFilePath; | ||
} | ||
async move(filePath, destFilePath) { | ||
const path = (0, node_path_1.join)(this.bucket, filePath); | ||
const destPath = (0, node_path_1.join)(this.bucket, destFilePath); | ||
try { | ||
await promises_1.default.rename(filePath, destFilePath); | ||
await promises_1.default.rename(path, destPath); | ||
} | ||
@@ -62,9 +75,3 @@ catch (err) { | ||
} | ||
async saveToTmpFolder(filePath, fileReadable, subFolder) { | ||
const outDir = await this.getTmpFolder(subFolder); | ||
const tmpFilePath = (0, node_path_1.join)(outDir, filePath); | ||
await this.write(tmpFilePath, fileReadable); | ||
return tmpFilePath; | ||
} | ||
async getTmpFolder(subFolder) { | ||
static async getTmpSubFolder(subFolder) { | ||
if (!subFolder) { | ||
@@ -71,0 +78,0 @@ subFolder = (0, node_crypto_1.randomUUID)(); |
{ | ||
"name": "@ducktors/storagebus-local", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11789
111