UNPKG

@storybooker/gcp

Version:

StoryBooker Adapter for interacting with GCP services.

85 lines (83 loc) 3.4 kB
import { Readable } from "node:stream"; //#region src/storage.ts var GcpGcsStorageService = class { #client; constructor(client) { this.createContainer = async (containerId, _options) => { const bucketName = genBucketNameFromContainerId(containerId); await this.#client.createBucket(bucketName, {}); }; this.deleteContainer = async (containerId, _options) => { const bucketName = genBucketNameFromContainerId(containerId); await this.#client.bucket(bucketName).delete(); }; this.hasContainer = async (containerId, _options) => { const bucketName = genBucketNameFromContainerId(containerId); const [exists] = await this.#client.bucket(bucketName).exists(); return exists; }; this.listContainers = async (_options) => { const [buckets] = await this.#client.getBuckets(); return buckets.map((bucket) => bucket.name); }; this.deleteFiles = async (containerId, filePathsOrPrefix, _options) => { const bucketName = genBucketNameFromContainerId(containerId); const bucket = this.#client.bucket(bucketName); if (typeof filePathsOrPrefix === "string") await bucket.deleteFiles({ prefix: filePathsOrPrefix }); else await Promise.all(filePathsOrPrefix.map(async (filepath) => await bucket.file(filepath).delete({ ignoreNotFound: true }))); }; this.uploadFiles = async (containerId, files, _options) => { const bucketName = genBucketNameFromContainerId(containerId); const bucket = this.#client.bucket(bucketName); await Promise.allSettled(files.map(async ({ content, path, mimeType }) => { await uploadFileToGcs(bucket.file(path), content, mimeType); })); }; this.hasFile = async (containerId, filepath, _options) => { const bucketName = genBucketNameFromContainerId(containerId); const [exists] = await this.#client.bucket(bucketName).file(filepath).exists(); return exists; }; this.downloadFile = async (containerId, filepath, _options) => { const bucketName = genBucketNameFromContainerId(containerId); const file = this.#client.bucket(bucketName).file(filepath); const [exists] = await file.exists(); if (!exists) throw new Error(`File '${filepath}' not found in bucket '${containerId}'.`); const [metadata] = await file.getMetadata(); const mimeType = metadata.contentType; const readable = file.createReadStream(); return { content: Readable.toWeb(readable), mimeType, path: filepath }; }; this.#client = client; } }; function genBucketNameFromContainerId(containerId) { return containerId.replaceAll(/[^\w.-]+/g, "-").replaceAll(/^-+|-+$/g, "").toLowerCase().slice(0, 63); } async function uploadFileToGcs(file, data, mimeType) { if (typeof data === "string" || data instanceof Buffer) { await file.save(data, { contentType: mimeType }); return; } if (data instanceof Blob) { const buffer = Buffer.from(await data.arrayBuffer()); await file.save(buffer, { contentType: mimeType }); return; } const readable = data instanceof ReadableStream ? Readable.fromWeb(data) : data; if (readable instanceof Readable) { await new Promise((resolve, reject) => { const writeStream = file.createWriteStream({ contentType: mimeType }); readable.pipe(writeStream).on("finish", resolve).on("error", reject); }); return; } throw new Error(`Unknown file type`); } //#endregion export { GcpGcsStorageService }; //# sourceMappingURL=storage.js.map