@cortexkit/aft-bridge 0.30.1 → 0.30.3
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/dist/bridge.d.ts +1 -0
- package/dist/bridge.d.ts.map +1 -1
- package/dist/bridge.js +40 -10
- package/dist/bridge.js.map +1 -1
- package/dist/downloader.d.ts +14 -0
- package/dist/downloader.d.ts.map +1 -1
- package/dist/downloader.js +58 -8
- package/dist/downloader.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/migration.d.ts +3 -0
- package/dist/migration.d.ts.map +1 -1
- package/dist/migration.js +13 -2
- package/dist/migration.js.map +1 -1
- package/dist/onnx-runtime.d.ts +6 -3
- package/dist/onnx-runtime.d.ts.map +1 -1
- package/dist/onnx-runtime.js +23 -13
- package/dist/onnx-runtime.js.map +1 -1
- package/dist/paths.d.ts +41 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +102 -0
- package/dist/paths.js.map +1 -0
- package/dist/pool.d.ts +1 -0
- package/dist/pool.d.ts.map +1 -1
- package/dist/pool.js +25 -9
- package/dist/pool.js.map +1 -1
- package/dist/resolver.d.ts +6 -11
- package/dist/resolver.d.ts.map +1 -1
- package/dist/resolver.js +34 -33
- package/dist/resolver.js.map +1 -1
- package/package.json +1 -1
- package/dist/url-fetch.d.ts +0 -33
- package/dist/url-fetch.d.ts.map +0 -1
- package/dist/url-fetch.js +0 -491
- package/dist/url-fetch.js.map +0 -1
package/dist/resolver.js
CHANGED
|
@@ -1,42 +1,16 @@
|
|
|
1
|
-
import { execSync
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
2
|
import { chmodSync, copyFileSync, existsSync, mkdirSync, renameSync } from "node:fs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { log, warn } from "./active-logger.js";
|
|
7
|
-
import { ensureBinary, getCacheDir } from "./downloader.js";
|
|
7
|
+
import { ensureBinary, getCacheDir, readBinaryVersion } from "./downloader.js";
|
|
8
8
|
import { PLATFORM_ARCH_MAP } from "./platform.js";
|
|
9
9
|
let ensureBinaryForResolver = ensureBinary;
|
|
10
10
|
export function __setEnsureBinaryForTests(impl) {
|
|
11
11
|
ensureBinaryForResolver = impl ?? ensureBinary;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
* Read the version string from an `aft` binary by invoking it with
|
|
15
|
-
* `--version`. Returns the bare version (e.g. `"0.22.1"`) without the
|
|
16
|
-
* leading `v` or the `aft` prefix, or `null` if the invocation fails.
|
|
17
|
-
*
|
|
18
|
-
* Exported so tests and the resolver can use it for version-match checks
|
|
19
|
-
* before deciding to return a candidate binary.
|
|
20
|
-
*/
|
|
21
|
-
export function readBinaryVersion(binaryPath) {
|
|
22
|
-
try {
|
|
23
|
-
const result = spawnSync(binaryPath, ["--version"], {
|
|
24
|
-
encoding: "utf-8",
|
|
25
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
26
|
-
timeout: 5000,
|
|
27
|
-
});
|
|
28
|
-
const stdoutVersion = result.stdout?.trim();
|
|
29
|
-
const stderrVersion = result.stderr?.trim();
|
|
30
|
-
const rawVersion = stdoutVersion || stderrVersion;
|
|
31
|
-
if (!rawVersion)
|
|
32
|
-
return null;
|
|
33
|
-
// `aft --version` outputs "aft 0.9.0" — extract just the version number
|
|
34
|
-
return rawVersion.replace(/^aft\s+/, "");
|
|
35
|
-
}
|
|
36
|
-
catch {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
13
|
+
export { readBinaryVersion };
|
|
40
14
|
/**
|
|
41
15
|
* Copy an npm platform binary to the versioned cache so we never run from
|
|
42
16
|
* node_modules directly. This prevents corruption when npm updates the
|
|
@@ -106,6 +80,24 @@ function isExpectedCachedBinary(binaryPath, expectedVersion) {
|
|
|
106
80
|
warn(`Cached binary at ${binaryPath} reports ${actual ?? "no version"}, expected ${expected}; skipping cache candidate`);
|
|
107
81
|
return false;
|
|
108
82
|
}
|
|
83
|
+
function probeBinaryCandidate(binaryPath, source, expectedVersion) {
|
|
84
|
+
const actual = readBinaryVersion(binaryPath);
|
|
85
|
+
if (actual === null) {
|
|
86
|
+
warn(`${source} binary at ${binaryPath} did not report a version; skipping`);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
if (expectedVersion && actual !== normalizeBareVersion(expectedVersion)) {
|
|
90
|
+
warn(`${source} binary at ${binaryPath} reports ${actual}, expected ${normalizeBareVersion(expectedVersion)}; skipping`);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return binaryPath;
|
|
94
|
+
}
|
|
95
|
+
function parsePathLookupOutput(output) {
|
|
96
|
+
return output
|
|
97
|
+
.split(/\r?\n/)
|
|
98
|
+
.map((candidate) => candidate.trim())
|
|
99
|
+
.filter(Boolean);
|
|
100
|
+
}
|
|
109
101
|
/**
|
|
110
102
|
* Map the current `process.platform` and `process.arch` to the npm platform
|
|
111
103
|
* package suffix (e.g. `"darwin-arm64"`, `"linux-x64"`).
|
|
@@ -203,18 +195,27 @@ export function findBinarySync(expectedVersion) {
|
|
|
203
195
|
env,
|
|
204
196
|
stdio: ["pipe", "pipe", "pipe"],
|
|
205
197
|
}).trim();
|
|
206
|
-
|
|
207
|
-
|
|
198
|
+
for (const candidate of parsePathLookupOutput(result)) {
|
|
199
|
+
const usable = probeBinaryCandidate(candidate, "PATH", expectedVersion);
|
|
200
|
+
if (usable)
|
|
201
|
+
return usable;
|
|
202
|
+
}
|
|
208
203
|
}
|
|
209
204
|
catch {
|
|
210
205
|
// not in PATH
|
|
211
206
|
}
|
|
212
207
|
// 4. Check ~/.cargo/bin/aft
|
|
213
208
|
const cargoPath = join(homeDirFromEnv(env), ".cargo", "bin", `aft${ext}`);
|
|
214
|
-
if (existsSync(cargoPath))
|
|
215
|
-
|
|
209
|
+
if (existsSync(cargoPath)) {
|
|
210
|
+
const usable = probeBinaryCandidate(cargoPath, "cargo", expectedVersion);
|
|
211
|
+
if (usable)
|
|
212
|
+
return usable;
|
|
213
|
+
}
|
|
216
214
|
return null;
|
|
217
215
|
}
|
|
216
|
+
export const __test__ = {
|
|
217
|
+
parsePathLookupOutput,
|
|
218
|
+
};
|
|
218
219
|
/**
|
|
219
220
|
* Locate the `aft` binary, with auto-download as a last resort.
|
|
220
221
|
*
|
package/dist/resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAIlD,IAAI,uBAAuB,GAAiB,YAAY,CAAC;AAEzD,MAAM,UAAU,yBAAyB,CAAC,IAAyB;IACjE,uBAAuB,GAAG,IAAI,IAAI,YAAY,CAAC;AACjD,CAAC;AAID,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAE7B;;;;;;;;GAQG;AACH,SAAS,oBAAoB,CAAC,aAAqB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MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,eAAwB;IACvD,+CAA+C;IAC/C,MAAM,UAAU,GAAG,cAAc,CAAC,eAAe,CAAC,CAAC;IACnD,IAAI,UAAU,EAAE,CAAC;QACf,GAAG,CAAC,oBAAoB,UAAU,EAAE,CAAC,CAAC;QACtC,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,wCAAwC;IACxC,GAAG,CAAC,uDAAuD,CAAC,CAAC;IAC7D,MAAM,UAAU,GAAG,MAAM,uBAAuB,CAAC,eAAe,CAAC,CAAC;IAClE,IAAI,UAAU;QAAE,OAAO,UAAU,CAAC;IAElC,wBAAwB;IACxB,MAAM,IAAI,KAAK,CACb;QACE,kCAAkC;QAClC,EAAE;QACF,oBAAoB;QACpB,yCAAyC;QACzC,sDAAsD;QACtD,6BAA6B;QAC7B,sBAAsB;QACtB,iDAAiD;QACjD,EAAE;QACF,wCAAwC;QACxC,0FAA0F;QAC1F,+DAA+D;QAC/D,8EAA8E;QAC9E,EAAE;QACF,wCAAwC;KACzC,CAAC,IAAI,CAAC,IAAI,CAAC,CACb,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/dist/url-fetch.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { lookup } from "node:dns/promises";
|
|
2
|
-
import { type Dispatcher } from "undici";
|
|
3
|
-
interface FetchUrlOptions {
|
|
4
|
-
allowPrivate?: boolean;
|
|
5
|
-
/** Test-only injection point. Production fetches use undici with DNS pinning. */
|
|
6
|
-
fetchImpl?: FetchImpl;
|
|
7
|
-
/** Test-only injection point. Production DNS resolution uses node:dns/promises.lookup. */
|
|
8
|
-
lookup?: LookupFn;
|
|
9
|
-
/** Test-only injection point for observing the DNS-pinned dispatcher. */
|
|
10
|
-
dispatcherFactory?: (validatedIp: string) => Dispatcher;
|
|
11
|
-
}
|
|
12
|
-
type LookupFn = typeof lookup;
|
|
13
|
-
interface FetchInit {
|
|
14
|
-
signal?: AbortSignal;
|
|
15
|
-
redirect?: "manual";
|
|
16
|
-
dispatcher?: Dispatcher;
|
|
17
|
-
headers?: Record<string, string>;
|
|
18
|
-
}
|
|
19
|
-
type FetchImpl = (input: string, init: FetchInit) => Promise<Response>;
|
|
20
|
-
/** Exported for unit tests. */
|
|
21
|
-
export declare function _isPrivateIpv4(address: string): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Fetch a URL to a cached temp file. Uses disk cache with 1-day TTL.
|
|
24
|
-
* Returns the cached file path the Rust outline/zoom command can read.
|
|
25
|
-
* Throws on errors (invalid URL, network failure, unsupported content type, oversized body).
|
|
26
|
-
*/
|
|
27
|
-
export declare function fetchUrlToTempFile(url: string, storageDir: string, options?: FetchUrlOptions): Promise<string>;
|
|
28
|
-
/**
|
|
29
|
-
* Remove cache entries older than TTL. Called periodically at plugin startup.
|
|
30
|
-
*/
|
|
31
|
-
export declare function cleanupUrlCache(storageDir: string): void;
|
|
32
|
-
export {};
|
|
33
|
-
//# sourceMappingURL=url-fetch.d.ts.map
|
package/dist/url-fetch.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"url-fetch.d.ts","sourceRoot":"","sources":["../src/url-fetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAW3C,OAAO,EAAS,KAAK,UAAU,EAAwB,MAAM,QAAQ,CAAC;AAoBtE,UAAU,eAAe;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,iFAAiF;IACjF,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,0FAA0F;IAC1F,MAAM,CAAC,EAAE,QAAQ,CAAC;IAClB,yEAAyE;IACzE,iBAAiB,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,UAAU,CAAC;CACzD;AAED,KAAK,QAAQ,GAAG,OAAO,MAAM,CAAC;AAC9B,UAAU,SAAS;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,KAAK,SAAS,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAyBvE,+BAA+B;AAC/B,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAavD;AAkTD;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAsIjB;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAkCxD"}
|
package/dist/url-fetch.js
DELETED
|
@@ -1,491 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { lookup } from "node:dns/promises";
|
|
3
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
4
|
-
import { isIP } from "node:net";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { Agent, fetch as undiciFetch } from "undici";
|
|
7
|
-
import { log, warn } from "./active-logger.js";
|
|
8
|
-
/** Max response body size (10 MB) */
|
|
9
|
-
const MAX_RESPONSE_BYTES = 10 * 1024 * 1024;
|
|
10
|
-
/** Cache TTL: 1 day */
|
|
11
|
-
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
12
|
-
/** Fetch timeout: 30 seconds — covers initial connect + headers */
|
|
13
|
-
const FETCH_TIMEOUT_MS = 30_000;
|
|
14
|
-
/**
|
|
15
|
-
* Per-chunk body read timeout: 15 seconds. Resets after every successful
|
|
16
|
-
* chunk so legitimate large/slow downloads still complete, but a stalled
|
|
17
|
-
* mid-body stream (e.g. CDN edge drops the connection without closing the
|
|
18
|
-
* socket cleanly) is aborted within this window. Without this guard the
|
|
19
|
-
* body loop blocks indefinitely because Node's `AbortController` from
|
|
20
|
-
* `fetch()` only covers the connect+headers phase, not body streaming.
|
|
21
|
-
*/
|
|
22
|
-
const BODY_CHUNK_TIMEOUT_MS = 15_000;
|
|
23
|
-
const MAX_REDIRECTS = 5;
|
|
24
|
-
function cacheDir(storageDir) {
|
|
25
|
-
return join(storageDir, "url_cache");
|
|
26
|
-
}
|
|
27
|
-
function hashUrl(url) {
|
|
28
|
-
return createHash("sha256").update(url).digest("hex").slice(0, 16);
|
|
29
|
-
}
|
|
30
|
-
function metaPath(storageDir, hash) {
|
|
31
|
-
return join(cacheDir(storageDir), `${hash}.meta.json`);
|
|
32
|
-
}
|
|
33
|
-
function contentPath(storageDir, hash, extension) {
|
|
34
|
-
return join(cacheDir(storageDir), `${hash}${extension}`);
|
|
35
|
-
}
|
|
36
|
-
/** Exported for unit tests. */
|
|
37
|
-
export function _isPrivateIpv4(address) {
|
|
38
|
-
const parts = address.split(".").map((part) => Number.parseInt(part, 10));
|
|
39
|
-
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part)))
|
|
40
|
-
return true;
|
|
41
|
-
const [a, b] = parts;
|
|
42
|
-
return (a === 0 || // 0.0.0.0/8 — wildcard, blocked to prevent local-host bypass
|
|
43
|
-
a === 10 ||
|
|
44
|
-
a === 127 ||
|
|
45
|
-
(a === 172 && b >= 16 && b <= 31) ||
|
|
46
|
-
(a === 192 && b === 168) ||
|
|
47
|
-
(a === 169 && b === 254) ||
|
|
48
|
-
a >= 224 // multicast (224/4) and reserved (240/4)
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Expand a possibly-compressed IPv6 address to 8 hextets (numbers).
|
|
53
|
-
* Returns null on parse failure.
|
|
54
|
-
*/
|
|
55
|
-
function expandIpv6(addr) {
|
|
56
|
-
if (addr === "::")
|
|
57
|
-
return [0, 0, 0, 0, 0, 0, 0, 0];
|
|
58
|
-
// Reject more than one "::"
|
|
59
|
-
const dcMatches = addr.match(/::/g);
|
|
60
|
-
if (dcMatches && dcMatches.length > 1)
|
|
61
|
-
return null;
|
|
62
|
-
// Handle dotted-quad tail by converting to two hextets.
|
|
63
|
-
let normalized = addr;
|
|
64
|
-
const lastColon = normalized.lastIndexOf(":");
|
|
65
|
-
if (lastColon !== -1) {
|
|
66
|
-
const tail = normalized.slice(lastColon + 1);
|
|
67
|
-
if (tail.includes(".")) {
|
|
68
|
-
const octets = tail.split(".").map((p) => Number.parseInt(p, 10));
|
|
69
|
-
if (octets.length !== 4 || octets.some((o) => Number.isNaN(o) || o < 0 || o > 255)) {
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
const h1 = ((octets[0] << 8) | octets[1]).toString(16);
|
|
73
|
-
const h2 = ((octets[2] << 8) | octets[3]).toString(16);
|
|
74
|
-
normalized = `${normalized.slice(0, lastColon)}:${h1}:${h2}`;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
let parts;
|
|
78
|
-
if (normalized.includes("::")) {
|
|
79
|
-
const [left, right] = normalized.split("::");
|
|
80
|
-
const leftParts = left ? left.split(":") : [];
|
|
81
|
-
const rightParts = right ? right.split(":") : [];
|
|
82
|
-
const fill = 8 - leftParts.length - rightParts.length;
|
|
83
|
-
if (fill < 0)
|
|
84
|
-
return null;
|
|
85
|
-
parts = [...leftParts, ...Array(fill).fill("0"), ...rightParts];
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
parts = normalized.split(":");
|
|
89
|
-
}
|
|
90
|
-
if (parts.length !== 8)
|
|
91
|
-
return null;
|
|
92
|
-
const hextets = parts.map((p) => {
|
|
93
|
-
const n = Number.parseInt(p, 16);
|
|
94
|
-
return Number.isNaN(n) || n < 0 || n > 0xffff ? -1 : n;
|
|
95
|
-
});
|
|
96
|
-
if (hextets.some((h) => h === -1))
|
|
97
|
-
return null;
|
|
98
|
-
return hextets;
|
|
99
|
-
}
|
|
100
|
-
function isPrivateIp(address) {
|
|
101
|
-
if (!address.includes(":")) {
|
|
102
|
-
return _isPrivateIpv4(address);
|
|
103
|
-
}
|
|
104
|
-
const lower = address.toLowerCase();
|
|
105
|
-
// Strip optional zone identifier (fe80::1%eth0 → fe80::1).
|
|
106
|
-
const noZone = lower.split("%")[0];
|
|
107
|
-
// Expand to 8 hextets so we can reliably detect IPv4-mapped/compatible
|
|
108
|
-
// forms even after URL canonicalization (e.g. [::ffff:127.0.0.1] →
|
|
109
|
-
// [::ffff:7f00:1]).
|
|
110
|
-
const hextets = expandIpv6(noZone);
|
|
111
|
-
if (hextets) {
|
|
112
|
-
// IPv4-mapped (::ffff:X.X.X.X) and IPv4-compatible (::X.X.X.X) — extract
|
|
113
|
-
// the embedded IPv4 from the last two hextets and check against IPv4 ranges.
|
|
114
|
-
const top6Zero = hextets.slice(0, 6).every((h) => h === 0);
|
|
115
|
-
const isMapped = hextets[0] === 0 &&
|
|
116
|
-
hextets[1] === 0 &&
|
|
117
|
-
hextets[2] === 0 &&
|
|
118
|
-
hextets[3] === 0 &&
|
|
119
|
-
hextets[4] === 0 &&
|
|
120
|
-
hextets[5] === 0xffff;
|
|
121
|
-
if (isMapped || top6Zero) {
|
|
122
|
-
const a = (hextets[6] >> 8) & 0xff;
|
|
123
|
-
const b = hextets[6] & 0xff;
|
|
124
|
-
const c = (hextets[7] >> 8) & 0xff;
|
|
125
|
-
const d = hextets[7] & 0xff;
|
|
126
|
-
const ipv4 = `${a}.${b}.${c}.${d}`;
|
|
127
|
-
// ::1 (loopback) and :: (unspecified) both fall into top6Zero and last
|
|
128
|
-
// hextet 0 or 1 — _isPrivateIpv4 already blocks 0.0.0.0 and 127/8.
|
|
129
|
-
if (top6Zero && hextets[6] === 0 && hextets[7] <= 1) {
|
|
130
|
-
return true;
|
|
131
|
-
}
|
|
132
|
-
return _isPrivateIpv4(ipv4);
|
|
133
|
-
}
|
|
134
|
-
const firstHextet = hextets[0];
|
|
135
|
-
return (
|
|
136
|
-
// fe80::/10 link-local
|
|
137
|
-
(firstHextet >= 0xfe80 && firstHextet <= 0xfebf) ||
|
|
138
|
-
// fc00::/7 unique local
|
|
139
|
-
(firstHextet >= 0xfc00 && firstHextet <= 0xfdff) ||
|
|
140
|
-
// ff00::/8 multicast
|
|
141
|
-
firstHextet >= 0xff00);
|
|
142
|
-
}
|
|
143
|
-
// Could not expand — be conservative and block.
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
async function assertPublicUrl(url, allowPrivate, dnsLookup = lookup) {
|
|
147
|
-
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
148
|
-
throw new Error(`Only http:// and https:// URLs are supported, got: ${url.protocol}`);
|
|
149
|
-
}
|
|
150
|
-
if (allowPrivate)
|
|
151
|
-
return undefined;
|
|
152
|
-
// URL.hostname returns IPv6 literals wrapped in brackets ("[::1]") per WHATWG.
|
|
153
|
-
// Strip them before checking the address.
|
|
154
|
-
const hostname = url.hostname.replace(/^\[|\]$/g, "");
|
|
155
|
-
// If the hostname is already a literal IP, check it directly — skip DNS so we
|
|
156
|
-
// also catch malformed IPv6 forms (::127.0.0.1, ::ffff:127.0.0.1) that some
|
|
157
|
-
// resolvers reject as "not found" instead of returning the embedded IPv4.
|
|
158
|
-
if (isIP(hostname) || hostname.includes(":")) {
|
|
159
|
-
if (isPrivateIp(hostname)) {
|
|
160
|
-
throw new Error(`Blocked private URL host ${url.hostname} (${hostname})`);
|
|
161
|
-
}
|
|
162
|
-
return hostname;
|
|
163
|
-
}
|
|
164
|
-
const addresses = await dnsLookup(hostname, { all: true, verbatim: true });
|
|
165
|
-
for (const { address } of addresses) {
|
|
166
|
-
if (isPrivateIp(address)) {
|
|
167
|
-
throw new Error(`Blocked private URL host ${url.hostname} (${address})`);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (addresses.length === 0) {
|
|
171
|
-
throw new Error(`Failed to resolve URL host ${url.hostname}`);
|
|
172
|
-
}
|
|
173
|
-
// Prefer IPv4 when available. AAAA can come first on many resolvers, but
|
|
174
|
-
// many user networks (corporate, home, mobile) don't have IPv6 transit, so
|
|
175
|
-
// pinning to AAAA causes EHOSTUNREACH / connect timeouts that surface as a
|
|
176
|
-
// bare `fetch failed`. Falls back to whatever DNS gave us if no IPv4 record.
|
|
177
|
-
// `family` is documented as numeric (4/6) but be defensive — some runtimes
|
|
178
|
-
// have surfaced it as the string "IPv4"/"IPv6" historically.
|
|
179
|
-
const ipv4 = addresses.find((entry) => entry.family === 4 ||
|
|
180
|
-
entry.family === "IPv4" ||
|
|
181
|
-
(typeof entry.address === "string" && isIP(entry.address) === 4));
|
|
182
|
-
const chosen = ipv4 ?? addresses[0];
|
|
183
|
-
if (!chosen || typeof chosen.address !== "string" || chosen.address.length === 0) {
|
|
184
|
-
// Should be unreachable given the length>0 check above, but the bare
|
|
185
|
-
// `Invalid IP address: undefined` failure surfaced by undici when an
|
|
186
|
-
// empty/undefined IP slips into connect.lookup is opaque enough that
|
|
187
|
-
// we want a clear, structured failure instead.
|
|
188
|
-
throw new Error(`DNS lookup for ${url.hostname} returned no usable address (got: ${JSON.stringify(addresses)})`);
|
|
189
|
-
}
|
|
190
|
-
return chosen.address;
|
|
191
|
-
}
|
|
192
|
-
function createPinnedDispatcher(validatedIp) {
|
|
193
|
-
const family = validatedIp.includes(":") ? 6 : 4;
|
|
194
|
-
// Node 18+ calls the lookup with `opts.all: true` (especially via TLS /
|
|
195
|
-
// happy-eyeballs paths). When `all` is true the callback expects an *array*
|
|
196
|
-
// of `{address, family}` objects, not the legacy `(err, address, family)`
|
|
197
|
-
// 3-arg shape. Calling the legacy form there causes Node to surface
|
|
198
|
-
// `TypeError [ERR_INVALID_IP_ADDRESS]: Invalid IP address: undefined` from
|
|
199
|
-
// net:emitLookup, wrapped by undici as a bare `fetch failed`. Honor
|
|
200
|
-
// whichever shape `opts.all` requested.
|
|
201
|
-
// (Cast to `any` because undici's `LookupFunction` type only models the
|
|
202
|
-
// single-address callback overload, not the `all: true` variadic.)
|
|
203
|
-
// biome-ignore lint/suspicious/noExplicitAny: dual-overload lookup callback
|
|
204
|
-
const pinnedLookup = (_hostname, opts,
|
|
205
|
-
// biome-ignore lint/suspicious/noExplicitAny: dual-overload lookup callback
|
|
206
|
-
callback) => {
|
|
207
|
-
if (opts?.all) {
|
|
208
|
-
callback(null, [{ address: validatedIp, family }]);
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
callback(null, validatedIp, family);
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
return new Agent({
|
|
215
|
-
connect: { lookup: pinnedLookup },
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
function resolveRedirectUrl(currentUrl, location) {
|
|
219
|
-
if (!location) {
|
|
220
|
-
throw new Error(`Redirect from ${currentUrl.href} missing Location header`);
|
|
221
|
-
}
|
|
222
|
-
return new URL(location, currentUrl);
|
|
223
|
-
}
|
|
224
|
-
async function fetchWithRedirects(startUrl, allowPrivate, deps = {}) {
|
|
225
|
-
let currentUrl = startUrl;
|
|
226
|
-
const fetchImpl = deps.fetchImpl ?? undiciFetch;
|
|
227
|
-
const dnsLookup = deps.lookup ?? lookup;
|
|
228
|
-
for (let redirectCount = 0; redirectCount <= MAX_REDIRECTS; redirectCount++) {
|
|
229
|
-
const validatedIp = await assertPublicUrl(currentUrl, allowPrivate, dnsLookup);
|
|
230
|
-
const dispatcher = validatedIp
|
|
231
|
-
? (deps.dispatcherFactory ?? createPinnedDispatcher)(validatedIp)
|
|
232
|
-
: undefined;
|
|
233
|
-
const controller = new AbortController();
|
|
234
|
-
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
235
|
-
let response;
|
|
236
|
-
try {
|
|
237
|
-
response = await fetchImpl(currentUrl.href, {
|
|
238
|
-
signal: controller.signal,
|
|
239
|
-
redirect: "manual",
|
|
240
|
-
dispatcher,
|
|
241
|
-
headers: {
|
|
242
|
-
"user-agent": "aft-opencode-plugin",
|
|
243
|
-
// Prioritize markdown for content-negotiating servers (GitHub API, many docs sites).
|
|
244
|
-
// `application/vnd.github.raw` is GitHub's custom type — returns raw markdown from
|
|
245
|
-
// repo file and readme endpoints. Falls back to HTML if markdown is not available.
|
|
246
|
-
accept: "application/vnd.github.raw, text/markdown, text/x-markdown, text/html;q=0.9, text/plain;q=0.5",
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
catch (err) {
|
|
251
|
-
// undici wraps the underlying network error (ECONNREFUSED / ETIMEDOUT /
|
|
252
|
-
// EHOSTUNREACH / ENOTFOUND / cert errors) inside `.cause` and exposes a
|
|
253
|
-
// bare "fetch failed" on the outer Error. Surfacing the cause's `code`
|
|
254
|
-
// and message turns an unactionable failure into something the agent
|
|
255
|
-
// (and the user) can actually triage.
|
|
256
|
-
const error = err;
|
|
257
|
-
const cause = error.cause;
|
|
258
|
-
const causeCode = cause?.code ?? error.code;
|
|
259
|
-
const causeMsg = cause?.message ?? "";
|
|
260
|
-
const detail = causeCode
|
|
261
|
-
? causeMsg
|
|
262
|
-
? `${causeCode}: ${causeMsg}`
|
|
263
|
-
: causeCode
|
|
264
|
-
: (error.message ?? "unknown error");
|
|
265
|
-
// Log the full chain so triage doesn't depend on the agent surface
|
|
266
|
-
// forwarding the wrapped error message.
|
|
267
|
-
warn(`URL fetch failed: ${currentUrl.href} — ${detail}`, {
|
|
268
|
-
outerMessage: error.message,
|
|
269
|
-
causeName: cause?.name,
|
|
270
|
-
causeCode,
|
|
271
|
-
causeErrno: cause?.errno,
|
|
272
|
-
causeSyscall: cause?.syscall,
|
|
273
|
-
});
|
|
274
|
-
throw new Error(`Failed to fetch ${currentUrl.href}: ${detail}`);
|
|
275
|
-
}
|
|
276
|
-
finally {
|
|
277
|
-
clearTimeout(timer);
|
|
278
|
-
}
|
|
279
|
-
if (response.status < 300 || response.status >= 400) {
|
|
280
|
-
return response;
|
|
281
|
-
}
|
|
282
|
-
if (redirectCount === MAX_REDIRECTS) {
|
|
283
|
-
throw new Error(`Too many redirects fetching ${startUrl.href}`);
|
|
284
|
-
}
|
|
285
|
-
currentUrl = resolveRedirectUrl(currentUrl, response.headers.get("location"));
|
|
286
|
-
}
|
|
287
|
-
throw new Error(`Too many redirects fetching ${startUrl.href}`);
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Map a Content-Type header to a file extension AFT can parse.
|
|
291
|
-
* Returns null for unsupported types.
|
|
292
|
-
*/
|
|
293
|
-
function resolveExtension(contentType) {
|
|
294
|
-
// Normalize: strip parameters (after `;`) AND pick the first value if the server
|
|
295
|
-
// echoed back a comma-separated list (GitHub API does this for /readme endpoints).
|
|
296
|
-
const lower = contentType.toLowerCase().split(";")[0].split(",")[0].trim();
|
|
297
|
-
if (lower === "text/html" ||
|
|
298
|
-
lower === "application/xhtml+xml" ||
|
|
299
|
-
lower === "application/vnd.github.html" ||
|
|
300
|
-
lower === "application/vnd.github+html") {
|
|
301
|
-
return ".html";
|
|
302
|
-
}
|
|
303
|
-
if (lower === "text/markdown" ||
|
|
304
|
-
lower === "text/x-markdown" ||
|
|
305
|
-
lower === "application/markdown" ||
|
|
306
|
-
// GitHub API raw content type — returns raw markdown for README endpoints
|
|
307
|
-
lower === "application/vnd.github.raw" ||
|
|
308
|
-
lower === "application/vnd.github+raw" ||
|
|
309
|
-
lower === "application/vnd.github.v3.raw") {
|
|
310
|
-
return ".md";
|
|
311
|
-
}
|
|
312
|
-
if (lower === "text/plain") {
|
|
313
|
-
// treat plain text as markdown so aft_outline can show headings if present
|
|
314
|
-
return ".md";
|
|
315
|
-
}
|
|
316
|
-
return null;
|
|
317
|
-
}
|
|
318
|
-
/**
|
|
319
|
-
* Fetch a URL to a cached temp file. Uses disk cache with 1-day TTL.
|
|
320
|
-
* Returns the cached file path the Rust outline/zoom command can read.
|
|
321
|
-
* Throws on errors (invalid URL, network failure, unsupported content type, oversized body).
|
|
322
|
-
*/
|
|
323
|
-
export async function fetchUrlToTempFile(url, storageDir, options = {}) {
|
|
324
|
-
let parsed;
|
|
325
|
-
try {
|
|
326
|
-
parsed = new URL(url);
|
|
327
|
-
}
|
|
328
|
-
catch {
|
|
329
|
-
throw new Error(`Invalid URL: ${url}`);
|
|
330
|
-
}
|
|
331
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
332
|
-
throw new Error(`Only http:// and https:// URLs are supported, got: ${parsed.protocol}`);
|
|
333
|
-
}
|
|
334
|
-
const allowPrivate = options.allowPrivate === true;
|
|
335
|
-
// Enforce the caller's current SSRF policy before consulting the disk cache.
|
|
336
|
-
// Otherwise a URL previously cached with allowPrivate=true could be replayed
|
|
337
|
-
// by a later allowPrivate=false call without re-validating the host.
|
|
338
|
-
await assertPublicUrl(parsed, allowPrivate, options.lookup);
|
|
339
|
-
const dir = cacheDir(storageDir);
|
|
340
|
-
mkdirSync(dir, { recursive: true });
|
|
341
|
-
const hash = hashUrl(url);
|
|
342
|
-
const metaFile = metaPath(storageDir, hash);
|
|
343
|
-
// Check cache
|
|
344
|
-
if (existsSync(metaFile)) {
|
|
345
|
-
try {
|
|
346
|
-
const meta = JSON.parse(readFileSync(metaFile, "utf8"));
|
|
347
|
-
const age = Date.now() - meta.fetchedAt;
|
|
348
|
-
const cached = contentPath(storageDir, hash, meta.extension);
|
|
349
|
-
if (age < CACHE_TTL_MS && existsSync(cached)) {
|
|
350
|
-
log(`URL cache hit: ${url} (${Math.round(age / 1000)}s old)`);
|
|
351
|
-
return cached;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
catch {
|
|
355
|
-
// corrupted meta, re-fetch
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
log(`Fetching URL: ${url}`);
|
|
359
|
-
const response = await fetchWithRedirects(parsed, allowPrivate, options);
|
|
360
|
-
if (!response.ok) {
|
|
361
|
-
throw new Error(`HTTP ${response.status} ${response.statusText} fetching ${url}`);
|
|
362
|
-
}
|
|
363
|
-
const contentType = response.headers.get("content-type") || "text/plain";
|
|
364
|
-
const extension = resolveExtension(contentType);
|
|
365
|
-
if (!extension) {
|
|
366
|
-
throw new Error(`Unsupported content type '${contentType}' for ${url}. Supported: text/html, text/markdown, text/plain`);
|
|
367
|
-
}
|
|
368
|
-
const lengthHeader = response.headers.get("content-length");
|
|
369
|
-
if (lengthHeader) {
|
|
370
|
-
const length = Number.parseInt(lengthHeader, 10);
|
|
371
|
-
if (Number.isFinite(length) && length > MAX_RESPONSE_BYTES) {
|
|
372
|
-
throw new Error(`Response too large: ${length} bytes (max ${MAX_RESPONSE_BYTES})`);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
// Stream with size cap and per-chunk timeout. The AbortController used for
|
|
376
|
-
// the initial fetch() only covers connect + headers — once we hold a
|
|
377
|
-
// ReadableStream reader, `reader.read()` has no built-in timeout, so a
|
|
378
|
-
// server that sends headers and then stalls mid-body (CDN edge drops the
|
|
379
|
-
// socket, partial response, rate limit hold) blocks this loop forever.
|
|
380
|
-
// The per-chunk timer is reset after every successful read, so legitimate
|
|
381
|
-
// large/slow downloads still complete cleanly.
|
|
382
|
-
const reader = response.body?.getReader();
|
|
383
|
-
if (!reader) {
|
|
384
|
-
throw new Error(`Failed to read response body for ${url}`);
|
|
385
|
-
}
|
|
386
|
-
const chunks = [];
|
|
387
|
-
let total = 0;
|
|
388
|
-
try {
|
|
389
|
-
while (true) {
|
|
390
|
-
let chunkTimer;
|
|
391
|
-
const stallSymbol = Symbol("body-stall");
|
|
392
|
-
const stallPromise = new Promise((resolve) => {
|
|
393
|
-
chunkTimer = setTimeout(() => resolve(stallSymbol), BODY_CHUNK_TIMEOUT_MS);
|
|
394
|
-
});
|
|
395
|
-
let result;
|
|
396
|
-
try {
|
|
397
|
-
result = await Promise.race([reader.read(), stallPromise]);
|
|
398
|
-
}
|
|
399
|
-
finally {
|
|
400
|
-
if (chunkTimer)
|
|
401
|
-
clearTimeout(chunkTimer);
|
|
402
|
-
}
|
|
403
|
-
if (result === stallSymbol) {
|
|
404
|
-
reader.cancel().catch(() => { });
|
|
405
|
-
throw new Error(`Body read stalled (no data for ${BODY_CHUNK_TIMEOUT_MS}ms) fetching ${url}`);
|
|
406
|
-
}
|
|
407
|
-
const { done, value } = result;
|
|
408
|
-
if (done)
|
|
409
|
-
break;
|
|
410
|
-
if (value) {
|
|
411
|
-
total += value.length;
|
|
412
|
-
if (total > MAX_RESPONSE_BYTES) {
|
|
413
|
-
reader.cancel().catch(() => { });
|
|
414
|
-
throw new Error(`Response exceeded ${MAX_RESPONSE_BYTES} bytes, aborted`);
|
|
415
|
-
}
|
|
416
|
-
chunks.push(value);
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
finally {
|
|
421
|
-
// Defensive: release any held lock so the underlying stream can be GCed
|
|
422
|
-
// even if a throw skipped the loop's normal exit. `releaseLock` is a no-op
|
|
423
|
-
// if the reader was already cancelled.
|
|
424
|
-
try {
|
|
425
|
-
reader.releaseLock();
|
|
426
|
-
}
|
|
427
|
-
catch { }
|
|
428
|
-
}
|
|
429
|
-
// Write content and meta atomically
|
|
430
|
-
const body = Buffer.concat(chunks);
|
|
431
|
-
const contentFile = contentPath(storageDir, hash, extension);
|
|
432
|
-
const tmpContent = `${contentFile}.tmp-${process.pid}`;
|
|
433
|
-
writeFileSync(tmpContent, body);
|
|
434
|
-
const { renameSync } = await import("node:fs");
|
|
435
|
-
renameSync(tmpContent, contentFile);
|
|
436
|
-
const meta = {
|
|
437
|
-
url,
|
|
438
|
-
contentType,
|
|
439
|
-
extension,
|
|
440
|
-
fetchedAt: Date.now(),
|
|
441
|
-
};
|
|
442
|
-
const tmpMeta = `${metaFile}.tmp-${process.pid}`;
|
|
443
|
-
writeFileSync(tmpMeta, JSON.stringify(meta));
|
|
444
|
-
renameSync(tmpMeta, metaFile);
|
|
445
|
-
log(`URL cached (${total} bytes): ${url}`);
|
|
446
|
-
return contentFile;
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* Remove cache entries older than TTL. Called periodically at plugin startup.
|
|
450
|
-
*/
|
|
451
|
-
export function cleanupUrlCache(storageDir) {
|
|
452
|
-
const dir = cacheDir(storageDir);
|
|
453
|
-
if (!existsSync(dir))
|
|
454
|
-
return;
|
|
455
|
-
let removed = 0;
|
|
456
|
-
try {
|
|
457
|
-
for (const entry of readdirSync(dir)) {
|
|
458
|
-
if (!entry.endsWith(".meta.json"))
|
|
459
|
-
continue;
|
|
460
|
-
const metaFile = join(dir, entry);
|
|
461
|
-
try {
|
|
462
|
-
const meta = JSON.parse(readFileSync(metaFile, "utf8"));
|
|
463
|
-
const age = Date.now() - meta.fetchedAt;
|
|
464
|
-
if (age > CACHE_TTL_MS) {
|
|
465
|
-
const hash = entry.slice(0, -".meta.json".length);
|
|
466
|
-
const content = contentPath(storageDir, hash, meta.extension);
|
|
467
|
-
if (existsSync(content))
|
|
468
|
-
unlinkSync(content);
|
|
469
|
-
unlinkSync(metaFile);
|
|
470
|
-
removed++;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
catch {
|
|
474
|
-
// corrupted meta, remove it too
|
|
475
|
-
try {
|
|
476
|
-
unlinkSync(metaFile);
|
|
477
|
-
removed++;
|
|
478
|
-
}
|
|
479
|
-
catch { }
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
catch (err) {
|
|
484
|
-
warn(`URL cache cleanup failed: ${err.message}`);
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
if (removed > 0) {
|
|
488
|
-
log(`URL cache cleanup: removed ${removed} stale entries`);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
//# sourceMappingURL=url-fetch.js.map
|