@eum-agent/skill-management 1.1.0 → 1.2.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.
@@ -1,31 +1,21 @@
1
- import { chmod, mkdir, writeFile } from "node:fs/promises";
2
1
  import { join, normalize, sep } from "node:path";
3
- import { unzipSync } from "fflate";
2
+ import { chmod, mkdir, writeFile } from "node:fs/promises";
3
+ import { strFromU8, unzipSync } from "fflate";
4
+ //#region src/install/limits.ts
5
+ const SOURCE_BYTE_LIMIT = 209715200;
6
+ //#endregion
4
7
  //#region src/install/zip.ts
5
- /**
6
- * A zip archive, unpacked into a directory — refusing any entry that would
7
- * land outside it and any symbolic link, and keeping each file's executable
8
- * bit.
9
- *
10
- * `fflate` inflates; the central directory is read here, because it is the
11
- * only place a zip keeps what an entry *is* (a link, a directory, a file and
12
- * its mode) and `fflate` does not hand that over. The archive comment is read
13
- * the same way: GitHub puts the commit an archive was cut from there.
14
- */
15
8
  const EOCD = 101010256;
16
9
  const CENTRAL = 33639248;
17
10
  function view(bytes) {
18
11
  return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
19
12
  }
20
- /** Where the end-of-central-directory record starts, searching back over
21
- * the longest comment a zip can carry. */
22
13
  function endRecord(bytes) {
23
14
  const data = view(bytes);
24
15
  const floor = Math.max(0, bytes.byteLength - 22 - 65535);
25
16
  for (let at = bytes.byteLength - 22; at >= floor; at -= 1) if (data.getUint32(at, true) === EOCD) return at;
26
- throw new InstallError("这个包无法解压");
17
+ throw new InstallError("\u8fd9\u4e2a\u5305\u65e0\u6cd5\u89e3\u538b");
27
18
  }
28
- /** The archive comment, as text. */
29
19
  function zipComment(bytes) {
30
20
  const at = endRecord(bytes);
31
21
  const length = view(bytes).getUint16(at + 20, true);
@@ -39,17 +29,20 @@ function entries(bytes) {
39
29
  const found = [];
40
30
  const decoder = new TextDecoder();
41
31
  for (let index = 0; index < count; index += 1) {
42
- if (at + 46 > bytes.byteLength || data.getUint32(at, true) !== CENTRAL) throw new InstallError("这个包无法解压");
32
+ if (at + 46 > bytes.byteLength || data.getUint32(at, true) !== CENTRAL) throw new InstallError("\u8fd9\u4e2a\u5305\u65e0\u6cd5\u89e3\u538b");
43
33
  const madeBy = data.getUint16(at + 4, true) >> 8;
34
+ const utf8 = (data.getUint16(at + 8, true) & 2048) !== 0;
44
35
  const nameLength = data.getUint16(at + 28, true);
45
36
  const extraLength = data.getUint16(at + 30, true);
46
37
  const commentLength = data.getUint16(at + 32, true);
47
38
  const external = data.getUint32(at + 38, true);
48
- const name = decoder.decode(bytes.subarray(at + 46, at + 46 + nameLength));
39
+ const raw = bytes.subarray(at + 46, at + 46 + nameLength);
40
+ const name = decoder.decode(raw);
49
41
  const mode = madeBy === 3 ? external >>> 16 : void 0;
50
42
  const type = mode === void 0 ? 0 : mode & 61440;
51
43
  found.push({
52
44
  name,
45
+ key: utf8 ? name : strFromU8(raw, true),
53
46
  bytes: data.getUint32(at + 24, true),
54
47
  directory: name.endsWith("/") || type === 16384 || (external & 16) !== 0,
55
48
  link: type === 40960,
@@ -59,8 +52,6 @@ function entries(bytes) {
59
52
  }
60
53
  return found;
61
54
  }
62
- /** The path an entry lands at, relative, or `undefined` when it would land
63
- * anywhere but inside the target. */
64
55
  function safeRelative(name) {
65
56
  if (name.includes("\\") || name.startsWith("/") || /^[A-Za-z]:/.test(name)) return void 0;
66
57
  const clean = normalize(name.replace(/\/+$/, ""));
@@ -68,13 +59,12 @@ function safeRelative(name) {
68
59
  if (clean.split(sep).some((part) => part === "..")) return void 0;
69
60
  return clean;
70
61
  }
71
- /** Checks the complete archive before inflating; no physical file IO or publication occurs. */
72
- function unpackZip(bytes, maxExpandedBytes = 20971520) {
62
+ function unpackZip(bytes, maxExpandedBytes = SOURCE_BYTE_LIMIT) {
73
63
  const list = entries(bytes);
74
64
  if (list.length > 1e4 || !Number.isSafeInteger(maxExpandedBytes) || maxExpandedBytes < 1 || list.reduce((sum, entry) => sum + entry.bytes, 0) > maxExpandedBytes) throw new InstallError("read-limit: expanded archive exceeds its source byte budget");
75
65
  for (const entry of list) {
76
- if (entry.link) throw new InstallError(`包里有符号链接:${entry.name}`);
77
- if (safeRelative(entry.name) === void 0) throw new InstallError(`包里有指向包外的路径:${entry.name}`);
66
+ if (entry.link) throw new InstallError(`\u5305\u91cc\u6709\u7b26\u53f7\u94fe\u63a5\uff1a${entry.name}`);
67
+ if (safeRelative(entry.name) === void 0) throw new InstallError(`\u5305\u91cc\u6709\u6307\u5411\u5305\u5916\u7684\u8def\u5f84\uff1a${entry.name}`);
78
68
  }
79
69
  if (new Set(list.map((entry) => entry.name)).size !== list.length) throw new InstallError("unsafe-entry: duplicate archive names");
80
70
  const tops = new Set(list.map((entry) => entry.name.split("/")[0]));
@@ -90,14 +80,14 @@ function unpackZip(bytes, maxExpandedBytes = 20971520) {
90
80
  } });
91
81
  } catch (error) {
92
82
  if (error instanceof InstallError) throw error;
93
- throw new InstallError("这个包无法解压");
83
+ throw new InstallError("\u8fd9\u4e2a\u5305\u65e0\u6cd5\u89e3\u538b");
94
84
  }
95
85
  const prepared = [];
96
86
  for (const entry of list) {
97
87
  const relative = safeRelative(entry.name.slice(strip.length));
98
88
  if (relative === void 0) throw new InstallError(`unsafe-entry: archive path escapes: ${entry.name}`);
99
89
  if (!relative) continue;
100
- const content = entry.directory ? /* @__PURE__ */ new Uint8Array() : files[entry.name];
90
+ const content = entry.directory ? /* @__PURE__ */ new Uint8Array() : files[entry.key];
101
91
  if (!content || content.byteLength !== (entry.directory ? 0 : entry.bytes)) throw new InstallError("read-limit: archive body does not match its complete central manifest");
102
92
  prepared.push({
103
93
  path: relative.split(sep).join("/"),
@@ -111,9 +101,8 @@ function unpackZip(bytes, maxExpandedBytes = 20971520) {
111
101
  ...strip ? { top: strip.slice(0, -1) } : {}
112
102
  };
113
103
  }
114
- /** Resolves only after the worker actually exits; aborted inflation never publishes a late body. */
115
104
  function createArchiveUnpacker(background) {
116
- return async (bytes, maximum = 20971520, signal = new AbortController().signal) => {
105
+ return async (bytes, maximum = SOURCE_BYTE_LIMIT, signal = new AbortController().signal) => {
117
106
  signal.throwIfAborted();
118
107
  try {
119
108
  const result = await background.execute({
@@ -132,7 +121,6 @@ function createArchiveUnpacker(background) {
132
121
  }
133
122
  };
134
123
  }
135
- /** Publishes checked bounded entries asynchronously into the caller-owned scratch directory. */
136
124
  async function unzipInto(bytes, target, maxExpandedBytes, unpack, signal) {
137
125
  const prepared = await unpack(bytes, maxExpandedBytes, signal);
138
126
  for (const entry of prepared.entries) {
@@ -151,20 +139,14 @@ async function unzipInto(bytes, target, maxExpandedBytes, unpack, signal) {
151
139
  //#endregion
152
140
  //#region src/install/http.ts
153
141
  const NETWORK_SERVICE = "network";
154
- /** A failure whose message is fit to show a person as it is. */
155
142
  var InstallError = class extends Error {
156
143
  name = "InstallError";
157
- /** Validation/download refusal has not published an installed skill. */
158
144
  confirmedNotCommitted = true;
159
- /** Failed physical release clears this flag before owner settlement. */
160
145
  cleanupComplete = true;
161
146
  };
162
147
  const USER_AGENT = "yaoguang-skill-install";
163
- /** How long a search may take. */
164
148
  const SEARCH_TIMEOUT_MS = 1e4;
165
- /** How long a download may go without a new byte. */
166
149
  const IDLE_TIMEOUT_MS = 3e4;
167
- /** Seconds until a rate limit lifts, from whichever header says so. */
168
150
  function retryAfter(headers) {
169
151
  for (const name of ["retry-after", "ratelimit-reset"]) {
170
152
  const value = Number(headers.get(name));
@@ -176,21 +158,20 @@ function retryAfter(headers) {
176
158
  if (seconds > 0) return seconds;
177
159
  }
178
160
  }
179
- /** The sentence for a response that is not a success. */
180
- function failureOf(label, response, what = "这个技能") {
161
+ function failureOf(label, response, what = "\u8fd9\u4e2a\u6280\u80fd") {
181
162
  const status = response.status;
182
- if (status === 404) return new InstallError(`找不到${what}(${label})`);
163
+ if (status === 404) return new InstallError(`\u627e\u4e0d\u5230${what}\uff08${label}\uff09`);
183
164
  if (status === 429) {
184
165
  const wait = retryAfter(response.headers);
185
- return new InstallError(wait === void 0 ? `${label} 限流,请稍后重试` : `${label} 限流,约 ${wait} 秒后可重试`);
166
+ return new InstallError(wait === void 0 ? `${label} \u9650\u6d41\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5` : `${label} \u9650\u6d41\uff0c\u7ea6 ${wait} \u79d2\u540e\u53ef\u91cd\u8bd5`);
186
167
  }
187
- if (status === 401 || status === 403) return new InstallError("这个技能需要登录才能下载,本版本暂不支持");
188
- return new InstallError(`${label} 返回了错误(HTTP ${status})`);
168
+ if (status === 401 || status === 403) return new InstallError("\u8fd9\u4e2a\u6280\u80fd\u9700\u8981\u767b\u5f55\u624d\u80fd\u4e0b\u8f7d\uff0c\u672c\u7248\u672c\u6682\u4e0d\u652f\u6301");
169
+ return new InstallError(`${label} \u8fd4\u56de\u4e86\u9519\u8bef\uff08HTTP ${status}\uff09`);
189
170
  }
190
171
  function unreachable(label, error) {
191
172
  if (error instanceof InstallError) return error;
192
- if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) return new InstallError(`${label} 响应超时`);
193
- return new InstallError(`连不上 ${label},请检查网络或代理`);
173
+ if (error instanceof Error && (error.name === "TimeoutError" || error.name === "AbortError")) return new InstallError(`${label} \u54cd\u5e94\u8d85\u65f6`);
174
+ return new InstallError(`\u8fde\u4e0d\u4e0a ${label}\uff0c\u8bf7\u68c0\u67e5\u7f51\u7edc\u6216\u4ee3\u7406`);
194
175
  }
195
176
  function createHttp(fetch, background) {
196
177
  const headers = { "user-agent": USER_AGENT };
@@ -206,7 +187,7 @@ function createHttp(fetch, background) {
206
187
  try {
207
188
  return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(result.body));
208
189
  } catch {
209
- throw new InstallError(`${label} 返回的内容无法识别`);
190
+ throw new InstallError(`${label} \u8fd4\u56de\u7684\u5185\u5bb9\u65e0\u6cd5\u8bc6\u522b`);
210
191
  }
211
192
  },
212
193
  async bytes(url, label, options = {}) {
@@ -232,8 +213,8 @@ function createHttp(fetch, background) {
232
213
  throw unreachable(label, error);
233
214
  }
234
215
  if (!response.ok && !(options.pass ?? []).includes(response.status)) throw failureOf(label, response, options.what);
235
- const maximum = options.maxBytes ?? 20971520;
236
- if (!Number.isSafeInteger(maximum) || maximum < 1 || maximum > 20971520) {
216
+ const maximum = options.maxBytes ?? 209715200;
217
+ if (!Number.isSafeInteger(maximum) || maximum < 1 || maximum > 209715200) {
237
218
  await response.body?.cancel();
238
219
  throw new InstallError("read-limit: invalid source byte budget");
239
220
  }
@@ -301,4 +282,4 @@ function createHttp(fetch, background) {
301
282
  return http;
302
283
  }
303
284
  //#endregion
304
- export { unpackZip as a, failureOf as i, NETWORK_SERVICE as n, unzipInto as o, createHttp as r, zipComment as s, InstallError as t };
285
+ export { unpackZip as a, SOURCE_BYTE_LIMIT as c, failureOf as i, NETWORK_SERVICE as n, unzipInto as o, createHttp as r, zipComment as s, InstallError as t };