@rivetkit/rivetkit-native 0.0.0-pr.4667.da01c89 → 0.0.0-pr.4669.7c31bd8

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 (4) hide show
  1. package/index.d.ts +56 -114
  2. package/package.json +9 -9
  3. package/wrapper.d.ts +18 -18
  4. package/wrapper.js +143 -27
package/index.d.ts CHANGED
@@ -4,54 +4,50 @@
4
4
  /* auto-generated by NAPI-RS */
5
5
 
6
6
  export interface JsBindParam {
7
- kind: string;
8
- intValue?: number;
9
- floatValue?: number;
10
- textValue?: string;
11
- blobValue?: Buffer;
7
+ kind: string
8
+ intValue?: number
9
+ floatValue?: number
10
+ textValue?: string
11
+ blobValue?: Buffer
12
12
  }
13
13
  export interface ExecuteResult {
14
- changes: number;
14
+ changes: number
15
15
  }
16
16
  export interface QueryResult {
17
- columns: Array<string>;
18
- rows: Array<Array<any>>;
17
+ columns: Array<string>
18
+ rows: Array<Array<any>>
19
19
  }
20
20
  /** Open a native SQLite database backed by the envoy's KV channel. */
21
- export declare function openDatabaseFromEnvoy(
22
- jsHandle: JsEnvoyHandle,
23
- actorId: string,
24
- preloadedEntries?: Array<JsKvEntry> | undefined | null,
25
- ): Promise<JsNativeDatabase>;
21
+ export declare function openDatabaseFromEnvoy(jsHandle: JsEnvoyHandle, actorId: string): Promise<JsNativeDatabase>
26
22
  /** Configuration for starting the native envoy client. */
27
23
  export interface JsEnvoyConfig {
28
- endpoint: string;
29
- token: string;
30
- namespace: string;
31
- poolName: string;
32
- version: number;
33
- metadata?: any;
34
- notGlobal: boolean;
35
- /**
36
- * Log level for the Rust tracing subscriber (e.g. "trace", "debug", "info", "warn", "error").
37
- * Falls back to RIVET_LOG_LEVEL, then LOG_LEVEL, then RUST_LOG env vars. Defaults to "warn".
38
- */
39
- logLevel?: string;
24
+ endpoint: string
25
+ token: string
26
+ namespace: string
27
+ poolName: string
28
+ version: number
29
+ metadata?: any
30
+ notGlobal: boolean
31
+ /**
32
+ * Log level for the Rust tracing subscriber (e.g. "trace", "debug", "info", "warn", "error").
33
+ * Falls back to RIVET_LOG_LEVEL, then LOG_LEVEL, then RUST_LOG env vars. Defaults to "warn".
34
+ */
35
+ logLevel?: string
40
36
  }
41
37
  /** Options for KV list operations. */
42
38
  export interface JsKvListOptions {
43
- reverse?: boolean;
44
- limit?: number;
39
+ reverse?: boolean
40
+ limit?: number
45
41
  }
46
42
  /** A key-value entry returned from KV list operations. */
47
43
  export interface JsKvEntry {
48
- key: Buffer;
49
- value: Buffer;
44
+ key: Buffer
45
+ value: Buffer
50
46
  }
51
47
  /** A single hibernating request entry. */
52
48
  export interface HibernatingRequestEntry {
53
- gatewayId: Buffer;
54
- requestId: Buffer;
49
+ gatewayId: Buffer
50
+ requestId: Buffer
55
51
  }
56
52
  /**
57
53
  * Start the native envoy client synchronously.
@@ -59,93 +55,39 @@ export interface HibernatingRequestEntry {
59
55
  * Returns a handle immediately. The caller must call `await handle.started()`
60
56
  * to wait for the connection to be ready.
61
57
  */
62
- export declare function startEnvoySyncJs(
63
- config: JsEnvoyConfig,
64
- eventCallback: (event: any) => void,
65
- ): JsEnvoyHandle;
58
+ export declare function startEnvoySyncJs(config: JsEnvoyConfig, eventCallback: (event: any) => void): JsEnvoyHandle
66
59
  /** Start the native envoy client asynchronously. */
67
- export declare function startEnvoyJs(
68
- config: JsEnvoyConfig,
69
- eventCallback: (event: any) => void,
70
- ): JsEnvoyHandle;
60
+ export declare function startEnvoyJs(config: JsEnvoyConfig, eventCallback: (event: any) => void): JsEnvoyHandle
71
61
  /** Native SQLite database handle exposed to JavaScript. */
72
62
  export declare class JsNativeDatabase {
73
- takeLastKvError(): string | null;
74
- run(
75
- sql: string,
76
- params?: Array<JsBindParam> | undefined | null,
77
- ): Promise<ExecuteResult>;
78
- query(
79
- sql: string,
80
- params?: Array<JsBindParam> | undefined | null,
81
- ): Promise<QueryResult>;
82
- exec(sql: string): Promise<QueryResult>;
83
- close(): Promise<void>;
63
+ run(sql: string, params?: Array<JsBindParam> | undefined | null): Promise<ExecuteResult>
64
+ query(sql: string, params?: Array<JsBindParam> | undefined | null): Promise<QueryResult>
65
+ exec(sql: string): Promise<QueryResult>
66
+ close(): Promise<void>
84
67
  }
85
68
  /** Native envoy handle exposed to JavaScript via N-API. */
86
69
  export declare class JsEnvoyHandle {
87
- started(): Promise<void>;
88
- shutdown(immediate: boolean): void;
89
- get envoyKey(): string;
90
- sleepActor(actorId: string, generation?: number | undefined | null): void;
91
- stopActor(
92
- actorId: string,
93
- generation?: number | undefined | null,
94
- error?: string | undefined | null,
95
- ): void;
96
- destroyActor(actorId: string, generation?: number | undefined | null): void;
97
- setAlarm(
98
- actorId: string,
99
- alarmTs?: number | undefined | null,
100
- generation?: number | undefined | null,
101
- ): void;
102
- kvGet(
103
- actorId: string,
104
- keys: Array<Buffer>,
105
- ): Promise<Array<Buffer | undefined | null>>;
106
- kvPut(actorId: string, entries: Array<JsKvEntry>): Promise<void>;
107
- kvDelete(actorId: string, keys: Array<Buffer>): Promise<void>;
108
- kvDeleteRange(actorId: string, start: Buffer, end: Buffer): Promise<void>;
109
- kvListAll(
110
- actorId: string,
111
- options?: JsKvListOptions | undefined | null,
112
- ): Promise<Array<JsKvEntry>>;
113
- kvListRange(
114
- actorId: string,
115
- start: Buffer,
116
- end: Buffer,
117
- exclusive?: boolean | undefined | null,
118
- options?: JsKvListOptions | undefined | null,
119
- ): Promise<Array<JsKvEntry>>;
120
- kvListPrefix(
121
- actorId: string,
122
- prefix: Buffer,
123
- options?: JsKvListOptions | undefined | null,
124
- ): Promise<Array<JsKvEntry>>;
125
- kvDrop(actorId: string): Promise<void>;
126
- restoreHibernatingRequests(
127
- actorId: string,
128
- requests: Array<HibernatingRequestEntry>,
129
- ): void;
130
- sendHibernatableWebSocketMessageAck(
131
- gatewayId: Buffer,
132
- requestId: Buffer,
133
- clientMessageIndex: number,
134
- ): void;
135
- /** Send a message on an open WebSocket connection identified by messageIdHex. */
136
- sendWsMessage(
137
- gatewayId: Buffer,
138
- requestId: Buffer,
139
- data: Buffer,
140
- binary: boolean,
141
- ): Promise<void>;
142
- /** Close an open WebSocket connection. */
143
- closeWebsocket(
144
- gatewayId: Buffer,
145
- requestId: Buffer,
146
- code?: number | undefined | null,
147
- reason?: string | undefined | null,
148
- ): Promise<void>;
149
- startServerless(payload: Buffer): Promise<void>;
150
- respondCallback(responseId: string, data: any): Promise<void>;
70
+ started(): Promise<void>
71
+ shutdown(immediate: boolean): void
72
+ get envoyKey(): string
73
+ sleepActor(actorId: string, generation?: number | undefined | null): void
74
+ stopActor(actorId: string, generation?: number | undefined | null, error?: string | undefined | null): void
75
+ destroyActor(actorId: string, generation?: number | undefined | null): void
76
+ setAlarm(actorId: string, alarmTs?: number | undefined | null, generation?: number | undefined | null): void
77
+ kvGet(actorId: string, keys: Array<Buffer>): Promise<Array<Buffer | undefined | null>>
78
+ kvPut(actorId: string, entries: Array<JsKvEntry>): Promise<void>
79
+ kvDelete(actorId: string, keys: Array<Buffer>): Promise<void>
80
+ kvDeleteRange(actorId: string, start: Buffer, end: Buffer): Promise<void>
81
+ kvListAll(actorId: string, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
82
+ kvListRange(actorId: string, start: Buffer, end: Buffer, exclusive?: boolean | undefined | null, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
83
+ kvListPrefix(actorId: string, prefix: Buffer, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
84
+ kvDrop(actorId: string): Promise<void>
85
+ restoreHibernatingRequests(actorId: string, requests: Array<HibernatingRequestEntry>): void
86
+ sendHibernatableWebSocketMessageAck(gatewayId: Buffer, requestId: Buffer, clientMessageIndex: number): void
87
+ /** Send a message on an open WebSocket connection identified by messageIdHex. */
88
+ sendWsMessage(gatewayId: Buffer, requestId: Buffer, data: Buffer, binary: boolean): Promise<void>
89
+ /** Close an open WebSocket connection. */
90
+ closeWebsocket(gatewayId: Buffer, requestId: Buffer, code?: number | undefined | null, reason?: string | undefined | null): Promise<void>
91
+ startServerless(payload: Buffer): Promise<void>
92
+ respondCallback(responseId: string, data: any): Promise<void>
151
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivetkit/rivetkit-native",
3
- "version": "0.0.0-pr.4667.da01c89",
3
+ "version": "0.0.0-pr.4669.7c31bd8",
4
4
  "description": "Native N-API addon for RivetKit providing envoy client and SQLite access",
5
5
  "license": "Apache-2.0",
6
6
  "main": "index.js",
@@ -49,15 +49,15 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "@napi-rs/cli": "^2.18.4",
52
- "@rivetkit/engine-envoy-protocol": "0.0.0-pr.4667.da01c89"
52
+ "@rivetkit/engine-envoy-protocol": "0.0.0-pr.4669.7c31bd8"
53
53
  },
54
54
  "optionalDependencies": {
55
- "@rivetkit/rivetkit-native-darwin-arm64": "0.0.0-pr.4667.da01c89",
56
- "@rivetkit/rivetkit-native-darwin-x64": "0.0.0-pr.4667.da01c89",
57
- "@rivetkit/rivetkit-native-linux-arm64-gnu": "0.0.0-pr.4667.da01c89",
58
- "@rivetkit/rivetkit-native-linux-arm64-musl": "0.0.0-pr.4667.da01c89",
59
- "@rivetkit/rivetkit-native-linux-x64-gnu": "0.0.0-pr.4667.da01c89",
60
- "@rivetkit/rivetkit-native-linux-x64-musl": "0.0.0-pr.4667.da01c89",
61
- "@rivetkit/rivetkit-native-win32-x64-msvc": "0.0.0-pr.4667.da01c89"
55
+ "@rivetkit/rivetkit-native-darwin-arm64": "0.0.0-pr.4669.7c31bd8",
56
+ "@rivetkit/rivetkit-native-darwin-x64": "0.0.0-pr.4669.7c31bd8",
57
+ "@rivetkit/rivetkit-native-linux-arm64-gnu": "0.0.0-pr.4669.7c31bd8",
58
+ "@rivetkit/rivetkit-native-linux-arm64-musl": "0.0.0-pr.4669.7c31bd8",
59
+ "@rivetkit/rivetkit-native-linux-x64-gnu": "0.0.0-pr.4669.7c31bd8",
60
+ "@rivetkit/rivetkit-native-linux-x64-musl": "0.0.0-pr.4669.7c31bd8",
61
+ "@rivetkit/rivetkit-native-win32-x64-msvc": "0.0.0-pr.4669.7c31bd8"
62
62
  }
63
63
  }
package/wrapper.d.ts CHANGED
@@ -29,16 +29,9 @@ export interface EnvoyHandle {
29
29
  sleepActor(actorId: string, generation?: number): void;
30
30
  stopActor(actorId: string, generation?: number, error?: string): void;
31
31
  destroyActor(actorId: string, generation?: number): void;
32
- setAlarm(
33
- actorId: string,
34
- alarmTs: number | null,
35
- generation?: number,
36
- ): void;
32
+ setAlarm(actorId: string, alarmTs: number | null, generation?: number): void;
37
33
  kvGet(actorId: string, keys: Uint8Array[]): Promise<(Uint8Array | null)[]>;
38
- kvListAll(
39
- actorId: string,
40
- options?: KvListOptions,
41
- ): Promise<[Uint8Array, Uint8Array][]>;
34
+ kvListAll(actorId: string, options?: KvListOptions): Promise<[Uint8Array, Uint8Array][]>;
42
35
  kvListRange(
43
36
  actorId: string,
44
37
  start: Uint8Array,
@@ -53,11 +46,7 @@ export interface EnvoyHandle {
53
46
  ): Promise<[Uint8Array, Uint8Array][]>;
54
47
  kvPut(actorId: string, entries: [Uint8Array, Uint8Array][]): Promise<void>;
55
48
  kvDelete(actorId: string, keys: Uint8Array[]): Promise<void>;
56
- kvDeleteRange(
57
- actorId: string,
58
- start: Uint8Array,
59
- end: Uint8Array,
60
- ): Promise<void>;
49
+ kvDeleteRange(actorId: string, start: Uint8Array, end: Uint8Array): Promise<void>;
61
50
  kvDrop(actorId: string): Promise<void>;
62
51
  restoreHibernatingRequests(
63
52
  actorId: string,
@@ -116,9 +105,7 @@ export interface EnvoyConfig {
116
105
  actorId: string,
117
106
  generation: number,
118
107
  config: import("@rivetkit/engine-envoy-protocol").ActorConfig,
119
- preloadedKv:
120
- | import("@rivetkit/engine-envoy-protocol").PreloadedKv
121
- | null,
108
+ preloadedKv: import("@rivetkit/engine-envoy-protocol").PreloadedKv | null,
122
109
  ) => Promise<void>;
123
110
  onActorStop: (
124
111
  envoyHandle: EnvoyHandle,
@@ -139,6 +126,19 @@ export declare function startEnvoy(config: EnvoyConfig): Promise<EnvoyHandle>;
139
126
  export declare function openDatabaseFromEnvoy(
140
127
  handle: EnvoyHandle,
141
128
  actorId: string,
142
- preloadedEntries?: readonly [Uint8Array, Uint8Array][] | null,
143
129
  ): Promise<JsNativeDatabase>;
130
+
131
+ export interface NativeRawDatabase {
132
+ execute: <TRow extends Record<string, unknown> = Record<string, unknown>>(
133
+ query: string,
134
+ ...args: unknown[]
135
+ ) => Promise<TRow[]>;
136
+ close: () => Promise<void>;
137
+ }
138
+
139
+ export declare function openRawDatabaseFromEnvoy(
140
+ handle: EnvoyHandle,
141
+ actorId: string,
142
+ ): Promise<NativeRawDatabase>;
143
+
144
144
  export declare const utils: {};
package/wrapper.js CHANGED
@@ -134,7 +134,7 @@ function startEnvoySync(config) {
134
134
  poolName: config.poolName,
135
135
  version: config.version,
136
136
  metadata: config.metadata || null,
137
- notGlobal: config.notGlobal ?? false,
137
+ notGlobal: config.notGlobal,
138
138
  },
139
139
  (event) => {
140
140
  handleEvent(event, config, wrappedHandle);
@@ -158,39 +158,154 @@ async function startEnvoy(config) {
158
158
  /**
159
159
  * Open a native database backed by envoy KV.
160
160
  */
161
- async function openDatabaseFromEnvoy(handle, actorId, preloadedEntries) {
161
+ async function openDatabaseFromEnvoy(handle, actorId) {
162
162
  const rawHandle = handle._raw || handle;
163
- const nativePreloadedEntries = preloadedEntries
164
- ? preloadedEntries.map(([key, value]) => ({
165
- key: Buffer.from(key),
166
- value: Buffer.from(value),
167
- }))
168
- : null;
169
- return native.openDatabaseFromEnvoy(
170
- rawHandle,
171
- actorId,
172
- nativePreloadedEntries,
163
+ return native.openDatabaseFromEnvoy(rawHandle, actorId);
164
+ }
165
+
166
+ function isPlainObject(value) {
167
+ return (
168
+ !!value &&
169
+ typeof value === "object" &&
170
+ !Array.isArray(value) &&
171
+ Object.getPrototypeOf(value) === Object.prototype
173
172
  );
174
173
  }
175
174
 
176
- function decodePreloadedKv(preloadedKv) {
177
- if (!preloadedKv) {
178
- return null;
175
+ function toNativeBinding(value) {
176
+ if (value === null || value === undefined) {
177
+ return { kind: "null" };
178
+ }
179
+ if (typeof value === "bigint") {
180
+ return { kind: "int", intValue: Number(value) };
181
+ }
182
+ if (typeof value === "number") {
183
+ return Number.isInteger(value)
184
+ ? { kind: "int", intValue: value }
185
+ : { kind: "float", floatValue: value };
186
+ }
187
+ if (typeof value === "string") {
188
+ return { kind: "text", textValue: value };
189
+ }
190
+ if (value instanceof ArrayBuffer) {
191
+ return { kind: "blob", blobValue: Buffer.from(value) };
179
192
  }
193
+ if (ArrayBuffer.isView(value)) {
194
+ return {
195
+ kind: "blob",
196
+ blobValue: Buffer.from(value.buffer, value.byteOffset, value.byteLength),
197
+ };
198
+ }
199
+
200
+ throw new Error(`unsupported sqlite binding type: ${typeof value}`);
201
+ }
180
202
 
181
- const decodeBytes = (value) => Uint8Array.from(Buffer.from(value, "base64"));
203
+ function extractNamedSqliteParameters(sql) {
204
+ return [...sql.matchAll(/([:@$][A-Za-z_][A-Za-z0-9_]*)/g)].map(
205
+ (match) => match[1],
206
+ );
207
+ }
208
+
209
+ function getNamedSqliteBinding(bindings, name) {
210
+ if (name in bindings) {
211
+ return bindings[name];
212
+ }
213
+
214
+ const bareName = name.slice(1);
215
+ if (bareName in bindings) {
216
+ return bindings[bareName];
217
+ }
218
+
219
+ for (const prefix of [":", "@", "$"]) {
220
+ const candidate = `${prefix}${bareName}`;
221
+ if (candidate in bindings) {
222
+ return bindings[candidate];
223
+ }
224
+ }
225
+
226
+ return undefined;
227
+ }
228
+
229
+ function normalizeBindings(sql, args) {
230
+ if (!args || args.length === 0) {
231
+ return [];
232
+ }
233
+
234
+ if (
235
+ args.length === 1 &&
236
+ isPlainObject(args[0]) &&
237
+ !(args[0] instanceof Uint8Array)
238
+ ) {
239
+ const names = extractNamedSqliteParameters(sql);
240
+ if (names.length === 0) {
241
+ throw new Error(
242
+ "native sqlite object bindings require named placeholders in the SQL statement",
243
+ );
244
+ }
245
+ return names.map((name) => {
246
+ const value = getNamedSqliteBinding(args[0], name);
247
+ if (value === undefined) {
248
+ throw new Error(`missing bind parameter: ${name}`);
249
+ }
250
+ return toNativeBinding(value);
251
+ });
252
+ }
253
+
254
+ return args.map(toNativeBinding);
255
+ }
256
+
257
+ function mapRows(rows, columns) {
258
+ return rows.map((row) => {
259
+ const rowObject = {};
260
+ for (let i = 0; i < columns.length; i++) {
261
+ rowObject[columns[i]] = row[i];
262
+ }
263
+ return rowObject;
264
+ });
265
+ }
266
+
267
+ async function openRawDatabaseFromEnvoy(handle, actorId) {
268
+ const nativeDb = await openDatabaseFromEnvoy(handle, actorId);
269
+ let closed = false;
270
+
271
+ const ensureOpen = () => {
272
+ if (closed) {
273
+ throw new Error("database is closed");
274
+ }
275
+ };
182
276
 
183
277
  return {
184
- entries: (preloadedKv.entries || []).map((entry) => ({
185
- key: decodeBytes(entry.key),
186
- value: decodeBytes(entry.value),
187
- metadata: {
188
- version: decodeBytes(entry.metadata.version),
189
- updateTs: entry.metadata.updateTs,
190
- },
191
- })),
192
- requestedGetKeys: (preloadedKv.requestedGetKeys || []).map(decodeBytes),
193
- requestedPrefixes: (preloadedKv.requestedPrefixes || []).map(decodeBytes),
278
+ execute: async (query, ...args) => {
279
+ ensureOpen();
280
+
281
+ if (args.length > 0) {
282
+ const bindings = normalizeBindings(query, args);
283
+ const token = query.trimStart().slice(0, 16).toUpperCase();
284
+ const returnsRows =
285
+ token.startsWith("SELECT") ||
286
+ token.startsWith("PRAGMA") ||
287
+ token.startsWith("WITH") ||
288
+ /\bRETURNING\b/i.test(query);
289
+
290
+ if (returnsRows) {
291
+ const result = await nativeDb.query(query, bindings);
292
+ return mapRows(result.rows, result.columns);
293
+ }
294
+
295
+ await nativeDb.run(query, bindings);
296
+ return [];
297
+ }
298
+
299
+ const result = await nativeDb.exec(query);
300
+ return mapRows(result.rows, result.columns);
301
+ },
302
+ close: async () => {
303
+ if (closed) {
304
+ return;
305
+ }
306
+ closed = true;
307
+ await nativeDb.close();
308
+ },
194
309
  };
195
310
  }
196
311
 
@@ -215,7 +330,7 @@ function handleEvent(event, config, wrappedHandle) {
215
330
  event.actorId,
216
331
  event.generation,
217
332
  actorConfig,
218
- decodePreloadedKv(event.preloadedKv),
333
+ null, // preloadedKv
219
334
  ),
220
335
  ).then(
221
336
  async () => {
@@ -459,3 +574,4 @@ function handleEvent(event, config, wrappedHandle) {
459
574
  module.exports.startEnvoy = startEnvoy;
460
575
  module.exports.startEnvoySync = startEnvoySync;
461
576
  module.exports.openDatabaseFromEnvoy = openDatabaseFromEnvoy;
577
+ module.exports.openRawDatabaseFromEnvoy = openRawDatabaseFromEnvoy;