@vanelsas/baredom 4.4.0 → 4.5.0
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 +21 -0
- package/dist/integrity.json +2 -2
- package/package.json +1 -1
- package/scripts/prerender.mjs +70 -20
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to BareDOM will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [4.5.0] - 2026-10-14
|
|
6
|
+
|
|
7
|
+
BareDOM 4.5 makes the prerender script open several pages at once. No component changes, and
|
|
8
|
+
the adapters are not released again.
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- **The prerender script opens four pages at once,** each in a tab of its own, or fewer on a
|
|
13
|
+
machine with fewer processors. `--tabs` says how many. 351 pages were written in half the
|
|
14
|
+
time. The pages are written in the order in which they are done. A page that fails
|
|
15
|
+
still stops the run: no tab takes another page, the pages that are open are written, and the
|
|
16
|
+
error says how many more failed. See [`docs/PRERENDER.md`](docs/PRERENDER.md).
|
|
17
|
+
- **The prerender script tells Chrome to draw every tab as the one in front:** it turns off
|
|
18
|
+
the slowing down of background tabs, and tells each tab that it has the focus. A page is
|
|
19
|
+
so drawn as if its window has the focus, also with one tab.
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- **Tests of the prerender script,** run with `node --test scripts/prerender.test.mjs`, and in
|
|
24
|
+
CI. They need Chrome.
|
|
25
|
+
|
|
5
26
|
## [4.4.0] - 2026-10-12
|
|
6
27
|
|
|
7
28
|
BareDOM 4.4 makes a prerendered page hold its pictures, and ships the script that prerenders.
|
package/dist/integrity.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "4.
|
|
2
|
+
"version": "4.5.0",
|
|
3
3
|
"algorithm": "sha384",
|
|
4
|
-
"generated-at": "2026-10-
|
|
4
|
+
"generated-at": "2026-10-14T08:05:10Z",
|
|
5
5
|
"files": {
|
|
6
6
|
"baremirror-alpha.js": "sha384-2gzoTzMRrPaie7wdJuKfkp0cjPgLIX01Bg72nb/Q8BEwfQJJXCMZfyHhU2vww74N",
|
|
7
7
|
"base.js": "sha384-TVCiuwGeloCLktG+9NSBLHSTll8xaBbXhpUFMB0foUjbt0dtO18lQILWA8qdpog3",
|
package/package.json
CHANGED
package/scripts/prerender.mjs
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// Writes a page back out with every shadow tree in its markup, as a declarative shadow root, so
|
|
3
3
|
// the page shows its components before any script has run.
|
|
4
4
|
//
|
|
5
|
-
// Usage: node scripts/prerender.mjs [--static] <url> <output.html> [<url> <output.html> ...]
|
|
5
|
+
// Usage: node scripts/prerender.mjs [--static] [--tabs <n>] <url> <output.html> [<url> <output.html> ...]
|
|
6
6
|
// --static leaves out every script and noscript element and sets data-static on the
|
|
7
7
|
// html element, so the page can style a copy that runs no script.
|
|
8
|
+
// --tabs says how many pages are opened at once: 4 when it is not given, or fewer on a
|
|
9
|
+
// machine with fewer processors.
|
|
8
10
|
// Each url is written to the output that follows it. One browser opens them all.
|
|
9
11
|
// Needs: Node 22 or later, and Chrome. Set CHROME_PATH when Chrome is not found, and
|
|
10
12
|
// CHROME_FLAGS for more flags, such as --no-sandbox in a container.
|
|
@@ -15,11 +17,14 @@
|
|
|
15
17
|
import { spawn } from 'node:child_process';
|
|
16
18
|
import { existsSync } from 'node:fs';
|
|
17
19
|
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
18
|
-
import { tmpdir } from 'node:os';
|
|
20
|
+
import { availableParallelism, tmpdir } from 'node:os';
|
|
19
21
|
import { basename, dirname, join } from 'node:path';
|
|
20
22
|
|
|
21
23
|
const STEP_LIMIT_MS = 10_000;
|
|
22
24
|
const PAGE_LIMIT_MS = 30_000;
|
|
25
|
+
// A tab draws its page on a processor of its own, and a page that waits for one may not finish
|
|
26
|
+
// within its limit. So no more tabs are opened than there are processors.
|
|
27
|
+
const TABS = Math.min(4, availableParallelism());
|
|
23
28
|
|
|
24
29
|
const CHROME_PATHS = [
|
|
25
30
|
process.env.CHROME_PATH,
|
|
@@ -102,10 +107,16 @@ const pageAsMarkup = async (limitMs, isStatic) => {
|
|
|
102
107
|
return `<!doctype html>\n${htmlTag}${inside}</html>\n`;
|
|
103
108
|
};
|
|
104
109
|
|
|
110
|
+
// Pages are opened in several tabs at once. Chrome slows down a tab that it takes to be in
|
|
111
|
+
// the background, its frames and its timers, and the script waits for frames. So no tab is
|
|
112
|
+
// slowed down, and each tab is told that it has the focus.
|
|
105
113
|
const startChrome = (chromePath, profileDir) =>
|
|
106
114
|
spawn(chromePath, [
|
|
107
115
|
'--headless=new', '--remote-debugging-port=0', `--user-data-dir=${profileDir}`,
|
|
108
|
-
'--no-first-run', '--disable-gpu',
|
|
116
|
+
'--no-first-run', '--disable-gpu',
|
|
117
|
+
'--disable-background-timer-throttling', '--disable-backgrounding-occluded-windows',
|
|
118
|
+
'--disable-renderer-backgrounding',
|
|
119
|
+
...extraFlags(), 'about:blank',
|
|
109
120
|
]);
|
|
110
121
|
|
|
111
122
|
const chromeAddress = chrome =>
|
|
@@ -126,7 +137,8 @@ const stopChrome = async (chrome, profileDir) => {
|
|
|
126
137
|
};
|
|
127
138
|
|
|
128
139
|
// A connection to Chrome over its debugging protocol: `send` gives the answer to one command,
|
|
129
|
-
// and `next` the next event of one name.
|
|
140
|
+
// and `next` the next event of one name in one tab. Several tabs share the connection, so an
|
|
141
|
+
// event is matched by the session of its tab as well as by its name.
|
|
130
142
|
const connect = async address => {
|
|
131
143
|
const socket = new WebSocket(address);
|
|
132
144
|
await new Promise((resolve, reject) => {
|
|
@@ -142,7 +154,8 @@ const connect = async address => {
|
|
|
142
154
|
resolve?.(message);
|
|
143
155
|
};
|
|
144
156
|
const onAnswer = message => settle(answers, message.id, message);
|
|
145
|
-
const
|
|
157
|
+
const eventKey = (method, sessionId) => `${sessionId ?? ''} ${method}`;
|
|
158
|
+
const onEvent = message => settle(events, eventKey(message.method, message.sessionId), message);
|
|
146
159
|
socket.onmessage = ({ data }) => {
|
|
147
160
|
const message = JSON.parse(data);
|
|
148
161
|
(message.id ? onAnswer : onEvent)(message);
|
|
@@ -155,7 +168,7 @@ const connect = async address => {
|
|
|
155
168
|
error ? reject(new Error(`${method}: ${error.message}`)) : resolve(result));
|
|
156
169
|
socket.send(JSON.stringify({ id: lastId, method, params, sessionId }));
|
|
157
170
|
}),
|
|
158
|
-
next: method => new Promise(resolve => events.set(method, resolve)),
|
|
171
|
+
next: (method, sessionId) => new Promise(resolve => events.set(eventKey(method, sessionId), resolve)),
|
|
159
172
|
close: () => socket.close(),
|
|
160
173
|
};
|
|
161
174
|
};
|
|
@@ -165,7 +178,8 @@ const prerender = async (browser, url, isStatic) => {
|
|
|
165
178
|
try {
|
|
166
179
|
const { sessionId } = await browser.send('Target.attachToTarget', { targetId, flatten: true });
|
|
167
180
|
await browser.send('Page.enable', {}, sessionId);
|
|
168
|
-
|
|
181
|
+
await browser.send('Emulation.setFocusEmulationEnabled', { enabled: true }, sessionId);
|
|
182
|
+
const loaded = browser.next('Page.loadEventFired', sessionId);
|
|
169
183
|
const { errorText } = await browser.send('Page.navigate', { url }, sessionId);
|
|
170
184
|
if (errorText) throw new Error(`Could not open ${url}: ${errorText}`);
|
|
171
185
|
await loaded;
|
|
@@ -185,21 +199,57 @@ const prerender = async (browser, url, isStatic) => {
|
|
|
185
199
|
|
|
186
200
|
const shadowRootCount = markup => markup.split('shadowrootmode').length - 1;
|
|
187
201
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
202
|
+
const writePage = async (browser, { url, output }, isStatic) => {
|
|
203
|
+
const markup = await within(PAGE_LIMIT_MS, `prerendering ${url}`, prerender(browser, url, isStatic));
|
|
204
|
+
await mkdir(dirname(output), { recursive: true });
|
|
205
|
+
await writeFile(output, markup);
|
|
206
|
+
console.log(`${output}: ${markup.length} characters, ${shadowRootCount(markup)} shadow roots`);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// The error of a run in which the pages of `failures` failed: the first, and how many more.
|
|
210
|
+
const failed = failures =>
|
|
211
|
+
failures.length === 1
|
|
212
|
+
? failures[0]
|
|
213
|
+
: new Error(`${failures[0].message}\n${failures.length - 1} more of the pages that were open failed too.`);
|
|
214
|
+
|
|
215
|
+
// Each page is opened in a tab of its own, and written as soon as it is read. `tabs` pages are
|
|
216
|
+
// open at once: each tab takes the next page of the list when it is done with one, so the pages
|
|
217
|
+
// are written in the order in which they are done. A page that fails stops the run: no tab
|
|
218
|
+
// takes another page, the pages that are open are written, and the run ends with the first
|
|
219
|
+
// error, and says how many more failed.
|
|
220
|
+
const writePages = async (browser, pages, isStatic, tabs) => {
|
|
221
|
+
let next = 0;
|
|
222
|
+
const failures = [];
|
|
223
|
+
const work = async () => {
|
|
224
|
+
while (failures.length === 0 && next < pages.length) {
|
|
225
|
+
const page = pages[next];
|
|
226
|
+
next += 1;
|
|
227
|
+
try {
|
|
228
|
+
await writePage(browser, page, isStatic);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
failures.push(error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
await Promise.all(Array.from({ length: Math.min(tabs, pages.length) }, work));
|
|
235
|
+
if (failures.length > 0) throw failed(failures);
|
|
197
236
|
};
|
|
198
237
|
|
|
199
238
|
const STATIC_FLAG = '--static';
|
|
239
|
+
const TABS_FLAG = '--tabs';
|
|
200
240
|
|
|
201
241
|
// The script is run by its file, or as the command of the package. It names itself as it was run.
|
|
202
|
-
const USAGE = `Usage: ${basename(process.argv[1] ?? 'prerender.mjs')} [--static] <url> <output.html> [<url> <output.html> ...]`;
|
|
242
|
+
const USAGE = `Usage: ${basename(process.argv[1] ?? 'prerender.mjs')} [--static] [--tabs <n>] <url> <output.html> [<url> <output.html> ...]`;
|
|
243
|
+
|
|
244
|
+
// The flags, and the arguments that are left: `--static`, and `--tabs` with the number that
|
|
245
|
+
// follows it.
|
|
246
|
+
const optionsOf = args => {
|
|
247
|
+
const at = args.indexOf(TABS_FLAG);
|
|
248
|
+
const tabs = at === -1 ? TABS : Number(args[at + 1]);
|
|
249
|
+
if (!Number.isInteger(tabs) || tabs < 1) throw new Error(`${TABS_FLAG} takes a whole number of 1 or more.\n${USAGE}`);
|
|
250
|
+
const rest = at === -1 ? args : [...args.slice(0, at), ...args.slice(at + 2)];
|
|
251
|
+
return { isStatic: rest.includes(STATIC_FLAG), tabs, rest: rest.filter(arg => arg !== STATIC_FLAG) };
|
|
252
|
+
};
|
|
203
253
|
|
|
204
254
|
// The arguments after the flag are pairs: a url, and the file it is written to.
|
|
205
255
|
const pagesOf = args => {
|
|
@@ -208,8 +258,8 @@ const pagesOf = args => {
|
|
|
208
258
|
};
|
|
209
259
|
|
|
210
260
|
const main = async args => {
|
|
211
|
-
const isStatic = args
|
|
212
|
-
const pages = pagesOf(
|
|
261
|
+
const { isStatic, tabs, rest } = optionsOf(args);
|
|
262
|
+
const pages = pagesOf(rest);
|
|
213
263
|
const chromePath = findChrome();
|
|
214
264
|
if (!chromePath) throw new Error('Chrome was not found. Set CHROME_PATH.');
|
|
215
265
|
const profileDir = await mkdtemp(join(tmpdir(), 'prerender-'));
|
|
@@ -217,7 +267,7 @@ const main = async args => {
|
|
|
217
267
|
try {
|
|
218
268
|
const browser = await connect(await chromeAddress(chrome));
|
|
219
269
|
try {
|
|
220
|
-
await writePages(browser, pages, isStatic);
|
|
270
|
+
await writePages(browser, pages, isStatic, tabs);
|
|
221
271
|
} finally {
|
|
222
272
|
browser.close();
|
|
223
273
|
}
|