@pgxsinkit/pgwasm-c 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.
Files changed (85) hide show
  1. package/LICENSE +23 -0
  2. package/NOTICE +1890 -0
  3. package/README.md +79 -0
  4. package/artefacts/amcheck.tar.gz +0 -0
  5. package/artefacts/initdb.d.ts +6 -0
  6. package/artefacts/initdb.js +2 -0
  7. package/artefacts/initdb.wasm +0 -0
  8. package/artefacts/postgres.d.ts +6 -0
  9. package/artefacts/postgres.data +37595 -8
  10. package/artefacts/postgres.js +2 -0
  11. package/artefacts/postgres.wasm +0 -0
  12. package/artefacts/prepopulated.tar.gz +0 -0
  13. package/dist/artefact-pins.d.ts +57 -0
  14. package/dist/artefact-pins.d.ts.map +1 -0
  15. package/dist/artefacts.d.ts +37 -0
  16. package/dist/artefacts.d.ts.map +1 -0
  17. package/dist/build.d.ts +53 -0
  18. package/dist/build.d.ts.map +1 -0
  19. package/dist/contrib/amcheck.d.ts +9 -0
  20. package/dist/contrib/amcheck.d.ts.map +1 -0
  21. package/dist/contrib/amcheck.js +13 -0
  22. package/dist/contrib/amcheck.js.map +10 -0
  23. package/dist/host/artefact-loader.d.ts +9 -0
  24. package/dist/host/artefact-loader.d.ts.map +1 -0
  25. package/dist/host/command-line.d.ts +11 -0
  26. package/dist/host/command-line.d.ts.map +1 -0
  27. package/dist/host/data-dir.d.ts +11 -0
  28. package/dist/host/data-dir.d.ts.map +1 -0
  29. package/dist/host/emscripten.d.ts +143 -0
  30. package/dist/host/emscripten.d.ts.map +1 -0
  31. package/dist/host/exit-code.d.ts +12 -0
  32. package/dist/host/exit-code.d.ts.map +1 -0
  33. package/dist/host/extensions.d.ts +15 -0
  34. package/dist/host/extensions.d.ts.map +1 -0
  35. package/dist/host/initdb.d.ts +25 -0
  36. package/dist/host/initdb.d.ts.map +1 -0
  37. package/dist/host/mounts/file.d.ts +17 -0
  38. package/dist/host/mounts/file.d.ts.map +1 -0
  39. package/dist/host/mounts/idb.d.ts +32 -0
  40. package/dist/host/mounts/idb.d.ts.map +1 -0
  41. package/dist/host/mounts/index.d.ts +6 -0
  42. package/dist/host/mounts/index.d.ts.map +1 -0
  43. package/dist/host/mounts/memory.d.ts +12 -0
  44. package/dist/host/mounts/memory.d.ts.map +1 -0
  45. package/dist/host/mounts/storage-mount.d.ts +15 -0
  46. package/dist/host/mounts/storage-mount.d.ts.map +1 -0
  47. package/dist/host/mounts/vfs.d.ts +15 -0
  48. package/dist/host/mounts/vfs.d.ts.map +1 -0
  49. package/dist/host/paths.d.ts +10 -0
  50. package/dist/host/paths.d.ts.map +1 -0
  51. package/dist/host/postgres-instance.d.ts +63 -0
  52. package/dist/host/postgres-instance.d.ts.map +1 -0
  53. package/dist/host/vfs-mount.d.ts +10 -0
  54. package/dist/host/vfs-mount.d.ts.map +1 -0
  55. package/dist/index.d.ts +4 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +1393 -0
  58. package/dist/index.js.map +26 -0
  59. package/dist/prepopulated.d.ts +28 -0
  60. package/dist/prepopulated.d.ts.map +1 -0
  61. package/dist/prepopulated.js +15 -0
  62. package/dist/prepopulated.js.map +10 -0
  63. package/package.json +56 -0
  64. package/src/artefact-pins.ts +36 -0
  65. package/src/artefacts.ts +46 -0
  66. package/src/build.ts +326 -0
  67. package/src/contrib/amcheck.ts +17 -0
  68. package/src/host/artefact-loader.ts +75 -0
  69. package/src/host/command-line.ts +64 -0
  70. package/src/host/data-dir.ts +70 -0
  71. package/src/host/emscripten.ts +158 -0
  72. package/src/host/exit-code.ts +29 -0
  73. package/src/host/extensions.ts +83 -0
  74. package/src/host/initdb.ts +180 -0
  75. package/src/host/mounts/file.ts +44 -0
  76. package/src/host/mounts/idb.ts +152 -0
  77. package/src/host/mounts/index.ts +26 -0
  78. package/src/host/mounts/memory.ts +22 -0
  79. package/src/host/mounts/storage-mount.ts +15 -0
  80. package/src/host/mounts/vfs.ts +43 -0
  81. package/src/host/paths.ts +18 -0
  82. package/src/host/postgres-instance.ts +495 -0
  83. package/src/host/vfs-mount.ts +258 -0
  84. package/src/index.ts +3 -0
  85. package/src/prepopulated.ts +38 -0
@@ -0,0 +1,258 @@
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 { ERRNO_CODES, type BaseFilesystem } from "@pgxsinkit/pgwasm/fs";
5
+
6
+ import type { EmscriptenFilesystemType, EmscriptenNode, EmscriptenStream, PostgresModule } from "./emscripten";
7
+
8
+ /** The node attributes Emscripten passes to `setattr`. */
9
+ interface NodeAttributes {
10
+ mode?: number;
11
+ size?: number;
12
+ timestamp?: number;
13
+ }
14
+
15
+ /** A Uint8Array over exactly the bytes of `buffer` (Emscripten hands the heap as an Int8Array). */
16
+ function asBytes(buffer: ArrayBufferView): Uint8Array {
17
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
18
+ }
19
+
20
+ /**
21
+ * An Emscripten filesystem over a pgwasm {@link BaseFilesystem}: each node and stream operation becomes a
22
+ * call on the filesystem, by path relative to the mount. A thrown error becomes an errno for Postgres —
23
+ * its `code` when it has one, `EIO` when it does not (a DOMException, a plain Error from the storage):
24
+ * rethrown, it would unwind the engine mid-syscall instead of failing that syscall.
25
+ */
26
+ export function createVfsFilesystem(module: PostgresModule, vfs: BaseFilesystem): EmscriptenFilesystemType {
27
+ const FS = module.FS;
28
+ const log = vfs.debug ? console.log : undefined;
29
+
30
+ const tryOperation = <T>(fn: () => T, op: string, path: string | (() => string)): T => {
31
+ try {
32
+ return fn();
33
+ } catch (error) {
34
+ // Already an errno (a filesystem may throw Emscripten's own error).
35
+ if (error instanceof FS.ErrnoError) throw error;
36
+ const code = (error as { code?: unknown } | null)?.code;
37
+ const errno = !code ? ERRNO_CODES.EIO : code === "UNKNOWN" ? ERRNO_CODES.EINVAL : code;
38
+ const message = (error as { message?: unknown } | null)?.message;
39
+ vfs.recordSyscallError(
40
+ op,
41
+ typeof path === "function" ? path() : path,
42
+ typeof errno === "number" ? errno : -1,
43
+ typeof message === "string" ? message : String(code ?? error),
44
+ );
45
+ throw new FS.ErrnoError(typeof errno === "number" ? errno : ERRNO_CODES.EIO);
46
+ }
47
+ };
48
+
49
+ const realPath = (node: EmscriptenNode): string => {
50
+ const parts: string[] = [];
51
+ let current = node;
52
+ while (current.parent !== current) {
53
+ parts.push(current.name);
54
+ current = current.parent;
55
+ }
56
+ parts.push(current.mount.opts.root ?? "");
57
+ parts.reverse();
58
+ return parts.join("/");
59
+ };
60
+
61
+ const createNode = (parent: EmscriptenNode | null, name: string, mode: number): EmscriptenNode => {
62
+ if (!FS.isDir(mode) && !FS.isFile(mode)) {
63
+ throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
64
+ }
65
+ const node = FS.createNode(parent, name, mode);
66
+ node.node_ops = nodeOps;
67
+ node.stream_ops = streamOps;
68
+ return node;
69
+ };
70
+
71
+ const getMode = (path: string): number => tryOperation(() => vfs.lstat(path).mode, "getMode", path);
72
+
73
+ const nodeOps = {
74
+ getattr(node: EmscriptenNode) {
75
+ const path = realPath(node);
76
+ log?.("getattr", path);
77
+ return tryOperation(
78
+ () => {
79
+ const stats = vfs.lstat(path);
80
+ return {
81
+ ...stats,
82
+ dev: 0,
83
+ ino: node.id,
84
+ nlink: 1,
85
+ rdev: node.rdev,
86
+ atime: new Date(stats.atime),
87
+ mtime: new Date(stats.mtime),
88
+ ctime: new Date(stats.ctime),
89
+ };
90
+ },
91
+ "getattr",
92
+ path,
93
+ );
94
+ },
95
+ setattr(node: EmscriptenNode, attr: NodeAttributes): void {
96
+ const path = realPath(node);
97
+ log?.("setattr", path, attr);
98
+ tryOperation(
99
+ () => {
100
+ if (attr.mode !== undefined) vfs.chmod(path, attr.mode);
101
+ if (attr.size !== undefined) vfs.truncate(path, attr.size);
102
+ if (attr.timestamp !== undefined) vfs.utimes(path, attr.timestamp, attr.timestamp);
103
+ },
104
+ "setattr",
105
+ path,
106
+ );
107
+ },
108
+ lookup(parent: EmscriptenNode, name: string): EmscriptenNode {
109
+ const path = `${realPath(parent)}/${name}`;
110
+ log?.("lookup", path);
111
+ return createNode(parent, name, getMode(path));
112
+ },
113
+ mknod(parent: EmscriptenNode, name: string, mode: number): EmscriptenNode {
114
+ const node = createNode(parent, name, mode);
115
+ const path = realPath(node);
116
+ log?.("mknod", path, mode);
117
+ return tryOperation(
118
+ () => {
119
+ if (FS.isDir(node.mode)) {
120
+ vfs.mkdir(path, { mode });
121
+ } else {
122
+ vfs.writeFile(path, "", { mode });
123
+ }
124
+ return node;
125
+ },
126
+ "mknod",
127
+ path,
128
+ );
129
+ },
130
+ rename(oldNode: EmscriptenNode, newDir: EmscriptenNode, newName: string): void {
131
+ const oldPath = realPath(oldNode);
132
+ const newPath = `${realPath(newDir)}/${newName}`;
133
+ log?.("rename", oldPath, newPath);
134
+ tryOperation(() => vfs.rename(oldPath, newPath), "rename", oldPath);
135
+ oldNode.name = newName;
136
+ },
137
+ unlink(parent: EmscriptenNode, name: string): void {
138
+ const path = `${realPath(parent)}/${name}`;
139
+ log?.("unlink", path);
140
+ try {
141
+ vfs.unlink(path);
142
+ } catch (error) {
143
+ // An unlink failure is not reported to Postgres, only recorded.
144
+ const code = (error as { code?: unknown } | null)?.code;
145
+ if (code) {
146
+ const message = (error as { message?: unknown } | null)?.message;
147
+ vfs.recordSyscallError(
148
+ "unlink",
149
+ path,
150
+ typeof code === "number" ? code : -1,
151
+ typeof message === "string" ? message : JSON.stringify(code),
152
+ );
153
+ }
154
+ }
155
+ },
156
+ rmdir(parent: EmscriptenNode, name: string): void {
157
+ const path = `${realPath(parent)}/${name}`;
158
+ log?.("rmdir", path);
159
+ tryOperation(() => vfs.rmdir(path), "rmdir", path);
160
+ },
161
+ readdir(node: EmscriptenNode): string[] {
162
+ const path = realPath(node);
163
+ log?.("readdir", path);
164
+ return tryOperation(() => vfs.readdir(path), "readdir", path);
165
+ },
166
+ symlink(): never {
167
+ throw new FS.ErrnoError(63); // EPERM: not supported
168
+ },
169
+ readlink(): never {
170
+ throw new FS.ErrnoError(63);
171
+ },
172
+ };
173
+
174
+ const streamOps = {
175
+ open(stream: EmscriptenStream): void {
176
+ const path = realPath(stream.node);
177
+ log?.("open", path);
178
+ tryOperation(
179
+ () => {
180
+ if (FS.isFile(stream.node.mode)) {
181
+ stream.shared.refcount = 1;
182
+ stream.nfd = vfs.open(path);
183
+ }
184
+ },
185
+ "open",
186
+ path,
187
+ );
188
+ },
189
+ close(stream: EmscriptenStream): void {
190
+ log?.("close", realPath(stream.node));
191
+ tryOperation(
192
+ () => {
193
+ if (FS.isFile(stream.node.mode) && stream.nfd !== undefined && --stream.shared.refcount === 0) {
194
+ vfs.close(stream.nfd);
195
+ }
196
+ },
197
+ "close",
198
+ () => realPath(stream.node),
199
+ );
200
+ },
201
+ dup(stream: EmscriptenStream): void {
202
+ stream.shared.refcount++;
203
+ },
204
+ read(stream: EmscriptenStream, buffer: ArrayBufferView, offset: number, length: number, position: number): number {
205
+ if (length === 0) return 0;
206
+ const fd = stream.nfd ?? -1;
207
+ return tryOperation(
208
+ () => vfs.read(fd, asBytes(buffer), offset, length, position),
209
+ "read",
210
+ () => realPath(stream.node),
211
+ );
212
+ },
213
+ write(stream: EmscriptenStream, buffer: ArrayBufferView, offset: number, length: number, position: number): number {
214
+ const fd = stream.nfd ?? -1;
215
+ return tryOperation(
216
+ () => vfs.write(fd, asBytes(buffer), offset, length, position),
217
+ "write",
218
+ () => realPath(stream.node),
219
+ );
220
+ },
221
+ llseek(stream: EmscriptenStream, offset: number, whence: number): number {
222
+ let position = offset;
223
+ if (whence === 1) {
224
+ position += stream.position;
225
+ } else if (whence === 2 && FS.isFile(stream.node.mode)) {
226
+ const fd = stream.nfd ?? -1;
227
+ tryOperation(
228
+ () => {
229
+ position += vfs.fstat(fd).size;
230
+ },
231
+ "llseek",
232
+ () => realPath(stream.node),
233
+ );
234
+ }
235
+ if (position < 0) throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
236
+ return position;
237
+ },
238
+ mmap(): never {
239
+ // The runtime exports no mmapAlloc; Postgres does not map data-directory files.
240
+ throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
241
+ },
242
+ msync(): number {
243
+ return 0;
244
+ },
245
+ };
246
+
247
+ return {
248
+ mount(): EmscriptenNode {
249
+ return createNode(null, "/", 0o40000 | 0o777);
250
+ },
251
+ syncfs(_mount: unknown, _populate: unknown, done: (error: unknown) => void): void {
252
+ done(null);
253
+ },
254
+ createNode,
255
+ node_ops: nodeOps,
256
+ stream_ops: streamOps,
257
+ };
258
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { cBuildArtefacts } from "./artefacts";
2
+ export { C_BUILD_IDENTITY, cBuild, createCBuild, type CBuildAssets, type CBuildOptions } from "./build";
3
+ export { idbDatabaseName } from "./host/mounts/idb";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The C build's prepopulated data directory: a Store backup of a freshly initialised database, to start
3
+ * from instead of running initdb.
4
+ *
5
+ * This entry point is emitted at its own depth (`dist/prepopulated.js`), so its relative reference to the
6
+ * artefact stays valid once bundled; the `new URL` stays a literal so bundlers copy and fingerprint the
7
+ * file.
8
+ */
9
+
10
+ const prepopulatedUrl = new URL("../artefacts/prepopulated.tar.gz", import.meta.url);
11
+
12
+ /**
13
+ * The prepopulated data directory, as a gzipped Store backup, for `createPgwasm({ build, loadDataDir })`:
14
+ *
15
+ * ```ts
16
+ * import { createPgwasm } from "@pgxsinkit/pgwasm";
17
+ * import { cBuild } from "@pgxsinkit/pgwasm-c";
18
+ * import { prepopulatedDataDir } from "@pgxsinkit/pgwasm-c/prepopulated";
19
+ *
20
+ * const pg = await createPgwasm({ build: cBuild, loadDataDir: await prepopulatedDataDir() });
21
+ * ```
22
+ *
23
+ * It is fetched by URL, like the build's other files, in Bun and in browsers. It is an asset of the pinned
24
+ * pgwasm-postgres release, made deterministically by that release's own initdb; it is left unmarked, and
25
+ * `createPgwasm` adds the marker whenever it restores an unmarked backup, so every data directory created
26
+ * from it is marked as the C build's. Its directories are 0750 and its files 0640; a restore does not
27
+ * carry a mode into the data directory. Like any backup of a running database it holds `/postmaster.pid`,
28
+ * recording the engine's own process id: the C build treats it as stale and writes its own on start.
29
+ */
30
+ export async function prepopulatedDataDir(): Promise<Blob> {
31
+ const response = await fetch(prepopulatedUrl);
32
+ if (!response.ok) {
33
+ throw new Error(
34
+ `Could not load the prepopulated data directory ${prepopulatedUrl.href}: HTTP ${response.status} ${response.statusText}`,
35
+ );
36
+ }
37
+ return await response.blob();
38
+ }