@bymaksym/loadline 0.1.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 +182 -0
- package/bin/loadline.js +50 -0
- package/dist/cli/cli/args.js +324 -0
- package/dist/cli/cli/args.types.js +3 -0
- package/dist/cli/cli/gates.js +173 -0
- package/dist/cli/cli/gates.types.js +3 -0
- package/dist/cli/cli/main.js +156 -0
- package/dist/cli/cli/read-build.js +382 -0
- package/dist/cli/cli/read-build.types.js +3 -0
- package/dist/cli/cli/render-json.js +116 -0
- package/dist/cli/cli/render-json.types.js +10 -0
- package/dist/cli/cli/render-markdown.js +30 -0
- package/dist/cli/cli/render-pr.js +76 -0
- package/dist/cli/cli/render-sarif.js +83 -0
- package/dist/cli/cli/render-text.js +388 -0
- package/dist/cli/cli/report.js +196 -0
- package/dist/cli/cli/report.types.js +3 -0
- package/dist/cli/cli/self-check.js +85 -0
- package/dist/cli/cli/text.js +180 -0
- package/dist/cli/cli/text.types.js +10 -0
- package/dist/cli/src/app/core/analysis/analysis.js +661 -0
- package/dist/cli/src/app/core/analysis/analysis.types.js +6 -0
- package/dist/cli/src/app/core/analysis/barrels.js +128 -0
- package/dist/cli/src/app/core/analysis/blast.js +172 -0
- package/dist/cli/src/app/core/analysis/cycles.js +110 -0
- package/dist/cli/src/app/core/analysis/delivery.js +143 -0
- package/dist/cli/src/app/core/analysis/entries.js +217 -0
- package/dist/cli/src/app/core/analysis/exclusive.js +91 -0
- package/dist/cli/src/app/core/analysis/importers.js +38 -0
- package/dist/cli/src/app/core/analysis/insights.js +202 -0
- package/dist/cli/src/app/core/analysis/insights.types.js +10 -0
- package/dist/cli/src/app/core/analysis/invariant.js +69 -0
- package/dist/cli/src/app/core/analysis/metafile.types.js +44 -0
- package/dist/cli/src/app/core/analysis/module-graph.js +192 -0
- package/dist/cli/src/app/core/analysis/sourcemap.js +231 -0
- package/dist/cli/src/app/core/analysis/sourcemap.types.js +3 -0
- package/dist/cli/src/app/core/analysis/split-drift.js +47 -0
- package/dist/cli/src/app/core/assets/assets.js +296 -0
- package/dist/cli/src/app/core/assets/assets.types.js +11 -0
- package/dist/cli/src/app/core/baseline/baseline.js +0 -0
- package/dist/cli/src/app/core/baseline/baseline.types.js +3 -0
- package/dist/cli/src/app/core/caching/caching.js +241 -0
- package/dist/cli/src/app/core/caching/caching.types.js +10 -0
- package/dist/cli/src/app/core/caching/from-analysis.js +73 -0
- package/dist/cli/src/app/core/config/loadline-config.js +110 -0
- package/dist/cli/src/app/core/config/loadline-config.types.js +12 -0
- package/dist/cli/src/app/core/criteria/criteria.js +228 -0
- package/dist/cli/src/app/core/criteria/criteria.types.js +3 -0
- package/dist/cli/src/app/core/deps/deps.js +225 -0
- package/dist/cli/src/app/core/deps/deps.types.js +15 -0
- package/dist/cli/src/app/core/export/markdown-table.utils.js +40 -0
- package/dist/cli/src/app/core/export/summary.js +72 -0
- package/dist/cli/src/app/core/findings/actions.js +59 -0
- package/dist/cli/src/app/core/findings/assets.js +130 -0
- package/dist/cli/src/app/core/findings/caching.js +165 -0
- package/dist/cli/src/app/core/findings/compose.js +41 -0
- package/dist/cli/src/app/core/findings/deps.js +66 -0
- package/dist/cli/src/app/core/findings/effort.js +117 -0
- package/dist/cli/src/app/core/findings/finding-html.js +13 -0
- package/dist/cli/src/app/core/findings/finding-plain.js +26 -0
- package/dist/cli/src/app/core/findings/finding-text.js +1231 -0
- package/dist/cli/src/app/core/findings/finding.types.js +80 -0
- package/dist/cli/src/app/core/findings/findings.js +514 -0
- package/dist/cli/src/app/core/findings/graph.js +167 -0
- package/dist/cli/src/app/core/findings/own-in-boot.js +88 -0
- package/dist/cli/src/app/core/findings/page.js +49 -0
- package/dist/cli/src/app/core/findings/requests.js +185 -0
- package/dist/cli/src/app/core/findings/scan.js +126 -0
- package/dist/cli/src/app/core/findings/shipped.js +244 -0
- package/dist/cli/src/app/core/findings/situation.js +116 -0
- package/dist/cli/src/app/core/format/format.utils.js +204 -0
- package/dist/cli/src/app/core/i18n/en.js +819 -0
- package/dist/cli/src/app/core/i18n/es.js +822 -0
- package/dist/cli/src/app/core/i18n/ui-strings.js +11 -0
- package/dist/cli/src/app/core/i18n/ui.js +7 -0
- package/dist/cli/src/app/core/intake/bundle-graph.js +346 -0
- package/dist/cli/src/app/core/intake/bundle-graph.types.js +3 -0
- package/dist/cli/src/app/core/intake/dist-files.js +160 -0
- package/dist/cli/src/app/core/intake/index-html.js +292 -0
- package/dist/cli/src/app/core/json/json.utils.js +55 -0
- package/dist/cli/src/app/core/measurement/measurement.types.js +14 -0
- package/dist/cli/src/app/core/project/budget-advice.js +76 -0
- package/dist/cli/src/app/core/project/project-context.js +394 -0
- package/dist/cli/src/app/core/project/project-context.types.js +3 -0
- package/dist/cli/src/app/core/scan/catalog.js +67 -0
- package/dist/cli/src/app/core/scan/scan.js +280 -0
- package/dist/cli/src/app/core/scan/scan.types.js +10 -0
- package/dist/cli/src/app/core/scan/secrets.js +0 -0
- package/dist/cli/src/app/core/situation/situation.js +261 -0
- package/dist/cli/src/app/core/situation/situation.types.js +19 -0
- package/dist/cli/src/app/core/timing/timing.js +87 -0
- package/dist/cli/src/app/core/timing/timing.types.js +3 -0
- package/dist/cli/src/app/core/whatif/defer.js +68 -0
- package/dist/cli/src/app/state/report-messages.utils.js +73 -0
- package/docs/HOW-IT-WORKS.md +1087 -0
- package/loadline.html +78 -0
- package/package.json +154 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reading and writing `loadline.json`, and applying what it says to a list of signals.
|
|
4
|
+
*
|
|
5
|
+
* Nothing here throws. A configuration file with a mistake in it is the worst possible thing to
|
|
6
|
+
* fail a build on silently — the report is what the person came for — so every problem is collected
|
|
7
|
+
* and printed, and the run carries on with whatever of the file could be understood.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.writeConfig = exports.applyAcceptances = exports.readConfig = exports.isLoadlineConfig = void 0;
|
|
11
|
+
const finding_types_1 = require("../findings/finding.types");
|
|
12
|
+
const situation_1 = require("../situation/situation");
|
|
13
|
+
const KINDS = new Set(finding_types_1.FINDING_KINDS);
|
|
14
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
15
|
+
/** Tells the file apart from anything else somebody points `--config` at. */
|
|
16
|
+
const isLoadlineConfig = (value) => {
|
|
17
|
+
if (!value || typeof value !== 'object') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const candidate = value;
|
|
21
|
+
return candidate.tool === 'loadline' && candidate.version === 1;
|
|
22
|
+
};
|
|
23
|
+
exports.isLoadlineConfig = isLoadlineConfig;
|
|
24
|
+
/**
|
|
25
|
+
* Reads the file, naming what is wrong with it instead of failing.
|
|
26
|
+
*
|
|
27
|
+
* An acceptance is dropped when it names a signal that does not exist or gives no reason: both are
|
|
28
|
+
* ways of suppressing something by accident, and a suppression nobody meant is worse than a noisy
|
|
29
|
+
* report. Everything else is kept.
|
|
30
|
+
*/
|
|
31
|
+
const readConfig = (value) => {
|
|
32
|
+
if (!(0, exports.isLoadlineConfig)(value)) {
|
|
33
|
+
return { config: null, problems: ['Not a loadline.json: it needs "tool": "loadline" and "version": 1.'] };
|
|
34
|
+
}
|
|
35
|
+
const problems = [];
|
|
36
|
+
const accepted = [];
|
|
37
|
+
const entries = value.accepted ?? [];
|
|
38
|
+
for (const entry of entries) {
|
|
39
|
+
const where = entry.key ? `${entry.kind} · ${entry.key}` : entry.kind;
|
|
40
|
+
if (!KINDS.has(entry.kind)) {
|
|
41
|
+
problems.push(`Unknown signal in "accepted": ${entry.kind}. It accepts nothing.`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!entry.why || entry.why.trim().length === 0) {
|
|
45
|
+
problems.push(`The acceptance of ${where} has no "why". An acceptance without a reason is a suppression.`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (entry.until && !DATE.test(entry.until)) {
|
|
49
|
+
problems.push(`The "until" of ${where} is not a YYYY-MM-DD date: ${entry.until}. It is ignored.`);
|
|
50
|
+
accepted.push({ ...entry, until: undefined });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
accepted.push(entry);
|
|
54
|
+
}
|
|
55
|
+
// The five answers get the same treatment as the acceptances, and for the same reason: an
|
|
56
|
+
// answer nobody recognises has to become "unanswered" and a line to print, never a silent
|
|
57
|
+
// nothing. An answer is the only thing in this file that can move a colour, so a typo in one
|
|
58
|
+
// is the one mistake here that would change a verdict without anybody noticing.
|
|
59
|
+
const { situation, problems: situationProblems } = (0, situation_1.readSituation)(value.situation);
|
|
60
|
+
problems.push(...situationProblems);
|
|
61
|
+
return { config: { ...value, accepted, situation }, problems };
|
|
62
|
+
};
|
|
63
|
+
exports.readConfig = readConfig;
|
|
64
|
+
/** Which named thing a signal is about, as an acceptance would name it. */
|
|
65
|
+
const keyOf = (finding) => finding.target?.key ?? '';
|
|
66
|
+
/**
|
|
67
|
+
* Applies the acceptances.
|
|
68
|
+
*
|
|
69
|
+
* Two things make an acceptance stop covering a signal, and both of them are the point rather than
|
|
70
|
+
* an edge case:
|
|
71
|
+
*
|
|
72
|
+
* - **It ran out.** The date passed. The signal comes back, with a line saying it was accepted
|
|
73
|
+
* until then, so nobody has to remember what the decision was.
|
|
74
|
+
* - **The figure moved.** What was accepted was 12 kB of duplication; the build now has 400. That
|
|
75
|
+
* is not the thing anybody agreed to live with.
|
|
76
|
+
*/
|
|
77
|
+
const applyAcceptances = (findings, config, today = new Date()) => {
|
|
78
|
+
const entries = config?.accepted ?? [];
|
|
79
|
+
if (entries.length === 0) {
|
|
80
|
+
return { kept: [...findings], accepted: [] };
|
|
81
|
+
}
|
|
82
|
+
const kept = [];
|
|
83
|
+
const accepted = [];
|
|
84
|
+
const day = today.toISOString().slice(0, 10);
|
|
85
|
+
for (const finding of findings) {
|
|
86
|
+
const entry = entries.find(item => item.kind === finding.kind && (!item.key || item.key === keyOf(finding)));
|
|
87
|
+
if (!entry) {
|
|
88
|
+
kept.push(finding);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const expired = !!entry.until && entry.until < day;
|
|
92
|
+
const grew = entry.bytes !== undefined && (finding.saving ?? 0) > entry.bytes;
|
|
93
|
+
const lapse = expired ? 'expired' : grew ? 'grew' : null;
|
|
94
|
+
accepted.push({ finding, entry, lapse });
|
|
95
|
+
if (lapse) {
|
|
96
|
+
kept.push(finding);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { kept, accepted };
|
|
100
|
+
};
|
|
101
|
+
exports.applyAcceptances = applyAcceptances;
|
|
102
|
+
/**
|
|
103
|
+
* The file as the page writes it.
|
|
104
|
+
*
|
|
105
|
+
* The whole criteria object goes in, not only what was changed: the file has to pin every threshold
|
|
106
|
+
* or the next release of the tool silently moves one of them, which is the same drift this file
|
|
107
|
+
* exists to end.
|
|
108
|
+
*/
|
|
109
|
+
const writeConfig = (config) => `${JSON.stringify({ tool: 'loadline', version: 1, ...config }, null, 4)}\n`;
|
|
110
|
+
exports.writeConfig = writeConfig;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `loadline.json`: the thresholds, the gates and the accepted signals, kept next to the code.
|
|
4
|
+
*
|
|
5
|
+
* It exists because the two halves of this tool used to disagree with each other. Somebody tuned
|
|
6
|
+
* the criteria on the page, and their pipeline went on judging the build by the recommended ones —
|
|
7
|
+
* which is exactly the failure the tool catches in `angular.json`, sitting inside the tool itself.
|
|
8
|
+
*
|
|
9
|
+
* The page writes this file and the command reads it. It is meant to be committed: a threshold that
|
|
10
|
+
* lives in one person's browser is a threshold the team has not agreed on.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Rating criteria: the thresholds Loadline uses to say "good", "fair" or "bad", and the ones that fire
|
|
4
|
+
* the signals. The recommended values live here; the person can change them from the UI.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.fromField = exports.toField = exports.CRITERIA_FIELDS = exports.worst = exports.rate = exports.RECOMMENDED = exports.MODES = void 0;
|
|
8
|
+
const KB = 1024;
|
|
9
|
+
/** The three units a report can be in. One list, so the page, the export and the command agree. */
|
|
10
|
+
exports.MODES = ['raw', 'gzip', 'brotli'];
|
|
11
|
+
/** Proportions and counts: nothing about them changes when the figures are compressed. */
|
|
12
|
+
const SHARES = {
|
|
13
|
+
sharedRatio: 0.6,
|
|
14
|
+
wideRatio: 0.3,
|
|
15
|
+
dominantRatio: 0.5,
|
|
16
|
+
bootPackageMaxImporters: 4,
|
|
17
|
+
heavyScreenFactor: 6,
|
|
18
|
+
heavyScreenMinScreens: 4,
|
|
19
|
+
growthRatio: 0.1,
|
|
20
|
+
sharedGrowthTolerance: 0.15,
|
|
21
|
+
sharedGrowthMinScreens: 3,
|
|
22
|
+
budgetSlackFactor: 2,
|
|
23
|
+
theirsRatio: 0.7,
|
|
24
|
+
latencyMs: 150,
|
|
25
|
+
manyCrumbs: 3,
|
|
26
|
+
concentratedRatio: 0.6,
|
|
27
|
+
heavyInZoneRatio: 0.25,
|
|
28
|
+
heavyShareRatio: 0.8,
|
|
29
|
+
minTinyChunks: 5,
|
|
30
|
+
minLocales: 3,
|
|
31
|
+
/**
|
|
32
|
+
* Thirty files for one screen. Requests over HTTP/2 are cheap and not free: each one is a
|
|
33
|
+
* round of headers, a cache entry and a module wrapper, and browsers schedule a handful at a
|
|
34
|
+
* time. The number is a place to start looking, not a budget — the signal it fires says as
|
|
35
|
+
* much, and says when to ignore it.
|
|
36
|
+
*/
|
|
37
|
+
screenFilesMax: 30,
|
|
38
|
+
/**
|
|
39
|
+
* Three round trips. One is the ideal — the loader asks for everything at once — and two is
|
|
40
|
+
* what a chunk importing a shared chunk costs, which is ordinary. At three there is a chain:
|
|
41
|
+
* the browser cannot know about the last file until two others have arrived and been parsed,
|
|
42
|
+
* and on a connection with 200 ms of latency that is half a second the size figures cannot see.
|
|
43
|
+
*/
|
|
44
|
+
screenWavesMax: 3,
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Sizes measured **inside** a chunk: what one source file or one package contributes to it.
|
|
48
|
+
*
|
|
49
|
+
* These do not have a compressed version and cannot have one — gzip works on the whole file, so
|
|
50
|
+
* there is no compressed share of a single module — so the figure they are compared against is
|
|
51
|
+
* always raw minified bytes and the threshold is the same number in the three modes.
|
|
52
|
+
*
|
|
53
|
+
* They used to sit in the per-mode table below, which meant that loading the build folder made
|
|
54
|
+
* `bootPackageMinBytes` drop from 25 kB to 8 kB while the figure it was compared against did not
|
|
55
|
+
* move at all: the same build raised the bootstrap-package signal about three times more often
|
|
56
|
+
* for no reason having anything to do with the build.
|
|
57
|
+
*/
|
|
58
|
+
const FILE_SIZES = {
|
|
59
|
+
bootPackageMinBytes: 25 * KB,
|
|
60
|
+
ownFolderMinBytes: 12 * KB,
|
|
61
|
+
bigOwnFileBytes: 20 * KB,
|
|
62
|
+
shippedMinBytes: 10 * KB,
|
|
63
|
+
grouperMaxBytes: 1 * KB,
|
|
64
|
+
barrelMinBytes: 20 * KB,
|
|
65
|
+
paidTwiceMinBytes: 10 * KB,
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Sizes that price a **request** rather than a budget: below them, asking for a file costs about
|
|
69
|
+
* what the file holds. What travels is the compressed file, so the compressed value is the one
|
|
70
|
+
* that was reasoned about and the raw column is the same line in uncompressed bytes.
|
|
71
|
+
*/
|
|
72
|
+
const REQUEST_SIZES = {
|
|
73
|
+
raw: { crumbMaxBytes: 15 * KB, tinyChunkBytes: 3 * KB },
|
|
74
|
+
gzip: { crumbMaxBytes: 5 * KB, tinyChunkBytes: 1 * KB },
|
|
75
|
+
brotli: { crumbMaxBytes: 5 * KB, tinyChunkBytes: 1 * KB },
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The signal thresholds that are sizes of a whole chunk, in the unit of the report.
|
|
79
|
+
*
|
|
80
|
+
* They used to be one set of raw figures for the three modes. That looked stable and was not: a
|
|
81
|
+
* 50 kB minimum applied to gzip figures is three times stricter than the same rule applied to raw
|
|
82
|
+
* ones, so on a compressed report every shared chunk fell under it and the tab said "67 below the
|
|
83
|
+
* size worth bothering with" while the same build in raw would have flagged several. A threshold
|
|
84
|
+
* that means something different depending on the unit is not one threshold.
|
|
85
|
+
*
|
|
86
|
+
* The raw column is the original set; the other two are the same budgets in their own unit, with
|
|
87
|
+
* the 3:1 ratio JavaScript compresses at and brotli's extra 15 % — exactly what the size thresholds
|
|
88
|
+
* above already do.
|
|
89
|
+
*/
|
|
90
|
+
const SIGNAL_SIZES = {
|
|
91
|
+
raw: {
|
|
92
|
+
sharedMinBytes: 50 * KB,
|
|
93
|
+
heavyScreenMinBytes: 100 * KB,
|
|
94
|
+
growthMinBytes: 10 * KB,
|
|
95
|
+
},
|
|
96
|
+
gzip: {
|
|
97
|
+
sharedMinBytes: 17 * KB,
|
|
98
|
+
heavyScreenMinBytes: 34 * KB,
|
|
99
|
+
growthMinBytes: 3 * KB,
|
|
100
|
+
},
|
|
101
|
+
brotli: {
|
|
102
|
+
sharedMinBytes: 14 * KB,
|
|
103
|
+
heavyScreenMinBytes: 29 * KB,
|
|
104
|
+
growthMinBytes: 3 * KB,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Recommended values.
|
|
109
|
+
*
|
|
110
|
+
* Compressed: ~170 kB of initial JavaScript is the usual budget for a page to become responsive
|
|
111
|
+
* within a few seconds on a mid-range phone; twice that is clearly too much. Per-screen and
|
|
112
|
+
* own-code values are reasonable multiples of that same budget.
|
|
113
|
+
*
|
|
114
|
+
* Raw: 500 kB and 1 MB are the budgets Angular CLI writes by default into `angular.json`
|
|
115
|
+
* (`maximumWarning` / `maximumError` of the initial bundle). The rest scales like compressed,
|
|
116
|
+
* with the typical 3:1 ratio between raw and gzip for JavaScript.
|
|
117
|
+
*/
|
|
118
|
+
exports.RECOMMENDED = {
|
|
119
|
+
gzip: {
|
|
120
|
+
bootOk: 170 * KB,
|
|
121
|
+
bootBad: 350 * KB,
|
|
122
|
+
screenOk: 300 * KB,
|
|
123
|
+
screenBad: 600 * KB,
|
|
124
|
+
ownOk: 50 * KB,
|
|
125
|
+
ownBad: 150 * KB,
|
|
126
|
+
...SHARES,
|
|
127
|
+
...FILE_SIZES,
|
|
128
|
+
...REQUEST_SIZES.gzip,
|
|
129
|
+
...SIGNAL_SIZES.gzip,
|
|
130
|
+
},
|
|
131
|
+
/**
|
|
132
|
+
* Brotli takes some 15 % more off minified JavaScript than gzip does. A threshold thought of
|
|
133
|
+
* in gzip has to come down by the same amount, or every figure turns green at once for a
|
|
134
|
+
* reason that has nothing to do with the app.
|
|
135
|
+
*/
|
|
136
|
+
brotli: {
|
|
137
|
+
bootOk: 145 * KB,
|
|
138
|
+
bootBad: 300 * KB,
|
|
139
|
+
screenOk: 255 * KB,
|
|
140
|
+
screenBad: 510 * KB,
|
|
141
|
+
ownOk: 42 * KB,
|
|
142
|
+
ownBad: 128 * KB,
|
|
143
|
+
...SHARES,
|
|
144
|
+
...FILE_SIZES,
|
|
145
|
+
...REQUEST_SIZES.brotli,
|
|
146
|
+
...SIGNAL_SIZES.brotli,
|
|
147
|
+
},
|
|
148
|
+
raw: {
|
|
149
|
+
bootOk: 500 * KB,
|
|
150
|
+
bootBad: 1024 * KB,
|
|
151
|
+
screenOk: 1024 * KB,
|
|
152
|
+
screenBad: 2048 * KB,
|
|
153
|
+
ownOk: 150 * KB,
|
|
154
|
+
ownBad: 450 * KB,
|
|
155
|
+
...SHARES,
|
|
156
|
+
...FILE_SIZES,
|
|
157
|
+
...REQUEST_SIZES.raw,
|
|
158
|
+
...SIGNAL_SIZES.raw,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
/** Rating of a figure against its two thresholds. */
|
|
162
|
+
const rate = (value, ok, bad) => {
|
|
163
|
+
if (value <= ok) {
|
|
164
|
+
return 'good';
|
|
165
|
+
}
|
|
166
|
+
return value <= bad ? 'ok' : 'bad';
|
|
167
|
+
};
|
|
168
|
+
exports.rate = rate;
|
|
169
|
+
/** The worst of several verdicts: useful to summarise a list. */
|
|
170
|
+
const worst = (verdicts) => {
|
|
171
|
+
if (verdicts.includes('bad')) {
|
|
172
|
+
return 'bad';
|
|
173
|
+
}
|
|
174
|
+
return verdicts.includes('ok') ? 'ok' : 'good';
|
|
175
|
+
};
|
|
176
|
+
exports.worst = worst;
|
|
177
|
+
exports.CRITERIA_FIELDS = [
|
|
178
|
+
{ key: 'bootOk', unit: 'kb', group: 'sizes', scale: 'chunk', pairWith: 'bootBad', from: 'external' },
|
|
179
|
+
{ key: 'screenOk', unit: 'kb', group: 'sizes', scale: 'chunk', pairWith: 'screenBad', from: 'derived' },
|
|
180
|
+
{ key: 'ownOk', unit: 'kb', group: 'sizes', scale: 'chunk', pairWith: 'ownBad', from: 'derived' },
|
|
181
|
+
{ key: 'sharedRatio', unit: 'pct', group: 'shared', from: 'convention' },
|
|
182
|
+
{ key: 'wideRatio', unit: 'pct', group: 'shared', from: 'convention' },
|
|
183
|
+
{ key: 'sharedMinBytes', unit: 'kb', group: 'shared', scale: 'chunk', from: 'convention' },
|
|
184
|
+
{ key: 'dominantRatio', unit: 'pct', group: 'shared', from: 'convention' },
|
|
185
|
+
{ key: 'bootPackageMinBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
186
|
+
{ key: 'bootPackageMaxImporters', unit: 'importers', group: 'signals', from: 'convention' },
|
|
187
|
+
{ key: 'ownFolderMinBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
188
|
+
{ key: 'bigOwnFileBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
189
|
+
{ key: 'heavyScreenFactor', unit: 'x', group: 'signals', from: 'convention' },
|
|
190
|
+
{ key: 'heavyScreenMinBytes', unit: 'kb', group: 'signals', scale: 'chunk', from: 'convention' },
|
|
191
|
+
{ key: 'heavyScreenMinScreens', unit: 'screens', group: 'signals', from: 'convention' },
|
|
192
|
+
{ key: 'screenFilesMax', unit: 'files', group: 'signals', from: 'convention' },
|
|
193
|
+
{ key: 'screenWavesMax', unit: 'trips', group: 'signals', from: 'convention' },
|
|
194
|
+
{ key: 'tinyChunkBytes', unit: 'kb', group: 'signals', scale: 'request', from: 'convention' },
|
|
195
|
+
{ key: 'minTinyChunks', unit: 'chunks', group: 'signals', from: 'convention' },
|
|
196
|
+
{ key: 'minLocales', unit: 'langs', group: 'signals', from: 'convention' },
|
|
197
|
+
{ key: 'shippedMinBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
198
|
+
{ key: 'barrelMinBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
199
|
+
{ key: 'paidTwiceMinBytes', unit: 'kb', group: 'signals', scale: 'file', from: 'convention' },
|
|
200
|
+
{ key: 'crumbMaxBytes', unit: 'kb', group: 'shape', scale: 'request', from: 'convention' },
|
|
201
|
+
{ key: 'manyCrumbs', unit: 'chunks', group: 'shape', from: 'convention' },
|
|
202
|
+
{ key: 'concentratedRatio', unit: 'pct', group: 'shape', from: 'convention' },
|
|
203
|
+
{ key: 'heavyInZoneRatio', unit: 'pct', group: 'shape', from: 'convention' },
|
|
204
|
+
{ key: 'heavyShareRatio', unit: 'pct', group: 'shape', from: 'convention' },
|
|
205
|
+
{ key: 'grouperMaxBytes', unit: 'kb', group: 'shape', scale: 'file', from: 'convention' },
|
|
206
|
+
{ key: 'growthRatio', unit: 'pct', group: 'context', from: 'convention' },
|
|
207
|
+
{ key: 'growthMinBytes', unit: 'kb', group: 'context', scale: 'chunk', from: 'convention' },
|
|
208
|
+
{ key: 'sharedGrowthTolerance', unit: 'pct', group: 'context', from: 'convention' },
|
|
209
|
+
{ key: 'sharedGrowthMinScreens', unit: 'screens', group: 'context', from: 'convention' },
|
|
210
|
+
{ key: 'budgetSlackFactor', unit: 'x', group: 'context', from: 'convention' },
|
|
211
|
+
{ key: 'theirsRatio', unit: 'pct', group: 'context', from: 'convention' },
|
|
212
|
+
{ key: 'latencyMs', unit: 'ms', group: 'context', from: 'external' },
|
|
213
|
+
];
|
|
214
|
+
/** From bytes or fraction to the number shown in the field, and back. */
|
|
215
|
+
const toField = (value, unit) => {
|
|
216
|
+
if (unit === 'kb') {
|
|
217
|
+
return Math.round(value / KB);
|
|
218
|
+
}
|
|
219
|
+
return unit === 'pct' ? Math.round(value * 100) : value;
|
|
220
|
+
};
|
|
221
|
+
exports.toField = toField;
|
|
222
|
+
const fromField = (value, unit) => {
|
|
223
|
+
if (unit === 'kb') {
|
|
224
|
+
return value * KB;
|
|
225
|
+
}
|
|
226
|
+
return unit === 'pct' ? value / 100 : value;
|
|
227
|
+
};
|
|
228
|
+
exports.fromField = fromField;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reading a lock file and an audit report, and crossing them with what ships.
|
|
4
|
+
*
|
|
5
|
+
* Three lock formats, because a project has whichever one its package manager writes and asking
|
|
6
|
+
* somebody to convert theirs would be worse than reading all three:
|
|
7
|
+
*
|
|
8
|
+
* - `package-lock.json` (npm): a `packages` object keyed by install path, or the older `dependencies`
|
|
9
|
+
* tree. Both are read.
|
|
10
|
+
* - `pnpm-lock.yaml`: YAML, and read with regular expressions rather than a YAML parser — the same
|
|
11
|
+
* decision `index-html.ts` makes about the DOM. A parser would be the only runtime dependency of
|
|
12
|
+
* a tool whose point is that it has none, and what is wanted here is two fields per entry.
|
|
13
|
+
* - `yarn.lock`: its own format, close enough to the pnpm reading to share the walk.
|
|
14
|
+
*
|
|
15
|
+
* Everything a lock file cannot say is left unsaid. There is no version resolution here, no range
|
|
16
|
+
* matching, no guessing: an advisory is attributed to a package by name, and the version is shown
|
|
17
|
+
* so whoever reads it can judge.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.readDeps = exports.readAudit = exports.readLock = void 0;
|
|
21
|
+
const json_utils_1 = require("../json/json.utils");
|
|
22
|
+
/** ` /lodash@4.17.21:` in pnpm, `"node_modules/lodash": {` in npm, `lodash@^4.0.0:` in yarn. */
|
|
23
|
+
const PNPM_ENTRY = /^ {2}\/?((?:@[^/@]+\/)?[^@/\s]+)@([^(:\s]+)/;
|
|
24
|
+
const YARN_ENTRY = /^"?((?:@[^/@]+\/)?[^@/\s]+)@[^:]*:?$/;
|
|
25
|
+
const YARN_VERSION = /^\s+version:?\s+"?([^"\s]+)"?/;
|
|
26
|
+
const SEVERITIES = new Set(['critical', 'high', 'moderate', 'low', 'info']);
|
|
27
|
+
/** The package name out of an npm lock key: `node_modules/a/node_modules/b` → `b`. */
|
|
28
|
+
const nameFromPath = (path) => {
|
|
29
|
+
const marker = path.lastIndexOf('node_modules/');
|
|
30
|
+
if (marker === -1) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const rest = path.slice(marker + 'node_modules/'.length);
|
|
34
|
+
const parts = rest.split('/');
|
|
35
|
+
return (parts[0]?.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0]) ?? null;
|
|
36
|
+
};
|
|
37
|
+
/** The `version` of one lock entry, when the entry is an object carrying one. */
|
|
38
|
+
const versionOf = (entry) => (0, json_utils_1.asText)((0, json_utils_1.asRecord)(entry)?.['version']);
|
|
39
|
+
const readNpmLock = (lock) => {
|
|
40
|
+
const found = new Map();
|
|
41
|
+
const installed = Object.entries((0, json_utils_1.asRecord)(lock['packages']) ?? {});
|
|
42
|
+
for (const [path, entry] of installed) {
|
|
43
|
+
const name = nameFromPath(path);
|
|
44
|
+
const version = versionOf(entry);
|
|
45
|
+
if (name && version) {
|
|
46
|
+
found.set(`${name}@${version}`, { name, version, requiredBy: [] });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// The older format, for a lock file written before npm 7. Same two fields, one level in.
|
|
50
|
+
const older = Object.entries((0, json_utils_1.asRecord)(lock['dependencies']) ?? {});
|
|
51
|
+
for (const [name, entry] of older) {
|
|
52
|
+
const version = versionOf(entry);
|
|
53
|
+
if (version) {
|
|
54
|
+
found.set(`${name}@${version}`, { name, version, requiredBy: [] });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return [...found.values()];
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The pnpm and yarn formats, read line by line.
|
|
61
|
+
*
|
|
62
|
+
* Regular expressions rather than a YAML parser, on purpose and for the same reason `index-html.ts`
|
|
63
|
+
* does not use `DOMParser`: what is wanted is two fields per entry, and a parser would be the only
|
|
64
|
+
* runtime dependency of a tool whose whole point is having none.
|
|
65
|
+
*/
|
|
66
|
+
const readYamlLock = (text) => {
|
|
67
|
+
const found = new Map();
|
|
68
|
+
const lines = text.split('\n');
|
|
69
|
+
let pending = null;
|
|
70
|
+
for (const line of lines) {
|
|
71
|
+
const pnpm = PNPM_ENTRY.exec(line);
|
|
72
|
+
if (pnpm?.[1] && pnpm[2]) {
|
|
73
|
+
found.set(`${pnpm[1]}@${pnpm[2]}`, { name: pnpm[1], version: pnpm[2], requiredBy: [] });
|
|
74
|
+
pending = null;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
// yarn writes the name on one line and the version on the next.
|
|
78
|
+
const version = YARN_VERSION.exec(line);
|
|
79
|
+
if (pending && version?.[1]) {
|
|
80
|
+
found.set(`${pending}@${version[1]}`, { name: pending, version: version[1], requiredBy: [] });
|
|
81
|
+
pending = null;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
pending = YARN_ENTRY.exec(line)?.[1] ?? null;
|
|
85
|
+
}
|
|
86
|
+
return [...found.values()];
|
|
87
|
+
};
|
|
88
|
+
/** Whichever format was dropped. `null` when it is none of them. */
|
|
89
|
+
const readLock = (name, text) => {
|
|
90
|
+
if (/package-lock\.json$|npm-shrinkwrap\.json$/i.test(name)) {
|
|
91
|
+
let parsed;
|
|
92
|
+
try {
|
|
93
|
+
parsed = JSON.parse(text);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const lock = (0, json_utils_1.asRecord)(parsed);
|
|
99
|
+
return lock ? readNpmLock(lock) : null;
|
|
100
|
+
}
|
|
101
|
+
return /lock\.ya?ml$|yarn\.lock$/i.test(name) ? readYamlLock(text) : null;
|
|
102
|
+
};
|
|
103
|
+
exports.readLock = readLock;
|
|
104
|
+
/**
|
|
105
|
+
* The audit report, in either shape npm and pnpm write.
|
|
106
|
+
*
|
|
107
|
+
* Nothing here checks a version range against a version: an advisory is attributed to a package by
|
|
108
|
+
* name, and the range is printed so whoever reads it can judge. Matching ranges properly needs a
|
|
109
|
+
* semver implementation, and being approximately right about whether somebody is vulnerable is
|
|
110
|
+
* worse than being explicit about what is and is not known.
|
|
111
|
+
*/
|
|
112
|
+
const readAudit = (text) => {
|
|
113
|
+
let parsed;
|
|
114
|
+
try {
|
|
115
|
+
parsed = JSON.parse(text);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
// An audit report is an object. `null`, a list and a bare number are all valid JSON, and the
|
|
121
|
+
// shape this used to assert made reading a property off any of them a crash rather than a
|
|
122
|
+
// rejection: `readAudit('null')` type-checked and threw.
|
|
123
|
+
const report = (0, json_utils_1.asRecord)(parsed);
|
|
124
|
+
if (!report) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const found = [];
|
|
128
|
+
const severityOf = (value) => (0, json_utils_1.asMember)((0, json_utils_1.asText)(value) ?? '', SEVERITIES) ?? 'info';
|
|
129
|
+
/** npm 7+ and pnpm: keyed by package name. */
|
|
130
|
+
const reported = (0, json_utils_1.asRecord)(report['vulnerabilities']);
|
|
131
|
+
const byPackage = Object.entries(reported ?? {});
|
|
132
|
+
for (const [name, value] of byPackage) {
|
|
133
|
+
const entry = (0, json_utils_1.asRecord)(value);
|
|
134
|
+
if (!entry) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const detail = ((0, json_utils_1.asArray)(entry['via']) ?? []).map(via => (0, json_utils_1.asRecord)(via)).find(via => via !== null);
|
|
138
|
+
found.push({
|
|
139
|
+
package: name,
|
|
140
|
+
severity: severityOf(entry['severity']),
|
|
141
|
+
title: (0, json_utils_1.asText)(detail?.['title']) ?? name,
|
|
142
|
+
url: (0, json_utils_1.asText)(detail?.['url']),
|
|
143
|
+
range: (0, json_utils_1.asText)(detail?.['range']),
|
|
144
|
+
// npm's `fixAvailable` is either `false` or an object naming a package and a version
|
|
145
|
+
// to move to — which is not the same as "this advisory is fixed in X". Rather than
|
|
146
|
+
// reword it into something it does not say, it is left out.
|
|
147
|
+
fixedIn: null,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/** npm 6 and the audit endpoint: a list of advisories. */
|
|
151
|
+
const legacy = (0, json_utils_1.asRecord)(report['advisories']);
|
|
152
|
+
const listed = Object.values(legacy ?? {});
|
|
153
|
+
for (const value of listed) {
|
|
154
|
+
const entry = (0, json_utils_1.asRecord)(value);
|
|
155
|
+
const module = (0, json_utils_1.asText)(entry?.['module_name']);
|
|
156
|
+
if (entry && module) {
|
|
157
|
+
found.push({
|
|
158
|
+
package: module,
|
|
159
|
+
severity: severityOf(entry['severity']),
|
|
160
|
+
title: (0, json_utils_1.asText)(entry['title']) ?? module,
|
|
161
|
+
url: (0, json_utils_1.asText)(entry['url']),
|
|
162
|
+
range: null,
|
|
163
|
+
fixedIn: null,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Neither key present means this JSON was not an audit report at all, which is not the same
|
|
168
|
+
// answer as an audit report with nothing in it.
|
|
169
|
+
return found.length > 0 || reported !== null || legacy !== null ? found : null;
|
|
170
|
+
};
|
|
171
|
+
exports.readAudit = readAudit;
|
|
172
|
+
const RANK = { critical: 0, high: 1, moderate: 2, low: 3, info: 4 };
|
|
173
|
+
/**
|
|
174
|
+
* The crossing. What every figure here is about is the word **shipped**: an advisory whose package
|
|
175
|
+
* never reaches a browser is real and is not what this report is for.
|
|
176
|
+
*/
|
|
177
|
+
const readDeps = (input) => {
|
|
178
|
+
const shippedBytes = new Map();
|
|
179
|
+
for (const module of input.modules) {
|
|
180
|
+
if (!module.pkg) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const entry = shippedBytes.get(module.pkg) ?? { bytes: 0, inBoot: false };
|
|
184
|
+
entry.bytes += module.bytes;
|
|
185
|
+
entry.inBoot ||= module.places.some(place => input.boot.has(place.chunk));
|
|
186
|
+
shippedBytes.set(module.pkg, entry);
|
|
187
|
+
}
|
|
188
|
+
const byName = new Map();
|
|
189
|
+
const locked = input.lock ?? [];
|
|
190
|
+
for (const entry of locked) {
|
|
191
|
+
byName.set(entry.name, [...(byName.get(entry.name) ?? []), entry]);
|
|
192
|
+
}
|
|
193
|
+
const advisories = input.advisories ?? [];
|
|
194
|
+
const shipped = advisories
|
|
195
|
+
.filter(advisory => shippedBytes.has(advisory.package))
|
|
196
|
+
.map(advisory => {
|
|
197
|
+
const where = shippedBytes.get(advisory.package);
|
|
198
|
+
return {
|
|
199
|
+
...advisory,
|
|
200
|
+
bytes: where?.bytes ?? 0,
|
|
201
|
+
inBoot: where?.inBoot ?? false,
|
|
202
|
+
chain: byName.get(advisory.package)?.[0]?.requiredBy ?? [],
|
|
203
|
+
};
|
|
204
|
+
})
|
|
205
|
+
.toSorted((a, b) => Number(b.inBoot) - Number(a.inBoot) || RANK[a.severity] - RANK[b.severity]);
|
|
206
|
+
return {
|
|
207
|
+
lockRead: input.lock !== null,
|
|
208
|
+
auditRead: input.advisories !== null,
|
|
209
|
+
shipped,
|
|
210
|
+
notShipped: advisories.filter(advisory => !shippedBytes.has(advisory.package)),
|
|
211
|
+
transitive: [...shippedBytes]
|
|
212
|
+
.filter(([name]) => input.direct.size > 0 && !input.direct.has(name))
|
|
213
|
+
.map(([name, entry]) => ({
|
|
214
|
+
name,
|
|
215
|
+
bytes: entry.bytes,
|
|
216
|
+
inBoot: entry.inBoot,
|
|
217
|
+
chain: byName.get(name)?.[0]?.requiredBy ?? [],
|
|
218
|
+
}))
|
|
219
|
+
.toSorted((a, b) => b.bytes - a.bytes),
|
|
220
|
+
multipleVersions: [...byName]
|
|
221
|
+
.filter(([name, entries]) => entries.length > 1 && shippedBytes.has(name))
|
|
222
|
+
.map(([name, entries]) => ({ name, versions: entries.map(entry => entry.version) })),
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
exports.readDeps = readDeps;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* What a lock file and an audit report say, crossed with what actually ships.
|
|
4
|
+
*
|
|
5
|
+
* This is the idea with the best value-to-rule ratio in the whole plan: it gives the entire
|
|
6
|
+
* "which of my dependencies are a problem" question without touching the rule that nothing goes out
|
|
7
|
+
* to the network. Both files are ones the person already has on their disk, and they come in through
|
|
8
|
+
* the same door `angular.json` does.
|
|
9
|
+
*
|
|
10
|
+
* The crossing is the part neither tool can do alone. `pnpm audit` knows you have 47
|
|
11
|
+
* vulnerabilities and nothing about which of them a browser downloads; Loadline knows what a
|
|
12
|
+
* browser downloads and nothing about vulnerabilities. "3 of your 47 are in the first load and
|
|
13
|
+
* everybody downloads them" is one afternoon well aimed; the other 44 are a backlog.
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.markdownTable = void 0;
|
|
4
|
+
const format_utils_1 = require("../format/format.utils");
|
|
5
|
+
/**
|
|
6
|
+
* The screens table as Markdown, to paste into a pull request or an issue. Takes the delta column
|
|
7
|
+
* when there is a baseline, and says in the lead line which unit the figures are in — a table of
|
|
8
|
+
* numbers with no unit is what makes a gzip figure get read as a raw one.
|
|
9
|
+
*/
|
|
10
|
+
const markdownTable = (analysis, comparison, t, unit) => {
|
|
11
|
+
const head = [t.colScreen, t.legBoot.split(' · ', 1)[0] ?? '', t.colShared, t.colOwn, t.colTotal, t.colWaves];
|
|
12
|
+
const align = ['---', '---:', '---:', '---:', '---:', '---:'];
|
|
13
|
+
if (comparison) {
|
|
14
|
+
head.push(`${t.colDelta} ${comparison.baselineName}`);
|
|
15
|
+
align.push('---:');
|
|
16
|
+
}
|
|
17
|
+
const rows = analysis.screens.map(screen => {
|
|
18
|
+
const cells = [
|
|
19
|
+
screen.label,
|
|
20
|
+
(0, format_utils_1.formatBytes)(screen.boot),
|
|
21
|
+
(0, format_utils_1.formatBytes)(screen.shared),
|
|
22
|
+
(0, format_utils_1.formatBytes)(screen.own),
|
|
23
|
+
(0, format_utils_1.formatBytes)(screen.total),
|
|
24
|
+
String(screen.waves),
|
|
25
|
+
];
|
|
26
|
+
if (comparison) {
|
|
27
|
+
const delta = comparison.screens.get(screen.source);
|
|
28
|
+
cells.push(delta ? (0, format_utils_1.formatDelta)(delta.total.diff) : t.compareNew);
|
|
29
|
+
}
|
|
30
|
+
return `| ${cells.join(' | ')} |`;
|
|
31
|
+
});
|
|
32
|
+
return [
|
|
33
|
+
`**${t.tileBoot}**: ${(0, format_utils_1.formatBytes)(analysis.bootBytes)} · ${analysis.screens.length} ${t.tabScreens.toLowerCase()} · ${unit}`,
|
|
34
|
+
'',
|
|
35
|
+
`| ${head.join(' | ')} |`,
|
|
36
|
+
`| ${align.join(' | ')} |`,
|
|
37
|
+
...rows,
|
|
38
|
+
].join('\n');
|
|
39
|
+
};
|
|
40
|
+
exports.markdownTable = markdownTable;
|