@nestm/storage 0.1.0-alpha.6 → 0.1.0-alpha.7
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/CHANGELOG.md +18 -0
- package/README.md +182 -2
- package/SECURITY.md +40 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts +63 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.d.ts.map +1 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.js +383 -0
- package/dist/ai-sdk/ai-sdk-workspace-tools.js.map +1 -0
- package/dist/ai-sdk/index.d.ts +2 -0
- package/dist/ai-sdk/index.d.ts.map +1 -0
- package/dist/ai-sdk/index.js +2 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/files-sdk/files-sdk.driver.d.ts +16 -2
- package/dist/files-sdk/files-sdk.driver.d.ts.map +1 -1
- package/dist/files-sdk/files-sdk.driver.js +159 -0
- package/dist/files-sdk/files-sdk.driver.js.map +1 -1
- package/dist/files-sdk/fs/index.d.ts +21 -2
- package/dist/files-sdk/fs/index.d.ts.map +1 -1
- package/dist/files-sdk/fs/index.js +600 -1
- package/dist/files-sdk/fs/index.js.map +1 -1
- package/dist/files-sdk/index.d.ts +1 -1
- package/dist/files-sdk/index.d.ts.map +1 -1
- package/dist/files-sdk/index.js.map +1 -1
- package/dist/files-sdk/provider/index.d.ts.map +1 -1
- package/dist/files-sdk/provider/index.js +7 -0
- package/dist/files-sdk/provider/index.js.map +1 -1
- package/dist/files-sdk/s3/index.d.ts +12 -3
- package/dist/files-sdk/s3/index.d.ts.map +1 -1
- package/dist/files-sdk/s3/index.js +203 -37
- package/dist/files-sdk/s3/index.js.map +1 -1
- package/dist/storage.client.d.ts +5 -1
- package/dist/storage.client.d.ts.map +1 -1
- package/dist/storage.client.js +49 -0
- package/dist/storage.client.js.map +1 -1
- package/dist/storage.driver.d.ts +3 -1
- package/dist/storage.driver.d.ts.map +1 -1
- package/dist/storage.driver.js.map +1 -1
- package/dist/storage.service.d.ts.map +1 -1
- package/dist/storage.service.js +28 -3
- package/dist/storage.service.js.map +1 -1
- package/dist/storage.types.d.ts +36 -0
- package/dist/storage.types.d.ts.map +1 -1
- package/dist/storage.types.js.map +1 -1
- package/dist/workspace/index.d.ts +4 -0
- package/dist/workspace/index.d.ts.map +1 -0
- package/dist/workspace/index.js +4 -0
- package/dist/workspace/index.js.map +1 -0
- package/dist/workspace/storage-workspace.cursor.d.ts +6 -0
- package/dist/workspace/storage-workspace.cursor.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.cursor.js +45 -0
- package/dist/workspace/storage-workspace.cursor.js.map +1 -0
- package/dist/workspace/storage-workspace.d.ts +6 -0
- package/dist/workspace/storage-workspace.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.error.d.ts +28 -0
- package/dist/workspace/storage-workspace.error.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.error.js +67 -0
- package/dist/workspace/storage-workspace.error.js.map +1 -0
- package/dist/workspace/storage-workspace.js +747 -0
- package/dist/workspace/storage-workspace.js.map +1 -0
- package/dist/workspace/storage-workspace.path.d.ts +9 -0
- package/dist/workspace/storage-workspace.path.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.path.js +68 -0
- package/dist/workspace/storage-workspace.path.js.map +1 -0
- package/dist/workspace/storage-workspace.types.d.ts +97 -0
- package/dist/workspace/storage-workspace.types.d.ts.map +1 -0
- package/dist/workspace/storage-workspace.types.js +20 -0
- package/dist/workspace/storage-workspace.types.js.map +1 -0
- package/package.json +23 -4
|
@@ -1,5 +1,597 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants as fsConstants } from 'node:fs';
|
|
3
|
+
import * as fsp from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
1
5
|
import { fs } from 'files-sdk/fs';
|
|
6
|
+
import { StorageError, StorageErrorCode } from '../../storage.error.js';
|
|
2
7
|
import { createFilesSdkDriver, } from '../files-sdk.driver.js';
|
|
8
|
+
const SIDECAR_SUFFIX = '.meta.json';
|
|
9
|
+
const TEMP_SUFFIX = '.fls-part';
|
|
10
|
+
const MAX_SIDECAR_BYTES = 1024 * 1024;
|
|
11
|
+
function fsErrorCode(error) {
|
|
12
|
+
if (typeof error === 'object' && error !== null && 'code' in error) {
|
|
13
|
+
return typeof error.code === 'string' ? error.code : undefined;
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
function storageFsError(error, key, operation) {
|
|
18
|
+
if (error instanceof StorageError) {
|
|
19
|
+
return error;
|
|
20
|
+
}
|
|
21
|
+
const systemCode = fsErrorCode(error);
|
|
22
|
+
const code = systemCode === 'ENOENT' || systemCode === 'ENOTDIR'
|
|
23
|
+
? StorageErrorCode.NOT_FOUND
|
|
24
|
+
: systemCode === 'EEXIST'
|
|
25
|
+
? StorageErrorCode.CONFLICT
|
|
26
|
+
: systemCode === 'EACCES' || systemCode === 'EPERM'
|
|
27
|
+
? StorageErrorCode.UNAUTHORIZED
|
|
28
|
+
: StorageErrorCode.PROVIDER;
|
|
29
|
+
return new StorageError(`Filesystem ${operation} failed for "${key}".`, {
|
|
30
|
+
cause: error,
|
|
31
|
+
code,
|
|
32
|
+
key,
|
|
33
|
+
operation,
|
|
34
|
+
permanent: code === StorageErrorCode.NOT_FOUND ||
|
|
35
|
+
code === StorageErrorCode.CONFLICT ||
|
|
36
|
+
code === StorageErrorCode.UNAUTHORIZED,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function invalidFsPath(key, message) {
|
|
40
|
+
throw new StorageError(`Invalid filesystem storage key: ${message}.`, {
|
|
41
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
42
|
+
key,
|
|
43
|
+
permanent: true,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function strictSegments(key) {
|
|
47
|
+
if (key.length === 0 || key.includes('\0')) {
|
|
48
|
+
invalidFsPath(key, 'the key is empty or contains a null byte');
|
|
49
|
+
}
|
|
50
|
+
if (path.isAbsolute(key) || key.includes('\\')) {
|
|
51
|
+
invalidFsPath(key, 'only relative POSIX paths are accepted');
|
|
52
|
+
}
|
|
53
|
+
const segments = key.split('/');
|
|
54
|
+
if (segments.some((segment) => segment.length === 0 || segment === '.' || segment === '..')) {
|
|
55
|
+
invalidFsPath(key, 'empty, dot, and parent segments are not accepted');
|
|
56
|
+
}
|
|
57
|
+
if (segments.some((segment) => /[. ]$/u.test(segment))) {
|
|
58
|
+
invalidFsPath(key, 'segments ending in a dot or space are not accepted');
|
|
59
|
+
}
|
|
60
|
+
const leaf = segments.at(-1)?.toLowerCase() ?? '';
|
|
61
|
+
if (leaf.endsWith(SIDECAR_SUFFIX) || leaf.endsWith(TEMP_SUFFIX)) {
|
|
62
|
+
invalidFsPath(key, 'the final segment uses a reserved adapter suffix');
|
|
63
|
+
}
|
|
64
|
+
return segments;
|
|
65
|
+
}
|
|
66
|
+
function symlinkError(key) {
|
|
67
|
+
return new StorageError(`Filesystem storage key "${key}" crosses a symbolic link.`, {
|
|
68
|
+
code: StorageErrorCode.INVALID_ARGUMENT,
|
|
69
|
+
key,
|
|
70
|
+
permanent: true,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function unsupportedUpload(key, message) {
|
|
74
|
+
throw new StorageError(message, {
|
|
75
|
+
code: StorageErrorCode.NOT_SUPPORTED,
|
|
76
|
+
key,
|
|
77
|
+
operation: 'upload',
|
|
78
|
+
permanent: true,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function conflict(key) {
|
|
82
|
+
throw new StorageError(`Conditional mutation conflicted for "${key}".`, {
|
|
83
|
+
code: StorageErrorCode.CONFLICT,
|
|
84
|
+
key,
|
|
85
|
+
permanent: true,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function notFound(key) {
|
|
89
|
+
throw new StorageError(`Storage object "${key}" was not found.`, {
|
|
90
|
+
code: StorageErrorCode.NOT_FOUND,
|
|
91
|
+
key,
|
|
92
|
+
permanent: true,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
function runtimeOf(options) {
|
|
96
|
+
const timeoutSignal = options.timeout === undefined || options.timeout <= 0
|
|
97
|
+
? undefined
|
|
98
|
+
: AbortSignal.timeout(options.timeout);
|
|
99
|
+
return {
|
|
100
|
+
callerSignal: options.signal,
|
|
101
|
+
signal: options.signal === undefined
|
|
102
|
+
? timeoutSignal
|
|
103
|
+
: timeoutSignal === undefined
|
|
104
|
+
? options.signal
|
|
105
|
+
: AbortSignal.any([options.signal, timeoutSignal]),
|
|
106
|
+
timeoutSignal,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function assertActive(runtime, key) {
|
|
110
|
+
if (runtime.signal?.aborted !== true) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const timedOut = runtime.timeoutSignal?.aborted === true &&
|
|
114
|
+
runtime.callerSignal?.aborted !== true;
|
|
115
|
+
throw new StorageError(timedOut
|
|
116
|
+
? `Filesystem mutation timed out for "${key}".`
|
|
117
|
+
: `Filesystem mutation was aborted for "${key}".`, {
|
|
118
|
+
aborted: true,
|
|
119
|
+
cause: runtime.signal.reason,
|
|
120
|
+
code: timedOut ? StorageErrorCode.TIMEOUT : StorageErrorCode.ABORTED,
|
|
121
|
+
key,
|
|
122
|
+
permanent: true,
|
|
123
|
+
timedOut,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
async function ensureRoot(root, key) {
|
|
127
|
+
await fsp.mkdir(root, { mode: 0o700, recursive: true });
|
|
128
|
+
const stat = await fsp.lstat(root);
|
|
129
|
+
if (stat.isSymbolicLink()) {
|
|
130
|
+
throw symlinkError(key);
|
|
131
|
+
}
|
|
132
|
+
if (!stat.isDirectory()) {
|
|
133
|
+
invalidFsPath(key, 'the configured root is not a directory');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async function ensureParents(root, segments, key, create, runtime) {
|
|
137
|
+
await ensureRoot(root, key);
|
|
138
|
+
let current = root;
|
|
139
|
+
for (const segment of segments.slice(0, -1)) {
|
|
140
|
+
assertActive(runtime, key);
|
|
141
|
+
current = path.join(current, segment);
|
|
142
|
+
let stat;
|
|
143
|
+
try {
|
|
144
|
+
stat = await fsp.lstat(current);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
if (fsErrorCode(error) !== 'ENOENT') {
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
if (!create) {
|
|
151
|
+
notFound(key);
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
await fsp.mkdir(current, { mode: 0o700 });
|
|
155
|
+
}
|
|
156
|
+
catch (mkdirError) {
|
|
157
|
+
if (fsErrorCode(mkdirError) !== 'EEXIST') {
|
|
158
|
+
throw mkdirError;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
stat = await fsp.lstat(current);
|
|
162
|
+
}
|
|
163
|
+
if (stat.isSymbolicLink()) {
|
|
164
|
+
throw symlinkError(key);
|
|
165
|
+
}
|
|
166
|
+
if (!stat.isDirectory()) {
|
|
167
|
+
invalidFsPath(key, 'a parent segment is not a directory');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return path.join(root, ...segments);
|
|
171
|
+
}
|
|
172
|
+
async function regularFileOrMissing(target, key) {
|
|
173
|
+
try {
|
|
174
|
+
const stat = await fsp.lstat(target);
|
|
175
|
+
if (stat.isSymbolicLink()) {
|
|
176
|
+
throw symlinkError(key);
|
|
177
|
+
}
|
|
178
|
+
if (!stat.isFile()) {
|
|
179
|
+
invalidFsPath(key, 'an object path is not a regular file');
|
|
180
|
+
}
|
|
181
|
+
if (stat.nlink !== 1) {
|
|
182
|
+
invalidFsPath(key, 'hard-linked object files are not accepted');
|
|
183
|
+
}
|
|
184
|
+
return stat;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
if (fsErrorCode(error) === 'ENOENT') {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function assertSymlinkFreeReadPath(root, key) {
|
|
194
|
+
const segments = strictSegments(key);
|
|
195
|
+
let rootStat;
|
|
196
|
+
try {
|
|
197
|
+
rootStat = await fsp.lstat(root);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (fsErrorCode(error) === 'ENOENT') {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
throw error;
|
|
204
|
+
}
|
|
205
|
+
if (rootStat.isSymbolicLink()) {
|
|
206
|
+
throw symlinkError(key);
|
|
207
|
+
}
|
|
208
|
+
if (!rootStat.isDirectory()) {
|
|
209
|
+
invalidFsPath(key, 'the configured root is not a directory');
|
|
210
|
+
}
|
|
211
|
+
let current = root;
|
|
212
|
+
for (const [index, segment] of segments.entries()) {
|
|
213
|
+
current = path.join(current, segment);
|
|
214
|
+
let stat;
|
|
215
|
+
try {
|
|
216
|
+
stat = await fsp.lstat(current);
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (fsErrorCode(error) === 'ENOENT') {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
if (stat.isSymbolicLink()) {
|
|
225
|
+
throw symlinkError(key);
|
|
226
|
+
}
|
|
227
|
+
const leaf = index === segments.length - 1;
|
|
228
|
+
if ((!leaf && !stat.isDirectory()) || (leaf && !stat.isFile())) {
|
|
229
|
+
invalidFsPath(key, leaf
|
|
230
|
+
? 'an object path is not a regular file'
|
|
231
|
+
: 'a parent segment is not a directory');
|
|
232
|
+
}
|
|
233
|
+
if (leaf && stat.nlink !== 1) {
|
|
234
|
+
invalidFsPath(key, 'hard-linked object files are not accepted');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const sidecar = await regularFileOrMissing(current + SIDECAR_SUFFIX, key);
|
|
238
|
+
if (sidecar?.isSymbolicLink()) {
|
|
239
|
+
throw symlinkError(key);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async function readSidecar(target, key) {
|
|
243
|
+
const handle = await fsp.open(target, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
|
244
|
+
try {
|
|
245
|
+
const stat = await handle.stat();
|
|
246
|
+
if (!stat.isFile() || stat.size > MAX_SIDECAR_BYTES) {
|
|
247
|
+
throw new StorageError(`Filesystem metadata is invalid for "${key}".`, {
|
|
248
|
+
code: StorageErrorCode.PROVIDER,
|
|
249
|
+
key,
|
|
250
|
+
permanent: true,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
const parsed = JSON.parse(await handle.readFile('utf8'));
|
|
254
|
+
if (typeof parsed !== 'object' ||
|
|
255
|
+
parsed === null ||
|
|
256
|
+
!('etag' in parsed) ||
|
|
257
|
+
typeof parsed.etag !== 'string' ||
|
|
258
|
+
!('contentType' in parsed) ||
|
|
259
|
+
typeof parsed.contentType !== 'string' ||
|
|
260
|
+
!('lastModified' in parsed) ||
|
|
261
|
+
typeof parsed.lastModified !== 'number') {
|
|
262
|
+
throw new StorageError(`Filesystem metadata is invalid for "${key}".`, {
|
|
263
|
+
code: StorageErrorCode.PROVIDER,
|
|
264
|
+
key,
|
|
265
|
+
permanent: true,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
return parsed;
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
await handle.close();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
function bodyBytes(body) {
|
|
275
|
+
if (typeof body === 'string') {
|
|
276
|
+
return new TextEncoder().encode(body);
|
|
277
|
+
}
|
|
278
|
+
if (body instanceof Uint8Array) {
|
|
279
|
+
return body;
|
|
280
|
+
}
|
|
281
|
+
if (body instanceof ArrayBuffer) {
|
|
282
|
+
return new Uint8Array(body);
|
|
283
|
+
}
|
|
284
|
+
if (ArrayBuffer.isView(body)) {
|
|
285
|
+
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
286
|
+
}
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
function defaultContentType(body, override) {
|
|
290
|
+
if (override !== undefined) {
|
|
291
|
+
return override;
|
|
292
|
+
}
|
|
293
|
+
if (typeof body === 'string') {
|
|
294
|
+
return 'text/plain; charset=utf-8';
|
|
295
|
+
}
|
|
296
|
+
if (body instanceof Blob && body.type.length > 0) {
|
|
297
|
+
return body.type;
|
|
298
|
+
}
|
|
299
|
+
return 'application/octet-stream';
|
|
300
|
+
}
|
|
301
|
+
async function writeAll(handle, bytes) {
|
|
302
|
+
let offset = 0;
|
|
303
|
+
while (offset < bytes.byteLength) {
|
|
304
|
+
const { bytesWritten } = await handle.write(bytes, offset, bytes.byteLength - offset);
|
|
305
|
+
offset += bytesWritten;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function tempPath(directory) {
|
|
309
|
+
return path.join(directory, `.nestm-${randomUUID()}${TEMP_SUFFIX}`);
|
|
310
|
+
}
|
|
311
|
+
async function openTemp(directory) {
|
|
312
|
+
const target = tempPath(directory);
|
|
313
|
+
const handle = await fsp.open(target, fsConstants.O_CREAT |
|
|
314
|
+
fsConstants.O_EXCL |
|
|
315
|
+
fsConstants.O_WRONLY |
|
|
316
|
+
fsConstants.O_NOFOLLOW, 0o600);
|
|
317
|
+
return { handle, path: target };
|
|
318
|
+
}
|
|
319
|
+
async function removeTemp(target) {
|
|
320
|
+
if (target === undefined) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
await fsp.unlink(target);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
if (fsErrorCode(error) !== 'ENOENT') {
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
async function stageBody(directory, key, body, options, runtime) {
|
|
333
|
+
const staged = await openTemp(directory);
|
|
334
|
+
const hash = createHash('sha256');
|
|
335
|
+
let size = 0;
|
|
336
|
+
const knownBytes = bodyBytes(body);
|
|
337
|
+
const knownSize = knownBytes?.byteLength ?? (body instanceof Blob ? body.size : undefined);
|
|
338
|
+
options.onProgress?.({
|
|
339
|
+
loaded: 0,
|
|
340
|
+
...(knownSize !== undefined && { total: knownSize }),
|
|
341
|
+
});
|
|
342
|
+
try {
|
|
343
|
+
if (knownBytes !== undefined) {
|
|
344
|
+
assertActive(runtime, key);
|
|
345
|
+
await writeAll(staged.handle, knownBytes);
|
|
346
|
+
hash.update(knownBytes);
|
|
347
|
+
size = knownBytes.byteLength;
|
|
348
|
+
options.onProgress?.({ loaded: size, total: size });
|
|
349
|
+
}
|
|
350
|
+
else if (body instanceof Blob) {
|
|
351
|
+
const bytes = new Uint8Array(await body.arrayBuffer());
|
|
352
|
+
assertActive(runtime, key);
|
|
353
|
+
await writeAll(staged.handle, bytes);
|
|
354
|
+
hash.update(bytes);
|
|
355
|
+
size = bytes.byteLength;
|
|
356
|
+
options.onProgress?.({ loaded: size, total: size });
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
const reader = body.getReader();
|
|
360
|
+
try {
|
|
361
|
+
while (true) {
|
|
362
|
+
assertActive(runtime, key);
|
|
363
|
+
const chunk = await reader.read();
|
|
364
|
+
if (chunk.done) {
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
await writeAll(staged.handle, chunk.value);
|
|
368
|
+
hash.update(chunk.value);
|
|
369
|
+
size += chunk.value.byteLength;
|
|
370
|
+
options.onProgress?.({ loaded: size });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
await reader.cancel(error).catch(() => undefined);
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
finally {
|
|
378
|
+
reader.releaseLock();
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
await staged.handle.sync();
|
|
382
|
+
await staged.handle.close();
|
|
383
|
+
return {
|
|
384
|
+
etag: `"${hash.digest('hex')}"`,
|
|
385
|
+
path: staged.path,
|
|
386
|
+
size,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
catch (error) {
|
|
390
|
+
await staged.handle.close().catch(() => undefined);
|
|
391
|
+
await removeTemp(staged.path).catch(() => undefined);
|
|
392
|
+
throw error;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async function stageSidecar(directory, sidecar) {
|
|
396
|
+
const staged = await openTemp(directory);
|
|
397
|
+
try {
|
|
398
|
+
await writeAll(staged.handle, new TextEncoder().encode(JSON.stringify(sidecar)));
|
|
399
|
+
await staged.handle.sync();
|
|
400
|
+
await staged.handle.close();
|
|
401
|
+
return staged.path;
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
await staged.handle.close().catch(() => undefined);
|
|
405
|
+
await removeTemp(staged.path).catch(() => undefined);
|
|
406
|
+
throw error;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const CONDITIONAL_FS_TAILS = new Map();
|
|
410
|
+
async function serializeFsKey(lockKey, operation) {
|
|
411
|
+
const previous = CONDITIONAL_FS_TAILS.get(lockKey) ?? Promise.resolve();
|
|
412
|
+
let release = () => undefined;
|
|
413
|
+
const gate = new Promise((resolve) => {
|
|
414
|
+
release = resolve;
|
|
415
|
+
});
|
|
416
|
+
const tail = previous.then(() => gate);
|
|
417
|
+
CONDITIONAL_FS_TAILS.set(lockKey, tail);
|
|
418
|
+
await previous;
|
|
419
|
+
try {
|
|
420
|
+
return await operation();
|
|
421
|
+
}
|
|
422
|
+
finally {
|
|
423
|
+
release();
|
|
424
|
+
if (CONDITIONAL_FS_TAILS.get(lockKey) === tail) {
|
|
425
|
+
CONDITIONAL_FS_TAILS.delete(lockKey);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Adds symlink-defended reads and process-local conditional mutations to a
|
|
431
|
+
* files-sdk fs adapter.
|
|
432
|
+
*
|
|
433
|
+
* The configured root must be dedicated to this driver: no other process and
|
|
434
|
+
* no unconditional filesystem writer may mutate it concurrently. Each call
|
|
435
|
+
* rejects symlinks in the existing key path and uses exclusive same-directory
|
|
436
|
+
* temporary files plus atomic renames, but Node has no portable `openat2`
|
|
437
|
+
* equivalent that could make those checks safe against a hostile concurrent
|
|
438
|
+
* tree rewrite. This is a CAS implementation for an exclusively-owned local
|
|
439
|
+
* workspace, not a host-filesystem sandbox. Body and metadata sidecars are two
|
|
440
|
+
* files, so a process crash between their renames can still require cleanup.
|
|
441
|
+
*/
|
|
442
|
+
export function withFsConditionalMutation(base) {
|
|
443
|
+
const root = path.resolve(base.root);
|
|
444
|
+
const download = base.download.bind(base);
|
|
445
|
+
const exists = base.exists.bind(base);
|
|
446
|
+
const head = base.head.bind(base);
|
|
447
|
+
const list = base.list.bind(base);
|
|
448
|
+
const serialize = (key, operation) => serializeFsKey(`${root}\0${key.normalize('NFC').toLowerCase()}`, operation);
|
|
449
|
+
return Object.assign(base, {
|
|
450
|
+
conditionalMutation: Object.freeze({
|
|
451
|
+
create: true,
|
|
452
|
+
delete: true,
|
|
453
|
+
etag: true,
|
|
454
|
+
replace: true,
|
|
455
|
+
}),
|
|
456
|
+
async download(key, options) {
|
|
457
|
+
await assertSymlinkFreeReadPath(root, key);
|
|
458
|
+
return download(key, options);
|
|
459
|
+
},
|
|
460
|
+
async exists(key, options) {
|
|
461
|
+
await assertSymlinkFreeReadPath(root, key);
|
|
462
|
+
return exists(key, options);
|
|
463
|
+
},
|
|
464
|
+
async head(key, options) {
|
|
465
|
+
await assertSymlinkFreeReadPath(root, key);
|
|
466
|
+
return head(key, options);
|
|
467
|
+
},
|
|
468
|
+
async list(options) {
|
|
469
|
+
const result = await list(options);
|
|
470
|
+
for (const item of result.items) {
|
|
471
|
+
await assertSymlinkFreeReadPath(root, item.key);
|
|
472
|
+
}
|
|
473
|
+
return result;
|
|
474
|
+
},
|
|
475
|
+
async deleteConditional(key, options) {
|
|
476
|
+
return serialize(key, async () => {
|
|
477
|
+
const runtime = runtimeOf(options);
|
|
478
|
+
try {
|
|
479
|
+
const segments = strictSegments(key);
|
|
480
|
+
const bodyPath = await ensureParents(root, segments, key, false, runtime);
|
|
481
|
+
const sidecarPath = bodyPath + SIDECAR_SUFFIX;
|
|
482
|
+
if ((await regularFileOrMissing(bodyPath, key)) === undefined ||
|
|
483
|
+
(await regularFileOrMissing(sidecarPath, key)) === undefined) {
|
|
484
|
+
notFound(key);
|
|
485
|
+
}
|
|
486
|
+
const sidecar = await readSidecar(sidecarPath, key);
|
|
487
|
+
if (sidecar.etag !== options.condition.etag) {
|
|
488
|
+
conflict(key);
|
|
489
|
+
}
|
|
490
|
+
assertActive(runtime, key);
|
|
491
|
+
await ensureParents(root, segments, key, false, runtime);
|
|
492
|
+
if ((await regularFileOrMissing(bodyPath, key)) === undefined ||
|
|
493
|
+
(await regularFileOrMissing(sidecarPath, key)) === undefined ||
|
|
494
|
+
(await readSidecar(sidecarPath, key)).etag !==
|
|
495
|
+
options.condition.etag) {
|
|
496
|
+
conflict(key);
|
|
497
|
+
}
|
|
498
|
+
await fsp.unlink(bodyPath);
|
|
499
|
+
try {
|
|
500
|
+
await fsp.unlink(sidecarPath);
|
|
501
|
+
}
|
|
502
|
+
catch (error) {
|
|
503
|
+
throw storageFsError(error, key, 'delete');
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
catch (error) {
|
|
507
|
+
throw storageFsError(error, key, 'delete');
|
|
508
|
+
}
|
|
509
|
+
});
|
|
510
|
+
},
|
|
511
|
+
async uploadConditional(key, body, options) {
|
|
512
|
+
return serialize(key, async () => {
|
|
513
|
+
let bodyTemp;
|
|
514
|
+
let sidecarTemp;
|
|
515
|
+
const runtime = runtimeOf(options);
|
|
516
|
+
try {
|
|
517
|
+
if (options.multipart !== undefined && options.multipart !== false) {
|
|
518
|
+
unsupportedUpload(key, 'Conditional filesystem uploads do not support multipart mode.');
|
|
519
|
+
}
|
|
520
|
+
if (options.control !== undefined) {
|
|
521
|
+
unsupportedUpload(key, 'Conditional filesystem uploads do not support resumable control.');
|
|
522
|
+
}
|
|
523
|
+
const segments = strictSegments(key);
|
|
524
|
+
const bodyPath = await ensureParents(root, segments, key, true, runtime);
|
|
525
|
+
const sidecarPath = bodyPath + SIDECAR_SUFFIX;
|
|
526
|
+
const existingBody = await regularFileOrMissing(bodyPath, key);
|
|
527
|
+
const existingSidecar = await regularFileOrMissing(sidecarPath, key);
|
|
528
|
+
if (options.condition.type === 'create') {
|
|
529
|
+
if (existingBody !== undefined || existingSidecar !== undefined) {
|
|
530
|
+
conflict(key);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
else {
|
|
534
|
+
if (existingBody === undefined || existingSidecar === undefined) {
|
|
535
|
+
notFound(key);
|
|
536
|
+
}
|
|
537
|
+
if ((await readSidecar(sidecarPath, key)).etag !==
|
|
538
|
+
options.condition.etag) {
|
|
539
|
+
conflict(key);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
const staged = await stageBody(path.dirname(bodyPath), key, body, options, runtime);
|
|
543
|
+
bodyTemp = staged.path;
|
|
544
|
+
const lastModified = Date.now();
|
|
545
|
+
const contentType = defaultContentType(body, options.contentType);
|
|
546
|
+
sidecarTemp = await stageSidecar(path.dirname(bodyPath), {
|
|
547
|
+
contentType,
|
|
548
|
+
etag: staged.etag,
|
|
549
|
+
lastModified,
|
|
550
|
+
...(options.cacheControl !== undefined && {
|
|
551
|
+
cacheControl: options.cacheControl,
|
|
552
|
+
}),
|
|
553
|
+
...(options.metadata !== undefined && {
|
|
554
|
+
metadata: options.metadata,
|
|
555
|
+
}),
|
|
556
|
+
});
|
|
557
|
+
assertActive(runtime, key);
|
|
558
|
+
await ensureParents(root, segments, key, false, runtime);
|
|
559
|
+
const currentBody = await regularFileOrMissing(bodyPath, key);
|
|
560
|
+
const currentSidecar = await regularFileOrMissing(sidecarPath, key);
|
|
561
|
+
if (options.condition.type === 'create') {
|
|
562
|
+
if (currentBody !== undefined || currentSidecar !== undefined) {
|
|
563
|
+
conflict(key);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
else if (currentBody === undefined ||
|
|
567
|
+
currentSidecar === undefined ||
|
|
568
|
+
(await readSidecar(sidecarPath, key)).etag !==
|
|
569
|
+
options.condition.etag) {
|
|
570
|
+
conflict(key);
|
|
571
|
+
}
|
|
572
|
+
await fsp.rename(bodyTemp, bodyPath);
|
|
573
|
+
bodyTemp = undefined;
|
|
574
|
+
await fsp.rename(sidecarTemp, sidecarPath);
|
|
575
|
+
sidecarTemp = undefined;
|
|
576
|
+
return {
|
|
577
|
+
contentType,
|
|
578
|
+
etag: staged.etag,
|
|
579
|
+
key,
|
|
580
|
+
lastModified: new Date(lastModified),
|
|
581
|
+
size: staged.size,
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
catch (error) {
|
|
585
|
+
throw storageFsError(error, key, 'upload');
|
|
586
|
+
}
|
|
587
|
+
finally {
|
|
588
|
+
await removeTemp(bodyTemp).catch(() => undefined);
|
|
589
|
+
await removeTemp(sidecarTemp).catch(() => undefined);
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
},
|
|
593
|
+
});
|
|
594
|
+
}
|
|
3
595
|
/**
|
|
4
596
|
* Creates the files-sdk filesystem driver from the storage package's own
|
|
5
597
|
* dependency context. The adapter reaches only `node:fs`, so this entry point
|
|
@@ -11,10 +603,17 @@ import { createFilesSdkDriver, } from '../files-sdk.driver.js';
|
|
|
11
603
|
* put. Sidecars never surface as keys: `list` and `search` skip them, and
|
|
12
604
|
* uploading a key that ends in `.meta.json` fails closed rather than colliding
|
|
13
605
|
* with one.
|
|
606
|
+
*
|
|
607
|
+
* Conditional mutations additionally require an exclusively-owned root. See
|
|
608
|
+
* {@link withFsConditionalMutation}; use an OS sandbox as well when running an
|
|
609
|
+
* agent that has direct shell or filesystem tools.
|
|
14
610
|
*/
|
|
15
611
|
export function createFsStorageDriver(options) {
|
|
16
612
|
const { adapter: adapterOptions, ...filesOptions } = options;
|
|
17
|
-
return createFilesSdkDriver({
|
|
613
|
+
return createFilesSdkDriver({
|
|
614
|
+
...filesOptions,
|
|
615
|
+
adapter: withFsConditionalMutation(fs(adapterOptions)),
|
|
616
|
+
});
|
|
18
617
|
}
|
|
19
618
|
export { fs, mapFsError } from 'files-sdk/fs';
|
|
20
619
|
//# sourceMappingURL=index.js.map
|