@c9up/archive 0.1.14 → 0.1.15

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.
@@ -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,M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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,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACrD,QAAQ,GAAG,OAAO,EAAE,CAAC;YACrB,OAAO,GAAG,SAAS,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,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.15",
4
4
  "description": "Archive — File storage abstraction for the Ream framework (Local + S3-compatible drivers)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -31,7 +31,7 @@ import {
31
31
  type StorageDriver,
32
32
  StorageManager,
33
33
  } from "./StorageManager.js";
34
- import { setStorage } from "./services/main.js";
34
+ import { setStorage, setStorageResolver } from "./services/main.js";
35
35
 
36
36
  interface ArchiveContainer {
37
37
  singleton(token: unknown, factory: () => unknown): void;
@@ -64,31 +64,70 @@ const DEFAULT_CONFIG: ArchiveConfig = {
64
64
  };
65
65
 
66
66
  export default class ArchiveProvider {
67
+ /**
68
+ * The one lazily-built manager, read by BOTH ways in. Set by
69
+ * {@link register}, which always runs before {@link boot}.
70
+ */
71
+ #storage: (() => StorageManager) | undefined;
72
+
67
73
  constructor(protected app: ArchiveAppContext) {}
68
74
 
69
75
  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
- });
76
+ // ONE cell, shared by the container's bindings and the service accessor.
77
+ //
78
+ // The container had its own factory and boot() built a second manager
79
+ // for the accessor, so `services/main`'s storage and
80
+ // `container.resolve(StorageManager)` were different objects over
81
+ // different drivers: a fake bound in a test, or a driver swapped at
82
+ // runtime, was invisible from the other side.
83
+ //
84
+ // Memoised here rather than resolved through the container because the
85
+ // accessor is synchronous and the container is not — which is exactly
86
+ // why boot() could not simply reuse the container's instance.
87
+ let drive: DriveManager | undefined;
88
+ const driveManager = (): DriveManager => {
89
+ drive ??= new DriveManager(
90
+ resolveDriveConfig(
91
+ this.app.config.get<ArchiveConfig | DriveConfig>("archive"),
92
+ ),
93
+ );
94
+ return drive;
95
+ };
96
+ let manager: StorageManager | undefined;
97
+ const storageManager = (): StorageManager => {
98
+ manager ??= driveManager().use();
99
+ return manager;
100
+ };
101
+ this.#storage = storageManager;
102
+
103
+ this.app.container.singleton(DriveManager, () => driveManager());
74
104
  // Backward-compatible bindings — the default disk is what apps
75
105
  // 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
- );
106
+ this.app.container.singleton(StorageManager, () => storageManager());
107
+ this.app.container.singleton("storage", () => storageManager());
108
+ this.app.container.singleton("drive", () => driveManager());
85
109
  }
86
110
 
87
111
  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.
112
+ const storage = this.#storage;
113
+ if (storage === undefined) {
114
+ throw new ArchiveError(
115
+ "E_ARCHIVE_PROVIDER_NOT_REGISTERED",
116
+ "ArchiveProvider.boot() ran before register().",
117
+ {
118
+ hint: "Providers register before they boot — call register() first.",
119
+ },
120
+ );
121
+ }
122
+ // The accessor reads the same cell the container's bindings read, so
123
+ // however storage is reached there is one manager behind it.
124
+ setStorageResolver(storage);
125
+
126
+ // No eager construction when archive is unconfigured: an app that
127
+ // registers this provider and never uses storage should not trigger
128
+ // LocalDriver's mkdir of './storage' at boot, which fails on a
129
+ // read-only rootfs. The resolver above is what keeps that lazy case
130
+ // answering instead of throwing "before boot".
92
131
  const archive = this.app.config.get<ArchiveConfig | DriveConfig>("archive");
93
132
  if (
94
133
  archive === undefined ||
@@ -97,9 +136,10 @@ export default class ArchiveProvider {
97
136
  ) {
98
137
  return;
99
138
  }
100
- const manager =
101
- await this.app.container.resolve<StorageManager>(StorageManager);
102
- setStorage(manager);
139
+ // Configured: build now, so a misspelled driver or a missing
140
+ // driver-specific block is a boot failure rather than a surprise on the
141
+ // first upload.
142
+ setStorage(storage());
103
143
  }
104
144
 
105
145
  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
 
@@ -142,13 +142,14 @@ export class StaticMiddleware {
142
142
  return next();
143
143
  }
144
144
 
145
- const etag = createHash("md5")
146
- .update(`${stat.size}-${stat.mtimeMs}`)
147
- .digest("hex");
145
+ // Quoted and weak (RFC 9110 §8.8.3): the bare md5 hex this used to send
146
+ // is not a valid entity-tag, and a strict cache may ignore it outright.
147
+ const etag = statTag(stat);
148
148
 
149
- // ETag check — 304 Not Modified
150
- const ifNoneMatch = ctx.request.header("if-none-match");
151
- if (ifNoneMatch === etag) {
149
+ // ETag check — 304 Not Modified. The header is a list compared weakly
150
+ // (§13.1.2); raw equality missed a client sending two tags, or one
151
+ // sending back `W/"x"` for a tag we wrote as `"x"`.
152
+ if (matchesIfNoneMatch(ctx.request.header("if-none-match"), etag)) {
152
153
  ctx.response.status(304);
153
154
  return;
154
155
  }
@@ -164,10 +165,30 @@ export class StaticMiddleware {
164
165
  return;
165
166
  }
166
167
 
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);
168
+ // Opened with O_NOFOLLOW rather than read by name.
169
+ //
170
+ // Reading the canonicalised path narrowed the window but did not close
171
+ // it: between `realpath` and the read, the last component of that path
172
+ // can be replaced by a symlink pointing anywhere, and a read by name
173
+ // follows it. Refusing to follow a link at open time takes the swap off
174
+ // the table — the open fails instead of serving a file outside the root.
175
+ //
176
+ // A parent directory swapped in the same window is not covered; that
177
+ // needs openat-relative-to-a-descriptor, which Node does not expose.
178
+ let content: Buffer;
179
+ try {
180
+ const handle = await fsp.open(
181
+ realPath,
182
+ fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW,
183
+ );
184
+ try {
185
+ content = await handle.readFile();
186
+ } finally {
187
+ await handle.close();
188
+ }
189
+ } catch {
190
+ return next();
191
+ }
171
192
  // Send the raw Buffer — NOT content.toString() which would destroy binary
172
193
  // data (PNG, PDF, ZIP, etc.) by interpreting it as UTF-8.
173
194
  ctx.response.status(200).sendBuffer(content);