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