@tangle-network/agent-app 0.60.54 → 0.60.56

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.
Files changed (38) hide show
  1. package/dist/assistant/index.js +5 -5
  2. package/dist/chat-react/index.js +1 -1
  3. package/dist/chat-routes/index.js +9 -9
  4. package/dist/chat-store/index.js +2 -2
  5. package/dist/{chunk-WKD4E52F.js → chunk-2YLSWZ5W.js} +4 -4
  6. package/dist/chunk-6DOQ6T2O.js +227 -0
  7. package/dist/chunk-6DOQ6T2O.js.map +1 -0
  8. package/dist/{chunk-EE7SZFS2.js → chunk-6LPQRKH7.js} +2 -2
  9. package/dist/{chunk-JSDTYUKF.js → chunk-CZ7KLS7N.js} +12 -12
  10. package/dist/object-store/index.js +11 -213
  11. package/dist/object-store/index.js.map +1 -1
  12. package/dist/public-consultation/index.js +6 -6
  13. package/dist/stream/index.js +17 -17
  14. package/dist/teams/invitations-api.js +3 -3
  15. package/dist/turn-health/index.js +35 -3
  16. package/dist/turn-health/index.js.map +1 -1
  17. package/dist/turn-health/sweep.d.ts +19 -1
  18. package/dist/web-react/index.js +8 -8
  19. package/dist/workspace-export/checksum.d.ts +15 -0
  20. package/dist/workspace-export/d1.d.ts +56 -0
  21. package/dist/workspace-export/engine.d.ts +51 -0
  22. package/dist/workspace-export/index.d.ts +16 -0
  23. package/dist/workspace-export/index.js +1438 -0
  24. package/dist/workspace-export/index.js.map +1 -0
  25. package/dist/workspace-export/react.d.ts +16 -0
  26. package/dist/workspace-export/react.js +158 -0
  27. package/dist/workspace-export/react.js.map +1 -0
  28. package/dist/workspace-export/routes.d.ts +53 -0
  29. package/dist/workspace-export/secrets.d.ts +41 -0
  30. package/dist/workspace-export/sources.d.ts +59 -0
  31. package/dist/workspace-export/storage.d.ts +59 -0
  32. package/dist/workspace-export/tar.d.ts +14 -0
  33. package/dist/workspace-export/types.d.ts +136 -0
  34. package/dist/workspace-export/zip.d.ts +26 -0
  35. package/package.json +11 -1
  36. /package/dist/{chunk-WKD4E52F.js.map → chunk-2YLSWZ5W.js.map} +0 -0
  37. /package/dist/{chunk-EE7SZFS2.js.map → chunk-6LPQRKH7.js.map} +0 -0
  38. /package/dist/{chunk-JSDTYUKF.js.map → chunk-CZ7KLS7N.js.map} +0 -0
@@ -0,0 +1,1438 @@
1
+ import {
2
+ assertSafeKeySegment,
3
+ signObjectUrl,
4
+ verifyObjectUrl
5
+ } from "../chunk-6DOQ6T2O.js";
6
+ import "../chunk-S5SRJJQG.js";
7
+
8
+ // src/workspace-export/checksum.ts
9
+ var CRC_TABLE = (() => {
10
+ const table = new Uint32Array(256);
11
+ for (let n = 0; n < 256; n += 1) {
12
+ let c = n;
13
+ for (let k = 0; k < 8; k += 1) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
14
+ table[n] = c >>> 0;
15
+ }
16
+ return table;
17
+ })();
18
+ function crc32Update(crc, bytes) {
19
+ let c = (crc ^ 4294967295) >>> 0;
20
+ for (let i = 0; i < bytes.length; i += 1) c = (CRC_TABLE[(c ^ bytes[i]) & 255] ^ c >>> 8) >>> 0;
21
+ return (c ^ 4294967295) >>> 0;
22
+ }
23
+ function toHex(bytes) {
24
+ let out = "";
25
+ for (const b of bytes) out += b.toString(16).padStart(2, "0");
26
+ return out;
27
+ }
28
+ function createSha256() {
29
+ const DigestStream = globalThis.crypto.DigestStream;
30
+ if (DigestStream) {
31
+ const stream = new DigestStream("SHA-256");
32
+ const writer = stream.getWriter();
33
+ return {
34
+ update: (bytes) => writer.write(bytes),
35
+ async digestHex() {
36
+ await writer.close();
37
+ return toHex(new Uint8Array(await stream.digest));
38
+ }
39
+ };
40
+ }
41
+ const pure = new PureSha256();
42
+ return {
43
+ async update(bytes) {
44
+ pure.update(bytes);
45
+ },
46
+ async digestHex() {
47
+ return toHex(pure.digest());
48
+ }
49
+ };
50
+ }
51
+ var K = new Uint32Array([
52
+ 1116352408,
53
+ 1899447441,
54
+ 3049323471,
55
+ 3921009573,
56
+ 961987163,
57
+ 1508970993,
58
+ 2453635748,
59
+ 2870763221,
60
+ 3624381080,
61
+ 310598401,
62
+ 607225278,
63
+ 1426881987,
64
+ 1925078388,
65
+ 2162078206,
66
+ 2614888103,
67
+ 3248222580,
68
+ 3835390401,
69
+ 4022224774,
70
+ 264347078,
71
+ 604807628,
72
+ 770255983,
73
+ 1249150122,
74
+ 1555081692,
75
+ 1996064986,
76
+ 2554220882,
77
+ 2821834349,
78
+ 2952996808,
79
+ 3210313671,
80
+ 3336571891,
81
+ 3584528711,
82
+ 113926993,
83
+ 338241895,
84
+ 666307205,
85
+ 773529912,
86
+ 1294757372,
87
+ 1396182291,
88
+ 1695183700,
89
+ 1986661051,
90
+ 2177026350,
91
+ 2456956037,
92
+ 2730485921,
93
+ 2820302411,
94
+ 3259730800,
95
+ 3345764771,
96
+ 3516065817,
97
+ 3600352804,
98
+ 4094571909,
99
+ 275423344,
100
+ 430227734,
101
+ 506948616,
102
+ 659060556,
103
+ 883997877,
104
+ 958139571,
105
+ 1322822218,
106
+ 1537002063,
107
+ 1747873779,
108
+ 1955562222,
109
+ 2024104815,
110
+ 2227730452,
111
+ 2361852424,
112
+ 2428436474,
113
+ 2756734187,
114
+ 3204031479,
115
+ 3329325298
116
+ ]);
117
+ var PureSha256 = class {
118
+ h = new Uint32Array([
119
+ 1779033703,
120
+ 3144134277,
121
+ 1013904242,
122
+ 2773480762,
123
+ 1359893119,
124
+ 2600822924,
125
+ 528734635,
126
+ 1541459225
127
+ ]);
128
+ block = new Uint8Array(64);
129
+ blockLength = 0;
130
+ totalLength = 0;
131
+ w = new Uint32Array(64);
132
+ update(bytes) {
133
+ this.totalLength += bytes.length;
134
+ let offset = 0;
135
+ while (offset < bytes.length) {
136
+ const take = Math.min(64 - this.blockLength, bytes.length - offset);
137
+ this.block.set(bytes.subarray(offset, offset + take), this.blockLength);
138
+ this.blockLength += take;
139
+ offset += take;
140
+ if (this.blockLength === 64) {
141
+ this.compress(this.block);
142
+ this.blockLength = 0;
143
+ }
144
+ }
145
+ }
146
+ digest() {
147
+ const bitLength = this.totalLength * 8;
148
+ const padLength = this.blockLength < 56 ? 56 - this.blockLength : 120 - this.blockLength;
149
+ const pad = new Uint8Array(padLength + 8);
150
+ pad[0] = 128;
151
+ const view = new DataView(pad.buffer);
152
+ view.setUint32(padLength, Math.floor(bitLength / 4294967296));
153
+ view.setUint32(padLength + 4, bitLength >>> 0);
154
+ const total = this.totalLength;
155
+ this.update(pad);
156
+ this.totalLength = total;
157
+ const out = new Uint8Array(32);
158
+ const outView = new DataView(out.buffer);
159
+ for (let i = 0; i < 8; i += 1) outView.setUint32(i * 4, this.h[i]);
160
+ return out;
161
+ }
162
+ compress(chunk) {
163
+ const w = this.w;
164
+ const view = new DataView(chunk.buffer, chunk.byteOffset, 64);
165
+ for (let i = 0; i < 16; i += 1) w[i] = view.getUint32(i * 4);
166
+ for (let i = 16; i < 64; i += 1) {
167
+ const a2 = w[i - 15];
168
+ const b2 = w[i - 2];
169
+ const s0 = (a2 >>> 7 | a2 << 25) ^ (a2 >>> 18 | a2 << 14) ^ a2 >>> 3;
170
+ const s1 = (b2 >>> 17 | b2 << 15) ^ (b2 >>> 19 | b2 << 13) ^ b2 >>> 10;
171
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
172
+ }
173
+ let [a, b, c, d, e, f, g, h] = this.h;
174
+ for (let i = 0; i < 64; i += 1) {
175
+ const S1 = (e >>> 6 | e << 26) ^ (e >>> 11 | e << 21) ^ (e >>> 25 | e << 7);
176
+ const ch = e & f ^ ~e & g;
177
+ const t1 = h + S1 + ch + K[i] + w[i] >>> 0;
178
+ const S0 = (a >>> 2 | a << 30) ^ (a >>> 13 | a << 19) ^ (a >>> 22 | a << 10);
179
+ const maj = a & b ^ a & c ^ b & c;
180
+ const t2 = S0 + maj >>> 0;
181
+ h = g;
182
+ g = f;
183
+ f = e;
184
+ e = d + t1 >>> 0;
185
+ d = c;
186
+ c = b;
187
+ b = a;
188
+ a = t1 + t2 >>> 0;
189
+ }
190
+ this.h[0] = this.h[0] + a >>> 0;
191
+ this.h[1] = this.h[1] + b >>> 0;
192
+ this.h[2] = this.h[2] + c >>> 0;
193
+ this.h[3] = this.h[3] + d >>> 0;
194
+ this.h[4] = this.h[4] + e >>> 0;
195
+ this.h[5] = this.h[5] + f >>> 0;
196
+ this.h[6] = this.h[6] + g >>> 0;
197
+ this.h[7] = this.h[7] + h >>> 0;
198
+ }
199
+ };
200
+
201
+ // src/workspace-export/secrets.ts
202
+ var REDACTED = "[redacted]";
203
+ var MASK_BYTE = 42;
204
+ var CREDENTIAL_PATTERNS = [
205
+ String.raw`sk-ant-[A-Za-z0-9_-]{16,}`,
206
+ String.raw`sk-(?:proj-|tan-|or-v1-|svcacct-)?[A-Za-z0-9_-]{20,}`,
207
+ String.raw`(?:sk|rk)_(?:live|test|sb)_[A-Za-z0-9_]{12,}`,
208
+ String.raw`whsec_[A-Za-z0-9+/=]{16,}`,
209
+ String.raw`gh[pousr]_[A-Za-z0-9]{30,}`,
210
+ String.raw`github_pat_[A-Za-z0-9_]{40,}`,
211
+ String.raw`xox[abposr]-[A-Za-z0-9-]{10,}`,
212
+ String.raw`AKIA[0-9A-Z]{16}`,
213
+ String.raw`AIza[0-9A-Za-z_-]{35}`,
214
+ String.raw`ya29\.[0-9A-Za-z_-]{20,}`,
215
+ String.raw`glpat-[A-Za-z0-9_-]{20,}`,
216
+ String.raw`npm_[A-Za-z0-9]{36}`,
217
+ String.raw`hf_[A-Za-z0-9]{30,}`,
218
+ String.raw`r8_[A-Za-z0-9]{30,}`,
219
+ String.raw`gsk_[A-Za-z0-9]{40,}`,
220
+ String.raw`xai-[A-Za-z0-9]{40,}`,
221
+ String.raw`lak_[A-Za-z0-9_-]{16,}`,
222
+ String.raw`eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}`,
223
+ String.raw`-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[A-Za-z0-9+/=\r\n\t ]*(?:-----END [A-Z0-9 ]*PRIVATE KEY-----)?`
224
+ ];
225
+ var SECRET_NAME = /(^|_)(secret|secrets|password|passwd|passphrase|token|credential|credentials|ciphertext|encrypted|bearer|apikey|privatekey|cookie)(_|$)|api_?key|private_?key|(key|secret|token|password|challenge|state|nonce|credential)_(hash|digest)$/;
226
+ var TOKEN_METRIC = /token_(count|counts|usage|budget|limit|cost)$|^(input|output|total|max|prompt|completion|reasoning|cached)_tokens?$/;
227
+ function snake(name) {
228
+ return name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[-\s.]+/g, "_").toLowerCase();
229
+ }
230
+ function isSecretName(name) {
231
+ const folded = snake(name);
232
+ if (TOKEN_METRIC.test(folded)) return false;
233
+ return SECRET_NAME.test(folded);
234
+ }
235
+ function escapeRegExp(value) {
236
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
237
+ }
238
+ function createSecretScanner(options = {}) {
239
+ const known = [...new Set(options.knownSecrets ?? [])].filter((s) => typeof s === "string" && s.length >= 8).sort((a, b) => b.length - a.length).map(escapeRegExp);
240
+ const source = [...known, ...CREDENTIAL_PATTERNS].join("|");
241
+ const pattern = () => new RegExp(source, "g");
242
+ function maskText(text) {
243
+ let count = 0;
244
+ const out = text.replace(pattern(), () => {
245
+ count += 1;
246
+ return REDACTED;
247
+ });
248
+ return { text: out, count };
249
+ }
250
+ function redactJson(value, depth) {
251
+ if (typeof value === "string") {
252
+ const masked = maskText(value);
253
+ return { value: masked.text, count: masked.count };
254
+ }
255
+ if (depth > 64 || value === null || typeof value !== "object") return { value, count: 0 };
256
+ let count = 0;
257
+ if (Array.isArray(value)) {
258
+ const out2 = value.map((item) => {
259
+ const result = redactJson(item, depth + 1);
260
+ count += result.count;
261
+ return result.value;
262
+ });
263
+ return { value: out2, count };
264
+ }
265
+ const out = {};
266
+ for (const [key, item] of Object.entries(value)) {
267
+ if (isSecretName(key) && item !== null && item !== "" && typeof item !== "boolean") {
268
+ out[key] = REDACTED;
269
+ count += 1;
270
+ continue;
271
+ }
272
+ const result = redactJson(item, depth + 1);
273
+ out[key] = result.value;
274
+ count += result.count;
275
+ }
276
+ return { value: out, count };
277
+ }
278
+ function redactValue(value) {
279
+ if (typeof value === "string") {
280
+ const trimmed = value.trimStart();
281
+ if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
282
+ try {
283
+ const parsed = JSON.parse(value);
284
+ const result = redactJson(parsed, 0);
285
+ return result.count ? { value: JSON.stringify(result.value), count: result.count } : { value, count: 0 };
286
+ } catch {
287
+ }
288
+ }
289
+ const masked = maskText(value);
290
+ return { value: masked.text, count: masked.count };
291
+ }
292
+ if (value instanceof Uint8Array || value instanceof ArrayBuffer) return { value, count: 0 };
293
+ return redactJson(value, 0);
294
+ }
295
+ function maskBytes(onMask) {
296
+ const window = 8192;
297
+ const maxCarry = 65536;
298
+ let carry = new Uint8Array(0);
299
+ function scan(buffer, final) {
300
+ let text = "";
301
+ for (let i = 0; i < buffer.length; i += 8192) {
302
+ text += String.fromCharCode(...buffer.subarray(i, i + 8192));
303
+ }
304
+ const regex = pattern();
305
+ let emitEnd = final ? buffer.length : Math.max(0, buffer.length - window);
306
+ let masked = 0;
307
+ for (let match = regex.exec(text); match; match = regex.exec(text)) {
308
+ const start = match.index;
309
+ const end = start + match[0].length;
310
+ if (match[0].length === 0) {
311
+ regex.lastIndex += 1;
312
+ continue;
313
+ }
314
+ if (!final && end === buffer.length) {
315
+ emitEnd = Math.min(emitEnd, start);
316
+ break;
317
+ }
318
+ buffer.fill(MASK_BYTE, start, end);
319
+ masked += 1;
320
+ }
321
+ if (masked) onMask(masked);
322
+ if (!final && buffer.length - emitEnd > maxCarry) emitEnd = buffer.length - window;
323
+ return emitEnd;
324
+ }
325
+ return new TransformStream({
326
+ transform(chunk, controller) {
327
+ const buffer = new Uint8Array(carry.length + chunk.length);
328
+ buffer.set(carry, 0);
329
+ buffer.set(chunk, carry.length);
330
+ const emitEnd = scan(buffer, false);
331
+ if (emitEnd > 0) controller.enqueue(buffer.slice(0, emitEnd));
332
+ carry = buffer.slice(emitEnd);
333
+ },
334
+ flush(controller) {
335
+ if (!carry.length) return;
336
+ scan(carry, true);
337
+ controller.enqueue(carry);
338
+ }
339
+ });
340
+ }
341
+ return { maskText, redactValue, maskBytes };
342
+ }
343
+ var CREDENTIAL_FILES = [
344
+ /(^|\/)\.git-credentials$/,
345
+ /(^|\/)\.netrc$/,
346
+ /(^|\/)\.pgpass$/,
347
+ /(^|\/)\.npmrc$/,
348
+ /(^|\/)\.pypirc$/,
349
+ /(^|\/)\.docker\/config\.json$/,
350
+ /(^|\/)\.aws\/credentials$/,
351
+ /(^|\/)\.kube\/config$/,
352
+ /(^|\/)\.config\/gh\/hosts\.ya?ml$/,
353
+ /(^|\/)\.codex\/auth\.json$/,
354
+ /(^|\/)\.claude\/\.credentials\.json$/,
355
+ /(^|\/)opencode\/auth\.json$/,
356
+ /(^|\/)\.ssh\/id_[^/]*$/
357
+ ];
358
+ function isCredentialFile(path) {
359
+ const normalized = path.replace(/^\.\//, "");
360
+ if (normalized.endsWith(".pub")) return false;
361
+ return CREDENTIAL_FILES.some((pattern) => pattern.test(normalized));
362
+ }
363
+
364
+ // src/workspace-export/tar.ts
365
+ var BLOCK = 512;
366
+ var MAX_NAME_RECORD = 1 << 20;
367
+ function readString(block, start, length) {
368
+ let end = start;
369
+ while (end < start + length && block[end] !== 0) end += 1;
370
+ return new TextDecoder().decode(block.subarray(start, end));
371
+ }
372
+ function readSize(block) {
373
+ if (block[124] & 128) {
374
+ let size = 0;
375
+ for (let i = 125; i < 136; i += 1) size = size * 256 + block[i];
376
+ return size;
377
+ }
378
+ const octal = readString(block, 124, 12).trim();
379
+ return octal ? Number.parseInt(octal, 8) : 0;
380
+ }
381
+ function paxPath(record) {
382
+ for (const line of record.split("\n")) {
383
+ const space = line.indexOf(" ");
384
+ if (space < 0) continue;
385
+ const field = line.slice(space + 1);
386
+ if (field.startsWith("path=")) return field.slice(5);
387
+ }
388
+ return void 0;
389
+ }
390
+ function createTarInspector(stats) {
391
+ const header = new Uint8Array(BLOCK);
392
+ let headerFill = 0;
393
+ let dataRemaining = 0;
394
+ let paddingRemaining = 0;
395
+ let blanking = false;
396
+ let capture = null;
397
+ let pendingName;
398
+ let ended = false;
399
+ function onHeader() {
400
+ if (header.every((b) => b === 0)) {
401
+ ended = true;
402
+ return;
403
+ }
404
+ const type = String.fromCharCode(header[156]);
405
+ const size = readSize(header);
406
+ dataRemaining = size;
407
+ paddingRemaining = (BLOCK - size % BLOCK) % BLOCK;
408
+ blanking = false;
409
+ capture = null;
410
+ if ((type === "L" || type === "x") && size <= MAX_NAME_RECORD) {
411
+ capture = { kind: type, parts: [], length: 0 };
412
+ return;
413
+ }
414
+ const prefix = header[257] === 117 ? readString(header, 345, 155) : "";
415
+ const base = readString(header, 0, 100);
416
+ const name = pendingName ?? (prefix ? `${prefix}/${base}` : base);
417
+ pendingName = void 0;
418
+ if (type === "0" || type === "\0" || type === "7") {
419
+ stats.files += 1;
420
+ stats.bytes += size;
421
+ if (isCredentialFile(name)) {
422
+ blanking = true;
423
+ stats.blanked.push(name);
424
+ }
425
+ }
426
+ }
427
+ return new TransformStream({
428
+ transform(chunk, controller) {
429
+ const out = chunk.slice();
430
+ let at = 0;
431
+ while (at < out.length && !ended) {
432
+ if (dataRemaining > 0) {
433
+ const take2 = Math.min(dataRemaining, out.length - at);
434
+ if (blanking) out.fill(42, at, at + take2);
435
+ if (capture) {
436
+ capture.parts.push(out.slice(at, at + take2));
437
+ capture.length += take2;
438
+ }
439
+ dataRemaining -= take2;
440
+ at += take2;
441
+ if (dataRemaining === 0 && capture) {
442
+ const joined = new Uint8Array(capture.length);
443
+ let offset = 0;
444
+ for (const part of capture.parts) {
445
+ joined.set(part, offset);
446
+ offset += part.length;
447
+ }
448
+ const text = new TextDecoder().decode(joined);
449
+ pendingName = capture.kind === "L" ? text.replace(/\0+$/, "") : paxPath(text) ?? pendingName;
450
+ capture = null;
451
+ }
452
+ continue;
453
+ }
454
+ if (paddingRemaining > 0) {
455
+ const take2 = Math.min(paddingRemaining, out.length - at);
456
+ paddingRemaining -= take2;
457
+ at += take2;
458
+ continue;
459
+ }
460
+ const take = Math.min(BLOCK - headerFill, out.length - at);
461
+ header.set(out.subarray(at, at + take), headerFill);
462
+ headerFill += take;
463
+ at += take;
464
+ if (headerFill === BLOCK) {
465
+ headerFill = 0;
466
+ onHeader();
467
+ }
468
+ }
469
+ controller.enqueue(out);
470
+ }
471
+ });
472
+ }
473
+
474
+ // src/workspace-export/zip.ts
475
+ var U32_MAX = 4294967295;
476
+ var U16_MAX = 65535;
477
+ function dosDateTime(date) {
478
+ const year = Math.max(1980, date.getUTCFullYear());
479
+ return {
480
+ time: date.getUTCHours() << 11 | date.getUTCMinutes() << 5 | Math.floor(date.getUTCSeconds() / 2),
481
+ date: year - 1980 << 9 | date.getUTCMonth() + 1 << 5 | date.getUTCDate()
482
+ };
483
+ }
484
+ function setU64(view, offset, value) {
485
+ view.setUint32(offset, value % 4294967296, true);
486
+ view.setUint32(offset + 4, Math.floor(value / 4294967296), true);
487
+ }
488
+ function localHeader(entry, name) {
489
+ const zip64 = entry.compressedSize >= U32_MAX || entry.uncompressedSize >= U32_MAX;
490
+ const extraLength = zip64 ? 20 : 0;
491
+ const out = new Uint8Array(30 + name.length + extraLength);
492
+ const view = new DataView(out.buffer);
493
+ const { time, date } = dosDateTime(entry.modifiedAt);
494
+ view.setUint32(0, 67324752, true);
495
+ view.setUint16(4, zip64 ? 45 : 20, true);
496
+ view.setUint16(6, 2048, true);
497
+ view.setUint16(8, entry.method, true);
498
+ view.setUint16(10, time, true);
499
+ view.setUint16(12, date, true);
500
+ view.setUint32(14, entry.crc32, true);
501
+ view.setUint32(18, zip64 ? U32_MAX : entry.compressedSize, true);
502
+ view.setUint32(22, zip64 ? U32_MAX : entry.uncompressedSize, true);
503
+ view.setUint16(26, name.length, true);
504
+ view.setUint16(28, extraLength, true);
505
+ out.set(name, 30);
506
+ if (zip64) {
507
+ const at = 30 + name.length;
508
+ view.setUint16(at, 1, true);
509
+ view.setUint16(at + 2, 16, true);
510
+ setU64(view, at + 4, entry.uncompressedSize);
511
+ setU64(view, at + 12, entry.compressedSize);
512
+ }
513
+ return out;
514
+ }
515
+ function centralHeader(entry, layout) {
516
+ const bigU = entry.uncompressedSize >= U32_MAX;
517
+ const bigC = entry.compressedSize >= U32_MAX;
518
+ const bigO = layout.offset >= U32_MAX;
519
+ const extraFields = (bigU ? 1 : 0) + (bigC ? 1 : 0) + (bigO ? 1 : 0);
520
+ const extraLength = extraFields ? 4 + extraFields * 8 : 0;
521
+ const name = layout.name;
522
+ const out = new Uint8Array(46 + name.length + extraLength);
523
+ const view = new DataView(out.buffer);
524
+ const { time, date } = dosDateTime(entry.modifiedAt);
525
+ const version = extraFields ? 45 : 20;
526
+ view.setUint32(0, 33639248, true);
527
+ view.setUint16(4, 3 << 8 | version, true);
528
+ view.setUint16(6, version, true);
529
+ view.setUint16(8, 2048, true);
530
+ view.setUint16(10, entry.method, true);
531
+ view.setUint16(12, time, true);
532
+ view.setUint16(14, date, true);
533
+ view.setUint32(16, entry.crc32, true);
534
+ view.setUint32(20, bigC ? U32_MAX : entry.compressedSize, true);
535
+ view.setUint32(24, bigU ? U32_MAX : entry.uncompressedSize, true);
536
+ view.setUint16(28, name.length, true);
537
+ view.setUint16(30, extraLength, true);
538
+ view.setUint32(38, 33188 << 16 >>> 0, true);
539
+ view.setUint32(42, bigO ? U32_MAX : layout.offset, true);
540
+ out.set(name, 46);
541
+ if (extraFields) {
542
+ let at = 46 + name.length;
543
+ view.setUint16(at, 1, true);
544
+ view.setUint16(at + 2, extraFields * 8, true);
545
+ at += 4;
546
+ if (bigU) {
547
+ setU64(view, at, entry.uncompressedSize);
548
+ at += 8;
549
+ }
550
+ if (bigC) {
551
+ setU64(view, at, entry.compressedSize);
552
+ at += 8;
553
+ }
554
+ if (bigO) setU64(view, at, layout.offset);
555
+ }
556
+ return out;
557
+ }
558
+ function endRecords(count, cdOffset, cdSize) {
559
+ const zip64 = count >= U16_MAX || cdOffset >= U32_MAX || cdSize >= U32_MAX;
560
+ const out = new Uint8Array((zip64 ? 56 + 20 : 0) + 22);
561
+ const view = new DataView(out.buffer);
562
+ let at = 0;
563
+ if (zip64) {
564
+ view.setUint32(0, 101075792, true);
565
+ setU64(view, 4, 44);
566
+ view.setUint16(12, 3 << 8 | 45, true);
567
+ view.setUint16(14, 45, true);
568
+ setU64(view, 24, count);
569
+ setU64(view, 32, count);
570
+ setU64(view, 40, cdSize);
571
+ setU64(view, 48, cdOffset);
572
+ view.setUint32(56, 117853008, true);
573
+ setU64(view, 64, cdOffset + cdSize);
574
+ view.setUint32(72, 1, true);
575
+ at = 76;
576
+ }
577
+ view.setUint32(at, 101010256, true);
578
+ view.setUint16(at + 8, zip64 ? U16_MAX : count, true);
579
+ view.setUint16(at + 10, zip64 ? U16_MAX : count, true);
580
+ view.setUint32(at + 12, zip64 ? U32_MAX : cdSize, true);
581
+ view.setUint32(at + 16, zip64 ? U32_MAX : cdOffset, true);
582
+ return out;
583
+ }
584
+ function createZipStream(entries) {
585
+ const encoder = new TextEncoder();
586
+ const layouts = [];
587
+ let offset = 0;
588
+ for (const entry of entries) {
589
+ const name = encoder.encode(entry.name);
590
+ const header = localHeader(entry, name);
591
+ layouts.push({ name, localHeader: header, offset });
592
+ offset += header.length + entry.compressedSize;
593
+ }
594
+ const central = entries.map((entry, i) => centralHeader(entry, layouts[i]));
595
+ const cdSize = central.reduce((sum, part) => sum + part.length, 0);
596
+ const end = endRecords(entries.length, offset, cdSize);
597
+ const length = offset + cdSize + end.length;
598
+ let index = 0;
599
+ let phase = "entries";
600
+ let reader = null;
601
+ let written = 0;
602
+ const stream = new ReadableStream({
603
+ async pull(controller) {
604
+ if (phase === "entries") {
605
+ if (reader) {
606
+ const { done, value } = await reader.read();
607
+ if (!done) {
608
+ written += value.length;
609
+ controller.enqueue(value);
610
+ return;
611
+ }
612
+ reader = null;
613
+ const expected = entries[index].compressedSize;
614
+ if (written !== expected) {
615
+ controller.error(new Error(`zip entry ${entries[index].name} produced ${written} bytes, expected ${expected}`));
616
+ return;
617
+ }
618
+ index += 1;
619
+ }
620
+ if (index < entries.length) {
621
+ controller.enqueue(layouts[index].localHeader);
622
+ written = 0;
623
+ reader = (await entries[index].open()).getReader();
624
+ return;
625
+ }
626
+ phase = "tail";
627
+ }
628
+ if (phase === "tail") {
629
+ for (const part of central) controller.enqueue(part);
630
+ controller.enqueue(end);
631
+ phase = "done";
632
+ controller.close();
633
+ }
634
+ },
635
+ async cancel(reason) {
636
+ await reader?.cancel(reason);
637
+ }
638
+ });
639
+ return { length, stream };
640
+ }
641
+
642
+ // src/workspace-export/engine.ts
643
+ var MANIFEST_SCHEMA = "tangle.workspace-export/v1";
644
+ var SEGMENT_BYTES = 8 * 1024 * 1024;
645
+ var LEASE_MS = 5 * 6e4;
646
+ var MAX_ATTEMPTS = 3;
647
+ var SOURCE_NAME = /^[a-z0-9][a-z0-9_.-]{0,95}$/;
648
+ var LeaseLost = class extends Error {
649
+ };
650
+ function iso(ms) {
651
+ return new Date(ms).toISOString();
652
+ }
653
+ function errorText(scanner, err) {
654
+ const raw = err instanceof Error ? err.message : String(err);
655
+ return scanner.maskText(raw).text.slice(0, 300);
656
+ }
657
+ function toStream(body) {
658
+ if (body instanceof ReadableStream) return body;
659
+ const bytes = typeof body === "string" ? new TextEncoder().encode(body) : body;
660
+ return new ReadableStream({
661
+ start(controller) {
662
+ if (bytes.length) controller.enqueue(bytes);
663
+ controller.close();
664
+ }
665
+ });
666
+ }
667
+ function concatenate(parts, length) {
668
+ const out = new Uint8Array(length);
669
+ let offset = 0;
670
+ for (const part of parts) {
671
+ out.set(part, offset);
672
+ offset += part.length;
673
+ }
674
+ return out;
675
+ }
676
+ function safePath(path, used) {
677
+ const parts = path.replace(/\\/g, "/").split("/").filter((p) => p && p !== "." && p !== "..").map((p) => p.replace(/[\0-\x1f]/g, "_"));
678
+ let candidate = parts.join("/") || "file";
679
+ let n = 1;
680
+ while (used.has(candidate)) {
681
+ n += 1;
682
+ candidate = `${parts.join("/") || "file"}~${n}`;
683
+ }
684
+ used.add(candidate);
685
+ return candidate;
686
+ }
687
+ function jsonCell(value) {
688
+ if (typeof value === "bigint") return value.toString();
689
+ if (value instanceof ArrayBuffer) value = new Uint8Array(value);
690
+ if (value instanceof Uint8Array) {
691
+ let bin = "";
692
+ for (const b of value) bin += String.fromCharCode(b);
693
+ return { $base64: btoa(bin) };
694
+ }
695
+ return value;
696
+ }
697
+ function createExportEngine(options) {
698
+ const { storage } = options;
699
+ const prefix = options.prefix ?? "workspace-exports";
700
+ const now = options.now ?? (() => Date.now());
701
+ const base = (workspaceId, id) => `${prefix}/${assertSafeKeySegment(workspaceId)}/${assertSafeKeySegment(id)}`;
702
+ const jobKey = (workspaceId, id) => `${base(workspaceId, id)}/job.json`;
703
+ const archiveKey = (workspaceId, id) => `${base(workspaceId, id)}/archive.zip`;
704
+ async function loadJob(workspaceId, id) {
705
+ if (!/^[a-z0-9-]{8,64}$/.test(id)) return null;
706
+ const object = await storage.get(jobKey(workspaceId, id));
707
+ if (!object) return null;
708
+ const job = JSON.parse(await new Response(object.body).text());
709
+ if (job.workspaceId !== workspaceId) return null;
710
+ return { job, etag: object.etag };
711
+ }
712
+ async function saveJob(job, etag) {
713
+ job.updatedAt = iso(now());
714
+ const result = await storage.put(jobKey(job.workspaceId, job.id), new TextEncoder().encode(JSON.stringify(job)), {
715
+ ifMatch: etag
716
+ });
717
+ if (!result) throw new LeaseLost("export job changed underneath this request");
718
+ return result.etag;
719
+ }
720
+ async function writeEntry(keyBase, path, input, compress) {
721
+ let crc = 0;
722
+ let size = 0;
723
+ const sha = createSha256();
724
+ let stream = input.pipeThrough(
725
+ new TransformStream({
726
+ async transform(chunk, controller) {
727
+ crc = crc32Update(crc, chunk);
728
+ size += chunk.length;
729
+ await sha.update(chunk);
730
+ controller.enqueue(chunk);
731
+ }
732
+ })
733
+ );
734
+ if (compress) stream = stream.pipeThrough(new CompressionStream("deflate-raw"));
735
+ const segments = [];
736
+ let pending = [];
737
+ let pendingLength = 0;
738
+ let compressedSize = 0;
739
+ const flush = async () => {
740
+ if (!pendingLength) return;
741
+ const key = `${keyBase}/${segments.length}`;
742
+ await storage.put(key, concatenate(pending, pendingLength));
743
+ segments.push({ key, size: pendingLength });
744
+ compressedSize += pendingLength;
745
+ pending = [];
746
+ pendingLength = 0;
747
+ };
748
+ const reader = stream.getReader();
749
+ for (; ; ) {
750
+ const { done, value } = await reader.read();
751
+ if (done) break;
752
+ pending.push(value);
753
+ pendingLength += value.length;
754
+ if (pendingLength >= SEGMENT_BYTES) await flush();
755
+ }
756
+ await flush();
757
+ return {
758
+ path,
759
+ method: compress ? 8 : 0,
760
+ crc32: crc,
761
+ sha256: await sha.digestHex(),
762
+ size,
763
+ compressedSize,
764
+ segments
765
+ };
766
+ }
767
+ function redactRow(row, omit, scanner) {
768
+ const out = {};
769
+ let count = 0;
770
+ for (const [column, raw] of Object.entries(row)) {
771
+ if (omit.has(column)) continue;
772
+ if (isSecretName(column)) {
773
+ if (raw === null || raw === void 0 || raw === "") out[column] = raw ?? null;
774
+ else {
775
+ out[column] = REDACTED;
776
+ count += 1;
777
+ }
778
+ continue;
779
+ }
780
+ const result = scanner.redactValue(jsonCell(raw));
781
+ out[column] = result.value;
782
+ count += result.count;
783
+ }
784
+ return { row: out, count };
785
+ }
786
+ async function runUnit(job, unit, index, source, scanner) {
787
+ const keyBase = `${base(job.workspaceId, job.id)}/u/${index}`;
788
+ unit.entries = [];
789
+ unit.redactions = 0;
790
+ const encoder = new TextEncoder();
791
+ if (source.kind === "rows") {
792
+ const omit = new Set(source.omitColumns ?? []);
793
+ unit.expectedRows = source.expectedCount ? await source.expectedCount() : void 0;
794
+ let rows = 0;
795
+ const iterator = source.rows()[Symbol.asyncIterator]();
796
+ const body = new ReadableStream({
797
+ async pull(controller) {
798
+ const { done, value } = await iterator.next();
799
+ if (done) {
800
+ controller.close();
801
+ return;
802
+ }
803
+ const page = Array.isArray(value) ? value : [value];
804
+ let text = "";
805
+ for (const row of page) {
806
+ const redacted = redactRow(row, omit, scanner);
807
+ unit.redactions += redacted.count;
808
+ text += `${JSON.stringify(redacted.row)}
809
+ `;
810
+ rows += 1;
811
+ }
812
+ if (text) controller.enqueue(encoder.encode(text));
813
+ },
814
+ async cancel() {
815
+ await iterator.return?.();
816
+ }
817
+ });
818
+ unit.entries.push(await writeEntry(`${keyBase}/0`, `data/${source.name}.ndjson`, body, true));
819
+ unit.rows = rows;
820
+ unit.status = "done";
821
+ return;
822
+ }
823
+ if (source.kind === "json") {
824
+ const result = scanner.redactValue(await source.value());
825
+ unit.redactions += result.count;
826
+ const text = `${JSON.stringify(result.value, null, 2)}
827
+ `;
828
+ unit.entries.push(await writeEntry(`${keyBase}/0`, `data/${source.name}.json`, toStream(text), true));
829
+ unit.status = "done";
830
+ return;
831
+ }
832
+ if (source.kind === "files") {
833
+ const used = /* @__PURE__ */ new Set();
834
+ let files = 0;
835
+ for await (const file of source.files()) {
836
+ const body = await file.open();
837
+ if (body === null) continue;
838
+ const path = `files/${source.name}/${safePath(file.path, used)}`;
839
+ const masked = toStream(body).pipeThrough(scanner.maskBytes((n) => unit.redactions += n));
840
+ unit.entries.push(await writeEntry(`${keyBase}/${files}`, path, masked, false));
841
+ files += 1;
842
+ }
843
+ unit.files = files;
844
+ unit.status = files ? "done" : "empty";
845
+ return;
846
+ }
847
+ const archive = await source.open();
848
+ if (!archive) {
849
+ unit.status = "empty";
850
+ return;
851
+ }
852
+ const stats = { files: 0, bytes: 0, blanked: [] };
853
+ const tarGz = archive.pipeThrough(new DecompressionStream("gzip")).pipeThrough(createTarInspector(stats)).pipeThrough(scanner.maskBytes((n) => unit.redactions += n)).pipeThrough(new CompressionStream("gzip"));
854
+ unit.entries.push(await writeEntry(`${keyBase}/0`, `sandbox/${source.name}.tar.gz`, tarGz, false));
855
+ unit.archive = stats;
856
+ unit.files = stats.files;
857
+ unit.status = "done";
858
+ }
859
+ function manifest(job, notes) {
860
+ const classes = {};
861
+ for (const unit of job.units) {
862
+ for (const entry of unit.entries) (classes[unit.dataClass] ??= []).push(entry.path);
863
+ }
864
+ return {
865
+ schema: MANIFEST_SCHEMA,
866
+ app: options.app,
867
+ workspaceId: job.workspaceId,
868
+ exportId: job.id,
869
+ requestedBy: job.requestedBy,
870
+ createdAt: job.createdAt,
871
+ completedAt: job.completedAt,
872
+ secretPolicy: `Credential columns and JSON keys are replaced with "[redacted]". Credential-shaped values and the product's own keys are masked in rows and files. Credential files inside sandbox archives are overwritten with "*". Connections are exported as metadata only.`,
873
+ totals: {
874
+ units: job.units.length,
875
+ files: job.units.reduce((n, u) => n + u.entries.length, 0),
876
+ rows: job.units.reduce((n, u) => n + (u.rows ?? 0), 0),
877
+ bytes: job.units.reduce((n, u) => n + u.entries.reduce((m, e) => m + e.size, 0), 0),
878
+ redactions: job.units.reduce((n, u) => n + u.redactions, 0)
879
+ },
880
+ classes,
881
+ sources: job.units.map((u) => ({
882
+ name: u.name,
883
+ kind: u.kind,
884
+ dataClass: u.dataClass,
885
+ description: u.description,
886
+ status: u.status,
887
+ rows: u.rows,
888
+ expectedRows: u.expectedRows,
889
+ files: u.files,
890
+ archive: u.archive,
891
+ redactions: u.redactions
892
+ })),
893
+ files: job.units.flatMap(
894
+ (u) => u.entries.map((e) => ({ path: e.path, size: e.size, sha256: e.sha256, crc32: e.crc32.toString(16).padStart(8, "0") }))
895
+ ),
896
+ notes
897
+ };
898
+ }
899
+ function readme(job) {
900
+ return [
901
+ `${options.app} workspace export`,
902
+ "",
903
+ `Workspace: ${job.workspaceId}`,
904
+ `Export: ${job.id}`,
905
+ `Created: ${job.createdAt}`,
906
+ "",
907
+ "manifest.json what this archive contains: every file with its size and SHA-256, row counts per source, and the redaction policy",
908
+ "data/*.ndjson one JSON object per line, one file per table or record type",
909
+ "data/*.json single documents",
910
+ "files/<source>/ uploaded and generated files, at their original paths",
911
+ "sandbox/*.tar.gz the agent workspace filesystem; extract with `tar -xzf`",
912
+ "",
913
+ "Verify a file: `shasum -a 256 <path>` and compare with manifest.json.",
914
+ 'Secrets are never exported. Credential fields read "[redacted]".',
915
+ ""
916
+ ].join("\n");
917
+ }
918
+ async function finalize(job, plan, scanner) {
919
+ job.completedAt = iso(now());
920
+ const notes = plan.notes ? scanner.redactValue(plan.notes).value : void 0;
921
+ const keyBase = `${base(job.workspaceId, job.id)}/meta`;
922
+ const manifestText = `${JSON.stringify(manifest(job, notes), null, 2)}
923
+ `;
924
+ job.meta = [
925
+ await writeEntry(`${keyBase}/0`, "manifest.json", toStream(manifestText), true),
926
+ await writeEntry(`${keyBase}/1`, "README.txt", toStream(readme(job)), true)
927
+ ];
928
+ job.status = "complete";
929
+ }
930
+ return {
931
+ archiveKey,
932
+ async list(workspaceId) {
933
+ const ids = /* @__PURE__ */ new Set();
934
+ let cursor;
935
+ do {
936
+ const page = await storage.list(`${prefix}/${assertSafeKeySegment(workspaceId)}/`, cursor);
937
+ for (const key of page.keys) if (key.endsWith("/job.json")) ids.add(key.split("/").at(-2));
938
+ cursor = page.cursor;
939
+ } while (cursor);
940
+ const jobs = [];
941
+ for (const id of [...ids].sort().reverse().slice(0, 10)) {
942
+ const loaded = await loadJob(workspaceId, id);
943
+ if (loaded) jobs.push(loaded.job);
944
+ }
945
+ return jobs;
946
+ },
947
+ async get(workspaceId, id) {
948
+ return (await loadJob(workspaceId, id))?.job ?? null;
949
+ },
950
+ /** Start an export, or return the one already running for this workspace. */
951
+ async start(workspaceId, requestedBy) {
952
+ const existing = (await this.list(workspaceId)).find(
953
+ (job2) => job2.status === "running" && now() - Date.parse(job2.updatedAt) < 15 * 6e4
954
+ );
955
+ if (existing) return existing;
956
+ const plan = await options.plan({ workspaceId });
957
+ const names = /* @__PURE__ */ new Set();
958
+ for (const source of plan.sources) {
959
+ if (!SOURCE_NAME.test(source.name)) throw new Error(`workspace-export: invalid source name "${source.name}"`);
960
+ if (names.has(source.name)) throw new Error(`workspace-export: duplicate source name "${source.name}"`);
961
+ names.add(source.name);
962
+ }
963
+ const created = now();
964
+ const job = {
965
+ v: 1,
966
+ id: `${created.toString(36)}-${crypto.randomUUID().slice(0, 8)}`,
967
+ workspaceId,
968
+ requestedBy,
969
+ createdAt: iso(created),
970
+ updatedAt: iso(created),
971
+ status: "running",
972
+ units: plan.sources.map((s) => ({
973
+ name: s.name,
974
+ kind: s.kind,
975
+ dataClass: s.dataClass,
976
+ description: s.description,
977
+ status: "pending",
978
+ attempts: 0,
979
+ entries: [],
980
+ redactions: 0
981
+ }))
982
+ };
983
+ await saveJob(job);
984
+ return job;
985
+ },
986
+ /**
987
+ * Run pending units until `budgetMs` passes. A unit that starts always
988
+ * finishes, so one call can run longer than the budget. Returns the job as
989
+ * stored; another request holding the lease leaves it unchanged.
990
+ */
991
+ async advance(workspaceId, id, budgetMs = 2e4) {
992
+ const loaded = await loadJob(workspaceId, id);
993
+ if (!loaded) return null;
994
+ const { job } = loaded;
995
+ if (job.status !== "running") return job;
996
+ if (job.lease && job.lease.until > now()) return job;
997
+ const deadline = now() + budgetMs;
998
+ let etag = loaded.etag;
999
+ try {
1000
+ job.lease = { id: crypto.randomUUID(), until: now() + LEASE_MS };
1001
+ etag = await saveJob(job, etag);
1002
+ const plan = await options.plan({ workspaceId });
1003
+ const scanner = createSecretScanner({ knownSecrets: await options.knownSecrets?.({ workspaceId }) ?? [] });
1004
+ const sources = new Map(plan.sources.map((s) => [s.name, s]));
1005
+ for (const [index, unit] of job.units.entries()) {
1006
+ if (unit.status !== "pending") continue;
1007
+ if (now() > deadline) break;
1008
+ const source = sources.get(unit.name);
1009
+ unit.attempts += 1;
1010
+ try {
1011
+ if (!source || source.kind !== unit.kind) throw new Error("source is no longer available");
1012
+ await runUnit(job, unit, index, source, scanner);
1013
+ unit.error = void 0;
1014
+ } catch (err) {
1015
+ unit.error = errorText(scanner, err);
1016
+ unit.entries = [];
1017
+ if (unit.attempts >= MAX_ATTEMPTS) {
1018
+ unit.status = "failed";
1019
+ job.status = "failed";
1020
+ job.error = `${unit.name}: ${unit.error}`;
1021
+ }
1022
+ job.lease = { id: job.lease.id, until: now() + LEASE_MS };
1023
+ etag = await saveJob(job, etag);
1024
+ break;
1025
+ }
1026
+ job.lease = { id: job.lease.id, until: now() + LEASE_MS };
1027
+ etag = await saveJob(job, etag);
1028
+ }
1029
+ if (job.status === "running" && job.units.every((u) => u.status === "done" || u.status === "empty")) {
1030
+ await finalize(job, plan, scanner);
1031
+ }
1032
+ job.lease = void 0;
1033
+ await saveJob(job, etag);
1034
+ return job;
1035
+ } catch (err) {
1036
+ if (err instanceof LeaseLost) return (await loadJob(workspaceId, id))?.job ?? null;
1037
+ throw err;
1038
+ }
1039
+ },
1040
+ progress(job) {
1041
+ const bytesOf = (u) => u.entries.reduce((n, e) => n + e.size, 0);
1042
+ return {
1043
+ id: job.id,
1044
+ status: job.status,
1045
+ createdAt: job.createdAt,
1046
+ completedAt: job.completedAt,
1047
+ error: job.error,
1048
+ unitsTotal: job.units.length,
1049
+ unitsDone: job.units.filter((u) => u.status !== "pending").length,
1050
+ current: job.status === "running" ? job.units.find((u) => u.status === "pending")?.name : void 0,
1051
+ bytes: job.units.reduce((n, u) => n + bytesOf(u), 0),
1052
+ units: job.units.map((u) => ({
1053
+ name: u.name,
1054
+ dataClass: u.dataClass,
1055
+ status: u.status,
1056
+ rows: u.rows,
1057
+ files: u.files,
1058
+ bytes: bytesOf(u),
1059
+ error: u.error
1060
+ }))
1061
+ };
1062
+ },
1063
+ /** The finished archive as one stream with an exact length. */
1064
+ zip(job) {
1065
+ if (job.status !== "complete" || !job.meta) throw new Error("workspace-export: export is not complete");
1066
+ const modifiedAt = new Date(job.completedAt ?? job.createdAt);
1067
+ const entries = [...job.meta, ...job.units.flatMap((u) => u.entries)].map((entry) => ({
1068
+ name: entry.path,
1069
+ method: entry.method,
1070
+ crc32: entry.crc32,
1071
+ compressedSize: entry.compressedSize,
1072
+ uncompressedSize: entry.size,
1073
+ modifiedAt,
1074
+ open: async () => {
1075
+ let index = 0;
1076
+ let reader = null;
1077
+ return new ReadableStream({
1078
+ async pull(controller) {
1079
+ for (; ; ) {
1080
+ if (!reader) {
1081
+ const segment = entry.segments[index];
1082
+ if (!segment) {
1083
+ controller.close();
1084
+ return;
1085
+ }
1086
+ index += 1;
1087
+ const object = await storage.get(segment.key);
1088
+ if (!object || object.size !== segment.size) {
1089
+ controller.error(new Error(`workspace-export: segment ${segment.key} is missing or changed`));
1090
+ return;
1091
+ }
1092
+ reader = object.body.getReader();
1093
+ }
1094
+ const { done, value } = await reader.read();
1095
+ if (!done) {
1096
+ controller.enqueue(value);
1097
+ return;
1098
+ }
1099
+ reader = null;
1100
+ }
1101
+ },
1102
+ async cancel(reason) {
1103
+ await reader?.cancel(reason);
1104
+ }
1105
+ });
1106
+ }
1107
+ }));
1108
+ const { length, stream } = createZipStream(entries);
1109
+ const date = (job.completedAt ?? job.createdAt).slice(0, 10);
1110
+ return { length, stream, filename: `${options.app}-workspace-export-${date}.zip` };
1111
+ },
1112
+ /** Delete exports older than `retentionMs`, and any export by id when given. */
1113
+ async prune(workspaceId, retentionMs, onlyId) {
1114
+ const cutoff = now() - retentionMs;
1115
+ const doomed = [];
1116
+ let cursor;
1117
+ const root = `${prefix}/${assertSafeKeySegment(workspaceId)}/`;
1118
+ do {
1119
+ const page = await storage.list(root, cursor);
1120
+ for (const key of page.keys) {
1121
+ const id = key.slice(root.length).split("/")[0] ?? "";
1122
+ const created = Number.parseInt(id.split("-")[0] ?? "", 36);
1123
+ if (onlyId ? id === onlyId : Number.isFinite(created) && created < cutoff) doomed.push(key);
1124
+ }
1125
+ cursor = page.cursor;
1126
+ } while (cursor);
1127
+ await storage.delete(doomed);
1128
+ return doomed.length;
1129
+ }
1130
+ };
1131
+ }
1132
+
1133
+ // src/workspace-export/routes.ts
1134
+ var NO_STORE = { "Cache-Control": "private, no-store" };
1135
+ function json(body, status = 200) {
1136
+ return Response.json(body, { status, headers: NO_STORE });
1137
+ }
1138
+ function refuse(status, code, message) {
1139
+ return json({ error: { code, message } }, status);
1140
+ }
1141
+ function sameOrigin(request) {
1142
+ const site = request.headers.get("Sec-Fetch-Site");
1143
+ if (site && site !== "same-origin" && site !== "none") return false;
1144
+ const origin = request.headers.get("Origin");
1145
+ if (origin && origin !== new URL(request.url).origin) return false;
1146
+ return true;
1147
+ }
1148
+ function createWorkspaceExport(options) {
1149
+ const engine = createExportEngine(options);
1150
+ const linkTtlMs = options.linkTtlMs ?? 15 * 6e4;
1151
+ const retentionMs = options.retentionMs ?? 7 * 24 * 60 * 6e4;
1152
+ const now = options.now ?? (() => Date.now());
1153
+ async function withProgress(job, mount) {
1154
+ const progress = engine.progress(job);
1155
+ if (job.status === "complete" && options.signingSecret) {
1156
+ const exp = now() + linkTtlMs;
1157
+ const query = await signObjectUrl({ key: engine.archiveKey(job.workspaceId, job.id), exp, secret: options.signingSecret });
1158
+ progress.downloadUrl = `${mount}/${job.id}/download${query}`;
1159
+ progress.downloadExpiresAt = new Date(exp).toISOString();
1160
+ }
1161
+ return progress;
1162
+ }
1163
+ async function owner(request, workspaceId) {
1164
+ let principal;
1165
+ try {
1166
+ principal = await options.authorize({ request, workspaceId });
1167
+ } catch {
1168
+ return refuse(503, "authorization_unavailable", "Could not check workspace access");
1169
+ }
1170
+ if (!principal) return refuse(403, "forbidden", "Workspace owner access is required");
1171
+ if (principal.role !== "owner") return refuse(403, "owner_required", "Only the workspace owner can export its data");
1172
+ return principal;
1173
+ }
1174
+ async function download(request, workspaceId, id) {
1175
+ const verified = await verifyObjectUrl(request, { secret: options.signingSecret });
1176
+ if (!verified.ok || verified.key !== engine.archiveKey(workspaceId, id)) {
1177
+ return refuse(403, "link_invalid", "This download link is invalid or has expired");
1178
+ }
1179
+ const job = await engine.get(workspaceId, id);
1180
+ if (!job || job.status !== "complete") return refuse(404, "not_found", "Export not found");
1181
+ const { length, stream, filename } = engine.zip(job);
1182
+ return new Response(stream, {
1183
+ headers: {
1184
+ ...NO_STORE,
1185
+ "Content-Type": "application/zip",
1186
+ "Content-Length": String(length),
1187
+ "Content-Disposition": `attachment; filename="${filename}"`,
1188
+ "X-Content-Type-Options": "nosniff"
1189
+ }
1190
+ });
1191
+ }
1192
+ async function route(request, args) {
1193
+ const { workspaceId } = args;
1194
+ const path = args.path.replace(/^\/+|\/+$/g, "");
1195
+ const url = new URL(request.url);
1196
+ const mount = path ? url.pathname.slice(0, url.pathname.length - path.length).replace(/\/+$/, "") : url.pathname.replace(/\/+$/, "");
1197
+ const [id, action, extra] = path ? path.split("/") : [];
1198
+ const method = request.method.toUpperCase();
1199
+ if (extra !== void 0) return refuse(404, "not_found", "Not found");
1200
+ try {
1201
+ if (id && action === "download") {
1202
+ if (method !== "GET") return refuse(405, "method_not_allowed", "Use GET");
1203
+ return await download(request, workspaceId, id);
1204
+ }
1205
+ if (method !== "GET" && !sameOrigin(request)) return refuse(403, "cross_origin", "Cross-origin request refused");
1206
+ const principal = await owner(request, workspaceId);
1207
+ if (principal instanceof Response) return principal;
1208
+ if (!id) {
1209
+ if (method === "GET") {
1210
+ const jobs = await engine.list(workspaceId);
1211
+ return json({ exports: await Promise.all(jobs.map((job) => withProgress(job, mount))) });
1212
+ }
1213
+ if (method === "POST") {
1214
+ await engine.prune(workspaceId, retentionMs);
1215
+ const job = await engine.start(workspaceId, principal.userId);
1216
+ return json(await withProgress(job, mount), 202);
1217
+ }
1218
+ return refuse(405, "method_not_allowed", "Use GET or POST");
1219
+ }
1220
+ if (!action) {
1221
+ if (method === "GET") {
1222
+ const job = await engine.get(workspaceId, id);
1223
+ return job ? json(await withProgress(job, mount)) : refuse(404, "not_found", "Export not found");
1224
+ }
1225
+ if (method === "DELETE") {
1226
+ const job = await engine.get(workspaceId, id);
1227
+ if (!job) return refuse(404, "not_found", "Export not found");
1228
+ if (job.status === "running") return refuse(409, "running", "Wait for the export to finish before deleting it");
1229
+ await engine.prune(workspaceId, 0, id);
1230
+ return json({ deleted: id });
1231
+ }
1232
+ return refuse(405, "method_not_allowed", "Use GET or DELETE");
1233
+ }
1234
+ if (action === "advance") {
1235
+ if (method !== "POST") return refuse(405, "method_not_allowed", "Use POST");
1236
+ const job = await engine.advance(workspaceId, id, options.advanceBudgetMs);
1237
+ return job ? json(await withProgress(job, mount)) : refuse(404, "not_found", "Export not found");
1238
+ }
1239
+ return refuse(404, "not_found", "Not found");
1240
+ } catch {
1241
+ return refuse(500, "export_error", "The export request failed");
1242
+ }
1243
+ }
1244
+ return { route, engine };
1245
+ }
1246
+
1247
+ // src/workspace-export/storage.ts
1248
+ function createR2ExportStorage(bucket) {
1249
+ return {
1250
+ async put(key, body, options) {
1251
+ const result = await bucket.put(key, body, options?.ifMatch ? { onlyIf: { etagMatches: options.ifMatch } } : void 0);
1252
+ return result ? { etag: result.etag } : null;
1253
+ },
1254
+ async get(key) {
1255
+ const object = await bucket.get(key);
1256
+ return object ? { body: object.body, size: object.size, etag: object.etag } : null;
1257
+ },
1258
+ async list(prefix, cursor) {
1259
+ const page = await bucket.list({ prefix, cursor, limit: 1e3 });
1260
+ return { keys: page.objects.map((o) => o.key), cursor: page.truncated ? page.cursor : void 0 };
1261
+ },
1262
+ async delete(keys) {
1263
+ for (let i = 0; i < keys.length; i += 1e3) await bucket.delete(keys.slice(i, i + 1e3));
1264
+ }
1265
+ };
1266
+ }
1267
+ function createMemoryExportStorage() {
1268
+ const objects = /* @__PURE__ */ new Map();
1269
+ const etags = /* @__PURE__ */ new Map();
1270
+ let version = 0;
1271
+ return {
1272
+ objects,
1273
+ async put(key, body, options) {
1274
+ if (options?.ifMatch && etags.get(key) !== options.ifMatch) return null;
1275
+ version += 1;
1276
+ objects.set(key, body.slice());
1277
+ etags.set(key, String(version));
1278
+ return { etag: String(version) };
1279
+ },
1280
+ async get(key) {
1281
+ const bytes = objects.get(key);
1282
+ if (!bytes) return null;
1283
+ return { body: new Blob([bytes]).stream(), size: bytes.length, etag: etags.get(key) };
1284
+ },
1285
+ async list(prefix) {
1286
+ return { keys: [...objects.keys()].filter((k) => k.startsWith(prefix)).sort() };
1287
+ },
1288
+ async delete(keys) {
1289
+ for (const key of keys) {
1290
+ objects.delete(key);
1291
+ etags.delete(key);
1292
+ }
1293
+ }
1294
+ };
1295
+ }
1296
+
1297
+ // src/workspace-export/d1.ts
1298
+ var INTERNAL = /^(sqlite_|_cf_|d1_|__drizzle|_litestream)/;
1299
+ function quote(identifier) {
1300
+ return `"${identifier.replace(/"/g, '""')}"`;
1301
+ }
1302
+ function sourceName(table) {
1303
+ return table.toLowerCase().replace(/[^a-z0-9_.-]/g, "_").replace(/^[^a-z0-9]+/, "") || "table";
1304
+ }
1305
+ async function d1WorkspaceTables(options) {
1306
+ const { db, workspaceId } = options;
1307
+ const pageSize = options.pageSize ?? 500;
1308
+ const tables = (await db.prepare(`SELECT name, sql FROM sqlite_master WHERE type = 'table' ORDER BY name`).all()).results.filter((t) => !INTERNAL.test(t.name));
1309
+ const sources = [];
1310
+ const coverage = [];
1311
+ for (const { name: table, sql } of tables) {
1312
+ const excluded = options.exclude?.[table];
1313
+ if (excluded !== void 0) {
1314
+ coverage.push({ table, status: "excluded", detail: excluded });
1315
+ continue;
1316
+ }
1317
+ let where = options.scoped?.[table];
1318
+ let detail = where;
1319
+ if (!where) {
1320
+ const columns = (await db.prepare(`PRAGMA table_info(${quote(table)})`).all()).results.map((c) => c.name);
1321
+ const column = options.workspaceColumns.find((c) => columns.includes(c));
1322
+ if (!column) {
1323
+ coverage.push({ table, status: "unscoped" });
1324
+ continue;
1325
+ }
1326
+ where = `${quote(column)} = ?`;
1327
+ detail = column;
1328
+ }
1329
+ coverage.push({ table, status: "exported", detail });
1330
+ const binds = Array.from({ length: (where.match(/\?/g) ?? []).length }, () => workspaceId);
1331
+ const hasRowid = !/WITHOUT\s+ROWID/i.test(sql ?? "");
1332
+ const transform = options.transform?.[table];
1333
+ sources.push({
1334
+ kind: "rows",
1335
+ name: `db.${sourceName(table)}`,
1336
+ dataClass: options.classify?.(table) ?? "records",
1337
+ description: `D1 table ${table} where ${detail}`,
1338
+ omitColumns: options.omitColumns?.[table],
1339
+ async expectedCount() {
1340
+ const result = await db.prepare(`SELECT COUNT(*) AS n FROM ${quote(table)} WHERE ${where}`).bind(...binds).all();
1341
+ return Number(result.results[0]?.n ?? 0);
1342
+ },
1343
+ async *rows() {
1344
+ let after = Number.MIN_SAFE_INTEGER;
1345
+ let offset = 0;
1346
+ for (; ; ) {
1347
+ const page = hasRowid ? await db.prepare(`SELECT rowid AS "__export_rowid", * FROM ${quote(table)} WHERE (${where}) AND rowid > ? ORDER BY rowid LIMIT ?`).bind(...binds, after, pageSize).all() : await db.prepare(`SELECT * FROM ${quote(table)} WHERE ${where} LIMIT ? OFFSET ?`).bind(...binds, pageSize, offset).all();
1348
+ const rows = page.results;
1349
+ if (!rows.length) return;
1350
+ const out = [];
1351
+ for (const row of rows) {
1352
+ if (hasRowid) {
1353
+ after = Number(row.__export_rowid);
1354
+ delete row.__export_rowid;
1355
+ }
1356
+ out.push(transform ? await transform(row) : row);
1357
+ }
1358
+ offset += rows.length;
1359
+ yield out;
1360
+ if (rows.length < pageSize) return;
1361
+ }
1362
+ }
1363
+ });
1364
+ }
1365
+ return { sources, coverage };
1366
+ }
1367
+
1368
+ // src/workspace-export/sources.ts
1369
+ function r2Files(args) {
1370
+ for (const prefix of args.prefixes) {
1371
+ if (!prefix.endsWith("/")) throw new Error(`workspace-export: R2 prefix "${prefix}" must end with "/"`);
1372
+ }
1373
+ return {
1374
+ kind: "files",
1375
+ name: args.name,
1376
+ dataClass: args.dataClass,
1377
+ description: args.description ?? `R2 objects under ${args.prefixes.join(", ")}`,
1378
+ async *files() {
1379
+ for (const prefix of args.prefixes) {
1380
+ let cursor;
1381
+ do {
1382
+ const page = await args.bucket.list({ prefix, cursor, limit: 1e3 });
1383
+ for (const object of page.objects) {
1384
+ yield {
1385
+ path: args.prefixes.length > 1 ? object.key : object.key.slice(prefix.length),
1386
+ async open() {
1387
+ const found = await args.bucket.get(object.key);
1388
+ if (!found) return null;
1389
+ return args.read ? args.read(object.key, found.body) : found.body;
1390
+ }
1391
+ };
1392
+ }
1393
+ cursor = page.truncated ? page.cursor : void 0;
1394
+ } while (cursor);
1395
+ }
1396
+ }
1397
+ };
1398
+ }
1399
+ function kvFiles(args) {
1400
+ return {
1401
+ kind: "files",
1402
+ name: args.name,
1403
+ dataClass: args.dataClass,
1404
+ description: args.description ?? `KV values under ${args.prefix}`,
1405
+ async *files() {
1406
+ let cursor;
1407
+ do {
1408
+ const page = await args.kv.list({ prefix: args.prefix, cursor, limit: 1e3 });
1409
+ for (const key of page.keys) {
1410
+ yield {
1411
+ path: key.name.slice(args.prefix.length),
1412
+ async open() {
1413
+ const body = await args.kv.get(key.name, "stream");
1414
+ if (!body) return null;
1415
+ return args.read ? args.read(key.name, body) : body;
1416
+ }
1417
+ };
1418
+ }
1419
+ cursor = page.list_complete ? void 0 : page.cursor;
1420
+ } while (cursor);
1421
+ }
1422
+ };
1423
+ }
1424
+ export {
1425
+ MANIFEST_SCHEMA,
1426
+ REDACTED,
1427
+ createExportEngine,
1428
+ createMemoryExportStorage,
1429
+ createR2ExportStorage,
1430
+ createSecretScanner,
1431
+ createWorkspaceExport,
1432
+ d1WorkspaceTables,
1433
+ isCredentialFile,
1434
+ isSecretName,
1435
+ kvFiles,
1436
+ r2Files
1437
+ };
1438
+ //# sourceMappingURL=index.js.map