jsql-neo 6.7.2 → 6.7.3

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 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.7.2** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.7.3** — 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
  ![Engines](https://img.shields.io/badge/engines-Native%20%7C%20WASM%20%7C%20Pure%20JS-7ee787)
9
9
  ![MySQL](https://img.shields.io/badge/protocol-MySQL%20compatible-1f6feb)
@@ -266,7 +266,18 @@ source stating why it cannot be done locally:
266
266
  | `jsql star --token-check` | `api.github.com` | star status only exists on the GitHub account | `JSQL_OFFLINE=1` |
267
267
  | `jsql update` | `registry.npmjs.org` | "is there a newer version" only exists in the registry | `JSQL_OFFLINE=1` |
268
268
  | `jsql plugin list\|search\|info\|install` | `registry-beta.vexify.top` | the plugin catalogue only exists remotely | `JSQL_OFFLINE=1` |
269
- | `npm install` (postinstall) | GitHub releases | the precompiled Rust `.node` is not in the npm tarball | `JSQL_NO_NATIVE_DOWNLOAD=1` or `JSQL_OFFLINE=1` |
269
+ | `npm install` (postinstall) | GitHub releases (`github.com`) | the precompiled Rust `.node` is not in the npm tarball | `JSQL_NO_NATIVE_DOWNLOAD=1` or `JSQL_OFFLINE=1` |
270
+
271
+ `npm install` downloads the prebuilt addon from the official GitHub release host
272
+ and verifies it against the published `SHA256SUMS`; a mismatch (or a missing
273
+ manifest) aborts the download. **No third-party mirror is used by default.** If
274
+ `github.com` is unreachable you can opt into a proxy explicitly:
275
+
276
+ | Variable | Meaning |
277
+ | --- | --- |
278
+ | `JSQL_GHPROXY` | Comma-separated proxy prefixes, tried in order (e.g. `https://gh-proxy.com/`). Empty/unset = direct only. |
279
+ | `JSQL_GHPROXY_TOKEN` | Optional bearer token, sent as `Authorization: Bearer …` **only** to the proxy origins listed in `JSQL_GHPROXY` (never to `github.com`, never on cross-origin redirects). |
280
+ | `JSQL_NATIVE_SHA256` | Pin the expected digest instead of reading `SHA256SUMS`. |
270
281
 
271
282
  `JSQL_OFFLINE=1` disables every external request at once; affected commands
272
283
  report the reason instead of failing silently. Loopback traffic (the
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.7.2 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.7.3 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/sql.js CHANGED
@@ -3404,9 +3404,90 @@ function mysqlNum(v) {
3404
3404
  const n = Number(m[0]);
3405
3405
  return Number.isFinite(n) ? n : 0;
3406
3406
  }
3407
+ // Issue #108:二进制值按 latin1 文本参与数字上下文(MySQL 的隐式转换)
3408
+ if (isBinaryValue(v)) return mysqlNum(v.toString());
3407
3409
  return 0;
3408
3410
  }
3409
3411
 
3412
+ // ---------------------------------------------------------------------------
3413
+ // 二进制字符串(Issue #108)
3414
+ //
3415
+ // 问题:UNHEX() 曾用 `Buffer.from(h, 'hex').toString('latin1')` 把字节塞进
3416
+ // JS 字符串,HEX()/LENGTH()/BIT_LENGTH() 再用 UTF-8 解释该字符串 —— 于是
3417
+ // 码点 0x80-0xFF 被二次编码成两个字节(UNHEX('DEADBEEF') 的 4 字节变成 8 字节,
3418
+ // HEX(UNHEX('DEADBEEF')) 变成 'C39EC2ADC2BEC3AF')。数据被静默放大,无法往返。
3419
+ //
3420
+ // 方案:UNHEX() 返回 BinaryValue(内部持有 Buffer),字节层面无损;HEX /
3421
+ // LENGTH / BIT_LENGTH / CHAR_LENGTH 识别该类型并直接按字节数/字节内容处理。
3422
+ // BinaryValue 不是字符串,但在需要文本的地方(比较、CONCAT、输出)通过
3423
+ // toString() 降级为 latin1 文本,与 MySQL「binary string 也当字符串用」一致。
3424
+ // 结果信封(rows)里仍会降级成普通字符串(见 finalizeResultBigInts),
3425
+ // 所以对外 API 与旧行为保持兼容。
3426
+ // ---------------------------------------------------------------------------
3427
+ class BinaryValue {
3428
+ constructor(buf) {
3429
+ this.bytes = Buffer.isBuffer(buf) ? Buffer.from(buf) : Buffer.from(String(buf), 'latin1');
3430
+ }
3431
+ /** 字节数(MySQL 的 LENGTH / CHAR_LENGTH 对二进制串都按字节计) */
3432
+ get length() { return this.bytes.length; }
3433
+ toString() { return this.bytes.toString('latin1'); }
3434
+ valueOf() { return this.bytes.toString('latin1'); }
3435
+ toJSON() { return this.bytes.toString('latin1'); }
3436
+ [Symbol.toPrimitive]() { return this.bytes.toString('latin1'); }
3437
+ }
3438
+
3439
+ function isBinaryValue(v) { return v instanceof BinaryValue; }
3440
+
3441
+ /** 去重 / GROUP BY / 窗口分组用的键:二进制值按文本,其余保持原逻辑 */
3442
+ function valueKey(v) {
3443
+ if (isBinaryValue(v)) return v.toString();
3444
+ return typeof v === 'object' ? JSON.stringify(v) : String(v);
3445
+ }
3446
+
3447
+ // ---------------------------------------------------------------------------
3448
+ // 位运算(Issue #109)
3449
+ //
3450
+ // MySQL 的 `<<` / `>>`:操作数先按 64 位无符号整数处理(小数部分被丢),
3451
+ // 移位量取整数部分(`1 << 1.9` = `1 << 1` = 2)。此前用的是
3452
+ // `l * Math.pow(2, r)`,既没截断小数(1 << 1.9 = 3.73),又在大移位量下
3453
+ // 丢浮点精度(1 << 63 = 9223372036854776000)。这里全程用 BigInt 精确计算,
3454
+ // 结果超过 2^53 时保留 BigInt(由 finalizeResultBigInts 转成字符串输出)。
3455
+ // 边界与 MySQL 一致:移位量 < 0 或 >= 64 → 0;64 位溢出按无符号回绕。
3456
+ // ---------------------------------------------------------------------------
3457
+ const U64_MASK = (1n << 64n) - 1n;
3458
+ const MAX_SAFE_BIGINT = BigInt(Number.MAX_SAFE_INTEGER);
3459
+
3460
+ /** MySQL 整数转换:小数四舍五入(远离零),负数按补码转到 64 位无符号 */
3461
+ function toUint64(v) {
3462
+ let n;
3463
+ if (typeof v === 'bigint') n = v;
3464
+ else if (typeof v === 'boolean') n = v ? 1n : 0n;
3465
+ else if (isBinaryValue(v)) n = BigInt(Math.round(mysqlNum(v.toString())));
3466
+ else {
3467
+ const x = mysqlNum(v);
3468
+ // 四舍五入且远离零:-1.5 → -2(Math.round 会得到 -1)
3469
+ n = BigInt(x >= 0 ? Math.floor(x + 0.5) : Math.ceil(x - 0.5));
3470
+ }
3471
+ return n & U64_MASK;
3472
+ }
3473
+
3474
+ /** BigInt 结果 → 对外值:安全整数范围内给 number,否则保留 BigInt(输出为字符串) */
3475
+ function fromUint64(n) {
3476
+ return n <= MAX_SAFE_BIGINT ? Number(n) : n;
3477
+ }
3478
+
3479
+ function sqlBitShift(l, r, op) {
3480
+ const a = toUint64(l);
3481
+ // 移位量只取整数部分(trunc,不是 round)—— MySQL 丢弃小数位
3482
+ const rawShift = mysqlNum(r);
3483
+ if (!Number.isFinite(rawShift)) return 0;
3484
+ const s = Math.trunc(rawShift);
3485
+ if (s < 0 || s >= 64) return 0;
3486
+ const shift = BigInt(s);
3487
+ if (op === '<<') return fromUint64((a << shift) & U64_MASK);
3488
+ return fromUint64(a >> shift);
3489
+ }
3490
+
3410
3491
  // ---------------------------------------------------------------------------
3411
3492
  // CHECK 约束(Issue #70)
3412
3493
  //
@@ -3638,8 +3719,8 @@ function resolveOperand(operand, row, ctx) {
3638
3719
  case '^': return (mysqlNum(l) | 0) ^ (mysqlNum(r) | 0);
3639
3720
  // P1-22: JavaScript 位运算只使用 32 位整数,移位量对 32 取模
3640
3721
  // 1 << 32 = 1 << 0 = 1,但 SQL 中 1 << 32 = 4294967296
3641
- case '<<': return mysqlNum(l) * Math.pow(2, mysqlNum(r));
3642
- case '>>': return Math.floor(mysqlNum(l) / Math.pow(2, mysqlNum(r)));
3722
+ // Issue #109:移位量截断为整数并用 BigInt 精确计算(见 sqlBitShift)
3723
+ case '<<': case '>>': return sqlBitShift(l, r, operand.op);
3643
3724
  }
3644
3725
  return null;
3645
3726
  }
@@ -3679,8 +3760,8 @@ function resolveOperand(operand, row, ctx) {
3679
3760
  case '&': return (mysqlNum(l) | 0) & (mysqlNum(r) | 0);
3680
3761
  case '|': return (mysqlNum(l) | 0) | (mysqlNum(r) | 0);
3681
3762
  case '^': return (mysqlNum(l) | 0) ^ (mysqlNum(r) | 0);
3682
- case '<<': return mysqlNum(l) * Math.pow(2, mysqlNum(r));
3683
- case '>>': return Math.floor(mysqlNum(l) / Math.pow(2, mysqlNum(r)));
3763
+ // Issue #109:移位量截断 + BigInt 精确计算
3764
+ case '<<': case '>>': return sqlBitShift(l, r, operand.op);
3684
3765
  default: return null;
3685
3766
  }
3686
3767
  }
@@ -4139,8 +4220,14 @@ function applyScalarFunction(fnNode, row, ctx) {
4139
4220
  // CHAR_LENGTH / CHARACTER_LENGTH 返回**字符**数。
4140
4221
  // 此前 LENGTH 返回 String#length:「héllo」得 5(应为 6)、「🚀🌟」得 4(应为 8),
4141
4222
  // 按字节计长的 varchar(n) 长度校验会被绕过;OCTET_LENGTH 则整个缺失(静默 NULL)。
4142
- case 'LENGTH': case 'OCTET_LENGTH': return args[0] == null ? null : Buffer.byteLength(String(args[0]), 'utf8');
4143
- case 'CHAR_LENGTH': case 'CHARACTER_LENGTH': return args[0] == null ? null : String(args[0]).length;
4223
+ // Issue #108:二进制值按原始字节计数,不能再走 UTF-8 编码(会被放大)
4224
+ case 'LENGTH': case 'OCTET_LENGTH':
4225
+ return args[0] == null ? null
4226
+ : (isBinaryValue(args[0]) ? args[0].bytes.length : Buffer.byteLength(String(args[0]), 'utf8'));
4227
+ case 'CHAR_LENGTH': case 'CHARACTER_LENGTH':
4228
+ // MySQL:二进制串的 CHAR_LENGTH 也等于字节数
4229
+ return args[0] == null ? null
4230
+ : (isBinaryValue(args[0]) ? args[0].bytes.length : String(args[0]).length);
4144
4231
  // TRIM(s) / TRIM([LEADING|TRAILING|BOTH] chars FROM s) / TRIM(s, chars)
4145
4232
  case 'TRIM': {
4146
4233
  if (args[0] == null) return null;
@@ -4422,6 +4509,8 @@ function applyScalarFunction(fnNode, row, ctx) {
4422
4509
  // MAKE_SET / EXPORT_SET / SOUNDEX 仍未实现(未在文档中承诺)。
4423
4510
  case 'BIT_LENGTH': {
4424
4511
  if (args[0] == null) return null;
4512
+ // Issue #108:二进制值按原始字节数 × 8
4513
+ if (isBinaryValue(args[0])) return args[0].bytes.length * 8;
4425
4514
  return Buffer.byteLength(String(args[0]), 'utf8') * 8;
4426
4515
  }
4427
4516
  case 'BIN': case 'OCT': {
@@ -4437,13 +4526,17 @@ function applyScalarFunction(fnNode, row, ctx) {
4437
4526
  const n = typeof v === 'bigint' ? v : BigInt(Math.trunc(v));
4438
4527
  return (n < 0n ? BigInt.asUintN(64, n) : n).toString(16).toUpperCase();
4439
4528
  }
4529
+ // Issue #108:二进制值直接按字节转十六进制,保证 HEX(UNHEX(x)) 恒等
4530
+ if (isBinaryValue(v)) return v.bytes.toString('hex').toUpperCase();
4440
4531
  return Buffer.from(String(v), 'utf8').toString('hex').toUpperCase();
4441
4532
  }
4442
4533
  case 'UNHEX': {
4443
4534
  if (args[0] == null) return null;
4444
4535
  const h = String(args[0]);
4445
4536
  if (h.length % 2 !== 0 || !/^[0-9a-fA-F]*$/.test(h)) return null;
4446
- return Buffer.from(h, 'hex').toString('latin1');
4537
+ // Issue #108:返回 BinaryValue 而不是 latin1 字符串 —— 后者在 UTF-8
4538
+ // 编解码往返中会把 0x80-0xFF 摊成两字节,数据被静默放大。
4539
+ return new BinaryValue(Buffer.from(h, 'hex'));
4447
4540
  }
4448
4541
  case 'CONV': {
4449
4542
  if (args[0] == null || args[1] == null || args[2] == null) return null;
@@ -5431,11 +5524,16 @@ function strictNumeric(v) {
5431
5524
  // MySQL 默认 collation(utf8mb4_general_ci)下字符串比较不区分大小写,
5432
5525
  // 与 LIKE 保持一致。cs=true 表示显式 BINARY/COLLATE 强制大小写敏感。
5433
5526
  function ciEq(a, b, cs) {
5527
+ // Issue #108:二进制值先降级为 latin1 文本再比较(MySQL 允许 binary 与文本互比)
5528
+ if (isBinaryValue(a)) a = a.toString();
5529
+ if (isBinaryValue(b)) b = b.toString();
5434
5530
  if (cs) return a === b;
5435
5531
  if (typeof a === 'string' && typeof b === 'string') return a.toLowerCase() === b.toLowerCase();
5436
5532
  return String(a) === String(b);
5437
5533
  }
5438
5534
  function ciCompare(a, b, cs) {
5535
+ if (isBinaryValue(a)) a = a.toString();
5536
+ if (isBinaryValue(b)) b = b.toString();
5439
5537
  if (cs) return a === b ? 0 : (a < b ? -1 : 1);
5440
5538
  const as = String(a).toLowerCase(), bs = String(b).toLowerCase();
5441
5539
  return as === bs ? 0 : (as < bs ? -1 : 1);
@@ -5449,6 +5547,8 @@ function sqlTruthy(v) {
5449
5547
  if (v === null || v === undefined) return null;
5450
5548
  if (typeof v === 'boolean') return v;
5451
5549
  if (typeof v === 'number') return v !== 0;
5550
+ // Issue #108:二进制值按文本判定真值
5551
+ if (isBinaryValue(v)) return sqlTruthy(v.toString());
5452
5552
  if (typeof v === 'string') return v !== '' && !(Number(v) === 0 && String(v).trim() !== '');
5453
5553
  return Boolean(v);
5454
5554
  }
@@ -5714,8 +5814,8 @@ function evaluateExpr(expr, row, ctx) {
5714
5814
  case '&': return (mysqlNum(l) | 0) & (mysqlNum(r) | 0);
5715
5815
  case '|': return (mysqlNum(l) | 0) | (mysqlNum(r) | 0);
5716
5816
  case '^': return (mysqlNum(l) | 0) ^ (mysqlNum(r) | 0);
5717
- case '<<': return mysqlNum(l) * Math.pow(2, mysqlNum(r));
5718
- case '>>': return Math.floor(mysqlNum(l) / Math.pow(2, mysqlNum(r)));
5817
+ // Issue #109:移位量截断 + BigInt 精确计算
5818
+ case '<<': case '>>': return sqlBitShift(l, r, expr.op);
5719
5819
  default: return null;
5720
5820
  }
5721
5821
  }
@@ -7484,7 +7584,7 @@ class SQLExecutor {
7484
7584
  if (distinct) {
7485
7585
  const seen = new Set();
7486
7586
  values = values.filter(v => {
7487
- const k = typeof v === 'object' ? JSON.stringify(v) : String(v);
7587
+ const k = valueKey(v); // Issue #108:二进制值按 latin1 文本去重
7488
7588
  if (seen.has(k)) return false;
7489
7589
  seen.add(k);
7490
7590
  return true;
@@ -7588,7 +7688,7 @@ class SQLExecutor {
7588
7688
  if (distinct) {
7589
7689
  const seen = new Set();
7590
7690
  joined = joined.filter(v => {
7591
- const k = typeof v === 'object' ? JSON.stringify(v) : String(v);
7691
+ const k = valueKey(v); // Issue #108:二进制值按 latin1 文本去重
7592
7692
  if (seen.has(k)) return false;
7593
7693
  seen.add(k);
7594
7694
  return true;
@@ -9503,6 +9603,7 @@ function escapeValue(value) {
9503
9603
  const p = n => String(n).padStart(2, '0');
9504
9604
  return `'${value.getUTCFullYear()}-${p(value.getUTCMonth() + 1)}-${p(value.getUTCDate())} ${p(value.getUTCHours())}:${p(value.getUTCMinutes())}:${p(value.getUTCSeconds())}'`;
9505
9605
  }
9606
+ if (isBinaryValue(value)) return "UNHEX('" + value.bytes.toString('hex').toUpperCase() + "')";
9506
9607
  if (Buffer.isBuffer(value)) return "X'" + value.toString('hex') + "'";
9507
9608
  if (Array.isArray(value)) {
9508
9609
  if (value.some(Array.isArray)) {
@@ -9755,6 +9856,8 @@ async function inferColumnTypes(engine, stmt, r) {
9755
9856
  // 保留精度且可序列化;内部运算仍走 BigInt,精度不丢。
9756
9857
  function finalizeBigInts(value, seen) {
9757
9858
  if (typeof value === 'bigint') return value.toString();
9859
+ // Issue #108:二进制值在结果信封里降级为 latin1 字符串,对外 API 形状不变
9860
+ if (isBinaryValue(value)) return value.toString();
9758
9861
  if (Array.isArray(value)) {
9759
9862
  if (!seen) seen = new Set();
9760
9863
  if (seen.has(value)) return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.7.2",
3
+ "version": "6.7.3",
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",
package/postinstall.js CHANGED
@@ -30,18 +30,18 @@ const NATIVE_PATH = process.env.JSQL_NATIVE_PATH
30
30
  ? path.resolve(process.env.JSQL_NATIVE_PATH)
31
31
  : path.join(ROOT, 'native', 'jsql-neo-native.node');
32
32
 
33
- // GitHub 加速代理前缀(可选)。加速站的用法是把原始 URL 直接拼在后面:
33
+ // GitHub 加速代理前缀(可选,默认关闭)。加速站的用法是把原始 URL 直接拼在后面:
34
34
  // https://<host>/https://github.com/owner/repo/releases/download/vX.Y.Z/asset
35
- // 鉴权走 `Authorization: Bearer <token>` 请求头(Worker 从该头读取,不读 ?token=)。
36
- // 前缀必须显式设置 JSQL_GHPROXY(逗号分隔多个,按序回退;'' 表示直连);
37
- // token 只能从环境变量 JSQL_GHPROXY_TOKEN 读取,代码内不再内置任何 token。
35
+ // 启用方式:显式设置 JSQL_GHPROXY(逗号分隔多个,按序回退;空/未设置 = 只直连)。
36
+ // token(可选)只从 JSQL_GHPROXY_TOKEN 读取,并以 `Authorization: Bearer <token>`
37
+ // 请求头发给 **JSQL_GHPROXY 里列出的代理主机** —— 直连 github.com 时不发送;
38
+ // 跨源重定向时也会丢弃该请求头(见 download/fetchText 的 sameOrigin 判断)。
38
39
  //
39
- // 供应链安全(issue #25):已移除内置 de5.net 加速站与硬编码 token。这里只内置
40
- // 「不需要 token 的公共加速站」,且所有代理下载仍强制通过 SHA256SUMS 校验,
41
- // 因此第三方端点被投毒也装不进去(校验不通过即回退到下一个候选/直连)。
42
- // gh-proxy.com 实测:v6.5.32 资产 SHA256 与官方 SHA256SUMS 完全一致,且为合法 ELF。
43
- // 优先级:JSQL_GHPROXY(用户显式)→ 内置公共加速站 → 直连 github.com。
44
- const DEFAULT_PROXIES = ['https://gh-proxy.com/'];
40
+ // 供应链安全(issue #25 / #110):代码内不内置任何加速站,也不内置任何 token。
41
+ // 原生下载只走两条路:官方 GitHub Release 主机(直连),或用户自己显式配置的
42
+ // JSQL_GHPROXY 代理;两者都必须通过 SHA256SUMS 校验(fail closed),
43
+ // 因此就算第三方代理被投毒也装不进去。
44
+ // 优先级:JSQL_GHPROXY(用户显式)→ 直连 github.com。
45
45
  const GHPROXY = process.env.JSQL_GHPROXY !== undefined
46
46
  ? process.env.JSQL_GHPROXY
47
47
  : '';
@@ -61,7 +61,8 @@ const GHPROXY_TOKEN = process.env.JSQL_GHPROXY_TOKEN || '';
61
61
  // JSQL_NATIVE_TAG=vX.Y.Z|latest 指定远端 tag;默认 v<package.version>
62
62
  // JSQL_AUTO_DOWNLOAD_NATIVE=1 / JSQL_NATIVE_DOWNLOAD=1 仍然兼容(显式开)
63
63
  //
64
- // 供应链安全(issue #25):不内置任何加速站/token,代理仍需显式 JSQL_GHPROXY,
64
+ // 供应链安全(issue #25 / #110):不内置任何加速站/token —— 代理只能由用户
65
+ // 显式 JSQL_GHPROXY 指定,未设置时只直连官方 GitHub Release 主机;
65
66
  // 且下载必须通过 SHA256SUMS 校验(除非用户显式 JSQL_ALLOW_UNVERIFIED_NATIVE=1)。
66
67
  const DOWNLOAD_DISABLED = isOffline()
67
68
  || process.env.JSQL_NO_NATIVE_DOWNLOAD === '1'
@@ -81,14 +82,14 @@ function proxyHeaders() {
81
82
 
82
83
  /**
83
84
  * 生成候选下载地址列表(按优先级):
84
- * JSQL_GHPROXY 指定的代理 → 内置加速站 → 直连
85
+ * JSQL_GHPROXY 指定的代理(用户显式)→ 直连官方 github.com
86
+ * 没有内置的第三方加速站;JSQL_GHPROXY 为空时列表里只有直连一项。
85
87
  */
86
88
  function proxyCandidates(raw) {
87
89
  const bases = String(GHPROXY || '')
88
90
  .split(',')
89
91
  .map((s) => s.trim())
90
92
  .filter(Boolean)
91
- .concat(DEFAULT_PROXIES)
92
93
  .concat('');
93
94
  const list = [];
94
95
  for (const base of bases) {
@@ -464,12 +465,12 @@ async function downloadAndVerify(opts = {}) {
464
465
  console.warn('[jsql-neo] WARNING: JSQL_ALLOW_UNVERIFIED_NATIVE=1 — native module integrity is NOT verified.');
465
466
  }
466
467
 
467
- // 依次尝试:环境变量指定的代理 → 内置加速站 → 直连
468
+ // 依次尝试:JSQL_GHPROXY 显式指定的代理 → 直连官方 GitHub Release 主机
468
469
  for (const cand of proxyCandidates(raw)) {
469
470
  console.log(`[jsql-neo] fetching native module (${tag}): ${cand.url}`);
470
471
  const tmp = `${NATIVE_PATH}.${process.pid}.download`;
471
472
  try {
472
- // 加速站鉴权走 Authorization 头,直连时不带
473
+ // 代理鉴权(JSQL_GHPROXY_TOKEN)走 Authorization 头,且只发给代理主机;直连时不带
473
474
  await download(cand.url, tmp, cand.viaProxy ? proxyHeaders() : undefined);
474
475
  if (expected) {
475
476
  const actual = sha256File(tmp);
@@ -62,6 +62,54 @@ function ok(name, cond, got) {
62
62
  ok('SOUNDEX 空串', await q("SELECT SOUNDEX('')") === '', null);
63
63
  ok('SOUNDEX(NULL) → NULL', await q('SELECT SOUNDEX(NULL)') === null, null);
64
64
 
65
+ // ---------- 位移运算符(Issue #109)----------
66
+ // MySQL:移位量只取整数部分(trunc),操作数按 64 位无符号整数处理,
67
+ // 结果超过 2^53 时保留精确值(以字符串输出,与 #38 的大整数约定一致)。
68
+ ok('#109 1 << 1.9 = 2', await q('SELECT 1 << 1.9') === 2, null);
69
+ ok('#109 1 << 1.5 = 2', await q('SELECT 1 << 1.5') === 2, null);
70
+ ok('#109 1 << 2.9 = 4', await q('SELECT 1 << 2.9') === 4, null);
71
+ ok('#109 8 >> 1.9 = 4', await q('SELECT 8 >> 1.9') === 4, null);
72
+ ok('#109 7 >> 1.5 = 3', await q('SELECT 7 >> 1.5') === 3, null);
73
+ ok('#109 1 << 0.9 = 1', await q('SELECT 1 << 0.9') === 1, null);
74
+ ok('#109 1 << 0 = 1', await q('SELECT 1 << 0') === 1, null);
75
+ ok('#109 1 << 8 = 256(普通值仍是 number)', await q('SELECT 1 << 8') === 256, null);
76
+ ok('#109 1 << 31 = 2147483648(不按 32 位截断)', await q('SELECT 1 << 31') === 2147483648, null);
77
+ ok('#109 1 << 63 精确', String(await q('SELECT 1 << 63')) === '9223372036854775808', null);
78
+ ok('#109 1 << 62 精确', String(await q('SELECT 1 << 62')) === '4611686018427387904', null);
79
+ ok('#109 1 << 64 按 64 位回绕为 0', await q('SELECT 1 << 64') === 0, null);
80
+ ok('#109 8 >> 64 = 0', await q('SELECT 8 >> 64') === 0, null);
81
+ ok('#109 负移位量 → 0', await q('SELECT 1 << -1') === 0, null);
82
+ ok('#109 18446744073709551615 >> 1 精确',
83
+ String(await q('SELECT 18446744073709551615 >> 1')) === '9223372036854775807', null);
84
+ ok('#109 负数按无符号 64 位解释(-1 << 1)',
85
+ String(await q('SELECT -1 << 1')) === '18446744073709551614', null);
86
+ ok('#109 负数右移按无符号(-8 >> 1)',
87
+ String(await q('SELECT -8 >> 1')) === '9223372036854775804', null);
88
+ ok('#109 小数操作数四舍五入(1.9 << 1 = 4)', await q('SELECT 1.9 << 1') === 4, null);
89
+ ok('#109 移位结果可继续参与运算(1 << 4 + 1)', await q('SELECT (1 << 4) + 1') === 17, null);
90
+
91
+ // ---------- UNHEX / HEX 字节往返(Issue #108)----------
92
+ ok("#108 HEX(UNHEX('DEADBEEF')) 往返", await q("SELECT HEX(UNHEX('DEADBEEF'))") === 'DEADBEEF', null);
93
+ ok("#108 LENGTH(UNHEX('DEADBEEF')) = 4", await q("SELECT LENGTH(UNHEX('DEADBEEF'))") === 4, null);
94
+ ok("#108 BIT_LENGTH(UNHEX('DEADBEEF')) = 32", await q("SELECT BIT_LENGTH(UNHEX('DEADBEEF'))") === 32, null);
95
+ ok("#108 HEX(UNHEX('80')) 不被 UTF-8 放大", await q("SELECT HEX(UNHEX('80'))") === '80', null);
96
+ ok("#108 HEX(UNHEX('FF'))", await q("SELECT HEX(UNHEX('FF'))") === 'FF', null);
97
+ ok("#108 LENGTH(UNHEX('80')) = 1", await q("SELECT LENGTH(UNHEX('80'))") === 1, null);
98
+ ok("#108 含 0x00 的二进制串", await q("SELECT HEX(UNHEX('00FF00'))") === '00FF00', null);
99
+ ok("#108 空串往返", await q("SELECT HEX(UNHEX(''))") === '', null);
100
+ {
101
+ const allBytes = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0')).join('').toUpperCase();
102
+ ok('#108 0x00-0xFF 全字节 HEX 往返恒等',
103
+ await q(`SELECT HEX(UNHEX('${allBytes}'))`) === allBytes, null);
104
+ ok('#108 全字节 LENGTH = 256', await q(`SELECT LENGTH(UNHEX('${allBytes}'))`) === 256, null);
105
+ ok('#108 全字节 BIT_LENGTH = 2048', await q(`SELECT BIT_LENGTH(UNHEX('${allBytes}'))`) === 2048, null);
106
+ ok('#108 全字节 CHAR_LENGTH = 256', await q(`SELECT CHAR_LENGTH(UNHEX('${allBytes}'))`) === 256, null);
107
+ }
108
+ ok('#108 二进制串与文本可比较', await q("SELECT UNHEX('4142') = 'AB'") === 1, null);
109
+ ok('#108 二进制串可 CONCAT', await q("SELECT CONCAT(UNHEX('4142'), 'c')") === 'ABc', null);
110
+ ok('#108 对 HEX 结果再取长度', await q("SELECT LENGTH(HEX(UNHEX('DEADBEEF')))") === 8, null);
111
+ ok('#108 文本 HEX 仍按 UTF-8 字节', await q("SELECT HEX('中文')") === 'E4B8ADE69687', null);
112
+
65
113
  console.log(`\n${pass} passed, ${fail} failed`);
66
114
  process.exit(fail === 0 ? 0 : 1);
67
115
  })().catch(e => {
@@ -104,6 +104,25 @@ function resetCalls() { calls.length = 0; }
104
104
  ok('jsql plugin list 离线抛 JSQL_OFFLINE', perr && perr.code === 'JSQL_OFFLINE', perr && perr.code);
105
105
  ok('jsql plugin list 离线不出网', calls.length === 0, calls);
106
106
 
107
+ // ---------- 原生下载供应链策略(Issue #110)----------
108
+ // 文档曾声称「只走官方 GitHub Release 主机」,而 postinstall.js 实际会默认
109
+ // 优先走第三方代理 gh-proxy.com。这里钉死修正后的策略:不内置任何代理,
110
+ // 代理只能由用户显式 JSQL_GHPROXY 指定,且文档与代码口径一致。
111
+ {
112
+ const post = fs.readFileSync(path.join(ROOT, 'postinstall.js'), 'utf8');
113
+ ok('#110 postinstall 不内置 DEFAULT_PROXIES', !/DEFAULT_PROXIES/.test(post), null);
114
+ ok('#110 postinstall 不内置 gh-proxy.com', !/gh-proxy\.com/.test(post), null);
115
+ ok('#110 postinstall 仍支持显式 JSQL_GHPROXY', /JSQL_GHPROXY/.test(post), null);
116
+ ok('#110 postinstall 仍强制 SHA256 校验(fail closed)',
117
+ /expectedHashFromManifest/.test(post) && /refusing to download an unverified native module/.test(post), null);
118
+ const readme = fs.readFileSync(path.join(ROOT, 'README.md'), 'utf8');
119
+ const security = fs.readFileSync(path.join(ROOT, 'SECURITY.md'), 'utf8');
120
+ ok('#110 README 说明 JSQL_GHPROXY 与 token 作用域',
121
+ /JSQL_GHPROXY/.test(readme) && /JSQL_GHPROXY_TOKEN/.test(readme), null);
122
+ ok('#110 SECURITY 不再声称「仅官方主机」而无代理例外',
123
+ /JSQL_GHPROXY/.test(security) && /No third-party mirror is built/i.test(security), null);
124
+ }
125
+
107
126
  // ---------- 恢复 ----------
108
127
  process.env.JSQL_OFFLINE = saved === undefined ? '' : saved;
109
128
  if (saved === undefined) delete process.env.JSQL_OFFLINE;