sqlite-executor 4.0.10 → 4.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,8 +5,9 @@ export const DEFAULT_STATEMENT_TIMEOUT: 30000;
5
5
  * 创建包含 SQL 上下文的超时错误。
6
6
  * 注意:sql 应已由调用方完成规范化,本函数不再内部调用 normalizeSQL。
7
7
  *
8
- * @param timeout - 超时时间(毫秒)
9
- * @param sql - 超时发生时正在执行的 SQL 语句(已规范化)
10
- * @returns 包含超时时间和 SQL 信息的 Error 实例
8
+ * @param timeout - 超时时间(毫秒)
9
+ * @param sql - 超时发生时正在执行的 SQL 语句(已规范化)
10
+ * @param startTime - 任务开始时的 Unix 毫秒时间戳或 performance.now() 值(可选)
11
+ * @returns 包含超时时间、时间信息和 SQL 的 Error 实例
11
12
  */
12
- export function createTimeoutError(timeout: number, sql: string): Error;
13
+ export function createTimeoutError(timeout: number, sql: string, startTime?: number): Error;
@@ -1,11 +1,112 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_node_child_process_27f17141__ from "node:child_process";
2
- import * as __WEBPACK_EXTERNAL_MODULE_node_events_0a6aefe7__ from "node:events";
3
2
  import * as __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__ from "node:fs";
4
3
  import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path";
5
4
  import * as __WEBPACK_EXTERNAL_MODULE_node_os_74b4b876__ from "node:os";
5
+ function _array_like_to_array(arr, len) {
6
+ if (null == len || len > arr.length) len = arr.length;
7
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
8
+ return arr2;
9
+ }
10
+ function _array_with_holes(arr) {
11
+ if (Array.isArray(arr)) return arr;
12
+ }
13
+ function _iterable_to_array_limit(arr, i) {
14
+ var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
15
+ if (null == _i) return;
16
+ var _arr = [];
17
+ var _n = true;
18
+ var _d = false;
19
+ var _s, _e;
20
+ try {
21
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
22
+ _arr.push(_s.value);
23
+ if (i && _arr.length === i) break;
24
+ }
25
+ } catch (err) {
26
+ _d = true;
27
+ _e = err;
28
+ } finally{
29
+ try {
30
+ if (!_n && null != _i["return"]) _i["return"]();
31
+ } finally{
32
+ if (_d) throw _e;
33
+ }
34
+ }
35
+ return _arr;
36
+ }
37
+ function _non_iterable_rest() {
38
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
39
+ }
40
+ function _sliced_to_array(arr, i) {
41
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
42
+ }
43
+ function _unsupported_iterable_to_array(o, minLen) {
44
+ if (!o) return;
45
+ if ("string" == typeof o) return _array_like_to_array(o, minLen);
46
+ var n = Object.prototype.toString.call(o).slice(8, -1);
47
+ if ("Object" === n && o.constructor) n = o.constructor.name;
48
+ if ("Map" === n || "Set" === n) return Array.from(n);
49
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
50
+ }
6
51
  var DEFAULT_STATEMENT_TIMEOUT = 30000;
7
- function createTimeoutError(timeout, sql) {
8
- return new Error("SQLite statement timed out after ".concat(timeout, "ms: ").concat(sql));
52
+ function createTimeoutError(timeout, sql, startTime) {
53
+ var startedAt = resolveStartTimestamp(startTime, timeout);
54
+ var deadline = startedAt + timeout;
55
+ var duration = formatDuration(timeout);
56
+ var rawDuration = duration === "".concat(timeout, "ms") ? "" : " (".concat(timeout, "ms)");
57
+ return new Error("SQLite statement timed out after ".concat(duration).concat(rawDuration, "; ") + "started at ".concat(formatTimestamp(startedAt), "; ") + "deadline at ".concat(formatTimestamp(deadline), "; SQL: ").concat(sql));
58
+ }
59
+ function resolveStartTimestamp(startTime, timeout) {
60
+ if (!Number.isFinite(startTime)) return Date.now() - timeout;
61
+ if (startTime >= 1000000000000) return startTime;
62
+ return Date.now() - Math.max(0, performance.now() - startTime);
63
+ }
64
+ function formatDuration(milliseconds) {
65
+ var remaining = milliseconds;
66
+ var parts = [];
67
+ var units = [
68
+ [
69
+ 86400000,
70
+ "d"
71
+ ],
72
+ [
73
+ 3600000,
74
+ "h"
75
+ ],
76
+ [
77
+ 60000,
78
+ "m"
79
+ ],
80
+ [
81
+ 1000,
82
+ "s"
83
+ ]
84
+ ];
85
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
86
+ try {
87
+ for(var _iterator = units[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
88
+ var _step_value = _sliced_to_array(_step.value, 2), unitMilliseconds = _step_value[0], suffix = _step_value[1];
89
+ var value = Math.floor(remaining / unitMilliseconds);
90
+ if (value > 0) {
91
+ parts.push("".concat(value).concat(suffix));
92
+ remaining -= value * unitMilliseconds;
93
+ }
94
+ }
95
+ } catch (err) {
96
+ _didIteratorError = true;
97
+ _iteratorError = err;
98
+ } finally{
99
+ try {
100
+ if (!_iteratorNormalCompletion && null != _iterator["return"]) _iterator["return"]();
101
+ } finally{
102
+ if (_didIteratorError) throw _iteratorError;
103
+ }
104
+ }
105
+ if (remaining > 0 || 0 === parts.length) parts.push("".concat(remaining, "ms"));
106
+ return parts.join(" ");
107
+ }
108
+ function formatTimestamp(timestamp) {
109
+ return new Date(timestamp).toISOString().replace("T", " ").replace("Z", " UTC");
9
110
  }
10
111
  function toError(value) {
11
112
  return value instanceof Error ? value : new Error(String(value));
@@ -940,7 +1041,7 @@ var process_ProcessManager = /*#__PURE__*/ function() {
940
1041
  value: function() {
941
1042
  var _this = this;
942
1043
  return _async_to_generator(function() {
943
- var proc, timer, _proc_stdin;
1044
+ var proc;
944
1045
  return _ts_generator(this, function(_state) {
945
1046
  switch(_state.label){
946
1047
  case 0:
@@ -948,6 +1049,9 @@ var process_ProcessManager = /*#__PURE__*/ function() {
948
1049
  if (!proc) return [
949
1050
  2
950
1051
  ];
1052
+ if (null !== proc.exitCode || null !== proc.signalCode) return [
1053
+ 2
1054
+ ];
951
1055
  if (!(process_class_private_field_get(_this, _draining) || process_class_private_field_get(_this, _writeBuffer).length > 0)) return [
952
1056
  3,
953
1057
  2
@@ -966,40 +1070,43 @@ var process_ProcessManager = /*#__PURE__*/ function() {
966
1070
  _state.sent();
967
1071
  _state.label = 2;
968
1072
  case 2:
969
- timer = setTimeout(function() {
970
- proc.kill();
971
- }, GRACEFUL_SHUTDOWN_TIMEOUT).unref();
972
- _state.label = 3;
973
- case 3:
974
- _state.trys.push([
975
- 3,
976
- 5,
977
- 6,
978
- 7
979
- ]);
980
- null == (_proc_stdin = proc.stdin) || _proc_stdin.write(".quit\n");
981
- return [
982
- 4,
983
- (0, __WEBPACK_EXTERNAL_MODULE_node_events_0a6aefe7__.once)(proc, "close")
1073
+ if (null !== proc.exitCode || null !== proc.signalCode) return [
1074
+ 2
984
1075
  ];
985
- case 4:
986
- _state.sent();
987
1076
  return [
988
- 3,
989
- 7
1077
+ 4,
1078
+ new Promise(function(resolve) {
1079
+ var settled = false;
1080
+ var finish = function() {
1081
+ if (settled) return;
1082
+ settled = true;
1083
+ clearTimeout(timer);
1084
+ proc.off("close", finish);
1085
+ proc.off("error", finish);
1086
+ resolve();
1087
+ };
1088
+ var forceClose = function() {
1089
+ try {
1090
+ proc.kill();
1091
+ } catch (e) {}
1092
+ finish();
1093
+ };
1094
+ var timer = setTimeout(forceClose, GRACEFUL_SHUTDOWN_TIMEOUT);
1095
+ proc.once("close", finish);
1096
+ proc.once("error", finish);
1097
+ if (null !== proc.exitCode || null !== proc.signalCode) return void finish();
1098
+ try {
1099
+ if (!proc.stdin) return void forceClose();
1100
+ proc.stdin.write(".quit\n", function(error) {
1101
+ if (error) forceClose();
1102
+ });
1103
+ } catch (e) {
1104
+ forceClose();
1105
+ }
1106
+ })
990
1107
  ];
991
- case 5:
1108
+ case 3:
992
1109
  _state.sent();
993
- return [
994
- 3,
995
- 7
996
- ];
997
- case 6:
998
- clearTimeout(timer);
999
- return [
1000
- 7
1001
- ];
1002
- case 7:
1003
1110
  return [
1004
1111
  2
1005
1112
  ];
@@ -1818,13 +1925,13 @@ function shrinkIfNeeded() {
1818
1925
  queue_class_private_field_set(this, _mask, INITIAL_CAPACITY - 1);
1819
1926
  }
1820
1927
  }
1821
- function _array_like_to_array(arr, len) {
1928
+ function inflightTracker_array_like_to_array(arr, len) {
1822
1929
  if (null == len || len > arr.length) len = arr.length;
1823
1930
  for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
1824
1931
  return arr2;
1825
1932
  }
1826
1933
  function _array_without_holes(arr) {
1827
- if (Array.isArray(arr)) return _array_like_to_array(arr);
1934
+ if (Array.isArray(arr)) return inflightTracker_array_like_to_array(arr);
1828
1935
  }
1829
1936
  function inflightTracker_check_private_redeclaration(obj, privateCollection) {
1830
1937
  if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
@@ -1901,15 +2008,15 @@ function _non_iterable_spread() {
1901
2008
  throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
1902
2009
  }
1903
2010
  function _to_consumable_array(arr) {
1904
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
2011
+ return _array_without_holes(arr) || _iterable_to_array(arr) || inflightTracker_unsupported_iterable_to_array(arr) || _non_iterable_spread();
1905
2012
  }
1906
- function _unsupported_iterable_to_array(o, minLen) {
2013
+ function inflightTracker_unsupported_iterable_to_array(o, minLen) {
1907
2014
  if (!o) return;
1908
- if ("string" == typeof o) return _array_like_to_array(o, minLen);
2015
+ if ("string" == typeof o) return inflightTracker_array_like_to_array(o, minLen);
1909
2016
  var n = Object.prototype.toString.call(o).slice(8, -1);
1910
2017
  if ("Object" === n && o.constructor) n = o.constructor.name;
1911
2018
  if ("Map" === n || "Set" === n) return Array.from(n);
1912
- if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
2019
+ if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return inflightTracker_array_like_to_array(o, minLen);
1913
2020
  }
1914
2021
  var _tasks = /*#__PURE__*/ new WeakMap(), inflightTracker_head = /*#__PURE__*/ new WeakMap();
1915
2022
  var inflightTracker_InflightTracker = /*#__PURE__*/ function() {
@@ -2210,7 +2317,7 @@ function prepareTaskTimeout(task, metrics) {
2210
2317
  if (task.settled) return null;
2211
2318
  task.timedout = true;
2212
2319
  null == metrics || metrics.incrementTasksTimeout();
2213
- return createTimeoutError(task.timeout, task.sql);
2320
+ return createTimeoutError(task.timeout, task.sql, task.startedAt);
2214
2321
  }
2215
2322
  function createSweeper(param) {
2216
2323
  var inflight = param.inflight, sweepIntervalMs = param.sweepIntervalMs, handleTaskTimeout = param.handleTaskTimeout;
@@ -2220,9 +2327,8 @@ function createSweeper(param) {
2220
2327
  sweepTimer = setTimeout(function() {
2221
2328
  sweepTimer = null;
2222
2329
  var now = performance.now();
2223
- inflight.forEach(function(task) {
2224
- if (now - task.startTime > task.timeout) handleTaskTimeout(task);
2225
- });
2330
+ var task = inflight.first;
2331
+ if (task && now - task.startTime > task.timeout) handleTaskTimeout(task);
2226
2332
  if (inflight.count > 0) schedule();
2227
2333
  }, sweepIntervalMs).unref();
2228
2334
  };
@@ -2328,7 +2434,10 @@ function advanceNextInflightStartTime(inflight) {
2328
2434
  var next = inflight.first;
2329
2435
  if (next && next.startTime > 0) {
2330
2436
  var now = performance.now();
2331
- if (now > next.startTime) next.startTime = now;
2437
+ if (now > next.startTime) {
2438
+ next.startTime = now;
2439
+ next.startedAt = Date.now();
2440
+ }
2332
2441
  }
2333
2442
  }
2334
2443
  function handleParsedValue(raw, inflight, param) {
@@ -2377,6 +2486,7 @@ function createPumpQueue(param) {
2377
2486
  }
2378
2487
  if (0 === batch.length) return;
2379
2488
  var now = performance.now();
2489
+ var startedAt = Date.now();
2380
2490
  var payload = buildBatchPayload(batch);
2381
2491
  var batchId = nextBatchId++;
2382
2492
  var useWalBatch = payload.startsWith("BEGIN;");
@@ -2385,6 +2495,7 @@ function createPumpQueue(param) {
2385
2495
  for(var _iterator = batch[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2386
2496
  var task1 = _step.value;
2387
2497
  task1.startTime = now;
2498
+ task1.startedAt = startedAt;
2388
2499
  task1.batchId = batchId;
2389
2500
  task1.walBatch = useWalBatch;
2390
2501
  }
@@ -2789,7 +2900,8 @@ var taskWorker_TaskWorker = /*#__PURE__*/ function() {
2789
2900
  consumerError: null,
2790
2901
  stderrText: "",
2791
2902
  settled: false,
2792
- startTime: 0
2903
+ startTime: 0,
2904
+ startedAt: 0
2793
2905
  };
2794
2906
  null == (_$_class_private_field_get = taskWorker_class_private_field_get(this, _metrics)) || _$_class_private_field_get.incrementTasksTotal(config.kind);
2795
2907
  taskWorker_class_private_field_get(this, _pendingQueue).enqueue(task);
@@ -4451,6 +4563,7 @@ function enqueueWriter(kind, sql, timeout, token, onRow, scopeId) {
4451
4563
  stderrText: "",
4452
4564
  settled: false,
4453
4565
  startTime: 0,
4566
+ startedAt: 0,
4454
4567
  rowParser: null,
4455
4568
  rows: "query" === kind ? [] : null
4456
4569
  };