falseok 0.0.0-stage → 0.5.1
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/LICENSE +21 -0
- package/README.md +127 -2
- package/bin/falseok.js +501 -0
- package/index.d.ts +171 -0
- package/index.js +18 -0
- package/index.mjs +5 -0
- package/lib/api.js +371 -0
- package/lib/cjk.json +1 -0
- package/lib/classes.json +1 -0
- package/lib/decode.js +304 -0
- package/lib/memory.js +559 -0
- package/lib/page.js +1296 -0
- package/lib/pyjson.js +248 -0
- package/lib/pyre.js +185 -0
- package/lib/tables.json +1 -0
- package/lib/text.js +128 -0
- package/lib/url.js +200 -0
- package/package.json +35 -4
- package/signatures.json +682 -0
package/lib/decode.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Bytes to text the way the Python version reads them: gunzip/zlib, BOM, strict UTF-8, declared charset, cp1252.
|
|
3
|
+
const zlib = require("zlib");
|
|
4
|
+
const T = require("./tables.json");
|
|
5
|
+
const { strip } = require("./text");
|
|
6
|
+
const { rx } = require("./pyre");
|
|
7
|
+
|
|
8
|
+
let CRC_TABLE = null;
|
|
9
|
+
|
|
10
|
+
/** CRC-32 of a buffer (zlib.crc32 only exists from Node 22.2, so the table version is used everywhere). */
|
|
11
|
+
function crc32(buf) {
|
|
12
|
+
if (!CRC_TABLE) {
|
|
13
|
+
CRC_TABLE = new Int32Array(256);
|
|
14
|
+
for (let n = 0; n < 256; n++) {
|
|
15
|
+
let c = n;
|
|
16
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
17
|
+
CRC_TABLE[n] = c;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
let crc = -1;
|
|
21
|
+
for (let i = 0; i < buf.length; i++) crc = CRC_TABLE[(crc ^ buf[i]) & 0xff] ^ (crc >>> 8);
|
|
22
|
+
return (crc ^ -1) >>> 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const ALIAS = {
|
|
26
|
+
"iso-8859-1": "cp1252", "iso8859-1": "cp1252", "latin1": "cp1252", "latin-1": "cp1252", "ascii": "cp1252",
|
|
27
|
+
"us-ascii": "cp1252", "iso-8859-9": "cp1254", "iso-8859-11": "cp874", "tis-620": "cp874", "windows-874": "cp874",
|
|
28
|
+
"gb2312": "gbk", "gb_2312-80": "gbk", "x-gbk": "gbk", "shift_jis": "cp932", "shift-jis": "cp932", "sjis": "cp932",
|
|
29
|
+
"x-sjis": "cp932", "windows-31j": "cp932", "euc-kr": "cp949", "ks_c_5601-1987": "cp949", "x-euc-jp": "euc_jp",
|
|
30
|
+
};
|
|
31
|
+
const MODULES = new Set(T.modules);
|
|
32
|
+
|
|
33
|
+
/** Python's gzip.decompress: every member, CRC and size checked, zero padding allowed; null on any error. */
|
|
34
|
+
function gunzip(data) {
|
|
35
|
+
const parts = [];
|
|
36
|
+
let at = 0;
|
|
37
|
+
for (;;) {
|
|
38
|
+
if (at >= data.length) return Buffer.concat(parts);
|
|
39
|
+
if (data.length - at < 2) return null;
|
|
40
|
+
if (data[at] !== 0x1f || data[at + 1] !== 0x8b) return null;
|
|
41
|
+
if (data.length - at < 10) return null;
|
|
42
|
+
if (data[at + 2] !== 8) return null;
|
|
43
|
+
const flag = data[at + 3];
|
|
44
|
+
let p = at + 10;
|
|
45
|
+
if (flag & 4) {
|
|
46
|
+
if (p + 2 > data.length) return null;
|
|
47
|
+
p += 2 + data[p] + data[p + 1] * 256;
|
|
48
|
+
}
|
|
49
|
+
for (const bit of [8, 16]) {
|
|
50
|
+
if (flag & bit) {
|
|
51
|
+
while (p < data.length && data[p] !== 0) p++;
|
|
52
|
+
if (p >= data.length) return null;
|
|
53
|
+
p++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (flag & 2) p += 2;
|
|
57
|
+
if (p > data.length) return null;
|
|
58
|
+
let res;
|
|
59
|
+
try {
|
|
60
|
+
res = zlib.inflateRawSync(data.subarray(p), { info: true });
|
|
61
|
+
} catch (e) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const used = res.engine.bytesWritten;
|
|
65
|
+
p += used;
|
|
66
|
+
if (p + 8 > data.length) return null;
|
|
67
|
+
const crc = data.readUInt32LE(p);
|
|
68
|
+
const size = data.readUInt32LE(p + 4);
|
|
69
|
+
if (crc !== crc32(res.buffer) || size !== (res.buffer.length >>> 0)) return null;
|
|
70
|
+
parts.push(res.buffer);
|
|
71
|
+
at = p + 8;
|
|
72
|
+
while (at < data.length && data[at] === 0) at++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Python's codec name lookup: the codec module name, or null (LookupError). */
|
|
77
|
+
function lookup(name) {
|
|
78
|
+
let s = name.toLowerCase().replace(/ /g, "-");
|
|
79
|
+
let norm = "";
|
|
80
|
+
let punct = false;
|
|
81
|
+
for (const c of s) {
|
|
82
|
+
if (/[\p{L}\p{N}.]/u.test(c)) {
|
|
83
|
+
if (punct && norm) norm += "_";
|
|
84
|
+
if (c < "\x80") norm += c;
|
|
85
|
+
punct = false;
|
|
86
|
+
} else punct = true;
|
|
87
|
+
}
|
|
88
|
+
const aliased = T.aliases[norm] || T.aliases[norm.replace(/\./g, "_")];
|
|
89
|
+
for (const mod of aliased ? [aliased, norm] : [norm]) {
|
|
90
|
+
if (!mod || mod.includes(".")) continue;
|
|
91
|
+
if (MODULES.has(mod)) return mod;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Double-byte codecs decoded from Python's own tables (cjk.json, loaded on first use): WHATWG decoders map some
|
|
97
|
+
// bytes differently and replace broken ones differently.
|
|
98
|
+
const CJK_CODECS = ["gbk", "gb2312", "cp932", "shift_jis", "euc_jp", "cp949", "euc_kr", "big5", "cp950", "johab"];
|
|
99
|
+
const NONE = "";
|
|
100
|
+
let CJK = null;
|
|
101
|
+
|
|
102
|
+
/** The double-byte tables, read once. */
|
|
103
|
+
function cjk() {
|
|
104
|
+
if (CJK === null) CJK = require("./cjk.json");
|
|
105
|
+
return CJK;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A double-byte codec with Python's "replace": the longest valid sequence (3, 2, 1 bytes), else U+FFFD for one byte. */
|
|
109
|
+
function decodeCjk(data, t) {
|
|
110
|
+
const out = [];
|
|
111
|
+
const n = data.length;
|
|
112
|
+
let i = 0;
|
|
113
|
+
while (i < n) {
|
|
114
|
+
const b = data[i];
|
|
115
|
+
if (b < 0x80 && t.single[b] !== NONE) {
|
|
116
|
+
out.push(t.single[b]);
|
|
117
|
+
i++;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (b === 0x8f && i + 2 < n) {
|
|
121
|
+
const row = t.triples[data[i + 1]];
|
|
122
|
+
if (row !== undefined && row[data[i + 2]] !== NONE) {
|
|
123
|
+
out.push(row[data[i + 2]]);
|
|
124
|
+
i += 3;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (i + 1 < n) {
|
|
129
|
+
const row = t.pairs[b];
|
|
130
|
+
if (row !== undefined && row[data[i + 1]] !== NONE) {
|
|
131
|
+
out.push(row[data[i + 1]]);
|
|
132
|
+
i += 2;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
out.push(t.single[b] !== NONE ? t.single[b] : "�");
|
|
137
|
+
i++;
|
|
138
|
+
}
|
|
139
|
+
return out.join("");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** UTF-32 with Python's "replace": a BOM picks the order ("utf_32" only), invalid units and a short tail become U+FFFD. */
|
|
143
|
+
function decodeUtf32(data, codec) {
|
|
144
|
+
let le = codec !== "utf_32_be";
|
|
145
|
+
let i = 0;
|
|
146
|
+
if (codec === "utf_32" && data.length >= 4) {
|
|
147
|
+
if (data[0] === 0xff && data[1] === 0xfe && data[2] === 0 && data[3] === 0) i = 4;
|
|
148
|
+
else if (data[0] === 0 && data[1] === 0 && data[2] === 0xfe && data[3] === 0xff) { le = false; i = 4; }
|
|
149
|
+
}
|
|
150
|
+
const out = [];
|
|
151
|
+
for (; i + 4 <= data.length; i += 4) {
|
|
152
|
+
const c = le ? data.readUInt32LE(i) : data.readUInt32BE(i);
|
|
153
|
+
out.push(c > 0x10ffff || (c >= 0xd800 && c <= 0xdfff) ? "�" : String.fromCodePoint(c));
|
|
154
|
+
}
|
|
155
|
+
if (i < data.length) out.push("�");
|
|
156
|
+
return out.join("");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
160
|
+
|
|
161
|
+
/** UTF-7 with Python's "replace", ported from CPython's decoder (errors, absorbed "-", lone surrogates kept). */
|
|
162
|
+
function decodeUtf7(data) {
|
|
163
|
+
const out = [];
|
|
164
|
+
const n = data.length;
|
|
165
|
+
let s = 0;
|
|
166
|
+
let inShift = false;
|
|
167
|
+
let bits = 0;
|
|
168
|
+
let buf = 0;
|
|
169
|
+
let surrogate = 0;
|
|
170
|
+
while (s < n) {
|
|
171
|
+
const ch = data[s];
|
|
172
|
+
if (inShift) {
|
|
173
|
+
const v = B64.indexOf(String.fromCharCode(ch));
|
|
174
|
+
if (ch < 128 && v >= 0) {
|
|
175
|
+
buf = buf * 64 + v;
|
|
176
|
+
bits += 6;
|
|
177
|
+
s++;
|
|
178
|
+
if (bits >= 16) {
|
|
179
|
+
const outCh = Math.floor(buf / 2 ** (bits - 16)) & 0xffff;
|
|
180
|
+
bits -= 16;
|
|
181
|
+
buf %= 2 ** bits;
|
|
182
|
+
if (surrogate) {
|
|
183
|
+
if (outCh >= 0xdc00 && outCh <= 0xdfff) {
|
|
184
|
+
out.push(String.fromCharCode(surrogate, outCh));
|
|
185
|
+
surrogate = 0;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
out.push(String.fromCharCode(surrogate));
|
|
189
|
+
surrogate = 0;
|
|
190
|
+
}
|
|
191
|
+
if (outCh >= 0xd800 && outCh <= 0xdbff) surrogate = outCh;
|
|
192
|
+
else out.push(String.fromCharCode(outCh));
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
inShift = false;
|
|
197
|
+
if (bits > 0 && (bits >= 6 || buf !== 0)) {
|
|
198
|
+
s++;
|
|
199
|
+
out.push("�");
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (surrogate && ch <= 127 && ch !== 0x2b) out.push(String.fromCharCode(surrogate));
|
|
203
|
+
surrogate = 0;
|
|
204
|
+
if (ch === 0x2d) s++;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (ch === 0x2b) {
|
|
208
|
+
s++;
|
|
209
|
+
if (s < n && data[s] === 0x2d) {
|
|
210
|
+
s++;
|
|
211
|
+
out.push("+");
|
|
212
|
+
} else if (s < n && !(data[s] < 128 && B64.includes(String.fromCharCode(data[s])))) {
|
|
213
|
+
s++;
|
|
214
|
+
out.push("�");
|
|
215
|
+
} else {
|
|
216
|
+
inShift = true;
|
|
217
|
+
surrogate = 0;
|
|
218
|
+
bits = 0;
|
|
219
|
+
buf = 0;
|
|
220
|
+
}
|
|
221
|
+
} else if (ch <= 127) {
|
|
222
|
+
out.push(String.fromCharCode(ch));
|
|
223
|
+
s++;
|
|
224
|
+
} else {
|
|
225
|
+
s++;
|
|
226
|
+
out.push("�");
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (inShift && (surrogate || bits >= 6 || (bits > 0 && buf !== 0))) out.push("�");
|
|
230
|
+
return out.join("");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** bytes.decode(codec, "replace"); throws for a codec Python would not decode with. */
|
|
234
|
+
function decodeWith(data, codec) {
|
|
235
|
+
const table = T.single[codec];
|
|
236
|
+
if (table !== undefined) {
|
|
237
|
+
let out = "";
|
|
238
|
+
for (let i = 0; i < data.length; i += 8192) {
|
|
239
|
+
const chunk = [];
|
|
240
|
+
for (let j = i; j < Math.min(i + 8192, data.length); j++) chunk.push(table[data[j]]);
|
|
241
|
+
out += chunk.join("");
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
if (CJK_CODECS.includes(codec)) return decodeCjk(data, cjk()[codec]);
|
|
246
|
+
if (codec === "utf_32" || codec === "utf_32_le" || codec === "utf_32_be") return decodeUtf32(data, codec);
|
|
247
|
+
if (codec === "utf_7") return decodeUtf7(data);
|
|
248
|
+
const label = T.multi[codec];
|
|
249
|
+
if (label === "utf-16") {
|
|
250
|
+
// Python: a BOM picks the order and is dropped, else little-endian
|
|
251
|
+
if (data[0] === 0xfe && data[1] === 0xff) return new TextDecoder("utf-16be", { ignoreBOM: true }).decode(data.subarray(2));
|
|
252
|
+
if (data[0] === 0xff && data[1] === 0xfe) return new TextDecoder("utf-16le", { ignoreBOM: true }).decode(data.subarray(2));
|
|
253
|
+
return new TextDecoder("utf-16le", { ignoreBOM: true }).decode(data);
|
|
254
|
+
}
|
|
255
|
+
if (label) return new TextDecoder(label, { ignoreBOM: true }).decode(data);
|
|
256
|
+
throw new Error("unsupported codec " + codec);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const CHARSET_IN_TYPE = rx("charset\\s*=\\s*[\"']?([\\w.:-]+)", "i", "");
|
|
260
|
+
const META_CHARSET = /<meta[^>]+charset=["']?([A-Za-z0-9_-]+)/i;
|
|
261
|
+
|
|
262
|
+
/** (text, isUtf8): the page's text, as Python's _decode makes it. */
|
|
263
|
+
function decode(data, charset) {
|
|
264
|
+
if (data[0] === 0x1f && data[1] === 0x8b) {
|
|
265
|
+
const out = gunzip(data);
|
|
266
|
+
if (out) data = out;
|
|
267
|
+
} else if (data[0] === 0x78 && data.length > 2 && (data[0] * 256 + data[1]) % 31 === 0) {
|
|
268
|
+
try {
|
|
269
|
+
data = zlib.inflateSync(data);
|
|
270
|
+
} catch (e) { /* kept as received */ }
|
|
271
|
+
}
|
|
272
|
+
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) {
|
|
273
|
+
return new TextDecoder("utf-8", { ignoreBOM: true }).decode(data.subarray(3));
|
|
274
|
+
}
|
|
275
|
+
if ((data[0] === 0xff && data[1] === 0xfe) || (data[0] === 0xfe && data[1] === 0xff)) {
|
|
276
|
+
return decodeWith(data, "utf_16");
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data);
|
|
280
|
+
} catch (e) { /* not UTF-8 */ }
|
|
281
|
+
let declared = null;
|
|
282
|
+
if (charset && typeof charset === "string") {
|
|
283
|
+
const m = CHARSET_IN_TYPE.exec(charset);
|
|
284
|
+
declared = m ? m[1] : (!charset.includes("/") && !charset.includes(";") ? strip(charset) : null);
|
|
285
|
+
}
|
|
286
|
+
if (!declared) {
|
|
287
|
+
const m = META_CHARSET.exec(Buffer.from(data.subarray(0, 4096)).toString("latin1"));
|
|
288
|
+
declared = m ? m[1] : null;
|
|
289
|
+
}
|
|
290
|
+
if (declared) {
|
|
291
|
+
declared = strip(declared).toLowerCase();
|
|
292
|
+
declared = ALIAS[declared] || declared;
|
|
293
|
+
if (declared === "utf-8" || declared === "utf8") return new TextDecoder("utf-8", { ignoreBOM: true }).decode(data);
|
|
294
|
+
const codec = lookup(declared);
|
|
295
|
+
if (codec) {
|
|
296
|
+
try {
|
|
297
|
+
return decodeWith(data, codec);
|
|
298
|
+
} catch (e) { /* falls back to cp1252 */ }
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return decodeWith(data, "cp1252");
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
module.exports = { decode, decodeWith, lookup, gunzip, crc32 };
|