rivetkit 0.0.0-sqlite-profiling-compat.4a9a623 → 0.0.0-sqlite-sync-3x-release.a00b07f
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/dist/browser/client.d.ts +1317 -765
- package/dist/browser/client.js +1457 -737
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.d.ts +252 -129
- package/dist/browser/inspector/client.js +2042 -228
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/actor/errors.cjs +6 -2
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/actor/errors.d.cts +1 -1
- package/dist/tsup/actor/errors.d.ts +1 -1
- package/dist/tsup/actor/errors.js +5 -1
- package/dist/tsup/agent-os/index.cjs +3 -2367
- package/dist/tsup/agent-os/index.cjs.map +1 -1
- package/dist/tsup/agent-os/index.d.cts +1 -2277
- package/dist/tsup/agent-os/index.d.ts +1 -2277
- package/dist/tsup/agent-os/index.js +3 -2367
- package/dist/tsup/agent-os/index.js.map +1 -1
- package/dist/tsup/{chunk-7QKCGUTJ.cjs → chunk-2MOLY6AM.cjs} +444 -29
- package/dist/tsup/chunk-2MOLY6AM.cjs.map +1 -0
- package/dist/tsup/{chunk-IICIOTML.js → chunk-3DONDAMG.js} +2 -2
- package/dist/tsup/{chunk-XIX5DOZN.js → chunk-6FEYCVZC.js} +196 -20
- package/dist/tsup/chunk-6FEYCVZC.js.map +1 -0
- package/dist/tsup/chunk-6KXVLIXL.cjs +14 -0
- package/dist/tsup/chunk-6KXVLIXL.cjs.map +1 -0
- package/dist/tsup/chunk-73RNTXHB.cjs +243 -0
- package/dist/tsup/chunk-73RNTXHB.cjs.map +1 -0
- package/dist/tsup/{chunk-HXUEHHJF.js → chunk-A2CF6PFH.js} +19 -11
- package/dist/tsup/chunk-A2CF6PFH.js.map +1 -0
- package/dist/tsup/{chunk-75UE45DE.js → chunk-BRH4PQS5.js} +436 -21
- package/dist/tsup/chunk-BRH4PQS5.js.map +1 -0
- package/dist/tsup/{chunk-2WMYYG4R.cjs → chunk-BTSDFFDC.cjs} +18 -11
- package/dist/tsup/chunk-BTSDFFDC.cjs.map +1 -0
- package/dist/tsup/{chunk-RIT7N3TX.js → chunk-D5WUPE47.js} +50 -3
- package/dist/tsup/chunk-D5WUPE47.js.map +1 -0
- package/dist/tsup/{chunk-4PX3ZKJR.js → chunk-DJUBJQEQ.js} +3 -3
- package/dist/tsup/chunk-DJUBJQEQ.js.map +1 -0
- package/dist/tsup/{chunk-3MHDOUD7.cjs → chunk-HDVZ6YZG.cjs} +197 -21
- package/dist/tsup/chunk-HDVZ6YZG.cjs.map +1 -0
- package/dist/tsup/{chunk-JZ7TWV65.js → chunk-JTHHCZCZ.js} +24 -1
- package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
- package/dist/tsup/{chunk-ND567FR5.cjs → chunk-JUHXV3HF.cjs} +3 -3
- package/dist/tsup/chunk-JUHXV3HF.cjs.map +1 -0
- package/dist/tsup/chunk-JUWMP5O3.js +14 -0
- package/dist/tsup/chunk-JUWMP5O3.js.map +1 -0
- package/dist/tsup/{chunk-ZA7FLHKH.cjs → chunk-KVFIONKQ.cjs} +25 -2
- package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
- package/dist/tsup/{chunk-4FC7TVS6.cjs → chunk-MGI2VVF6.cjs} +39 -7
- package/dist/tsup/chunk-MGI2VVF6.cjs.map +1 -0
- package/dist/tsup/{chunk-NHUTYWKU.cjs → chunk-OTVJYZ2L.cjs} +1139 -470
- package/dist/tsup/chunk-OTVJYZ2L.cjs.map +1 -0
- package/dist/tsup/{chunk-VE2X4KMG.cjs → chunk-RTWH3AX3.cjs} +18 -10
- package/dist/tsup/chunk-RTWH3AX3.cjs.map +1 -0
- package/dist/tsup/{chunk-QAVN4QNO.cjs → chunk-TM76Z7XT.cjs} +7 -7
- package/dist/tsup/chunk-TM76Z7XT.cjs.map +1 -0
- package/dist/tsup/{chunk-AGDY6TKH.js → chunk-TRUHDXOU.js} +1023 -354
- package/dist/tsup/chunk-TRUHDXOU.js.map +1 -0
- package/dist/tsup/{chunk-XFYEVYHG.js → chunk-TSQTI6H6.js} +16 -9
- package/dist/tsup/chunk-TSQTI6H6.js.map +1 -0
- package/dist/tsup/{chunk-2CICCOBL.cjs → chunk-Z7E2G3JZ.cjs} +52 -5
- package/dist/tsup/chunk-Z7E2G3JZ.cjs.map +1 -0
- package/dist/tsup/{chunk-ZZ3WBRPD.js → chunk-ZOVJDAOW.js} +38 -6
- package/dist/tsup/chunk-ZOVJDAOW.js.map +1 -0
- package/dist/tsup/chunk-ZYK4B5WV.js +243 -0
- package/dist/tsup/chunk-ZYK4B5WV.js.map +1 -0
- package/dist/tsup/client/mod.cjs +8 -9
- package/dist/tsup/client/mod.cjs.map +1 -1
- package/dist/tsup/client/mod.d.cts +32 -6
- package/dist/tsup/client/mod.d.ts +32 -6
- package/dist/tsup/client/mod.js +7 -8
- package/dist/tsup/common/log.cjs +5 -3
- package/dist/tsup/common/log.cjs.map +1 -1
- package/dist/tsup/common/log.d.cts +5 -1
- package/dist/tsup/common/log.d.ts +5 -1
- package/dist/tsup/common/log.js +6 -4
- package/dist/tsup/common/websocket.cjs +4 -4
- package/dist/tsup/common/websocket.cjs.map +1 -1
- package/dist/tsup/common/websocket.js +3 -3
- package/dist/tsup/{config-BR1Nn0QQ.d.ts → config-B4erdavw.d.ts} +571 -65
- package/dist/tsup/{config-B0LNDrLY.d.ts → config-BfG1qpYz.d.cts} +80 -14
- package/dist/tsup/{config-B0LNDrLY.d.cts → config-BfG1qpYz.d.ts} +80 -14
- package/dist/tsup/{config-tCvkwzyN.d.cts → config-DK144OAw.d.cts} +571 -65
- package/dist/tsup/{context-oc9zhA3x.d.ts → context-Cvnf3BRe.d.cts} +3 -2
- package/dist/tsup/{context-D4WDwf9m.d.cts → context-Oxi6YWcG.d.ts} +3 -2
- package/dist/tsup/db/drizzle.cjs +127 -67
- package/dist/tsup/db/drizzle.cjs.map +1 -1
- package/dist/tsup/db/drizzle.d.cts +8 -4
- package/dist/tsup/db/drizzle.d.ts +8 -4
- package/dist/tsup/db/drizzle.js +127 -67
- package/dist/tsup/db/drizzle.js.map +1 -1
- package/dist/tsup/db/mod.cjs +6 -126
- package/dist/tsup/db/mod.cjs.map +1 -1
- package/dist/tsup/db/mod.d.cts +8 -4
- package/dist/tsup/db/mod.d.ts +8 -4
- package/dist/tsup/db/mod.js +5 -125
- package/dist/tsup/db/mod.js.map +1 -1
- package/dist/tsup/dynamic/mod.cjs +2 -2
- package/dist/tsup/dynamic/mod.cjs.map +1 -1
- package/dist/tsup/dynamic/mod.d.cts +6 -4
- package/dist/tsup/dynamic/mod.d.ts +6 -4
- package/dist/tsup/dynamic/mod.js +1 -1
- package/dist/tsup/inspector/mod.cjs +47 -12
- package/dist/tsup/inspector/mod.cjs.map +1 -1
- package/dist/tsup/inspector/mod.d.cts +26 -136
- package/dist/tsup/inspector/mod.d.ts +26 -136
- package/dist/tsup/inspector/mod.js +46 -11
- package/dist/tsup/inspector/mod.js.map +1 -1
- package/dist/tsup/inspector/workflow.cjs +21 -0
- package/dist/tsup/inspector/workflow.cjs.map +1 -0
- package/dist/tsup/inspector/workflow.d.cts +144 -0
- package/dist/tsup/inspector/workflow.d.ts +144 -0
- package/dist/tsup/inspector/workflow.js +21 -0
- package/dist/tsup/inspector/workflow.js.map +1 -0
- package/dist/tsup/inspector-tab/mod.cjs +2 -13
- package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
- package/dist/tsup/inspector-tab/mod.d.cts +9 -17
- package/dist/tsup/inspector-tab/mod.d.ts +9 -17
- package/dist/tsup/inspector-tab/mod.js +1 -12
- package/dist/tsup/inspector-tab/mod.js.map +1 -1
- package/dist/tsup/mod.cjs +2724 -611
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +9 -7
- package/dist/tsup/mod.d.ts +9 -7
- package/dist/tsup/mod.js +2641 -528
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +19 -17
- package/dist/tsup/test/mod.cjs.map +1 -1
- package/dist/tsup/test/mod.d.cts +6 -4
- package/dist/tsup/test/mod.d.ts +6 -4
- package/dist/tsup/test/mod.js +18 -16
- package/dist/tsup/test/mod.js.map +1 -1
- package/dist/tsup/unstable/migrations.cjs +101 -0
- package/dist/tsup/unstable/migrations.cjs.map +1 -0
- package/dist/tsup/unstable/migrations.d.cts +29 -0
- package/dist/tsup/unstable/migrations.d.ts +29 -0
- package/dist/tsup/unstable/migrations.js +101 -0
- package/dist/tsup/unstable/migrations.js.map +1 -0
- package/dist/tsup/{utils-CqDnC_PS.d.cts → utils-3rsE0JvI.d.cts} +25 -2
- package/dist/tsup/{utils-CqDnC_PS.d.ts → utils-3rsE0JvI.d.ts} +25 -2
- package/dist/tsup/utils-C1O-SklO.d.ts +162 -0
- package/dist/tsup/utils-C6b0aYUM.d.cts +162 -0
- package/dist/tsup/utils.cjs +7 -3
- package/dist/tsup/utils.cjs.map +1 -1
- package/dist/tsup/utils.d.cts +4 -147
- package/dist/tsup/utils.d.ts +4 -147
- package/dist/tsup/utils.js +6 -2
- package/dist/tsup/workflow/mod.cjs +260 -200
- package/dist/tsup/workflow/mod.cjs.map +1 -1
- package/dist/tsup/workflow/mod.d.cts +8 -6
- package/dist/tsup/workflow/mod.d.ts +8 -6
- package/dist/tsup/workflow/mod.js +249 -189
- package/dist/tsup/workflow/mod.js.map +1 -1
- package/package.json +34 -14
- package/src/actor/actions.ts +155 -0
- package/src/actor/config.ts +403 -43
- package/src/actor/definition.ts +26 -7
- package/src/actor/errors.ts +87 -5
- package/src/actor/keys.ts +86 -0
- package/src/actor/mod.ts +15 -1
- package/src/actor/tracing.ts +28 -0
- package/src/agent-os/index.ts +5 -65
- package/src/client/actor-common.ts +8 -15
- package/src/client/actor-conn.ts +135 -8
- package/src/client/actor-handle.ts +180 -42
- package/src/client/actor-query.ts +8 -1
- package/src/client/auth.ts +37 -0
- package/src/client/client.ts +89 -21
- package/src/client/config.ts +27 -2
- package/src/client/errors.ts +2 -1
- package/src/client/lifecycle-errors.ts +16 -1
- package/src/client/mod.ts +9 -0
- package/src/client/outbound-telemetry.ts +66 -0
- package/src/client/query.ts +2 -0
- package/src/client/queue.ts +3 -0
- package/src/client/raw-utils.ts +37 -8
- package/src/client/resolve-gateway-target.ts +2 -0
- package/src/client/retry-budget.ts +78 -0
- package/src/client/token-provider.ts +93 -0
- package/src/client/utils.ts +16 -2
- package/src/common/actor-router-consts.ts +5 -0
- package/src/common/actor-telemetry-context.ts +19 -0
- package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
- package/src/common/bare/generated/inspector/v5.ts +1016 -0
- package/src/common/bare/generated/inspector/v6.ts +1367 -0
- package/src/common/bare/transport/v1.ts +36 -0
- package/src/common/database/config.ts +108 -15
- package/src/common/database/mod.ts +247 -76
- package/src/common/database/native-database.ts +433 -23
- package/src/common/database/shared.ts +24 -0
- package/src/common/encoding.ts +31 -21
- package/src/common/fetch-like.ts +63 -0
- package/src/common/inspector-transport.ts +12 -2
- package/src/common/log.ts +14 -8
- package/src/common/otel-context.ts +99 -0
- package/src/common/router.ts +1 -1
- package/src/common/utils.ts +7 -19
- package/src/db/drizzle.ts +182 -80
- package/src/db/mod.ts +2 -0
- package/src/dynamic/isolate-runtime.ts +0 -2
- package/src/engine-api/actors.ts +3 -3
- package/src/engine-client/actor-http-client.ts +29 -20
- package/src/engine-client/actor-websocket-client.ts +6 -3
- package/src/engine-client/api-endpoints.ts +72 -14
- package/src/engine-client/driver.ts +8 -15
- package/src/engine-client/mod.ts +248 -112
- package/src/inspector/client.browser.ts +134 -16
- package/src/inspector/mod.ts +1 -1
- package/src/inspector/workflow.ts +61 -0
- package/src/inspector-tab/mod.ts +1 -15
- package/src/registry/config/envoy.ts +2 -18
- package/src/registry/config/index.ts +53 -61
- package/src/registry/index.ts +311 -21
- package/src/registry/napi-runtime.ts +583 -39
- package/src/registry/native-http.ts +225 -0
- package/src/registry/native.ts +1365 -414
- package/src/registry/run-handler-coordinator.ts +219 -0
- package/src/registry/runtime.ts +372 -7
- package/src/registry/wasm-runtime.ts +490 -21
- package/src/registry/write-through-proxy.ts +101 -0
- package/src/test/mod.ts +1 -19
- package/src/test/ready.ts +22 -0
- package/src/unstable/migrations.ts +141 -0
- package/src/utils/env-vars.ts +15 -6
- package/src/utils/node.ts +44 -28
- package/src/utils.ts +3 -0
- package/src/workflow/context.ts +9 -27
- package/src/workflow/driver.ts +253 -116
- package/src/workflow/inspector.ts +23 -24
- package/src/workflow/mod.ts +50 -72
- package/dist/tsup/chunk-2CICCOBL.cjs.map +0 -1
- package/dist/tsup/chunk-2WMYYG4R.cjs.map +0 -1
- package/dist/tsup/chunk-3MHDOUD7.cjs.map +0 -1
- package/dist/tsup/chunk-4FC7TVS6.cjs.map +0 -1
- package/dist/tsup/chunk-4PX3ZKJR.js.map +0 -1
- package/dist/tsup/chunk-75UE45DE.js.map +0 -1
- package/dist/tsup/chunk-7QKCGUTJ.cjs.map +0 -1
- package/dist/tsup/chunk-AGDY6TKH.js.map +0 -1
- package/dist/tsup/chunk-HXUEHHJF.js.map +0 -1
- package/dist/tsup/chunk-JZ7TWV65.js.map +0 -1
- package/dist/tsup/chunk-ND567FR5.cjs.map +0 -1
- package/dist/tsup/chunk-NHUTYWKU.cjs.map +0 -1
- package/dist/tsup/chunk-QAVN4QNO.cjs.map +0 -1
- package/dist/tsup/chunk-RIT7N3TX.js.map +0 -1
- package/dist/tsup/chunk-RV5MAFDT.cjs +0 -382
- package/dist/tsup/chunk-RV5MAFDT.cjs.map +0 -1
- package/dist/tsup/chunk-VE2X4KMG.cjs.map +0 -1
- package/dist/tsup/chunk-XFYEVYHG.js.map +0 -1
- package/dist/tsup/chunk-XHGDS3DZ.js +0 -382
- package/dist/tsup/chunk-XHGDS3DZ.js.map +0 -1
- package/dist/tsup/chunk-XIX5DOZN.js.map +0 -1
- package/dist/tsup/chunk-ZA7FLHKH.cjs.map +0 -1
- package/dist/tsup/chunk-ZZ3WBRPD.js.map +0 -1
- package/src/actor/driver.ts +0 -141
- package/src/actor/instance/mod.ts +0 -2484
- package/src/actor/keys.test.ts +0 -275
- package/src/actor/utils.test.ts +0 -48
- package/src/agent-os/actor/cron.ts +0 -84
- package/src/agent-os/actor/db.ts +0 -54
- package/src/agent-os/actor/filesystem.ts +0 -131
- package/src/agent-os/actor/index.ts +0 -285
- package/src/agent-os/actor/network.ts +0 -58
- package/src/agent-os/actor/preview.ts +0 -190
- package/src/agent-os/actor/process.ts +0 -177
- package/src/agent-os/actor/session.ts +0 -518
- package/src/agent-os/actor/shell.ts +0 -60
- package/src/agent-os/config.ts +0 -79
- package/src/agent-os/fs/database-vfs.ts +0 -624
- package/src/agent-os/types.ts +0 -148
- package/src/client/test.ts +0 -44
- package/src/common/database/mod.test.ts +0 -137
- package/src/common/database/native-database.test.ts +0 -211
- package/src/common/inline-websocket-adapter.test.ts +0 -51
- package/src/drivers/engine/actor-driver.ts +0 -2473
- package/src/registry/process-metrics.ts +0 -250
- package/src/registry/runtime.test.ts +0 -50
- package/src/utils/endpoint-parser.test.ts +0 -206
- package/src/utils.test.ts +0 -34
- /package/dist/tsup/{chunk-IICIOTML.js.map → chunk-3DONDAMG.js.map} +0 -0
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { decodeBridgeRivetError } from "@/actor/errors";
|
|
2
2
|
import type {
|
|
3
|
+
SqliteBatchStatement,
|
|
3
4
|
SqliteBindings,
|
|
4
5
|
SqliteDatabase,
|
|
5
6
|
SqliteExecuteResult,
|
|
6
7
|
SqliteNativeMetrics,
|
|
8
|
+
SqliteTransactionDatabase,
|
|
9
|
+
SynchronousSqliteTransactionDatabase,
|
|
7
10
|
} from "./config";
|
|
8
11
|
|
|
9
12
|
type NativeBindNoValues = {
|
|
@@ -65,12 +68,37 @@ interface NativeExecuteResult {
|
|
|
65
68
|
lastInsertRowId?: number | null;
|
|
66
69
|
}
|
|
67
70
|
|
|
71
|
+
interface NativeBatchStatement {
|
|
72
|
+
sql: string;
|
|
73
|
+
params?: NativeBindParam[] | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
68
76
|
export interface JsNativeDatabaseLike {
|
|
69
77
|
exec(sql: string): Promise<NativeExecResult>;
|
|
78
|
+
execSync(sql: string): NativeExecResult;
|
|
70
79
|
execute(
|
|
71
80
|
sql: string,
|
|
72
81
|
params?: NativeBindParam[] | null,
|
|
73
82
|
): Promise<NativeExecuteResult>;
|
|
83
|
+
executeSync(
|
|
84
|
+
sql: string,
|
|
85
|
+
params?: NativeBindParam[] | null,
|
|
86
|
+
): NativeExecuteResult;
|
|
87
|
+
executeBatch?(
|
|
88
|
+
statements: NativeBatchStatement[],
|
|
89
|
+
): Promise<NativeExecuteResult[]>;
|
|
90
|
+
beginStateTransaction?(
|
|
91
|
+
timeoutMs?: number,
|
|
92
|
+
context?: unknown,
|
|
93
|
+
): Promise<JsNativeTransactionLike>;
|
|
94
|
+
beginTransaction(
|
|
95
|
+
timeoutMs?: number,
|
|
96
|
+
name?: string,
|
|
97
|
+
): Promise<JsNativeTransactionLike>;
|
|
98
|
+
beginTransactionSync(
|
|
99
|
+
timeoutMs?: number,
|
|
100
|
+
name?: string,
|
|
101
|
+
): JsNativeSynchronousTransactionLike;
|
|
74
102
|
query(
|
|
75
103
|
sql: string,
|
|
76
104
|
params?: NativeBindParam[] | null,
|
|
@@ -81,8 +109,58 @@ export interface JsNativeDatabaseLike {
|
|
|
81
109
|
): Promise<NativeRunResult>;
|
|
82
110
|
metrics?(): SqliteNativeMetrics | null;
|
|
83
111
|
takeLastKvError?(): string | null;
|
|
112
|
+
open(): Promise<void>;
|
|
113
|
+
commitTicket(): number;
|
|
114
|
+
whenDurable(ticket: number): Promise<void>;
|
|
115
|
+
abortGeneration(reason: string): void;
|
|
84
116
|
close(): Promise<void>;
|
|
85
117
|
}
|
|
118
|
+
export type StateAwareSqliteDatabase = SqliteDatabase & {
|
|
119
|
+
open(): Promise<void>;
|
|
120
|
+
commitTicket(): number;
|
|
121
|
+
whenDurable(ticket: number): Promise<void>;
|
|
122
|
+
beginStateTransaction(
|
|
123
|
+
timeoutMs?: number,
|
|
124
|
+
context?: unknown,
|
|
125
|
+
): Promise<SqliteTransactionDatabase>;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
type NativeSqliteDatabase = StateAwareSqliteDatabase & {
|
|
129
|
+
/** Stop JS calls while leaving the runtime-owned backend open. */
|
|
130
|
+
detachForShutdown(): Promise<void>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export interface JsNativeTransactionLike {
|
|
134
|
+
exec(sql: string): Promise<NativeExecResult>;
|
|
135
|
+
execute(
|
|
136
|
+
sql: string,
|
|
137
|
+
params?: NativeBindParam[] | null,
|
|
138
|
+
): Promise<NativeExecuteResult>;
|
|
139
|
+
commit(): Promise<void>;
|
|
140
|
+
rollback(): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface JsNativeSynchronousTransactionLike
|
|
144
|
+
extends JsNativeTransactionLike {
|
|
145
|
+
execSync(sql: string): NativeExecResult;
|
|
146
|
+
executeSync(
|
|
147
|
+
sql: string,
|
|
148
|
+
params?: NativeBindParam[] | null,
|
|
149
|
+
): NativeExecuteResult;
|
|
150
|
+
commitSync(): void;
|
|
151
|
+
rollbackSync(): void;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isSynchronousTransaction(
|
|
155
|
+
transaction: JsNativeTransactionLike,
|
|
156
|
+
): transaction is JsNativeSynchronousTransactionLike {
|
|
157
|
+
const candidate =
|
|
158
|
+
transaction as Partial<JsNativeSynchronousTransactionLike>;
|
|
159
|
+
return (
|
|
160
|
+
typeof candidate.commitSync === "function" &&
|
|
161
|
+
typeof candidate.rollbackSync === "function"
|
|
162
|
+
);
|
|
163
|
+
}
|
|
86
164
|
|
|
87
165
|
function shouldAttachNativeKvError(message: string): boolean {
|
|
88
166
|
return /i\/o error|unable to open database file/i.test(message);
|
|
@@ -122,13 +200,19 @@ function toNativeBinding(arg: unknown): NativeBindParam {
|
|
|
122
200
|
if (arg === null || arg === undefined) {
|
|
123
201
|
return { kind: "null" };
|
|
124
202
|
}
|
|
125
|
-
if (
|
|
126
|
-
|
|
203
|
+
if (
|
|
204
|
+
typeof arg === "bigint" ||
|
|
205
|
+
(typeof arg === "number" && Number.isInteger(arg))
|
|
206
|
+
) {
|
|
207
|
+
const value = Number(arg);
|
|
208
|
+
if (!Number.isSafeInteger(value)) {
|
|
209
|
+
throw new RangeError(
|
|
210
|
+
"SQLite integer bindings must be within the JavaScript safe integer range.",
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
return { kind: "int", intValue: value };
|
|
127
214
|
}
|
|
128
215
|
if (typeof arg === "number") {
|
|
129
|
-
if (Number.isInteger(arg)) {
|
|
130
|
-
return { kind: "int", intValue: arg };
|
|
131
|
-
}
|
|
132
216
|
return { kind: "float", floatValue: arg };
|
|
133
217
|
}
|
|
134
218
|
if (typeof arg === "string") {
|
|
@@ -146,9 +230,13 @@ function toNativeBinding(arg: unknown): NativeBindParam {
|
|
|
146
230
|
function extractNamedSqliteParameters(sql: string): string[] {
|
|
147
231
|
const orderedNames: string[] = [];
|
|
148
232
|
const seen = new Set<string>();
|
|
149
|
-
|
|
233
|
+
// SQLite identifiers may contain "$" and any non-ASCII character. Consume
|
|
234
|
+
// whole identifiers so their suffixes cannot be mistaken for parameters.
|
|
235
|
+
const pattern =
|
|
236
|
+
/'(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`|\[[^\]]*\]|--[^\n]*|\/\*[\s\S]*?(?:\*\/|$)|([:@$][\w$\u0080-\u{10FFFF}]+(?:::[\w$\u0080-\u{10FFFF}]+)*(?:\([^)]*\))?)|[A-Za-z_\u0080-\u{10FFFF}][\w$\u0080-\u{10FFFF}]*/gu;
|
|
150
237
|
for (const match of sql.matchAll(pattern)) {
|
|
151
238
|
const name = match[1];
|
|
239
|
+
if (!name) continue;
|
|
152
240
|
if (seen.has(name)) {
|
|
153
241
|
continue;
|
|
154
242
|
}
|
|
@@ -207,6 +295,15 @@ function toNativeBindings(
|
|
|
207
295
|
});
|
|
208
296
|
}
|
|
209
297
|
|
|
298
|
+
function toNativeBatchStatement(
|
|
299
|
+
statement: SqliteBatchStatement,
|
|
300
|
+
): NativeBatchStatement {
|
|
301
|
+
return {
|
|
302
|
+
sql: statement.sql,
|
|
303
|
+
params: toNativeBindings(statement.sql, statement.params),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
210
307
|
function normalizeNativeMetrics(
|
|
211
308
|
metrics: SqliteNativeMetrics | null | undefined,
|
|
212
309
|
): SqliteNativeMetrics | null {
|
|
@@ -268,7 +365,7 @@ class NativeCloseGate {
|
|
|
268
365
|
};
|
|
269
366
|
}
|
|
270
367
|
|
|
271
|
-
async close(callback
|
|
368
|
+
async close(callback?: () => Promise<void>): Promise<void> {
|
|
272
369
|
if (this.#closed) {
|
|
273
370
|
return;
|
|
274
371
|
}
|
|
@@ -276,36 +373,66 @@ class NativeCloseGate {
|
|
|
276
373
|
if (this.#active > 0) {
|
|
277
374
|
await new Promise<void>((resolve) => this.#waiters.push(resolve));
|
|
278
375
|
}
|
|
279
|
-
await callback();
|
|
376
|
+
await callback?.();
|
|
280
377
|
}
|
|
281
378
|
}
|
|
282
379
|
|
|
283
380
|
export function wrapJsNativeDatabase(
|
|
284
381
|
database: JsNativeDatabaseLike,
|
|
285
|
-
):
|
|
382
|
+
): NativeSqliteDatabase {
|
|
286
383
|
const gate = new NativeCloseGate();
|
|
287
|
-
|
|
384
|
+
// Detach and explicit close are mutually exclusive terminal operations.
|
|
385
|
+
// Whichever starts first defines whether the backend is physically closed.
|
|
386
|
+
let terminalPromise: Promise<void> | undefined;
|
|
288
387
|
let lastInsertRowId: number | null = null;
|
|
289
388
|
|
|
290
389
|
const executeNative = async (
|
|
291
390
|
sql: string,
|
|
292
391
|
params?: SqliteBindings,
|
|
293
392
|
): Promise<SqliteExecuteResult> => {
|
|
294
|
-
const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
|
|
295
|
-
if (lastInsertRowIdColumn) {
|
|
296
|
-
return {
|
|
297
|
-
columns: [lastInsertRowIdColumn],
|
|
298
|
-
rows: [[lastInsertRowId ?? 0]],
|
|
299
|
-
changes: 0,
|
|
300
|
-
lastInsertRowId,
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
|
|
304
393
|
const release = gate.enter();
|
|
305
394
|
try {
|
|
395
|
+
const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
|
|
396
|
+
if (lastInsertRowIdColumn) {
|
|
397
|
+
return {
|
|
398
|
+
columns: [lastInsertRowIdColumn],
|
|
399
|
+
rows: [[lastInsertRowId ?? 0]],
|
|
400
|
+
changes: 0,
|
|
401
|
+
lastInsertRowId,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
306
405
|
const nativeParams = toNativeBindings(sql, params);
|
|
307
406
|
const result = await database.execute(sql, nativeParams);
|
|
308
|
-
if (result.lastInsertRowId
|
|
407
|
+
if (result.lastInsertRowId != null) {
|
|
408
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
} catch (error) {
|
|
412
|
+
enrichNativeDatabaseError(database, error);
|
|
413
|
+
} finally {
|
|
414
|
+
release();
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
const executeNativeSync = (
|
|
418
|
+
sql: string,
|
|
419
|
+
params?: SqliteBindings,
|
|
420
|
+
): SqliteExecuteResult => {
|
|
421
|
+
const release = gate.enter();
|
|
422
|
+
try {
|
|
423
|
+
const nativeParams = toNativeBindings(sql, params);
|
|
424
|
+
const result = database.executeSync(sql, nativeParams);
|
|
425
|
+
// Execute first so runtime availability and transaction admission are enforced.
|
|
426
|
+
const rowIdColumn = lastInsertRowIdColumnName(sql);
|
|
427
|
+
if (rowIdColumn) {
|
|
428
|
+
return {
|
|
429
|
+
columns: [rowIdColumn],
|
|
430
|
+
rows: [[lastInsertRowId ?? 0]],
|
|
431
|
+
changes: 0,
|
|
432
|
+
lastInsertRowId,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
if (result.lastInsertRowId != null) {
|
|
309
436
|
lastInsertRowId = result.lastInsertRowId;
|
|
310
437
|
}
|
|
311
438
|
return result;
|
|
@@ -337,12 +464,141 @@ export function wrapJsNativeDatabase(
|
|
|
337
464
|
callback(row, result.columns);
|
|
338
465
|
}
|
|
339
466
|
},
|
|
467
|
+
execSync(
|
|
468
|
+
sql: string,
|
|
469
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
470
|
+
): void {
|
|
471
|
+
const release = gate.enter();
|
|
472
|
+
let result: NativeExecResult;
|
|
473
|
+
try {
|
|
474
|
+
result = database.execSync(sql);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
enrichNativeDatabaseError(database, error);
|
|
477
|
+
} finally {
|
|
478
|
+
release();
|
|
479
|
+
}
|
|
480
|
+
if (callback) {
|
|
481
|
+
for (const row of result.rows) {
|
|
482
|
+
callback(row, result.columns);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
},
|
|
340
486
|
async execute(
|
|
341
487
|
sql: string,
|
|
342
488
|
params?: SqliteBindings,
|
|
343
489
|
): Promise<SqliteExecuteResult> {
|
|
344
490
|
return await executeNative(sql, params);
|
|
345
491
|
},
|
|
492
|
+
executeSync(sql: string, params?: SqliteBindings): SqliteExecuteResult {
|
|
493
|
+
return executeNativeSync(sql, params);
|
|
494
|
+
},
|
|
495
|
+
async executeBatch(
|
|
496
|
+
statements: SqliteBatchStatement[],
|
|
497
|
+
): Promise<SqliteExecuteResult[]> {
|
|
498
|
+
const nativeStatements = statements.map(toNativeBatchStatement);
|
|
499
|
+
const release = gate.enter();
|
|
500
|
+
let transaction: JsNativeTransactionLike | undefined;
|
|
501
|
+
try {
|
|
502
|
+
const results = database.executeBatch
|
|
503
|
+
? await database.executeBatch(nativeStatements)
|
|
504
|
+
: await (async () => {
|
|
505
|
+
transaction = await database.beginTransaction();
|
|
506
|
+
const transactionResults: NativeExecuteResult[] =
|
|
507
|
+
[];
|
|
508
|
+
for (const statement of nativeStatements) {
|
|
509
|
+
transactionResults.push(
|
|
510
|
+
await transaction.execute(
|
|
511
|
+
statement.sql,
|
|
512
|
+
statement.params,
|
|
513
|
+
),
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
await transaction.commit();
|
|
517
|
+
transaction = undefined;
|
|
518
|
+
return transactionResults;
|
|
519
|
+
})();
|
|
520
|
+
for (const result of results) {
|
|
521
|
+
if (result.lastInsertRowId != null) {
|
|
522
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return results;
|
|
526
|
+
} catch (error) {
|
|
527
|
+
if (transaction) {
|
|
528
|
+
try {
|
|
529
|
+
await transaction.rollback();
|
|
530
|
+
} catch {
|
|
531
|
+
// The original batch error is more actionable than cleanup failure.
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
enrichNativeDatabaseError(database, error);
|
|
535
|
+
} finally {
|
|
536
|
+
release();
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
async beginTransaction(
|
|
540
|
+
timeoutMs?: number,
|
|
541
|
+
name?: string,
|
|
542
|
+
): Promise<SqliteTransactionDatabase> {
|
|
543
|
+
const release = gate.enter();
|
|
544
|
+
let transaction: JsNativeTransactionLike;
|
|
545
|
+
try {
|
|
546
|
+
transaction = await database.beginTransaction(timeoutMs, name);
|
|
547
|
+
} catch (error) {
|
|
548
|
+
enrichNativeDatabaseError(database, error);
|
|
549
|
+
} finally {
|
|
550
|
+
release();
|
|
551
|
+
}
|
|
552
|
+
return wrapTransaction(database, transaction, gate, (result) => {
|
|
553
|
+
if (result.lastInsertRowId != null) {
|
|
554
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
},
|
|
558
|
+
beginTransactionSync(
|
|
559
|
+
timeoutMs?: number,
|
|
560
|
+
name?: string,
|
|
561
|
+
): SynchronousSqliteTransactionDatabase {
|
|
562
|
+
const release = gate.enter();
|
|
563
|
+
let transaction: JsNativeSynchronousTransactionLike;
|
|
564
|
+
try {
|
|
565
|
+
transaction = database.beginTransactionSync(timeoutMs, name);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
enrichNativeDatabaseError(database, error);
|
|
568
|
+
} finally {
|
|
569
|
+
release();
|
|
570
|
+
}
|
|
571
|
+
return wrapTransaction(database, transaction, gate, (result) => {
|
|
572
|
+
if (result.lastInsertRowId != null) {
|
|
573
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
},
|
|
577
|
+
async beginStateTransaction(
|
|
578
|
+
timeoutMs?: number,
|
|
579
|
+
context?: unknown,
|
|
580
|
+
): Promise<SqliteTransactionDatabase> {
|
|
581
|
+
if (!database.beginStateTransaction) {
|
|
582
|
+
throw new Error("actor state transactions are not configured");
|
|
583
|
+
}
|
|
584
|
+
const release = gate.enter();
|
|
585
|
+
let transaction: JsNativeTransactionLike;
|
|
586
|
+
try {
|
|
587
|
+
transaction = await database.beginStateTransaction(
|
|
588
|
+
timeoutMs,
|
|
589
|
+
context,
|
|
590
|
+
);
|
|
591
|
+
} catch (error) {
|
|
592
|
+
enrichNativeDatabaseError(database, error);
|
|
593
|
+
} finally {
|
|
594
|
+
release();
|
|
595
|
+
}
|
|
596
|
+
return wrapTransaction(database, transaction, gate, (result) => {
|
|
597
|
+
if (result.lastInsertRowId != null) {
|
|
598
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
},
|
|
346
602
|
async run(sql: string, params?: SqliteBindings): Promise<void> {
|
|
347
603
|
await executeNative(sql, params);
|
|
348
604
|
},
|
|
@@ -353,11 +609,165 @@ export function wrapJsNativeDatabase(
|
|
|
353
609
|
nativeMetrics(): SqliteNativeMetrics | null {
|
|
354
610
|
return normalizeNativeMetrics(database.metrics?.());
|
|
355
611
|
},
|
|
612
|
+
async open(): Promise<void> {
|
|
613
|
+
const release = gate.enter();
|
|
614
|
+
try {
|
|
615
|
+
await database.open();
|
|
616
|
+
} catch (error) {
|
|
617
|
+
enrichNativeDatabaseError(database, error);
|
|
618
|
+
} finally {
|
|
619
|
+
release();
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
commitTicket(): number {
|
|
623
|
+
return database.commitTicket();
|
|
624
|
+
},
|
|
625
|
+
async whenDurable(ticket: number): Promise<void> {
|
|
626
|
+
await database.whenDurable(ticket);
|
|
627
|
+
},
|
|
628
|
+
abortGeneration(reason: string): void {
|
|
629
|
+
database.abortGeneration(reason);
|
|
630
|
+
},
|
|
356
631
|
async close(): Promise<void> {
|
|
357
|
-
|
|
358
|
-
await
|
|
632
|
+
terminalPromise ??= gate.close(() => database.close());
|
|
633
|
+
await terminalPromise;
|
|
634
|
+
},
|
|
635
|
+
async detachForShutdown(): Promise<void> {
|
|
636
|
+
terminalPromise ??= gate.close();
|
|
637
|
+
await terminalPromise;
|
|
638
|
+
},
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function wrapTransaction(
|
|
643
|
+
database: JsNativeDatabaseLike,
|
|
644
|
+
transaction: JsNativeSynchronousTransactionLike,
|
|
645
|
+
gate: NativeCloseGate,
|
|
646
|
+
onExecute: (result: NativeExecuteResult) => void,
|
|
647
|
+
): SynchronousSqliteTransactionDatabase;
|
|
648
|
+
function wrapTransaction(
|
|
649
|
+
database: JsNativeDatabaseLike,
|
|
650
|
+
transaction: JsNativeTransactionLike,
|
|
651
|
+
gate: NativeCloseGate,
|
|
652
|
+
onExecute: (result: NativeExecuteResult) => void,
|
|
653
|
+
): SqliteTransactionDatabase;
|
|
654
|
+
function wrapTransaction(
|
|
655
|
+
database: JsNativeDatabaseLike,
|
|
656
|
+
transaction: JsNativeTransactionLike,
|
|
657
|
+
gate: NativeCloseGate,
|
|
658
|
+
onExecute: (result: NativeExecuteResult) => void,
|
|
659
|
+
): SqliteTransactionDatabase {
|
|
660
|
+
const wrapped: SqliteTransactionDatabase = {
|
|
661
|
+
async exec(sql, callback) {
|
|
662
|
+
const release = gate.enter();
|
|
663
|
+
let result: NativeExecResult;
|
|
664
|
+
try {
|
|
665
|
+
result = await transaction.exec(sql);
|
|
666
|
+
} catch (error) {
|
|
667
|
+
enrichNativeDatabaseError(database, error);
|
|
668
|
+
} finally {
|
|
669
|
+
release();
|
|
670
|
+
}
|
|
671
|
+
if (callback) {
|
|
672
|
+
for (const row of result.rows) callback(row, result.columns);
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
async execute(sql, params) {
|
|
676
|
+
const release = gate.enter();
|
|
677
|
+
try {
|
|
678
|
+
const result = await transaction.execute(
|
|
679
|
+
sql,
|
|
680
|
+
toNativeBindings(sql, params),
|
|
681
|
+
);
|
|
682
|
+
onExecute(result);
|
|
683
|
+
return result;
|
|
684
|
+
} catch (error) {
|
|
685
|
+
enrichNativeDatabaseError(database, error);
|
|
686
|
+
} finally {
|
|
687
|
+
release();
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
async commit() {
|
|
691
|
+
const release = gate.enter();
|
|
692
|
+
try {
|
|
693
|
+
await transaction.commit();
|
|
694
|
+
} catch (error) {
|
|
695
|
+
enrichNativeDatabaseError(database, error);
|
|
696
|
+
} finally {
|
|
697
|
+
release();
|
|
698
|
+
}
|
|
699
|
+
},
|
|
700
|
+
async rollback() {
|
|
701
|
+
const release = gate.enter();
|
|
702
|
+
try {
|
|
703
|
+
await transaction.rollback();
|
|
704
|
+
} catch (error) {
|
|
705
|
+
enrichNativeDatabaseError(database, error);
|
|
706
|
+
} finally {
|
|
707
|
+
release();
|
|
708
|
+
}
|
|
359
709
|
},
|
|
360
710
|
};
|
|
711
|
+
|
|
712
|
+
if (isSynchronousTransaction(transaction)) {
|
|
713
|
+
return Object.assign(wrapped, {
|
|
714
|
+
execSync(
|
|
715
|
+
sql: string,
|
|
716
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
717
|
+
) {
|
|
718
|
+
const release = gate.enter();
|
|
719
|
+
let result: NativeExecResult;
|
|
720
|
+
try {
|
|
721
|
+
result = transaction.execSync(sql);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
enrichNativeDatabaseError(database, error);
|
|
724
|
+
} finally {
|
|
725
|
+
release();
|
|
726
|
+
}
|
|
727
|
+
if (callback) {
|
|
728
|
+
for (const row of result.rows)
|
|
729
|
+
callback(row, result.columns);
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
executeSync(sql: string, params?: SqliteBindings) {
|
|
733
|
+
const release = gate.enter();
|
|
734
|
+
try {
|
|
735
|
+
const result = transaction.executeSync(
|
|
736
|
+
sql,
|
|
737
|
+
toNativeBindings(sql, params),
|
|
738
|
+
);
|
|
739
|
+
onExecute(result);
|
|
740
|
+
return result;
|
|
741
|
+
} catch (error) {
|
|
742
|
+
enrichNativeDatabaseError(database, error);
|
|
743
|
+
} finally {
|
|
744
|
+
release();
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
commitSync() {
|
|
748
|
+
const release = gate.enter();
|
|
749
|
+
try {
|
|
750
|
+
transaction.commitSync();
|
|
751
|
+
} catch (error) {
|
|
752
|
+
enrichNativeDatabaseError(database, error);
|
|
753
|
+
} finally {
|
|
754
|
+
release();
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
rollbackSync() {
|
|
758
|
+
const release = gate.enter();
|
|
759
|
+
try {
|
|
760
|
+
transaction.rollbackSync();
|
|
761
|
+
} catch (error) {
|
|
762
|
+
enrichNativeDatabaseError(database, error);
|
|
763
|
+
} finally {
|
|
764
|
+
release();
|
|
765
|
+
}
|
|
766
|
+
},
|
|
767
|
+
}) as SynchronousSqliteTransactionDatabase;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
return wrapped;
|
|
361
771
|
}
|
|
362
772
|
|
|
363
773
|
function lastInsertRowIdColumnName(sql: string): string | undefined {
|
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import type { SqliteBindings } from "./config";
|
|
2
2
|
|
|
3
|
+
/** Migrations may legitimately do substantially more work than request transactions. */
|
|
4
|
+
export const MIGRATION_TRANSACTION_TIMEOUT_MS = 5 * 60_000;
|
|
5
|
+
|
|
6
|
+
export function isManualTransactionControl(query: string): boolean {
|
|
7
|
+
return /^\s*(?:BEGIN|SAVEPOINT|COMMIT|END|ROLLBACK)\b/i.test(query);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function validateTransactionTimeout(timeout: number | undefined): void {
|
|
11
|
+
if (timeout !== undefined && (!Number.isFinite(timeout) || timeout <= 0)) {
|
|
12
|
+
throw new Error(
|
|
13
|
+
"db.transaction() timeout must be a positive finite number of milliseconds",
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function validateTransactionName(name: string | undefined): void {
|
|
19
|
+
if (name === undefined) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (name.length === 0) {
|
|
23
|
+
throw new Error("db.transaction() name must not be empty");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
3
27
|
type SqliteBindingObject = Record<string, unknown>;
|
|
4
28
|
|
|
5
29
|
function isSqliteBindingValue(value: unknown): boolean {
|
package/src/common/encoding.ts
CHANGED
|
@@ -473,27 +473,37 @@ function base64DecodeToArrayBuffer(base64: string): ArrayBuffer {
|
|
|
473
473
|
}
|
|
474
474
|
|
|
475
475
|
/** Stringifies with compat for values that BARE & CBOR supports. */
|
|
476
|
-
export function jsonStringifyCompat(
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
476
|
+
export function jsonStringifyCompat(
|
|
477
|
+
input: any,
|
|
478
|
+
space?: string | number,
|
|
479
|
+
): string {
|
|
480
|
+
return JSON.stringify(
|
|
481
|
+
input,
|
|
482
|
+
(_key, value) => {
|
|
483
|
+
if (typeof value === "bigint") {
|
|
484
|
+
return [JSON_COMPAT_BIGINT, value.toString()];
|
|
485
|
+
}
|
|
486
|
+
if (value instanceof ArrayBuffer) {
|
|
487
|
+
return [
|
|
488
|
+
JSON_COMPAT_ARRAY_BUFFER,
|
|
489
|
+
base64EncodeArrayBuffer(value),
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
if (value instanceof Uint8Array) {
|
|
493
|
+
return [JSON_COMPAT_UINT8_ARRAY, base64EncodeUint8Array(value)];
|
|
494
|
+
}
|
|
495
|
+
if (
|
|
496
|
+
Array.isArray(value) &&
|
|
497
|
+
value.length === 2 &&
|
|
498
|
+
typeof value[0] === "string" &&
|
|
499
|
+
value[0].startsWith("$")
|
|
500
|
+
) {
|
|
501
|
+
return [`$${value[0]}`, value[1]];
|
|
502
|
+
}
|
|
503
|
+
return value;
|
|
504
|
+
},
|
|
505
|
+
space,
|
|
506
|
+
);
|
|
497
507
|
}
|
|
498
508
|
|
|
499
509
|
/** Parses JSON with compat for values that BARE & CBOR supports. */
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
type HeadersLike = {
|
|
2
|
+
entries(): IterableIterator<[string, string]>;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export type RequestLike = Request & {
|
|
6
|
+
readonly headers: HeadersLike & Request["headers"];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type ResponseLike = Response & {
|
|
10
|
+
readonly headers: HeadersLike & Response["headers"];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function isUrlLike(value: unknown): value is URL {
|
|
14
|
+
return (
|
|
15
|
+
typeof value === "object" &&
|
|
16
|
+
value !== null &&
|
|
17
|
+
typeof (value as URL).href === "string" &&
|
|
18
|
+
typeof (value as URL).pathname === "string" &&
|
|
19
|
+
typeof (value as URL).search === "string"
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isRequestLike(value: unknown): value is RequestLike {
|
|
24
|
+
return (
|
|
25
|
+
typeof value === "object" &&
|
|
26
|
+
value !== null &&
|
|
27
|
+
typeof (value as Request).url === "string" &&
|
|
28
|
+
typeof (value as Request).method === "string" &&
|
|
29
|
+
isHeadersLike((value as Request).headers)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isResponseLike(value: unknown): value is ResponseLike {
|
|
34
|
+
return (
|
|
35
|
+
typeof value === "object" &&
|
|
36
|
+
value !== null &&
|
|
37
|
+
typeof (value as Response).status === "number" &&
|
|
38
|
+
isHeadersLike((value as Response).headers) &&
|
|
39
|
+
typeof (value as Response).arrayBuffer === "function"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isHeadersLike(value: unknown): value is HeadersLike {
|
|
44
|
+
return (
|
|
45
|
+
typeof value === "object" &&
|
|
46
|
+
value !== null &&
|
|
47
|
+
typeof (value as HeadersLike).entries === "function"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Reads a request body into bytes. Aborting the request's signal cancels the
|
|
53
|
+
* body and rejects the read.
|
|
54
|
+
*/
|
|
55
|
+
export function readRequestBody(request: Request): Promise<ArrayBuffer> {
|
|
56
|
+
// Firefox has no Request.body.
|
|
57
|
+
if (!request.body) return request.arrayBuffer();
|
|
58
|
+
return new Response(
|
|
59
|
+
request.body.pipeThrough(new TransformStream(), {
|
|
60
|
+
signal: request.signal,
|
|
61
|
+
}),
|
|
62
|
+
).arrayBuffer();
|
|
63
|
+
}
|