@ogygia/insights 0.0.0 → 0.14.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/LICENSE +21 -0
- package/README.md +37 -1
- package/dist/attr-rules.d.ts +15 -0
- package/dist/attr-rules.js +27 -0
- package/dist/barrel-left.d.ts +6 -0
- package/dist/barrel-left.js +34 -0
- package/dist/cpu.d.ts +82 -0
- package/dist/cpu.js +177 -0
- package/dist/fp-drift.d.ts +28 -0
- package/dist/fp-drift.js +170 -0
- package/dist/hole-segments.d.ts +26 -0
- package/dist/hole-segments.js +50 -0
- package/dist/html-diff.d.ts +25 -0
- package/dist/html-diff.js +156 -0
- package/dist/hydration-status.d.ts +2 -0
- package/dist/hydration-status.js +0 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/dist/page-assets.d.ts +211 -0
- package/dist/page-assets.js +565 -0
- package/dist/page-insights.d.ts +744 -0
- package/dist/page-insights.js +2289 -0
- package/dist/session-data.d.ts +124 -0
- package/dist/session-data.js +0 -0
- package/dist/session-insights.d.ts +121 -0
- package/dist/session-insights.js +575 -0
- package/dist/testing-version.d.ts +19 -0
- package/dist/testing-version.js +21 -0
- package/dist/third-party.d.ts +75 -0
- package/dist/third-party.js +168 -0
- package/package.json +58 -12
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import { find_unscoped, next_any_tag, next_tag, tag_name, tags, text_of } from "@ogygia/core/internal/instruments";
|
|
2
|
+
import { clock, scope } from "@ogygia/machine";
|
|
3
|
+
// src/page-assets.ts
|
|
4
|
+
/**
|
|
5
|
+
* THE PAGE'S WEIGHT — every file the rendered document makes the browser fetch at start, found in
|
|
6
|
+
* the HTML and weighed through the app. Not only the islands: the page's own `<script>`s, every
|
|
7
|
+
* `modulepreload` (a Kit-hydrated page lists its whole start graph there), the modules an inline
|
|
8
|
+
* script imports (Kit's start/app entries), each module's STATIC imports followed to the end,
|
|
9
|
+
* stylesheets (and which of them block the first paint), fonts and images preloaded or in the
|
|
10
|
+
* markup. A page that ships 6 MB of JS shows 6 MB here, whatever made it.
|
|
11
|
+
*
|
|
12
|
+
* Two halves: `scan_assets` (pure: HTML in, references out — string scanning, no regex) and
|
|
13
|
+
* `weigh_assets` (fetches each once through the app, decoded + gzip-estimated sizes, follows static
|
|
14
|
+
* imports, bounded in time and count). The score (score.ts) reads the totals.
|
|
15
|
+
*/
|
|
16
|
+
/** Attributes of one start tag `tags()` read, names lower-cased, values entity-decoded; valueless → ''. */
|
|
17
|
+
function tag_attrs(tag) {
|
|
18
|
+
const out = /* @__PURE__ */ new Map();
|
|
19
|
+
for (const [name, value] of tag.attrs) out.set(name, decode_entities(value));
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function is_space(c) {
|
|
23
|
+
return c === 32 || c === 9 || c === 10 || c === 13 || c === 12;
|
|
24
|
+
}
|
|
25
|
+
function decode_entities(s) {
|
|
26
|
+
if (s.indexOf("&") === -1) return s;
|
|
27
|
+
return s.split("&").join("&").split(""").join("\"").split("'").join("'");
|
|
28
|
+
}
|
|
29
|
+
const FONT_EXT = /* @__PURE__ */ new Set([
|
|
30
|
+
"woff",
|
|
31
|
+
"woff2",
|
|
32
|
+
"ttf",
|
|
33
|
+
"otf"
|
|
34
|
+
]);
|
|
35
|
+
const IMG_EXT = /* @__PURE__ */ new Set([
|
|
36
|
+
"png",
|
|
37
|
+
"jpg",
|
|
38
|
+
"jpeg",
|
|
39
|
+
"gif",
|
|
40
|
+
"webp",
|
|
41
|
+
"avif",
|
|
42
|
+
"svg",
|
|
43
|
+
"ico"
|
|
44
|
+
]);
|
|
45
|
+
function ext_of(url) {
|
|
46
|
+
let end = url.length;
|
|
47
|
+
const q = url.indexOf("?");
|
|
48
|
+
if (q !== -1) end = q;
|
|
49
|
+
const h = url.indexOf("#");
|
|
50
|
+
if (h !== -1 && h < end) end = h;
|
|
51
|
+
const path = url.slice(0, end);
|
|
52
|
+
const dot = path.lastIndexOf(".");
|
|
53
|
+
return dot > path.lastIndexOf("/") ? path.slice(dot + 1).toLowerCase() : "";
|
|
54
|
+
}
|
|
55
|
+
function kind_of(url, as) {
|
|
56
|
+
if (as === "script" || as === "module") return "script";
|
|
57
|
+
if (as === "style") return "style";
|
|
58
|
+
if (as === "font") return "font";
|
|
59
|
+
if (as === "image") return "image";
|
|
60
|
+
const e = ext_of(url);
|
|
61
|
+
if (e === "js" || e === "mjs") return "script";
|
|
62
|
+
if (e === "css") return "style";
|
|
63
|
+
if (FONT_EXT.has(e)) return "font";
|
|
64
|
+
if (IMG_EXT.has(e)) return "image";
|
|
65
|
+
return "other";
|
|
66
|
+
}
|
|
67
|
+
/** `import("x")` / `import('x')` specifiers in a script's text (Kit's inline start). */
|
|
68
|
+
function dynamic_imports(text) {
|
|
69
|
+
const out = [];
|
|
70
|
+
let at = text.indexOf("import(");
|
|
71
|
+
while (at !== -1) {
|
|
72
|
+
let i = at + 7;
|
|
73
|
+
while (i < text.length && is_space(text.charCodeAt(i))) i++;
|
|
74
|
+
const q = text[i];
|
|
75
|
+
if (q === "\"" || q === "'" || q === "`") {
|
|
76
|
+
const end = text.indexOf(q, i + 1);
|
|
77
|
+
if (end !== -1) {
|
|
78
|
+
const spec = text.slice(i + 1, end);
|
|
79
|
+
if (spec && spec.indexOf("${") === -1) out.push(spec);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
at = text.indexOf("import(", at + 7);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The STATIC imports of a bundled module (`import{a}from"./x.js"`, `import"./y.js"`,
|
|
88
|
+
* `export*from"./z.js"`): specifiers after `from` / a bare `import` that look like module paths
|
|
89
|
+
* (relative or absolute, ending in .js/.mjs). Bundler output writes them plainly; a string that
|
|
90
|
+
* merely contains such text inflates nothing that does not exist — every hit is fetched, and a
|
|
91
|
+
* miss is dropped.
|
|
92
|
+
*/
|
|
93
|
+
function static_imports(code) {
|
|
94
|
+
const out = /* @__PURE__ */ new Set();
|
|
95
|
+
const grab = (from) => {
|
|
96
|
+
let i = from;
|
|
97
|
+
while (i < code.length && is_space(code.charCodeAt(i))) i++;
|
|
98
|
+
const q = code[i];
|
|
99
|
+
if (q !== "\"" && q !== "'") return;
|
|
100
|
+
const end = code.indexOf(q, i + 1);
|
|
101
|
+
if (end === -1 || end - i > 400) return;
|
|
102
|
+
const spec = code.slice(i + 1, end);
|
|
103
|
+
const e = ext_of(spec);
|
|
104
|
+
if ((spec.startsWith("./") || spec.startsWith("../") || spec.startsWith("/")) && (e === "js" || e === "mjs")) out.add(spec);
|
|
105
|
+
};
|
|
106
|
+
for (const word of ["from", "import"]) {
|
|
107
|
+
let at = code.indexOf(word);
|
|
108
|
+
while (at !== -1) {
|
|
109
|
+
const before = at > 0 ? code.charCodeAt(at - 1) : 32;
|
|
110
|
+
const ok = !(is_ident(before) || before === 46);
|
|
111
|
+
const next = code[at + word.length];
|
|
112
|
+
if (ok && next !== "(" && next !== void 0 && !is_ident(code.charCodeAt(at + word.length))) grab(at + word.length);
|
|
113
|
+
at = code.indexOf(word, at + word.length);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return [...out];
|
|
117
|
+
}
|
|
118
|
+
function is_ident(c) {
|
|
119
|
+
return c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122 || c === 95 || c === 36;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* A Kit-hydrated page's route nodes: its inline start script passes `node_ids: [0, 5]` to
|
|
123
|
+
* `kit.start`, and `app.js` imports each node on demand (`import("../nodes/5.<hash>.js")`) — the
|
|
124
|
+
* page's OWN code, the heaviest part, reachable only through that dynamic import. Empty when the
|
|
125
|
+
* page is not Kit-hydrated.
|
|
126
|
+
*/
|
|
127
|
+
function kit_node_ids(script) {
|
|
128
|
+
const at = script.indexOf("node_ids:");
|
|
129
|
+
if (at === -1) return [];
|
|
130
|
+
const open = script.indexOf("[", at);
|
|
131
|
+
const close = script.indexOf("]", open);
|
|
132
|
+
if (open === -1 || close === -1 || open - at > 20) return [];
|
|
133
|
+
const out = [];
|
|
134
|
+
for (const part of script.slice(open + 1, close).split(",")) {
|
|
135
|
+
const n = Number(part.trim());
|
|
136
|
+
if (Number.isInteger(n) && n >= 0) out.push(n);
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
/** Every file the document asks for at start, in document order, deduped by URL (the first,
|
|
141
|
+
* strongest ask wins: a blocking stylesheet stays blocking). Inline script/style bytes counted. */
|
|
142
|
+
function scan_assets(html) {
|
|
143
|
+
const refs = [];
|
|
144
|
+
const seen = /* @__PURE__ */ new Set();
|
|
145
|
+
const add = (url, kind, via, blocking) => {
|
|
146
|
+
if (!url) return;
|
|
147
|
+
const u = url.trim();
|
|
148
|
+
if (!u || u.startsWith("data:") || u.startsWith("blob:") || u.startsWith("#") || u.startsWith("javascript:")) return;
|
|
149
|
+
if (seen.has(u)) return;
|
|
150
|
+
seen.add(u);
|
|
151
|
+
refs.push({
|
|
152
|
+
url: u,
|
|
153
|
+
kind,
|
|
154
|
+
blocking,
|
|
155
|
+
via
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
const inline = {
|
|
159
|
+
script: 0,
|
|
160
|
+
style: 0
|
|
161
|
+
};
|
|
162
|
+
let kit_nodes = [];
|
|
163
|
+
const head = next_tag(html, "head", 0);
|
|
164
|
+
const head_text = head === null ? null : text_of(html, head, "head");
|
|
165
|
+
const head_end = head_text === null ? -1 : head.end + head_text.length;
|
|
166
|
+
for (let tag = next_any_tag(html, 0); tag !== null; tag = next_any_tag(html, tag.next)) {
|
|
167
|
+
const name = tag_name(html, tag);
|
|
168
|
+
const in_head = head_end !== -1 && tag.start < head_end;
|
|
169
|
+
if (name === "script") {
|
|
170
|
+
const a = tag_attrs(tag);
|
|
171
|
+
const body = text_of(html, tag, "script") ?? "";
|
|
172
|
+
const type = (a.get("type") ?? "").toLowerCase();
|
|
173
|
+
const is_js = type === "" || type === "module" || type === "text/javascript" || type === "application/javascript";
|
|
174
|
+
if (a.has("src")) {
|
|
175
|
+
if (is_js) {
|
|
176
|
+
const n = refs.length;
|
|
177
|
+
add(a.get("src"), "script", "script", type !== "module" && !a.has("async") && !a.has("defer") && in_head);
|
|
178
|
+
if (refs.length > n && a.has("data-ogygia-runtime")) refs[n].runtime = true;
|
|
179
|
+
}
|
|
180
|
+
} else if (is_js) {
|
|
181
|
+
inline.script += body.length;
|
|
182
|
+
for (const spec of dynamic_imports(body)) add(spec, "script", "inline-import", false);
|
|
183
|
+
if (!kit_nodes.length) kit_nodes = kit_node_ids(body);
|
|
184
|
+
if (type === "module") for (const spec of static_imports(body)) add(spec, "script", "inline-import", false);
|
|
185
|
+
}
|
|
186
|
+
} else if (name === "style") {
|
|
187
|
+
const text = text_of(html, tag, "style");
|
|
188
|
+
if (text !== null) inline.style += text.length;
|
|
189
|
+
} else if (name === "link") {
|
|
190
|
+
const a = tag_attrs(tag);
|
|
191
|
+
const rel = words((a.get("rel") ?? "").toLowerCase());
|
|
192
|
+
const href = a.get("href");
|
|
193
|
+
if (rel.includes("stylesheet")) {
|
|
194
|
+
const media = (a.get("media") ?? "").toLowerCase();
|
|
195
|
+
if (!a.has("disabled")) add(href, "style", "stylesheet", media === "" || media === "all" || media === "screen");
|
|
196
|
+
} else if (rel.includes("modulepreload")) add(href, "script", "modulepreload", false);
|
|
197
|
+
else if (rel.includes("preload")) add(href, kind_of(href ?? "", (a.get("as") ?? "").toLowerCase()), "preload", false);
|
|
198
|
+
else if (rel.includes("icon")) add(href, "image", "img", false);
|
|
199
|
+
} else if (name === "img") {
|
|
200
|
+
const a = tag_attrs(tag);
|
|
201
|
+
if ((a.get("loading") ?? "").toLowerCase() !== "lazy") add(a.get("src"), "image", "img", false);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
refs,
|
|
206
|
+
inline,
|
|
207
|
+
kit_nodes
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function words(s) {
|
|
211
|
+
const out = [];
|
|
212
|
+
let w = "";
|
|
213
|
+
for (let i = 0; i < s.length; i++) if (is_space(s.charCodeAt(i))) {
|
|
214
|
+
if (w) out.push(w);
|
|
215
|
+
w = "";
|
|
216
|
+
} else w += s[i];
|
|
217
|
+
if (w) out.push(w);
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
/** Resolve a reference against the page (and a module's import against its module). */
|
|
221
|
+
function resolve_ref(ref, base) {
|
|
222
|
+
try {
|
|
223
|
+
const u = new URL(ref, base);
|
|
224
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
|
|
225
|
+
return u.href;
|
|
226
|
+
} catch {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
async function weigh_assets(opts) {
|
|
231
|
+
const { refs, inline, kit_nodes } = scan_assets(opts.html);
|
|
232
|
+
const node_marks = kit_nodes.map((n) => `/nodes/${n}.`);
|
|
233
|
+
const deadline = clock.mono() + (opts.budget_ms ?? 6e3);
|
|
234
|
+
const max_files = opts.max_files ?? 400;
|
|
235
|
+
const origin = new URL(opts.page_url).origin;
|
|
236
|
+
const queue = [];
|
|
237
|
+
const queued = /* @__PURE__ */ new Set();
|
|
238
|
+
const push = (r) => {
|
|
239
|
+
const abs = resolve_ref(r.url, opts.page_url);
|
|
240
|
+
if (!abs || queued.has(abs)) return;
|
|
241
|
+
queued.add(abs);
|
|
242
|
+
queue.push({
|
|
243
|
+
...r,
|
|
244
|
+
url: abs
|
|
245
|
+
});
|
|
246
|
+
};
|
|
247
|
+
for (const r of refs) push(r);
|
|
248
|
+
const extra = opts.extra ?? [];
|
|
249
|
+
for (const r of extra) if (!r.lazy) push(r);
|
|
250
|
+
for (const r of extra) if (r.lazy) push(r);
|
|
251
|
+
const assets = [];
|
|
252
|
+
const missed = [];
|
|
253
|
+
const missed_why = [];
|
|
254
|
+
const miss = (url, why) => {
|
|
255
|
+
missed.push(url);
|
|
256
|
+
missed_why.push({
|
|
257
|
+
url,
|
|
258
|
+
why
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
let at = 0;
|
|
262
|
+
const worker = async () => {
|
|
263
|
+
while (at < queue.length && clock.mono() < deadline && assets.length + missed.length < max_files) {
|
|
264
|
+
const r = queue[at++];
|
|
265
|
+
let w = opts.cache.get(r.url);
|
|
266
|
+
if (w === void 0) {
|
|
267
|
+
const got = await weigh_one(r, opts, deadline, origin);
|
|
268
|
+
if ("miss" in got) {
|
|
269
|
+
if (got.gone) opts.cache.set(r.url, null);
|
|
270
|
+
miss(r.url, got.miss);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
w = got;
|
|
274
|
+
opts.cache.set(r.url, w);
|
|
275
|
+
}
|
|
276
|
+
if (!w) {
|
|
277
|
+
miss(r.url, "an error status before (cached)");
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
const contains = r.kind === "script" || r.kind === "style" ? opts.contents_of?.(r.url) : null;
|
|
281
|
+
assets.push({
|
|
282
|
+
...r,
|
|
283
|
+
bytes: w.bytes,
|
|
284
|
+
wire: w.wire,
|
|
285
|
+
...contains?.length ? { contains } : {},
|
|
286
|
+
...w.vary ? { vary: w.vary } : {}
|
|
287
|
+
});
|
|
288
|
+
if (r.kind === "script" && w.imports) for (const spec of w.imports) push({
|
|
289
|
+
url: spec,
|
|
290
|
+
kind: "script",
|
|
291
|
+
blocking: false,
|
|
292
|
+
via: "static-import",
|
|
293
|
+
...r.lazy ? { lazy: true } : {},
|
|
294
|
+
...r.phase ? { phase: true } : {}
|
|
295
|
+
});
|
|
296
|
+
if (r.runtime && w.dynamic) for (const spec of w.dynamic) push({
|
|
297
|
+
url: spec,
|
|
298
|
+
kind: "script",
|
|
299
|
+
blocking: false,
|
|
300
|
+
via: "runtime-phase",
|
|
301
|
+
lazy: true,
|
|
302
|
+
phase: true
|
|
303
|
+
});
|
|
304
|
+
if (node_marks.length && w.dynamic) {
|
|
305
|
+
for (const spec of w.dynamic) if (node_marks.some((m) => spec.includes(m))) push({
|
|
306
|
+
url: spec,
|
|
307
|
+
kind: "script",
|
|
308
|
+
blocking: false,
|
|
309
|
+
via: "route-node"
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
while (at < queue.length && clock.mono() < deadline && assets.length + missed.length < max_files) await Promise.all(Array.from({ length: Math.min(6, queue.length - at) }, worker));
|
|
315
|
+
for (let k = at; k < queue.length; k++) miss(queue[k].url, assets.length + missed.length >= max_files ? "file cap" : "budget");
|
|
316
|
+
const html_bytes = byte_length(opts.html);
|
|
317
|
+
const html_wire = opts.gzip ? opts.gzip(new TextEncoder().encode(opts.html)) : html_bytes;
|
|
318
|
+
const t = {
|
|
319
|
+
js: 0,
|
|
320
|
+
js_wire: 0,
|
|
321
|
+
css: 0,
|
|
322
|
+
css_wire: 0,
|
|
323
|
+
font: 0,
|
|
324
|
+
image: 0,
|
|
325
|
+
wire: html_wire,
|
|
326
|
+
lazy_js: 0,
|
|
327
|
+
blocking: 0,
|
|
328
|
+
blocking_count: 0,
|
|
329
|
+
js_files: 0
|
|
330
|
+
};
|
|
331
|
+
for (const a of assets) {
|
|
332
|
+
if (a.lazy) {
|
|
333
|
+
if (a.kind === "script") t.lazy_js += a.bytes;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
t.wire += a.wire;
|
|
337
|
+
if (a.kind === "script") {
|
|
338
|
+
t.js += a.bytes;
|
|
339
|
+
t.js_wire += a.wire;
|
|
340
|
+
t.js_files++;
|
|
341
|
+
} else if (a.kind === "style") {
|
|
342
|
+
t.css += a.bytes;
|
|
343
|
+
t.css_wire += a.wire;
|
|
344
|
+
} else if (a.kind === "font") t.font += a.wire;
|
|
345
|
+
else if (a.kind === "image") t.image += a.wire;
|
|
346
|
+
if (a.blocking) {
|
|
347
|
+
t.blocking += a.bytes;
|
|
348
|
+
t.blocking_count++;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const unscoped = find_unscoped(inline_style_text(opts.html)).map((u) => ({
|
|
352
|
+
...u,
|
|
353
|
+
in: "the document"
|
|
354
|
+
}));
|
|
355
|
+
for (const a of assets) if (a.kind === "style") {
|
|
356
|
+
for (const u of opts.cache.get(a.url)?.unscoped ?? []) if (!unscoped.some((x) => x.file === u.file)) unscoped.push({
|
|
357
|
+
...u,
|
|
358
|
+
in: a.url
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
assets: assets.sort((x, y) => y.bytes - x.bytes),
|
|
363
|
+
missed,
|
|
364
|
+
...missed_why.length ? { missed_why } : {},
|
|
365
|
+
...unscoped.length ? { unscoped } : {},
|
|
366
|
+
html: {
|
|
367
|
+
bytes: html_bytes,
|
|
368
|
+
wire: html_wire
|
|
369
|
+
},
|
|
370
|
+
inline,
|
|
371
|
+
totals: t
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
/** The text of every inline `<style>` in the document (only there is a marker the build's: a page
|
|
375
|
+
* that quotes one in its content is not a fallback). */
|
|
376
|
+
function inline_style_text(html) {
|
|
377
|
+
const out = [];
|
|
378
|
+
for (const tag of tags(html, "style")) {
|
|
379
|
+
const text = text_of(html, tag, "style");
|
|
380
|
+
if (text === null) break;
|
|
381
|
+
out.push(`${text}\n`);
|
|
382
|
+
}
|
|
383
|
+
return out.join("");
|
|
384
|
+
}
|
|
385
|
+
const MIN_CHANGE = 5120;
|
|
386
|
+
function assets_diff(a, b) {
|
|
387
|
+
const key = (x) => x.contains?.length ? `c:${[...x.contains].sort().join("|")}` : `u:${x.url}`;
|
|
388
|
+
const scripts = (p) => {
|
|
389
|
+
const m = /* @__PURE__ */ new Map();
|
|
390
|
+
for (const x of p.assets) if (x.kind === "script") m.set(key(x), x);
|
|
391
|
+
return m;
|
|
392
|
+
};
|
|
393
|
+
const A = scripts(a);
|
|
394
|
+
const B = scripts(b);
|
|
395
|
+
const name = (x) => {
|
|
396
|
+
const q = x.url.indexOf("?");
|
|
397
|
+
const path = q === -1 ? x.url : x.url.slice(0, q);
|
|
398
|
+
return path.slice(path.lastIndexOf("/") + 1);
|
|
399
|
+
};
|
|
400
|
+
const change = (x, av, bv) => ({
|
|
401
|
+
name: name(x),
|
|
402
|
+
...x.contains?.length ? { contains: x.contains.slice(0, 4) } : {},
|
|
403
|
+
a: av,
|
|
404
|
+
b: bv,
|
|
405
|
+
...x.lazy ? { lazy: true } : {}
|
|
406
|
+
});
|
|
407
|
+
const added = [];
|
|
408
|
+
const removed = [];
|
|
409
|
+
const grew = [];
|
|
410
|
+
const shrank = [];
|
|
411
|
+
const to_start = [];
|
|
412
|
+
const to_later = [];
|
|
413
|
+
for (const [k, x] of B) {
|
|
414
|
+
const was = A.get(k);
|
|
415
|
+
if (!was) {
|
|
416
|
+
if (x.bytes >= MIN_CHANGE) added.push(change(x, 0, x.bytes));
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (x.bytes - was.bytes >= MIN_CHANGE) grew.push(change(x, was.bytes, x.bytes));
|
|
420
|
+
else if (was.bytes - x.bytes >= MIN_CHANGE) shrank.push(change(x, was.bytes, x.bytes));
|
|
421
|
+
if (!!was.lazy !== !!x.lazy && x.bytes >= MIN_CHANGE) (x.lazy ? to_later : to_start).push(change(x, was.bytes, x.bytes));
|
|
422
|
+
}
|
|
423
|
+
for (const [k, x] of A) if (!B.has(k) && x.bytes >= MIN_CHANGE) removed.push(change(x, x.bytes, 0));
|
|
424
|
+
const by = (s) => s.sort((p, q) => Math.abs(q.b - q.a) - Math.abs(p.b - p.a)).slice(0, 5);
|
|
425
|
+
const big = (s) => s.sort((p, q) => q.b - p.b).slice(0, 5);
|
|
426
|
+
return {
|
|
427
|
+
js: {
|
|
428
|
+
a: a.totals.js,
|
|
429
|
+
b: b.totals.js
|
|
430
|
+
},
|
|
431
|
+
lazy_js: {
|
|
432
|
+
a: a.totals.lazy_js,
|
|
433
|
+
b: b.totals.lazy_js
|
|
434
|
+
},
|
|
435
|
+
added: by(added),
|
|
436
|
+
removed: by(removed),
|
|
437
|
+
grew: by(grew),
|
|
438
|
+
shrank: by(shrank),
|
|
439
|
+
to_start: big(to_start),
|
|
440
|
+
to_later: big(to_later)
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* SCRIPTS LOADED AT RUNTIME — what the browser fetched that neither the HTML nor any module's
|
|
445
|
+
* imports name: a script that loads more scripts (a CDN component library fetching its parts, a tag
|
|
446
|
+
* manager, a runtime's on-demand chunk). The server cannot see them coming; the profiler user's own
|
|
447
|
+
* visit saw them. `until`: the end of "at start" on the visit's clock (a lazy island woken by a
|
|
448
|
+
* scroll later does not count). Null when there are none.
|
|
449
|
+
*/
|
|
450
|
+
function runtime_scripts(assets, resources, origin, until) {
|
|
451
|
+
const named = new Set(assets.map((a) => a.url));
|
|
452
|
+
const by = /* @__PURE__ */ new Map();
|
|
453
|
+
let files = 0;
|
|
454
|
+
let bytes = 0;
|
|
455
|
+
let wire = 0;
|
|
456
|
+
let entries = 0;
|
|
457
|
+
const seen = /* @__PURE__ */ new Set();
|
|
458
|
+
for (const r of resources) {
|
|
459
|
+
if (r.type !== "script" || r.start > until || named.has(r.url) || seen.has(r.url) || r.url.includes("/__profiler/")) continue;
|
|
460
|
+
seen.add(r.url);
|
|
461
|
+
const size = r.size ?? 0;
|
|
462
|
+
files++;
|
|
463
|
+
bytes += size;
|
|
464
|
+
wire += r.transfer || Math.round(size / 3);
|
|
465
|
+
const who = serves(r.url, origin);
|
|
466
|
+
const g = by.get(who) ?? {
|
|
467
|
+
who,
|
|
468
|
+
files: 0,
|
|
469
|
+
bytes: 0
|
|
470
|
+
};
|
|
471
|
+
g.files++;
|
|
472
|
+
g.bytes += size;
|
|
473
|
+
by.set(who, g);
|
|
474
|
+
if (who === "this app" && r.url.slice(r.url.lastIndexOf("/") + 1).startsWith("og-region.")) entries++;
|
|
475
|
+
}
|
|
476
|
+
return files ? {
|
|
477
|
+
files,
|
|
478
|
+
bytes,
|
|
479
|
+
wire,
|
|
480
|
+
by: [...by.values()].sort((a, b) => b.bytes - a.bytes),
|
|
481
|
+
...entries ? { island_entries: entries } : {}
|
|
482
|
+
} : null;
|
|
483
|
+
}
|
|
484
|
+
/** `this app`, a CDN package (`@scope/name` after `/npm/`), or the host. */
|
|
485
|
+
function serves(url, origin) {
|
|
486
|
+
let u;
|
|
487
|
+
try {
|
|
488
|
+
u = new URL(url);
|
|
489
|
+
} catch {
|
|
490
|
+
return "unknown";
|
|
491
|
+
}
|
|
492
|
+
if (u.origin === origin) return "this app";
|
|
493
|
+
const at = u.pathname.indexOf("/npm/");
|
|
494
|
+
if (at !== -1) {
|
|
495
|
+
const parts = u.pathname.slice(at + 5).split("/");
|
|
496
|
+
const pkg = parts[0].startsWith("@") ? `${parts[0]}/${parts[1] ?? ""}` : parts[0];
|
|
497
|
+
const bare = pkg.lastIndexOf("@") > 0 ? pkg.slice(0, pkg.lastIndexOf("@")) : pkg;
|
|
498
|
+
return `${u.host} ${bare}`;
|
|
499
|
+
}
|
|
500
|
+
return u.host;
|
|
501
|
+
}
|
|
502
|
+
function byte_length(s) {
|
|
503
|
+
let n = 0;
|
|
504
|
+
for (let i = 0; i < s.length; i++) {
|
|
505
|
+
const c = s.charCodeAt(i);
|
|
506
|
+
if (c < 128) n += 1;
|
|
507
|
+
else if (c < 2048) n += 2;
|
|
508
|
+
else if (c >= 55296 && c <= 56319) {
|
|
509
|
+
n += 4;
|
|
510
|
+
i++;
|
|
511
|
+
} else n += 3;
|
|
512
|
+
}
|
|
513
|
+
return n;
|
|
514
|
+
}
|
|
515
|
+
/** A status that says the file is not there for good: a client error, but not a timeout or a
|
|
516
|
+
* rate limit (408, 429), which say "not now", as a 5xx does. */
|
|
517
|
+
const gone_for_good = (status) => status >= 400 && status < 500 && status !== 408 && status !== 429;
|
|
518
|
+
async function weigh_one(r, opts, deadline, origin) {
|
|
519
|
+
const left = deadline - clock.mono();
|
|
520
|
+
if (left < 100) return { miss: "budget" };
|
|
521
|
+
try {
|
|
522
|
+
const wait = scope();
|
|
523
|
+
const res = await Promise.race([opts.fetch_url(r.url.startsWith(origin) ? r.url.slice(origin.length) || "/" : r.url), new Promise((ok) => wait.timeout(() => ok(null), left))]).finally(() => wait.close());
|
|
524
|
+
if (!res) return { miss: "timeout" };
|
|
525
|
+
if (!res.ok) return gone_for_good(res.status) ? {
|
|
526
|
+
miss: `status ${res.status}`,
|
|
527
|
+
gone: true
|
|
528
|
+
} : { miss: `status ${res.status}` };
|
|
529
|
+
const enc = res.headers.get("content-encoding");
|
|
530
|
+
const len = Number(res.headers.get("content-length"));
|
|
531
|
+
const buf = new Uint8Array(await res.arrayBuffer());
|
|
532
|
+
let wire = buf.byteLength;
|
|
533
|
+
if (enc && Number.isFinite(len) && len > 0) wire = len;
|
|
534
|
+
else if (opts.gzip && buf.byteLength > 256) wire = opts.gzip(buf);
|
|
535
|
+
const w = {
|
|
536
|
+
bytes: buf.byteLength,
|
|
537
|
+
wire
|
|
538
|
+
};
|
|
539
|
+
if (r.kind === "script") {
|
|
540
|
+
const code = new TextDecoder().decode(buf);
|
|
541
|
+
const imports = [];
|
|
542
|
+
for (const spec of static_imports(code)) {
|
|
543
|
+
const abs = resolve_ref(spec, r.url);
|
|
544
|
+
if (abs) imports.push(abs);
|
|
545
|
+
}
|
|
546
|
+
if (imports.length) w.imports = imports;
|
|
547
|
+
const dynamic = [];
|
|
548
|
+
for (const spec of dynamic_imports(code)) {
|
|
549
|
+
const abs = resolve_ref(spec, r.url);
|
|
550
|
+
if (abs) dynamic.push(abs);
|
|
551
|
+
}
|
|
552
|
+
if (dynamic.length) w.dynamic = dynamic;
|
|
553
|
+
} else if (r.kind === "style") {
|
|
554
|
+
const u = find_unscoped(new TextDecoder().decode(buf));
|
|
555
|
+
if (u.length) w.unscoped = u;
|
|
556
|
+
} else if (r.kind === "font") {
|
|
557
|
+
const vary = res.headers.get("vary");
|
|
558
|
+
if (vary) w.vary = vary.slice(0, 120);
|
|
559
|
+
}
|
|
560
|
+
return w;
|
|
561
|
+
} catch (e) {
|
|
562
|
+
return { miss: `error: ${(e instanceof Error ? e.message : String(e)).slice(0, 120)}` };
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
export { assets_diff, dynamic_imports, kit_node_ids, resolve_ref, runtime_scripts, scan_assets, static_imports, tag_attrs, weigh_assets };
|