wire-mesh-core 4.2.6 → 4.2.7
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/adapters/node-fs-layout.cjs +35 -0
- package/dist/adapters/node-fs-layout.d.cts +24 -0
- package/dist/adapters/node-fs-layout.d.mts +24 -0
- package/dist/adapters/node-fs-layout.mjs +32 -0
- package/dist/adapters/node-fs-storage.cjs +5 -4
- package/dist/adapters/node-fs-storage.mjs +5 -4
- package/package.json +5 -1
|
@@ -0,0 +1,35 @@
|
|
|
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, `.`, `..`). 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
|
+
}
|
|
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
|
+
exports.InvalidStorageKeyError = InvalidStorageKeyError;
|
|
34
|
+
exports.refusalOfKey = refusalOfKey;
|
|
35
|
+
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. The refusals are the same on every host as far as separators and traversal go: `/` 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, drive-relative forms, 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";
|
|
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, `.`, `..`). 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. The refusals are the same on every host as far as separators and traversal go: `/` 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, drive-relative forms, 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";
|
|
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, `.`, `..`). 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,32 @@
|
|
|
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, `.`, `..`). 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
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Thrown by a storage operation given a key the layout refuses, rather than storing it under another name. */
|
|
17
|
+
var InvalidStorageKeyError = class extends Error {
|
|
18
|
+
refusal;
|
|
19
|
+
constructor(key, refusal) {
|
|
20
|
+
super(`invalid storage key ${JSON.stringify(key)}: ${refusal}`);
|
|
21
|
+
this.name = "InvalidStorageKeyError";
|
|
22
|
+
this.refusal = refusal;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
/** 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. */
|
|
26
|
+
function segmentsOfKey(key) {
|
|
27
|
+
const refusal = refusalOfKey(key);
|
|
28
|
+
if (refusal !== void 0) throw new InvalidStorageKeyError(key, refusal);
|
|
29
|
+
return key.split("/");
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
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
|
|
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 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
|
|
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
|
|
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 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
|
|
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.
|
|
3
|
+
"version": "4.2.7",
|
|
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"
|