@parcha/pi-browser 0.1.0-beta.82 → 0.1.0-beta.83
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 +4 -0
- package/README.md +5 -2
- package/dist/core/page-scan.d.ts +12 -2
- package/dist/core/page-scan.js +20 -3
- package/dist/core/read-page-worker.js +5 -4
- package/dist/core/read-page.d.ts +23 -8
- package/dist/core/read-page.js +44 -16
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.1.0-beta.83 (2026-12-02)
|
|
6
|
+
|
|
7
|
+
- `readPage` now bounds the links it resolves, and a read whose worker runs out of memory is refused instead of being read again on the caller's thread. A page's links stop at `READ_LINKS_MAX` (20,000) or once `READ_LINKS_MAX_CHARS` (4,000,000) characters of hrefs and their base have been resolved, duplicates included; the read then carries `links_truncated: true` and its first links, and its title and text are unchanged. Before, a page under the 1,000,000-character read cap with a long `<base href>` and many short relative anchors resolved to billions of characters: the worker ran out of its heap, and the plain-text fallback then resolved them again on the event loop, holding it for minutes and filling the process's heap. A worker that runs out of memory now rejects the read with `PageReadOutOfMemory` (`code` `PAGE_READ_OUT_OF_MEMORY`), which `web_fetch` answers as a failed fetch; a worker that times out or fails any other way still falls back to the plain-text read, which the same link caps now bound. `web_fetch`'s first part of a page whose links were cut says `links_truncated: true`, so a model knows its `links` and `links_omitted` cover only the page's first links. `ReadPageOptions.heapMb` sets the worker's heap (default `READ_WORKER_HEAP_MB`, 1024, at least 16). Pages within the caps read exactly as before. <!-- changes/read-page-link-caps.md -->
|
|
8
|
+
|
|
5
9
|
## 0.1.0-beta.82 (2026-12-02)
|
|
6
10
|
|
|
7
11
|
- A run's own process no longer chooses where the server connects for its browser viewer. The runner makes the viewer's listening socket (on loopback, or on its `AGENTRUN_RUNNER_LIVE_HOST` address) and hands it to the run's process on fd 5 (`AGENTRUN_VIEWER_FD`); each hand-over names that socket's port and the runner's own host, and the process supplies only the session's tag, the viewer's path id and its token. Before, the process named the port and host itself, so code in a run could point the server's viewer check and WebSocket proxy at another service on the runner's loopback or at another listed host. <!-- changes/viewer-listener-from-runner.md -->
|
package/README.md
CHANGED
|
@@ -272,11 +272,14 @@ The pi peers are `@earendil-works/pi-durable` and `chord` `1.5.2` exactly. CI te
|
|
|
272
272
|
variant `publicDownload(url, { lookup?, fetchImpl?, signal?, maxBytes? })` (one file of any type, `{ url, status,
|
|
273
273
|
contentType, bytes }`, `bytes` null for a status that is not 2xx; the same hops, checks and pinned dial as
|
|
274
274
|
`publicFetch`, a declared length over `maxBytes` refused before the body is read and a body over it refused as it is
|
|
275
|
-
read), and `readPage(html, url, { deadlineMs?, signal? })`, with `isPrivateAddress` (which also counts the ranges in `CLUSTER_INTERNAL_CIDRS` as private, GKE's Service range
|
|
275
|
+
read), and `readPage(html, url, { deadlineMs?, heapMb?, signal? })`, with `isPrivateAddress` (which also counts the ranges in `CLUSTER_INTERNAL_CIDRS` as private, GKE's Service range
|
|
276
276
|
`34.118.224.0/20` when it is unset or blank) and `pinnedLookup`. `readPage` never holds
|
|
277
277
|
the event loop: it drops a page's script, style, svg, noscript and template blocks, cuts what is left at
|
|
278
278
|
`READ_MAX_CHARS` (`truncated`, with the page's length in `html_chars`), and converts it to markdown in a worker thread
|
|
279
|
-
within `READ_DEADLINE_MS`, falling back to the page's plain text (`converted: "text"`).
|
|
279
|
+
within `READ_DEADLINE_MS`, falling back to the page's plain text (`converted: "text"`). A worker that runs out of its
|
|
280
|
+
heap (`heapMb`, default `READ_WORKER_HEAP_MB`) is not followed by that fallback: the read rejects with
|
|
281
|
+
`PageReadOutOfMemory`. Its links stop at `READ_LINKS_MAX`, or once `READ_LINKS_MAX_CHARS` characters of hrefs and
|
|
282
|
+
their base are resolved (`links_truncated`). `browser_read`'s markdown goes
|
|
280
283
|
the same way (of the page's first article, else main, else body, with the browser's own text as its fallback), and
|
|
281
284
|
its frame scan reads the page's first 2,000,000 characters in steps that yield the loop. Outside Node's builtins and
|
|
282
285
|
undici it loads only node-html-parser and node-html-markdown, on demand and in that worker thread.
|
package/dist/core/page-scan.d.ts
CHANGED
|
@@ -139,9 +139,17 @@ export declare class FrameScan {
|
|
|
139
139
|
base: string | null;
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
|
+
/** The most links one page's read gives: a page's links past them are not resolved (`links_truncated`). */
|
|
143
|
+
export declare const READ_LINKS_MAX = 20000;
|
|
144
|
+
/** The most characters one page's links are resolved from: each href read, kept or not, costs its own length and its
|
|
145
|
+
* base's. A relative href resolves to its base and itself, so without a bound a long base and many short hrefs (a
|
|
146
|
+
* `<base href>` of 100,000 characters and 50,000 anchors, in under a million characters of HTML) resolve to billions
|
|
147
|
+
* of characters. The links held stay within a small multiple of it (percent-encoding grows a character to at most 9). */
|
|
148
|
+
export declare const READ_LINKS_MAX_CHARS = 4000000;
|
|
142
149
|
/** One pass over a page's tags, in steps so a caller on the event loop can yield between them: its title (the first
|
|
143
150
|
* title element's), the http(s) URLs its `a` elements link to (each resolved against the page's first `<base href>`,
|
|
144
|
-
* or `url`, without its fragment, once, in page order
|
|
151
|
+
* or `url`, without its fragment, once, in page order, within READ_LINKS_MAX and READ_LINKS_MAX_CHARS, `linksCut` when
|
|
152
|
+
* they stopped the resolution) and, when asked for, its plain text: the text between its tags,
|
|
145
153
|
* entities decoded and whitespace collapsed, a block's start and end a paragraph, a row's start or a `br` a line, and a
|
|
146
154
|
* row's cells joined with " | ". A "<" that starts no tag is text (tagsOf); one with no ">" after it ends the page. */
|
|
147
155
|
export declare class PageScan {
|
|
@@ -154,11 +162,13 @@ export declare class PageScan {
|
|
|
154
162
|
get done(): boolean;
|
|
155
163
|
/** Scans the tags that start in the next `chars` characters (a tag that starts in them is read whole). */
|
|
156
164
|
step(chars: number): boolean;
|
|
157
|
-
/** The title, the links and the plain text ("" when it was not asked for) of what was scanned
|
|
165
|
+
/** The title, the links and the plain text ("" when it was not asked for) of what was scanned; `linksCut` when
|
|
166
|
+
* READ_LINKS_MAX or READ_LINKS_MAX_CHARS stopped the links' resolution before the last href. */
|
|
158
167
|
result(): {
|
|
159
168
|
title: string;
|
|
160
169
|
links: string[];
|
|
161
170
|
text: string;
|
|
171
|
+
linksCut?: true;
|
|
162
172
|
};
|
|
163
173
|
}
|
|
164
174
|
export {};
|
package/dist/core/page-scan.js
CHANGED
|
@@ -885,9 +885,17 @@ export class FrameScan {
|
|
|
885
885
|
return { frames: [...this.#frames], base: this.#base };
|
|
886
886
|
}
|
|
887
887
|
}
|
|
888
|
+
/** The most links one page's read gives: a page's links past them are not resolved (`links_truncated`). */
|
|
889
|
+
export const READ_LINKS_MAX = 20_000;
|
|
890
|
+
/** The most characters one page's links are resolved from: each href read, kept or not, costs its own length and its
|
|
891
|
+
* base's. A relative href resolves to its base and itself, so without a bound a long base and many short hrefs (a
|
|
892
|
+
* `<base href>` of 100,000 characters and 50,000 anchors, in under a million characters of HTML) resolve to billions
|
|
893
|
+
* of characters. The links held stay within a small multiple of it (percent-encoding grows a character to at most 9). */
|
|
894
|
+
export const READ_LINKS_MAX_CHARS = 4_000_000;
|
|
888
895
|
/** One pass over a page's tags, in steps so a caller on the event loop can yield between them: its title (the first
|
|
889
896
|
* title element's), the http(s) URLs its `a` elements link to (each resolved against the page's first `<base href>`,
|
|
890
|
-
* or `url`, without its fragment, once, in page order
|
|
897
|
+
* or `url`, without its fragment, once, in page order, within READ_LINKS_MAX and READ_LINKS_MAX_CHARS, `linksCut` when
|
|
898
|
+
* they stopped the resolution) and, when asked for, its plain text: the text between its tags,
|
|
891
899
|
* entities decoded and whitespace collapsed, a block's start and end a paragraph, a row's start or a `br` a line, and a
|
|
892
900
|
* row's cells joined with " | ". A "<" that starts no tag is text (tagsOf); one with no ">" after it ends the page. */
|
|
893
901
|
export class PageScan {
|
|
@@ -969,7 +977,8 @@ export class PageScan {
|
|
|
969
977
|
// A NUL is a space here: it would read as CELL.
|
|
970
978
|
this.#text.push((raw.includes("&") ? this.decode(raw) : raw).replace(/[\s\u0000]+/g, " "));
|
|
971
979
|
}
|
|
972
|
-
/** The title, the links and the plain text ("" when it was not asked for) of what was scanned
|
|
980
|
+
/** The title, the links and the plain text ("" when it was not asked for) of what was scanned; `linksCut` when
|
|
981
|
+
* READ_LINKS_MAX or READ_LINKS_MAX_CHARS stopped the links' resolution before the last href. */
|
|
973
982
|
result() {
|
|
974
983
|
let base = this.url;
|
|
975
984
|
if (this.#base !== null) {
|
|
@@ -979,7 +988,15 @@ export class PageScan {
|
|
|
979
988
|
catch { /* a base that does not parse: the page's own URL stands */ }
|
|
980
989
|
}
|
|
981
990
|
const links = new Set();
|
|
991
|
+
let spent = 0;
|
|
992
|
+
let linksCut = false;
|
|
982
993
|
for (const href of this.#hrefs) {
|
|
994
|
+
// Charged before it is resolved: what a resolution costs, in time and in the string it makes, grows with both.
|
|
995
|
+
spent += base.length + href.length;
|
|
996
|
+
if (links.size === READ_LINKS_MAX || spent > READ_LINKS_MAX_CHARS) {
|
|
997
|
+
linksCut = true;
|
|
998
|
+
break;
|
|
999
|
+
}
|
|
983
1000
|
const link = URL.parse(href, base);
|
|
984
1001
|
if (!link || !/^https?:$/.test(link.protocol))
|
|
985
1002
|
continue;
|
|
@@ -994,6 +1011,6 @@ export class PageScan {
|
|
|
994
1011
|
.replace(/ ?\n ?/g, "\n")
|
|
995
1012
|
.replace(/\n{3,}/g, "\n\n")
|
|
996
1013
|
.trim() : "";
|
|
997
|
-
return { title: this.#title ?? "", links: [...links], text };
|
|
1014
|
+
return { title: this.#title ?? "", links: [...links], text, ...(linksCut ? { linksCut: true } : {}) };
|
|
998
1015
|
}
|
|
999
1016
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// read-page.ts's worker. For `workerData`, a Job: the page's markdown, posted once as { text } (with { title, links }
|
|
2
|
-
// when the job has a `url`,
|
|
3
|
-
// conversion threw (a page nested past the stack,
|
|
2
|
+
// when the job has a `url`, { links_truncated } too when the link caps stopped their resolution, and { truncated,
|
|
3
|
+
// html_chars } when the worker cut the page), or as { error } when the conversion threw (a page nested past the stack,
|
|
4
|
+
// say).
|
|
4
5
|
import { parentPort, workerData } from "node:worker_threads";
|
|
5
6
|
import { TextNode } from "node-html-parser";
|
|
6
7
|
import { markdownOf } from "./page-markdown.js";
|
|
@@ -21,8 +22,8 @@ if (parentPort) {
|
|
|
21
22
|
if (job.url !== undefined) {
|
|
22
23
|
const scan = new PageScan(html, job.url, (text) => new TextNode(text).text, false);
|
|
23
24
|
scan.step(Number.POSITIVE_INFINITY);
|
|
24
|
-
const { title, links } = scan.result();
|
|
25
|
-
read = { title, links };
|
|
25
|
+
const { title, links, linksCut } = scan.result();
|
|
26
|
+
read = { title, links, ...(linksCut ? { links_truncated: true } : {}) };
|
|
26
27
|
}
|
|
27
28
|
parentPort.postMessage({ ...read, ...cut, text: markdownOf(html) });
|
|
28
29
|
}
|
package/dist/core/read-page.d.ts
CHANGED
|
@@ -1,41 +1,56 @@
|
|
|
1
|
+
export { READ_LINKS_MAX, READ_LINKS_MAX_CHARS } from "./page-scan.js";
|
|
1
2
|
/** The most of a page's HTML that is read, counted once its script, style, svg, noscript and template blocks and its
|
|
2
3
|
* comments are dropped. A million characters of go.dev/dl converts in about 0.8 s on a busy 48-core host and gives
|
|
3
4
|
* 590,000 characters of markdown, ten times the 60,000 one web_fetch answer carries. */
|
|
4
5
|
export declare const READ_MAX_CHARS = 1000000;
|
|
5
6
|
/** How long the worker has for the markdown by default: six times what READ_MAX_CHARS of tables took. */
|
|
6
7
|
export declare const READ_DEADLINE_MS = 5000;
|
|
8
|
+
/** The worker's heap by default, in MB (`heapMb`). */
|
|
9
|
+
export declare const READ_WORKER_HEAP_MB = 1024;
|
|
7
10
|
/** `n` with its digits in groups of three ("3,154,060"): Intl's first use costs tens of milliseconds on the loop. */
|
|
8
11
|
export declare const thousands: (n: number) => string;
|
|
9
12
|
/** `converted`: "markdown", or "text" when the conversion did not finish in time (or failed) and the text is the page's
|
|
10
13
|
* plain text. `truncated` and `html_chars` (the page's whole length, in characters) when the page was cut at
|
|
11
|
-
* READ_MAX_CHARS before it was read: the text, the title and the links are then the cut page's.
|
|
14
|
+
* READ_MAX_CHARS before it was read: the text, the title and the links are then the cut page's. `links_truncated` when
|
|
15
|
+
* READ_LINKS_MAX or READ_LINKS_MAX_CHARS stopped the links' resolution: `links` are then the page's first ones. */
|
|
12
16
|
export type PageRead = {
|
|
13
17
|
title: string;
|
|
14
18
|
text: string;
|
|
15
19
|
links: string[];
|
|
20
|
+
links_truncated?: true;
|
|
16
21
|
converted: "markdown" | "text";
|
|
17
22
|
truncated?: true;
|
|
18
23
|
html_chars?: number;
|
|
19
24
|
};
|
|
20
|
-
/** `deadlineMs`: how long the conversion to markdown has (default READ_DEADLINE_MS). `
|
|
21
|
-
* conversion stopped, with the signal's reason. */
|
|
25
|
+
/** `deadlineMs`: how long the conversion to markdown has (default READ_DEADLINE_MS). `heapMb`: the worker's heap
|
|
26
|
+
* (default READ_WORKER_HEAP_MB, at least 16). `signal` ends the read, the conversion stopped, with the signal's reason. */
|
|
22
27
|
export type ReadPageOptions = {
|
|
23
28
|
deadlineMs?: number;
|
|
29
|
+
heapMb?: number;
|
|
24
30
|
signal?: AbortSignal;
|
|
25
31
|
};
|
|
32
|
+
/** readPage's refusal of a page whose read filled the worker's heap: it is not read again on the caller's thread. */
|
|
33
|
+
export declare class PageReadOutOfMemory extends Error {
|
|
34
|
+
readonly heapMb: number;
|
|
35
|
+
readonly name = "PageReadOutOfMemory";
|
|
36
|
+
readonly code = "PAGE_READ_OUT_OF_MEMORY";
|
|
37
|
+
constructor(heapMb: number);
|
|
38
|
+
}
|
|
26
39
|
/**
|
|
27
40
|
* An HTML page's title, its text as markdown, and the absolute http(s) URLs it links to (resolved against its
|
|
28
41
|
* `<base href>` or `url`), each once, in page order. The event loop is never held for longer than one linear step of a
|
|
29
|
-
* bounded part of the page (see the file's head and PageRead). Rejects
|
|
42
|
+
* bounded part of the page (see the file's head and PageRead). Rejects with `signal`'s reason, or with
|
|
43
|
+
* PageReadOutOfMemory when the worker ran out of memory (the page is then not read on this thread).
|
|
30
44
|
*/
|
|
31
|
-
export declare function readPage(html: string, url: string, { deadlineMs, signal }?: ReadPageOptions): Promise<PageRead>;
|
|
45
|
+
export declare function readPage(html: string, url: string, { deadlineMs, heapMb, signal }?: ReadPageOptions): Promise<PageRead>;
|
|
32
46
|
/**
|
|
33
47
|
* `html`'s markdown, made in the worker within `deadlineMs` (default READ_DEADLINE_MS): of its first article, else its
|
|
34
48
|
* main, else its body element when `main` (mainOf), once its inert blocks are dropped, cut at READ_MAX_CHARS (then
|
|
35
|
-
* `truncated`, and `html_chars` is the page's whole length). Null when the worker has not answered in time, failed
|
|
36
|
-
* could not start. The event loop does no more than hand the worker the page. Rejects only
|
|
49
|
+
* `truncated`, and `html_chars` is the page's whole length). Null when the worker has not answered in time, failed (ran
|
|
50
|
+
* out of memory included), or could not start. The event loop does no more than hand the worker the page. Rejects only
|
|
51
|
+
* with `signal`'s reason.
|
|
37
52
|
*/
|
|
38
|
-
export declare function htmlMarkdown(html: string, { main, deadlineMs, signal }?: {
|
|
53
|
+
export declare function htmlMarkdown(html: string, { main, deadlineMs, heapMb, signal }?: {
|
|
39
54
|
main?: boolean;
|
|
40
55
|
} & ReadPageOptions): Promise<{
|
|
41
56
|
text: string;
|
package/dist/core/read-page.js
CHANGED
|
@@ -6,28 +6,48 @@
|
|
|
6
6
|
// then `truncated`, and `html_chars` is its whole length); and a worker thread converts that to markdown, in pieces
|
|
7
7
|
// small enough that the converter stays linear (page-markdown.ts), within a deadline. A worker that has not answered by
|
|
8
8
|
// then, or that fails, is stopped, and the text is the page's plain text, read on the loop in steps that yield it
|
|
9
|
-
// (`converted: "text"`)
|
|
10
|
-
//
|
|
9
|
+
// (`converted: "text"`); a worker that ran out of memory is not: the read is refused (PageReadOutOfMemory), since what
|
|
10
|
+
// filled the worker's heap would fill the run's own. The links resolved are bounded (READ_LINKS_MAX and
|
|
11
|
+
// READ_LINKS_MAX_CHARS, page-scan.ts). browser_read's markdown (htmlMarkdown) goes through the same worker, which drops,
|
|
12
|
+
// picks and cuts the live page's HTML itself, since that HTML has no bound; its fallback is the browser's own text.
|
|
11
13
|
import { setImmediate as yieldToLoop } from "node:timers/promises";
|
|
12
14
|
import { Worker } from "node:worker_threads";
|
|
13
15
|
import { decodeEntities } from "./entities.js";
|
|
14
16
|
import { cutHtml, HiddenDrop, InertDrop, pageHidingOf, PageScan } from "./page-scan.js";
|
|
17
|
+
export { READ_LINKS_MAX, READ_LINKS_MAX_CHARS } from "./page-scan.js";
|
|
15
18
|
/** The most of a page's HTML that is read, counted once its script, style, svg, noscript and template blocks and its
|
|
16
19
|
* comments are dropped. A million characters of go.dev/dl converts in about 0.8 s on a busy 48-core host and gives
|
|
17
20
|
* 590,000 characters of markdown, ten times the 60,000 one web_fetch answer carries. */
|
|
18
21
|
export const READ_MAX_CHARS = 1_000_000;
|
|
19
22
|
/** How long the worker has for the markdown by default: six times what READ_MAX_CHARS of tables took. */
|
|
20
23
|
export const READ_DEADLINE_MS = 5_000;
|
|
24
|
+
/** The worker's heap by default, in MB (`heapMb`). */
|
|
25
|
+
export const READ_WORKER_HEAP_MB = 1024;
|
|
26
|
+
/** The least heap a worker is given, in MB: one of a few MB fails while it starts, and takes its whole process down. */
|
|
27
|
+
const WORKER_HEAP_MIN_MB = 16;
|
|
21
28
|
/** `n` with its digits in groups of three ("3,154,060"): Intl's first use costs tens of milliseconds on the loop. */
|
|
22
29
|
export const thousands = (n) => String(n).replace(/\B(?=(\d{3})+$)/g, ",");
|
|
23
30
|
/** How much of the page one step of the plain-text scan reads before it yields the loop. */
|
|
24
31
|
const STEP_CHARS = 32_768;
|
|
32
|
+
/** The worker ran out of its heap (Node's ERR_WORKER_OUT_OF_MEMORY). */
|
|
33
|
+
const OUT_OF_MEMORY = Symbol("out of memory");
|
|
34
|
+
/** readPage's refusal of a page whose read filled the worker's heap: it is not read again on the caller's thread. */
|
|
35
|
+
export class PageReadOutOfMemory extends Error {
|
|
36
|
+
heapMb;
|
|
37
|
+
name = "PageReadOutOfMemory";
|
|
38
|
+
code = "PAGE_READ_OUT_OF_MEMORY";
|
|
39
|
+
constructor(heapMb) {
|
|
40
|
+
super(`the page was not read: reading it filled the reader's ${heapMb} MB heap`);
|
|
41
|
+
this.heapMb = heapMb;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
25
44
|
/**
|
|
26
45
|
* An HTML page's title, its text as markdown, and the absolute http(s) URLs it links to (resolved against its
|
|
27
46
|
* `<base href>` or `url`), each once, in page order. The event loop is never held for longer than one linear step of a
|
|
28
|
-
* bounded part of the page (see the file's head and PageRead). Rejects
|
|
47
|
+
* bounded part of the page (see the file's head and PageRead). Rejects with `signal`'s reason, or with
|
|
48
|
+
* PageReadOutOfMemory when the worker ran out of memory (the page is then not read on this thread).
|
|
29
49
|
*/
|
|
30
|
-
export async function readPage(html, url, { deadlineMs = READ_DEADLINE_MS, signal } = {}) {
|
|
50
|
+
export async function readPage(html, url, { deadlineMs = READ_DEADLINE_MS, heapMb = READ_WORKER_HEAP_MB, signal } = {}) {
|
|
31
51
|
signal?.throwIfAborted();
|
|
32
52
|
// What the page hides from everyone who reads it (a help pop-up until its link is clicked, a closed dialog) is not
|
|
33
53
|
// its text: what its own live style elements declare is read from those the inert drop took out.
|
|
@@ -38,29 +58,36 @@ export async function readPage(html, url, { deadlineMs = READ_DEADLINE_MS, signa
|
|
|
38
58
|
const cut = read.length < kept.length ? { truncated: true, html_chars: html.length } : {};
|
|
39
59
|
await yieldToLoop();
|
|
40
60
|
signal?.throwIfAborted();
|
|
41
|
-
const markdown = await inWorker({ html: read, url }, deadlineMs, signal);
|
|
61
|
+
const markdown = await inWorker({ html: read, url }, deadlineMs, heapMb, signal);
|
|
62
|
+
if (markdown === OUT_OF_MEMORY)
|
|
63
|
+
throw new PageReadOutOfMemory(Math.max(WORKER_HEAP_MIN_MB, heapMb));
|
|
42
64
|
if (markdown)
|
|
43
|
-
return { title: markdown.title ?? "", text: markdown.text, links: markdown.links ?? [], converted: "markdown", ...cut };
|
|
65
|
+
return { title: markdown.title ?? "", text: markdown.text, links: markdown.links ?? [], ...(markdown.links_truncated ? { links_truncated: true } : {}), converted: "markdown", ...cut };
|
|
44
66
|
return { ...(await plainRead(read, url, signal)), converted: "text", ...cut };
|
|
45
67
|
}
|
|
46
68
|
/**
|
|
47
69
|
* `html`'s markdown, made in the worker within `deadlineMs` (default READ_DEADLINE_MS): of its first article, else its
|
|
48
70
|
* main, else its body element when `main` (mainOf), once its inert blocks are dropped, cut at READ_MAX_CHARS (then
|
|
49
|
-
* `truncated`, and `html_chars` is the page's whole length). Null when the worker has not answered in time, failed
|
|
50
|
-
* could not start. The event loop does no more than hand the worker the page. Rejects only
|
|
71
|
+
* `truncated`, and `html_chars` is the page's whole length). Null when the worker has not answered in time, failed (ran
|
|
72
|
+
* out of memory included), or could not start. The event loop does no more than hand the worker the page. Rejects only
|
|
73
|
+
* with `signal`'s reason.
|
|
51
74
|
*/
|
|
52
|
-
export async function htmlMarkdown(html, { main = false, deadlineMs = READ_DEADLINE_MS, signal } = {}) {
|
|
75
|
+
export async function htmlMarkdown(html, { main = false, deadlineMs = READ_DEADLINE_MS, heapMb = READ_WORKER_HEAP_MB, signal } = {}) {
|
|
53
76
|
signal?.throwIfAborted();
|
|
54
|
-
const answer = await inWorker({ html, main, max: READ_MAX_CHARS }, deadlineMs, signal);
|
|
55
|
-
|
|
77
|
+
const answer = await inWorker({ html, main, max: READ_MAX_CHARS }, deadlineMs, heapMb, signal);
|
|
78
|
+
// Out of memory too: the caller's fallback is the browser's own text, never this HTML read on its thread.
|
|
79
|
+
if (!answer || answer === OUT_OF_MEMORY)
|
|
80
|
+
return null;
|
|
81
|
+
return { text: answer.text, ...(answer.truncated ? { truncated: true, html_chars: answer.html_chars } : {}) };
|
|
56
82
|
}
|
|
57
|
-
/** The worker's answer to `job`,
|
|
58
|
-
|
|
83
|
+
/** The worker's answer to `job`, OUT_OF_MEMORY when it ran out of its `heapMb` heap, or null when it has not answered
|
|
84
|
+
* within `deadlineMs`, failed otherwise, or could not start. */
|
|
85
|
+
function inWorker(job, deadlineMs, heapMb, signal) {
|
|
59
86
|
return new Promise((resolve, reject) => {
|
|
60
87
|
let worker;
|
|
61
88
|
// execArgv: none of the host's own flags (a test runner's --import, say) reach the worker.
|
|
62
89
|
try {
|
|
63
|
-
worker = new Worker(new URL("./read-page-worker.js", import.meta.url), { workerData: job, execArgv: [], resourceLimits: { maxOldGenerationSizeMb:
|
|
90
|
+
worker = new Worker(new URL("./read-page-worker.js", import.meta.url), { workerData: job, execArgv: [], resourceLimits: { maxOldGenerationSizeMb: Math.max(WORKER_HEAP_MIN_MB, heapMb) } });
|
|
64
91
|
}
|
|
65
92
|
catch {
|
|
66
93
|
resolve(null);
|
|
@@ -76,7 +103,7 @@ function inWorker(job, deadlineMs, signal) {
|
|
|
76
103
|
const timer = setTimeout(() => settle(() => resolve(null)), deadlineMs);
|
|
77
104
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
78
105
|
worker.once("message", (answer) => settle(() => resolve(typeof answer?.text === "string" ? answer : null)));
|
|
79
|
-
worker.once("error", () => settle(() => resolve(null)));
|
|
106
|
+
worker.once("error", (error) => settle(() => resolve(error?.code === "ERR_WORKER_OUT_OF_MEMORY" ? OUT_OF_MEMORY : null)));
|
|
80
107
|
worker.once("exit", () => settle(() => resolve(null)));
|
|
81
108
|
});
|
|
82
109
|
}
|
|
@@ -108,5 +135,6 @@ async function plainRead(html, url, signal) {
|
|
|
108
135
|
await yieldToLoop();
|
|
109
136
|
signal?.throwIfAborted();
|
|
110
137
|
}
|
|
111
|
-
|
|
138
|
+
const { title, text, links, linksCut } = scan.result();
|
|
139
|
+
return { title, text, links, ...(linksCut ? { links_truncated: true } : {}) };
|
|
112
140
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export * from "./core/images.js";
|
|
|
14
14
|
export * from "./core/lease.js";
|
|
15
15
|
export * from "./core/page-classifier.js";
|
|
16
16
|
export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicCheck, publicDownload, publicFetch, type PublicAnswer, type PublicFetchOptions, type PublicFile } from "./core/public-fetch.js";
|
|
17
|
-
export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage, type PageRead, type ReadPageOptions } from "./core/read-page.js";
|
|
17
|
+
export { PageReadOutOfMemory, READ_DEADLINE_MS, READ_LINKS_MAX, READ_LINKS_MAX_CHARS, READ_MAX_CHARS, READ_WORKER_HEAP_MB, readPage, type PageRead, type ReadPageOptions } from "./core/read-page.js";
|
|
18
18
|
export * from "./core/receipts.js";
|
|
19
19
|
export * from "./core/redact.js";
|
|
20
20
|
export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
|
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ export * from "./core/images.js";
|
|
|
15
15
|
export * from "./core/lease.js";
|
|
16
16
|
export * from "./core/page-classifier.js";
|
|
17
17
|
export { isPrivateAddress, pinnedLookup, PUBLIC_MAX_BYTES, publicCheck, publicDownload, publicFetch } from "./core/public-fetch.js";
|
|
18
|
-
export { READ_DEADLINE_MS, READ_MAX_CHARS, readPage } from "./core/read-page.js";
|
|
18
|
+
export { PageReadOutOfMemory, READ_DEADLINE_MS, READ_LINKS_MAX, READ_LINKS_MAX_CHARS, READ_MAX_CHARS, READ_WORKER_HEAP_MB, readPage } from "./core/read-page.js";
|
|
19
19
|
export * from "./core/receipts.js";
|
|
20
20
|
export * from "./core/redact.js";
|
|
21
21
|
export { DEFAULT_BROWSER_POLICY } from "./core/session-control.js";
|