@vibeorm/adapter-bun 4.2.0 → 5.0.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/README.md +22 -4
- package/dist/bun-version.d.ts +9 -0
- package/dist/config.d.ts +8 -0
- package/dist/connection-url.d.ts +48 -4
- package/dist/index.d.ts +17 -2
- package/dist/index.js +773 -35
- package/dist/index.js.map +7 -7
- package/dist/template-cache.d.ts +5 -0
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ function createBunSql(params) {
|
|
|
7
7
|
|
|
8
8
|
// packages/adapter-bun/src/config.ts
|
|
9
9
|
var MINIMUM_BUN_VERSION = "1.4.1";
|
|
10
|
+
var NAMED_UNSAFE_BUN_VERSION = "1.4.3";
|
|
10
11
|
var MAX_BIND_PARAMETERS = 65535;
|
|
11
12
|
var SESSION_CONNECTION = Object.freeze({
|
|
12
13
|
max: 1,
|
|
@@ -24,6 +25,9 @@ function refuseUnsupportedBun(params) {
|
|
|
24
25
|
meta: { bunVersion: params.version, minimumBunVersion: MINIMUM_BUN_VERSION }
|
|
25
26
|
});
|
|
26
27
|
}
|
|
28
|
+
function namedUnsafeBinds(params) {
|
|
29
|
+
return Bun.semver.satisfies(params.version, `>=${NAMED_UNSAFE_BUN_VERSION}`);
|
|
30
|
+
}
|
|
27
31
|
|
|
28
32
|
// packages/adapter-bun/src/index.ts
|
|
29
33
|
import { postgresDialect, renderDiagnostic } from "@vibeorm/sql";
|
|
@@ -41,7 +45,11 @@ import {
|
|
|
41
45
|
transactionLifecycleRefusal,
|
|
42
46
|
createSavepointScope,
|
|
43
47
|
engineStatementBudgetMs,
|
|
48
|
+
findRefusedSetting,
|
|
44
49
|
markTransactionAborted,
|
|
50
|
+
readTransactionSettings,
|
|
51
|
+
renderTransactionSettings,
|
|
52
|
+
transactionSettingRefusedError,
|
|
45
53
|
runNestedSavepoint,
|
|
46
54
|
runRootScope,
|
|
47
55
|
scopeTurn,
|
|
@@ -55,6 +63,9 @@ import { VibeError as VibeError7 } from "@vibeorm/schema";
|
|
|
55
63
|
|
|
56
64
|
// packages/adapter-bun/src/connection-url.ts
|
|
57
65
|
import { VibeError as VibeError2 } from "@vibeorm/schema";
|
|
66
|
+
function defaultPlanCacheMode(params) {
|
|
67
|
+
return params.preparedStatements ? "force_custom_plan" : "auto";
|
|
68
|
+
}
|
|
58
69
|
function buildStartupOptions(params) {
|
|
59
70
|
const { planCacheMode, statementTimeout } = params;
|
|
60
71
|
const parts = [];
|
|
@@ -91,6 +102,39 @@ function takeConnectTimeout(params) {
|
|
|
91
102
|
url.search = url.searchParams.toString().replace(/\+/g, "%20");
|
|
92
103
|
return seconds > 0 ? { url: url.toString(), seconds } : { url: url.toString() };
|
|
93
104
|
}
|
|
105
|
+
var SSLMODE_KEYS = new Set(["sslmode", "ssl-mode", "ssl_mode"]);
|
|
106
|
+
var TLS_FLAG_KEYS = new Set(["ssl", "tls"]);
|
|
107
|
+
var VERIFY_FULL = new Set(["verify-full", "verify_full", "verify-identity", "verify_identity"]);
|
|
108
|
+
function takeSystemRootCert(params) {
|
|
109
|
+
if (!URL.canParse(params.url))
|
|
110
|
+
return params.url;
|
|
111
|
+
const url = new URL(params.url);
|
|
112
|
+
const entries = [...url.searchParams.entries()];
|
|
113
|
+
const rootCert = entries.filter(([key]) => key.toLowerCase() === "sslrootcert").at(-1);
|
|
114
|
+
if (rootCert?.[1] !== "system")
|
|
115
|
+
return params.url;
|
|
116
|
+
for (const [key, value] of entries) {
|
|
117
|
+
const lower = key.toLowerCase();
|
|
118
|
+
const isFlag = TLS_FLAG_KEYS.has(lower);
|
|
119
|
+
if (!SSLMODE_KEYS.has(lower) && !isFlag)
|
|
120
|
+
continue;
|
|
121
|
+
if (isFlag && ["", "true", "1"].includes(value.toLowerCase()))
|
|
122
|
+
continue;
|
|
123
|
+
if (VERIFY_FULL.has(value.toLowerCase()))
|
|
124
|
+
continue;
|
|
125
|
+
throw new VibeError2({
|
|
126
|
+
code: "VIBE_CONFIG",
|
|
127
|
+
message: `bunAdapter \`url\` combines sslrootcert=system with ${key}=${JSON.stringify(value)}. ` + "libpq allows sslrootcert=system only with sslmode=verify-full (its default when sslmode is omitted): any certificate " + "the system trusts would otherwise vouch for a server that is not yours. Use sslmode=verify-full, or remove the setting.",
|
|
128
|
+
meta: { adapter: "bun", reason: "sslrootcert-system-weak-sslmode", parameter: key, value }
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
const kept = entries.filter(([key]) => {
|
|
132
|
+
const lower = key.toLowerCase();
|
|
133
|
+
return lower !== "sslrootcert" && !SSLMODE_KEYS.has(lower) && !TLS_FLAG_KEYS.has(lower);
|
|
134
|
+
});
|
|
135
|
+
url.search = new URLSearchParams([...kept, ["sslmode", "verify-full"]]).toString().replace(/\+/g, "%20");
|
|
136
|
+
return url.toString();
|
|
137
|
+
}
|
|
94
138
|
function applyStartupOptions(params) {
|
|
95
139
|
if (!URL.canParse(params.url)) {
|
|
96
140
|
throw new VibeError2({ code: "VIBE_VALIDATION", message: "invalid PostgreSQL connection URL" });
|
|
@@ -346,7 +390,7 @@ function scanPlaceholders(params) {
|
|
|
346
390
|
index += 1;
|
|
347
391
|
}
|
|
348
392
|
chunks.push(text.slice(chunkStart));
|
|
349
|
-
const template = Object.
|
|
393
|
+
const template = Object.defineProperty(chunks, "raw", { value: chunks, enumerable: false });
|
|
350
394
|
return { template, indices, identityOrder: indices.every((placeholder, index) => placeholder === index + 1) };
|
|
351
395
|
}
|
|
352
396
|
function templateChunks(params) {
|
|
@@ -447,9 +491,70 @@ import {
|
|
|
447
491
|
transactionOutcomeUnknownError,
|
|
448
492
|
validateTransactionDeadline as validateTransactionDeadline2
|
|
449
493
|
} from "@vibeorm/runtime/adapter-kit";
|
|
494
|
+
function commitSentRefusal() {
|
|
495
|
+
return new VibeError7({
|
|
496
|
+
code: "VIBE_TRANSACTION",
|
|
497
|
+
message: "this transaction already sent its COMMIT together with its last statement, so it accepts no further statement",
|
|
498
|
+
meta: { provider: "postgresql", reason: "commit-sent" }
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
function finalStatementMisuse() {
|
|
502
|
+
return new VibeError7({
|
|
503
|
+
code: "VIBE_INTERNAL",
|
|
504
|
+
message: "withCommit(): a view carries ONE statement, the transaction's last",
|
|
505
|
+
meta: { reason: "with-commit-second-statement" }
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
function laneGroupMisuse(params) {
|
|
509
|
+
return new VibeError7({ code: "VIBE_INTERNAL", message: `pipeline lane group: ${params.message}`, meta: { reason: "pipeline-lane-group" } });
|
|
510
|
+
}
|
|
511
|
+
function discardedRows() {
|
|
512
|
+
return Object.assign([], { count: 0 });
|
|
513
|
+
}
|
|
450
514
|
var ignore = () => {
|
|
451
515
|
return;
|
|
452
516
|
};
|
|
517
|
+
var LANE_GO = Object.freeze({ kind: "go" });
|
|
518
|
+
var LANE_CLOSED = Object.freeze({ kind: "closed" });
|
|
519
|
+
function serverAnswered(params) {
|
|
520
|
+
const { values } = params;
|
|
521
|
+
if (values.length > MAX_BIND_PARAMETERS)
|
|
522
|
+
return false;
|
|
523
|
+
for (const value of values) {
|
|
524
|
+
if (value !== null && typeof value !== "string" && !(value instanceof Uint8Array))
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
function predecessorClosedError() {
|
|
530
|
+
return new VibeError7({
|
|
531
|
+
code: "VIBE_TRANSACTION",
|
|
532
|
+
message: "pipeline(): an earlier lane of this ordered scope closed without issuing its statement, so this statement was not sent " + "(the operation before it failed before it reached the database)",
|
|
533
|
+
meta: { reason: "pipeline-predecessor-closed" }
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
function orderedLaneMisuse(params) {
|
|
537
|
+
return new VibeError7({ code: "VIBE_INTERNAL", message: `pipeline(): ${params.message}`, meta: { reason: "pipeline-ordered-lane" } });
|
|
538
|
+
}
|
|
539
|
+
function executorFor(params) {
|
|
540
|
+
const { dispatch, effect, guard, final } = params;
|
|
541
|
+
const finalOf = final === undefined ? {} : { final: true, ...final.discardRows ? { discardRows: true } : {} };
|
|
542
|
+
return Object.assign((strings, ...values) => dispatch({
|
|
543
|
+
effect,
|
|
544
|
+
guard,
|
|
545
|
+
extended: true,
|
|
546
|
+
answered: () => serverAnswered({ values }),
|
|
547
|
+
...finalOf,
|
|
548
|
+
send: (connection) => connection(strings, ...values)
|
|
549
|
+
}), { unsafe: (text, values) => dispatch({
|
|
550
|
+
effect,
|
|
551
|
+
guard,
|
|
552
|
+
extended: values !== undefined && values.length > 0,
|
|
553
|
+
answered: () => values !== undefined && values.length > 0 && serverAnswered({ values }),
|
|
554
|
+
...finalOf,
|
|
555
|
+
send: (connection) => connection.unsafe(text, values)
|
|
556
|
+
}) });
|
|
557
|
+
}
|
|
453
558
|
var DIALECT = "postgres";
|
|
454
559
|
var PROVIDER = "postgresql";
|
|
455
560
|
var BUDGETS = Object.freeze({
|
|
@@ -458,9 +563,10 @@ var BUDGETS = Object.freeze({
|
|
|
458
563
|
transactionDeadline: "cancel-running-statements",
|
|
459
564
|
accessModes: Object.freeze(["readOnly"])
|
|
460
565
|
});
|
|
461
|
-
var WIRE_TEXT = Object.freeze({ dateTime: "native", json: "text", numbers: "native" });
|
|
462
|
-
var WIRE_PARSED = Object.freeze({ dateTime: "native", json: "parsed", numbers: "native" });
|
|
566
|
+
var WIRE_TEXT = Object.freeze({ dateTime: "native", json: "text", numbers: "native", bytes: "buffer" });
|
|
567
|
+
var WIRE_PARSED = Object.freeze({ dateTime: "native", json: "parsed", numbers: "native", bytes: "buffer" });
|
|
463
568
|
var ROUND_TRIPS = "network";
|
|
569
|
+
var READ_SNAPSHOT = Object.freeze({ isolationLevel: "RepeatableRead", accessMode: "readOnly" });
|
|
464
570
|
function requireUrl(params) {
|
|
465
571
|
const url = params.options?.url;
|
|
466
572
|
if (typeof url === "string" && url.trim() !== "")
|
|
@@ -471,10 +577,24 @@ function requireUrl(params) {
|
|
|
471
577
|
meta: { adapter: "bun", reason: "missing-address" }
|
|
472
578
|
});
|
|
473
579
|
}
|
|
580
|
+
var MAX_STATEMENT_TIMEOUT_MS = 2147483647;
|
|
581
|
+
function assertPoolMin(params) {
|
|
582
|
+
const { value, max } = params;
|
|
583
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= max)
|
|
584
|
+
return value;
|
|
585
|
+
throw new VibeError7({
|
|
586
|
+
code: "VIBE_CONFIG",
|
|
587
|
+
message: `bunAdapter \`min\` must be an integer from 0 through \`max\` (${max}).`,
|
|
588
|
+
meta: { adapter: "bun", reason: "invalid-pool-min", option: "min", max }
|
|
589
|
+
});
|
|
590
|
+
}
|
|
474
591
|
function bunAdapter(options) {
|
|
475
592
|
refuseUnsupportedBun({ version: Bun.version });
|
|
476
593
|
const usePrepared = options?.preparedStatements ?? false;
|
|
477
|
-
const
|
|
594
|
+
const poolMin = options?.min === undefined ? 0 : assertPoolMin({ value: options.min, max: options?.max ?? 10 });
|
|
595
|
+
const pipelining = usePrepared ? "batched" : "ordered";
|
|
596
|
+
const namedUnsafe = usePrepared && namedUnsafeBinds({ version: Bun.version });
|
|
597
|
+
const address = takeConnectTimeout({ url: takeSystemRootCert({ url: requireUrl({ options }) }) });
|
|
478
598
|
const templates = createTemplateCache({ max: options?.stmtCacheMax ?? 1000 });
|
|
479
599
|
let activeWire = WIRE_TEXT;
|
|
480
600
|
let wireSettled = false;
|
|
@@ -484,15 +604,28 @@ function bunAdapter(options) {
|
|
|
484
604
|
function controlQuery(params) {
|
|
485
605
|
return params.connection(controlTemplates.getTemplate({ text: params.text }).template, ...params.values ?? []);
|
|
486
606
|
}
|
|
607
|
+
function controlStep(params) {
|
|
608
|
+
const { connection, step } = params;
|
|
609
|
+
if (step.values === undefined || step.values.length === 0)
|
|
610
|
+
return controlQuery({ connection, text: step.text });
|
|
611
|
+
const prepared = controlTemplates.getTemplate({ text: step.text });
|
|
612
|
+
return connection(prepared.template, ...bindTemplateValues({ text: step.text, indices: prepared.indices, values: step.values }));
|
|
613
|
+
}
|
|
487
614
|
function createReservedControl(params) {
|
|
488
615
|
const { reserved, budget, scope } = params;
|
|
489
|
-
const deferred = createDeferredControl({ opening: params.opening === null ? [] : [params.opening] });
|
|
616
|
+
const deferred = createDeferredControl({ opening: params.opening === null ? [] : [params.opening], deferReleases: true });
|
|
490
617
|
let openingFailure;
|
|
491
618
|
let openingStep = null;
|
|
492
619
|
let openingPending = null;
|
|
493
620
|
let waitTail = null;
|
|
494
621
|
let deadlineTail = Promise.resolve();
|
|
495
622
|
let lastFailure;
|
|
623
|
+
let ridden;
|
|
624
|
+
let queued = 0;
|
|
625
|
+
const inFlight = new Set;
|
|
626
|
+
const openTracks = new Set;
|
|
627
|
+
const openOrderedLanes = new Set;
|
|
628
|
+
let settingsSent = false;
|
|
496
629
|
const step = (query) => new Promise((resolve) => resolve(query())).then(undefined, (error) => {
|
|
497
630
|
const mapped = mapBunDriverError({ error });
|
|
498
631
|
if (mapped.meta.sqlstate === "25P02") {
|
|
@@ -516,25 +649,84 @@ function bunAdapter(options) {
|
|
|
516
649
|
return settled;
|
|
517
650
|
};
|
|
518
651
|
const ridesOpening = (send) => send.extended && send.effect === "read";
|
|
652
|
+
const sendStep = (deferredStep) => {
|
|
653
|
+
const settings = params.openingSettings;
|
|
654
|
+
if (settings === undefined || settingsSent || deferredStep.text !== settings.text) {
|
|
655
|
+
return step(() => controlStep({ connection: reserved, step: deferredStep }));
|
|
656
|
+
}
|
|
657
|
+
settingsSent = true;
|
|
658
|
+
return new Promise((resolve) => resolve(controlStep({ connection: reserved, step: deferredStep }))).then(undefined, (error) => {
|
|
659
|
+
const mapped = mapBunDriverError({ error });
|
|
660
|
+
if (mapped.meta.sqlstate === "25P02")
|
|
661
|
+
throw transactionAbortedError({ cause: lastFailure ?? scope.state.aborted?.cause });
|
|
662
|
+
if (typeof mapped.meta.sqlstate !== "string")
|
|
663
|
+
throw mapped;
|
|
664
|
+
const refused = settings.refused(mapped);
|
|
665
|
+
lastFailure = refused;
|
|
666
|
+
throw refused;
|
|
667
|
+
});
|
|
668
|
+
};
|
|
669
|
+
const ridesNow = (send) => {
|
|
670
|
+
if (send.final !== true || !params.commits || ridden !== undefined || openingFailure !== undefined || budget.totalMs !== null)
|
|
671
|
+
return false;
|
|
672
|
+
if (scope.state.open !== null || scope.state.aborted !== null || scope.state.poisoned !== null)
|
|
673
|
+
return false;
|
|
674
|
+
if (!send.extended || send.answered?.() !== true || queued > 0)
|
|
675
|
+
return false;
|
|
676
|
+
for (const other of inFlight)
|
|
677
|
+
if (other.answered?.() !== true)
|
|
678
|
+
return false;
|
|
679
|
+
for (const track of openTracks)
|
|
680
|
+
if (!track.final || track.group !== undefined && !track.group.sent)
|
|
681
|
+
return false;
|
|
682
|
+
for (const lane of openOrderedLanes)
|
|
683
|
+
if (!lane.final && !(lane.followed && lane.issued > 0))
|
|
684
|
+
return false;
|
|
685
|
+
return true;
|
|
686
|
+
};
|
|
687
|
+
const rideCommit = () => {
|
|
688
|
+
deferred.dropReleases();
|
|
689
|
+
const reply = step(() => controlQuery({ connection: reserved, text: "COMMIT" }));
|
|
690
|
+
const entry = { settled: Promise.resolve(), state: "in-flight" };
|
|
691
|
+
entry.settled = reply.then((result) => {
|
|
692
|
+
if (finalRows({ result }).command === "ROLLBACK") {
|
|
693
|
+
entry.state = "failed";
|
|
694
|
+
entry.error = transactionAbortedError({ cause: lastFailure ?? scope.state.aborted?.cause });
|
|
695
|
+
} else if (openingFailure !== undefined) {
|
|
696
|
+
entry.state = "failed";
|
|
697
|
+
entry.error = transactionAbortedError({ cause: openingFailure });
|
|
698
|
+
} else {
|
|
699
|
+
entry.state = "committed";
|
|
700
|
+
}
|
|
701
|
+
}, (error) => {
|
|
702
|
+
entry.state = isConnectionLoss({ error }) ? "lost" : "failed";
|
|
703
|
+
entry.error = error;
|
|
704
|
+
});
|
|
705
|
+
ridden = entry;
|
|
706
|
+
return entry;
|
|
707
|
+
};
|
|
519
708
|
const sendNow = (send) => {
|
|
520
709
|
const cleanup = send.cleanup === true;
|
|
521
710
|
if (!cleanup) {
|
|
711
|
+
if (ridden !== undefined)
|
|
712
|
+
throw commitSentRefusal();
|
|
522
713
|
if (openingFailure !== undefined)
|
|
523
714
|
throw transactionAbortedError({ cause: openingFailure });
|
|
524
715
|
budget.assertUsable({ stage: send.stage ?? "statement" });
|
|
525
716
|
send.guard?.();
|
|
526
717
|
}
|
|
527
718
|
const carriesOpening = !cleanup && !deferred.opened();
|
|
528
|
-
const control = cleanup ? [] : [...deferred.
|
|
719
|
+
const control = cleanup ? [] : [...send.carry ?? [], ...deferred.takeSteps()];
|
|
529
720
|
if (carriesOpening && !ridesOpening(send)) {
|
|
530
721
|
const [opening, ...rest] = control;
|
|
531
|
-
const confirmed = trackOpening(
|
|
532
|
-
const
|
|
533
|
-
|
|
722
|
+
const confirmed = trackOpening(sendStep(opening));
|
|
723
|
+
const bound = rest.findIndex((pending) => pending.values !== undefined);
|
|
724
|
+
const riding = (bound === -1 ? rest : rest.slice(0, bound)).map((text) => {
|
|
725
|
+
const sent = sendStep(text);
|
|
534
726
|
sent.catch(ignore);
|
|
535
727
|
return sent;
|
|
536
728
|
});
|
|
537
|
-
const next = { ...send, riding };
|
|
729
|
+
const next = { ...send, riding, carry: bound === -1 ? [] : rest.slice(bound) };
|
|
538
730
|
if (budget.totalMs !== null)
|
|
539
731
|
return confirmed.then(() => sendNow(next));
|
|
540
732
|
return enqueue(next);
|
|
@@ -543,22 +735,47 @@ function bunAdapter(options) {
|
|
|
543
735
|
const steps = [...send.riding ?? []];
|
|
544
736
|
if (!cleanup && !carriesOpening && openingStep !== null)
|
|
545
737
|
steps.push(openingStep);
|
|
546
|
-
control.forEach((
|
|
547
|
-
const sent =
|
|
738
|
+
control.forEach((pending, index) => {
|
|
739
|
+
const sent = sendStep(pending);
|
|
548
740
|
if (index === 0 && carriesOpening)
|
|
549
741
|
trackOpening(sent);
|
|
550
742
|
steps.push(sent);
|
|
551
743
|
});
|
|
552
744
|
if (ms !== null)
|
|
553
745
|
steps.push(step(() => controlQuery({ connection: reserved, text: STATEMENT_BOUND_TEXT, values: [String(ms)] })));
|
|
554
|
-
const
|
|
746
|
+
const rides = !cleanup && ridesNow(send);
|
|
747
|
+
let handed = false;
|
|
748
|
+
const statement = step(() => {
|
|
749
|
+
const query = send.send(reserved);
|
|
750
|
+
handed = true;
|
|
751
|
+
return query;
|
|
752
|
+
});
|
|
753
|
+
let riding;
|
|
754
|
+
if (handed) {
|
|
755
|
+
if (!cleanup) {
|
|
756
|
+
inFlight.add(send);
|
|
757
|
+
statement.then(() => inFlight.delete(send), () => inFlight.delete(send));
|
|
758
|
+
}
|
|
759
|
+
send.onSent?.();
|
|
760
|
+
if (rides)
|
|
761
|
+
riding = rideCommit();
|
|
762
|
+
}
|
|
555
763
|
steps.push(statement);
|
|
556
|
-
|
|
557
|
-
const failed = settled.find((
|
|
764
|
+
const outcome = Promise.allSettled(steps).then((settled) => {
|
|
765
|
+
const failed = settled.find((result) => result.status === "rejected");
|
|
558
766
|
if (failed !== undefined)
|
|
559
767
|
throw failed.reason;
|
|
560
768
|
return statement;
|
|
561
769
|
});
|
|
770
|
+
if (riding === undefined || send.discardRows !== true)
|
|
771
|
+
return outcome;
|
|
772
|
+
const commitReply = riding;
|
|
773
|
+
return outcome.then(undefined, async (error) => {
|
|
774
|
+
await commitReply.settled;
|
|
775
|
+
if (commitReply.state === "committed" && !(error instanceof VibeError7 && typeof error.meta.sqlstate === "string"))
|
|
776
|
+
return discardedRows();
|
|
777
|
+
throw error;
|
|
778
|
+
});
|
|
562
779
|
};
|
|
563
780
|
const enqueue = (send) => {
|
|
564
781
|
const prior = waitTail ?? openingPending ?? Promise.resolve();
|
|
@@ -567,7 +784,9 @@ function bunAdapter(options) {
|
|
|
567
784
|
markStarted = resolve;
|
|
568
785
|
});
|
|
569
786
|
waitTail = started;
|
|
787
|
+
queued += 1;
|
|
570
788
|
return prior.then(() => {
|
|
789
|
+
queued -= 1;
|
|
571
790
|
try {
|
|
572
791
|
return sendNow(send);
|
|
573
792
|
} finally {
|
|
@@ -587,11 +806,243 @@ function bunAdapter(options) {
|
|
|
587
806
|
return enqueue(send);
|
|
588
807
|
return new Promise((resolve) => resolve(sendNow(send)));
|
|
589
808
|
};
|
|
590
|
-
|
|
809
|
+
const refusalBehind = (cause) => {
|
|
810
|
+
if (openingFailure !== undefined)
|
|
811
|
+
return transactionAbortedError({ cause: openingFailure });
|
|
812
|
+
if (scope.state.poisoned !== null)
|
|
813
|
+
return scope.state.poisoned;
|
|
814
|
+
return transactionAbortedError({ cause: scope.state.aborted?.cause ?? cause });
|
|
815
|
+
};
|
|
816
|
+
const newLane = (orderedScope, index) => {
|
|
817
|
+
let resolveRelease = ignore;
|
|
818
|
+
const release = new Promise((resolve) => {
|
|
819
|
+
resolveRelease = resolve;
|
|
820
|
+
});
|
|
821
|
+
let settled = false;
|
|
822
|
+
const lane = {
|
|
823
|
+
scope: orderedScope,
|
|
824
|
+
index,
|
|
825
|
+
open: true,
|
|
826
|
+
issued: 0,
|
|
827
|
+
followed: false,
|
|
828
|
+
final: false,
|
|
829
|
+
release,
|
|
830
|
+
settle: (outcome) => {
|
|
831
|
+
if (settled)
|
|
832
|
+
return;
|
|
833
|
+
settled = true;
|
|
834
|
+
resolveRelease(outcome);
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
openOrderedLanes.add(lane);
|
|
838
|
+
return lane;
|
|
839
|
+
};
|
|
840
|
+
const laneDispatch = (lane, send) => {
|
|
841
|
+
if (lane.followed && lane.issued > 0) {
|
|
842
|
+
return Promise.reject(orderedLaneMisuse({ message: "a lane that has a follower may issue ONE statement" }));
|
|
843
|
+
}
|
|
844
|
+
const first = lane.issued === 0;
|
|
845
|
+
lane.issued += 1;
|
|
846
|
+
const go = (before) => {
|
|
847
|
+
if (before.kind === "closed") {
|
|
848
|
+
if (first)
|
|
849
|
+
lane.settle(LANE_CLOSED);
|
|
850
|
+
return Promise.reject(predecessorClosedError());
|
|
851
|
+
}
|
|
852
|
+
const failed = before.kind === "failed" ? { cause: before.cause } : lane.scope.failed;
|
|
853
|
+
if (failed !== undefined) {
|
|
854
|
+
if (first)
|
|
855
|
+
lane.settle({ kind: "failed", cause: failed.cause });
|
|
856
|
+
return Promise.reject(refusalBehind(failed.cause));
|
|
857
|
+
}
|
|
858
|
+
const pipelinable = first && send.answered?.() === true;
|
|
859
|
+
return dispatch({
|
|
860
|
+
...send,
|
|
861
|
+
onSent: () => {
|
|
862
|
+
send.onSent?.();
|
|
863
|
+
if (pipelinable)
|
|
864
|
+
lane.settle(LANE_GO);
|
|
865
|
+
}
|
|
866
|
+
}).then((value) => {
|
|
867
|
+
if (first)
|
|
868
|
+
lane.settle(LANE_GO);
|
|
869
|
+
return value;
|
|
870
|
+
}, (error) => {
|
|
871
|
+
lane.scope.failed ??= { cause: error };
|
|
872
|
+
if (first)
|
|
873
|
+
lane.settle({ kind: "failed", cause: error });
|
|
874
|
+
throw error;
|
|
875
|
+
});
|
|
876
|
+
};
|
|
877
|
+
const before = lane.scope.lanes[lane.index - 1];
|
|
878
|
+
return before === undefined ? go(LANE_GO) : before.release.then(go);
|
|
879
|
+
};
|
|
880
|
+
const ordered = {
|
|
881
|
+
open: (count) => {
|
|
882
|
+
const orderedScope = { lanes: [] };
|
|
883
|
+
for (let index = 0;index < count; index++)
|
|
884
|
+
orderedScope.lanes.push(newLane(orderedScope, index));
|
|
885
|
+
return [...orderedScope.lanes];
|
|
886
|
+
},
|
|
887
|
+
follow: (lane) => {
|
|
888
|
+
if (!lane.open)
|
|
889
|
+
throw orderedLaneMisuse({ message: "follow() on a closed lane" });
|
|
890
|
+
if (lane.followed || lane.scope.lanes.at(-1) !== lane)
|
|
891
|
+
throw orderedLaneMisuse({ message: "follow() opens one lane after the LAST lane of its scope" });
|
|
892
|
+
if (lane.issued > 0)
|
|
893
|
+
throw orderedLaneMisuse({ message: "follow() must come before the lane issues its statement" });
|
|
894
|
+
lane.followed = true;
|
|
895
|
+
const next = newLane(lane.scope, lane.scope.lanes.length);
|
|
896
|
+
lane.scope.lanes.push(next);
|
|
897
|
+
return next;
|
|
898
|
+
},
|
|
899
|
+
close: (lane) => {
|
|
900
|
+
if (!lane.open)
|
|
901
|
+
return;
|
|
902
|
+
lane.open = false;
|
|
903
|
+
openOrderedLanes.delete(lane);
|
|
904
|
+
if (lane.issued === 0)
|
|
905
|
+
lane.settle(LANE_CLOSED);
|
|
906
|
+
},
|
|
907
|
+
control: (lane) => {
|
|
908
|
+
const laneSend = (send) => {
|
|
909
|
+
if (send.cleanup === true || !lane.open)
|
|
910
|
+
return dispatch(send);
|
|
911
|
+
if (send.final === true)
|
|
912
|
+
lane.final = true;
|
|
913
|
+
return laneDispatch(lane, send);
|
|
914
|
+
};
|
|
915
|
+
return {
|
|
916
|
+
...base,
|
|
917
|
+
dispatch: laneSend,
|
|
918
|
+
executor: ({ effect, guard, final }) => executorFor({ dispatch: laneSend, effect, guard, ...final === undefined ? {} : { final } }),
|
|
919
|
+
turn: () => {
|
|
920
|
+
const before = lane.scope.lanes[lane.index - 1];
|
|
921
|
+
if (!lane.open || before === undefined)
|
|
922
|
+
return Promise.resolve();
|
|
923
|
+
return before.release.then((release) => {
|
|
924
|
+
if (release.kind === "closed")
|
|
925
|
+
throw predecessorClosedError();
|
|
926
|
+
if (release.kind === "failed")
|
|
927
|
+
throw refusalBehind(release.cause);
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
const refuses = () => ridden !== undefined || openingFailure !== undefined || scope.state.poisoned !== null || scope.state.aborted !== null;
|
|
934
|
+
const groupOver = (group) => group.sealed && group.seats.every((seat) => !seat.open);
|
|
935
|
+
const sendGroup = async (group, entries) => {
|
|
936
|
+
let failure;
|
|
937
|
+
for (const [index, entry] of entries.entries()) {
|
|
938
|
+
if (failure !== undefined) {
|
|
939
|
+
entry.reject(refusalBehind(failure.cause));
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
const send = group.final && index === entries.length - 1 ? { ...entry.send, final: true } : entry.send;
|
|
943
|
+
const sent = dispatch(send);
|
|
944
|
+
sent.then(entry.resolve, entry.reject);
|
|
945
|
+
if (send.answered?.() !== true) {
|
|
946
|
+
const outcome = await sent.then(() => {
|
|
947
|
+
return;
|
|
948
|
+
}, (cause) => ({ cause }));
|
|
949
|
+
if (outcome !== undefined)
|
|
950
|
+
failure = outcome;
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
const flushGroup = (group) => {
|
|
955
|
+
if (!group.sealed || group.sent)
|
|
956
|
+
return;
|
|
957
|
+
for (const seat of group.seats)
|
|
958
|
+
if (seat.open && !seat.done && seat.held === undefined)
|
|
959
|
+
return;
|
|
960
|
+
group.sent = true;
|
|
961
|
+
sendGroup(group, group.seats.flatMap((seat) => seat.held === undefined ? [] : [seat.held]));
|
|
962
|
+
};
|
|
963
|
+
const seatControl = (group, seat) => {
|
|
964
|
+
const hold = (send) => {
|
|
965
|
+
if (send.cleanup === true)
|
|
966
|
+
return dispatch(send);
|
|
967
|
+
if (seat.held !== undefined || seat.done || !seat.open || group.sent) {
|
|
968
|
+
return Promise.reject(laneGroupMisuse({ message: "a sub-lane issues ONE statement, before the group left" }));
|
|
969
|
+
}
|
|
970
|
+
return new Promise((resolve, reject) => {
|
|
971
|
+
seat.held = { send, resolve, reject };
|
|
972
|
+
flushGroup(group);
|
|
973
|
+
});
|
|
974
|
+
};
|
|
975
|
+
return { ...base, dispatch: hold, executor: ({ effect, guard }) => executorFor({ dispatch: hold, effect, guard }), commits: false };
|
|
976
|
+
};
|
|
977
|
+
const openLane = () => {
|
|
978
|
+
const track = { open: true, final: false };
|
|
979
|
+
openTracks.add(track);
|
|
980
|
+
const laneSend = (send) => {
|
|
981
|
+
if (send.cleanup !== true && track.open) {
|
|
982
|
+
if (track.group !== undefined && !groupOver(track.group)) {
|
|
983
|
+
return Promise.reject(laneGroupMisuse({ message: "a lane issues nothing while its group is open" }));
|
|
984
|
+
}
|
|
985
|
+
if (send.final === true)
|
|
986
|
+
track.final = true;
|
|
987
|
+
}
|
|
988
|
+
return dispatch(send);
|
|
989
|
+
};
|
|
990
|
+
const control = {
|
|
991
|
+
...base,
|
|
992
|
+
dispatch: laneSend,
|
|
993
|
+
executor: ({ effect, guard, final }) => executorFor({ dispatch: laneSend, effect, guard, ...final === undefined ? {} : { final } })
|
|
994
|
+
};
|
|
995
|
+
return {
|
|
996
|
+
control,
|
|
997
|
+
close: () => {
|
|
998
|
+
track.open = false;
|
|
999
|
+
openTracks.delete(track);
|
|
1000
|
+
},
|
|
1001
|
+
group: (final) => {
|
|
1002
|
+
if (!track.open || track.group !== undefined && !groupOver(track.group) || refuses() || !wireSettled)
|
|
1003
|
+
return;
|
|
1004
|
+
const group = { final: final && params.commits, seats: [], sealed: false, sent: false };
|
|
1005
|
+
track.group = group;
|
|
1006
|
+
if (group.final)
|
|
1007
|
+
track.final = true;
|
|
1008
|
+
return {
|
|
1009
|
+
seat: () => {
|
|
1010
|
+
if (group.sealed)
|
|
1011
|
+
throw laneGroupMisuse({ message: "a sub-lane is opened only before the group is sealed" });
|
|
1012
|
+
const seat = { open: true, used: false, done: false };
|
|
1013
|
+
group.seats.push(seat);
|
|
1014
|
+
return { seat, control: seatControl(group, seat) };
|
|
1015
|
+
},
|
|
1016
|
+
seal: () => {
|
|
1017
|
+
if (group.sealed)
|
|
1018
|
+
return;
|
|
1019
|
+
group.sealed = true;
|
|
1020
|
+
flushGroup(group);
|
|
1021
|
+
},
|
|
1022
|
+
close: (seat) => {
|
|
1023
|
+
if (!seat.open)
|
|
1024
|
+
return;
|
|
1025
|
+
seat.open = false;
|
|
1026
|
+
flushGroup(group);
|
|
1027
|
+
},
|
|
1028
|
+
drop: (seat) => {
|
|
1029
|
+
seat.done = true;
|
|
1030
|
+
flushGroup(group);
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
};
|
|
1036
|
+
const base = {
|
|
591
1037
|
deferred,
|
|
592
1038
|
dispatch,
|
|
593
|
-
executor: ({ effect, guard
|
|
1039
|
+
executor: ({ effect, guard, final }) => executorFor({ dispatch, effect, guard, ...final === undefined ? {} : { final } }),
|
|
1040
|
+
ordered,
|
|
1041
|
+
commits: params.commits,
|
|
1042
|
+
ridden: () => ridden,
|
|
1043
|
+
openLane
|
|
594
1044
|
};
|
|
1045
|
+
return base;
|
|
595
1046
|
}
|
|
596
1047
|
let rawSession = null;
|
|
597
1048
|
function createSqlInstance(params) {
|
|
@@ -602,7 +1053,7 @@ function bunAdapter(options) {
|
|
|
602
1053
|
sqlOptions.connectionTimeout = address.seconds;
|
|
603
1054
|
}
|
|
604
1055
|
const startupOptions = buildStartupOptions({
|
|
605
|
-
planCacheMode: options?.planCacheMode ??
|
|
1056
|
+
planCacheMode: options?.planCacheMode ?? defaultPlanCacheMode({ preparedStatements: usePrepared }),
|
|
606
1057
|
statementTimeout: options?.statementTimeout
|
|
607
1058
|
});
|
|
608
1059
|
const url = startupOptions === undefined ? address.url : applyStartupOptions({ url: address.url, startupOptions });
|
|
@@ -667,8 +1118,9 @@ function bunAdapter(options) {
|
|
|
667
1118
|
const values = serializeParams({ values: params.values, namedStatements: usePrepared });
|
|
668
1119
|
if (usePrepared) {
|
|
669
1120
|
const prepared = templates.getTemplate({ text: params.text });
|
|
670
|
-
const
|
|
671
|
-
const
|
|
1121
|
+
const identity = prepared.identityOrder && prepared.indices.length === values.length;
|
|
1122
|
+
const bound = identity ? values : bindTemplateValues({ text: params.text, indices: prepared.indices, values });
|
|
1123
|
+
const result = bound.length > MAX_BIND_PARAMETERS || namedUnsafe && identity && values.length > 0 ? await runMapped(() => params.source.unsafe(params.text, values)) : await runMapped(() => params.source(prepared.template, ...bound));
|
|
672
1124
|
observeResult({ scope: params.scope, effect: params.effect, result });
|
|
673
1125
|
return plainRows({ rows: finalRows({ result }) });
|
|
674
1126
|
}
|
|
@@ -683,8 +1135,152 @@ function bunAdapter(options) {
|
|
|
683
1135
|
const rows = finalRows({ result });
|
|
684
1136
|
return { rows: plainRows({ rows }), affectedRows: readAffectedRows({ rows }) };
|
|
685
1137
|
}
|
|
1138
|
+
const refusedOnSubLane = (message) => () => Promise.reject(laneGroupMisuse({ message }));
|
|
1139
|
+
function subLaneAdapter(params) {
|
|
1140
|
+
let used = false;
|
|
1141
|
+
return {
|
|
1142
|
+
dialect: DIALECT,
|
|
1143
|
+
provider: PROVIDER,
|
|
1144
|
+
budgets: BUDGETS,
|
|
1145
|
+
roundTrips: ROUND_TRIPS,
|
|
1146
|
+
get wire() {
|
|
1147
|
+
return activeWire;
|
|
1148
|
+
},
|
|
1149
|
+
execute: (call) => {
|
|
1150
|
+
if (used)
|
|
1151
|
+
return Promise.reject(laneGroupMisuse({ message: "a sub-lane issues ONE statement" }));
|
|
1152
|
+
used = true;
|
|
1153
|
+
if (call.execution !== undefined) {
|
|
1154
|
+
params.drop();
|
|
1155
|
+
return Promise.reject(laneGroupMisuse({ message: "a sub-lane statement takes no read options" }));
|
|
1156
|
+
}
|
|
1157
|
+
return params.issue(call);
|
|
1158
|
+
},
|
|
1159
|
+
executeUnsafe: refusedOnSubLane("a sub-lane runs no raw statement"),
|
|
1160
|
+
transaction: refusedOnSubLane("a sub-lane opens no transaction"),
|
|
1161
|
+
connect: refusedOnSubLane("a sub-lane does not connect"),
|
|
1162
|
+
disconnect: refusedOnSubLane("a sub-lane does not disconnect"),
|
|
1163
|
+
formatArrayParam: (values) => toPgArrayLiteral(values)
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
function groupSeatAdapter(params) {
|
|
1167
|
+
const { seat, opened, inner } = params;
|
|
1168
|
+
return subLaneAdapter({
|
|
1169
|
+
drop: () => {
|
|
1170
|
+
opened.drop(seat);
|
|
1171
|
+
},
|
|
1172
|
+
issue: (call) => {
|
|
1173
|
+
const running = inner.execute(call);
|
|
1174
|
+
running.then(undefined, () => {
|
|
1175
|
+
if (seat.held === undefined)
|
|
1176
|
+
opened.drop(seat);
|
|
1177
|
+
});
|
|
1178
|
+
return running;
|
|
1179
|
+
}
|
|
1180
|
+
});
|
|
1181
|
+
}
|
|
1182
|
+
function openPoolGroup() {
|
|
1183
|
+
const seats = [];
|
|
1184
|
+
let sealed = false;
|
|
1185
|
+
let sent = false;
|
|
1186
|
+
const flush = () => {
|
|
1187
|
+
if (!sealed || sent)
|
|
1188
|
+
return;
|
|
1189
|
+
for (const seat of seats)
|
|
1190
|
+
if (seat.open && !seat.done && seat.issued === undefined)
|
|
1191
|
+
return;
|
|
1192
|
+
sent = true;
|
|
1193
|
+
const entries = seats.flatMap((seat) => seat.issued === undefined ? [] : [seat.issued]);
|
|
1194
|
+
if (entries.length === 0)
|
|
1195
|
+
return;
|
|
1196
|
+
lifecycle.admit({ run: () => Promise.allSettled(entries.map((entry) => root.execute(entry.call))) }).then((settled) => {
|
|
1197
|
+
let failure;
|
|
1198
|
+
settled.forEach((outcome, index) => {
|
|
1199
|
+
const entry = entries[index];
|
|
1200
|
+
if (failure !== undefined)
|
|
1201
|
+
entry.reject(transactionAbortedError({ cause: failure.cause }));
|
|
1202
|
+
else if (outcome.status === "fulfilled")
|
|
1203
|
+
entry.resolve(outcome.value);
|
|
1204
|
+
else {
|
|
1205
|
+
failure = { cause: outcome.reason };
|
|
1206
|
+
entry.reject(outcome.reason);
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
}, (error) => {
|
|
1210
|
+
for (const entry of entries)
|
|
1211
|
+
entry.reject(error);
|
|
1212
|
+
});
|
|
1213
|
+
};
|
|
1214
|
+
return {
|
|
1215
|
+
lane: () => {
|
|
1216
|
+
if (sealed)
|
|
1217
|
+
throw laneGroupMisuse({ message: "a sub-lane is opened only before the group is sealed" });
|
|
1218
|
+
const seat = { open: true, done: false };
|
|
1219
|
+
seats.push(seat);
|
|
1220
|
+
return {
|
|
1221
|
+
adapter: subLaneAdapter({
|
|
1222
|
+
drop: () => {
|
|
1223
|
+
seat.done = true;
|
|
1224
|
+
flush();
|
|
1225
|
+
},
|
|
1226
|
+
issue: (call) => {
|
|
1227
|
+
if (!seat.open || sent) {
|
|
1228
|
+
seat.done = true;
|
|
1229
|
+
flush();
|
|
1230
|
+
return Promise.reject(laneGroupMisuse({ message: "a closed sub-lane issues nothing" }));
|
|
1231
|
+
}
|
|
1232
|
+
return new Promise((resolve, reject) => {
|
|
1233
|
+
seat.issued = { call, resolve, reject };
|
|
1234
|
+
flush();
|
|
1235
|
+
});
|
|
1236
|
+
}
|
|
1237
|
+
}),
|
|
1238
|
+
close: () => {
|
|
1239
|
+
if (!seat.open)
|
|
1240
|
+
return;
|
|
1241
|
+
seat.open = false;
|
|
1242
|
+
flush();
|
|
1243
|
+
}
|
|
1244
|
+
};
|
|
1245
|
+
},
|
|
1246
|
+
seal: () => {
|
|
1247
|
+
if (sealed)
|
|
1248
|
+
return;
|
|
1249
|
+
sealed = true;
|
|
1250
|
+
flush();
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
686
1254
|
function createAdapter(params) {
|
|
687
|
-
const { source, parentMode, scope, budget, lifetime, control } = params;
|
|
1255
|
+
const { source, parentMode, scope, budget, lifetime, control, final } = params;
|
|
1256
|
+
const finalOf = () => {
|
|
1257
|
+
if (final === undefined)
|
|
1258
|
+
return {};
|
|
1259
|
+
if (final.used)
|
|
1260
|
+
throw finalStatementMisuse();
|
|
1261
|
+
final.used = true;
|
|
1262
|
+
return { final: { discardRows: final.discardRows } };
|
|
1263
|
+
};
|
|
1264
|
+
const unorderedLane = (lane) => ({
|
|
1265
|
+
adapter: createAdapter({ source, parentMode: "tx", scope, budget, control: lane.control, ...params.commits === true ? { commits: true } : {} }),
|
|
1266
|
+
close: lane.close,
|
|
1267
|
+
group: (options) => {
|
|
1268
|
+
const opened = lane.group(options?.final === true && params.commits === true);
|
|
1269
|
+
if (opened === undefined)
|
|
1270
|
+
return;
|
|
1271
|
+
return {
|
|
1272
|
+
lane: () => {
|
|
1273
|
+
const { seat, control: seatCtl } = opened.seat();
|
|
1274
|
+
return { adapter: groupSeatAdapter({ seat, opened, inner: createAdapter({ source, parentMode: "tx", scope, budget, control: seatCtl }) }), close: () => {
|
|
1275
|
+
opened.close(seat);
|
|
1276
|
+
} };
|
|
1277
|
+
},
|
|
1278
|
+
seal: () => {
|
|
1279
|
+
opened.seal();
|
|
1280
|
+
}
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
688
1284
|
function assertHandle() {
|
|
689
1285
|
budget?.assertUsable({ stage: "statement" });
|
|
690
1286
|
if (lifetime?.active === false) {
|
|
@@ -701,8 +1297,12 @@ function bunAdapter(options) {
|
|
|
701
1297
|
refuseNestedTransactionOptions({ options: nestedOptions, provider: PROVIDER });
|
|
702
1298
|
assertHandle();
|
|
703
1299
|
budget?.assertUsable({ stage: "nested" });
|
|
1300
|
+
if (reservedControl.turn !== undefined) {
|
|
1301
|
+
await reservedControl.turn();
|
|
1302
|
+
assertHandle();
|
|
1303
|
+
}
|
|
704
1304
|
const savepointControl = (statement, cleanup) => {
|
|
705
|
-
if (reservedControl.deferred.routeSavepoint({ text: statement }) !== "send")
|
|
1305
|
+
if (reservedControl.deferred.routeSavepoint({ text: statement, cleanup }) !== "send")
|
|
706
1306
|
return;
|
|
707
1307
|
return reservedControl.dispatch({
|
|
708
1308
|
extended: true,
|
|
@@ -730,11 +1330,19 @@ function bunAdapter(options) {
|
|
|
730
1330
|
validateTransactionDeadline({
|
|
731
1331
|
options: transactionOptions,
|
|
732
1332
|
support: BUDGETS.transactionDeadline,
|
|
733
|
-
provider: PROVIDER
|
|
1333
|
+
provider: PROVIDER,
|
|
1334
|
+
settings: "transaction-local"
|
|
734
1335
|
});
|
|
735
1336
|
const readOnly = readOnlyRequested({ options: transactionOptions, provider: PROVIDER });
|
|
736
1337
|
const openingOptions = { isolationLevel: transactionOptions?.isolationLevel, readOnly };
|
|
737
|
-
const
|
|
1338
|
+
const timeout = transactionOptions?.timeout;
|
|
1339
|
+
if (timeout !== undefined)
|
|
1340
|
+
statementTimeoutStatement({ timeout });
|
|
1341
|
+
const settings = readTransactionSettings({ settings: transactionOptions?.settings, provider: PROVIDER });
|
|
1342
|
+
const settingsStatement = settings.length === 0 ? undefined : renderTransactionSettings({ settings });
|
|
1343
|
+
const settingsDeferred = settingsStatement !== undefined && usePrepared && transactionOptions?.deferSettings === true;
|
|
1344
|
+
const eagerOpening = settingsStatement !== undefined && !settingsDeferred || timeout !== undefined && (!usePrepared || timeout > MAX_STATEMENT_TIMEOUT_MS);
|
|
1345
|
+
const eagerOpen = eagerOpening && timeout !== undefined ? openTransactionStatement({ ...openingOptions, timeout }) : null;
|
|
738
1346
|
const reserved = await runMapped(() => pool.reserve());
|
|
739
1347
|
const budget = startTransactionBudget({ options: transactionOptions, provider: PROVIDER });
|
|
740
1348
|
const txScope = createSavepointScope();
|
|
@@ -742,26 +1350,74 @@ function bunAdapter(options) {
|
|
|
742
1350
|
reserved,
|
|
743
1351
|
budget,
|
|
744
1352
|
scope: txScope,
|
|
745
|
-
opening:
|
|
1353
|
+
opening: eagerOpening ? null : beginStatement(openingOptions),
|
|
1354
|
+
commits: usePrepared && budget.totalMs === null,
|
|
1355
|
+
...settingsDeferred ? {
|
|
1356
|
+
openingSettings: {
|
|
1357
|
+
text: settingsStatement.text,
|
|
1358
|
+
refused: (cause) => transactionSettingRefusedError({ provider: PROVIDER, settings, setting: findRefusedSetting({ settings, cause }), cause, deferred: true })
|
|
1359
|
+
}
|
|
1360
|
+
} : {}
|
|
746
1361
|
});
|
|
1362
|
+
if (!eagerOpening) {
|
|
1363
|
+
if (timeout !== undefined)
|
|
1364
|
+
control.deferred.defer({ text: STATEMENT_BOUND_TEXT, values: [String(timeout)] });
|
|
1365
|
+
if (settingsDeferred)
|
|
1366
|
+
control.deferred.defer({ text: settingsStatement.text, values: settingsStatement.values });
|
|
1367
|
+
}
|
|
747
1368
|
const commit = { progress: "not-sent" };
|
|
748
1369
|
let discard = false;
|
|
1370
|
+
let openingRollback;
|
|
1371
|
+
const openWithSettings = async (statement) => {
|
|
1372
|
+
await (eagerOpen === null ? controlQuery({ connection: reserved, text: beginStatement(openingOptions) }) : reserved.unsafe(eagerOpen)).then(undefined, (error) => {
|
|
1373
|
+
throw mapBunDriverError({ error });
|
|
1374
|
+
});
|
|
1375
|
+
const failed = await controlQuery({ connection: reserved, text: statement.text, values: statement.values }).then(() => {
|
|
1376
|
+
return;
|
|
1377
|
+
}, (error) => ({ error }));
|
|
1378
|
+
if (failed === undefined)
|
|
1379
|
+
return;
|
|
1380
|
+
const cause = mapBunDriverError({ error: failed.error });
|
|
1381
|
+
if (typeof cause.meta.sqlstate !== "string")
|
|
1382
|
+
throw cause;
|
|
1383
|
+
openingRollback = await reserved.unsafe("ROLLBACK").then(() => "acknowledged", () => "failed");
|
|
1384
|
+
if (openingRollback === "failed")
|
|
1385
|
+
discard = true;
|
|
1386
|
+
const setting = findRefusedSetting({ settings, cause });
|
|
1387
|
+
throw transactionSettingRefusedError({ provider: PROVIDER, settings, setting, cause });
|
|
1388
|
+
};
|
|
749
1389
|
try {
|
|
750
|
-
if (
|
|
1390
|
+
if (eagerOpening && settingsStatement !== undefined)
|
|
1391
|
+
await openWithSettings(settingsStatement);
|
|
1392
|
+
else if (eagerOpen !== null)
|
|
751
1393
|
await runMapped(() => reserved.unsafe(eagerOpen));
|
|
752
1394
|
const txAdapter = createAdapter({
|
|
753
1395
|
source: reserved,
|
|
754
1396
|
parentMode: "tx",
|
|
755
1397
|
scope: txScope,
|
|
756
1398
|
budget,
|
|
757
|
-
control
|
|
1399
|
+
control,
|
|
1400
|
+
ordered: usePrepared && budget.totalMs === null,
|
|
1401
|
+
topLevel: true,
|
|
1402
|
+
...control.commits ? { commits: true } : {}
|
|
758
1403
|
});
|
|
759
1404
|
const result = await runRootScope({ scope: txScope, body: () => fn(txAdapter) });
|
|
1405
|
+
const riding = control.ridden();
|
|
1406
|
+
if (riding !== undefined) {
|
|
1407
|
+
commit.progress = "sent";
|
|
1408
|
+
await riding.settled;
|
|
1409
|
+
if (riding.state === "committed") {
|
|
1410
|
+
budget.close({ outcome: "committed" });
|
|
1411
|
+
return result;
|
|
1412
|
+
}
|
|
1413
|
+
throw riding.error;
|
|
1414
|
+
}
|
|
760
1415
|
budget.assertUsable({ stage: "commit" });
|
|
761
1416
|
if (!control.deferred.opened()) {
|
|
762
1417
|
budget.close({ outcome: "committed" });
|
|
763
1418
|
return result;
|
|
764
1419
|
}
|
|
1420
|
+
control.deferred.dropReleases();
|
|
765
1421
|
const reply = await control.dispatch({
|
|
766
1422
|
extended: true,
|
|
767
1423
|
effect: "write",
|
|
@@ -778,13 +1434,26 @@ function bunAdapter(options) {
|
|
|
778
1434
|
budget.close({ outcome: "committed" });
|
|
779
1435
|
return result;
|
|
780
1436
|
} catch (error) {
|
|
1437
|
+
const riding = control.ridden();
|
|
1438
|
+
if (riding !== undefined) {
|
|
1439
|
+
await riding.settled;
|
|
1440
|
+
if (riding.state === "committed") {
|
|
1441
|
+
budget.close({ outcome: "committed" });
|
|
1442
|
+
throw error;
|
|
1443
|
+
}
|
|
1444
|
+
commit.progress = riding.state === "lost" ? "unanswered" : "answered";
|
|
1445
|
+
if (riding.state === "lost")
|
|
1446
|
+
discard = true;
|
|
1447
|
+
}
|
|
781
1448
|
if (txScope.state.poisoned !== null)
|
|
782
1449
|
discard = true;
|
|
783
1450
|
if (commit.progress === "sent")
|
|
784
1451
|
commit.progress = isConnectionLoss({ error }) ? "unanswered" : "answered";
|
|
785
1452
|
let rollback = "not-needed";
|
|
786
1453
|
try {
|
|
787
|
-
if (
|
|
1454
|
+
if (openingRollback !== undefined)
|
|
1455
|
+
rollback = openingRollback;
|
|
1456
|
+
else if (riding === undefined && control.deferred.opened()) {
|
|
788
1457
|
await control.dispatch({ extended: false, cleanup: true, send: (connection) => connection.unsafe("ROLLBACK") });
|
|
789
1458
|
rollback = "acknowledged";
|
|
790
1459
|
}
|
|
@@ -792,7 +1461,7 @@ function bunAdapter(options) {
|
|
|
792
1461
|
discard = true;
|
|
793
1462
|
rollback = "failed";
|
|
794
1463
|
}
|
|
795
|
-
const own = commit.progress === "unanswered" && txScope.state.poisoned === null ? unknownOutcome({ cause: error }) : error;
|
|
1464
|
+
const own = commit.progress === "unanswered" && txScope.state.poisoned === null ? unknownOutcome({ cause: riding?.state === "lost" ? riding.error : error }) : error;
|
|
796
1465
|
throw settleFailedTransaction({
|
|
797
1466
|
error: own,
|
|
798
1467
|
provider: PROVIDER,
|
|
@@ -809,16 +1478,67 @@ function bunAdapter(options) {
|
|
|
809
1478
|
reserved.release();
|
|
810
1479
|
}
|
|
811
1480
|
}
|
|
812
|
-
|
|
1481
|
+
const handle = {
|
|
813
1482
|
dialect: DIALECT,
|
|
814
1483
|
provider: PROVIDER,
|
|
815
1484
|
budgets: BUDGETS,
|
|
816
1485
|
readCancellation: "before-dispatch",
|
|
817
1486
|
roundTrips: ROUND_TRIPS,
|
|
1487
|
+
readSnapshot: READ_SNAPSHOT,
|
|
1488
|
+
pipelining,
|
|
1489
|
+
...params.ordered !== true || control === undefined ? {} : {
|
|
1490
|
+
orderedPipeline: true,
|
|
1491
|
+
pipeline: ({ lanes: count, ordered }) => {
|
|
1492
|
+
assertHandle();
|
|
1493
|
+
if (!Number.isSafeInteger(count) || count < 1) {
|
|
1494
|
+
throw new VibeError7({ code: "VIBE_VALIDATION", message: "pipeline() needs a positive whole number of lanes", meta: { lanes: count } });
|
|
1495
|
+
}
|
|
1496
|
+
if (ordered !== true)
|
|
1497
|
+
return Array.from({ length: count }, () => unorderedLane(control.openLane()));
|
|
1498
|
+
const laneHandle = (lane) => ({
|
|
1499
|
+
adapter: createAdapter({ source, parentMode: "tx", scope, budget, control: control.ordered.control(lane), ...params.commits === true ? { commits: true } : {} }),
|
|
1500
|
+
close: () => {
|
|
1501
|
+
control.ordered.close(lane);
|
|
1502
|
+
},
|
|
1503
|
+
follow: () => {
|
|
1504
|
+
assertHandle();
|
|
1505
|
+
return laneHandle(control.ordered.follow(lane));
|
|
1506
|
+
}
|
|
1507
|
+
});
|
|
1508
|
+
return control.ordered.open(count).map(laneHandle);
|
|
1509
|
+
}
|
|
1510
|
+
},
|
|
818
1511
|
...parentMode === "pool" ? {} : { rowChanges: () => {
|
|
819
1512
|
assertHandle();
|
|
820
1513
|
return readRowChanges({ scope });
|
|
821
1514
|
} },
|
|
1515
|
+
...control === undefined || params.commits !== true || !control.commits || scope.depth !== 0 || final !== undefined ? {} : {
|
|
1516
|
+
withCommit: (options) => createAdapter({
|
|
1517
|
+
source,
|
|
1518
|
+
parentMode: "tx",
|
|
1519
|
+
scope,
|
|
1520
|
+
budget,
|
|
1521
|
+
control,
|
|
1522
|
+
commits: true,
|
|
1523
|
+
final: { used: false, discardRows: options?.rows === "discard" }
|
|
1524
|
+
})
|
|
1525
|
+
},
|
|
1526
|
+
...control === undefined || params.topLevel !== true || !usePrepared ? {} : {
|
|
1527
|
+
deferStatement: (statement) => {
|
|
1528
|
+
assertHandle();
|
|
1529
|
+
if (control.ridden() !== undefined)
|
|
1530
|
+
throw commitSentRefusal();
|
|
1531
|
+
const values = serializeParams({ values: [...statement.values], namedStatements: true });
|
|
1532
|
+
if (!serverAnswered({ values })) {
|
|
1533
|
+
throw new VibeError7({
|
|
1534
|
+
code: "VIBE_VALIDATION",
|
|
1535
|
+
message: "deferStatement(): adapter-bun defers a statement only when every value is text, null or bytes after serialization",
|
|
1536
|
+
meta: { reason: "deferred-value-not-text" }
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1539
|
+
control.deferred.defer({ text: statement.text, values });
|
|
1540
|
+
}
|
|
1541
|
+
},
|
|
822
1542
|
get wire() {
|
|
823
1543
|
return activeWire;
|
|
824
1544
|
},
|
|
@@ -833,7 +1553,8 @@ function bunAdapter(options) {
|
|
|
833
1553
|
assertHandle();
|
|
834
1554
|
assertReadExecution({ ...execParams, provider: PROVIDER, stage: "waiting-for-connection" });
|
|
835
1555
|
};
|
|
836
|
-
const
|
|
1556
|
+
const view = finalOf();
|
|
1557
|
+
const executor = control === undefined ? source : control.executor({ effect: execParams.effect, guard, ...wireSettled ? view : {} });
|
|
837
1558
|
const result = runExecute({ source: executor, ...parentMode === "pool" ? {} : { scope }, ...execParams });
|
|
838
1559
|
return parentMode === "pool" ? result : result.catch((error) => abortTransactionOnStatementFailure({ scope, error }));
|
|
839
1560
|
} });
|
|
@@ -844,7 +1565,8 @@ function bunAdapter(options) {
|
|
|
844
1565
|
if (turn !== undefined)
|
|
845
1566
|
await turn;
|
|
846
1567
|
assertHandle();
|
|
847
|
-
const
|
|
1568
|
+
const view = finalOf();
|
|
1569
|
+
const executor = control === undefined ? source : control.executor({ effect: execParams.effect, guard: assertHandle, ...view });
|
|
848
1570
|
const result = runExecuteUnsafe({ source: executor, ...parentMode === "pool" ? {} : { scope }, ...execParams });
|
|
849
1571
|
return parentMode === "pool" ? result : result.catch((error) => abortTransactionOnStatementFailure({ scope, error }));
|
|
850
1572
|
},
|
|
@@ -856,8 +1578,13 @@ function bunAdapter(options) {
|
|
|
856
1578
|
assertHandle();
|
|
857
1579
|
throw transactionLifecycleRefusal({ method: "connect" });
|
|
858
1580
|
}
|
|
859
|
-
const
|
|
1581
|
+
const opened = await Promise.allSettled(Array.from({ length: Math.max(1, poolMin) }, () => runMapped(() => source.reserve())));
|
|
1582
|
+
const held = opened.flatMap((outcome) => outcome.status === "fulfilled" ? [outcome.value] : []);
|
|
860
1583
|
try {
|
|
1584
|
+
const failed = opened.find((outcome) => outcome.status === "rejected");
|
|
1585
|
+
if (failed !== undefined)
|
|
1586
|
+
throw failed.reason;
|
|
1587
|
+
const reserved = held[0];
|
|
861
1588
|
if (!wireSettled && wireProbe === null) {
|
|
862
1589
|
wireProbe = probeJsonWire(reserved).finally(() => {
|
|
863
1590
|
wireProbe = null;
|
|
@@ -865,7 +1592,8 @@ function bunAdapter(options) {
|
|
|
865
1592
|
await wireProbe;
|
|
866
1593
|
}
|
|
867
1594
|
} finally {
|
|
868
|
-
reserved
|
|
1595
|
+
for (const reserved of held)
|
|
1596
|
+
reserved.release();
|
|
869
1597
|
}
|
|
870
1598
|
},
|
|
871
1599
|
async disconnect() {
|
|
@@ -880,6 +1608,7 @@ function bunAdapter(options) {
|
|
|
880
1608
|
return toPgArrayLiteral(values);
|
|
881
1609
|
}
|
|
882
1610
|
};
|
|
1611
|
+
return handle;
|
|
883
1612
|
}
|
|
884
1613
|
function poolAdapter() {
|
|
885
1614
|
return createAdapter({
|
|
@@ -976,7 +1705,16 @@ function bunAdapter(options) {
|
|
|
976
1705
|
budgets: BUDGETS,
|
|
977
1706
|
readCancellation: "before-dispatch",
|
|
978
1707
|
roundTrips: ROUND_TRIPS,
|
|
1708
|
+
readSnapshot: READ_SNAPSHOT,
|
|
1709
|
+
pipelining,
|
|
979
1710
|
transactionOutcome: "reported",
|
|
1711
|
+
transactionSettings: "transaction-local",
|
|
1712
|
+
connectionGroup: () => {
|
|
1713
|
+
const closed = lifecycle.refusal();
|
|
1714
|
+
if (closed !== undefined)
|
|
1715
|
+
throw closed;
|
|
1716
|
+
return openPoolGroup();
|
|
1717
|
+
},
|
|
980
1718
|
get wire() {
|
|
981
1719
|
return activeWire;
|
|
982
1720
|
},
|
|
@@ -1072,4 +1810,4 @@ export {
|
|
|
1072
1810
|
validateTransactionDeadline2 as validateTransactionDeadline
|
|
1073
1811
|
};
|
|
1074
1812
|
|
|
1075
|
-
//# debugId=
|
|
1813
|
+
//# debugId=B36822475D1CE05764756E2164756E21
|