@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,109 @@
|
|
|
1
|
+
// Began as a copy of `@electric-sql/pglite` (taken under its PostgreSQL License option, © ElectricSQL
|
|
2
|
+
// — see NOTICE). Owned outright (ADR-0062); compatibility with PGlite is an anti-goal — evolve freely.
|
|
3
|
+
|
|
4
|
+
import type { Pgwasm, Results, Row } from "../interface";
|
|
5
|
+
|
|
6
|
+
export interface LiveQueryOptions<T = Row> {
|
|
7
|
+
query: string;
|
|
8
|
+
params?: readonly unknown[] | null;
|
|
9
|
+
offset?: number;
|
|
10
|
+
limit?: number;
|
|
11
|
+
callback?: (results: Results<T>) => void;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface LiveChangesOptions<T = Row> {
|
|
16
|
+
query: string;
|
|
17
|
+
params?: readonly unknown[] | null;
|
|
18
|
+
key: string;
|
|
19
|
+
callback?: (changes: Change<T>[]) => void;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface LiveIncrementalQueryOptions<T = Row> {
|
|
24
|
+
query: string;
|
|
25
|
+
params?: readonly unknown[] | null;
|
|
26
|
+
key: string;
|
|
27
|
+
callback?: (results: Results<T>) => void;
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The `live` namespace the extension attaches. */
|
|
32
|
+
export interface LiveNamespace {
|
|
33
|
+
/**
|
|
34
|
+
* A query whose results are re-run and delivered whenever a table it reads changes. Resolves to the
|
|
35
|
+
* initial results plus `subscribe` / `unsubscribe` / `refresh`.
|
|
36
|
+
*/
|
|
37
|
+
query<T = Row>(
|
|
38
|
+
query: string,
|
|
39
|
+
params?: readonly unknown[] | null,
|
|
40
|
+
callback?: (results: Results<T>) => void,
|
|
41
|
+
): Promise<LiveQuery<T>>;
|
|
42
|
+
query<T = Row>(options: LiveQueryOptions<T>): Promise<LiveQuery<T>>;
|
|
43
|
+
|
|
44
|
+
/** The changes to a query's results, keyed by a column (`key`) that identifies a row. */
|
|
45
|
+
changes<T = Row>(
|
|
46
|
+
query: string,
|
|
47
|
+
params: readonly unknown[] | undefined | null,
|
|
48
|
+
key: string,
|
|
49
|
+
callback?: (changes: Change<T>[]) => void,
|
|
50
|
+
): Promise<LiveChanges<T>>;
|
|
51
|
+
changes<T = Row>(options: LiveChangesOptions<T>): Promise<LiveChanges<T>>;
|
|
52
|
+
|
|
53
|
+
/** Like {@link query}, but maintained from `changes`, keyed by `key`: cheaper for large results. */
|
|
54
|
+
incrementalQuery<T = Row>(
|
|
55
|
+
query: string,
|
|
56
|
+
params: readonly unknown[] | undefined | null,
|
|
57
|
+
key: string,
|
|
58
|
+
callback?: (results: Results<T>) => void,
|
|
59
|
+
): Promise<LiveQuery<T>>;
|
|
60
|
+
incrementalQuery<T = Row>(options: LiveIncrementalQueryOptions<T>): Promise<LiveQuery<T>>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface LiveQueryResults<T> extends Results<T> {
|
|
64
|
+
totalCount?: number;
|
|
65
|
+
offset?: number;
|
|
66
|
+
limit?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface LiveQuery<T> {
|
|
70
|
+
initialResults: LiveQueryResults<T>;
|
|
71
|
+
subscribe: (callback: (results: LiveQueryResults<T>) => void) => void;
|
|
72
|
+
unsubscribe: (callback?: (results: LiveQueryResults<T>) => void) => Promise<void>;
|
|
73
|
+
refresh: (options?: { offset?: number; limit?: number }) => Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface LiveChanges<T = Row> {
|
|
77
|
+
fields: { name: string; dataTypeID: number }[];
|
|
78
|
+
initialChanges: Change<T>[];
|
|
79
|
+
subscribe: (callback: (changes: Change<T>[]) => void) => void;
|
|
80
|
+
unsubscribe: (callback?: (changes: Change<T>[]) => void) => Promise<void>;
|
|
81
|
+
refresh: () => Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type ChangeInsert<T> = {
|
|
85
|
+
__changed_columns__: string[];
|
|
86
|
+
__op__: "INSERT";
|
|
87
|
+
__after__: number;
|
|
88
|
+
} & T;
|
|
89
|
+
|
|
90
|
+
export type ChangeDelete<T> = {
|
|
91
|
+
__changed_columns__: string[];
|
|
92
|
+
__op__: "DELETE";
|
|
93
|
+
__after__: undefined;
|
|
94
|
+
} & T;
|
|
95
|
+
|
|
96
|
+
export type ChangeUpdate<T> = {
|
|
97
|
+
__changed_columns__: string[];
|
|
98
|
+
__op__: "UPDATE";
|
|
99
|
+
__after__: number;
|
|
100
|
+
} & T;
|
|
101
|
+
|
|
102
|
+
export type ChangeReset<T> = {
|
|
103
|
+
__op__: "RESET";
|
|
104
|
+
} & T;
|
|
105
|
+
|
|
106
|
+
export type Change<T> = ChangeInsert<T> | ChangeDelete<T> | ChangeUpdate<T> | ChangeReset<T>;
|
|
107
|
+
|
|
108
|
+
/** A database with the `live` extension attached under the key `live`. */
|
|
109
|
+
export type PgwasmWithLive = Pgwasm & { readonly live: LiveNamespace };
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `RepackedSyncClient` — the backend side of the synchronous broker.
|
|
3
|
+
*
|
|
4
|
+
* Every method blocks the calling thread in `Atomics.wait` until the coordinator answers, which is the
|
|
5
|
+
* whole point: a thread running wasm parks in futexes and can never observe a promise, so the file
|
|
6
|
+
* layer under it has to be synchronous. Nothing here is async, and nothing here knows about wasm.
|
|
7
|
+
*
|
|
8
|
+
* ## What throws and what does not
|
|
9
|
+
*
|
|
10
|
+
* A FILE rejection is data: every method returns `{ errno }` (0 on success) with whatever else the
|
|
11
|
+
* operation produced. Callers map the errno straight into their own ABI — the values are WASI preview1
|
|
12
|
+
* errno numbers already.
|
|
13
|
+
*
|
|
14
|
+
* A TRANSPORT failure throws, because there is no answer to return: the server detached this client,
|
|
15
|
+
* the client sent something the server refused, the server never replied inside the timeout, or the
|
|
16
|
+
* store itself failed. Those are `RepackedBrokerTransportError` and `RepackedBrokerStoreError`.
|
|
17
|
+
*
|
|
18
|
+
* ## Mapping onto WASI preview1
|
|
19
|
+
*
|
|
20
|
+
* The method set is shaped so a preview1 adapter is a straight translation, with no state of its own
|
|
21
|
+
* beyond the preopen table:
|
|
22
|
+
*
|
|
23
|
+
* path_open → open(path, flags, mode) (flags are the POSIX bits, see protocol)
|
|
24
|
+
* fd_close → close(fd)
|
|
25
|
+
* fd_read → read(fd, length) (no position: the store's own cursor)
|
|
26
|
+
* fd_pread → read(fd, length, position)
|
|
27
|
+
* fd_write → write(fd, bytes)
|
|
28
|
+
* fd_pwrite → write(fd, bytes, position)
|
|
29
|
+
* fd_seek → resolved by the caller; the store's cursor moves on cursor reads/writes
|
|
30
|
+
* fd_sync / fd_datasync → fsync(fd) (store-wide; see the server's note)
|
|
31
|
+
* fd_filestat_get → fstat(fd)
|
|
32
|
+
* path_filestat_get → stat(path) / lstat(path)
|
|
33
|
+
* fd_readdir → readdir(path) or readdirPage(path, cursor) for the cookie form
|
|
34
|
+
* path_create_directory → mkdir(path)
|
|
35
|
+
* path_remove_directory → rmdir(path)
|
|
36
|
+
* path_unlink_file → unlink(path)
|
|
37
|
+
* path_rename → rename(oldPath, newPath)
|
|
38
|
+
* path_symlink → symlink(target, path) (targets are ABSOLUTE; see the core)
|
|
39
|
+
* path_readlink → readlink(path)
|
|
40
|
+
* fd_filestat_set_size → truncate(path, size) — the core resizes by PATH only, so the adapter
|
|
41
|
+
* keeps the path it opened each fd with and resolves it here
|
|
42
|
+
*
|
|
43
|
+
* Reads and writes larger than the channel's payload region are split transparently; a caller never
|
|
44
|
+
* has to know the channel size.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { FsError } from "../core/errors";
|
|
48
|
+
import {
|
|
49
|
+
FAULT_NONE,
|
|
50
|
+
FAULT_PROTOCOL,
|
|
51
|
+
FAULT_STORE,
|
|
52
|
+
HEADER_ERRNO,
|
|
53
|
+
HEADER_FAULT,
|
|
54
|
+
HEADER_OPCODE,
|
|
55
|
+
HEADER_REQUEST,
|
|
56
|
+
HEADER_RESPONSE,
|
|
57
|
+
HEADER_RESULT_HI,
|
|
58
|
+
HEADER_RESULT_LO,
|
|
59
|
+
HEADER_SEQUENCE,
|
|
60
|
+
HEADER_STATE,
|
|
61
|
+
OPCODE_CLOSE,
|
|
62
|
+
OPCODE_FSTAT,
|
|
63
|
+
OPCODE_FSYNC,
|
|
64
|
+
OPCODE_LSTAT,
|
|
65
|
+
OPCODE_MKDIR,
|
|
66
|
+
OPCODE_OPEN,
|
|
67
|
+
OPCODE_READ,
|
|
68
|
+
OPCODE_READDIR,
|
|
69
|
+
OPCODE_READLINK,
|
|
70
|
+
OPCODE_RENAME,
|
|
71
|
+
OPCODE_RMDIR,
|
|
72
|
+
OPCODE_SIZE,
|
|
73
|
+
OPCODE_STAT,
|
|
74
|
+
OPCODE_SYMLINK,
|
|
75
|
+
OPCODE_TRUNCATE,
|
|
76
|
+
OPCODE_UNLINK,
|
|
77
|
+
OPCODE_WRITE,
|
|
78
|
+
PayloadReader,
|
|
79
|
+
PayloadWriter,
|
|
80
|
+
READDIR_DONE,
|
|
81
|
+
STATE_DETACHED,
|
|
82
|
+
STATE_IDLE,
|
|
83
|
+
STATE_REQUEST,
|
|
84
|
+
STATE_RESPONSE,
|
|
85
|
+
errnoName,
|
|
86
|
+
fsErrorNameOf,
|
|
87
|
+
joinResult,
|
|
88
|
+
readStat,
|
|
89
|
+
} from "./protocol";
|
|
90
|
+
import type { BrokerStat, RepackedChannel } from "./protocol";
|
|
91
|
+
|
|
92
|
+
/** The transport failed: there is no answer, and this client can no longer make progress. */
|
|
93
|
+
export class RepackedBrokerTransportError extends Error {
|
|
94
|
+
readonly brokerCode: "detached" | "protocol" | "timeout";
|
|
95
|
+
|
|
96
|
+
constructor(brokerCode: "detached" | "protocol" | "timeout", message: string) {
|
|
97
|
+
super(message);
|
|
98
|
+
this.name = "RepackedBrokerTransportError";
|
|
99
|
+
this.brokerCode = brokerCode;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The store behind the broker failed. Not a file rejection, and not this client's mistake. */
|
|
104
|
+
export class RepackedBrokerStoreError extends Error {
|
|
105
|
+
readonly brokerCode = "store";
|
|
106
|
+
|
|
107
|
+
constructor(message: string) {
|
|
108
|
+
super(`the repacked store behind the broker failed: ${message}`);
|
|
109
|
+
this.name = "RepackedBrokerStoreError";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Every result carries an errno; 0 means the operation succeeded. */
|
|
114
|
+
export interface BrokerResult {
|
|
115
|
+
readonly errno: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface BrokerOpenResult extends BrokerResult {
|
|
119
|
+
readonly fd: number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface BrokerCountResult extends BrokerResult {
|
|
123
|
+
/** Bytes actually transferred. Short of the request means end-of-file (read) or a store limit (write). */
|
|
124
|
+
readonly count: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface BrokerReadResult extends BrokerCountResult {
|
|
128
|
+
readonly bytes: Uint8Array;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface BrokerStatResult extends BrokerResult {
|
|
132
|
+
readonly stat: BrokerStat | undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface BrokerSizeResult extends BrokerResult {
|
|
136
|
+
readonly size: bigint;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface BrokerReaddirResult extends BrokerResult {
|
|
140
|
+
readonly entries: readonly string[];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface BrokerReadlinkResult extends BrokerResult {
|
|
144
|
+
/** The link's target, or `undefined` when the call was rejected. */
|
|
145
|
+
readonly target: string | undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface BrokerReaddirPageResult extends BrokerReaddirResult {
|
|
149
|
+
/** The cursor to resume from, or `undefined` when the listing is complete. */
|
|
150
|
+
readonly nextCursor: number | undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface RepackedSyncClientOptions {
|
|
154
|
+
/**
|
|
155
|
+
* How long one request may go unanswered before the client gives up and throws. A dead coordinator
|
|
156
|
+
* would otherwise park this thread forever, which is worse than a loud failure. Pass `Infinity` for
|
|
157
|
+
* a host that genuinely prefers to wait.
|
|
158
|
+
*/
|
|
159
|
+
readonly requestTimeoutMs?: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 30_000;
|
|
163
|
+
|
|
164
|
+
/** The fixed bytes a read/write request spends before its data: fd + hasPosition + position + length. */
|
|
165
|
+
const TRANSFER_OVERHEAD_BYTES = 4 + 1 + 8 + 4;
|
|
166
|
+
|
|
167
|
+
export class RepackedSyncClient {
|
|
168
|
+
readonly channel: RepackedChannel;
|
|
169
|
+
readonly #timeoutMs: number;
|
|
170
|
+
readonly #reply: Uint8Array;
|
|
171
|
+
#sequence = 0;
|
|
172
|
+
|
|
173
|
+
constructor(channel: RepackedChannel, options: RepackedSyncClientOptions = {}) {
|
|
174
|
+
this.channel = channel;
|
|
175
|
+
this.#timeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
176
|
+
// A scratch copy of the reply region: the payload is shared memory the server reuses on the very
|
|
177
|
+
// next request, so nothing handed to a caller may point into it.
|
|
178
|
+
this.#reply = new Uint8Array(channel.payloadBytes);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The largest single read or write that fits one request. Reads/writes above it are chunked. */
|
|
182
|
+
get maxTransferBytes(): number {
|
|
183
|
+
return this.channel.payloadBytes - TRANSFER_OVERHEAD_BYTES;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
open(path: string, flags: number, mode = 0o100666): BrokerOpenResult {
|
|
187
|
+
const reply = this.#call(OPCODE_OPEN, (writer) => {
|
|
188
|
+
writer.string(path);
|
|
189
|
+
writer.u32(flags);
|
|
190
|
+
writer.u32(mode);
|
|
191
|
+
});
|
|
192
|
+
return { errno: reply.errno, fd: reply.errno === 0 ? Number(reply.result) : -1 };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
close(fd: number): BrokerResult {
|
|
196
|
+
return { errno: this.#call(OPCODE_CLOSE, (writer) => writer.u32(fd)).errno };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Read up to `length` bytes. With no `position` the store's own descriptor cursor is used and
|
|
201
|
+
* advances; with a `position` the cursor is untouched (`fd_pread`). A result shorter than `length`
|
|
202
|
+
* means end-of-file, never a partial transport.
|
|
203
|
+
*/
|
|
204
|
+
read(fd: number, length: number, position?: bigint): BrokerReadResult {
|
|
205
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
206
|
+
throw new RangeError("a broker read length must be a non-negative safe integer");
|
|
207
|
+
}
|
|
208
|
+
const output = new Uint8Array(length);
|
|
209
|
+
let filled = 0;
|
|
210
|
+
let errno = 0;
|
|
211
|
+
while (filled < length) {
|
|
212
|
+
const want = Math.min(length - filled, this.maxTransferBytes);
|
|
213
|
+
const at = position === undefined ? undefined : position + BigInt(filled);
|
|
214
|
+
const reply = this.#call(OPCODE_READ, (writer) => {
|
|
215
|
+
writer.u32(fd);
|
|
216
|
+
writer.u8(at === undefined ? 0 : 1);
|
|
217
|
+
writer.u64(at ?? 0n);
|
|
218
|
+
writer.u32(want);
|
|
219
|
+
});
|
|
220
|
+
if (reply.errno !== 0) {
|
|
221
|
+
errno = reply.errno;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
output.set(reply.bytes, filled);
|
|
225
|
+
filled += reply.bytes.byteLength;
|
|
226
|
+
if (reply.bytes.byteLength < want) break; // end-of-file
|
|
227
|
+
}
|
|
228
|
+
// A rejection part-way through still reports what already transferred, as a short read does.
|
|
229
|
+
return { errno, count: filled, bytes: output.subarray(0, filled) };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Write `bytes`. With no `position` the store's own descriptor cursor is used and advances; with a
|
|
234
|
+
* `position` the cursor is untouched (`fd_pwrite`). A descriptor opened with `O_APPEND` always
|
|
235
|
+
* writes at end-of-file and ignores `position`, exactly as POSIX requires.
|
|
236
|
+
*/
|
|
237
|
+
write(fd: number, bytes: Uint8Array, position?: bigint): BrokerCountResult {
|
|
238
|
+
let written = 0;
|
|
239
|
+
let errno = 0;
|
|
240
|
+
while (written < bytes.byteLength) {
|
|
241
|
+
const chunk = bytes.subarray(written, written + Math.min(bytes.byteLength - written, this.maxTransferBytes));
|
|
242
|
+
const at = position === undefined ? undefined : position + BigInt(written);
|
|
243
|
+
const reply = this.#call(OPCODE_WRITE, (writer) => {
|
|
244
|
+
writer.u32(fd);
|
|
245
|
+
writer.u8(at === undefined ? 0 : 1);
|
|
246
|
+
writer.u64(at ?? 0n);
|
|
247
|
+
writer.u32(chunk.byteLength);
|
|
248
|
+
writer.bytes(chunk);
|
|
249
|
+
});
|
|
250
|
+
if (reply.errno !== 0) {
|
|
251
|
+
errno = reply.errno;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
const count = Number(reply.result);
|
|
255
|
+
written += count;
|
|
256
|
+
if (count < chunk.byteLength) break; // the store admitted less than asked; stop rather than spin
|
|
257
|
+
}
|
|
258
|
+
return { errno, count: written };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Flush the store. Durability is STORE-WIDE, not per-descriptor: on success every byte written
|
|
263
|
+
* through this broker by any client before the call returned is recoverable. `fd` is validated so
|
|
264
|
+
* the call still rejects a descriptor this client does not own.
|
|
265
|
+
*/
|
|
266
|
+
fsync(fd?: number): BrokerResult {
|
|
267
|
+
return {
|
|
268
|
+
errno: this.#call(OPCODE_FSYNC, (writer) => {
|
|
269
|
+
writer.u8(fd === undefined ? 0 : 1);
|
|
270
|
+
writer.u32(fd ?? 0);
|
|
271
|
+
}).errno,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
fstat(fd: number): BrokerStatResult {
|
|
276
|
+
return this.#statCall(OPCODE_FSTAT, (writer) => writer.u32(fd));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
stat(path: string): BrokerStatResult {
|
|
280
|
+
return this.#statCall(OPCODE_STAT, (writer) => writer.string(path));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Reports the LINK itself when the final component is one; `stat` follows it instead. */
|
|
284
|
+
lstat(path: string): BrokerStatResult {
|
|
285
|
+
return this.#statCall(OPCODE_LSTAT, (writer) => writer.string(path));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Create a symbolic link at `path` pointing at `target`. Targets are ABSOLUTE — the store refuses
|
|
290
|
+
* a relative one with `EINVAL` rather than reinterpreting it against the link's directory.
|
|
291
|
+
*/
|
|
292
|
+
symlink(target: string, path: string): BrokerResult {
|
|
293
|
+
return {
|
|
294
|
+
errno: this.#call(OPCODE_SYMLINK, (writer) => {
|
|
295
|
+
writer.string(target);
|
|
296
|
+
writer.string(path);
|
|
297
|
+
}).errno,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The target of the symbolic link at `path`. `EINVAL` when the path is not a link. */
|
|
302
|
+
readlink(path: string): BrokerReadlinkResult {
|
|
303
|
+
const reply = this.#call(OPCODE_READLINK, (writer) => writer.string(path));
|
|
304
|
+
if (reply.errno !== 0) return { errno: reply.errno, target: undefined };
|
|
305
|
+
return { errno: 0, target: new PayloadReader(reply.bytes, reply.bytes.byteLength).string() };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** The complete listing, paged transparently over as many requests as the channel needs. */
|
|
309
|
+
readdir(path: string): BrokerReaddirResult {
|
|
310
|
+
const entries: string[] = [];
|
|
311
|
+
let cursor: number | undefined = 0;
|
|
312
|
+
while (cursor !== undefined) {
|
|
313
|
+
const page: BrokerReaddirPageResult = this.readdirPage(path, cursor);
|
|
314
|
+
if (page.errno !== 0) return { errno: page.errno, entries: [] };
|
|
315
|
+
entries.push(...page.entries);
|
|
316
|
+
cursor = page.nextCursor;
|
|
317
|
+
}
|
|
318
|
+
return { errno: 0, entries };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* One page of a listing, for a caller that owns its own cookie (WASI `fd_readdir`). The cursor is an
|
|
323
|
+
* index into the store's sorted listing, recomputed per call — a paged listing is therefore not an
|
|
324
|
+
* atomic snapshot, which is exactly what POSIX `readdir` allows.
|
|
325
|
+
*/
|
|
326
|
+
readdirPage(path: string, cursor = 0): BrokerReaddirPageResult {
|
|
327
|
+
const reply = this.#call(OPCODE_READDIR, (writer) => {
|
|
328
|
+
writer.string(path);
|
|
329
|
+
writer.u32(cursor);
|
|
330
|
+
});
|
|
331
|
+
if (reply.errno !== 0) return { errno: reply.errno, entries: [], nextCursor: undefined };
|
|
332
|
+
const reader = new PayloadReader(reply.bytes, reply.bytes.byteLength);
|
|
333
|
+
const count = reader.u32();
|
|
334
|
+
const next = reader.i32();
|
|
335
|
+
const entries: string[] = [];
|
|
336
|
+
for (let index = 0; index < count; index += 1) entries.push(reader.string());
|
|
337
|
+
return { errno: 0, entries, nextCursor: next === READDIR_DONE ? undefined : next };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
mkdir(path: string, options: { recursive?: boolean; mode?: number } = {}): BrokerResult {
|
|
341
|
+
return {
|
|
342
|
+
errno: this.#call(OPCODE_MKDIR, (writer) => {
|
|
343
|
+
writer.string(path);
|
|
344
|
+
writer.u8(options.recursive === true ? 1 : 0);
|
|
345
|
+
writer.u32(options.mode ?? 0o40777);
|
|
346
|
+
}).errno,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
rmdir(path: string): BrokerResult {
|
|
351
|
+
return { errno: this.#call(OPCODE_RMDIR, (writer) => writer.string(path)).errno };
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
unlink(path: string): BrokerResult {
|
|
355
|
+
return { errno: this.#call(OPCODE_UNLINK, (writer) => writer.string(path)).errno };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
rename(oldPath: string, newPath: string): BrokerResult {
|
|
359
|
+
return {
|
|
360
|
+
errno: this.#call(OPCODE_RENAME, (writer) => {
|
|
361
|
+
writer.string(oldPath);
|
|
362
|
+
writer.string(newPath);
|
|
363
|
+
}).errno,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Resize by PATH. The core store has no resize-by-descriptor, so a WASI `fd_filestat_set_size`
|
|
369
|
+
* adapter keeps the path each fd was opened with and calls this. Nothing is lost by that: the store
|
|
370
|
+
* has no hard links, and a descriptor whose path was unlinked meanwhile is an orphan the core keeps
|
|
371
|
+
* readable but no longer resizes.
|
|
372
|
+
*/
|
|
373
|
+
truncate(path: string, size: bigint): BrokerResult {
|
|
374
|
+
return {
|
|
375
|
+
errno: this.#call(OPCODE_TRUNCATE, (writer) => {
|
|
376
|
+
writer.string(path);
|
|
377
|
+
writer.u64(size);
|
|
378
|
+
}).errno,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** The size of one file, without the rest of a stat. `EISDIR` for a directory. */
|
|
383
|
+
size(path: string): BrokerSizeResult {
|
|
384
|
+
const reply = this.#call(OPCODE_SIZE, (writer) => writer.string(path));
|
|
385
|
+
return { errno: reply.errno, size: reply.errno === 0 ? reply.result : 0n };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
#statCall(opcode: number, encode: (writer: PayloadWriter) => void): BrokerStatResult {
|
|
389
|
+
const reply = this.#call(opcode, encode);
|
|
390
|
+
if (reply.errno !== 0) return { errno: reply.errno, stat: undefined };
|
|
391
|
+
return { errno: 0, stat: readStat(new PayloadReader(reply.bytes, reply.bytes.byteLength)) };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Publish one request, ring the doorbell, and park until the server answers. The returned `bytes`
|
|
396
|
+
* are a private copy: the shared payload is reused by the next request.
|
|
397
|
+
*/
|
|
398
|
+
#call(
|
|
399
|
+
opcode: number,
|
|
400
|
+
encode: (writer: PayloadWriter) => void,
|
|
401
|
+
): { readonly errno: number; readonly result: bigint; readonly bytes: Uint8Array } {
|
|
402
|
+
const header = this.channel.header;
|
|
403
|
+
const state = Atomics.load(header, HEADER_STATE);
|
|
404
|
+
if (state === STATE_DETACHED) {
|
|
405
|
+
throw new RepackedBrokerTransportError("detached", "the repacked broker detached this client");
|
|
406
|
+
}
|
|
407
|
+
if (state === STATE_REQUEST) {
|
|
408
|
+
throw new RepackedBrokerTransportError("protocol", "a repacked broker request is already in flight");
|
|
409
|
+
}
|
|
410
|
+
const writer = new PayloadWriter(this.channel.payload);
|
|
411
|
+
encode(writer);
|
|
412
|
+
this.#sequence = (this.#sequence + 1) | 0;
|
|
413
|
+
Atomics.store(header, HEADER_SEQUENCE, this.#sequence);
|
|
414
|
+
Atomics.store(header, HEADER_OPCODE, opcode);
|
|
415
|
+
Atomics.store(header, HEADER_REQUEST, writer.length);
|
|
416
|
+
Atomics.store(header, HEADER_RESPONSE, 0);
|
|
417
|
+
Atomics.store(header, HEADER_ERRNO, 0);
|
|
418
|
+
Atomics.store(header, HEADER_FAULT, FAULT_NONE);
|
|
419
|
+
// Publishing the state is the release edge for every payload byte written above; the doorbell is
|
|
420
|
+
// the only wakeup path, so nothing waits on this word from the server side.
|
|
421
|
+
Atomics.store(header, HEADER_STATE, STATE_REQUEST);
|
|
422
|
+
this.channel.doorbell.ring();
|
|
423
|
+
|
|
424
|
+
while (Atomics.load(header, HEADER_STATE) === STATE_REQUEST) {
|
|
425
|
+
const outcome = Atomics.wait(header, HEADER_STATE, STATE_REQUEST, this.#timeoutMs);
|
|
426
|
+
if (outcome === "timed-out" && Atomics.load(header, HEADER_STATE) === STATE_REQUEST) {
|
|
427
|
+
throw new RepackedBrokerTransportError(
|
|
428
|
+
"timeout",
|
|
429
|
+
`the repacked broker did not answer opcode ${opcode} within ${this.#timeoutMs} ms`,
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const responseBytes = Atomics.load(header, HEADER_RESPONSE);
|
|
435
|
+
const fault = Atomics.load(header, HEADER_FAULT);
|
|
436
|
+
const errno = Atomics.load(header, HEADER_ERRNO);
|
|
437
|
+
const result = joinResult(Atomics.load(header, HEADER_RESULT_LO), Atomics.load(header, HEADER_RESULT_HI));
|
|
438
|
+
let bytes: Uint8Array = new Uint8Array();
|
|
439
|
+
if (responseBytes > 0 && responseBytes <= this.channel.payloadBytes) {
|
|
440
|
+
this.#reply.set(this.channel.payload.subarray(0, responseBytes));
|
|
441
|
+
bytes = this.#reply.subarray(0, responseBytes);
|
|
442
|
+
}
|
|
443
|
+
if (Atomics.load(header, HEADER_STATE) === STATE_DETACHED) {
|
|
444
|
+
throw new RepackedBrokerTransportError(
|
|
445
|
+
fault === FAULT_PROTOCOL ? "protocol" : "detached",
|
|
446
|
+
fault === FAULT_PROTOCOL
|
|
447
|
+
? `the repacked broker rejected opcode ${opcode} as a protocol violation and detached this client`
|
|
448
|
+
: "the repacked broker detached this client",
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
if (Atomics.load(header, HEADER_STATE) !== STATE_RESPONSE) {
|
|
452
|
+
throw new RepackedBrokerTransportError("protocol", "the repacked broker left the channel in an unknown state");
|
|
453
|
+
}
|
|
454
|
+
Atomics.store(header, HEADER_STATE, STATE_IDLE);
|
|
455
|
+
if (fault === FAULT_STORE) throw new RepackedBrokerStoreError(this.#faultMessage(bytes));
|
|
456
|
+
if (fault !== FAULT_NONE) {
|
|
457
|
+
throw new RepackedBrokerTransportError("protocol", `the repacked broker reported fault ${fault}`);
|
|
458
|
+
}
|
|
459
|
+
return { errno, result, bytes };
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#faultMessage(bytes: Uint8Array): string {
|
|
463
|
+
if (bytes.byteLength === 0) return "no detail was reported";
|
|
464
|
+
try {
|
|
465
|
+
return new PayloadReader(bytes, bytes.byteLength).string();
|
|
466
|
+
} catch {
|
|
467
|
+
return "the fault detail was malformed";
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Turn a broker result into the throw the core store would have produced, for a caller that prefers
|
|
474
|
+
* exceptions to errnos. The rebuilt `FsError` carries the same numeric `code` the broker reported.
|
|
475
|
+
*/
|
|
476
|
+
export function throwOnErrno(result: BrokerResult, operation: string, path?: string): void {
|
|
477
|
+
if (result.errno === 0) return;
|
|
478
|
+
const name = fsErrorNameOf(result.errno);
|
|
479
|
+
const detail = `${operation} failed with ${errnoName(result.errno)}`;
|
|
480
|
+
if (name === undefined) throw new RepackedBrokerTransportError("protocol", `${detail} (unknown to the core)`);
|
|
481
|
+
throw new FsError(name, detail, path === undefined ? { operation } : { operation, path });
|
|
482
|
+
}
|