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/url.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Python's urllib.parse (urlsplit, urljoin, unquote, parse_qsl), ported as is: WHATWG URL reads addresses differently.
|
|
3
|
+
const net = require("net");
|
|
4
|
+
|
|
5
|
+
const SCHEME_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.";
|
|
6
|
+
const USES_RELATIVE = ["", "ftp", "http", "gopher", "nntp", "imap", "wais", "file", "https", "shttp", "mms", "prospero",
|
|
7
|
+
"rtsp", "rtsps", "rtspu", "sftp", "svn", "svn+ssh", "ws", "wss"];
|
|
8
|
+
const USES_NETLOC = ["", "ftp", "http", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "https", "shttp",
|
|
9
|
+
"snews", "prospero", "rtsp", "rtsps", "rtspu", "rsync", "svn", "svn+ssh", "sftp", "nfs", "git", "git+ssh", "ws", "wss",
|
|
10
|
+
"itms-services"];
|
|
11
|
+
const USES_PARAMS = ["", "ftp", "hdl", "prospero", "http", "imap", "https", "shttp", "rtsp", "rtsps", "rtspu", "sip",
|
|
12
|
+
"sips", "mms", "sftp", "tel"];
|
|
13
|
+
|
|
14
|
+
/** str.partition. */
|
|
15
|
+
function partition(s, sep) {
|
|
16
|
+
const i = s.indexOf(sep);
|
|
17
|
+
return i < 0 ? [s, "", ""] : [s.slice(0, i), sep, s.slice(i + sep.length)];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** str.rpartition. */
|
|
21
|
+
function rpartition(s, sep) {
|
|
22
|
+
const i = s.lastIndexOf(sep);
|
|
23
|
+
return i < 0 ? ["", "", s] : [s.slice(0, i), sep, s.slice(i + sep.length)];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Python's _check_bracketed_host: a valid IPv6 or IPvFuture address, else throws. */
|
|
27
|
+
function checkBracketedHost(host) {
|
|
28
|
+
if (host.startsWith("v")) {
|
|
29
|
+
if (!/^v[a-fA-F0-9]+\.[a-zA-Z0-9\-._~!$&'()*+,;=:]+$/.test(host)) throw new Error("IPvFuture address is invalid");
|
|
30
|
+
} else if (!net.isIPv6(host.split("%")[0])) {
|
|
31
|
+
throw new Error("invalid IPv6");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** urlsplit: [scheme, netloc, path, query, fragment]; throws where Python raises ValueError. */
|
|
36
|
+
function urlsplit(url) {
|
|
37
|
+
url = url.replace(/^[\x00-\x20]+/, "").replace(/[\t\r\n]/g, "");
|
|
38
|
+
let scheme = "";
|
|
39
|
+
let netloc = "";
|
|
40
|
+
let query = "";
|
|
41
|
+
let fragment = "";
|
|
42
|
+
const i = url.indexOf(":");
|
|
43
|
+
if (i > 0 && /^[A-Za-z]/.test(url)) {
|
|
44
|
+
let ok = true;
|
|
45
|
+
for (const c of url.slice(0, i)) if (!SCHEME_CHARS.includes(c)) { ok = false; break; }
|
|
46
|
+
if (ok) { scheme = url.slice(0, i).toLowerCase(); url = url.slice(i + 1); }
|
|
47
|
+
}
|
|
48
|
+
if (url.slice(0, 2) === "//") {
|
|
49
|
+
let delim = url.length;
|
|
50
|
+
for (const c of "/?#") {
|
|
51
|
+
const w = url.indexOf(c, 2);
|
|
52
|
+
if (w >= 0) delim = Math.min(delim, w);
|
|
53
|
+
}
|
|
54
|
+
netloc = url.slice(2, delim);
|
|
55
|
+
url = url.slice(delim);
|
|
56
|
+
if ((netloc.includes("[") && !netloc.includes("]")) || (netloc.includes("]") && !netloc.includes("["))) {
|
|
57
|
+
throw new Error("Invalid IPv6 URL");
|
|
58
|
+
}
|
|
59
|
+
if (netloc.includes("[") && netloc.includes("]")) {
|
|
60
|
+
const hp = rpartition(netloc, "@")[2];
|
|
61
|
+
const [before, open, bracketed] = partition(hp, "[");
|
|
62
|
+
let host;
|
|
63
|
+
if (open) {
|
|
64
|
+
if (before) throw new Error("Invalid IPv6 URL");
|
|
65
|
+
const [h, , port] = partition(bracketed, "]");
|
|
66
|
+
if (port && !port.startsWith(":")) throw new Error("Invalid IPv6 URL");
|
|
67
|
+
host = h;
|
|
68
|
+
} else host = partition(hp, ":")[0];
|
|
69
|
+
checkBracketedHost(host);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (url.includes("#")) [url, fragment] = [url.slice(0, url.indexOf("#")), url.slice(url.indexOf("#") + 1)];
|
|
73
|
+
if (url.includes("?")) [url, query] = [url.slice(0, url.indexOf("?")), url.slice(url.indexOf("?") + 1)];
|
|
74
|
+
if (netloc && /[^\x00-\x7f]/.test(netloc)) {
|
|
75
|
+
const n = netloc.replace(/[@:#?]/g, "");
|
|
76
|
+
const n2 = n.normalize("NFKC");
|
|
77
|
+
if (n !== n2) for (const c of "/?#@:") if (n2.includes(c)) throw new Error("netloc contains invalid characters");
|
|
78
|
+
}
|
|
79
|
+
return [scheme, netloc, url, query, fragment];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The .hostname of a urlsplit result (lowercase, zone kept), or null. */
|
|
83
|
+
function hostname(netloc) {
|
|
84
|
+
const hostinfo = rpartition(netloc, "@")[2];
|
|
85
|
+
const [, open, bracketed] = partition(hostinfo, "[");
|
|
86
|
+
const host = open ? partition(bracketed, "]")[0] : partition(hostinfo, ":")[0];
|
|
87
|
+
if (!host) return null;
|
|
88
|
+
const [h, pct, zone] = partition(host, "%");
|
|
89
|
+
return h.toLowerCase() + pct + zone;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** urlparse: urlsplit plus ;params. */
|
|
93
|
+
function urlparse(url, defScheme) {
|
|
94
|
+
let [scheme, netloc, path, query, fragment] = urlsplit(url);
|
|
95
|
+
if (!scheme) scheme = defScheme;
|
|
96
|
+
let params = "";
|
|
97
|
+
if (USES_PARAMS.includes(scheme) && path.includes(";")) {
|
|
98
|
+
let i;
|
|
99
|
+
if (path.includes("/")) {
|
|
100
|
+
i = path.indexOf(";", path.lastIndexOf("/"));
|
|
101
|
+
} else i = path.indexOf(";");
|
|
102
|
+
if (i >= 0) { params = path.slice(i + 1); path = path.slice(0, i); }
|
|
103
|
+
}
|
|
104
|
+
return [scheme, netloc, path, params, query, fragment];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** urlunsplit. */
|
|
108
|
+
function urlunsplit(scheme, netloc, url, query, fragment) {
|
|
109
|
+
if (netloc || (scheme && USES_NETLOC.includes(scheme) && url.slice(0, 2) !== "//")) {
|
|
110
|
+
if (url && url[0] !== "/") url = "/" + url;
|
|
111
|
+
url = "//" + (netloc || "") + url;
|
|
112
|
+
}
|
|
113
|
+
if (scheme) url = scheme + ":" + url;
|
|
114
|
+
if (query) url = url + "?" + query;
|
|
115
|
+
if (fragment) url = url + "#" + fragment;
|
|
116
|
+
return url;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** urlunparse. */
|
|
120
|
+
function urlunparse(scheme, netloc, url, params, query, fragment) {
|
|
121
|
+
if (params) url = url + ";" + params;
|
|
122
|
+
return urlunsplit(scheme, netloc, url, query, fragment);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** urljoin. */
|
|
126
|
+
function urljoin(base, url) {
|
|
127
|
+
if (!base) return url;
|
|
128
|
+
if (!url) return base;
|
|
129
|
+
const [bscheme, bnetloc, bpath, bparams, bquery] = urlparse(base, "");
|
|
130
|
+
let [scheme, netloc, path, params, query, fragment] = urlparse(url, bscheme);
|
|
131
|
+
if (scheme !== bscheme || !USES_RELATIVE.includes(scheme)) return url;
|
|
132
|
+
if (USES_NETLOC.includes(scheme)) {
|
|
133
|
+
if (netloc) return urlunparse(scheme, netloc, path, params, query, fragment);
|
|
134
|
+
netloc = bnetloc;
|
|
135
|
+
}
|
|
136
|
+
if (!path && !params) {
|
|
137
|
+
path = bpath;
|
|
138
|
+
params = bparams;
|
|
139
|
+
if (!query) query = bquery;
|
|
140
|
+
return urlunparse(scheme, netloc, path, params, query, fragment);
|
|
141
|
+
}
|
|
142
|
+
const baseParts = bpath.split("/");
|
|
143
|
+
if (baseParts[baseParts.length - 1] !== "") baseParts.pop();
|
|
144
|
+
let segments;
|
|
145
|
+
if (path[0] === "/") segments = path.split("/");
|
|
146
|
+
else {
|
|
147
|
+
segments = baseParts.concat(path.split("/"));
|
|
148
|
+
if (segments.length > 2) {
|
|
149
|
+
segments = [segments[0], ...segments.slice(1, -1).filter(Boolean), segments[segments.length - 1]];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const resolved = [];
|
|
153
|
+
for (const seg of segments) {
|
|
154
|
+
if (seg === "..") resolved.pop();
|
|
155
|
+
else if (seg === ".") continue;
|
|
156
|
+
else resolved.push(seg);
|
|
157
|
+
}
|
|
158
|
+
const last = segments[segments.length - 1];
|
|
159
|
+
if (last === "." || last === "..") resolved.push("");
|
|
160
|
+
return urlunparse(scheme, netloc, resolved.join("/") || "/", params, query, fragment);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** _unquote_impl on an ASCII run, then UTF-8 with replacement. */
|
|
164
|
+
function unquoteAscii(s) {
|
|
165
|
+
const bits = s.split("%");
|
|
166
|
+
if (bits.length === 1) return s;
|
|
167
|
+
const bytes = [];
|
|
168
|
+
const push = (t) => { for (let i = 0; i < t.length; i++) bytes.push(t.charCodeAt(i)); };
|
|
169
|
+
push(bits[0]);
|
|
170
|
+
for (const item of bits.slice(1)) {
|
|
171
|
+
if (/^[0-9a-fA-F]{2}/.test(item)) {
|
|
172
|
+
bytes.push(parseInt(item.slice(0, 2), 16));
|
|
173
|
+
push(item.slice(2));
|
|
174
|
+
} else {
|
|
175
|
+
bytes.push(0x25);
|
|
176
|
+
push(item);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return new TextDecoder("utf-8").decode(Uint8Array.from(bytes));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** unquote. */
|
|
183
|
+
function unquote(s) {
|
|
184
|
+
if (!s.includes("%")) return s;
|
|
185
|
+
return s.replace(/[\x00-\x7f]+/g, unquoteAscii);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** parse_qsl(qs, keep_blank_values=True). */
|
|
189
|
+
function parseQsl(qs) {
|
|
190
|
+
if (!qs) return [];
|
|
191
|
+
const r = [];
|
|
192
|
+
for (const nv of qs.split("&")) {
|
|
193
|
+
if (!nv) continue;
|
|
194
|
+
const [name, , value] = partition(nv, "=");
|
|
195
|
+
r.push([unquote(name.replace(/\+/g, " ")), unquote(value.replace(/\+/g, " "))]);
|
|
196
|
+
}
|
|
197
|
+
return r;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
module.exports = { urlsplit, urljoin, unquote, parseQsl, hostname };
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "falseok",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"description": "Catch the 200 OK that isn't: tell when a scraped page is really a block, a login wall or an empty shell.",
|
|
5
|
+
"keywords": ["scraping", "web-scraping", "captcha", "block-detection", "soft-404", "data-quality", "monitoring", "crawler"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Benchmarked",
|
|
8
|
+
"homepage": "https://github.com/benchmarked-hq/falseok/tree/main/js#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/benchmarked-hq/falseok.git",
|
|
12
|
+
"directory": "js"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/benchmarked-hq/falseok/issues"
|
|
16
|
+
},
|
|
17
|
+
"main": "index.js",
|
|
18
|
+
"types": "index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"import": "./index.mjs",
|
|
23
|
+
"require": "./index.js"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"falseok": "bin/falseok.js"
|
|
29
|
+
},
|
|
30
|
+
"files": ["index.js", "index.mjs", "index.d.ts", "bin/falseok.js", "lib/*.js", "lib/*.json", "signatures.json"],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node --test"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
}
|
|
37
|
+
}
|