UNPKG

flydrive

Version:

File storage library with unified API to manage files across multiple cloud storage providers like S3, GCS, R2 and so on

192 lines (191 loc) 6.45 kB
import { a as E_CANNOT_DELETE_DIRECTORY, c as E_CANNOT_SET_VISIBILITY, i as E_CANNOT_COPY_FILE, l as E_CANNOT_WRITE_FILE, n as DriveFile, o as E_CANNOT_DELETE_FILE, r as KeyNormalizer, s as E_CANNOT_MOVE_FILE, t as DriveDirectory, u as errors_exports } from "./drive_directory-Av1MhRnI.js"; import { t as FSDriver } from "./driver-BxVDERAv.js"; import { unlink } from "node:fs/promises"; import { createReadStream } from "node:fs"; import { RuntimeException } from "@poppinss/utils/exception"; import { join } from "node:path"; import { debuglog } from "node:util"; import { AssertionError } from "node:assert"; var Disk = class { #normalizer = new KeyNormalizer(); constructor(driver) { this.driver = driver; } file(key) { return new DriveFile(key, this.driver); } fromSnapshot(snapshot) { return new DriveFile(snapshot.key, this.driver, { contentLength: snapshot.contentLength, etag: snapshot.etag, lastModified: new Date(snapshot.lastModified), contentType: snapshot.contentType }); } exists(key) { return this.file(key).exists(); } get(key) { return this.file(key).get(); } getStream(key) { return this.file(key).getStream(); } getBytes(key) { return this.file(key).getBytes(); } getArrayBuffer(key) { return this.file(key).getArrayBuffer(); } getMetaData(key) { return this.file(key).getMetaData(); } getVisibility(key) { return this.file(key).getVisibility(); } getUrl(key) { return this.file(key).getUrl(); } getSignedUrl(key, options) { return this.file(key).getSignedUrl(options); } getSignedUploadUrl(key, options) { return this.file(key).getSignedUploadUrl(options); } async setVisibility(key, visibility) { key = this.#normalizer.normalize(key); try { return await this.driver.setVisibility(key, visibility); } catch (error) { throw new E_CANNOT_SET_VISIBILITY([key], { cause: error }); } } async put(key, contents, options) { key = this.#normalizer.normalize(key); try { return await this.driver.put(key, contents, options); } catch (error) { throw new E_CANNOT_WRITE_FILE([key], { cause: error }); } } async putStream(key, contents, options) { key = this.#normalizer.normalize(key); try { return await this.driver.putStream(key, contents, options); } catch (error) { throw new E_CANNOT_WRITE_FILE([key], { cause: error }); } } async copy(source, destination, options) { source = this.#normalizer.normalize(source); destination = this.#normalizer.normalize(destination); try { return await this.driver.copy(source, destination, options); } catch (error) { throw new E_CANNOT_COPY_FILE([source, destination], { cause: error }); } } copyFromFs(source, destination, options) { return this.putStream(destination, createReadStream(source), options); } async move(source, destination, options) { source = this.#normalizer.normalize(source); destination = this.#normalizer.normalize(destination); try { return await this.driver.move(source, destination, options); } catch (error) { throw new E_CANNOT_MOVE_FILE([source, destination], { cause: error }); } } async moveFromFs(source, destination, options) { await this.putStream(destination, createReadStream(source), options); await unlink(source); } async delete(key) { key = this.#normalizer.normalize(key); try { return await this.driver.delete(key); } catch (error) { throw new E_CANNOT_DELETE_FILE([key], { cause: error }); } } async deleteAll(prefix) { prefix = prefix && prefix !== "/" ? this.#normalizer.normalize(prefix) : "/"; try { return await this.driver.deleteAll(prefix); } catch (error) { throw new E_CANNOT_DELETE_DIRECTORY([prefix], { cause: error }); } } listAll(prefix, options) { prefix = prefix && prefix !== "/" ? this.#normalizer.normalize(prefix) : "/"; return this.driver.listAll(prefix, options); } }; var debug_default = debuglog("flydrive:core"); var FakeDisk = class extends Disk { constructor(disk, fakesConfig) { super(new FSDriver({ location: typeof fakesConfig.location === "string" ? join(fakesConfig.location, disk) : new URL(disk, fakesConfig.location), visibility: "public", urlBuilder: fakesConfig.urlBuilder })); this.disk = disk; } assertExists(paths) { const pathsToVerify = Array.isArray(paths) ? paths : [paths]; for (let filePath of pathsToVerify) if (!this.driver.existsSync(filePath)) throw new AssertionError({ message: `Expected "${filePath}" to exist, but file not found.` }); } assertMissing(paths) { const pathsToVerify = Array.isArray(paths) ? paths : [paths]; for (let filePath of pathsToVerify) if (this.driver.existsSync(filePath)) throw new AssertionError({ message: `Expected "${filePath}" to be missing, but file exists` }); } clear() { this.driver.clearSync(); } }; var DriveManager = class { #config; #cachedServices = /* @__PURE__ */ new Map(); #fakes = /* @__PURE__ */ new Map(); constructor(config) { this.#config = config; debug_default("driver manager config %O", config); } use(service) { const serviceToUse = service || this.#config.default; const fake = this.#fakes.get(serviceToUse); if (fake) { debug_default("returning fake for service %s", serviceToUse); return fake; } const cachedDisk = this.#cachedServices.get(serviceToUse); if (cachedDisk) { debug_default("use cached disk instance for service %s", serviceToUse); return cachedDisk; } const disk = new Disk(this.#config.services[serviceToUse]()); debug_default("creating disk instance for service %s", serviceToUse); this.#cachedServices.set(serviceToUse, disk); return disk; } fake(service) { const serviceToUse = service || this.#config.default; if (!this.#config.fakes) throw new RuntimeException("Cannot use \"drive.fake\". Make sure to define fakes configuration when creating DriveManager instance"); this.restore(serviceToUse); debug_default("creating fake for service %s", serviceToUse); const fake = new FakeDisk(serviceToUse, this.#config.fakes); this.#fakes.set(serviceToUse, fake); return fake; } restore(service) { const serviceToUse = service || this.#config.default; const fake = this.#fakes.get(serviceToUse); if (fake) { debug_default("restoring fake for service %s", serviceToUse); fake.clear(); this.#fakes.delete(serviceToUse); } } }; export { Disk, DriveDirectory, DriveFile, DriveManager, KeyNormalizer, errors_exports as errors };