@zhushanwen/pi-permission 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/ast/loader.ts +18 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhushanwen/pi-permission",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Pi permission extension — four permission modes (yolo/auto/approve/strict) with three-layer pipeline (AST + rules + AI classifier).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/ast/loader.ts CHANGED
@@ -22,6 +22,7 @@
22
22
  import { createRequire } from "node:module";
23
23
  import { existsSync } from "node:fs";
24
24
  import { dirname, join } from "node:path";
25
+ import { fileURLToPath } from "node:url";
25
26
 
26
27
  import { Language, Parser } from "web-tree-sitter";
27
28
 
@@ -45,9 +46,25 @@ export interface WasmPaths {
45
46
  * 所以这里只定位 .js 所在目录,不硬编码 wasm 文件名(跨版本健壮)。
46
47
  *
47
48
  * 任一不存在则 throw(fail-loud,由 getBashParser 捕获后 fail-closed)。
49
+ *
50
+ * 双模式(R1,bundle 适配):builtin extension 经 esbuild bundle 成自包含 index.js 后,
51
+ * web-tree-sitter 被 inline、staged 产物无 node_modules,nodeRequire.resolve 必然失败。
52
+ * 故优先尝试 bundle 模式 —— 用 fileURLToPath(import.meta.url) 定位 index.js 所在目录,
53
+ * 查同目录的 tree-sitter-bash.wasm;命中则返回(wasm 由 bundle-extensions.mjs 拷至此)。
54
+ * 未命中则 fallback 到 nodeRequire.resolve(开发期 dev-link,源码 node_modules 完整)。
48
55
  */
49
56
  export function resolveWasmPaths(): WasmPaths {
50
- // 1. tree-sitter-bash.wasm —— grammar wasm 在包根目录
57
+ // bundle 模式:bundle 后 wasm 与 index.js 同目录(bundle-extensions.mjs 拷贝至此)。
58
+ // 查两个 wasm 都存在才返回,避免不对称 guard(bash.wasm 在但 web-tree-sitter.wasm
59
+ // 缺失时 emscripten locateFile 会失败,虽 fail-closed 但不如入口 guard 显式拦截)。
60
+ const bundleDir = dirname(fileURLToPath(import.meta.url));
61
+ const bundleBashWasm = join(bundleDir, "tree-sitter-bash.wasm");
62
+ const bundleRuntimeWasm = join(bundleDir, "web-tree-sitter.wasm");
63
+ if (existsSync(bundleBashWasm) && existsSync(bundleRuntimeWasm)) {
64
+ return { bashWasmPath: bundleBashWasm, runtimeWasmDir: bundleDir };
65
+ }
66
+
67
+ // fallback:开发期从 node_modules 解析(dev-link,源码目录完整)
51
68
  const bashPkgDir = dirname(nodeRequire.resolve("tree-sitter-bash/package.json"));
52
69
  const bashWasmPath = join(bashPkgDir, "tree-sitter-bash.wasm");
53
70
  if (!existsSync(bashWasmPath)) {
@@ -56,8 +73,6 @@ export function resolveWasmPaths(): WasmPaths {
56
73
  );
57
74
  }
58
75
 
59
- // 2. 运行时 wasm 目录 —— web-tree-sitter 的 .js / .wasm 同目录
60
- // 用 resolve("web-tree-sitter") 定位 .js 入口,取其 dirname。
61
76
  const runtimeEntry = nodeRequire.resolve("web-tree-sitter");
62
77
  const runtimeWasmDir = dirname(runtimeEntry);
63
78
  if (!existsSync(runtimeWasmDir)) {