wire-mesh-core 4.2.6 → 4.2.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.
@@ -0,0 +1,36 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/adapters/node-fs-layout.ts
3
+ /**
4
+ * Checks `key` against the layout. The first rule it breaks is the refusal, taken in this order: empty, NUL, backslash, a leading slash, then each segment from the left (empty, `.`, `..`, a drive prefix: an ASCII letter followed by a colon). Undefined when the key is a key of the layout.
5
+ */
6
+ function refusalOfKey(key) {
7
+ if (key === "") return "empty";
8
+ if (key.includes("\0")) return "nul";
9
+ if (key.includes("\\")) return "backslash";
10
+ if (key.startsWith("/")) return "absolute";
11
+ for (const segment of key.split("/")) {
12
+ if (segment === "") return "empty_segment";
13
+ if (segment === ".") return "dot_segment";
14
+ if (segment === "..") return "parent_segment";
15
+ if (/^[A-Za-z]:/u.test(segment)) return "drive_prefix";
16
+ }
17
+ }
18
+ /** Thrown by a storage operation given a key the layout refuses, rather than storing it under another name. */
19
+ var InvalidStorageKeyError = class extends Error {
20
+ refusal;
21
+ constructor(key, refusal) {
22
+ super(`invalid storage key ${JSON.stringify(key)}: ${refusal}`);
23
+ this.name = "InvalidStorageKeyError";
24
+ this.refusal = refusal;
25
+ }
26
+ };
27
+ /** The path segments of a key of the layout, in order: the file is these joined onto the store's directory. Throws InvalidStorageKeyError for a key the layout refuses. */
28
+ function segmentsOfKey(key) {
29
+ const refusal = refusalOfKey(key);
30
+ if (refusal !== void 0) throw new InvalidStorageKeyError(key, refusal);
31
+ return key.split("/");
32
+ }
33
+ //#endregion
34
+ exports.InvalidStorageKeyError = InvalidStorageKeyError;
35
+ exports.refusalOfKey = refusalOfKey;
36
+ exports.segmentsOfKey = segmentsOfKey;
@@ -0,0 +1,24 @@
1
+ //#region src/adapters/node-fs-layout.d.ts
2
+ /**
3
+ The layout of a file-backed key-value store, as pure functions: which keys the layout accepts, and the file a key names. The TypeScript implementation of the layout lives here, and a directory written by any implementation that follows it is read by another. A key is a relative path of `/`-separated segments under the store's directory, and its file holds the value's bytes and nothing else.
4
+
5
+ A key that `path.join` would rewrite (empty, `.` and `..` segments, a doubled or trailing separator) or that leaves the directory would be stored under a name other than its own and listed back as another key, so it is refused. So is a segment that starts with an ASCII letter and a colon, which Windows reads as a drive prefix that replaces the directory when joined onto it (`C:/x`) or names the drive's current directory (`C:x`). The refusals are the same on every host: `/` is the only separator, and a backslash is refused so that the layout does not depend on the operating system. Names that only some filesystems rewrite (Windows alternate data streams, trailing dots or spaces, reserved device names, case folding, Unicode normalisation) are not covered.
6
+
7
+ Containment is lexical: the rule decides from the key's text alone that the file lies inside the directory, so a symlink placed inside the directory by someone who can already write there is still followed.
8
+
9
+ `KeyRefusal` is a closed union, so a rule added later is a type-level change for a consumer that switches on it exhaustively.
10
+ */
11
+ /** Why a key is not a key of the layout, by the name the conformance vectors use. */
12
+ export type KeyRefusal = "empty" | "nul" | "backslash" | "absolute" | "empty_segment" | "dot_segment" | "parent_segment" | "drive_prefix";
13
+ /**
14
+ * Checks `key` against the layout. The first rule it breaks is the refusal, taken in this order: empty, NUL, backslash, a leading slash, then each segment from the left (empty, `.`, `..`, a drive prefix: an ASCII letter followed by a colon). Undefined when the key is a key of the layout.
15
+ */
16
+ export declare function refusalOfKey(key: string): KeyRefusal | undefined;
17
+ /** Thrown by a storage operation given a key the layout refuses, rather than storing it under another name. */
18
+ export declare class InvalidStorageKeyError extends Error {
19
+ readonly refusal: KeyRefusal;
20
+ constructor(key: string, refusal: KeyRefusal);
21
+ }
22
+ /** The path segments of a key of the layout, in order: the file is these joined onto the store's directory. Throws InvalidStorageKeyError for a key the layout refuses. */
23
+ export declare function segmentsOfKey(key: string): string[];
24
+ //#endregion
@@ -0,0 +1,24 @@
1
+ //#region src/adapters/node-fs-layout.d.ts
2
+ /**
3
+ The layout of a file-backed key-value store, as pure functions: which keys the layout accepts, and the file a key names. The TypeScript implementation of the layout lives here, and a directory written by any implementation that follows it is read by another. A key is a relative path of `/`-separated segments under the store's directory, and its file holds the value's bytes and nothing else.
4
+
5
+ A key that `path.join` would rewrite (empty, `.` and `..` segments, a doubled or trailing separator) or that leaves the directory would be stored under a name other than its own and listed back as another key, so it is refused. So is a segment that starts with an ASCII letter and a colon, which Windows reads as a drive prefix that replaces the directory when joined onto it (`C:/x`) or names the drive's current directory (`C:x`). The refusals are the same on every host: `/` is the only separator, and a backslash is refused so that the layout does not depend on the operating system. Names that only some filesystems rewrite (Windows alternate data streams, trailing dots or spaces, reserved device names, case folding, Unicode normalisation) are not covered.
6
+
7
+ Containment is lexical: the rule decides from the key's text alone that the file lies inside the directory, so a symlink placed inside the directory by someone who can already write there is still followed.
8
+
9
+ `KeyRefusal` is a closed union, so a rule added later is a type-level change for a consumer that switches on it exhaustively.
10
+ */
11
+ /** Why a key is not a key of the layout, by the name the conformance vectors use. */
12
+ export type KeyRefusal = "empty" | "nul" | "backslash" | "absolute" | "empty_segment" | "dot_segment" | "parent_segment" | "drive_prefix";
13
+ /**
14
+ * Checks `key` against the layout. The first rule it breaks is the refusal, taken in this order: empty, NUL, backslash, a leading slash, then each segment from the left (empty, `.`, `..`, a drive prefix: an ASCII letter followed by a colon). Undefined when the key is a key of the layout.
15
+ */
16
+ export declare function refusalOfKey(key: string): KeyRefusal | undefined;
17
+ /** Thrown by a storage operation given a key the layout refuses, rather than storing it under another name. */
18
+ export declare class InvalidStorageKeyError extends Error {
19
+ readonly refusal: KeyRefusal;
20
+ constructor(key: string, refusal: KeyRefusal);
21
+ }
22
+ /** The path segments of a key of the layout, in order: the file is these joined onto the store's directory. Throws InvalidStorageKeyError for a key the layout refuses. */
23
+ export declare function segmentsOfKey(key: string): string[];
24
+ //#endregion
@@ -0,0 +1,33 @@
1
+ //#region src/adapters/node-fs-layout.ts
2
+ /**
3
+ * Checks `key` against the layout. The first rule it breaks is the refusal, taken in this order: empty, NUL, backslash, a leading slash, then each segment from the left (empty, `.`, `..`, a drive prefix: an ASCII letter followed by a colon). Undefined when the key is a key of the layout.
4
+ */
5
+ function refusalOfKey(key) {
6
+ if (key === "") return "empty";
7
+ if (key.includes("\0")) return "nul";
8
+ if (key.includes("\\")) return "backslash";
9
+ if (key.startsWith("/")) return "absolute";
10
+ for (const segment of key.split("/")) {
11
+ if (segment === "") return "empty_segment";
12
+ if (segment === ".") return "dot_segment";
13
+ if (segment === "..") return "parent_segment";
14
+ if (/^[A-Za-z]:/u.test(segment)) return "drive_prefix";
15
+ }
16
+ }
17
+ /** Thrown by a storage operation given a key the layout refuses, rather than storing it under another name. */
18
+ var InvalidStorageKeyError = class extends Error {
19
+ refusal;
20
+ constructor(key, refusal) {
21
+ super(`invalid storage key ${JSON.stringify(key)}: ${refusal}`);
22
+ this.name = "InvalidStorageKeyError";
23
+ this.refusal = refusal;
24
+ }
25
+ };
26
+ /** The path segments of a key of the layout, in order: the file is these joined onto the store's directory. Throws InvalidStorageKeyError for a key the layout refuses. */
27
+ function segmentsOfKey(key) {
28
+ const refusal = refusalOfKey(key);
29
+ if (refusal !== void 0) throw new InvalidStorageKeyError(key, refusal);
30
+ return key.split("/");
31
+ }
32
+ //#endregion
33
+ export { InvalidStorageKeyError, refusalOfKey, segmentsOfKey };
@@ -1,20 +1,21 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_rolldown_runtime = require("../rolldown-runtime-VH7oDXx4.cjs");
3
+ const require_adapters_node_fs_layout = require("./node-fs-layout.cjs");
3
4
  let node_fs_promises = require("node:fs/promises");
4
5
  node_fs_promises = require_rolldown_runtime.__toESM(node_fs_promises, 1);
5
6
  let node_path = require("node:path");
6
7
  node_path = require_rolldown_runtime.__toESM(node_path, 1);
7
8
  //#region src/adapters/node-fs-storage.ts
8
- /** A Node filesystem-backed KeyValueStorage -- the durable Node-side counterpart to indexeddb-storage.ts's browser one, and memory-storage.ts's in-process one, satisfying the identical contract. Every key is treated directly as a relative path under the given directory (creating nested directories as needed), which every key this codebase constructs is already suited to: internally generated, ASCII, path-shaped by design (e.g. "data/<peer-hex>/entry/<seq>") rather than arbitrary untrusted input, matching the same assumption indexeddb-storage.ts's own comment already states for its key range bounds. */
9
+ /** A Node filesystem-backed KeyValueStorage, the durable Node-side counterpart to indexeddb-storage.ts's browser one, and memory-storage.ts's in-process one, satisfying the identical contract. Every key is a relative path under the given directory (creating nested directories as needed), in the layout node-fs-layout.ts defines, so a directory written by one implementation is read by another. A key the layout refuses (empty, `.` and `..` segments, a segment starting like a drive such as `C:`, a leading, doubled or trailing separator, a backslash, a NUL) throws InvalidStorageKeyError, since a key that left the directory or that `path.join` rewrites would be stored under a name other than its own. keys() lists only names the layout accepts: a file already in the directory under a refused name (one containing a backslash, or written outside the layout by other means) is skipped, so every listed key can be passed to get, set and delete. A store written before the layout was enforced keeps its files: a key `path.join` rewrote is read under its normalised spelling, and the old spelling now throws. */
9
10
  function isEnoent(error) {
10
11
  if (!(error instanceof Error)) return false;
11
12
  if (!("code" in error)) return false;
12
13
  return error.code === "ENOENT";
13
14
  }
14
15
  function keyToFilePath(baseDir, key) {
15
- return node_path.join(baseDir, key);
16
+ return node_path.join(baseDir, ...require_adapters_node_fs_layout.segmentsOfKey(key));
16
17
  }
17
- /** Every regular file under baseDir, as a relative path using "/" separators regardless of the host OS -- so a caller's own "/"-joined key strings compare correctly against what this returns, the same key shape set()/get() already use. */
18
+ /** Every regular file under baseDir, as a relative path using "/" separators regardless of the host OS, so a caller's own "/"-joined key strings compare correctly against what this returns, the same key shape set()/get() already use. */
18
19
  async function listAllRelativePaths(baseDir) {
19
20
  let entries;
20
21
  try {
@@ -26,7 +27,7 @@ async function listAllRelativePaths(baseDir) {
26
27
  return (await Promise.all(entries.map(async (entry) => ({
27
28
  entry,
28
29
  stat: await node_fs_promises.stat(node_path.join(baseDir, entry)).catch(() => null)
29
- })))).filter(({ stat }) => stat?.isFile() === true).map(({ entry }) => entry.split(node_path.sep).join("/"));
30
+ })))).filter(({ stat }) => stat?.isFile() === true).map(({ entry }) => entry.split(node_path.sep).join("/")).filter((key) => require_adapters_node_fs_layout.refusalOfKey(key) === void 0);
30
31
  }
31
32
  function createNodeFsStorage(options) {
32
33
  const baseDir = options.dir;
@@ -1,16 +1,17 @@
1
+ import { refusalOfKey, segmentsOfKey } from "./node-fs-layout.mjs";
1
2
  import * as fs from "node:fs/promises";
2
3
  import * as path from "node:path";
3
4
  //#region src/adapters/node-fs-storage.ts
4
- /** A Node filesystem-backed KeyValueStorage -- the durable Node-side counterpart to indexeddb-storage.ts's browser one, and memory-storage.ts's in-process one, satisfying the identical contract. Every key is treated directly as a relative path under the given directory (creating nested directories as needed), which every key this codebase constructs is already suited to: internally generated, ASCII, path-shaped by design (e.g. "data/<peer-hex>/entry/<seq>") rather than arbitrary untrusted input, matching the same assumption indexeddb-storage.ts's own comment already states for its key range bounds. */
5
+ /** A Node filesystem-backed KeyValueStorage, the durable Node-side counterpart to indexeddb-storage.ts's browser one, and memory-storage.ts's in-process one, satisfying the identical contract. Every key is a relative path under the given directory (creating nested directories as needed), in the layout node-fs-layout.ts defines, so a directory written by one implementation is read by another. A key the layout refuses (empty, `.` and `..` segments, a segment starting like a drive such as `C:`, a leading, doubled or trailing separator, a backslash, a NUL) throws InvalidStorageKeyError, since a key that left the directory or that `path.join` rewrites would be stored under a name other than its own. keys() lists only names the layout accepts: a file already in the directory under a refused name (one containing a backslash, or written outside the layout by other means) is skipped, so every listed key can be passed to get, set and delete. A store written before the layout was enforced keeps its files: a key `path.join` rewrote is read under its normalised spelling, and the old spelling now throws. */
5
6
  function isEnoent(error) {
6
7
  if (!(error instanceof Error)) return false;
7
8
  if (!("code" in error)) return false;
8
9
  return error.code === "ENOENT";
9
10
  }
10
11
  function keyToFilePath(baseDir, key) {
11
- return path.join(baseDir, key);
12
+ return path.join(baseDir, ...segmentsOfKey(key));
12
13
  }
13
- /** Every regular file under baseDir, as a relative path using "/" separators regardless of the host OS -- so a caller's own "/"-joined key strings compare correctly against what this returns, the same key shape set()/get() already use. */
14
+ /** Every regular file under baseDir, as a relative path using "/" separators regardless of the host OS, so a caller's own "/"-joined key strings compare correctly against what this returns, the same key shape set()/get() already use. */
14
15
  async function listAllRelativePaths(baseDir) {
15
16
  let entries;
16
17
  try {
@@ -22,7 +23,7 @@ async function listAllRelativePaths(baseDir) {
22
23
  return (await Promise.all(entries.map(async (entry) => ({
23
24
  entry,
24
25
  stat: await fs.stat(path.join(baseDir, entry)).catch(() => null)
25
- })))).filter(({ stat }) => stat?.isFile() === true).map(({ entry }) => entry.split(path.sep).join("/"));
26
+ })))).filter(({ stat }) => stat?.isFile() === true).map(({ entry }) => entry.split(path.sep).join("/")).filter((key) => refusalOfKey(key) === void 0);
26
27
  }
27
28
  function createNodeFsStorage(options) {
28
29
  const baseDir = options.dir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wire-mesh-core",
3
- "version": "4.2.6",
3
+ "version": "4.2.8",
4
4
  "dependencies": {
5
5
  "cbor2": "2.3.0",
6
6
  "cddl.js": "2.0.0",
@@ -39,6 +39,10 @@
39
39
  "import": "./dist/adapters/memory-storage.mjs",
40
40
  "require": "./dist/adapters/memory-storage.cjs"
41
41
  },
42
+ "./adapters/node-fs-layout": {
43
+ "import": "./dist/adapters/node-fs-layout.mjs",
44
+ "require": "./dist/adapters/node-fs-layout.cjs"
45
+ },
42
46
  "./adapters/node-fs-storage": {
43
47
  "import": "./dist/adapters/node-fs-storage.mjs",
44
48
  "require": "./dist/adapters/node-fs-storage.cjs"