@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,177 @@
|
|
|
1
|
+
const failure = (code, message)=>({
|
|
2
|
+
ok: false,
|
|
3
|
+
error: {
|
|
4
|
+
code,
|
|
5
|
+
message
|
|
6
|
+
}
|
|
7
|
+
});
|
|
8
|
+
function createWorkerSqliteBridge(spawn) {
|
|
9
|
+
const sessions = new Map();
|
|
10
|
+
const txOwner = new Map();
|
|
11
|
+
let seq = 0;
|
|
12
|
+
function request(s, body, commit = false) {
|
|
13
|
+
if (s.dead) return Promise.resolve({
|
|
14
|
+
ok: false,
|
|
15
|
+
error: s.dead
|
|
16
|
+
});
|
|
17
|
+
const id = ++seq;
|
|
18
|
+
return new Promise((resolve)=>{
|
|
19
|
+
s.pending.set(id, {
|
|
20
|
+
resolve: resolve,
|
|
21
|
+
commit
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
s.worker.postMessage({
|
|
25
|
+
id,
|
|
26
|
+
request: body
|
|
27
|
+
});
|
|
28
|
+
} catch (error) {
|
|
29
|
+
var _ref;
|
|
30
|
+
s.pending.delete(id);
|
|
31
|
+
resolve(failure('interrupted', `The database worker is gone: ${String(null != (_ref = null == error ? void 0 : error.message) ? _ref : error)}`));
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const attach = (token, worker)=>{
|
|
36
|
+
const s = {
|
|
37
|
+
token,
|
|
38
|
+
worker,
|
|
39
|
+
pending: new Map(),
|
|
40
|
+
dead: null,
|
|
41
|
+
tx: null
|
|
42
|
+
};
|
|
43
|
+
worker.on('message', (message)=>{
|
|
44
|
+
const pending = s.pending.get(message.id);
|
|
45
|
+
if (!pending) return;
|
|
46
|
+
s.pending.delete(message.id);
|
|
47
|
+
pending.resolve(message.ok ? {
|
|
48
|
+
ok: true,
|
|
49
|
+
value: message.value
|
|
50
|
+
} : {
|
|
51
|
+
ok: false,
|
|
52
|
+
error: message.error
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
const crash = (reason)=>{
|
|
56
|
+
if (s.dead) return;
|
|
57
|
+
s.dead = {
|
|
58
|
+
code: 'interrupted',
|
|
59
|
+
message: `The database worker stopped (${reason}); reopen the database.`
|
|
60
|
+
};
|
|
61
|
+
for (const [id, pending] of s.pending){
|
|
62
|
+
s.pending.delete(id);
|
|
63
|
+
pending.resolve(pending.commit ? failure('outcome-unknown', `The database worker stopped during COMMIT (${reason}); the commit may or may not have applied.`) : {
|
|
64
|
+
ok: false,
|
|
65
|
+
error: s.dead
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (s.tx) txOwner.delete(s.tx);
|
|
69
|
+
s.tx = null;
|
|
70
|
+
};
|
|
71
|
+
worker.on('error', (error)=>{
|
|
72
|
+
var _ref;
|
|
73
|
+
return crash(String(null != (_ref = null == error ? void 0 : error.message) ? _ref : error));
|
|
74
|
+
});
|
|
75
|
+
worker.on('exit', (code)=>crash(`exit ${code}`));
|
|
76
|
+
return s;
|
|
77
|
+
};
|
|
78
|
+
const sessionOf = (token)=>{
|
|
79
|
+
var _sessions_get;
|
|
80
|
+
return null != (_sessions_get = sessions.get(token)) ? _sessions_get : null;
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
async capabilities () {
|
|
84
|
+
const s = attach(`caps-${++seq}`, spawn());
|
|
85
|
+
try {
|
|
86
|
+
return await request(s, {
|
|
87
|
+
op: 'capabilities'
|
|
88
|
+
});
|
|
89
|
+
} finally{
|
|
90
|
+
await s.worker.terminate();
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
async open (options) {
|
|
94
|
+
const token = `lynxtron-session-${++seq}`;
|
|
95
|
+
const s = attach(token, spawn());
|
|
96
|
+
const opened = await request(s, {
|
|
97
|
+
op: 'open',
|
|
98
|
+
options
|
|
99
|
+
});
|
|
100
|
+
if (!opened.ok) {
|
|
101
|
+
await s.worker.terminate();
|
|
102
|
+
return opened;
|
|
103
|
+
}
|
|
104
|
+
sessions.set(token, s);
|
|
105
|
+
return {
|
|
106
|
+
ok: true,
|
|
107
|
+
value: token
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
run (session, statements, options) {
|
|
111
|
+
const s = sessionOf(session);
|
|
112
|
+
if (!s) return Promise.resolve(failure('misuse', 'The database session is closed or unknown.'));
|
|
113
|
+
return request(s, {
|
|
114
|
+
op: 'run',
|
|
115
|
+
statements,
|
|
116
|
+
options
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
async begin (session, mode) {
|
|
120
|
+
const s = sessionOf(session);
|
|
121
|
+
if (!s) return failure('misuse', 'The database session is closed or unknown.');
|
|
122
|
+
const tx = `lynxtron-tx-${++seq}`;
|
|
123
|
+
const begun = await request(s, {
|
|
124
|
+
op: 'begin',
|
|
125
|
+
mode,
|
|
126
|
+
tx
|
|
127
|
+
});
|
|
128
|
+
if (!begun.ok) return begun;
|
|
129
|
+
s.tx = tx;
|
|
130
|
+
txOwner.set(tx, s);
|
|
131
|
+
return {
|
|
132
|
+
ok: true,
|
|
133
|
+
value: tx
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
async commit (tx) {
|
|
137
|
+
const s = txOwner.get(tx);
|
|
138
|
+
if (!s) return failure('misuse', 'The transaction is finished or unknown.');
|
|
139
|
+
const done = await request(s, {
|
|
140
|
+
op: 'commit',
|
|
141
|
+
tx
|
|
142
|
+
}, true);
|
|
143
|
+
if (done.ok) {
|
|
144
|
+
s.tx = null;
|
|
145
|
+
txOwner.delete(tx);
|
|
146
|
+
}
|
|
147
|
+
return done;
|
|
148
|
+
},
|
|
149
|
+
async rollback (tx) {
|
|
150
|
+
const s = txOwner.get(tx);
|
|
151
|
+
if (!s) return failure('misuse', 'The transaction is finished or unknown.');
|
|
152
|
+
s.tx = null;
|
|
153
|
+
txOwner.delete(tx);
|
|
154
|
+
return request(s, {
|
|
155
|
+
op: 'rollback',
|
|
156
|
+
tx
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
async close (session) {
|
|
160
|
+
const s = sessionOf(session);
|
|
161
|
+
if (!s) return failure('misuse', 'The database session is closed or unknown.');
|
|
162
|
+
sessions.delete(session);
|
|
163
|
+
if (s.tx) txOwner.delete(s.tx);
|
|
164
|
+
const closed = s.dead ? {
|
|
165
|
+
ok: true,
|
|
166
|
+
value: null
|
|
167
|
+
} : await request(s, {
|
|
168
|
+
op: 'close'
|
|
169
|
+
});
|
|
170
|
+
await s.worker.terminate();
|
|
171
|
+
return closed;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export { createWorkerSqliteBridge };
|
|
176
|
+
|
|
177
|
+
//# sourceMappingURL=worker-host.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node/worker-host.js","sources":["../../src/node/worker-host.ts"],"sourcesContent":["// The Node side of the desktop driver (runs in the Lynxtron preload): one worker per open database, request ids,\n// replies as envelopes (errors never rely on Error objects surviving the bridge), and crash recovery: a worker that\n// dies fails every pending request with `interrupted`, or `outcome-unknown` when a COMMIT was in flight.\nimport type {\n G9EngineRequest,\n G9Envelope,\n G9SqliteBridge,\n G9WireCapabilities,\n G9WireError,\n G9WireResult,\n} from '../protocol.ts';\n\n/** The parts of `node:worker_threads` Worker the host uses (injectable for tests). */\nexport interface G9WorkerLike {\n postMessage(message: unknown): void;\n on(\n event: 'message',\n listener: (message: { id: number; ok: boolean; value?: unknown; error?: G9WireError }) => void,\n ): unknown;\n on(event: 'error', listener: (error: unknown) => void): unknown;\n on(event: 'exit', listener: (code: number) => void): unknown;\n terminate(): Promise<number> | unknown;\n}\n\ninterface Pending {\n readonly resolve: (envelope: G9Envelope<unknown>) => void;\n readonly commit: boolean;\n}\n\ninterface WorkerSession {\n readonly token: string;\n readonly worker: G9WorkerLike;\n readonly pending: Map<number, Pending>;\n dead: G9WireError | null;\n tx: string | null;\n}\n\nconst failure = (code: string, message: string): G9Envelope<never> => ({ ok: false, error: { code, message } });\n\n/**\n * Creates the bridge object over worker threads. `spawn` starts one worker (production: `new Worker(workerFile)`);\n * `capabilities` is answered by a short-lived worker too, so the preload process never opens a database itself.\n */\nexport function createWorkerSqliteBridge(spawn: () => G9WorkerLike): G9SqliteBridge {\n const sessions = new Map<string, WorkerSession>();\n const txOwner = new Map<string, WorkerSession>();\n let seq = 0;\n\n function request<T>(s: WorkerSession, body: G9EngineRequest, commit = false): Promise<G9Envelope<T>> {\n if (s.dead) return Promise.resolve({ ok: false, error: s.dead });\n const id = ++seq;\n return new Promise<G9Envelope<T>>((resolve) => {\n s.pending.set(id, { resolve: resolve as (e: G9Envelope<unknown>) => void, commit });\n try {\n s.worker.postMessage({ id, request: body });\n } catch (error) {\n s.pending.delete(id);\n resolve(failure('interrupted', `The database worker is gone: ${String((error as Error)?.message ?? error)}`));\n }\n });\n }\n\n const attach = (token: string, worker: G9WorkerLike): WorkerSession => {\n const s: WorkerSession = { token, worker, pending: new Map(), dead: null, tx: null };\n worker.on('message', (message) => {\n const pending = s.pending.get(message.id);\n if (!pending) return;\n s.pending.delete(message.id);\n pending.resolve(\n message.ok ? { ok: true, value: message.value } : { ok: false, error: message.error as G9WireError },\n );\n });\n const crash = (reason: string) => {\n if (s.dead) return;\n s.dead = { code: 'interrupted', message: `The database worker stopped (${reason}); reopen the database.` };\n for (const [id, pending] of s.pending) {\n s.pending.delete(id);\n pending.resolve(\n pending.commit\n ? failure(\n 'outcome-unknown',\n `The database worker stopped during COMMIT (${reason}); the commit may or may not have applied.`,\n )\n : { ok: false, error: s.dead },\n );\n }\n if (s.tx) txOwner.delete(s.tx);\n s.tx = null;\n };\n worker.on('error', (error) => crash(String((error as Error)?.message ?? error)));\n worker.on('exit', (code) => crash(`exit ${code}`));\n return s;\n };\n\n const sessionOf = (token: string): WorkerSession | null => sessions.get(token) ?? null;\n\n return {\n async capabilities() {\n const s = attach(`caps-${++seq}`, spawn());\n try {\n return await request<G9WireCapabilities>(s, { op: 'capabilities' });\n } finally {\n await s.worker.terminate();\n }\n },\n\n async open(options) {\n const token = `lynxtron-session-${++seq}`;\n const s = attach(token, spawn());\n const opened = await request<null>(s, { op: 'open', options });\n if (!opened.ok) {\n await s.worker.terminate();\n return opened;\n }\n sessions.set(token, s);\n return { ok: true, value: token };\n },\n\n run(session, statements, options) {\n const s = sessionOf(session);\n if (!s) return Promise.resolve(failure('misuse', 'The database session is closed or unknown.'));\n return request<G9WireResult[]>(s, { op: 'run', statements, options });\n },\n\n async begin(session, mode) {\n const s = sessionOf(session);\n if (!s) return failure('misuse', 'The database session is closed or unknown.');\n const tx = `lynxtron-tx-${++seq}`;\n const begun = await request<null>(s, { op: 'begin', mode, tx });\n if (!begun.ok) return begun;\n s.tx = tx;\n txOwner.set(tx, s);\n return { ok: true, value: tx };\n },\n\n async commit(tx) {\n const s = txOwner.get(tx);\n if (!s) return failure('misuse', 'The transaction is finished or unknown.');\n const done = await request<null>(s, { op: 'commit', tx }, true);\n if (done.ok) {\n s.tx = null;\n txOwner.delete(tx);\n }\n return done;\n },\n\n async rollback(tx) {\n const s = txOwner.get(tx);\n if (!s) return failure('misuse', 'The transaction is finished or unknown.');\n s.tx = null;\n txOwner.delete(tx);\n return request<null>(s, { op: 'rollback', tx });\n },\n\n async close(session) {\n const s = sessionOf(session);\n if (!s) return failure('misuse', 'The database session is closed or unknown.');\n sessions.delete(session);\n if (s.tx) txOwner.delete(s.tx);\n const closed = s.dead ? ({ ok: true, value: null } as const) : await request<null>(s, { op: 'close' });\n // The worker is idle now (its database is closed): terminating it cannot cut a statement short.\n await s.worker.terminate();\n return closed;\n },\n };\n}\n"],"names":["failure","code","message","createWorkerSqliteBridge","spawn","sessions","Map","txOwner","seq","request","s","body","commit","Promise","id","resolve","error","String","attach","token","worker","pending","crash","reason","sessionOf","_sessions_get","options","opened","session","statements","mode","tx","begun","done","closed"],"mappings":"AAqCA,MAAMA,UAAU,CAACC,MAAcC,UAAwC;QAAE,IAAI;QAAO,OAAO;YAAED;YAAMC;QAAQ;IAAE;AAMtG,SAASC,yBAAyBC,KAAyB;IACh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,OAAMH,OAAO;YACjB,MAAMlB,IAAIc,UAAUI;YACpB,IAAI,CAAClB,GAAG,OAAOV,QAAQ,UAAU;YACjCK,SAAS,MAAM,CAACuB;YAChB,IAAIlB,EAAE,EAAE,EAAEH,QAAQ,MAAM,CAACG,EAAE,EAAE;YAC7B,MAAMwB,SAASxB,EAAE,IAAI,GAAI;gBAAE,IAAI;gBAAM,OAAO;YAAK,IAAc,MAAMD,QAAcC,GAAG;gBAAE,IAAI;YAAQ;YAEpG,MAAMA,EAAE,MAAM,CAAC,SAAS;YACxB,OAAOwB;QACT;IACF;AACF"}
|
package/dist/preload.cjs
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_exports__ = {};
|
|
3
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
4
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
5
|
+
const external_node_os_namespaceObject = require("node:os");
|
|
6
|
+
const external_node_worker_threads_namespaceObject = require("node:worker_threads");
|
|
7
|
+
const context_bridge_namespaceObject = require("@lynx-js/lynxtron/context-bridge");
|
|
8
|
+
function preferenceFilePath(dir, namespace) {
|
|
9
|
+
return (0, external_node_path_namespaceObject.join)(dir, `${namespace.replace(/[^A-Za-z0-9._-]/g, '_') || 'default'}.prefs.json`);
|
|
10
|
+
}
|
|
11
|
+
function openPreferenceFile(path, fs = external_node_fs_namespaceObject) {
|
|
12
|
+
let data = null;
|
|
13
|
+
const load = ()=>{
|
|
14
|
+
if (data) return data;
|
|
15
|
+
data = {};
|
|
16
|
+
if (!fs.existsSync(path)) return data;
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(fs.readFileSync(path, 'utf8'));
|
|
19
|
+
if (parsed && 'object' == typeof parsed && !Array.isArray(parsed)) {
|
|
20
|
+
for (const [key, value] of Object.entries(parsed))if ('number' == typeof value || 'string' == typeof value) data[key] = value;
|
|
21
|
+
}
|
|
22
|
+
} catch {}
|
|
23
|
+
return data;
|
|
24
|
+
};
|
|
25
|
+
const save = ()=>{
|
|
26
|
+
const current = load();
|
|
27
|
+
const temp = `${path}.tmp`;
|
|
28
|
+
fs.mkdirSync((0, external_node_path_namespaceObject.dirname)(path), {
|
|
29
|
+
recursive: true
|
|
30
|
+
});
|
|
31
|
+
fs.writeFileSync(temp, JSON.stringify(current));
|
|
32
|
+
fs.renameSync(temp, path);
|
|
33
|
+
};
|
|
34
|
+
const mutate = (change)=>{
|
|
35
|
+
const current = load();
|
|
36
|
+
const before = {
|
|
37
|
+
...current
|
|
38
|
+
};
|
|
39
|
+
change(current);
|
|
40
|
+
try {
|
|
41
|
+
save();
|
|
42
|
+
} catch (error) {
|
|
43
|
+
data = before;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
path,
|
|
49
|
+
getInt (key, defaultValue) {
|
|
50
|
+
const v = load()[key];
|
|
51
|
+
return 'number' == typeof v && Number.isInteger(v) ? v : defaultValue;
|
|
52
|
+
},
|
|
53
|
+
setInt (key, value) {
|
|
54
|
+
mutate((d)=>{
|
|
55
|
+
d[key] = value;
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
getString (key, defaultValue) {
|
|
59
|
+
const v = load()[key];
|
|
60
|
+
return 'string' == typeof v ? v : defaultValue;
|
|
61
|
+
},
|
|
62
|
+
setString (key, value) {
|
|
63
|
+
mutate((d)=>{
|
|
64
|
+
if (null === value) delete d[key];
|
|
65
|
+
else d[key] = value;
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
remove (key) {
|
|
69
|
+
if (!(key in load())) return;
|
|
70
|
+
mutate((d)=>{
|
|
71
|
+
delete d[key];
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
entries (prefix) {
|
|
75
|
+
return Object.entries(load()).filter(([key])=>key.startsWith(prefix));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function createHostBridge(options) {
|
|
80
|
+
const file = openPreferenceFile(preferenceFilePath(options.userDataDir, options.namespace ?? 'g9'));
|
|
81
|
+
return {
|
|
82
|
+
getInt: (key, defaultValue)=>file.getInt(key, defaultValue),
|
|
83
|
+
setInt: (key, value)=>file.setInt(key, value),
|
|
84
|
+
getString: (key, defaultValue)=>file.getString(key, defaultValue),
|
|
85
|
+
setString: (key, value)=>file.setString(key, value),
|
|
86
|
+
remove: (key)=>file.remove(key),
|
|
87
|
+
entries: (prefix)=>file.entries(prefix),
|
|
88
|
+
exit: options.exit ?? (()=>process.exit(0)),
|
|
89
|
+
appDataDirectory: ()=>options.userDataDir
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const failure = (code, message)=>({
|
|
93
|
+
ok: false,
|
|
94
|
+
error: {
|
|
95
|
+
code,
|
|
96
|
+
message
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
function createWorkerSqliteBridge(spawn) {
|
|
100
|
+
const sessions = new Map();
|
|
101
|
+
const txOwner = new Map();
|
|
102
|
+
let seq = 0;
|
|
103
|
+
function request(s, body, commit = false) {
|
|
104
|
+
if (s.dead) return Promise.resolve({
|
|
105
|
+
ok: false,
|
|
106
|
+
error: s.dead
|
|
107
|
+
});
|
|
108
|
+
const id = ++seq;
|
|
109
|
+
return new Promise((resolve)=>{
|
|
110
|
+
s.pending.set(id, {
|
|
111
|
+
resolve: resolve,
|
|
112
|
+
commit
|
|
113
|
+
});
|
|
114
|
+
try {
|
|
115
|
+
s.worker.postMessage({
|
|
116
|
+
id,
|
|
117
|
+
request: body
|
|
118
|
+
});
|
|
119
|
+
} catch (error) {
|
|
120
|
+
s.pending.delete(id);
|
|
121
|
+
resolve(failure('interrupted', `The database worker is gone: ${String(error?.message ?? error)}`));
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const attach = (token, worker)=>{
|
|
126
|
+
const s = {
|
|
127
|
+
token,
|
|
128
|
+
worker,
|
|
129
|
+
pending: new Map(),
|
|
130
|
+
dead: null,
|
|
131
|
+
tx: null
|
|
132
|
+
};
|
|
133
|
+
worker.on('message', (message)=>{
|
|
134
|
+
const pending = s.pending.get(message.id);
|
|
135
|
+
if (!pending) return;
|
|
136
|
+
s.pending.delete(message.id);
|
|
137
|
+
pending.resolve(message.ok ? {
|
|
138
|
+
ok: true,
|
|
139
|
+
value: message.value
|
|
140
|
+
} : {
|
|
141
|
+
ok: false,
|
|
142
|
+
error: message.error
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
const crash = (reason)=>{
|
|
146
|
+
if (s.dead) return;
|
|
147
|
+
s.dead = {
|
|
148
|
+
code: 'interrupted',
|
|
149
|
+
message: `The database worker stopped (${reason}); reopen the database.`
|
|
150
|
+
};
|
|
151
|
+
for (const [id, pending] of s.pending){
|
|
152
|
+
s.pending.delete(id);
|
|
153
|
+
pending.resolve(pending.commit ? failure('outcome-unknown', `The database worker stopped during COMMIT (${reason}); the commit may or may not have applied.`) : {
|
|
154
|
+
ok: false,
|
|
155
|
+
error: s.dead
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
if (s.tx) txOwner.delete(s.tx);
|
|
159
|
+
s.tx = null;
|
|
160
|
+
};
|
|
161
|
+
worker.on('error', (error)=>crash(String(error?.message ?? error)));
|
|
162
|
+
worker.on('exit', (code)=>crash(`exit ${code}`));
|
|
163
|
+
return s;
|
|
164
|
+
};
|
|
165
|
+
const sessionOf = (token)=>sessions.get(token) ?? null;
|
|
166
|
+
return {
|
|
167
|
+
async capabilities () {
|
|
168
|
+
const s = attach(`caps-${++seq}`, spawn());
|
|
169
|
+
try {
|
|
170
|
+
return await request(s, {
|
|
171
|
+
op: 'capabilities'
|
|
172
|
+
});
|
|
173
|
+
} finally{
|
|
174
|
+
await s.worker.terminate();
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
async open (options) {
|
|
178
|
+
const token = `lynxtron-session-${++seq}`;
|
|
179
|
+
const s = attach(token, spawn());
|
|
180
|
+
const opened = await request(s, {
|
|
181
|
+
op: 'open',
|
|
182
|
+
options
|
|
183
|
+
});
|
|
184
|
+
if (!opened.ok) {
|
|
185
|
+
await s.worker.terminate();
|
|
186
|
+
return opened;
|
|
187
|
+
}
|
|
188
|
+
sessions.set(token, s);
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
value: token
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
run (session, statements, options) {
|
|
195
|
+
const s = sessionOf(session);
|
|
196
|
+
if (!s) return Promise.resolve(failure('misuse', 'The database session is closed or unknown.'));
|
|
197
|
+
return request(s, {
|
|
198
|
+
op: 'run',
|
|
199
|
+
statements,
|
|
200
|
+
options
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
async begin (session, mode) {
|
|
204
|
+
const s = sessionOf(session);
|
|
205
|
+
if (!s) return failure('misuse', 'The database session is closed or unknown.');
|
|
206
|
+
const tx = `lynxtron-tx-${++seq}`;
|
|
207
|
+
const begun = await request(s, {
|
|
208
|
+
op: 'begin',
|
|
209
|
+
mode,
|
|
210
|
+
tx
|
|
211
|
+
});
|
|
212
|
+
if (!begun.ok) return begun;
|
|
213
|
+
s.tx = tx;
|
|
214
|
+
txOwner.set(tx, s);
|
|
215
|
+
return {
|
|
216
|
+
ok: true,
|
|
217
|
+
value: tx
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
async commit (tx) {
|
|
221
|
+
const s = txOwner.get(tx);
|
|
222
|
+
if (!s) return failure('misuse', 'The transaction is finished or unknown.');
|
|
223
|
+
const done = await request(s, {
|
|
224
|
+
op: 'commit',
|
|
225
|
+
tx
|
|
226
|
+
}, true);
|
|
227
|
+
if (done.ok) {
|
|
228
|
+
s.tx = null;
|
|
229
|
+
txOwner.delete(tx);
|
|
230
|
+
}
|
|
231
|
+
return done;
|
|
232
|
+
},
|
|
233
|
+
async rollback (tx) {
|
|
234
|
+
const s = txOwner.get(tx);
|
|
235
|
+
if (!s) return failure('misuse', 'The transaction is finished or unknown.');
|
|
236
|
+
s.tx = null;
|
|
237
|
+
txOwner.delete(tx);
|
|
238
|
+
return request(s, {
|
|
239
|
+
op: 'rollback',
|
|
240
|
+
tx
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
async close (session) {
|
|
244
|
+
const s = sessionOf(session);
|
|
245
|
+
if (!s) return failure('misuse', 'The database session is closed or unknown.');
|
|
246
|
+
sessions.delete(session);
|
|
247
|
+
if (s.tx) txOwner.delete(s.tx);
|
|
248
|
+
const closed = s.dead ? {
|
|
249
|
+
ok: true,
|
|
250
|
+
value: null
|
|
251
|
+
} : await request(s, {
|
|
252
|
+
op: 'close'
|
|
253
|
+
});
|
|
254
|
+
await s.worker.terminate();
|
|
255
|
+
return closed;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function exposeG9Apis(env, workerFile) {
|
|
260
|
+
const userDataDir = env.G9_USER_DATA || (0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.homedir)(), '.g9lynx');
|
|
261
|
+
const report = env.G9_REPORT;
|
|
262
|
+
const apis = {
|
|
263
|
+
g9sqlite: createWorkerSqliteBridge(()=>new external_node_worker_threads_namespaceObject.Worker(workerFile, {
|
|
264
|
+
execArgv: [
|
|
265
|
+
'--disable-warning=ExperimentalWarning'
|
|
266
|
+
]
|
|
267
|
+
})),
|
|
268
|
+
g9host: createHostBridge({
|
|
269
|
+
userDataDir
|
|
270
|
+
})
|
|
271
|
+
};
|
|
272
|
+
if (report) apis.g9test = {
|
|
273
|
+
tempDir: ()=>(0, external_node_fs_namespaceObject.mkdtempSync)((0, external_node_path_namespaceObject.join)((0, external_node_os_namespaceObject.tmpdir)(), 'g9-lynxtron-contract-')),
|
|
274
|
+
report: (json)=>{
|
|
275
|
+
(0, external_node_fs_namespaceObject.writeFileSync)(report, json);
|
|
276
|
+
setTimeout(()=>process.exit(0), 50);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
context_bridge_namespaceObject.contextBridge.exposeInLynxBTS(apis);
|
|
280
|
+
return apis;
|
|
281
|
+
}
|
|
282
|
+
exposeG9Apis(process.env, (0, external_node_path_namespaceObject.join)(__dirname, 'sqlite-worker.cjs'));
|
|
283
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
284
|
+
Object.defineProperty(exports, '__esModule', {
|
|
285
|
+
value: true
|
|
286
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** A driver value on the wire: safe integers as numbers, larger ones as `{ int64 }`, BLOBs as ArrayBuffer. */
|
|
2
|
+
export type G9WireValue = null | number | string | ArrayBuffer | {
|
|
3
|
+
readonly int64: string;
|
|
4
|
+
};
|
|
5
|
+
/** One statement of a batch on the wire. */
|
|
6
|
+
export interface G9WireStatement {
|
|
7
|
+
readonly sql: string;
|
|
8
|
+
readonly params: readonly G9WireValue[];
|
|
9
|
+
}
|
|
10
|
+
/** The result of one statement on the wire: column names, rows of wire values, change count, last rowid. */
|
|
11
|
+
export interface G9WireResult {
|
|
12
|
+
readonly columns: string[];
|
|
13
|
+
readonly rows: G9WireValue[][];
|
|
14
|
+
readonly changes: number;
|
|
15
|
+
readonly lastInsertRowId: G9WireValue;
|
|
16
|
+
}
|
|
17
|
+
/** A `G9SqliteError` as plain data. */
|
|
18
|
+
export interface G9WireError {
|
|
19
|
+
readonly code: string;
|
|
20
|
+
readonly message: string;
|
|
21
|
+
readonly sqliteCode?: number;
|
|
22
|
+
readonly sql?: string;
|
|
23
|
+
readonly completedStatements?: number;
|
|
24
|
+
}
|
|
25
|
+
/** How the Node engine opens a database (the wire form of `G9OpenOptions`). */
|
|
26
|
+
export interface G9WireOpenOptions {
|
|
27
|
+
readonly path: string;
|
|
28
|
+
readonly readOnly?: boolean;
|
|
29
|
+
readonly create?: boolean;
|
|
30
|
+
/** PRAGMA statements to apply on open (built by `pragmaStatements`). */
|
|
31
|
+
readonly pragmas?: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
/** Per-batch options on the wire: the transaction token, a row cap, and all-or-nothing execution. */
|
|
34
|
+
export interface G9WireRunOptions {
|
|
35
|
+
readonly tx?: string;
|
|
36
|
+
readonly maxRows?: number;
|
|
37
|
+
readonly atomic?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** What the Node SQLite engine supports (the wire form of `G9DriverCapabilities`). */
|
|
40
|
+
export interface G9WireCapabilities {
|
|
41
|
+
readonly sqliteVersion: string;
|
|
42
|
+
readonly maxVariables: number;
|
|
43
|
+
readonly json1: boolean;
|
|
44
|
+
readonly returning: boolean;
|
|
45
|
+
readonly upsert: boolean;
|
|
46
|
+
readonly interrupt: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** One request to a database worker. */
|
|
49
|
+
export type G9EngineRequest = {
|
|
50
|
+
readonly op: 'open';
|
|
51
|
+
readonly options: G9WireOpenOptions;
|
|
52
|
+
} | {
|
|
53
|
+
readonly op: 'run';
|
|
54
|
+
readonly statements: readonly G9WireStatement[];
|
|
55
|
+
readonly options: G9WireRunOptions;
|
|
56
|
+
} | {
|
|
57
|
+
readonly op: 'begin';
|
|
58
|
+
readonly mode: 'deferred' | 'immediate';
|
|
59
|
+
readonly tx: string;
|
|
60
|
+
} | {
|
|
61
|
+
readonly op: 'commit';
|
|
62
|
+
readonly tx: string;
|
|
63
|
+
} | {
|
|
64
|
+
readonly op: 'rollback';
|
|
65
|
+
readonly tx: string;
|
|
66
|
+
} | {
|
|
67
|
+
readonly op: 'close';
|
|
68
|
+
} | {
|
|
69
|
+
readonly op: 'capabilities';
|
|
70
|
+
};
|
|
71
|
+
/** Every bridge reply: errors are data, so their fields survive the bridge (a rejected Error may not). */
|
|
72
|
+
export type G9Envelope<T> = {
|
|
73
|
+
readonly ok: true;
|
|
74
|
+
readonly value: T;
|
|
75
|
+
} | {
|
|
76
|
+
readonly ok: false;
|
|
77
|
+
readonly error: G9WireError;
|
|
78
|
+
};
|
|
79
|
+
/** The `g9sqlite` object exposed by the preload. */
|
|
80
|
+
export interface G9SqliteBridge {
|
|
81
|
+
capabilities(): Promise<G9Envelope<G9WireCapabilities>>;
|
|
82
|
+
open(options: G9WireOpenOptions): Promise<G9Envelope<string>>;
|
|
83
|
+
run(session: string, statements: readonly G9WireStatement[], options: G9WireRunOptions): Promise<G9Envelope<G9WireResult[]>>;
|
|
84
|
+
begin(session: string, mode: 'deferred' | 'immediate'): Promise<G9Envelope<string>>;
|
|
85
|
+
commit(tx: string): Promise<G9Envelope<null>>;
|
|
86
|
+
rollback(tx: string): Promise<G9Envelope<null>>;
|
|
87
|
+
close(session: string): Promise<G9Envelope<null>>;
|
|
88
|
+
}
|
|
89
|
+
/** A preference value on the wire. */
|
|
90
|
+
export type G9WirePreferenceValue = number | string;
|
|
91
|
+
/** The `g9host` object exposed by the preload (synchronous calls; the bridge call itself is synchronous, E07). */
|
|
92
|
+
export interface G9HostBridge {
|
|
93
|
+
getInt(key: string, defaultValue: number): number;
|
|
94
|
+
setInt(key: string, value: number): void;
|
|
95
|
+
getString(key: string, defaultValue: string | null): string | null;
|
|
96
|
+
setString(key: string, value: string | null): void;
|
|
97
|
+
remove(key: string): void;
|
|
98
|
+
entries(prefix: string): Array<[string, G9WirePreferenceValue]>;
|
|
99
|
+
/** Closes the app (desktop has no "move to background"). */
|
|
100
|
+
exit(): void;
|
|
101
|
+
/** The app's data directory (the main process's `app.getPath('userData')`), absolute. */
|
|
102
|
+
appDataDirectory(): string;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAGA,8GAA8G;AAC9G,MAAM,MAAM,WAAW,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5F,4CAA4C;AAC5C,MAAM,WAAW,eAA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IAAI,CAAC;IAC1B,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAChE,4DAA4D;IAC5D,IAAI,IAAI,IAAI,CAAC;IACb,yFAAyF;IACzF,gBAAgB,IAAI,MAAM,CAAC;CAC5B"}
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { G9HostServices, G9PreferenceStore } from '@g9tm/lynxcontrols-foundation';
|
|
2
|
+
import type { G9HostBridge } from './protocol.js';
|
|
3
|
+
/** The `g9host` bridge exposed by `@g9tm/lynxcontrols-lynxtron/preload`, or `null` outside Lynxtron. */
|
|
4
|
+
export declare function lynxtronHostBridge(): G9HostBridge | null;
|
|
5
|
+
/** What {@link createLynxtronHostServices} returns: the two services to hand to `G9Provider`. */
|
|
6
|
+
export interface G9LynxtronServices {
|
|
7
|
+
readonly host: G9HostServices;
|
|
8
|
+
readonly preferences: G9PreferenceStore;
|
|
9
|
+
/**
|
|
10
|
+
* The app's data directory, absolute (the main process's `app.getPath('userData')`, passed to the preload as
|
|
11
|
+
* `G9_USER_DATA`): where the database goes, as `appDataDirectory()` of `@g9tm/lynxcontrols-native` on mobile.
|
|
12
|
+
*/
|
|
13
|
+
appDataDirectory(): Promise<string>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Desktop host services (plan/specs/06 §7): preferences in an atomic JSON file in userData, `exit` closes the app,
|
|
17
|
+
* no safe-area insets, `hideKeyboard` is a no-op (a desktop has a physical keyboard; blur happens by focus change),
|
|
18
|
+
* no soft-input modes. Throws outside Lynxtron without a bridge.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createLynxtronHostServices(bridge?: G9HostBridge | null): G9LynxtronServices;
|
|
21
|
+
//# sourceMappingURL=services.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.d.ts","sourceRoot":"","sources":["../src/services.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAgB,MAAM,+BAA+B,CAAC;AACrG,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI/C,wGAAwG;AACxG,wBAAgB,kBAAkB,IAAI,YAAY,GAAG,IAAI,CAMxD;AAID,iGAAiG;AACjG,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,WAAW,EAAE,iBAAiB,CAAC;IACxC;;;OAGG;IACH,gBAAgB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACrC;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,GAAE,YAAY,GAAG,IAA2B,GAAG,kBAAkB,CAkBjH"}
|
package/dist/services.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
function lynxtronHostBridge() {
|
|
2
|
+
try {
|
|
3
|
+
var _ref;
|
|
4
|
+
var _NativeModules_nodejs_exposed, _NativeModules_nodejs;
|
|
5
|
+
return null != (_ref = "u" < typeof NativeModules ? void 0 : null == NativeModules ? void 0 : null == (_NativeModules_nodejs = NativeModules.nodejs) ? void 0 : null == (_NativeModules_nodejs_exposed = _NativeModules_nodejs.exposed) ? void 0 : _NativeModules_nodejs_exposed.g9host) ? _ref : null;
|
|
6
|
+
} catch {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const NO_INSETS = Object.freeze({
|
|
11
|
+
top: 0,
|
|
12
|
+
end: 0,
|
|
13
|
+
bottom: 0,
|
|
14
|
+
start: 0
|
|
15
|
+
});
|
|
16
|
+
function createLynxtronHostServices(bridge = lynxtronHostBridge()) {
|
|
17
|
+
if (!bridge) throw new Error('The Lynxtron host bridge is missing: load @g9tm/lynxcontrols-lynxtron/preload in the LynxWindow.');
|
|
18
|
+
const host = {
|
|
19
|
+
exit: ()=>bridge.exit(),
|
|
20
|
+
hideKeyboard: ()=>void 0,
|
|
21
|
+
getSafeInsets: ()=>NO_INSETS,
|
|
22
|
+
subscribeSafeInsets: ()=>()=>void 0
|
|
23
|
+
};
|
|
24
|
+
const preferences = {
|
|
25
|
+
getInt: (key, defaultValue)=>bridge.getInt(key, defaultValue),
|
|
26
|
+
setInt: (key, value)=>bridge.setInt(key, value),
|
|
27
|
+
getString: (key, defaultValue)=>bridge.getString(key, defaultValue),
|
|
28
|
+
setString: (key, value)=>bridge.setString(key, value),
|
|
29
|
+
remove: (key)=>bridge.remove(key),
|
|
30
|
+
entries: (prefix)=>bridge.entries(prefix)
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
host,
|
|
34
|
+
preferences,
|
|
35
|
+
appDataDirectory: async ()=>bridge.appDataDirectory()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export { createLynxtronHostServices, lynxtronHostBridge };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=services.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","sources":["../src/services.ts"],"sourcesContent":["// The Lynx side of the desktop host services: G9HostServices and a G9PreferenceStore over the `g9host` bridge.\nimport type { G9HostServices, G9PreferenceStore, G9SafeInsets } from '@g9tm/lynxcontrols-foundation';\nimport type { G9HostBridge } from './protocol';\n\ndeclare const NativeModules: { nodejs?: { exposed?: { g9host?: G9HostBridge } } } | undefined;\n\n/** The `g9host` bridge exposed by `@g9tm/lynxcontrols-lynxtron/preload`, or `null` outside Lynxtron. */\nexport function lynxtronHostBridge(): G9HostBridge | null {\n try {\n return (typeof NativeModules === 'undefined' ? undefined : NativeModules?.nodejs?.exposed?.g9host) ?? null;\n } catch {\n return null;\n }\n}\n\nconst NO_INSETS: G9SafeInsets = Object.freeze({ top: 0, end: 0, bottom: 0, start: 0 });\n\n/** What {@link createLynxtronHostServices} returns: the two services to hand to `G9Provider`. */\nexport interface G9LynxtronServices {\n readonly host: G9HostServices;\n readonly preferences: G9PreferenceStore;\n /**\n * The app's data directory, absolute (the main process's `app.getPath('userData')`, passed to the preload as\n * `G9_USER_DATA`): where the database goes, as `appDataDirectory()` of `@g9tm/lynxcontrols-native` on mobile.\n */\n appDataDirectory(): Promise<string>;\n}\n\n/**\n * Desktop host services (plan/specs/06 §7): preferences in an atomic JSON file in userData, `exit` closes the app,\n * no safe-area insets, `hideKeyboard` is a no-op (a desktop has a physical keyboard; blur happens by focus change),\n * no soft-input modes. Throws outside Lynxtron without a bridge.\n */\nexport function createLynxtronHostServices(bridge: G9HostBridge | null = lynxtronHostBridge()): G9LynxtronServices {\n if (!bridge)\n throw new Error('The Lynxtron host bridge is missing: load @g9tm/lynxcontrols-lynxtron/preload in the LynxWindow.');\n const host: G9HostServices = {\n exit: () => bridge.exit(),\n hideKeyboard: () => undefined,\n getSafeInsets: () => NO_INSETS,\n subscribeSafeInsets: () => () => undefined,\n };\n const preferences: G9PreferenceStore = {\n getInt: (key, defaultValue) => bridge.getInt(key, defaultValue),\n setInt: (key, value) => bridge.setInt(key, value),\n getString: (key, defaultValue) => bridge.getString(key, defaultValue),\n setString: (key, value) => bridge.setString(key, value),\n remove: (key) => bridge.remove(key),\n entries: (prefix) => bridge.entries(prefix),\n };\n return { host, preferences, appDataDirectory: async () => bridge.appDataDirectory() };\n}\n"],"names":["lynxtronHostBridge","_ref","_NativeModules_nodejs_exposed","NativeModules","undefined","NO_INSETS","Object","createLynxtronHostServices","bridge","Error","host","preferences","key","defaultValue","value","prefix"],"mappings":"AAOO,SAASA;IACd,IAAI;YACMC;YAAmDC,+BAAAA;QAA3D,OAAO,QAACD,CAAAA,OAAAA,AAAyB,MAAzBA,OAAOE,gBAAgCC,SAAYF,QAAAA,gBAAAA,KAAAA,IAAAA,QAAAA,CAAAA,wBAAAA,cAAe,MAAM,AAAD,IAApBA,KAAAA,IAAAA,QAAAA,CAAAA,gCAAAA,sBAAuB,OAAO,AAAD,IAA7BA,KAAAA,IAAAA,8BAAgC,MAAM,AAAD,IAAxFD,OAA8F;IACxG,EAAE,OAAM;QACN,OAAO;IACT;AACF;AAEA,MAAMI,YAA0BC,OAAO,MAAM,CAAC;IAAE,KAAK;IAAG,KAAK;IAAG,QAAQ;IAAG,OAAO;AAAE;AAkB7E,SAASC,2BAA2BC,SAA8BR,oBAAoB;IAC3F,IAAI,CAACQ,QACH,MAAM,IAAIC,MAAM;IAClB,MAAMC,OAAuB;QAC3B,MAAM,IAAMF,OAAO,IAAI;QACvB,cAAc,IAAMJ;QACpB,eAAe,IAAMC;QACrB,qBAAqB,IAAM,IAAMD;IACnC;IACA,MAAMO,cAAiC;QACrC,QAAQ,CAACC,KAAKC,eAAiBL,OAAO,MAAM,CAACI,KAAKC;QAClD,QAAQ,CAACD,KAAKE,QAAUN,OAAO,MAAM,CAACI,KAAKE;QAC3C,WAAW,CAACF,KAAKC,eAAiBL,OAAO,SAAS,CAACI,KAAKC;QACxD,WAAW,CAACD,KAAKE,QAAUN,OAAO,SAAS,CAACI,KAAKE;QACjD,QAAQ,CAACF,MAAQJ,OAAO,MAAM,CAACI;QAC/B,SAAS,CAACG,SAAWP,OAAO,OAAO,CAACO;IACtC;IACA,OAAO;QAAEL;QAAMC;QAAa,kBAAkB,UAAYH,OAAO,gBAAgB;IAAG;AACtF"}
|