@basaltkit/storage-gcs 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Machize Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @basaltkit/storage-gcs
2
+
3
+ **Google Cloud Storage** driver for [`@basaltkit/storage`](https://www.npmjs.com/package/@basaltkit/storage): stores files on GCS without changing your app's code. You need this module when you run on Google Cloud and want GCS instead of S3 or local disk.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @basaltkit/storage-gcs @google-cloud/storage
9
+ ```
10
+
11
+ `@google-cloud/storage` is a **peer dependency**. Credentials follow GCP's standard chain (ADC, `keyFilename`, service account).
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { storagePlugin } from '@basaltkit/storage'
17
+ import { GcsStorageDriver } from '@basaltkit/storage-gcs'
18
+
19
+ storagePlugin({
20
+ disks: { uploads: { driver: new GcsStorageDriver({ bucket: 'my-bucket', projectId: 'my-project' }) } },
21
+ })
22
+ ```
23
+
24
+ Implements the `StorageDriver` contract — `put`, `get`, `exists`, `delete`, `list`, and **signed URLs** (`temporaryUrl`). Like all Basalt disks, per-tenant isolation is automatic via `Disk`.
25
+
26
+ ## Testable without the cloud
27
+
28
+ The client (bucket) is **injectable**, so the driver's logic can be tested with a fake — no GCS required:
29
+
30
+ ```ts
31
+ new GcsStorageDriver({ bucket: 'b', client: fakeBucket })
32
+ ```
33
+
34
+ ## How it connects to other modules
35
+
36
+ - **`@basaltkit/storage`** — this is a driver for that package; the API (`Disk`, `storagePlugin`) comes from there.
37
+ - Sibling drivers: `S3StorageDriver` (in core) and [`@basaltkit/storage-azure`](https://www.npmjs.com/package/@basaltkit/storage-azure).
@@ -0,0 +1,52 @@
1
+ import { StorageDriver, PutOptions } from '@basaltkit/storage';
2
+
3
+ /** The subset of a `@google-cloud/storage` File this driver uses. */
4
+ interface GcsFileLike {
5
+ save(data: Buffer, options?: {
6
+ contentType?: string;
7
+ }): Promise<unknown>;
8
+ download(): Promise<[Buffer]>;
9
+ exists(): Promise<[boolean]>;
10
+ delete(): Promise<unknown>;
11
+ getSignedUrl(config: {
12
+ action: 'read';
13
+ expires: number;
14
+ }): Promise<[string]>;
15
+ }
16
+ /** The subset of a `@google-cloud/storage` Bucket this driver uses. */
17
+ interface GcsBucketLike {
18
+ file(path: string): GcsFileLike;
19
+ getFiles(options?: {
20
+ prefix?: string;
21
+ }): Promise<[{
22
+ name: string;
23
+ }[]]>;
24
+ }
25
+ interface GcsDriverOptions {
26
+ bucket: string;
27
+ projectId?: string;
28
+ keyFilename?: string;
29
+ /** Injectable bucket — defaults to `@google-cloud/storage`. Tests pass a fake. */
30
+ client?: GcsBucketLike;
31
+ }
32
+ /**
33
+ * Google Cloud Storage driver for `@basaltkit/storage`. Uses
34
+ * `@google-cloud/storage` (an optional peer dependency) via an injectable
35
+ * bucket, so its logic is unit-tested without touching GCS.
36
+ */
37
+ declare class GcsStorageDriver implements StorageDriver {
38
+ private readonly options;
39
+ readonly name = "gcs";
40
+ private bucketPromise;
41
+ constructor(options: GcsDriverOptions);
42
+ put(path: string, content: Buffer | string, options?: PutOptions): Promise<void>;
43
+ get(path: string): Promise<Buffer>;
44
+ exists(path: string): Promise<boolean>;
45
+ delete(path: string): Promise<boolean>;
46
+ list(prefix: string): Promise<string[]>;
47
+ temporaryUrl(path: string, expiresInMs: number): Promise<string>;
48
+ disconnect(): Promise<void>;
49
+ private bucket;
50
+ }
51
+
52
+ export { type GcsBucketLike, type GcsDriverOptions, type GcsFileLike, GcsStorageDriver };
package/dist/index.js ADDED
@@ -0,0 +1,60 @@
1
+ // src/index.ts
2
+ import { StorageFileNotFoundError } from "@basaltkit/storage";
3
+ var isNotFound = (error) => error?.code === 404;
4
+ var GcsStorageDriver = class {
5
+ constructor(options) {
6
+ this.options = options;
7
+ }
8
+ options;
9
+ name = "gcs";
10
+ bucketPromise;
11
+ async put(path, content, options) {
12
+ const data = Buffer.isBuffer(content) ? content : Buffer.from(content);
13
+ await (await this.bucket()).file(path).save(data, options?.contentType !== void 0 ? { contentType: options.contentType } : {});
14
+ }
15
+ async get(path) {
16
+ try {
17
+ const [buffer] = await (await this.bucket()).file(path).download();
18
+ return buffer;
19
+ } catch (error) {
20
+ if (isNotFound(error)) throw new StorageFileNotFoundError(path);
21
+ throw error;
22
+ }
23
+ }
24
+ async exists(path) {
25
+ const [exists] = await (await this.bucket()).file(path).exists();
26
+ return exists;
27
+ }
28
+ async delete(path) {
29
+ if (!await this.exists(path)) return false;
30
+ await (await this.bucket()).file(path).delete();
31
+ return true;
32
+ }
33
+ async list(prefix) {
34
+ const [files] = await (await this.bucket()).getFiles({ prefix });
35
+ return files.map((file) => file.name);
36
+ }
37
+ async temporaryUrl(path, expiresInMs) {
38
+ const [url] = await (await this.bucket()).file(path).getSignedUrl({ action: "read", expires: Date.now() + expiresInMs });
39
+ return url;
40
+ }
41
+ async disconnect() {
42
+ }
43
+ bucket() {
44
+ if (!this.bucketPromise) {
45
+ this.bucketPromise = this.options.client ? Promise.resolve(this.options.client) : (async () => {
46
+ const specifier = "@google-cloud/storage";
47
+ const mod = await import(specifier);
48
+ const storage = new mod.Storage({
49
+ ...this.options.projectId ? { projectId: this.options.projectId } : {},
50
+ ...this.options.keyFilename ? { keyFilename: this.options.keyFilename } : {}
51
+ });
52
+ return storage.bucket(this.options.bucket);
53
+ })();
54
+ }
55
+ return this.bucketPromise;
56
+ }
57
+ };
58
+ export {
59
+ GcsStorageDriver
60
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@basaltkit/storage-gcs",
3
+ "version": "1.0.0",
4
+ "description": "Google Cloud Storage driver for @basaltkit/storage — put/get/list/delete/signed URLs over @google-cloud/storage, with an injectable client for testing.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "dependencies": {
17
+ "@basaltkit/storage": "^1.0.0"
18
+ },
19
+ "peerDependencies": {
20
+ "@google-cloud/storage": "^7.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^22.15.0",
24
+ "tsup": "^8.4.0",
25
+ "typescript": "^5.8.0",
26
+ "vitest": "^3.1.0",
27
+ "@basaltkit/tsconfig": "^0.24.0"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/Zebedeu/basalt.git",
35
+ "directory": "packages/storage-gcs"
36
+ },
37
+ "homepage": "https://github.com/Zebedeu/basalt/tree/main/packages/storage-gcs#readme",
38
+ "bugs": "https://github.com/Zebedeu/basalt/issues",
39
+ "keywords": [
40
+ "basalt",
41
+ "typescript",
42
+ "storage",
43
+ "gcs",
44
+ "google-cloud"
45
+ ],
46
+ "scripts": {
47
+ "build": "tsup src/index.ts --format esm --dts --clean",
48
+ "test": "vitest run",
49
+ "typecheck": "tsc --noEmit"
50
+ }
51
+ }