@sproutboat/wire 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +70 -0
- package/package.json +2 -2
- package/src/broker.test.ts +370 -97
- package/src/broker.ts +348 -170
- package/src/d1.ts +19 -2
package/src/d1.ts
CHANGED
|
@@ -77,7 +77,10 @@ export function d1Error(error: Error): Error {
|
|
|
77
77
|
}
|
|
78
78
|
export type D1Request = { owner: string; key: string; path: string; name: string; msg: Frame };
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
/** maxOpen > 0 (Durable Object SQL, one file per object): at most that many connections stay
|
|
81
|
+
* open, and the one used longest ago is closed for a new one. A closed database reopens on
|
|
82
|
+
* its next call. */
|
|
83
|
+
export function createD1Store(executionMs = 0, capability = "", maxOpen = 0) {
|
|
81
84
|
if (executionMs && !/^[a-f0-9]{64}$/.test(capability)) throw new Error("invalid host budget capability");
|
|
82
85
|
const connections = new Map<string, { db: Database; owners: Set<string> }>();
|
|
83
86
|
// Strict calls (the shim's compatibility date 2026-10-24): the parameter count must match
|
|
@@ -145,6 +148,18 @@ export function createD1Store(executionMs = 0, capability = "") {
|
|
|
145
148
|
return {
|
|
146
149
|
execute({ owner, key, path, name, msg }: D1Request): Frame {
|
|
147
150
|
let entry = connections.get(key);
|
|
151
|
+
if (entry && maxOpen) {
|
|
152
|
+
// Most recently used last: Map iteration follows insertion order.
|
|
153
|
+
connections.delete(key);
|
|
154
|
+
connections.set(key, entry);
|
|
155
|
+
}
|
|
156
|
+
if (!entry && maxOpen && connections.size >= maxOpen) {
|
|
157
|
+
const [oldest, old] = connections.entries().next().value ?? [];
|
|
158
|
+
if (oldest !== undefined && old) {
|
|
159
|
+
connections.delete(oldest);
|
|
160
|
+
old.db.close();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
148
163
|
if (!entry) {
|
|
149
164
|
if (path !== ":memory:") mkdirSync(dirname(path), { recursive: true });
|
|
150
165
|
const db = new Database(path, { create: true });
|
|
@@ -200,7 +215,9 @@ export function createD1Store(executionMs = 0, capability = "") {
|
|
|
200
215
|
try {
|
|
201
216
|
db.exec(line);
|
|
202
217
|
} catch (error) {
|
|
203
|
-
throw new Error(
|
|
218
|
+
throw new Error(
|
|
219
|
+
`D1_EXEC_ERROR: Error in line ${i + 1}: ${line}: ${error instanceof Error ? error.message : String(error)}`,
|
|
220
|
+
);
|
|
204
221
|
}
|
|
205
222
|
}),
|
|
206
223
|
)();
|