jsql-neo 6.5.16 → 6.5.20

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.
@@ -461,7 +461,7 @@ class MongoServer {
461
461
  await engine.insert(coll, merged);
462
462
  value = doc.new ? { ...merged } : null;
463
463
  }
464
- return { ok: 1, value: value ? this._publicDoc(value) : value, lastErrorObject: { n: found ? 1 : (doc.upsert ? 1 : 0), updatedExisting: !!found && !doc.remove } };
464
+ return { ok: 1, value, lastErrorObject: { n: found ? 1 : (doc.upsert ? 1 : 0), updatedExisting: !!found && !doc.remove } };
465
465
  }
466
466
  case 'distinct': {
467
467
  const coll = String(doc.distinct);
@@ -496,7 +496,7 @@ class MongoServer {
496
496
  if (skip > 0) rows = rows.slice(skip);
497
497
  const limit = typeof doc.limit === 'number' ? doc.limit : 0;
498
498
  if (limit > 0) rows = rows.slice(0, limit);
499
- return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows.map(r => this._publicDoc(r)) } };
499
+ return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows } };
500
500
  }
501
501
  case 'getMore':
502
502
  return { ok: 1, cursor: { id: { $long: 0 }, ns: String(doc.collection || ''), nextBatch: [] } };
@@ -673,7 +673,7 @@ class MongoServer {
673
673
  rows = out;
674
674
  }
675
675
  }
676
- return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows.map(r => this._publicDoc(r)) } };
676
+ return { ok: 1, cursor: { id: { $long: 0 }, ns: `${this._dbOf(doc)}.${coll}`, firstBatch: rows } };
677
677
  }
678
678
  default:
679
679
  return { ok: 1, n: 0 };
@@ -684,23 +684,6 @@ class MongoServer {
684
684
  return row._id != null ? row._id : row.id;
685
685
  }
686
686
 
687
- /**
688
- * 剥离 JSQL 内部字段后再返回给 Mongo 客户端。
689
- *
690
- * _deleted(软删除标记)、_rid(内部行号)、_score(全文检索得分)
691
- * 都只属于引擎内部实现,泄漏到文档里会污染用户数据、影响后续 $set/$unset
692
- * 以及客户端 schema 推断。
693
- */
694
- _publicDoc(row) {
695
- if (!row || typeof row !== 'object') return row;
696
- if (row._deleted === undefined && row._rid === undefined && row._score === undefined) return row;
697
- const out = { ...row };
698
- delete out._deleted;
699
- delete out._rid;
700
- delete out._score;
701
- return out;
702
- }
703
-
704
687
  async _matching(engine, coll, filter) {
705
688
  const rows = await engine.find(coll, {});
706
689
  return rows.filter((r) => this._match(r, filter || {}));
@@ -146,8 +146,8 @@ class Connection extends EventEmitter {
146
146
  const r = await executeSQL(engine, finalSql, {
147
147
  safety: this.safety,
148
148
  allowComments: this.allowComments,
149
- allowWrites: true,
150
149
  maxStatements: this.config.multipleStatements ? null : 1,
150
+ allowStacked: this.config.multipleStatements === true,
151
151
  });
152
152
  const results = Array.isArray(r) ? r : [r];
153
153
  const last = results[results.length - 1];
@@ -578,9 +578,8 @@ class MysqlConnection {
578
578
  case 0x19: this._handleStmtClose(body); break; // COM_STMT_CLOSE
579
579
  case 0x1a: this._send(okPacket()); break; // COM_STMT_RESET
580
580
  case 0x0a: { // COM_PROCESS_INFO
581
- // _handlePacket 已把 this.sequence 设为 cmdSeq+1,_send 会在写入后自增。
582
- // 此前这里额外自增一次,导致首个响应包序列号比请求大 2(跳号),
583
- // 严格校验序列号的客户端会判定协议错误并断开。
581
+ const seq = this.sequence;
582
+ this.sequence = (this.sequence + 1) & 0xff;
584
583
  this._send(encodeLenenc(1));
585
584
  this._send(columnDefinition({ name: 'Id', type: MYSQL_TYPE_LONG }));
586
585
  this._send(eofPacket());
@@ -957,8 +956,6 @@ class MysqlConnection {
957
956
  const r = await executeSQL(engine, sqlToRun, {
958
957
  allowComments: this.server.allowComments,
959
958
  safety: this.server.safety,
960
- // 协议服务器本身就是数据库端:客户端发来的 DROP/DELETE/UPDATE 必须执行。
961
- allowWrites: true,
962
959
  maxStatements: 1,
963
960
  session: this.session,
964
961
  });
@@ -1,6 +1,7 @@
1
1
  const path = require('path');
2
2
  const os = require('os');
3
3
  const { executeSQL } = require('./sql');
4
+ const { JSQL_Error, ErrorCodes } = require('./errors');
4
5
  const {
5
6
  createHookRegistry, applyPlugin, buildPluginContext, registerHook,
6
7
  runHooks, getPluginApis
@@ -66,6 +67,25 @@ function safeParse(str, fallback) {
66
67
  try { return JSON.parse(str); } catch (e) { return fallback; }
67
68
  }
68
69
 
70
+ /**
71
+ * 把原生层返回的 `"ER_XXX: message"` 还原成纯 JS 引擎同样的异常:
72
+ * JSQL_Error、message 一致、code 为 MySQL 错误码(1062)、并带上 codeKey/errno。
73
+ * 没有错误码前缀的消息原样透传,普通错误行为不变。
74
+ */
75
+ function nativeError(raw, fallback) {
76
+ const text = String(raw || fallback || 'native error');
77
+ const m = /^(ER_[A-Z0-9_]+):\s*([\s\S]*)$/.exec(text);
78
+ if (!m) return new Error(text);
79
+ const codeKey = m[1];
80
+ const t = ErrorCodes[codeKey];
81
+ const e = new JSQL_Error(t ? t.code : codeKey, m[2] || text, { codeKey });
82
+ e.codeKey = codeKey;
83
+ if (t) e.errno = t.code;
84
+ return e;
85
+ }
86
+
87
+ function throwNative(raw, fallback) { throw nativeError(raw, fallback); }
88
+
69
89
  /**
70
90
  * 深度相等比较两个行对象(用于无主键表行匹配)。
71
91
  * 剥离内部字段(_rid, _deleted)后逐字段比较。
@@ -628,7 +648,7 @@ class JSQL {
628
648
  } else {
629
649
  r = safeParse(this._n('jsqlInsertBuf', table, encodeBatch(rows)));
630
650
  }
631
- if (r && r.error) throw new Error(r.error);
651
+ if (r && r.error) throwNative(r.error);
632
652
  if (r && Array.isArray(r) && !this._pkAutoIncrement(table)) {
633
653
  const pk = this._pkOf(table);
634
654
  if (pk) {
@@ -734,7 +754,7 @@ class JSQL {
734
754
  for (let i = 0; i < arr.length; i += this._flushThreshold) {
735
755
  const chunk = arr.slice(i, i + this._flushThreshold);
736
756
  const r = await this._insertBatch(table, chunk);
737
- if (r && r.error) throw new Error(r.error);
757
+ if (r && r.error) throwNative(r.error);
738
758
  if (!result) result = r;
739
759
  }
740
760
  this._emit('insert', { table, count: arr.length, ids: result });
@@ -77,21 +77,8 @@ function matchOperator(value, op, expected) {
77
77
  case '$in': return Array.isArray(expected) && expected.some(e => looseEqual(value, e));
78
78
  case '$nin': return Array.isArray(expected) && !expected.some(e => looseEqual(value, e));
79
79
  case '$exists': return expected ? value !== undefined : value === undefined;
80
- case '$regex': {
81
- if (typeof value !== 'string') return false;
82
- // expected 允许两种写法:/re/flags(RegExp),或 MySQL 风格的字符串模式。
83
- // 此前直接读 expected.source:字符串没有 source → new RegExp(undefined)
84
- // 等价于 /(?:)/,静默匹配所有文档。
85
- let re;
86
- if (expected instanceof RegExp) {
87
- re = expected;
88
- } else if (typeof expected === 'string') {
89
- try { re = new RegExp(expected); } catch { return false; }
90
- } else {
91
- return false;
92
- }
93
- return re.test(value);
94
- }
80
+ case '$regex':
81
+ return typeof value === 'string' && new RegExp(expected.source, expected.flags || 'i').test(value);
95
82
  case '$size': return Array.isArray(value) && value.length === expected;
96
83
  default: return false;
97
84
  }
package/lib/pg_server.js CHANGED
@@ -792,10 +792,7 @@ class PgServer {
792
792
  if (user !== this.options.user) return undefined;
793
793
  return this.options.password || '';
794
794
  }
795
- // 未配置任何认证信息:默认拒绝连接(返回 undefined → 认证失败)。
796
- // 此前返回 '' 会被 _startAuth 当成 trust,导致任意用户名 + 空密码即可连入,
797
- // 与 MysqlServer 的默认拒绝策略保持一致。仅显式 noAuth: true 才放行。
798
- return undefined;
795
+ return '';
799
796
  }
800
797
 
801
798
  _canAccessDb(user, dbName) {
package/lib/plugin.js CHANGED
@@ -104,16 +104,10 @@ function definePlugin(name, def = {}) {
104
104
  }
105
105
 
106
106
  /** 登记单个钩子;未知钩子名会自动创建,便于插件自定义扩展点 */
107
- function registerHook(engine, hook, fn, priority = 0) {
107
+ function registerHook(engine, hook, fn) {
108
108
  if (typeof fn !== 'function') return engine;
109
109
  if (!engine._hooks) engine._hooks = createHookRegistry();
110
110
  if (!engine._hooks[hook]) engine._hooks[hook] = [];
111
- // 记录优先级:即使插件是后加载的,也应按 priority 决定执行顺序,
112
- // 而不是简单追加在已有钩子之后(此前 sort(_plugins) 只排插件列表,
113
- // 钩子链仍按注册顺序执行,导致 priority 实际无效)。
114
- try {
115
- Object.defineProperty(fn, '__jsqlHookPriority', { value: priority, configurable: true, writable: true });
116
- } catch (e) { /* 冻结函数等无法打标,按 0 处理 */ }
117
111
  engine._hooks[hook].push(fn);
118
112
  return engine;
119
113
  }
@@ -121,10 +115,9 @@ function registerHook(engine, hook, fn, priority = 0) {
121
115
  /** 把一个插件声明的所有钩子登记到引擎上 */
122
116
  function registerHooks(engine, plugin) {
123
117
  if (!plugin || !plugin.hooks) return engine;
124
- const priority = plugin.priority || 0;
125
118
  for (const [hook, val] of Object.entries(plugin.hooks)) {
126
119
  const fns = Array.isArray(val) ? val : [val];
127
- for (const fn of fns) registerHook(engine, hook, fn, priority);
120
+ for (const fn of fns) registerHook(engine, hook, fn);
128
121
  }
129
122
  return engine;
130
123
  }
@@ -260,11 +253,7 @@ function runHooks(engine, hookName, args) {
260
253
 
261
254
  return (async () => {
262
255
  try {
263
- // priority 大的先执行;同 priority 保持注册顺序(sort 在现代引擎是稳定的)
264
- const ordered = hooks.length > 1
265
- ? hooks.slice().sort((a, b) => (b.__jsqlHookPriority || 0) - (a.__jsqlHookPriority || 0))
266
- : hooks;
267
- for (const fn of ordered) {
256
+ for (const fn of hooks) {
268
257
  const result = await fn(...args);
269
258
  if (result === false) return false;
270
259
  }