@ohos-ports/libsql-client 0.18.0-beta.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 +128 -0
- package/lib-cjs/hrana.js +372 -0
- package/lib-cjs/http.js +268 -0
- package/lib-cjs/node.js +41 -0
- package/lib-cjs/package.json +3 -0
- package/lib-cjs/sql_cache.js +91 -0
- package/lib-cjs/sqlite3.js +720 -0
- package/lib-cjs/web.js +41 -0
- package/lib-cjs/ws.js +395 -0
- package/lib-esm/hrana.d.ts +23 -0
- package/lib-esm/hrana.js +341 -0
- package/lib-esm/http.d.ts +39 -0
- package/lib-esm/http.js +232 -0
- package/lib-esm/node.d.ts +7 -0
- package/lib-esm/node.js +23 -0
- package/lib-esm/sql_cache.d.ts +7 -0
- package/lib-esm/sql_cache.js +87 -0
- package/lib-esm/sqlite3.d.ts +53 -0
- package/lib-esm/sqlite3.js +695 -0
- package/lib-esm/web.d.ts +6 -0
- package/lib-esm/web.js +22 -0
- package/lib-esm/ws.d.ts +50 -0
- package/lib-esm/ws.js +359 -0
- package/package.json +123 -0
package/lib-cjs/web.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports._createClient = exports.createClient = void 0;
|
|
18
|
+
const api_1 = require("@libsql/core/api");
|
|
19
|
+
const config_1 = require("@libsql/core/config");
|
|
20
|
+
const util_1 = require("@libsql/core/util");
|
|
21
|
+
const ws_js_1 = require("./ws.js");
|
|
22
|
+
const http_js_1 = require("./http.js");
|
|
23
|
+
__exportStar(require("@libsql/core/api"), exports);
|
|
24
|
+
function createClient(config) {
|
|
25
|
+
return _createClient((0, config_1.expandConfig)(config, true));
|
|
26
|
+
}
|
|
27
|
+
exports.createClient = createClient;
|
|
28
|
+
/** @private */
|
|
29
|
+
function _createClient(config) {
|
|
30
|
+
if (config.scheme === "ws" || config.scheme === "wss") {
|
|
31
|
+
return (0, ws_js_1._createClient)(config);
|
|
32
|
+
}
|
|
33
|
+
else if (config.scheme === "http" || config.scheme === "https") {
|
|
34
|
+
return (0, http_js_1._createClient)(config);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
throw new api_1.LibsqlError('The client that uses Web standard APIs supports only "libsql:", "wss:", "ws:", "https:" and "http:" URLs, ' +
|
|
38
|
+
`got ${JSON.stringify(config.scheme + ":")}. For more information, please read ${util_1.supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports._createClient = _createClient;
|
package/lib-cjs/ws.js
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
29
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.WsTransaction = exports.WsClient = exports._createClient = exports.createClient = void 0;
|
|
33
|
+
const hrana = __importStar(require("@libsql/hrana-client"));
|
|
34
|
+
const api_1 = require("@libsql/core/api");
|
|
35
|
+
const config_1 = require("@libsql/core/config");
|
|
36
|
+
const hrana_js_1 = require("./hrana.js");
|
|
37
|
+
const sql_cache_js_1 = require("./sql_cache.js");
|
|
38
|
+
const uri_1 = require("@libsql/core/uri");
|
|
39
|
+
const util_1 = require("@libsql/core/util");
|
|
40
|
+
const promise_limit_1 = __importDefault(require("promise-limit"));
|
|
41
|
+
__exportStar(require("@libsql/core/api"), exports);
|
|
42
|
+
function createClient(config) {
|
|
43
|
+
return _createClient((0, config_1.expandConfig)(config, false));
|
|
44
|
+
}
|
|
45
|
+
exports.createClient = createClient;
|
|
46
|
+
/** @private */
|
|
47
|
+
function _createClient(config) {
|
|
48
|
+
if (config.scheme !== "wss" && config.scheme !== "ws") {
|
|
49
|
+
throw new api_1.LibsqlError('The WebSocket client supports only "libsql:", "wss:" and "ws:" URLs, ' +
|
|
50
|
+
`got ${JSON.stringify(config.scheme + ":")}. For more information, please read ${util_1.supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
|
|
51
|
+
}
|
|
52
|
+
if (config.encryptionKey !== undefined) {
|
|
53
|
+
throw new api_1.LibsqlError("Encryption key is not supported by the remote client.", "ENCRYPTION_KEY_NOT_SUPPORTED");
|
|
54
|
+
}
|
|
55
|
+
if (config.scheme === "ws" && config.tls) {
|
|
56
|
+
throw new api_1.LibsqlError(`A "ws:" URL cannot opt into TLS by using ?tls=1`, "URL_INVALID");
|
|
57
|
+
}
|
|
58
|
+
else if (config.scheme === "wss" && !config.tls) {
|
|
59
|
+
throw new api_1.LibsqlError(`A "wss:" URL cannot opt out of TLS by using ?tls=0`, "URL_INVALID");
|
|
60
|
+
}
|
|
61
|
+
const url = (0, uri_1.encodeBaseUrl)(config.scheme, config.authority, config.path);
|
|
62
|
+
let client;
|
|
63
|
+
try {
|
|
64
|
+
client = hrana.openWs(url, config.authToken);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
if (e instanceof hrana.WebSocketUnsupportedError) {
|
|
68
|
+
const suggestedScheme = config.scheme === "wss" ? "https" : "http";
|
|
69
|
+
const suggestedUrl = (0, uri_1.encodeBaseUrl)(suggestedScheme, config.authority, config.path);
|
|
70
|
+
throw new api_1.LibsqlError("This environment does not support WebSockets, please switch to the HTTP client by using " +
|
|
71
|
+
`a "${suggestedScheme}:" URL (${JSON.stringify(suggestedUrl)}). ` +
|
|
72
|
+
`For more information, please read ${util_1.supportedUrlLink}`, "WEBSOCKETS_NOT_SUPPORTED");
|
|
73
|
+
}
|
|
74
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
75
|
+
}
|
|
76
|
+
return new WsClient(client, url, config.authToken, config.intMode, config.concurrency);
|
|
77
|
+
}
|
|
78
|
+
exports._createClient = _createClient;
|
|
79
|
+
const maxConnAgeMillis = 60 * 1000;
|
|
80
|
+
const sqlCacheCapacity = 100;
|
|
81
|
+
class WsClient {
|
|
82
|
+
#url;
|
|
83
|
+
#authToken;
|
|
84
|
+
#intMode;
|
|
85
|
+
// State of the current connection. The `hrana.WsClient` inside may be closed at any moment due to an
|
|
86
|
+
// asynchronous error.
|
|
87
|
+
#connState;
|
|
88
|
+
// If defined, this is a connection that will be used in the future, once it is ready.
|
|
89
|
+
#futureConnState;
|
|
90
|
+
closed;
|
|
91
|
+
protocol;
|
|
92
|
+
#isSchemaDatabase;
|
|
93
|
+
#promiseLimitFunction;
|
|
94
|
+
/** @private */
|
|
95
|
+
constructor(client, url, authToken, intMode, concurrency) {
|
|
96
|
+
this.#url = url;
|
|
97
|
+
this.#authToken = authToken;
|
|
98
|
+
this.#intMode = intMode;
|
|
99
|
+
this.#connState = this.#openConn(client);
|
|
100
|
+
this.#futureConnState = undefined;
|
|
101
|
+
this.closed = false;
|
|
102
|
+
this.protocol = "ws";
|
|
103
|
+
this.#promiseLimitFunction = (0, promise_limit_1.default)(concurrency);
|
|
104
|
+
}
|
|
105
|
+
async limit(fn) {
|
|
106
|
+
return this.#promiseLimitFunction(fn);
|
|
107
|
+
}
|
|
108
|
+
async execute(stmtOrSql, args) {
|
|
109
|
+
let stmt;
|
|
110
|
+
if (typeof stmtOrSql === "string") {
|
|
111
|
+
stmt = {
|
|
112
|
+
sql: stmtOrSql,
|
|
113
|
+
args: args || [],
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
stmt = stmtOrSql;
|
|
118
|
+
}
|
|
119
|
+
return this.limit(async () => {
|
|
120
|
+
const streamState = await this.#openStream();
|
|
121
|
+
try {
|
|
122
|
+
const hranaStmt = (0, hrana_js_1.stmtToHrana)(stmt);
|
|
123
|
+
// Schedule all operations synchronously, so they will be pipelined and executed in a single
|
|
124
|
+
// network roundtrip.
|
|
125
|
+
streamState.conn.sqlCache.apply([hranaStmt]);
|
|
126
|
+
const hranaRowsPromise = streamState.stream.query(hranaStmt);
|
|
127
|
+
streamState.stream.closeGracefully();
|
|
128
|
+
const hranaRowsResult = await hranaRowsPromise;
|
|
129
|
+
return (0, hrana_js_1.resultSetFromHrana)(hranaRowsResult);
|
|
130
|
+
}
|
|
131
|
+
catch (e) {
|
|
132
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
this._closeStream(streamState);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async batch(stmts, mode = "deferred") {
|
|
140
|
+
return this.limit(async () => {
|
|
141
|
+
const streamState = await this.#openStream();
|
|
142
|
+
try {
|
|
143
|
+
const normalizedStmts = stmts.map((stmt) => {
|
|
144
|
+
if (Array.isArray(stmt)) {
|
|
145
|
+
return {
|
|
146
|
+
sql: stmt[0],
|
|
147
|
+
args: stmt[1] || [],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return stmt;
|
|
151
|
+
});
|
|
152
|
+
const hranaStmts = normalizedStmts.map(hrana_js_1.stmtToHrana);
|
|
153
|
+
const version = await streamState.conn.client.getVersion();
|
|
154
|
+
// Schedule all operations synchronously, so they will be pipelined and executed in a single
|
|
155
|
+
// network roundtrip.
|
|
156
|
+
streamState.conn.sqlCache.apply(hranaStmts);
|
|
157
|
+
const batch = streamState.stream.batch(version >= 3);
|
|
158
|
+
const resultsPromise = (0, hrana_js_1.executeHranaBatch)(mode, version, batch, hranaStmts);
|
|
159
|
+
const results = await resultsPromise;
|
|
160
|
+
return results;
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
this._closeStream(streamState);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async migrate(stmts) {
|
|
171
|
+
return this.limit(async () => {
|
|
172
|
+
const streamState = await this.#openStream();
|
|
173
|
+
try {
|
|
174
|
+
const hranaStmts = stmts.map(hrana_js_1.stmtToHrana);
|
|
175
|
+
const version = await streamState.conn.client.getVersion();
|
|
176
|
+
// Schedule all operations synchronously, so they will be pipelined and executed in a single
|
|
177
|
+
// network roundtrip.
|
|
178
|
+
const batch = streamState.stream.batch(version >= 3);
|
|
179
|
+
const resultsPromise = (0, hrana_js_1.executeHranaBatch)("deferred", version, batch, hranaStmts, true);
|
|
180
|
+
const results = await resultsPromise;
|
|
181
|
+
return results;
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
185
|
+
}
|
|
186
|
+
finally {
|
|
187
|
+
this._closeStream(streamState);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
async transaction(mode = "write") {
|
|
192
|
+
return this.limit(async () => {
|
|
193
|
+
const streamState = await this.#openStream();
|
|
194
|
+
try {
|
|
195
|
+
const version = await streamState.conn.client.getVersion();
|
|
196
|
+
// the BEGIN statement will be batched with the first statement on the transaction to save a
|
|
197
|
+
// network roundtrip
|
|
198
|
+
return new WsTransaction(this, streamState, mode, version);
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
this._closeStream(streamState);
|
|
202
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
async executeMultiple(sql) {
|
|
207
|
+
return this.limit(async () => {
|
|
208
|
+
const streamState = await this.#openStream();
|
|
209
|
+
try {
|
|
210
|
+
// Schedule all operations synchronously, so they will be pipelined and executed in a single
|
|
211
|
+
// network roundtrip.
|
|
212
|
+
const promise = streamState.stream.sequence(sql);
|
|
213
|
+
streamState.stream.closeGracefully();
|
|
214
|
+
await promise;
|
|
215
|
+
}
|
|
216
|
+
catch (e) {
|
|
217
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
this._closeStream(streamState);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
sync() {
|
|
225
|
+
throw new api_1.LibsqlError("sync not supported in ws mode", "SYNC_NOT_SUPPORTED");
|
|
226
|
+
}
|
|
227
|
+
async #openStream() {
|
|
228
|
+
if (this.closed) {
|
|
229
|
+
throw new api_1.LibsqlError("The client is closed", "CLIENT_CLOSED");
|
|
230
|
+
}
|
|
231
|
+
const now = new Date();
|
|
232
|
+
const ageMillis = now.valueOf() - this.#connState.openTime.valueOf();
|
|
233
|
+
if (ageMillis > maxConnAgeMillis &&
|
|
234
|
+
this.#futureConnState === undefined) {
|
|
235
|
+
// The existing connection is too old, let's open a new one.
|
|
236
|
+
const futureConnState = this.#openConn();
|
|
237
|
+
this.#futureConnState = futureConnState;
|
|
238
|
+
// However, if we used `futureConnState` immediately, we would introduce additional latency,
|
|
239
|
+
// because we would have to wait for the WebSocket handshake to complete, even though we may a
|
|
240
|
+
// have perfectly good existing connection in `this.#connState`!
|
|
241
|
+
//
|
|
242
|
+
// So we wait until the `hrana.Client.getVersion()` operation completes (which happens when the
|
|
243
|
+
// WebSocket hanshake completes), and only then we replace `this.#connState` with
|
|
244
|
+
// `futureConnState`, which is stored in `this.#futureConnState` in the meantime.
|
|
245
|
+
futureConnState.client.getVersion().then((_version) => {
|
|
246
|
+
if (this.#connState !== futureConnState) {
|
|
247
|
+
// We need to close `this.#connState` before we replace it. However, it is possible
|
|
248
|
+
// that `this.#connState` has already been replaced: see the code below.
|
|
249
|
+
if (this.#connState.streamStates.size === 0) {
|
|
250
|
+
this.#connState.client.close();
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
// If there are existing streams on the connection, we must not close it, because
|
|
254
|
+
// these streams would be broken. The last stream to be closed will also close the
|
|
255
|
+
// connection in `_closeStream()`.
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
this.#connState = futureConnState;
|
|
259
|
+
this.#futureConnState = undefined;
|
|
260
|
+
}, (_e) => {
|
|
261
|
+
// If the new connection could not be established, let's just ignore the error and keep
|
|
262
|
+
// using the existing connection.
|
|
263
|
+
this.#futureConnState = undefined;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
if (this.#connState.client.closed) {
|
|
267
|
+
// An error happened on this connection and it has been closed. Let's try to seamlessly reconnect.
|
|
268
|
+
try {
|
|
269
|
+
if (this.#futureConnState !== undefined) {
|
|
270
|
+
// We are already in the process of opening a new connection, so let's just use it
|
|
271
|
+
// immediately.
|
|
272
|
+
this.#connState = this.#futureConnState;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
this.#connState = this.#openConn();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const connState = this.#connState;
|
|
283
|
+
try {
|
|
284
|
+
// Now we wait for the WebSocket handshake to complete (if it hasn't completed yet). Note that
|
|
285
|
+
// this does not increase latency, because any messages that we would send on the WebSocket before
|
|
286
|
+
// the handshake would be queued until the handshake is completed anyway.
|
|
287
|
+
if (connState.useSqlCache === undefined) {
|
|
288
|
+
connState.useSqlCache =
|
|
289
|
+
(await connState.client.getVersion()) >= 2;
|
|
290
|
+
if (connState.useSqlCache) {
|
|
291
|
+
connState.sqlCache.capacity = sqlCacheCapacity;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const stream = connState.client.openStream();
|
|
295
|
+
stream.intMode = this.#intMode;
|
|
296
|
+
const streamState = { conn: connState, stream };
|
|
297
|
+
connState.streamStates.add(streamState);
|
|
298
|
+
return streamState;
|
|
299
|
+
}
|
|
300
|
+
catch (e) {
|
|
301
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
#openConn(client) {
|
|
305
|
+
try {
|
|
306
|
+
client ??= hrana.openWs(this.#url, this.#authToken);
|
|
307
|
+
return {
|
|
308
|
+
client,
|
|
309
|
+
useSqlCache: undefined,
|
|
310
|
+
sqlCache: new sql_cache_js_1.SqlCache(client, 0),
|
|
311
|
+
openTime: new Date(),
|
|
312
|
+
streamStates: new Set(),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
catch (e) {
|
|
316
|
+
throw (0, hrana_js_1.mapHranaError)(e);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async reconnect() {
|
|
320
|
+
try {
|
|
321
|
+
for (const st of Array.from(this.#connState.streamStates)) {
|
|
322
|
+
try {
|
|
323
|
+
st.stream.close();
|
|
324
|
+
}
|
|
325
|
+
catch { }
|
|
326
|
+
}
|
|
327
|
+
this.#connState.client.close();
|
|
328
|
+
}
|
|
329
|
+
catch { }
|
|
330
|
+
if (this.#futureConnState) {
|
|
331
|
+
try {
|
|
332
|
+
this.#futureConnState.client.close();
|
|
333
|
+
}
|
|
334
|
+
catch { }
|
|
335
|
+
this.#futureConnState = undefined;
|
|
336
|
+
}
|
|
337
|
+
const next = this.#openConn();
|
|
338
|
+
const version = await next.client.getVersion();
|
|
339
|
+
next.useSqlCache = version >= 2;
|
|
340
|
+
if (next.useSqlCache) {
|
|
341
|
+
next.sqlCache.capacity = sqlCacheCapacity;
|
|
342
|
+
}
|
|
343
|
+
this.#connState = next;
|
|
344
|
+
this.closed = false;
|
|
345
|
+
}
|
|
346
|
+
_closeStream(streamState) {
|
|
347
|
+
streamState.stream.close();
|
|
348
|
+
const connState = streamState.conn;
|
|
349
|
+
connState.streamStates.delete(streamState);
|
|
350
|
+
if (connState.streamStates.size === 0 &&
|
|
351
|
+
connState !== this.#connState) {
|
|
352
|
+
// We are not using this connection anymore and this is the last stream that was using it, so we
|
|
353
|
+
// must close it now.
|
|
354
|
+
connState.client.close();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
close() {
|
|
358
|
+
this.#connState.client.close();
|
|
359
|
+
this.closed = true;
|
|
360
|
+
if (this.#futureConnState) {
|
|
361
|
+
try {
|
|
362
|
+
this.#futureConnState.client.close();
|
|
363
|
+
}
|
|
364
|
+
catch { }
|
|
365
|
+
this.#futureConnState = undefined;
|
|
366
|
+
}
|
|
367
|
+
this.closed = true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
exports.WsClient = WsClient;
|
|
371
|
+
class WsTransaction extends hrana_js_1.HranaTransaction {
|
|
372
|
+
#client;
|
|
373
|
+
#streamState;
|
|
374
|
+
/** @private */
|
|
375
|
+
constructor(client, state, mode, version) {
|
|
376
|
+
super(mode, version);
|
|
377
|
+
this.#client = client;
|
|
378
|
+
this.#streamState = state;
|
|
379
|
+
}
|
|
380
|
+
/** @private */
|
|
381
|
+
_getStream() {
|
|
382
|
+
return this.#streamState.stream;
|
|
383
|
+
}
|
|
384
|
+
/** @private */
|
|
385
|
+
_getSqlCache() {
|
|
386
|
+
return this.#streamState.conn.sqlCache;
|
|
387
|
+
}
|
|
388
|
+
close() {
|
|
389
|
+
this.#client._closeStream(this.#streamState);
|
|
390
|
+
}
|
|
391
|
+
get closed() {
|
|
392
|
+
return this.#streamState.stream.closed;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
exports.WsTransaction = WsTransaction;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as hrana from "@libsql/hrana-client";
|
|
2
|
+
import type { InStatement, ResultSet, Transaction, TransactionMode, InArgs } from "@libsql/core/api";
|
|
3
|
+
import type { SqlCache } from "./sql_cache.js";
|
|
4
|
+
export declare abstract class HranaTransaction implements Transaction {
|
|
5
|
+
#private;
|
|
6
|
+
/** @private */
|
|
7
|
+
constructor(mode: TransactionMode, version: hrana.ProtocolVersion);
|
|
8
|
+
/** @private */
|
|
9
|
+
abstract _getStream(): hrana.Stream;
|
|
10
|
+
/** @private */
|
|
11
|
+
abstract _getSqlCache(): SqlCache;
|
|
12
|
+
abstract close(): void;
|
|
13
|
+
abstract get closed(): boolean;
|
|
14
|
+
execute(stmt: InStatement): Promise<ResultSet>;
|
|
15
|
+
batch(stmts: Array<InStatement>): Promise<Array<ResultSet>>;
|
|
16
|
+
executeMultiple(sql: string): Promise<void>;
|
|
17
|
+
rollback(): Promise<void>;
|
|
18
|
+
commit(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export declare function executeHranaBatch(mode: TransactionMode, version: hrana.ProtocolVersion, batch: hrana.Batch, hranaStmts: Array<hrana.Stmt>, disableForeignKeys?: boolean): Promise<Array<ResultSet>>;
|
|
21
|
+
export declare function stmtToHrana(stmt: InStatement | [string, InArgs?]): hrana.Stmt;
|
|
22
|
+
export declare function resultSetFromHrana(hranaRows: hrana.RowsResult): ResultSet;
|
|
23
|
+
export declare function mapHranaError(e: unknown): unknown;
|