@storecraft/storage-google 1.0.9 → 1.0.11
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/README.md +1 -8
- package/adapter.js +31 -29
- package/package.json +1 -1
- package/types.public.d.ts +8 -8
package/README.md
CHANGED
@@ -71,14 +71,7 @@ const app = new App(
|
|
71
71
|
.withPlatform(new NodePlatform())
|
72
72
|
.withDatabase(new MongoDB())
|
73
73
|
.withStorage(
|
74
|
-
new GoogleStorage(
|
75
|
-
{
|
76
|
-
bucket: process.env.GS_BUCKET,
|
77
|
-
client_email: process.env.GS_CLIENT_EMAIL,
|
78
|
-
private_key: process.env.GS_PRIVATE_KEY,
|
79
|
-
private_key_id: process.env.GS_PRIVATE_KEY_ID
|
80
|
-
}
|
81
|
-
)
|
74
|
+
new GoogleStorage() // config inferred from env variables
|
82
75
|
);
|
83
76
|
|
84
77
|
await app.init();
|
package/adapter.js
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* @import { Config, ServiceFile } from './types.public.js'
|
3
|
+
* @import { ENV } from '@storecraft/core';
|
4
|
+
* @import { storage_driver, StorageFeatures } from '@storecraft/core/storage'
|
5
|
+
*/
|
6
|
+
|
1
7
|
import { getJWTFromServiceAccount, presign } from './adapter.utils.js';
|
2
8
|
|
3
9
|
const types = {
|
@@ -23,25 +29,27 @@ const infer_content_type = (name) => {
|
|
23
29
|
}
|
24
30
|
|
25
31
|
|
26
|
-
/**
|
27
|
-
* @typedef {import('./types.public.d.ts').ServiceFile} ServiceFile
|
28
|
-
* @typedef {import('./types.public.d.ts').Config} Config
|
29
|
-
*/
|
30
|
-
|
31
32
|
/**
|
32
33
|
* @description Google Storage adapter
|
33
|
-
* @typedef {import('@storecraft/core/storage').storage_driver} storage
|
34
34
|
*
|
35
|
-
* @implements {
|
35
|
+
* @implements {storage_driver}
|
36
36
|
*/
|
37
37
|
export class GoogleStorage {
|
38
38
|
|
39
|
+
/** @satisfies {ENV<Config>} */
|
40
|
+
static EnvConfig = /** @type{const} */ ({
|
41
|
+
bucket: 'GS_BUCKET',
|
42
|
+
client_email: 'GS_CLIENT_EMAIL',
|
43
|
+
private_key: 'GS_PRIVATE_KEY',
|
44
|
+
private_key_id: 'GS_PRIVATE_KEY_ID',
|
45
|
+
});
|
46
|
+
|
39
47
|
/** @type {Config} */ #_config;
|
40
48
|
|
41
49
|
/**
|
42
50
|
* @param {Config} [config]
|
43
51
|
*/
|
44
|
-
constructor(config) {
|
52
|
+
constructor(config={}) {
|
45
53
|
this.#_config = config;
|
46
54
|
}
|
47
55
|
|
@@ -49,24 +57,22 @@ export class GoogleStorage {
|
|
49
57
|
get config() { return this.#_config; }
|
50
58
|
|
51
59
|
/**
|
52
|
-
* @type {
|
60
|
+
* @type {storage_driver["init"]}
|
53
61
|
*/
|
54
62
|
async init(app) {
|
55
63
|
if(!app)
|
56
64
|
return this;
|
57
65
|
|
58
|
-
this.#_config
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
private_key_id: app.platform.env.GS_PRIVATE_KEY_ID,
|
63
|
-
}
|
66
|
+
this.#_config.bucket ??= app.platform.env[GoogleStorage.EnvConfig.bucket];
|
67
|
+
this.#_config.client_email ??= app.platform.env[GoogleStorage.EnvConfig.client_email];
|
68
|
+
this.#_config.private_key ??= app.platform.env[GoogleStorage.EnvConfig.private_key];
|
69
|
+
this.#_config.private_key_id ??= app.platform.env[GoogleStorage.EnvConfig.private_key_id];
|
64
70
|
|
65
71
|
return this;
|
66
72
|
}
|
67
73
|
|
68
74
|
features() {
|
69
|
-
/** @type {
|
75
|
+
/** @type {StorageFeatures} */
|
70
76
|
const f = {
|
71
77
|
supports_signed_urls: true
|
72
78
|
}
|
@@ -113,8 +119,7 @@ export class GoogleStorage {
|
|
113
119
|
|
114
120
|
/**
|
115
121
|
*
|
116
|
-
* @
|
117
|
-
* @param {Blob} blob
|
122
|
+
* @type {storage_driver["putBlob"]}
|
118
123
|
*/
|
119
124
|
async putBlob(key, blob) {
|
120
125
|
return this.#put_internal(key, blob);
|
@@ -122,8 +127,7 @@ export class GoogleStorage {
|
|
122
127
|
|
123
128
|
/**
|
124
129
|
*
|
125
|
-
* @
|
126
|
-
* @param {ArrayBuffer} buffer
|
130
|
+
* @type {storage_driver["putArraybuffer"]}
|
127
131
|
*/
|
128
132
|
async putArraybuffer(key, buffer) {
|
129
133
|
return this.#put_internal(key, buffer);
|
@@ -131,8 +135,7 @@ export class GoogleStorage {
|
|
131
135
|
|
132
136
|
/**
|
133
137
|
*
|
134
|
-
* @
|
135
|
-
* @param {ReadableStream} stream
|
138
|
+
* @type {storage_driver["putStream"]}
|
136
139
|
*/
|
137
140
|
async putStream(key, stream) {
|
138
141
|
return this.#put_internal(key, stream);
|
@@ -140,7 +143,7 @@ export class GoogleStorage {
|
|
140
143
|
|
141
144
|
/**
|
142
145
|
*
|
143
|
-
* @
|
146
|
+
* @type {storage_driver["getSigned"]}
|
144
147
|
*/
|
145
148
|
async putSigned(key) {
|
146
149
|
const ct = infer_content_type(key);
|
@@ -190,7 +193,7 @@ export class GoogleStorage {
|
|
190
193
|
|
191
194
|
/**
|
192
195
|
*
|
193
|
-
* @
|
196
|
+
* @type {storage_driver["getArraybuffer"]}
|
194
197
|
*/
|
195
198
|
async getArraybuffer(key) {
|
196
199
|
const r = await this.#get_request(key);
|
@@ -205,7 +208,7 @@ export class GoogleStorage {
|
|
205
208
|
|
206
209
|
/**
|
207
210
|
*
|
208
|
-
* @
|
211
|
+
* @type {storage_driver["getBlob"]}
|
209
212
|
*/
|
210
213
|
async getBlob(key) {
|
211
214
|
const r = await this.#get_request(key);
|
@@ -222,8 +225,7 @@ export class GoogleStorage {
|
|
222
225
|
|
223
226
|
/**
|
224
227
|
*
|
225
|
-
* @
|
226
|
-
* @param {Response} key
|
228
|
+
* @type {storage_driver["getStream"]}
|
227
229
|
*/
|
228
230
|
async getStream(key) {
|
229
231
|
|
@@ -238,7 +240,7 @@ export class GoogleStorage {
|
|
238
240
|
|
239
241
|
/**
|
240
242
|
*
|
241
|
-
* @
|
243
|
+
* @type {storage_driver["getSigned"]}
|
242
244
|
*/
|
243
245
|
async getSigned(key) {
|
244
246
|
const sf = this.config;
|
@@ -260,7 +262,7 @@ export class GoogleStorage {
|
|
260
262
|
|
261
263
|
/**
|
262
264
|
*
|
263
|
-
* @
|
265
|
+
* @type {storage_driver["remove"]}
|
264
266
|
*/
|
265
267
|
async remove(key) {
|
266
268
|
const Authorization = 'Bearer ' + await getJWTFromServiceAccount(this.config);
|
package/package.json
CHANGED
package/types.public.d.ts
CHANGED
@@ -15,12 +15,12 @@ export type ServiceFile = {
|
|
15
15
|
}
|
16
16
|
|
17
17
|
export type Config = {
|
18
|
-
/** bucket name */
|
19
|
-
bucket
|
20
|
-
/** client email from the service file */
|
21
|
-
client_email
|
22
|
-
/** private key */
|
23
|
-
private_key
|
24
|
-
/** private key id */
|
25
|
-
private_key_id
|
18
|
+
/** bucket name, if missing will be inferred by env variable `GS_BUCKET` */
|
19
|
+
bucket?: string;
|
20
|
+
/** client email from the service file, if missing will be inferred by env variable `GS_CLIENT_EMAIL` */
|
21
|
+
client_email?: string;
|
22
|
+
/** private key, if missing will be inferred by env variable `GS_PRIVATE_KEY` */
|
23
|
+
private_key?: string;
|
24
|
+
/** private key id, if missing will be inferred by env variable `GS_PRIVATE_KEY_ID` */
|
25
|
+
private_key_id?: string;
|
26
26
|
}
|