@yydb/yydb-unknown-wasm32 0.0.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/README.md +27 -0
- package/package.json +43 -0
- package/src/index.ts +152 -0
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# `@yydb/yydb-unknown-wasm32`
|
|
2
|
+
|
|
3
|
+
Low-level wasm-pack artifacts for the YYDB browser core. **App authors should use
|
|
4
|
+
[`@yydb/yydb/wasm`](https://www.npmjs.com/package/@yydb/yydb)** instead of this package.
|
|
5
|
+
|
|
6
|
+
Import here only when you need the raw glue or bundled `.wasm` path. For durable reads and writes in production, prefer
|
|
7
|
+
[`@yydb/yydb-client`](https://www.npmjs.com/package/@yydb/yydb-client) against `yydb serve` until wasm persistence
|
|
8
|
+
lands.
|
|
9
|
+
|
|
10
|
+
## Example
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import {initWasm, checkSchema, yydbVersion} from "@yydb/yydb/wasm";
|
|
14
|
+
|
|
15
|
+
await initWasm();
|
|
16
|
+
|
|
17
|
+
console.log(yydbVersion());
|
|
18
|
+
console.log(
|
|
19
|
+
checkSchema("table Setting { @@id: uuid, key: utf8 }"),
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Call `initWasm()` once before semantic methods. Omit `options.module` to load the bundled `lib/yydb_wasm_bg.wasm` asset.
|
|
24
|
+
|
|
25
|
+
Low-level glue: `@yydb/yydb-unknown-wasm32/wasm` (raw `.wasm` path).
|
|
26
|
+
|
|
27
|
+
[YYDB overview](https://github.com/yy-database/yydb.rs)
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bugs": {
|
|
3
|
+
"url": "https://github.com/yy-database/yydb.rs/issues"
|
|
4
|
+
},
|
|
5
|
+
"description": "Browser WebAssembly binary for `@yydb/yydb`",
|
|
6
|
+
"devDependencies": {
|
|
7
|
+
"@types/node": "^20.10.0",
|
|
8
|
+
"typescript": "~5.3.0"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"default": "./src/index.ts",
|
|
13
|
+
"import": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts"
|
|
15
|
+
},
|
|
16
|
+
"./wasm": "./lib/yydb_wasm_bg.wasm"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"lib",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://github.com/yy-database/yydb.rs/tree/dev/projects/packages/yydb-unknown-wasm32#readme",
|
|
24
|
+
"license": "MPL-2.0",
|
|
25
|
+
"name": "@yydb/yydb-unknown-wasm32",
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"directory": "projects/packages/yydb-unknown-wasm32",
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/yy-database/yydb.rs.git"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "pnpm run typecheck",
|
|
36
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
37
|
+
},
|
|
38
|
+
"sideEffects": [
|
|
39
|
+
"./lib/yydb_wasm.js"
|
|
40
|
+
],
|
|
41
|
+
"type": "module",
|
|
42
|
+
"version": "0.0.0"
|
|
43
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import initGlue, * as glue from '../lib/yydb_wasm.js';
|
|
2
|
+
|
|
3
|
+
export type WasmInitInput = URL | Request | Response | ArrayBuffer | Uint8Array | WebAssembly.Module;
|
|
4
|
+
|
|
5
|
+
export type InitWasmOptions = {
|
|
6
|
+
/** When omitted, loads the bundled `lib/yydb_wasm_bg.wasm` asset. */
|
|
7
|
+
module?: WasmInitInput;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type CheckSchemaResult = {
|
|
11
|
+
ok: boolean;
|
|
12
|
+
tableCount: number;
|
|
13
|
+
schemaFingerprint: string;
|
|
14
|
+
engineVersion: string;
|
|
15
|
+
error?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type MicroHostInvokePayload = {
|
|
19
|
+
hostId: number;
|
|
20
|
+
handleVersion: number;
|
|
21
|
+
functionId: string;
|
|
22
|
+
args: readonly unknown[];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Stateful wasm session (memory or OPFS-backed). */
|
|
26
|
+
export type WasmSession = {
|
|
27
|
+
ensureSchema(document: string): string;
|
|
28
|
+
query(source: string): string;
|
|
29
|
+
execute(source: string): string;
|
|
30
|
+
get(key: string): string;
|
|
31
|
+
put(key: string, value: Uint8Array): string;
|
|
32
|
+
getSchema(): string;
|
|
33
|
+
info(): string;
|
|
34
|
+
setMicroHostInvoker(invoker: (payload: MicroHostInvokePayload) => unknown): void;
|
|
35
|
+
registerMicro(body: Uint8Array): string;
|
|
36
|
+
callScalar(body: Uint8Array): string;
|
|
37
|
+
close(): void;
|
|
38
|
+
readonly path?: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const glueApi = glue as {
|
|
42
|
+
yydbVersion?: () => string;
|
|
43
|
+
checkSchema?: (source: string) => CheckSchemaResult;
|
|
44
|
+
introspectSchema?: (source: string) => string;
|
|
45
|
+
queryMemory?: (source: string) => string;
|
|
46
|
+
MemorySession?: new () => WasmSession;
|
|
47
|
+
PersistentSession?: new (path: string) => WasmSession;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
let ready = false;
|
|
51
|
+
|
|
52
|
+
function assertReady(): void {
|
|
53
|
+
if (!ready) {
|
|
54
|
+
throw new Error('@yydb/yydb-unknown-wasm32: call initWasm() before semantic core methods');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function resolveDefaultWasmBytes(): Promise<ArrayBuffer | Uint8Array | URL> {
|
|
59
|
+
const wasmUrl = new URL('../lib/yydb_wasm_bg.wasm', import.meta.url);
|
|
60
|
+
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
61
|
+
const { readFileSync } = await import('node:fs');
|
|
62
|
+
const { fileURLToPath } = await import('node:url');
|
|
63
|
+
return readFileSync(fileURLToPath(wasmUrl));
|
|
64
|
+
}
|
|
65
|
+
return wasmUrl;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function normalizeInitInput(input: WasmInitInput | undefined): Promise<WasmInitInput | ArrayBuffer | Uint8Array> {
|
|
69
|
+
if (input === undefined) {
|
|
70
|
+
return resolveDefaultWasmBytes();
|
|
71
|
+
}
|
|
72
|
+
if (
|
|
73
|
+
input instanceof URL ||
|
|
74
|
+
input instanceof Request ||
|
|
75
|
+
input instanceof Response ||
|
|
76
|
+
input instanceof ArrayBuffer ||
|
|
77
|
+
input instanceof Uint8Array ||
|
|
78
|
+
input instanceof WebAssembly.Module
|
|
79
|
+
) {
|
|
80
|
+
return input;
|
|
81
|
+
}
|
|
82
|
+
throw new Error(`@yydb/yydb-unknown-wasm32: unsupported WASM init input (${typeof input})`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One-time WASM init. Required before semantic core methods. */
|
|
86
|
+
export async function initWasm(options: InitWasmOptions = {}): Promise<void> {
|
|
87
|
+
if (ready) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const moduleOrPath = await normalizeInitInput(options.module);
|
|
91
|
+
await initGlue({ module_or_path: moduleOrPath });
|
|
92
|
+
ready = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Library version (matches workspace `@yydb/yydb` semver). */
|
|
96
|
+
export function yydbVersion(): string {
|
|
97
|
+
assertReady();
|
|
98
|
+
if (!glueApi.yydbVersion) {
|
|
99
|
+
throw new Error('@yydb/yydb-unknown-wasm32: yydbVersion export missing, rebuild wasm artifacts');
|
|
100
|
+
}
|
|
101
|
+
return glueApi.yydbVersion();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Parse and validate a VOS schema document via the Rust core. */
|
|
105
|
+
export function checkSchema(source: string): CheckSchemaResult {
|
|
106
|
+
assertReady();
|
|
107
|
+
if (!glueApi.checkSchema) {
|
|
108
|
+
throw new Error('@yydb/yydb-unknown-wasm32: checkSchema export missing, rebuild wasm artifacts');
|
|
109
|
+
}
|
|
110
|
+
return glueApi.checkSchema(source);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Read-only schema introspection JSON. */
|
|
114
|
+
export function introspectSchema(source: string): string {
|
|
115
|
+
assertReady();
|
|
116
|
+
if (!glueApi.introspectSchema) {
|
|
117
|
+
throw new Error('@yydb/yydb-unknown-wasm32: introspectSchema export missing, rebuild wasm artifacts');
|
|
118
|
+
}
|
|
119
|
+
return glueApi.introspectSchema(source);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Execute a VOS query on a fresh in-memory database. Returns JSON `{ ok, rows, error }`. */
|
|
123
|
+
export function queryMemory(source: string): string {
|
|
124
|
+
assertReady();
|
|
125
|
+
if (!glueApi.queryMemory) {
|
|
126
|
+
throw new Error('@yydb/yydb-unknown-wasm32: queryMemory export missing, rebuild wasm artifacts');
|
|
127
|
+
}
|
|
128
|
+
return glueApi.queryMemory(source);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Open a stateful in-memory session. */
|
|
132
|
+
export function createMemorySession(): WasmSession {
|
|
133
|
+
assertReady();
|
|
134
|
+
if (!glueApi.MemorySession) {
|
|
135
|
+
throw new Error('@yydb/yydb-unknown-wasm32: MemorySession export missing, rebuild wasm artifacts');
|
|
136
|
+
}
|
|
137
|
+
return new glueApi.MemorySession();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Open a stateful OPFS-backed session at `path`. */
|
|
141
|
+
export function openPersistentSession(path: string): WasmSession {
|
|
142
|
+
assertReady();
|
|
143
|
+
if (!glueApi.PersistentSession) {
|
|
144
|
+
throw new Error('@yydb/yydb-unknown-wasm32: PersistentSession export missing, rebuild wasm artifacts');
|
|
145
|
+
}
|
|
146
|
+
return new glueApi.PersistentSession(path);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @internal Reset init gate (binding tests only). */
|
|
150
|
+
export function resetWasmBindingForTests(): void {
|
|
151
|
+
ready = false;
|
|
152
|
+
}
|