jsql-neo 6.6.1 → 6.6.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.
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.6.1** — stable release · [github.com/vexify-org/JSQL-neo](https://github.com/vexify-org/JSQL-neo) · [jsql.vexify.top](https://jsql.vexify.top)
6
+ > **v6.6.2** — 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)
@@ -223,6 +223,26 @@ db.xqlIndex('users', 'score', 'range');
223
223
 
224
224
  ---
225
225
 
226
+ ## Network access
227
+
228
+ The **CLI startup path makes no network requests at all** — queries, scripts and
229
+ `jsql serve` are entirely local. External access is confined to commands you
230
+ invoke explicitly, and every such site carries a `NETWORK:` comment in the
231
+ source stating why it cannot be done locally:
232
+
233
+ | Command / timing | Target | Why it needs the network | How to disable |
234
+ | --- | --- | --- | --- |
235
+ | `jsql star` | `auth.vexify.top` + GitHub | OAuth authorization has to go through the server | `JSQL_OFFLINE=1` |
236
+ | `jsql star --token-check` | `api.github.com` | star status only exists on the GitHub account | `JSQL_OFFLINE=1` |
237
+ | `jsql update` | `registry.npmjs.org` | "is there a newer version" only exists in the registry | `JSQL_OFFLINE=1` |
238
+ | `jsql plugin list\|search\|info\|install` | `registry-beta.vexify.top` | the plugin catalogue only exists remotely | `JSQL_OFFLINE=1` |
239
+ | `npm install` (postinstall) | GitHub releases | the precompiled Rust `.node` is not in the npm tarball | `JSQL_NO_NATIVE_DOWNLOAD=1` or `JSQL_OFFLINE=1` |
240
+
241
+ `JSQL_OFFLINE=1` disables every external request at once; affected commands
242
+ report the reason instead of failing silently. Loopback traffic (the
243
+ `127.0.0.1` links between `jsql serve` and the client / Web UI) is not
244
+ external access and is unaffected.
245
+
226
246
  ## 📖 Documentation
227
247
 
228
248
  Detailed docs live in `doc/` — click to read.
package/bin/jsql CHANGED
@@ -319,9 +319,23 @@ const cli = (yaggs || require('./yaggs-fallback').buildYaggsFallback(_pkg))({
319
319
  })
320
320
  .command('star', 'Confirm whether you starred this repo (opens browser once)', (sub) => {
321
321
  sub.option('reset', { type: 'boolean', description: 'Forget the saved star state and re-check' });
322
+ sub.option('token-check', { type: 'boolean', description: 'Check via GitHub API using GITHUB_TOKEN/gh (no browser)' });
322
323
  }, async (argv) => {
323
324
  const sn = require('../lib/star-nag');
325
+ const { isOffline } = require('../lib/network-policy');
326
+ if (isOffline()) {
327
+ console.log('离线模式(JSQL_OFFLINE=1):无法确认 star。取消该环境变量后重试。');
328
+ return;
329
+ }
324
330
  if (argv.reset) sn.setStarred(false);
331
+ // 显式选择的联网路径:用 token 直接查 GitHub API,不开浏览器。
332
+ // 启动路径不会走到这里(nag() 已完全本地化)。
333
+ if (argv['token-check']) {
334
+ const s = sn.checkStarred();
335
+ if (s === true) { sn.setStarred(true); console.log('已确认 star ✓(via GitHub API)'); }
336
+ else console.log('未能确认(无凭据 / 未 star / 网络不通)。去掉 --token-check 可走浏览器授权。');
337
+ return;
338
+ }
325
339
  const c = sn.readConfig();
326
340
  if (c.starred) {
327
341
  console.log(`Already confirmed starred 已确认过: ${sn.configPath()}`);
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * JSQL-NEO v6.6.1 — Rust-Powered Embedded Database (WASM + HTTP)
2
+ * JSQL-NEO v6.6.2 — Rust-Powered Embedded Database (WASM + HTTP)
3
3
  *
4
4
  * @example
5
5
  * const jsql = require('jsql-neo');
package/lib/client.js CHANGED
@@ -9,6 +9,10 @@ function binPath() {
9
9
  return path.join(dir, name);
10
10
  }
11
11
 
12
+ /* LOCAL-ONLY: 这个客户端默认只连 127.0.0.1:3000 的本机 server
13
+ * (下面的 http.get / http.request 都是同机回环,不是外部网络)。
14
+ * 例外:调用方显式传 opts.host 指向别的机器时才会出网 —— 那是用户
15
+ * 主动配置的远程模式,由调用方负责。 */
12
16
  class JSQL {
13
17
  constructor(opts = {}) {
14
18
  this.host = opts.host || '127.0.0.1';
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+ /**
3
+ * 统一的网络访问策略 —— 「默认不出网,必须出网的地方写明原因」。
4
+ *
5
+ * 约定:仓库里任何会真正访问**外部网络**的位置,都要在紧邻处写一段
6
+ * `NETWORK:` 注释,四行说清楚:
7
+ *
8
+ * Why: 为什么非联网不可(本地为什么做不到)
9
+ * Trigger: 什么操作才会触发(必须能由用户主动预期)
10
+ * Offline: JSQL_OFFLINE=1 时的行为
11
+ * Scope: 访问的目标主机 / 数据
12
+ *
13
+ * 只连 127.0.0.1 / localhost 的本地回环(各 * _server.js、client.js、
14
+ * web_ui 的同源 fetch)不算外部网络,写 `LOCAL-ONLY:` 一句即可。
15
+ *
16
+ * 环境变量:
17
+ * JSQL_OFFLINE=1|true|yes|on 一键禁用所有外部网络访问。
18
+ * 已有的专用开关(JSQL_NO_NATIVE_DOWNLOAD / JSQL_NATIVE_DOWNLOAD=0 /
19
+ * JSQL_NATIVE_FORCE_REFRESH=0 / JSQL_NO_STAR_NAG=1)继续有效。
20
+ */
21
+
22
+ const TRUTHY = /^(1|true|yes|on)$/i;
23
+
24
+ /** 当前进程是否处于离线模式。 */
25
+ function isOffline() {
26
+ return TRUTHY.test(String(process.env.JSQL_OFFLINE || ''));
27
+ }
28
+
29
+ /**
30
+ * 构造一个可读的离线错误。带 code,方便调用方区分「用户主动离线」和
31
+ * 「网络真的不通」—— 这两者不该给用户同一句报错。
32
+ */
33
+ function offlineError(feature, why) {
34
+ const err = new Error(
35
+ `${feature} 需要联网(${why})。当前 JSQL_OFFLINE=1,已跳过;` +
36
+ '取消该环境变量即可恢复。'
37
+ );
38
+ err.code = 'JSQL_OFFLINE';
39
+ return err;
40
+ }
41
+
42
+ module.exports = { isOffline, offlineError, TRUTHY };
package/lib/star-nag.js CHANGED
@@ -3,23 +3,25 @@
3
3
  /**
4
4
  * CLI 启动时的 star 提示。
5
5
  *
6
- * 行为:
6
+ * 行为(启动路径 **零网络访问**):
7
7
  * 1. 已确认 star 过 → 什么都不说
8
- * 2. 距上次提示不足 N 天 → 什么都不说
9
- * 3. 否则打印一行提示,并在配置里记下时间
8
+ * 2. 否则打印一行提示,指向 `jsql star`
10
9
  *
11
- * star 检测:若环境里有 GitHub token(GITHUB_TOKEN / GH_TOKEN),
12
- * 就问 GitHub「这个用户 star 过没有」。查到了就记住,以后彻底闭嘴 ——
13
- * 用户已经 star 过了,再提就是噪音。
10
+ * 是否 star 只以本地 ~/.jsqlmr 为准。此前这里会同步 curl
11
+ * api.github.com(仅为少印一行提示,却让每条 jsql 命令都可能偷偷出网
12
+ * 并阻塞最多 ~9.5 秒),现已移除。需要联网确认时由用户显式跑
13
+ * `jsql star`(OAuth)或 `jsql star --token-check`(GitHub API)。
14
14
  *
15
15
  * 配置文件:~/.jsqlmr(JSON)。可用 JSQL_MR_PATH 覆盖。
16
- *
17
16
  * 全程不影响命令本身:任何异常都吞掉,绝不因为提示语让 CLI 起不来。
17
+ *
18
+ * 外部网络访问只存在于显式命令路径,见各处 NETWORK: 标注。
18
19
  */
19
20
 
20
21
  const fs = require('fs');
21
22
  const os = require('os');
22
23
  const path = require('path');
24
+ const { isOffline, offlineError } = require('./network-policy');
23
25
 
24
26
  const REPO = 'vexify-org/JSQL-neo';
25
27
  const URL = `https://github.com/${REPO}`;
@@ -54,7 +56,14 @@ function parseAuthResult(j) {
54
56
  return { status: st, error: r.error || (st === 'denied' ? 'authorization denied' : 'authorization failed') };
55
57
  }
56
58
 
59
+ /* NETWORK: OAuth 授权(star 确认)
60
+ * Why: 需要 GitHub 登录态与服务端换 token,本地无法完成。
61
+ * Trigger: 仅 `jsql star` 显式执行;启动路径不会走到这里。
62
+ * Offline: JSQL_OFFLINE=1 → 抛 JSQL_OFFLINE 错误,不开浏览器、不轮询。
63
+ * Scope: https://auth.vexify.top/start|result + GitHub 授权页。
64
+ */
57
65
  function startOauthCheck() {
66
+ if (isOffline()) throw offlineError('jsql star', 'OAuth 授权需要访问 GitHub');
58
67
  const crypto = require('crypto');
59
68
  const nonce = crypto.randomBytes(24).toString('base64url');
60
69
  const url = AUTH_START + '?nonce=' + nonce;
@@ -150,7 +159,15 @@ function findToken() {
150
159
  *
151
160
  * @returns {boolean|null} true=star 过 false=没 star null=无法确认
152
161
  */
162
+ /* NETWORK: GitHub star 状态查询
163
+ * Why: star 状态只存在于 GitHub 账号侧,本地无法判定。
164
+ * Trigger: 仅显式调用(jsql star --token-check / 外部脚本);
165
+ * 已从 CLI 启动路径移除,nag() 不再调用它。
166
+ * Offline: JSQL_OFFLINE=1 → 返回 null(无法确认),不发起任何请求。
167
+ * Scope: https://api.github.com/user/starred/<repo>,带 Bearer token。
168
+ */
153
169
  function checkStarred() {
170
+ if (isOffline()) return null;
154
171
  const token = findToken();
155
172
  if (!token) return null; // 没有可用凭据 → 无法确认
156
173
  // 用同步 API:CLI 启动阶段还没有 event loop 可用
@@ -199,14 +216,10 @@ function nag(opts = {}) {
199
216
  // 已确认 star 过 → 永久闭嘴。jsqlmr 就是干这个的。
200
217
  if (!opts.force && cfg.starred) return false;
201
218
 
202
- // 有凭据就先查一下,省得对已经 star 的人反复啰嗦(查不到就当没查,照印)
203
- const starred = checkStarred();
204
- if (starred) {
205
- cfg.starred = true;
206
- cfg.starredAt = Date.now();
207
- writeConfig(cfg);
208
- return false;
209
- }
219
+ // LOCAL-ONLY: 启动路径绝不联网,只按上面的本地配置判定。
220
+ // 不调用 checkStarred()(同步 curl api.github.com,最多 ~9.5s)——
221
+ // 为了少印一行提示而让每条命令偷偷出网,代价不对等。
222
+ // 用户需要确认时显式跑 `jsql star`。
210
223
 
211
224
  out.write(`\n If JSQL-NEO saved you time, a star helps others find it: ${URL}\n`);
212
225
  out.write(` 如果帮到了你,点个 star;运行 \`jsql star\` 确认 · JSQL_NO_STAR_NAG=1 关闭提示\n\n`);
@@ -249,7 +262,10 @@ function setStarred(v) {
249
262
  * parseAuthResult 一律判为未完成,继续轮询直到超时,而不是打印一段
250
263
  * login/repo 全为 undefined 的假失败信息。
251
264
  */
265
+ /* NETWORK: OAuth 结果轮询(同 startOauthCheck 的 NETWORK 标注);
266
+ * 仅由 `jsql star` 前台等待时调用,离线时直接返回 null。 */
252
267
  function waitForResult(startUrl, timeoutMs = 180000) {
268
+ if (isOffline()) return null;
253
269
  const { execFileSync } = require('child_process');
254
270
  // 注意:本模块顶部有 const URL = 'https://github.com/...' 会遮蔽全局 URL 类,
255
271
  // 必须显式从 url 模块取,否则 new URL(...) 抛 TypeError
package/lib/updater.js CHANGED
@@ -15,6 +15,7 @@
15
15
  const path = require('path');
16
16
  const https = require('https');
17
17
  const { execFileSync, spawnSync } = require('child_process');
18
+ const { isOffline } = require('./network-policy');
18
19
 
19
20
  const PKG_NAME = 'jsql-neo';
20
21
  const REGISTRY_LATEST = `https://registry.npmjs.org/${PKG_NAME}/latest`;
@@ -67,6 +68,12 @@ function isNewer(latest, current) {
67
68
 
68
69
  // ─── 网络 ───────────────────────────────────────────────────
69
70
 
71
+ /* NETWORK: 查询 npm registry 上的最新版本号
72
+ * Why: 「有没有新版本」只存在于 registry,本地包里没有这个信息。
73
+ * Trigger: 仅 `jsql update` / `jsql update --check` 显式调用。
74
+ * Offline: JSQL_OFFLINE=1 → runUpdate 直接返回离线结果,不发请求。
75
+ * Scope: https://registry.npmjs.org/jsql-neo/latest(只读,无认证)。
76
+ */
70
77
  function httpGetText(url, redirects = 0) {
71
78
  return new Promise((resolve, reject) => {
72
79
  if (redirects > 5) {
@@ -227,6 +234,13 @@ function installCommand(version) {
227
234
  */
228
235
  async function runUpdate(opts = {}) {
229
236
  const current = currentVersion();
237
+ if (isOffline()) {
238
+ return {
239
+ ok: false, current, latest: null,
240
+ error: '离线模式(JSQL_OFFLINE=1):jsql update 需要访问 npm registry',
241
+ hints: ['取消 JSQL_OFFLINE 后重试;或手动执行 npm install -g jsql-neo@latest'],
242
+ };
243
+ }
230
244
  let latestInfo;
231
245
  try {
232
246
  latestInfo = await fetchLatestVersion({ fetchImpl: opts.fetchImpl, npmView: opts.npmView });
package/lib/web_ui.js CHANGED
@@ -68,6 +68,7 @@ tr:nth-child(even) td { background: #0d1117; }
68
68
  </div>
69
69
  <script>
70
70
  let dbs = [], cur = null;
71
+ // LOCAL-ONLY: 浏览器同源请求(/api/*),不访问任何外部网络。
71
72
  const q = async (u, o) => { const r = await fetch(u, o); return r.json(); };
72
73
  function esc(s) { return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;'); }
73
74
  async function load() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsql-neo",
3
- "version": "6.6.1",
3
+ "version": "6.6.2",
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",
@@ -50,12 +50,12 @@
50
50
  "scripts": {
51
51
  "postinstall": "node postinstall.js",
52
52
  "build:native": "node scripts/build-native.js",
53
- "test": "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/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",
53
+ "test": "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",
54
54
  "test:parser": "node test/sql-parser.test.js",
55
55
  "test:btree": "node test/btree.test.js",
56
56
  "test:native": "node test/native.test.js",
57
57
  "test:regress": "node test/regress-5.1.0.js",
58
- "test:all": "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/issue-69-70.test.js && node test/issue-71-72.test.js && node test/issue-77-84.test.js",
58
+ "test:all": "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",
59
59
  "test:coverage": "c8 --all --include='lib/**' --exclude='lib/web_ui.js' --reporter=text --reporter=lcov node test/coverage.js",
60
60
  "test:orms": "node examples/orms/run-all.js",
61
61
  "test:dml": "node test/dml-constraints.test.js",
package/postinstall.js CHANGED
@@ -5,10 +5,20 @@ const path = require('path');
5
5
  const http = require('http');
6
6
  const https = require('https');
7
7
  const { detectLibc, nativeTargetSuffix, nativeNapiVersion, supportsNativeNapi, MIN_NAPI_VERSION } = require('./lib/native_target');
8
+ const { isOffline } = require('./lib/network-policy');
8
9
 
9
10
  const ROOT = __dirname;
10
11
  const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
11
12
 
13
+ /* NETWORK: 安装期拉取预编译的 native .node 二进制
14
+ * Why: Rust 编译产物不在 npm tarball 里;不下载就只能本地 cargo 编译
15
+ * (要求用户装 Rust toolchain),或直接降级到 WASM/JS 引擎。
16
+ * Trigger: 仅 `npm install` 触发的 postinstall,或手动 `node postinstall.js`。
17
+ * 运行时 / CLI 启动绝不会走到这里。
18
+ * Offline: JSQL_OFFLINE=1 或 JSQL_NO_NATIVE_DOWNLOAD=1 → 完全跳过下载;
19
+ * 有本地 .node 就用,没有则走源码编译 / WASM 降级。
20
+ * Scope: GitHub releases 的固定 tag 资源 + SHA256SUMS(强制校验)。
21
+ */
12
22
  // Server CLI binary (bin/jsql-neo-server)
13
23
  const BIN_NAME = process.platform === 'win32' ? 'jsql-neo-server.exe' : 'jsql-neo-server';
14
24
  const BP = path.join(ROOT, 'bin', BIN_NAME);
@@ -53,7 +63,8 @@ const GHPROXY_TOKEN = process.env.JSQL_GHPROXY_TOKEN || '';
53
63
  //
54
64
  // 供应链安全(issue #25):不内置任何加速站/token,代理仍需显式 JSQL_GHPROXY,
55
65
  // 且下载必须通过 SHA256SUMS 校验(除非用户显式 JSQL_ALLOW_UNVERIFIED_NATIVE=1)。
56
- const DOWNLOAD_DISABLED = process.env.JSQL_NO_NATIVE_DOWNLOAD === '1'
66
+ const DOWNLOAD_DISABLED = isOffline()
67
+ || process.env.JSQL_NO_NATIVE_DOWNLOAD === '1'
57
68
  || process.env.JSQL_NATIVE_DOWNLOAD === '0'
58
69
  || process.env.JSQL_AUTO_DOWNLOAD_NATIVE === '0';
59
70
  const FORCE_REFRESH = process.env.JSQL_NATIVE_FORCE_REFRESH !== '0';
@@ -101,7 +112,12 @@ function sameOrigin(a, b) {
101
112
  * 解析 JSQL_NATIVE_TAG=latest:跟随 GitHub `/releases/latest` 的 302 跳转,
102
113
  * 从 Location 里取出真实 tag(vX.Y.Z)。失败返回 null —— 调用方回退到当前版本 tag。
103
114
  */
115
+ /* NETWORK: 解析 JSQL_NATIVE_TAG=latest(GET GitHub /releases/latest 的 302)
116
+ * Why: "latest" 指向哪个 tag 只有远端知道。
117
+ * Trigger: 仅用户显式设置 JSQL_NATIVE_TAG=latest 时;默认用 v<pkg.version> 不发请求。
118
+ * Offline: JSQL_OFFLINE=1 → 返回 null,调用方回退到当前版本 tag。 */
104
119
  function resolveLatestTag() {
120
+ if (isOffline()) return Promise.resolve(null);
105
121
  return new Promise((resolve) => {
106
122
  const url = `https://github.com/${REPO}/releases/latest`;
107
123
  let settled = false;
package/src/cli/plugin.js CHANGED
@@ -12,6 +12,7 @@ const fs = require('fs');
12
12
  const os = require('os');
13
13
  const path = require('path');
14
14
  const { execFileSync } = require('child_process');
15
+ const { isOffline, offlineError } = require('../../lib/network-policy');
15
16
 
16
17
  const REGISTRY_BASE = 'https://registry-beta.vexify.top';
17
18
  const PLUGIN_DIR = path.join(os.homedir(), '.jsql', 'plugins');
@@ -94,8 +95,15 @@ function extractJson(text) {
94
95
  }
95
96
  }
96
97
 
98
+ /* NETWORK: 插件 registry 的元数据查询(plugins / search)
99
+ * Why: 可安装插件的目录只在远端,本地只有「已安装」列表。
100
+ * Trigger: 仅 `jsql plugin list|search|info` 显式调用。
101
+ * Offline: JSQL_OFFLINE=1 → 抛 JSQL_OFFLINE 错误,不发起 fetch。
102
+ * Scope: https://registry-beta.vexify.top/?action=...(只读)。
103
+ */
97
104
  /** 发起 registry API 请求,返回解析后的 JSON */
98
105
  async function apiRequest(action, params = {}) {
106
+ if (isOffline()) throw offlineError('jsql plugin', '插件目录在远端 registry');
99
107
  const url = new URL(REGISTRY_BASE);
100
108
  url.searchParams.set('action', action);
101
109
  for (const [k, v] of Object.entries(params)) {
@@ -151,7 +159,12 @@ async function getPluginInfo(name) {
151
159
  * 带重试的 fetch(P1 修复:install 网络间歇性失败 30-40%)
152
160
  * 3 次重试 + 指数退避(500ms, 1s, 2s)
153
161
  */
162
+ /* NETWORK: 下载插件压缩包(同 apiRequest 的 NETWORK 标注)
163
+ * Why: 包体只在远端;Trigger: 仅 `jsql plugin install <name>`;
164
+ * Offline: JSQL_OFFLINE=1 → 抛 JSQL_OFFLINE 错误;
165
+ * Scope: registry-beta.vexify.top 的 download action。 */
154
166
  async function fetchWithRetry(url, retries = 3, baseDelay = 500) {
167
+ if (isOffline()) throw offlineError('jsql plugin install', '插件包需要下载');
155
168
  for (let i = 0; i < retries; i++) {
156
169
  try {
157
170
  const res = await fetch(url.toString());
@@ -0,0 +1,120 @@
1
+ /* ---- 网络访问策略:启动路径零出网 + JSQL_OFFLINE 统一开关 ---- */
2
+ /*
3
+ * 背景:CLI 启动路径曾会同步 curl api.github.com(只为少印一行 star 提示),
4
+ * 让每条 jsql 命令都可能偷偷出网并阻塞最多 ~9.5 秒。本测试把「启动不联网」
5
+ * 钉死,并验证 JSQL_OFFLINE=1 能拦住所有显式联网入口。
6
+ *
7
+ * 全程用临时目录:JSQL_MR_PATH 指向临时路径,绝不碰真实的 ~/.jsqlmr。
8
+ */
9
+ const path = require('path');
10
+ const fs = require('fs');
11
+ const os = require('os');
12
+
13
+ const ROOT = path.join(__dirname, '..');
14
+ const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'jsql-netpolicy-'));
15
+ process.env.JSQL_MR_PATH = path.join(TMP, 'jsqlmr'); // 必须在 require 之前
16
+
17
+ const { isOffline, offlineError } = require(path.join(ROOT, 'lib', 'network-policy'));
18
+ const sn = require(path.join(ROOT, 'lib', 'star-nag'));
19
+ const updater = require(path.join(ROOT, 'lib', 'updater'));
20
+ const plugin = require(path.join(ROOT, 'src', 'cli', 'plugin'));
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
+ // 记录所有子进程调用(curl / gh / npm),用来证明「没有出网」
29
+ const cp = require('child_process');
30
+ const realExecFileSync = cp.execFileSync;
31
+ const realExecFile = cp.execFile;
32
+ const realSpawn = cp.spawn;
33
+ const calls = [];
34
+ cp.execFileSync = function (cmd, ...rest) { calls.push('execFileSync:' + cmd); return realExecFileSync.call(this, cmd, ...rest); };
35
+ cp.execFile = function (cmd, ...rest) { calls.push('execFile:' + cmd); return realExecFile.call(this, cmd, ...rest); };
36
+ cp.spawn = function (cmd, ...rest) { calls.push('spawn:' + cmd); return realSpawn.call(this, cmd, ...rest); };
37
+
38
+ function resetCalls() { calls.length = 0; }
39
+
40
+ (async () => {
41
+ // ---------- 环境变量解析 ----------
42
+ const saved = process.env.JSQL_OFFLINE;
43
+ delete process.env.JSQL_OFFLINE;
44
+ ok('默认不离线', isOffline() === false, process.env.JSQL_OFFLINE);
45
+ for (const v of ['1', 'true', 'yes', 'on', 'TRUE']) {
46
+ process.env.JSQL_OFFLINE = v;
47
+ ok('JSQL_OFFLINE=' + v + ' → 离线', isOffline() === true, v);
48
+ }
49
+ for (const v of ['0', 'false', '', 'no']) {
50
+ process.env.JSQL_OFFLINE = v;
51
+ ok('JSQL_OFFLINE=' + JSON.stringify(v) + ' → 不离线', isOffline() === false, v);
52
+ }
53
+ process.env.JSQL_OFFLINE = '1';
54
+ const oe = offlineError('jsql update', '需要访问 npm registry');
55
+ ok('offlineError 带 code=JSQL_OFFLINE', oe && oe.code === 'JSQL_OFFLINE', oe && oe.code);
56
+ ok('offlineError 文案含功能名与原因', /jsql update/.test(oe.message) && /npm registry/.test(oe.message), oe && oe.message);
57
+
58
+ // ---------- 启动路径零出网 ----------
59
+ process.env.JSQL_OFFLINE = '';
60
+ delete process.env.JSQL_OFFLINE;
61
+ process.env.GITHUB_TOKEN = 'fake-token-should-never-be-used';
62
+ const out = [];
63
+ resetCalls();
64
+ const printed = sn.nag({ out: { write: (s) => out.push(s) } });
65
+ ok('nag() 正常打印提示', printed === true && out.join('').includes('star helps others find it'), out.join(''));
66
+ ok('nag() 不发起任何子进程网络调用', calls.length === 0, calls);
67
+ ok('nag() 不写 lastNagAt', (() => {
68
+ try { const c = sn.readConfig(); return c.lastNagAt === undefined; } catch (e) { return true; }
69
+ })(), null);
70
+ delete process.env.GITHUB_TOKEN;
71
+
72
+ // 已标记 starred 时应彻底沉默,且同样不出网
73
+ sn.setStarred(true);
74
+ const out2 = [];
75
+ resetCalls();
76
+ const printed2 = sn.nag({ out: { write: (s) => out2.push(s) } });
77
+ ok('已 starred 时不打印', printed2 === false && out2.length === 0, out2.join(''));
78
+ ok('已 starred 时不出网', calls.length === 0, calls);
79
+ sn.setStarred(false);
80
+
81
+ // ---------- 离线时各联网入口都被拦住 ----------
82
+ process.env.JSQL_OFFLINE = '1';
83
+ resetCalls();
84
+ ok('checkStarred() 离线返回 null', sn.checkStarred() === null, null);
85
+ ok('checkStarred() 离线不出网', calls.length === 0, calls);
86
+ resetCalls();
87
+ let threw = null;
88
+ try { sn.startOauthCheck(); } catch (e) { threw = e; }
89
+ ok('startOauthCheck() 离线抛 JSQL_OFFLINE', threw && threw.code === 'JSQL_OFFLINE', threw && threw.code);
90
+ ok('startOauthCheck() 离线不开浏览器', calls.length === 0, calls);
91
+ resetCalls();
92
+ ok('waitForResult() 离线返回 null', sn.waitForResult('https://auth.vexify.top/start?nonce=x') === null, null);
93
+ ok('waitForResult() 离线不轮询', calls.length === 0, calls);
94
+
95
+ resetCalls();
96
+ const up = await updater.runUpdate({ checkOnly: true });
97
+ ok('jsql update 离线返回 ok=false', up && up.ok === false, up);
98
+ ok('jsql update 离线给出可读原因', up && /JSQL_OFFLINE|离线/.test(up.error || ''), up && up.error);
99
+ ok('jsql update 离线不出网', calls.length === 0, calls);
100
+
101
+ resetCalls();
102
+ let perr = null;
103
+ try { await plugin.listPlugins({}); } catch (e) { perr = e; }
104
+ ok('jsql plugin list 离线抛 JSQL_OFFLINE', perr && perr.code === 'JSQL_OFFLINE', perr && perr.code);
105
+ ok('jsql plugin list 离线不出网', calls.length === 0, calls);
106
+
107
+ // ---------- 恢复 ----------
108
+ process.env.JSQL_OFFLINE = saved === undefined ? '' : saved;
109
+ if (saved === undefined) delete process.env.JSQL_OFFLINE;
110
+ cp.execFileSync = realExecFileSync;
111
+ cp.execFile = realExecFile;
112
+ cp.spawn = realSpawn;
113
+ try { fs.rmSync(TMP, { recursive: true, force: true }); } catch (_) {}
114
+
115
+ console.log(`\n${pass} passed, ${fail} failed`);
116
+ process.exit(fail === 0 ? 0 : 1);
117
+ })().catch(e => {
118
+ console.log('[FAIL] unexpected exception: ' + e.stack.split('\n').slice(0, 3).join('\n'));
119
+ process.exit(1);
120
+ });