@preventive/triage 1.0.0-alpha.87 → 1.0.0-alpha.88
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/common/default-scan-models.ts +1 -0
- package/common/line-diff.d.ts +2 -1
- package/common/line-diff.js +12 -5
- package/common/pretty-json.js +25 -0
- package/common/pretty-print.d.ts +1 -0
- package/common/pretty-print.js +5 -0
- package/out/activity.js +2 -2
- package/out/{chunk-Q2NFBTNH.js → chunk-4GHLPL2P.js} +1 -1
- package/out/chunk-5PWEAEA5.js +1 -0
- package/out/chunk-7KUZQB6S.js +267 -0
- package/out/{chunk-PSS7UX5T.js → chunk-A4HSJCXG.js} +1 -1
- package/out/{chunk-6CCSTJWB.js → chunk-ESXA7KID.js} +1 -1
- package/out/{chunk-WKXQOUNH.js → chunk-U6JONPTN.js} +1 -1
- package/out/chunk-VRFGZ6MV.js +1 -0
- package/out/client-managed.js +1 -1
- package/out/compare.js +13 -13
- package/out/dialogs.js +22 -22
- package/out/index.html +4 -4
- package/out/scan.js +1 -1
- package/out/view.js +15 -15
- package/package.json +2 -2
- package/server-managed/bundle-build-worker.js +13 -6
- package/server-managed/bundle-build.ts +7 -5
- package/server-managed/npm-delta-worker.js +56 -8
- package/server-managed/npm-delta.ts +1 -1
- package/server-managed/pretty-format.d.ts +3 -2
- package/server-managed/pretty-format.js +17 -3
- package/server-managed/pretty-print.ts +18 -16
- package/server-managed/stasis-dependencies.ts +14 -5
- package/server-managed/type-only-proof.js +22 -2
- package/out/chunk-5LY6QSPD.js +0 -265
- package/out/chunk-7PXMWWTQ.js +0 -1
- package/out/chunk-ZKJ7COEH.js +0 -1
- /package/out/{chunk-3YPZ7UBC.js → chunk-WBK5ZN4K.js} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preventive/triage",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.88",
|
|
4
4
|
"description": "Client & relay server for triaging of automated reports",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -462,7 +462,7 @@
|
|
|
462
462
|
"@exodus/stasis": "1.0.0-rc.11",
|
|
463
463
|
"@exodus/stasis-core": "1.0.0-rc.11",
|
|
464
464
|
"@preventive/archive": "^1.5.0",
|
|
465
|
-
"@preventive/compare": "^1.5.
|
|
465
|
+
"@preventive/compare": "^1.5.1",
|
|
466
466
|
"@preventive/diff": "^1.1.1",
|
|
467
467
|
"@preventive/lockfile": "^1.3.1",
|
|
468
468
|
"@preventive/publishers": "^1.1.0",
|
|
@@ -205,20 +205,27 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
|
|
|
205
205
|
// The npm package versions a Stasis lockfile holds code of (an evidence
|
|
206
206
|
// file, by Stasis's audit rule), each once as it was installed: with its
|
|
207
207
|
// tarball's integrity and, patched, pnpm's hash of its patch (a sha256 in
|
|
208
|
-
// hex, as pnpm's lockfile names it), where the lockfile attests them
|
|
209
|
-
//
|
|
210
|
-
//
|
|
208
|
+
// hex, as pnpm's lockfile names it), where the lockfile attests them; and
|
|
209
|
+
// the `files` of it the build reaches, by their paths in the package, each
|
|
210
|
+
// with the integrity the lockfile gives it, those of every copy installed
|
|
211
|
+
// together. One installed from a directory, the repository's own code, is
|
|
212
|
+
// none of them; one whose record tells it malformed fails the build
|
|
213
|
+
// (isDirectoryPackage).
|
|
211
214
|
export function lockfileDependencies(lockfile) {
|
|
212
215
|
const found = new Map()
|
|
216
|
+
const byPath = ([a], [b]) => a < b ? -1 : 1
|
|
213
217
|
for (const [dir, info] of lockfile.modules) {
|
|
214
218
|
const { name, version } = info
|
|
215
219
|
if (dir === '.' || isDirectoryPackage(info) || dependencyEcosystem(dir, info.ecosystem) !== 'npm' || typeof name !== 'string' || !name || typeof version !== 'string' || !version) continue
|
|
216
220
|
if (!Object.keys(info.files).some(file => isEvidenceFile(name, version, file, 'npm'))) continue
|
|
217
221
|
const patch = typeof info.patch?.integrity === 'string' ? integrityHex(info.patch.integrity) : undefined
|
|
218
|
-
const
|
|
219
|
-
|
|
222
|
+
const integrity = typeof info.integrity === 'string' ? info.integrity : undefined
|
|
223
|
+
const key = `${name}\0${version}\0${integrity ?? ''}\0${patch ?? ''}`
|
|
224
|
+
// No prototype: a package's file may be named __proto__.
|
|
225
|
+
if (!found.has(key)) found.set(key, { name, version, ...(integrity && { integrity }), ...(patch && { patch }), files: Object.create(null) })
|
|
226
|
+
Object.assign(found.get(key).files, info.files)
|
|
220
227
|
}
|
|
221
|
-
return [...found].toSorted(([
|
|
228
|
+
return [...found].toSorted(byPath).map(([, one]) => ({ ...one, files: Object.fromEntries(Object.entries(one.files).toSorted(byPath)) }))
|
|
222
229
|
}
|
|
223
230
|
|
|
224
231
|
// The npm tarballs and version documents a build downloads, kept as files in
|
|
@@ -129,10 +129,12 @@ interface BuildRequest {
|
|
|
129
129
|
cacheDir: string | null
|
|
130
130
|
}
|
|
131
131
|
// A version a Stasis lockfile holds, as the worker tells it in place of a
|
|
132
|
-
// bundle (bundle-build-worker.js buildStasisLockfile)
|
|
133
|
-
//
|
|
134
|
-
//
|
|
132
|
+
// bundle (bundle-build-worker.js buildStasisLockfile), with the files of it
|
|
133
|
+
// the build reaches, each path's integrity (StasisReached); and what it is
|
|
134
|
+
// asked to build one of, with the `branch` the commit is of, where its asker
|
|
135
|
+
// knows it, as a bundle's is (BundleBuildInput).
|
|
135
136
|
export interface StasisDependency { name: string; version: string; integrity?: string; patch?: string }
|
|
137
|
+
export type StasisReached = StasisDependency & { files: Record<string, string> }
|
|
136
138
|
export interface LockfileInput { repoId: number; commit: string; config: string; branch?: string }
|
|
137
139
|
interface LockfileRequest { input: LockfileInput; github: string; token: string | null; cacheDir: string | null }
|
|
138
140
|
// An extra per-process ceiling, one build at a time, its worker given the heap
|
|
@@ -228,7 +230,7 @@ export function buildRepositoryBundle(userId: string, request: BuildRequest, sig
|
|
|
228
230
|
// the deployment's lease as its one builder of them: in the same one slot,
|
|
229
231
|
// which it gives up to a build asked for, with its lease (`build-yielded`),
|
|
230
232
|
// and is refused while another holds (`build-busy`), to be built later.
|
|
231
|
-
export async function buildRepositoryLockfile(db: BundleBuildLeaseStore, request: LockfileRequest, signal: AbortSignal): Promise<
|
|
233
|
+
export async function buildRepositoryLockfile(db: BundleBuildLeaseStore, request: LockfileRequest, signal: AbortSignal): Promise<StasisReached[]> {
|
|
232
234
|
if (behind || active.size >= MAX_ACTIVE_BUILDS) throw new BundleBuildError(429, 'build-busy')
|
|
233
235
|
const context = { repoId: request.input.repoId, github: request.github, commit: request.input.commit, config: request.input.config, branch: request.input.branch, lockfileOnly: true }
|
|
234
236
|
const asked = { ...request, instanceDir: INSTANCE_DIR, lockfileOnly: true }
|
|
@@ -236,7 +238,7 @@ export async function buildRepositoryLockfile(db: BundleBuildLeaseStore, request
|
|
|
236
238
|
let gone = () => {}
|
|
237
239
|
behind = { yield: () => yielding.abort(new BundleBuildError(429, 'build-yielded')), gone: new Promise(resolve => { gone = resolve }) }
|
|
238
240
|
try {
|
|
239
|
-
return await withBundleBuildLease(db, LOCKFILE_BUILDER, AbortSignal.any([signal, yielding.signal]), leased => runBuild<{ dependencies:
|
|
241
|
+
return await withBundleBuildLease(db, LOCKFILE_BUILDER, AbortSignal.any([signal, yielding.signal]), leased => runBuild<{ dependencies: StasisReached[] }, StasisReached[]>(
|
|
240
242
|
LOCKFILE_BUILDER, asked, context, leased, ({ dependencies }) => ({ value: dependencies, logged: { dependencies: dependencies.length } }), true))
|
|
241
243
|
} finally {
|
|
242
244
|
behind = null
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// The lines a package's files change (npm-delta.ts), off the event loop, paired as Compare pairs them (rename-diff.ts
|
|
2
2
|
// comparedFiles); and each file's fewest (`best`): the least of the diffs Compare draws of it (drawn), one per setting of
|
|
3
|
-
// its toggles the comparison has (`proves` types, `prints` code
|
|
3
|
+
// its toggles the comparison has (`proves` types, `prints` code, JSON folded where it offers that: foldsOffered), those
|
|
4
|
+
// needing proofs or copies smallest first.
|
|
4
5
|
import { parentPort, workerData } from 'node:worker_threads'
|
|
5
6
|
import { treeLayout } from '@preventive/compare'
|
|
6
7
|
import { renamedDiff } from '@preventive/compare/names.js'
|
|
7
8
|
import { hidesMixedWhitespace } from '@preventive/compare/view.js'
|
|
8
9
|
import { computeBundleDiff } from '../common/bundle-compare-diff.js'
|
|
9
10
|
import { bundlePkgOf } from '../common/bundle-pkg-of.js'
|
|
10
|
-
import { blocksHold, changeBlocks, diffSize, isLargeDiff, markedCounts, textLines, typeFolds, withSharedBlankLines } from '../common/line-diff.js'
|
|
11
|
+
import { blocksHold, changeBlocks, copiesRuns, diffSize, isLargeDiff, markedCounts, textLines, typeFolds, withSharedBlankLines } from '../common/line-diff.js'
|
|
12
|
+
import { comparisonFolds, offersFold } from '../common/pretty-json.js'
|
|
11
13
|
import { viewerKind, viewerPrintable } from '../common/pretty-kinds.js'
|
|
12
14
|
import { prettyExtension, printableSize } from '../common/pretty-print.js'
|
|
13
15
|
import { whitespaceInsensitive } from '../common/whitespace-insensitive.js'
|
|
@@ -49,7 +51,7 @@ async function copyOf(at, path, text, layout) {
|
|
|
49
51
|
if (kind) return viewerPrintable(kind, text) ? { text: kind.copy(text, layout), blank: [] } : null
|
|
50
52
|
if (!prints || prettyExtension(path) === null || !printableSize(text)) return null
|
|
51
53
|
const known = kept.get(`${at}\0${path}`)
|
|
52
|
-
const copy = known ?? await prettyFormat(text, prettyExtension(path))
|
|
54
|
+
const copy = known ?? await prettyFormat(text, prettyExtension(path), path)
|
|
53
55
|
if ('error' in copy) return null
|
|
54
56
|
if (!known) made.push([at, path, copy])
|
|
55
57
|
return copy
|
|
@@ -69,21 +71,66 @@ async function printingOf(file, layout, ws) {
|
|
|
69
71
|
const copy = ([at, path, text]) => once(file, `copy\0${layout}\0${at}`, () => copyOf(at, path, text, layout))
|
|
70
72
|
const a = await copy(sides[0]), b = a && await copy(sides[1])
|
|
71
73
|
if (!a || !b) return null
|
|
72
|
-
const shown = file.generated ? { before: a.text, after: b.text } : withSharedBlankLines(a.text, b.text, a.blank, b.blank)
|
|
73
|
-
if (viewed || unreadable) return { ...shown, kind: viewed && kinds[0]
|
|
74
|
+
const shown = { ...file.generated ? { before: a.text, after: b.text } : withSharedBlankLines(a.text, b.text, a.blank, b.blank), copies: [a.text, b.text], runs: copiesRuns(a, b) }
|
|
75
|
+
if (viewed || unreadable) return { ...shown, kind: viewed && kinds[0] }
|
|
74
76
|
if (isLargeDiff(file.before, file.after) || isLargeDiff(shown.before, shown.after)) return null
|
|
75
77
|
const mode = ws && 'leading', now = diffSize(shown.before, shown.after, mode), was = diffSize(file.before, file.after, mode)
|
|
76
78
|
return now.lines < was.lines || (now.lines === was.lines && now.marks < was.marks) ? shown : null
|
|
77
79
|
}
|
|
78
80
|
|
|
81
|
+
// A JSON file's two sides' copies in both its layouts, `[indented, folded]` each, '' both for a side it lacks, as
|
|
82
|
+
// Compare's _folds reads them; null where a side isn't JSON or can't be printed.
|
|
83
|
+
async function layoutsOf(file) {
|
|
84
|
+
const sides = []
|
|
85
|
+
for (const [at, path, text] of [[0, file.basePath, file.before], [1, file.path, file.after]]) {
|
|
86
|
+
if (text !== '' && !viewerKind(path)?.layouts) return null
|
|
87
|
+
const pair = []
|
|
88
|
+
for (const layout of ['indented', 'folded']) {
|
|
89
|
+
const copy = await once(file, `copy\0${layout}\0${at}`, () => copyOf(at, path, text, layout))
|
|
90
|
+
if (!copy) return null
|
|
91
|
+
pair.push(copy.text)
|
|
92
|
+
}
|
|
93
|
+
sides.push(pair)
|
|
94
|
+
}
|
|
95
|
+
return sides
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Whether Compare's Diff view offers JSON folded, whitespace hidden (`ws`) or not: where any JSON file but a
|
|
99
|
+
// package.json folds enough (comparisonFolds), counted as Compare's _folds counts; its folded setting then draws every
|
|
100
|
+
// JSON file folded, a package.json too. Those smallest first, and none once past `prettyUntil`, as the count then ends.
|
|
101
|
+
const offered = new Map()
|
|
102
|
+
function foldsOffered(ws) {
|
|
103
|
+
if (!offered.has(ws)) {
|
|
104
|
+
offered.set(ws, (async () => {
|
|
105
|
+
for (const file of ordered) {
|
|
106
|
+
if (Date.now() >= prettyUntil) return false
|
|
107
|
+
if (!file.layouts || !offersFold(file.path) || !offersFold(file.basePath)) continue
|
|
108
|
+
const mode = ws && file.insensitive && 'leading', sides = await layoutsOf(file)
|
|
109
|
+
if (sides && comparisonFolds(sides, (before, after) => (isLargeDiff(before, after) ? null : diffSize(before, after, mode).lines))) return true
|
|
110
|
+
}
|
|
111
|
+
return false
|
|
112
|
+
})())
|
|
113
|
+
}
|
|
114
|
+
return offered.get(ws)
|
|
115
|
+
}
|
|
116
|
+
|
|
79
117
|
// The diff Compare draws of a file with its toggles so (_fileState): whitespace hidden (`ws`), type-only changes
|
|
80
|
-
// (`types`), renamed names (`renames`), printed in `layout` or not (null); null where it draws none
|
|
118
|
+
// (`types`), renamed names (`renames`), printed in `layout` or not (null); null where it draws none, or the same as
|
|
119
|
+
// another setting: folded where the comparison doesn't offer it (foldsOffered), drawn indented.
|
|
81
120
|
async function drawn(file, { ws, types, layout, renames }) {
|
|
121
|
+
if (layout === 'folded' && !await foldsOffered(ws)) return null
|
|
82
122
|
const proof = types ? (file.stamped || once(file, 'runs', () => typeOnlyRuns(file.from, file.to))) : null
|
|
83
123
|
if (proof === true) return NONE
|
|
84
124
|
const folds = proof && once(file, 'folds', () => typeFolds(file.before, file.after, proof))
|
|
85
|
-
if (folds) return diffed(file, 'folds', folds.before, folds.after, ws)
|
|
86
125
|
const printing = layout && await once(file, `printing\0${layout}\0${ws}`, () => printingOf(file, layout, ws))
|
|
126
|
+
if (folds) {
|
|
127
|
+
// As _fileState folds it: by its copies' own lines that run where they kept them, none apart its types alone
|
|
128
|
+
// changed, drawn as printed where their types read alike; else as published.
|
|
129
|
+
const printed = printing?.runs ? once(file, `printed folds\0${layout}`, () => typeFolds(...printing.copies, printing.runs)) : undefined
|
|
130
|
+
if (printed === false) return NONE
|
|
131
|
+
if (printed) return diffed(file, `printed folds\0${layout}`, printed.before, printed.after, ws)
|
|
132
|
+
if (printed === undefined) return diffed(file, 'folds', folds.before, folds.after, ws)
|
|
133
|
+
}
|
|
87
134
|
if (!printing) return diffed(file, 'texts', file.before, file.after, ws)
|
|
88
135
|
if (file.generated) {
|
|
89
136
|
const diff = await once(file, `renamed\0${layout}`, () => renamedDiff(printing.before, printing.after,
|
|
@@ -126,8 +173,9 @@ for (const { path, basePath = path, side } of [...files.changed, ...files.onlyBa
|
|
|
126
173
|
const count = complete => ({ ...total, best, complete })
|
|
127
174
|
tell({ sofar: count(false) })
|
|
128
175
|
|
|
176
|
+
const ordered = later.toSorted((a, b) => a.size - b.size)
|
|
129
177
|
let complete = true
|
|
130
|
-
for (const file of
|
|
178
|
+
for (const file of ordered) {
|
|
131
179
|
if (Date.now() >= prettyUntil) { complete = false; break }
|
|
132
180
|
let now = file.fewest
|
|
133
181
|
for (const setting of settingsOf(file)) {
|
|
@@ -38,7 +38,7 @@ const PRETTY_MARGIN_MS = 15_000
|
|
|
38
38
|
// The text the two sides may hold together, past which none is counted.
|
|
39
39
|
const MAX_TEXT_LENGTH = 64 * 1024 * 1024
|
|
40
40
|
// Bumped when what a delta counts changes; its keys carry it and PRETTY_VERSION, so none counted otherwise is read.
|
|
41
|
-
const DELTA_VERSION =
|
|
41
|
+
const DELTA_VERSION = 5
|
|
42
42
|
const COUNTING = `v${DELTA_VERSION}.${PRETTY_VERSION}`
|
|
43
43
|
const COPIES_AT_ONCE = 8
|
|
44
44
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export const PRETTY_VERSION: number
|
|
2
|
-
export
|
|
3
|
-
export
|
|
2
|
+
export function copyName(path: string, extension: string): string
|
|
3
|
+
export interface PrettyCopy { text: string; blank: number[]; runs?: [number, number][] }
|
|
4
|
+
export function prettyFormat(text: string, extension: string, path?: string): Promise<PrettyCopy | { error: 'unsupported' | 'unformattable' | 'pretty-mismatch' }>
|
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
// A file pretty-printed as Compare draws it, with its copy's layout blank lines: the server's
|
|
2
2
|
// copies (pretty-print.ts) and a delta's (npm-delta-worker.js) alike.
|
|
3
|
+
import { createHash } from 'node:crypto'
|
|
3
4
|
import { format, layoutBlankLines } from '@preventive/compare/pretty.js'
|
|
5
|
+
import { declarationRuns, runsName } from './type-only-proof.js'
|
|
4
6
|
|
|
5
7
|
// Part of every kept copy's name; bumped when what the formatter writes for
|
|
6
8
|
// the same file changes, or what its check lets through, so copies written
|
|
7
9
|
// before are no longer read.
|
|
8
|
-
export const PRETTY_VERSION =
|
|
10
|
+
export const PRETTY_VERSION = 8
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
// A copy's name but for its file's hash: a declaration file's kept apart by
|
|
13
|
+
// its name, which its lines that run depend on too (runsName), hashed, as a
|
|
14
|
+
// cache key takes no `_` (validateCacheKey) and a file's name is bounded.
|
|
15
|
+
export function copyName(path, extension) {
|
|
16
|
+
const name = runsName(path)
|
|
17
|
+
if (name === null) return extension
|
|
18
|
+
return name ? `d.${createHash('sha256').update(name).digest('hex').slice(0, 32)}.${extension}` : `d.${extension}`
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// A declaration file's copy keeps its lines that run, as the type-only proof finds a file's, for Compare to fold it by.
|
|
22
|
+
export async function prettyFormat(text, extension, path = '') {
|
|
11
23
|
const result = await format(text, extension)
|
|
12
|
-
|
|
24
|
+
if ('error' in result) return result
|
|
25
|
+
const runs = declarationRuns(path, result.code)
|
|
26
|
+
return { text: result.code, blank: await layoutBlankLines(result.code, extension), ...runs ? { runs } : {} }
|
|
13
27
|
}
|
|
@@ -16,7 +16,7 @@ import { computeFileHash } from '@preventive/report'
|
|
|
16
16
|
import { FILE_HASH, computeBundleFileHashes } from '../common/bundle-metadata.js'
|
|
17
17
|
import { bundleCompareFiles, bundleCompareSideScopes } from '../common/bundle-compare-inputs.js'
|
|
18
18
|
import { bundlePackageDirs, bundleSourcesAsMap } from '../common/bundle-sources.js'
|
|
19
|
-
import { MAX_PRETTY_BYTES, layoutBlankList, needsPrettyPrint, prettyExtension } from '../common/pretty-print.js'
|
|
19
|
+
import { MAX_PRETTY_BYTES, layoutBlankList, needsPrettyPrint, prettyExtension, runRangeList } from '../common/pretty-print.js'
|
|
20
20
|
import { type OpenedBlob, readBytes } from './blob-store.ts'
|
|
21
21
|
import { type BundleCacheStorage, readSizedBundleDetails } from './bundle-cache.ts'
|
|
22
22
|
import type { BundleStore } from './bundle-store.ts'
|
|
@@ -27,7 +27,7 @@ import { keptBriefly } from './kept-briefly.ts'
|
|
|
27
27
|
import { loadNpmFiles } from './npm-loads.ts'
|
|
28
28
|
import { type NpmVersionDocument, npmFileText, pathOrder } from './npm-packages.ts'
|
|
29
29
|
import { inTurns } from './queue.ts'
|
|
30
|
-
import { PRETTY_VERSION, type PrettyCopy, prettyFormat } from './pretty-format.js'
|
|
30
|
+
import { PRETTY_VERSION, type PrettyCopy, copyName, prettyFormat } from './pretty-format.js'
|
|
31
31
|
|
|
32
32
|
// Formats running at once per process, each holding its file's syntax tree,
|
|
33
33
|
// and those waiting their turn past them (a version's files are formatted one
|
|
@@ -58,8 +58,9 @@ export interface PrettyRequest { path: string; hash: string; extension: string }
|
|
|
58
58
|
|
|
59
59
|
// One of a version's files pretty-printed (npmAll), named by `source`, the
|
|
60
60
|
// hash of the file it was formatted from: its copy's text and layout blank
|
|
61
|
-
// lines
|
|
62
|
-
|
|
61
|
+
// lines, and a declaration file's its lines that run (prettyCopy), or why it
|
|
62
|
+
// has none, as a PrettyError tells it.
|
|
63
|
+
export type PrettyLine = ({ path: string; source: string } & PrettyCopy) | { path: string; source: string; error: string; status: number }
|
|
63
64
|
|
|
64
65
|
// Which of a comparison's two its files are of: the bundle or version asked
|
|
65
66
|
// about, or the `other`.
|
|
@@ -91,26 +92,27 @@ export function prettyRequest(params: URLSearchParams): PrettyRequest | null {
|
|
|
91
92
|
return extension !== null && FILE_HASH.test(hash) ? { path, hash, extension } : null
|
|
92
93
|
}
|
|
93
94
|
|
|
94
|
-
// A copy's name: its file's hash in hex, with the extension it was parsed by
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
// A copy's name: its file's hash in hex, with the extension it was parsed by,
|
|
96
|
+
// or a declaration file's name (copyName).
|
|
97
|
+
export const prettyFile = ({ path, hash, extension }: PrettyRequest) =>
|
|
98
|
+
`pretty-v${PRETTY_VERSION}/${Buffer.from(hash.slice('sha512-'.length), 'base64').toString('hex')}.${copyName(path, extension)}.br`
|
|
97
99
|
|
|
98
100
|
let activeFormats = 0
|
|
99
101
|
const waitingFormats: Array<() => void> = []
|
|
100
102
|
|
|
101
103
|
// The text formatted, where @preventive/compare's checks find formatting
|
|
102
|
-
// changed only its layout, its copy `{ text, blank }` (its layout blank
|
|
103
|
-
// lines' numbers) as JSON, Brotli-encoded:
|
|
104
|
+
// changed only its layout, its copy `{ text, blank, runs? }` (its layout blank
|
|
105
|
+
// lines' numbers, and a declaration file's lines that run) as JSON, Brotli-encoded:
|
|
104
106
|
// refused as `unsupported` where it has no check for the kind of file,
|
|
105
107
|
// `unformattable` or `pretty-mismatch`. Its blank lines are read in the
|
|
106
108
|
// same turn, as they parse and check the copy again.
|
|
107
|
-
export async function prettyBody(text: string, extension: string): Promise<Buffer> {
|
|
109
|
+
export async function prettyBody(text: string, extension: string, path = ''): Promise<Buffer> {
|
|
108
110
|
if (Buffer.byteLength(text) > MAX_PRETTY_BYTES) throw new PrettyError(413, 'file-too-large')
|
|
109
111
|
if (activeFormats < MAX_ACTIVE_FORMATS) activeFormats++
|
|
110
112
|
else if (waitingFormats.length >= MAX_WAITING_FORMATS) throw new PrettyError(429, 'pretty-busy')
|
|
111
113
|
else await new Promise<void>(resolve => { waitingFormats.push(resolve) })
|
|
112
114
|
let result
|
|
113
|
-
try { result = await prettyFormat(text, extension) }
|
|
115
|
+
try { result = await prettyFormat(text, extension, path) }
|
|
114
116
|
finally {
|
|
115
117
|
const next = waitingFormats.shift()
|
|
116
118
|
if (next) next()
|
|
@@ -142,10 +144,10 @@ const decompress = promisify(brotliDecompress)
|
|
|
142
144
|
// A pretty-printed copy's text and layout blank lines, from its Brotli-encoded
|
|
143
145
|
// bytes (prettyBody).
|
|
144
146
|
export async function prettyCopy(copy: OpenedBlob): Promise<PrettyCopy> {
|
|
145
|
-
const { text, blank } = JSON.parse((await decompress(await readBytes(copy))).toString()) as { text: unknown; blank: unknown }
|
|
146
|
-
const numbers = layoutBlankList(blank)
|
|
147
|
+
const { text, blank, runs } = JSON.parse((await decompress(await readBytes(copy))).toString()) as { text: unknown; blank: unknown; runs: unknown }
|
|
148
|
+
const numbers = layoutBlankList(blank), ranges = runRangeList(runs)
|
|
147
149
|
if (typeof text !== 'string' || !numbers) throw new Error('managed: a kept pretty-printed copy is not one')
|
|
148
|
-
return { text, blank: numbers }
|
|
150
|
+
return { text, blank: numbers, ...ranges ? { runs: ranges } : {} }
|
|
149
151
|
}
|
|
150
152
|
|
|
151
153
|
// A pretty-printed copy's text alone, as a rename diff reads it.
|
|
@@ -344,7 +346,7 @@ export function createPrettyCache(bundles: BundleCacheStorage, npm: CacheStorage
|
|
|
344
346
|
function bundleCopy(record: ManagedBundle, request: PrettyRequest, read: () => Promise<string>): Promise<Buffer> {
|
|
345
347
|
const file = prettyFile(request)
|
|
346
348
|
return shared(`bundle ${record.id} ${file}`, async () => {
|
|
347
|
-
const body = await prettyBody(await read(), request.extension)
|
|
349
|
+
const body = await prettyBody(await read(), request.extension, request.path)
|
|
348
350
|
// As for its metadata (bundle-cache.ts): a bundle deleted meanwhile
|
|
349
351
|
// keeps no copy, nor does one deleted while it was written.
|
|
350
352
|
if (!await db.getBundle(record.id)) throw new PrettyError(404, 'no-bundle')
|
|
@@ -369,7 +371,7 @@ export function createPrettyCache(bundles: BundleCacheStorage, npm: CacheStorage
|
|
|
369
371
|
async function npmMade(doc: NpmVersionDocument, request: PrettyRequest, read: () => Promise<string>): Promise<OpenedBlob> {
|
|
370
372
|
const file = prettyFile(request)
|
|
371
373
|
return opened(await shared(doc.private ? `npm ${doc.dist.integrity} ${file}` : `npm ${file}`, async () => {
|
|
372
|
-
const body = await prettyBody(await read(), request.extension)
|
|
374
|
+
const body = await prettyBody(await read(), request.extension, request.path)
|
|
373
375
|
if (!doc.private) await keep(() => npm.put(file, body))
|
|
374
376
|
return body
|
|
375
377
|
}))
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// stasisConfigs): what the build worker tells of the lockfile it builds
|
|
4
4
|
// (bundle-build-worker.js buildStasisLockfile), kept by the repository, the
|
|
5
5
|
// commit and the file's path, which never change, in this instance and in
|
|
6
|
-
// the database, the versions alone
|
|
6
|
+
// the database, the versions alone (the files of each kept apart, filesKeyOf).
|
|
7
|
+
// A build takes minutes, so none is waited
|
|
7
8
|
// for: one not kept is told as pending (read), and its answer asks for it
|
|
8
9
|
// once its review is known (build), with a priority: how many versions the
|
|
9
10
|
// lockfiles beside it add that want reviews (wantingBeside). Those asked for
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
// so), is kept too, for it to tell what it reached while one of a later
|
|
17
18
|
// commit is still to be built (last): one read as the head later alone
|
|
18
19
|
// replaces it, whenever each is built.
|
|
19
|
-
import { BundleBuildError, type LockfileInput, type StasisDependency } from './bundle-build.ts'
|
|
20
|
+
import { BundleBuildError, type LockfileInput, type StasisDependency, type StasisReached } from './bundle-build.ts'
|
|
20
21
|
import { directoryOf, installsBundle, lockedVersions } from './bundle-dependency-reviews.ts'
|
|
21
22
|
import type { CommitLockfiles } from './commit-lockfiles.ts'
|
|
22
23
|
import type { LockedPackage } from './locked-packages.ts'
|
|
@@ -73,7 +74,7 @@ export interface StasisDependencyReader {
|
|
|
73
74
|
interface ReaderOptions {
|
|
74
75
|
store: KeptStore<unknown>
|
|
75
76
|
// Builds one, as the deployment's builds are admitted (http.ts stasisReader).
|
|
76
|
-
build(input: LockfileInput, github: string, token: string, signal: AbortSignal): Promise<
|
|
77
|
+
build(input: LockfileInput, github: string, token: string, signal: AbortSignal): Promise<StasisReached[]>
|
|
77
78
|
// Work going on behind an answer, handed to its owner (http.ts trackBehind).
|
|
78
79
|
behind(work: Promise<unknown>): void
|
|
79
80
|
stopping(): boolean
|
|
@@ -84,6 +85,10 @@ type Head = { branch: string; at: number }
|
|
|
84
85
|
type Job = { key: string; input: LockfileInput; github: string; token: string; priority: number; asked: number; head: Head | null }
|
|
85
86
|
const later = (one: Head | null, other: Head | null) => !one || (other && other.at > one.at) ? other : one
|
|
86
87
|
const keyOf = (repoId: number, commit: string, config: string) => `${repoId}\0${STASIS_DEPENDENCIES_READ}\0${commit}\0${config}`
|
|
88
|
+
// Where its versions are kept with the files of each the build reaches
|
|
89
|
+
// (StasisReached), in the database alone, apart from them as none of their
|
|
90
|
+
// reads need them; none of those built before they were kept.
|
|
91
|
+
export const filesKeyOf = (repoId: number, commit: string, config: string) => `${repoId}\0${STASIS_DEPENDENCIES_READ}\0files\0${commit}\0${config}`
|
|
87
92
|
// Where the commit it was last built of at a branch's head is kept.
|
|
88
93
|
const lastKeyOf = (repoId: number, branch: string, config: string) => `${repoId}\0${STASIS_DEPENDENCIES_READ}\0last\0${branch}\0${config}`
|
|
89
94
|
const optional = (value: unknown) => value === undefined || typeof value === 'string'
|
|
@@ -119,8 +124,12 @@ export function stasisDependencyReader({ store, build, behind, stopping }: Reade
|
|
|
119
124
|
const watching = setInterval(() => { if (stopping()) stop.abort() }, STOPPING_EVERY_MS)
|
|
120
125
|
watching.unref()
|
|
121
126
|
try {
|
|
122
|
-
const
|
|
123
|
-
|
|
127
|
+
const reached = await build(job.input, job.github, job.token, stop.signal)
|
|
128
|
+
const at = Date.now()
|
|
129
|
+
const dependencies = reached.map(({ files: _files, ...one }) => one)
|
|
130
|
+
// Its files first: where its versions are kept, so are they.
|
|
131
|
+
await store.write(filesKeyOf(job.input.repoId, job.input.commit, job.input.config), reached, at)
|
|
132
|
+
await store.write(job.key, dependencies, at)
|
|
124
133
|
hold(job.key, { status: 'ready', dependencies })
|
|
125
134
|
if (job.head) await lasts.keep({ ...job.input, branch: job.head.branch }, job.head.at)
|
|
126
135
|
} catch (err) {
|
|
@@ -54,10 +54,14 @@ const tree = file => (file ? [{ name: file.path, data: file.text }] : [])
|
|
|
54
54
|
// would end the comment within it, and what follows would run, so no other
|
|
55
55
|
// character than Latin letters, digits, `-`, `_` and `.` is taken in one.
|
|
56
56
|
const PLAIN_NAME = /^[A-Za-z0-9._-]+$/u
|
|
57
|
+
const plainName = path => {
|
|
58
|
+
const name = path.slice(path.lastIndexOf('/') + 1)
|
|
59
|
+
return PLAIN_NAME.test(name) ? name : null
|
|
60
|
+
}
|
|
57
61
|
function withoutOwnMap(file) {
|
|
58
62
|
if (file === null) return null
|
|
59
|
-
const name =
|
|
60
|
-
if (
|
|
63
|
+
const name = plainName(file.path)
|
|
64
|
+
if (name === null) return file
|
|
61
65
|
const comment = `//# sourceMappingURL=${name}.map`
|
|
62
66
|
for (const last of [comment, `${comment}\n`]) {
|
|
63
67
|
const before = file.text.endsWith(last) ? file.text.slice(0, -last.length) : null
|
|
@@ -142,6 +146,22 @@ function runLines(text) {
|
|
|
142
146
|
// line reads the same on both.
|
|
143
147
|
const moved = (one, other) => [...one.leading].some(comment => !other.leading.has(comment) && other.below.has(comment))
|
|
144
148
|
|
|
149
|
+
// A declaration file's lines that run, as the proof tells them, but for its own
|
|
150
|
+
// map comment; null for any other file, or where they can't be told.
|
|
151
|
+
export function declarationRuns(path, text) {
|
|
152
|
+
if (!DECLARATION.test(path)) return null
|
|
153
|
+
try {
|
|
154
|
+
return runLines(withoutOwnMap({ path, text }).text).lines
|
|
155
|
+
} catch {
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// What a file's lines that run depend on besides its text: a declaration
|
|
161
|
+
// file's name, which its own map comment names, or '' where it is no plain
|
|
162
|
+
// name (withoutOwnMap); null for any other file.
|
|
163
|
+
export const runsName = path => (DECLARATION.test(path) ? plainName(path) ?? '' : null)
|
|
164
|
+
|
|
145
165
|
// `{ runs, otherRuns }` where each side there is a declaration file proven to
|
|
146
166
|
// hold types alone but for what runs, and no directive steers on one side
|
|
147
167
|
// alone unseen (`moved`); else false, and where the proof throws, as no
|