@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,125 @@
|
|
|
1
|
+
import type { PostgresBuild } from "../build/seam";
|
|
2
|
+
import { registerStrictSync, strictSyncOf } from "../core/internals";
|
|
3
|
+
import { createPgwasm } from "../create";
|
|
4
|
+
import { UnsupportedFeatureError } from "../errors";
|
|
5
|
+
import type { Extensions, Pgwasm, PgwasmOptions, PgwasmWithExtensions } from "../interface";
|
|
6
|
+
import { OpfsRepackedPort } from "./opfs-port";
|
|
7
|
+
import type { OpfsDirectoryHandle } from "./opfs-port";
|
|
8
|
+
import { openOpfsRepackedFsForPort } from "./opfs-repacked-fs";
|
|
9
|
+
import type { RepackedFilesystemOptions } from "./opfs-repacked-fs";
|
|
10
|
+
|
|
11
|
+
/** The database options `createOpfsPgwasm` passes through: all but the ones the store owns. */
|
|
12
|
+
export type OpfsPgwasmHostOptions<E extends Extensions = Record<never, never>> = Omit<
|
|
13
|
+
PgwasmOptions<E>,
|
|
14
|
+
"build" | "dataDir" | "fs" | "relaxedDurability"
|
|
15
|
+
>;
|
|
16
|
+
|
|
17
|
+
export interface CreateOpfsPgwasmOptions<
|
|
18
|
+
E extends Extensions = Record<never, never>,
|
|
19
|
+
> extends RepackedFilesystemOptions {
|
|
20
|
+
/** The Postgres build to run, e.g. `cBuild` from `@pgxsinkit/pgwasm-c`. */
|
|
21
|
+
readonly build: PostgresBuild;
|
|
22
|
+
/** Dedicated directory owned in full by this store. */
|
|
23
|
+
readonly directory: OpfsDirectoryHandle;
|
|
24
|
+
/** Database options other than the store-owned `build`, `dataDir`, `fs` and `relaxedDurability`. */
|
|
25
|
+
readonly pgwasm?: OpfsPgwasmHostOptions<E>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional progress callback for the two long, otherwise-invisible steps of an OPFS-repacked create.
|
|
28
|
+
* Called once per phase, in order, and only on the success path:
|
|
29
|
+
*
|
|
30
|
+
* - `"store-opened"` — the store's sync access handles are acquired and the repacked filesystem is open.
|
|
31
|
+
* Everything before this is handle acquisition, which is where a create that never returns stalls.
|
|
32
|
+
* - `"pgwasm-ready"` — the database's own boot (WASM instantiation, initdb or catalog open) has completed.
|
|
33
|
+
*
|
|
34
|
+
* Diagnosability only: it carries no policy, must not throw, and a create's outcome never depends on it.
|
|
35
|
+
* The host uses it to stamp a boot rail, so a stalled create is attributable to a phase rather than
|
|
36
|
+
* silent.
|
|
37
|
+
*/
|
|
38
|
+
readonly onPhase?: (phase: OpfsCreatePhase) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The create phases {@link CreateOpfsPgwasmOptions.onPhase} reports, in the order they occur. */
|
|
42
|
+
export type OpfsCreatePhase = "store-opened" | "pgwasm-ready";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A database on an OPFS-repacked store: a plain pgwasm database. Its one strict operation, reserved for
|
|
46
|
+
* the sync layer above the store, is {@link strictSync}.
|
|
47
|
+
*/
|
|
48
|
+
export type OpfsPgwasm<E extends Extensions = Record<never, never>> = PgwasmWithExtensions<E>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Construct the only supported OPFS-repacked/pgwasm pairing.
|
|
52
|
+
*
|
|
53
|
+
* pgwasm always awaits the filesystem sync (`relaxedDurability: false`). Physical durability is selected
|
|
54
|
+
* once, here, by the store's `durability` option and is never delegated to the database option. A
|
|
55
|
+
* strict sync completes successful database initialization before return. Closing the database closes
|
|
56
|
+
* the store: its build releases the filesystem (`closeFs`) on every close, a failed one included.
|
|
57
|
+
*/
|
|
58
|
+
export async function createOpfsPgwasm<E extends Extensions = Record<never, never>>(
|
|
59
|
+
options: CreateOpfsPgwasmOptions<E>,
|
|
60
|
+
): Promise<OpfsPgwasm<E>> {
|
|
61
|
+
assertHostOptions(options.pgwasm);
|
|
62
|
+
const store = await openOpfsRepackedFsForPort(new OpfsRepackedPort(options.directory), filesystemOptions(options));
|
|
63
|
+
options.onPhase?.("store-opened");
|
|
64
|
+
|
|
65
|
+
let pg: OpfsPgwasm<E> | undefined;
|
|
66
|
+
try {
|
|
67
|
+
pg = await createPgwasm<E>({
|
|
68
|
+
...options.pgwasm,
|
|
69
|
+
build: options.build,
|
|
70
|
+
fs: store,
|
|
71
|
+
relaxedDurability: false,
|
|
72
|
+
});
|
|
73
|
+
registerStrictSync(pg, () => store.strictSync());
|
|
74
|
+
options.onPhase?.("pgwasm-ready");
|
|
75
|
+
store.strictSync();
|
|
76
|
+
return pg;
|
|
77
|
+
} catch (cause) {
|
|
78
|
+
try {
|
|
79
|
+
// The database is unusable once its first strict sync failed: close it, which releases the store.
|
|
80
|
+
await pg?.close();
|
|
81
|
+
} catch {
|
|
82
|
+
// Preserve the first cause.
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
await store.cleanupFailedInit();
|
|
86
|
+
} catch {
|
|
87
|
+
// Initialization is already unusable. Preserve its first cause after
|
|
88
|
+
// cleanup has attempted every owned handle.
|
|
89
|
+
}
|
|
90
|
+
throw cause;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Stabilize every preceding data and metadata operation of a database on an OPFS-repacked store, in
|
|
96
|
+
* strict order, serialized against its queries. Throws {@link UnsupportedFeatureError} for a database
|
|
97
|
+
* that `createOpfsPgwasm` did not create.
|
|
98
|
+
*/
|
|
99
|
+
export async function strictSync(pg: Pgwasm): Promise<void> {
|
|
100
|
+
const sync = strictSyncOf(pg);
|
|
101
|
+
if (sync === undefined) {
|
|
102
|
+
throw new UnsupportedFeatureError(
|
|
103
|
+
"strictSync needs a database on an OPFS-repacked store (created by createOpfsPgwasm).",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
await pg.runExclusive(async () => {
|
|
107
|
+
sync();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function filesystemOptions(options: CreateOpfsPgwasmOptions<Extensions>): RepackedFilesystemOptions {
|
|
112
|
+
return {
|
|
113
|
+
...(options.extentSize === undefined ? {} : { extentSize: options.extentSize }),
|
|
114
|
+
...(options.durability === undefined ? {} : { durability: options.durability }),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function assertHostOptions(options: object | undefined): void {
|
|
119
|
+
if (options === undefined) return;
|
|
120
|
+
for (const reserved of ["build", "dataDir", "fs", "relaxedDurability"] as const) {
|
|
121
|
+
if (reserved in options) {
|
|
122
|
+
throw new TypeError(`pgwasm.${reserved} is owned by createOpfsPgwasm`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The FILE-backed `RepackedPort`: the same four owned files, in an ordinary directory on an ordinary
|
|
3
|
+
* filesystem, opened with `node:fs`'s synchronous calls.
|
|
4
|
+
*
|
|
5
|
+
* It exists because the store's four files are the store — nothing about the format is OPFS's, and
|
|
6
|
+
* nothing about it is a browser's. A host running under Node or Bun can therefore BUILD a store
|
|
7
|
+
* (seed it, fill it, repack it) where a build step can see the bytes, hand those four files to a
|
|
8
|
+
* browser, and have the browser's `OpfsRepackedPort` open them as the same store. Format identity
|
|
9
|
+
* is by construction: the port answers `getSize`/`read`/`write`/`truncate`/`flush` and the layout
|
|
10
|
+
* above it never learns which port it is on.
|
|
11
|
+
*
|
|
12
|
+
* **`node:fs` is imported LAZILY, and that is load-bearing.** This module is exported from the
|
|
13
|
+
* package entry, and the package's redistribution artifact (`dist/browser-bundle.js`) is built with
|
|
14
|
+
* `--target browser`, where a STATIC `import { openSync } from "node:fs"` is compiled to an empty
|
|
15
|
+
* object — every call would then be `undefined is not a function`, in Node as well as in a browser,
|
|
16
|
+
* because that one bundle is what the wasm hosts load in BOTH. A dynamic `import("node:fs")` is left
|
|
17
|
+
* verbatim by every target, so the same file resolves the real module under Node/Bun and rejects
|
|
18
|
+
* where there is no such module. `enumerate` and `acquire` are already async, which is the whole
|
|
19
|
+
* reason the seam fits: every method on the HANDLE stays synchronous, as the store requires.
|
|
20
|
+
*
|
|
21
|
+
* **Ownership is not exclusion.** OPFS's `createSyncAccessHandle()` is exclusive per origin, and
|
|
22
|
+
* `StoreOwnedError` there means another live owner. POSIX `open` grants nothing of the kind: two
|
|
23
|
+
* processes may hold the same file open and interleave writes, and no advisory lock this port could
|
|
24
|
+
* take would be honoured by a process that did not take one. So the guarantee here is bounded and
|
|
25
|
+
* stated — a second `acquire` of a name this port already has open raises `StoreOwnedError`, and a
|
|
26
|
+
* SECOND PROCESS is the host's problem to arrange. The intended use is a single build-time owner.
|
|
27
|
+
*/
|
|
28
|
+
// Type-only, and therefore erased before any bundler sees it — the RUNTIME import is the dynamic one
|
|
29
|
+
// in `loadNodeFileSystem` below, which is the whole point of this module's shape.
|
|
30
|
+
import type * as NodeFsModule from "node:fs";
|
|
31
|
+
|
|
32
|
+
import { StoreOwnedError } from "./core/errors";
|
|
33
|
+
import type { OwnedFileName, RepackedFileHandle, RepackedPort, RepackedPortEntry } from "./core/port";
|
|
34
|
+
|
|
35
|
+
/** The `node:fs` surface this port uses, and nothing else. Never part of an exported signature. */
|
|
36
|
+
type NodeFileSystem = Pick<
|
|
37
|
+
typeof NodeFsModule,
|
|
38
|
+
| "closeSync"
|
|
39
|
+
| "constants"
|
|
40
|
+
| "fstatSync"
|
|
41
|
+
| "ftruncateSync"
|
|
42
|
+
| "fsyncSync"
|
|
43
|
+
| "mkdirSync"
|
|
44
|
+
| "openSync"
|
|
45
|
+
| "readSync"
|
|
46
|
+
| "readdirSync"
|
|
47
|
+
| "writeSync"
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
/** The mode a newly created owned file takes; the process umask applies to it as it does anywhere. */
|
|
51
|
+
const CREATED_FILE_MODE = 0o666;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Resolve `node:fs`, or say why there is none.
|
|
55
|
+
*
|
|
56
|
+
* A bare rejected `import()` in a browser reads as "Failed to resolve module specifier", which names
|
|
57
|
+
* neither this port nor the reason; the check in front of it is what turns that into an answer.
|
|
58
|
+
*/
|
|
59
|
+
async function loadNodeFileSystem(): Promise<NodeFileSystem> {
|
|
60
|
+
const runtime = globalThis as { process?: { versions?: Record<string, string | undefined> } };
|
|
61
|
+
if (runtime.process?.versions?.["node"] === undefined) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
"FileRepackedPort needs node:fs and this scope has none (no process.versions.node); " +
|
|
64
|
+
"a browser store belongs on OpfsRepackedPort",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return (await import("node:fs")) as NodeFileSystem;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function joinPath(directory: string, name: string): string {
|
|
71
|
+
return `${directory.replace(/\/+$/, "")}/${name}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class FileRepackedPort implements RepackedPort {
|
|
75
|
+
readonly #directory: string;
|
|
76
|
+
readonly #open = new Set<OwnedFileName>();
|
|
77
|
+
#fs: NodeFileSystem | undefined;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param directory The directory this store owns in full. It is created on first use, with every
|
|
81
|
+
* parent, exactly as the OPFS port's caller creates its directory handle with `create: true`.
|
|
82
|
+
*/
|
|
83
|
+
constructor(directory: string) {
|
|
84
|
+
if (directory.trim() === "") throw new TypeError("FileRepackedPort needs a directory path");
|
|
85
|
+
this.#directory = directory;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The directory this port reads and writes. Absolute or relative, exactly as it was given. */
|
|
89
|
+
get directory(): string {
|
|
90
|
+
return this.#directory;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async enumerate(_label: string): Promise<readonly RepackedPortEntry[]> {
|
|
94
|
+
const fs = await this.#ready();
|
|
95
|
+
// Everything that is there, not just the four names: an entry the store did not write is what
|
|
96
|
+
// `UnexpectedStoreEntryError` is for, and hiding it here would open a store over a stranger's
|
|
97
|
+
// directory.
|
|
98
|
+
return fs.readdirSync(this.#directory, { withFileTypes: true }).map((entry) => ({
|
|
99
|
+
name: entry.name,
|
|
100
|
+
kind: entry.isDirectory() ? ("directory" as const) : ("file" as const),
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async acquire(name: OwnedFileName, _label: string): Promise<RepackedFileHandle> {
|
|
105
|
+
const fs = await this.#ready();
|
|
106
|
+
// The bounded half of the ownership guarantee; see the file header for the half POSIX cannot give.
|
|
107
|
+
if (this.#open.has(name)) throw new StoreOwnedError();
|
|
108
|
+
// `r+` that creates: O_RDWR|O_CREAT, which is `openSync(path, "r+")`'s flags plus the create bit
|
|
109
|
+
// and NOT `a+` — an append-mode fd ignores the position argument on every write, which would
|
|
110
|
+
// turn the arena's random writes into a log.
|
|
111
|
+
const fd = fs.openSync(
|
|
112
|
+
joinPath(this.#directory, name),
|
|
113
|
+
fs.constants.O_RDWR | fs.constants.O_CREAT,
|
|
114
|
+
CREATED_FILE_MODE,
|
|
115
|
+
);
|
|
116
|
+
this.#open.add(name);
|
|
117
|
+
return new FileRepackedFileHandle(fs, name, fd, () => this.#open.delete(name));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async #ready(): Promise<NodeFileSystem> {
|
|
121
|
+
if (this.#fs === undefined) {
|
|
122
|
+
const fs = await loadNodeFileSystem();
|
|
123
|
+
fs.mkdirSync(this.#directory, { recursive: true });
|
|
124
|
+
this.#fs = fs;
|
|
125
|
+
}
|
|
126
|
+
return this.#fs;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class FileRepackedFileHandle implements RepackedFileHandle {
|
|
131
|
+
readonly name: OwnedFileName;
|
|
132
|
+
readonly #fs: NodeFileSystem;
|
|
133
|
+
readonly #fd: number;
|
|
134
|
+
readonly #released: () => void;
|
|
135
|
+
#closed = false;
|
|
136
|
+
|
|
137
|
+
constructor(fs: NodeFileSystem, name: OwnedFileName, fd: number, released: () => void) {
|
|
138
|
+
this.#fs = fs;
|
|
139
|
+
this.name = name;
|
|
140
|
+
this.#fd = fd;
|
|
141
|
+
this.#released = released;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
getSize(_label: string): number {
|
|
145
|
+
this.#assertOpen();
|
|
146
|
+
return this.#fs.fstatSync(this.#fd).size;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
read(target: Uint8Array, at: number, _label: string): number {
|
|
150
|
+
this.#assertOpen();
|
|
151
|
+
// A read wholly past the end answers 0, which `readExact` reports as a store that ended early —
|
|
152
|
+
// the same thing an OPFS access handle answers there.
|
|
153
|
+
return this.#fs.readSync(this.#fd, target, 0, target.byteLength, at);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
write(source: Uint8Array, at: number, _label: string): number {
|
|
157
|
+
this.#assertOpen();
|
|
158
|
+
return this.#fs.writeSync(this.#fd, source, 0, source.byteLength, at);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
truncate(size: number, _label: string): void {
|
|
162
|
+
this.#assertOpen();
|
|
163
|
+
this.#fs.ftruncateSync(this.#fd, size);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
flush(_label: string): void {
|
|
167
|
+
this.#assertOpen();
|
|
168
|
+
// The platform's durability boundary, not a userspace buffer drain: nothing here buffers, so
|
|
169
|
+
// `fsync` is the only thing `flush` can honestly mean.
|
|
170
|
+
this.#fs.fsyncSync(this.#fd);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
close(): void {
|
|
174
|
+
if (this.#closed) return;
|
|
175
|
+
this.#closed = true;
|
|
176
|
+
this.#released();
|
|
177
|
+
this.#fs.closeSync(this.#fd);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#assertOpen(): void {
|
|
181
|
+
if (this.#closed) throw new Error(`file handle ${this.name} is closed`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
export {
|
|
2
|
+
CorruptStoreError,
|
|
3
|
+
DurabilityModeMismatchError,
|
|
4
|
+
ExtentSizeMismatchError,
|
|
5
|
+
FsError,
|
|
6
|
+
StoreClosedError,
|
|
7
|
+
StoreFailedError,
|
|
8
|
+
StoreLimitError,
|
|
9
|
+
StoreOwnedError,
|
|
10
|
+
StoreRecreationRequiredError,
|
|
11
|
+
UnexpectedStoreEntryError,
|
|
12
|
+
} from "./core/errors";
|
|
13
|
+
export {
|
|
14
|
+
createOpfsPgwasm,
|
|
15
|
+
strictSync,
|
|
16
|
+
type CreateOpfsPgwasmOptions,
|
|
17
|
+
type OpfsCreatePhase,
|
|
18
|
+
type OpfsPgwasm,
|
|
19
|
+
type OpfsPgwasmHostOptions,
|
|
20
|
+
} from "./create";
|
|
21
|
+
export { OpfsRepackedFS, type RepackedDurability } from "./opfs-repacked-fs";
|
|
22
|
+
|
|
23
|
+
// The engine-agnostic store core: a `RepackedVfs` over any `RepackedPort`, with no Postgres, wasm, or
|
|
24
|
+
// OPFS anywhere in it. This is what a coordinator worker owns and what the sync broker serves.
|
|
25
|
+
export {
|
|
26
|
+
RepackedVfs,
|
|
27
|
+
type RepackedFileSystem,
|
|
28
|
+
type RepackedStat,
|
|
29
|
+
type RepackedVfsMetrics,
|
|
30
|
+
type RepackedVfsOpenOptions,
|
|
31
|
+
} from "./core/repacked-vfs";
|
|
32
|
+
export { MemoryRepackedPort, type MemoryFault, type MemoryOperation } from "./core/memory-port";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Several stores as ONE tree, joined at path prefixes: a durable OPFS root with a memory-backed
|
|
36
|
+
* mount at a chosen prefix, handed to the broker as a single store. Which prefix, which port, and
|
|
37
|
+
* whether a mount is durable are the STORAGE OWNER's declarations — nothing is inferred.
|
|
38
|
+
*/
|
|
39
|
+
export { MountedRepackedVfs, type MountedRepackedVfsOptions, type RepackedMount } from "./core/mounted-vfs";
|
|
40
|
+
export type { RepackedFileHandle, RepackedPort, RepackedPortEntry } from "./core/port";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The OPFS-backed port for that same store core: four `FileSystemSyncAccessHandle`s over one dedicated
|
|
44
|
+
* OPFS directory. `createOpfsPgwasm` builds this internally, so a pgwasm consumer never needs
|
|
45
|
+
* it; it is exported for a host that owns the store itself — a coordinator worker serving the sync
|
|
46
|
+
* broker to a wasm engine — and wants that store persisted rather than in memory. It must be
|
|
47
|
+
* constructed in a scope where `createSyncAccessHandle()` actually SUCCEEDS (a dedicated worker in
|
|
48
|
+
* every engine, plus a SharedWorker on real Safari); method presence is not proof, the window main
|
|
49
|
+
* thread is never such a scope, and a denial surfaces as an open failure rather than a fallback.
|
|
50
|
+
* `OpfsDirectoryHandle` is the structural slice of `FileSystemDirectoryHandle` the port actually uses,
|
|
51
|
+
* so a real handle satisfies it without a cast.
|
|
52
|
+
*/
|
|
53
|
+
export { OpfsRepackedPort, type OpfsDirectoryHandle } from "./opfs-port";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The same store's four files in an ordinary DIRECTORY, over `node:fs`. The port a build step uses:
|
|
57
|
+
* a host under Node or Bun fills a store where the bytes are ordinary files, ships those four files,
|
|
58
|
+
* and a browser's `OpfsRepackedPort` opens them as the same store — the format lives above the port,
|
|
59
|
+
* so neither side learns which one it is on. `node:fs` is imported lazily so the browser bundle can
|
|
60
|
+
* carry this module without a static `node:` import being compiled to nothing (see `./file-port`).
|
|
61
|
+
*/
|
|
62
|
+
export { FileRepackedPort } from "./file-port";
|
|
63
|
+
|
|
64
|
+
// The synchronous broker: one coordinator worker owns the store, every other thread reaches it over a
|
|
65
|
+
// SharedArrayBuffer channel and blocks in `Atomics.wait` for the answer.
|
|
66
|
+
export { RepackedSyncBroker, type RepackedSyncBrokerOptions } from "./broker/server";
|
|
67
|
+
export {
|
|
68
|
+
RepackedBrokerStoreError,
|
|
69
|
+
RepackedBrokerTransportError,
|
|
70
|
+
RepackedSyncClient,
|
|
71
|
+
throwOnErrno,
|
|
72
|
+
type BrokerCountResult,
|
|
73
|
+
type BrokerOpenResult,
|
|
74
|
+
type BrokerReadResult,
|
|
75
|
+
type BrokerReaddirPageResult,
|
|
76
|
+
type BrokerReaddirResult,
|
|
77
|
+
type BrokerReadlinkResult,
|
|
78
|
+
type BrokerResult,
|
|
79
|
+
type BrokerSizeResult,
|
|
80
|
+
type BrokerStatResult,
|
|
81
|
+
type RepackedSyncClientOptions,
|
|
82
|
+
} from "./broker/client";
|
|
83
|
+
export {
|
|
84
|
+
DEFAULT_PAYLOAD_BYTES,
|
|
85
|
+
O_APPEND,
|
|
86
|
+
O_CREAT,
|
|
87
|
+
O_EXCL,
|
|
88
|
+
O_NOFOLLOW,
|
|
89
|
+
O_RDONLY,
|
|
90
|
+
O_RDWR,
|
|
91
|
+
O_TRUNC,
|
|
92
|
+
O_WRONLY,
|
|
93
|
+
RepackedChannel,
|
|
94
|
+
RepackedDoorbell,
|
|
95
|
+
errnoName,
|
|
96
|
+
fsErrorNameOf,
|
|
97
|
+
planOpen,
|
|
98
|
+
type BrokerStat,
|
|
99
|
+
type OpenPlan,
|
|
100
|
+
type RepackedChannelTransfer,
|
|
101
|
+
} from "./broker/protocol";
|
|
102
|
+
|
|
103
|
+
// The WASI preview1 filesystem adapter: the seam where a wasm engine's file calls reach ONE store
|
|
104
|
+
// through the broker, with fds 0-2 and every non-filesystem import left to the host.
|
|
105
|
+
export {
|
|
106
|
+
WASI_ERRNO,
|
|
107
|
+
WASI_FILETYPE,
|
|
108
|
+
createWasiPreview1Fs,
|
|
109
|
+
normalizeWasiPath,
|
|
110
|
+
type WasiPreview1Fs,
|
|
111
|
+
type WasiPreview1FsFunctions,
|
|
112
|
+
type WasiPreview1FsOptions,
|
|
113
|
+
} from "./wasi/preview1";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { StoreOwnedError } from "./core/errors";
|
|
2
|
+
import type { OwnedFileName, RepackedFileHandle, RepackedPort, RepackedPortEntry } from "./core/port";
|
|
3
|
+
|
|
4
|
+
interface OpfsDirectoryEntry {
|
|
5
|
+
readonly kind: "file" | "directory";
|
|
6
|
+
readonly name: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface OpfsSyncAccessHandle {
|
|
10
|
+
getSize(): number;
|
|
11
|
+
read(target: Uint8Array, options: { at: number }): number;
|
|
12
|
+
write(source: Uint8Array, options: { at: number }): number;
|
|
13
|
+
truncate(size: number): void;
|
|
14
|
+
flush(): void;
|
|
15
|
+
close(): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface OpfsFileHandle {
|
|
19
|
+
createSyncAccessHandle(): Promise<OpfsSyncAccessHandle>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface OpfsDirectoryHandle {
|
|
23
|
+
values(): AsyncIterable<OpfsDirectoryEntry>;
|
|
24
|
+
getFileHandle(name: string, options?: { create?: boolean }): Promise<OpfsFileHandle>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class OpfsRepackedPort implements RepackedPort {
|
|
28
|
+
readonly #directory: OpfsDirectoryHandle;
|
|
29
|
+
|
|
30
|
+
constructor(directory: OpfsDirectoryHandle) {
|
|
31
|
+
this.#directory = directory;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async enumerate(_label: string): Promise<readonly RepackedPortEntry[]> {
|
|
35
|
+
const entries: RepackedPortEntry[] = [];
|
|
36
|
+
for await (const entry of this.#directory.values()) {
|
|
37
|
+
entries.push({ name: entry.name, kind: entry.kind });
|
|
38
|
+
}
|
|
39
|
+
return entries;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async acquire(name: OwnedFileName, _label: string): Promise<RepackedFileHandle> {
|
|
43
|
+
const file = await this.#directory.getFileHandle(name, { create: true });
|
|
44
|
+
try {
|
|
45
|
+
return new OpfsRepackedFileHandle(name, await file.createSyncAccessHandle());
|
|
46
|
+
} catch (cause) {
|
|
47
|
+
if (isOwnershipFailure(cause)) throw new StoreOwnedError({ cause });
|
|
48
|
+
throw cause;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class OpfsRepackedFileHandle implements RepackedFileHandle {
|
|
54
|
+
readonly name: OwnedFileName;
|
|
55
|
+
readonly #handle: OpfsSyncAccessHandle;
|
|
56
|
+
|
|
57
|
+
constructor(name: OwnedFileName, handle: OpfsSyncAccessHandle) {
|
|
58
|
+
this.name = name;
|
|
59
|
+
this.#handle = handle;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getSize(_label: string): number {
|
|
63
|
+
return this.#handle.getSize();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
read(target: Uint8Array, at: number, _label: string): number {
|
|
67
|
+
return this.#handle.read(target, { at });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
write(source: Uint8Array, at: number, _label: string): number {
|
|
71
|
+
return this.#handle.write(source, { at });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
truncate(size: number, _label: string): void {
|
|
75
|
+
this.#handle.truncate(size);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
flush(_label: string): void {
|
|
79
|
+
this.#handle.flush();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
close(): void {
|
|
83
|
+
this.#handle.close();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isOwnershipFailure(cause: unknown): boolean {
|
|
88
|
+
if (typeof cause !== "object" || cause === null || !("name" in cause)) return false;
|
|
89
|
+
return cause.name === "NoModificationAllowedError" || cause.name === "InvalidStateError";
|
|
90
|
+
}
|