@storacha/encrypt-upload-client 1.1.56 → 1.1.58

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 (72) hide show
  1. package/dist/config/constants.d.ts +3 -3
  2. package/dist/config/constants.js +4 -3
  3. package/dist/config/env.d.ts +9 -6
  4. package/dist/config/service.d.ts +13 -13
  5. package/dist/core/client.d.ts +54 -41
  6. package/dist/core/client.js +68 -56
  7. package/dist/core/errors.d.ts +6 -6
  8. package/dist/core/metadata/encrypted-metadata.d.ts +13 -8
  9. package/dist/core/metadata/kms-metadata.d.ts +68 -36
  10. package/dist/core/metadata/lit-metadata.d.ts +63 -28
  11. package/dist/crypto/adapters/kms-crypto-adapter.d.ts +172 -137
  12. package/dist/crypto/adapters/lit-crypto-adapter.d.ts +107 -86
  13. package/dist/crypto/factories.browser.d.ts +9 -5
  14. package/dist/crypto/factories.browser.js +15 -7
  15. package/dist/crypto/factories.node.d.ts +13 -6
  16. package/dist/crypto/factories.node.js +19 -13
  17. package/dist/crypto/index.d.ts +5 -5
  18. package/dist/crypto/index.js +5 -5
  19. package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.d.ts +58 -54
  20. package/dist/crypto/symmetric/generic-aes-ctr-streaming-crypto.js +174 -146
  21. package/dist/crypto/symmetric/node-aes-cbc-crypto.d.ts +36 -32
  22. package/dist/crypto/symmetric/node-aes-cbc-crypto.js +101 -95
  23. package/dist/examples/decrypt-test.d.ts +2 -2
  24. package/dist/examples/decrypt-test.js +78 -69
  25. package/dist/examples/encrypt-test.d.ts +5 -3
  26. package/dist/examples/encrypt-test.js +58 -55
  27. package/dist/handlers/decrypt-handler.d.ts +19 -5
  28. package/dist/handlers/encrypt-handler.d.ts +9 -3
  29. package/dist/handlers/encrypt-handler.js +93 -57
  30. package/dist/index.d.ts +2 -2
  31. package/dist/index.js +2 -2
  32. package/dist/protocols/lit.d.ts +33 -9
  33. package/dist/protocols/lit.js +134 -98
  34. package/dist/test/cid-verification.spec.d.ts +2 -2
  35. package/dist/test/cid-verification.spec.js +341 -313
  36. package/dist/test/crypto-compatibility.spec.d.ts +2 -2
  37. package/dist/test/crypto-compatibility.spec.js +184 -120
  38. package/dist/test/crypto-counter-security.spec.d.ts +2 -2
  39. package/dist/test/crypto-counter-security.spec.js +177 -138
  40. package/dist/test/crypto-streaming.spec.d.ts +2 -2
  41. package/dist/test/crypto-streaming.spec.js +208 -126
  42. package/dist/test/encrypted-metadata.spec.d.ts +2 -2
  43. package/dist/test/encrypted-metadata.spec.js +89 -62
  44. package/dist/test/factories.spec.d.ts +2 -2
  45. package/dist/test/factories.spec.js +275 -139
  46. package/dist/test/file-metadata.spec.d.ts +2 -2
  47. package/dist/test/file-metadata.spec.js +472 -416
  48. package/dist/test/fixtures/test-fixtures.d.ts +25 -20
  49. package/dist/test/fixtures/test-fixtures.js +61 -53
  50. package/dist/test/helpers/test-file-utils.d.ts +19 -14
  51. package/dist/test/helpers/test-file-utils.js +78 -76
  52. package/dist/test/https-enforcement.spec.d.ts +2 -2
  53. package/dist/test/https-enforcement.spec.js +278 -124
  54. package/dist/test/kms-crypto-adapter.spec.d.ts +2 -2
  55. package/dist/test/kms-crypto-adapter.spec.js +473 -304
  56. package/dist/test/lit-crypto-adapter.spec.d.ts +2 -2
  57. package/dist/test/lit-crypto-adapter.spec.js +206 -118
  58. package/dist/test/memory-efficiency.spec.d.ts +2 -2
  59. package/dist/test/memory-efficiency.spec.js +100 -87
  60. package/dist/test/mocks/key-manager.d.ts +71 -38
  61. package/dist/test/mocks/key-manager.js +129 -113
  62. package/dist/test/node-crypto-adapter.spec.d.ts +2 -2
  63. package/dist/test/node-crypto-adapter.spec.js +155 -102
  64. package/dist/test/node-generic-crypto-adapter.spec.d.ts +2 -2
  65. package/dist/test/node-generic-crypto-adapter.spec.js +134 -94
  66. package/dist/test/setup.d.ts +2 -2
  67. package/dist/test/setup.js +8 -9
  68. package/dist/tsconfig.spec.tsbuildinfo +1 -1
  69. package/dist/types.d.ts +219 -181
  70. package/dist/utils/file-metadata.d.ts +19 -13
  71. package/dist/utils.d.ts +14 -5
  72. package/package.json +4 -4
@@ -1,6 +1,9 @@
1
- import { describe, it } from 'node:test';
2
- import assert from 'assert';
3
- import { createFileWithMetadata, extractFileMetadata, } from '../src/utils/file-metadata.js';
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'assert'
3
+ import {
4
+ createFileWithMetadata,
5
+ extractFileMetadata,
6
+ } from '../src/utils/file-metadata.js'
4
7
  /**
5
8
  * Create a mock file for testing
6
9
  *
@@ -8,9 +11,12 @@ import { createFileWithMetadata, extractFileMetadata, } from '../src/utils/file-
8
11
  * @param {string} filename - The filename
9
12
  * @returns {Blob} Mock file blob
10
13
  */
11
- const createMockFile = (content = 'test file content', filename = 'test.txt') => {
12
- return new Blob([content], { type: 'text/plain' });
13
- };
14
+ const createMockFile = (
15
+ content = 'test file content',
16
+ filename = 'test.txt'
17
+ ) => {
18
+ return new Blob([content], { type: 'text/plain' })
19
+ }
14
20
  /**
15
21
  * Convert stream to array buffer for testing
16
22
  *
@@ -18,416 +24,466 @@ const createMockFile = (content = 'test file content', filename = 'test.txt') =>
18
24
  * @returns {Promise<ArrayBuffer>} The converted array buffer
19
25
  */
20
26
  const streamToArrayBuffer = async (stream) => {
21
- const reader = stream.getReader();
22
- const chunks = [];
23
- let done = false;
24
- while (!done) {
25
- const result = await reader.read();
26
- done = result.done;
27
- if (!done) {
28
- chunks.push(result.value);
29
- }
27
+ const reader = stream.getReader()
28
+ const chunks = []
29
+ let done = false
30
+ while (!done) {
31
+ const result = await reader.read()
32
+ done = result.done
33
+ if (!done) {
34
+ chunks.push(result.value)
30
35
  }
31
- const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
32
- const result = new Uint8Array(totalLength);
33
- let offset = 0;
34
- for (const chunk of chunks) {
35
- result.set(chunk, offset);
36
- offset += chunk.length;
37
- }
38
- return result.buffer;
39
- };
36
+ }
37
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0)
38
+ const result = new Uint8Array(totalLength)
39
+ let offset = 0
40
+ for (const chunk of chunks) {
41
+ result.set(chunk, offset)
42
+ offset += chunk.length
43
+ }
44
+ return result.buffer
45
+ }
40
46
  await describe('File Metadata Utils', async () => {
41
- await describe('createFileWithMetadata', async () => {
42
- await it('should create file without metadata', () => {
43
- const originalFile = createMockFile();
44
- const result = createFileWithMetadata(originalFile);
45
- assert(result instanceof Blob);
46
- assert.equal(result.size, originalFile.size);
47
- });
48
- await it('should create file with metadata', () => {
49
- const originalFile = createMockFile();
50
- const metadata = {
51
- name: 'test.txt',
52
- type: 'text/plain',
53
- extension: 'txt',
54
- };
55
- const result = createFileWithMetadata(originalFile, metadata);
56
- assert(result instanceof Blob);
57
- assert.equal(result.size, originalFile.size + 1024); // Original file + 1KB header
58
- });
59
- await it('should reject metadata that is too large', () => {
60
- const originalFile = createMockFile();
61
- const largeMetadata = {
62
- name: 'test.txt',
63
- type: 'text/plain',
64
- extension: 'txt',
65
- metadata: {
66
- description: 'a'.repeat(1000), // Very long description
67
- },
68
- };
69
- assert.throws(() => createFileWithMetadata(originalFile, largeMetadata), /Metadata too large/);
70
- });
71
- await it('should handle empty files', () => {
72
- const emptyFile = new Blob(['']);
73
- const metadata = {
74
- name: 'empty.txt',
75
- type: 'text/plain',
76
- extension: 'txt',
77
- };
78
- const result = createFileWithMetadata(emptyFile, metadata);
79
- assert.equal(result.size, 1024); // Just the header
80
- });
81
- await it('should handle binary files', () => {
82
- const binaryData = new Uint8Array([
83
- 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
84
- ]); // PNG header
85
- const binaryFile = new Blob([binaryData], { type: 'image/png' });
86
- const metadata = {
87
- name: 'image.png',
88
- type: 'image/png',
89
- extension: 'png',
90
- };
91
- const result = createFileWithMetadata(binaryFile, metadata);
92
- assert.equal(result.size, binaryData.length + 1024);
93
- });
94
- await it('should handle international characters in metadata', () => {
95
- const originalFile = createMockFile();
96
- const metadata = {
97
- name: '测试文件.txt', // Chinese characters
98
- type: 'text/plain',
99
- extension: 'txt',
100
- metadata: {
101
- author: '作者',
102
- emoji: '🎉📁',
103
- },
104
- };
105
- const result = createFileWithMetadata(originalFile, metadata);
106
- assert(result instanceof Blob);
107
- assert.equal(result.size, originalFile.size + 1024);
108
- });
109
- await it('should reject invalid metadata structure - null', () => {
110
- const originalFile = createMockFile();
111
- assert.throws(() => createFileWithMetadata(originalFile, /** @type {any} */ (null)), /Invalid metadata structure/);
112
- });
113
- await it('should reject invalid metadata structure - array', () => {
114
- const originalFile = createMockFile();
115
- assert.throws(() => createFileWithMetadata(originalFile,
116
- /** @type {any} */ (['name', 'type'])), /Invalid metadata structure/);
117
- });
118
- await it('should reject missing required fields', () => {
119
- const originalFile = createMockFile();
120
- const incompleteMetadata = { name: 'test.txt' }; // Missing type and extension
121
- assert.throws(() => createFileWithMetadata(originalFile,
122
- /** @type {any} */ (incompleteMetadata)), /Invalid metadata structure/);
123
- });
124
- await it('should reject fields that are too long', () => {
125
- const originalFile = createMockFile();
126
- const longFieldMetadata = {
127
- name: 'a'.repeat(250), // Exceeds MAX_FIELD_LENGTH
128
- type: 'text/plain',
129
- extension: 'txt',
130
- };
131
- assert.throws(() => createFileWithMetadata(originalFile, longFieldMetadata), /Metadata field too long/);
132
- });
133
- await it('should reject wrong field types', () => {
134
- const originalFile = createMockFile();
135
- const wrongTypeMetadata = {
136
- name: 123, // Should be string
137
- type: 'text/plain',
138
- extension: 'txt',
139
- };
140
- assert.throws(() => createFileWithMetadata(originalFile,
141
- /** @type {any} */ (wrongTypeMetadata)), /Invalid metadata structure/);
142
- });
143
- await it('should handle Uint8Array input without metadata', () => {
144
- const data = new Uint8Array([1, 2, 3, 4, 5]);
145
- const result = createFileWithMetadata(/** @type {any} */ (data));
146
- assert(result instanceof Blob);
147
- assert.equal(result.size, data.length);
148
- });
149
- await it('should handle Uint8Array input with metadata', () => {
150
- const data = new Uint8Array([1, 2, 3, 4, 5]);
151
- const metadata = {
152
- name: 'data.bin',
153
- type: 'application/octet-stream',
154
- extension: 'bin',
155
- };
156
- const result = createFileWithMetadata(/** @type {any} */ (data), metadata);
157
- assert(result instanceof Blob);
158
- assert.equal(result.size, data.length + 1024); // Data + 1KB header
159
- });
160
- await it('should handle ArrayBuffer input without metadata', () => {
161
- const buffer = new ArrayBuffer(10);
162
- const result = createFileWithMetadata(/** @type {any} */ (buffer));
163
- assert(result instanceof Blob);
164
- assert.equal(result.size, buffer.byteLength);
165
- });
166
- await it('should handle ArrayBuffer input with metadata', () => {
167
- const buffer = new ArrayBuffer(10);
168
- const metadata = {
169
- name: 'buffer.dat',
170
- type: 'application/octet-stream',
171
- extension: 'dat',
172
- };
173
- const result = createFileWithMetadata(
174
- /** @type {any} */ (buffer), metadata);
175
- assert(result instanceof Blob);
176
- assert.equal(result.size, buffer.byteLength + 1024); // Buffer + 1KB header
177
- });
178
- await it('should throw error for unsupported BlobLike type', () => {
179
- const unsupportedBlobLike = {
180
- stream: () => new ReadableStream(),
181
- size: 100,
182
- };
183
- assert.throws(() => createFileWithMetadata(/** @type {any} */ (unsupportedBlobLike)), /Unsupported BlobLike type - must be Blob, Uint8Array, or ArrayBuffer/);
184
- });
185
- await it('should throw error for unsupported BlobLike type with metadata', () => {
186
- const unsupportedBlobLike = {
187
- stream: () => new ReadableStream(),
188
- size: 100,
189
- };
190
- const metadata = {
191
- name: 'test.txt',
192
- type: 'text/plain',
193
- extension: 'txt',
194
- };
195
- assert.throws(() => createFileWithMetadata(
196
- /** @type {any} */ (unsupportedBlobLike), metadata), /Unsupported BlobLike type - must be Blob, Uint8Array, or ArrayBuffer/);
197
- });
198
- });
199
- await describe('extractFileMetadata', async () => {
200
- await it('should extract metadata from file with header', async () => {
201
- const originalContent = 'test file content';
202
- const originalFile = createMockFile(originalContent);
203
- const metadata = {
204
- name: 'document.pdf',
205
- type: 'application/pdf',
206
- extension: 'pdf',
207
- };
208
- // Create file with metadata
209
- const fileWithMetadata = createFileWithMetadata(originalFile, metadata);
210
- // Convert to stream (simulating decrypted stream)
211
- const stream = fileWithMetadata.stream();
212
- // Extract metadata
213
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
214
- // Verify metadata was extracted correctly
215
- assert.deepEqual(fileMetadata, metadata);
216
- // Verify file content is preserved
217
- const extractedContent = await streamToArrayBuffer(fileStream);
218
- const originalArrayBuffer = await originalFile.arrayBuffer();
219
- assert.deepEqual(new Uint8Array(extractedContent), new Uint8Array(originalArrayBuffer));
220
- });
221
- await it('should handle file without metadata', async () => {
222
- const originalFile = createMockFile();
223
- const stream = originalFile.stream();
224
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
225
- assert.equal(fileMetadata, undefined);
226
- assert(fileStream instanceof ReadableStream);
227
- });
228
- await it('should handle malformed metadata gracefully', async () => {
229
- // Create a file with invalid header
230
- const invalidHeader = new Uint8Array(1024);
231
- invalidHeader[0] = 255; // Invalid length
232
- invalidHeader[1] = 255;
233
- invalidHeader[2] = 255;
234
- invalidHeader[3] = 255;
235
- const originalContent = 'test content';
236
- const combined = new Blob([invalidHeader, originalContent]);
237
- const stream = combined.stream();
238
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
239
- // Should gracefully handle error and return stream without metadata
240
- assert.equal(fileMetadata, undefined);
241
- assert(fileStream instanceof ReadableStream);
242
- });
243
- // NEW EDGE CASE TESTS
244
- await it('should handle empty streams', async () => {
245
- const emptyBlob = new Blob([]);
246
- const stream = emptyBlob.stream();
247
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
248
- assert.equal(fileMetadata, undefined);
249
- assert(fileStream instanceof ReadableStream);
250
- });
251
- await it('should handle streams smaller than header size', async () => {
252
- const smallBlob = new Blob(['small']);
253
- const stream = smallBlob.stream();
254
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
255
- assert.equal(fileMetadata, undefined);
256
- assert(fileStream instanceof ReadableStream);
257
- });
258
- await it('should handle malformed JSON in metadata', async () => {
259
- const header = new Uint8Array(1024);
260
- const malformedJson = '{name:"test",invalid}';
261
- const jsonBytes = new TextEncoder().encode(malformedJson);
262
- // Set valid length but invalid JSON
263
- const lengthBytes = new Uint8Array(new Uint32Array([jsonBytes.length]).buffer);
264
- header.set(lengthBytes, 0);
265
- header.set(jsonBytes, 4);
266
- const combined = new Blob([header, 'content']);
267
- const stream = combined.stream();
268
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
269
- // Should handle malformed JSON gracefully
270
- assert.equal(fileMetadata, undefined);
271
- assert(fileStream instanceof ReadableStream);
272
- });
273
- await it('should handle JSON that is too large', async () => {
274
- const header = new Uint8Array(1024);
275
- const largeJson = JSON.stringify({
276
- name: 'test.txt',
277
- type: 'text/plain',
278
- extension: 'txt',
279
- metadata: { data: 'x'.repeat(800) }, // Creates JSON > 800 chars
280
- });
281
- const jsonBytes = new TextEncoder().encode(largeJson);
282
- if (jsonBytes.length <= 1020) {
283
- // If it fits in header
284
- const lengthBytes = new Uint8Array(new Uint32Array([jsonBytes.length]).buffer);
285
- header.set(lengthBytes, 0);
286
- header.set(jsonBytes, 4);
287
- const combined = new Blob([header, 'content']);
288
- const stream = combined.stream();
289
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
290
- // Should handle oversized JSON gracefully
291
- assert.equal(fileMetadata, undefined);
292
- assert(fileStream instanceof ReadableStream);
293
- }
294
- });
295
- await it('should handle deeply nested JSON', async () => {
296
- const header = new Uint8Array(1024);
297
- const deeplyNested = {
298
- name: 'test.txt',
299
- type: 'text/plain',
300
- extension: 'txt',
301
- metadata: {
302
- level1: {
303
- level2: {
304
- level3: {
305
- level4: {
306
- level5: {
307
- level6: 'too deep', // Exceeds MAX_JSON_DEPTH (5)
308
- },
309
- },
310
- },
311
- },
312
- },
313
- },
314
- };
315
- const jsonBytes = new TextEncoder().encode(JSON.stringify(deeplyNested));
316
- if (jsonBytes.length <= 1020) {
317
- // If it fits in header
318
- const lengthBytes = new Uint8Array(new Uint32Array([jsonBytes.length]).buffer);
319
- header.set(lengthBytes, 0);
320
- header.set(jsonBytes, 4);
321
- const combined = new Blob([header, 'content']);
322
- const stream = combined.stream();
323
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
324
- // Should handle deeply nested JSON gracefully
325
- assert.equal(fileMetadata, undefined);
326
- assert(fileStream instanceof ReadableStream);
327
- }
328
- });
329
- await it('should handle binary file content after metadata', async () => {
330
- const binaryData = new Uint8Array([
331
- 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
332
- ]);
333
- const binaryFile = new Blob([binaryData]);
334
- const metadata = { name: 'test.png', type: 'image/png', extension: 'png' };
335
- const fileWithMetadata = createFileWithMetadata(binaryFile, metadata);
336
- const stream = fileWithMetadata.stream();
337
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
338
- assert.deepEqual(fileMetadata, metadata);
339
- // Verify binary content is preserved
340
- const extractedContent = await streamToArrayBuffer(fileStream);
341
- assert.deepEqual(new Uint8Array(extractedContent), binaryData);
342
- });
343
- await it('should handle international characters in extracted metadata', async () => {
344
- const originalFile = createMockFile('内容');
345
- const metadata = {
346
- name: '测试文件.txt',
347
- type: 'text/plain',
348
- extension: 'txt',
349
- metadata: {
350
- author: '作者',
351
- emoji: '🎉📁',
352
- },
353
- };
354
- const fileWithMetadata = createFileWithMetadata(originalFile, metadata);
355
- const stream = fileWithMetadata.stream();
356
- const { fileMetadata } = await extractFileMetadata(stream);
357
- assert.deepEqual(fileMetadata, metadata);
358
- });
359
- await it('should handle zero-length metadata', async () => {
360
- const header = new Uint8Array(1024);
361
- // Set length to 0
362
- const lengthBytes = new Uint8Array(new Uint32Array([0]).buffer);
363
- header.set(lengthBytes, 0);
364
- const combined = new Blob([header, 'content']);
365
- const stream = combined.stream();
366
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
367
- assert.equal(fileMetadata, undefined);
368
- assert(fileStream instanceof ReadableStream);
369
- });
370
- });
371
- await describe('round-trip test', async () => {
372
- await it('should preserve file content and metadata through full cycle', async () => {
373
- const originalContent = 'This is a test file with some content.';
374
- const originalFile = createMockFile(originalContent);
375
- const metadata = {
376
- name: 'test-document.txt',
377
- type: 'text/plain',
378
- extension: 'txt',
379
- metadata: {
380
- author: 'Test Author',
381
- created: '2024-01-15',
382
- },
383
- };
384
- // Step 1: Create file with metadata
385
- const fileWithMetadata = createFileWithMetadata(originalFile, metadata);
386
- // Step 2: Extract metadata (simulating decryption)
387
- const stream = fileWithMetadata.stream();
388
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
389
- // Step 3: Verify metadata
390
- assert.deepEqual(fileMetadata, metadata);
391
- // Step 4: Verify file content
392
- const extractedContent = await streamToArrayBuffer(fileStream);
393
- const originalArrayBuffer = await originalFile.arrayBuffer();
394
- assert.deepEqual(new Uint8Array(extractedContent), new Uint8Array(originalArrayBuffer));
395
- });
396
- await it('should preserve binary files with international metadata', async () => {
397
- const binaryData = new Uint8Array([0xff, 0xd8, 0xff, 0xe0]); // JPEG header
398
- const originalFile = new Blob([binaryData], { type: 'image/jpeg' });
399
- const metadata = {
400
- name: '照片.jpg',
401
- type: 'image/jpeg',
402
- extension: 'jpg',
403
- metadata: {
404
- camera: 'Canon EOS 📷',
405
- location: 'Tokyo 🗾',
47
+ await describe('createFileWithMetadata', async () => {
48
+ await it('should create file without metadata', () => {
49
+ const originalFile = createMockFile()
50
+ const result = createFileWithMetadata(originalFile)
51
+ assert(result instanceof Blob)
52
+ assert.equal(result.size, originalFile.size)
53
+ })
54
+ await it('should create file with metadata', () => {
55
+ const originalFile = createMockFile()
56
+ const metadata = {
57
+ name: 'test.txt',
58
+ type: 'text/plain',
59
+ extension: 'txt',
60
+ }
61
+ const result = createFileWithMetadata(originalFile, metadata)
62
+ assert(result instanceof Blob)
63
+ assert.equal(result.size, originalFile.size + 1024) // Original file + 1KB header
64
+ })
65
+ await it('should reject metadata that is too large', () => {
66
+ const originalFile = createMockFile()
67
+ const largeMetadata = {
68
+ name: 'test.txt',
69
+ type: 'text/plain',
70
+ extension: 'txt',
71
+ metadata: {
72
+ description: 'a'.repeat(1000), // Very long description
73
+ },
74
+ }
75
+ assert.throws(
76
+ () => createFileWithMetadata(originalFile, largeMetadata),
77
+ /Metadata too large/
78
+ )
79
+ })
80
+ await it('should handle empty files', () => {
81
+ const emptyFile = new Blob([''])
82
+ const metadata = {
83
+ name: 'empty.txt',
84
+ type: 'text/plain',
85
+ extension: 'txt',
86
+ }
87
+ const result = createFileWithMetadata(emptyFile, metadata)
88
+ assert.equal(result.size, 1024) // Just the header
89
+ })
90
+ await it('should handle binary files', () => {
91
+ const binaryData = new Uint8Array([
92
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
93
+ ]) // PNG header
94
+ const binaryFile = new Blob([binaryData], { type: 'image/png' })
95
+ const metadata = {
96
+ name: 'image.png',
97
+ type: 'image/png',
98
+ extension: 'png',
99
+ }
100
+ const result = createFileWithMetadata(binaryFile, metadata)
101
+ assert.equal(result.size, binaryData.length + 1024)
102
+ })
103
+ await it('should handle international characters in metadata', () => {
104
+ const originalFile = createMockFile()
105
+ const metadata = {
106
+ name: '测试文件.txt', // Chinese characters
107
+ type: 'text/plain',
108
+ extension: 'txt',
109
+ metadata: {
110
+ author: '作者',
111
+ emoji: '🎉📁',
112
+ },
113
+ }
114
+ const result = createFileWithMetadata(originalFile, metadata)
115
+ assert(result instanceof Blob)
116
+ assert.equal(result.size, originalFile.size + 1024)
117
+ })
118
+ await it('should reject invalid metadata structure - null', () => {
119
+ const originalFile = createMockFile()
120
+ assert.throws(
121
+ () => createFileWithMetadata(originalFile, /** @type {any} */ (null)),
122
+ /Invalid metadata structure/
123
+ )
124
+ })
125
+ await it('should reject invalid metadata structure - array', () => {
126
+ const originalFile = createMockFile()
127
+ assert.throws(
128
+ () =>
129
+ createFileWithMetadata(
130
+ originalFile,
131
+ /** @type {any} */ (['name', 'type'])
132
+ ),
133
+ /Invalid metadata structure/
134
+ )
135
+ })
136
+ await it('should reject missing required fields', () => {
137
+ const originalFile = createMockFile()
138
+ const incompleteMetadata = { name: 'test.txt' } // Missing type and extension
139
+ assert.throws(
140
+ () =>
141
+ createFileWithMetadata(
142
+ originalFile,
143
+ /** @type {any} */ (incompleteMetadata)
144
+ ),
145
+ /Invalid metadata structure/
146
+ )
147
+ })
148
+ await it('should reject fields that are too long', () => {
149
+ const originalFile = createMockFile()
150
+ const longFieldMetadata = {
151
+ name: 'a'.repeat(250), // Exceeds MAX_FIELD_LENGTH
152
+ type: 'text/plain',
153
+ extension: 'txt',
154
+ }
155
+ assert.throws(
156
+ () => createFileWithMetadata(originalFile, longFieldMetadata),
157
+ /Metadata field too long/
158
+ )
159
+ })
160
+ await it('should reject wrong field types', () => {
161
+ const originalFile = createMockFile()
162
+ const wrongTypeMetadata = {
163
+ name: 123, // Should be string
164
+ type: 'text/plain',
165
+ extension: 'txt',
166
+ }
167
+ assert.throws(
168
+ () =>
169
+ createFileWithMetadata(
170
+ originalFile,
171
+ /** @type {any} */ (wrongTypeMetadata)
172
+ ),
173
+ /Invalid metadata structure/
174
+ )
175
+ })
176
+ await it('should handle Uint8Array input without metadata', () => {
177
+ const data = new Uint8Array([1, 2, 3, 4, 5])
178
+ const result = createFileWithMetadata(/** @type {any} */ (data))
179
+ assert(result instanceof Blob)
180
+ assert.equal(result.size, data.length)
181
+ })
182
+ await it('should handle Uint8Array input with metadata', () => {
183
+ const data = new Uint8Array([1, 2, 3, 4, 5])
184
+ const metadata = {
185
+ name: 'data.bin',
186
+ type: 'application/octet-stream',
187
+ extension: 'bin',
188
+ }
189
+ const result = createFileWithMetadata(/** @type {any} */ (data), metadata)
190
+ assert(result instanceof Blob)
191
+ assert.equal(result.size, data.length + 1024) // Data + 1KB header
192
+ })
193
+ await it('should handle ArrayBuffer input without metadata', () => {
194
+ const buffer = new ArrayBuffer(10)
195
+ const result = createFileWithMetadata(/** @type {any} */ (buffer))
196
+ assert(result instanceof Blob)
197
+ assert.equal(result.size, buffer.byteLength)
198
+ })
199
+ await it('should handle ArrayBuffer input with metadata', () => {
200
+ const buffer = new ArrayBuffer(10)
201
+ const metadata = {
202
+ name: 'buffer.dat',
203
+ type: 'application/octet-stream',
204
+ extension: 'dat',
205
+ }
206
+ const result = createFileWithMetadata(
207
+ /** @type {any} */ (buffer),
208
+ metadata
209
+ )
210
+ assert(result instanceof Blob)
211
+ assert.equal(result.size, buffer.byteLength + 1024) // Buffer + 1KB header
212
+ })
213
+ await it('should throw error for unsupported BlobLike type', () => {
214
+ const unsupportedBlobLike = {
215
+ stream: () => new ReadableStream(),
216
+ size: 100,
217
+ }
218
+ assert.throws(
219
+ () => createFileWithMetadata(/** @type {any} */ (unsupportedBlobLike)),
220
+ /Unsupported BlobLike type - must be Blob, Uint8Array, or ArrayBuffer/
221
+ )
222
+ })
223
+ await it('should throw error for unsupported BlobLike type with metadata', () => {
224
+ const unsupportedBlobLike = {
225
+ stream: () => new ReadableStream(),
226
+ size: 100,
227
+ }
228
+ const metadata = {
229
+ name: 'test.txt',
230
+ type: 'text/plain',
231
+ extension: 'txt',
232
+ }
233
+ assert.throws(
234
+ () =>
235
+ createFileWithMetadata(
236
+ /** @type {any} */ (unsupportedBlobLike),
237
+ metadata
238
+ ),
239
+ /Unsupported BlobLike type - must be Blob, Uint8Array, or ArrayBuffer/
240
+ )
241
+ })
242
+ })
243
+ await describe('extractFileMetadata', async () => {
244
+ await it('should extract metadata from file with header', async () => {
245
+ const originalContent = 'test file content'
246
+ const originalFile = createMockFile(originalContent)
247
+ const metadata = {
248
+ name: 'document.pdf',
249
+ type: 'application/pdf',
250
+ extension: 'pdf',
251
+ }
252
+ // Create file with metadata
253
+ const fileWithMetadata = createFileWithMetadata(originalFile, metadata)
254
+ // Convert to stream (simulating decrypted stream)
255
+ const stream = fileWithMetadata.stream()
256
+ // Extract metadata
257
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
258
+ // Verify metadata was extracted correctly
259
+ assert.deepEqual(fileMetadata, metadata)
260
+ // Verify file content is preserved
261
+ const extractedContent = await streamToArrayBuffer(fileStream)
262
+ const originalArrayBuffer = await originalFile.arrayBuffer()
263
+ assert.deepEqual(
264
+ new Uint8Array(extractedContent),
265
+ new Uint8Array(originalArrayBuffer)
266
+ )
267
+ })
268
+ await it('should handle file without metadata', async () => {
269
+ const originalFile = createMockFile()
270
+ const stream = originalFile.stream()
271
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
272
+ assert.equal(fileMetadata, undefined)
273
+ assert(fileStream instanceof ReadableStream)
274
+ })
275
+ await it('should handle malformed metadata gracefully', async () => {
276
+ // Create a file with invalid header
277
+ const invalidHeader = new Uint8Array(1024)
278
+ invalidHeader[0] = 255 // Invalid length
279
+ invalidHeader[1] = 255
280
+ invalidHeader[2] = 255
281
+ invalidHeader[3] = 255
282
+ const originalContent = 'test content'
283
+ const combined = new Blob([invalidHeader, originalContent])
284
+ const stream = combined.stream()
285
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
286
+ // Should gracefully handle error and return stream without metadata
287
+ assert.equal(fileMetadata, undefined)
288
+ assert(fileStream instanceof ReadableStream)
289
+ })
290
+ // NEW EDGE CASE TESTS
291
+ await it('should handle empty streams', async () => {
292
+ const emptyBlob = new Blob([])
293
+ const stream = emptyBlob.stream()
294
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
295
+ assert.equal(fileMetadata, undefined)
296
+ assert(fileStream instanceof ReadableStream)
297
+ })
298
+ await it('should handle streams smaller than header size', async () => {
299
+ const smallBlob = new Blob(['small'])
300
+ const stream = smallBlob.stream()
301
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
302
+ assert.equal(fileMetadata, undefined)
303
+ assert(fileStream instanceof ReadableStream)
304
+ })
305
+ await it('should handle malformed JSON in metadata', async () => {
306
+ const header = new Uint8Array(1024)
307
+ const malformedJson = '{name:"test",invalid}'
308
+ const jsonBytes = new TextEncoder().encode(malformedJson)
309
+ // Set valid length but invalid JSON
310
+ const lengthBytes = new Uint8Array(
311
+ new Uint32Array([jsonBytes.length]).buffer
312
+ )
313
+ header.set(lengthBytes, 0)
314
+ header.set(jsonBytes, 4)
315
+ const combined = new Blob([header, 'content'])
316
+ const stream = combined.stream()
317
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
318
+ // Should handle malformed JSON gracefully
319
+ assert.equal(fileMetadata, undefined)
320
+ assert(fileStream instanceof ReadableStream)
321
+ })
322
+ await it('should handle JSON that is too large', async () => {
323
+ const header = new Uint8Array(1024)
324
+ const largeJson = JSON.stringify({
325
+ name: 'test.txt',
326
+ type: 'text/plain',
327
+ extension: 'txt',
328
+ metadata: { data: 'x'.repeat(800) }, // Creates JSON > 800 chars
329
+ })
330
+ const jsonBytes = new TextEncoder().encode(largeJson)
331
+ if (jsonBytes.length <= 1020) {
332
+ // If it fits in header
333
+ const lengthBytes = new Uint8Array(
334
+ new Uint32Array([jsonBytes.length]).buffer
335
+ )
336
+ header.set(lengthBytes, 0)
337
+ header.set(jsonBytes, 4)
338
+ const combined = new Blob([header, 'content'])
339
+ const stream = combined.stream()
340
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
341
+ // Should handle oversized JSON gracefully
342
+ assert.equal(fileMetadata, undefined)
343
+ assert(fileStream instanceof ReadableStream)
344
+ }
345
+ })
346
+ await it('should handle deeply nested JSON', async () => {
347
+ const header = new Uint8Array(1024)
348
+ const deeplyNested = {
349
+ name: 'test.txt',
350
+ type: 'text/plain',
351
+ extension: 'txt',
352
+ metadata: {
353
+ level1: {
354
+ level2: {
355
+ level3: {
356
+ level4: {
357
+ level5: {
358
+ level6: 'too deep', // Exceeds MAX_JSON_DEPTH (5)
359
+ },
406
360
  },
407
- };
408
- const fileWithMetadata = createFileWithMetadata(originalFile, metadata);
409
- const stream = fileWithMetadata.stream();
410
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
411
- assert.deepEqual(fileMetadata, metadata);
412
- const extractedContent = await streamToArrayBuffer(fileStream);
413
- assert.deepEqual(new Uint8Array(extractedContent), binaryData);
414
- });
415
- await it('should handle large files efficiently', async () => {
416
- // Create a 1MB file
417
- const largeContent = new Uint8Array(1024 * 1024).fill(42);
418
- const largeFile = new Blob([largeContent]);
419
- const metadata = {
420
- name: 'large-file.bin',
421
- type: 'application/octet-stream',
422
- extension: 'bin',
423
- };
424
- const fileWithMetadata = createFileWithMetadata(largeFile, metadata);
425
- const stream = fileWithMetadata.stream();
426
- const { fileStream, fileMetadata } = await extractFileMetadata(stream);
427
- assert.deepEqual(fileMetadata, metadata);
428
- const extractedContent = await streamToArrayBuffer(fileStream);
429
- assert.deepEqual(new Uint8Array(extractedContent), largeContent);
430
- });
431
- });
432
- });
433
- //# sourceMappingURL=file-metadata.spec.js.map
361
+ },
362
+ },
363
+ },
364
+ },
365
+ }
366
+ const jsonBytes = new TextEncoder().encode(JSON.stringify(deeplyNested))
367
+ if (jsonBytes.length <= 1020) {
368
+ // If it fits in header
369
+ const lengthBytes = new Uint8Array(
370
+ new Uint32Array([jsonBytes.length]).buffer
371
+ )
372
+ header.set(lengthBytes, 0)
373
+ header.set(jsonBytes, 4)
374
+ const combined = new Blob([header, 'content'])
375
+ const stream = combined.stream()
376
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
377
+ // Should handle deeply nested JSON gracefully
378
+ assert.equal(fileMetadata, undefined)
379
+ assert(fileStream instanceof ReadableStream)
380
+ }
381
+ })
382
+ await it('should handle binary file content after metadata', async () => {
383
+ const binaryData = new Uint8Array([
384
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
385
+ ])
386
+ const binaryFile = new Blob([binaryData])
387
+ const metadata = { name: 'test.png', type: 'image/png', extension: 'png' }
388
+ const fileWithMetadata = createFileWithMetadata(binaryFile, metadata)
389
+ const stream = fileWithMetadata.stream()
390
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
391
+ assert.deepEqual(fileMetadata, metadata)
392
+ // Verify binary content is preserved
393
+ const extractedContent = await streamToArrayBuffer(fileStream)
394
+ assert.deepEqual(new Uint8Array(extractedContent), binaryData)
395
+ })
396
+ await it('should handle international characters in extracted metadata', async () => {
397
+ const originalFile = createMockFile('内容')
398
+ const metadata = {
399
+ name: '测试文件.txt',
400
+ type: 'text/plain',
401
+ extension: 'txt',
402
+ metadata: {
403
+ author: '作者',
404
+ emoji: '🎉📁',
405
+ },
406
+ }
407
+ const fileWithMetadata = createFileWithMetadata(originalFile, metadata)
408
+ const stream = fileWithMetadata.stream()
409
+ const { fileMetadata } = await extractFileMetadata(stream)
410
+ assert.deepEqual(fileMetadata, metadata)
411
+ })
412
+ await it('should handle zero-length metadata', async () => {
413
+ const header = new Uint8Array(1024)
414
+ // Set length to 0
415
+ const lengthBytes = new Uint8Array(new Uint32Array([0]).buffer)
416
+ header.set(lengthBytes, 0)
417
+ const combined = new Blob([header, 'content'])
418
+ const stream = combined.stream()
419
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
420
+ assert.equal(fileMetadata, undefined)
421
+ assert(fileStream instanceof ReadableStream)
422
+ })
423
+ })
424
+ await describe('round-trip test', async () => {
425
+ await it('should preserve file content and metadata through full cycle', async () => {
426
+ const originalContent = 'This is a test file with some content.'
427
+ const originalFile = createMockFile(originalContent)
428
+ const metadata = {
429
+ name: 'test-document.txt',
430
+ type: 'text/plain',
431
+ extension: 'txt',
432
+ metadata: {
433
+ author: 'Test Author',
434
+ created: '2024-01-15',
435
+ },
436
+ }
437
+ // Step 1: Create file with metadata
438
+ const fileWithMetadata = createFileWithMetadata(originalFile, metadata)
439
+ // Step 2: Extract metadata (simulating decryption)
440
+ const stream = fileWithMetadata.stream()
441
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
442
+ // Step 3: Verify metadata
443
+ assert.deepEqual(fileMetadata, metadata)
444
+ // Step 4: Verify file content
445
+ const extractedContent = await streamToArrayBuffer(fileStream)
446
+ const originalArrayBuffer = await originalFile.arrayBuffer()
447
+ assert.deepEqual(
448
+ new Uint8Array(extractedContent),
449
+ new Uint8Array(originalArrayBuffer)
450
+ )
451
+ })
452
+ await it('should preserve binary files with international metadata', async () => {
453
+ const binaryData = new Uint8Array([0xff, 0xd8, 0xff, 0xe0]) // JPEG header
454
+ const originalFile = new Blob([binaryData], { type: 'image/jpeg' })
455
+ const metadata = {
456
+ name: '照片.jpg',
457
+ type: 'image/jpeg',
458
+ extension: 'jpg',
459
+ metadata: {
460
+ camera: 'Canon EOS 📷',
461
+ location: 'Tokyo 🗾',
462
+ },
463
+ }
464
+ const fileWithMetadata = createFileWithMetadata(originalFile, metadata)
465
+ const stream = fileWithMetadata.stream()
466
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
467
+ assert.deepEqual(fileMetadata, metadata)
468
+ const extractedContent = await streamToArrayBuffer(fileStream)
469
+ assert.deepEqual(new Uint8Array(extractedContent), binaryData)
470
+ })
471
+ await it('should handle large files efficiently', async () => {
472
+ // Create a 1MB file
473
+ const largeContent = new Uint8Array(1024 * 1024).fill(42)
474
+ const largeFile = new Blob([largeContent])
475
+ const metadata = {
476
+ name: 'large-file.bin',
477
+ type: 'application/octet-stream',
478
+ extension: 'bin',
479
+ }
480
+ const fileWithMetadata = createFileWithMetadata(largeFile, metadata)
481
+ const stream = fileWithMetadata.stream()
482
+ const { fileStream, fileMetadata } = await extractFileMetadata(stream)
483
+ assert.deepEqual(fileMetadata, metadata)
484
+ const extractedContent = await streamToArrayBuffer(fileStream)
485
+ assert.deepEqual(new Uint8Array(extractedContent), largeContent)
486
+ })
487
+ })
488
+ })
489
+ //# sourceMappingURL=file-metadata.spec.js.map