@genoacms/adapter-gcp 0.9.0 → 0.10.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 +21 -0
- package/dist/storage/runtime.js +74 -45
- package/package.json +4 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2026 Filip Holčík
|
|
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/dist/storage/runtime.js
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import { defineRuntime } from '@genoacms/contracts';
|
|
2
|
-
import { PreconditionFailedError } from '@genoacms/contracts/storage';
|
|
2
|
+
import { PreconditionFailedError, ObjectNotFoundError, NotADirectoryError, isDirectoryName } from '@genoacms/contracts/storage';
|
|
3
3
|
import { Storage } from '@google-cloud/storage';
|
|
4
4
|
const HTTP_PRECONDITION_FAILED = 412;
|
|
5
|
+
const HTTP_NOT_FOUND = 404;
|
|
5
6
|
const DELETE_CONCURRENCY = 10;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const hasStatus = (error, status) => error?.code === status;
|
|
8
|
+
// STO-4, STO-7, STOR-16
|
|
9
|
+
async function rejectingMissing(reference, call) {
|
|
10
|
+
try {
|
|
11
|
+
return await call();
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (hasStatus(error, HTTP_NOT_FOUND))
|
|
15
|
+
throw new ObjectNotFoundError(reference);
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
9
18
|
}
|
|
10
19
|
// STO-9
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
20
|
+
function listingQuery(name, { limit, startAfter, pageToken }) {
|
|
21
|
+
return {
|
|
22
|
+
prefix: name,
|
|
23
|
+
delimiter: '/',
|
|
24
|
+
autoPaginate: false,
|
|
25
|
+
...(limit === undefined ? {} : { maxResults: limit }),
|
|
26
|
+
...(startAfter === undefined ? {} : { startOffset: startAfter }),
|
|
27
|
+
...(pageToken === undefined ? {} : { pageToken })
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// STO-11, GF38
|
|
31
|
+
function everyDepthPage(prefix, pageToken) {
|
|
32
|
+
return pageToken === undefined ? { prefix, autoPaginate: false } : { prefix, autoPaginate: false, pageToken };
|
|
16
33
|
}
|
|
17
34
|
// STO-11, GF29
|
|
18
35
|
async function deleteAtMost(concurrency, files) {
|
|
@@ -46,6 +63,16 @@ export default defineRuntime({
|
|
|
46
63
|
throw new Error('bucket-unregistered');
|
|
47
64
|
return storage.bucket(name);
|
|
48
65
|
};
|
|
66
|
+
// STO-3, STOR-3
|
|
67
|
+
const requireDirectoryName = (reference, { allowRoot }) => {
|
|
68
|
+
if (!isDirectoryName(reference.name) || (!allowRoot && reference.name === ''))
|
|
69
|
+
throw new NotADirectoryError(reference);
|
|
70
|
+
};
|
|
71
|
+
const getDirectoryBucket = (reference, options) => {
|
|
72
|
+
const bucketInstance = getBucket(reference.bucket);
|
|
73
|
+
requireDirectoryName(reference, options);
|
|
74
|
+
return bucketInstance;
|
|
75
|
+
};
|
|
49
76
|
// STO-4
|
|
50
77
|
const getObject = async ({ bucket, name }) => {
|
|
51
78
|
const bucketInstance = getBucket(bucket);
|
|
@@ -55,7 +82,9 @@ export default defineRuntime({
|
|
|
55
82
|
const [metadata] = await file.getMetadata();
|
|
56
83
|
version = metadata.generation === undefined ? undefined : String(metadata.generation);
|
|
57
84
|
}
|
|
58
|
-
catch {
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (hasStatus(error, HTTP_NOT_FOUND))
|
|
87
|
+
throw new ObjectNotFoundError({ bucket, name });
|
|
59
88
|
version = undefined;
|
|
60
89
|
}
|
|
61
90
|
return {
|
|
@@ -80,16 +109,16 @@ export default defineRuntime({
|
|
|
80
109
|
const uploadObject = async ({ bucket, name }, stream, options) => {
|
|
81
110
|
const bucketInstance = getBucket(bucket);
|
|
82
111
|
const file = bucketInstance.file(name);
|
|
83
|
-
const { ifVersion, ifAbsent
|
|
112
|
+
const { ifVersion, ifAbsent } = options ?? {};
|
|
84
113
|
// STO-6
|
|
85
114
|
const generation = ifAbsent === true ? 0 : ifVersion === undefined ? undefined : Number(ifVersion);
|
|
86
115
|
try {
|
|
87
116
|
await file.save(stream, generation === undefined
|
|
88
|
-
?
|
|
89
|
-
: {
|
|
117
|
+
? {}
|
|
118
|
+
: { preconditionOpts: { ifGenerationMatch: generation } });
|
|
90
119
|
}
|
|
91
120
|
catch (error) {
|
|
92
|
-
if (error
|
|
121
|
+
if (hasStatus(error, HTTP_PRECONDITION_FAILED)) {
|
|
93
122
|
throw new PreconditionFailedError({ bucket, name }, ifAbsent === true
|
|
94
123
|
? 'object already exists'
|
|
95
124
|
: 'object changed since it was read');
|
|
@@ -105,26 +134,22 @@ export default defineRuntime({
|
|
|
105
134
|
const deleteObject = async ({ bucket, name }) => {
|
|
106
135
|
const bucketInstance = getBucket(bucket);
|
|
107
136
|
const file = bucketInstance.file(name);
|
|
108
|
-
await file.delete();
|
|
137
|
+
await rejectingMissing({ bucket, name }, async () => await file.delete());
|
|
109
138
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// STO-9, GF21
|
|
122
|
-
const skipped = (itemName) => itemName === name || itemName === listingParams?.startAfter;
|
|
123
|
-
files = files.filter(f => !skipped(f.name));
|
|
124
|
-
const prefixes = (apiResponse?.prefixes ?? []).filter((item) => !skipped(item));
|
|
125
|
-
const kept = firstByName([...files.map(f => f.name), ...prefixes], listingParams?.limit);
|
|
139
|
+
// STO-9, GF21
|
|
140
|
+
const listDirectory = async (reference, listingParams = {}) => {
|
|
141
|
+
const bucketInstance = getDirectoryBucket(reference, { allowRoot: true });
|
|
142
|
+
const { bucket, name } = reference;
|
|
143
|
+
if (listingParams.limit === 0)
|
|
144
|
+
return { files: [], directories: [] };
|
|
145
|
+
const [listed, nextQuery, apiResponse] = (await bucketInstance.getFiles(listingQuery(name, listingParams)));
|
|
146
|
+
const skipped = (itemName) => itemName === name || itemName === listingParams.startAfter;
|
|
147
|
+
const files = listed.filter(file => !file.name.endsWith('.folderPlaceholder') && !skipped(file.name));
|
|
148
|
+
const prefixes = (apiResponse?.prefixes ?? []).filter(item => !skipped(item));
|
|
149
|
+
const next = nextQuery?.pageToken;
|
|
126
150
|
return {
|
|
127
|
-
|
|
151
|
+
...(next === undefined ? {} : { next }),
|
|
152
|
+
files: files.map((file) => {
|
|
128
153
|
return {
|
|
129
154
|
name: file.name,
|
|
130
155
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
@@ -132,7 +157,7 @@ export default defineRuntime({
|
|
|
132
157
|
lastModified: new Date(file.metadata.updated)
|
|
133
158
|
};
|
|
134
159
|
}),
|
|
135
|
-
directories: prefixes.
|
|
160
|
+
directories: prefixes.map(i => {
|
|
136
161
|
const object = {
|
|
137
162
|
bucket,
|
|
138
163
|
name: i
|
|
@@ -141,24 +166,28 @@ export default defineRuntime({
|
|
|
141
166
|
})
|
|
142
167
|
};
|
|
143
168
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
169
|
+
// STO-10, CD10
|
|
170
|
+
const createDirectory = async (reference) => {
|
|
171
|
+
const bucketInstance = getDirectoryBucket(reference, { allowRoot: false });
|
|
172
|
+
const file = bucketInstance.file(`${reference.name}.folderPlaceholder`);
|
|
147
173
|
await file.save('');
|
|
148
174
|
};
|
|
149
175
|
// STO-11, GD7, GF29
|
|
150
|
-
const deleteDirectory = async (
|
|
151
|
-
const bucketInstance =
|
|
152
|
-
let
|
|
153
|
-
|
|
154
|
-
const [files, nextQuery] = await bucketInstance.getFiles(
|
|
176
|
+
const deleteDirectory = async (reference) => {
|
|
177
|
+
const bucketInstance = getDirectoryBucket(reference, { allowRoot: false });
|
|
178
|
+
let pageToken;
|
|
179
|
+
do {
|
|
180
|
+
const [files, nextQuery] = await bucketInstance.getFiles(everyDepthPage(reference.name, pageToken));
|
|
155
181
|
await deleteAtMost(DELETE_CONCURRENCY, files);
|
|
156
|
-
|
|
157
|
-
}
|
|
182
|
+
pageToken = nextQuery?.pageToken;
|
|
183
|
+
} while (pageToken !== undefined);
|
|
158
184
|
};
|
|
159
185
|
// STO-12, GD7, GF22
|
|
160
|
-
const moveDirectory = async (
|
|
161
|
-
const
|
|
186
|
+
const moveDirectory = async (reference, newName) => {
|
|
187
|
+
const bucketInstance = getDirectoryBucket(reference, { allowRoot: false });
|
|
188
|
+
requireDirectoryName({ bucket: reference.bucket, name: newName }, { allowRoot: false });
|
|
189
|
+
const { name } = reference;
|
|
190
|
+
const [files] = await bucketInstance.getFiles({ prefix: name });
|
|
162
191
|
for (const file of files)
|
|
163
192
|
await file.move(newName + file.name.slice(name.length));
|
|
164
193
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@genoacms/adapter-gcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Implementation of abstraction layer of GenoaCMS for GCP",
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"name": "Filip Holčík",
|
|
13
13
|
"email": "filip.holcik.official@gmail.com"
|
|
14
14
|
},
|
|
15
|
-
"license": "
|
|
15
|
+
"license": "MIT",
|
|
16
16
|
"bugs": {
|
|
17
17
|
"url": "https://github.com/GenoaCMS/genoacms/issues"
|
|
18
18
|
},
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@google-cloud/storage": "^7.7.0",
|
|
28
28
|
"archiver": "^7.0.0",
|
|
29
29
|
"google-auth-library": "^9.15.1",
|
|
30
|
-
"@genoacms/contracts": "^0.0.
|
|
30
|
+
"@genoacms/contracts": "^0.0.3",
|
|
31
31
|
"@genoacms/sveltekit-adapter-cloud-run-functions": "^1.0.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"rimraf": "^5.0.5",
|
|
42
42
|
"typescript": "^5.2.2",
|
|
43
43
|
"vitest": "^3.2.7",
|
|
44
|
-
"@genoacms/conformance": "^0.0.
|
|
44
|
+
"@genoacms/conformance": "^0.0.3"
|
|
45
45
|
},
|
|
46
46
|
"files": [
|
|
47
47
|
"dist"
|