@oliphaunt/wasix-ts 0.1.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 (169) hide show
  1. package/ARCHITECTURE.md +655 -0
  2. package/CHANGELOG.md +33 -0
  3. package/LICENSE +21 -0
  4. package/README.md +404 -0
  5. package/THIRD_PARTY_NOTICES.md +20 -0
  6. package/lib/archive.d.ts +21 -0
  7. package/lib/archive.js +336 -0
  8. package/lib/asset-source.d.ts +4 -0
  9. package/lib/asset-source.js +15 -0
  10. package/lib/byte-channel.d.ts +27 -0
  11. package/lib/byte-channel.js +170 -0
  12. package/lib/client-common.d.ts +6 -0
  13. package/lib/client-common.js +36 -0
  14. package/lib/client.d.ts +7 -0
  15. package/lib/client.js +24 -0
  16. package/lib/database-root.d.ts +19 -0
  17. package/lib/database-root.js +139 -0
  18. package/lib/database.d.ts +135 -0
  19. package/lib/database.js +1039 -0
  20. package/lib/descriptor-validation.d.ts +10 -0
  21. package/lib/descriptor-validation.js +75 -0
  22. package/lib/direct-client-common.d.ts +50 -0
  23. package/lib/direct-client-common.js +717 -0
  24. package/lib/direct-client.d.ts +4 -0
  25. package/lib/direct-client.js +13 -0
  26. package/lib/direct.node.d.ts +2 -0
  27. package/lib/direct.node.js +2 -0
  28. package/lib/errors.d.ts +25 -0
  29. package/lib/errors.js +34 -0
  30. package/lib/extension-descriptor.d.ts +14 -0
  31. package/lib/extension-descriptor.js +419 -0
  32. package/lib/extensions.d.ts +68 -0
  33. package/lib/extensions.js +769 -0
  34. package/lib/host/LICENSE +21 -0
  35. package/lib/host/index.d.mts +123 -0
  36. package/lib/host/index.mjs +11 -0
  37. package/lib/host/provenance.json +16 -0
  38. package/lib/host/wasmer_js_bg.wasm +0 -0
  39. package/lib/host/worker.mjs +11 -0
  40. package/lib/host-runtime.d.ts +5 -0
  41. package/lib/host-runtime.js +13 -0
  42. package/lib/icu-descriptor.d.ts +3 -0
  43. package/lib/icu-descriptor.js +92 -0
  44. package/lib/index.bun.d.ts +2 -0
  45. package/lib/index.bun.js +2 -0
  46. package/lib/index.d.ts +2 -0
  47. package/lib/index.deno.d.ts +2 -0
  48. package/lib/index.deno.js +2 -0
  49. package/lib/index.js +2 -0
  50. package/lib/index.node.d.ts +2 -0
  51. package/lib/index.node.js +2 -0
  52. package/lib/internal-common.d.ts +12 -0
  53. package/lib/internal-common.js +274 -0
  54. package/lib/internal.d.ts +5 -0
  55. package/lib/internal.js +42 -0
  56. package/lib/internal.node.d.ts +5 -0
  57. package/lib/internal.node.js +8 -0
  58. package/lib/native-addon.d.ts +111 -0
  59. package/lib/native-addon.js +223 -0
  60. package/lib/native-server.d.ts +21 -0
  61. package/lib/native-server.js +109 -0
  62. package/lib/native-session.d.ts +60 -0
  63. package/lib/native-session.js +565 -0
  64. package/lib/node-actor.d.ts +7 -0
  65. package/lib/node-actor.js +10 -0
  66. package/lib/node-client-common.d.ts +9 -0
  67. package/lib/node-client-common.js +35 -0
  68. package/lib/node-client.d.ts +4 -0
  69. package/lib/node-client.js +13 -0
  70. package/lib/node-direct.d.ts +7 -0
  71. package/lib/node-direct.js +10 -0
  72. package/lib/node-worker-options.d.ts +5 -0
  73. package/lib/node-worker-options.js +65 -0
  74. package/lib/node-worker-port.d.ts +4 -0
  75. package/lib/node-worker-port.js +116 -0
  76. package/lib/node-worker.d.ts +1 -0
  77. package/lib/node-worker.js +38 -0
  78. package/lib/pgwire-connection.d.ts +60 -0
  79. package/lib/pgwire-connection.js +528 -0
  80. package/lib/pgwire.d.ts +3 -0
  81. package/lib/pgwire.js +105 -0
  82. package/lib/physical-archive.d.ts +29 -0
  83. package/lib/physical-archive.js +527 -0
  84. package/lib/protocol.d.ts +1 -0
  85. package/lib/protocol.js +1 -0
  86. package/lib/public.d.ts +4 -0
  87. package/lib/public.js +3 -0
  88. package/lib/query.d.ts +1 -0
  89. package/lib/query.js +1 -0
  90. package/lib/rpc.d.ts +203 -0
  91. package/lib/rpc.js +84 -0
  92. package/lib/runtime-descriptor.d.ts +3 -0
  93. package/lib/runtime-descriptor.js +79 -0
  94. package/lib/server.node.d.ts +1 -0
  95. package/lib/server.node.js +1 -0
  96. package/lib/startup-config.d.ts +2 -0
  97. package/lib/startup-config.js +19 -0
  98. package/lib/storage/bun.d.ts +6 -0
  99. package/lib/storage/bun.js +6 -0
  100. package/lib/storage/deno.d.ts +7 -0
  101. package/lib/storage/deno.js +7 -0
  102. package/lib/storage/incremental-storage.d.ts +25 -0
  103. package/lib/storage/incremental-storage.js +154 -0
  104. package/lib/storage/indexed-db-provider.d.ts +40 -0
  105. package/lib/storage/indexed-db-provider.js +259 -0
  106. package/lib/storage/indexed-db.d.ts +9 -0
  107. package/lib/storage/indexed-db.js +11 -0
  108. package/lib/storage/node.d.ts +10 -0
  109. package/lib/storage/node.js +13 -0
  110. package/lib/storage/opfs-pool.d.ts +31 -0
  111. package/lib/storage/opfs-pool.js +1271 -0
  112. package/lib/storage/opfs-provider.d.ts +4 -0
  113. package/lib/storage/opfs-provider.js +257 -0
  114. package/lib/storage/opfs.d.ts +8 -0
  115. package/lib/storage/opfs.js +10 -0
  116. package/lib/storage/restore-cleanup.d.ts +4 -0
  117. package/lib/storage/restore-cleanup.js +22 -0
  118. package/lib/storage/web-lock.d.ts +2 -0
  119. package/lib/storage/web-lock.js +64 -0
  120. package/lib/storage-provider.d.ts +47 -0
  121. package/lib/storage-provider.js +141 -0
  122. package/lib/storage-snapshot.d.ts +44 -0
  123. package/lib/storage-snapshot.js +274 -0
  124. package/lib/storage.d.ts +46 -0
  125. package/lib/storage.js +83 -0
  126. package/lib/tool-runtime.d.ts +43 -0
  127. package/lib/tool-runtime.js +93 -0
  128. package/lib/tool-worker-common.d.ts +48 -0
  129. package/lib/tool-worker-common.js +97 -0
  130. package/lib/tool-worker.d.ts +1 -0
  131. package/lib/tool-worker.js +10 -0
  132. package/lib/types.d.ts +230 -0
  133. package/lib/types.js +1 -0
  134. package/lib/wasix-runtime.d.ts +24 -0
  135. package/lib/wasix-runtime.js +186 -0
  136. package/lib/worker-client.d.ts +4 -0
  137. package/lib/worker-client.js +45 -0
  138. package/lib/worker-dispatch.d.ts +11 -0
  139. package/lib/worker-dispatch.js +174 -0
  140. package/lib/worker-entry.bun.d.ts +2 -0
  141. package/lib/worker-entry.bun.js +2 -0
  142. package/lib/worker-entry.d.ts +2 -0
  143. package/lib/worker-entry.deno.d.ts +2 -0
  144. package/lib/worker-entry.deno.js +2 -0
  145. package/lib/worker-entry.js +2 -0
  146. package/lib/worker-entry.node.d.ts +2 -0
  147. package/lib/worker-entry.node.js +2 -0
  148. package/lib/worker-node-client.d.ts +4 -0
  149. package/lib/worker-node-client.js +55 -0
  150. package/lib/worker-rpc.d.ts +36 -0
  151. package/lib/worker-rpc.js +422 -0
  152. package/lib/worker-transfer.d.ts +6 -0
  153. package/lib/worker-transfer.js +11 -0
  154. package/lib/worker.d.ts +1 -0
  155. package/lib/worker.js +21 -0
  156. package/lib/zstd.d.ts +4 -0
  157. package/lib/zstd.js +12 -0
  158. package/node_modules/@oliphaunt/js-core/README.md +7 -0
  159. package/node_modules/@oliphaunt/js-core/dist/commonjs/protocol.d.ts +1 -0
  160. package/node_modules/@oliphaunt/js-core/dist/commonjs/protocol.js +22 -0
  161. package/node_modules/@oliphaunt/js-core/dist/commonjs/query.d.ts +255 -0
  162. package/node_modules/@oliphaunt/js-core/dist/commonjs/query.js +2068 -0
  163. package/node_modules/@oliphaunt/js-core/dist/module/package.json +3 -0
  164. package/node_modules/@oliphaunt/js-core/dist/module/protocol.d.ts +1 -0
  165. package/node_modules/@oliphaunt/js-core/dist/module/protocol.js +19 -0
  166. package/node_modules/@oliphaunt/js-core/dist/module/query.d.ts +255 -0
  167. package/node_modules/@oliphaunt/js-core/dist/module/query.js +2039 -0
  168. package/node_modules/@oliphaunt/js-core/package.json +21 -0
  169. package/package.json +122 -0
@@ -0,0 +1,21 @@
1
+ import type { OpenConfig } from './types.js';
2
+ export type ServerListen = Readonly<{
3
+ transport: 'tcp';
4
+ port?: number;
5
+ }> | Readonly<{
6
+ transport: 'unix';
7
+ directory: string;
8
+ port?: number;
9
+ }>;
10
+ export type ServerOpenConfig = OpenConfig & Readonly<{
11
+ listen?: ServerListen;
12
+ }>;
13
+ export type OliphauntServer = Readonly<{
14
+ connectionString: string;
15
+ /** True after the one terminal close attempt settles, including on failure. */
16
+ readonly closed: boolean;
17
+ close(): Promise<void>;
18
+ [Symbol.asyncDispose](): Promise<void>;
19
+ }>;
20
+ /** Open the Rust WASIX local server without a JavaScript socket/protocol relay. */
21
+ export declare function openServer(config?: ServerOpenConfig): Promise<OliphauntServer>;
@@ -0,0 +1,109 @@
1
+ import { resolve } from 'node:path';
2
+ import { serializeOpenConfig } from './client-common.js';
3
+ import { mapNativeError, nativeWasixOpenOptions, requireCompatibleNativeWasixAddon, } from './native-session.js';
4
+ import { requireNodeStorage } from './node-client-common.js';
5
+ /** Open the Rust WASIX local server without a JavaScript socket/protocol relay. */
6
+ export async function openServer(config = {}) {
7
+ const { listen: requestedListen, ...databaseConfig } = config;
8
+ const options = serializeOpenConfig(databaseConfig);
9
+ requireNodeStorage(options);
10
+ const addon = requireCompatibleNativeWasixAddon(options);
11
+ const listen = nativeListen(requestedListen ?? { transport: 'tcp' });
12
+ let storage;
13
+ if (options.storage.kind === 'memory') {
14
+ storage = { kind: 'memory' };
15
+ }
16
+ else if (options.storage.kind === 'directory') {
17
+ storage = { kind: 'directory', path: options.storage.path };
18
+ }
19
+ else {
20
+ throw new TypeError('Oliphaunt WASIX native server requires memory or directory storage');
21
+ }
22
+ let handle;
23
+ let connectionString;
24
+ try {
25
+ handle = await addon.NativeWasixServer.open({
26
+ ...nativeWasixOpenOptions(options, storage),
27
+ listen,
28
+ });
29
+ connectionString = validateServerHandle(handle);
30
+ }
31
+ catch (error) {
32
+ throw mapNativeError(error);
33
+ }
34
+ return new NativeServerState(handle, connectionString).publicHandle();
35
+ }
36
+ class NativeServerState {
37
+ #handle;
38
+ #connectionString;
39
+ #closed = false;
40
+ #closeAttempt;
41
+ constructor(handle, connectionString) {
42
+ this.#handle = handle;
43
+ this.#connectionString = connectionString;
44
+ }
45
+ publicHandle() {
46
+ const state = this;
47
+ return Object.freeze({
48
+ connectionString: this.#connectionString,
49
+ get closed() {
50
+ return state.#closed || state.#handle.closed;
51
+ },
52
+ close: () => state.close(),
53
+ [Symbol.asyncDispose]: () => state.close(),
54
+ });
55
+ }
56
+ close() {
57
+ if (this.#closeAttempt !== undefined)
58
+ return this.#closeAttempt;
59
+ this.#closeAttempt = this.#close();
60
+ return this.#closeAttempt;
61
+ }
62
+ async #close() {
63
+ try {
64
+ await this.#handle.close();
65
+ }
66
+ catch (error) {
67
+ throw mapNativeError(error);
68
+ }
69
+ finally {
70
+ this.#closed = true;
71
+ }
72
+ }
73
+ }
74
+ function nativeListen(listen) {
75
+ if (listen.transport === 'tcp') {
76
+ if (listen.port !== undefined &&
77
+ (!Number.isSafeInteger(listen.port) || listen.port < 1 || listen.port > 65_535)) {
78
+ throw new TypeError('Oliphaunt WASIX TCP port must be an integer in 1..=65535');
79
+ }
80
+ return listen.port === undefined
81
+ ? { transport: 'tcp' }
82
+ : { transport: 'tcp', port: listen.port };
83
+ }
84
+ if (process.platform === 'win32') {
85
+ throw new Error('Unix-domain server listeners are not supported on Windows');
86
+ }
87
+ if (typeof listen.directory !== 'string' ||
88
+ listen.directory.trim().length === 0 ||
89
+ listen.directory.includes('\0')) {
90
+ throw new TypeError('WASIX server Unix socket directory must be non-empty without NUL bytes');
91
+ }
92
+ const port = listen.port ?? 5432;
93
+ if (!Number.isSafeInteger(port) || port < 1 || port > 65_535) {
94
+ throw new TypeError('Oliphaunt WASIX Unix socket port must be an integer in 1..=65535');
95
+ }
96
+ const directory = resolve(listen.directory);
97
+ return { transport: 'unix', directory, port };
98
+ }
99
+ function validateServerHandle(handle) {
100
+ if (typeof handle !== 'object' ||
101
+ handle === null ||
102
+ typeof handle.closed !== 'boolean' ||
103
+ typeof handle.connectionString !== 'string' ||
104
+ handle.connectionString.length === 0 ||
105
+ typeof handle.close !== 'function') {
106
+ throw new Error('Oliphaunt WASIX native addon returned an invalid server handle');
107
+ }
108
+ return handle.connectionString;
109
+ }
@@ -0,0 +1,60 @@
1
+ import { type NativeWasixAddon, type NativeWasixOpenOptions } from './native-addon.js';
2
+ import { type WasixDatabaseIdentity, type WasixDatabaseSession, type WasixDatabaseSessionTerminalState, type WasixPersistenceMode, type WasixProtocolStreamOutcome } from './database.js';
3
+ import type { SerializedOpenOptions } from './rpc.js';
4
+ import type { WasixStorageSyncBoundary } from './storage-provider.js';
5
+ import type { WasixPgDumpProcessOptions, WasixToolProcessOptions, WasixToolProcessResult } from './tool-runtime.js';
6
+ /** @internal A synchronous Rust Oliphaunt owned by the importing JavaScript realm. */
7
+ export declare class NativeWasixSession implements WasixDatabaseSession {
8
+ #private;
9
+ readonly identity: WasixDatabaseIdentity;
10
+ readonly terminalState: NativeHandleTerminalState;
11
+ private constructor();
12
+ static open(options: SerializedOpenOptions): Promise<NativeWasixSession>;
13
+ exec(input: Uint8Array, _persistence?: WasixPersistenceMode): Promise<Uint8Array>;
14
+ execStream(input: Uint8Array, onChunk: (chunk: Uint8Array) => void, _persistence?: WasixPersistenceMode): Promise<WasixProtocolStreamOutcome>;
15
+ sync(_boundary: WasixStorageSyncBoundary): Promise<void>;
16
+ backup(): Promise<Uint8Array>;
17
+ runPgDump(options: WasixPgDumpProcessOptions): Promise<WasixToolProcessResult>;
18
+ runTool(options: WasixToolProcessOptions): Promise<WasixToolProcessResult>;
19
+ close(): Promise<void>;
20
+ }
21
+ /** @internal A Rust-owner actor whose Promise completions return to the importing realm. */
22
+ export declare class NativeWasixActorSession implements WasixDatabaseSession {
23
+ #private;
24
+ readonly identity: WasixDatabaseIdentity;
25
+ readonly terminalState: NativeHandleTerminalState;
26
+ private constructor();
27
+ static open(options: SerializedOpenOptions): Promise<NativeWasixActorSession>;
28
+ exec(input: Uint8Array, _persistence?: WasixPersistenceMode): Promise<Uint8Array>;
29
+ execStream(input: Uint8Array, onChunk: (chunk: Uint8Array) => void, _persistence?: WasixPersistenceMode): Promise<WasixProtocolStreamOutcome>;
30
+ sync(_boundary: WasixStorageSyncBoundary): Promise<void>;
31
+ backup(): Promise<Uint8Array>;
32
+ runPgDump(options: WasixPgDumpProcessOptions): Promise<WasixToolProcessResult>;
33
+ runTool(options: WasixToolProcessOptions): Promise<WasixToolProcessResult>;
34
+ close(): Promise<void>;
35
+ }
36
+ /** @internal Stable observation of a native owner which can retire below TypeScript. */
37
+ declare class NativeHandleTerminalState implements WasixDatabaseSessionTerminalState {
38
+ #private;
39
+ constructor(isClosed: () => boolean, ownerLossMessage: string);
40
+ get terminal(): boolean;
41
+ get failure(): Error | undefined;
42
+ assertActive(): void;
43
+ beginOrderlyClose(): void;
44
+ recordFailure(error: unknown): void;
45
+ }
46
+ /** @internal Restore through the synchronous Rust archive validator. */
47
+ export declare function restoreNativeWasix(options: SerializedOpenOptions, bytes: Uint8Array): Promise<void>;
48
+ /** @internal Worker-side restore after the parent has validated package config. */
49
+ export declare function restoreNativeWasixStorage(storage: SerializedOpenOptions['storage'], bytes: Uint8Array, addon?: NativeWasixAddon): Promise<void>;
50
+ /** @internal Restore synchronously inside `/direct` or its dedicated Worker realm. */
51
+ export declare function restoreNativeWasixDirect(options: SerializedOpenOptions, bytes: Uint8Array): Promise<void>;
52
+ /** @internal Worker-side synchronous restore after the parent validates config. */
53
+ export declare function restoreNativeWasixStorageDirect(storage: SerializedOpenOptions['storage'], bytes: Uint8Array, addon?: NativeWasixAddon): Promise<void>;
54
+ /** @internal Validate serialized package descriptors against the selected addon. */
55
+ export declare function requireCompatibleNativeWasixAddon(options: SerializedOpenOptions): NativeWasixAddon;
56
+ /** @internal Project already-validated TS config onto the narrow native ABI. */
57
+ export declare function nativeWasixOpenOptions(options: SerializedOpenOptions, storage: NativeWasixOpenOptions['storage']): NativeWasixOpenOptions;
58
+ /** @internal Translate only the exact tagged native storage contract. */
59
+ export declare function mapNativeError(error: unknown): unknown;
60
+ export {};