jsql-neo 6.8.27 → 6.10.2

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.
Files changed (139) hide show
  1. package/README.md +9 -4
  2. package/bin/jsql-server-control.js +58 -14
  3. package/index.d.ts +3 -0
  4. package/index.js +1 -1
  5. package/lib/bigint_json.js +13 -1
  6. package/lib/btree.js +12 -3
  7. package/lib/cache.js +12 -1
  8. package/lib/database.js +272 -36
  9. package/lib/jsql_format.js +22 -9
  10. package/lib/migrate.js +184 -98
  11. package/lib/mod.js +48 -13
  12. package/lib/mongo_server.js +652 -246
  13. package/lib/mysql_compat.js +54 -11
  14. package/lib/mysql_server.js +248 -65
  15. package/lib/nedb_compat.js +7 -1
  16. package/lib/objectid.js +14 -6
  17. package/lib/pg_server.js +385 -9
  18. package/lib/plugin.js +34 -3
  19. package/lib/query.js +12 -6
  20. package/lib/redis_server.js +102 -42
  21. package/lib/sql.js +446 -48
  22. package/lib/table.js +111 -41
  23. package/lib/tui.js +28 -9
  24. package/lib/web_ui.js +33 -2
  25. package/package.json +4 -4
  26. package/test/alter-table.test.js +11 -5
  27. package/test/compat-v1.test.js +1 -1
  28. package/test/extra-coverage.test.js +6 -7
  29. package/test/issue-258-300.test.js +6 -0
  30. package/test/issue-312-320.test.js +11 -4
  31. package/test/issue-326.test.js +42 -0
  32. package/test/issue-327-418.test.js +1 -1
  33. package/test/issue-336.test.js +56 -0
  34. package/test/issue-353.test.js +34 -0
  35. package/test/issue-356.test.js +27 -0
  36. package/test/issue-357-365.test.js +1 -1
  37. package/test/issue-361.test.js +55 -0
  38. package/test/issue-370.test.js +43 -0
  39. package/test/issue-373.test.js +40 -0
  40. package/test/issue-374.test.js +62 -0
  41. package/test/issue-378.test.js +39 -0
  42. package/test/issue-379.test.js +39 -0
  43. package/test/issue-38.test.js +2 -2
  44. package/test/issue-380.test.js +27 -0
  45. package/test/issue-383-402.test.js +4 -3
  46. package/test/issue-384.test.js +41 -0
  47. package/test/issue-392.test.js +25 -0
  48. package/test/issue-397.test.js +44 -0
  49. package/test/issue-403.test.js +47 -0
  50. package/test/issue-404.test.js +58 -0
  51. package/test/issue-407.test.js +40 -0
  52. package/test/issue-408.test.js +92 -0
  53. package/test/issue-409.test.js +32 -0
  54. package/test/issue-410.test.js +36 -0
  55. package/test/issue-411.test.js +38 -0
  56. package/test/issue-412.test.js +50 -0
  57. package/test/issue-414.test.js +46 -0
  58. package/test/issue-417.test.js +56 -0
  59. package/test/issue-420.test.js +87 -0
  60. package/test/issue-421.test.js +30 -0
  61. package/test/issue-422.test.js +33 -0
  62. package/test/issue-424.test.js +44 -0
  63. package/test/issue-425.test.js +22 -0
  64. package/test/issue-426.test.js +22 -0
  65. package/test/issue-427.test.js +36 -0
  66. package/test/issue-429.test.js +38 -0
  67. package/test/issue-431.test.js +41 -0
  68. package/test/issue-432.test.js +37 -0
  69. package/test/issue-436.test.js +47 -0
  70. package/test/issue-445-449.test.js +14 -7
  71. package/test/issue-455-459.test.js +6 -3
  72. package/test/issue-474.test.js +59 -0
  73. package/test/issue-475.test.js +38 -0
  74. package/test/issue-476.test.js +50 -0
  75. package/test/issue-477.test.js +54 -0
  76. package/test/issue-478.test.js +52 -0
  77. package/test/issue-479.test.js +44 -0
  78. package/test/issue-480.test.js +42 -0
  79. package/test/issue-481.test.js +51 -0
  80. package/test/issue-482.test.js +36 -0
  81. package/test/issue-483.test.js +57 -0
  82. package/test/issue-485.test.js +47 -0
  83. package/test/issue-486.test.js +69 -0
  84. package/test/issue-487.test.js +54 -0
  85. package/test/issue-488.test.js +69 -0
  86. package/test/issue-490.test.js +20 -0
  87. package/test/issue-491.test.js +21 -0
  88. package/test/issue-492.test.js +19 -0
  89. package/test/issue-493.test.js +30 -0
  90. package/test/issue-494.test.js +82 -0
  91. package/test/issue-495.test.js +86 -0
  92. package/test/issue-496.test.js +98 -0
  93. package/test/issue-497.test.js +29 -0
  94. package/test/issue-498.test.js +32 -0
  95. package/test/issue-499.test.js +41 -0
  96. package/test/issue-500.test.js +39 -0
  97. package/test/issue-501-506.test.js +29 -0
  98. package/test/issue-502.test.js +18 -0
  99. package/test/issue-503.test.js +43 -0
  100. package/test/issue-504.test.js +83 -0
  101. package/test/issue-505.test.js +29 -0
  102. package/test/issue-529.test.js +39 -0
  103. package/test/issue-530.test.js +69 -0
  104. package/test/issue-536.test.js +46 -0
  105. package/test/issue-537.test.js +52 -0
  106. package/test/issue-539.test.js +65 -0
  107. package/test/issue-540.test.js +31 -0
  108. package/test/issue-545.test.js +38 -0
  109. package/test/issue-546.test.js +68 -0
  110. package/test/issue-547.test.js +27 -0
  111. package/test/issue-548.test.js +68 -0
  112. package/test/issue-549.test.js +26 -0
  113. package/test/issue-550.test.js +52 -0
  114. package/test/issue-551.test.js +34 -0
  115. package/test/issue-552.test.js +30 -0
  116. package/test/issue-553.test.js +29 -0
  117. package/test/issue-554.test.js +29 -0
  118. package/test/issue-555.test.js +22 -0
  119. package/test/issue-556.test.js +25 -0
  120. package/test/issue-565.test.js +32 -0
  121. package/test/issue-566.test.js +30 -0
  122. package/test/issue-567.test.js +56 -0
  123. package/test/issue-568.test.js +46 -0
  124. package/test/issue-569.test.js +25 -0
  125. package/test/issue-570.test.js +30 -0
  126. package/test/issue-571.test.js +32 -0
  127. package/test/issue-572.test.js +64 -0
  128. package/test/issue-573.test.js +71 -0
  129. package/test/issue-574.test.js +63 -0
  130. package/test/issue-581.test.js +46 -0
  131. package/test/issue-59.test.js +17 -4
  132. package/test/issue-69-70.test.js +1 -1
  133. package/test/issue-mysql-harness.js +260 -0
  134. package/test/issue-pg5-helper.js +135 -0
  135. package/test/mongo-arrays.test.js +63 -0
  136. package/test/mongo-objectid.test.js +2 -2
  137. package/test/mongo-persistence.test.js +3 -3
  138. package/test/mongo-update-dates.test.js +128 -0
  139. package/test/wal-recovery.test.js +17 -9
@@ -46,6 +46,17 @@ function parseRedisInt(raw) {
46
46
  return n;
47
47
  }
48
48
 
49
+ // #420:INCR/DECR 系列是 64 位整数运算,Number 超过 2^53 会丢精度,必须用 BigInt
50
+ const INT64_MIN = -(1n << 63n);
51
+ const INT64_MAX = (1n << 63n) - 1n;
52
+ const SAFE_INT_MIN = BigInt(Number.MIN_SAFE_INTEGER);
53
+ const SAFE_INT_MAX = BigInt(Number.MAX_SAFE_INTEGER);
54
+ function parseRedisBigInt(raw) {
55
+ const s = String(raw).trim();
56
+ if (!/^-?\d+$/.test(s)) throw new Error('ERR value is not an integer or out of range');
57
+ return BigInt(s);
58
+ }
59
+
49
60
  // Redis glob:* / ? / [abc] / [!abc] / [a-c];其余正则元字符按字面转义
50
61
  function globToRegex(pattern) {
51
62
  const p = String(pattern);
@@ -255,24 +266,31 @@ class RedisServer {
255
266
  }
256
267
 
257
268
  /* ---------- command execution ---------- */
258
- execute(cmd, args) {
269
+ execute(cmd, args, rawCmd) {
270
+ const name = String(cmd).toLowerCase(); // #572:arity 错误引用规范小写命令名
259
271
  const sig = TYPE_SIGNATURES[cmd];
260
- if (sig === undefined) throw new Error(`ERR unknown command '${cmd}'`);
272
+ // #573:unknown command 原样回显客户端令牌大小写,不用归一后的 cmd
273
+ if (sig === undefined) throw new Error(`ERR unknown command '${rawCmd || cmd}'`);
261
274
  // #172:负 arity 表示最少参数个数,-2/-3 等也必须校验
262
- if (sig >= 0 && args.length < sig) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
263
- if (sig < 0 && args.length < -sig) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
275
+ if (sig >= 0 && args.length < sig) throw new Error(`ERR wrong number of arguments for '${name}' command`);
276
+ if (sig < 0 && args.length < -sig) throw new Error(`ERR wrong number of arguments for '${name}' command`);
264
277
  switch (cmd) {
265
278
  case 'PING':
266
279
  // #402:PING 只接受 0/1 个参数,多传必须报错而不是静默取第一个
267
- if (args.length > 1) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
280
+ if (args.length > 1) throw new Error(`ERR wrong number of arguments for '${name}' command`);
268
281
  return args.length ? args[0] : 'PONG';
269
282
  case 'ECHO':
270
- if (args.length !== 1) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
283
+ if (args.length !== 1) throw new Error(`ERR wrong number of arguments for '${name}' command`);
271
284
  return args[0];
272
- case 'AUTH':
285
+ case 'AUTH': {
286
+ if (args.length > 2) throw new Error(`ERR wrong number of arguments for '${name}' command`);
273
287
  if (!this.password) throw new Error('ERR Client sent AUTH, but no password is set');
274
- if (args[0] !== this.password) throw new Error('ERR invalid password');
288
+ // #504:同时支持 AUTH <password> 与 AUTH <user> <pass>。
289
+ // 本实现无 ACL 用户表,两参形式忽略用户名按密码校验(等价 default 用户)。
290
+ const pass = args.length === 2 ? args[1] : args[0];
291
+ if (pass !== this.password) throw new Error('ERR invalid password');
275
292
  return 'OK';
293
+ }
276
294
  case 'SELECT': {
277
295
  if (!/^\d+$/.test(args[0])) throw new Error(`ERR invalid DB index`);
278
296
  const idx = parseInt(args[0], 10);
@@ -284,17 +302,19 @@ class RedisServer {
284
302
  case 'SET': {
285
303
  const key = args[0];
286
304
  const val = args[1];
287
- // Parse optional arguments: EX seconds | PX milliseconds | NX | XX
305
+ // Parse optional arguments: EX seconds | PX milliseconds | NX | XX | KEEPTTL
288
306
  let ttl = null;
289
- let nx = false, xx = false;
307
+ let nx = false, xx = false, keepTtl = false;
290
308
  const seen = new Set();
291
309
  for (let i = 2; i < args.length; i++) {
292
310
  const opt = String(args[i]).toUpperCase();
293
- if ((opt === 'EX' || opt === 'PX' || opt === 'NX' || opt === 'XX') && seen.has(opt)) {
311
+ if ((opt === 'EX' || opt === 'PX' || opt === 'NX' || opt === 'XX' || opt === 'KEEPTTL') && seen.has(opt)) {
294
312
  throw new Error('ERR syntax error');
295
313
  }
296
- // #286:EX 与 PX 互斥
297
- if ((opt === 'EX' && seen.has('PX')) || (opt === 'PX' && seen.has('EX'))) {
314
+ // #286:EX 与 PX 互斥;#494:KEEPTTL 与 EX/PX 互斥
315
+ if ((opt === 'EX' && (seen.has('PX') || seen.has('KEEPTTL'))) ||
316
+ (opt === 'PX' && (seen.has('EX') || seen.has('KEEPTTL'))) ||
317
+ (opt === 'KEEPTTL' && (seen.has('EX') || seen.has('PX')))) {
298
318
  throw new Error('ERR syntax error');
299
319
  }
300
320
  seen.add(opt);
@@ -314,6 +334,8 @@ class RedisServer {
314
334
  nx = true;
315
335
  } else if (opt === 'XX') {
316
336
  xx = true;
337
+ } else if (opt === 'KEEPTTL') {
338
+ keepTtl = true;
317
339
  } else {
318
340
  throw new Error('ERR syntax error');
319
341
  }
@@ -322,7 +344,7 @@ class RedisServer {
322
344
  const prev = this._get(key);
323
345
  if (nx && prev) return null;
324
346
  if (xx && !prev) return null;
325
- this.db.set(key, { type: 'string', val, ttl });
347
+ this.db.set(key, { type: 'string', val, ttl: keepTtl ? (prev ? prev.ttl : null) : ttl });
326
348
  this._scheduleSnapshot();
327
349
  return 'OK';
328
350
  }
@@ -358,21 +380,23 @@ class RedisServer {
358
380
  return Buffer.byteLength(v.val, 'utf8');
359
381
  }
360
382
  case 'INCR': case 'DECR': case 'INCRBY': case 'DECRBY': {
361
- const by = cmd === 'INCRBY' || cmd === 'DECRBY' ? parseRedisInt(args[1]) : 1;
362
- if (isNaN(by)) throw new Error('ERR value is not an integer or out of range');
363
- const dir = cmd === 'DECR' || cmd === 'DECRBY' ? -1 : 1;
383
+ // #420:全程 BigInt,避免 2^53 以上精度丢失;越界按 Redis 报 overflow
384
+ const by = cmd === 'INCRBY' || cmd === 'DECRBY' ? parseRedisBigInt(args[1]) : 1n;
385
+ const dir = cmd === 'DECR' || cmd === 'DECRBY' ? -1n : 1n;
364
386
  const v = this._get(args[0]);
365
387
  let n;
366
388
  // #458:非字符串键(hash/list/set/zset)必须回 WRONGTYPE;
367
389
  // `ERR value is not an integer` 仅用于键存在但值不是整数字符串。
368
390
  if (v && v.type !== 'string') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
369
- if (!v) { n = 0; }
370
- else if (/^-?\d+$/.test(v.val)) { n = parseInt(v.val, 10); }
391
+ if (!v) { n = 0n; }
392
+ else if (/^-?\d+$/.test(v.val)) { n = BigInt(v.val); }
371
393
  else throw new Error('ERR value is not an integer or out of range');
372
394
  n += dir * by;
395
+ if (n > INT64_MAX || n < INT64_MIN) throw new Error('ERR increment or decrement would overflow');
373
396
  this.db.set(args[0], { type: 'string', val: String(n), ttl: v ? v.ttl : null });
374
397
  this._scheduleSnapshot();
375
- return n;
398
+ // 安全整数内保持既有 number 回复;更大的 INT64 值交给 _send 按 BigInt 原样输出
399
+ return n >= SAFE_INT_MIN && n <= SAFE_INT_MAX ? Number(n) : n;
376
400
  }
377
401
  case 'DEL': {
378
402
  // #176:过期但尚未清理的键不应计入删除数
@@ -409,7 +433,8 @@ class RedisServer {
409
433
  const v = this._get(args[0]);
410
434
  if (!v) return 0;
411
435
  const secs = parseRedisInt(args[1]);
412
- if (isNaN(secs)) throw new Error('ERR value is not an integer or out of range');
436
+ // #495:与 SET EX 对称,过期时间必须为正整数秒
437
+ if (!(secs > 0)) throw new Error("ERR invalid expire time in 'expire' command");
413
438
  // #313:Redis 7 的 EXPIRE key seconds [NX|XX|GT|LT] 条件选项。
414
439
  // 此前 args[2] 起的条件完全不解析:无条件覆盖已有 TTL 并恒返回 1,
415
440
  // 拿 XX 做「只刷新已有临时键」的守卫时会给永久键静默加上过期时间。
@@ -642,20 +667,49 @@ class RedisServer {
642
667
  const key = args[0];
643
668
  const v = this._get(key);
644
669
  if (v && v.type !== 'zset') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
645
- const rest = args.slice(1);
646
- if (rest.length % 2 !== 0) throw new Error('ERR syntax error');
670
+ let rest = args.slice(1);
671
+ // #496:选项位于 key 之后、score member 对之前,与 SET 的选项解析对称
672
+ let nx = false, xx = false, ch = false, incr = false;
673
+ const seen = new Set();
674
+ while (rest.length) {
675
+ const opt = String(rest[0]).toUpperCase();
676
+ if (opt !== 'NX' && opt !== 'XX' && opt !== 'CH' && opt !== 'INCR') break;
677
+ if (seen.has(opt)) throw new Error('ERR syntax error');
678
+ seen.add(opt);
679
+ if (opt === 'NX') nx = true;
680
+ else if (opt === 'XX') xx = true;
681
+ else if (opt === 'CH') ch = true;
682
+ else incr = true;
683
+ rest = rest.slice(1);
684
+ }
685
+ if (nx && xx) throw new Error('ERR syntax error');
686
+ if (incr && rest.length !== 2) throw new Error('ERR syntax error'); // INCR 只接受一个 score member
687
+ if (rest.length === 0 || rest.length % 2 !== 0) throw new Error('ERR syntax error');
647
688
  // #174:先完整校验所有 (score, member),任一非法则整条命令不产生变更
689
+ const pending = [];
690
+ for (let i = 0; i < rest.length; i += 2) pending.push([parseRedisFloat(rest[i]), rest[i + 1]]);
691
+ if (xx && !v) return 0; // XX 仅更新已存在的 zset,不新建键
648
692
  const zset = v ? Object.assign(Object.create(null), v.val) : Object.create(null);
649
- let added = 0;
650
- for (let i = 0; i < rest.length; i += 2) {
651
- const score = parseRedisFloat(rest[i]);
652
- const member = rest[i + 1];
653
- if (!Object.prototype.hasOwnProperty.call(zset, member)) added++;
654
- zset[member] = score;
693
+ let added = 0, changed = 0, newScore = null;
694
+ for (const [score, member] of pending) {
695
+ const exists = Object.prototype.hasOwnProperty.call(zset, member);
696
+ if (!exists) {
697
+ if (xx) continue;
698
+ zset[member] = score;
699
+ added++; changed++; newScore = score;
700
+ } else {
701
+ if (nx) continue;
702
+ const updated = incr ? zset[member] + score : score;
703
+ if (updated !== zset[member]) changed++;
704
+ zset[member] = updated; newScore = updated;
705
+ }
655
706
  }
656
- this.db.set(key, { type: 'zset', val: zset, ttl: v ? v.ttl : null });
657
- this._scheduleSnapshot();
658
- return added;
707
+ if (v || added > 0 || changed > 0) {
708
+ this.db.set(key, { type: 'zset', val: zset, ttl: v ? v.ttl : null });
709
+ this._scheduleSnapshot();
710
+ }
711
+ if (incr) return newScore === null ? null : formatScore(newScore);
712
+ return ch ? changed : added; // CH:新增或分数被改动的成员数;默认只计新增
659
713
  }
660
714
  case 'ZRANGE': {
661
715
  const v = this._get(args[0]);
@@ -735,7 +789,8 @@ class RedisServer {
735
789
  }
736
790
  case 'MSET': {
737
791
  const rest = args;
738
- if (rest.length % 2 !== 0) throw new Error('ERR wrong number of arguments');
792
+ // #574:与通用 arity 口径一致,必须带规范命令名
793
+ if (rest.length % 2 !== 0) throw new Error("ERR wrong number of arguments for 'mset' command");
739
794
  for (let i = 0; i < rest.length; i += 2) this.db.set(rest[i], { type: 'string', val: rest[i + 1], ttl: null });
740
795
  this._scheduleSnapshot();
741
796
  return 'OK';
@@ -783,7 +838,7 @@ class RedisServer {
783
838
  return lines.join('\r\n') + '\r\n';
784
839
  }
785
840
  case 'QUIT': return 'OK';
786
- default: throw new Error(`ERR unknown command '${cmd}'`);
841
+ default: throw new Error(`ERR unknown command '${rawCmd || cmd}'`);
787
842
  }
788
843
  }
789
844
 
@@ -821,7 +876,7 @@ class RedisServer {
821
876
  if (!msg) break;
822
877
  buf = buf.slice(msg.consumed);
823
878
  if (msg.ignore) continue;
824
- this._handle(socket, msg.cmd, msg.args);
879
+ this._handle(socket, msg.cmd, msg.args, msg.rawCmd);
825
880
  }
826
881
  };
827
882
 
@@ -860,8 +915,9 @@ class RedisServer {
860
915
  const parts = line.split(/\s+/);
861
916
  // 之前这里返回的是数组(cmd.map(...)),导致 switch(cmd) 永远匹配不上,
862
917
  // inline 输入一律被当成 unknown command。必须是字符串。
863
- const cmd = parts[0].toUpperCase();
864
- return { consumed: i + 2, cmd, args: parts.slice(1) };
918
+ const rawCmd = parts[0]; // #573:保留原始令牌大小写供 unknown command 回显
919
+ const cmd = rawCmd.toUpperCase();
920
+ return { consumed: i + 2, cmd, rawCmd, args: parts.slice(1) };
865
921
  }
866
922
 
867
923
  const i = crlf(0);
@@ -890,11 +946,12 @@ class RedisServer {
890
946
  parts.push(decodeBulk(buf.slice(start, start + len)));
891
947
  off = start + len + 2;
892
948
  }
893
- const cmd = String(parts[0] || '').toUpperCase();
894
- return { consumed: off, cmd, args: parts.slice(1) };
949
+ const rawCmd = String(parts[0] || ''); // #573:保留原始令牌大小写
950
+ const cmd = rawCmd.toUpperCase();
951
+ return { consumed: off, cmd, rawCmd, args: parts.slice(1) };
895
952
  }
896
953
 
897
- _handle(socket, cmd, args) {
954
+ _handle(socket, cmd, args, rawCmd) {
898
955
  const idx = this._selected.get(socket);
899
956
  const prevDb = this.db;
900
957
  const prevSelected = this.selected;
@@ -907,7 +964,7 @@ class RedisServer {
907
964
  }
908
965
  if (this.password && !this._authedSockets.get(socket)) {
909
966
  if (cmd === 'AUTH') {
910
- const ok = this.execute('AUTH', args);
967
+ const ok = this.execute('AUTH', args, rawCmd);
911
968
  if (ok === 'OK') {
912
969
  this._authedSockets.set(socket, true);
913
970
  socket.write('+OK\r\n');
@@ -930,7 +987,7 @@ class RedisServer {
930
987
  }
931
988
  return;
932
989
  }
933
- const r = this.execute(cmd, args);
990
+ const r = this.execute(cmd, args, rawCmd);
934
991
  if (cmd === 'SELECT' && r === 'OK') this._selected.set(socket, this.selected);
935
992
  // #177:只有命令级状态回复用 simple string;数据内容(ECHO/HGET/...)一律 bulk
936
993
  if (cmd === 'PING' && args.length === 0) {
@@ -962,6 +1019,9 @@ class RedisServer {
962
1019
  out = Buffer.from('-' + val.message + '\r\n', 'utf8');
963
1020
  } else if (val === null) {
964
1021
  out = Buffer.from('$-1\r\n');
1022
+ } else if (typeof val === 'bigint') {
1023
+ // #420:INT64 内但超出 JS 安全整数范围,仍按完整十进制整数回复
1024
+ out = Buffer.from(':' + val + '\r\n');
965
1025
  } else if (typeof val === 'number') {
966
1026
  // RESP integer reply 只能是十进制整数;小数必须走 bulk string(#67)。
967
1027
  out = Number.isInteger(val)