rivetkit 0.0.0-sqlite-query-metrics-v2.f5d3605 → 0.0.0-sqlite-sync-3-0-1-release.95f6e2d

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 (139) hide show
  1. package/dist/browser/client.d.ts +99 -59
  2. package/dist/browser/client.js +228 -34
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +1 -2
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/{chunk-3URZ4UV5.js → chunk-2HWY7FZU.js} +2 -2
  7. package/dist/tsup/{chunk-PJ5EXY6F.js → chunk-62WFU3BQ.js} +223 -42
  8. package/dist/tsup/chunk-62WFU3BQ.js.map +1 -0
  9. package/dist/tsup/{chunk-JI6GZ2C2.js → chunk-A2CF6PFH.js} +18 -1
  10. package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
  11. package/dist/tsup/{chunk-KDT56V3Y.cjs → chunk-AQWZWR2P.cjs} +10 -11
  12. package/dist/tsup/chunk-AQWZWR2P.cjs.map +1 -0
  13. package/dist/tsup/{chunk-L3Z3W44Q.cjs → chunk-E4KDQJCR.cjs} +3 -3
  14. package/dist/tsup/{chunk-L3Z3W44Q.cjs.map → chunk-E4KDQJCR.cjs.map} +1 -1
  15. package/dist/tsup/{chunk-DHL2M2E2.cjs → chunk-E6HKVCPA.cjs} +3 -3
  16. package/dist/tsup/{chunk-DHL2M2E2.cjs.map → chunk-E6HKVCPA.cjs.map} +1 -1
  17. package/dist/tsup/{chunk-DDS7OOVF.cjs → chunk-EQBUH25O.cjs} +362 -181
  18. package/dist/tsup/chunk-EQBUH25O.cjs.map +1 -0
  19. package/dist/tsup/{chunk-USKGKQZM.js → chunk-HQHTTJW5.js} +10 -11
  20. package/dist/tsup/chunk-HQHTTJW5.js.map +1 -0
  21. package/dist/tsup/{chunk-JW2WPE6Z.cjs → chunk-M5SQGPRJ.cjs} +4 -4
  22. package/dist/tsup/{chunk-JW2WPE6Z.cjs.map → chunk-M5SQGPRJ.cjs.map} +1 -1
  23. package/dist/tsup/{chunk-FYDB45YU.cjs → chunk-MYWNJP55.cjs} +10 -10
  24. package/dist/tsup/{chunk-FYDB45YU.cjs.map → chunk-MYWNJP55.cjs.map} +1 -1
  25. package/dist/tsup/{chunk-F7IGTKGM.js → chunk-OWZ76NSH.js} +2 -2
  26. package/dist/tsup/{chunk-KSP3NXGY.js → chunk-QE5MTZHB.js} +3 -3
  27. package/dist/tsup/{chunk-D4G35VTO.js → chunk-QFZY2CK3.js} +4 -4
  28. package/dist/tsup/{chunk-E2CUROR3.js → chunk-QIIIRYN7.js} +2 -2
  29. package/dist/tsup/{chunk-MYWZCDML.cjs → chunk-RTWH3AX3.cjs} +19 -2
  30. package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
  31. package/dist/tsup/{chunk-HO76AEDH.cjs → chunk-UHMB5FFB.cjs} +3 -3
  32. package/dist/tsup/{chunk-HO76AEDH.cjs.map → chunk-UHMB5FFB.cjs.map} +1 -1
  33. package/dist/tsup/{chunk-QR6EAWEC.cjs → chunk-VKXEBB3Z.cjs} +7 -7
  34. package/dist/tsup/{chunk-QR6EAWEC.cjs.map → chunk-VKXEBB3Z.cjs.map} +1 -1
  35. package/dist/tsup/{chunk-EPRA2TCT.js → chunk-ZMVYZ7ZI.js} +2 -2
  36. package/dist/tsup/client/mod.cjs +6 -6
  37. package/dist/tsup/client/mod.d.cts +15 -3
  38. package/dist/tsup/client/mod.d.ts +15 -3
  39. package/dist/tsup/client/mod.js +5 -5
  40. package/dist/tsup/common/log.cjs +2 -2
  41. package/dist/tsup/common/log.js +1 -1
  42. package/dist/tsup/common/websocket.cjs +3 -3
  43. package/dist/tsup/common/websocket.js +2 -2
  44. package/dist/tsup/{config-Cw_BwH_G.d.ts → config-B4erdavw.d.ts} +58 -12
  45. package/dist/tsup/{config-D4clT6mq.d.ts → config-BfG1qpYz.d.cts} +34 -52
  46. package/dist/tsup/{config-D4clT6mq.d.cts → config-BfG1qpYz.d.ts} +34 -52
  47. package/dist/tsup/{config-CkmVg2fG.d.cts → config-DK144OAw.d.cts} +58 -12
  48. package/dist/tsup/{context-_xIcgBnE.d.cts → context-Cvnf3BRe.d.cts} +2 -2
  49. package/dist/tsup/{context-BYOKTQJi.d.ts → context-Oxi6YWcG.d.ts} +2 -2
  50. package/dist/tsup/db/drizzle.cjs +2 -2
  51. package/dist/tsup/db/drizzle.d.cts +1 -1
  52. package/dist/tsup/db/drizzle.d.ts +1 -1
  53. package/dist/tsup/db/drizzle.js +1 -1
  54. package/dist/tsup/db/mod.cjs +3 -3
  55. package/dist/tsup/db/mod.d.cts +2 -2
  56. package/dist/tsup/db/mod.d.ts +2 -2
  57. package/dist/tsup/db/mod.js +2 -2
  58. package/dist/tsup/dynamic/mod.d.cts +2 -2
  59. package/dist/tsup/dynamic/mod.d.ts +2 -2
  60. package/dist/tsup/inspector/mod.cjs +5 -5
  61. package/dist/tsup/inspector/mod.js +4 -4
  62. package/dist/tsup/inspector/workflow.cjs +4 -4
  63. package/dist/tsup/inspector/workflow.js +3 -3
  64. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  65. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  66. package/dist/tsup/mod.cjs +553 -202
  67. package/dist/tsup/mod.cjs.map +1 -1
  68. package/dist/tsup/mod.d.cts +4 -4
  69. package/dist/tsup/mod.d.ts +4 -4
  70. package/dist/tsup/mod.js +467 -116
  71. package/dist/tsup/mod.js.map +1 -1
  72. package/dist/tsup/test/mod.cjs +7 -7
  73. package/dist/tsup/test/mod.d.cts +2 -2
  74. package/dist/tsup/test/mod.d.ts +2 -2
  75. package/dist/tsup/test/mod.js +5 -5
  76. package/dist/tsup/unstable/migrations.d.cts +1 -1
  77. package/dist/tsup/unstable/migrations.d.ts +1 -1
  78. package/dist/tsup/utils.cjs +2 -2
  79. package/dist/tsup/utils.js +1 -1
  80. package/dist/tsup/workflow/mod.cjs +22 -19
  81. package/dist/tsup/workflow/mod.cjs.map +1 -1
  82. package/dist/tsup/workflow/mod.d.cts +4 -4
  83. package/dist/tsup/workflow/mod.d.ts +4 -4
  84. package/dist/tsup/workflow/mod.js +10 -7
  85. package/dist/tsup/workflow/mod.js.map +1 -1
  86. package/package.json +9 -10
  87. package/src/actor/keys.ts +86 -0
  88. package/src/client/actor-conn.ts +136 -24
  89. package/src/client/actor-handle.ts +21 -3
  90. package/src/client/client.ts +6 -0
  91. package/src/client/config.ts +12 -1
  92. package/src/client/lifecycle-errors.ts +35 -2
  93. package/src/client/retry-budget.ts +78 -0
  94. package/src/client/utils.ts +13 -2
  95. package/src/common/database/config.ts +46 -52
  96. package/src/common/database/native-database.ts +247 -27
  97. package/src/common/router.ts +1 -1
  98. package/src/db/mod.ts +0 -1
  99. package/src/engine-api/actors.ts +3 -3
  100. package/src/engine-client/actor-websocket-client.ts +4 -2
  101. package/src/engine-client/api-endpoints.ts +44 -14
  102. package/src/engine-client/mod.ts +2 -2
  103. package/src/registry/config/envoy.ts +1 -1
  104. package/src/registry/config/index.ts +29 -31
  105. package/src/registry/index.ts +66 -21
  106. package/src/registry/napi-runtime.ts +82 -3
  107. package/src/registry/native.ts +184 -52
  108. package/src/registry/runtime.ts +79 -10
  109. package/src/registry/wasm-runtime.ts +94 -3
  110. package/src/utils/env-vars.ts +15 -10
  111. package/src/workflow/driver.ts +5 -3
  112. package/dist/tsup/chunk-DDS7OOVF.cjs.map +0 -1
  113. package/dist/tsup/chunk-JI6GZ2C2.js.map +0 -1
  114. package/dist/tsup/chunk-KDT56V3Y.cjs.map +0 -1
  115. package/dist/tsup/chunk-MYWZCDML.cjs.map +0 -1
  116. package/dist/tsup/chunk-PJ5EXY6F.js.map +0 -1
  117. package/dist/tsup/chunk-USKGKQZM.js.map +0 -1
  118. package/src/actor/config.test.ts +0 -78
  119. package/src/actor/keys.test.ts +0 -275
  120. package/src/actor/utils.test.ts +0 -48
  121. package/src/client/test.ts +0 -44
  122. package/src/client/token-provider.test.ts +0 -86
  123. package/src/common/database/mod.test.ts +0 -364
  124. package/src/common/database/native-database.test.ts +0 -220
  125. package/src/common/inline-websocket-adapter.test.ts +0 -51
  126. package/src/db/drizzle.test.ts +0 -164
  127. package/src/registry/config/index.test.ts +0 -25
  128. package/src/registry/run-handler-coordinator.test.ts +0 -252
  129. package/src/registry/runtime.test.ts +0 -124
  130. package/src/unstable/migrations.test.ts +0 -170
  131. package/src/utils/endpoint-parser.test.ts +0 -206
  132. package/src/utils.test.ts +0 -34
  133. package/src/workflow/driver.test.ts +0 -62
  134. /package/dist/tsup/{chunk-3URZ4UV5.js.map → chunk-2HWY7FZU.js.map} +0 -0
  135. /package/dist/tsup/{chunk-F7IGTKGM.js.map → chunk-OWZ76NSH.js.map} +0 -0
  136. /package/dist/tsup/{chunk-KSP3NXGY.js.map → chunk-QE5MTZHB.js.map} +0 -0
  137. /package/dist/tsup/{chunk-D4G35VTO.js.map → chunk-QFZY2CK3.js.map} +0 -0
  138. /package/dist/tsup/{chunk-E2CUROR3.js.map → chunk-QIIIRYN7.js.map} +0 -0
  139. /package/dist/tsup/{chunk-EPRA2TCT.js.map → chunk-ZMVYZ7ZI.js.map} +0 -0
@@ -1,86 +0,0 @@
1
- import { describe, expect, test, vi } from "vitest";
2
- import {
3
- isInvalidToken,
4
- isInvalidTokenResponse,
5
- TokenProvider,
6
- } from "./token-provider";
7
-
8
- describe("renewable client tokens", () => {
9
- test("single-flights initial issuance and concurrent rejection refreshes", async () => {
10
- const getToken = vi.fn(
11
- async ({ forceRefresh }: { forceRefresh: boolean }) =>
12
- forceRefresh ? "renewed" : "initial",
13
- );
14
- const provider = new TokenProvider(getToken);
15
- expect(
16
- await Promise.all([provider.current(), provider.current()]),
17
- ).toEqual(["initial", "initial"]);
18
- expect(getToken).toHaveBeenCalledTimes(1);
19
- expect(
20
- await Promise.all([
21
- provider.refreshIfCurrent("initial"),
22
- provider.refreshIfCurrent("initial"),
23
- ]),
24
- ).toEqual(["renewed", "renewed"]);
25
- expect(getToken).toHaveBeenCalledTimes(2);
26
- expect(await provider.refreshIfCurrent("initial")).toBe("renewed");
27
- expect(getToken).toHaveBeenCalledTimes(2);
28
- });
29
-
30
- test("retries failed issuance and refuses empty credentials", async () => {
31
- const getToken = vi
32
- .fn()
33
- .mockRejectedValueOnce(new Error("issuer down"))
34
- .mockResolvedValueOnce("ok");
35
- const provider = new TokenProvider(getToken);
36
- await expect(provider.current()).rejects.toThrow("issuer down");
37
- expect(await provider.current()).toBe("ok");
38
- await expect(
39
- new TokenProvider(async () => "").current(),
40
- ).rejects.toThrow("nonempty");
41
- });
42
-
43
- test("refreshes before a known JWT expiry and treats opaque tokens as unexpiring hints", async () => {
44
- const payload = btoa(
45
- JSON.stringify({ exp: Math.floor(Date.now() / 1_000) + 1 }),
46
- ).replaceAll("=", "");
47
- const getToken = vi.fn(async () => `header.${payload}.signature`);
48
- const provider = new TokenProvider(getToken);
49
- await provider.current();
50
- await provider.current();
51
- expect(getToken).toHaveBeenCalledTimes(2);
52
- const opaque = vi.fn(async () => "opaque");
53
- const opaqueProvider = new TokenProvider(opaque);
54
- await opaqueProvider.current();
55
- await opaqueProvider.current();
56
- expect(opaque).toHaveBeenCalledTimes(1);
57
- });
58
-
59
- test("only confirmed 401 invalid/expired errors permit renewal", () => {
60
- expect(isInvalidToken("auth", "invalid_token")).toBe(true);
61
- expect(isInvalidToken("auth", "token_expired")).toBe(true);
62
- for (const code of [
63
- "insufficient_permissions",
64
- "verification_unavailable",
65
- "issuance_unavailable",
66
- ]) {
67
- expect(isInvalidToken("auth", code)).toBe(false);
68
- }
69
- expect(
70
- isInvalidTokenResponse(
71
- new Response(null, {
72
- status: 401,
73
- headers: { "x-rivet-error": "auth.token_expired" },
74
- }),
75
- ),
76
- ).toBe(true);
77
- expect(
78
- isInvalidTokenResponse(
79
- new Response(null, {
80
- status: 503,
81
- headers: { "x-rivet-error": "auth.token_expired" },
82
- }),
83
- ),
84
- ).toBe(false);
85
- });
86
- });
@@ -1,364 +0,0 @@
1
- import { pino } from "pino";
2
- import { beforeEach, describe, expect, test } from "vitest";
3
- import { configureBaseLogger } from "@/common/log";
4
- import type {
5
- DatabaseProviderContext,
6
- SqliteBindings,
7
- SqliteDatabase,
8
- SqliteExecuteResult,
9
- SqliteTransactionDatabase,
10
- } from "./config";
11
- import { db, registerNativeStateTransactionOpener } from "./mod";
12
-
13
- let logLines: string[];
14
-
15
- class FakeSqliteDatabase implements SqliteDatabase {
16
- failSql = new Map<string, Error>();
17
- executeCalls: { sql: string; params?: SqliteBindings }[] = [];
18
- stateTransactionTimeouts: Array<number | undefined> = [];
19
- transactionTimeouts: Array<number | undefined> = [];
20
- transactionNames: Array<string | undefined> = [];
21
-
22
- async exec(): Promise<void> {}
23
-
24
- async execute(
25
- sql: string,
26
- params?: SqliteBindings,
27
- ): Promise<SqliteExecuteResult> {
28
- this.record(sql, params);
29
- return emptyResult();
30
- }
31
-
32
- async beginTransaction(
33
- timeoutMs?: number,
34
- name?: string,
35
- ): Promise<SqliteTransactionDatabase> {
36
- this.transactionTimeouts.push(timeoutMs);
37
- this.transactionNames.push(name);
38
- this.record("BEGIN");
39
- return {
40
- exec: async () => {},
41
- execute: async (sql, params) => {
42
- this.record(sql, params);
43
- return emptyResult();
44
- },
45
- commit: async () => this.record("COMMIT"),
46
- rollback: async () => this.record("ROLLBACK"),
47
- };
48
- }
49
- async beginStateTransaction(
50
- timeoutMs?: number,
51
- ): Promise<SqliteTransactionDatabase> {
52
- this.stateTransactionTimeouts.push(timeoutMs);
53
- this.record("BEGIN_STATE");
54
- return {
55
- exec: async () => {},
56
- execute: async (sql, params) => {
57
- this.record(sql, params);
58
- return emptyResult();
59
- },
60
- commit: async () => this.record("COMMIT"),
61
- rollback: async () => this.record("ROLLBACK"),
62
- };
63
- }
64
-
65
- async executeBatch(
66
- statements: Array<{ sql: string; params?: SqliteBindings }>,
67
- ): Promise<SqliteExecuteResult[]> {
68
- const results: SqliteExecuteResult[] = [];
69
- for (const statement of statements) {
70
- results.push(await this.execute(statement.sql, statement.params));
71
- }
72
- return results;
73
- }
74
-
75
- async run(sql: string, params?: SqliteBindings): Promise<void> {
76
- await this.execute(sql, params);
77
- }
78
-
79
- async query(sql: string, params?: SqliteBindings) {
80
- const { columns, rows } = await this.execute(sql, params);
81
- return { columns, rows };
82
- }
83
-
84
- async close(): Promise<void> {}
85
-
86
- private record(sql: string, params?: SqliteBindings): void {
87
- this.executeCalls.push({ sql, params });
88
- const error = this.failSql.get(sql);
89
- if (error) throw error;
90
- }
91
- }
92
-
93
- function emptyResult(): SqliteExecuteResult {
94
- return {
95
- columns: [],
96
- rows: [],
97
- changes: 0,
98
- lastInsertRowId: null,
99
- };
100
- }
101
-
102
- function testProviderContext(
103
- database: FakeSqliteDatabase,
104
- includeStateTransactions = false,
105
- ): DatabaseProviderContext {
106
- return {
107
- actorId: "actor-a",
108
- kv: {
109
- batchPut: async () => {},
110
- batchGet: async (keys) => keys.map(() => null),
111
- batchDelete: async () => {},
112
- deleteRange: async () => {},
113
- },
114
- nativeDatabaseProvider: includeStateTransactions
115
- ? registerNativeStateTransactionOpener(
116
- { open: async () => database },
117
- async (timeoutMs?: number) =>
118
- await database.beginStateTransaction(timeoutMs),
119
- )
120
- : { open: async () => database },
121
- };
122
- }
123
-
124
- describe("db", () => {
125
- beforeEach(() => {
126
- logLines = [];
127
- configureBaseLogger(
128
- pino(
129
- { level: "warn", base: {}, timestamp: false },
130
- { write: (line: string) => logLines.push(line) },
131
- ),
132
- );
133
- });
134
-
135
- test("runs onMigrate through the shared transaction handle", async () => {
136
- const nativeDb = new FakeSqliteDatabase();
137
- const provider = db({
138
- onMigrate: async (client) => {
139
- await client.execute(
140
- "CREATE TABLE items(id INTEGER PRIMARY KEY)",
141
- );
142
- },
143
- });
144
- const client = await provider.createClient(
145
- testProviderContext(nativeDb),
146
- );
147
- await provider.onMigrate(client);
148
-
149
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
150
- "BEGIN",
151
- "SAVEPOINT __rivet_on_migrate",
152
- "CREATE TABLE items(id INTEGER PRIMARY KEY)",
153
- "RELEASE SAVEPOINT __rivet_on_migrate",
154
- "COMMIT",
155
- ]);
156
- expect(nativeDb.transactionTimeouts).toEqual([300_000]);
157
- expect(nativeDb.transactionNames).toEqual(["rivetkit-migration"]);
158
- });
159
-
160
- test("exposes profiling controls to the actor runtime", () => {
161
- const provider = db({
162
- profiling: {
163
- enabled: false,
164
- maxTrackedStatementFingerprints: 12,
165
- baselineSampleRate: 0.25,
166
- },
167
- });
168
-
169
- expect(provider.sqliteProfiling).toEqual({
170
- enabled: false,
171
- maxTrackedStatementFingerprints: 12,
172
- baselineSampleRate: 0.25,
173
- });
174
- });
175
-
176
- test("rolls back migrations when onMigrate fails", async () => {
177
- const nativeDb = new FakeSqliteDatabase();
178
- const provider = db({
179
- onMigrate: async () => {
180
- throw new Error("migration failed");
181
- },
182
- });
183
- const client = await provider.createClient(
184
- testProviderContext(nativeDb),
185
- );
186
- await expect(provider.onMigrate(client)).rejects.toThrow(
187
- "migration failed",
188
- );
189
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
190
- "BEGIN",
191
- "SAVEPOINT __rivet_on_migrate",
192
- "ROLLBACK TO SAVEPOINT __rivet_on_migrate",
193
- "RELEASE SAVEPOINT __rivet_on_migrate",
194
- "ROLLBACK",
195
- ]);
196
- });
197
-
198
- test("commits transaction work and forwards timeout", async () => {
199
- const nativeDb = new FakeSqliteDatabase();
200
- const client = await db().createClient(testProviderContext(nativeDb));
201
- const value = await client.transaction(
202
- async (tx) => {
203
- await tx.execute(
204
- "INSERT INTO items(value) VALUES (?)",
205
- "inside",
206
- );
207
- return 42;
208
- },
209
- { name: "insert-item", timeout: 120_000 },
210
- );
211
- expect(value).toBe(42);
212
- expect(nativeDb.transactionTimeouts).toEqual([120_000]);
213
- expect(nativeDb.transactionNames).toEqual(["insert-item"]);
214
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
215
- "BEGIN",
216
- "INSERT INTO items(value) VALUES (?)",
217
- "COMMIT",
218
- ]);
219
- });
220
-
221
- test("validates transaction names", async () => {
222
- const client = await db().createClient(
223
- testProviderContext(new FakeSqliteDatabase()),
224
- );
225
- await expect(
226
- client.transaction(async () => {}, { name: "" }),
227
- ).rejects.toThrow("must not be empty");
228
- });
229
-
230
- test("defers the configured transaction name byte limit to the runtime", async () => {
231
- const nativeDb = new FakeSqliteDatabase();
232
- const client = await db().createClient(testProviderContext(nativeDb));
233
- const name = "x".repeat(129);
234
-
235
- await client.transaction(async () => {}, { name });
236
-
237
- expect(nativeDb.transactionNames).toEqual([name]);
238
- });
239
-
240
- test("rolls back a transaction when the callback throws", async () => {
241
- const nativeDb = new FakeSqliteDatabase();
242
- const client = await db().createClient(testProviderContext(nativeDb));
243
- await expect(
244
- client.transaction(async (tx) => {
245
- await tx.execute(
246
- "INSERT INTO items(value) VALUES (?)",
247
- "inside",
248
- );
249
- throw new Error("callback failed");
250
- }),
251
- ).rejects.toThrow("callback failed");
252
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
253
- "BEGIN",
254
- "INSERT INTO items(value) VALUES (?)",
255
- "ROLLBACK",
256
- ]);
257
- });
258
-
259
- test("uses the state-aware transaction bridge when explicitly requested", async () => {
260
- const nativeDb = new FakeSqliteDatabase();
261
- const client = await db().createClient(
262
- testProviderContext(nativeDb, true),
263
- );
264
-
265
- await client.transaction(
266
- async (tx) => {
267
- await tx.execute(
268
- "INSERT INTO items(value) VALUES (?)",
269
- "inside",
270
- );
271
- },
272
- {
273
- timeout: 120_000,
274
- experimental: { includeState: true },
275
- },
276
- );
277
-
278
- expect(nativeDb.transactionTimeouts).toEqual([]);
279
- expect(nativeDb.stateTransactionTimeouts).toEqual([120_000]);
280
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
281
- "BEGIN_STATE",
282
- "INSERT INTO items(value) VALUES (?)",
283
- "COMMIT",
284
- ]);
285
- });
286
-
287
- test("rolls back state-aware transactions when the callback throws", async () => {
288
- const nativeDb = new FakeSqliteDatabase();
289
- const client = await db().createClient(
290
- testProviderContext(nativeDb, true),
291
- );
292
-
293
- await expect(
294
- client.transaction(
295
- async (tx) => {
296
- await tx.execute(
297
- "INSERT INTO items(value) VALUES (?)",
298
- "inside",
299
- );
300
- throw new Error("callback failed");
301
- },
302
- { experimental: { includeState: true } },
303
- ),
304
- ).rejects.toThrow("callback failed");
305
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
306
- "BEGIN_STATE",
307
- "INSERT INTO items(value) VALUES (?)",
308
- "ROLLBACK",
309
- ]);
310
- });
311
-
312
- test("rejects nested state-aware transactions", async () => {
313
- const nativeDb = new FakeSqliteDatabase();
314
- const client = await db().createClient(
315
- testProviderContext(nativeDb, true),
316
- );
317
-
318
- await expect(
319
- client.transaction(
320
- async (tx) => {
321
- await tx.transaction(async () => {}, {
322
- experimental: { includeState: true },
323
- });
324
- },
325
- { experimental: { includeState: true } },
326
- ),
327
- ).rejects.toThrow("not supported for nested transactions");
328
- expect(nativeDb.executeCalls.map(({ sql }) => sql)).toEqual([
329
- "BEGIN_STATE",
330
- "ROLLBACK",
331
- ]);
332
- });
333
-
334
- test("validates transaction timeouts", async () => {
335
- const client = await db().createClient(
336
- testProviderContext(new FakeSqliteDatabase()),
337
- );
338
- for (const timeout of [0, -1, Number.NaN, Number.POSITIVE_INFINITY]) {
339
- await expect(
340
- client.transaction(async () => {}, { timeout }),
341
- ).rejects.toThrow("positive finite");
342
- }
343
- });
344
-
345
- test("warns once for manual cross-call transactions and names the opt-out", async () => {
346
- const client = await db().createClient(
347
- testProviderContext(new FakeSqliteDatabase()),
348
- );
349
- await client.execute("BEGIN");
350
- await client.execute("COMMIT");
351
- expect(logLines).toHaveLength(1);
352
- expect(JSON.parse(logLines[0] ?? "{}").msg).toContain(
353
- "Set warnOnManualTransactions: false",
354
- );
355
- });
356
-
357
- test("can suppress the manual transaction warning", async () => {
358
- const client = await db({
359
- warnOnManualTransactions: false,
360
- }).createClient(testProviderContext(new FakeSqliteDatabase()));
361
- await client.execute("BEGIN");
362
- expect(logLines).toHaveLength(0);
363
- });
364
- });
@@ -1,220 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { BRIDGE_RIVET_ERROR_PREFIX } from "@/actor/errors";
3
- import {
4
- type JsNativeDatabaseLike,
5
- wrapJsNativeDatabase,
6
- } from "./native-database";
7
-
8
- type NativeParams = Parameters<JsNativeDatabaseLike["execute"]>[1];
9
- type NativeExecuteResult = Awaited<ReturnType<JsNativeDatabaseLike["execute"]>>;
10
-
11
- function deferred<T>() {
12
- let resolve!: (value: T) => void;
13
- let reject!: (error: unknown) => void;
14
- const promise = new Promise<T>((resolvePromise, rejectPromise) => {
15
- resolve = resolvePromise;
16
- reject = rejectPromise;
17
- });
18
- return { promise, resolve, reject };
19
- }
20
-
21
- class FakeNativeDatabase implements JsNativeDatabaseLike {
22
- async beginTransaction() {
23
- return {
24
- exec: async (_sql: string) => this.exec(),
25
- execute: async (sql: string, params?: NativeParams) =>
26
- this.execute(sql, params),
27
- commit: async () => {},
28
- rollback: async () => {},
29
- };
30
- }
31
- active = 0;
32
- maxActive = 0;
33
- closed = false;
34
- executeCalls: { sql: string; params?: NativeParams; write: boolean }[] = [];
35
- #pending: ReturnType<typeof deferred<NativeExecuteResult>>[] = [];
36
-
37
- async exec() {
38
- return { columns: [], rows: [] };
39
- }
40
-
41
- async execute(sql: string, params?: NativeParams) {
42
- return await this.#startExecute(sql, params, false);
43
- }
44
-
45
- async query(sql: string, params?: NativeParams) {
46
- const { columns, rows } = await this.execute(sql, params);
47
- return { columns, rows };
48
- }
49
-
50
- async run(sql: string, params?: NativeParams) {
51
- const { changes } = await this.execute(sql, params);
52
- return { changes };
53
- }
54
-
55
- takeLastKvError() {
56
- return null;
57
- }
58
-
59
- async close() {
60
- this.closed = true;
61
- }
62
-
63
- resolveNext(result: Partial<NativeExecuteResult> = {}) {
64
- const pending = this.#pending.shift();
65
- if (!pending) {
66
- throw new Error("no pending native execute call");
67
- }
68
- pending.resolve({
69
- columns: [],
70
- rows: [],
71
- changes: 0,
72
- lastInsertRowId: null,
73
- ...result,
74
- });
75
- }
76
-
77
- rejectNext(error: unknown) {
78
- const pending = this.#pending.shift();
79
- if (!pending) {
80
- throw new Error("no pending native execute call");
81
- }
82
- pending.reject(error);
83
- }
84
-
85
- async #startExecute(
86
- sql: string,
87
- params: NativeParams,
88
- write: boolean,
89
- ): Promise<NativeExecuteResult> {
90
- this.executeCalls.push({ sql, params, write });
91
- this.active++;
92
- this.maxActive = Math.max(this.maxActive, this.active);
93
- const pending = deferred<NativeExecuteResult>();
94
- this.#pending.push(pending);
95
- try {
96
- return await pending.promise;
97
- } finally {
98
- this.active--;
99
- }
100
- }
101
- }
102
-
103
- describe("wrapJsNativeDatabase", () => {
104
- test("admits Promise.all read queries concurrently", async () => {
105
- const native = new FakeNativeDatabase();
106
- const db = wrapJsNativeDatabase(native);
107
-
108
- const first = db.query("SELECT 1");
109
- const second = db.query("SELECT 2");
110
-
111
- expect(native.maxActive).toBe(2);
112
- native.resolveNext({ columns: ["value"], rows: [[1]] });
113
- native.resolveNext({ columns: ["value"], rows: [[2]] });
114
-
115
- await expect(first).resolves.toEqual({
116
- columns: ["value"],
117
- rows: [[1]],
118
- });
119
- await expect(second).resolves.toEqual({
120
- columns: ["value"],
121
- rows: [[2]],
122
- });
123
- });
124
-
125
- test("normalizes supported sqlite bind values", async () => {
126
- const native = new FakeNativeDatabase();
127
- const db = wrapJsNativeDatabase(native);
128
- const blob = new Uint8Array([1, 2, 3]);
129
-
130
- const query = db.query("SELECT ?, ?, ?, ?, ?, ?, ?", [
131
- 1n,
132
- true,
133
- "text",
134
- 1.5,
135
- null,
136
- undefined,
137
- blob,
138
- ]);
139
-
140
- expect(native.executeCalls[0]?.params).toEqual([
141
- { kind: "int", intValue: 1 },
142
- { kind: "int", intValue: 1 },
143
- { kind: "text", textValue: "text" },
144
- { kind: "float", floatValue: 1.5 },
145
- { kind: "null" },
146
- { kind: "null" },
147
- { kind: "blob", blobValue: Buffer.from(blob) },
148
- ]);
149
-
150
- native.resolveNext({ columns: ["value"], rows: [[1]] });
151
-
152
- await expect(query).resolves.toEqual({
153
- columns: ["value"],
154
- rows: [[1]],
155
- });
156
- });
157
-
158
- test("returns native execute metadata", async () => {
159
- const native = new FakeNativeDatabase();
160
- const db = wrapJsNativeDatabase(native);
161
-
162
- const write = db.execute("INSERT INTO test VALUES (1)");
163
- native.resolveNext({ changes: 1, lastInsertRowId: 7 });
164
- await expect(write).resolves.toMatchObject({
165
- changes: 1,
166
- lastInsertRowId: 7,
167
- });
168
-
169
- const fallback = db.execute("SELECT last_insert_rowid()");
170
- await expect(fallback).resolves.toMatchObject({
171
- rows: [[7]],
172
- });
173
- });
174
-
175
- test("close waits for admitted native calls and rejects new work", async () => {
176
- const native = new FakeNativeDatabase();
177
- const db = wrapJsNativeDatabase(native);
178
-
179
- const query = db.query("SELECT 1");
180
- const close = db.close();
181
- await Promise.resolve();
182
-
183
- expect(native.closed).toBe(false);
184
- native.resolveNext({ columns: ["value"], rows: [[1]] });
185
-
186
- await query;
187
- await close;
188
-
189
- expect(native.closed).toBe(true);
190
- await expect(db.query("SELECT 2")).rejects.toThrow(
191
- "Database is closed",
192
- );
193
- });
194
-
195
- test("decodes sanitized native bridge errors without logging", async () => {
196
- const native = new FakeNativeDatabase();
197
- const db = wrapJsNativeDatabase(native);
198
- const query = db.execute("SELECT broken", [1, "two"]);
199
- const bridgeReason = `${BRIDGE_RIVET_ERROR_PREFIX}${JSON.stringify({
200
- group: "rivetkit",
201
- code: "internal_error",
202
- message: "An internal error occurred",
203
- statusCode: 500,
204
- })}`;
205
-
206
- native.rejectNext(new Error(bridgeReason));
207
-
208
- await expect(query).rejects.toMatchObject({
209
- name: "RivetError",
210
- group: "rivetkit",
211
- code: "internal_error",
212
- message: "An internal error occurred",
213
- });
214
-
215
- await query.catch((error) => {
216
- expect(error.stack).toContain("decodeBridgeRivetError");
217
- expect(error.stack).toContain("enrichNativeDatabaseError");
218
- });
219
- });
220
- });