@sriinnu/fiche 0.18.0-bootstrap.0 → 0.19.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/CHANGELOG.md +134 -0
- package/LICENSE +22 -0
- package/NOTICE +13 -0
- package/PRIVACY.md +41 -0
- package/README.md +289 -0
- package/SECURITY.md +53 -0
- package/assets/JETBRAINS_MONO_LICENSE.txt +89 -0
- package/assets/SPLEEN_LICENSE.txt +24 -0
- package/assets/UNIFONT_LICENSE.txt +476 -0
- package/assets/fiche-logo-dark.svg +61 -0
- package/assets/fiche-logo-light.svg +61 -0
- package/assets/fiche-logo.svg +71 -0
- package/assets/fiche-mark.svg +48 -0
- package/bin/cli.js +7 -0
- package/bin/mcp.js +8 -0
- package/dist/calibrate.d.ts +30 -0
- package/dist/calibrate.js +588 -0
- package/dist/core/anthropic-vision.d.ts +28 -0
- package/dist/core/anthropic-vision.js +34 -0
- package/dist/core/applicability.d.ts +63 -0
- package/dist/core/applicability.js +151 -0
- package/dist/core/atlas-gray-jbmono10.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono10.js +64 -0
- package/dist/core/atlas-gray-jbmono12.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono12.js +64 -0
- package/dist/core/atlas-gray-jbmono14.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono14.js +64 -0
- package/dist/core/atlas-gray.d.ts +26 -0
- package/dist/core/atlas-gray.js +64 -0
- package/dist/core/atlas-jbmono10.d.ts +34 -0
- package/dist/core/atlas-jbmono10.js +71 -0
- package/dist/core/atlas-jbmono12.d.ts +34 -0
- package/dist/core/atlas-jbmono12.js +71 -0
- package/dist/core/atlas-jbmono14.d.ts +34 -0
- package/dist/core/atlas-jbmono14.js +71 -0
- package/dist/core/atlas.d.ts +34 -0
- package/dist/core/atlas.js +71 -0
- package/dist/core/baseline.d.ts +121 -0
- package/dist/core/baseline.js +200 -0
- package/dist/core/calibration.d.ts +86 -0
- package/dist/core/calibration.js +547 -0
- package/dist/core/casi-delta.d.ts +63 -0
- package/dist/core/casi-delta.js +1271 -0
- package/dist/core/claude-model-profiles.d.ts +53 -0
- package/dist/core/claude-model-profiles.js +160 -0
- package/dist/core/client-tool-round.d.ts +31 -0
- package/dist/core/client-tool-round.js +58 -0
- package/dist/core/codex-cache.d.ts +20 -0
- package/dist/core/codex-cache.js +59 -0
- package/dist/core/context-mmu.d.ts +350 -0
- package/dist/core/context-mmu.js +3028 -0
- package/dist/core/cpt-fit.d.ts +70 -0
- package/dist/core/cpt-fit.js +197 -0
- package/dist/core/env.d.ts +8 -0
- package/dist/core/env.js +15 -0
- package/dist/core/export.d.ts +127 -0
- package/dist/core/export.js +385 -0
- package/dist/core/factsheet.d.ts +77 -0
- package/dist/core/factsheet.js +494 -0
- package/dist/core/gemini-model-profiles.d.ts +20 -0
- package/dist/core/gemini-model-profiles.js +134 -0
- package/dist/core/google.d.ts +76 -0
- package/dist/core/google.js +1380 -0
- package/dist/core/gpt-model-profiles.d.ts +218 -0
- package/dist/core/gpt-model-profiles.js +570 -0
- package/dist/core/gpt-tokens.d.ts +14 -0
- package/dist/core/gpt-tokens.js +165 -0
- package/dist/core/history.d.ts +197 -0
- package/dist/core/history.js +1003 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/library.d.ts +80 -0
- package/dist/core/library.js +131 -0
- package/dist/core/measurement.d.ts +34 -0
- package/dist/core/measurement.js +280 -0
- package/dist/core/messages-chat-bridge.d.ts +23 -0
- package/dist/core/messages-chat-bridge.js +747 -0
- package/dist/core/messages-responses-bridge.d.ts +11 -0
- package/dist/core/messages-responses-bridge.js +893 -0
- package/dist/core/openai-history.d.ts +184 -0
- package/dist/core/openai-history.js +1279 -0
- package/dist/core/openai-savings.d.ts +57 -0
- package/dist/core/openai-savings.js +116 -0
- package/dist/core/openai.d.ts +68 -0
- package/dist/core/openai.js +2523 -0
- package/dist/core/pin.d.ts +199 -0
- package/dist/core/pin.js +1353 -0
- package/dist/core/png.d.ts +14 -0
- package/dist/core/png.js +188 -0
- package/dist/core/prefix-tape.d.ts +102 -0
- package/dist/core/prefix-tape.js +315 -0
- package/dist/core/price-id.d.ts +10 -0
- package/dist/core/price-id.js +26 -0
- package/dist/core/prices.generated.d.ts +36 -0
- package/dist/core/prices.generated.js +369 -0
- package/dist/core/pricing.d.ts +134 -0
- package/dist/core/pricing.js +335 -0
- package/dist/core/profile-base.d.ts +30 -0
- package/dist/core/profile-base.js +67 -0
- package/dist/core/provider-router.d.ts +51 -0
- package/dist/core/provider-router.js +121 -0
- package/dist/core/proxy.d.ts +252 -0
- package/dist/core/proxy.js +2929 -0
- package/dist/core/reader-tier.d.ts +24 -0
- package/dist/core/reader-tier.js +118 -0
- package/dist/core/render.d.ts +280 -0
- package/dist/core/render.js +1302 -0
- package/dist/core/request-guard.d.ts +31 -0
- package/dist/core/request-guard.js +126 -0
- package/dist/core/request-transform.d.ts +43 -0
- package/dist/core/request-transform.js +173 -0
- package/dist/core/schema-compiler.d.ts +64 -0
- package/dist/core/schema-compiler.js +1696 -0
- package/dist/core/schema-strip.d.ts +29 -0
- package/dist/core/schema-strip.js +173 -0
- package/dist/core/session-state.d.ts +219 -0
- package/dist/core/session-state.js +929 -0
- package/dist/core/stage-plugins.d.ts +45 -0
- package/dist/core/stage-plugins.js +205 -0
- package/dist/core/text-history.d.ts +74 -0
- package/dist/core/text-history.js +612 -0
- package/dist/core/text-hygiene.d.ts +160 -0
- package/dist/core/text-hygiene.js +2259 -0
- package/dist/core/toon.d.ts +29 -0
- package/dist/core/toon.js +425 -0
- package/dist/core/tracker.d.ts +223 -0
- package/dist/core/tracker.js +290 -0
- package/dist/core/transform.d.ts +589 -0
- package/dist/core/transform.js +3153 -0
- package/dist/core/types.d.ts +87 -0
- package/dist/core/types.js +8 -0
- package/dist/core/upstream-hosts.d.ts +127 -0
- package/dist/core/upstream-hosts.js +398 -0
- package/dist/core/vision-cost.d.ts +63 -0
- package/dist/core/vision-cost.js +137 -0
- package/dist/dashboard/fragments.d.ts +87 -0
- package/dist/dashboard/fragments.js +1515 -0
- package/dist/dashboard/types.d.ts +217 -0
- package/dist/dashboard/types.js +3 -0
- package/dist/dashboard/vendor.d.ts +3 -0
- package/dist/dashboard/vendor.js +6 -0
- package/dist/dashboard.d.ts +372 -0
- package/dist/dashboard.js +2731 -0
- package/dist/export-collect.d.ts +36 -0
- package/dist/export-collect.js +59 -0
- package/dist/file-tracker.d.ts +42 -0
- package/dist/file-tracker.js +234 -0
- package/dist/kosha-sync.d.ts +74 -0
- package/dist/kosha-sync.js +272 -0
- package/dist/launchd-socket.d.ts +22 -0
- package/dist/launchd-socket.js +210 -0
- package/dist/mcp/cli.d.ts +2 -0
- package/dist/mcp/cli.js +55 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +987 -0
- package/dist/mmu-store.d.ts +31 -0
- package/dist/mmu-store.js +179 -0
- package/dist/node-fetch.d.ts +22 -0
- package/dist/node-fetch.js +489 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +244396 -0
- package/dist/request-decoding.d.ts +30 -0
- package/dist/request-decoding.js +85 -0
- package/dist/sessions.d.ts +179 -0
- package/dist/sessions.js +837 -0
- package/dist/stats.d.ts +114 -0
- package/dist/stats.js +439 -0
- package/dist/turn-report.d.ts +65 -0
- package/dist/turn-report.js +724 -0
- package/dist/warp/app-launch.d.ts +26 -0
- package/dist/warp/app-launch.js +155 -0
- package/dist/warp/ca.d.ts +74 -0
- package/dist/warp/ca.js +416 -0
- package/dist/warp/connect.d.ts +42 -0
- package/dist/warp/connect.js +686 -0
- package/dist/warp/der.d.ts +57 -0
- package/dist/warp/der.js +151 -0
- package/dist/warp/index.d.ts +79 -0
- package/dist/warp/index.js +650 -0
- package/dist/warp/resolve.d.ts +12 -0
- package/dist/warp/resolve.js +49 -0
- package/dist/warp/route.d.ts +71 -0
- package/dist/warp/route.js +134 -0
- package/dist/worker.d.ts +71 -0
- package/dist/worker.js +174 -0
- package/docs/SECURITY_MODEL.md +84 -0
- package/package.json +115 -3
- package/plugins/fiche-companion/README.md +42 -0
- package/plugins/fiche-companion/assets/logo.svg +48 -0
- package/plugins/fiche-companion/hooks.json +11 -0
- package/plugins/fiche-companion/mcp_config.json +8 -0
- package/plugins/fiche-companion/plugin.json +18 -0
- package/plugins/fiche-companion/scripts/claude-statusline.mjs +35 -0
- package/plugins/fiche-companion/scripts/post-invocation.mjs +122 -0
- package/plugins/fiche-companion/scripts/resolve-turn-report.mjs +132 -0
- package/plugins/fiche-companion/skills/fiche/SKILL.md +61 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node HTTP/1.1 keep-alive `fetch` implementation for upstream proxy calls.
|
|
3
|
+
*
|
|
4
|
+
* Homebrew Node v26.13.1 bundles undici v8.12.4, whose HTTP/2 idle reaper
|
|
5
|
+
* (`onHttp2SessionIdleTimeout`, nodejs/undici#6143) passes `InformationalError`
|
|
6
|
+
* into `util.destroy(socket, err)` and crashes the process on lingering
|
|
7
|
+
* `ClientHttp2Stream` handles after aborted fetches. Using `node:http` and
|
|
8
|
+
* `node:https` keep-alive agents avoids HTTP/2 transport altogether while
|
|
9
|
+
* enforcing explicit stream error handlers, pull-based backpressure, and prompt
|
|
10
|
+
* upstream teardown on client disconnect or `AbortSignal`.
|
|
11
|
+
*/
|
|
12
|
+
import * as http from 'node:http';
|
|
13
|
+
import * as https from 'node:https';
|
|
14
|
+
import * as zlib from 'node:zlib';
|
|
15
|
+
const NULL_BODY_STATUSES = new Set([204, 205, 304]);
|
|
16
|
+
function toAbortError(reason) {
|
|
17
|
+
if (reason instanceof Error)
|
|
18
|
+
return reason;
|
|
19
|
+
return new DOMException(typeof reason === 'string' && reason.length > 0 ? reason : 'The operation was aborted.', 'AbortError');
|
|
20
|
+
}
|
|
21
|
+
function isReadableStreamBody(body) {
|
|
22
|
+
return (typeof body === 'object'
|
|
23
|
+
&& body !== null
|
|
24
|
+
&& typeof body.getReader === 'function');
|
|
25
|
+
}
|
|
26
|
+
function resolveBufferedBody(body) {
|
|
27
|
+
if (typeof body === 'string')
|
|
28
|
+
return body;
|
|
29
|
+
if (body instanceof Uint8Array)
|
|
30
|
+
return body;
|
|
31
|
+
if (body instanceof ArrayBuffer)
|
|
32
|
+
return new Uint8Array(body);
|
|
33
|
+
if (ArrayBuffer.isView(body)) {
|
|
34
|
+
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
35
|
+
}
|
|
36
|
+
if (body instanceof URLSearchParams)
|
|
37
|
+
return body.toString();
|
|
38
|
+
throw new TypeError('Unsupported request body type for Node upstream fetch');
|
|
39
|
+
}
|
|
40
|
+
const MAX_RESPONSE_CODINGS = 2;
|
|
41
|
+
function createSingleDecompressStream(coding) {
|
|
42
|
+
if (coding === 'gzip' || coding === 'x-gzip') {
|
|
43
|
+
return zlib.createGunzip({ flush: zlib.constants.Z_SYNC_FLUSH });
|
|
44
|
+
}
|
|
45
|
+
if (coding === 'deflate') {
|
|
46
|
+
return zlib.createInflate({ flush: zlib.constants.Z_SYNC_FLUSH });
|
|
47
|
+
}
|
|
48
|
+
if (coding === 'br') {
|
|
49
|
+
return zlib.createBrotliDecompress({ flush: zlib.constants.BROTLI_OPERATION_FLUSH });
|
|
50
|
+
}
|
|
51
|
+
const createZstd = zlib.createZstdDecompress;
|
|
52
|
+
if (coding === 'zstd' && typeof createZstd === 'function') {
|
|
53
|
+
return createZstd();
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
function parseContentEncodings(contentEncoding) {
|
|
58
|
+
return contentEncoding
|
|
59
|
+
.split(',')
|
|
60
|
+
.map((c) => c.trim().toLowerCase())
|
|
61
|
+
.filter((c) => c !== '' && c !== 'identity');
|
|
62
|
+
}
|
|
63
|
+
function createDecompressStream(contentEncoding) {
|
|
64
|
+
const codings = parseContentEncodings(contentEncoding);
|
|
65
|
+
if (codings.length === 0 || codings.length > MAX_RESPONSE_CODINGS)
|
|
66
|
+
return null;
|
|
67
|
+
const streams = [];
|
|
68
|
+
for (const coding of codings.reverse()) {
|
|
69
|
+
const stream = createSingleDecompressStream(coding);
|
|
70
|
+
if (!stream) {
|
|
71
|
+
for (const s of streams) {
|
|
72
|
+
if (!s.destroyed)
|
|
73
|
+
s.destroy();
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
streams.push(stream);
|
|
78
|
+
}
|
|
79
|
+
return { streams, decompressed: true };
|
|
80
|
+
}
|
|
81
|
+
function waitForRequestDrain(req, isAborted) {
|
|
82
|
+
if (isAborted() || req.destroyed || req.writableEnded) {
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
const cleanup = () => {
|
|
87
|
+
req.off('drain', onDrain);
|
|
88
|
+
req.off('close', onClose);
|
|
89
|
+
req.off('error', onError);
|
|
90
|
+
};
|
|
91
|
+
const onDrain = () => {
|
|
92
|
+
cleanup();
|
|
93
|
+
resolve();
|
|
94
|
+
};
|
|
95
|
+
const onClose = () => {
|
|
96
|
+
cleanup();
|
|
97
|
+
resolve();
|
|
98
|
+
};
|
|
99
|
+
const onError = (err) => {
|
|
100
|
+
cleanup();
|
|
101
|
+
reject(err);
|
|
102
|
+
};
|
|
103
|
+
req.once('drain', onDrain);
|
|
104
|
+
req.once('close', onClose);
|
|
105
|
+
req.once('error', onError);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function toIncomingHeaders(rawHeaders, dropEncodingHeaders) {
|
|
109
|
+
const out = new Headers();
|
|
110
|
+
for (const [key, value] of Object.entries(rawHeaders)) {
|
|
111
|
+
if (value === undefined)
|
|
112
|
+
continue;
|
|
113
|
+
const lower = key.toLowerCase();
|
|
114
|
+
if (dropEncodingHeaders && (lower === 'content-encoding' || lower === 'content-length')) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (Array.isArray(value)) {
|
|
118
|
+
for (const item of value) {
|
|
119
|
+
try {
|
|
120
|
+
out.append(key, item);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Ignore header values rejected by undici Web Headers validation.
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
try {
|
|
129
|
+
out.append(key, value);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// Ignore header values rejected by undici Web Headers validation.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Create a Web `fetch`-compatible function backed by dedicated HTTP/1.1
|
|
140
|
+
* `node:http` and `node:https` keep-alive agents.
|
|
141
|
+
*/
|
|
142
|
+
export function createNodeUpstreamFetch(options = {}) {
|
|
143
|
+
const keepAliveMsecs = options.keepAliveMsecs ?? 1000;
|
|
144
|
+
const maxSockets = options.maxSockets ?? 256;
|
|
145
|
+
const keepAliveTimeout = options.keepAliveTimeout;
|
|
146
|
+
const rejectUnauthorized = options.rejectUnauthorized ?? true;
|
|
147
|
+
const httpAgent = new http.Agent({
|
|
148
|
+
keepAlive: true,
|
|
149
|
+
keepAliveMsecs,
|
|
150
|
+
maxSockets,
|
|
151
|
+
...(keepAliveTimeout !== undefined ? { keepAliveTimeout } : {}),
|
|
152
|
+
});
|
|
153
|
+
const httpsAgent = new https.Agent({
|
|
154
|
+
keepAlive: true,
|
|
155
|
+
keepAliveMsecs,
|
|
156
|
+
maxSockets,
|
|
157
|
+
rejectUnauthorized,
|
|
158
|
+
...(options.ca !== undefined ? { ca: options.ca } : {}),
|
|
159
|
+
...(keepAliveTimeout !== undefined ? { keepAliveTimeout } : {}),
|
|
160
|
+
});
|
|
161
|
+
const nodeFetch = (input, init) => {
|
|
162
|
+
const reqObj = input instanceof Request ? input : null;
|
|
163
|
+
const urlStr = reqObj ? reqObj.url : input instanceof URL ? input.href : String(input);
|
|
164
|
+
const targetUrl = new URL(urlStr);
|
|
165
|
+
if (targetUrl.protocol !== 'http:' && targetUrl.protocol !== 'https:') {
|
|
166
|
+
return Promise.reject(new TypeError(`Unsupported protocol: ${targetUrl.protocol}`));
|
|
167
|
+
}
|
|
168
|
+
const method = (init?.method ?? reqObj?.method ?? 'GET').toUpperCase();
|
|
169
|
+
const headers = new Headers(init?.headers ?? reqObj?.headers);
|
|
170
|
+
if (!headers.has('accept-encoding')) {
|
|
171
|
+
headers.set('accept-encoding', 'gzip, deflate, br');
|
|
172
|
+
}
|
|
173
|
+
const signal = init?.signal ?? reqObj?.signal;
|
|
174
|
+
if (signal?.aborted) {
|
|
175
|
+
return Promise.reject(toAbortError(signal.reason));
|
|
176
|
+
}
|
|
177
|
+
const outgoingHeaders = {};
|
|
178
|
+
headers.forEach((value, key) => {
|
|
179
|
+
outgoingHeaders[key] = value;
|
|
180
|
+
});
|
|
181
|
+
const rawBody = init?.body !== undefined ? init.body : reqObj?.body ?? null;
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
let settled = false;
|
|
184
|
+
let aborted = false;
|
|
185
|
+
let activeIncoming;
|
|
186
|
+
let activeDecoders = [];
|
|
187
|
+
let activeBodyController;
|
|
188
|
+
let activeStreamEnded = false;
|
|
189
|
+
let bodyReader;
|
|
190
|
+
const noop = () => { };
|
|
191
|
+
const cleanupAbortListener = () => {
|
|
192
|
+
if (signal)
|
|
193
|
+
signal.removeEventListener('abort', onAbort);
|
|
194
|
+
};
|
|
195
|
+
const isHttps = targetUrl.protocol === 'https:';
|
|
196
|
+
const requestFn = isHttps ? https.request : http.request;
|
|
197
|
+
const defaultPort = isHttps ? 443 : 80;
|
|
198
|
+
const hostname = targetUrl.hostname.startsWith('[') && targetUrl.hostname.endsWith(']')
|
|
199
|
+
? targetUrl.hostname.slice(1, -1)
|
|
200
|
+
: targetUrl.hostname;
|
|
201
|
+
const clientReq = requestFn({
|
|
202
|
+
protocol: targetUrl.protocol,
|
|
203
|
+
hostname,
|
|
204
|
+
port: targetUrl.port ? Number(targetUrl.port) : defaultPort,
|
|
205
|
+
method,
|
|
206
|
+
path: `${targetUrl.pathname}${targetUrl.search}`,
|
|
207
|
+
headers: outgoingHeaders,
|
|
208
|
+
agent: isHttps ? httpsAgent : httpAgent,
|
|
209
|
+
});
|
|
210
|
+
const tearDownStreams = (err) => {
|
|
211
|
+
if (bodyReader) {
|
|
212
|
+
void bodyReader.cancel(err).catch(() => undefined);
|
|
213
|
+
bodyReader = undefined;
|
|
214
|
+
}
|
|
215
|
+
for (const decoder of activeDecoders) {
|
|
216
|
+
if (!decoder.destroyed) {
|
|
217
|
+
decoder.destroy(err);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (activeIncoming && !activeIncoming.destroyed) {
|
|
221
|
+
activeIncoming.destroy(err);
|
|
222
|
+
}
|
|
223
|
+
if (!clientReq.destroyed) {
|
|
224
|
+
clientReq.destroy(err);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const failBeforeResponse = (err) => {
|
|
228
|
+
if (!settled) {
|
|
229
|
+
settled = true;
|
|
230
|
+
cleanupAbortListener();
|
|
231
|
+
tearDownStreams(err instanceof Error ? err : new Error(String(err)));
|
|
232
|
+
reject(err);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const onAbort = () => {
|
|
236
|
+
if (aborted)
|
|
237
|
+
return;
|
|
238
|
+
aborted = true;
|
|
239
|
+
const abortErr = toAbortError(signal?.reason);
|
|
240
|
+
cleanupAbortListener();
|
|
241
|
+
if (!settled) {
|
|
242
|
+
settled = true;
|
|
243
|
+
tearDownStreams(abortErr);
|
|
244
|
+
reject(abortErr);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (!activeStreamEnded && activeBodyController) {
|
|
248
|
+
activeStreamEnded = true;
|
|
249
|
+
try {
|
|
250
|
+
activeBodyController.error(abortErr);
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
// Stream controller already closed/errored.
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
tearDownStreams(abortErr);
|
|
257
|
+
};
|
|
258
|
+
if (signal) {
|
|
259
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
260
|
+
}
|
|
261
|
+
// Keep an error sink attached for the entire ClientRequest lifetime so
|
|
262
|
+
// late teardown errors (after headers or abort) never trigger uncaughtException.
|
|
263
|
+
clientReq.on('error', (err) => {
|
|
264
|
+
if (!settled) {
|
|
265
|
+
failBeforeResponse(err);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (!activeStreamEnded && activeBodyController) {
|
|
269
|
+
activeStreamEnded = true;
|
|
270
|
+
try {
|
|
271
|
+
activeBodyController.error(err);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// Already closed/errored.
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
tearDownStreams(err);
|
|
278
|
+
});
|
|
279
|
+
clientReq.on('response', (incoming) => {
|
|
280
|
+
activeIncoming = incoming;
|
|
281
|
+
// Attach a persistent sink immediately so HTTP socket teardown or idle
|
|
282
|
+
// closes can never emit an unhandled 'error' on IncomingMessage.
|
|
283
|
+
incoming.on('error', noop);
|
|
284
|
+
if (aborted || signal?.aborted) {
|
|
285
|
+
const abortErr = toAbortError(signal?.reason);
|
|
286
|
+
if (!settled) {
|
|
287
|
+
settled = true;
|
|
288
|
+
cleanupAbortListener();
|
|
289
|
+
tearDownStreams(abortErr);
|
|
290
|
+
reject(abortErr);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
tearDownStreams(abortErr);
|
|
294
|
+
}
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const status = incoming.statusCode ?? 200;
|
|
298
|
+
const statusText = incoming.statusMessage ?? '';
|
|
299
|
+
const hasNullBody = method === 'HEAD' || NULL_BODY_STATUSES.has(status);
|
|
300
|
+
if (hasNullBody) {
|
|
301
|
+
settled = true;
|
|
302
|
+
cleanupAbortListener();
|
|
303
|
+
const resHeaders = toIncomingHeaders(incoming.headers, false);
|
|
304
|
+
incoming.resume();
|
|
305
|
+
resolve(new Response(null, { status, statusText, headers: resHeaders }));
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const rawEncoding = typeof incoming.headers['content-encoding'] === 'string'
|
|
309
|
+
? incoming.headers['content-encoding']
|
|
310
|
+
: '';
|
|
311
|
+
const nonIdentityCodings = parseContentEncodings(rawEncoding);
|
|
312
|
+
const decoderSpec = createDecompressStream(rawEncoding);
|
|
313
|
+
const resHeaders = toIncomingHeaders(incoming.headers, Boolean(decoderSpec?.decompressed));
|
|
314
|
+
if (nonIdentityCodings.length > 0 && !decoderSpec) {
|
|
315
|
+
// Signal to core proxy.ts that the response body remains compressed so
|
|
316
|
+
// it preserves content-encoding and skips UTF-8 telemetry scanning.
|
|
317
|
+
resHeaders.set('x-fiche-undecoded-encoding', '1');
|
|
318
|
+
}
|
|
319
|
+
let bodyStream = incoming;
|
|
320
|
+
if (decoderSpec) {
|
|
321
|
+
activeDecoders = decoderSpec.streams;
|
|
322
|
+
for (const dec of decoderSpec.streams) {
|
|
323
|
+
dec.on('error', noop);
|
|
324
|
+
bodyStream = bodyStream.pipe(dec);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
settled = true;
|
|
328
|
+
let cleanupStream;
|
|
329
|
+
const webBody = new ReadableStream({
|
|
330
|
+
start(controller) {
|
|
331
|
+
activeBodyController = controller;
|
|
332
|
+
bodyStream.pause();
|
|
333
|
+
cleanupStream = () => {
|
|
334
|
+
cleanupAbortListener();
|
|
335
|
+
bodyStream.off('data', onData);
|
|
336
|
+
bodyStream.off('end', onEnd);
|
|
337
|
+
bodyStream.off('error', onStreamError);
|
|
338
|
+
incoming.off('aborted', onUpstreamAborted);
|
|
339
|
+
incoming.off('close', onUpstreamClose);
|
|
340
|
+
if (decoderSpec) {
|
|
341
|
+
incoming.off('error', onStreamError);
|
|
342
|
+
for (const dec of decoderSpec.streams) {
|
|
343
|
+
if (dec !== bodyStream)
|
|
344
|
+
dec.off('error', onStreamError);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
const failStream = (err) => {
|
|
349
|
+
if (activeStreamEnded)
|
|
350
|
+
return;
|
|
351
|
+
activeStreamEnded = true;
|
|
352
|
+
cleanupStream?.();
|
|
353
|
+
tearDownStreams(err);
|
|
354
|
+
try {
|
|
355
|
+
controller.error(err);
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
// Controller already closed or errored.
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
const onData = (chunk) => {
|
|
362
|
+
if (activeStreamEnded)
|
|
363
|
+
return;
|
|
364
|
+
bodyStream.pause();
|
|
365
|
+
try {
|
|
366
|
+
// Copy out of Node's shared slab pool so callers retaining
|
|
367
|
+
// chunks see immutable bytes.
|
|
368
|
+
controller.enqueue(new Uint8Array(chunk));
|
|
369
|
+
}
|
|
370
|
+
catch (enqueueErr) {
|
|
371
|
+
failStream(enqueueErr instanceof Error ? enqueueErr : new Error(String(enqueueErr)));
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
const onEnd = () => {
|
|
375
|
+
if (activeStreamEnded)
|
|
376
|
+
return;
|
|
377
|
+
activeStreamEnded = true;
|
|
378
|
+
cleanupStream?.();
|
|
379
|
+
try {
|
|
380
|
+
controller.close();
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
// Controller already closed.
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
const onStreamError = (err) => {
|
|
387
|
+
failStream(err);
|
|
388
|
+
};
|
|
389
|
+
const onUpstreamAborted = () => {
|
|
390
|
+
if (!activeStreamEnded) {
|
|
391
|
+
failStream(new Error('upstream response aborted'));
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
const onUpstreamClose = () => {
|
|
395
|
+
if (!activeStreamEnded && !incoming.complete) {
|
|
396
|
+
failStream(new Error('upstream response closed prematurely'));
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
bodyStream.on('data', onData);
|
|
400
|
+
bodyStream.once('end', onEnd);
|
|
401
|
+
bodyStream.on('error', onStreamError);
|
|
402
|
+
if (decoderSpec) {
|
|
403
|
+
incoming.on('error', onStreamError);
|
|
404
|
+
for (const dec of decoderSpec.streams) {
|
|
405
|
+
if (dec !== bodyStream)
|
|
406
|
+
dec.on('error', onStreamError);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
incoming.once('aborted', onUpstreamAborted);
|
|
410
|
+
incoming.once('close', onUpstreamClose);
|
|
411
|
+
},
|
|
412
|
+
pull() {
|
|
413
|
+
if (!activeStreamEnded && !aborted) {
|
|
414
|
+
bodyStream.resume();
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
cancel() {
|
|
418
|
+
activeStreamEnded = true;
|
|
419
|
+
cleanupStream?.();
|
|
420
|
+
cleanupAbortListener();
|
|
421
|
+
tearDownStreams();
|
|
422
|
+
},
|
|
423
|
+
});
|
|
424
|
+
resolve(new Response(webBody, { status, statusText, headers: resHeaders }));
|
|
425
|
+
});
|
|
426
|
+
// Write request body (empty, buffered, or streamed).
|
|
427
|
+
if (rawBody === null || rawBody === undefined) {
|
|
428
|
+
clientReq.end();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
if (isReadableStreamBody(rawBody)) {
|
|
432
|
+
const reader = rawBody.getReader();
|
|
433
|
+
bodyReader = reader;
|
|
434
|
+
void (async () => {
|
|
435
|
+
try {
|
|
436
|
+
while (!aborted && !clientReq.destroyed) {
|
|
437
|
+
const { done, value } = await reader.read();
|
|
438
|
+
if (aborted || clientReq.destroyed) {
|
|
439
|
+
await reader.cancel().catch(() => undefined);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (done) {
|
|
443
|
+
if (!clientReq.destroyed && !clientReq.writableEnded) {
|
|
444
|
+
clientReq.end();
|
|
445
|
+
}
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (value && value.byteLength > 0) {
|
|
449
|
+
const ok = clientReq.write(value);
|
|
450
|
+
if (!ok) {
|
|
451
|
+
await waitForRequestDrain(clientReq, () => aborted);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
catch (err) {
|
|
457
|
+
failBeforeResponse(err);
|
|
458
|
+
}
|
|
459
|
+
finally {
|
|
460
|
+
if (bodyReader === reader)
|
|
461
|
+
bodyReader = undefined;
|
|
462
|
+
try {
|
|
463
|
+
reader.releaseLock();
|
|
464
|
+
}
|
|
465
|
+
catch {
|
|
466
|
+
// Reader already released or errored.
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
})();
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
const payload = resolveBufferedBody(rawBody);
|
|
474
|
+
clientReq.end(payload);
|
|
475
|
+
}
|
|
476
|
+
catch (err) {
|
|
477
|
+
failBeforeResponse(err);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
};
|
|
481
|
+
return {
|
|
482
|
+
fetch: nodeFetch,
|
|
483
|
+
destroy: () => {
|
|
484
|
+
httpAgent.destroy();
|
|
485
|
+
httpsAgent.destroy();
|
|
486
|
+
},
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
//# sourceMappingURL=node-fetch.js.map
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node entrypoint — `node:http` server + minimal CLI flag parsing.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the runtime-agnostic `createProxy` from src/core/proxy.ts. The
|
|
5
|
+
* heavy lifting (transform, render, PNG) is identical to the Worker
|
|
6
|
+
* version; only the request/response plumbing differs.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=node.d.ts.map
|