@preventive/triage 1.0.0-alpha.66 → 1.0.0-alpha.68
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/bundle-imports.js +20 -0
- package/common/bundle-metadata.js +1 -1
- package/common/bundle-reasons.d.ts +4 -0
- package/common/bundle-reasons.js +38 -1
- package/common/bundle-sources.d.ts +1 -0
- package/common/bundle-sources.js +22 -0
- package/common/managed/npm-packages.js +3 -3
- package/common/managed/routes.js +37 -18
- package/common/managed/stasis-config.ts +7 -0
- package/out/activity.js +4 -4
- package/out/{chunk-J2FWBXFV.js → chunk-2F3CYNPZ.js} +1 -1
- package/out/chunk-2HGKTQ5Q.js +52 -0
- package/out/chunk-4RS3NDOG.js +1 -0
- package/out/chunk-CGRIZ5M5.js +1 -0
- package/out/{chunk-Z6VEV2QL.js → chunk-D37M7J4B.js} +1 -1
- package/out/chunk-GZ46SSTF.js +1 -0
- package/out/{chunk-IZ6MYRYB.js → chunk-HETCVNO4.js} +1 -1
- package/out/chunk-MPXVKVPF.js +3 -0
- package/out/{chunk-24AKGEH3.js → chunk-NB4JBTKK.js} +1 -1
- package/out/{chunk-OVGR4D3M.js → chunk-OAWY4T6R.js} +1 -1
- package/out/chunk-QPGM3DTI.js +42 -0
- package/out/chunk-SCN2TBP2.js +247 -0
- package/out/chunk-VG5RH6KN.js +2 -0
- package/out/chunk-WFDXHPT2.js +12 -0
- package/out/client-managed-import.js +2 -2
- package/out/client-managed.js +19 -19
- package/out/compare.js +12 -12
- package/out/dialogs.js +38 -38
- package/out/graph.js +8 -8
- package/out/index.html +9 -9
- package/out/scan.js +2 -2
- package/out/view.css +1 -1
- package/out/view.js +15 -15
- package/package.json +11 -8
- package/server-managed/activity.ts +21 -12
- package/server-managed/bundle-build-diagnostics.d.ts +1 -1
- package/server-managed/bundle-build-worker.js +140 -19
- package/server-managed/bundle-build.ts +102 -21
- package/server-managed/bundle-cache.ts +28 -6
- package/server-managed/bundle-dependency-reviews.ts +38 -22
- package/server-managed/commit-lockfiles.ts +2 -1
- package/server-managed/db-neon.ts +5 -2
- package/server-managed/dependency-review-comments.ts +38 -16
- package/server-managed/dependency-reviews.ts +17 -14
- package/server-managed/http.ts +178 -70
- package/server-managed/locked-packages.ts +34 -14
- package/server-managed/lockfile-scope.ts +10 -1
- package/server-managed/npm-kept.ts +10 -0
- package/server-managed/npm-patch.ts +188 -53
- package/server-managed/public-workspace.ts +3 -2
- package/server-managed/pull-dependencies.ts +58 -37
- package/server-managed/pull-dependency-reviews.ts +35 -22
- package/server-managed/report-sources.ts +6 -3
- package/server-managed/repository-dependency-reviews.ts +4 -2
- package/server-managed/repository-issues.ts +14 -11
- package/server-managed/stasis-dependencies.ts +170 -0
- package/out/chunk-65PLAG3I.js +0 -12
- package/out/chunk-DCBXCBLL.js +0 -52
- package/out/chunk-F3UI65ZI.js +0 -42
- package/out/chunk-FAG6E5RC.js +0 -3
- package/out/chunk-OBZWBTZL.js +0 -247
- package/out/chunk-PTFQKLKO.js +0 -1
- package/out/chunk-QVKFY4FA.js +0 -1
- package/out/chunk-SYXT3SFP.js +0 -2
- package/out/chunk-WL3SPQB4.js +0 -1
- /package/out/{chunk-WCPJDAYA.js → chunk-D6NVKGGG.js} +0 -0
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { createHash } from 'node:crypto'
|
|
25
25
|
import { plainObject } from './npm-manifest.ts'
|
|
26
26
|
import { type NpmPublishTimeStore, type NpmVersionStore, VERSION_KEPT_MS } from './npm-packages.ts'
|
|
27
|
+
import type { KeptPatch } from './npm-patch.ts'
|
|
27
28
|
import type { UpstreamCacheStore } from './upstream-cache.ts'
|
|
28
29
|
|
|
29
30
|
// A value kept by key, as JSON, `at` when it was.
|
|
@@ -228,7 +229,16 @@ export const reviewBaselineStore = (db: UpstreamCacheStore, debug = false): Kept
|
|
|
228
229
|
// what their reviews are held to.
|
|
229
230
|
export const lockfileReadStore = <T>(db: UpstreamCacheStore, type: 'listings' | 'lockfiles', debug = false): KeptStore<T> =>
|
|
230
231
|
keptStore<T>(db, `repository/review-${type}`, HISTORY_PAGE_KEPT_MS, debug)
|
|
232
|
+
// The pnpm patches a repository's dependency reviews read (npm-patch.ts), by
|
|
233
|
+
// the repository and each's hash, a sha256, which names its text wherever
|
|
234
|
+
// and whenever it is read: kept for good.
|
|
235
|
+
export const patchStore = (db: UpstreamCacheStore, debug = false): KeptStore<KeptPatch> => keptStore<KeptPatch>(db, 'repository/patches', Infinity, debug)
|
|
231
236
|
// A bundle's validations (bundle-validation.ts), by the bundle, where it is,
|
|
232
237
|
// what its reader could check and the rules they were made by: kept a month
|
|
233
238
|
// after they were made, as what they check never changes.
|
|
234
239
|
export const bundleValidationStore = <T>(db: UpstreamCacheStore, debug = false): KeptStore<T> => keptStore<T>(db, 'bundles/validations', HISTORY_PAGE_KEPT_MS, debug)
|
|
240
|
+
// What a repository's stasis.config.json has Stasis reach at a commit
|
|
241
|
+
// (stasis-dependencies.ts): the versions, never the lockfile, by the
|
|
242
|
+
// repository, the commit and the file's path, kept as its history's pages are.
|
|
243
|
+
export const stasisDependencyStore = (db: UpstreamCacheStore, debug = false): KeptStore<unknown> =>
|
|
244
|
+
keptStore<unknown>(db, 'repository/stasis-dependencies', HISTORY_PAGE_KEPT_MS, debug)
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// A pnpm patch of an npm package version (patchedDependencies), as its
|
|
2
2
|
// dependency review reads it (pull-dependency-reviews.ts): its file, which a
|
|
3
|
-
// lockfile names (locked-packages.ts `patchPath`),
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
3
|
+
// lockfile names (locked-packages.ts `patchPath`), or which is found where it
|
|
4
|
+
// names it by its hash alone (withPatchFiles), or by its hash among a
|
|
5
|
+
// commit's patches (foundPatch), read of a repository at a commit, and held
|
|
6
|
+
// to the hash the lockfile names it by, as pnpm 10 and later hash it
|
|
7
|
+
// (hashOfPatch; one pnpm 9 names by an md5 isn't read), by which it is kept
|
|
8
|
+
// for good, read of GitHub once; and applied to the version's files as pnpm applies
|
|
9
|
+
// it as it installs it (patch-package's applyPatch, which pnpm 10 and 11
|
|
8
10
|
// bundle): each file it deletes, creates, renames or changes, a change's
|
|
9
11
|
// hunks each found in the file as it was, where its header says, else as
|
|
10
12
|
// many as FUZZ lines either side, its lines the same but for the space they
|
|
@@ -14,54 +16,60 @@
|
|
|
14
16
|
// file's mode is left out, as the files it reads have none.
|
|
15
17
|
import { Buffer } from 'node:buffer'
|
|
16
18
|
import { createHash } from 'node:crypto'
|
|
19
|
+
import { YamlError, parseYaml } from '@preventive/lockfile/yaml.js'
|
|
20
|
+
import { isGithubRepoName } from '../common/github-pr.ts'
|
|
17
21
|
import { isPatchHash, isPatchPath } from '../common/managed/npm-packages.js'
|
|
18
|
-
import {
|
|
22
|
+
import { GithubApiError } from './github-errors.ts'
|
|
23
|
+
import { isStasisConfig } from '../common/managed/stasis-config.ts'
|
|
24
|
+
import { SHA, isId } from './github-values.ts'
|
|
25
|
+
import { type LockedPackage, pathFrom } from './locked-packages.ts'
|
|
19
26
|
import { type Lru, lru } from './lru.ts'
|
|
27
|
+
import type { KeptStore } from './npm-kept.ts'
|
|
28
|
+
import { plainObject } from './npm-manifest.ts'
|
|
20
29
|
import { NpmPackageError, type NpmPackageFile, npmFileText, pathOrder } from './npm-packages.ts'
|
|
21
|
-
import { type GithubAsked, repositoryFile } from './pull-dependencies.ts'
|
|
30
|
+
import { type GithubAsked, type PullDependencies, lockfileFormat, repositoryFile } from './pull-dependencies.ts'
|
|
31
|
+
import { eachOf } from './queue.ts'
|
|
22
32
|
|
|
23
33
|
// Where a patch is read: its file in a repository (by its id) at a commit,
|
|
24
|
-
// from the repository's root; and the patch, by its hash besides.
|
|
34
|
+
// from the repository's root; and the patch, by its hash besides. A request
|
|
35
|
+
// names its repository by its id, or by its name (`owner/name`) in its place
|
|
36
|
+
// (http.ts wholeRepositoryAccess), and may leave its path out, the patch
|
|
37
|
+
// looked for there (foundPatch); or the patch by its hash alone, which only
|
|
38
|
+
// one kept answers (keptPatch).
|
|
25
39
|
export interface PatchSource { repoId: number; commit: string; path: string }
|
|
26
40
|
export interface NpmPatch extends PatchSource { hash: string }
|
|
41
|
+
export type RequestedPatch = (Omit<NpmPatch, 'repoId' | 'path'> & { repoId: number | string; path?: string }) | { hash: string }
|
|
27
42
|
|
|
28
43
|
// The most a patch's file may hold, and how many lines either side of where a
|
|
29
|
-
// hunk says its lines are they are looked for
|
|
44
|
+
// hunk says its lines are they are looked for; and how many patches an
|
|
45
|
+
// answer looks for, those past them left for the next, and how many at once.
|
|
30
46
|
const MAX_PATCH_BYTES = 8 * 1024 * 1024
|
|
31
47
|
const FUZZ = 20
|
|
32
|
-
|
|
48
|
+
const MAX_FOUND = 100
|
|
49
|
+
const FIND_AT_ONCE = 4
|
|
33
50
|
|
|
34
51
|
// The patch a request names, by its hash (`hash`), its repository's id
|
|
35
|
-
// (`repoId`, a number or its digits), the commit and its file's
|
|
36
|
-
//
|
|
37
|
-
// otherwise.
|
|
38
|
-
export function requestedPatch({ hash, repoId, commit, path }: { hash: unknown; repoId
|
|
39
|
-
if ([repoId, commit, path].every(field => field == null)) return null
|
|
52
|
+
// (`repoId`, a number or its digits) or name, the commit and its file's
|
|
53
|
+
// path or none, or by its hash alone; null where it names none, and
|
|
54
|
+
// 'invalid' where it names one otherwise.
|
|
55
|
+
export function requestedPatch({ hash, repoId, commit, path }: { hash: unknown; repoId?: unknown; commit?: unknown; path?: unknown }): RequestedPatch | null | 'invalid' {
|
|
56
|
+
if ([repoId, commit, path].every(field => field == null)) return hash == null ? null : isPatchHash(hash) ? { hash } : 'invalid'
|
|
40
57
|
const id = typeof repoId === 'string' && /^[1-9]\d{0,15}$/u.test(repoId) ? Number(repoId) : repoId
|
|
41
|
-
if (!isPatchHash(hash) || !
|
|
42
|
-
|| !isPatchPath(path)) return 'invalid'
|
|
43
|
-
return { hash, repoId: id
|
|
58
|
+
if (!isPatchHash(hash) || !(isId(id) || typeof id === 'string' && isGithubRepoName(id)) || typeof commit !== 'string' || !SHA.test(commit)
|
|
59
|
+
|| path != null && !isPatchPath(path)) return 'invalid'
|
|
60
|
+
return { hash, repoId: id, commit, ...typeof path === 'string' ? { path } : {} }
|
|
44
61
|
}
|
|
45
62
|
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
for (const byte of bytes) {
|
|
52
|
-
value = ((value << 8) | byte) & 0xfff
|
|
53
|
-
bits += 8
|
|
54
|
-
for (; bits >= 5; bits -= 5) spelled += BASE32[(value >>> (bits - 5)) & 31]
|
|
55
|
-
}
|
|
56
|
-
return bits > 0 ? spelled + BASE32[(value << (5 - bits)) & 31] : spelled
|
|
57
|
-
}
|
|
63
|
+
// The hash pnpm names a patch's text by, a sha256 in hex of it with its
|
|
64
|
+
// CRLFs as LFs, as pnpm 10 and later read a patch's file (the CRs of lines
|
|
65
|
+
// it ends otherwise kept); and whether `text` is the patch `hash` names.
|
|
66
|
+
export const hashOfPatch = (text: string) => createHash('sha256').update(text.replaceAll('\r\n', '\n')).digest('hex')
|
|
67
|
+
export const isPatchOf = (text: string, hash: string) => hashOfPatch(text) === hash
|
|
58
68
|
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
? createHash('sha256').update(read).digest('hex') === hash : base32(createHash('md5').update(read).digest()) === hash)
|
|
64
|
-
}
|
|
69
|
+
// The text of a patch's file in repository `fullName` at `commit`, read with
|
|
70
|
+
// `token`; null where GitHub has no text of it, or it is past MAX_PATCH_BYTES.
|
|
71
|
+
const patchFileAt = (fullName: string, token: string, path: string, commit: string) => repositoryFile(fullName, token, path, commit).then(text => text !== null
|
|
72
|
+
&& Buffer.byteLength(text) > MAX_PATCH_BYTES ? null : text, (err: unknown) => { if (err instanceof GithubApiError && err.message === 'github-malformed') return null; throw err })
|
|
65
73
|
|
|
66
74
|
// A change a patch makes to a file at `path` of the package: deleting it,
|
|
67
75
|
// creating it, renaming it from another (`from`), and its hunks, as
|
|
@@ -293,25 +301,152 @@ const held = (lines: string[]) => lines.reduce((sum, line) => sum + LINE_COST +
|
|
|
293
301
|
export const patchWeight = (files: PatchedFile[]) => files.reduce((sum, file) => sum + HUNK_COST + file.path.length + (file.from?.length ?? 0)
|
|
294
302
|
+ file.hunks.reduce((bytes, hunk) => bytes + HUNK_COST + hunk.parts.reduce((parts, part) => parts + held(part.lines), 0), 0), 1)
|
|
295
303
|
|
|
296
|
-
//
|
|
297
|
-
//
|
|
304
|
+
// What is kept of a patch, for good, by its repository and hash (patchKey):
|
|
305
|
+
// where it was read, and its text, which its hash names wherever it is.
|
|
306
|
+
// Kept by its repository too, as only one who reads all of that is given it.
|
|
307
|
+
export interface KeptPatch { commit: string; path: string; text: string }
|
|
308
|
+
export const patchKey = (repoId: number, hash: string) => `${repoId}\0${hash}`
|
|
309
|
+
|
|
310
|
+
// Patches read lately, with where each was read, by their keys, as many as
|
|
311
|
+
// MAX_KEPT_PATCHES of the bytes they take (patchWeight); and the path each
|
|
312
|
+
// found or kept is at (`located`): '' by its key and a commit for one not
|
|
313
|
+
// found there, lest each telling look again, none for one not looked for;
|
|
314
|
+
// and '' by its repository's key and a commit whose patches were all kept.
|
|
298
315
|
const MAX_KEPT_PATCHES = 64 * 1024 * 1024
|
|
299
|
-
|
|
316
|
+
export interface ReadPatch { source: PatchSource; files: PatchedFile[] }
|
|
317
|
+
const patches: Lru<ReadPatch> = lru(MAX_KEPT_PATCHES, read => patchWeight(read.files))
|
|
318
|
+
const located: Lru<string> = lru(100_000)
|
|
300
319
|
|
|
301
|
-
// The patch `patch` names,
|
|
302
|
-
//
|
|
303
|
-
//
|
|
304
|
-
//
|
|
305
|
-
|
|
306
|
-
|
|
320
|
+
// The patch `patch` names, and where it was read (`source`): as kept by its
|
|
321
|
+
// repository and hash, in this instance, else beyond it (`kept`, held to its
|
|
322
|
+
// hash again), wherever the request says it is; else read of repository
|
|
323
|
+
// `fullName` with `token` at the commit and path it names, held to its
|
|
324
|
+
// hash, and kept for good; `asked` told where GitHub was. Its file missing
|
|
325
|
+
// there, or other than its hash says, or past MAX_PATCH_BYTES, fails, as
|
|
326
|
+
// does one that isn't read as git writes a diff (parsePatch).
|
|
327
|
+
export async function readPatch(fullName: string, token: string, patch: NpmPatch, kept: KeptStore<KeptPatch>, asked?: GithubAsked): Promise<ReadPatch> {
|
|
328
|
+
const key = patchKey(patch.repoId, patch.hash)
|
|
307
329
|
const known = patches.get(key)
|
|
308
330
|
if (known) return known
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
331
|
+
let stored = (await kept.read(key))?.value
|
|
332
|
+
if (typeof stored?.text !== 'string' || !isPatchOf(stored.text, patch.hash)) {
|
|
333
|
+
if (asked) asked.github = true
|
|
334
|
+
const text = await repositoryFile(fullName, token, patch.path, patch.commit)
|
|
335
|
+
if (text === null) throw new NpmPackageError(404, 'patch-not-found')
|
|
336
|
+
if (Buffer.byteLength(text) > MAX_PATCH_BYTES) throw new NpmPackageError(413, 'patch-too-large')
|
|
337
|
+
if (!isPatchOf(text, patch.hash)) throw new NpmPackageError(409, 'patch-mismatch')
|
|
338
|
+
stored = { commit: patch.commit, path: patch.path, text }
|
|
339
|
+
await kept.write(key, stored, Date.now())
|
|
340
|
+
}
|
|
341
|
+
const read = { source: { repoId: patch.repoId, commit: stored.commit, path: stored.path }, files: parsePatch(stored.text) }
|
|
342
|
+
patches.set(key, read)
|
|
343
|
+
return read
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// The patch `hash` names as kept, of the first of repositories `repoIds`
|
|
347
|
+
// that keeps it, in this instance, else beyond it (`kept`, held to its hash
|
|
348
|
+
// again), GitHub asked for none of it; null where none does.
|
|
349
|
+
export async function keptPatch(repoIds: number[], hash: string, kept: KeptStore<KeptPatch>): Promise<ReadPatch | null> {
|
|
350
|
+
const keys = repoIds.map(repoId => patchKey(repoId, hash))
|
|
351
|
+
const known = keys.map(key => patches.get(key)).find(Boolean)
|
|
352
|
+
if (known) return known
|
|
353
|
+
const stored = keys.length > 0 ? await kept.readMany(keys) : new Map()
|
|
354
|
+
for (const [at, key] of keys.entries()) {
|
|
355
|
+
const patch = stored.get(key)?.value
|
|
356
|
+
if (typeof patch?.text !== 'string' || !isPatchOf(patch.text, hash)) continue
|
|
357
|
+
const read = { source: { repoId: repoIds[at]!, commit: patch.commit, path: patch.path }, files: parsePatch(patch.text) }
|
|
358
|
+
patches.set(key, read)
|
|
359
|
+
return read
|
|
360
|
+
}
|
|
361
|
+
return null
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// The patch `patch` names by its hash at a commit of its repository, its
|
|
365
|
+
// file unnamed: as kept by its repository and hash (keptPatch); else among
|
|
366
|
+
// the patches of the commit's files (`listed` lists them, with their bytes;
|
|
367
|
+
// null where its repository isn't looked in), each `.patch` file in a
|
|
368
|
+
// `patches` directory beside a pnpm lockfile or a stasis.config.json, where
|
|
369
|
+
// pnpm patch-commit writes them, as many as MAX_FOUND, read of repository
|
|
370
|
+
// `fullName` with `token` and each kept for good by its own hash, so a
|
|
371
|
+
// commit is looked in once; `asked` told where GitHub was. One none of them
|
|
372
|
+
// is fails as not found.
|
|
373
|
+
export async function foundPatch(fullName: string, token: string, patch: Omit<NpmPatch, 'path'>, kept: KeptStore<KeptPatch>,
|
|
374
|
+
listed: () => Promise<{ files: string[]; sizes: number[] } | null>, asked?: GithubAsked): Promise<ReadPatch> {
|
|
375
|
+
const ofIt = () => keptPatch([patch.repoId], patch.hash, kept), searched = `${patchKey(patch.repoId, '')}\0${patch.commit}`
|
|
376
|
+
let found = await ofIt()
|
|
377
|
+
const tree = found || located.get(searched) !== undefined ? null : await listed()
|
|
378
|
+
if (tree) {
|
|
379
|
+
if (asked) asked.github = true
|
|
380
|
+
const dirOf = (path: string) => path.slice(0, path.lastIndexOf('/') + 1)
|
|
381
|
+
const beside = new Set(tree.files.filter(path => lockfileFormat(path) === 'pnpm' || isStasisConfig(path)).map(path => `${dirOf(path)}patches/`))
|
|
382
|
+
const paths = tree.files.filter((path, at) => path.endsWith('.patch') && beside.has(dirOf(path)) && (tree.sizes[at] ?? 0) <= MAX_PATCH_BYTES)
|
|
383
|
+
await eachOf(paths.slice(0, MAX_FOUND), FIND_AT_ONCE, path => patchFileAt(fullName, token, path, patch.commit)
|
|
384
|
+
.then(text => text === null ? undefined : kept.write(patchKey(patch.repoId, hashOfPatch(text)), { commit: patch.commit, path, text }, Date.now())))
|
|
385
|
+
located.set(searched, '')
|
|
386
|
+
found = await ofIt()
|
|
387
|
+
}
|
|
388
|
+
if (!found) throw new NpmPackageError(404, 'patch-not-found')
|
|
389
|
+
return found
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// The files a pnpm-workspace.yaml (`workspace`, its text, null for none)
|
|
393
|
+
// names its patches by, as pnpm 11 and later read their patchedDependencies
|
|
394
|
+
// there alone, by a version or a package; none where its reader refuses it.
|
|
395
|
+
function patchedBy(workspace: string | null): Record<string, unknown> {
|
|
396
|
+
let settings: unknown
|
|
397
|
+
try { settings = workspace === null ? null : parseYaml(workspace, { workflow: true }) } catch (err) {
|
|
398
|
+
if (!(err instanceof YamlError)) throw err
|
|
399
|
+
}
|
|
400
|
+
const patched = plainObject(settings) ? settings['patchedDependencies'] : null
|
|
401
|
+
return plainObject(patched) ? patched : {}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// A version pnpm patches by a patch named by a sha256, with its lockfile's
|
|
405
|
+
// directory: one its lockfile names by its hash alone (pnpm 11's and later),
|
|
406
|
+
// which wants its file found; and, `keepNamed`, one whose file it names.
|
|
407
|
+
type Patched = LockedPackage & { patchHash: string; patchDir: string; version: string }
|
|
408
|
+
const patchedBy256 = (pkg: LockedPackage, keepNamed = false): pkg is Patched => (pkg.patchPath === undefined || keepNamed) && pkg.patchDir !== undefined
|
|
409
|
+
&& pkg.version !== null && isPatchHash(pkg.patchHash)
|
|
410
|
+
|
|
411
|
+
// `told`, each version of its that pnpm patches by a patch its lockfile names
|
|
412
|
+
// by its hash alone with its patch's file (`patchPath`), as a lockfile naming
|
|
413
|
+
// one tells it: as kept by its repository and hash, else the file the
|
|
414
|
+
// pnpm-workspace.yaml beside its lockfile names of it (patchedBy: its
|
|
415
|
+
// version's, else its package's) at `told.head`, held to its hash and kept
|
|
416
|
+
// for good, so each is found once, as many an answer as MAX_FOUND; one not
|
|
417
|
+
// found left with none. With `keepNamed`, as a default branch's are told,
|
|
418
|
+
// each patch whose file a lockfile names is read there and kept as well, so
|
|
419
|
+
// that a link naming it by its hash alone finds it later (keptPatch).
|
|
420
|
+
// `asked` told where GitHub was. A file GitHub has no text of, or past
|
|
421
|
+
// MAX_PATCH_BYTES, is none; any other failure of GitHub's is the whole
|
|
422
|
+
// answer's, as a lockfile's is.
|
|
423
|
+
export async function withPatchFiles<T extends PullDependencies>(told: T, repoId: number, fullName: string, token: string, kept: KeptStore<KeptPatch>,
|
|
424
|
+
asked?: GithubAsked, { keepNamed = false } = {}): Promise<T> {
|
|
425
|
+
const wanted = new Map<string, Patched>()
|
|
426
|
+
for (const dir of told.lockfiles) {
|
|
427
|
+
for (const pkg of 'error' in dir ? [] : [...dir.added, ...dir.locked]) if (patchedBy256(pkg, keepNamed)) wanted.set(patchKey(repoId, pkg.patchHash), pkg)
|
|
428
|
+
}
|
|
429
|
+
if (wanted.size === 0) return told
|
|
430
|
+
const at = (key: string) => located.get(key) ?? located.get(`${key}\0${told.head}`)
|
|
431
|
+
const unknown = [...wanted.keys()].filter(key => at(key) === undefined)
|
|
432
|
+
for (const [key, { value }] of unknown.length > 0 ? await kept.readMany(unknown) : []) if (typeof value?.path === 'string') located.set(key, value.path)
|
|
433
|
+
const files = new Map<string, Promise<string | null>>(), read = (path: string) => files.get(path) ?? files.set(path, patchFileAt(fullName, token, path, told.head)).get(path)!
|
|
434
|
+
const workspaces = new Map<string, Promise<Record<string, unknown>>>()
|
|
435
|
+
const namedIn = (dir: string) => workspaces.get(dir) ?? workspaces.set(dir, read(pathFrom(dir, 'pnpm-workspace.yaml')!).then(patchedBy)).get(dir)!
|
|
436
|
+
await eachOf(unknown.filter(key => at(key) === undefined).slice(0, MAX_FOUND), FIND_AT_ONCE, async key => {
|
|
437
|
+
const pkg = wanted.get(key)!
|
|
438
|
+
if (asked) asked.github = true
|
|
439
|
+
const patched = pkg.patchPath === undefined ? await namedIn(pkg.patchDir) : {}
|
|
440
|
+
const file = pkg.patchPath ?? patched[`${pkg.name}@${pkg.version}`] ?? patched[pkg.name]
|
|
441
|
+
const path = typeof file === 'string' ? pkg.patchPath ?? pathFrom(pkg.patchDir, file) : null
|
|
442
|
+
const text = path === null ? null : await read(path)
|
|
443
|
+
if (path === null || text === null || !isPatchOf(text, pkg.patchHash)) { located.set(`${key}\0${told.head}`, ''); return }
|
|
444
|
+
await kept.write(key, { commit: told.head, path, text }, Date.now())
|
|
445
|
+
located.set(key, path)
|
|
446
|
+
})
|
|
447
|
+
const named = <P extends LockedPackage>(pkg: P): P => {
|
|
448
|
+
const patchPath = patchedBy256(pkg) && at(patchKey(repoId, pkg.patchHash))
|
|
449
|
+
return patchPath ? { ...pkg, patchPath } : pkg
|
|
450
|
+
}
|
|
451
|
+
return { ...told, lockfiles: told.lockfiles.map(dir => 'error' in dir ? dir : { ...dir, added: dir.added.map(named), locked: dir.locked.map(named) }) }
|
|
317
452
|
}
|
|
@@ -202,10 +202,11 @@ async function serveBundle(res: ServerResponse, deps: ManagedHttpDeps, bundle: M
|
|
|
202
202
|
if (!deps.bundleCache) { json(res, 503, { error: 'unavailable' }); return }
|
|
203
203
|
if (part === 'advisories') {
|
|
204
204
|
if (bundle.kind !== 'stasis') { json(res, 422, { error: 'unsupported-bundle' }); return }
|
|
205
|
-
const
|
|
205
|
+
const entry = url.searchParams.get('entry') ?? ''
|
|
206
|
+
const inventory = await deps.bundleCache.advisoryInventory(bundle, url.searchParams.get('reason') ?? '', entry)
|
|
206
207
|
await recheck()
|
|
207
208
|
if (inventory === null) { json(res, 413, { error: 'payload-too-large' }); return }
|
|
208
|
-
if (inventory === undefined) { json(res, 400, { error: 'unknown-reason' }); return }
|
|
209
|
+
if (inventory === undefined) { json(res, 400, { error: entry ? 'unknown-entry' : 'unknown-reason' }); return }
|
|
209
210
|
if (Buffer.byteLength(JSON.stringify(inventory)) > MAX_PACKAGE_INVENTORY_BYTES) { json(res, 413, { error: 'payload-too-large' }); return }
|
|
210
211
|
const signal = AbortSignal.timeout(ADVISORIES_TIMEOUT_MS)
|
|
211
212
|
const repoAdvisories = url.searchParams.get('repoAdvisories') === 'true'
|
|
@@ -29,9 +29,11 @@
|
|
|
29
29
|
import { createClient } from '@preventive/upstream/github.js'
|
|
30
30
|
import { GithubApiError, upstreamGithub } from './github-errors.ts'
|
|
31
31
|
import { USER_AGENT, githubJson, repoApi } from './github-requests.ts'
|
|
32
|
+
import { branchName } from './bundle-build.ts'
|
|
32
33
|
import { SHA } from './github-values.ts'
|
|
33
34
|
import { compareVersions, isExactVersion } from '@preventive/upstream/semver.js'
|
|
34
35
|
import { type LockedPackage, isTestFixture, lockedPackages, patchedIn } from './locked-packages.ts'
|
|
36
|
+
import { isStasisConfig } from './lockfile-scope.ts'
|
|
35
37
|
import { type Lru, lru } from './lru.ts'
|
|
36
38
|
import { eachOf } from './queue.ts'
|
|
37
39
|
import { type ChangedFile, commitChanges } from './repository-changes.ts'
|
|
@@ -53,8 +55,9 @@ const READ_PARALLEL = 4
|
|
|
53
55
|
// The rules what is told was told by, kept with it (http.ts
|
|
54
56
|
// pullDependencyKept), and the lockfiles read for it (lockfileKept): one
|
|
55
57
|
// told or read by others is told or read again, as those read before pnpm's
|
|
56
|
-
// patches' files were
|
|
57
|
-
|
|
58
|
+
// patches' files were, and those told before their lockfiles' directories,
|
|
59
|
+
// or read with the files of patches pnpm 9 names by an md5.
|
|
60
|
+
export const PULL_DEPENDENCIES_READ = 5
|
|
58
61
|
|
|
59
62
|
type Format = 'npm' | 'pnpm' | 'yarn' | 'bun'
|
|
60
63
|
|
|
@@ -73,11 +76,11 @@ export function lockfileFormat(path: string): Format | null {
|
|
|
73
76
|
return name === 'yarn.lock' ? 'yarn' : null
|
|
74
77
|
}
|
|
75
78
|
|
|
76
|
-
// Whether a pull request changing `paths` changes a lockfile read here
|
|
77
|
-
//
|
|
78
|
-
// (`changed`), which it may yet.
|
|
79
|
+
// Whether a pull request changing `paths` changes a lockfile read here, or
|
|
80
|
+
// a stasis.config.json, which a review may hold to: null where none of them
|
|
81
|
+
// is one but they are fewer than it changes (`changed`), which it may yet.
|
|
79
82
|
export function touchesLockfile(paths: string[], changed: number): boolean | null {
|
|
80
|
-
if (paths.some(path => lockfileFormat(path) !== null)) return true
|
|
83
|
+
if (paths.some(path => lockfileFormat(path) !== null || isStasisConfig(path))) return true
|
|
81
84
|
return changed > paths.length ? null : false
|
|
82
85
|
}
|
|
83
86
|
|
|
@@ -93,6 +96,8 @@ export type AddedPackage = LockedPackage & { was?: string[] }
|
|
|
93
96
|
// `moreLocked`); or why they couldn't be told, a lockfile's reader refusing one
|
|
94
97
|
// (`lockfile-unreadable`, with what it said), one of a format not read
|
|
95
98
|
// (`lockfile-unsupported`, Bun's) or GitHub having none at a commit compared.
|
|
99
|
+
// A stasis.config.json a review holds to is told as one too, by its own path
|
|
100
|
+
// (stasis-dependencies.ts stasisDirectory), or as `stasis-pending` or `stasis-failed`.
|
|
96
101
|
export type DirectoryDependencies = { paths: string[] } & ({ added: AddedPackage[]; more: number; locked: LockedPackage[]; moreLocked: number }
|
|
97
102
|
| { error: string; detail?: string })
|
|
98
103
|
// What a pull request's lockfiles add: the commits compared (`base`, its
|
|
@@ -141,15 +146,26 @@ const readKey = (ref: string, { path, format, sha }: { path: string; format: For
|
|
|
141
146
|
|
|
142
147
|
// The pull request's base's and head's commits, as GitHub has them now: its
|
|
143
148
|
// base's moving on as it does, its head's with each push, both kept once it
|
|
144
|
-
// is merged or closed.
|
|
145
|
-
|
|
149
|
+
// is merged or closed. With the branches they are of, where told: its base's
|
|
150
|
+
// (`baseRef`), whose history holds its merge base, and its head's
|
|
151
|
+
// (`headRef`) where that is the repository's own, not a fork's; and that an
|
|
152
|
+
// open one's head is a fork's (`fork`), a commit of none of the repository's
|
|
153
|
+
// branches till it is merged, which Stasis builds nothing of.
|
|
154
|
+
export interface PullRefs { baseRef?: string; headRef?: string; fork?: true }
|
|
155
|
+
export interface PullCommits extends PullRefs { base: string; head: string }
|
|
156
|
+
// Those of `base` and `head` that are a branch's name, and `fork` where it is told.
|
|
157
|
+
export const pullRefs = (base: unknown, head: unknown, fork?: unknown): PullRefs =>
|
|
158
|
+
({ ...branchName(base) ? { baseRef: base } : {}, ...branchName(head) ? { headRef: head } : {}, ...fork === true ? { fork } : {} })
|
|
146
159
|
async function pullCommits(fullName: string, token: string, number: number, fetchImpl: typeof fetch): Promise<PullCommits> {
|
|
147
160
|
const pull = await githubJson(`${repoApi(fullName)}/pulls/${number}`, token, fetchImpl) as
|
|
148
|
-
{ number?: unknown; base?: { sha?: unknown;
|
|
161
|
+
{ number?: unknown; state?: unknown; base?: { sha?: unknown; ref?: unknown; repo?: { full_name?: unknown } | null }
|
|
162
|
+
head?: { sha?: unknown; ref?: unknown; repo?: { full_name?: unknown } | null } } | null
|
|
149
163
|
const base = pull?.base?.sha, head = pull?.head?.sha, repo = pull?.base?.repo?.full_name
|
|
150
164
|
if (pull?.number !== number || typeof base !== 'string' || !SHA.test(base) || typeof head !== 'string' || !SHA.test(head)
|
|
151
165
|
|| typeof repo !== 'string' || repo.toLowerCase() !== fullName.toLowerCase()) throw new GithubApiError(502, 'github-malformed')
|
|
152
|
-
|
|
166
|
+
const from = pull.head?.repo?.full_name
|
|
167
|
+
const own = typeof from === 'string' && from.toLowerCase() === repo.toLowerCase()
|
|
168
|
+
return { base, head, ...pullRefs(pull.base?.ref, own ? pull.head?.ref : undefined, pull.state === 'open' && !own) }
|
|
153
169
|
}
|
|
154
170
|
|
|
155
171
|
// The pull request's merge base and the files it changes from it: as GitHub
|
|
@@ -263,38 +279,42 @@ const byName = (a: AddedPackage, b: AddedPackage) => a.name < b.name ? -1 : a.na
|
|
|
263
279
|
export const lockedAs = (pkg: LockedPackage) => `${pkg.source}\0${pkg.name}@${pkg.version ?? ''}\0${pkg.from ?? ''}\0${pkg.patchHash ?? ''}`
|
|
264
280
|
export const lockedWith = (pkg: LockedPackage) => `${lockedAs(pkg)}\0${pkg.integrity ?? ''}\0${pkg.shasum ?? ''}\0${pkg.lockfile ?? ''}`
|
|
265
281
|
|
|
266
|
-
// What a directory's lockfiles add, from each read before and after
|
|
267
|
-
//
|
|
268
|
-
//
|
|
269
|
-
// (`more`), not told but reviewed as they are; and every one after, each
|
|
270
|
-
// integrity it is locked with (`locked`), as many as MAX_LOCKED, each patch's
|
|
271
|
-
// file from the repository's root (patchedIn); or the first failure among
|
|
272
|
-
// them.
|
|
282
|
+
// What a directory's lockfiles add, from each read before and after
|
|
283
|
+
// (addedBetween), each patch's file from the repository's root, and its
|
|
284
|
+
// lockfile's directory (patchedIn); or the first failure among them.
|
|
273
285
|
function addedIn(paths: string[], before: LockfileRead[], after: LockfileRead[]): DirectoryDependencies {
|
|
274
286
|
const failed = [...before, ...after].find(read => 'error' in read)
|
|
275
287
|
if (failed) return { paths, error: failed.error, ...failed.detail ? { detail: failed.detail } : {} }
|
|
288
|
+
const packages = (reads: LockfileRead[]) => reads.flatMap(read => 'packages' in read ? read.packages : [])
|
|
289
|
+
return addedBetween(paths, packages(before), packages(after).map(pkg => patchedIn(paths[0]!, pkg)))
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// What the versions locked `after` add to those locked `before`, as a
|
|
293
|
+
// directory's lockfiles' at `paths`: the package versions after none before
|
|
294
|
+
// has, each once, by name and version, each with the versions of its name
|
|
295
|
+
// before, those past MAX_ADDED counted (`more`), not told but reviewed as
|
|
296
|
+
// they are; and every one after, each integrity it is locked with
|
|
297
|
+
// (`locked`), as many as MAX_LOCKED.
|
|
298
|
+
export function addedBetween(paths: string[], before: LockedPackage[], after: LockedPackage[]): DirectoryDependencies {
|
|
276
299
|
const known = new Set<string>()
|
|
277
300
|
const versions = new Map<string, Set<string>>()
|
|
278
|
-
for (const pkg of before
|
|
301
|
+
for (const pkg of before) {
|
|
279
302
|
known.add(lockedAs(pkg))
|
|
280
303
|
const of = versions.get(pkg.name) ?? versions.set(pkg.name, new Set()).get(pkg.name)!
|
|
281
304
|
if (pkg.version) of.add(pkg.version)
|
|
282
305
|
}
|
|
283
306
|
const added = new Map<string, AddedPackage>()
|
|
284
307
|
const locked = new Map<string, LockedPackage>()
|
|
285
|
-
for (const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (!had) added.set(key, { ...pkg, ...was ? { was: [...was].toSorted(byVersion) } : {} })
|
|
296
|
-
else if (had.dev && !pkg.dev) delete had.dev
|
|
297
|
-
}
|
|
308
|
+
for (const pkg of after) {
|
|
309
|
+
const key = lockedAs(pkg)
|
|
310
|
+
const held = locked.get(lockedWith(pkg))
|
|
311
|
+
if (!held) locked.set(lockedWith(pkg), { ...pkg })
|
|
312
|
+
else if (held.dev && !pkg.dev) delete held.dev
|
|
313
|
+
if (known.has(key)) continue
|
|
314
|
+
const had = added.get(key)
|
|
315
|
+
const was = versions.get(pkg.name)
|
|
316
|
+
if (!had) added.set(key, { ...pkg, ...was ? { was: [...was].toSorted(byVersion) } : {} })
|
|
317
|
+
else if (had.dev && !pkg.dev) delete had.dev
|
|
298
318
|
}
|
|
299
319
|
const all = [...added.values()].toSorted(byName)
|
|
300
320
|
return { paths, added: all.slice(0, MAX_LOCKED), more: Math.max(0, all.length - MAX_ADDED),
|
|
@@ -311,14 +331,15 @@ export async function pullDependencies(fullName: string, token: string, number:
|
|
|
311
331
|
// The same, with the commits it is kept by (`commits`), its base's and
|
|
312
332
|
// head's, by which `kept` reads it: those `given`, as a page of its
|
|
313
333
|
// repository's open pull requests read them (repository-issues.ts), else as
|
|
314
|
-
// GitHub has them now
|
|
315
|
-
// longer of are read as
|
|
334
|
+
// GitHub has them now; and the branches they are of, where told (PullRefs).
|
|
335
|
+
// Commits given that its own list of its files is no longer of are read as
|
|
336
|
+
// GitHub has them now.
|
|
316
337
|
export async function pullDependenciesAt(fullName: string, token: string, number: number, kept: PullDependencyStore,
|
|
317
|
-
fetchImpl: typeof fetch = globalThis.fetch, given?: PullCommits): Promise<{ commits: string; told: PullDependencies }> {
|
|
318
|
-
const { base, head } = given ?? await pullCommits(fullName, token, number, fetchImpl)
|
|
338
|
+
fetchImpl: typeof fetch = globalThis.fetch, given?: PullCommits): Promise<PullRefs & { commits: string; told: PullDependencies }> {
|
|
339
|
+
const { base, head, ...refs } = given ?? await pullCommits(fullName, token, number, fetchImpl)
|
|
319
340
|
const key = `${base}...${head}`
|
|
320
341
|
const held = await kept.read(key)
|
|
321
|
-
if (held) return { commits: key, told: held }
|
|
342
|
+
if (held) return { ...refs, commits: key, told: held }
|
|
322
343
|
const current = async () => {
|
|
323
344
|
const now = given && await pullCommits(fullName, token, number, fetchImpl)
|
|
324
345
|
return !now || (now.base === base && now.head === head)
|
|
@@ -336,5 +357,5 @@ export async function pullDependenciesAt(fullName: string, token: string, number
|
|
|
336
357
|
})
|
|
337
358
|
const told = { base: mergeBase, head, lockfiles, cut: filesCut || cut, ...cut && !filesCut && omitted.length <= MAX_OMITTED ? { omitted } : {} }
|
|
338
359
|
await kept.write(key, told)
|
|
339
|
-
return { commits: key, told }
|
|
360
|
+
return { ...refs, commits: key, told }
|
|
340
361
|
}
|