@g9tm/lynxcontrols-lynxtron 0.0.0-stage → 1.4.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 +44 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/driver.d.ts +21 -0
- package/dist/driver.d.ts.map +1 -0
- package/dist/driver.js +139 -0
- package/dist/driver.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/node/engine.d.ts +31 -0
- package/dist/node/engine.d.ts.map +1 -0
- package/dist/node/engine.js +295 -0
- package/dist/node/engine.js.map +1 -0
- package/dist/node/expose.d.ts +13 -0
- package/dist/node/expose.d.ts.map +1 -0
- package/dist/node/expose.js +33 -0
- package/dist/node/expose.js.map +1 -0
- package/dist/node/host-bridge.d.ts +16 -0
- package/dist/node/host-bridge.d.ts.map +1 -0
- package/dist/node/host-bridge.js +18 -0
- package/dist/node/host-bridge.js.map +1 -0
- package/dist/node/index.d.ts +5 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +4 -0
- package/dist/node/preferences.d.ts +34 -0
- package/dist/node/preferences.d.ts.map +1 -0
- package/dist/node/preferences.js +76 -0
- package/dist/node/preferences.js.map +1 -0
- package/dist/node/preload.d.ts +2 -0
- package/dist/node/preload.d.ts.map +1 -0
- package/dist/node/sqlite-worker.d.ts +2 -0
- package/dist/node/sqlite-worker.d.ts.map +1 -0
- package/dist/node/worker-host.d.ts +20 -0
- package/dist/node/worker-host.d.ts.map +1 -0
- package/dist/node/worker-host.js +177 -0
- package/dist/node/worker-host.js.map +1 -0
- package/dist/preload.cjs +286 -0
- package/dist/protocol.d.ts +104 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +1 -0
- package/dist/services.d.ts +21 -0
- package/dist/services.d.ts.map +1 -0
- package/dist/services.js +40 -0
- package/dist/services.js.map +1 -0
- package/dist/sqlite-worker.cjs +301 -0
- package/package.json +30 -4
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_node_worker_threads_namespaceObject = require("node:worker_threads");
|
|
4
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
5
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
6
|
+
const external_node_sqlite_namespaceObject = require("node:sqlite");
|
|
7
|
+
class G9EngineError extends Error {
|
|
8
|
+
wire;
|
|
9
|
+
constructor(wire){
|
|
10
|
+
super(wire.message);
|
|
11
|
+
this.name = 'G9EngineError';
|
|
12
|
+
this.wire = wire;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const fail = (code, message, extra = {})=>{
|
|
16
|
+
throw new G9EngineError({
|
|
17
|
+
code,
|
|
18
|
+
message,
|
|
19
|
+
...extra
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
function wireErrorCode(resultCode) {
|
|
23
|
+
switch(0xff & resultCode){
|
|
24
|
+
case 5:
|
|
25
|
+
return 'busy';
|
|
26
|
+
case 6:
|
|
27
|
+
return 'locked';
|
|
28
|
+
case 19:
|
|
29
|
+
return 'constraint';
|
|
30
|
+
case 11:
|
|
31
|
+
case 26:
|
|
32
|
+
return 'corrupt';
|
|
33
|
+
case 8:
|
|
34
|
+
return 'readonly';
|
|
35
|
+
case 13:
|
|
36
|
+
return 'full';
|
|
37
|
+
case 9:
|
|
38
|
+
return 'interrupted';
|
|
39
|
+
case 21:
|
|
40
|
+
return 'misuse';
|
|
41
|
+
case 14:
|
|
42
|
+
return 'not-found';
|
|
43
|
+
default:
|
|
44
|
+
return 'other';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function toWireError(error, sql, completedStatements) {
|
|
48
|
+
if (error instanceof G9EngineError) return {
|
|
49
|
+
...error.wire,
|
|
50
|
+
...void 0 !== sql && void 0 === error.wire.sql ? {
|
|
51
|
+
sql
|
|
52
|
+
} : {},
|
|
53
|
+
...void 0 !== completedStatements && void 0 === error.wire.completedStatements ? {
|
|
54
|
+
completedStatements
|
|
55
|
+
} : {}
|
|
56
|
+
};
|
|
57
|
+
const e = error;
|
|
58
|
+
const sqliteCode = 'number' == typeof e?.errcode ? e.errcode : void 0;
|
|
59
|
+
return {
|
|
60
|
+
code: void 0 !== sqliteCode ? wireErrorCode(sqliteCode) : 'misuse',
|
|
61
|
+
message: e?.message ?? String(error),
|
|
62
|
+
...void 0 !== sqliteCode ? {
|
|
63
|
+
sqliteCode
|
|
64
|
+
} : {},
|
|
65
|
+
...void 0 !== sql ? {
|
|
66
|
+
sql
|
|
67
|
+
} : {},
|
|
68
|
+
...void 0 !== completedStatements ? {
|
|
69
|
+
completedStatements
|
|
70
|
+
} : {}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function bind(value, sql, index) {
|
|
74
|
+
if (null === value) return value;
|
|
75
|
+
if ('string' == typeof value) {
|
|
76
|
+
if (value.indexOf('\u0000') >= 0) fail('misuse', `Parameter ${index + 1} is text containing NUL; use a BLOB.`, {
|
|
77
|
+
sql
|
|
78
|
+
});
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
if ('number' == typeof value) {
|
|
82
|
+
if (Number.isNaN(value)) fail('misuse', `Parameter ${index + 1} is NaN.`, {
|
|
83
|
+
sql
|
|
84
|
+
});
|
|
85
|
+
return Number.isSafeInteger(value) ? BigInt(value) : value;
|
|
86
|
+
}
|
|
87
|
+
if (value instanceof ArrayBuffer) return 0 === value.byteLength ? new Uint8Array(0) : new Uint8Array(value);
|
|
88
|
+
if (ArrayBuffer.isView(value)) {
|
|
89
|
+
const view = value;
|
|
90
|
+
return new Uint8Array(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength));
|
|
91
|
+
}
|
|
92
|
+
if ('object' == typeof value && 'string' == typeof value.int64) return BigInt(value.int64);
|
|
93
|
+
return fail('misuse', `Parameter ${index + 1} is not a SQL value (${void 0 === value ? 'undefined' : typeof value}).`, {
|
|
94
|
+
sql
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function read(value) {
|
|
98
|
+
if ('bigint' == typeof value) {
|
|
99
|
+
const n = Number(value);
|
|
100
|
+
return Number.isSafeInteger(n) ? n : {
|
|
101
|
+
int64: value.toString()
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (ArrayBuffer.isView(value)) {
|
|
105
|
+
const copy = new Uint8Array(value.byteLength);
|
|
106
|
+
copy.set(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
107
|
+
return copy.buffer;
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
class G9SqliteEngine {
|
|
112
|
+
db = null;
|
|
113
|
+
statements = new Map();
|
|
114
|
+
tx = null;
|
|
115
|
+
lastInsertRowId = 0;
|
|
116
|
+
handle(request) {
|
|
117
|
+
switch(request.op){
|
|
118
|
+
case 'capabilities':
|
|
119
|
+
return G9SqliteEngine.capabilities();
|
|
120
|
+
case 'open':
|
|
121
|
+
return this.open(request.options);
|
|
122
|
+
case 'run':
|
|
123
|
+
return this.run(request.statements, request.options);
|
|
124
|
+
case 'begin':
|
|
125
|
+
return this.begin(request.mode, request.tx);
|
|
126
|
+
case 'commit':
|
|
127
|
+
return this.commit(request.tx);
|
|
128
|
+
case 'rollback':
|
|
129
|
+
return this.rollback(request.tx);
|
|
130
|
+
case 'close':
|
|
131
|
+
return this.close();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
static capabilities() {
|
|
135
|
+
const db = new external_node_sqlite_namespaceObject.DatabaseSync(':memory:');
|
|
136
|
+
try {
|
|
137
|
+
const row = db.prepare('SELECT sqlite_version() AS v').get();
|
|
138
|
+
return {
|
|
139
|
+
sqliteVersion: row.v,
|
|
140
|
+
maxVariables: 32766,
|
|
141
|
+
json1: true,
|
|
142
|
+
returning: true,
|
|
143
|
+
upsert: true,
|
|
144
|
+
interrupt: false
|
|
145
|
+
};
|
|
146
|
+
} finally{
|
|
147
|
+
db.close();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
database() {
|
|
151
|
+
return this.db ?? fail('misuse', 'The database session is closed or unknown.');
|
|
152
|
+
}
|
|
153
|
+
open(options) {
|
|
154
|
+
if (this.db) fail('misuse', 'This engine already holds an open database.');
|
|
155
|
+
const memory = ':memory:' === options.path || '' === options.path;
|
|
156
|
+
if (!memory && !options.create && !(0, external_node_fs_namespaceObject.existsSync)(options.path)) fail('not-found', `Database file does not exist: ${options.path}`);
|
|
157
|
+
if (!memory && options.create) (0, external_node_fs_namespaceObject.mkdirSync)((0, external_node_path_namespaceObject.dirname)(options.path), {
|
|
158
|
+
recursive: true
|
|
159
|
+
});
|
|
160
|
+
let db;
|
|
161
|
+
try {
|
|
162
|
+
db = new external_node_sqlite_namespaceObject.DatabaseSync(options.path || ':memory:', {
|
|
163
|
+
readOnly: true === options.readOnly
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
throw new G9EngineError(toWireError(error));
|
|
167
|
+
}
|
|
168
|
+
for (const sql of options.pragmas ?? [])try {
|
|
169
|
+
db.exec(sql);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (!sql.startsWith('PRAGMA journal_mode')) {
|
|
172
|
+
db.close();
|
|
173
|
+
throw new G9EngineError(toWireError(error, sql));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this.db = db;
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
prepare(sql) {
|
|
180
|
+
let stmt = this.statements.get(sql);
|
|
181
|
+
if (!stmt) {
|
|
182
|
+
stmt = this.database().prepare(sql);
|
|
183
|
+
stmt.setReadBigInts(true);
|
|
184
|
+
stmt.setReturnArrays(true);
|
|
185
|
+
this.statements.set(sql, stmt);
|
|
186
|
+
}
|
|
187
|
+
return stmt;
|
|
188
|
+
}
|
|
189
|
+
execute(statement, maxRows) {
|
|
190
|
+
if (statement.params.some((p)=>void 0 === p)) fail('misuse', 'undefined is not a SQL value (use null).', {
|
|
191
|
+
sql: statement.sql
|
|
192
|
+
});
|
|
193
|
+
const stmt = this.prepare(statement.sql);
|
|
194
|
+
const args = statement.params.map((p, i)=>bind(p, statement.sql, i));
|
|
195
|
+
const columns = stmt.columns().map((c)=>c.name);
|
|
196
|
+
if (columns.length > 0) {
|
|
197
|
+
const rows = [];
|
|
198
|
+
for (const row of stmt.iterate(...args)){
|
|
199
|
+
if (void 0 !== maxRows && rows.length >= maxRows) fail('too-many-rows', `The statement returned more than ${maxRows} rows.`, {
|
|
200
|
+
sql: statement.sql
|
|
201
|
+
});
|
|
202
|
+
rows.push(row.map(read));
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
columns,
|
|
206
|
+
rows,
|
|
207
|
+
changes: 0,
|
|
208
|
+
lastInsertRowId: this.lastInsertRowId
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
const result = stmt.run(...args);
|
|
212
|
+
this.lastInsertRowId = read(result.lastInsertRowid);
|
|
213
|
+
return {
|
|
214
|
+
columns: [],
|
|
215
|
+
rows: [],
|
|
216
|
+
changes: Number(result.changes),
|
|
217
|
+
lastInsertRowId: this.lastInsertRowId
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
run(statements, options) {
|
|
221
|
+
const db = this.database();
|
|
222
|
+
if (void 0 !== options.tx && this.tx !== options.tx) fail('misuse', 'The transaction is finished or belongs to another session.');
|
|
223
|
+
if (void 0 === options.tx && null !== this.tx) fail('misuse', 'A transaction is open on this session; pass its token (tx).');
|
|
224
|
+
const atomic = true === options.atomic && void 0 === options.tx && statements.length > 0;
|
|
225
|
+
if (atomic) db.exec('BEGIN');
|
|
226
|
+
const results = [];
|
|
227
|
+
try {
|
|
228
|
+
for (const statement of statements)try {
|
|
229
|
+
results.push(this.execute(statement, options.maxRows));
|
|
230
|
+
} catch (error) {
|
|
231
|
+
throw new G9EngineError(toWireError(error, statement.sql, results.length));
|
|
232
|
+
}
|
|
233
|
+
if (atomic) db.exec('COMMIT');
|
|
234
|
+
} catch (error) {
|
|
235
|
+
if (atomic) try {
|
|
236
|
+
db.exec('ROLLBACK');
|
|
237
|
+
} catch {}
|
|
238
|
+
throw error instanceof G9EngineError ? error : new G9EngineError(toWireError(error));
|
|
239
|
+
}
|
|
240
|
+
return results;
|
|
241
|
+
}
|
|
242
|
+
begin(mode, tx) {
|
|
243
|
+
const db = this.database();
|
|
244
|
+
if (null !== this.tx) fail('misuse', 'A transaction is already open on this session.');
|
|
245
|
+
try {
|
|
246
|
+
db.exec('immediate' === mode ? 'BEGIN IMMEDIATE' : 'BEGIN DEFERRED');
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw new G9EngineError(toWireError(error));
|
|
249
|
+
}
|
|
250
|
+
this.tx = tx;
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
commit(tx) {
|
|
254
|
+
const db = this.database();
|
|
255
|
+
if (this.tx !== tx) fail('misuse', 'The transaction is finished or unknown.');
|
|
256
|
+
try {
|
|
257
|
+
db.exec('COMMIT');
|
|
258
|
+
} catch (error) {
|
|
259
|
+
throw new G9EngineError(toWireError(error));
|
|
260
|
+
}
|
|
261
|
+
this.tx = null;
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
rollback(tx) {
|
|
265
|
+
const db = this.database();
|
|
266
|
+
if (this.tx !== tx) fail('misuse', 'The transaction is finished or unknown.');
|
|
267
|
+
this.tx = null;
|
|
268
|
+
try {
|
|
269
|
+
db.exec('ROLLBACK');
|
|
270
|
+
} catch {}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
close() {
|
|
274
|
+
const db = this.database();
|
|
275
|
+
this.db = null;
|
|
276
|
+
this.tx = null;
|
|
277
|
+
this.statements.clear();
|
|
278
|
+
db.close();
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const engine = new G9SqliteEngine();
|
|
283
|
+
external_node_worker_threads_namespaceObject.parentPort?.on('message', (message)=>{
|
|
284
|
+
try {
|
|
285
|
+
external_node_worker_threads_namespaceObject.parentPort?.postMessage({
|
|
286
|
+
id: message.id,
|
|
287
|
+
ok: true,
|
|
288
|
+
value: engine.handle(message.request)
|
|
289
|
+
});
|
|
290
|
+
} catch (error) {
|
|
291
|
+
external_node_worker_threads_namespaceObject.parentPort?.postMessage({
|
|
292
|
+
id: message.id,
|
|
293
|
+
ok: false,
|
|
294
|
+
error: toWireError(error)
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
299
|
+
Object.defineProperty(exports, '__esModule', {
|
|
300
|
+
value: true
|
|
301
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/lynxcontrols-lynxtron",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Desktop platform layer for Lynxtron: worker-thread SQLite driver and host services behind a contextBridge preload.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./node": {
|
|
15
|
+
"source": "./src/node/index.ts",
|
|
16
|
+
"types": "./dist/node/index.d.ts",
|
|
17
|
+
"default": "./dist/node/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./preload": "./dist/preload.cjs"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rslib build"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@g9tm/lynxcontrols-foundation": "1.4.0",
|
|
30
|
+
"@g9tm/lynxcontrols-sqlite": "1.4.0"
|
|
31
|
+
}
|
|
32
|
+
}
|