-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-capture.mjs
More file actions
169 lines (152 loc) · 5.5 KB
/
Copy pathextract-capture.mjs
File metadata and controls
169 lines (152 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { publicFetch } from "./extract-batch-c1/public-fetch.mjs";
/**
* Shared capture/decode helpers for single-URL extract/read and batch transport.
* Labels the actual no-JS HTTP capture. Does not claim discussion completeness.
*/
export const EXTRACT_UA = "Mozilla/5.0 (compatible; SameDayDeskExtractor/1.0; +https://samedaydesk.com)";
export const EXTRACT_MAX_BODY_BYTES = 3_000_000;
export const EXTRACT_TIMEOUT_MS = 12_000;
export const EXTRACT_TEXT_EXCERPT_CHARS = 1200;
export const READ_MARKDOWN_MAX_CHARS = 40_000;
const CHARSET_ALIASES = Object.freeze({
utf8: "utf-8",
"utf-8": "utf-8",
latin1: "iso-8859-1",
"latin-1": "iso-8859-1",
"iso-8859-1": "iso-8859-1",
"iso8859-1": "iso-8859-1",
"windows-1252": "windows-1252",
"cp1252": "windows-1252",
"windows-1251": "windows-1251",
"iso-8859-2": "iso-8859-2",
"shift_jis": "shift_jis",
"euc-jp": "euc-jp",
"gbk": "gbk",
"gb2312": "gbk",
"big5": "big5",
});
export const CAPTURE_CHARSET_SOURCES = Object.freeze([
"content-type",
"html-meta",
"default-utf-8",
"invalid-charset-fallback",
]);
function normalizeCharset(raw) {
if (!raw) return null;
const key = String(raw).trim().toLowerCase().replace(/_/g, "-");
return CHARSET_ALIASES[key] || key;
}
export function parseCharsetFromContentType(contentType) {
if (!contentType) return null;
const match = String(contentType).match(/charset\s*=\s*["']?([^"';\s]+)/i);
return match ? normalizeCharset(match[1]) : null;
}
export function parseCharsetFromHtmlBytes(bytes) {
const head = Buffer.from(bytes.subarray(0, Math.min(bytes.byteLength, 4096))).toString("latin1");
const charset = head.match(/<meta\b[^>]*\bcharset\s*=\s*["']?\s*([^\s"'>;]+)/i)
|| head.match(/<meta\b[^>]*http-equiv\s*=\s*["']content-type["'][^>]*content\s*=\s*["'][^"']*charset=([^\s"';]+)/i)
|| head.match(/<meta\b[^>]*content\s*=\s*["'][^"']*charset=([^\s"';]+)[^"']*["'][^>]*http-equiv\s*=\s*["']content-type["']/i);
return charset ? normalizeCharset(charset[1]) : null;
}
function decoderLabel(charset) {
try {
return new TextDecoder(charset).encoding;
} catch {
return null;
}
}
export function decodeHttpBody(bytes, { contentType, allowHtmlMeta = true } = {}) {
const buffer = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes || []);
let charset = parseCharsetFromContentType(contentType);
let charsetSource = charset ? "content-type" : null;
if (!charset && allowHtmlMeta && buffer.byteLength) {
charset = parseCharsetFromHtmlBytes(buffer);
if (charset) charsetSource = "html-meta";
}
if (!charset) {
charset = "utf-8";
charsetSource = "default-utf-8";
}
const supported = decoderLabel(charset);
if (!supported) {
charset = "utf-8";
charsetSource = "invalid-charset-fallback";
}
charset = supported || "utf-8";
const html = new TextDecoder(charset, { fatal: false }).decode(buffer);
return { html, charset, charsetSource, bytes: buffer.byteLength };
}
export function classifyHttpStatus(status) {
const code = Number(status);
if (!Number.isInteger(code) || code < 100 || code > 599) {
return {
sourceOk: false,
error: { code: "invalid_status", message: "source HTTP status missing or invalid" },
};
}
if (code >= 200 && code < 300) {
return { sourceOk: true, error: null };
}
const kind = code === 429 ? "source rate-limited" : code >= 500 ? "source error" : "source refused";
return {
sourceOk: false,
error: { code: `http_${code}`, message: `${kind}: HTTP ${code}` },
};
}
export function buildCapture({
maxBodyBytes = EXTRACT_MAX_BODY_BYTES,
textExcerptLimitChars = EXTRACT_TEXT_EXCERPT_CHARS,
markdownLimitChars = null,
bodyBytes = 0,
bodyTruncated = false,
textTruncated = false,
charset = "utf-8",
charsetSource = "default-utf-8",
} = {}) {
return {
method: "http-get-no-javascript",
javascriptExecuted: false,
maxBodyBytes,
textExcerptLimitChars,
markdownLimitChars,
bodyBytes,
bodyTruncated: Boolean(bodyTruncated),
textTruncated: Boolean(textTruncated),
charset: charset || null,
charsetSource: CAPTURE_CHARSET_SOURCES.includes(charsetSource) ? charsetSource : "default-utf-8",
};
}
export function concatBytes(chunks) {
const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
const out = new Uint8Array(total);
let offset = 0;
for (const chunk of chunks) {
out.set(chunk, offset);
offset += chunk.byteLength;
}
return out;
}
export function resolveExtractFetch() {
// In-process test injection only; no HTTP/query/env binding. Production uses pinned DNS.
return globalThis.__SAMEDAYDESK_EXTRACT_FETCH__ || publicFetch;
}
export function resolveExtractTimeoutMs(fallback = EXTRACT_TIMEOUT_MS) {
const override = globalThis.__SAMEDAYDESK_EXTRACT_TIMEOUT_MS__;
if (Number.isSafeInteger(override) && override >= 1 && override <= 60_000) return override;
return fallback;
}
export function fetchFailureCode(error) {
if (error?.name === "AbortError" || error?.code === "ABORT_ERR") return "timeout";
return typeof error?.code === "string" && error.code ? error.code : "fetch_error";
}
// Enforce the deadline even if an injected response reader ignores AbortSignal.
export async function abortableRead(promise, signal) {
signal.throwIfAborted();
let listener;
try {
return await Promise.race([promise, new Promise((_, reject) => {
listener = () => reject(signal.reason);
signal.addEventListener("abort", listener, { once: true });
})]);
} finally { signal.removeEventListener("abort", listener); }
}