jsql-neo 6.8.8 → 6.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/index.js +1 -1
- package/lib/cache.js +19 -10
- package/lib/database.js +21 -0
- package/lib/mongo_server.js +46 -4
- package/lib/redis_server.js +32 -3
- package/lib/sql.js +18 -1
- package/package.json +3 -3
- package/test/issue-312-320.test.js +232 -0
- package/test/issue-314-321.test.js +162 -0
- package/test/release-metadata.test.js +5 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> **One engine to rule them all** — a Rust-powered embedded database that speaks your language:
|
|
4
4
|
> MySQL, PostgreSQL, MongoDB, Redis, SQL, TypeScript, **and the browser**.
|
|
5
5
|
|
|
6
|
-
> **v6.8.
|
|
6
|
+
> **v6.8.10** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|

|
package/index.js
CHANGED
package/lib/cache.js
CHANGED
|
@@ -365,16 +365,25 @@ class Cache {
|
|
|
365
365
|
} catch (_) { /* ignore */ }
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
377
|
-
|
|
368
|
+
// #319:两个后台定时器都必须 unref()。
|
|
369
|
+
// 嵌入式库被实例化后如果调用方忘记 close(),未 unref 的 setInterval 会一直
|
|
370
|
+
// 持有 Node 事件循环,CLI / 批处理 / 测试脚本跑完就「挂在那里」不退出,
|
|
371
|
+
// CI 里表现为超时被杀。与 lib/database.js 的 _flushTimer / _monitorTimer
|
|
372
|
+
// 保持同一套处理。
|
|
373
|
+
_startAutoSave() {
|
|
374
|
+
this._saveTimer = setInterval(() => { if (this._dirty) this.save(); }, this._saveInterval);
|
|
375
|
+
if (this._saveTimer && this._saveTimer.unref) this._saveTimer.unref();
|
|
376
|
+
}
|
|
377
|
+
_startTTLCleaner() {
|
|
378
|
+
this._ttlTimer = setInterval(() => {
|
|
379
|
+
for (let i = 0; i < this._dbs.length; i++) {
|
|
380
|
+
const exp = this._dbsExpires[i]; if (!exp) continue;
|
|
381
|
+
const now = Date.now();
|
|
382
|
+
for (const [key, ttl] of exp) { if (ttl <= now) { if (this._dbs[i]) this._dbs[i].delete(key); exp.delete(key); } }
|
|
383
|
+
}
|
|
384
|
+
}, 1000);
|
|
385
|
+
if (this._ttlTimer && this._ttlTimer.unref) this._ttlTimer.unref();
|
|
386
|
+
}
|
|
378
387
|
|
|
379
388
|
close() { if (this._persistPath && this._dirty) this.save(); if (this._saveTimer) clearInterval(this._saveTimer); if (this._ttlTimer) clearInterval(this._ttlTimer); }
|
|
380
389
|
_isExpired(key) {
|
package/lib/database.js
CHANGED
|
@@ -143,7 +143,24 @@ class Database {
|
|
|
143
143
|
this._autoSaveTimer = null;
|
|
144
144
|
this._loading = false;
|
|
145
145
|
this._transaction = null;
|
|
146
|
+
// #320:encryptKey 的字节长度必须落在 AES 密钥长度集合 {16, 24, 32} 内。
|
|
147
|
+
// 此前构造阶段不校验,写入侧用 padEnd(32).slice(0, 32) 兜底:
|
|
148
|
+
// · 1 字节密钥被空格补齐到 32 字节 → 有效熵只有 1 字节,属可爆破弱密钥
|
|
149
|
+
// · 48 字节密钥被静默截断到 32 字节 → 用户高估密钥熵值
|
|
150
|
+
// 两者都只在事后审计密钥时才发现。这里构造时直接抛错。
|
|
146
151
|
this._encryptKey = options.encryptKey || null;
|
|
152
|
+
if (this._encryptKey !== null && this._encryptKey !== undefined) {
|
|
153
|
+
const keyBytes = Buffer.byteLength(String(this._encryptKey), 'utf8');
|
|
154
|
+
if (keyBytes !== 16 && keyBytes !== 24 && keyBytes !== 32) {
|
|
155
|
+
const err = new Error(
|
|
156
|
+
`Invalid encryptKey length: ${keyBytes} bytes (expected 16, 24 or 32)`
|
|
157
|
+
);
|
|
158
|
+
err.code = 'ER_INVALID_KEY';
|
|
159
|
+
err.errno = 1820;
|
|
160
|
+
err.sqlState = 'HY000';
|
|
161
|
+
throw err;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
147
164
|
this._versioning = options.versioning || false;
|
|
148
165
|
this._versions = {}; // tableName -> [{ rows, timestamp }]
|
|
149
166
|
this._maxVersions = options.maxVersions || 50;
|
|
@@ -2731,6 +2748,10 @@ class Database {
|
|
|
2731
2748
|
this._autoSaveTimer = null;
|
|
2732
2749
|
if (this._dirty) this.save();
|
|
2733
2750
|
}, this._options.autoSaveInterval);
|
|
2751
|
+
// #321:legacy 文件模式的自动保存定时器也要 unref()。
|
|
2752
|
+
// 同文件的 _flushTimer / _monitorTimer 都已经 unref,唯独这个漏了,
|
|
2753
|
+
// 漏调 close() 时进程会被挂到下一次自动保存触发才退出。
|
|
2754
|
+
if (this._autoSaveTimer && this._autoSaveTimer.unref) this._autoSaveTimer.unref();
|
|
2734
2755
|
} else {
|
|
2735
2756
|
this.save();
|
|
2736
2757
|
}
|
package/lib/mongo_server.js
CHANGED
|
@@ -334,6 +334,25 @@ class MongoServer {
|
|
|
334
334
|
return defs;
|
|
335
335
|
}
|
|
336
336
|
|
|
337
|
+
/**
|
|
338
|
+
* 集合是否真实存在(#315)。
|
|
339
|
+
* 判据:engine 侧存在该表,或本进程已登记过该集合。
|
|
340
|
+
* 注意不能调用 _ensureCollection —— 那会把集合「创建」出来。
|
|
341
|
+
*/
|
|
342
|
+
async _collectionExists(name, db = null) {
|
|
343
|
+
const engine = await this._getEngine(db);
|
|
344
|
+
if (!engine) return false;
|
|
345
|
+
const key = (db || 'default') + '.' + name;
|
|
346
|
+
if (this._autoCollections && this._autoCollections.has(key)) return true;
|
|
347
|
+
if (this._dbCollections && this._dbCollections.has(db) && this._dbCollections.get(db).has(name)) return true;
|
|
348
|
+
if (typeof engine.hasTable === 'function') {
|
|
349
|
+
try { if (engine.hasTable(name)) return true; } catch (_) { /* ignore */ }
|
|
350
|
+
}
|
|
351
|
+
if (engine._tables && engine._tables[name]) return true;
|
|
352
|
+
if (engine._meta && engine._meta.tables && engine._meta.tables[name]) return true;
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
|
|
337
356
|
async _ensureCollection(name, db = null, sample = null) {
|
|
338
357
|
const engine = await this._getEngine(db);
|
|
339
358
|
const key = (db || 'default') + '.' + name;
|
|
@@ -751,7 +770,10 @@ class MongoServer {
|
|
|
751
770
|
const skip = typeof doc.skip === 'number' ? doc.skip : 0;
|
|
752
771
|
if (skip > 0) rows = rows.slice(skip);
|
|
753
772
|
const limit = typeof doc.limit === 'number' ? doc.limit : 0;
|
|
754
|
-
|
|
773
|
+
// #316:MongoDB 的 find 负 limit 表示「取 |limit| 条并单批返回」。
|
|
774
|
+
// 此前只判断 limit > 0,负值落到「不截断」分支,把整批文档全返回了。
|
|
775
|
+
const effLimit = limit < 0 ? -limit : limit;
|
|
776
|
+
if (effLimit > 0) rows = rows.slice(0, effLimit);
|
|
755
777
|
const projection = doc.projection || doc.fields;
|
|
756
778
|
if (projection) rows = rows.map(r => applyProjection(r, projection));
|
|
757
779
|
return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows } };
|
|
@@ -841,6 +863,12 @@ class MongoServer {
|
|
|
841
863
|
const coll = String(doc.listIndexes);
|
|
842
864
|
const dbName = this._dbOf(doc);
|
|
843
865
|
const key = dbName + '.' + coll;
|
|
866
|
+
// #315:对不存在的集合必须按协议报 nsNotFound(26),
|
|
867
|
+
// 此前兜底塞了一条虚构的 _id_ 索引并回 ok:1,
|
|
868
|
+
// 元数据巡检工具会把不存在的集合误判为「已有 _id_ 索引」。
|
|
869
|
+
if (!this._indexesByColl.has(key) && !(await this._collectionExists(coll, dbName))) {
|
|
870
|
+
return { ok: 0, errmsg: 'ns not found', code: 26 };
|
|
871
|
+
}
|
|
844
872
|
const list = this._indexesByColl.get(key) || [{ name: '_id_', columns: ['_id'], unique: true }];
|
|
845
873
|
const ns = `${dbName}.${coll}`;
|
|
846
874
|
const firstBatch = list.map(i => ({
|
|
@@ -965,8 +993,11 @@ class MongoServer {
|
|
|
965
993
|
for (const [k, v] of Object.entries(groupDef)) {
|
|
966
994
|
if (k === '_id' || v == null) continue;
|
|
967
995
|
if (typeof v === 'object') {
|
|
968
|
-
// $sum, $avg, $min, $max, $first, $last
|
|
969
|
-
|
|
996
|
+
// $sum, $avg, $min, $max, $first, $last, $push, $addToSet
|
|
997
|
+
// #312:$push / $addToSet 此前完全没实现 —— 累加器对象落到
|
|
998
|
+
// 取值链之外,doc[k] 从未被赋值,整个数组列静默缺失。
|
|
999
|
+
const acc = v.$sum ?? v.$avg ?? v.$min ?? v.$max ?? v.$first ?? v.$last
|
|
1000
|
+
?? v.$push ?? v.$addToSet;
|
|
970
1001
|
if (acc !== undefined) {
|
|
971
1002
|
const isNumeric = v.$sum !== undefined || v.$avg !== undefined;
|
|
972
1003
|
if (typeof acc === 'number') {
|
|
@@ -975,7 +1006,18 @@ class MongoServer {
|
|
|
975
1006
|
else if (v.$avg !== undefined) doc[k] = (doc[k] || 0) + acc;
|
|
976
1007
|
} else if (typeof acc === 'string' && acc.startsWith('$')) {
|
|
977
1008
|
const field = acc.slice(1);
|
|
978
|
-
if (v.$
|
|
1009
|
+
if (v.$push !== undefined) {
|
|
1010
|
+
// 数组累加器:按组内文档顺序收集该字段值(MongoDB 语义)
|
|
1011
|
+
doc[k] = grow.map(r => getPath(r, field));
|
|
1012
|
+
} else if (v.$addToSet !== undefined) {
|
|
1013
|
+
// 去重收集:按值深相等去重,保持首次出现顺序
|
|
1014
|
+
const seen = [];
|
|
1015
|
+
for (const r of grow) {
|
|
1016
|
+
const val = getPath(r, field);
|
|
1017
|
+
if (!seen.some(x => deepEqualMongo(x, val))) seen.push(val);
|
|
1018
|
+
}
|
|
1019
|
+
doc[k] = seen;
|
|
1020
|
+
} else if (v.$sum !== undefined) {
|
|
979
1021
|
const sum = grow.reduce((s, r) => s + (Number(getPath(r, field)) || 0), 0);
|
|
980
1022
|
doc[k] = (doc[k] || 0) + sum;
|
|
981
1023
|
} else if (v.$avg !== undefined) {
|
package/lib/redis_server.js
CHANGED
|
@@ -390,7 +390,33 @@ class RedisServer {
|
|
|
390
390
|
if (!v) return 0;
|
|
391
391
|
const secs = parseRedisInt(args[1]);
|
|
392
392
|
if (isNaN(secs)) throw new Error('ERR value is not an integer or out of range');
|
|
393
|
-
|
|
393
|
+
// #313:Redis 7 的 EXPIRE key seconds [NX|XX|GT|LT] 条件选项。
|
|
394
|
+
// 此前 args[2] 起的条件完全不解析:无条件覆盖已有 TTL 并恒返回 1,
|
|
395
|
+
// 拿 XX 做「只刷新已有临时键」的守卫时会给永久键静默加上过期时间。
|
|
396
|
+
// NX:仅当键当前**没有**过期时间时设置
|
|
397
|
+
// XX:仅当键当前**已有**过期时间时设置
|
|
398
|
+
// GT:仅当新 TTL 大于现有 TTL 时设置
|
|
399
|
+
// LT:仅当新 TTL 小于现有 TTL 时设置
|
|
400
|
+
// 条件不满足 → 返回 0 且不改动 TTL。
|
|
401
|
+
let cond = null;
|
|
402
|
+
for (let i = 2; i < args.length; i++) {
|
|
403
|
+
const opt = String(args[i]).toUpperCase();
|
|
404
|
+
if (opt === 'NX' || opt === 'XX' || opt === 'GT' || opt === 'LT') {
|
|
405
|
+
if (cond) throw new Error('ERR syntax error');
|
|
406
|
+
cond = opt;
|
|
407
|
+
} else {
|
|
408
|
+
// 未识别的选项必须报错,不能静默跳过
|
|
409
|
+
throw new Error('ERR syntax error');
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const now = Date.now();
|
|
413
|
+
const currentTtl = v.ttl ? v.ttl - now : null; // null = 无过期时间
|
|
414
|
+
const newTtl = secs * 1000;
|
|
415
|
+
if (cond === 'NX' && currentTtl !== null) return 0;
|
|
416
|
+
if (cond === 'XX' && currentTtl === null) return 0;
|
|
417
|
+
if (cond === 'GT' && currentTtl !== null && newTtl <= currentTtl) return 0;
|
|
418
|
+
if (cond === 'LT' && currentTtl !== null && newTtl >= currentTtl) return 0;
|
|
419
|
+
v.ttl = now + newTtl;
|
|
394
420
|
this._scheduleSnapshot();
|
|
395
421
|
return 1;
|
|
396
422
|
}
|
|
@@ -485,8 +511,11 @@ class RedisServer {
|
|
|
485
511
|
const hasCount = args.length >= 2;
|
|
486
512
|
let count = 1;
|
|
487
513
|
if (hasCount) {
|
|
488
|
-
count
|
|
489
|
-
|
|
514
|
+
// #314:count 必须是严格十进制非负整数。此前用裸 parseInt,
|
|
515
|
+
// '1.5' 被截成 1、'2abc' 被截成 2,尾随垃圾静默接受。
|
|
516
|
+
// 复用库内已有的 parseRedisInt(与 #175/#227 同口径)。
|
|
517
|
+
count = parseRedisInt(args[1]);
|
|
518
|
+
if (count < 0) throw new Error('ERR value is out of range, must be positive');
|
|
490
519
|
}
|
|
491
520
|
const v = this._get(args[0]);
|
|
492
521
|
if (!v) return hasCount ? [] : null;
|
package/lib/sql.js
CHANGED
|
@@ -5161,7 +5161,11 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
5161
5161
|
// · 需要填充时 padstr **循环使用并截断**到恰好 len(LPAD('hi',5,'ab') = 'abahi')
|
|
5162
5162
|
// · padstr 为空串且 len > 原长 → ''(MySQL 语义);旧 while 循环此时会死循环
|
|
5163
5163
|
case 'LPAD': {
|
|
5164
|
+
// #318:填充串为 NULL → 整体返回 NULL(MySQL 三值逻辑)。
|
|
5165
|
+
// 此前只守卫 args[0]/args[1],args[2] 为 NULL 时用字面量 ' ' 兜底,
|
|
5166
|
+
// 产出 ' 7' 这种错误字符串。缺省第三参(args.length < 3)才用空格填充。
|
|
5164
5167
|
if (args[0] == null || args[1] == null) return null;
|
|
5168
|
+
if (args.length > 2 && args[2] == null) return null;
|
|
5165
5169
|
const s = String(args[0]);
|
|
5166
5170
|
const cps = Array.from(s); // Issue #111:长度/截断都按 code point
|
|
5167
5171
|
const n = Math.trunc(Number(args[1]));
|
|
@@ -5176,7 +5180,9 @@ function applyScalarFunction(fnNode, row, ctx) {
|
|
|
5176
5180
|
return fill + s;
|
|
5177
5181
|
}
|
|
5178
5182
|
case 'RPAD': {
|
|
5183
|
+
// #318:同 LPAD,填充串为 NULL → 整体 NULL。
|
|
5179
5184
|
if (args[0] == null || args[1] == null) return null;
|
|
5185
|
+
if (args.length > 2 && args[2] == null) return null;
|
|
5180
5186
|
const s = String(args[0]);
|
|
5181
5187
|
const cps = Array.from(s); // Issue #111:长度/截断都按 code point
|
|
5182
5188
|
const n = Math.trunc(Number(args[1]));
|
|
@@ -6105,7 +6111,18 @@ function evaluateExpr(expr, row, ctx) {
|
|
|
6105
6111
|
if (p === null || p === undefined) return null;
|
|
6106
6112
|
// REGEXP / RLIKE 历史上就是大小写不敏感的;`~` 敏感、`~*` 不敏感
|
|
6107
6113
|
const binaryCmp = isBinaryNode(expr.operand) || isBinaryNode(expr.pattern);
|
|
6108
|
-
|
|
6114
|
+
// #317:非法正则模式串原来直接抛 V8 的裸 SyntaxError(无 errno/SQLSTATE),
|
|
6115
|
+
// 跨协议透传时客户端无法按 MySQL 错误码处理。归一为 ER_REGEXP_ERROR(1139)。
|
|
6116
|
+
let re;
|
|
6117
|
+
try {
|
|
6118
|
+
re = new RegExp(String(p), (expr.ci === false || binaryCmp) ? '' : 'i');
|
|
6119
|
+
} catch (e) {
|
|
6120
|
+
const err = new Error('Illegal argument to a regular expression');
|
|
6121
|
+
err.code = 'ER_REGEXP_ERROR';
|
|
6122
|
+
err.errno = 1139;
|
|
6123
|
+
err.sqlState = '42000';
|
|
6124
|
+
throw err;
|
|
6125
|
+
}
|
|
6109
6126
|
const r = re.test(String(v));
|
|
6110
6127
|
return expr.not ? !r : r;
|
|
6111
6128
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsql-neo",
|
|
3
|
-
"version": "6.8.
|
|
3
|
+
"version": "6.8.10",
|
|
4
4
|
"description": "JSQL-NEO — Rust-powered embedded database with WASM, REST API, B-Tree indexes, WAL, crash recovery",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -52,12 +52,12 @@
|
|
|
52
52
|
"build:native": "node scripts/build-native.js",
|
|
53
53
|
"check:release": "node scripts/check-release-metadata.js",
|
|
54
54
|
"release:prep": "node scripts/release.js",
|
|
55
|
-
"test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
55
|
+
"test": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/native.test.js && node test/json-functions.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/query-opt.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/dml-constraints.test.js && node test/star-nag.test.js && node test/plugin-xql.test.js && node test/bug-regression-2.test.js && node test/issue-34-35-36.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-73-76.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js",
|
|
56
56
|
"test:parser": "node test/sql-parser.test.js",
|
|
57
57
|
"test:btree": "node test/btree.test.js",
|
|
58
58
|
"test:native": "node test/native.test.js",
|
|
59
59
|
"test:regress": "node test/regress-5.1.0.js",
|
|
60
|
-
"test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js",
|
|
60
|
+
"test:all": "npm run check:release && node test/smoke.js && node test/result-raw.test.js && node test/native-install.test.js && node test/native-cli.test.js && node test/finalize-bigint.test.js && node test/sql-parser.test.js && node test/readme-audit.test.js && node test/native.test.js && node test/native-multi-instance.test.js && node test/wal-recovery.test.js && node test/mysql-compat-hijack.test.js && node test/wasm.test.js && node test/coverage.js && node test/regress-5.1.0.js && node test/join.test.js && node test/btree.test.js && node test/dml-constraints.test.js && node test/savepoint.test.js && node test/alter-table.test.js && node test/null-semantics.test.js && node test/identifier-keywords.test.js && node test/issue-34-35-36.test.js && node test/sql-semantics.test.js && node test/extra-coverage.test.js && node test/filter-compile.test.js && node test/recursive-cte.test.js && node test/index-ddl.test.js && node test/mongo-group.test.js && node test/multiprotocol.test.js && node test/issue-g4.test.js && node test/issue-38.test.js && node test/issue-39.test.js && node test/issue-40.test.js && node test/issue-44.test.js && node test/issue-45.test.js && node test/issue-46.test.js && node test/issue-47.test.js && node test/issue-48.test.js && node test/issue-50.test.js && node test/issue-51.test.js && node test/issue-52.test.js && node test/issue-53.test.js && node test/issue-54.test.js && node test/issue-55.test.js && node test/issue-56.test.js && node test/issue-57.test.js && node test/issue-58.test.js && node test/issue-59.test.js && node test/issue-60.test.js && node test/issue-63-68.test.js && node test/network-policy.test.js && node test/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js && node test/issue-86-99.test.js && node test/issue-101-103.test.js && node test/issue-105.test.js && node test/issue-118-136.test.js && node test/issue-127.test.js && node test/issue-138-156.test.js && node test/issue-157-161.test.js && node test/issue-162-179.test.js && node test/issue-181-186.test.js && node test/issue-187.test.js && node test/issue-188-237.test.js && node test/issue-238-241.test.js && node test/bigint-math.test.js && node test/release-metadata.test.js && node test/date-conversions.test.js && node test/mysql-utility-functions.test.js && node test/limit-pushdown.test.js && node test/columnar.test.js && node test/columnar-vector.test.js && node test/cast-rounding.test.js && node test/convert-subquery.test.js && node test/mongo-persistence.test.js && node test/mongo-objectid.test.js && node test/mongo-arrays.test.js && node test/issue-117.test.js && node test/issue-314-321.test.js && node test/issue-312-320.test.js",
|
|
61
61
|
"test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
|
|
62
62
|
"test:orms": "node examples/orms/run-all.js",
|
|
63
63
|
"test:dml": "node test/dml-constraints.test.js",
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/* ---- 回归:#312 / #313 / #315 / #316 / #320 ---- */
|
|
2
|
+
/*
|
|
3
|
+
* #312 Mongo `$group` 的 `$push` / `$addToSet` 累加器静默无效:累加器对象落到
|
|
4
|
+
* 取值链之外,输出文档缺整列。
|
|
5
|
+
* #313 Redis `EXPIRE` 的 NX/XX/GT/LT 条件选项被静默忽略,无条件覆盖已有 TTL
|
|
6
|
+
* 并恒返回 1。
|
|
7
|
+
* #315 Mongo `listIndexes` 对不存在的集合伪造一条 `_id_` 唯一索引并回 ok:1,
|
|
8
|
+
* 未按协议报 nsNotFound(26)。
|
|
9
|
+
* #316 Mongo `find` 负 limit 被忽略,不按绝对值取单批,而是返回全部文档。
|
|
10
|
+
* #320 `encryptKey` 长度不校验:1 字节密钥被空格补齐到 32、48 字节密钥被静默
|
|
11
|
+
* 截断到 32,与文档承诺的 16/24/32 字节不符。
|
|
12
|
+
*/
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const net = require('net');
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const os = require('os');
|
|
17
|
+
const ROOT = path.join(__dirname, '..');
|
|
18
|
+
const { Database } = require(path.join(ROOT, 'index.js'));
|
|
19
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib/redis_server.js'));
|
|
20
|
+
const { createMongoServer, bsonDocument, bsonReadDocument } = require(path.join(ROOT, 'lib/mongo_server.js'));
|
|
21
|
+
|
|
22
|
+
let pass = 0, fail = 0;
|
|
23
|
+
function ok(name, cond, got) {
|
|
24
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
25
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---------- Mongo 线协议客户端 ----------
|
|
29
|
+
function mongoOpMsg(doc) {
|
|
30
|
+
const p = bsonDocument(doc);
|
|
31
|
+
const h = Buffer.alloc(16);
|
|
32
|
+
h.writeInt32LE(16 + 4 + 1 + p.length, 0);
|
|
33
|
+
h.writeInt32LE(1, 4); h.writeInt32LE(0, 8); h.writeInt32LE(2013, 12);
|
|
34
|
+
return Buffer.concat([h, Buffer.alloc(4), Buffer.from([0]), p]);
|
|
35
|
+
}
|
|
36
|
+
function mongoCall(srv, doc) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const sock = net.connect(srv._server.address().port, '127.0.0.1', () => sock.write(mongoOpMsg(doc)));
|
|
39
|
+
const chunks = [];
|
|
40
|
+
sock.on('error', reject);
|
|
41
|
+
sock.on('data', (c) => {
|
|
42
|
+
chunks.push(c);
|
|
43
|
+
const buf = Buffer.concat(chunks);
|
|
44
|
+
if (buf.length >= 16 && buf.length >= buf.readInt32LE(0)) {
|
|
45
|
+
try { resolve(bsonReadDocument(buf.slice(21), 0).value); }
|
|
46
|
+
catch (e) { reject(e); }
|
|
47
|
+
sock.end();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---------- Redis RESP 客户端 ----------
|
|
54
|
+
function redisFrame(args) {
|
|
55
|
+
const parts = args.map((a) => { const b = Buffer.from(String(a)); return '$' + b.length + '\r\n' + b + '\r\n'; });
|
|
56
|
+
return Buffer.from('*' + args.length + '\r\n' + parts.join(''));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function main() {
|
|
60
|
+
// ================= #320 encryptKey 长度校验 =================
|
|
61
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-key-'));
|
|
62
|
+
const badKeys = ['x', '0123456789abcdef0123456789abcdef0123456789abcdef', 'short'];
|
|
63
|
+
for (const k of badKeys) {
|
|
64
|
+
let threw = null;
|
|
65
|
+
try {
|
|
66
|
+
const d = new Database(path.join(tmpDir, `db-${k.length}.json`), { encryptKey: k, autoSave: false });
|
|
67
|
+
d.close();
|
|
68
|
+
} catch (e) { threw = e; }
|
|
69
|
+
ok(`encryptKey 长度 ${Buffer.byteLength(k)} 字节被拒绝`, !!threw, threw && threw.message);
|
|
70
|
+
ok(`encryptKey 长度 ${Buffer.byteLength(k)} 字节报 ER_INVALID_KEY`,
|
|
71
|
+
threw && threw.code === 'ER_INVALID_KEY', threw && threw.code);
|
|
72
|
+
}
|
|
73
|
+
// 合法长度必须照常工作
|
|
74
|
+
for (const len of [16, 24, 32]) {
|
|
75
|
+
let db = null, threw = null;
|
|
76
|
+
try {
|
|
77
|
+
db = new Database(path.join(tmpDir, `good-${len}.json`), { encryptKey: 'k'.repeat(len), autoSave: false });
|
|
78
|
+
} catch (e) { threw = e; }
|
|
79
|
+
ok(`encryptKey 长度 ${len} 字节被接受`, !threw, threw && threw.message);
|
|
80
|
+
if (db) db.close();
|
|
81
|
+
}
|
|
82
|
+
// 多字节 UTF-8 按字节计数(不是字符数)
|
|
83
|
+
let utf8Threw = null;
|
|
84
|
+
try {
|
|
85
|
+
const d = new Database(path.join(tmpDir, 'utf8.json'), { encryptKey: '中'.repeat(8), autoSave: false });
|
|
86
|
+
d.close();
|
|
87
|
+
} catch (e) { utf8Threw = e; }
|
|
88
|
+
ok('encryptKey 按字节计数(8 个中文字 = 24 字节)被接受', !utf8Threw, utf8Threw && utf8Threw.message);
|
|
89
|
+
|
|
90
|
+
// ================= #313 Redis EXPIRE 条件选项 =================
|
|
91
|
+
const rsrv = createRedisServer({ port: 0, dataDir: null });
|
|
92
|
+
await new Promise((r) => rsrv.listen(r));
|
|
93
|
+
const rsock = net.connect(rsrv.server.address().port, '127.0.0.1');
|
|
94
|
+
await new Promise((r) => rsock.on('connect', r));
|
|
95
|
+
const rchunks = [];
|
|
96
|
+
rsock.on('data', (c) => rchunks.push(c));
|
|
97
|
+
const rsend = async (args) => {
|
|
98
|
+
rchunks.length = 0;
|
|
99
|
+
rsock.write(redisFrame(args));
|
|
100
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
101
|
+
return Buffer.concat(rchunks).toString('utf8');
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await rsend(['SET', 'e', 'x']);
|
|
105
|
+
ok('SET 返回 +OK', (await rsend(['SET', 'e', 'x'])) === '+OK\r\n', null);
|
|
106
|
+
|
|
107
|
+
// XX:键无 TTL 时应返回 0 且不设置
|
|
108
|
+
ok('EXPIRE e 100 XX(无 TTL)返回 0', (await rsend(['EXPIRE', 'e', '100', 'XX'])) === ':0\r\n', null);
|
|
109
|
+
ok('EXPIRE e 100 XX 后 TTL 仍为 -1', (await rsend(['TTL', 'e'])) === ':-1\r\n', null);
|
|
110
|
+
|
|
111
|
+
// NX:键无 TTL 时应成功
|
|
112
|
+
ok('EXPIRE e 100 NX(无 TTL)返回 1', (await rsend(['EXPIRE', 'e', '100', 'NX'])) === ':1\r\n', null);
|
|
113
|
+
ok('EXPIRE e 100 NX 后 TTL = 100', (await rsend(['TTL', 'e'])) === ':100\r\n', null);
|
|
114
|
+
|
|
115
|
+
// NX:键已有 TTL 时应返回 0
|
|
116
|
+
ok('EXPIRE e 200 NX(已有 TTL)返回 0', (await rsend(['EXPIRE', 'e', '200', 'NX'])) === ':0\r\n', null);
|
|
117
|
+
ok('EXPIRE e 200 NX 后 TTL 未变', (await rsend(['TTL', 'e'])) === ':100\r\n', null);
|
|
118
|
+
|
|
119
|
+
// XX:键已有 TTL 时应成功
|
|
120
|
+
ok('EXPIRE e 300 XX(已有 TTL)返回 1', (await rsend(['EXPIRE', 'e', '300', 'XX'])) === ':1\r\n', null);
|
|
121
|
+
ok('EXPIRE e 300 XX 后 TTL = 300', (await rsend(['TTL', 'e'])) === ':300\r\n', null);
|
|
122
|
+
|
|
123
|
+
// GT:新 TTL 不大于现有 → 返回 0
|
|
124
|
+
ok('EXPIRE e 100 GT(新 TTL 更小)返回 0', (await rsend(['EXPIRE', 'e', '100', 'GT'])) === ':0\r\n', null);
|
|
125
|
+
ok('EXPIRE e 100 GT 后 TTL 未变', (await rsend(['TTL', 'e'])) === ':300\r\n', null);
|
|
126
|
+
|
|
127
|
+
// GT:新 TTL 大于现有 → 返回 1
|
|
128
|
+
ok('EXPIRE e 400 GT(新 TTL 更大)返回 1', (await rsend(['EXPIRE', 'e', '400', 'GT'])) === ':1\r\n', null);
|
|
129
|
+
ok('EXPIRE e 400 GT 后 TTL = 400', (await rsend(['TTL', 'e'])) === ':400\r\n', null);
|
|
130
|
+
|
|
131
|
+
// LT:新 TTL 不小于现有 → 返回 0
|
|
132
|
+
ok('EXPIRE e 400 LT(相等)返回 0', (await rsend(['EXPIRE', 'e', '400', 'LT'])) === ':0\r\n', null);
|
|
133
|
+
ok('EXPIRE e 100 LT(新 TTL 更小)返回 1', (await rsend(['EXPIRE', 'e', '100', 'LT'])) === ':1\r\n', null);
|
|
134
|
+
|
|
135
|
+
// 无条件 EXPIRE 仍照常工作
|
|
136
|
+
ok('EXPIRE e 100(无条件)返回 1', (await rsend(['EXPIRE', 'e', '100'])) === ':1\r\n', null);
|
|
137
|
+
|
|
138
|
+
// 未知选项必须报语法错误,不能静默忽略
|
|
139
|
+
const badOpt = await rsend(['EXPIRE', 'e', '100', 'ZZ']);
|
|
140
|
+
ok('EXPIRE 未知选项报 ERR syntax error', badOpt.startsWith('-ERR syntax error'), badOpt);
|
|
141
|
+
|
|
142
|
+
// 多个条件选项冲突
|
|
143
|
+
const twoOpt = await rsend(['EXPIRE', 'e', '100', 'NX', 'XX']);
|
|
144
|
+
ok('EXPIRE 同时给 NX/XX 报 ERR syntax error', twoOpt.startsWith('-ERR syntax error'), twoOpt);
|
|
145
|
+
|
|
146
|
+
rsock.destroy();
|
|
147
|
+
rsrv.stop();
|
|
148
|
+
|
|
149
|
+
// ================= Mongo 相关(#312 / #315 / #316)=================
|
|
150
|
+
const msrv = createMongoServer({ dataDir: ':memory:' });
|
|
151
|
+
await new Promise((r) => msrv.listen(r));
|
|
152
|
+
|
|
153
|
+
// #315:listIndexes 对不存在的集合必须报 nsNotFound
|
|
154
|
+
const missing = await mongoCall(msrv, { listIndexes: 'nope_coll', $db: 'test' });
|
|
155
|
+
ok('listIndexes 不存在的集合 ok = 0', missing.ok === 0, missing);
|
|
156
|
+
ok('listIndexes 不存在的集合 code = 26(nsNotFound)', missing.code === 26, missing);
|
|
157
|
+
ok('listIndexes 不存在的集合不含 _id_ 索引',
|
|
158
|
+
!missing.cursor || !(missing.cursor.firstBatch || []).length, missing);
|
|
159
|
+
|
|
160
|
+
// 存在的集合应正常返回 _id_ 索引
|
|
161
|
+
await mongoCall(msrv, { insert: 'nl', documents: [{ n: 1 }, { n: 2 }, { n: 3 }], $db: 'test' });
|
|
162
|
+
const existing = await mongoCall(msrv, { listIndexes: 'nl', $db: 'test' });
|
|
163
|
+
ok('listIndexes 已存在的集合 ok = 1', existing.ok === 1, existing);
|
|
164
|
+
ok('listIndexes 已存在的集合返回 _id_ 索引',
|
|
165
|
+
(existing.cursor.firstBatch || []).some(i => i.name === '_id_'), existing);
|
|
166
|
+
|
|
167
|
+
// #316:负 limit 按绝对值取单批
|
|
168
|
+
const negLimit = await mongoCall(msrv, { find: 'nl', filter: {}, limit: -2, $db: 'test' });
|
|
169
|
+
ok('find limit=-2 只返回 2 条', (negLimit.cursor.firstBatch || []).length === 2, negLimit);
|
|
170
|
+
const negLimit3 = await mongoCall(msrv, { find: 'nl', filter: {}, limit: -3, $db: 'test' });
|
|
171
|
+
ok('find limit=-3 返回 3 条', (negLimit3.cursor.firstBatch || []).length === 3, negLimit3);
|
|
172
|
+
const posLimit = await mongoCall(msrv, { find: 'nl', filter: {}, limit: 1, $db: 'test' });
|
|
173
|
+
ok('find limit=1 返回 1 条(不回归)', (posLimit.cursor.firstBatch || []).length === 1, posLimit);
|
|
174
|
+
const noLimit = await mongoCall(msrv, { find: 'nl', filter: {}, $db: 'test' });
|
|
175
|
+
ok('find 无 limit 返回全部(不回归)', (noLimit.cursor.firstBatch || []).length === 3, noLimit);
|
|
176
|
+
|
|
177
|
+
// #312:$group 的 $push / $addToSet
|
|
178
|
+
await mongoCall(msrv, { insert: 'gp', documents: [{ g: 'x', t: 1 }, { g: 'x', t: 2 }, { g: 'y', t: 5 }], $db: 'test' });
|
|
179
|
+
const push = await mongoCall(msrv, {
|
|
180
|
+
aggregate: 'gp', pipeline: [{ $group: { _id: '$g', vals: { $push: '$t' } } }], $db: 'test'
|
|
181
|
+
});
|
|
182
|
+
const pushRows = push.cursor.firstBatch || [];
|
|
183
|
+
const pushX = pushRows.find(r => r._id === 'x');
|
|
184
|
+
const pushY = pushRows.find(r => r._id === 'y');
|
|
185
|
+
ok('$push 输出文档含 vals 字段', pushX && pushX.vals !== undefined, pushRows);
|
|
186
|
+
ok('$push x 组 = [1,2]', Array.isArray(pushX && pushX.vals) && pushX.vals.length === 2
|
|
187
|
+
&& pushX.vals[0] === 1 && pushX.vals[1] === 2, pushX);
|
|
188
|
+
ok('$push y 组 = [5]', Array.isArray(pushY && pushY.vals) && pushY.vals.length === 1
|
|
189
|
+
&& pushY.vals[0] === 5, pushY);
|
|
190
|
+
|
|
191
|
+
const addToSet = await mongoCall(msrv, {
|
|
192
|
+
aggregate: 'gp', pipeline: [{ $group: { _id: '$g', vals: { $addToSet: '$t' } } }], $db: 'test'
|
|
193
|
+
});
|
|
194
|
+
const atsRows = addToSet.cursor.firstBatch || [];
|
|
195
|
+
const atsX = atsRows.find(r => r._id === 'x');
|
|
196
|
+
ok('$addToSet 输出文档含 vals 字段', atsX && atsX.vals !== undefined, atsRows);
|
|
197
|
+
ok('$addToSet x 组 = [1,2]', Array.isArray(atsX && atsX.vals) && atsX.vals.length === 2, atsX);
|
|
198
|
+
|
|
199
|
+
// 去重:同一组内插入重复值
|
|
200
|
+
await mongoCall(msrv, { insert: 'gp2', documents: [{ g: 'x', t: 1 }, { g: 'x', t: 1 }, { g: 'x', t: 2 }], $db: 'test' });
|
|
201
|
+
const ats2 = await mongoCall(msrv, {
|
|
202
|
+
aggregate: 'gp2', pipeline: [{ $group: { _id: '$g', vals: { $addToSet: '$t' } } }], $db: 'test'
|
|
203
|
+
});
|
|
204
|
+
const ats2X = (ats2.cursor.firstBatch || []).find(r => r._id === 'x');
|
|
205
|
+
ok('$addToSet 去重后只剩 2 个元素',
|
|
206
|
+
Array.isArray(ats2X && ats2X.vals) && ats2X.vals.length === 2, ats2X);
|
|
207
|
+
|
|
208
|
+
// $push 保留全部元素(含重复)
|
|
209
|
+
const push2 = await mongoCall(msrv, {
|
|
210
|
+
aggregate: 'gp2', pipeline: [{ $group: { _id: '$g', vals: { $push: '$t' } } }], $db: 'test'
|
|
211
|
+
});
|
|
212
|
+
const push2X = (push2.cursor.firstBatch || []).find(r => r._id === 'x');
|
|
213
|
+
ok('$push 保留重复元素(3 个)',
|
|
214
|
+
Array.isArray(push2X && push2X.vals) && push2X.vals.length === 3, push2X);
|
|
215
|
+
|
|
216
|
+
// 已有累加器不回归
|
|
217
|
+
const sum = await mongoCall(msrv, {
|
|
218
|
+
aggregate: 'gp', pipeline: [{ $group: { _id: '$g', total: { $sum: '$t' } } }], $db: 'test'
|
|
219
|
+
});
|
|
220
|
+
const sumX = (sum.cursor.firstBatch || []).find(r => r._id === 'x');
|
|
221
|
+
ok('$sum 不回归:x 组 = 3', sumX && sumX.total === 3, sumX);
|
|
222
|
+
|
|
223
|
+
msrv.close();
|
|
224
|
+
|
|
225
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
226
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
main().catch(e => {
|
|
230
|
+
console.log('[FAIL] unexpected exception: ' + (e.stack || e).split('\n').slice(0, 4).join('\n'));
|
|
231
|
+
process.exit(1);
|
|
232
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/* ---- 回归:#314 / #317 / #318 / #319 / #321 ---- */
|
|
2
|
+
/*
|
|
3
|
+
* #314 Redis LPOP/RPOP 的 count 参数用裸 parseInt —— '1.5' 被截成 1、'2abc'
|
|
4
|
+
* 被截成 2,尾随垃圾静默接受。改为复用 parseRedisInt(严格十进制)。
|
|
5
|
+
* #317 REGEXP/RLIKE 非法正则模式串抛裸 V8 SyntaxError(无 errno/SQLSTATE),
|
|
6
|
+
* 归一为 MySQL 的 ER_REGEXP_ERROR(1139) / SQLSTATE 42000。
|
|
7
|
+
* #318 LPAD/RPAD 的填充串为 NULL 时退化成空格填充(' 7'),MySQL 应整体 NULL。
|
|
8
|
+
* #319 Cache 的 TTL 清理定时器与自动保存定时器未 unref(),漏调 close() 时
|
|
9
|
+
* 挂住 Node 事件循环,进程无法自然退出。
|
|
10
|
+
* #321 legacy 单文件模式的自动保存定时器未 unref()(同文件 _flushTimer /
|
|
11
|
+
* _monitorTimer 已 unref),处理不一致。
|
|
12
|
+
*/
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { execFileSync } = require('child_process');
|
|
15
|
+
const ROOT = path.join(__dirname, '..');
|
|
16
|
+
const { Database, executeSQL } = require(path.join(ROOT, 'index.js'));
|
|
17
|
+
|
|
18
|
+
let pass = 0, fail = 0;
|
|
19
|
+
function ok(name, cond, got) {
|
|
20
|
+
if (cond) { pass++; console.log('[OK] ' + name); }
|
|
21
|
+
else { fail++; console.log('[FAIL] ' + name + ' -> ' + JSON.stringify(got)); }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function main() {
|
|
25
|
+
const db = new Database(':memory:', { autoSave: false });
|
|
26
|
+
const q = async (sql) => (await executeSQL(db, sql, { safety: false })).rows[0][0];
|
|
27
|
+
|
|
28
|
+
// ================= #318 LPAD / RPAD 的 NULL 填充串 =================
|
|
29
|
+
ok("LPAD('7',3,NULL) = NULL(MySQL 三值逻辑)",
|
|
30
|
+
await q("SELECT LPAD('7', 3, NULL)") === null, null);
|
|
31
|
+
ok("RPAD('7',3,NULL) = NULL(MySQL 三值逻辑)",
|
|
32
|
+
await q("SELECT RPAD('7', 3, NULL)") === null, null);
|
|
33
|
+
ok("LPAD('7',3,NULL) IS NULL 为真",
|
|
34
|
+
await q("SELECT LPAD('7', 3, NULL) IS NULL") === 1, null);
|
|
35
|
+
ok("LPAD('7',3,'0') = '007'(不回归)",
|
|
36
|
+
await q("SELECT LPAD('7', 3, '0')") === '007', null);
|
|
37
|
+
ok("RPAD('7',3,'0') = '700'(不回归)",
|
|
38
|
+
await q("SELECT RPAD('7', 3, '0')") === '700', null);
|
|
39
|
+
ok("LPAD('hi',5,'ab') = 'abahi'(填充串循环不回归)",
|
|
40
|
+
await q("SELECT LPAD('hi', 5, 'ab')") === 'abahi', null);
|
|
41
|
+
ok("LPAD('hi',4,'') = ''(空填充串不回归)",
|
|
42
|
+
await q("SELECT LPAD('hi', 4, '')") === '', null);
|
|
43
|
+
// 缺省第三参仍按空格填充(MySQL 允许省略 padstr)
|
|
44
|
+
ok("LPAD('7',3) = ' 7'(缺省填充串仍用空格)",
|
|
45
|
+
await q("SELECT LPAD('7', 3)") === ' 7', null);
|
|
46
|
+
|
|
47
|
+
// ================= #317 REGEXP 非法正则 =================
|
|
48
|
+
let regexpErr = null;
|
|
49
|
+
try { await q("SELECT 'abc' REGEXP '['"); }
|
|
50
|
+
catch (e) { regexpErr = e; }
|
|
51
|
+
ok("非法正则 '[' 抛出错误而不是静默", !!regexpErr, regexpErr && regexpErr.message);
|
|
52
|
+
ok("非法正则归一为 errno 1139", regexpErr && regexpErr.errno === 1139,
|
|
53
|
+
regexpErr && regexpErr.errno);
|
|
54
|
+
ok("非法正则带 SQLSTATE 42000", regexpErr && regexpErr.sqlState === '42000',
|
|
55
|
+
regexpErr && regexpErr.sqlState);
|
|
56
|
+
ok("非法正则带 code ER_REGEXP_ERROR", regexpErr && regexpErr.code === 'ER_REGEXP_ERROR',
|
|
57
|
+
regexpErr && regexpErr.code);
|
|
58
|
+
|
|
59
|
+
let rlikeErr = null;
|
|
60
|
+
try { await q("SELECT 'abc' RLIKE '('"); }
|
|
61
|
+
catch (e) { rlikeErr = e; }
|
|
62
|
+
ok("非法正则 '('(RLIKE)同样归一为 1139", rlikeErr && rlikeErr.errno === 1139,
|
|
63
|
+
rlikeErr && rlikeErr.errno);
|
|
64
|
+
|
|
65
|
+
// 合法正则行为不变
|
|
66
|
+
ok("合法 REGEXP 不受影响:'abc' REGEXP '^a' = 1",
|
|
67
|
+
await q("SELECT 'abc' REGEXP '^a'") === 1, null);
|
|
68
|
+
ok("合法 REGEXP 不受影响:'abc' REGEXP '^z' = 0",
|
|
69
|
+
await q("SELECT 'abc' REGEXP '^z'") === 0, null);
|
|
70
|
+
ok("REGEXP 操作数为 NULL 仍返回 NULL(#230 不回归)",
|
|
71
|
+
await q("SELECT NULL REGEXP 'a'") === null, null);
|
|
72
|
+
|
|
73
|
+
// ================= #314 Redis LPOP / RPOP count 校验 =================
|
|
74
|
+
const { createRedisServer } = require(path.join(ROOT, 'lib/redis_server.js'));
|
|
75
|
+
const net = require('net');
|
|
76
|
+
|
|
77
|
+
function frame(args) {
|
|
78
|
+
const parts = args.map((a) => { const b = Buffer.from(String(a)); return '$' + b.length + '\r\n' + b + '\r\n'; });
|
|
79
|
+
return Buffer.from('*' + args.length + '\r\n' + parts.join(''));
|
|
80
|
+
}
|
|
81
|
+
const srv = createRedisServer({ port: 0, dataDir: null });
|
|
82
|
+
await new Promise((r) => srv.listen(r));
|
|
83
|
+
const sock = net.connect(srv.server.address().port, '127.0.0.1');
|
|
84
|
+
await new Promise((r) => sock.on('connect', r));
|
|
85
|
+
const chunks = [];
|
|
86
|
+
sock.on('data', (c) => chunks.push(c));
|
|
87
|
+
const send = async (args) => {
|
|
88
|
+
chunks.length = 0;
|
|
89
|
+
sock.write(frame(args));
|
|
90
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
91
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
await send(['RPUSH', 'l', '1', '2', '3']);
|
|
95
|
+
ok('RPUSH 返回 :3', (await send(['RPUSH', 'l2', '1', '2', '3'])) === ':3\r\n', null);
|
|
96
|
+
|
|
97
|
+
const lpopFrac = await send(['LPOP', 'l', '1.5']);
|
|
98
|
+
ok('LPOP count=1.5 报 integer 错误', lpopFrac.startsWith('-ERR'), lpopFrac);
|
|
99
|
+
ok('LPOP count=1.5 错误文案为 not an integer',
|
|
100
|
+
/not an integer or out of range/.test(lpopFrac), lpopFrac);
|
|
101
|
+
|
|
102
|
+
const lpopJunk = await send(['LPOP', 'l', '2abc']);
|
|
103
|
+
ok('LPOP count=2abc 报 integer 错误', lpopJunk.startsWith('-ERR'), lpopJunk);
|
|
104
|
+
|
|
105
|
+
const lpopNeg = await send(['LPOP', 'l', '-1']);
|
|
106
|
+
ok('LPOP count=-1 报 out of range 错误', lpopNeg.startsWith('-ERR'), lpopNeg);
|
|
107
|
+
|
|
108
|
+
// 报错后不应弹出任何元素
|
|
109
|
+
const llen = await send(['LLEN', 'l']);
|
|
110
|
+
ok('非法 count 不弹出元素(LLEN 仍为 3)', llen === ':3\r\n', llen);
|
|
111
|
+
|
|
112
|
+
const lpopOk = await send(['LPOP', 'l', '2']);
|
|
113
|
+
ok('LPOP count=2 正常返回两个元素', /^\*2\r\n/.test(lpopOk), lpopOk);
|
|
114
|
+
const llen2 = await send(['LLEN', 'l']);
|
|
115
|
+
ok('LPOP count=2 后 LLEN = 1', llen2 === ':1\r\n', llen2);
|
|
116
|
+
|
|
117
|
+
const rpopOk = await send(['RPOP', 'l2', '1']);
|
|
118
|
+
ok('RPOP count=1 正常返回', /^\*1\r\n/.test(rpopOk), rpopOk);
|
|
119
|
+
|
|
120
|
+
sock.destroy();
|
|
121
|
+
srv.stop();
|
|
122
|
+
|
|
123
|
+
// ================= #319 / #321 定时器 unref =================
|
|
124
|
+
// 用子进程跑一个「实例化后不 close()」的脚本,检查进程能自然退出。
|
|
125
|
+
const timerProbe = `
|
|
126
|
+
const root = ${JSON.stringify(ROOT)};
|
|
127
|
+
const { Database } = require(root + '/index.js');
|
|
128
|
+
const Cache = require(root + '/lib/cache.js');
|
|
129
|
+
const fs = require('fs'), os = require('os'), path = require('path');
|
|
130
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-timer-'));
|
|
131
|
+
const db = new Database(path.join(dir, 'db.json'), { autoSave: true, autoSaveInterval: 5000 });
|
|
132
|
+
const cache = new Cache();
|
|
133
|
+
cache.set('a', '1');
|
|
134
|
+
db.createTable('t', { id: { type: 'integer', primaryKey: true } }).then(() => {
|
|
135
|
+
db.insert('t', { id: 1 }).then(() => {
|
|
136
|
+
console.log('READY');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
`;
|
|
140
|
+
const probeFile = path.join(ROOT, 'test', '.tmp-timer-probe.js');
|
|
141
|
+
fs.writeFileSync(probeFile, timerProbe, 'utf8');
|
|
142
|
+
let exited = false;
|
|
143
|
+
try {
|
|
144
|
+
execFileSync(process.execPath, [probeFile], { timeout: 4000, stdio: 'pipe' });
|
|
145
|
+
exited = true;
|
|
146
|
+
} catch (e) {
|
|
147
|
+
// 超时(SIGTERM)说明定时器挂住了事件循环
|
|
148
|
+
exited = false;
|
|
149
|
+
} finally {
|
|
150
|
+
fs.unlinkSync(probeFile);
|
|
151
|
+
}
|
|
152
|
+
ok('#319/#321 未 close() 时进程能自然退出(定时器已 unref)', exited, null);
|
|
153
|
+
|
|
154
|
+
console.log(`\n${pass} passed, ${fail} failed`);
|
|
155
|
+
process.exit(fail === 0 ? 0 : 1);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const fs = require('fs');
|
|
159
|
+
main().catch(e => {
|
|
160
|
+
console.log('[FAIL] unexpected exception: ' + (e.stack || e).split('\n').slice(0, 4).join('\n'));
|
|
161
|
+
process.exit(1);
|
|
162
|
+
});
|
|
@@ -73,8 +73,12 @@ try {
|
|
|
73
73
|
ok('非法版本号 → FAIL', r4.ok === false, r4.checks.filter(c => !c.ok));
|
|
74
74
|
|
|
75
75
|
// 开发中往 Unreleased 累积内容不应让门禁变红(只给软提示)
|
|
76
|
+
// 注意:工作区可能是 CRLF(core.autocrlf=true),用字面量 '\n' 匹配会静默失配,
|
|
77
|
+
// 反例没构造出来 → 断言变成假红/假绿。这里用 \r?\n 且必须真的替换成功。
|
|
76
78
|
const wip = fixture('wip-unreleased', (c) => {
|
|
77
|
-
|
|
79
|
+
const re = /## \[Unreleased\]\r?\n/;
|
|
80
|
+
if (!re.test(c['CHANGELOG.md'])) throw new Error('反例构造失败:CHANGELOG 缺 [Unreleased] 段');
|
|
81
|
+
c['CHANGELOG.md'] = c['CHANGELOG.md'].replace(re, '## [Unreleased]\r\n\r\n### Added\r\n\r\n- 开发中\r\n');
|
|
78
82
|
});
|
|
79
83
|
const r5 = checkReleaseMetadata(wip);
|
|
80
84
|
ok('开发中 Unreleased 有内容仍通过', r5.ok === true, r5.checks.filter(c => !c.ok));
|