@nestm/storage 0.1.0-alpha.5 → 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 +26 -0
- package/README.md +158 -92
- 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 -20
- package/dist/artifacts/artifact-storage.d.ts +0 -161
- package/dist/artifacts/artifact-storage.d.ts.map +0 -1
- package/dist/artifacts/artifact-storage.js +0 -452
- package/dist/artifacts/artifact-storage.js.map +0 -1
- package/dist/artifacts/crypto/codec.d.ts +0 -44
- package/dist/artifacts/crypto/codec.d.ts.map +0 -1
- package/dist/artifacts/crypto/codec.js +0 -273
- package/dist/artifacts/crypto/codec.js.map +0 -1
- package/dist/artifacts/crypto/context.d.ts +0 -48
- package/dist/artifacts/crypto/context.d.ts.map +0 -1
- package/dist/artifacts/crypto/context.js +0 -37
- package/dist/artifacts/crypto/context.js.map +0 -1
- package/dist/artifacts/crypto/dek-cache.d.ts +0 -38
- package/dist/artifacts/crypto/dek-cache.d.ts.map +0 -1
- package/dist/artifacts/crypto/dek-cache.js +0 -137
- package/dist/artifacts/crypto/dek-cache.js.map +0 -1
- package/dist/artifacts/crypto/index.d.ts +0 -25
- package/dist/artifacts/crypto/index.d.ts.map +0 -1
- package/dist/artifacts/crypto/index.js +0 -29
- package/dist/artifacts/crypto/index.js.map +0 -1
- package/dist/artifacts/crypto/key-provider.d.ts +0 -44
- package/dist/artifacts/crypto/key-provider.d.ts.map +0 -1
- package/dist/artifacts/crypto/key-provider.js +0 -36
- package/dist/artifacts/crypto/key-provider.js.map +0 -1
- package/dist/artifacts/crypto/kms-provider.d.ts +0 -16
- package/dist/artifacts/crypto/kms-provider.d.ts.map +0 -1
- package/dist/artifacts/crypto/kms-provider.js +0 -90
- package/dist/artifacts/crypto/kms-provider.js.map +0 -1
- package/dist/artifacts/crypto/local-provider.d.ts +0 -17
- package/dist/artifacts/crypto/local-provider.d.ts.map +0 -1
- package/dist/artifacts/crypto/local-provider.js +0 -58
- package/dist/artifacts/crypto/local-provider.js.map +0 -1
- package/dist/artifacts/env-config.d.ts +0 -15
- package/dist/artifacts/env-config.d.ts.map +0 -1
- package/dist/artifacts/env-config.js +0 -95
- package/dist/artifacts/env-config.js.map +0 -1
- package/dist/artifacts/index.d.ts +0 -6
- package/dist/artifacts/index.d.ts.map +0 -1
- package/dist/artifacts/index.js +0 -6
- package/dist/artifacts/index.js.map +0 -1
- package/dist/artifacts/nest/index.d.ts +0 -29
- package/dist/artifacts/nest/index.d.ts.map +0 -1
- package/dist/artifacts/nest/index.js +0 -129
- package/dist/artifacts/nest/index.js.map +0 -1
- package/dist/artifacts/object-store.d.ts +0 -27
- package/dist/artifacts/object-store.d.ts.map +0 -1
- package/dist/artifacts/object-store.js +0 -198
- package/dist/artifacts/object-store.js.map +0 -1
- package/dist/artifacts/storage-driver.d.ts +0 -48
- package/dist/artifacts/storage-driver.d.ts.map +0 -1
- package/dist/artifacts/storage-driver.js +0 -89
- package/dist/artifacts/storage-driver.js.map +0 -1
|
@@ -0,0 +1,747 @@
|
|
|
1
|
+
import { StorageClient } from '../storage.client.js';
|
|
2
|
+
import { StorageErrorCode } from '../storage.error.js';
|
|
3
|
+
import { StorageWorkspaceCursorStore } from './storage-workspace.cursor.js';
|
|
4
|
+
import { sanitizeWorkspaceError, workspaceError, } from './storage-workspace.error.js';
|
|
5
|
+
import { assertWorkspacePath, containsControlCharacter, isPathInside, joinWorkspacePath, workspaceBasename, } from './storage-workspace.path.js';
|
|
6
|
+
import { DEFAULT_STORAGE_WORKSPACE_LIMITS, STORAGE_WORKSPACE_PERMISSIONS, } from './storage-workspace.types.js';
|
|
7
|
+
const DEFAULT_PERMISSIONS = new Set([
|
|
8
|
+
'list',
|
|
9
|
+
'read',
|
|
10
|
+
'search',
|
|
11
|
+
]);
|
|
12
|
+
const PERMISSION_VALUES = new Set(STORAGE_WORKSPACE_PERMISSIONS);
|
|
13
|
+
const encoder = new TextEncoder();
|
|
14
|
+
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
15
|
+
const WORKSPACE_CONSTRUCTOR = Symbol('StorageWorkspace.constructor');
|
|
16
|
+
function operationOptions(options) {
|
|
17
|
+
if (options === undefined) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
...(options.retries !== undefined && { retries: options.retries }),
|
|
22
|
+
...(options.signal !== undefined && { signal: options.signal }),
|
|
23
|
+
...(options.timeout !== undefined && { timeout: options.timeout }),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function positiveSafeInteger(value, label) {
|
|
27
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
28
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, `${label} must be a positive safe integer.`, { permanent: true });
|
|
29
|
+
}
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
function assertEtag(etag, maxBytes, operation) {
|
|
33
|
+
if (typeof etag !== 'string' ||
|
|
34
|
+
etag.length === 0 ||
|
|
35
|
+
containsControlCharacter(etag) ||
|
|
36
|
+
encoder.encode(etag).byteLength > maxBytes) {
|
|
37
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, `${operation} requires a valid non-empty etag.`, { permanent: true });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function resolveLimits(requested, parent) {
|
|
41
|
+
const baseline = parent ?? DEFAULT_STORAGE_WORKSPACE_LIMITS;
|
|
42
|
+
const next = {
|
|
43
|
+
cursorTtlMs: requested?.cursorTtlMs ?? baseline.cursorTtlMs,
|
|
44
|
+
maxPageSize: requested?.maxPageSize ?? baseline.maxPageSize,
|
|
45
|
+
maxPathBytes: requested?.maxPathBytes ?? baseline.maxPathBytes,
|
|
46
|
+
maxReadBytes: requested?.maxReadBytes ?? baseline.maxReadBytes,
|
|
47
|
+
maxSearchResults: requested?.maxSearchResults ?? baseline.maxSearchResults,
|
|
48
|
+
maxSearchScan: requested?.maxSearchScan ?? baseline.maxSearchScan,
|
|
49
|
+
maxWriteBytes: requested?.maxWriteBytes ?? baseline.maxWriteBytes,
|
|
50
|
+
};
|
|
51
|
+
for (const [name, value] of Object.entries(next)) {
|
|
52
|
+
positiveSafeInteger(value, `limits.${name}`);
|
|
53
|
+
const parentValue = baseline[name];
|
|
54
|
+
if (parent !== undefined && value > parentValue) {
|
|
55
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, `Child workspace limit "${name}" cannot exceed its parent limit.`, { permanent: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return Object.freeze(next);
|
|
59
|
+
}
|
|
60
|
+
function resolvePermissions(requested, parent) {
|
|
61
|
+
const baseline = parent ?? DEFAULT_PERMISSIONS;
|
|
62
|
+
const values = requested === undefined ? [...baseline] : [...requested];
|
|
63
|
+
const permissions = new Set();
|
|
64
|
+
for (const permission of values) {
|
|
65
|
+
if (!PERMISSION_VALUES.has(permission)) {
|
|
66
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, `Unknown workspace permission "${String(permission)}".`, { permanent: true });
|
|
67
|
+
}
|
|
68
|
+
if (parent !== undefined && !parent.has(permission)) {
|
|
69
|
+
throw workspaceError(StorageErrorCode.UNAUTHORIZED, `Child workspace cannot add the "${permission}" permission.`, { permanent: true });
|
|
70
|
+
}
|
|
71
|
+
permissions.add(permission);
|
|
72
|
+
}
|
|
73
|
+
return permissions;
|
|
74
|
+
}
|
|
75
|
+
function logicalFile(metadata, path) {
|
|
76
|
+
if (!Number.isSafeInteger(metadata.size) ||
|
|
77
|
+
metadata.size < 0 ||
|
|
78
|
+
typeof metadata.contentType !== 'string' ||
|
|
79
|
+
metadata.contentType.length === 0 ||
|
|
80
|
+
(metadata.etag !== undefined &&
|
|
81
|
+
(typeof metadata.etag !== 'string' || metadata.etag.length === 0)) ||
|
|
82
|
+
(metadata.lastModified !== undefined &&
|
|
83
|
+
!Number.isFinite(new Date(metadata.lastModified).getTime()))) {
|
|
84
|
+
throw workspaceError(StorageErrorCode.PROVIDER, 'Storage provider returned malformed file metadata.', { permanent: true });
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
contentType: metadata.contentType,
|
|
88
|
+
...(metadata.etag !== undefined && { etag: metadata.etag }),
|
|
89
|
+
kind: 'file',
|
|
90
|
+
...(metadata.lastModified !== undefined && {
|
|
91
|
+
lastModified: new Date(metadata.lastModified),
|
|
92
|
+
}),
|
|
93
|
+
name: workspaceBasename(path),
|
|
94
|
+
path,
|
|
95
|
+
size: metadata.size,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function logicalDirectory(path) {
|
|
99
|
+
const normalized = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
100
|
+
return {
|
|
101
|
+
kind: 'directory',
|
|
102
|
+
name: normalized.length === 0 ? '' : workspaceBasename(normalized),
|
|
103
|
+
path: normalized,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function segmentGlobMatches(pattern, value) {
|
|
107
|
+
let patternIndex = 0;
|
|
108
|
+
let valueIndex = 0;
|
|
109
|
+
let starIndex = -1;
|
|
110
|
+
let starValueIndex = -1;
|
|
111
|
+
while (valueIndex < value.length) {
|
|
112
|
+
if (patternIndex < pattern.length &&
|
|
113
|
+
(pattern[patternIndex] === '?' ||
|
|
114
|
+
pattern[patternIndex] === value[valueIndex])) {
|
|
115
|
+
patternIndex += 1;
|
|
116
|
+
valueIndex += 1;
|
|
117
|
+
}
|
|
118
|
+
else if (pattern[patternIndex] === '*') {
|
|
119
|
+
starIndex = patternIndex;
|
|
120
|
+
patternIndex += 1;
|
|
121
|
+
starValueIndex = valueIndex;
|
|
122
|
+
}
|
|
123
|
+
else if (starIndex !== -1) {
|
|
124
|
+
patternIndex = starIndex + 1;
|
|
125
|
+
starValueIndex += 1;
|
|
126
|
+
valueIndex = starValueIndex;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
while (pattern[patternIndex] === '*') {
|
|
133
|
+
patternIndex += 1;
|
|
134
|
+
}
|
|
135
|
+
return patternIndex === pattern.length;
|
|
136
|
+
}
|
|
137
|
+
function globMatches(pattern, value) {
|
|
138
|
+
const patternSegments = pattern.split('/');
|
|
139
|
+
const valueSegments = value.split('/');
|
|
140
|
+
let patternIndex = 0;
|
|
141
|
+
let valueIndex = 0;
|
|
142
|
+
let globstarIndex = -1;
|
|
143
|
+
let globstarValueIndex = -1;
|
|
144
|
+
while (valueIndex < valueSegments.length) {
|
|
145
|
+
const patternSegment = patternSegments[patternIndex];
|
|
146
|
+
const valueSegment = valueSegments[valueIndex];
|
|
147
|
+
if (patternSegment === '**') {
|
|
148
|
+
globstarIndex = patternIndex;
|
|
149
|
+
globstarValueIndex = valueIndex;
|
|
150
|
+
patternIndex += 1;
|
|
151
|
+
}
|
|
152
|
+
else if (patternSegment !== undefined &&
|
|
153
|
+
valueSegment !== undefined &&
|
|
154
|
+
segmentGlobMatches(patternSegment, valueSegment)) {
|
|
155
|
+
patternIndex += 1;
|
|
156
|
+
valueIndex += 1;
|
|
157
|
+
}
|
|
158
|
+
else if (globstarIndex !== -1) {
|
|
159
|
+
patternIndex = globstarIndex + 1;
|
|
160
|
+
globstarValueIndex += 1;
|
|
161
|
+
valueIndex = globstarValueIndex;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
while (patternSegments[patternIndex] === '**') {
|
|
168
|
+
patternIndex += 1;
|
|
169
|
+
}
|
|
170
|
+
return patternIndex === patternSegments.length;
|
|
171
|
+
}
|
|
172
|
+
function searchMatches(query, path, match, caseInsensitive) {
|
|
173
|
+
const candidate = caseInsensitive ? path.toLowerCase() : path;
|
|
174
|
+
const needle = caseInsensitive ? query.toLowerCase() : query;
|
|
175
|
+
if (match === 'exact') {
|
|
176
|
+
return candidate === needle;
|
|
177
|
+
}
|
|
178
|
+
if (match === 'substring') {
|
|
179
|
+
return candidate.includes(needle);
|
|
180
|
+
}
|
|
181
|
+
return globMatches(needle, candidate);
|
|
182
|
+
}
|
|
183
|
+
async function collectBoundedText(stream, announcedSize, maxBytes, path) {
|
|
184
|
+
if (announcedSize > maxBytes) {
|
|
185
|
+
await stream.cancel().catch(() => undefined);
|
|
186
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Workspace file "${path}" exceeds the ${maxBytes}-byte read limit.`, { path, permanent: true });
|
|
187
|
+
}
|
|
188
|
+
const reader = stream.getReader();
|
|
189
|
+
const chunks = [];
|
|
190
|
+
let size = 0;
|
|
191
|
+
try {
|
|
192
|
+
while (true) {
|
|
193
|
+
const result = await reader.read();
|
|
194
|
+
if (result.done) {
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
if (result.value.byteLength === 0) {
|
|
198
|
+
throw workspaceError(StorageErrorCode.PROVIDER, 'Storage provider returned an empty non-terminal body chunk.', { path, permanent: true });
|
|
199
|
+
}
|
|
200
|
+
size += result.value.byteLength;
|
|
201
|
+
if (size > maxBytes) {
|
|
202
|
+
await reader.cancel().catch(() => undefined);
|
|
203
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Workspace file "${path}" exceeded the ${maxBytes}-byte read limit.`, { path, permanent: true });
|
|
204
|
+
}
|
|
205
|
+
chunks.push(result.value);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
reader.releaseLock();
|
|
210
|
+
}
|
|
211
|
+
const bytes = new Uint8Array(size);
|
|
212
|
+
let offset = 0;
|
|
213
|
+
for (const chunk of chunks) {
|
|
214
|
+
bytes.set(chunk, offset);
|
|
215
|
+
offset += chunk.byteLength;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
return decoder.decode(bytes);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, `Workspace file "${path}" is not valid UTF-8 text.`, { path, permanent: true });
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
async function collectBoundedBytes(stream, announcedSize, maxBytes, path) {
|
|
225
|
+
if (announcedSize > maxBytes) {
|
|
226
|
+
await stream.cancel().catch(() => undefined);
|
|
227
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Workspace file "${path}" exceeds the ${maxBytes}-byte copy limit.`, { path, permanent: true });
|
|
228
|
+
}
|
|
229
|
+
const reader = stream.getReader();
|
|
230
|
+
const chunks = [];
|
|
231
|
+
let size = 0;
|
|
232
|
+
try {
|
|
233
|
+
while (true) {
|
|
234
|
+
const result = await reader.read();
|
|
235
|
+
if (result.done) {
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
if (result.value.byteLength === 0) {
|
|
239
|
+
throw workspaceError(StorageErrorCode.PROVIDER, 'Storage provider returned an empty non-terminal body chunk.', { path, permanent: true });
|
|
240
|
+
}
|
|
241
|
+
size += result.value.byteLength;
|
|
242
|
+
if (size > maxBytes) {
|
|
243
|
+
await reader.cancel().catch(() => undefined);
|
|
244
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Workspace file "${path}" exceeded the ${maxBytes}-byte copy limit.`, { path, permanent: true });
|
|
245
|
+
}
|
|
246
|
+
chunks.push(result.value);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
finally {
|
|
250
|
+
reader.releaseLock();
|
|
251
|
+
}
|
|
252
|
+
const bytes = new Uint8Array(size);
|
|
253
|
+
let offset = 0;
|
|
254
|
+
for (const chunk of chunks) {
|
|
255
|
+
bytes.set(chunk, offset);
|
|
256
|
+
offset += chunk.byteLength;
|
|
257
|
+
}
|
|
258
|
+
return bytes;
|
|
259
|
+
}
|
|
260
|
+
class StorageWorkspaceImplementation {
|
|
261
|
+
#state;
|
|
262
|
+
#permissions;
|
|
263
|
+
#limits;
|
|
264
|
+
/** @internal Construct workspaces through {@link mountStorageWorkspace}. */
|
|
265
|
+
constructor(constructorToken, state, permissions, limits) {
|
|
266
|
+
if (constructorToken !== WORKSPACE_CONSTRUCTOR) {
|
|
267
|
+
throw workspaceError(StorageErrorCode.UNAUTHORIZED, 'StorageWorkspace must be created by mountStorageWorkspace().', { permanent: true });
|
|
268
|
+
}
|
|
269
|
+
this.#state = state;
|
|
270
|
+
this.#permissions = permissions;
|
|
271
|
+
this.#limits = limits;
|
|
272
|
+
}
|
|
273
|
+
get permissions() {
|
|
274
|
+
return new Set(this.#permissions);
|
|
275
|
+
}
|
|
276
|
+
get limits() {
|
|
277
|
+
return Object.freeze({ ...this.#limits });
|
|
278
|
+
}
|
|
279
|
+
allows(permission) {
|
|
280
|
+
return this.#permissions.has(permission);
|
|
281
|
+
}
|
|
282
|
+
async stat(path, options) {
|
|
283
|
+
this.#require('read');
|
|
284
|
+
const logicalPath = this.#filePath(path);
|
|
285
|
+
try {
|
|
286
|
+
const metadata = await this.#state.client.head(this.#scope(logicalPath), options);
|
|
287
|
+
this.#assertResultPath(metadata.key, logicalPath);
|
|
288
|
+
return logicalFile(metadata, logicalPath);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
throw sanitizeWorkspaceError(error, {
|
|
292
|
+
operation: 'stat',
|
|
293
|
+
path: logicalPath,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
async readText(path, options) {
|
|
298
|
+
this.#require('read');
|
|
299
|
+
const logicalPath = this.#filePath(path);
|
|
300
|
+
const maxBytes = options?.maxBytes ?? this.#limits.maxReadBytes;
|
|
301
|
+
positiveSafeInteger(maxBytes, 'maxBytes');
|
|
302
|
+
if (maxBytes > this.#limits.maxReadBytes) {
|
|
303
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `maxBytes cannot exceed the ${this.#limits.maxReadBytes}-byte workspace read limit.`, { path: logicalPath, permanent: true });
|
|
304
|
+
}
|
|
305
|
+
try {
|
|
306
|
+
const object = await this.#state.client.downloadStream(this.#scope(logicalPath), operationOptions(options));
|
|
307
|
+
this.#assertResultPath(object.key, logicalPath);
|
|
308
|
+
const text = await collectBoundedText(object.body, object.size, maxBytes, logicalPath);
|
|
309
|
+
return { ...logicalFile(object, logicalPath), text };
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
throw sanitizeWorkspaceError(error, {
|
|
313
|
+
operation: 'read',
|
|
314
|
+
path: logicalPath,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async list(options = {}) {
|
|
319
|
+
this.#require('list');
|
|
320
|
+
const continuation = options.cursor
|
|
321
|
+
? this.#state.cursorStore.consume(options.cursor, this.#binding('list'))
|
|
322
|
+
: undefined;
|
|
323
|
+
if (continuation !== undefined &&
|
|
324
|
+
((options.directory !== undefined &&
|
|
325
|
+
options.directory !== continuation.directory) ||
|
|
326
|
+
(options.limit !== undefined && options.limit !== continuation.limit) ||
|
|
327
|
+
(options.recursive !== undefined &&
|
|
328
|
+
options.recursive !== continuation.recursive))) {
|
|
329
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Workspace cursor conflicts with the supplied list query.', { permanent: true });
|
|
330
|
+
}
|
|
331
|
+
const directory = this.#directoryPath(continuation?.directory ?? options.directory ?? '');
|
|
332
|
+
const limit = this.#pageLimit(continuation?.limit ?? options.limit, this.#limits.maxPageSize);
|
|
333
|
+
const recursive = continuation?.recursive ?? options.recursive === true;
|
|
334
|
+
const binding = this.#binding('list');
|
|
335
|
+
const scopedDirectory = this.#scopeDirectory(directory);
|
|
336
|
+
try {
|
|
337
|
+
const page = await this.#state.client.list({
|
|
338
|
+
...(continuation !== undefined && {
|
|
339
|
+
cursor: continuation.backendCursor,
|
|
340
|
+
}),
|
|
341
|
+
...(!recursive && { delimiter: '/' }),
|
|
342
|
+
limit,
|
|
343
|
+
...operationOptions(options),
|
|
344
|
+
prefix: scopedDirectory,
|
|
345
|
+
});
|
|
346
|
+
if (page.items.length + (page.prefixes?.length ?? 0) > limit) {
|
|
347
|
+
this.#malformedPage();
|
|
348
|
+
}
|
|
349
|
+
if (page.cursor !== undefined && page.cursor.length === 0) {
|
|
350
|
+
this.#malformedPage();
|
|
351
|
+
}
|
|
352
|
+
if (page.cursor !== undefined &&
|
|
353
|
+
(page.items.length + (page.prefixes?.length ?? 0) === 0 ||
|
|
354
|
+
page.cursor === continuation?.backendCursor)) {
|
|
355
|
+
this.#malformedPage();
|
|
356
|
+
}
|
|
357
|
+
const entries = [];
|
|
358
|
+
for (const item of page.items) {
|
|
359
|
+
const path = this.#unscoped(item.key);
|
|
360
|
+
if (!isPathInside(directory, path)) {
|
|
361
|
+
this.#providerScopeFailure();
|
|
362
|
+
}
|
|
363
|
+
if (!recursive) {
|
|
364
|
+
const relative = directory.length === 0 ? path : path.slice(directory.length + 1);
|
|
365
|
+
if (relative.includes('/')) {
|
|
366
|
+
this.#malformedPage();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
entries.push(logicalFile(item, path));
|
|
370
|
+
}
|
|
371
|
+
for (const prefix of page.prefixes ?? []) {
|
|
372
|
+
const path = this.#unscoped(prefix.endsWith('/') ? prefix.slice(0, -1) : prefix);
|
|
373
|
+
if (!isPathInside(directory, path)) {
|
|
374
|
+
this.#providerScopeFailure();
|
|
375
|
+
}
|
|
376
|
+
if (!recursive) {
|
|
377
|
+
const relative = directory.length === 0 ? path : path.slice(directory.length + 1);
|
|
378
|
+
if (relative.includes('/')) {
|
|
379
|
+
this.#malformedPage();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
entries.push(logicalDirectory(path));
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
...(page.cursor !== undefined && {
|
|
386
|
+
cursor: this.#state.cursorStore.issue(binding, {
|
|
387
|
+
backendCursor: page.cursor,
|
|
388
|
+
directory,
|
|
389
|
+
limit,
|
|
390
|
+
recursive,
|
|
391
|
+
}, this.#limits.cursorTtlMs),
|
|
392
|
+
}),
|
|
393
|
+
entries,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
throw sanitizeWorkspaceError(error, {
|
|
398
|
+
operation: 'list',
|
|
399
|
+
...(directory.length > 0 && { path: directory }),
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async search(query, options = {}) {
|
|
404
|
+
this.#require('search');
|
|
405
|
+
const continuation = options.cursor
|
|
406
|
+
? this.#state.cursorStore.consume(options.cursor, this.#binding('search'))
|
|
407
|
+
: undefined;
|
|
408
|
+
if (continuation !== undefined &&
|
|
409
|
+
((query.length > 0 && query !== continuation.query) ||
|
|
410
|
+
(options.directory !== undefined &&
|
|
411
|
+
options.directory !== continuation.directory) ||
|
|
412
|
+
(options.match !== undefined && options.match !== continuation.match) ||
|
|
413
|
+
(options.caseInsensitive !== undefined &&
|
|
414
|
+
options.caseInsensitive !== continuation.caseInsensitive) ||
|
|
415
|
+
(options.limit !== undefined && options.limit !== continuation.limit))) {
|
|
416
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Workspace cursor conflicts with the supplied search query.', { permanent: true });
|
|
417
|
+
}
|
|
418
|
+
query = continuation?.query ?? query;
|
|
419
|
+
if (typeof query !== 'string' || query.length === 0) {
|
|
420
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Search query must be a non-empty string.', { permanent: true });
|
|
421
|
+
}
|
|
422
|
+
if (query.includes('\\') || containsControlCharacter(query)) {
|
|
423
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Search query contains forbidden characters.', { permanent: true });
|
|
424
|
+
}
|
|
425
|
+
if (encoder.encode(query).byteLength > this.#limits.maxPathBytes) {
|
|
426
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, 'Search query exceeds the workspace path-byte limit.', { permanent: true });
|
|
427
|
+
}
|
|
428
|
+
const directory = this.#directoryPath(continuation?.directory ?? options.directory ?? '');
|
|
429
|
+
const match = continuation?.match ?? options.match ?? 'glob';
|
|
430
|
+
if (match !== 'glob' && match !== 'substring' && match !== 'exact') {
|
|
431
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Search match must be glob, substring, or exact.', { permanent: true });
|
|
432
|
+
}
|
|
433
|
+
const caseInsensitive = continuation?.caseInsensitive ?? options.caseInsensitive === true;
|
|
434
|
+
const limit = this.#pageLimit(continuation?.limit ?? options.limit, this.#limits.maxSearchResults);
|
|
435
|
+
const binding = this.#binding('search');
|
|
436
|
+
const prior = continuation ?? {
|
|
437
|
+
backendCursor: undefined,
|
|
438
|
+
caseInsensitive,
|
|
439
|
+
directory,
|
|
440
|
+
limit,
|
|
441
|
+
match,
|
|
442
|
+
query,
|
|
443
|
+
scanned: 0,
|
|
444
|
+
};
|
|
445
|
+
return this.#searchPage(query, {
|
|
446
|
+
binding,
|
|
447
|
+
caseInsensitive,
|
|
448
|
+
directory,
|
|
449
|
+
limit,
|
|
450
|
+
match,
|
|
451
|
+
operation: operationOptions(options),
|
|
452
|
+
state: prior,
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
async writeFile(path, body, options) {
|
|
456
|
+
this.#require(options.mode);
|
|
457
|
+
const logicalPath = this.#filePath(path);
|
|
458
|
+
if (typeof body !== 'string' && !(body instanceof Uint8Array)) {
|
|
459
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'Workspace writes accept only strings or Uint8Array bodies.', { path: logicalPath, permanent: true });
|
|
460
|
+
}
|
|
461
|
+
const size = typeof body === 'string'
|
|
462
|
+
? encoder.encode(body).byteLength
|
|
463
|
+
: body.byteLength;
|
|
464
|
+
if (size > this.#limits.maxWriteBytes) {
|
|
465
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Workspace write exceeds the ${this.#limits.maxWriteBytes}-byte limit.`, { path: logicalPath, permanent: true });
|
|
466
|
+
}
|
|
467
|
+
if (options.mode === 'replace') {
|
|
468
|
+
assertEtag(options.etag, this.#limits.maxPathBytes, 'Replace');
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
const common = {
|
|
472
|
+
...(options.contentType !== undefined && {
|
|
473
|
+
contentType: options.contentType,
|
|
474
|
+
}),
|
|
475
|
+
...(options.metadata !== undefined && { metadata: options.metadata }),
|
|
476
|
+
...operationOptions(options),
|
|
477
|
+
};
|
|
478
|
+
const result = options.mode === 'create'
|
|
479
|
+
? await this.#state.client.uploadConditional(this.#scope(logicalPath), body, { ...common, condition: { type: 'create' } })
|
|
480
|
+
: await this.#state.client.uploadConditional(this.#scope(logicalPath), body, {
|
|
481
|
+
...common,
|
|
482
|
+
condition: { etag: options.etag, type: 'replace' },
|
|
483
|
+
});
|
|
484
|
+
this.#assertResultPath(result.key, logicalPath);
|
|
485
|
+
return logicalFile(result, logicalPath);
|
|
486
|
+
}
|
|
487
|
+
catch (error) {
|
|
488
|
+
throw sanitizeWorkspaceError(error, {
|
|
489
|
+
operation: options.mode,
|
|
490
|
+
path: logicalPath,
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async copyFile(source, destination, options) {
|
|
495
|
+
this.#require('copy');
|
|
496
|
+
this.#require('read');
|
|
497
|
+
this.#require('create');
|
|
498
|
+
const sourcePath = this.#filePath(source, 'source path');
|
|
499
|
+
const destinationPath = this.#filePath(destination, 'destination path');
|
|
500
|
+
if (sourcePath === destinationPath) {
|
|
501
|
+
throw workspaceError(StorageErrorCode.CONFLICT, 'Copy destination must differ from its source.', { path: destinationPath, permanent: true });
|
|
502
|
+
}
|
|
503
|
+
if (this.#state.client.capabilities.conditionalMutation?.create !== true) {
|
|
504
|
+
throw workspaceError(StorageErrorCode.NOT_SUPPORTED, 'Safe copy requires create-only conditional upload support.', { operation: 'copy', path: destinationPath, permanent: true });
|
|
505
|
+
}
|
|
506
|
+
return this.#copyCreate(sourcePath, destinationPath, options);
|
|
507
|
+
}
|
|
508
|
+
async moveFile(source, destination, options) {
|
|
509
|
+
this.#require('move');
|
|
510
|
+
this.#require('read');
|
|
511
|
+
this.#require('create');
|
|
512
|
+
this.#require('delete');
|
|
513
|
+
const conditional = this.#state.client.capabilities.conditionalMutation;
|
|
514
|
+
if (conditional?.create !== true ||
|
|
515
|
+
conditional.delete !== true ||
|
|
516
|
+
conditional.etag !== true) {
|
|
517
|
+
throw workspaceError(StorageErrorCode.NOT_SUPPORTED, 'Safe move requires create-only upload, conditional delete, and upload ETag support.', { operation: 'move', permanent: true });
|
|
518
|
+
}
|
|
519
|
+
const sourcePath = this.#filePath(source, 'source path');
|
|
520
|
+
const destinationPath = this.#filePath(destination, 'destination path');
|
|
521
|
+
if (sourcePath === destinationPath) {
|
|
522
|
+
throw workspaceError(StorageErrorCode.CONFLICT, 'Move destination must differ from its source.', { path: destinationPath, permanent: true });
|
|
523
|
+
}
|
|
524
|
+
assertEtag(options.etag, this.#limits.maxPathBytes, 'Move');
|
|
525
|
+
const copied = await this.#copyCreate(sourcePath, destinationPath, options);
|
|
526
|
+
if (copied.etag === undefined) {
|
|
527
|
+
throw workspaceError(StorageErrorCode.NOT_SUPPORTED, 'Safe move requires the destination provider to return an etag.', { path: destinationPath, permanent: true });
|
|
528
|
+
}
|
|
529
|
+
try {
|
|
530
|
+
await this.#state.client.deleteConditional(this.#scope(sourcePath), {
|
|
531
|
+
condition: { etag: options.etag },
|
|
532
|
+
...operationOptions(options),
|
|
533
|
+
});
|
|
534
|
+
return copied;
|
|
535
|
+
}
|
|
536
|
+
catch {
|
|
537
|
+
throw workspaceError(StorageErrorCode.CONFLICT, `Workspace move could not confirm source deletion; destination "${destinationPath}" was retained. Inspect both paths before retrying.`, {
|
|
538
|
+
operation: 'move',
|
|
539
|
+
path: destinationPath,
|
|
540
|
+
permanent: true,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async deleteFile(path, options) {
|
|
545
|
+
this.#require('delete');
|
|
546
|
+
const logicalPath = this.#filePath(path);
|
|
547
|
+
assertEtag(options.etag, this.#limits.maxPathBytes, 'Delete');
|
|
548
|
+
try {
|
|
549
|
+
await this.#state.client.deleteConditional(this.#scope(logicalPath), {
|
|
550
|
+
condition: { etag: options.etag },
|
|
551
|
+
...operationOptions(options),
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
catch (error) {
|
|
555
|
+
throw sanitizeWorkspaceError(error, {
|
|
556
|
+
operation: 'delete',
|
|
557
|
+
path: logicalPath,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
mount(directory, options = {}) {
|
|
562
|
+
const path = this.#directoryPath(directory);
|
|
563
|
+
if (path.length === 0) {
|
|
564
|
+
throw workspaceError(StorageErrorCode.INVALID_ARGUMENT, 'A child workspace mount directory must not be empty.', { permanent: true });
|
|
565
|
+
}
|
|
566
|
+
return new StorageWorkspaceImplementation(WORKSPACE_CONSTRUCTOR, {
|
|
567
|
+
...this.#state,
|
|
568
|
+
id: `${this.#state.id}/${path}`,
|
|
569
|
+
prefix: joinWorkspacePath(this.#state.prefix, path),
|
|
570
|
+
}, resolvePermissions(options.permissions, this.#permissions), resolveLimits(options.limits, this.#limits));
|
|
571
|
+
}
|
|
572
|
+
async #copyCreate(sourcePath, destinationPath, options) {
|
|
573
|
+
try {
|
|
574
|
+
const source = await this.#state.client.downloadStream(this.#scope(sourcePath), operationOptions(options));
|
|
575
|
+
this.#assertResultPath(source.key, sourcePath);
|
|
576
|
+
const bytes = await collectBoundedBytes(source.body, source.size, this.#limits.maxWriteBytes, sourcePath);
|
|
577
|
+
const result = await this.#state.client.uploadConditional(this.#scope(destinationPath), bytes, {
|
|
578
|
+
condition: { type: 'create' },
|
|
579
|
+
contentType: source.contentType,
|
|
580
|
+
...(source.metadata !== undefined && { metadata: source.metadata }),
|
|
581
|
+
...operationOptions(options),
|
|
582
|
+
});
|
|
583
|
+
this.#assertResultPath(result.key, destinationPath);
|
|
584
|
+
return logicalFile(result, destinationPath);
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
throw sanitizeWorkspaceError(error, {
|
|
588
|
+
operation: 'copy',
|
|
589
|
+
path: destinationPath,
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
async #searchPage(query, context) {
|
|
594
|
+
const entries = [];
|
|
595
|
+
let backendCursor = context.state.backendCursor;
|
|
596
|
+
let scanned = context.state.scanned;
|
|
597
|
+
try {
|
|
598
|
+
while (entries.length < context.limit &&
|
|
599
|
+
scanned < this.#limits.maxSearchScan) {
|
|
600
|
+
const remaining = this.#limits.maxSearchScan - scanned;
|
|
601
|
+
const remainingResults = context.limit - entries.length;
|
|
602
|
+
const page = await this.#state.client.list({
|
|
603
|
+
...(backendCursor !== undefined && { cursor: backendCursor }),
|
|
604
|
+
limit: Math.min(this.#limits.maxPageSize, remaining, remainingResults),
|
|
605
|
+
...context.operation,
|
|
606
|
+
prefix: this.#scopeDirectory(context.directory),
|
|
607
|
+
});
|
|
608
|
+
const requested = Math.min(this.#limits.maxPageSize, remaining, remainingResults);
|
|
609
|
+
if (page.items.length > requested) {
|
|
610
|
+
this.#malformedPage();
|
|
611
|
+
}
|
|
612
|
+
if ((page.prefixes?.length ?? 0) > 0) {
|
|
613
|
+
this.#malformedPage();
|
|
614
|
+
}
|
|
615
|
+
if (page.cursor !== undefined && page.cursor.length === 0) {
|
|
616
|
+
this.#malformedPage();
|
|
617
|
+
}
|
|
618
|
+
if (page.cursor !== undefined &&
|
|
619
|
+
(page.items.length === 0 || page.cursor === backendCursor)) {
|
|
620
|
+
this.#malformedPage();
|
|
621
|
+
}
|
|
622
|
+
for (let index = 0; index < page.items.length; index += 1) {
|
|
623
|
+
const item = page.items[index];
|
|
624
|
+
if (item === undefined) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
scanned += 1;
|
|
628
|
+
const path = this.#unscoped(item.key);
|
|
629
|
+
if (!isPathInside(context.directory, path)) {
|
|
630
|
+
this.#providerScopeFailure();
|
|
631
|
+
}
|
|
632
|
+
if (searchMatches(query, context.directory.length === 0
|
|
633
|
+
? path
|
|
634
|
+
: path.slice(context.directory.length + 1), context.match, context.caseInsensitive)) {
|
|
635
|
+
entries.push(logicalFile(item, path));
|
|
636
|
+
}
|
|
637
|
+
if (entries.length === context.limit ||
|
|
638
|
+
scanned === this.#limits.maxSearchScan) {
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
backendCursor = page.cursor;
|
|
643
|
+
if (backendCursor === undefined) {
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const hasMore = backendCursor !== undefined;
|
|
648
|
+
if (scanned >= this.#limits.maxSearchScan && hasMore) {
|
|
649
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `Search exceeded the ${this.#limits.maxSearchScan}-object scan limit.`, { permanent: true });
|
|
650
|
+
}
|
|
651
|
+
return {
|
|
652
|
+
...(hasMore && {
|
|
653
|
+
cursor: this.#state.cursorStore.issue(context.binding, {
|
|
654
|
+
backendCursor,
|
|
655
|
+
caseInsensitive: context.caseInsensitive,
|
|
656
|
+
directory: context.directory,
|
|
657
|
+
limit: context.limit,
|
|
658
|
+
match: context.match,
|
|
659
|
+
query,
|
|
660
|
+
scanned,
|
|
661
|
+
}, this.#limits.cursorTtlMs),
|
|
662
|
+
}),
|
|
663
|
+
entries,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
catch (error) {
|
|
667
|
+
throw sanitizeWorkspaceError(error, {
|
|
668
|
+
operation: 'search',
|
|
669
|
+
...(context.directory.length > 0 && { path: context.directory }),
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
#require(permission) {
|
|
674
|
+
if (!this.#permissions.has(permission)) {
|
|
675
|
+
throw workspaceError(StorageErrorCode.UNAUTHORIZED, `Workspace does not allow ${permission} operations.`, { permanent: true });
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
#filePath(path, label = 'path') {
|
|
679
|
+
return assertWorkspacePath(path, this.#limits.maxPathBytes, {
|
|
680
|
+
allowRoot: false,
|
|
681
|
+
label,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
#directoryPath(path) {
|
|
685
|
+
return assertWorkspacePath(path, this.#limits.maxPathBytes, {
|
|
686
|
+
allowRoot: true,
|
|
687
|
+
label: 'directory',
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
#scope(path) {
|
|
691
|
+
return `${this.#state.prefix}/${path}`;
|
|
692
|
+
}
|
|
693
|
+
#scopeDirectory(directory) {
|
|
694
|
+
return directory.length === 0
|
|
695
|
+
? `${this.#state.prefix}/`
|
|
696
|
+
: `${this.#state.prefix}/${directory}/`;
|
|
697
|
+
}
|
|
698
|
+
#unscoped(key) {
|
|
699
|
+
const scoped = `${this.#state.prefix}/`;
|
|
700
|
+
if (!key.startsWith(scoped)) {
|
|
701
|
+
this.#providerScopeFailure();
|
|
702
|
+
}
|
|
703
|
+
const path = key.slice(scoped.length);
|
|
704
|
+
return assertWorkspacePath(path, this.#limits.maxPathBytes, {
|
|
705
|
+
allowRoot: false,
|
|
706
|
+
label: 'backend result path',
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
#assertResultPath(key, expected) {
|
|
710
|
+
if (this.#unscoped(key) !== expected) {
|
|
711
|
+
this.#providerScopeFailure();
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
#providerScopeFailure() {
|
|
715
|
+
throw workspaceError(StorageErrorCode.PROVIDER, 'Storage provider returned data outside the mounted workspace.', { permanent: true });
|
|
716
|
+
}
|
|
717
|
+
#malformedPage() {
|
|
718
|
+
throw workspaceError(StorageErrorCode.PROVIDER, 'Storage provider returned a malformed workspace page.', { permanent: true });
|
|
719
|
+
}
|
|
720
|
+
#pageLimit(value, maximum) {
|
|
721
|
+
const limit = value ?? maximum;
|
|
722
|
+
positiveSafeInteger(limit, 'limit');
|
|
723
|
+
if (limit > maximum) {
|
|
724
|
+
throw workspaceError(StorageErrorCode.LIMIT_EXCEEDED, `limit cannot exceed the workspace maximum of ${maximum}.`, { permanent: true });
|
|
725
|
+
}
|
|
726
|
+
return limit;
|
|
727
|
+
}
|
|
728
|
+
#binding(operation) {
|
|
729
|
+
return `${this.#state.id}:${operation}`;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
export function mountStorageWorkspace(client, options) {
|
|
733
|
+
const limits = resolveLimits(options.limits);
|
|
734
|
+
const prefix = assertWorkspacePath(options.prefix, limits.maxPathBytes, {
|
|
735
|
+
allowRoot: false,
|
|
736
|
+
label: 'workspace prefix',
|
|
737
|
+
});
|
|
738
|
+
return new StorageWorkspaceImplementation(WORKSPACE_CONSTRUCTOR, {
|
|
739
|
+
client,
|
|
740
|
+
cursorStore: new StorageWorkspaceCursorStore(),
|
|
741
|
+
id: `workspace:${crypto.randomUUID()}`,
|
|
742
|
+
prefix,
|
|
743
|
+
}, resolvePermissions(options.permissions), limits);
|
|
744
|
+
}
|
|
745
|
+
/** @deprecated Prefer {@link mountStorageWorkspace}. */
|
|
746
|
+
export const createStorageWorkspace = mountStorageWorkspace;
|
|
747
|
+
//# sourceMappingURL=storage-workspace.js.map
|