@bastani/atomic-storage-libsql 0.9.88 → 0.9.89-alpha.2
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 +15 -0
- package/libsql.js +414 -56
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.9.89-alpha.2] - 2026-11-18
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- On a libSQL server (sqld) shared by stores that create and drop their tables, such as several sessions, workflows or teams using one server, a statement no longer fails with `SQLITE_SCHEMA: database schema has changed` when other connections change the schema while the server prepares it. The server ran nothing of such a statement, so it is sent again, as a statement the server answers busy is, for up to 5 seconds. A write outside a transaction (`INSERT`, `UPDATE`, `DELETE`, `REPLACE`, or a `WITH` that writes), or a schema change (`CREATE`, `DROP`, `ALTER`) sent alone in one `exec`, also no longer fails with `SQLITE_SCHEMA` while other connections keep changing the schema for longer than the 5-second wait. Sent again as it was, such a write could meet a changed schema on every try, and on a loaded server only two or three tries fit in the wait. After the first `SQLITE_SCHEMA` answer, such a statement is now sent again inside a write transaction of its own, in one request, with a wait of its own. While the transaction holds the write lock no other connection can change the schema, so the statement runs, and it commits once or not at all. Reads (including `VALUES` and a `WITH` that only selects) and other statements, such as pragmas, are still sent again as they were. A statement inside a transaction, and several statements sent in one `exec`, are not sent again
|
|
10
|
+
- On a libSQL server (sqld) shared by stores that create and drop their tables, a read or write outside a transaction no longer fails now and then with `SQLITE_UNKNOWN: no such table` for a table that exists. sqld gives that answer, without running the statement, when it compiles the statement while other connections change the schema; it happened to a `DELETE` from a table whose rows a trigger also removes from a full-text index, on kept and new streams alike. When the server names a table the database's schema lists, the statement now runs again with growing waits, up to 20 times; a table that does not exist is still reported at once ([#5464](https://github.com/bastani-inc/atomic/issues/5464))
|
|
11
|
+
|
|
12
|
+
## [0.9.89-alpha.1] - 2026-11-18
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- A write that follows an outage on a libSQL server (sqld) or Turso database no longer waits for the adapter's next timed resend of the close of a stream the outage cut while it held the write lock. The adapter sent such a close again only on its backoff timer, 250 ms and then twice as long each time, so an outage longer than a wait left the close for the next one, up to seconds after the server was back, while the next write waited on the server for the lock the stream still held. Now every request sends the kept closes ahead of itself. A write waits for them, so that on a server that answers them the close arrives first and frees the lock; a request made only of plain `SELECT` statements does not wait, and any other statement, including `WITH`, `PRAGMA` and `EXPLAIN` queries, is treated as a possible write and may share the same bounded wait. Writes wait at most 2 seconds in all for closes the server takes but does not answer, then go ahead and wait for those closes no more, though a close kept later is waited for again; a caller's abort ends the wait at once. A close the server refuses because the outage lost the answer to a request it had already run still frees nothing: the lock clears when the server expires the stream. The timer still sends the closes while the database sends nothing else, and only its own unanswered resends lengthen its wait, once each. Against sqld after a 300 ms outage, the next write committed 4–14 ms after the server was back instead of 455–466 ms ([#5114](https://github.com/bastani-inc/atomic/issues/5114)).
|
|
17
|
+
- Fixed a worker host crashing after a libSQL server rolled back a write batch. Batch execution now observes every statement, COMMIT and ROLLBACK result, so later timeout rejections cannot escape the reported storage failure. A batch whose COMMIT never ran is no longer reported as successful ([#4768](https://github.com/bastani-inc/atomic/issues/4768))
|
|
18
|
+
- Fixed `run`, `get` and `all` statements failing with `Storage unavailable` on `ws:` and `wss:` URLs. These statements now use WebSockets instead of attempting HTTP requests ([#4768](https://github.com/bastani-inc/atomic/issues/4768))
|
|
19
|
+
|
|
5
20
|
## [0.9.88] - 2026-11-16
|
|
6
21
|
|
|
7
22
|
### Changed
|
package/libsql.js
CHANGED
|
@@ -50,7 +50,8 @@ const REQUEST_TIMEOUT_MS = 30_000;
|
|
|
50
50
|
* The wait before the first resend of the close of a stream abandoned in an outage (#5114); each later wait doubles, up
|
|
51
51
|
* to `ABANDONED_STREAM_CLOSE_LAST_WAIT_MS`. A stream the server keeps holds its open transaction, and with it the
|
|
52
52
|
* database's write lock, until the server expires it: every writer waits for the lock meanwhile, this process's and
|
|
53
|
-
* every other's. A close the server answers, whatever the answer, is done.
|
|
53
|
+
* every other's. A close the server answers, whatever the answer, is done. Each request of the same client also sends
|
|
54
|
+
* the closes ahead of itself, whatever the wait (`AbandonedStreams.start` and `AbandonedStreams.beforeWrite`).
|
|
54
55
|
*/
|
|
55
56
|
const ABANDONED_STREAM_CLOSE_FIRST_WAIT_MS = 250;
|
|
56
57
|
/** The longest wait between two resends of abandoned stream closes. */
|
|
@@ -62,6 +63,13 @@ const ABANDONED_STREAM_CLOSE_LAST_WAIT_MS = 8_000;
|
|
|
62
63
|
const ABANDONED_STREAM_CLOSE_MAX_AGE_MS = 5 * 60_000;
|
|
63
64
|
/** How long one resend of an abandoned stream's close may go unanswered; a later resend tries again. */
|
|
64
65
|
const ABANDONED_STREAM_CLOSE_TIMEOUT_MS = 5_000;
|
|
66
|
+
/**
|
|
67
|
+
* How long writes wait, in all, for a round of abandoned stream closes the server takes but does not answer. A server
|
|
68
|
+
* that is reachable answers a close at once, without waiting for any lock; one that has not answered by then may never
|
|
69
|
+
* answer, and a write that goes ahead waits on the server for the lock as it would have anyway. Writes wait no more
|
|
70
|
+
* until the server answers a close again.
|
|
71
|
+
*/
|
|
72
|
+
const ABANDONED_STREAM_CLOSE_WRITE_WAIT_MS = 2_000;
|
|
65
73
|
/** The most abandoned stream closes kept for resending; the oldest is dropped past it, its stream long expired. */
|
|
66
74
|
const MAX_ABANDONED_STREAM_CLOSES = 16;
|
|
67
75
|
|
|
@@ -124,6 +132,53 @@ async function closesNoStream(request) {
|
|
|
124
132
|
return closesStream(body, true);
|
|
125
133
|
}
|
|
126
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Whether `request`, a Hrana pipeline or cursor request, may take the database's write lock, so it would wait on the
|
|
137
|
+
* server behind a lock an abandoned stream holds: it runs a statement that is not one `SELECT`, a sequence of
|
|
138
|
+
* statements, or SQL it stored earlier. A request without a body, such as a version check, takes no lock; one whose body
|
|
139
|
+
* is not JSON counts as a write.
|
|
140
|
+
* @param {Request} request an unread copy
|
|
141
|
+
*/
|
|
142
|
+
async function mayWrite(request) {
|
|
143
|
+
const text = await request.text();
|
|
144
|
+
if (text === "") return false;
|
|
145
|
+
/** @type {{ requests?: unknown, batch?: { steps?: unknown } }} */
|
|
146
|
+
let body;
|
|
147
|
+
try {
|
|
148
|
+
body = JSON.parse(text);
|
|
149
|
+
} catch {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
/** @type {unknown[]} */
|
|
153
|
+
const statements = [];
|
|
154
|
+
const steps = (/** @type {unknown} */ batch) =>
|
|
155
|
+
Array.isArray(batch) ? batch.map((step) => /** @type {{ stmt?: unknown }} */ (step)?.stmt) : [];
|
|
156
|
+
for (const each of Array.isArray(body?.requests) ? body.requests : []) {
|
|
157
|
+
const { type, stmt, batch } = /** @type {{ type?: unknown, stmt?: unknown, batch?: { steps?: unknown } }} */ (each ?? {});
|
|
158
|
+
if (type === "sequence") return true;
|
|
159
|
+
if (type === "execute") statements.push(stmt);
|
|
160
|
+
else if (type === "batch") statements.push(...steps(batch?.steps));
|
|
161
|
+
}
|
|
162
|
+
statements.push(...steps(body?.batch?.steps));
|
|
163
|
+
return statements.some((stmt) => !readsOnly(/** @type {{ sql?: unknown }} */ (stmt)?.sql));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The keyword of a statement that only reads. */
|
|
167
|
+
const READ_KEYWORD = /SELECT\b/iy;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Whether `sql` is one `SELECT`, which never takes the write lock. SQL that is not text, as a statement stored earlier
|
|
171
|
+
* leaves it, may write.
|
|
172
|
+
* @param {unknown} sql
|
|
173
|
+
*/
|
|
174
|
+
function readsOnly(sql) {
|
|
175
|
+
if (typeof sql !== "string") return false;
|
|
176
|
+
const start = firstTokenStart(sql);
|
|
177
|
+
if (start === -1) return false;
|
|
178
|
+
READ_KEYWORD.lastIndex = start;
|
|
179
|
+
return READ_KEYWORD.test(sql) && isOneStatement(sql);
|
|
180
|
+
}
|
|
181
|
+
|
|
127
182
|
/** The answer a server gives a request that only closes a stream. */
|
|
128
183
|
function closedResponse() {
|
|
129
184
|
return new Response(JSON.stringify({ baton: null, base_url: null, results: [{ type: "ok", response: { type: "close" } }] }), {
|
|
@@ -136,15 +191,27 @@ function closedResponse() {
|
|
|
136
191
|
* The closes of streams this client abandoned that never reached the server, sent again until the server answers one.
|
|
137
192
|
* The HTTP client sends a stream's close once, when a request of the stream fails; in an outage that close fails too,
|
|
138
193
|
* and the server keeps the stream, with its open transaction and the write lock it holds, for the stream's whole
|
|
139
|
-
* lifetime. Sending the close again as soon as the server is reachable ends the transaction there and frees the lock
|
|
194
|
+
* lifetime. Sending the close again as soon as the server is reachable ends the transaction there and frees the lock:
|
|
195
|
+
* a timer sends the closes with growing waits while the client sends nothing else, and each request the client sends
|
|
196
|
+
* sends them ahead of itself. A write waits for them (`beforeWrite`), so it does not queue on the server behind a lock
|
|
197
|
+
* its own client's abandoned stream holds; any other request goes at once.
|
|
140
198
|
*/
|
|
141
199
|
class AbandonedStreams {
|
|
142
200
|
/** @type {{ url: string, headers: [string, string][], body: ArrayBuffer, since: number }[]} */
|
|
143
201
|
pending = [];
|
|
144
202
|
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
145
203
|
timer;
|
|
146
|
-
/**
|
|
204
|
+
/** Whether a round the timer started, or joined, is under way: the timer plans the next one when it ends. */
|
|
205
|
+
timerRound = false;
|
|
206
|
+
/** @type {Promise<boolean> | undefined} */
|
|
147
207
|
sending;
|
|
208
|
+
/** When the writes waiting for the round under way stop waiting for it. @type {number | undefined} */
|
|
209
|
+
waitUntil;
|
|
210
|
+
/**
|
|
211
|
+
* Whether a round left the writes waiting for it unanswered: writes wait no more for the closes kept then, until one is
|
|
212
|
+
* answered, given up or joined by a newly kept close.
|
|
213
|
+
*/
|
|
214
|
+
stalled = false;
|
|
148
215
|
wait = ABANDONED_STREAM_CLOSE_FIRST_WAIT_MS;
|
|
149
216
|
closed = false;
|
|
150
217
|
|
|
@@ -156,30 +223,83 @@ class AbandonedStreams {
|
|
|
156
223
|
const body = await request.arrayBuffer();
|
|
157
224
|
if (this.closed || !closesStream(new Uint8Array(body))) return;
|
|
158
225
|
this.pending.push({ url: request.url, headers: [...request.headers], body, since: Date.now() });
|
|
226
|
+
// A stall belongs to the closes it was found for: a newly kept close, whose stream may hold the lock now, is waited for.
|
|
227
|
+
this.stalled = false;
|
|
159
228
|
if (this.pending.length > MAX_ABANDONED_STREAM_CLOSES) this.pending.shift();
|
|
160
229
|
this.schedule();
|
|
161
230
|
}
|
|
162
231
|
|
|
163
|
-
/**
|
|
232
|
+
/**
|
|
233
|
+
* Sends the kept closes again after the current wait, unless a resend is already planned or the timer's round is under
|
|
234
|
+
* way. A round the timer started that leaves a close unanswered doubles the wait before the next one, once; a round a
|
|
235
|
+
* request started, which the timer joined, leaves it as it is.
|
|
236
|
+
*/
|
|
164
237
|
schedule() {
|
|
165
|
-
if (this.timer !== undefined || this.pending.length === 0) return;
|
|
238
|
+
if (this.timer !== undefined || this.timerRound || this.pending.length === 0) return;
|
|
166
239
|
this.timer = setTimeout(() => {
|
|
167
240
|
this.timer = undefined;
|
|
168
|
-
|
|
241
|
+
this.timerRound = true;
|
|
242
|
+
const joined = this.sending !== undefined;
|
|
243
|
+
void this.send().then((answered) => {
|
|
244
|
+
this.timerRound = false;
|
|
245
|
+
if (!answered && !joined) this.wait = Math.min(this.wait * 2, ABANDONED_STREAM_CLOSE_LAST_WAIT_MS);
|
|
246
|
+
this.schedule();
|
|
247
|
+
});
|
|
169
248
|
}, this.wait);
|
|
170
249
|
// A close waiting to be sent again never keeps the process alive.
|
|
171
250
|
this.timer.unref();
|
|
172
251
|
}
|
|
173
252
|
|
|
253
|
+
/** Sends the kept closes ahead of a request that does not wait for them, unless a round is under way. */
|
|
254
|
+
start() {
|
|
255
|
+
if (this.pending.length > 0) void this.send();
|
|
256
|
+
}
|
|
257
|
+
|
|
174
258
|
/**
|
|
175
|
-
* Sends
|
|
176
|
-
*
|
|
177
|
-
*
|
|
259
|
+
* Sends the kept closes ahead of a write, unless a round is under way, and waits for the round. A server reachable
|
|
260
|
+
* again would otherwise take the write while an abandoned stream still holds the write lock, and the write would wait
|
|
261
|
+
* for that lock until the next timed resend. The writes waiting for one round wait for it at most
|
|
262
|
+
* `ABANDONED_STREAM_CLOSE_WRITE_WAIT_MS` in all; a round still unanswered then stalls the closes, and no write waits
|
|
263
|
+
* for them again until the server answers one, they are given up, or another close is kept. A round that fails at once costs nothing: the write finds the server
|
|
264
|
+
* unreachable on its own. Rejects with the reason of `signal` when it aborts first; the round goes on.
|
|
265
|
+
* @param {AbortSignal} signal
|
|
266
|
+
*/
|
|
267
|
+
async beforeWrite(signal) {
|
|
268
|
+
if (this.pending.length === 0) return;
|
|
269
|
+
signal.throwIfAborted();
|
|
270
|
+
const round = this.send();
|
|
271
|
+
if (this.stalled) return;
|
|
272
|
+
this.waitUntil ??= performance.now() + ABANDONED_STREAM_CLOSE_WRITE_WAIT_MS;
|
|
273
|
+
const left = this.waitUntil - performance.now();
|
|
274
|
+
/** @type {ReturnType<typeof setTimeout> | undefined} */
|
|
275
|
+
let timer;
|
|
276
|
+
/** @type {(() => void) | undefined} */
|
|
277
|
+
let abort;
|
|
278
|
+
try {
|
|
279
|
+
const late = await new Promise((resolve, reject) => {
|
|
280
|
+
abort = () => reject(signal.reason);
|
|
281
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
282
|
+
timer = setTimeout(() => resolve(true), Math.max(left, 0));
|
|
283
|
+
void round.then(() => resolve(false));
|
|
284
|
+
});
|
|
285
|
+
if (late && this.sending === round) this.stalled = true;
|
|
286
|
+
} finally {
|
|
287
|
+
clearTimeout(timer);
|
|
288
|
+
if (abort !== undefined) signal.removeEventListener("abort", abort);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Sends each kept close again, in order, until one goes unanswered; an answered close is done, whatever it says, and
|
|
294
|
+
* resets the wait and any stall. A close older than `ABANDONED_STREAM_CLOSE_MAX_AGE_MS` is given up. Joins the round
|
|
295
|
+
* under way, if any. Resolves whether every kept close was answered.
|
|
296
|
+
* @returns {Promise<boolean>}
|
|
178
297
|
*/
|
|
179
298
|
send() {
|
|
180
299
|
this.sending ??= (async () => {
|
|
181
300
|
const oldest = Date.now() - ABANDONED_STREAM_CLOSE_MAX_AGE_MS;
|
|
182
301
|
this.pending = this.pending.filter((close) => close.since > oldest);
|
|
302
|
+
if (this.pending.length === 0) this.stalled = false;
|
|
183
303
|
while (this.pending.length > 0) {
|
|
184
304
|
const [close] = this.pending;
|
|
185
305
|
try {
|
|
@@ -191,14 +311,16 @@ class AbandonedStreams {
|
|
|
191
311
|
});
|
|
192
312
|
await response.arrayBuffer().catch(() => {});
|
|
193
313
|
} catch {
|
|
194
|
-
|
|
195
|
-
return;
|
|
314
|
+
return false;
|
|
196
315
|
}
|
|
197
316
|
this.wait = ABANDONED_STREAM_CLOSE_FIRST_WAIT_MS;
|
|
317
|
+
this.stalled = false;
|
|
198
318
|
this.pending = this.pending.filter((kept) => kept !== close);
|
|
199
319
|
}
|
|
320
|
+
return true;
|
|
200
321
|
})().finally(() => {
|
|
201
322
|
this.sending = undefined;
|
|
323
|
+
this.waitUntil = undefined;
|
|
202
324
|
});
|
|
203
325
|
return this.sending;
|
|
204
326
|
}
|
|
@@ -213,8 +335,9 @@ class AbandonedStreams {
|
|
|
213
335
|
}
|
|
214
336
|
|
|
215
337
|
/**
|
|
216
|
-
* `fetch` that adds `headers` to every request the libSQL HTTP client sends,
|
|
217
|
-
* `REQUEST_TIMEOUT_MS`, and hands a stream's close
|
|
338
|
+
* `fetch` that adds `headers` to every request the libSQL HTTP client sends, sends the closes `abandoned` keeps ahead of
|
|
339
|
+
* it (a write waits for them), abandons a request after `REQUEST_TIMEOUT_MS`, wait included, and hands a stream's close
|
|
340
|
+
* that failed to `abandoned` for resending.
|
|
218
341
|
* @param {[string, string][]} headers
|
|
219
342
|
* @param {AbandonedStreams} abandoned
|
|
220
343
|
* @returns {(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>}
|
|
@@ -226,8 +349,14 @@ function fetchWith(headers, abandoned) {
|
|
|
226
349
|
const copy = request.clone();
|
|
227
350
|
if (await closesNoStream(request.clone())) return closedResponse();
|
|
228
351
|
const timeout = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
|
|
352
|
+
// The caller's signal: the one `init` passes, or else the one `input`, a request, carries.
|
|
353
|
+
const signal = AbortSignal.any([request.signal, timeout]);
|
|
354
|
+
if (abandoned.pending.length > 0) {
|
|
355
|
+
if (await mayWrite(request.clone())) await abandoned.beforeWrite(signal);
|
|
356
|
+
else abandoned.start();
|
|
357
|
+
}
|
|
229
358
|
try {
|
|
230
|
-
return await fetch(request, { signal
|
|
359
|
+
return await fetch(request, { signal });
|
|
231
360
|
} catch (error) {
|
|
232
361
|
await abandoned.failed(copy).catch(() => {});
|
|
233
362
|
throw error;
|
|
@@ -280,6 +409,17 @@ const STREAM_CLOSE_WAIT_MS = 1_000;
|
|
|
280
409
|
*/
|
|
281
410
|
const SCHEMA_CHECK = "SELECT 1 FROM sqlite_schema LIMIT 0";
|
|
282
411
|
|
|
412
|
+
/** The table a server's `no such table` answer names, at the end of its message. */
|
|
413
|
+
const MISSING_TABLE = /no such table: (\S+)$/;
|
|
414
|
+
/** A read of the schema itself, not of the connection's copy of it, for a table or view of the main database. */
|
|
415
|
+
const TABLE_LOOKUP = "SELECT 1 FROM sqlite_schema WHERE type IN ('table', 'view') AND name = ? COLLATE NOCASE";
|
|
416
|
+
/**
|
|
417
|
+
* How many times a statement the server answered `no such table` for a table that exists runs again. Each answer is
|
|
418
|
+
* an independent loss of a race with other connections' schema changes, so the bound counts runs, not time: a server
|
|
419
|
+
* slowed by load answers each run late, yet each run loses as rarely.
|
|
420
|
+
*/
|
|
421
|
+
const MISSING_TABLE_RERUNS = 20;
|
|
422
|
+
|
|
283
423
|
/**
|
|
284
424
|
* What sqld answers, as plain text with HTTP 400, for a request whose baton it refuses when it looks the stream up,
|
|
285
425
|
* before it runs any request of the pipeline (`stream::acquire` and `Guard::get_db` in sqld's
|
|
@@ -360,10 +500,13 @@ function hranaCode(hrana, error) {
|
|
|
360
500
|
* tables; a stream kept open loads it once. A stream that failed, sat idle `STREAM_REUSE_IDLE_MS` or lived
|
|
361
501
|
* `STREAM_REUSE_MAX_AGE_MS` is closed and a new one opened. A statement is sent again only when the server refused its
|
|
362
502
|
* request without running it: it found the stream expired, or refused its baton (`REFUSED_BATON`). A request whose
|
|
363
|
-
* answer was lost may have run, so its failure is the caller's, as it is for a statement on a stream of its own.
|
|
503
|
+
* answer was lost may have run, so its failure is the caller's, as it is for a statement on a stream of its own. A
|
|
504
|
+
* statement the server answered `no such table` for a table the schema holds runs again, with growing waits, up to
|
|
505
|
+
* `MISSING_TABLE_RERUNS` times (#5464): the server compiled it while other connections changed the schema and ran none
|
|
506
|
+
* of it.
|
|
364
507
|
*/
|
|
365
508
|
class ReusedStream {
|
|
366
|
-
/** @type {import("@libsql/hrana-client").
|
|
509
|
+
/** @type {import("@libsql/hrana-client").Stream | undefined} */
|
|
367
510
|
stream;
|
|
368
511
|
openedAt = 0;
|
|
369
512
|
usedAt = 0;
|
|
@@ -373,7 +516,7 @@ class ReusedStream {
|
|
|
373
516
|
/**
|
|
374
517
|
* @param {typeof import("@libsql/hrana-client")} hrana
|
|
375
518
|
* @param {typeof import("@libsql/core/api").LibsqlError} LibsqlError
|
|
376
|
-
* @param {() => import("@libsql/hrana-client").
|
|
519
|
+
* @param {() => import("@libsql/hrana-client").Client} client
|
|
377
520
|
*/
|
|
378
521
|
constructor(hrana, LibsqlError, client) {
|
|
379
522
|
this.hrana = hrana;
|
|
@@ -391,7 +534,9 @@ class ReusedStream {
|
|
|
391
534
|
if (this.busy) return undefined;
|
|
392
535
|
this.busy = true;
|
|
393
536
|
try {
|
|
394
|
-
|
|
537
|
+
let resent = false;
|
|
538
|
+
let reruns = 0;
|
|
539
|
+
for (let wait = SERVER_BUSY_FIRST_WAIT_MS; ; ) {
|
|
395
540
|
const now = performance.now();
|
|
396
541
|
const kept = this.stream;
|
|
397
542
|
if (
|
|
@@ -420,7 +565,15 @@ class ReusedStream {
|
|
|
420
565
|
};
|
|
421
566
|
} catch (error) {
|
|
422
567
|
if (stream.closed && this.stream === stream) this.stream = undefined;
|
|
423
|
-
if (fresh
|
|
568
|
+
if (!fresh && !resent && this.ranNothing(error)) {
|
|
569
|
+
resent = true;
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
if (reruns >= MISSING_TABLE_RERUNS || !(await this.namesExistingTable(stream, error)))
|
|
573
|
+
throw this.libsqlError(error);
|
|
574
|
+
reruns++;
|
|
575
|
+
await new Promise((resolve) => setTimeout(resolve, wait));
|
|
576
|
+
wait = Math.min(wait * 2, SERVER_BUSY_LAST_WAIT_MS);
|
|
424
577
|
} finally {
|
|
425
578
|
this.usedAt = performance.now();
|
|
426
579
|
}
|
|
@@ -445,6 +598,26 @@ class ReusedStream {
|
|
|
445
598
|
);
|
|
446
599
|
}
|
|
447
600
|
|
|
601
|
+
/**
|
|
602
|
+
* Whether `error` is the server's answer `no such table` for a table that `stream` finds in the schema. sqld gives
|
|
603
|
+
* that answer, without running the statement, when it prepares the statement while other connections change the
|
|
604
|
+
* schema; the same statement sent again finds the table (#5464). A table that does not exist is reported.
|
|
605
|
+
* @param {import("@libsql/hrana-client").Stream} stream
|
|
606
|
+
* @param {unknown} error
|
|
607
|
+
*/
|
|
608
|
+
async namesExistingTable(stream, error) {
|
|
609
|
+
if (!(error instanceof this.hrana.ResponseError) || stream.closed) return false;
|
|
610
|
+
const named = MISSING_TABLE.exec(error.message)?.[1];
|
|
611
|
+
if (named === undefined) return false;
|
|
612
|
+
try {
|
|
613
|
+
const lookup = new this.hrana.Stmt(TABLE_LOOKUP);
|
|
614
|
+
lookup.bindIndexes([named.replace(/^main\./i, "")]);
|
|
615
|
+
return (await stream.query(lookup)).rows.length > 0;
|
|
616
|
+
} catch {
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
448
621
|
/**
|
|
449
622
|
* `error` as `@libsql/client` reports it.
|
|
450
623
|
* @param {unknown} error
|
|
@@ -462,27 +635,163 @@ class ReusedStream {
|
|
|
462
635
|
}
|
|
463
636
|
}
|
|
464
637
|
|
|
638
|
+
/**
|
|
639
|
+
* @template T
|
|
640
|
+
* @param {Promise<T>} promise
|
|
641
|
+
* @returns {Promise<{ ok: true, value: T } | { ok: false, error: unknown }>}
|
|
642
|
+
*/
|
|
643
|
+
function settle(promise) {
|
|
644
|
+
return promise.then(
|
|
645
|
+
(value) => ({ ok: true, value }),
|
|
646
|
+
(error) => ({ ok: false, error }),
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* @param {typeof import("@libsql/hrana-client")} hrana
|
|
652
|
+
* @param {typeof import("@libsql/core/api")} api
|
|
653
|
+
* @param {import("@libsql/hrana-client").Client} client
|
|
654
|
+
* @param {readonly import("@bastani/atomic/storage").SqliteStatement[]} batched
|
|
655
|
+
* @returns {Promise<import("@bastani/atomic/storage").SqliteDriverResult[]>}
|
|
656
|
+
*/
|
|
657
|
+
async function writeBatch(hrana, api, client, batched) {
|
|
658
|
+
/** @param {unknown} error */
|
|
659
|
+
const mapped = (error) =>
|
|
660
|
+
error instanceof hrana.ClientError
|
|
661
|
+
? new api.LibsqlError(error.message, hranaCode(hrana, error), undefined, undefined, error)
|
|
662
|
+
: error;
|
|
663
|
+
const version = await client.getVersion();
|
|
664
|
+
const stream = client.openStream();
|
|
665
|
+
const { began, ran, committed, executed } = (() => {
|
|
666
|
+
try {
|
|
667
|
+
const batch = stream.batch(false);
|
|
668
|
+
const inTransaction = () => hrana.BatchCond.not(hrana.BatchCond.isAutocommit(batch));
|
|
669
|
+
const begin = batch.step();
|
|
670
|
+
const began = settle(begin.run("BEGIN IMMEDIATE"));
|
|
671
|
+
let last = begin;
|
|
672
|
+
const ran = batched.map(({ sql, params }) => {
|
|
673
|
+
const step = batch.step().condition(hrana.BatchCond.ok(last));
|
|
674
|
+
if (version >= 3) step.condition(inTransaction());
|
|
675
|
+
const statement = new hrana.Stmt(sql);
|
|
676
|
+
statement.bindIndexes(/** @type {import("@libsql/hrana-client").InValue[]} */ ([...params]));
|
|
677
|
+
last = step;
|
|
678
|
+
return settle(step.query(statement));
|
|
679
|
+
});
|
|
680
|
+
const commit = batch.step().condition(hrana.BatchCond.ok(last));
|
|
681
|
+
if (version >= 3) commit.condition(inTransaction());
|
|
682
|
+
const committed = settle(commit.run("COMMIT"));
|
|
683
|
+
void settle(batch.step().condition(hrana.BatchCond.not(hrana.BatchCond.ok(commit))).run("ROLLBACK"));
|
|
684
|
+
return { began, ran, committed, executed: batch.execute() };
|
|
685
|
+
} finally {
|
|
686
|
+
stream.closeGracefully();
|
|
687
|
+
}
|
|
688
|
+
})();
|
|
689
|
+
try {
|
|
690
|
+
await executed;
|
|
691
|
+
} catch (error) {
|
|
692
|
+
throw mapped(error);
|
|
693
|
+
}
|
|
694
|
+
const beginning = await began;
|
|
695
|
+
if (!beginning.ok) throw mapped(beginning.error);
|
|
696
|
+
/** @type {import("@bastani/atomic/storage").SqliteDriverResult[]} */
|
|
697
|
+
const results = [];
|
|
698
|
+
for (const [index, outcome] of (await Promise.all(ran)).entries()) {
|
|
699
|
+
if (!outcome.ok) {
|
|
700
|
+
const failure = mapped(outcome.error);
|
|
701
|
+
if (!(failure instanceof api.LibsqlError)) throw failure;
|
|
702
|
+
throw new api.LibsqlBatchError(
|
|
703
|
+
failure.message,
|
|
704
|
+
index,
|
|
705
|
+
failure.code,
|
|
706
|
+
failure.extendedCode,
|
|
707
|
+
failure.rawCode,
|
|
708
|
+
outcome.error instanceof Error ? outcome.error : undefined,
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
if (outcome.value === undefined)
|
|
712
|
+
throw new api.LibsqlBatchError(
|
|
713
|
+
"Statement in a batch was not executed, probably because the transaction has been rolled back",
|
|
714
|
+
index,
|
|
715
|
+
"TRANSACTION_CLOSED",
|
|
716
|
+
);
|
|
717
|
+
results.push({
|
|
718
|
+
columns: outcome.value.columnNames.map((name) => name ?? ""),
|
|
719
|
+
rows: /** @type {import("@bastani/atomic/storage").SqliteDriverResult["rows"]} */ (outcome.value.rows),
|
|
720
|
+
changes: outcome.value.affectedRowCount,
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
const commitment = await committed;
|
|
724
|
+
if (!commitment.ok) throw mapped(commitment.error);
|
|
725
|
+
if (commitment.value === undefined)
|
|
726
|
+
throw new api.LibsqlError(
|
|
727
|
+
"The batch did not execute its COMMIT; its transaction ended before completion could be confirmed",
|
|
728
|
+
"TRANSACTION_CLOSED",
|
|
729
|
+
);
|
|
730
|
+
return results;
|
|
731
|
+
}
|
|
732
|
+
|
|
465
733
|
/**
|
|
466
734
|
* @param {() => Promise<import("@libsql/client").Client | import("@libsql/client").Transaction>} connection
|
|
467
735
|
* @param {<T>(operation: () => Promise<T>) => Promise<T>} watch
|
|
468
|
-
* @param {<T>(operation: () => Promise<T>) => Promise<T>} [single] runs one
|
|
736
|
+
* @param {<T>(operation: () => Promise<T>, afterSchemaChange?: () => Promise<T>) => Promise<T>} [single] runs one
|
|
737
|
+
* statement, which a failure may run again; after `SQLITE_SCHEMA` it may run `afterSchemaChange` in its place
|
|
469
738
|
* @param {() => Promise<ReusedStream | undefined>} [shared] the stream statements outside a transaction reuse
|
|
739
|
+
* @param {(batched: readonly import("@bastani/atomic/storage").SqliteStatement[]) => Promise<import("@bastani/atomic/storage").SqliteDriverResult[]>} [locked]
|
|
740
|
+
* runs statements in one write transaction, where no other connection can change the schema
|
|
470
741
|
* @returns {import("@bastani/atomic/storage").SqliteDriverExecutor}
|
|
471
742
|
*/
|
|
472
|
-
function statements(connection, watch, single = watch, shared = async () => undefined) {
|
|
743
|
+
function statements(connection, watch, single = watch, shared = async () => undefined, locked) {
|
|
473
744
|
return {
|
|
474
745
|
execute: (sql, params) =>
|
|
475
|
-
single(
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
746
|
+
single(
|
|
747
|
+
async () => {
|
|
748
|
+
const reused = reusable(sql) ? await (await shared())?.execute(sql, params) : undefined;
|
|
749
|
+
return reused ?? driverResult(await (await connection()).execute({ sql, args: [...params] }));
|
|
750
|
+
},
|
|
751
|
+
// A write that commits on its own and that the server answered `SQLITE_SCHEMA` runs again in a write
|
|
752
|
+
// transaction of its own: no other connection changes the schema while the transaction holds the write lock,
|
|
753
|
+
// so sent as it was it could keep meeting a changed schema for as long as other connections keep changing it.
|
|
754
|
+
locked !== undefined && rerunsLocked(sql) ? async () => (await locked([{ sql, params }]))[0] : undefined,
|
|
755
|
+
),
|
|
479
756
|
// An `exec` of one statement wrote nothing when it fails, as one `execute` did; one of several statements may
|
|
480
757
|
// fail after the statements before it committed.
|
|
481
758
|
executeMultiple: (sql) =>
|
|
482
|
-
|
|
759
|
+
isOneStatement(sql)
|
|
760
|
+
? single(
|
|
761
|
+
async () => (await connection()).executeMultiple(sql),
|
|
762
|
+
locked !== undefined && rerunsLocked(sql)
|
|
763
|
+
? async () => {
|
|
764
|
+
await locked([{ sql, params: [] }]);
|
|
765
|
+
}
|
|
766
|
+
: undefined,
|
|
767
|
+
)
|
|
768
|
+
: watch(async () => (await connection()).executeMultiple(sql)),
|
|
483
769
|
};
|
|
484
770
|
}
|
|
485
771
|
|
|
772
|
+
/** The keyword of a statement that writes and runs the same inside a write transaction as on its own. */
|
|
773
|
+
const LOCKED_RERUN_KEYWORD = /(?:INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|ALTER)\b/iy;
|
|
774
|
+
/** The keyword of a statement that starts with common table expressions, which may read or write. */
|
|
775
|
+
const COMMON_TABLE_KEYWORD = /WITH\b/iy;
|
|
776
|
+
/** A write inside a statement that starts with common table expressions. */
|
|
777
|
+
const COMMON_TABLE_WRITE = /\b(?:INSERT|UPDATE|DELETE|REPLACE)\b/i;
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Whether `sql`, one statement the server answered `SQLITE_SCHEMA`, runs again in a write transaction of its own: a
|
|
781
|
+
* write (also one that starts with common table expressions) or a schema change. Not a read, such as `SELECT`,
|
|
782
|
+
* `VALUES` or a `WITH` that only selects, and not a statement that runs differently in a transaction or not at all, as
|
|
783
|
+
* `PRAGMA foreign_keys` and `VACUUM` do.
|
|
784
|
+
* @param {string} sql
|
|
785
|
+
*/
|
|
786
|
+
function rerunsLocked(sql) {
|
|
787
|
+
const start = firstTokenStart(sql);
|
|
788
|
+
if (start === -1 || readsOnly(sql) || !isOneStatement(sql)) return false;
|
|
789
|
+
LOCKED_RERUN_KEYWORD.lastIndex = start;
|
|
790
|
+
if (LOCKED_RERUN_KEYWORD.test(sql)) return true;
|
|
791
|
+
COMMON_TABLE_KEYWORD.lastIndex = start;
|
|
792
|
+
return COMMON_TABLE_KEYWORD.test(sql) && COMMON_TABLE_WRITE.test(sql.slice(start));
|
|
793
|
+
}
|
|
794
|
+
|
|
486
795
|
/**
|
|
487
796
|
* Whether `sql` is one statement: it has no `;` but at its end. A `;` in a literal or a comment also counts, so
|
|
488
797
|
* several statements are never taken for one.
|
|
@@ -509,9 +818,10 @@ function driverResult(result) {
|
|
|
509
818
|
const FILE_BUSY_TIMEOUT_MS = 5000;
|
|
510
819
|
|
|
511
820
|
/**
|
|
512
|
-
* How long a call to a libSQL server runs again while the server answers it `SQLITE_BUSY`. sqld
|
|
513
|
-
* meets its checkpoint `SQLITE_BUSY` at once, without waiting and without running any of the
|
|
514
|
-
* waiting to the client; its own checkpoint waits this long for the lock.
|
|
821
|
+
* How long a call to a libSQL server runs again while the server answers it `SQLITE_BUSY` or `SQLITE_SCHEMA`. sqld
|
|
822
|
+
* answers a writer that meets its checkpoint `SQLITE_BUSY` at once, without waiting and without running any of the
|
|
823
|
+
* call, and leaves the waiting to the client; its own checkpoint waits this long for the lock. It answers
|
|
824
|
+
* `SQLITE_SCHEMA`, also without running the call, while other connections keep changing the schema.
|
|
515
825
|
*/
|
|
516
826
|
const SERVER_BUSY_TIMEOUT_MS = 5000;
|
|
517
827
|
/** The first wait before a call a libSQL server answered busy runs again; each later wait doubles, up to the last. */
|
|
@@ -520,22 +830,28 @@ const SERVER_BUSY_LAST_WAIT_MS = 200;
|
|
|
520
830
|
|
|
521
831
|
/**
|
|
522
832
|
* Runs `operation` again, with growing waits, while it fails with `SQLITE_BUSY` and `ranNothing` says the failed run
|
|
523
|
-
* ran nothing, until `SERVER_BUSY_TIMEOUT_MS` has passed; then it fails with the last run's
|
|
524
|
-
* before each run after the first.
|
|
833
|
+
* ran nothing, or with `SQLITE_SCHEMA`, until `SERVER_BUSY_TIMEOUT_MS` has passed; then it fails with the last run's
|
|
834
|
+
* failure. `prepare` runs before each run after the first.
|
|
835
|
+
*
|
|
836
|
+
* A server answers `SQLITE_SCHEMA` when other connections kept changing the database's schema while it prepared the
|
|
837
|
+
* statement, as on a server shared by several stores that create and drop their tables: the statement never ran, so
|
|
838
|
+
* a statement or batch that commits on its own runs again. `rerunsAfterSchemaChange` says whether the run may.
|
|
525
839
|
* @template T
|
|
526
840
|
* @param {() => Promise<T>} operation
|
|
527
841
|
* @param {() => boolean} ranNothing
|
|
528
842
|
* @param {() => Promise<void>} [prepare]
|
|
843
|
+
* @param {() => boolean} [rerunsAfterSchemaChange] whether a run that `SQLITE_SCHEMA` ended may run again
|
|
529
844
|
* @returns {Promise<T>}
|
|
530
845
|
*/
|
|
531
|
-
async function whileServerBusy(operation, ranNothing, prepare) {
|
|
846
|
+
async function whileServerBusy(operation, ranNothing, prepare, rerunsAfterSchemaChange = () => true) {
|
|
532
847
|
const deadline = performance.now() + SERVER_BUSY_TIMEOUT_MS;
|
|
533
848
|
for (let wait = SERVER_BUSY_FIRST_WAIT_MS; ; wait = Math.min(wait * 2, SERVER_BUSY_LAST_WAIT_MS)) {
|
|
534
849
|
try {
|
|
535
850
|
return await operation();
|
|
536
851
|
} catch (error) {
|
|
537
852
|
const left = deadline - performance.now();
|
|
538
|
-
|
|
853
|
+
const rerun = isBusy(error) ? ranNothing() : isSchemaChanged(error) && rerunsAfterSchemaChange();
|
|
854
|
+
if (!rerun || left <= 0) throw error;
|
|
539
855
|
await new Promise((resolve) => setTimeout(resolve, Math.min(wait, left)));
|
|
540
856
|
await prepare?.();
|
|
541
857
|
}
|
|
@@ -550,6 +866,14 @@ function isBusy(error) {
|
|
|
550
866
|
return /** @type {{ readonly code?: unknown } | undefined} */ (error)?.code === "SQLITE_BUSY";
|
|
551
867
|
}
|
|
552
868
|
|
|
869
|
+
/**
|
|
870
|
+
* Whether a server refused a statement because the schema it was prepared against kept changing.
|
|
871
|
+
* @param {unknown} error
|
|
872
|
+
*/
|
|
873
|
+
function isSchemaChanged(error) {
|
|
874
|
+
return /** @type {{ readonly code?: unknown } | undefined} */ (error)?.code === "SQLITE_SCHEMA";
|
|
875
|
+
}
|
|
876
|
+
|
|
553
877
|
/** V8's `gc`, taken from a context created while `--expose-gc` is set; the flag is off again for code that runs later. */
|
|
554
878
|
function exposeGc() {
|
|
555
879
|
setFlagsFromString("--expose-gc");
|
|
@@ -890,18 +1214,19 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
890
1214
|
});
|
|
891
1215
|
// The Hrana client whose stream statements outside a transaction reuse, made on first use inside a driver call, as the
|
|
892
1216
|
// client is, so the facade redacts credentials from a failure to read the URL.
|
|
893
|
-
/** @type {import("@libsql/hrana-client").
|
|
1217
|
+
/** @type {import("@libsql/hrana-client").Client | undefined} */
|
|
894
1218
|
let hranaClient;
|
|
895
1219
|
/** @type {ReusedStream | undefined} */
|
|
896
1220
|
let reused;
|
|
897
1221
|
/** The requests of the reused stream's client not yet answered, which a close waits for. @type {Set<Promise<Response>>} */
|
|
898
1222
|
const streamRequests = new Set();
|
|
899
|
-
const
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1223
|
+
const remote = () => {
|
|
1224
|
+
const [hrana, { expandConfig }, { encodeBaseUrl }] = /** @type {NonNullable<typeof protocol>} */ (protocol);
|
|
1225
|
+
if (hranaClient === undefined || hranaClient.closed) {
|
|
1226
|
+
const config = expandConfig({ url, authToken }, true);
|
|
1227
|
+
const base = encodeBaseUrl(config.scheme, config.authority, config.path);
|
|
1228
|
+
if (config.scheme === "ws" || config.scheme === "wss") hranaClient = hrana.openWs(base, config.authToken);
|
|
1229
|
+
else {
|
|
905
1230
|
const send = fetchWith(added, abandoned);
|
|
906
1231
|
/** @type {(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>} */
|
|
907
1232
|
const tracked = (input, init) => {
|
|
@@ -910,11 +1235,16 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
910
1235
|
void sent.finally(() => streamRequests.delete(sent)).catch(() => {});
|
|
911
1236
|
return sent;
|
|
912
1237
|
};
|
|
913
|
-
hranaClient = hrana.openHttp(
|
|
914
|
-
hranaClient.intMode = "bigint";
|
|
1238
|
+
hranaClient = hrana.openHttp(base, config.authToken, tracked);
|
|
915
1239
|
}
|
|
916
|
-
|
|
917
|
-
}
|
|
1240
|
+
hranaClient.intMode = "bigint";
|
|
1241
|
+
}
|
|
1242
|
+
return hranaClient;
|
|
1243
|
+
};
|
|
1244
|
+
const shared = async () => {
|
|
1245
|
+
if (protocol === undefined) return undefined;
|
|
1246
|
+
const [hrana, , , { LibsqlError }] = protocol;
|
|
1247
|
+
return (reused ??= new ReusedStream(hrana, LibsqlError, remote));
|
|
918
1248
|
};
|
|
919
1249
|
// A local statement that fails part way, as a write does when another process holds the lock past the busy timeout,
|
|
920
1250
|
// stays active on its connection: the driver resets and finalizes a statement only when it is garbage collected.
|
|
@@ -944,10 +1274,29 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
944
1274
|
const version = (await probe.execute("PRAGMA data_version")).rows[0]?.data_version;
|
|
945
1275
|
return typeof version === "bigint" || typeof version === "number" ? version : undefined;
|
|
946
1276
|
};
|
|
947
|
-
/** @type {<T>(operation: () => Promise<T>) => Promise<T>} */
|
|
948
|
-
const waitForLock = async (operation) => {
|
|
949
|
-
// A server answers busy before it runs anything of one statement or a batch
|
|
950
|
-
|
|
1277
|
+
/** @type {<T>(operation: () => Promise<T>, afterSchemaChange?: () => Promise<T>) => Promise<T>} */
|
|
1278
|
+
const waitForLock = async (operation, afterSchemaChange) => {
|
|
1279
|
+
// A server answers busy before it runs anything of one statement or a batch, and `SQLITE_SCHEMA` before it runs
|
|
1280
|
+
// anything of one statement. After `SQLITE_SCHEMA`, `afterSchemaChange`, when given, runs in its place, with a
|
|
1281
|
+
// wait of its own: the run the server answered `SQLITE_SCHEMA` may itself have taken the whole wait.
|
|
1282
|
+
if (!local) {
|
|
1283
|
+
if (afterSchemaChange === undefined) return whileServerBusy(() => watch(operation), () => true);
|
|
1284
|
+
let schemaChanged = false;
|
|
1285
|
+
try {
|
|
1286
|
+
return await whileServerBusy(
|
|
1287
|
+
() => watch(operation),
|
|
1288
|
+
() => true,
|
|
1289
|
+
undefined,
|
|
1290
|
+
() => {
|
|
1291
|
+
schemaChanged = true;
|
|
1292
|
+
return false;
|
|
1293
|
+
},
|
|
1294
|
+
);
|
|
1295
|
+
} catch (error) {
|
|
1296
|
+
if (!schemaChanged) throw error;
|
|
1297
|
+
}
|
|
1298
|
+
return whileServerBusy(() => watch(afterSchemaChange), () => true);
|
|
1299
|
+
}
|
|
951
1300
|
/** @type {bigint | number | undefined} */
|
|
952
1301
|
let version;
|
|
953
1302
|
for (;;) {
|
|
@@ -973,6 +1322,18 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
973
1322
|
}
|
|
974
1323
|
return (client ??= create());
|
|
975
1324
|
};
|
|
1325
|
+
/**
|
|
1326
|
+
* Runs `batched` in one write transaction, in one request to a server.
|
|
1327
|
+
* @param {readonly import("@bastani/atomic/storage").SqliteStatement[]} batched
|
|
1328
|
+
*/
|
|
1329
|
+
const writeTransaction = async (batched) => {
|
|
1330
|
+
if (protocol !== undefined) return writeBatch(protocol[0], protocol[3], remote(), batched);
|
|
1331
|
+
const results = await (await connection()).batch(
|
|
1332
|
+
batched.map(({ sql, params }) => ({ sql, args: [...params] })),
|
|
1333
|
+
"write",
|
|
1334
|
+
);
|
|
1335
|
+
return results.map(driverResult);
|
|
1336
|
+
};
|
|
976
1337
|
const redact = redactSecrets([authToken ?? "", ...urlCredentials(url), ...Object.values(headers ?? {})]);
|
|
977
1338
|
/** The server's location as diagnostics show it, without credentials. */
|
|
978
1339
|
const redacted = redact(url);
|
|
@@ -986,18 +1347,12 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
986
1347
|
// One statement outside a transaction, run alone or as an `exec` of one statement, that waited out the busy
|
|
987
1348
|
// timeout wrote nothing and runs again. Several statements in one `exec` do not: the ones before the failed
|
|
988
1349
|
// statement were committed.
|
|
989
|
-
...statements(connection, watch, waitForLock, shared),
|
|
1350
|
+
...statements(connection, watch, waitForLock, shared, local ? undefined : writeTransaction),
|
|
990
1351
|
// One request for the whole batch, in a write transaction: the journal sends a batch of appends this way. Only
|
|
991
1352
|
// its `BEGIN IMMEDIATE` waits for the write lock, so a batch the busy timeout ended wrote nothing (`lockBusy`). A
|
|
992
1353
|
// server's batch counts against this process's writes in flight to that server, unless it has priority.
|
|
993
1354
|
batch: (batched, options) => {
|
|
994
|
-
const send =
|
|
995
|
-
const results = await (await connection()).batch(
|
|
996
|
-
batched.map(({ sql, params }) => ({ sql, args: [...params] })),
|
|
997
|
-
"write",
|
|
998
|
-
);
|
|
999
|
-
return results.map(driverResult);
|
|
1000
|
-
};
|
|
1355
|
+
const send = () => writeTransaction(batched);
|
|
1001
1356
|
if (local) return waitForLock(send);
|
|
1002
1357
|
const sent = () =>
|
|
1003
1358
|
watch(() =>
|
|
@@ -1027,6 +1382,8 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
1027
1382
|
// again in a new transaction. A busy answer after the transaction began is not run again: a busy statement
|
|
1028
1383
|
// leaves the transaction open, and a busy answer that ends its stream also ends the statements it already ran.
|
|
1029
1384
|
// The transaction began once a statement in it settled and left it open: the server acknowledged its BEGIN.
|
|
1385
|
+
// A statement in a transaction that the server answered `SQLITE_SCHEMA` is not run again here: the server may
|
|
1386
|
+
// have ended the transaction, and the statement would then run, and commit, on its own.
|
|
1030
1387
|
let started = false;
|
|
1031
1388
|
/** @type {<T>(operation: () => Promise<T>) => Promise<T>} */
|
|
1032
1389
|
const inTransaction = async (operation) => {
|
|
@@ -1037,6 +1394,7 @@ export async function openLibsqlDatabase({ url, authToken, namespace, headers })
|
|
|
1037
1394
|
async () => {
|
|
1038
1395
|
transaction = await watch(begin);
|
|
1039
1396
|
},
|
|
1397
|
+
() => false,
|
|
1040
1398
|
);
|
|
1041
1399
|
} finally {
|
|
1042
1400
|
if (!transaction.closed) started = true;
|
package/package.json
CHANGED