@antfly/embedded 0.0.0-stage → 0.3.0-rc.1

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.
@@ -0,0 +1,168 @@
1
+ // ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.7.0_postcss@8.5.15_supports-color@10.2.2_tsx@4.22.4_typescript@5.9.3_yaml@2.9.0/node_modules/tsup/assets/esm_shims.js
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+ var getFilename = () => fileURLToPath(import.meta.url);
5
+ var __filename = /* @__PURE__ */ getFilename();
6
+
7
+ // src/sql-types.ts
8
+ var SqlError = class extends Error {
9
+ constructor(diagnostic, transactionId) {
10
+ super(`${diagnostic.code}: ${diagnostic.message}`);
11
+ this.diagnostic = diagnostic;
12
+ this.transactionId = transactionId;
13
+ this.name = "SqlError";
14
+ this.sqlstate = diagnostic.code;
15
+ }
16
+ diagnostic;
17
+ transactionId;
18
+ sqlstate;
19
+ };
20
+
21
+ // src/sql.ts
22
+ function request(statement, parameters, session, streaming = false) {
23
+ return JSON.stringify(
24
+ { statement, parameters, session_id: session, limit: streaming ? void 0 : 4096 },
25
+ (_key, value) => {
26
+ if (typeof value === "bigint") {
27
+ if (value < -(1n << 63n) || value >= 1n << 63n)
28
+ throw new RangeError("integer parameter is outside the signed 64-bit range");
29
+ return value.toString();
30
+ }
31
+ if (typeof value === "number" && (!Number.isFinite(value) || Number.isInteger(value) && !Number.isSafeInteger(value)))
32
+ throw new RangeError("use bigint for integer parameters outside JavaScript's safe range");
33
+ return value;
34
+ }
35
+ );
36
+ }
37
+ function result(wire) {
38
+ return {
39
+ columns: wire.columns,
40
+ commandTag: wire.command_tag,
41
+ rowsAffected: BigInt(wire.rows_affected),
42
+ rows: wire.rows.map(
43
+ (row, rowIndex) => Object.fromEntries(
44
+ wire.columns.map((column, index) => [
45
+ column.name,
46
+ wire.sql_nulls?.[rowIndex]?.[index] ? null : column.type === "integer" && row[index] !== null ? BigInt(row[index]) : row[index]
47
+ ])
48
+ )
49
+ )
50
+ };
51
+ }
52
+ var Connection = class _Connection {
53
+ constructor(database, session, direct = false) {
54
+ this.database = database;
55
+ this.session = session;
56
+ this.direct = direct;
57
+ }
58
+ database;
59
+ session;
60
+ direct;
61
+ #pending = Promise.resolve();
62
+ #closed = false;
63
+ static async open(database) {
64
+ return new _Connection(database, await database.openSqlSession());
65
+ }
66
+ #run(operation) {
67
+ if (this.#closed) return Promise.reject(new Error("SQL connection is closed"));
68
+ if (this.direct) return operation();
69
+ const work = this.#pending.then(operation);
70
+ this.#pending = work.catch(() => void 0);
71
+ return work;
72
+ }
73
+ execute(statement, parameters = []) {
74
+ return this.#run(
75
+ async () => result(
76
+ await this.database.sqlJson(request(statement, parameters, this.session))
77
+ )
78
+ );
79
+ }
80
+ query(statement, parameters = []) {
81
+ return this.#run(async () => {
82
+ let cursor;
83
+ try {
84
+ cursor = await this.database.openSqlCursor(
85
+ request(statement, parameters, this.session, true)
86
+ );
87
+ } catch (error) {
88
+ if (!(error instanceof SqlError) || error.sqlstate !== "0A000") throw error;
89
+ return result(
90
+ await this.database.sqlJson(request(statement, parameters, this.session))
91
+ );
92
+ }
93
+ let output;
94
+ try {
95
+ while (true) {
96
+ const page = await this.database.fetchSqlCursor(cursor);
97
+ const decoded = result(page.result);
98
+ if (output) output.rows.push(...decoded.rows);
99
+ else output = decoded;
100
+ if (page.exhausted) return output;
101
+ }
102
+ } finally {
103
+ await this.database.closeSqlCursor(cursor);
104
+ }
105
+ });
106
+ }
107
+ async *stream(statement, parameters = [], chunkSize = 128) {
108
+ if (!Number.isInteger(chunkSize) || chunkSize < 1 || chunkSize > 4096)
109
+ throw new RangeError("chunkSize must be 1..4096");
110
+ if (this.#closed) throw new Error("SQL connection is closed");
111
+ const previous = this.#pending;
112
+ let release = () => {
113
+ };
114
+ if (!this.direct)
115
+ this.#pending = new Promise((resolve) => {
116
+ release = resolve;
117
+ });
118
+ await previous;
119
+ let cursor;
120
+ try {
121
+ cursor = await this.database.openSqlCursor(
122
+ request(statement, parameters, this.session, true)
123
+ );
124
+ while (true) {
125
+ const page = await this.database.fetchSqlCursor(cursor, chunkSize);
126
+ yield result(page.result);
127
+ if (page.exhausted) break;
128
+ }
129
+ } finally {
130
+ try {
131
+ if (cursor !== void 0) await this.database.closeSqlCursor(cursor);
132
+ } finally {
133
+ release();
134
+ }
135
+ }
136
+ }
137
+ transaction(operation) {
138
+ return this.#run(async () => {
139
+ const transaction = new _Connection(this.database, this.session, true);
140
+ await transaction.execute("BEGIN ISOLATION LEVEL READ COMMITTED");
141
+ try {
142
+ const value = await operation(transaction);
143
+ await transaction.execute("COMMIT");
144
+ return value;
145
+ } catch (error) {
146
+ try {
147
+ await transaction.execute("ROLLBACK");
148
+ } catch {
149
+ }
150
+ throw error;
151
+ } finally {
152
+ transaction.#closed = true;
153
+ }
154
+ });
155
+ }
156
+ async close() {
157
+ if (this.#closed) return;
158
+ this.#closed = true;
159
+ await this.#pending;
160
+ await this.database.closeSqlSession(this.session);
161
+ }
162
+ };
163
+
164
+ export {
165
+ __filename,
166
+ SqlError,
167
+ Connection
168
+ };