@modusensus/dsh-mneme 0.7.31 → 0.8.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.
- package/README.md +65 -27
- package/lib/api.js +139 -6
- package/lib/client.js +369 -32
- package/lib/commands.js +36 -5
- package/lib/config.js +101 -14
- package/lib/dream/decisions.js +147 -14
- package/lib/dream/sleep.js +84 -34
- package/lib/dream.js +250 -60
- package/lib/embedding.js +22 -2
- package/lib/index.js +68 -37
- package/lib/inject.js +54 -13
- package/lib/lang.js +168 -0
- package/lib/local-embedder.js +23 -9
- package/lib/mirror.js +17 -14
- package/lib/reranker.js +23 -8
- package/lib/runtime/adopt.js +176 -0
- package/lib/runtime/closure.js +172 -0
- package/lib/runtime/download.js +247 -0
- package/lib/runtime/layout.js +235 -0
- package/lib/runtime/loader.js +249 -0
- package/lib/runtime/provision.js +237 -0
- package/lib/runtime/tarball.js +130 -0
- package/lib/runtime/verify.js +191 -0
- package/lib/scope.js +83 -0
- package/lib/service.js +374 -55
- package/lib/settings.js +43 -2
- package/lib/store.js +126 -13
- package/lib/summarize.js +108 -35
- package/lib/tools.js +205 -12
- package/package.json +7 -2
- package/runtime-manifest.json +596 -0
- package/scripts/build-runtime-manifest.mjs +217 -0
- package/scripts/mneme-runtime.mjs +277 -0
- package/src/api.js +139 -6
- package/src/commands.js +36 -5
- package/src/config.js +101 -14
- package/src/dream/decisions.js +147 -14
- package/src/dream/sleep.js +84 -34
- package/src/dream.js +250 -60
- package/src/embedding.js +22 -2
- package/src/index.js +68 -37
- package/src/inject.js +54 -13
- package/src/lang.js +168 -0
- package/src/local-embedder.js +23 -9
- package/src/mirror.js +17 -14
- package/src/reranker.js +23 -8
- package/src/runtime/adopt.js +176 -0
- package/src/runtime/closure.js +172 -0
- package/src/runtime/download.js +247 -0
- package/src/runtime/layout.js +235 -0
- package/src/runtime/loader.js +249 -0
- package/src/runtime/provision.js +237 -0
- package/src/runtime/tarball.js +130 -0
- package/src/runtime/verify.js +191 -0
- package/src/scope.js +83 -0
- package/src/service.js +374 -55
- package/src/settings.js +43 -2
- package/src/store.js +126 -13
- package/src/summarize.js +108 -35
- package/src/tools.js +205 -12
- package/test/api.test.js +175 -9
- package/test/client.test.js +152 -10
- package/test/commands.test.js +40 -3
- package/test/config.test.js +17 -1
- package/test/dream-candidate-hybrid.test.js +281 -0
- package/test/dream.test.js +172 -0
- package/test/embedding-ready.test.js +196 -0
- package/test/helpers/runtime-source.js +60 -0
- package/test/helpers/tar-builder.js +80 -0
- package/test/inject.test.js +130 -1
- package/test/lang.test.js +87 -0
- package/test/quality-filter.test.js +68 -0
- package/test/reasoning-effort.test.js +106 -15
- package/test/reindex-backfill.test.js +62 -0
- package/test/runtime-adopt.test.js +206 -0
- package/test/runtime-cli.test.js +89 -0
- package/test/runtime-closure.test.js +193 -0
- package/test/runtime-download.test.js +302 -0
- package/test/runtime-layout.test.js +163 -0
- package/test/runtime-loader.test.js +237 -0
- package/test/runtime-manifest.test.js +158 -0
- package/test/runtime-provision.test.js +101 -0
- package/test/runtime-tarball.test.js +82 -0
- package/test/runtime-verify.test.js +214 -0
- package/test/scope-retrieval.test.js +197 -0
- package/test/scope-storage.test.js +247 -0
- package/test/scope-strict.test.js +165 -0
- package/test/service.test.js +76 -0
- package/test/sleep-action-set.test.js +422 -0
- package/test/store.test.js +14 -0
- package/test/summarize.test.js +224 -0
- package/test/tools.test.js +30 -5
- package/test/webServer-optional.test.js +34 -1
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { applyMirror, downloadRuntime, fetchTarball, localTarballPath } from "../src/runtime/download.js";
|
|
8
|
+
import { describePayload } from "../src/runtime/layout.js";
|
|
9
|
+
import { sha512Base64 } from "../src/runtime/tarball.js";
|
|
10
|
+
import { makeTgz } from "./helpers/tar-builder.js";
|
|
11
|
+
|
|
12
|
+
// 取件层(PR-C 第三层)。整条链都在本地 http 服务器上测:重试、Range 续传、sha512 校验、
|
|
13
|
+
// 落盘布局、失败清理、路径遍历拦截 —— 不依赖外网,也不依赖某个包恰好能下载到。
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 三个包,凑够 describePayload 要求的必需包 + 入口 + 平台二进制子树。
|
|
17
|
+
*
|
|
18
|
+
* 条目路径必须**相对包根**(`package/dist/...`),这正是 npm tarball 的形态 ——
|
|
19
|
+
* 下载器会自己把包放进 `node_modules/<rel>`,条目里再写一遍包名就会变成
|
|
20
|
+
* `node_modules/<名>/<名>/...`,结构检查随即报「缺必需包」。
|
|
21
|
+
*/
|
|
22
|
+
function makePackageTgz(name) {
|
|
23
|
+
if (name === "@huggingface/transformers") {
|
|
24
|
+
return makeTgz([
|
|
25
|
+
{ path: "package.json", body: JSON.stringify({ name, version: "4.2.0" }) },
|
|
26
|
+
{ path: "dist/transformers.node.mjs", body: "export const x = 1;\n" },
|
|
27
|
+
// 塞一点体积,让「掐断一半」在续传测试里真的能切出两段。
|
|
28
|
+
{ path: "dist/padding.bin", body: "p".repeat(4096) }
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
if (name === "onnxruntime-node") {
|
|
32
|
+
return makeTgz([
|
|
33
|
+
{ path: "package.json", body: JSON.stringify({ name, version: "1.24.3" }) },
|
|
34
|
+
{ path: `bin/napi-v6/${process.platform}/${process.arch}/ort.bin`, body: "native\n" }
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
return makeTgz([{ path: "package.json", body: JSON.stringify({ name, version: "1.0.0" }) }]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function entryFor(name, rel, tgz, version = "1.0.0") {
|
|
41
|
+
return { name, version, rel, integrity: `sha512-${sha512Base64(tgz)}`, tarball: "" };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* v2 清单是**平台无关**的:包列表只有一份,下载器按 os/cpu 过滤。
|
|
46
|
+
* 夹具里的包不带 os/cpu = 所有平台都适用;要造「本平台不适用」就给包加上 os。
|
|
47
|
+
*/
|
|
48
|
+
function manifestFor(packages, { transformersVersion = "4.2.0" } = {}) {
|
|
49
|
+
return {
|
|
50
|
+
manifestVersion: 2,
|
|
51
|
+
entry: "@huggingface/transformers",
|
|
52
|
+
transformersVersion,
|
|
53
|
+
excluded: {},
|
|
54
|
+
packages
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 起一个只服务内存里那几份字节的服务器。
|
|
60
|
+
* `cutFirst` 命中时第一次请求只发一半就掐断,用来逼出重试 + 续传;它会记录是否收到过 Range。
|
|
61
|
+
*/
|
|
62
|
+
async function serve(files, { cutFirst = null } = {}) {
|
|
63
|
+
const seen = { rangeHeader: null, requests: 0 };
|
|
64
|
+
let cutDone = false;
|
|
65
|
+
const server = createServer((req, res) => {
|
|
66
|
+
seen.requests++;
|
|
67
|
+
const body = files[req.url];
|
|
68
|
+
if (body === undefined) {
|
|
69
|
+
res.writeHead(404).end();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const range = req.headers.range;
|
|
73
|
+
if (typeof range === "string") seen.rangeHeader = range;
|
|
74
|
+
const start = range ? Number(/bytes=(\d+)-/.exec(range)?.[1] ?? 0) : 0;
|
|
75
|
+
|
|
76
|
+
if (cutFirst === req.url && !cutDone && start === 0) {
|
|
77
|
+
cutDone = true;
|
|
78
|
+
res.writeHead(200, { "Content-Length": String(body.length) });
|
|
79
|
+
res.write(body.subarray(0, Math.floor(body.length / 2)));
|
|
80
|
+
// 等一小会儿再掐:立刻 destroy 会在客户端读到任何字节之前就断掉,
|
|
81
|
+
// 那样就测不到「带着已收字节续传」这条路径(测试脚手架的问题,不是被测代码的)。
|
|
82
|
+
setTimeout(() => res.destroy(), 30);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (start > 0) {
|
|
86
|
+
const rest = body.subarray(start);
|
|
87
|
+
res.writeHead(206, { "Content-Range": `bytes ${start}-${body.length - 1}/${body.length}` });
|
|
88
|
+
res.end(rest);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
res.writeHead(200, { "Content-Length": String(body.length) });
|
|
92
|
+
res.end(body);
|
|
93
|
+
});
|
|
94
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
95
|
+
const base = `http://127.0.0.1:${server.address().port}`;
|
|
96
|
+
return { server, base, seen, close: () => new Promise((resolve) => server.close(resolve)) };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function standardSet(base) {
|
|
100
|
+
const tgz = {
|
|
101
|
+
transformers: makePackageTgz("@huggingface/transformers"),
|
|
102
|
+
onnx: makePackageTgz("onnxruntime-node"),
|
|
103
|
+
sharp: makePackageTgz("sharp")
|
|
104
|
+
};
|
|
105
|
+
const packages = [
|
|
106
|
+
{ ...entryFor("@huggingface/transformers", "@huggingface/transformers", tgz.transformers, "4.2.0"), tarball: `${base}/transformers.tgz` },
|
|
107
|
+
{ ...entryFor("onnxruntime-node", "onnxruntime-node", tgz.onnx, "1.24.3"), tarball: `${base}/onnx.tgz` },
|
|
108
|
+
{ ...entryFor("sharp", "sharp", tgz.sharp), tarball: `${base}/sharp.tgz` }
|
|
109
|
+
];
|
|
110
|
+
return { files: { "/transformers.tgz": tgz.transformers, "/onnx.tgz": tgz.onnx, "/sharp.tgz": tgz.sharp }, packages };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
test("downloadRuntime:确认本机 libc 后,另一套 libc 的必需包算缺失(拉取前就失败,不装出跑不起来的运行时)", async () => {
|
|
114
|
+
const set = standardSet("http://127.0.0.1:0");
|
|
115
|
+
// 把原生依赖标成 musl:确认本机是 glibc 时它就不该进 wanted 列表,
|
|
116
|
+
// 于是「缺必需包」这条守卫生效 —— 而不是先下 200MB 再让 verify 发现在炸。
|
|
117
|
+
const packages = set.packages.map((p) =>
|
|
118
|
+
p.rel === "onnxruntime-node" ? { ...p, libc: ["musl"] } : p
|
|
119
|
+
);
|
|
120
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-libc-"));
|
|
121
|
+
const result = await downloadRuntime({
|
|
122
|
+
manifest: manifestFor(packages),
|
|
123
|
+
runtimeDir,
|
|
124
|
+
libc: "glibc",
|
|
125
|
+
fetchImpl: () => {
|
|
126
|
+
throw new Error("不该联网:过滤之后就该在拉取前失败");
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
assert.equal(result.ok, false);
|
|
130
|
+
assert.match(result.reason, /缺少必需包/);
|
|
131
|
+
assert.match(result.reason, /onnxruntime-node/);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("applyMirror:只换前缀,保留 /name/-/file.tgz;空镜像与非 registry 地址原样返回", () => {
|
|
135
|
+
const url = "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.24.3.tgz";
|
|
136
|
+
assert.equal(applyMirror(url, ""), url);
|
|
137
|
+
assert.equal(applyMirror(url, "https://npmmirror.com/mirrors/npm/"), "https://npmmirror.com/mirrors/npm/onnxruntime-node/-/onnxruntime-node-1.24.3.tgz");
|
|
138
|
+
assert.equal(applyMirror("https://example.com/x.tgz", "https://mirror/"), "https://example.com/x.tgz");
|
|
139
|
+
// 含 /-/ 但不是 registry.npmjs.org 的地址(自建 registry、或清单里已经指向镜像)必须原样返回:
|
|
140
|
+
// 这里曾经用 indexOf 的返回值直接加偏移量,域名不存在时 slice 出垃圾名字、把地址改坏。
|
|
141
|
+
const foreign = "https://my.registry.local/foo/-/foo-1.0.0.tgz";
|
|
142
|
+
assert.equal(applyMirror(foreign, "https://mirror/"), foreign);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("downloadRuntime:走网络取件、校验、按 rel 落盘,产出能通过结构检查的 payload", async () => {
|
|
146
|
+
const set = standardSet("http://127.0.0.1:0");
|
|
147
|
+
const srv = await serve(set.files);
|
|
148
|
+
const packages = set.packages.map((p) => ({ ...p, tarball: p.tarball.replace("127.0.0.1:0", `127.0.0.1:${new URL(srv.base).port}`) }));
|
|
149
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-dl-"));
|
|
150
|
+
try {
|
|
151
|
+
const result = await downloadRuntime({ manifest: manifestFor(packages), runtimeDir });
|
|
152
|
+
assert.equal(result.ok, true, result.reason);
|
|
153
|
+
assert.equal(result.totals.fromNetwork, 3);
|
|
154
|
+
assert.equal(result.totals.fromLocal, 0);
|
|
155
|
+
assert.equal(result.manifest.procedure, "downloaded");
|
|
156
|
+
// 落盘位置必须与 adopt 的镜像布局一致,否则 loader 找不到入口。
|
|
157
|
+
const report = describePayload(result.payloadDir);
|
|
158
|
+
assert.equal(report.ok, true, `missing=${report.missing} reasons=${report.reasons}`);
|
|
159
|
+
assert.equal(report.version, "4.2.0");
|
|
160
|
+
const entry = join(result.payloadDir, "node_modules", "@huggingface", "transformers", "dist", "transformers.node.mjs");
|
|
161
|
+
assert.equal(readFileSync(entry, "utf8"), "export const x = 1;\n");
|
|
162
|
+
} finally {
|
|
163
|
+
await srv.close();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test("downloadRuntime:sha512 不匹配即失败,且不留下半份 payload", async () => {
|
|
168
|
+
const set = standardSet("http://127.0.0.1:0");
|
|
169
|
+
const srv = await serve(set.files);
|
|
170
|
+
const port = new URL(srv.base).port;
|
|
171
|
+
const packages = set.packages.map((p) => ({ ...p, tarball: p.tarball.replace("127.0.0.1:0", `127.0.0.1:${port}`) }));
|
|
172
|
+
// 篡改第二个包的期望哈希:它永远对不上。
|
|
173
|
+
packages[1] = { ...packages[1], integrity: `sha512-${sha512Base64(Buffer.from("not the tarball"))}` };
|
|
174
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-dl-"));
|
|
175
|
+
try {
|
|
176
|
+
const result = await downloadRuntime({ manifest: manifestFor(packages), runtimeDir });
|
|
177
|
+
assert.equal(result.ok, false);
|
|
178
|
+
assert.match(result.reason, /sha512 与清单不符/);
|
|
179
|
+
// 半份 payload 比没有更糟:目录必须被清掉。
|
|
180
|
+
assert.equal(readdirCount(runtimeDir), 0, "校验失败后不该留下任何 payload 目录");
|
|
181
|
+
} finally {
|
|
182
|
+
await srv.close();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("downloadRuntime:首次被掐断后用 Range 续传成功(不从头再来)", async () => {
|
|
187
|
+
const set = standardSet("http://127.0.0.1:0");
|
|
188
|
+
const srv = await serve(set.files, { cutFirst: "/onnx.tgz" });
|
|
189
|
+
const port = new URL(srv.base).port;
|
|
190
|
+
const packages = set.packages.map((p) => ({ ...p, tarball: p.tarball.replace("127.0.0.1:0", `127.0.0.1:${port}`) }));
|
|
191
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-dl-"));
|
|
192
|
+
try {
|
|
193
|
+
const result = await downloadRuntime({ manifest: manifestFor(packages), runtimeDir });
|
|
194
|
+
assert.equal(result.ok, true, result.reason);
|
|
195
|
+
// 关键证据:第二次请求带了 Range —— 说明是续传而不是重来。
|
|
196
|
+
assert.match(String(srv.seen.rangeHeader), /^bytes=\d+-$/, `续传请求应当带 Range,实际=${srv.seen.rangeHeader}`);
|
|
197
|
+
assert.equal(describePayload(result.payloadDir).ok, true);
|
|
198
|
+
} finally {
|
|
199
|
+
await srv.close();
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test("downloadRuntime:归档里的 ../ 越界路径必须整次失败,且不能写出目录之外", async () => {
|
|
204
|
+
// 越界的那份伪装成 sharp(必需包之一)—— 否则会被「缺必需包」先挡住,走不到解包那一步。
|
|
205
|
+
const evil = makeTgz([
|
|
206
|
+
{ path: "package.json", body: JSON.stringify({ name: "sharp", version: "1.0.0" }) },
|
|
207
|
+
{ path: "../../escaped.txt", body: "pwned\n" }
|
|
208
|
+
]);
|
|
209
|
+
const transformers = makePackageTgz("@huggingface/transformers");
|
|
210
|
+
const onnx = makePackageTgz("onnxruntime-node");
|
|
211
|
+
const srv = await serve({ "/transformers.tgz": transformers, "/onnx.tgz": onnx, "/sharp.tgz": evil });
|
|
212
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-dl-"));
|
|
213
|
+
const manifest = manifestFor([
|
|
214
|
+
{ ...entryFor("@huggingface/transformers", "@huggingface/transformers", transformers, "4.2.0"), tarball: `${srv.base}/transformers.tgz` },
|
|
215
|
+
{ ...entryFor("onnxruntime-node", "onnxruntime-node", onnx, "1.24.3"), tarball: `${srv.base}/onnx.tgz` },
|
|
216
|
+
{ ...entryFor("sharp", "sharp", evil), tarball: `${srv.base}/sharp.tgz` }
|
|
217
|
+
]);
|
|
218
|
+
try {
|
|
219
|
+
const result = await downloadRuntime({ manifest, runtimeDir });
|
|
220
|
+
assert.equal(result.ok, false);
|
|
221
|
+
assert.match(result.reason, /越出包目录/);
|
|
222
|
+
assert.equal(existsSync(join(runtimeDir, "..", "escaped.txt")), false, "不能把外来归档的越界路径写出去");
|
|
223
|
+
assert.equal(readdirCount(runtimeDir), 0, "失败后不该留下半份 payload");
|
|
224
|
+
} finally {
|
|
225
|
+
await srv.close();
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("downloadRuntime:清单里本平台缺必需包时明确失败(不装出一份看似完整的运行时)", async () => {
|
|
230
|
+
const set = standardSet("http://127.0.0.1:0");
|
|
231
|
+
// 把所有包都标成只适用于 plan9:本机 win32-x64 过滤后一个都不剩,必须失败并说清缺什么。
|
|
232
|
+
const onlyPlan9 = set.packages.map((pkg) => ({ ...pkg, os: ["plan9"], cpu: ["mips"] }));
|
|
233
|
+
const result = await downloadRuntime({
|
|
234
|
+
manifest: manifestFor(onlyPlan9),
|
|
235
|
+
runtimeDir: mkdtempSync(join(tmpdir(), "mneme-dl-")),
|
|
236
|
+
platform: "win32",
|
|
237
|
+
arch: "x64"
|
|
238
|
+
});
|
|
239
|
+
assert.equal(result.ok, false);
|
|
240
|
+
assert.match(result.reason, /缺少必需包:/);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("downloadRuntime:本地 tarball 目录优先于网络,且目标已存在时默认拒绝", async () => {
|
|
244
|
+
const transformers = makePackageTgz("@huggingface/transformers");
|
|
245
|
+
const onnx = makePackageTgz("onnxruntime-node");
|
|
246
|
+
const sharp = makePackageTgz("sharp");
|
|
247
|
+
const localDir = mkdtempSync(join(tmpdir(), "mneme-tgz-"));
|
|
248
|
+
// 名字用 npm 约定 <basename>-<version>.tgz —— `npm pack` 出来的东西直接丢进去就能用。
|
|
249
|
+
assert.equal(localTarballPath(localDir, { name: "sharp", version: "1.0.0" }), join(localDir, "sharp-1.0.0.tgz"));
|
|
250
|
+
assert.equal(
|
|
251
|
+
localTarballPath(localDir, { name: "@huggingface/transformers", version: "4.2.0" }),
|
|
252
|
+
join(localDir, "transformers-4.2.0.tgz")
|
|
253
|
+
);
|
|
254
|
+
writeFileSync(join(localDir, "transformers-4.2.0.tgz"), transformers);
|
|
255
|
+
writeFileSync(join(localDir, "onnxruntime-node-1.24.3.tgz"), onnx);
|
|
256
|
+
writeFileSync(join(localDir, "sharp-1.0.0.tgz"), sharp);
|
|
257
|
+
|
|
258
|
+
const manifest = manifestFor([
|
|
259
|
+
entryFor("@huggingface/transformers", "@huggingface/transformers", transformers, "4.2.0"),
|
|
260
|
+
entryFor("onnxruntime-node", "onnxruntime-node", onnx, "1.24.3"),
|
|
261
|
+
entryFor("sharp", "sharp", sharp)
|
|
262
|
+
]);
|
|
263
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-dl-"));
|
|
264
|
+
// 注入一个一被调用就抛的 fetch:只要它去联网就会炸,从而证明这次确实走的是本地文件。
|
|
265
|
+
const first = await downloadRuntime({
|
|
266
|
+
manifest,
|
|
267
|
+
runtimeDir,
|
|
268
|
+
localTarballDir: localDir,
|
|
269
|
+
fetchImpl: () => { throw new Error("不该联网"); }
|
|
270
|
+
});
|
|
271
|
+
assert.equal(first.ok, true, first.reason);
|
|
272
|
+
assert.equal(first.totals.fromLocal, 3);
|
|
273
|
+
assert.equal(first.totals.fromNetwork, 0);
|
|
274
|
+
|
|
275
|
+
const again = await downloadRuntime({ manifest, runtimeDir, localTarballDir: localDir });
|
|
276
|
+
assert.equal(again.ok, false);
|
|
277
|
+
assert.match(again.reason, /目标已存在/);
|
|
278
|
+
const forced = await downloadRuntime({ manifest, runtimeDir, localTarballDir: localDir, overwrite: true });
|
|
279
|
+
assert.equal(forced.ok, true, forced.reason);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
/** 运行时根目录下现有的目录数(用来断言「失败后没留下半份 payload」)。 */
|
|
283
|
+
function readdirCount(dir) {
|
|
284
|
+
return existsSync(dir) ? readdirSync(dir).length : 0;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
test("fetchTarball:对端僵住时按 timeoutMs 失败,而不是永远挂着", async () => {
|
|
288
|
+
// 连接既不回也不断:没有超时的话,用户看到的是一个卡死的按钮、而且没有任何错误可看。
|
|
289
|
+
const server = createServer((req, res) => {
|
|
290
|
+
res.writeHead(200, { "Content-Length": "1000" });
|
|
291
|
+
res.write("x");
|
|
292
|
+
});
|
|
293
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
294
|
+
const url = `http://127.0.0.1:${server.address().port}/stall.tgz`;
|
|
295
|
+
try {
|
|
296
|
+
const started = Date.now();
|
|
297
|
+
await assert.rejects(() => fetchTarball(url, { attempts: 1, timeoutMs: 200 }), /取件失败/);
|
|
298
|
+
assert.ok(Date.now() - started < 5000, "应当因超时返回,而不是挂住");
|
|
299
|
+
} finally {
|
|
300
|
+
await new Promise((resolve) => server.close(resolve));
|
|
301
|
+
}
|
|
302
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import {
|
|
7
|
+
RUNTIME_MANIFEST,
|
|
8
|
+
TRANSFORMERS_ENTRY,
|
|
9
|
+
defaultRuntimeDir,
|
|
10
|
+
describePayload,
|
|
11
|
+
listPayloadDirs,
|
|
12
|
+
nodeModulesDir,
|
|
13
|
+
payloadDir,
|
|
14
|
+
payloadId,
|
|
15
|
+
runtimeManifestPath,
|
|
16
|
+
transformersVersionOk
|
|
17
|
+
} from "../src/runtime/layout.js";
|
|
18
|
+
|
|
19
|
+
// 运行时目录契约(issue #131 / PR-A):这里守住的是「结构检查能不能准确地
|
|
20
|
+
// 指出缺了什么」。功能验证(真 import 并跑推理)不在本文件覆盖范围,
|
|
21
|
+
// 由后续的 verify 模块负责。
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 造一份假的 payload。默认是一个「完整可用」的形态,各选项用来单独破坏一处,
|
|
25
|
+
* 这样每个用例只验证一条判据。
|
|
26
|
+
*/
|
|
27
|
+
function makePayload({
|
|
28
|
+
version = "4.2.0",
|
|
29
|
+
platform = "win32",
|
|
30
|
+
arch = "x64",
|
|
31
|
+
packages = ["@huggingface/transformers", "onnxruntime-node", "sharp"],
|
|
32
|
+
withEntry = true,
|
|
33
|
+
nodePlatform = platform,
|
|
34
|
+
nodeArch = arch,
|
|
35
|
+
withManifest = false
|
|
36
|
+
} = {}) {
|
|
37
|
+
const runtimeDir = mkdtempSync(join(tmpdir(), "mneme-runtime-"));
|
|
38
|
+
const dir = payloadDir(runtimeDir, payloadId({ version, platform, arch }));
|
|
39
|
+
const nm = nodeModulesDir(dir);
|
|
40
|
+
|
|
41
|
+
for (const name of packages) {
|
|
42
|
+
const pkgDir = join(nm, name);
|
|
43
|
+
mkdirSync(pkgDir, { recursive: true });
|
|
44
|
+
writeFileSync(
|
|
45
|
+
join(pkgDir, "package.json"),
|
|
46
|
+
JSON.stringify({ name, version: name === "@huggingface/transformers" ? version : "1.0.0" })
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (withEntry) {
|
|
51
|
+
const entry = join(dir, TRANSFORMERS_ENTRY);
|
|
52
|
+
mkdirSync(dirname(entry), { recursive: true });
|
|
53
|
+
writeFileSync(entry, "// 占位:真实运行时这里是可被 import 的 ESM 入口\n");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (nodePlatform && packages.includes("onnxruntime-node")) {
|
|
57
|
+
// 真实包结构是 bin/napi-v6/<平台>/<架构>/,夹具必须照做,否则测不到架构这一层。
|
|
58
|
+
mkdirSync(join(nm, "onnxruntime-node", "bin", "napi-v6", nodePlatform, nodeArch), { recursive: true });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (withManifest) {
|
|
62
|
+
writeFileSync(
|
|
63
|
+
runtimeManifestPath(dir),
|
|
64
|
+
JSON.stringify({ payloadId: payloadId({ version, platform, arch }), procedure: "adopted" })
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { runtimeDir, dir };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
test("payloadId 带版本、平台与架构,多份可共存", () => {
|
|
72
|
+
assert.equal(payloadId({ version: "4.2.0", platform: "win32", arch: "x64" }), "transformers-4.2.0-node-win32-x64");
|
|
73
|
+
assert.equal(payloadId({ version: "4.3.1", platform: "darwin", arch: "arm64" }), "transformers-4.3.1-node-darwin-arm64");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("运行时根目录默认挂在 ~/.dsh/mneme/runtime 下", () => {
|
|
77
|
+
assert.equal(defaultRuntimeDir("/home/u"), join("/home/u", ".dsh", "mneme", "runtime"));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("transformersVersionOk 只认 >=4.2.0 <5", () => {
|
|
81
|
+
assert.equal(transformersVersionOk("4.2.0"), true);
|
|
82
|
+
assert.equal(transformersVersionOk("4.10.3"), true);
|
|
83
|
+
assert.equal(transformersVersionOk("4.2.0-rc.1"), true, "预发布后缀按主版本号判断即可");
|
|
84
|
+
assert.equal(transformersVersionOk("4.1.9"), false);
|
|
85
|
+
assert.equal(transformersVersionOk("5.0.0"), false);
|
|
86
|
+
assert.equal(transformersVersionOk("garbage"), false);
|
|
87
|
+
assert.equal(transformersVersionOk(undefined), false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("完整的 payload:结构检查通过,且不报任何缺件", () => {
|
|
91
|
+
const { dir } = makePayload({ withManifest: true });
|
|
92
|
+
const report = describePayload(dir, { platform: "win32", arch: "x64" });
|
|
93
|
+
assert.equal(report.ok, true, `不应有失败:missing=${report.missing} reasons=${report.reasons}`);
|
|
94
|
+
assert.deepEqual(report.missing, []);
|
|
95
|
+
assert.deepEqual(report.reasons, []);
|
|
96
|
+
assert.equal(report.version, "4.2.0");
|
|
97
|
+
assert.equal(report.manifest?.procedure, "adopted");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("目录不存在(没装过)时给出可读原因,而不是抛异常", () => {
|
|
101
|
+
const { runtimeDir } = makePayload();
|
|
102
|
+
const report = describePayload(join(runtimeDir, "transformers-9.9.9-node-win32-x64"), { platform: "win32", arch: "x64" });
|
|
103
|
+
assert.equal(report.ok, false);
|
|
104
|
+
assert.equal(report.missing.length, 0);
|
|
105
|
+
assert.match(report.reasons.join("\n"), /node_modules 不存在/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("缺入口文件时点名入口本身", () => {
|
|
109
|
+
const { dir } = makePayload({ withEntry: false });
|
|
110
|
+
const report = describePayload(dir, { platform: "win32", arch: "x64" });
|
|
111
|
+
assert.equal(report.ok, false);
|
|
112
|
+
assert.ok(report.missing.includes(TRANSFORMERS_ENTRY), `missing=${JSON.stringify(report.missing)}`);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("缺必需包时逐个点名", () => {
|
|
116
|
+
const { dir } = makePayload({ packages: ["@huggingface/transformers"] });
|
|
117
|
+
const report = describePayload(dir, { platform: "win32", arch: "x64" });
|
|
118
|
+
assert.equal(report.ok, false);
|
|
119
|
+
assert.deepEqual(report.missing.includes("onnxruntime-node"), true);
|
|
120
|
+
assert.deepEqual(report.missing.includes("sharp"), true);
|
|
121
|
+
// 包整个不在时,不应再额外抱怨平台子树——否则同一件事会被报两次。
|
|
122
|
+
assert.equal(report.reasons.some((r) => r.includes("平台")), false);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("onnxruntime-node 在,但缺少当前平台的二进制子树:报平台原因(裁剪/搬运最常见的错)", () => {
|
|
126
|
+
const { dir } = makePayload({ nodePlatform: "darwin" });
|
|
127
|
+
const report = describePayload(dir, { platform: "win32", arch: "x64" });
|
|
128
|
+
assert.equal(report.ok, false);
|
|
129
|
+
assert.equal(report.missing.length, 0, "包都在,不该算缺件");
|
|
130
|
+
assert.match(report.reasons.join("\n"), /win32\/x64 的二进制子树/);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("同平台但架构不符必须拒绝:linux-arm64 的 payload 不能在 linux-x64 上通过", () => {
|
|
134
|
+
// 真实包内是 bin/napi-v6/{darwin,linux,win32}/{arm64,x64},只查平台会让架构不符的
|
|
135
|
+
// payload 通过结构检查、被 loader 选中,然后在 import 原生模块时才失败。
|
|
136
|
+
const { dir } = makePayload({ platform: "linux", arch: "arm64" });
|
|
137
|
+
assert.equal(describePayload(dir, { platform: "linux", arch: "arm64" }).ok, true, "同架构应当通过");
|
|
138
|
+
const mismatched = describePayload(dir, { platform: "linux", arch: "x64" });
|
|
139
|
+
assert.equal(mismatched.ok, false, "架构不符必须不通过");
|
|
140
|
+
assert.equal(mismatched.missing.length, 0, "包都在,不该算缺件");
|
|
141
|
+
assert.match(mismatched.reasons.join("\n"), /linux\/x64 的二进制子树/);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("版本落在认可区间之外:结构不缺件,但要报版本原因", () => {
|
|
145
|
+
const { dir } = makePayload({ version: "5.0.0" });
|
|
146
|
+
const report = describePayload(dir, { platform: "win32", arch: "x64" });
|
|
147
|
+
assert.equal(report.ok, false);
|
|
148
|
+
assert.equal(report.missing.length, 0);
|
|
149
|
+
assert.match(report.reasons.join("\n"), /不在认可区间/);
|
|
150
|
+
assert.equal(report.version, "5.0.0");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("listPayloadDirs 只列目录、名称排序,且对不存在的根目录返回空数组", () => {
|
|
154
|
+
const { runtimeDir } = makePayload();
|
|
155
|
+
makePayload();
|
|
156
|
+
assert.deepEqual(listPayloadDirs(join(runtimeDir, "nope")), []);
|
|
157
|
+
assert.deepEqual(listPayloadDirs(runtimeDir), ["transformers-4.2.0-node-win32-x64"]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("清单文件名与路径稳定(契约的一部分)", () => {
|
|
161
|
+
assert.equal(RUNTIME_MANIFEST, "mneme-runtime.json");
|
|
162
|
+
assert.equal(runtimeManifestPath("/rt/p"), join("/rt/p", "mneme-runtime.json"));
|
|
163
|
+
});
|