@c9up/archive 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/etag.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Entity tags for static responses.
3
+ *
4
+ * Two rules from RFC 9110 that a hand-rolled digest tends to miss, and that
5
+ * both cost a conditional request when missed:
6
+ *
7
+ * - §8.8.3 — an entity-tag IS a quoted string. A bare hex digest is not one,
8
+ * and a strict cache is entitled to ignore it, which turns every
9
+ * revalidation back into a full transfer.
10
+ * - §13.1.2 — `If-None-Match` is a LIST, compared with the WEAK comparison
11
+ * function, so `W/"x"` and `"x"` match. Raw equality against the whole
12
+ * header value fails a client that sends two tags, and fails every client
13
+ * that sends back the weak form of a strong tag.
14
+ *
15
+ * Archive is agnostic, so this lives here rather than being imported from the
16
+ * framework; `@c9up/ream` carries the same two rules in its own `http/etag`.
17
+ */
18
+ /**
19
+ * The tag for a file, from its metadata rather than its bytes.
20
+ *
21
+ * `W/"<size hex>-<mtime hex>"`, which is what the `etag` package produces for
22
+ * a `Stats` and therefore what every static file server people have cached
23
+ * against emits. Weak because it is derived from metadata: two files with the
24
+ * same size and mtime are equivalent for caching, not provably identical.
25
+ */
26
+ export declare function statTag(stat: {
27
+ size: number;
28
+ mtimeMs: number;
29
+ }): string;
30
+ /** Whether `If-None-Match` covers `tag` (RFC 9110 §13.1.2). */
31
+ export declare function matchesIfNoneMatch(header: string | undefined, tag: string): boolean;
32
+ //# sourceMappingURL=etag.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"etag.d.ts","sourceRoot":"","sources":["../src/etag.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAEvE;AAED,+DAA+D;AAC/D,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,GAAG,EAAE,MAAM,GACT,OAAO,CAST"}
package/dist/etag.js ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Entity tags for static responses.
3
+ *
4
+ * Two rules from RFC 9110 that a hand-rolled digest tends to miss, and that
5
+ * both cost a conditional request when missed:
6
+ *
7
+ * - §8.8.3 — an entity-tag IS a quoted string. A bare hex digest is not one,
8
+ * and a strict cache is entitled to ignore it, which turns every
9
+ * revalidation back into a full transfer.
10
+ * - §13.1.2 — `If-None-Match` is a LIST, compared with the WEAK comparison
11
+ * function, so `W/"x"` and `"x"` match. Raw equality against the whole
12
+ * header value fails a client that sends two tags, and fails every client
13
+ * that sends back the weak form of a strong tag.
14
+ *
15
+ * Archive is agnostic, so this lives here rather than being imported from the
16
+ * framework; `@c9up/ream` carries the same two rules in its own `http/etag`.
17
+ */
18
+ /**
19
+ * The tag for a file, from its metadata rather than its bytes.
20
+ *
21
+ * `W/"<size hex>-<mtime hex>"`, which is what the `etag` package produces for
22
+ * a `Stats` and therefore what every static file server people have cached
23
+ * against emits. Weak because it is derived from metadata: two files with the
24
+ * same size and mtime are equivalent for caching, not provably identical.
25
+ */
26
+ export function statTag(stat) {
27
+ return `W/"${stat.size.toString(16)}-${Math.floor(stat.mtimeMs).toString(16)}"`;
28
+ }
29
+ /** Whether `If-None-Match` covers `tag` (RFC 9110 §13.1.2). */
30
+ export function matchesIfNoneMatch(header, tag) {
31
+ if (!header || tag === "")
32
+ return false;
33
+ if (header.trim() === "*")
34
+ return true;
35
+ const bare = (value) => value.startsWith("W/") ? value.slice(2) : value;
36
+ const current = bare(tag);
37
+ return header
38
+ .split(",")
39
+ .some((candidate) => bare(candidate.trim()) === current);
40
+ }
41
+ //# sourceMappingURL=etag.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"etag.js","sourceRoot":"","sources":["../src/etag.ts"],"names":[],"mappings":"AAAA;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M,GAAG,IAAI,CAsBzD"}
1
+ {"version":3,"file":"expiry.d.ts","sourceRoot":"","sources":["../src/expiry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,2EAA2E;AAC3E,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAEtC,8CAA8C;AAC9C,eAAO,MAAM,cAAc,SAAU,CAAC;AA+BtC;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAgB9D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAsBzD"}
package/dist/expiry.js CHANGED
@@ -53,7 +53,8 @@ export function parseExpiry(expiresIn) {
53
53
  hint: "Use a number of seconds, or a duration like '30mins', '1h', '7 days'.",
54
54
  });
55
55
  }
56
- return Math.round(Number.parseFloat(match[1]) * multiplier);
56
+ const [, amount] = match;
57
+ return Math.round(Number.parseFloat(amount ?? "0") * multiplier);
57
58
  }
58
59
  /**
59
60
  * Reject non-finite, zero, negative, or out-of-range expiries. Throws
@@ -1 +1 @@
1
- {"version":3,"file":"expiry.js","sourceRoot":"","sources":["../src/expiry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,2EAA2E;AAC3E,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC,CAAC,YAAY;AAEnD,8CAA8C;AAC9C,MAAM,CAAC,MAAM,cAAc,GAAG,OAAO,CAAC,CAAC,SAAS;AAEhD;;;;GAIG;AACH,MAAM,YAAY,GAA2B;IAC5C,CAAC,EAAE,CAAC;IACJ,GAAG,EAAE,CAAC;IACN,IAAI,EAAE,CAAC;IACP,MAAM,EAAE,CAAC;IACT,OAAO,EAAE,CAAC;IACV,CAAC,EAAE,EAAE;IACL,GAAG,EAAE,EAAE;IACP,IAAI,EAAE,EAAE;IACR,MAAM,EAAE,EAAE;IACV,OAAO,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AAI,YAAY,CACrB,0BAA0B,EAC1B,mCAAmC,cAAc,0BAA0B,SAAS,EAAE,EACtF,EAAE,IAAI,EAAE,0CAA0C,EAAE,CACpD,CAAC;IACH,CAAC;AACF,CAAC"}
package/dist/index.d.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  * (`use(name?)`), plus a `StaticMiddleware` for serving files over HTTP
11
11
  * and a signed-route handler for serving HMAC-signed Local URLs.
12
12
  */
13
+ import "./augmentations.js";
13
14
  export type { ArchiveConfig } from "./ArchiveProvider.js";
14
15
  export { type DriveConfig, DriveManager, type DriveServiceFactory, type FsServiceOptions, services, } from "./DriveManager.js";
15
16
  export { ArchiveError } from "./errors.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,YAAY,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EACN,KAAK,WAAW,EAChB,YAAY,EACZ,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,QAAQ,GACR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EACN,KAAK,SAAS,EACd,SAAS,EACT,KAAK,iBAAiB,GACtB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,KAAK,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EACN,KAAK,YAAY,EACjB,gBAAgB,EAChB,KAAK,2BAA2B,EAChC,KAAK,wBAAwB,GAC7B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EACb,KAA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,EACnB,KAAK,aAAa,EAClB,WAAW,EACX,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,cAAc,EACd,KAAK,UAAU,EACf,KAAK,YAAY,GACjB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,wBAAwB,EACxB,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,GACxB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,WAAW,GAAG,aAAa,EACjE,MAAM,EAAE,CAAC,GACP,CAAC,CAEH"}
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@
10
10
  * (`use(name?)`), plus a `StaticMiddleware` for serving files over HTTP
11
11
  * and a signed-route handler for serving HMAC-signed Local URLs.
12
12
  */
13
+ import "./augmentations.js";
13
14
  export { DriveManager, services, } from "./DriveManager.js";
14
15
  export { ArchiveError } from "./errors.js";
15
16
  export { GcsDriver, } from "./GcsDriver.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAEN,YAAY,EAGZ,QAAQ,GACR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAEN,SAAS,GAET,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAiB,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAEN,gBAAgB,GAGhB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAGN,WAAW,EAOX,cAAc,GAGd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,wBAAwB,GAIxB,MAAM,mBAAmB,CAAC;AAK3B;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAC3B,MAAS;IAET,OAAO,MAAM,CAAC;AACf,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,oBAAoB,CAAC;AAG5B,OAAO,EAEN,YAAY,EAGZ,QAAQ,GACR,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAEN,SAAS,GAET,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAiB,QAAQ,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAEN,gBAAgB,GAGhB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAGN,WAAW,EAOX,cAAc,GAGd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,wBAAwB,GAIxB,MAAM,mBAAmB,CAAC;AAK3B;;;;;;;;GAQG;AACH,MAAM,UAAU,YAAY,CAC3B,MAAS;IAET,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -12,6 +12,17 @@
12
12
  import type { StorageManager } from "../StorageManager.js";
13
13
  /** @internal Bind the singleton (called by ArchiveProvider). */
14
14
  export declare function setStorage(value: StorageManager): void;
15
+ /**
16
+ * @internal Bind a way to build the singleton on first genuine use.
17
+ *
18
+ * For the unconfigured case, where building it at boot would create
19
+ * `./storage` for an application that may never touch storage — and fail
20
+ * outright on a read-only rootfs. Skipping it entirely left the container
21
+ * serving `storage` while this accessor threw "before boot": the same
22
+ * application, two answers. Nothing is built until something reaches for it,
23
+ * and then both paths give the same manager.
24
+ */
25
+ export declare function setStorageResolver(factory: () => StorageManager): void;
15
26
  /** @internal Read the singleton (or `undefined` pre-boot). */
16
27
  export declare function getStorage(): StorageManager | undefined;
17
28
  declare const storage: StorageManager;
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/services/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAI3D,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAEtD;AAED,8DAA8D;AAC9D,wBAAgB,UAAU,IAAI,cAAc,GAAG,SAAS,CAEvD;AAED,QAAA,MAAM,OAAO,EAAE,cAqBb,CAAC;AAEH,eAAe,OAAO,CAAC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/services/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAK3D,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAGtD;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,cAAc,GAAG,IAAI,CAGtE;AAED,8DAA8D;AAC9D,wBAAgB,UAAU,IAAI,cAAc,GAAG,SAAS,CAEvD;AAED,QAAA,MAAM,OAAO,EAAE,cAyBb,CAAC;AAEH,eAAe,OAAO,CAAC"}
@@ -10,9 +10,26 @@
10
10
  * Populated by `ArchiveProvider.boot()`.
11
11
  */
12
12
  let instance;
13
+ let resolve;
13
14
  /** @internal Bind the singleton (called by ArchiveProvider). */
14
15
  export function setStorage(value) {
15
16
  instance = value;
17
+ resolve = undefined;
18
+ }
19
+ /**
20
+ * @internal Bind a way to build the singleton on first genuine use.
21
+ *
22
+ * For the unconfigured case, where building it at boot would create
23
+ * `./storage` for an application that may never touch storage — and fail
24
+ * outright on a read-only rootfs. Skipping it entirely left the container
25
+ * serving `storage` while this accessor threw "before boot": the same
26
+ * application, two answers. Nothing is built until something reaches for it,
27
+ * and then both paths give the same manager.
28
+ */
29
+ export function setStorageResolver(factory) {
30
+ if (instance !== undefined)
31
+ return;
32
+ resolve = factory;
16
33
  }
17
34
  /** @internal Read the singleton (or `undefined` pre-boot). */
18
35
  export function getStorage() {
@@ -29,6 +46,10 @@ const storage = new Proxy({}, {
29
46
  if (typeof prop === "symbol" || prop === "then") {
30
47
  return undefined;
31
48
  }
49
+ if (instance === undefined && resolve !== undefined) {
50
+ instance = resolve();
51
+ resolve = undefined;
52
+ }
32
53
  if (!instance) {
33
54
  throw new Error("[archive] StorageManager singleton accessed before ArchiveProvider.boot() ran. " +
34
55
  "Check that `@c9up/archive/provider` is listed in your reamrc.ts providers, " +
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","sourceRoot":"","sources":["../../src/services/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,IAAI,QAAoC,CAAC;AAEzC,gEAAgE;AAChE,MAAM,UAAU,UAAU,CAAC,KAAqB;IAC/C,QAAQ,GAAG,KAAK,CAAC;AAClB,CAAC;AAED,8DAA8D;AAC9D,MAAM,UAAU,UAAU;IACzB,OAAO,QAAQ,CAAC;AACjB,CAAC;AAED,MAAM,OAAO,GAAmB,IAAI,KAAK,CAAC,EAAoB,EAAE;IAC/D,GAAG,CAAC,OAAO,EAAE,IAAI;QAChB,2EAA2E;QAC3E,0EAA0E;QAC1E,8DAA8D;QAC9D,0EAA0E;QAC1E,0EAA0E;QAC1E,wEAAwE;QACxE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACjD,OAAO,SAAS,CAAC;QAClB,CAAC;QACD,IAAI,CAAC,QAAQ,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CACd,iFAAiF;gBAChF,6EAA6E;gBAC7E,4CAA4C,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AC,QAAQ,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CACd,iFAAiF;gBAChF,6EAA6E;gBAC7E,4CAA4C,CAC7C,CAAC;QACH,CAAC;QACD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QACpD,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACnE,CAAC;CACD,CAAC,CAAC;AAEH,eAAe,OAAO,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c9up/archive",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Archive — File storage abstraction for the Ream framework (Local + S3-compatible drivers)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -41,7 +41,8 @@
41
41
  "@c9up/helix": "^0.2.0",
42
42
  "@types/node": "^22.19.15",
43
43
  "typescript": "^6.0.2",
44
- "vitest": "^4.1.2"
44
+ "vitest": "^4.1.2",
45
+ "@c9up/ream": "^0.2.0"
45
46
  },
46
47
  "files": [
47
48
  "LICENSE",
@@ -21,6 +21,7 @@
21
21
  * import storage from '@c9up/archive/services/main'
22
22
  * await storage.put('uploads/avatar.png', buffer)
23
23
  */
24
+ import "./augmentations.js";
24
25
  import { type DriveConfig, DriveManager } from "./DriveManager.js";
25
26
  import { ArchiveError } from "./errors.js";
26
27
 
@@ -31,7 +32,7 @@ import {
31
32
  type StorageDriver,
32
33
  StorageManager,
33
34
  } from "./StorageManager.js";
34
- import { setStorage } from "./services/main.js";
35
+ import { setStorage, setStorageResolver } from "./services/main.js";
35
36
 
36
37
  interface ArchiveContainer {
37
38
  singleton(token: unknown, factory: () => unknown): void;
@@ -64,31 +65,77 @@ const DEFAULT_CONFIG: ArchiveConfig = {
64
65
  };
65
66
 
66
67
  export default class ArchiveProvider {
68
+ /**
69
+ * The one lazily-built manager, read by BOTH ways in. Set by
70
+ * {@link register}, which always runs before {@link boot}.
71
+ */
72
+ #storage: (() => StorageManager) | undefined;
73
+
67
74
  constructor(protected app: ArchiveAppContext) {}
68
75
 
69
76
  register(): void {
70
- this.app.container.singleton(DriveManager, () => {
71
- const raw = this.app.config.get<ArchiveConfig | DriveConfig>("archive");
72
- return new DriveManager(resolveDriveConfig(raw));
73
- });
77
+ // ONE cell, shared by the container's bindings and the service accessor.
78
+ //
79
+ // The container had its own factory and boot() built a second manager
80
+ // for the accessor, so `services/main`'s storage and
81
+ // `container.resolve(StorageManager)` were different objects over
82
+ // different drivers: a fake bound in a test, or a driver swapped at
83
+ // runtime, was invisible from the other side.
84
+ //
85
+ // Memoised here rather than resolved through the container because the
86
+ // accessor is synchronous and the container is not — which is exactly
87
+ // why boot() could not simply reuse the container's instance.
88
+ let drive: DriveManager | undefined;
89
+ const driveManager = (): DriveManager => {
90
+ drive ??= new DriveManager(
91
+ resolveDriveConfig(
92
+ this.app.config.get<ArchiveConfig | DriveConfig>("archive"),
93
+ ),
94
+ );
95
+ return drive;
96
+ };
97
+ let manager: StorageManager | undefined;
98
+ const storageManager = (): StorageManager => {
99
+ manager ??= driveManager().use();
100
+ return manager;
101
+ };
102
+ this.#storage = storageManager;
103
+
104
+ this.app.container.singleton(DriveManager, () => driveManager());
74
105
  // Backward-compatible bindings — the default disk is what apps
75
106
  // previously resolved via `StorageManager` / the `storage` token.
76
- this.app.container.singleton(StorageManager, async () =>
77
- (await this.app.container.resolve<DriveManager>(DriveManager)).use(),
78
- );
79
- this.app.container.singleton("storage", async () =>
80
- this.app.container.resolve<StorageManager>(StorageManager),
81
- );
82
- this.app.container.singleton("drive", async () =>
83
- this.app.container.resolve<DriveManager>(DriveManager),
84
- );
107
+ this.app.container.singleton(StorageManager, () => storageManager());
108
+ // Namespaced by the package that owns it, the way upstream namespaces
109
+ // `lucid.db`, `auth.manager` and `drive.manager` by theirs. The bare
110
+ // token stays bound beside it: it is what every existing
111
+ // `container.make(...)` asks for, and a token is not worth breaking an
112
+ // application over.
113
+ this.app.container.singleton("archive.storage", () => storageManager());
114
+ this.app.container.singleton("storage", () => storageManager());
115
+ this.app.container.singleton("archive.drive", () => driveManager());
116
+ this.app.container.singleton("drive", () => driveManager());
85
117
  }
86
118
 
87
119
  async boot(): Promise<void> {
88
- // Skip eager resolution when archive is unconfigured: an app that
89
- // registers this provider but never uses storage should not trigger
90
- // LocalDriver's mkdirSync of './storage' at boot (breaks on
91
- // read-only rootfs). Explicit (object-shaped) configs still resolve.
120
+ const storage = this.#storage;
121
+ if (storage === undefined) {
122
+ throw new ArchiveError(
123
+ "E_ARCHIVE_PROVIDER_NOT_REGISTERED",
124
+ "ArchiveProvider.boot() ran before register().",
125
+ {
126
+ hint: "Providers register before they boot — call register() first.",
127
+ },
128
+ );
129
+ }
130
+ // The accessor reads the same cell the container's bindings read, so
131
+ // however storage is reached there is one manager behind it.
132
+ setStorageResolver(storage);
133
+
134
+ // No eager construction when archive is unconfigured: an app that
135
+ // registers this provider and never uses storage should not trigger
136
+ // LocalDriver's mkdir of './storage' at boot, which fails on a
137
+ // read-only rootfs. The resolver above is what keeps that lazy case
138
+ // answering instead of throwing "before boot".
92
139
  const archive = this.app.config.get<ArchiveConfig | DriveConfig>("archive");
93
140
  if (
94
141
  archive === undefined ||
@@ -97,9 +144,10 @@ export default class ArchiveProvider {
97
144
  ) {
98
145
  return;
99
146
  }
100
- const manager =
101
- await this.app.container.resolve<StorageManager>(StorageManager);
102
- setStorage(manager);
147
+ // Configured: build now, so a misspelled driver or a missing
148
+ // driver-specific block is a boot failure rather than a surprise on the
149
+ // first upload.
150
+ setStorage(storage());
103
151
  }
104
152
 
105
153
  async start(): Promise<void> {}
@@ -10,10 +10,10 @@
10
10
  * @implements MISS-25
11
11
  */
12
12
 
13
- import { createHash } from "node:crypto";
14
13
  import * as fs from "node:fs";
15
14
  import * as fsp from "node:fs/promises";
16
15
  import * as path from "node:path";
16
+ import { matchesIfNoneMatch, statTag } from "./etag.js";
17
17
  import { DEFAULT_EXTENSIONS, inferMimeType } from "./mime-types.js";
18
18
  import { VISIBILITY_SIDECAR_SUFFIX } from "./StorageManager.js";
19
19
 
@@ -43,7 +43,22 @@ export interface StaticConfig {
43
43
  root: string;
44
44
  /** URL prefix (default: /static). */
45
45
  prefix?: string;
46
- /** Max-age for Cache-Control header in seconds (default: 86400 = 1 day). */
46
+ /**
47
+ * `max-age` in seconds. Default 0, as AdonisJS's static server is.
48
+ *
49
+ * Zero does NOT mean "do not cache": the response still carries an ETag and
50
+ * a Last-Modified, so a browser keeps the bytes and asks whether they are
51
+ * still good — answered with a 304 and no body. What zero removes is the
52
+ * window in which it does not ask at all.
53
+ *
54
+ * The old default was a day, which is right for fingerprinted assets and
55
+ * wrong for everything else: a stylesheet rebuilt during development was
56
+ * not re-fetched for 24 hours, so the page kept rendering with the previous
57
+ * one and nothing about it looked broken.
58
+ *
59
+ * Set it high for content served under a content-addressed name, or put a
60
+ * CDN in front — which is what upstream recommends for production anyway.
61
+ */
47
62
  maxAge?: number;
48
63
  /** File extensions to serve (default: common web assets). */
49
64
  extensions?: string[];
@@ -71,7 +86,7 @@ export class StaticMiddleware {
71
86
  // detect symlink-based escapes consistently.
72
87
  this.#root = fs.existsSync(resolved) ? fs.realpathSync(resolved) : resolved;
73
88
  this.#prefix = prefix;
74
- this.#maxAge = config.maxAge ?? 86400;
89
+ this.#maxAge = config.maxAge ?? 0;
75
90
  this.#extensions = new Set(config.extensions ?? DEFAULT_EXTENSIONS);
76
91
  }
77
92
 
@@ -142,13 +157,14 @@ export class StaticMiddleware {
142
157
  return next();
143
158
  }
144
159
 
145
- const etag = createHash("md5")
146
- .update(`${stat.size}-${stat.mtimeMs}`)
147
- .digest("hex");
160
+ // Quoted and weak (RFC 9110 §8.8.3): the bare md5 hex this used to send
161
+ // is not a valid entity-tag, and a strict cache may ignore it outright.
162
+ const etag = statTag(stat);
148
163
 
149
- // ETag check — 304 Not Modified
150
- const ifNoneMatch = ctx.request.header("if-none-match");
151
- if (ifNoneMatch === etag) {
164
+ // ETag check — 304 Not Modified. The header is a list compared weakly
165
+ // (§13.1.2); raw equality missed a client sending two tags, or one
166
+ // sending back `W/"x"` for a tag we wrote as `"x"`.
167
+ if (matchesIfNoneMatch(ctx.request.header("if-none-match"), etag)) {
152
168
  ctx.response.status(304);
153
169
  return;
154
170
  }
@@ -158,16 +174,40 @@ export class StaticMiddleware {
158
174
  ctx.response.header("Content-Length", String(stat.size));
159
175
  ctx.response.header("Cache-Control", `public, max-age=${this.#maxAge}`);
160
176
  ctx.response.header("ETag", etag);
177
+ // Sent alongside the ETag, as upstream does. A client that revalidates
178
+ // with `If-Modified-Since` rather than `If-None-Match` has nothing to
179
+ // compare against without it.
180
+ ctx.response.header("Last-Modified", stat.mtime.toUTCString());
161
181
 
162
182
  if (ctx.request.method() === "HEAD") {
163
183
  ctx.response.status(200);
164
184
  return;
165
185
  }
166
186
 
167
- // Read the canonicalised path — closes the symlink TOCTOU window
168
- // between the realpath check above and the read. `filePath` could
169
- // resolve through a symlink that was swapped after we validated it.
170
- const content = await fsp.readFile(realPath);
187
+ // Opened with O_NOFOLLOW rather than read by name.
188
+ //
189
+ // Reading the canonicalised path narrowed the window but did not close
190
+ // it: between `realpath` and the read, the last component of that path
191
+ // can be replaced by a symlink pointing anywhere, and a read by name
192
+ // follows it. Refusing to follow a link at open time takes the swap off
193
+ // the table — the open fails instead of serving a file outside the root.
194
+ //
195
+ // A parent directory swapped in the same window is not covered; that
196
+ // needs openat-relative-to-a-descriptor, which Node does not expose.
197
+ let content: Buffer;
198
+ try {
199
+ const handle = await fsp.open(
200
+ realPath,
201
+ fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW,
202
+ );
203
+ try {
204
+ content = await handle.readFile();
205
+ } finally {
206
+ await handle.close();
207
+ }
208
+ } catch {
209
+ return next();
210
+ }
171
211
  // Send the raw Buffer — NOT content.toString() which would destroy binary
172
212
  // data (PNG, PDF, ZIP, etc.) by interpreting it as UTF-8.
173
213
  ctx.response.status(200).sendBuffer(content);