launch-film 0.3.1 → 0.3.2

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/src/stills.js CHANGED
@@ -134,7 +134,7 @@ async function drawStills(filmFile, { film, brand, clips }, { chosen, outDir, ru
134
134
  const look = film.look;
135
135
  const colours = brandColours(brand);
136
136
  const fonts = await resolveFonts({ style: look, font: colours.headingFont || "", bodyFont: colours.bodyFont || "" });
137
- if (fonts.missing.length) notes.push(`Not on Google Fonts, so the look's own typeface stood in: ${[...new Set(fonts.missing)].join(", ")}.`);
137
+ if (fonts.missing.length) notes.push(`Could not be had from Google Fonts, so the look's own typeface stood in: ${[...new Set(fonts.missing)].join(", ")}.`);
138
138
 
139
139
  // The words: the opener's name and promise, else the headline, else the
140
140
  // brand's own name and description.
@@ -219,7 +219,7 @@ async function drawStills(filmFile, { film, brand, clips }, { chosen, outDir, ru
219
219
  jobs.push({ name: target.name, W: target.W, H: target.H, use: target.use, page: { html: page.html, files, W: 1920, H, at: 0 } });
220
220
  }
221
221
  }
222
- log(`Drawing ${jobs.length} stills in the ${look} look...\n`);
222
+ log(`Drawing ${jobs.length} ${jobs.length === 1 ? "still" : "stills"} in the ${look} look...\n`);
223
223
  const shot = await snapshots(jobs.map((j) => j.page));
224
224
  const made = [];
225
225
  for (const [i, j] of jobs.entries()) {
package/src/store.js CHANGED
@@ -6,19 +6,29 @@
6
6
  // site, when the listing names one, is read with easybrandkit for its colors,
7
7
  // fonts and first screen; the app's icon and screenshots are added to that
8
8
  // kit. Without a site the kit is made from the listing alone.
9
+ //
10
+ // Each request here, the lookup, the icon and the screenshots, is held to
11
+ // the run's rule for the addresses it opens, and the maker's site is judged
12
+ // by it before easybrandkit reads it (web.js).
9
13
 
10
14
  const fs = require("fs");
11
15
  const path = require("path");
12
16
  const { readPixels } = require("./ffmpeg");
13
17
  const { save } = require("./out");
18
+ const web = require("./web");
14
19
 
15
20
  const STORE_HOSTS = /(^|\.)(apple\.com|google\.com|youtube\.com|instagram\.com|x\.com|twitter\.com|facebook\.com|linkedin\.com|tiktok\.com|linktr\.ee)$/i;
16
21
 
17
- // The App Store id and country of an address, or null.
22
+ // The App Store id and country of an address, or null. A listing is read
23
+ // at apps.apple.com and itunes.apple.com alone, the hosts easybrandkit reads
24
+ // one at: another name under them is a site of its own to easybrandkit, and
25
+ // a plan that read the lookup in its place, as one easybrandkit could not
26
+ // read, had easybrandkit read the site the listing names as the address
27
+ // given.
18
28
  function appStoreId(url) {
19
29
  try {
20
30
  const u = new URL(url);
21
- if (!/(^|\.)apps\.apple\.com$|(^|\.)itunes\.apple\.com$/i.test(u.hostname)) return null;
31
+ if (!/^(apps|itunes)\.apple\.com$/i.test(u.hostname)) return null;
22
32
  const id = /\/id(\d{5,})/.exec(u.pathname);
23
33
  if (!id) return null;
24
34
  const country = (/^\/([a-z]{2})\//i.exec(u.pathname) || [])[1] || "us";
@@ -29,29 +39,33 @@ function appStoreId(url) {
29
39
  }
30
40
  const isStoreUrl = (url) => Boolean(appStoreId(url)) || /play\.google\.com\/store\/apps/i.test(String(url));
31
41
 
32
- async function getJson(url) {
33
- const res = await fetch(url, { signal: AbortSignal.timeout(20_000) });
42
+ // Each held to the run's rule, guard (web.js).
43
+ async function getJson(url, guard) {
44
+ const res = await web.get(url, { signal: AbortSignal.timeout(20_000), guard });
34
45
  if (!res.ok) throw new Error(`HTTP ${res.status} from ${new URL(url).hostname}`);
35
- return res.json();
46
+ return JSON.parse(web.textOf(res.body));
36
47
  }
37
- async function download(url, file) {
38
- const res = await fetch(url, { signal: AbortSignal.timeout(30_000) });
48
+ async function download(url, file, guard) {
49
+ const res = await web.get(url, { signal: AbortSignal.timeout(30_000), guard });
39
50
  if (!res.ok) throw new Error(`HTTP ${res.status} for ${path.basename(file)}`);
40
- save(file, Buffer.from(await res.arrayBuffer()));
51
+ save(file, res.body);
41
52
  return file;
42
53
  }
43
54
 
44
55
  // The listing from Apple's lookup API.
45
- async function appStoreListing(url) {
56
+ async function appStoreListing(url, guard = null) {
46
57
  const at = appStoreId(url);
47
58
  if (!at) throw new Error("not an App Store address");
48
- const data = await getJson(`https://itunes.apple.com/lookup?id=${at.id}&country=${at.country}&entity=software`);
59
+ const data = await getJson(`https://itunes.apple.com/lookup?id=${at.id}&country=${at.country}&entity=software`, guard || web.ruleFor(url));
49
60
  const app = (data.results || []).find((r) => r.kind === "software" || r.trackName);
50
61
  if (!app) throw new Error(`the App Store has no app ${at.id} in ${at.country.toUpperCase()}`);
62
+ // The maker's own site: an http or https address alone, kept as the URL
63
+ // parser writes it, which writes no control character and no line break,
64
+ // so it is said on one line and easybrandkit is given a web address.
51
65
  let seller = "";
52
66
  try {
53
- const host = new URL(app.sellerUrl).hostname;
54
- if (!STORE_HOSTS.test(host)) seller = app.sellerUrl;
67
+ const site = new URL(app.sellerUrl);
68
+ if (/^https?:$/.test(site.protocol) && !STORE_HOSTS.test(site.hostname)) seller = site.href;
55
69
  } catch {
56
70
  // No site of its own.
57
71
  }
@@ -115,15 +129,25 @@ function wordsFromListing(listing) {
115
129
 
116
130
  // Makes the brand folder for an App Store address: easybrandkit on the maker's
117
131
  // site when there is one (runEasybrandkit), the listing's icon and screenshots
118
- // added to it. Returns { brandFile, words }.
119
- async function storeBrand(url, dir, { runEasybrandkit, log = () => {} }) {
120
- const listing = await appStoreListing(url);
132
+ // added to it. Returns { brandFile, words }. guard is the run's rule for the
133
+ // addresses it opens (web.js), which by default takes url for the address
134
+ // given. The maker's site is an address the listing names, not the one
135
+ // given: it is judged as such before easybrandkit reads it, and one the rule
136
+ // refuses leaves the film to the listing alone; easybrandkit is told that
137
+ // the listing named it (--named-by), so it holds that site, each hop of its
138
+ // redirects and every request of its browser to its rule for a page it
139
+ // chose, never to the one for the address given.
140
+ async function storeBrand(url, dir, { runEasybrandkit, log = () => {}, guard = null }) {
141
+ guard = guard || web.ruleFor(url);
142
+ const listing = await appStoreListing(url, guard);
121
143
  fs.mkdirSync(dir, { recursive: true });
122
144
  let brand = null;
123
- if (listing.seller) {
145
+ const barred = listing.seller ? await guard.judge(listing.seller) : "";
146
+ if (barred) log(`The maker's site ${listing.seller} ${barred}, so it was not read; the film is made from the App Store listing alone.\n`);
147
+ else if (listing.seller) {
124
148
  log(`Reading the maker's site ${listing.seller} with easybrandkit...\n`);
125
149
  try {
126
- await runEasybrandkit(listing.seller, dir, { log: () => {} });
150
+ await runEasybrandkit(listing.seller, dir, { log: () => {}, namedBy: url });
127
151
  brand = JSON.parse(fs.readFileSync(path.join(dir, "brand.json"), "utf8"));
128
152
  } catch (err) {
129
153
  log(`The maker's site could not be read (${err.message}); the film is made from the App Store listing alone.\n`);
@@ -131,13 +155,13 @@ async function storeBrand(url, dir, { runEasybrandkit, log = () => {} }) {
131
155
  }
132
156
  const assets = [];
133
157
  if (listing.icon) {
134
- await download(listing.icon, path.join(dir, "appstore-icon.png"));
158
+ await download(listing.icon, path.join(dir, "appstore-icon.png"), guard);
135
159
  assets.push({ file: "appstore-icon.png", kind: "icon", width: 1024, height: 1024, alpha: false });
136
160
  }
137
161
  for (const [i, shot] of listing.screenshots.slice(0, 3).entries()) {
138
162
  const file = `appstore-shot-${String(i + 1).padStart(2, "0")}.jpg`;
139
163
  try {
140
- await download(shot, path.join(dir, file));
164
+ await download(shot, path.join(dir, file), guard);
141
165
  const p = readPixels(path.join(dir, file), 64);
142
166
  assets.push({ file, kind: "screenshot", width: p.sourceWidth, height: p.sourceHeight, alpha: false });
143
167
  } catch (err) {
package/src/web.js ADDED
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ // ─── the addresses launch-film opens ───────────────────────────────────────
3
+ // Every request launch-film makes itself (the page a plan reads the words
4
+ // of, an App Store listing's lookup with its icon and screenshots, and the
5
+ // faces Google Fonts gives) and every request of the Chrome that reads a
6
+ // site's words (chrome.js, through its proxy, proxy.js) is held to the rule
7
+ // the kits share (address.js): the address the run was given keeps its
8
+ // scheme, host and port, and any other address is asked for only on port 80
9
+ // or 443 of the public internet, never at an address of this machine or on
10
+ // one of its networks. A name whose answers mix, some on the public internet
11
+ // and some not, is asked for nowhere, the address given included, and
12
+ // MAKERSMCP_REFUSE_ADDRESSES names more addresses that are refused, the
13
+ // address given's among them. Each hop of a redirect is judged before it is
14
+ // asked for. Each name is looked up once a run, by the run's rule, and a
15
+ // request connects only to an address that lookup found and the rule took,
16
+ // through no proxy the environment names (dial.js).
17
+
18
+ const http = require("http");
19
+ const https = require("https");
20
+ const zlib = require("zlib");
21
+ const { guardFor, refusedAddresses, interfacesUnread, UNRESOLVED } = require("./address");
22
+ const { judgedRequest } = require("./dial");
23
+
24
+ // The rule for a run that was given `given`, a web address, or none (null):
25
+ // a value of MAKERSMCP_REFUSE_ADDRESSES that does not parse fails it, in
26
+ // words that name the setting.
27
+ const ruleFor = (given = null) => guardFor(given || null, { refused: refusedAddresses() });
28
+
29
+ // The run's own rule, which the command sets for the address it was given
30
+ // (setRun), and which is one with no address given where it sets none, as
31
+ // for a render. Every request of the run and of its browser that reads a
32
+ // site goes by it, so a name is looked up once a run.
33
+ let current = null;
34
+ function setRun(given = null) {
35
+ current = ruleFor(given);
36
+ return current;
37
+ }
38
+ const run = () => current || setRun(null);
39
+
40
+ // Why a command that opens addresses cannot run, said before any work, or
41
+ // null: a MAKERSMCP_REFUSE_ADDRESSES that does not parse, or a system that
42
+ // does not let it read this machine's network interfaces, whose addresses
43
+ // and networks the rule refuses away from the address given (address.js
44
+ // ownIp), so no address could be told from one of this machine's.
45
+ function unready() {
46
+ try {
47
+ refusedAddresses();
48
+ } catch (err) {
49
+ return err.message;
50
+ }
51
+ const unread = interfacesUnread();
52
+ if (!unread) return null;
53
+ return `this machine's network interfaces could not be read: ${unread.words}. Without them launch-film cannot tell this machine's own addresses from those on the public internet, ` +
54
+ "so it opens none: run it where the system lets a program read them; on Linux, a service needs AF_NETLINK among its RestrictAddressFamilies";
55
+ }
56
+
57
+ // What a request sends besides the headers it is given, as fetch() sends
58
+ // them, so a site answers it as it answered fetch().
59
+ const ASKED = { accept: "*/*", "accept-language": "*", "sec-fetch-mode": "cors", "accept-encoding": "gzip, deflate" };
60
+
61
+ // The redirects a request follows, as fetch() follows them.
62
+ const FOLLOWED = 20;
63
+ const MOVES = new Set([301, 302, 303, 307, 308]);
64
+
65
+ // A request the rule did not let go to `at`, the address asked for itself
66
+ // (hop 0) or one its redirects led to: refused { at, why, hop }, why as the
67
+ // rule says it, to follow "it" or "which".
68
+ function refusal(raw, at, why, hop) {
69
+ return Object.assign(new Error(hop ? `${raw} led to ${at}, which ${why}` : `${raw} ${why}`), { refused: { at, why, hop } });
70
+ }
71
+
72
+ // The answer to a GET of url, connected to one of the addresses the rule
73
+ // took for its host, which addressesOf() resolves to (dial.js).
74
+ function answerOf(url, { headers, signal, addressesOf }) {
75
+ return new Promise((resolve, reject) => {
76
+ const client = url.protocol === "https:" ? https : http;
77
+ const req = client.get(url, { headers, signal, ...judgedRequest(url, addressesOf, { signal }) }, resolve);
78
+ req.on("error", reject);
79
+ });
80
+ }
81
+
82
+ // The most bytes an answer's body may come to, as it came or once
83
+ // decompressed: past it the request fails, as a small answer can unpack to
84
+ // gigabytes.
85
+ const BODY_MOST = 64 * 1024 * 1024;
86
+
87
+ // An answer's body, decompressed as fetch() decompresses one: gzip, deflate
88
+ // and br, and one in an encoding it does not know as it came; one of more
89
+ // than BODY_MOST fails the request, naming where it came from (at).
90
+ async function bodyOf(res, at) {
91
+ const codings = String(res.headers["content-encoding"] || "").toLowerCase().split(",").map((one) => one.trim()).filter((one) => one && one !== "identity");
92
+ const flush = { flush: zlib.constants.Z_SYNC_FLUSH, finishFlush: zlib.constants.Z_SYNC_FLUSH };
93
+ const decoders = [];
94
+ for (const coding of codings.reverse()) {
95
+ const decoder = coding === "gzip" || coding === "x-gzip" ? zlib.createGunzip(flush) : coding === "deflate" ? zlib.createInflate(flush) : coding === "br" ? zlib.createBrotliDecompress() : null;
96
+ if (!decoder) {
97
+ decoders.length = 0;
98
+ break;
99
+ }
100
+ decoders.push(decoder);
101
+ }
102
+ let stream = res;
103
+ for (const decoder of decoders) {
104
+ const from = stream;
105
+ from.on("error", (err) => decoder.destroy(err));
106
+ stream = from.pipe(decoder);
107
+ }
108
+ const chunks = [];
109
+ let size = 0;
110
+ try {
111
+ for await (const chunk of stream) {
112
+ size += chunk.length;
113
+ if (size > BODY_MOST) throw new Error(`${at} answered with more than ${BODY_MOST / 1024 / 1024} MB${decoders.length ? " once unpacked" : ""}, which launch-film does not read`);
114
+ chunks.push(chunk);
115
+ }
116
+ } finally {
117
+ res.destroy();
118
+ for (const decoder of decoders) decoder.destroy();
119
+ }
120
+ return Buffer.concat(chunks);
121
+ }
122
+
123
+ // A GET of raw, as fetch() makes one: its redirects followed, each hop
124
+ // judged by the rule (guard, the run's own unless another is given) before
125
+ // it is asked for, and connected only to an address the rule took. Resolves
126
+ // to { status, ok, url, headers, body }, url being where the redirects led
127
+ // and body the bytes of the answer. A hop the rule refuses fails it with
128
+ // refused (see refusal), and so does a name not found, which the rule cannot
129
+ // judge, in its words. signal gives the whole request up, its redirects and
130
+ // its body too.
131
+ async function get(raw, { headers = {}, signal = null, guard = run() } = {}) {
132
+ let url = new URL(raw);
133
+ for (let hop = 0; ; hop++) {
134
+ if (!/^https?:$/.test(url.protocol)) throw new Error(`${hop ? `${raw} led to ${url.href}, which` : raw} is not an http or https address`);
135
+ // No request goes to an address with a user name or password in it, at
136
+ // any hop, as fetch() sends none: Node's own request sends them as the
137
+ // answer to a password prompt the server did not give.
138
+ if (url.username || url.password) throw new Error(hop ? `${raw} led to ${url.href}, which has a user name or password in it` : `Request cannot be constructed from a URL that includes credentials: ${raw}`);
139
+ const why = await guard.judge(url.href);
140
+ if (why) throw refusal(raw, url.href, why, hop);
141
+ const at = url.href;
142
+ let res;
143
+ try {
144
+ res = await answerOf(url, { headers: { ...ASKED, ...headers }, signal, addressesOf: () => guard.addressesFor(at) });
145
+ } catch (err) {
146
+ // The address given at a name not found, which the rule lets go.
147
+ if (err.code === "ENOTFOUND" && err.message.endsWith(UNRESOLVED)) throw refusal(raw, at, UNRESOLVED, hop);
148
+ throw err;
149
+ }
150
+ const location = MOVES.has(res.statusCode) ? res.headers.location : undefined;
151
+ if (location === undefined) {
152
+ const body = await bodyOf(res, at);
153
+ return { status: res.statusCode, ok: res.statusCode >= 200 && res.statusCode < 300, url: url.href, headers: res.headers, body };
154
+ }
155
+ res.destroy();
156
+ if (hop >= FOLLOWED) throw new Error(`${raw} was redirected more than ${FOLLOWED} times`);
157
+ url = new URL(location, url);
158
+ }
159
+ }
160
+
161
+ // How a request the rule refused is said after "could not be read" or the
162
+ // like: "it ..." for the address asked for, "it led to ..., which ..." for a
163
+ // hop of its redirects.
164
+ const refusedWords = ({ at, why, hop }) => (hop ? `it led to ${at}, which ${why}` : `it ${why}`);
165
+
166
+ // A body as fetch()'s text() reads one: UTF-8, a byte order mark left off.
167
+ const textOf = (body) => new TextDecoder().decode(body);
168
+
169
+ module.exports = { ruleFor, setRun, run, unready, get, refusedWords, textOf };
package/src/words.js CHANGED
@@ -5,8 +5,14 @@
5
5
  // the line under each, and the numbers it is proud of. They are read from
6
6
  // the page's HTML; a page that builds its text in the browser is read in
7
7
  // Chrome instead.
8
+ //
9
+ // The page, each hop of its redirects and every request of that Chrome are
10
+ // held to the run's rule for the addresses it opens (web.js): a page the
11
+ // rule refuses is not read, and an address it refuses that a page leads to
12
+ // is not opened, the reason naming that address and why.
8
13
 
9
- const { launch, evaluate } = require("./chrome");
14
+ const { launch, evaluate, proxyFailure, proxyNotes, PROXY_SAID } = require("./chrome");
15
+ const web = require("./web");
10
16
 
11
17
  const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " ", rsquo: "\u2019", lsquo: "\u2018", rdquo: "\u201d", ldquo: "\u201c",
12
18
  hellip: "\u2026", mdash: "\u2014", ndash: "\u2013", middot: "\u00b7", bull: "\u2022", copy: "\u00a9", reg: "\u00ae", trade: "\u2122", times: "\u00d7" };
@@ -124,13 +130,41 @@ function webPage(address) {
124
130
  // before any work (programs.js): one that cannot be started is told of in
125
131
  // those words, and the plan goes on with the words of the page's HTML, as it
126
132
  // does when no Chrome is found.
127
- async function fromChrome(url) {
133
+ //
134
+ // guard is the run's rule for the addresses it opens (web.js), which by
135
+ // default takes url for the address given. A page it refuses is not opened.
136
+ // Every request of the Chrome goes through a proxy that holds it to the rule
137
+ // (chrome.js), so a page that leads to an address the rule refuses, by a
138
+ // redirect or a move of its own, ends on Chrome's error page and is not
139
+ // read: the reason names where it led and why. One the proxy could not
140
+ // reach is told in the words Chrome has for a page it could not reach by
141
+ // itself. notes, where given, takes the notes on what the proxy refused
142
+ // while the page was read, as a site's scripts or data on another port
143
+ // (chrome.js proxyNotes).
144
+ async function fromChrome(url, { guard = null, notes = null } = {}) {
128
145
  const unread = (why) => Object.assign(new Error(why), { unread: true });
129
146
  if (!webPage(url)) throw unread("it is not an http or https address");
147
+ guard = guard || web.ruleFor(url);
148
+ const barred = await guard.judge(url);
149
+ if (barred) throw unread(`it ${barred}`);
130
150
  const chrome = require("./programs").check("chrome");
131
151
  if (chrome.problem) throw new Error(chrome.problem);
132
- const browser = await launch({ W: 1440, H: 900, network: true });
152
+ const browser = await launch({ W: 1440, H: 900, network: true, guard });
133
153
  try {
154
+ // What the proxy refuses from here on is the page's (proxyNotes).
155
+ const { proxy } = browser;
156
+ proxy.reading = url;
157
+ // Why a page that ended on Chrome's error page was not read: the
158
+ // address that failed, as the frame tells it, when the rule refuses it,
159
+ // else what Chrome said of it (said), told by why the proxy could not
160
+ // reach it where Chrome says only that the proxy gave no answer.
161
+ const failedWhy = async (page, said) => {
162
+ const { frameTree } = await page.call("Page.getFrameTree").catch(() => ({}));
163
+ const at = (frameTree && frameTree.frame && frameTree.frame.unreachableUrl) || url;
164
+ const why = await guard.judge(at);
165
+ if (why) return at === url ? `it ${why}` : `it led to ${at}, which ${why}`;
166
+ return (said && PROXY_SAID.has(said) && proxyFailure(proxy, at)) || said;
167
+ };
134
168
  const page = await browser.page();
135
169
  await page.call("Page.enable");
136
170
  await page.call("Emulation.setDeviceMetricsOverride", { width: 1440, height: 900, deviceScaleFactor: 1, mobile: false });
@@ -145,7 +179,7 @@ async function fromChrome(url) {
145
179
  setTimeout(() => resolve(false), 30_000).unref();
146
180
  });
147
181
  const { errorText } = await page.call("Page.navigate", { url });
148
- if (errorText) throw unread(errorText);
182
+ if (errorText) throw unread(await failedWhy(page, errorText));
149
183
  await loaded;
150
184
  // Pages that fetch their text after load get a moment more.
151
185
  await new Promise((r) => setTimeout(r, 2500));
@@ -153,10 +187,14 @@ async function fromChrome(url) {
153
187
  // whose address passed.
154
188
  const seen = await evaluate(page, `({ at: document.URL, words: ${IN_PAGE} })`);
155
189
  const at = String(seen.at);
156
- if (/^chrome-error:/.test(at)) throw unread("it led to an address that could not be opened");
190
+ if (/^chrome-error:/.test(at)) {
191
+ const why = await failedWhy(page, "");
192
+ throw unread(why || "it led to an address that could not be opened");
193
+ }
157
194
  if (!webPage(at)) throw unread(`it went on to ${at.length > 80 ? `${at.slice(0, 80)}...` : at}, which is not a web page`);
158
195
  return seen.words;
159
196
  } finally {
197
+ if (notes) notes.push(...proxyNotes(browser.proxy.refused, browser.proxy.refusedMore));
160
198
  browser.close();
161
199
  }
162
200
  }
@@ -164,31 +202,45 @@ async function fromChrome(url) {
164
202
  // The page's words: from its HTML, or from Chrome when the HTML has no
165
203
  // headline. A page neither of them could read stops the plan, which went on
166
204
  // with no words of the page's, or with those of Chrome's error page. An
167
- // address that is not a web page's is refused before either is tried.
168
- async function readWords(url, { log = () => {} } = {}) {
205
+ // address that is not a web page's is refused before either is tried, and
206
+ // so is one the run's rule refuses (guard, web.js, which by default takes
207
+ // url for the address given). The fetch, its redirects and every request of
208
+ // the Chrome are held to that rule; notes, where given, takes the notes on
209
+ // what that Chrome's proxy refused (fromChrome).
210
+ async function readWords(url, { log = () => {}, guard = null, notes = null } = {}) {
169
211
  if (!webPage(url)) throw new Error(`the page ${url} could not be read (it is not an http or https address): give the site's address, or a words.json with --words`);
212
+ guard = guard || web.ruleFor(url);
213
+ const barred = await guard.judge(url);
214
+ if (barred) throw new Error(`the page ${url} could not be read (it ${barred}): check the address and the network, or give a words.json with --words`);
170
215
  let words = null;
171
216
  // Why the page was not read: what Chrome said of an address it could not
172
217
  // open, else what the fetch said.
173
218
  let why = "";
219
+ // The fetch's time, its body's included: a page that sent its headers and
220
+ // then stopped is given up on by it too.
221
+ const signal = AbortSignal.timeout(15_000);
174
222
  try {
175
- const res = await fetch(url, {
223
+ const res = await web.get(url, {
176
224
  headers: { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36", "Accept-Language": "en;q=0.9,*;q=0.5" },
177
- redirect: "follow",
178
- signal: AbortSignal.timeout(15_000),
225
+ signal,
226
+ guard,
179
227
  });
180
- const type = res.headers.get("content-type") || "";
181
- if (res.ok && /html/i.test(type)) words = fromHtml(await res.text());
228
+ const type = String(res.headers["content-type"] || "");
229
+ if (res.ok && /html/i.test(type)) words = fromHtml(web.textOf(res.body));
182
230
  else why = res.ok ? `${type || "a file of no type"}, not HTML` : `HTTP ${res.status}`;
183
231
  } catch (err) {
184
- // Node says "fetch failed", and why in its cause.
185
- why = (err.cause && err.cause.message) || err.message;
232
+ // An address the rule refused, said by where it is; a page that gave no
233
+ // answer in its time, said so, where Node's words for it name nothing,
234
+ // as the plain "aborted" of a body cut off in the middle; else Node's own
235
+ // words, of a cause where it gives one.
236
+ const late = signal.aborted || err.name === "TimeoutError" || (err.cause && err.cause.name === "TimeoutError");
237
+ why = err.refused ? web.refusedWords(err.refused) : late ? "it gave no answer in 15 s" : (err.cause && err.cause.message) || err.message;
186
238
  log(`The page could not be fetched (${why}); reading it in Chrome.\n`);
187
239
  }
188
240
  const enough = (w) => w && w.headings.some((h) => h.level === 1) && w.headings.length >= 3;
189
241
  if (!enough(words)) {
190
242
  try {
191
- const seen = await fromChrome(url);
243
+ const seen = await fromChrome(url, { guard, notes });
192
244
  if (seen && (!words || seen.headings.length > words.headings.length)) words = { ...(words || {}), ...seen, jsonld: (words && words.jsonld) || [] };
193
245
  } catch (err) {
194
246
  if (err.unread) why = err.message;