@c9up/archive 0.1.6 → 0.1.8

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 (46) hide show
  1. package/dist/ArchiveProvider.d.ts +7 -1
  2. package/dist/ArchiveProvider.d.ts.map +1 -1
  3. package/dist/ArchiveProvider.js +35 -9
  4. package/dist/ArchiveProvider.js.map +1 -1
  5. package/dist/DriveManager.d.ts +80 -0
  6. package/dist/DriveManager.d.ts.map +1 -0
  7. package/dist/DriveManager.js +102 -0
  8. package/dist/DriveManager.js.map +1 -0
  9. package/dist/GcsDriver.d.ts +14 -4
  10. package/dist/GcsDriver.d.ts.map +1 -1
  11. package/dist/GcsDriver.js +126 -15
  12. package/dist/GcsDriver.js.map +1 -1
  13. package/dist/S3Driver.d.ts +14 -4
  14. package/dist/S3Driver.d.ts.map +1 -1
  15. package/dist/S3Driver.js +268 -15
  16. package/dist/S3Driver.js.map +1 -1
  17. package/dist/StorageManager.d.ts +124 -18
  18. package/dist/StorageManager.d.ts.map +1 -1
  19. package/dist/StorageManager.js +139 -13
  20. package/dist/StorageManager.js.map +1 -1
  21. package/dist/expiry.d.ts +7 -0
  22. package/dist/expiry.d.ts.map +1 -1
  23. package/dist/expiry.js +47 -0
  24. package/dist/expiry.js.map +1 -1
  25. package/dist/index.d.ts +16 -6
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +13 -4
  28. package/dist/index.js.map +1 -1
  29. package/dist/objects.d.ts +50 -0
  30. package/dist/objects.d.ts.map +1 -0
  31. package/dist/objects.js +77 -0
  32. package/dist/objects.js.map +1 -0
  33. package/dist/testing/FakeStorage.d.ts +43 -4
  34. package/dist/testing/FakeStorage.d.ts.map +1 -1
  35. package/dist/testing/FakeStorage.js +153 -12
  36. package/dist/testing/FakeStorage.js.map +1 -1
  37. package/package.json +2 -1
  38. package/src/ArchiveProvider.ts +53 -12
  39. package/src/DriveManager.ts +151 -0
  40. package/src/GcsDriver.ts +166 -15
  41. package/src/S3Driver.ts +317 -15
  42. package/src/StorageManager.ts +284 -25
  43. package/src/expiry.ts +52 -0
  44. package/src/index.ts +26 -5
  45. package/src/objects.ts +92 -0
  46. package/src/testing/FakeStorage.ts +191 -12
@@ -13,19 +13,29 @@
13
13
  * Kept behind the `./testing` subpath so production imports stay clean.
14
14
  */
15
15
 
16
+ import { AssertionError } from "node:assert";
16
17
  import { createHash } from "node:crypto";
17
18
  import { Readable } from "node:stream";
18
19
  import { ArchiveError } from "../errors.js";
19
- import { assertValidExpiry, DEFAULT_EXPIRES_IN } from "../expiry.js";
20
+ import {
21
+ assertValidExpiry,
22
+ DEFAULT_EXPIRES_IN,
23
+ parseExpiry,
24
+ } from "../expiry.js";
20
25
  import { inferMimeType } from "../mime-types.js";
26
+ import { DriveDirectory, DriveFile } from "../objects.js";
21
27
  import type {
28
+ ListAllOptions,
29
+ ListAllResult,
22
30
  Metadata,
23
31
  PutStreamOptions,
24
32
  SignedUrlOptions,
25
33
  StorageDriver,
26
34
  StorageEntry,
27
35
  Visibility,
36
+ WriteOptions,
28
37
  } from "../StorageManager.js";
38
+ import { StorageManager } from "../StorageManager.js";
29
39
 
30
40
  interface StoredObject {
31
41
  content: Buffer;
@@ -41,17 +51,37 @@ const FAKE_SECRET = "archive-fake-signing-secret-do-not-use-in-prod";
41
51
  export class FakeStorage implements StorageDriver {
42
52
  #store = new Map<string, StoredObject>();
43
53
 
44
- async put(filePath: string, content: Buffer | string): Promise<void> {
54
+ async put(
55
+ filePath: string,
56
+ content: Buffer | string,
57
+ options?: WriteOptions,
58
+ ): Promise<void> {
45
59
  const buf = typeof content === "string" ? Buffer.from(content) : content;
46
60
  this.#store.set(filePath, {
47
61
  content: Buffer.from(buf),
48
- visibility: this.#store.get(filePath)?.visibility ?? "public",
62
+ visibility:
63
+ options?.visibility ??
64
+ this.#store.get(filePath)?.visibility ??
65
+ "public",
49
66
  lastModified: new Date(),
50
67
  mimeType:
51
- inferMimeType(extFromPath(filePath)) || "application/octet-stream",
68
+ options?.contentType ??
69
+ (inferMimeType(extFromPath(filePath)) || "application/octet-stream"),
52
70
  });
53
71
  }
54
72
 
73
+ async getBytes(filePath: string): Promise<Uint8Array> {
74
+ const obj = this.#store.get(filePath);
75
+ if (!obj) {
76
+ throw new ArchiveError(
77
+ "ARCHIVE_NOT_FOUND",
78
+ `Fake object does not exist at path '${filePath}'`,
79
+ { hint: "put() it first, or verify the test setup." },
80
+ );
81
+ }
82
+ return new Uint8Array(obj.content);
83
+ }
84
+
55
85
  async putStream(
56
86
  filePath: string,
57
87
  readable: NodeJS.ReadableStream,
@@ -110,7 +140,7 @@ export class FakeStorage implements StorageDriver {
110
140
  }
111
141
 
112
142
  getSignedUrl(filePath: string, options?: SignedUrlOptions): string {
113
- const expiresIn = options?.expiresIn ?? DEFAULT_EXPIRES_IN;
143
+ const expiresIn = parseExpiry(options?.expiresIn ?? DEFAULT_EXPIRES_IN);
114
144
  assertValidExpiry(expiresIn);
115
145
  const exp = Math.floor(Date.now() / 1000) + expiresIn;
116
146
  const sig = createHash("sha256")
@@ -119,6 +149,28 @@ export class FakeStorage implements StorageDriver {
119
149
  return `fake://${filePath}?exp=${exp}&sig=${sig}`;
120
150
  }
121
151
 
152
+ getSignedUploadUrl(filePath: string, options?: SignedUrlOptions): string {
153
+ const expiresIn = parseExpiry(options?.expiresIn ?? DEFAULT_EXPIRES_IN);
154
+ assertValidExpiry(expiresIn);
155
+ const exp = Math.floor(Date.now() / 1000) + expiresIn;
156
+ const sig = createHash("sha256")
157
+ .update(`upload.${filePath}.${exp}.${FAKE_SECRET}`)
158
+ .digest("hex");
159
+ return `fake://${filePath}?upload=1&exp=${exp}&sig=${sig}`;
160
+ }
161
+
162
+ async getVisibility(filePath: string): Promise<Visibility> {
163
+ const obj = this.#store.get(filePath);
164
+ if (!obj) {
165
+ throw new ArchiveError(
166
+ "ARCHIVE_NOT_FOUND",
167
+ `Fake object does not exist at path '${filePath}'`,
168
+ { hint: "put() it first, or verify the test setup." },
169
+ );
170
+ }
171
+ return obj.visibility;
172
+ }
173
+
122
174
  async getMetadata(filePath: string): Promise<Metadata> {
123
175
  const obj = this.#store.get(filePath);
124
176
  if (!obj) {
@@ -133,7 +185,9 @@ export class FakeStorage implements StorageDriver {
133
185
  const etagBody = createHash("sha1").update(obj.content).digest("hex");
134
186
  return {
135
187
  size: obj.content.length,
188
+ contentLength: obj.content.length,
136
189
  mimeType: obj.mimeType,
190
+ contentType: obj.mimeType,
137
191
  lastModified: obj.lastModified,
138
192
  etag: `W/"${etagBody}"`,
139
193
  visibility: obj.visibility,
@@ -152,7 +206,7 @@ export class FakeStorage implements StorageDriver {
152
206
  obj.visibility = visibility;
153
207
  }
154
208
 
155
- async copy(from: string, to: string): Promise<void> {
209
+ async copy(from: string, to: string, options?: WriteOptions): Promise<void> {
156
210
  const src = this.#store.get(from);
157
211
  if (!src) {
158
212
  throw new ArchiveError(
@@ -162,16 +216,16 @@ export class FakeStorage implements StorageDriver {
162
216
  );
163
217
  }
164
218
  // Default visibility on the new object — matches LocalDriver
165
- // semantics (AC 4 from Story 43.6).
219
+ // semantics (AC 4 from Story 43.6) unless the caller overrides it.
166
220
  this.#store.set(to, {
167
221
  content: Buffer.from(src.content),
168
- visibility: "public",
222
+ visibility: options?.visibility ?? "public",
169
223
  lastModified: new Date(),
170
- mimeType: src.mimeType,
224
+ mimeType: options?.contentType ?? src.mimeType,
171
225
  });
172
226
  }
173
227
 
174
- async move(from: string, to: string): Promise<void> {
228
+ async move(from: string, to: string, options?: WriteOptions): Promise<void> {
175
229
  const src = this.#store.get(from);
176
230
  if (!src) {
177
231
  throw new ArchiveError(
@@ -180,11 +234,22 @@ export class FakeStorage implements StorageDriver {
180
234
  { hint: "Confirm the source path was put() first." },
181
235
  );
182
236
  }
183
- // Move preserves visibility — identity is preserved.
184
- this.#store.set(to, { ...src, content: Buffer.from(src.content) });
237
+ // Move preserves visibility — identity is preserved (an explicit
238
+ // override wins).
239
+ this.#store.set(to, {
240
+ ...src,
241
+ content: Buffer.from(src.content),
242
+ visibility: options?.visibility ?? src.visibility,
243
+ });
185
244
  this.#store.delete(from);
186
245
  }
187
246
 
247
+ async deleteAll(prefix: string): Promise<void> {
248
+ for (const key of [...this.#store.keys()]) {
249
+ if (key.startsWith(prefix)) this.#store.delete(key);
250
+ }
251
+ }
252
+
188
253
  async *list(prefix: string): AsyncIterable<StorageEntry> {
189
254
  for (const [path, obj] of this.#store) {
190
255
  if (!path.startsWith(prefix)) continue;
@@ -192,6 +257,45 @@ export class FakeStorage implements StorageDriver {
192
257
  }
193
258
  }
194
259
 
260
+ async listAll(
261
+ prefix: string,
262
+ options?: ListAllOptions,
263
+ ): Promise<ListAllResult> {
264
+ const recursive = options?.recursive ?? false;
265
+ const normalizedPrefix =
266
+ prefix === "" || prefix === "/"
267
+ ? ""
268
+ : prefix.endsWith("/")
269
+ ? prefix
270
+ : `${prefix}/`;
271
+ const files: DriveFile[] = [];
272
+ const directories = new Map<string, DriveDirectory>();
273
+ for (const [key, obj] of this.#store) {
274
+ if (!key.startsWith(normalizedPrefix)) continue;
275
+ const rest = key.slice(normalizedPrefix.length);
276
+ const slash = rest.indexOf("/");
277
+ if (recursive || slash === -1) {
278
+ files.push(
279
+ new DriveFile(key, this, {
280
+ size: obj.content.length,
281
+ contentLength: obj.content.length,
282
+ mimeType: obj.mimeType,
283
+ contentType: obj.mimeType,
284
+ lastModified: obj.lastModified,
285
+ etag: "",
286
+ visibility: obj.visibility,
287
+ }),
288
+ );
289
+ } else {
290
+ const dirPrefix = normalizedPrefix + rest.slice(0, slash);
291
+ if (!directories.has(dirPrefix)) {
292
+ directories.set(dirPrefix, new DriveDirectory(dirPrefix));
293
+ }
294
+ }
295
+ }
296
+ return { objects: [...directories.values(), ...files] };
297
+ }
298
+
195
299
  /** Test helper — wipe all state. */
196
300
  clear(): void {
197
301
  this.#store.clear();
@@ -243,6 +347,35 @@ export class FakeStorage implements StorageDriver {
243
347
  return out;
244
348
  }
245
349
 
350
+ /**
351
+ * AdonisJS Drive parity: assert the given path(s) exist. Throws a
352
+ * Node `AssertionError` otherwise. Complements the richer
353
+ * {@link FakeStorage.assertStored} predicate form (kept as a superset).
354
+ */
355
+ assertExists(paths: string | string[]): void {
356
+ for (const p of Array.isArray(paths) ? paths : [paths]) {
357
+ if (!this.#store.has(p)) {
358
+ throw new AssertionError({
359
+ message: `Expected "${p}" to exist, but file not found.`,
360
+ });
361
+ }
362
+ }
363
+ }
364
+
365
+ /**
366
+ * AdonisJS Drive parity: assert the given path(s) do NOT exist. Throws
367
+ * a Node `AssertionError` otherwise.
368
+ */
369
+ assertMissing(paths: string | string[]): void {
370
+ for (const p of Array.isArray(paths) ? paths : [paths]) {
371
+ if (this.#store.has(p)) {
372
+ throw new AssertionError({
373
+ message: `Expected "${p}" to be missing, but file exists.`,
374
+ });
375
+ }
376
+ }
377
+ }
378
+
246
379
  assertStored(path: string, predicate?: FakeStoragePredicateArg): void {
247
380
  const match = makeStorageMatcher(path, predicate);
248
381
  for (const [p, obj] of this.#store) {
@@ -265,6 +398,52 @@ export class FakeStorage implements StorageDriver {
265
398
  }
266
399
  }
267
400
 
401
+ /**
402
+ * A {@link StorageManager} backed by an in-memory {@link FakeStorage},
403
+ * surfacing the fake's assertion helpers. Returned by
404
+ * `DriveManager.fake()` — mirrors flydrive's `FakeDisk extends Disk`, so
405
+ * `use()` and `fake()` are drop-in compatible.
406
+ */
407
+ export class FakeStorageManager extends StorageManager {
408
+ #fake: FakeStorage;
409
+
410
+ constructor() {
411
+ const fake = new FakeStorage();
412
+ super(fake);
413
+ this.#fake = fake;
414
+ }
415
+
416
+ /** AdonisJS Drive parity — assert the given path(s) exist. */
417
+ assertExists(paths: string | string[]): void {
418
+ this.#fake.assertExists(paths);
419
+ }
420
+
421
+ /** AdonisJS Drive parity — assert the given path(s) do NOT exist. */
422
+ assertMissing(paths: string | string[]): void {
423
+ this.#fake.assertMissing(paths);
424
+ }
425
+
426
+ /** Ream superset — predicate-based captured-object assertion. */
427
+ assertStored(path: string, predicate?: FakeStoragePredicateArg): void {
428
+ this.#fake.assertStored(path, predicate);
429
+ }
430
+
431
+ /** Ream superset — negated predicate assertion. */
432
+ assertNotStored(path: string, predicate?: FakeStoragePredicateArg): void {
433
+ this.#fake.assertNotStored(path, predicate);
434
+ }
435
+
436
+ /** Snapshot of the captured objects. */
437
+ getStored(): ReturnType<FakeStorage["getStored"]> {
438
+ return this.#fake.getStored();
439
+ }
440
+
441
+ /** Wipe all in-memory state. */
442
+ clear(): void {
443
+ this.#fake.clear();
444
+ }
445
+ }
446
+
268
447
  export interface FakeStoragePredicate {
269
448
  /** Substring match against the stored content, viewed as UTF-8.
270
449
  * Non-UTF-8 binary payloads (PNG, PDF, ...) will be lossily