1. 1// MIT License
2. 1//
3. 1// Copyright (c) 2021 Kai Zhu
4. 1//
5. 1// Permission is hereby granted, free of charge, to any person obtaining a copy
6. 1// of this software and associated documentation files (the "Software"), to deal
7. 1// in the Software without restriction, including without limitation the rights
8. 1// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9. 1// copies of the Software, and to permit persons to whom the Software is
10. 1// furnished to do so, subject to the following conditions:
11. 1//
12. 1// The above copyright notice and this permission notice shall be included in
13. 1// all copies or substantial portions of the Software.
14. 1//
15. 1// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16. 1// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17. 1// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18. 1// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19. 1// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20. 1// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21. 1// SOFTWARE.
22. 1
23. 1
24. 1/*jslint beta, bitwise, name, node*/
25. 1/*global FinalizationRegistry*/
26. 1"use strict";
27. 1
28. 1const JSBATON_ARGC = 8;
29. 1const JSBATON_OFFSET_ALL = 256;
30. 1const JSBATON_OFFSET_ARGV = 128;
31. 1const JSBATON_OFFSET_BUFV = 192;
32. 1// const JSBATON_OFFSET_ERRMSG = 48;
33. 1const JSBATON_OFFSET_FUNCNAME = 8;
34. 1const JS_MAX_SAFE_INTEGER = 0x1f_ffff_ffff_ffff;
35. 1const JS_MIN_SAFE_INTEGER = -0x1f_ffff_ffff_ffff;
36. 1const SIZEOF_BLOB_MAX = 1_000_000_000;
37. 1// const SIZEOF_ERRMSG = 80;
38. 1const SIZEOF_FUNCNAME = 16;
39. 1const SQLITE_DATATYPE_BLOB = 0x04;
40. 1const SQLITE_DATATYPE_EXTERNALBUFFER = 0x71;
41. 1const SQLITE_DATATYPE_FLOAT = 0x02;
42. 1const SQLITE_DATATYPE_INTEGER = 0x01;
43. 1const SQLITE_DATATYPE_INTEGER_0 = 0x00;
44. 1const SQLITE_DATATYPE_INTEGER_1 = 0x21;
45. 1const SQLITE_DATATYPE_NULL = 0x05;
46. 1const SQLITE_DATATYPE_TEXT = 0x03;
47. 1const SQLITE_DATATYPE_TEXT_0 = 0x13;
48. 1const SQLITE_RESPONSETYPE_LASTBLOB = 1;
49. 1const SQLITE_RESPONSETYPE_LASTVALUE = 2;
50. 1
51. 1const FILENAME_DBTMP = "/tmp/__dbtmp1"; //jslint-ignore-line
52. 1
53. 1const LGBM_DTYPE_FLOAT32 = 0; /* float32 (single precision float)*/
54. 1const LGBM_DTYPE_FLOAT64 = 1; /* float64 (double precision float)*/
55. 1const LGBM_DTYPE_INT32 = 2; /* int32*/
56. 1const LGBM_DTYPE_INT64 = 3; /* int64*/
57. 1const LGBM_FEATURE_IMPORTANCE_GAIN = 1; /* Gain type of feature importance*/
58. 1const LGBM_FEATURE_IMPORTANCE_SPLIT = 0;/* Split type of feature importance*/
59. 1const LGBM_MATRIX_TYPE_CSC = 1; /* CSC sparse matrix type*/
60. 1const LGBM_MATRIX_TYPE_CSR = 0; /* CSR sparse matrix type*/
61. 1const LGBM_PREDICT_CONTRIB = 3; /* Predict feature contributions (SHAP values)*/
62. 1const LGBM_PREDICT_LEAF_INDEX = 2; /* Predict leaf index*/
63. 1const LGBM_PREDICT_NORMAL = 0; /* Normal prediction w/ transform (if needed)*/
64. 1const LGBM_PREDICT_RAW_SCORE = 1; /* Predict raw score*/
65. 1const SQLITE_OPEN_AUTOPROXY = 0x00000020; /* VFS only */
66. 1const SQLITE_OPEN_CREATE = 0x00000004; /* Ok for sqlite3_open_v2() */
67. 1const SQLITE_OPEN_DELETEONCLOSE = 0x00000008; /* VFS only */
68. 1const SQLITE_OPEN_EXCLUSIVE = 0x00000010; /* VFS only */
69. 1const SQLITE_OPEN_FULLMUTEX = 0x00010000; /* Ok for sqlite3_open_v2() */
70. 1const SQLITE_OPEN_MAIN_DB = 0x00000100; /* VFS only */
71. 1const SQLITE_OPEN_MAIN_JOURNAL = 0x00000800; /* VFS only */
72. 1const SQLITE_OPEN_MEMORY = 0x00000080; /* Ok for sqlite3_open_v2() */
73. 1const SQLITE_OPEN_NOFOLLOW = 0x01000000; /* Ok for sqlite3_open_v2() */
74. 1const SQLITE_OPEN_NOMUTEX = 0x00008000; /* Ok for sqlite3_open_v2() */
75. 1const SQLITE_OPEN_PRIVATECACHE = 0x00040000; /* Ok for sqlite3_open_v2() */
76. 1const SQLITE_OPEN_READONLY = 0x00000001; /* Ok for sqlite3_open_v2() */
77. 1const SQLITE_OPEN_READWRITE = 0x00000002; /* Ok for sqlite3_open_v2() */
78. 1const SQLITE_OPEN_SHAREDCACHE = 0x00020000; /* Ok for sqlite3_open_v2() */
79. 1const SQLITE_OPEN_SUBJOURNAL = 0x00002000; /* VFS only */
80. 1const SQLITE_OPEN_SUPER_JOURNAL = 0x00004000; /* VFS only */
81. 1const SQLITE_OPEN_TEMP_DB = 0x00000200; /* VFS only */
82. 1const SQLITE_OPEN_TEMP_JOURNAL = 0x00001000; /* VFS only */
83. 1const SQLITE_OPEN_TRANSIENT_DB = 0x00000400; /* VFS only */
84. 1const SQLITE_OPEN_URI = 0x00000040; /* Ok for sqlite3_open_v2() */
85. 1const SQLITE_OPEN_WAL = 0x00080000; /* VFS only */
86. 1
87. 1let DB_EXEC_PROFILE_DICT = {};
88. 1let DB_EXEC_PROFILE_MODE;
89. 1let DB_EXEC_PROFILE_SQL_LENGTH;
90. 1let DB_STATE = {};
91. 1let IS_BROWSER;
92. 1let SQLMATH_EXE;
93. 1let SQLMATH_NODE;
94. 1let cModule;
95. 1let cModulePath;
96. 1let consoleError = console.error;
97. 1let dbFinalizationRegistry;
98. 1// init debugInline
99. 1let debugInline = (function () {
100. 3 let __consoleError = function () {
101. 3 return;
102. 3 };
103. 1 function debug(...argv) {
104. 1
105. 1// This function will print <argv> to stderr and then return <argv>[0].
106. 1
107. 1 __consoleError("\n\ndebugInline");
108. 1 __consoleError(...argv);
109. 1 __consoleError("\n");
110. 1 return argv[0];
111. 1 }
112. 1 debug(); // Coverage-hack.
113. 1 __consoleError = console.error; //jslint-ignore-line
114. 1 return debug;
115. 1}());
116. 1let moduleChildProcess;
117. 1let moduleChildProcessSpawn;
118. 1let moduleCrypto;
119. 1let moduleFs;
120. 1let moduleFsInitResolveList;
121. 1let modulePath;
122. 1let moduleUrl;
123. 1let {
124. 1 npm_config_mode_debug,
125. 1 npm_config_mode_setup,
126. 1 npm_config_mode_test
127. 1} = typeof process === "object" && process?.env;
128. 1let sqlMessageDict = {}; // dict of web-worker-callbacks
129. 1let sqlMessageId = 0;
130. 1let sqlWorker;
131. 1let version = "v2026.5.31";
132. 1
133. 86async function assertErrorThrownAsync(asyncFunc, regexp) {
134. 86
135. 86// This function will assert calling <asyncFunc> throws an error.
136. 86
137. 86 let err;
138. 86 try {
139. 1 await asyncFunc();
140. 85 } catch (errCaught) {
141. 85 err = errCaught;
142. 85 }
143. 86 assertOrThrow(err, "No error thrown.");
144. 86 assertOrThrow(
145. 83 !regexp || new RegExp(regexp).test(err.message),
146. 86 err
147. 86 );
148. 86}
149. 1
150. 14016function assertInt64(val) {
151. 14016 // This function will assert <val> is within range of c99-signed-long-long.
152. 14016 val = BigInt(val);
153. 14016 if (!(
154. 14009 -9_223_372_036_854_775_808n <= val && val <= 9_223_372_036_854_775_807n
155. 14 )) {
156. 14 throw new Error(
157. 14 `integer ${val} outside signed-64-bit inclusive-range`
158. 14 + " -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807"
159. 14 );
160. 14 }
161. 14016}
162. 1
163. 4930function assertJsonEqual(aa, bb, message) {
164. 4930
165. 4930// This function will assert JSON.stringify(<aa>) === JSON.stringify(<bb>).
166. 4930
167. 4930 aa = JSON.stringify(objectDeepCopyWithKeysSorted(aa), undefined, 1);
168. 4930 bb = JSON.stringify(objectDeepCopyWithKeysSorted(bb), undefined, 1);
169. 3 if (aa !== bb) {
170. 3 throw new Error(
171. 3 "\n" + aa + "\n!==\n" + bb
172. 3 + (
173. 3 typeof message === "string"
174. 3 ? " - " + message
175. 3 : message
176. 3 ? " - " + JSON.stringify(message)
177. 3 : ""
178. 3 )
179. 3 );
180. 3 }
181. 4930}
182. 1
183. 4function assertNumericalEqual(aa, bb, message) {
184. 4
185. 4// This function will assert aa - bb <= Number.EPSILON.
186. 4
187. 4 assertOrThrow(aa, "value cannot be 0 or falsy");
188. 2 if (!(Math.abs((aa - bb) / Math.max(aa, bb)) <= 256 * Number.EPSILON)) {
189. 2 throw new Error(
190. 2 JSON.stringify(aa) + " != " + JSON.stringify(bb) + (
191. 2 message
192. 2 ? " - " + message
193. 2 : ""
194. 2 )
195. 2 );
196. 2 }
197. 4}
198. 1
199. 49203function assertOrThrow(condition, message) {
200. 49203
201. 49203// This function will throw <message> if <condition> is falsy.
202. 49203
203. 70 if (!condition) {
204. 70 throw (
205. 70 (!message || typeof message === "string")
206. 70 ? new Error(String(message).slice(0, 2048))
207. 70 : message
208. 70 );
209. 70 }
210. 49203}
211. 1
212. 15async function childProcessSpawn2(command, args, option) {
213. 15
214. 15// This function will run child_process.spawn as a promise.
215. 15
216. 15 return await new Promise(function (resolve, reject) {
217. 15 let bufList = [[], [], []];
218. 15 let child;
219. 15 let {
220. 15 modeCapture,
221. 15 modeDebug,
222. 15 stdio = []
223. 15 } = option;
224. 1 if (modeDebug) {
225. 1 consoleError(
226. 1 `childProcessSpawn2 - ${command} ${JSON.stringify(args)}`
227. 1 );
228. 1 }
229. 15 child = moduleChildProcessSpawn(
230. 15 command,
231. 15 args,
232. 15 Object.assign({}, option, {
233. 15 stdio: [
234. 15 "ignore",
235. 15 (
236. 15 modeCapture
237. 1 ? "pipe"
238. 14 : stdio[1]
239. 15 ),
240. 15 (
241. 15 modeCapture
242. 1 ? "pipe"
243. 14 : stdio[2]
244. 15 )
245. 15 ]
246. 15 })
247. 15 );
248. 1 if (modeCapture) {
249. 1 [
250. 1 child.stdin, child.stdout, child.stderr
251. 3 ].forEach(function (pipe, ii) {
252. 1 if (ii === 0) {
253. 1 return;
254. 2 }
255. 2 pipe.on("data", function (chunk) {
256. 2 bufList[ii].push(chunk);
257. 2 if (stdio[ii] !== "ignore") {
258. 2 switch (ii) {
259. 2 case 1:
260. 2 process.stdout.write(chunk);
261. 2 break;
262. 2 case 2:
263. 2 process.stderr.write(chunk);
264. 2 break;
265. 2 }
266. 2 }
267. 2 });
268. 2 });
269. 1 }
270. 15 child.on("exit", function (exitCode) {
271. 15 let resolve0 = resolve;
272. 15 let stderr;
273. 15 let stdout;
274. 15 // coverage-hack
275. 15 if (exitCode || npm_config_mode_test) {
276. 15 resolve = reject;
277. 15 }
278. 15 // coverage-hack
279. 15 if (npm_config_mode_test) {
280. 15 resolve = resolve0;
281. 15 }
282. 15 [
283. 15 stdout, stderr
284. 30 ] = bufList.slice(1).map(function (buf) {
285. 30 return (
286. 30 typeof modeCapture === "string"
287. 2 ? Buffer.concat(buf).toString(modeCapture)
288. 28 : Buffer.concat(buf)
289. 30 );
290. 30 });
291. 15 resolve([
292. 15 exitCode, stdout, stderr
293. 15 ]);
294. 15 });
295. 15 });
296. 15}
297. 1
298. 7async function ciBuildExt({
299. 7 process
300. 7}) {
301. 7
302. 7// This function will build sqlmath from c.
303. 7
304. 7 let binNodegyp;
305. 7 let exitCode;
306. 7 binNodegyp = modulePath.resolve(
307. 7 modulePath.dirname(process.execPath || ""),
308. 7 "node_modules/npm/node_modules/node-gyp/bin/node-gyp.js"
309. 7 ).replace("/bin/node_modules/", "/lib/node_modules/");
310. 7 if (!noop(
311. 7 await fsExistsUnlessTest(cModulePath)
312. 7 )) {
313. 7 await ciBuildExt1NodejsConfigure({
314. 7 binNodegyp,
315. 7 process
316. 7 });
317. 7 }
318. 7 consoleError(
319. 7 `ciBuildExt2Nodejs - linking lib ${modulePath.resolve(cModulePath)}`
320. 7 );
321. 7 [
322. 7 exitCode
323. 7 ] = await childProcessSpawn2(
324. 7 "sh",
325. 7 [
326. 7 "-c",
327. 7 (`
328. 7(set -e
329. 7 # rebuild binding
330. 7 node "${binNodegyp}" build --release
331. 7 # node "${binNodegyp}" build --release --loglevel=verbose
332. 7 mv build/Release/binding.node "${cModulePath}"
333. 7 mv build/Release/shell "${SQLMATH_EXE}"
334. 7 # ugly-hack - win32-sqlite-shell doesn't like nodejs-builtin-zlib,
335. 7 # so link with external-zlib.
336. 7 if (uname | grep -q "MING\\|MSYS")
337. 7 then
338. 7 rm -f ${SQLMATH_EXE}
339. 7 python setup.py exe_link \
340. 7 ./build/Release/SRC_SQLITE_BASE.lib \
341. 7 ./build/Release/SRC_SQLMATH_BASE.lib \
342. 7 ./build/Release/obj/shell/sqlmath_external_sqlite.obj \
343. 7 ./zlib.v1.3.1.vcpkg.x64-windows-static.lib \
344. 7 \
345. 7 -ltcg \
346. 7 -nologo \
347. 7 -out:${SQLMATH_EXE} \
348. 7 -subsystem:console
349. 7 fi
350. 7)
351. 7 `)
352. 7 ],
353. 7 {modeDebug: npm_config_mode_debug, stdio: ["ignore", 1, 2]}
354. 7 );
355. 7 assertOrThrow(!exitCode, `ciBuildExt - exitCode=${exitCode}`);
356. 7}
357. 1
358. 7async function ciBuildExt1NodejsConfigure({
359. 7 binNodegyp
360. 7 // process
361. 7}) {
362. 7
363. 7// This function will setup posix/win32 env for building c-extension.
364. 7
365. 7 let cflagWallList = [];
366. 7 let cflagWnoList = [];
367. 7 String(
368. 7 await fsReadFileUnlessTest(".ci.sh", "utf8", (`
369. 7SQLMATH_CFLAG_WALL_LIST=" \\
370. 7"
371. 7SQLMATH_CFLAG_WNO_LIST=" \\
372. 7"
373. 7 `))
374. 7 ).replace((
375. 7 /(SQLMATH_CFLAG_WALL_LIST|SQLMATH_CFLAG_WNO_LIST)=" \\([\S\s]*?)"/g
376. 14 ), function (ignore, cflagType, cflagList) {
377. 14 cflagList = cflagList.split(/[\s\\]/).filter(noop);
378. 14 switch (cflagType) {
379. 7 case "SQLMATH_CFLAG_WALL_LIST":
380. 7 cflagWallList = cflagList;
381. 7 break;
382. 7 case "SQLMATH_CFLAG_WNO_LIST":
383. 7 cflagWnoList = cflagList;
384. 7 break;
385. 14 }
386. 14 return "";
387. 14 });
388. 7 consoleError(`ciBuildExt1Nodejs - configure binding.gyp`);
389. 7 await fsWriteFileUnlessTest("binding.gyp", JSON.stringify({
390. 7 "target_defaults": {
391. 7 "cflags": cflagWallList,
392. 7// https://github.com/nodejs/node-gyp/blob/v10.3.1/gyp/pylib/gyp/MSVSSettings.py
393. 7 "msvs_settings": {
394. 7 "VCCLCompilerTool": {
395. 7 "WarnAsError": 1,
396. 7 "WarningLevel": 4
397. 7 }
398. 7 },
399. 7 "xcode_settings": {
400. 7 "OTHER_CFLAGS": cflagWallList
401. 7 }
402. 7 },
403. 7 "targets": [
404. 7 {
405. 7 "cflags": cflagWnoList,
406. 7 "defines": [
407. 7 "SRC_SQLITE_BASE_C2"
408. 7 ],
409. 7 "msvs_settings": {
410. 7 "VCCLCompilerTool": {
411. 7 "WarnAsError": 1,
412. 7 "WarningLevel": 2
413. 7 }
414. 7 },
415. 7 "sources": [
416. 7 "sqlmath_external_sqlite.c"
417. 7 ],
418. 7 "target_name": "SRC_SQLITE_BASE",
419. 7 "type": "static_library",
420. 7 "xcode_settings": {
421. 7 "OTHER_CFLAGS": cflagWnoList
422. 7 }
423. 7 },
424. 7 {
425. 7 "defines": [
426. 7 "SRC_SQLMATH_BASE_C2"
427. 7 ],
428. 7 "dependencies": [
429. 7 "SRC_SQLITE_BASE"
430. 7 ],
431. 7 "sources": [
432. 7 "sqlmath_base.c"
433. 7 ],
434. 7 "target_name": "SRC_SQLMATH_BASE",
435. 7 "type": "static_library"
436. 7 },
437. 7 {
438. 7 "defines": [
439. 7 "SRC_SQLMATH_NODEJS_C2"
440. 7 ],
441. 7 "dependencies": [
442. 7 "SRC_SQLMATH_BASE"
443. 7 ],
444. 7 "sources": [
445. 7 "sqlmath_base.c"
446. 7 ],
447. 7 "target_name": "binding"
448. 7 },
449. 7 {
450. 7 "conditions": [
451. 7 [
452. 7 "OS==\"win\"",
453. 7 {},
454. 7 {
455. 7 "libraries": [
456. 7 "-lz"
457. 7 ]
458. 7 }
459. 7 ]
460. 7 ],
461. 7 "defines": [
462. 7 "SRC_SQLITE_SHELL_C2"
463. 7 ],
464. 7 "dependencies": [
465. 7 "SRC_SQLMATH_BASE"
466. 7 ],
467. 7 "sources": [
468. 7 "sqlmath_external_sqlite.c"
469. 7 ],
470. 7 "target_name": "shell",
471. 7 "type": "executable"
472. 7 }
473. 7 ]
474. 7 }, undefined, 4) + "\n");
475. 7 await childProcessSpawn2(
476. 7 "sh",
477. 7 [
478. 7 "-c",
479. 7 (`
480. 7(set -e
481. 7 # node "${binNodegyp}" clean
482. 7 node "${binNodegyp}" configure
483. 7)
484. 7 `)
485. 7 ],
486. 7 {modeDebug: npm_config_mode_debug, stdio: ["ignore", 1, 2]}
487. 7 );
488. 7}
489. 1
490. 1function csvFromListofList({
491. 1 colList,
492. 1 rowList
493. 1}) {
494. 1// this function will convert json <rowList> to csv with given <colList>
495. 1 let data = JSON.stringify([[colList], rowList].flat(), undefined, 1);
496. 1 // convert data to csv
497. 1 data = data.replace((
498. 1 /\n /g
499. 1 ), "");
500. 1 data = data.replace((
501. 1 /\n \[/g
502. 1 ), "");
503. 1 data = data.replace((
504. 1 /\n \],?/g
505. 1 ), "\r\n");
506. 1 data = data.slice(1, -2);
507. 1 // sqlite-strings are c-strings which should never contain null-char
508. 1 data = data.replace((
509. 1 /\u0000/g
510. 1 ), "");
511. 1 // hide double-backslash `\\\\` as null-char
512. 1 data = data.replace((
513. 1 /\\\\/g
514. 1 ), "\u0000");
515. 1 // escape double-quote `\\"` to `""`
516. 1 data = data.replace((
517. 1 /\\"/g
518. 1 ), "\"\"");
519. 1 // replace newline with space
520. 1 data = data.replace((
521. 1 /\\r\\n|\\r|\\n/g
522. 1 ), " ");
523. 1 // restore double-backslash `\\\\` from null-char
524. 1 data = data.replace((
525. 1 /\u0000/g
526. 1 ), "\\\\");
527. 1 return data;
528. 1}
529. 1
530. 2function csvToListofList({
531. 2 csv
532. 2}) {
533. 2// This function will convert <csv>-text to json list-of-list.
534. 2//
535. 2// https://tools.ietf.org/html/rfc4180#section-2
536. 2// Definition of the CSV Format
537. 2// While there are various specifications and implementations for the
538. 2// CSV format (for ex. [4], [5], [6] and [7]), there is no formal
539. 2// specification in existence, which allows for a wide variety of
540. 2// interpretations of CSV files. This section documents the format that
541. 2// seems to be followed by most implementations:
542. 2//
543. 2// 1. Each record is located on a separate line, delimited by a line
544. 2// break (CRLF). For example:
545. 2// aaa,bbb,ccc CRLF
546. 2// zzz,yyy,xxx CRLF
547. 2//
548. 2// 2. The last record in the file may or may not have an ending line
549. 2// break. For example:
550. 2// aaa,bbb,ccc CRLF
551. 2// zzz,yyy,xxx
552. 2//
553. 2// 3. There maybe an optional header line appearing as the first line
554. 2// of the file with the same format as normal record lines. This
555. 2// header will contain names corresponding to the fields in the file
556. 2// and should contain the same number of fields as the records in
557. 2// the rest of the file (the presence or absence of the header line
558. 2// should be indicated via the optional "header" parameter of this
559. 2// MIME type). For example:
560. 2// field_name,field_name,field_name CRLF
561. 2// aaa,bbb,ccc CRLF
562. 2// zzz,yyy,xxx CRLF
563. 2//
564. 2// 4. Within the header and each record, there may be one or more
565. 2// fields, separated by commas. Each line should contain the same
566. 2// number of fields throughout the file. Spaces are considered part
567. 2// of a field and should not be ignored. The last field in the
568. 2// record must not be followed by a comma. For example:
569. 2// aaa,bbb,ccc
570. 2//
571. 2// 5. Each field may or may not be enclosed in double quotes (however
572. 2// some programs, such as Microsoft Excel, do not use double quotes
573. 2// at all). If fields are not enclosed with double quotes, then
574. 2// double quotes may not appear inside the fields. For example:
575. 2// "aaa","bbb","ccc" CRLF
576. 2// zzz,yyy,xxx
577. 2//
578. 2// 6. Fields containing line breaks (CRLF), double quotes, and commas
579. 2// should be enclosed in double-quotes. For example:
580. 2// "aaa","b CRLF
581. 2// bb","ccc" CRLF
582. 2// zzz,yyy,xxx
583. 2//
584. 2// 7. If double-quotes are used to enclose fields, then a double-quote
585. 2// appearing inside a field must be escaped by preceding it with
586. 2// another double quote. For example:
587. 2// "aaa","b""bb","ccc"
588. 2 let match;
589. 2 let quote;
590. 2 let rgx;
591. 2 let row;
592. 2 let rowList;
593. 2 let val;
594. 2 // normalize "\r\n" to "\n"
595. 2 csv = csv.trimEnd().replace((
596. 2 /\r\n?/gu
597. 2 ), "\n") + "\n";
598. 2 rgx = (
599. 2 /(.*?)(""|"|,|\n)/gu
600. 2 );
601. 2 rowList = [];
602. 2 // reset row
603. 2 row = [];
604. 2 val = "";
605. 28 while (true) {
606. 28 match = rgx.exec(csv);
607. 28 if (!match) {
608. 28// 2. The last record in the file may or may not have an ending line
609. 28// break. For example:
610. 28// aaa,bbb,ccc CRLF
611. 28// zzz,yyy,xxx
612. 28 if (!row.length) {
613. 28 break;
614. 28 }
615. 28 // // if eof missing crlf, then mock it
616. 28 // rgx.lastIndex = csv.length;
617. 28 // match = [
618. 28 // "\n", "", "\n"
619. 28 // ];
620. 28 }
621. 28 // build val
622. 28 val += match[1];
623. 28 if (match[2] === "\"") {
624. 28// 5. Each field may or may not be enclosed in double quotes (however
625. 28// some programs, such as Microsoft Excel, do not use double quotes
626. 28// at all). If fields are not enclosed with double quotes, then
627. 28// double quotes may not appear inside the fields. For example:
628. 28// "aaa","bbb","ccc" CRLF
629. 28// zzz,yyy,xxx
630. 28 quote = !quote;
631. 28 } else if (quote) {
632. 28// 7. If double-quotes are used to enclose fields, then a double-quote
633. 28// appearing inside a field must be escaped by preceding it with
634. 28// another double quote. For example:
635. 28// "aaa","b""bb","ccc"
636. 28 if (match[2] === "\"\"") {
637. 28 val += "\"";
638. 28// 6. Fields containing line breaks (CRLF), double quotes, and commas
639. 28// should be enclosed in double-quotes. For example:
640. 28// "aaa","b CRLF
641. 28// bb","ccc" CRLF
642. 28// zzz,yyy,xxx
643. 28 } else {
644. 28 val += match[2];
645. 28 }
646. 28 } else if (match[2] === ",") {
647. 28// 4. Within the header and each record, there may be one or more
648. 28// fields, separated by commas. Each line should contain the same
649. 28// number of fields throughout the file. Spaces are considered part
650. 28// of a field and should not be ignored. The last field in the
651. 28// record must not be followed by a comma. For example:
652. 28// aaa,bbb,ccc
653. 28 // delimit val
654. 28 row.push(val);
655. 28 val = "";
656. 28 } else if (match[2] === "\n") {
657. 28// 1. Each record is located on a separate line, delimited by a line
658. 28// break (CRLF). For example:
659. 28// aaa,bbb,ccc CRLF
660. 28// zzz,yyy,xxx CRLF
661. 28 // delimit val
662. 28 row.push(val);
663. 28 val = "";
664. 28 // append row
665. 28 rowList.push(row);
666. 28 // reset row
667. 28 row = [];
668. 28 }
669. 28 }
670. 2 // // append val
671. 2 // if (val) {
672. 2 // row.push(val);
673. 2 // }
674. 2 // // append row
675. 2 // if (row.length) {
676. 2 // rowList.push(row);
677. 2 // }
678. 2 return rowList;
679. 2}
680. 1
681. 3912async function dbCallAsync(baton, argList, mode, db) {
682. 3912
683. 3912// This function will call c-function dbXxx() with given <funcname>
684. 3912// and return [<baton>, ...argList].
685. 3912
686. 3912 let errStack;
687. 3912 let funcname;
688. 3912 let id;
689. 3912 let profileObj;
690. 3912 let profileStart;
691. 3912 let result;
692. 3912 let sql;
693. 3912 let timeElapsed;
694. 3912 // If argList contains <db>, then mark it as busy.
695. 2025 if (mode === "modeDbExec" || mode === "modeDbFile") {
696. 1902 // init db
697. 1902 db = argList[0];
698. 1902 assertOrThrow(
699. 1902 db.busy >= 0,
700. 1902 `dbCallAsync - invalid db.busy = ${db.busy}`
701. 1902 );
702. 1902 db.ii = (db.ii + 1) % db.connPool.length;
703. 1902 db.ptr = db.connPool[db.ii][0];
704. 1902 // increment db.busy
705. 1902 db.busy += 1;
706. 1902 // init profileObj
707. 1902 if (DB_EXEC_PROFILE_MODE && mode === "modeDbExec") {
708. 1902 profileStart = Date.now();
709. 1902 sql = String(argList[1]);
710. 1902 // sql-hash - remove comment
711. 1902 sql = sql.replace((/(?:^|\s+?)--.*/gm), "");
712. 1902 // sql-hash - remove vowel
713. 1902 sql = sql.replace((/[aeiou]\b/gi), "\u0000$&");
714. 1902 sql = sql.replace((/([bcdfghjklmnpqrstvwxyz])[aeiou]+/gi), "$1");
715. 1902 sql = sql.replace((/\u0000([aeiou])\b/gi), "$1");
716. 1902 // sql-hash - remove underscore
717. 1902 sql = sql.replace((/_+/g), "");
718. 1902 // sql-hash - truncate long text
719. 1902 sql = sql.replace((/(\S{16})\S+/g), "$1");
720. 1902 // sql-hash - remove whitespace
721. 1902 sql = sql.replace((/\s+/g), " ");
722. 1902 sql = sql.trim().slice(0, DB_EXEC_PROFILE_SQL_LENGTH);
723. 1902 DB_EXEC_PROFILE_DICT[sql] = DB_EXEC_PROFILE_DICT[sql] || {
724. 1902 busy: 0,
725. 1902 count: 0,
726. 1902 sql,
727. 1902 timeElapsed: 0
728. 1902 };
729. 1902 profileObj = DB_EXEC_PROFILE_DICT[sql];
730. 1902 // increment profileObj.busy
731. 1902 profileObj.busy += 1;
732. 1902 profileObj.count += 1;
733. 1902 }
734. 1902 try {
735. 1902 return await dbCallAsync(
736. 1902 baton,
737. 1902 [
738. 1902 db.ptr,
739. 1902 ...argList.slice(1)
740. 1902 ],
741. 1902 undefined,
742. 1902 db
743. 1902 );
744. 1902 } finally {
745. 1902 // decrement db.busy
746. 1902 db.busy -= 1;
747. 1902 assertOrThrow(
748. 1902 db.busy >= 0,
749. 1902 `dbCallAsync - invalid db.busy = ${db.busy}`
750. 1902 );
751. 1902 // update profileObj
752. 1902 if (profileObj) {
753. 1902 // decrement profileObj.busy
754. 1902 profileObj.busy -= 1;
755. 1902 assertOrThrow(
756. 1902 profileObj.busy >= 0,
757. 1902 `dbCallAsync - invalid profileObj.busy = ${profileObj.busy}`
758. 1902 );
759. 1902 if (profileObj.busy === 0) {
760. 1902 profileObj.timeElapsed += Date.now() - profileStart;
761. 1902 }
762. 1902 }
763. 1902 }
764. 2010 }
765. 2010 // copy argList to avoid side-effect
766. 2010 argList = [...argList];
767. 2010 assertOrThrow(
768. 2010 argList.length <= JSBATON_ARGC,
769. 2010 `dbCallAsync - argList.length must be less than than ${JSBATON_ARGC}`
770. 2010 );
771. 2010 // pad argList to length JSBATON_ARGC
772. 6221 while (argList.length < JSBATON_ARGC) {
773. 6221 argList.push(0n);
774. 6221 }
775. 2010 // serialize js-value to c-value
776. 15942 argList = argList.map(function (val, argi) {
777. 15941 if (val === null || val === undefined) {
778. 2010 val = 0;
779. 2010 }
780. 15942 switch (typeof val) {
781. 8039 case "bigint":
782. 9929 case "boolean":
783. 13984 case "number":
784. 13984 // check for min/max safe-integer
785. 13984 assertOrThrow(
786. 13984 (
787. 13984 (JS_MIN_SAFE_INTEGER <= val && val <= JS_MAX_SAFE_INTEGER)
788. 13984 || typeof val === "bigint"
789. 13984 ),
790. 13984 (
791. 13984 "dbCallAsync - "
792. 13984 + "non-bigint-integer must be within inclusive-range"
793. 13984 + ` ${JS_MIN_SAFE_INTEGER} to ${JS_MAX_SAFE_INTEGER}`
794. 13984 )
795. 13984 );
796. 13984 val = BigInt(val);
797. 13984 assertInt64(val);
798. 13984 baton.setBigInt64(JSBATON_OFFSET_ARGV + argi * 8, val, true);
799. 13984 return val;
800. 15942 // case "object":
801. 15942 // break;
802. 2010 case "string":
803. 2010 baton = jsbatonSetValue(baton, argi, (
804. 2010 val.endsWith("\u0000")
805. 2010 ? val
806. 2010 // append null-terminator to string
807. 2010 : val + "\u0000"
808. 2010 ));
809. 2010 return;
810. 2010 }
811. 2010 assertOrThrow(
812. 2010 !ArrayBuffer.isView(val) || val.byteOffset === 0,
813. 15942 (
814. 15942 "dbCallAsync - argList cannot contain arraybuffer-views"
815. 15942 + " with non-zero byteOffset"
816. 15942 )
817. 15942 );
818. 2010 if (isExternalBuffer(val)) {
819. 2010 return val;
820. 2010 }
821. 2010 throw new Error(`dbCallAsync - invalid arg-type "${typeof val}"`);
822. 2010 });
823. 2010 // assert byteOffset === 0
824. 17883 [baton, ...argList].forEach(function (arg) {
825. 2010 assertOrThrow(!ArrayBuffer.isView(arg) || arg.byteOffset === 0, arg);
826. 17883 });
827. 2010 // extract funcname
828. 2010 funcname = new TextDecoder().decode(
829. 2010 new DataView(baton.buffer, JSBATON_OFFSET_FUNCNAME, SIZEOF_FUNCNAME)
830. 2010 ).replace((/\u0000/g), "");
831. 2010 // preserve stack-trace
832. 2010 errStack = new Error().stack.replace((/.*$/m), "");
833. 2010 try {
834. 2010
835. 2010// Dispatch to nodejs-napi.
836. 2010
837. 2010 if (!IS_BROWSER) {
838. 1985 await cModule._jspromiseCreate(baton.buffer, argList, funcname);
839. 1985 // prepend baton to argList
840. 1985 return [baton, ...argList];
841. 1985 }
842. 2
843. 2// Dispatch to web-worker.
844. 2
845. 2 // increment sqlMessageId
846. 2 sqlMessageId += 1;
847. 2 id = sqlMessageId;
848. 2 // postMessage to web-worker
849. 2 sqlWorker.postMessage(
850. 2 {
851. 2 FILENAME_DBTMP,
852. 2 JSBATON_OFFSET_ALL,
853. 2 JSBATON_OFFSET_BUFV,
854. 2 argList,
855. 2 baton,
856. 2 funcname,
857. 2 id
858. 2 },
859. 2 // transfer arraybuffer without copying
860. 2 [baton.buffer, ...argList.filter(isExternalBuffer)]
861. 2 );
862. 2 // init timeElapsed
863. 2 timeElapsed = Date.now();
864. 2 // await result from web-worker
865. 2 result = await new Promise(function (resolve) {
866. 2 sqlMessageDict[id] = resolve;
867. 2 });
868. 2 // cleanup sqlMessageDict
869. 2 delete sqlMessageDict[id];
870. 2 // debug slow postMessage
871. 2 timeElapsed = Date.now() - timeElapsed;
872. 2 if (timeElapsed > 500 || funcname === "testTimeElapsed") {
873. 1 consoleError(
874. 1 "sqlMessagePost - "
875. 1 + JSON.stringify({funcname, timeElapsed})
876. 1 + errStack
877. 1 );
878. 2 }
879. 2 assertOrThrow(!result.errmsg, result.errmsg);
880. 2 // prepend baton to argList
881. 2 return [result.baton, ...result.argList];
882. 55 } catch (err) {
883. 55 // debug db.filename
884. 55 if (db?.filename2 || db?.filename) {
885. 55 err.message += ` (from ${db?.filename2 || db?.filename})`;
886. 55 }
887. 55 err.stack += errStack;
888. 55 assertOrThrow(undefined, err);
889. 55 }
890. 3912}
891. 1
892. 3async function dbCloseAsync(db) {
893. 3
894. 3// This function will close sqlite-database-connection <db>.
895. 3
896. 3 // prevent segfault - do not close db if actions are pending
897. 3 assertOrThrow(
898. 3 db.busy === 0,
899. 3 `dbCloseAsync - cannot close db with ${db.busy} actions pending`
900. 3 );
901. 3 // cleanup connPool
902. 1 await Promise.all(db.connPool.map(async function (ptr) {
903. 1 let val = ptr[0];
904. 1 ptr[0] = 0n;
905. 1 await dbCallAsync(
906. 1 jsbatonCreate("_dbClose"),
907. 1 [
908. 1 val,
909. 1 db.filename
910. 1 ]
911. 1 );
912. 1 }));
913. 1}
914. 1
915. 58function dbExecAndReturnLastBlob(option) {
916. 58
917. 58// This function will exec <sql> in <db>,
918. 58// and return last-value retrieved from execution as raw blob/buffer.
919. 58
920. 58 return dbExecAsync(Object.assign({
921. 58 responseType: "lastblob"
922. 58 }, option));
923. 58}
924. 1
925. 115async function dbExecAndReturnLastRow(option) {
926. 115
927. 115// This function will exec <sql> in <db>,
928. 115// and return last-row or empty-object.
929. 115
930. 101 let result = await dbExecAsync(option);
931. 101 result = result[result.length - 1] || [];
932. 1 result = result[result.length - 1] || {};
933. 115 return result;
934. 115}
935. 1
936. 65async function dbExecAndReturnLastTable(option) {
937. 65
938. 65// This function will exec <sql> in <db>,
939. 65// and return last-table or empty-list.
940. 65
941. 65 let result = await dbExecAsync(option);
942. 1 result = result[result.length - 1] || [];
943. 65 return result;
944. 65}
945. 1
946. 1184function dbExecAndReturnLastValue(option) {
947. 1184
948. 1184// This function will exec <sql> in <db>,
949. 1184// and return last-json-value.
950. 1184
951. 1184 return dbExecAsync(Object.assign({
952. 1184 responseType: "lastvalue"
953. 1184 }, option));
954. 1184}
955. 1
956. 1900async function dbExecAsync({
957. 1900 bindList = [],
958. 1900 db,
959. 1900 modeNoop,
960. 1900 responseType,
961. 1900 sql
962. 1900}) {
963. 1900
964. 1900// This function will exec <sql> in <db> and return <result>.
965. 1900
966. 1900 let baton = jsbatonCreate("_dbExec");
967. 1900 let bindByKey = !Array.isArray(bindList);
968. 1900 let bufi = [0];
969. 1900 let referenceList = [];
970. 1900 let result;
971. 1 if (modeNoop) {
972. 1 return;
973. 1899 }
974. 1899 if (bindByKey) {
975. 1347 Object.entries(bindList).forEach(function ([key, val]) {
976. 1347 baton = jsbatonSetValue(baton, undefined, `:${key}\u0000`);
977. 1347 baton = jsbatonSetValue(
978. 1347 baton,
979. 1347 undefined,
980. 1347 val,
981. 1347 bufi,
982. 1347 referenceList
983. 1347 );
984. 1347 });
985. 1235 } else {
986. 664 bindList.forEach(function (val) {
987. 664 baton = jsbatonSetValue(
988. 664 baton,
989. 664 undefined,
990. 664 val,
991. 664 bufi,
992. 664 referenceList
993. 664 );
994. 664 });
995. 1887 }
996. 1887 [
997. 1887 baton, ...result
998. 1887 ] = await dbCallAsync(
999. 1887 baton,
1000. 1887 [
1001. 1887 // 0. db
1002. 1887 db,
1003. 1887 // 1. sql
1004. 1887 String(sql) + "\n;\nPRAGMA noop",
1005. 1887 // 2. bindList.length
1006. 1887 (
1007. 1887 bindByKey
1008. 1887 ? Object.keys(bindList).length
1009. 652 : bindList.length
1010. 1900 ),
1011. 1900 // 3. bindByKey
1012. 1900 bindByKey,
1013. 1900 // 4. responseType
1014. 1900 (
1015. 1900 responseType === "lastblob"
1016. 56 ? SQLITE_RESPONSETYPE_LASTBLOB
1017. 1831 : responseType === "lastvalue"
1018. 1831 ? SQLITE_RESPONSETYPE_LASTVALUE
1019. 1831 : 0
1020. 1900 )
1021. 1900 ],
1022. 1900 "modeDbExec"
1023. 1832 );
1024. 1832 result = result[0];
1025. 1832 if (!IS_BROWSER) {
1026. 1832 result = cModule._jsbatonStealCbuffer(
1027. 1832 baton.buffer,
1028. 1832 0,
1029. 1832 Number(
1030. 1832 responseType !== "arraybuffer" && responseType !== "lastblob"
1031. 1832 )
1032. 1832 );
1033. 1832 }
1034. 1832 switch (responseType) {
1035. 1832 case "arraybuffer":
1036. 110 case "lastblob":
1037. 110 break;
1038. 1236 case "lastvalue":
1039. 1403 case "list":
1040. 1403 result = jsonParseArraybuffer(result);
1041. 1403 break;
1042. 319 default:
1043. 345 result = jsonParseArraybuffer(result).map(function (table) {
1044. 345 let colList = table.shift();
1045. 13913 return table.map(function (row) {
1046. 13913 let dict = {};
1047. 43338 colList.forEach(function (key, ii) {
1048. 43338 dict[key] = row[ii];
1049. 43338 });
1050. 13913 return dict;
1051. 13913 });
1052. 345 });
1053. 1832 }
1054. 1832 return result;
1055. 1832}
1056. 1
1057. 2function dbExecProfile({
1058. 2 limit = 20,
1059. 2 lineLength = 80,
1060. 2 modeInit,
1061. 2 sqlLength = 256
1062. 2}) {
1063. 2
1064. 2// This function will profile dbExecAsync.
1065. 2
1066. 2 let result;
1067. 1 if (modeInit && !DB_EXEC_PROFILE_MODE) {
1068. 1 DB_EXEC_PROFILE_MODE = Date.now();
1069. 1 DB_EXEC_PROFILE_SQL_LENGTH = sqlLength;
1070. 1 process.on("exit", function () {
1071. 1 console.error(dbExecProfile({
1072. 1 limit,
1073. 1 lineLength
1074. 1 }));
1075. 1 });
1076. 1 return;
1077. 1 }
1078. 1 result = Object.values(DB_EXEC_PROFILE_DICT);
1079. 1581 result.sort(function (aa, bb) {
1080. 449 return ((bb.timeElapsed - aa.timeElapsed) || (bb.count - aa.count));
1081. 1581 });
1082. 20 result = result.slice(0, limit).map(function ({
1083. 20 count,
1084. 20 sql,
1085. 20 timeElapsed
1086. 20 }, ii) {
1087. 20 return String(
1088. 20 `${Number(ii + 1).toFixed(0).padStart(2, " ")}.`
1089. 20 + ` ${timeElapsed.toFixed(0).padStart(4)}`
1090. 20 + ` ${count.toFixed(0).padStart(3)}`
1091. 20 + " " + JSON.stringify(sql)
1092. 20 ).slice(0, lineLength);
1093. 20 }).join("\n");
1094. 1 result = (
1095. 1 `\ndbExecProfile:\n`
1096. 1 + ` # time cnt sql\n`
1097. 1 + `${result}\n`
1098. 1 );
1099. 1 return result;
1100. 1}
1101. 1
1102. 17async function dbFileLoadAsync({
1103. 17 db,
1104. 17 dbData,
1105. 17 filename,
1106. 17 modeNoop,
1107. 17 modeSave = 0
1108. 17}) {
1109. 17
1110. 17// This function will load <filename> to <db>.
1111. 17
1112. 17 let filename2;
1113. 15 async function _dbFileLoad() {
1114. 15 dbData = await dbCallAsync(
1115. 15 jsbatonCreate("_dbFileLoad"),
1116. 15 [
1117. 15 // 0. sqlite3 * pInMemory
1118. 15 db,
1119. 15 // 1. char *zFilename
1120. 15 filename,
1121. 15 // 2. const int isSave
1122. 15 modeSave,
1123. 15 // 3. undefined
1124. 15 undefined,
1125. 15 // 4. dbData - same position as dbOpenAsync
1126. 15 dbData
1127. 15 ],
1128. 15 "modeDbFile"
1129. 15 );
1130. 15 }
1131. 1 if (modeNoop) {
1132. 1 return;
1133. 16 }
1134. 16 if (IS_BROWSER) {
1135. 1 filename = FILENAME_DBTMP;
1136. 16 }
1137. 16 assertOrThrow(
1138. 16 typeof filename === "string" && filename,
1139. 17 `invalid filename ${filename}`
1140. 17 );
1141. 17 db.filename2 = filename;
1142. 17 // Save to tmpfile and then atomically-rename to actual-filename.
1143. 15 if (moduleFs && modeSave) {
1144. 13 filename2 = filename;
1145. 13 filename = modulePath.join(
1146. 13 modulePath.dirname(filename),
1147. 13 `.dbFileSaveAsync.${moduleCrypto.randomUUID()}`
1148. 13 );
1149. 13 try {
1150. 13 await _dbFileLoad();
1151. 13 await moduleFs.promises.rename(filename, filename2);
1152. 13 } finally {
1153. 13 await moduleFs.promises.unlink(filename).catch(noop);
1154. 13 }
1155. 13 } else {
1156. 2 await _dbFileLoad();
1157. 15 }
1158. 15 return dbData[1 + 0];
1159. 15}
1160. 1
1161. 14async function dbFileSaveAsync({
1162. 14 db,
1163. 14 dbData,
1164. 14 filename,
1165. 14 modeNoop
1166. 14}) {
1167. 14
1168. 14// This function will save <db> to <filename>.
1169. 14
1170. 14 return await dbFileLoadAsync({
1171. 14 db,
1172. 14 dbData,
1173. 14 filename,
1174. 14 modeNoop,
1175. 14 modeSave: 1
1176. 14 });
1177. 14}
1178. 1
1179. 56async function dbNoopAsync(...argList) {
1180. 56
1181. 56// This function will do nothing except return <argList>.
1182. 56
1183. 56 return await dbCallAsync(
1184. 56 jsbatonCreate("_dbNoop"),
1185. 56 argList
1186. 56 );
1187. 56}
1188. 1
1189. 32async function dbOpenAsync({
1190. 32 afterFinalization,
1191. 32 dbData,
1192. 32 filename = ":memory:",
1193. 32 flags,
1194. 32 threadCount = 1,
1195. 32 timeoutBusy = 5000
1196. 32}) {
1197. 32
1198. 32// This function will open and return sqlite-database-connection <db>.
1199. 32
1200. 32// int sqlite3_open_v2(
1201. 32// const char *filename, /* Database filename (UTF-8) */
1202. 32// sqlite3 **ppDb, /* OUT: SQLite db handle */
1203. 32// int flags, /* Flags */
1204. 32// const char *zVfs /* Name of VFS module to use */
1205. 32// );
1206. 32 let connPool;
1207. 32 let db = {busy: 0, filename, ii: 0};
1208. 32 assertOrThrow(typeof filename === "string", `invalid filename ${filename}`);
1209. 32 assertOrThrow(
1210. 1 !dbData || isExternalBuffer(dbData),
1211. 32 "dbData must be ArrayBuffer"
1212. 32 );
1213. 32 connPool = await Promise.all(Array.from(new Array(
1214. 32 threadCount
1215. 32 ), async function () {
1216. 32 let [ptr] = await dbCallAsync(
1217. 32 jsbatonCreate("_dbOpen"),
1218. 32 [
1219. 32 // 0. const char *filename, Database filename (UTF-8)
1220. 32 filename,
1221. 32 // 1. sqlite3 **ppDb, OUT: SQLite db handle
1222. 32 undefined,
1223. 32 // 2. int flags, Flags
1224. 32 flags ?? (
1225. 32 SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI
1226. 32 ),
1227. 32 // 3. const char *zVfs Name of VFS module to use
1228. 32 undefined,
1229. 32 // 4. wasm-only - arraybuffer of raw sqlite-database
1230. 32 dbData
1231. 32 ]
1232. 32 );
1233. 32 ptr = [ptr.getBigInt64(JSBATON_OFFSET_ARGV + 0, true)];
1234. 32 dbFinalizationRegistry.register(db, {afterFinalization, ptr});
1235. 32 return ptr;
1236. 32 }));
1237. 32 db.connPool = connPool;
1238. 1 if (!IS_BROWSER && !DB_STATE.init) {
1239. 1 DB_STATE.init = true;
1240. 1 await Promise.all([
1241. 1 // PRAGMA busy_timeout
1242. 1 dbExecAsync({
1243. 1 db,
1244. 1 sql: (`
1245. 1PRAGMA busy_timeout = ${timeoutBusy};
1246. 1 `)
1247. 1 }),
1248. 1 // LGBM_DLOPEN
1249. 1 (async function () {
1250. 1 let libLgbm = `lib_lightgbm_${libPlatformArchExt()}`;
1251. 1 libLgbm = `${import.meta.dirname}/sqlmath/${libLgbm}`;
1252. 1 await moduleFs.promises.access(
1253. 1 libLgbm
1254. 1 ).then(async function () {
1255. 1 await dbExecAsync({
1256. 1 db,
1257. 1 sql: (`
1258. 1SELECT LGBM_DLOPEN('${libLgbm}');
1259. 1 `)
1260. 1 });
1261. 1 DB_STATE.lgbm = true;
1262. 1 }).catch(noop);
1263. 1 }())
1264. 1 ]);
1265. 1 }
1266. 32 return db;
1267. 32}
1268. 1
1269. 47async function dbTableImportAsync({
1270. 47 db,
1271. 47 filename,
1272. 47 headerMissing,
1273. 47 mode,
1274. 47 tableName,
1275. 47 textData
1276. 47}) {
1277. 47// This function will create table from imported csv/json <textData>.
1278. 47 let colList;
1279. 47 let rowList;
1280. 47 let rowidList;
1281. 47 let tmp;
1282. 42 if (filename) {
1283. 42 textData = await moduleFs.promises.readFile(filename, "utf8");
1284. 42 }
1285. 47 switch (mode) {
1286. 2 case "csv":
1287. 2 rowList = csvToListofList({
1288. 2 csv: textData
1289. 2 });
1290. 2 break;
1291. 43 case "tsv":
1292. 43 rowList = [];
1293. 99001 textData.trimEnd().replace((/.+/g), function (line) {
1294. 99001 rowList.push(line.split("\t"));
1295. 99001 });
1296. 43 break;
1297. 47 // case "json":
1298. 2 default:
1299. 2 rowList = JSON.parse(textData);
1300. 47 }
1301. 1 if (!(typeof rowList === "object" && rowList)) {
1302. 1 rowList = [];
1303. 1 }
1304. 47 // normalize rowList to list
1305. 1 if (!Array.isArray(rowList)) {
1306. 1 rowidList = [];
1307. 2 rowList = Object.entries(rowList).map(function ([
1308. 2 key, val
1309. 2 ]) {
1310. 2 rowidList.push(key);
1311. 2 return val;
1312. 2 });
1313. 1 }
1314. 47 // headerMissing
1315. 42 if (headerMissing && (rowList.length > 0 && Array.isArray(rowList[0]))) {
1316. 714 rowList.unshift(Array.from(rowList[0]).map(function (ignore, ii) {
1317. 714 return String(ii + 1);
1318. 714 }));
1319. 42 }
1320. 47 // normalize rowList[ii] to list
1321. 1 if (rowList.length === 0) {
1322. 1 rowList.push([
1323. 1 "undefined"
1324. 1 ]);
1325. 1 }
1326. 1 if (!Array.isArray(rowList[0])) {
1327. 1 colList = Array.from(
1328. 1 new Set(
1329. 2 rowList.map(function (obj) {
1330. 2 return Object.keys(obj);
1331. 2 }).flat()
1332. 1 )
1333. 1 );
1334. 2 rowList = rowList.map(function (obj) {
1335. 4 return colList.map(function (key) {
1336. 4 return obj[key];
1337. 4 });
1338. 2 });
1339. 1 rowList.unshift(colList);
1340. 1 }
1341. 47 // init colList
1342. 47 colList = rowList.shift();
1343. 47 // preserve rowid
1344. 1 if (rowidList) {
1345. 1 colList.unshift("rowid");
1346. 2 rowList.forEach(function (row, ii) {
1347. 2 row.unshift(rowidList[ii]);
1348. 2 });
1349. 1 }
1350. 47 // normalize colList
1351. 47 tmp = new Set();
1352. 723 colList = colList.map(function (colName) {
1353. 723 let colName2;
1354. 723 let duplicate = 0;
1355. 723 colName = colName.trim();
1356. 723 colName = colName.replace((/\W/g), "_");
1357. 723 colName = colName.replace((/^[^A-Z_a-z]|^$/gm), "_$&");
1358. 724 while (true) {
1359. 724 duplicate += 1;
1360. 724 colName2 = (
1361. 724 duplicate === 1
1362. 724 ? colName
1363. 724 : colName + "_" + duplicate
1364. 724 );
1365. 724 if (!tmp.has(colName2)) {
1366. 724 tmp.add(colName2);
1367. 724 return colName2;
1368. 724 }
1369. 724 }
1370. 723 });
1371. 47 // create dbtable from rowList
1372. 47 await dbExecAsync({
1373. 47 bindList: {
1374. 47 rowList: JSON.stringify(rowList)
1375. 47 },
1376. 47 db,
1377. 47 sql: (
1378. 47 rowList.length === 0
1379. 3 ? `CREATE TABLE ${tableName} (${colList.join(",")});`
1380. 44 : (
1381. 44 `CREATE TABLE ${tableName} AS SELECT `
1382. 719 + colList.map(function (colName, ii) {
1383. 719 return "value->>" + ii + " AS " + colName;
1384. 719 }).join(",")
1385. 44 + " FROM JSON_EACH($rowList);"
1386. 44 )
1387. 47 )
1388. 47 });
1389. 47}
1390. 1
1391. 2async function fsCopyFileUnlessTest(file1, file2, mode) {
1392. 2
1393. 2// This function will copy <file1> to <file2> unless <npm_config_mode_test> = 1.
1394. 2
1395. 1 if (npm_config_mode_test && mode !== "force") {
1396. 1 return;
1397. 1 }
1398. 1 await moduleFs.promises.copyFile(file1, file2, mode | 0);
1399. 1}
1400. 1
1401. 10async function fsExistsUnlessTest(file, mode) {
1402. 10
1403. 10// This function will test if <file> exists unless <npm_config_mode_test> = 1.
1404. 10
1405. 8 if (npm_config_mode_test && mode !== "force") {
1406. 8 return false;
1407. 8 }
1408. 2 try {
1409. 2 await moduleFs.promises.access(file, moduleFs.promises.constants.F_OK);
1410. 1 return true;
1411. 1 } catch (ignore) {
1412. 1 return false;
1413. 1 }
1414. 10}
1415. 1
1416. 22async function fsReadFileUnlessTest(file, mode, defaultData) {
1417. 22
1418. 22// This function will read <data> from <file> unless <npm_config_mode_test> = 1.
1419. 22
1420. 8 if (npm_config_mode_test && mode !== "force") {
1421. 8 return defaultData;
1422. 14 }
1423. 14 return await moduleFs.promises.readFile(
1424. 14 file,
1425. 14 mode && mode.replace("force", "utf8")
1426. 22 );
1427. 22}
1428. 1
1429. 10async function fsWriteFileUnlessTest(file, data, mode) {
1430. 10
1431. 10// This function will write <data> to <file> unless <npm_config_mode_test> = 1.
1432. 10
1433. 9 if (npm_config_mode_test && mode !== "force") {
1434. 9 return;
1435. 9 }
1436. 1 await moduleFs.promises.writeFile(file, data);
1437. 1}
1438. 1
1439. 31function isExternalBuffer(buf) {
1440. 31
1441. 31// This function will check if <buf> is ArrayBuffer.
1442. 31
1443. 17 return buf && buf.constructor === ArrayBuffer;
1444. 31}
1445. 1
1446. 2023function jsbatonCreate(funcname) {
1447. 2023
1448. 2023// This function will create buffer <baton>.
1449. 2023
1450. 2023 let baton = new DataView(new ArrayBuffer(JSBATON_OFFSET_ALL));
1451. 2023 // init nallc, nused
1452. 2023 baton.setInt32(4, JSBATON_OFFSET_ALL, true);
1453. 2023 // copy funcname into baton
1454. 2023 new Uint8Array(
1455. 2023 baton.buffer,
1456. 2023 baton.byteOffset + JSBATON_OFFSET_FUNCNAME,
1457. 2023 SIZEOF_FUNCNAME - 1
1458. 2023 ).set(new TextEncoder().encode(funcname));
1459. 2023 return baton;
1460. 2023}
1461. 1
1462. 24function jsbatonGetInt64(baton, argi) {
1463. 24
1464. 24// This function will return int64-value from <baton> at <argi>.
1465. 24
1466. 24 return baton.getBigInt64(JSBATON_OFFSET_ARGV + argi * 8, true);
1467. 24}
1468. 1
1469. 9function jsbatonGetString(baton, argi) {
1470. 9
1471. 9// This function will return string-value from <baton> at <argi>.
1472. 9
1473. 9 let offset = baton.getInt32(JSBATON_OFFSET_ARGV + argi * 8, true);
1474. 9 return new TextDecoder().decode(new Uint8Array(
1475. 9 baton.buffer,
1476. 9 baton.byteOffset + offset + 1 + 4,
1477. 9 // remove null-terminator from string
1478. 9 baton.getInt32(offset + 1, true) - 1
1479. 9 ));
1480. 9}
1481. 1
1482. 5154function jsbatonSetValue(baton, argi, val, bufi, referenceList) {
1483. 5154
1484. 5154// This function will set <val> to buffer <baton>.
1485. 5154
1486. 5154 let nn;
1487. 5154 let nused;
1488. 5154 let tmp;
1489. 5154 let vsize;
1490. 5154 let vtype;
1491. 5154/*
1492. 5154#define SQLITE_DATATYPE_BLOB 0x04
1493. 5154#define SQLITE_DATATYPE_EXTERNALBUFFER 0x71
1494. 5154#define SQLITE_DATATYPE_FLOAT 0x02
1495. 5154#define SQLITE_DATATYPE_INTEGER 0x01
1496. 5154#define SQLITE_DATATYPE_INTEGER_0 0x00
1497. 5154#define SQLITE_DATATYPE_INTEGER_1 0x21
1498. 5154#define SQLITE_DATATYPE_NULL 0x05
1499. 5154#define SQLITE_DATATYPE_TEXT 0x03
1500. 5154#define SQLITE_DATATYPE_TEXT_0 0x13
1501. 5154 // 1. 0.bigint
1502. 5154 // 2. 0.boolean
1503. 5154 // 3. 0.function
1504. 5154 // 4. 0.number
1505. 5154 // 5. 0.object
1506. 5154 // 6. 0.string
1507. 5154 // 7. 0.symbol
1508. 5154 // 8. 0.undefined
1509. 5154 // 9. 1.bigint
1510. 5154 // 10. 1.boolean
1511. 5154 // 11. 1.function
1512. 5154 // 12. 1.number
1513. 5154 // 13. 1.object
1514. 5154 // 14. 1.string
1515. 5154 // 15. 1.symbol
1516. 5154 // 16. 1.undefined
1517. 5154 // 17. 1.buffer
1518. 5154 // 18. 1.externalbuffer
1519. 5154*/
1520. 5154 // 10. 1.boolean
1521. 5130 if (val === 1 || val === 1n) {
1522. 30 val = true;
1523. 30 }
1524. 5154 switch (
1525. 5154 val
1526. 4773 ? "1." + typeof(val)
1527. 381 : "0." + typeof(val)
1528. 5154 ) {
1529. 5154 // 1. 0.bigint
1530. 24 case "0.bigint":
1531. 5154 // 2. 0.boolean
1532. 30 case "0.boolean":
1533. 5154 // 4. 0.number
1534. 250 case "0.number":
1535. 250 if (Number.isNaN(val)) {
1536. 250 vtype = SQLITE_DATATYPE_NULL;
1537. 250 vsize = 0;
1538. 250 break;
1539. 250 }
1540. 250 vtype = SQLITE_DATATYPE_INTEGER_0;
1541. 250 vsize = 0;
1542. 250 break;
1543. 5154 // 3. 0.function
1544. 5154 // case "0.function":
1545. 5154 // 5. 0.object
1546. 98 case "0.object":
1547. 5154 // 7. 0.symbol
1548. 98 case "0.symbol":
1549. 5154 // 8. 0.undefined
1550. 105 case "0.undefined":
1551. 5154 // 11. 1.function
1552. 119 case "1.function":
1553. 5154 // 15. 1.symbol
1554. 125 case "1.symbol":
1555. 125 // 16. 1.undefined
1556. 125 // case "1.undefined":
1557. 125 vtype = SQLITE_DATATYPE_NULL;
1558. 125 vsize = 0;
1559. 125 break;
1560. 5154 // 6. 0.string
1561. 26 case "0.string":
1562. 26 vtype = SQLITE_DATATYPE_TEXT_0;
1563. 26 vsize = 0;
1564. 26 break;
1565. 5154 // 9. 1.bigint
1566. 42 case "1.bigint":
1567. 42 vtype = SQLITE_DATATYPE_INTEGER;
1568. 42 vsize = 8;
1569. 42 break;
1570. 5154 // 10. 1.boolean
1571. 36 case "1.boolean":
1572. 36 vtype = SQLITE_DATATYPE_INTEGER_1;
1573. 36 vsize = 0;
1574. 36 break;
1575. 5154 // 12. 1.number
1576. 561 case "1.number":
1577. 561 vtype = SQLITE_DATATYPE_FLOAT;
1578. 561 vsize = 8;
1579. 561 break;
1580. 5154 // 14. 1.string
1581. 4032 case "1.string":
1582. 4032 val = new TextEncoder().encode(val);
1583. 4032 vtype = SQLITE_DATATYPE_TEXT;
1584. 4032 vsize = 4 + val.byteLength;
1585. 4032 break;
1586. 5154 // 13. 1.object
1587. 82 default:
1588. 82 // 18. 1.externalbuffer
1589. 82 if (val.constructor === ArrayBuffer) {
1590. 82 assertOrThrow(
1591. 82 !IS_BROWSER,
1592. 82 "external ArrayBuffer cannot be passed directly to wasm"
1593. 82 );
1594. 82 vtype = SQLITE_DATATYPE_EXTERNALBUFFER;
1595. 82 vsize = 4;
1596. 82 break;
1597. 82 }
1598. 82 // 17. 1.buffer
1599. 82 if (ArrayBuffer.isView(val)) {
1600. 82 if (val.byteLength === 0) {
1601. 82 vtype = SQLITE_DATATYPE_NULL;
1602. 82 vsize = 0;
1603. 82 break;
1604. 82 }
1605. 82 vtype = SQLITE_DATATYPE_BLOB;
1606. 82 vsize = 4 + val.byteLength;
1607. 82 break;
1608. 82 }
1609. 82 // 13. 1.object
1610. 82 val = new TextEncoder().encode(
1611. 82 typeof val.toJSON === "function"
1612. 82 ? val.toJSON()
1613. 82 : JSON.stringify(val)
1614. 82 );
1615. 82 vtype = SQLITE_DATATYPE_TEXT;
1616. 82 vsize = 4 + val.byteLength;
1617. 5154 }
1618. 5154 nused = baton.getInt32(4, true);
1619. 5154 nn = nused + 1 + vsize;
1620. 5154 assertOrThrow(
1621. 5154 nn <= 0xffff_ffff,
1622. 5154 "jsbaton cannot exceed 0x7fff_ffff / 2,147,483,647 bytes"
1623. 5154 );
1624. 5154 // exponentially grow baton as needed
1625. 2136 if (baton.byteLength < nn) {
1626. 2136 tmp = baton;
1627. 2136 baton = new DataView(new ArrayBuffer(
1628. 2136 Math.min(2 ** Math.ceil(Math.log2(nn)), 0x7fff_ffff)
1629. 2136 ));
1630. 2136 // update nallc
1631. 2136 baton.setInt32(0, baton.byteLength, true);
1632. 2136 // copy old-baton into new-baton
1633. 2136 new Uint8Array(baton.buffer, baton.byteOffset, nused).set(
1634. 2136 new Uint8Array(tmp.buffer, tmp.byteOffset, nused)
1635. 2136 );
1636. 2136 }
1637. 5154 // push vtype - 1-byte
1638. 5154 baton.setUint8(nused, vtype);
1639. 5154 // update nused
1640. 5154 baton.setInt32(4, nused + 1 + vsize, true);
1641. 5154 // handle blob-value
1642. 5154 switch (vtype) {
1643. 30 case SQLITE_DATATYPE_BLOB:
1644. 4090 case SQLITE_DATATYPE_TEXT:
1645. 4090 // set argv[ii] to blob/text location
1646. 4090 if (argi !== undefined) {
1647. 4090 baton.setInt32(JSBATON_OFFSET_ARGV + argi * 8, nused, true);
1648. 4090 }
1649. 4090 vsize -= 4;
1650. 4090 assertOrThrow(
1651. 4090 0 <= vsize && vsize <= SIZEOF_BLOB_MAX,
1652. 4090 (
1653. 4090 "sqlite-blob byte-length must be within inclusive-range"
1654. 4090 + ` 0 to ${SIZEOF_BLOB_MAX}`
1655. 4090 )
1656. 4090 );
1657. 4090 // push vsize - 4-byte
1658. 4090 baton.setInt32(nused + 1, vsize, true);
1659. 4090 // push SQLITE-BLOB/TEXT - vsize-byte
1660. 4090 new Uint8Array(
1661. 4090 baton.buffer,
1662. 4090 baton.byteOffset + nused + 1 + 4,
1663. 4090 vsize
1664. 4090 ).set(new Uint8Array(val.buffer, val.byteOffset, vsize));
1665. 4090 break;
1666. 12 case SQLITE_DATATYPE_EXTERNALBUFFER:
1667. 12 vsize = val.byteLength;
1668. 12 assertOrThrow(
1669. 12 0 <= vsize && vsize <= SIZEOF_BLOB_MAX,
1670. 12 (
1671. 12 "sqlite-blob byte-length must be within inclusive-range"
1672. 12 + ` 0 to ${SIZEOF_BLOB_MAX}`
1673. 12 )
1674. 12 );
1675. 12 assertOrThrow(
1676. 12 bufi[0] < JSBATON_ARGC,
1677. 12 `cannot pass more than ${JSBATON_ARGC} arraybuffers`
1678. 12 );
1679. 12 // push externalbuffer - 4-byte
1680. 12 baton.setInt32(nused + 1, bufi[0], true);
1681. 12 // set buffer
1682. 12 cModule._jsbatonSetArraybuffer(baton.buffer, bufi[0], val);
1683. 12 // increment bufi
1684. 12 bufi[0] += 1;
1685. 12 // add buffer to reference_list to prevent gc during db_call.
1686. 12 referenceList.push(val);
1687. 12 break;
1688. 561 case SQLITE_DATATYPE_FLOAT:
1689. 561 // push SQLITE-REAL - 8-byte
1690. 561 baton.setFloat64(nused + 1, val, true);
1691. 561 break;
1692. 42 case SQLITE_DATATYPE_INTEGER:
1693. 42 assertInt64(val);
1694. 42 // push SQLITE-INTEGER - 8-byte
1695. 42 baton.setBigInt64(nused + 1, val, true);
1696. 42 break;
1697. 5142 }
1698. 5142 return baton;
1699. 5142}
1700. 1
1701. 1723function jsonParseArraybuffer(buf) {
1702. 1723
1703. 1723// This function will JSON.parse arraybuffer <buf>.
1704. 1723
1705. 1723 return JSON.parse(
1706. 1723 (
1707. 1723 IS_BROWSER
1708. 1 ? new TextDecoder().decode(buf)
1709. 1722 : buf
1710. 1723 )
1711. 1 || "null"
1712. 1723 );
1713. 1723}
1714. 1
1715. 1function libPlatformArchExt() {
1716. 1 let libArch = process.arch;
1717. 1 let libExt = process.platform;
1718. 1 let libPlatform = process.platform;
1719. 1 libExt = libExt.replace("darwin", "dylib");
1720. 1 libExt = libExt.replace("win32", "dll");
1721. 1 libExt = libExt.replace(libPlatform, "so");
1722. 1 return `${libPlatform}_${libArch}.${libExt}`;
1723. 1}
1724. 1
1725. 1function listOrEmptyList(list) {
1726. 1
1727. 1// This function will return <list> or empty-list if falsy.
1728. 1
1729. 1 return list || [];
1730. 1}
1731. 1
1732. 8async function moduleFsInit() {
1733. 8
1734. 8// This function will import nodejs builtin-modules if they have not yet been
1735. 8// imported.
1736. 8
1737. 8// State 3 - Modules already imported.
1738. 8
1739. 6 if (moduleFs !== undefined) {
1740. 6 return;
1741. 6 }
1742. 2
1743. 2// State 2 - Wait while modules are importing.
1744. 2
1745. 2 if (moduleFsInitResolveList !== undefined) {
1746. 1 return new Promise(function (resolve) {
1747. 1 moduleFsInitResolveList.push(resolve);
1748. 1 });
1749. 1 }
1750. 1
1751. 1// State 1 - Start importing modules.
1752. 1
1753. 1 moduleFsInitResolveList = [];
1754. 1 [
1755. 1 moduleChildProcess,
1756. 1 moduleCrypto,
1757. 1 moduleFs,
1758. 1 modulePath,
1759. 1 moduleUrl
1760. 1 ] = await Promise.all([
1761. 1 import("child_process"),
1762. 1 import("crypto"),
1763. 1 import("fs"),
1764. 1 import("path"),
1765. 1 import("url")
1766. 1 ]);
1767. 1 while (moduleFsInitResolveList.length > 0) {
1768. 1 moduleFsInitResolveList.shift()();
1769. 1 }
1770. 1 SQLMATH_NODE = `_sqlmath.napi6_${process.platform}_${process.arch}.node`;
1771. 1 SQLMATH_EXE = (
1772. 1 `_sqlmath.shell_${process.platform}_${process.arch}`
1773. 1 + process.platform.replace(
1774. 1 "win32",
1775. 1 ".exe"
1776. 1 ).replace(
1777. 1 process.platform,
1778. 1 ""
1779. 1 )
1780. 1 );
1781. 1}
1782. 1
1783. 263function noop(val) {
1784. 263
1785. 263// This function will do nothing except return <val>.
1786. 263
1787. 263 return val;
1788. 263}
1789. 1
1790. 42774function objectDeepCopyWithKeysSorted(obj) {
1791. 42774
1792. 42774// This function will recursively deep-copy <obj> with keys sorted.
1793. 42774
1794. 42774 let sorted;
1795. 28326 if (typeof obj !== "object" || !obj) {
1796. 28326 return obj;
1797. 28326 }
1798. 14448
1799. 14448// Recursively deep-copy list with child-keys sorted.
1800. 14448
1801. 14448 if (Array.isArray(obj)) {
1802. 2202 return obj.map(objectDeepCopyWithKeysSorted);
1803. 12246 }
1804. 12246
1805. 12246// Recursively deep-copy obj with keys sorted.
1806. 12246
1807. 12246 sorted = Object.create(null);
1808. 13714 Object.keys(obj).sort().forEach(function (key) {
1809. 13714 sorted[key] = objectDeepCopyWithKeysSorted(obj[key]);
1810. 13714 });
1811. 12246 return sorted;
1812. 12246}
1813. 1
1814. 3async function sqlmathInit() {
1815. 3
1816. 3// This function will init sqlmath.
1817. 3
1818. 3 let moduleModule;
1819. 3 dbFinalizationRegistry = (
1820. 3 dbFinalizationRegistry
1821. 18 ) || new FinalizationRegistry(function ({afterFinalization, ptr}) {
1822. 18
1823. 18// This function will auto-close any open sqlite3-db-pointer,
1824. 18// after its js-wrapper has been garbage-collected.
1825. 18
1826. 18 dbCallAsync(
1827. 18 jsbatonCreate("_dbClose"),
1828. 18 [
1829. 18 ptr[0]
1830. 18 ]
1831. 18 );
1832. 1 if (afterFinalization) {
1833. 1 afterFinalization();
1834. 1 }
1835. 18 });
1836. 3
1837. 3// Feature-detect nodejs.
1838. 3
1839. 3 if (
1840. 3 typeof process !== "object"
1841. 3 || typeof process?.versions?.node !== "string"
1842. 3 || cModule
1843. 1 ) {
1844. 1 return;
1845. 2 }
1846. 2
1847. 2// Init moduleFs.
1848. 2
1849. 2 await moduleFsInit();
1850. 2 moduleFsInit(); // coverage-hack
1851. 2 moduleChildProcessSpawn = moduleChildProcess.spawn;
1852. 2
1853. 2// Init moduleFs.
1854. 2
1855. 2 await moduleFsInit();
1856. 2 moduleFsInit(); // coverage-hack
1857. 2 moduleChildProcessSpawn = moduleChildProcess.spawn;
1858. 2 cModulePath = moduleUrl.fileURLToPath(import.meta.url).replace(
1859. 2 (/\bsqlmath\.mjs$/),
1860. 2 SQLMATH_NODE
1861. 2 );
1862. 2
1863. 2// Import napi c-addon.
1864. 2
1865. 2 if (!npm_config_mode_setup) {
1866. 2 moduleModule = await import("module");
1867. 2 if (!cModule) {
1868. 2 cModule = moduleModule.createRequire(cModulePath);
1869. 2 cModule = cModule(cModulePath);
1870. 2 }
1871. 2 }
1872. 2 if (npm_config_mode_test) {
1873. 2
1874. 2// Mock consoleError.
1875. 2
1876. 2 consoleError = noop;
1877. 2
1878. 2// Mock moduleChildProcessSpawn.
1879. 2
1880. 15 moduleChildProcessSpawn = function () {
1881. 15 let child = {
1882. 15 end: noop,
1883. 17 on: function (onType, resolve) {
1884. 17 switch (onType) {
1885. 2 case "data":
1886. 2 resolve(Buffer.alloc(0));
1887. 2 return;
1888. 15 default:
1889. 15 resolve(0);
1890. 17 }
1891. 17 },
1892. 15 setEncoding: noop,
1893. 15 write: noop
1894. 15 };
1895. 15 child.stderr = child;
1896. 15 child.stdin = child;
1897. 15 child.stdout = child;
1898. 15 return child;
1899. 15 };
1900. 2 }
1901. 3}
1902. 1
1903. 1function sqlmathWebworkerInit({
1904. 1 db,
1905. 1 modeTest
1906. 1}) {
1907. 1
1908. 1// This function will init sqlmath web-worker.
1909. 1
1910. 1// Feature-detect browser.
1911. 1
1912. 1 let Worker = globalThis.Worker;
1913. 1 IS_BROWSER = true;
1914. 1 if (modeTest) {
1915. 1 Worker = function () {
1916. 1 return;
1917. 1 };
1918. 1 }
1919. 1 sqlWorker = new Worker("sqlmath_wasm.js");
1920. 2 sqlWorker.onmessage = function ({
1921. 2 data
1922. 2 }) {
1923. 2 sqlMessageDict[data.id](data);
1924. 2 };
1925. 1 if (modeTest) {
1926. 2 sqlWorker.postMessage = function (data) {
1927. 2 setTimeout(function () {
1928. 2 sqlWorker.onmessage({data});
1929. 2 });
1930. 2 };
1931. 1 // test dbCallAsync handling-behavior
1932. 1 dbCallAsync(
1933. 1 jsbatonCreate("testTimeElapsed"),
1934. 1 [
1935. 1 true
1936. 1 ]
1937. 1 );
1938. 1 // test dbFileLoadAsync handling-behavior
1939. 1 dbFileLoadAsync({db, filename: "aa", modeTest});
1940. 1 // test jsonParseArraybuffer handling-behavior
1941. 1 jsonParseArraybuffer(new TextEncoder().encode("0"));
1942. 1 // revert IS_BROWSER
1943. 1 IS_BROWSER = undefined;
1944. 1 }
1945. 1}
1946. 1
1947. 1async function uvthreadpoolsizeGet() {
1948. 1
1949. 1// This function will guess how many logical-processors on current-host.
1950. 1
1951. 1 let uvthreadpoolsize;
1952. 1 await new Promise(function (resolve) {
1953. 1 let child;
1954. 1 child = moduleChildProcess.spawn(
1955. 1 "sh",
1956. 1 ["-c", (`
1957. 1 case "$(uname)" in
1958. 1 Darwin)
1959. 1 sysctl -n hw.logicalcpu
1960. 1 ;;
1961. 1 Linux)
1962. 1 nproc
1963. 1 ;;
1964. 1 *NT*)
1965. 1 wmic cpu get NumberOfLogicalProcessors | \
1966. 1 grep -v "NumberOfLogicalProcessors"
1967. 1 ;;
1968. 1 esac
1969. 1 `)],
1970. 1 {stdio: ["ignore", "pipe", "ignore"]}
1971. 1 );
1972. 1 child.stdout.setEncoding("utf8");
1973. 1 child.stdout.on("data", function (chunk) {
1974. 1 uvthreadpoolsize = Number(chunk);
1975. 1 });
1976. 1 child.on("exit", resolve);
1977. 1 });
1978. 1 if (npm_config_mode_test) {
1979. 1 uvthreadpoolsize = undefined;
1980. 1 }
1981. 1 return uvthreadpoolsize || 4;
1982. 1}
1983. 1
1984. 1function waitAsync(timeout) {
1985. 1
1986. 1// This function will wait <timeout> ms.
1987. 1
1988. 1 return new Promise(function (resolve) {
1989. 1 let ms = Number(timeout);
1990. 1 if (!Number.isFinite(ms)) {
1991. 1 ms = 0;
1992. 1 }
1993. 1 setTimeout(resolve, ms * !npm_config_mode_test);
1994. 1 });
1995. 1}
1996. 1
1997. 1sqlmathInit(); // coverage-hack
1998. 1await sqlmathInit();
1999. 1sqlmathInit(); // coverage-hack
2000. 1
2001. 1export {
2002. 1 DB_EXEC_PROFILE_DICT,
2003. 1 DB_STATE,
2004. 1 LGBM_DTYPE_FLOAT32,
2005. 1 LGBM_DTYPE_FLOAT64,
2006. 1 LGBM_DTYPE_INT32,
2007. 1 LGBM_DTYPE_INT64,
2008. 1 LGBM_FEATURE_IMPORTANCE_GAIN,
2009. 1 LGBM_FEATURE_IMPORTANCE_SPLIT,
2010. 1 LGBM_MATRIX_TYPE_CSC,
2011. 1 LGBM_MATRIX_TYPE_CSR,
2012. 1 LGBM_PREDICT_CONTRIB,
2013. 1 LGBM_PREDICT_LEAF_INDEX,
2014. 1 LGBM_PREDICT_NORMAL,
2015. 1 LGBM_PREDICT_RAW_SCORE,
2016. 1 SQLITE_OPEN_AUTOPROXY,
2017. 1 SQLITE_OPEN_CREATE,
2018. 1 SQLITE_OPEN_DELETEONCLOSE,
2019. 1 SQLITE_OPEN_EXCLUSIVE,
2020. 1 SQLITE_OPEN_FULLMUTEX,
2021. 1 SQLITE_OPEN_MAIN_DB,
2022. 1 SQLITE_OPEN_MAIN_JOURNAL,
2023. 1 SQLITE_OPEN_MEMORY,
2024. 1 SQLITE_OPEN_NOFOLLOW,
2025. 1 SQLITE_OPEN_NOMUTEX,
2026. 1 SQLITE_OPEN_PRIVATECACHE,
2027. 1 SQLITE_OPEN_READONLY,
2028. 1 SQLITE_OPEN_READWRITE,
2029. 1 SQLITE_OPEN_SHAREDCACHE,
2030. 1 SQLITE_OPEN_SUBJOURNAL,
2031. 1 SQLITE_OPEN_SUPER_JOURNAL,
2032. 1 SQLITE_OPEN_TEMP_DB,
2033. 1 SQLITE_OPEN_TEMP_JOURNAL,
2034. 1 SQLITE_OPEN_TRANSIENT_DB,
2035. 1 SQLITE_OPEN_URI,
2036. 1 SQLITE_OPEN_WAL,
2037. 1 SQLMATH_EXE,
2038. 1 SQLMATH_NODE,
2039. 1 assertErrorThrownAsync,
2040. 1 assertInt64,
2041. 1 assertJsonEqual,
2042. 1 assertNumericalEqual,
2043. 1 assertOrThrow,
2044. 1 childProcessSpawn2,
2045. 1 ciBuildExt,
2046. 1 csvFromListofList,
2047. 1 csvToListofList,
2048. 1 dbCloseAsync,
2049. 1 dbExecAndReturnLastBlob,
2050. 1 dbExecAndReturnLastRow,
2051. 1 dbExecAndReturnLastTable,
2052. 1 dbExecAndReturnLastValue,
2053. 1 dbExecAsync,
2054. 1 dbExecProfile,
2055. 1 dbFileLoadAsync,
2056. 1 dbFileSaveAsync,
2057. 1 dbNoopAsync,
2058. 1 dbOpenAsync,
2059. 1 dbTableImportAsync,
2060. 1 debugInline,
2061. 1 fsCopyFileUnlessTest,
2062. 1 fsExistsUnlessTest,
2063. 1 fsReadFileUnlessTest,
2064. 1 fsWriteFileUnlessTest,
2065. 1 jsbatonGetInt64,
2066. 1 jsbatonGetString,
2067. 1 libPlatformArchExt,
2068. 1 listOrEmptyList,
2069. 1 noop,
2070. 1 objectDeepCopyWithKeysSorted,
2071. 1 sqlmathWebworkerInit,
2072. 1 uvthreadpoolsizeGet,
2073. 1 version,
2074. 1 waitAsync
2075. 1};
2076. 1