proxylang 0.1.3 → 0.1.5

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
@@ -36,7 +36,7 @@ npx proxylang --help
36
36
  git clone https://github.com/CorrectRoadH/proxylang.git
37
37
  cd proxylang
38
38
  vp install --frozen-lockfile
39
- vp pack
39
+ vp run build
40
40
 
41
41
  # 检查现成的四客户端示例,不读取密码
42
42
  vp exec node dist/cli.js check examples/clients.ts
@@ -45,6 +45,8 @@ vp exec node dist/cli.js check examples/clients.ts
45
45
  DEMO_PASSWORD=example-only vp exec node dist/cli.js build examples/clients.ts --out out/clients
46
46
  ```
47
47
 
48
+ 仓库开发统一使用 Vite+:`vp run dev` 监听构建,`vp run check` 检查源码,`vp run verify` 完成源码与隔离安装包验收。`vp add` / `vp remove` 管理依赖,提交更新后的 `package-lock.json`。
49
+
48
50
  [四客户端示例](examples/clients.ts)展示完整配置;下面从规则开始,逐步写出自己的版本。
49
51
 
50
52
  ## Usage
@@ -109,7 +111,23 @@ const routing = sequence(
109
111
 
110
112
  `slot` 是普通 TypeScript 函数,表示模板的出口参数;`route` 引用具体节点或组。编辑器会检查字面量槽位名称、缺失或多余的绑定,并沿嵌套块与模板保留提示。`defineConfig` 也会检查目标与案例引用的程序名称。动态输入仍由 `check` 验证。
111
113
 
112
- ### 2. 一个出口,显式选择平台策略
114
+ ### 2. 一个出口,共享算法意图
115
+
116
+ 自动择优可以只声明一次,让各平台选择实现:
117
+
118
+ ```ts
119
+ const automatic = strategy({
120
+ id: "automatic",
121
+ members: ["edge"],
122
+ algorithm: group.auto(),
123
+ });
124
+ ```
125
+
126
+ Surge 使用 Smart,sing-box 与 Mihomo 使用 URLTest。`auto` 允许评分和切换时机不同;`check` 会显示声明的算法与实际实现。默认 URLTest 使用 `https://www.gstatic.com/generate_204`、180 秒周期和 50 毫秒容差。Stash 当前不支持 `auto`。
127
+
128
+ 要求固定机制时使用 `algorithm: group.select()` 或 `algorithm: group.urltest({ url: "https://example.com/probe" })`;目标不能满足就报错。也可以给 `auto` 添加 `implementations`,明确覆写某个平台的实现。
129
+
130
+ 原有显式平台配置仍然支持:
113
131
 
114
132
  规则引用 `adaptive`,节点成员与出口名称可以共用,具体选路算法由每个平台独立声明。
115
133
 
@@ -192,6 +210,17 @@ PROXY_PASSWORD=example-only npx proxylang build proxy.config.ts --out out
192
210
 
193
211
  默认输出是包含节点、策略组和规则的 **fragment**,需要与客户端的入口、DNS 等配置组合。可选 `standalone` 提供本机显式代理配置;生成文件不等于部署或系统接管。
194
212
 
213
+ ### 从文件解释请求
214
+
215
+ 把请求元数据保存为 UTF-8 JSON,避免 shell 转义:
216
+
217
+ ```sh
218
+ npx proxylang explain proxy.config.ts --target backup --request-file request.json
219
+ npx proxylang build --help
220
+ ```
221
+
222
+ `--request` 与 `--request-file` 二选一。请求文件和输出目录相对调用目录,配置内的相对 import 仍相对配置文件。构建失败会给出受控原因与恢复建议;锁占用时不会自动抢锁。
223
+
195
224
  ## 组合规则集与原生能力
196
225
 
197
226
  规则集是可以命名、复用的匹配条件。`any`、`all`、`not` 可以组合本地条件、其它规则集,以及客户端原生能力;动作由使用处绑定。
package/dist/cli.js CHANGED
@@ -1,7 +1,9 @@
1
1
  #!/usr/bin/env node
2
- import { R as requestSchema, a as explain, i as compile, l as targetCatalog, p as StaticRuleSetUnknownError, t as buildToDirectory } from "./host-CB08cTs0.js";
2
+ import { R as requestSchema, a as explain, i as compile, l as targetCatalog, p as StaticRuleSetUnknownError, t as buildToDirectory } from "./host-Bea3np6i.js";
3
+ import { open } from "node:fs/promises";
3
4
  import { resolve } from "node:path";
4
5
  import { pathToFileURL } from "node:url";
6
+ import { constants } from "node:fs";
5
7
  import { tsImport } from "tsx/esm/api";
6
8
  //#region src/cli.ts
7
9
  const print = (value) => process.stdout.write(JSON.stringify(value, null, 2) + "\n");
@@ -12,6 +14,32 @@ const failure = (code) => {
12
14
  });
13
15
  return 1;
14
16
  };
17
+ const usages = {
18
+ targets: ["proxylang targets"],
19
+ check: ["proxylang check config.ts"],
20
+ explain: ["proxylang explain config.ts --target ID --request JSON", "proxylang explain config.ts --target ID --request-file FILE"],
21
+ build: ["proxylang build config.ts --out DIRECTORY"]
22
+ };
23
+ async function readRequestFile(path) {
24
+ const limit = 1048576;
25
+ const file = await open(resolve(path), constants.O_RDONLY | constants.O_NONBLOCK);
26
+ try {
27
+ const stat = await file.stat();
28
+ if (!stat.isFile()) throw new Error("REQUEST_FILE_READ_FAILED");
29
+ if (stat.size > limit) throw new Error("REQUEST_FILE_TOO_LARGE");
30
+ const buffer = Buffer.alloc(1048577);
31
+ let size = 0;
32
+ while (size < buffer.length) {
33
+ const read = await file.read(buffer, size, buffer.length - size, null);
34
+ if (!read.bytesRead) break;
35
+ size += read.bytesRead;
36
+ }
37
+ if (size > limit) throw new Error("REQUEST_FILE_TOO_LARGE");
38
+ return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, size));
39
+ } finally {
40
+ await file.close();
41
+ }
42
+ }
15
43
  function loadFailure(cause) {
16
44
  let code;
17
45
  let name;
@@ -47,14 +75,10 @@ async function main(args) {
47
75
  print(targetCatalog);
48
76
  return 0;
49
77
  }
50
- if (!command || command === "--help") {
78
+ const help = (value) => value === "--help" || value === "-h";
79
+ if (!command || help(command) && args.length === 1 || Object.hasOwn(usages, command) && help(file) && args.length === 2) {
51
80
  print({
52
- usage: [
53
- "proxylang targets",
54
- "proxylang check config.ts",
55
- "proxylang explain config.ts --target ID --request JSON",
56
- "proxylang build config.ts --out DIRECTORY"
57
- ],
81
+ usage: command && Object.hasOwn(usages, command) ? usages[command] : Object.values(usages).flat(),
58
82
  trustedConfig: true
59
83
  });
60
84
  return 0;
@@ -63,16 +87,21 @@ async function main(args) {
63
87
  "check",
64
88
  "explain",
65
89
  "build"
66
- ].includes(command) || !file) return failure("INVALID_ARGUMENTS");
90
+ ].includes(command) || !file?.trim() || help(file)) return failure("INVALID_ARGUMENTS");
67
91
  const options = /* @__PURE__ */ new Map();
68
92
  for (let i = 0; i < rest.length; i += 2) {
69
93
  const key = rest[i];
70
94
  const value = rest[i + 1];
71
- if (!key.startsWith("--") || value === void 0 || options.has(key)) return failure("INVALID_ARGUMENTS");
95
+ if (!key.startsWith("--") || !value?.trim() || options.has(key)) return failure("INVALID_ARGUMENTS");
72
96
  options.set(key, value);
73
97
  }
74
- const allowed = command === "check" ? [] : command === "build" ? ["--out"] : ["--target", "--request"];
75
- if ([...options.keys()].some((key) => !allowed.includes(key)) || allowed.some((key) => !options.has(key))) return failure("INVALID_ARGUMENTS");
98
+ const allowed = command === "check" ? [] : command === "build" ? ["--out"] : [
99
+ "--target",
100
+ "--request",
101
+ "--request-file"
102
+ ];
103
+ const required = command === "check" ? [] : command === "build" ? ["--out"] : ["--target"];
104
+ if ([...options.keys()].some((key) => !allowed.includes(key)) || required.some((key) => !options.has(key)) || command === "explain" && options.has("--request") === options.has("--request-file")) return failure("INVALID_ARGUMENTS");
76
105
  let config;
77
106
  try {
78
107
  const loaded = await tsImport(pathToFileURL(resolve(file)).href, import.meta.url);
@@ -106,9 +135,15 @@ async function main(args) {
106
135
  return 0;
107
136
  }
108
137
  if (command === "explain") {
138
+ let source = options.get("--request");
139
+ if (options.has("--request-file")) try {
140
+ source = await readRequestFile(options.get("--request-file"));
141
+ } catch (cause) {
142
+ return failure(cause instanceof Error && cause.message === "REQUEST_FILE_TOO_LARGE" ? "REQUEST_FILE_TOO_LARGE" : "REQUEST_FILE_READ_FAILED");
143
+ }
109
144
  let request;
110
145
  try {
111
- request = JSON.parse(options.get("--request"));
146
+ request = JSON.parse(source);
112
147
  } catch {
113
148
  return failure("INVALID_REQUEST");
114
149
  }