@pgxsinkit/pgwasm 0.5.0
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/LICENSE +21 -0
- package/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +111 -0
- package/README.md +56 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +21 -0
- package/dist/build/index.js.map +9 -0
- package/dist/build/seam.d.ts +174 -0
- package/dist/build/seam.d.ts.map +1 -0
- package/dist/chunk-0w227vw2.js +233 -0
- package/dist/chunk-0w227vw2.js.map +11 -0
- package/dist/chunk-74gcrk47.js +563 -0
- package/dist/chunk-74gcrk47.js.map +14 -0
- package/dist/chunk-at1py5s5.js +373 -0
- package/dist/chunk-at1py5s5.js.map +12 -0
- package/dist/chunk-cpsnmzrf.js +126 -0
- package/dist/chunk-cpsnmzrf.js.map +10 -0
- package/dist/chunk-d82gg1hx.js +306 -0
- package/dist/chunk-d82gg1hx.js.map +10 -0
- package/dist/chunk-gjf102c9.js +1005 -0
- package/dist/chunk-gjf102c9.js.map +16 -0
- package/dist/chunk-kb3rsr7n.js +19 -0
- package/dist/chunk-kb3rsr7n.js.map +9 -0
- package/dist/chunk-tdvrvgt5.js +83 -0
- package/dist/chunk-tdvrvgt5.js.map +11 -0
- package/dist/chunk-z0c40tja.js +50 -0
- package/dist/chunk-z0c40tja.js.map +10 -0
- package/dist/core/data-dir-archive.d.ts +17 -0
- package/dist/core/data-dir-archive.d.ts.map +1 -0
- package/dist/core/internals.d.ts +59 -0
- package/dist/core/internals.d.ts.map +1 -0
- package/dist/core/marker.d.ts +35 -0
- package/dist/core/marker.d.ts.map +1 -0
- package/dist/core/mutex.d.ts +9 -0
- package/dist/core/mutex.d.ts.map +1 -0
- package/dist/core/names.d.ts +11 -0
- package/dist/core/names.d.ts.map +1 -0
- package/dist/core/parse.d.ts +11 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/core/pgwasm.d.ts +53 -0
- package/dist/core/pgwasm.d.ts.map +1 -0
- package/dist/core/storage.d.ts +14 -0
- package/dist/core/storage.d.ts.map +1 -0
- package/dist/create.d.ts +10 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/drizzle/codecs.d.ts +9 -0
- package/dist/drizzle/codecs.d.ts.map +1 -0
- package/dist/drizzle/driver.d.ts +50 -0
- package/dist/drizzle/driver.d.ts.map +1 -0
- package/dist/drizzle/index.d.ts +4 -0
- package/dist/drizzle/index.d.ts.map +1 -0
- package/dist/drizzle/index.js +261 -0
- package/dist/drizzle/index.js.map +12 -0
- package/dist/drizzle/session.d.ts +39 -0
- package/dist/drizzle/session.d.ts.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fs/base-filesystem.d.ts +118 -0
- package/dist/fs/base-filesystem.d.ts.map +1 -0
- package/dist/fs/index.d.ts +2 -0
- package/dist/fs/index.d.ts.map +1 -0
- package/dist/fs/index.js +11 -0
- package/dist/fs/index.js.map +9 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +9 -0
- package/dist/interface.d.ts +165 -0
- package/dist/interface.d.ts.map +1 -0
- package/dist/live/debounce-mutex.d.ts +6 -0
- package/dist/live/debounce-mutex.d.ts.map +1 -0
- package/dist/live/format-query.d.ts +8 -0
- package/dist/live/format-query.d.ts.map +1 -0
- package/dist/live/index.d.ts +9 -0
- package/dist/live/index.d.ts.map +1 -0
- package/dist/live/index.js +601 -0
- package/dist/live/index.js.map +12 -0
- package/dist/live/interface.d.ts +86 -0
- package/dist/live/interface.d.ts.map +1 -0
- package/dist/opfs/broker/client.d.ts +163 -0
- package/dist/opfs/broker/client.d.ts.map +1 -0
- package/dist/opfs/broker/protocol.d.ts +326 -0
- package/dist/opfs/broker/protocol.d.ts.map +1 -0
- package/dist/opfs/broker/server.d.ts +84 -0
- package/dist/opfs/broker/server.d.ts.map +1 -0
- package/dist/opfs/core/checksum.d.ts +3 -0
- package/dist/opfs/core/checksum.d.ts.map +1 -0
- package/dist/opfs/core/codec.d.ts +73 -0
- package/dist/opfs/core/codec.d.ts.map +1 -0
- package/dist/opfs/core/errors.d.ts +80 -0
- package/dist/opfs/core/errors.d.ts.map +1 -0
- package/dist/opfs/core/limits.d.ts +35 -0
- package/dist/opfs/core/limits.d.ts.map +1 -0
- package/dist/opfs/core/memory-port.d.ts +51 -0
- package/dist/opfs/core/memory-port.d.ts.map +1 -0
- package/dist/opfs/core/metadata-store.d.ts +19 -0
- package/dist/opfs/core/metadata-store.d.ts.map +1 -0
- package/dist/opfs/core/mounted-vfs.d.ts +119 -0
- package/dist/opfs/core/mounted-vfs.d.ts.map +1 -0
- package/dist/opfs/core/path.d.ts +14 -0
- package/dist/opfs/core/path.d.ts.map +1 -0
- package/dist/opfs/core/port.d.ts +30 -0
- package/dist/opfs/core/port.d.ts.map +1 -0
- package/dist/opfs/core/repacked-vfs.d.ts +138 -0
- package/dist/opfs/core/repacked-vfs.d.ts.map +1 -0
- package/dist/opfs/core/state-machine.d.ts +296 -0
- package/dist/opfs/core/state-machine.d.ts.map +1 -0
- package/dist/opfs/create.d.ts +50 -0
- package/dist/opfs/create.d.ts.map +1 -0
- package/dist/opfs/file-port.d.ts +14 -0
- package/dist/opfs/file-port.d.ts.map +1 -0
- package/dist/opfs/index.d.ts +37 -0
- package/dist/opfs/index.d.ts.map +1 -0
- package/dist/opfs/index.js +6671 -0
- package/dist/opfs/index.js.map +28 -0
- package/dist/opfs/opfs-port.d.ts +34 -0
- package/dist/opfs/opfs-port.d.ts.map +1 -0
- package/dist/opfs/opfs-repacked-fs.d.ts +55 -0
- package/dist/opfs/opfs-repacked-fs.d.ts.map +1 -0
- package/dist/opfs/wasi/preview1.d.ts +228 -0
- package/dist/opfs/wasi/preview1.d.ts.map +1 -0
- package/dist/protocol/index.d.ts +15 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +23 -0
- package/dist/protocol/index.js.map +10 -0
- package/dist/protocol/wire/buffer-reader.d.ts +13 -0
- package/dist/protocol/wire/buffer-reader.d.ts.map +1 -0
- package/dist/protocol/wire/buffer-writer.d.ts +12 -0
- package/dist/protocol/wire/buffer-writer.d.ts.map +1 -0
- package/dist/protocol/wire/index.d.ts +5 -0
- package/dist/protocol/wire/index.d.ts.map +1 -0
- package/dist/protocol/wire/messages.d.ts +194 -0
- package/dist/protocol/wire/messages.d.ts.map +1 -0
- package/dist/protocol/wire/parser.d.ts +12 -0
- package/dist/protocol/wire/parser.d.ts.map +1 -0
- package/dist/protocol/wire/serializer.d.ts +61 -0
- package/dist/protocol/wire/serializer.d.ts.map +1 -0
- package/dist/protocol/wire/string-utils.d.ts +6 -0
- package/dist/protocol/wire/string-utils.d.ts.map +1 -0
- package/dist/protocol/wire/types.d.ts +7 -0
- package/dist/protocol/wire/types.d.ts.map +1 -0
- package/dist/tar/gzip.d.ts +7 -0
- package/dist/tar/gzip.d.ts.map +1 -0
- package/dist/tar/tar.d.ts +39 -0
- package/dist/tar/tar.d.ts.map +1 -0
- package/dist/templating.d.ts +42 -0
- package/dist/templating.d.ts.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
- package/skills/opfs/SKILL.md +138 -0
- package/src/build/index.ts +23 -0
- package/src/build/seam.ts +179 -0
- package/src/core/data-dir-archive.ts +68 -0
- package/src/core/internals.ts +87 -0
- package/src/core/marker.ts +94 -0
- package/src/core/mutex.ts +16 -0
- package/src/core/names.ts +26 -0
- package/src/core/parse.ts +79 -0
- package/src/core/pgwasm.ts +876 -0
- package/src/core/storage.ts +70 -0
- package/src/create.ts +134 -0
- package/src/drizzle/codecs.ts +114 -0
- package/src/drizzle/driver.ts +141 -0
- package/src/drizzle/index.ts +15 -0
- package/src/drizzle/session.ts +133 -0
- package/src/errors.ts +201 -0
- package/src/fs/base-filesystem.ts +149 -0
- package/src/fs/index.ts +7 -0
- package/src/index.ts +44 -0
- package/src/interface.ts +186 -0
- package/src/live/debounce-mutex.ts +45 -0
- package/src/live/format-query.ts +47 -0
- package/src/live/index.ts +707 -0
- package/src/live/interface.ts +109 -0
- package/src/opfs/broker/client.ts +482 -0
- package/src/opfs/broker/protocol.ts +624 -0
- package/src/opfs/broker/server.ts +522 -0
- package/src/opfs/core/checksum.ts +85 -0
- package/src/opfs/core/codec.ts +1051 -0
- package/src/opfs/core/errors.ts +134 -0
- package/src/opfs/core/limits.ts +68 -0
- package/src/opfs/core/memory-port.ts +459 -0
- package/src/opfs/core/metadata-store.ts +512 -0
- package/src/opfs/core/mounted-vfs.ts +407 -0
- package/src/opfs/core/path.ts +82 -0
- package/src/opfs/core/port.ts +114 -0
- package/src/opfs/core/repacked-vfs.ts +1464 -0
- package/src/opfs/core/state-machine.ts +1699 -0
- package/src/opfs/create.ts +125 -0
- package/src/opfs/file-port.ts +183 -0
- package/src/opfs/index.ts +113 -0
- package/src/opfs/opfs-port.ts +90 -0
- package/src/opfs/opfs-repacked-fs.ts +210 -0
- package/src/opfs/wasi/preview1.ts +1016 -0
- package/src/protocol/index.ts +24 -0
- package/src/protocol/wire/buffer-reader.ts +60 -0
- package/src/protocol/wire/buffer-writer.ts +93 -0
- package/src/protocol/wire/index.ts +15 -0
- package/src/protocol/wire/messages.ts +332 -0
- package/src/protocol/wire/parser.ts +373 -0
- package/src/protocol/wire/serializer.ts +233 -0
- package/src/protocol/wire/string-utils.ts +18 -0
- package/src/protocol/wire/types.ts +12 -0
- package/src/tar/gzip.ts +36 -0
- package/src/tar/tar.ts +253 -0
- package/src/templating.ts +103 -0
- package/src/types.ts +283 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `MountedRepackedVfs` — one {@link RepackedFileSystem} made of several stores, joined at path
|
|
3
|
+
* prefixes.
|
|
4
|
+
*
|
|
5
|
+
* ## Why
|
|
6
|
+
*
|
|
7
|
+
* A store is a single durable object: four exclusive handles, one arena, one metadata generation.
|
|
8
|
+
* That is exactly right for a datadir and exactly wrong for the parts of a datadir that must NOT be
|
|
9
|
+
* durable — a Postgres temp tablespace, an ephemeral relation, a scratch area whose whole point is
|
|
10
|
+
* that it never reaches the platform. A mount serves one path prefix from a SECOND store, so the
|
|
11
|
+
* durable root and the volatile subtree are one tree to the guest and two stores to the host.
|
|
12
|
+
*
|
|
13
|
+
* ## The shape
|
|
14
|
+
*
|
|
15
|
+
* / -> root store (OPFS: durable)
|
|
16
|
+
* /pgdata/... -> root store
|
|
17
|
+
* /scratch -> mount store (memory: nothing durable)
|
|
18
|
+
* /scratch/anything/... -> mount store, as "/anything/..."
|
|
19
|
+
*
|
|
20
|
+
* A mount's prefix is its root: `/scratch/a/b` reaches the mount as `/a/b`, and the prefix itself as
|
|
21
|
+
* `/`. The prefix directory is also created in the ROOT store, so a `readdir` of its parent lists
|
|
22
|
+
* it; every operation AT or BELOW the prefix is answered by the mount, so that root-side directory
|
|
23
|
+
* is a name and nothing more.
|
|
24
|
+
*
|
|
25
|
+
* ## What the composite owns, and what it delegates
|
|
26
|
+
*
|
|
27
|
+
* - **Symlink resolution is the composite's**, because a link is the one thing that can move a path
|
|
28
|
+
* from one store to another: `pg_tblspc/<oid>` lives in the root and points at `/scratch`, and
|
|
29
|
+
* neither store alone can resolve it (the root would resolve it against its own placeholder
|
|
30
|
+
* directory and find nothing there). The fast path is one `resolvePath` call on the store the
|
|
31
|
+
* path already routes to — if that store says the path holds no link, no link exists on it at
|
|
32
|
+
* all, because every entry on the path lives in that store. Only a path that really does traverse
|
|
33
|
+
* a link pays the component-by-component walk.
|
|
34
|
+
* - **Descriptors are the composite's.** Each store numbers its own fds from 3, so the composite
|
|
35
|
+
* hands out its own numbers and remembers which store each belongs to.
|
|
36
|
+
* - **`rename` across a boundary is `EXDEV`**, exactly as it is between two real filesystems: a
|
|
37
|
+
* rename is a metadata move inside one store and cannot span two.
|
|
38
|
+
* - **`strictSync()` skips a mount declared non-durable.** A memory-backed mount has nothing to make
|
|
39
|
+
* durable, and a store-wide sync that walked it would charge the guest for flushes that cannot
|
|
40
|
+
* protect anything. Durability is a property the STORAGE OWNER declares per mount; nothing here
|
|
41
|
+
* infers it from the port.
|
|
42
|
+
* - Everything else is a route plus a delegation.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
import { FS_ERRNO, FsError } from "./errors";
|
|
46
|
+
import { MAX_SYMLINK_HOPS } from "./limits";
|
|
47
|
+
import { parsePath } from "./path";
|
|
48
|
+
import type { RepackReason, RepackedFileSystem, RepackedStat, RepackedVfsMetrics } from "./repacked-vfs";
|
|
49
|
+
|
|
50
|
+
/** One store served at one path prefix. */
|
|
51
|
+
export interface RepackedMount {
|
|
52
|
+
/** Absolute, canonical, and never `/`: the path the mount's own root appears at. */
|
|
53
|
+
readonly prefix: string;
|
|
54
|
+
readonly vfs: RepackedFileSystem;
|
|
55
|
+
/**
|
|
56
|
+
* Whether a store-wide `strictSync()` reaches this mount. Defaults to `true`. A volatile mount
|
|
57
|
+
* (memory-backed scratch) declares `false`: there is nothing to flush, and pretending otherwise
|
|
58
|
+
* charges every guest `fsync` for it.
|
|
59
|
+
*/
|
|
60
|
+
readonly durable?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface MountedRepackedVfsOptions {
|
|
64
|
+
/** The store that answers for every path no mount claims. */
|
|
65
|
+
readonly root: RepackedFileSystem;
|
|
66
|
+
readonly mounts: readonly RepackedMount[];
|
|
67
|
+
/** The clock used to create each mount's placeholder directory in the root. */
|
|
68
|
+
readonly nowMs?: () => bigint;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface ResolvedMount {
|
|
72
|
+
readonly prefix: string;
|
|
73
|
+
readonly boundary: string;
|
|
74
|
+
readonly vfs: RepackedFileSystem;
|
|
75
|
+
readonly durable: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface Route {
|
|
79
|
+
readonly fs: RepackedFileSystem;
|
|
80
|
+
/** The path as the routed store sees it. */
|
|
81
|
+
readonly path: string;
|
|
82
|
+
/** The mount this route landed on, or `undefined` for the root store. */
|
|
83
|
+
readonly mount: ResolvedMount | undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface MountedDescriptor {
|
|
87
|
+
readonly fs: RepackedFileSystem;
|
|
88
|
+
readonly fd: number;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function joinPath(base: string, rest: readonly string[]): string {
|
|
92
|
+
if (rest.length === 0) return base;
|
|
93
|
+
return base === "/" ? `/${rest.join("/")}` : `${base}/${rest.join("/")}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isMissing(cause: unknown): boolean {
|
|
97
|
+
return cause instanceof FsError && (cause.code === FS_ERRNO.ENOENT || cause.code === FS_ERRNO.ENOTDIR);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export class MountedRepackedVfs implements RepackedFileSystem {
|
|
101
|
+
readonly #root: RepackedFileSystem;
|
|
102
|
+
readonly #mounts: readonly ResolvedMount[];
|
|
103
|
+
readonly #descriptors = new Map<number, MountedDescriptor>();
|
|
104
|
+
#nextDescriptor = 3;
|
|
105
|
+
|
|
106
|
+
constructor(options: MountedRepackedVfsOptions) {
|
|
107
|
+
this.#root = options.root;
|
|
108
|
+
const nowMs = (options.nowMs ?? (() => BigInt(Date.now())))();
|
|
109
|
+
const mounts: ResolvedMount[] = [];
|
|
110
|
+
for (const mount of options.mounts) {
|
|
111
|
+
if (parsePath(mount.prefix).length === 0) {
|
|
112
|
+
throw new FsError("EINVAL", "a mount prefix must be an absolute path below the root", {
|
|
113
|
+
path: mount.prefix,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
for (const existing of mounts) {
|
|
117
|
+
if (
|
|
118
|
+
existing.prefix === mount.prefix ||
|
|
119
|
+
mount.prefix.startsWith(existing.boundary) ||
|
|
120
|
+
existing.prefix.startsWith(`${mount.prefix}/`)
|
|
121
|
+
) {
|
|
122
|
+
throw new FsError("EINVAL", `mount prefix ${mount.prefix} overlaps ${existing.prefix}`, {
|
|
123
|
+
path: mount.prefix,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
mounts.push({
|
|
128
|
+
prefix: mount.prefix,
|
|
129
|
+
boundary: `${mount.prefix}/`,
|
|
130
|
+
vfs: mount.vfs,
|
|
131
|
+
durable: mount.durable ?? true,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
this.#mounts = mounts;
|
|
135
|
+
// The prefix is a NAME in the root store so a listing of its parent shows it; every operation
|
|
136
|
+
// at or below it is answered by the mount, so nothing is ever stored behind that name.
|
|
137
|
+
for (const mount of mounts) {
|
|
138
|
+
try {
|
|
139
|
+
this.#root.mkdir(mount.prefix, { recursive: true, nowMs });
|
|
140
|
+
} catch (cause) {
|
|
141
|
+
if (!(cause instanceof FsError) || cause.code !== FS_ERRNO.EEXIST) throw cause;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The mounts this composite serves, in declaration order. */
|
|
147
|
+
get mounts(): readonly RepackedMount[] {
|
|
148
|
+
return this.#mounts.map((mount) => ({ prefix: mount.prefix, vfs: mount.vfs, durable: mount.durable }));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The store that answers for everything no mount claims. */
|
|
152
|
+
get root(): RepackedFileSystem {
|
|
153
|
+
return this.#root;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ---- routing and resolution ---------------------------------------------
|
|
157
|
+
|
|
158
|
+
#route(path: string): Route {
|
|
159
|
+
parsePath(path);
|
|
160
|
+
for (const mount of this.#mounts) {
|
|
161
|
+
if (path === mount.prefix) return { fs: mount.vfs, path: "/", mount };
|
|
162
|
+
if (path.startsWith(mount.boundary)) {
|
|
163
|
+
return { fs: mount.vfs, path: path.slice(mount.prefix.length), mount };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { fs: this.#root, path, mount: undefined };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The link-free composite path `path` names.
|
|
171
|
+
*
|
|
172
|
+
* The fast path asks the routed store to resolve its own view: a store that reports the path
|
|
173
|
+
* unchanged proves there is no link on it, because every entry on that path lives in that store.
|
|
174
|
+
* Anything else falls through to the careful walk, which resolves one component at a time and can
|
|
175
|
+
* therefore cross a boundary in either direction.
|
|
176
|
+
*/
|
|
177
|
+
resolvePath(path: string, follow = true): string {
|
|
178
|
+
const route = this.#route(path);
|
|
179
|
+
if (route.fs.resolvePath(route.path, follow) === route.path) return path;
|
|
180
|
+
return this.#walk(path, follow);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
#walk(path: string, follow: boolean): string {
|
|
184
|
+
let pending = parsePath(path);
|
|
185
|
+
let index = 0;
|
|
186
|
+
const resolved: string[] = [];
|
|
187
|
+
let hops = 0;
|
|
188
|
+
while (index < pending.length) {
|
|
189
|
+
const candidate = joinPath(`/${resolved.join("/")}`, [pending[index]!]);
|
|
190
|
+
const route = this.#route(candidate);
|
|
191
|
+
let stat: RepackedStat;
|
|
192
|
+
try {
|
|
193
|
+
stat = route.fs.lstat(route.path);
|
|
194
|
+
} catch (cause) {
|
|
195
|
+
if (isMissing(cause)) break;
|
|
196
|
+
throw cause;
|
|
197
|
+
}
|
|
198
|
+
if (stat.kind === "symlink" && (follow || index < pending.length - 1)) {
|
|
199
|
+
hops += 1;
|
|
200
|
+
if (hops > MAX_SYMLINK_HOPS) {
|
|
201
|
+
throw new FsError("ELOOP", `path traverses more than ${MAX_SYMLINK_HOPS} symbolic links`, { path });
|
|
202
|
+
}
|
|
203
|
+
pending = parsePath(joinPath(route.fs.readlink(route.path), pending.slice(index + 1)));
|
|
204
|
+
index = 0;
|
|
205
|
+
resolved.length = 0;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
resolved.push(pending[index]!);
|
|
209
|
+
index += 1;
|
|
210
|
+
}
|
|
211
|
+
return joinPath(`/${resolved.join("/")}`, pending.slice(index));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Resolve, then route. Every path operation below starts here. */
|
|
215
|
+
#at(path: string, follow: boolean): Route {
|
|
216
|
+
return this.#route(this.resolvePath(path, follow));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
#descriptor(fd: number): MountedDescriptor {
|
|
220
|
+
const entry = this.#descriptors.get(fd);
|
|
221
|
+
if (entry === undefined) throw new FsError("EBADF", "descriptor is invalid");
|
|
222
|
+
return entry;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ---- lifecycle -----------------------------------------------------------
|
|
226
|
+
|
|
227
|
+
/** Flushes the root and every mount the owner declared durable; a volatile mount is skipped. */
|
|
228
|
+
strictSync(): void {
|
|
229
|
+
this.#root.strictSync();
|
|
230
|
+
for (const mount of this.#mounts) {
|
|
231
|
+
if (mount.durable) mount.vfs.strictSync();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
assertHealthy(): void {
|
|
236
|
+
this.#root.assertHealthy();
|
|
237
|
+
for (const mount of this.#mounts) mount.vfs.assertHealthy();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
fail(cause: unknown): never {
|
|
241
|
+
for (const mount of this.#mounts) {
|
|
242
|
+
try {
|
|
243
|
+
mount.vfs.fail(cause);
|
|
244
|
+
} catch {
|
|
245
|
+
// Every store is poisoned with the same cause; the root's throw is the one that escapes.
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return this.#root.fail(cause);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The ROOT store's metrics. A mount's own are read through {@link MountedRepackedVfs.mounts}. */
|
|
252
|
+
metrics(): RepackedVfsMetrics {
|
|
253
|
+
return this.#root.metrics();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
repack(reason?: RepackReason): void {
|
|
257
|
+
this.#root.repack(reason);
|
|
258
|
+
for (const mount of this.#mounts) mount.vfs.repack(reason);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
runScheduledRepack(nowMs?: number): boolean {
|
|
262
|
+
let repacked = this.#root.runScheduledRepack(nowMs);
|
|
263
|
+
for (const mount of this.#mounts) {
|
|
264
|
+
if (mount.vfs.runScheduledRepack(nowMs)) repacked = true;
|
|
265
|
+
}
|
|
266
|
+
return repacked;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
close(): void;
|
|
270
|
+
close(fd: number): void;
|
|
271
|
+
close(fd?: number): void {
|
|
272
|
+
if (fd !== undefined) {
|
|
273
|
+
const entry = this.#descriptor(fd);
|
|
274
|
+
this.#descriptors.delete(fd);
|
|
275
|
+
entry.fs.close(entry.fd);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
this.#descriptors.clear();
|
|
279
|
+
let firstError: unknown;
|
|
280
|
+
for (const mount of this.#mounts) {
|
|
281
|
+
try {
|
|
282
|
+
mount.vfs.close();
|
|
283
|
+
} catch (cause) {
|
|
284
|
+
firstError ??= cause;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
this.#root.close();
|
|
289
|
+
} catch (cause) {
|
|
290
|
+
firstError ??= cause;
|
|
291
|
+
}
|
|
292
|
+
if (firstError !== undefined) throw firstError;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ---- path operations -----------------------------------------------------
|
|
296
|
+
|
|
297
|
+
stat(path: string): RepackedStat {
|
|
298
|
+
const route = this.#at(path, true);
|
|
299
|
+
return route.fs.stat(route.path);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
lstat(path: string): RepackedStat {
|
|
303
|
+
const route = this.#at(path, false);
|
|
304
|
+
return route.fs.lstat(route.path);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
symlink(target: string, path: string, nowMs: bigint): void {
|
|
308
|
+
const route = this.#at(path, false);
|
|
309
|
+
route.fs.symlink(target, route.path, nowMs);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
readlink(path: string): string {
|
|
313
|
+
const route = this.#at(path, false);
|
|
314
|
+
return route.fs.readlink(route.path);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
readdir(path: string): string[] {
|
|
318
|
+
const route = this.#at(path, true);
|
|
319
|
+
return route.fs.readdir(route.path);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
mkdir(path: string, options: { recursive?: boolean; mode?: number; nowMs: bigint }): void {
|
|
323
|
+
const route = this.#at(path, false);
|
|
324
|
+
route.fs.mkdir(route.path, options);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
writeFile(
|
|
328
|
+
path: string,
|
|
329
|
+
data: string | Uint8Array,
|
|
330
|
+
options: { encoding?: string; mode?: number; flag?: string; nowMs: bigint },
|
|
331
|
+
): void {
|
|
332
|
+
const route = this.#at(path, true);
|
|
333
|
+
route.fs.writeFile(route.path, data, options);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
readFile(path: string): Uint8Array {
|
|
337
|
+
const route = this.#at(path, true);
|
|
338
|
+
return route.fs.readFile(route.path);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
truncate(path: string, size: bigint, nowMs: bigint): void {
|
|
342
|
+
const route = this.#at(path, true);
|
|
343
|
+
route.fs.truncate(route.path, size, nowMs);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
chmod(path: string, mode: number, nowMs: bigint): void {
|
|
347
|
+
const route = this.#at(path, true);
|
|
348
|
+
route.fs.chmod(route.path, mode, nowMs);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
utimes(path: string, atimeMs: bigint, mtimeMs: bigint, ctimeMs: bigint): void {
|
|
352
|
+
const route = this.#at(path, true);
|
|
353
|
+
route.fs.utimes(route.path, atimeMs, mtimeMs, ctimeMs);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
unlink(path: string, nowMs: bigint): void {
|
|
357
|
+
const route = this.#at(path, false);
|
|
358
|
+
route.fs.unlink(route.path, nowMs);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
rmdir(path: string, nowMs: bigint): void {
|
|
362
|
+
const route = this.#at(path, false);
|
|
363
|
+
route.fs.rmdir(route.path, nowMs);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** A rename is one store's metadata move; across a boundary it is `EXDEV`, as on any host. */
|
|
367
|
+
rename(oldPath: string, newPath: string, nowMs: bigint): void {
|
|
368
|
+
const source = this.#at(oldPath, false);
|
|
369
|
+
const destination = this.#at(newPath, false);
|
|
370
|
+
if (source.mount !== destination.mount) {
|
|
371
|
+
throw new FsError("EXDEV", "rename crosses a mount boundary", { operation: "rename", path: newPath });
|
|
372
|
+
}
|
|
373
|
+
source.fs.rename(source.path, destination.path, nowMs);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ---- descriptor operations -----------------------------------------------
|
|
377
|
+
|
|
378
|
+
open(path: string, flags = "r", mode = 0o100666, nowMs = 0n): number {
|
|
379
|
+
const route = this.#at(path, true);
|
|
380
|
+
const inner = route.fs.open(route.path, flags, mode, nowMs);
|
|
381
|
+
const fd = this.#nextDescriptor++;
|
|
382
|
+
this.#descriptors.set(fd, { fs: route.fs, fd: inner });
|
|
383
|
+
return fd;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
fstat(fd: number): RepackedStat {
|
|
387
|
+
const entry = this.#descriptor(fd);
|
|
388
|
+
return entry.fs.fstat(entry.fd);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
read(fd: number, buffer: Uint8Array, offset: number, length: number, position?: bigint): number {
|
|
392
|
+
const entry = this.#descriptor(fd);
|
|
393
|
+
return entry.fs.read(entry.fd, buffer, offset, length, position);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
write(
|
|
397
|
+
fd: number,
|
|
398
|
+
buffer: Uint8Array,
|
|
399
|
+
offset: number,
|
|
400
|
+
length: number,
|
|
401
|
+
position: bigint | undefined,
|
|
402
|
+
nowMs: bigint,
|
|
403
|
+
): number {
|
|
404
|
+
const entry = this.#descriptor(fd);
|
|
405
|
+
return entry.fs.write(entry.fd, buffer, offset, length, position, nowMs);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { FsError } from "./errors";
|
|
2
|
+
import { MAX_COMPONENT_BYTES, MAX_PATH_BYTES, MAX_PATH_DEPTH } from "./limits";
|
|
3
|
+
|
|
4
|
+
function utf8Length(value: string, label: string): number {
|
|
5
|
+
let bytes = 0;
|
|
6
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
7
|
+
const codeUnit = value.charCodeAt(index);
|
|
8
|
+
if (codeUnit <= 0x7f) {
|
|
9
|
+
bytes += 1;
|
|
10
|
+
} else if (codeUnit <= 0x7ff) {
|
|
11
|
+
bytes += 2;
|
|
12
|
+
} else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
|
|
13
|
+
const next = value.charCodeAt(index + 1);
|
|
14
|
+
if (index + 1 >= value.length || next < 0xdc00 || next > 0xdfff) {
|
|
15
|
+
throw new FsError("EINVAL", `${label} is not canonically representable as UTF-8`);
|
|
16
|
+
}
|
|
17
|
+
bytes += 4;
|
|
18
|
+
index += 1;
|
|
19
|
+
} else if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
|
|
20
|
+
throw new FsError("EINVAL", `${label} is not canonically representable as UTF-8`);
|
|
21
|
+
} else {
|
|
22
|
+
bytes += 3;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return bytes;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function encodedUtf8Length(value: string): number {
|
|
29
|
+
return utf8Length(value, "string");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function validatePathComponent(component: string): number {
|
|
33
|
+
if (component.length === 0 || component === "." || component === "..") {
|
|
34
|
+
throw new FsError("EINVAL", "path contains an empty or reserved component");
|
|
35
|
+
}
|
|
36
|
+
if (component.includes("/") || component.includes("\0")) {
|
|
37
|
+
throw new FsError("EINVAL", "path component contains a forbidden character");
|
|
38
|
+
}
|
|
39
|
+
const bytes = utf8Length(component, "path component");
|
|
40
|
+
if (bytes > MAX_COMPONENT_BYTES) {
|
|
41
|
+
throw new FsError("EINVAL", `path component exceeds ${MAX_COMPONENT_BYTES} UTF-8 bytes`);
|
|
42
|
+
}
|
|
43
|
+
return bytes;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Validate a symbolic link's target and report its UTF-8 byte length.
|
|
48
|
+
*
|
|
49
|
+
* Targets are ABSOLUTE ONLY, and deliberately so: an absolute target resolves by restarting the
|
|
50
|
+
* walk at the store root, which needs no notion of "the directory the link lives in" and therefore
|
|
51
|
+
* survives a rename of the link. It is also all a Postgres datadir ever writes — `CREATE TABLESPACE`
|
|
52
|
+
* links `pg_tblspc/<oid>` at the absolute `LOCATION`. A relative target is `EINVAL`, not silently
|
|
53
|
+
* reinterpreted.
|
|
54
|
+
*/
|
|
55
|
+
export function validateSymlinkTarget(target: string): number {
|
|
56
|
+
const bytes = utf8Length(target, "symlink target");
|
|
57
|
+
if (bytes === 0 || bytes > MAX_PATH_BYTES) {
|
|
58
|
+
throw new FsError("EINVAL", `symlink target must be 1 to ${MAX_PATH_BYTES} UTF-8 bytes`);
|
|
59
|
+
}
|
|
60
|
+
parsePath(target);
|
|
61
|
+
return bytes;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function parsePath(path: string): string[] {
|
|
65
|
+
if (path === "/") {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
if (!path.startsWith("/") || path.endsWith("/") || path.includes("//")) {
|
|
69
|
+
throw new FsError("EINVAL", "path must be absolute and canonical", { path });
|
|
70
|
+
}
|
|
71
|
+
if (utf8Length(path, "path") > MAX_PATH_BYTES) {
|
|
72
|
+
throw new FsError("EINVAL", `path exceeds ${MAX_PATH_BYTES} UTF-8 bytes`, { path });
|
|
73
|
+
}
|
|
74
|
+
const components = path.slice(1).split("/");
|
|
75
|
+
if (components.length > MAX_PATH_DEPTH) {
|
|
76
|
+
throw new FsError("EINVAL", `path exceeds ${MAX_PATH_DEPTH} components`, { path });
|
|
77
|
+
}
|
|
78
|
+
for (const component of components) {
|
|
79
|
+
validatePathComponent(component);
|
|
80
|
+
}
|
|
81
|
+
return components;
|
|
82
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { CorruptStoreError, StoreLimitError } from "./errors";
|
|
2
|
+
import {
|
|
3
|
+
ARENA_HEADER_BYTES,
|
|
4
|
+
MAX_TOTAL_EXTENTS,
|
|
5
|
+
checkedAdd,
|
|
6
|
+
checkedMultiply,
|
|
7
|
+
checkedSafeNumber,
|
|
8
|
+
checkedU64,
|
|
9
|
+
validateExtentSize,
|
|
10
|
+
} from "./limits";
|
|
11
|
+
import type { MetadataFile } from "./state-machine";
|
|
12
|
+
|
|
13
|
+
export const OWNED_FILE_NAMES = ["arena.bin", "metadata-a.bin", "metadata-b.bin", "activation.bin"] as const;
|
|
14
|
+
export type OwnedFileName = (typeof OWNED_FILE_NAMES)[number];
|
|
15
|
+
export type MetadataFileName = Extract<OwnedFileName, "metadata-a.bin" | "metadata-b.bin">;
|
|
16
|
+
|
|
17
|
+
export function metadataFileName(file: MetadataFile): MetadataFileName {
|
|
18
|
+
return file === "a" ? "metadata-a.bin" : "metadata-b.bin";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface RepackedPortEntry {
|
|
22
|
+
name: string;
|
|
23
|
+
kind: "file" | "directory";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RepackedFileHandle {
|
|
27
|
+
readonly name: OwnedFileName;
|
|
28
|
+
getSize(label: string): number;
|
|
29
|
+
read(target: Uint8Array, at: number, label: string): number;
|
|
30
|
+
write(source: Uint8Array, at: number, label: string): number;
|
|
31
|
+
truncate(size: number, label: string): void;
|
|
32
|
+
flush(label: string): void;
|
|
33
|
+
close(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RepackedPort {
|
|
37
|
+
enumerate(label: string): Promise<readonly RepackedPortEntry[]>;
|
|
38
|
+
acquire(name: OwnedFileName, label: string): Promise<RepackedFileHandle>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class PortWriteError extends Error {
|
|
42
|
+
constructor(label: string) {
|
|
43
|
+
super(`${label} made no valid write progress`);
|
|
44
|
+
this.name = "PortWriteError";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function checkedRange(offset: bigint, length: number, label: string): number {
|
|
49
|
+
checkedU64(offset, `${label} offset`);
|
|
50
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
51
|
+
throw new StoreLimitError(`${label} length is invalid`);
|
|
52
|
+
}
|
|
53
|
+
checkedSafeNumber(checkedAdd(offset, BigInt(length), `${label} end`), `${label} end`);
|
|
54
|
+
return checkedSafeNumber(offset, `${label} offset`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function readExact(
|
|
58
|
+
handle: RepackedFileHandle,
|
|
59
|
+
offset: bigint,
|
|
60
|
+
length: number,
|
|
61
|
+
maximumLength: number,
|
|
62
|
+
label: string,
|
|
63
|
+
): Uint8Array {
|
|
64
|
+
if (!Number.isSafeInteger(maximumLength) || maximumLength < 0 || length > maximumLength) {
|
|
65
|
+
throw new StoreLimitError(`${label} length exceeds the bounded read limit`);
|
|
66
|
+
}
|
|
67
|
+
const start = checkedRange(offset, length, label);
|
|
68
|
+
const output = new Uint8Array(length);
|
|
69
|
+
let completed = 0;
|
|
70
|
+
while (completed < output.byteLength) {
|
|
71
|
+
const count = handle.read(output.subarray(completed), start + completed, label);
|
|
72
|
+
if (!Number.isSafeInteger(count) || count <= 0 || count > output.byteLength - completed) {
|
|
73
|
+
throw new CorruptStoreError(`${label} ended before the declared byte length`);
|
|
74
|
+
}
|
|
75
|
+
completed += count;
|
|
76
|
+
}
|
|
77
|
+
return output;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function writeExact(handle: RepackedFileHandle, offset: bigint, bytes: Uint8Array, label: string): void {
|
|
81
|
+
const start = checkedRange(offset, bytes.byteLength, label);
|
|
82
|
+
let completed = 0;
|
|
83
|
+
while (completed < bytes.byteLength) {
|
|
84
|
+
const count = handle.write(bytes.subarray(completed), start + completed, label);
|
|
85
|
+
if (!Number.isSafeInteger(count) || count <= 0 || count > bytes.byteLength - completed) {
|
|
86
|
+
throw new PortWriteError(label);
|
|
87
|
+
}
|
|
88
|
+
completed += count;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function truncateChecked(handle: RepackedFileHandle, size: bigint, label: string): void {
|
|
93
|
+
handle.truncate(checkedSafeNumber(size, `${label} size`), label);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function arenaByteOffset(extentId: bigint, extentSize: number, withinExtent = 0): bigint {
|
|
97
|
+
validateExtentSize(extentSize);
|
|
98
|
+
checkedU64(extentId, "extent ID");
|
|
99
|
+
if (extentId >= BigInt(MAX_TOTAL_EXTENTS)) {
|
|
100
|
+
throw new StoreLimitError(`extent ID exceeds ${MAX_TOTAL_EXTENTS - 1}`);
|
|
101
|
+
}
|
|
102
|
+
if (!Number.isSafeInteger(withinExtent) || withinExtent < 0 || withinExtent >= extentSize) {
|
|
103
|
+
throw new StoreLimitError("within-extent offset is outside the extent");
|
|
104
|
+
}
|
|
105
|
+
return checkedAdd(
|
|
106
|
+
checkedAdd(
|
|
107
|
+
BigInt(ARENA_HEADER_BYTES),
|
|
108
|
+
checkedMultiply(extentId, BigInt(extentSize), "arena offset"),
|
|
109
|
+
"arena offset",
|
|
110
|
+
),
|
|
111
|
+
BigInt(withinExtent),
|
|
112
|
+
"arena offset",
|
|
113
|
+
);
|
|
114
|
+
}
|