@yydb/yydb 0.0.11 → 0.0.15

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 (53) hide show
  1. package/README.md +86 -86
  2. package/dist/api/database.d.ts +127 -12
  3. package/dist/api/database.js +156 -14
  4. package/dist/cli/cli.js +28 -1
  5. package/dist/cli/info-cmd.js +30 -1
  6. package/dist/cli/verify-cmd.d.ts +11 -0
  7. package/dist/cli/verify-cmd.js +73 -0
  8. package/dist/database.d.ts +50 -0
  9. package/dist/database.js +181 -0
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/micro.d.ts +63 -0
  13. package/dist/micro.js +160 -0
  14. package/dist/native/binding.d.ts +28 -0
  15. package/dist/native.d.ts +9 -0
  16. package/dist/native.js +32 -0
  17. package/dist/node/cli/cli.d.ts +1 -0
  18. package/dist/node/cli/cli.js +25 -0
  19. package/dist/node/cli/info-cmd.d.ts +3 -0
  20. package/dist/node/cli/info-cmd.js +10 -0
  21. package/dist/node/cli/init-cmd.d.ts +3 -0
  22. package/dist/node/cli/init-cmd.js +18 -0
  23. package/dist/node/cli/options.d.ts +4 -0
  24. package/dist/node/cli/options.js +14 -0
  25. package/dist/node/cli/serve-cmd.d.ts +3 -0
  26. package/dist/node/cli/serve-cmd.js +14 -0
  27. package/dist/node/cli/version-cmd.d.ts +3 -0
  28. package/dist/node/cli/version-cmd.js +8 -0
  29. package/dist/node/cli.d.ts +2 -0
  30. package/dist/node/cli.js +6 -0
  31. package/dist/node/database.d.ts +50 -0
  32. package/dist/node/database.js +173 -0
  33. package/dist/node/index.d.ts +7 -0
  34. package/dist/node/index.js +5 -0
  35. package/dist/node/native.d.ts +9 -0
  36. package/dist/node/native.js +32 -0
  37. package/dist/node/resolve-bin.d.ts +15 -0
  38. package/dist/node/resolve-bin.js +62 -0
  39. package/dist/resolve-bin.d.ts +15 -0
  40. package/dist/resolve-bin.js +62 -0
  41. package/dist/shared/errors.d.ts +90 -15
  42. package/dist/shared/errors.js +112 -26
  43. package/dist/shared/wasm-envelope.d.ts +41 -0
  44. package/dist/shared/wasm-envelope.js +56 -0
  45. package/dist/udf-types.d.ts +26 -0
  46. package/dist/udf-types.js +16 -0
  47. package/dist/wasm/binding.d.ts +1 -0
  48. package/dist/wasm/binding.js +1 -0
  49. package/dist/wasm/database.d.ts +54 -0
  50. package/dist/wasm/database.js +118 -0
  51. package/dist/wasm/index.d.ts +8 -0
  52. package/dist/wasm/index.js +5 -0
  53. package/package.json +40 -40
@@ -2,12 +2,46 @@
2
2
  * Host-facing YYDB error surface.
3
3
  *
4
4
  * Every public failure carries a stable `code`: engine codes come from the
5
- * Rust core (`yydb.schema.invalid`, …) over N-API or the wire, client codes
6
- * are minted locally with the `yydb.client.` prefix. Branch on `code` —
7
- * `message` is display text only.
5
+ * Rust core (`yydb.schema.invalid`, …) over N-API or the wire; client codes
6
+ * are minted locally with the `yydb.client.` prefix. **Branch on `code`** —
7
+ * `message` is display text only and must never be used for retry decisions.
8
+ *
9
+ * ## Where codes live
10
+ *
11
+ * Engine codes live in Rust (`yydb_types::Error` variants → DiagnosticEnvelope).
12
+ * The TS side no longer keeps a hardcoded engine-code table — when Rust gains a
13
+ * new variant, hosts pick up the new code string via {@link fromDiagnosticJson}
14
+ * without any TS change. Client codes (`yydb.client.*`) are minted only on the
15
+ * host side and remain available as constants below.
16
+ */
17
+ /**
18
+ * Client-side error codes that never originate from Rust.
19
+ *
20
+ * Engine codes (`yydb.schema.*`, `yydb.udf.*`, `yydb.io`, …) are emitted by
21
+ * Rust and should be matched via string literals or from the diagnostic JSON
22
+ * envelope — they are intentionally not duplicated here.
23
+ */
24
+ export const YydbClientErrorCodes = {
25
+ /** serve() endpoint is empty or invalid. */
26
+ clientInvalidEndpoint: 'yydb.client.invalid_endpoint',
27
+ /** connect() socket / transport layer failure. */
28
+ clientTransportFailure: 'yydb.client.transport_failure',
29
+ /** Server replied with a request id we did not send. */
30
+ clientRequestIdMismatch: 'yydb.client.request_id_mismatch',
31
+ /** Server replied with a msg_type we did not request. */
32
+ clientUnexpectedResponse: 'yydb.client.unexpected_response',
33
+ /** Could not parse an Error frame body from the wire. */
34
+ clientDecodeFailure: 'yydb.client.decode_failure',
35
+ /** The YYDB session is already closed. */
36
+ sessionClosed: 'yydb.session.closed',
37
+ };
38
+ /**
39
+ * @deprecated Engine codes moved to Rust — use string literals or match on
40
+ * {@link YydbError.code} returned from {@link fromDiagnosticJson}. This table
41
+ * exists only for transition; new engine error variants will **not** be added here.
8
42
  */
9
- /** Stable codes known to the TypeScript hosts. */
10
43
  export const YydbErrorCodes = {
44
+ // Engine codes (deprecated)
11
45
  io: 'yydb.io',
12
46
  corrupt: 'yydb.corrupt',
13
47
  schemaConflict: 'yydb.schema.conflict',
@@ -25,41 +59,93 @@ export const YydbErrorCodes = {
25
59
  fencingMismatch: 'yydb.fencing.mismatch',
26
60
  leaseExpired: 'yydb.lease.expired',
27
61
  quotaExceeded: 'yydb.quota.exceeded',
28
- sessionClosed: 'yydb.session.closed',
29
- clientInvalidEndpoint: 'yydb.client.invalid_endpoint',
30
- clientTransportFailure: 'yydb.client.transport_failure',
31
- clientRequestIdMismatch: 'yydb.client.request_id_mismatch',
32
- clientUnexpectedResponse: 'yydb.client.unexpected_response',
33
- clientDecodeFailure: 'yydb.client.decode_failure',
62
+ // Client codes (still canonical for client-local errors)
63
+ ...YydbClientErrorCodes,
34
64
  };
35
- /**
36
- * Codes where retrying the same operation can legitimately succeed: a socket or
37
- * lease hiccup, or a compare-and-set that lost a benign race. Every other code
38
- * needs the caller to change input, schema, or wiring first.
39
- */
40
- const RETRYABLE_CODES = new Set([
41
- YydbErrorCodes.clientTransportFailure,
42
- YydbErrorCodes.casConflict,
43
- YydbErrorCodes.leaseUnavailable,
44
- ]);
45
- /** Whether `code` may be retried with the same arguments. */
46
- export function isRetryableYydbCode(code) {
47
- return RETRYABLE_CODES.has(code);
48
- }
49
65
  /** YYDB failure with a stable machine-readable {@link YydbError.code}. */
50
66
  export class YydbError extends Error {
51
- /** Stable code — engine code or `yydb.client.*`. */
67
+ /** Stable code — engine code (from Rust) or `yydb.client.*`. */
52
68
  code;
53
69
  /** Operation that failed. */
54
70
  operation;
55
71
  /** Whether the same call may be retried. */
56
72
  retryable;
73
+ /** Structured parameters for catalog templating (when present). */
74
+ params;
57
75
  constructor(code, message, options = {}) {
58
76
  super(message, { cause: options.cause });
59
77
  this.name = 'YydbError';
60
78
  this.code = code;
61
79
  this.operation = options.operation ?? 'unknown';
62
- this.retryable = isRetryableYydbCode(code);
80
+ this.retryable = options.retryable ?? false;
81
+ if (options.params !== undefined) {
82
+ this.params = options.params;
83
+ }
84
+ }
85
+ /**
86
+ * Parse the diagnostic JSON that N-API and WASM surfaces embed in their
87
+ * `Error.message`. Every boundary surface (N-API reason field, WASM JSON
88
+ * envelope, wire protocol) carries the same `{ code, operation, retryable,
89
+ * message, params? }` contract produced by `yydb::Error::to_json`.
90
+ *
91
+ * Returns the parsed {@link YydbError} if `value` is a diagnostic object,
92
+ * or `null` when the input is plain text. Hosts typically use this as a
93
+ * fallback in catch blocks:
94
+ *
95
+ * ```ts
96
+ * try {
97
+ * await db.put('key', value);
98
+ * } catch (err) {
99
+ * const parsed = YydbError.fromDiagnosticJson(
100
+ * err instanceof Error ? err.message : String(err),
101
+ * );
102
+ * if (parsed) {
103
+ * // branch on parsed.code / retryable / operation
104
+ * }
105
+ * }
106
+ * ```
107
+ */
108
+ static fromDiagnosticJson(value) {
109
+ let obj;
110
+ if (typeof value === 'string') {
111
+ try {
112
+ const parsed = JSON.parse(value);
113
+ if (!parsed || typeof parsed !== 'object')
114
+ return null;
115
+ obj = parsed;
116
+ }
117
+ catch {
118
+ return null;
119
+ }
120
+ }
121
+ else if (value && typeof value === 'object') {
122
+ obj = value;
123
+ }
124
+ else {
125
+ return null;
126
+ }
127
+ // WASM envelopes nest the diagnostic under `{ ok, error: {...} }`.
128
+ let diag = obj;
129
+ if (typeof obj['ok'] === 'boolean' && !obj['ok'] && obj['error'] && typeof obj['error'] === 'object') {
130
+ diag = obj['error'];
131
+ }
132
+ const code = typeof diag['code'] === 'string' ? diag['code'] : null;
133
+ const message = typeof diag['message'] === 'string' ? diag['message'] : '';
134
+ if (!code)
135
+ return null;
136
+ const operation = typeof diag['operation'] === 'string' ? diag['operation'] : undefined;
137
+ const retryable = typeof diag['retryable'] === 'boolean' ? diag['retryable'] : undefined;
138
+ const params = diag['params'] && typeof diag['params'] === 'object'
139
+ ? diag['params']
140
+ : undefined;
141
+ const opts = {};
142
+ if (operation !== undefined)
143
+ opts.operation = operation;
144
+ if (retryable !== undefined)
145
+ opts.retryable = retryable;
146
+ if (params !== undefined)
147
+ opts.params = params;
148
+ return new YydbError(code, message, opts);
63
149
  }
64
150
  }
65
151
  /** Narrowing helper so callers can branch without `instanceof` imports. */
@@ -0,0 +1,41 @@
1
+ /** Parsed `{ ok, rows, error, code }` envelope from wasm VOS calls. */
2
+ export type WasmRowsResult = {
3
+ ok: boolean;
4
+ rows: Record<string, unknown>[];
5
+ error: string | null;
6
+ code: string | null;
7
+ };
8
+ /** Parsed `{ ok, schema, error, code }` envelope from wasm `getSchema`. */
9
+ export type WasmSchemaResult = {
10
+ ok: boolean;
11
+ schema: {
12
+ version: number;
13
+ document: string;
14
+ } | null;
15
+ error: string | null;
16
+ code: string | null;
17
+ };
18
+ /** Parsed `{ ok, value, error, code }` envelope from wasm `get`. */
19
+ export type WasmKvGetResult = {
20
+ ok: boolean;
21
+ value: number[] | null;
22
+ error: string | null;
23
+ code: string | null;
24
+ };
25
+ /** Parsed `{ ok, value, error, code }` envelope from wasm `callScalar`. */
26
+ export type WasmScalarResult = {
27
+ ok: boolean;
28
+ value: string | number | boolean | null;
29
+ error: string | null;
30
+ code: string | null;
31
+ };
32
+ export type WireScalarValue = string | number | boolean | null;
33
+ export type SchemaVersion = {
34
+ version: number;
35
+ document: string;
36
+ };
37
+ export declare function parseWasmRowsResult(payload: string, operation: string): WasmRowsResult;
38
+ export declare function parseWasmUnitResult(payload: string, operation: string): void;
39
+ export declare function parseWasmSchemaResult(payload: string, operation: string): SchemaVersion | null;
40
+ export declare function parseWasmScalarResult(payload: string, operation: string): WireScalarValue;
41
+ export declare function parseWasmKvGetResult(payload: string, operation: string): Uint8Array | null;
@@ -0,0 +1,56 @@
1
+ import { YydbError, YydbErrorCodes } from '@yydb/yydb-client';
2
+ function parseJson(payload, operation) {
3
+ try {
4
+ return JSON.parse(payload);
5
+ }
6
+ catch (cause) {
7
+ throw new YydbError(YydbErrorCodes.clientDecodeFailure, `${operation}: invalid JSON envelope`, { operation, cause });
8
+ }
9
+ }
10
+ /**
11
+ * Build the error for a failed wasm envelope. The Rust core always sends a
12
+ * stable `code`; a missing one means the wasm artifact is stale, which is a
13
+ * decode failure rather than an engine failure.
14
+ */
15
+ function envelopeError(envelope, operation) {
16
+ const code = envelope.code ?? YydbErrorCodes.clientDecodeFailure;
17
+ const message = envelope.error ?? `${operation}: wasm envelope carried no message`;
18
+ return new YydbError(code, message, { operation });
19
+ }
20
+ export function parseWasmRowsResult(payload, operation) {
21
+ const parsed = parseJson(payload, operation);
22
+ if (!parsed.ok) {
23
+ throw envelopeError(parsed, operation);
24
+ }
25
+ return parsed;
26
+ }
27
+ export function parseWasmUnitResult(payload, operation) {
28
+ const parsed = parseJson(payload, operation);
29
+ if (!parsed.ok) {
30
+ throw envelopeError(parsed, operation);
31
+ }
32
+ }
33
+ export function parseWasmSchemaResult(payload, operation) {
34
+ const parsed = parseJson(payload, operation);
35
+ if (!parsed.ok) {
36
+ throw envelopeError(parsed, operation);
37
+ }
38
+ return parsed.schema;
39
+ }
40
+ export function parseWasmScalarResult(payload, operation) {
41
+ const parsed = parseJson(payload, operation);
42
+ if (!parsed.ok) {
43
+ throw envelopeError(parsed, operation);
44
+ }
45
+ return parsed.value;
46
+ }
47
+ export function parseWasmKvGetResult(payload, operation) {
48
+ const parsed = parseJson(payload, operation);
49
+ if (!parsed.ok) {
50
+ throw envelopeError(parsed, operation);
51
+ }
52
+ if (parsed.value === null) {
53
+ return null;
54
+ }
55
+ return Uint8Array.from(parsed.value);
56
+ }
@@ -0,0 +1,26 @@
1
+ /** Phase-1 scalar UDF wire kinds aligned with `yydb-udf`. */
2
+ export type UdfScalarKind = 'i64' | 'bool' | 'text' | 'null';
3
+ /** Runtime type descriptor used in `defineMicro` contracts. */
4
+ export interface UdfTypeDescriptor<T = unknown> {
5
+ readonly kind: UdfScalarKind;
6
+ /** Phantom type for author-side inference only. */
7
+ readonly _type?: T;
8
+ }
9
+ /** `i64` argument or return descriptor. */
10
+ export declare function i64(): UdfTypeDescriptor<number>;
11
+ /** `bool` argument or return descriptor. */
12
+ export declare function bool(): UdfTypeDescriptor<boolean>;
13
+ /** `text` argument or return descriptor. */
14
+ export declare function text(): UdfTypeDescriptor<string>;
15
+ /** `null` return descriptor. */
16
+ export declare function nullType(): UdfTypeDescriptor<null>;
17
+ /** Maps descriptor tuple types to positional argument types. */
18
+ export type InferUdfArgs<T extends readonly UdfTypeDescriptor[]> = {
19
+ [K in keyof T]: T[K] extends UdfTypeDescriptor<infer V> ? V : never;
20
+ };
21
+ /** Maps a return descriptor to its value type. */
22
+ export type InferUdfReturn<T extends UdfTypeDescriptor> = T extends UdfTypeDescriptor<infer V> ? V : never;
23
+ /** Session micro effect surface (Phase 1: pure only). */
24
+ export type MicroEffect = 'pure';
25
+ /** Invocation mode stored with the definition for future batch host calls. */
26
+ export type MicroInvocationMode = 'scalar' | 'batch';
@@ -0,0 +1,16 @@
1
+ /** `i64` argument or return descriptor. */
2
+ export function i64() {
3
+ return { kind: 'i64' };
4
+ }
5
+ /** `bool` argument or return descriptor. */
6
+ export function bool() {
7
+ return { kind: 'bool' };
8
+ }
9
+ /** `text` argument or return descriptor. */
10
+ export function text() {
11
+ return { kind: 'text' };
12
+ }
13
+ /** `null` return descriptor. */
14
+ export function nullType() {
15
+ return { kind: 'null' };
16
+ }
@@ -0,0 +1 @@
1
+ export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, type CheckSchemaResult, type InitWasmOptions, type WasmInitInput, type WasmSession, } from '@yydb/yydb-unknown-wasm32';
@@ -0,0 +1 @@
1
+ export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, } from '@yydb/yydb-unknown-wasm32';
@@ -0,0 +1,54 @@
1
+ import { defineMicro as buildMicro, type DefinedMicro, type MicroHandle } from '../shared/micro.js';
2
+ import type { UdfScalarDescriptor } from '../shared/udf-types.js';
3
+ import { type SchemaVersion } from '../shared/wasm-envelope.js';
4
+ import { type CheckSchemaResult, type InitWasmOptions } from './binding.js';
5
+ export type WasmOpenOptions = InitWasmOptions;
6
+ /**
7
+ * Browser-facing YYDB handle backed by an in-process wasm session.
8
+ * Use `open` for OPFS-backed paths and `openInMemory` for ephemeral state.
9
+ */
10
+ export declare class Database {
11
+ readonly path: string;
12
+ private session;
13
+ private readonly kind;
14
+ private readonly microRegistry;
15
+ private constructor();
16
+ /** Open an OPFS-backed database at `path` (for example `app.yydb`). */
17
+ static open(path: string, options?: WasmOpenOptions): Promise<Database>;
18
+ /** Open a stateful in-memory database session. */
19
+ static openInMemory(options?: WasmOpenOptions): Promise<Database>;
20
+ /** Library version from the wasm binding. */
21
+ version(): string;
22
+ /** Engine version string (Node API parity). */
23
+ serverVersion(): Promise<string>;
24
+ /** Database info text (`path=…\\n…`), matching the YY wire `Info` reply. */
25
+ info(): Promise<string>;
26
+ /** Parse and validate a VOS schema document without opening a session. */
27
+ checkSchema(source: string): CheckSchemaResult;
28
+ /** Persist schema document. `version` is accepted for Node API parity; wasm uses the document body. */
29
+ ensureSchema(_version: number, document: string): Promise<void>;
30
+ getSchema(): Promise<SchemaVersion | null>;
31
+ get(key: string): Promise<Uint8Array | null>;
32
+ put(key: string, value: Uint8Array | string): Promise<void>;
33
+ /** VOS read pipeline (for example `User.filter(x => x.active).collect()`). */
34
+ query(source: string): Record<string, unknown>[];
35
+ /** Unit-valued VOS write programs (for example `User { … }.insert()`). */
36
+ execute(source: string): void;
37
+ /**
38
+ * Build a typed session-local micro definition. Does not register it with
39
+ * the engine until `registerMicro` is called.
40
+ */
41
+ defineMicro<TArgs extends readonly UdfScalarDescriptor[], TReturn extends UdfScalarDescriptor>(definition: Parameters<typeof buildMicro<TArgs, TReturn>>[0]): DefinedMicro<TArgs, TReturn>;
42
+ /**
43
+ * Install a `defineMicro` definition into the current session registry and
44
+ * sync metadata to the embedded engine. Host micros are session-local.
45
+ */
46
+ registerMicro<TArgs extends readonly UdfScalarDescriptor[], TReturn extends UdfScalarDescriptor>(definition: DefinedMicro<TArgs, TReturn>): Promise<MicroHandle>;
47
+ /** Invoke a registered scalar UDF on the embedded engine. */
48
+ callScalar(name: string, version: number, args: readonly (string | number | boolean | null)[]): Promise<string | number | boolean | null>;
49
+ /** Whether this handle owns a persistent OPFS session. */
50
+ isPersistent(): boolean;
51
+ /** Close the embedded session. Idempotent; further calls fail with `yydb.session.closed`. */
52
+ close(): Promise<void>;
53
+ private requireSession;
54
+ }
@@ -0,0 +1,118 @@
1
+ import { encodeMicroRegister, encodeScalarCall, YydbError, YydbErrorCodes } from '@yydb/yydb-client';
2
+ import { defineMicro as buildMicro, microFingerprint, udfWireKind, MicroSessionRegistry } from '../shared/micro.js';
3
+ import { parseWasmKvGetResult, parseWasmRowsResult, parseWasmScalarResult, parseWasmSchemaResult, parseWasmUnitResult, } from '../shared/wasm-envelope.js';
4
+ import { checkSchema, createMemorySession, initWasm, openPersistentSession, yydbVersion, } from './binding.js';
5
+ /**
6
+ * Browser-facing YYDB handle backed by an in-process wasm session.
7
+ * Use `open` for OPFS-backed paths and `openInMemory` for ephemeral state.
8
+ */
9
+ export class Database {
10
+ path;
11
+ session;
12
+ kind;
13
+ microRegistry;
14
+ constructor(kind, path, session) {
15
+ this.kind = kind;
16
+ this.path = path;
17
+ this.session = session;
18
+ this.microRegistry = new MicroSessionRegistry();
19
+ session.setMicroHostInvoker((payload) => this.microRegistry.invokeFromWire(payload));
20
+ }
21
+ /** Open an OPFS-backed database at `path` (for example `app.yydb`). */
22
+ static async open(path, options = {}) {
23
+ await initWasm(options);
24
+ const session = openPersistentSession(path);
25
+ return new Database('persistent', session.path ?? path, session);
26
+ }
27
+ /** Open a stateful in-memory database session. */
28
+ static async openInMemory(options = {}) {
29
+ await initWasm(options);
30
+ const session = createMemorySession();
31
+ return new Database('memory', ':memory:', session);
32
+ }
33
+ /** Library version from the wasm binding. */
34
+ version() {
35
+ return yydbVersion();
36
+ }
37
+ /** Engine version string (Node API parity). */
38
+ async serverVersion() {
39
+ return yydbVersion();
40
+ }
41
+ /** Database info text (`path=…\\n…`), matching the YY wire `Info` reply. */
42
+ async info() {
43
+ return this.requireSession('info').info();
44
+ }
45
+ /** Parse and validate a VOS schema document without opening a session. */
46
+ checkSchema(source) {
47
+ return checkSchema(source);
48
+ }
49
+ /** Persist schema document. `version` is accepted for Node API parity; wasm uses the document body. */
50
+ async ensureSchema(_version, document) {
51
+ parseWasmUnitResult(this.requireSession('schemaEnsure').ensureSchema(document), 'schemaEnsure');
52
+ }
53
+ async getSchema() {
54
+ return parseWasmSchemaResult(this.requireSession('schemaGet').getSchema(), 'schemaGet');
55
+ }
56
+ async get(key) {
57
+ return parseWasmKvGetResult(this.requireSession('kvGet').get(key), 'kvGet');
58
+ }
59
+ async put(key, value) {
60
+ const bytes = typeof value === 'string' ? new TextEncoder().encode(value) : value;
61
+ parseWasmUnitResult(this.requireSession('kvPut').put(key, bytes), 'kvPut');
62
+ }
63
+ /** VOS read pipeline (for example `User.filter(x => x.active).collect()`). */
64
+ query(source) {
65
+ return parseWasmRowsResult(this.requireSession('query').query(source), 'query').rows;
66
+ }
67
+ /** Unit-valued VOS write programs (for example `User { … }.insert()`). */
68
+ execute(source) {
69
+ parseWasmUnitResult(this.requireSession('execute').execute(source), 'execute');
70
+ }
71
+ /**
72
+ * Build a typed session-local micro definition. Does not register it with
73
+ * the engine until `registerMicro` is called.
74
+ */
75
+ defineMicro(definition) {
76
+ return buildMicro(definition);
77
+ }
78
+ /**
79
+ * Install a `defineMicro` definition into the current session registry and
80
+ * sync metadata to the embedded engine. Host micros are session-local.
81
+ */
82
+ async registerMicro(definition) {
83
+ const handle = this.microRegistry.register(definition);
84
+ const body = encodeMicroRegister({
85
+ hostId: handle.hostId,
86
+ handleVersion: handle.implementationVersion,
87
+ udfVersion: definition.version,
88
+ name: definition.name,
89
+ functionId: handle.functionId,
90
+ args: definition.args.map(udfWireKind),
91
+ returns: udfWireKind(definition.returns),
92
+ fingerprint: microFingerprint(definition.name, definition.version),
93
+ });
94
+ parseWasmUnitResult(this.requireSession('microRegister').registerMicro(body), 'microRegister');
95
+ return handle;
96
+ }
97
+ /** Invoke a registered scalar UDF on the embedded engine. */
98
+ async callScalar(name, version, args) {
99
+ const body = encodeScalarCall(name, version, args);
100
+ return parseWasmScalarResult(this.requireSession('callScalar').callScalar(body), 'callScalar');
101
+ }
102
+ /** Whether this handle owns a persistent OPFS session. */
103
+ isPersistent() {
104
+ return this.kind === 'persistent';
105
+ }
106
+ /** Close the embedded session. Idempotent; further calls fail with `yydb.session.closed`. */
107
+ async close() {
108
+ const session = this.session;
109
+ this.session = null;
110
+ session?.close();
111
+ }
112
+ requireSession(operation) {
113
+ if (!this.session) {
114
+ throw new YydbError(YydbErrorCodes.sessionClosed, `${operation}: @yydb/yydb/wasm session is closed`, { operation });
115
+ }
116
+ return this.session;
117
+ }
118
+ }
@@ -0,0 +1,8 @@
1
+ export { Database, type WasmOpenOptions } from './database.js';
2
+ export { YydbError, YydbErrorCodes, isRetryableYydbCode, isYydbError } from '@yydb/yydb-client';
3
+ export type { YydbErrorCode, YydbErrorOptions } from '@yydb/yydb-client';
4
+ export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, type CheckSchemaResult, type InitWasmOptions, type WasmInitInput, type WasmSession, } from './binding.js';
5
+ export { defineMicro, type DefinedMicro, type MicroHandle, type TsMicroDefinitionInput, MicroSessionRegistry } from '../shared/micro.js';
6
+ export { type InferUdfArgs, type InferUdfReturn, type MicroEffect, type MicroInvocationMode, type UdfScalarDescriptor, type UdfScalarKind, } from '../shared/udf-types.js';
7
+ export type { SchemaVersion } from '../shared/wasm-envelope.js';
8
+ export * from '../shared/vos.js';
@@ -0,0 +1,5 @@
1
+ export { Database } from './database.js';
2
+ export { YydbError, YydbErrorCodes, isRetryableYydbCode, isYydbError } from '@yydb/yydb-client';
3
+ export { checkSchema, createMemorySession, initWasm, introspectSchema, openPersistentSession, queryMemory, resetWasmBindingForTests, yydbVersion, } from './binding.js';
4
+ export { defineMicro, MicroSessionRegistry } from '../shared/micro.js';
5
+ export * from '../shared/vos.js';
package/package.json CHANGED
@@ -1,41 +1,38 @@
1
1
  {
2
- "name": "@yydb/yydb",
3
- "version": "0.0.11",
4
- "description": "YYDB Node embedded engine and CLI",
5
- "license": "MPL-2.0",
6
- "type": "module",
7
2
  "bin": {
8
3
  "yydb": "./dist/cli.js"
9
4
  },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
5
+ "bugs": {
6
+ "url": "https://github.com/yy-database/yydb.rs/issues"
7
+ },
8
+ "dependencies": {
9
+ "@vmz/commander": "^0.3.5",
10
+ "@vmz/diagnostic": "^0.3.5",
11
+ "@voml/vos": "^0.0.1"
12
+ },
13
+ "description": "YYDB Node embedded engine and CLI",
14
+ "devDependencies": {
15
+ "@types/node": "^20.10.0",
16
+ "typescript": "~5.3.0"
17
+ },
18
+ "engines": {
19
+ "node": ">=18"
20
+ },
12
21
  "exports": {
13
22
  ".": {
14
- "types": "./dist/index.d.ts",
15
- "import": "./dist/index.js"
23
+ "import": "./dist/index.js",
24
+ "types": "./dist/index.d.ts"
16
25
  },
17
26
  "./cli": {
18
- "types": "./dist/cli.d.ts",
19
- "import": "./dist/cli.js"
27
+ "import": "./dist/cli.js",
28
+ "types": "./dist/cli.d.ts"
20
29
  }
21
30
  },
22
31
  "files": [
23
32
  "dist",
24
33
  "README.md"
25
34
  ],
26
- "scripts": {
27
- "build": "tsc -p tsconfig.json",
28
- "test": "pnpm run build && node --experimental-strip-types --test src/**/*.test.ts"
29
- },
30
- "repository": {
31
- "type": "git",
32
- "url": "git+https://github.com/yy-database/yydb.git",
33
- "directory": "projects/packages/yydb"
34
- },
35
- "homepage": "https://github.com/yy-database/yydb/tree/dev/projects/packages/yydb#readme",
36
- "bugs": {
37
- "url": "https://github.com/yy-database/yydb/issues"
38
- },
35
+ "homepage": "https://github.com/yy-database/yydb.rs/tree/dev/projects/packages/yydb#readme",
39
36
  "keywords": [
40
37
  "yydb",
41
38
  "vos",
@@ -43,25 +40,28 @@
43
40
  "embedded",
44
41
  "typescript"
45
42
  ],
43
+ "license": "MPL-2.0",
44
+ "main": "./dist/index.js",
45
+ "name": "@yydb/yydb",
46
+ "optionalDependencies": {
47
+ "@yydb/yydb-darwin-arm64": "0.0.15",
48
+ "@yydb/yydb-darwin-x64": "0.0.15",
49
+ "@yydb/yydb-linux-x64": "0.0.15",
50
+ "@yydb/yydb-win32-x64": "0.0.15"
51
+ },
46
52
  "publishConfig": {
47
53
  "access": "public"
48
54
  },
49
- "dependencies": {
50
- "@vmz/commander": "^0.3.5",
51
- "@vmz/diagnostic": "^0.3.5",
52
- "@voml/vos": "^0.0.1"
53
- },
54
- "devDependencies": {
55
- "@types/node": "^20.10.0",
56
- "typescript": "~5.3.0"
55
+ "repository": {
56
+ "directory": "projects/packages/yydb",
57
+ "type": "git",
58
+ "url": "git+https://github.com/yy-database/yydb.rs.git"
57
59
  },
58
- "engines": {
59
- "node": ">=18"
60
+ "scripts": {
61
+ "build": "tsc -p tsconfig.json",
62
+ "test": "pnpm run build && node --experimental-strip-types --test src/**/*.test.ts"
60
63
  },
61
- "optionalDependencies": {
62
- "@yydb/yydb-darwin-arm64": "0.0.11",
63
- "@yydb/yydb-darwin-x64": "0.0.11",
64
- "@yydb/yydb-linux-x64": "0.0.11",
65
- "@yydb/yydb-win32-x64": "0.0.11"
66
- }
64
+ "type": "module",
65
+ "types": "./dist/index.d.ts",
66
+ "version": "0.0.15"
67
67
  }