@basaltkit/storage-gcs 1.0.0 → 1.1.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 +1 -1
- package/README.md +6 -0
- package/dist/index.d.ts +7 -9
- package/dist/index.js +72 -59
- package/package.json +9 -10
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://basaltkit-docs.pages.dev">
|
|
3
|
+
<img src="https://basaltkit-docs.pages.dev/social-card.png" alt="Basalt" width="440">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
1
7
|
# @basaltkit/storage-gcs
|
|
2
8
|
|
|
3
9
|
**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.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { type TemporaryUrlOptions, type PutOptions, type StorageDriver } from '@basaltkit/storage';
|
|
3
2
|
/** The subset of a `@google-cloud/storage` File this driver uses. */
|
|
4
|
-
interface GcsFileLike {
|
|
3
|
+
export interface GcsFileLike {
|
|
5
4
|
save(data: Buffer, options?: {
|
|
6
5
|
contentType?: string;
|
|
7
6
|
}): Promise<unknown>;
|
|
@@ -11,10 +10,11 @@ interface GcsFileLike {
|
|
|
11
10
|
getSignedUrl(config: {
|
|
12
11
|
action: 'read';
|
|
13
12
|
expires: number;
|
|
13
|
+
responseDisposition?: string;
|
|
14
14
|
}): Promise<[string]>;
|
|
15
15
|
}
|
|
16
16
|
/** The subset of a `@google-cloud/storage` Bucket this driver uses. */
|
|
17
|
-
interface GcsBucketLike {
|
|
17
|
+
export interface GcsBucketLike {
|
|
18
18
|
file(path: string): GcsFileLike;
|
|
19
19
|
getFiles(options?: {
|
|
20
20
|
prefix?: string;
|
|
@@ -22,7 +22,7 @@ interface GcsBucketLike {
|
|
|
22
22
|
name: string;
|
|
23
23
|
}[]]>;
|
|
24
24
|
}
|
|
25
|
-
interface GcsDriverOptions {
|
|
25
|
+
export interface GcsDriverOptions {
|
|
26
26
|
bucket: string;
|
|
27
27
|
projectId?: string;
|
|
28
28
|
keyFilename?: string;
|
|
@@ -34,7 +34,7 @@ interface GcsDriverOptions {
|
|
|
34
34
|
* `@google-cloud/storage` (an optional peer dependency) via an injectable
|
|
35
35
|
* bucket, so its logic is unit-tested without touching GCS.
|
|
36
36
|
*/
|
|
37
|
-
declare class GcsStorageDriver implements StorageDriver {
|
|
37
|
+
export declare class GcsStorageDriver implements StorageDriver {
|
|
38
38
|
private readonly options;
|
|
39
39
|
readonly name = "gcs";
|
|
40
40
|
private bucketPromise;
|
|
@@ -44,9 +44,7 @@ declare class GcsStorageDriver implements StorageDriver {
|
|
|
44
44
|
exists(path: string): Promise<boolean>;
|
|
45
45
|
delete(path: string): Promise<boolean>;
|
|
46
46
|
list(prefix: string): Promise<string[]>;
|
|
47
|
-
temporaryUrl(path: string, expiresInMs: number): Promise<string>;
|
|
47
|
+
temporaryUrl(path: string, expiresInMs: number, options?: TemporaryUrlOptions): Promise<string>;
|
|
48
48
|
disconnect(): Promise<void>;
|
|
49
49
|
private bucket;
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
export { type GcsBucketLike, type GcsDriverOptions, type GcsFileLike, GcsStorageDriver };
|
package/dist/index.js
CHANGED
|
@@ -1,60 +1,73 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
import { StorageFileNotFoundError } from '@basaltkit/storage';
|
|
2
|
+
const isNotFound = (error) => error?.code === 404;
|
|
3
|
+
/**
|
|
4
|
+
* Google Cloud Storage driver for `@basaltkit/storage`. Uses
|
|
5
|
+
* `@google-cloud/storage` (an optional peer dependency) via an injectable
|
|
6
|
+
* bucket, so its logic is unit-tested without touching GCS.
|
|
7
|
+
*/
|
|
8
|
+
export class GcsStorageDriver {
|
|
9
|
+
options;
|
|
10
|
+
name = 'gcs';
|
|
11
|
+
bucketPromise;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
}
|
|
15
|
+
async put(path, content, options) {
|
|
16
|
+
const data = Buffer.isBuffer(content) ? content : Buffer.from(content);
|
|
17
|
+
await (await this.bucket())
|
|
18
|
+
.file(path)
|
|
19
|
+
.save(data, options?.contentType !== undefined ? { contentType: options.contentType } : {});
|
|
20
|
+
}
|
|
21
|
+
async get(path) {
|
|
22
|
+
try {
|
|
23
|
+
const [buffer] = await (await this.bucket()).file(path).download();
|
|
24
|
+
return buffer;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (isNotFound(error))
|
|
28
|
+
throw new StorageFileNotFoundError(path);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async exists(path) {
|
|
33
|
+
const [exists] = await (await this.bucket()).file(path).exists();
|
|
34
|
+
return exists;
|
|
35
|
+
}
|
|
36
|
+
async delete(path) {
|
|
37
|
+
if (!(await this.exists(path)))
|
|
38
|
+
return false;
|
|
39
|
+
await (await this.bucket()).file(path).delete();
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
async list(prefix) {
|
|
43
|
+
const [files] = await (await this.bucket()).getFiles({ prefix });
|
|
44
|
+
return files.map((file) => file.name);
|
|
45
|
+
}
|
|
46
|
+
async temporaryUrl(path, expiresInMs, options) {
|
|
47
|
+
const [url] = await (await this.bucket()).file(path).getSignedUrl({
|
|
48
|
+
action: 'read',
|
|
49
|
+
expires: Date.now() + expiresInMs,
|
|
50
|
+
// Signed response header: uploaded HTML/SVG downloads instead of
|
|
51
|
+
// rendering on the storage origin ('attachment' is the Disk default).
|
|
52
|
+
responseDisposition: options?.disposition ?? 'attachment',
|
|
51
53
|
});
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
return url;
|
|
55
|
+
}
|
|
56
|
+
async disconnect() { }
|
|
57
|
+
bucket() {
|
|
58
|
+
if (!this.bucketPromise) {
|
|
59
|
+
this.bucketPromise = this.options.client
|
|
60
|
+
? Promise.resolve(this.options.client)
|
|
61
|
+
: (async () => {
|
|
62
|
+
const specifier = '@google-cloud/storage';
|
|
63
|
+
const mod = (await import(specifier));
|
|
64
|
+
const storage = new mod.Storage({
|
|
65
|
+
...(this.options.projectId ? { projectId: this.options.projectId } : {}),
|
|
66
|
+
...(this.options.keyFilename ? { keyFilename: this.options.keyFilename } : {}),
|
|
67
|
+
});
|
|
68
|
+
return storage.bucket(this.options.bucket);
|
|
69
|
+
})();
|
|
70
|
+
}
|
|
71
|
+
return this.bucketPromise;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/storage-gcs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
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
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,16 +14,15 @@
|
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@basaltkit/storage": "^1.
|
|
17
|
+
"@basaltkit/storage": "^1.3.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@google-cloud/storage": "^7.0.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@types/node": "^
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"vitest": "^3.1.0",
|
|
23
|
+
"@types/node": "^26.3.0",
|
|
24
|
+
"typescript": "^7.0.2",
|
|
25
|
+
"vitest": "^4.1.11",
|
|
27
26
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
28
27
|
},
|
|
29
28
|
"publishConfig": {
|
|
@@ -31,11 +30,11 @@
|
|
|
31
30
|
},
|
|
32
31
|
"repository": {
|
|
33
32
|
"type": "git",
|
|
34
|
-
"url": "git+https://github.com/
|
|
33
|
+
"url": "git+https://github.com/basaltkit/basalt.git",
|
|
35
34
|
"directory": "packages/storage-gcs"
|
|
36
35
|
},
|
|
37
|
-
"homepage": "https://github.com/
|
|
38
|
-
"bugs": "https://github.com/
|
|
36
|
+
"homepage": "https://github.com/basaltkit/basalt/tree/main/packages/storage-gcs#readme",
|
|
37
|
+
"bugs": "https://github.com/basaltkit/basalt/issues",
|
|
39
38
|
"keywords": [
|
|
40
39
|
"basalt",
|
|
41
40
|
"typescript",
|
|
@@ -44,7 +43,7 @@
|
|
|
44
43
|
"google-cloud"
|
|
45
44
|
],
|
|
46
45
|
"scripts": {
|
|
47
|
-
"build": "
|
|
46
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
47
|
"test": "vitest run",
|
|
49
48
|
"typecheck": "tsc --noEmit"
|
|
50
49
|
}
|