@preventive/triage 1.0.0-alpha.76 → 1.0.0-alpha.78

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.
Files changed (64) hide show
  1. package/common/code-language.js +3 -2
  2. package/common/line-diff.d.ts +2 -0
  3. package/common/line-diff.js +54 -0
  4. package/common/managed/dependency-routes.js +25 -0
  5. package/common/managed/routes.js +9 -12
  6. package/common/pretty-json.js +95 -0
  7. package/common/pretty-print.d.ts +1 -0
  8. package/common/pretty-print.js +6 -0
  9. package/common/whitespace-insensitive.js +19 -0
  10. package/out/activity.js +2 -2
  11. package/out/{chunk-OCE6KQGJ.js → chunk-26W3E5JE.js} +1 -1
  12. package/out/chunk-3S4YWYOM.js +1 -0
  13. package/out/{chunk-OQS2USZM.js → chunk-55YIWY3H.js} +1 -1
  14. package/out/{chunk-IVDE66N5.js → chunk-6VXEPL32.js} +1 -1
  15. package/out/{chunk-XVKDPU4D.js → chunk-6Y6PZ2BU.js} +1 -1
  16. package/out/{chunk-HLMQ7QYS.js → chunk-ATT2QA77.js} +1 -1
  17. package/out/{chunk-2V6Q4ZOZ.js → chunk-B64CTMVU.js} +1 -1
  18. package/out/{chunk-EXQQ5VQJ.js → chunk-BEKYZCPX.js} +1 -1
  19. package/out/{chunk-EYTPAODY.js → chunk-BGLN56X6.js} +1 -1
  20. package/out/{chunk-S2ZIKK45.js → chunk-EEJA7JBI.js} +1 -1
  21. package/out/{chunk-O6FA2R5G.js → chunk-EMNIV37E.js} +1 -1
  22. package/out/{chunk-4IW3VD3H.js → chunk-FSU2PLJL.js} +1 -1
  23. package/out/{chunk-MFAB7P3T.js → chunk-I4YPK32I.js} +1 -1
  24. package/out/{chunk-I6UQQKAH.js → chunk-LVSLM7HB.js} +1 -1
  25. package/out/{chunk-T6EXDRB7.js → chunk-MKX6JODK.js} +1 -1
  26. package/out/{chunk-24LZZYAP.js → chunk-NCJQDKPK.js} +1 -1
  27. package/out/chunk-O2NYWLX3.js +259 -0
  28. package/out/{chunk-62QT2RXR.js → chunk-OWIBDEJJ.js} +1 -1
  29. package/out/{chunk-NEFCZAIY.js → chunk-PKLVXUEI.js} +1 -1
  30. package/out/chunk-QBIUW5EF.js +2 -0
  31. package/out/{chunk-ADMVRLNP.js → chunk-QD3IBSF4.js} +1 -1
  32. package/out/{chunk-GSNSP3EF.js → chunk-RPNO56H2.js} +1 -1
  33. package/out/{chunk-QBFT2DDR.js → chunk-UR4WYMIP.js} +1 -1
  34. package/out/{chunk-UH6NZBSU.js → chunk-VHEXSGGP.js} +1 -1
  35. package/out/{chunk-RHOQDAU4.js → chunk-WB3NRZL7.js} +1 -1
  36. package/out/chunk-XLX6XXJF.js +1 -0
  37. package/out/{chunk-BXQCCW7F.js → chunk-XM4RLC4A.js} +1 -1
  38. package/out/client-managed-import.js +1 -1
  39. package/out/client-managed.js +2 -2
  40. package/out/compare.js +14 -14
  41. package/out/dialogs.js +22 -22
  42. package/out/graph.js +1 -1
  43. package/out/index.html +11 -9
  44. package/out/prism.js +1 -1
  45. package/out/scan.js +2 -2
  46. package/out/terminal.js +7 -7
  47. package/out/view.css +1 -1
  48. package/out/view.js +8 -8
  49. package/package.json +8 -2
  50. package/server-managed/dependency-review-comments.ts +35 -27
  51. package/server-managed/http.ts +13 -11
  52. package/server-managed/npm-delta-worker.js +58 -12
  53. package/server-managed/npm-delta.ts +52 -28
  54. package/server-managed/pretty-copies.ts +22 -0
  55. package/server-managed/pretty-format.d.ts +3 -0
  56. package/server-managed/pretty-format.js +13 -0
  57. package/server-managed/pretty-print.ts +34 -24
  58. package/server-managed/public-workspace.ts +1 -1
  59. package/server-managed/pull-dependency-reviews.ts +23 -12
  60. package/out/chunk-JCIIBLKR.js +0 -1
  61. package/out/chunk-UZC7POI4.js +0 -255
  62. package/out/chunk-W5PRH5D7.js +0 -2
  63. package/out/chunk-WGQ7D5DB.js +0 -1
  64. /package/out/{chunk-Z667U2UJ.js → chunk-SZWPYLPC.js} +0 -0
@@ -2,7 +2,9 @@
2
2
  // one file at a time, GET /api/npm/pretty all a version's at once), and the
3
3
  // files that changed between two bundles or versions for Compare (the same
4
4
  // with `other`, or `otherVersion`), named by path and content hash, formatted
5
- // with oxfmt once and kept Brotli-encoded by that hash: a bundle's in its
5
+ // with oxfmt once, with the copy's blank lines that are its layout alone
6
+ // (@preventive/compare layoutBlankLines), which Compare leaves out where one
7
+ // side alone has them, and kept Brotli-encoded by that hash: a bundle's in its
6
8
  // cache directory (so only made from, and read by readers of, that bundle), a
7
9
  // public npm version's under `cache/npm/`; a private version's are never
8
10
  // kept. Access is the caller's to check.
@@ -10,12 +12,11 @@ import { Buffer } from 'node:buffer'
10
12
  import { Readable } from 'node:stream'
11
13
  import { promisify } from 'node:util'
12
14
  import { brotliDecompress } from 'node:zlib'
13
- import { format } from '@preventive/compare/pretty.js'
14
15
  import { computeFileHash } from '@preventive/report'
15
16
  import { FILE_HASH, computeBundleFileHashes } from '../common/bundle-metadata.js'
16
17
  import { bundleCompareFiles, bundleCompareSideScopes } from '../common/bundle-compare-inputs.js'
17
18
  import { bundlePackageDirs, bundleSourcesAsMap } from '../common/bundle-sources.js'
18
- import { MAX_PRETTY_BYTES, needsPrettyPrint, prettyExtension } from '../common/pretty-print.js'
19
+ import { MAX_PRETTY_BYTES, layoutBlankList, needsPrettyPrint, prettyExtension } from '../common/pretty-print.js'
19
20
  import { type OpenedBlob, readBytes } from './blob-store.ts'
20
21
  import { type BundleCacheStorage, readSizedBundleDetails } from './bundle-cache.ts'
21
22
  import type { BundleStore } from './bundle-store.ts'
@@ -26,11 +27,8 @@ import { keptBriefly } from './kept-briefly.ts'
26
27
  import { loadNpmFiles } from './npm-loads.ts'
27
28
  import { type NpmVersionDocument, npmFileText, pathOrder } from './npm-packages.ts'
28
29
  import { inTurns } from './queue.ts'
30
+ import { PRETTY_VERSION, type PrettyCopy, prettyFormat } from './pretty-format.js'
29
31
 
30
- // Part of every kept copy's name; bumped when what the formatter writes for
31
- // the same file changes, or what its check lets through, so copies written
32
- // before are no longer read.
33
- const PRETTY_VERSION = 4
34
32
  // Formats running at once per process, each holding its file's syntax tree,
35
33
  // and those waiting their turn past them (a version's files are formatted one
36
34
  // after another, beside other readers'), any more refused.
@@ -59,9 +57,9 @@ export class PrettyError extends Error {
59
57
  export interface PrettyRequest { path: string; hash: string; extension: string }
60
58
 
61
59
  // One of a version's files pretty-printed (npmAll), named by `source`, the
62
- // hash of the file it was formatted from: its copy's text, or why it has
63
- // none, as a PrettyError tells it.
64
- export type PrettyLine = { path: string; source: string; text: string } | { path: string; source: string; error: string; status: number }
60
+ // hash of the file it was formatted from: its copy's text and layout blank
61
+ // lines (prettyCopy), or why it has none, as a PrettyError tells it.
62
+ export type PrettyLine = { path: string; source: string; text: string; blank: number[] } | { path: string; source: string; error: string; status: number }
65
63
 
66
64
  // Which of a comparison's two its files are of: the bundle or version asked
67
65
  // about, or the `other`.
@@ -94,22 +92,25 @@ export function prettyRequest(params: URLSearchParams): PrettyRequest | null {
94
92
  }
95
93
 
96
94
  // A copy's name: its file's hash in hex, with the extension it was parsed by.
97
- const prettyFile = ({ hash, extension }: PrettyRequest) =>
95
+ export const prettyFile = ({ hash, extension }: PrettyRequest) =>
98
96
  `pretty-v${PRETTY_VERSION}/${Buffer.from(hash.slice('sha512-'.length), 'base64').toString('hex')}.${extension}.br`
99
97
 
100
98
  let activeFormats = 0
101
99
  const waitingFormats: Array<() => void> = []
102
100
 
103
- // The text formatted and Brotli-encoded, where @preventive/compare's checks
104
- // find formatting changed only its layout: refused as `unsupported` where it
105
- // has no check for the kind of file, `unformattable` or `pretty-mismatch`.
101
+ // 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
+ // refused as `unsupported` where it has no check for the kind of file,
105
+ // `unformattable` or `pretty-mismatch`. Its blank lines are read in the
106
+ // same turn, as they parse and check the copy again.
106
107
  export async function prettyBody(text: string, extension: string): Promise<Buffer> {
107
108
  if (Buffer.byteLength(text) > MAX_PRETTY_BYTES) throw new PrettyError(413, 'file-too-large')
108
109
  if (activeFormats < MAX_ACTIVE_FORMATS) activeFormats++
109
110
  else if (waitingFormats.length >= MAX_WAITING_FORMATS) throw new PrettyError(429, 'pretty-busy')
110
111
  else await new Promise<void>(resolve => { waitingFormats.push(resolve) })
111
112
  let result
112
- try { result = await format(text, extension) }
113
+ try { result = await prettyFormat(text, extension) }
113
114
  finally {
114
115
  const next = waitingFormats.shift()
115
116
  if (next) next()
@@ -119,7 +120,7 @@ export async function prettyBody(text: string, extension: string): Promise<Buffe
119
120
  if (result.error === 'pretty-mismatch') console.warn(`managed: pretty-printing a .${extension} file changed more than its layout; not served`)
120
121
  throw new PrettyError(422, result.error)
121
122
  }
122
- return encodeBrotli(Buffer.from(result.code))
123
+ return encodeBrotli(Buffer.from(JSON.stringify(result)))
123
124
  }
124
125
 
125
126
  export const opened = (body: Buffer): OpenedBlob => ({ size: body.byteLength, stream: Readable.from([body]) })
@@ -127,7 +128,7 @@ export const opened = (body: Buffer): OpenedBlob => ({ size: body.byteLength, st
127
128
  // A version's file's hash, by its bytes: those of a version's files loaded
128
129
  // a little while since are the same bytes (npm-loads.ts), hashed once.
129
130
  const fileHashes = new WeakMap<Uint8Array, Promise<string>>()
130
- function npmFileHash(bytes: Uint8Array): Promise<string> {
131
+ export function npmFileHash(bytes: Uint8Array): Promise<string> {
131
132
  let hash = fileHashes.get(bytes)
132
133
  if (!hash) {
133
134
  fileHashes.set(bytes, hash = computeFileHash(bytes))
@@ -138,9 +139,18 @@ function npmFileHash(bytes: Uint8Array): Promise<string> {
138
139
 
139
140
  const decompress = promisify(brotliDecompress)
140
141
 
141
- // A pretty-printed copy's text, from its Brotli-encoded bytes.
142
+ // A pretty-printed copy's text and layout blank lines, from its Brotli-encoded
143
+ // bytes (prettyBody).
144
+ 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
+ if (typeof text !== 'string' || !numbers) throw new Error('managed: a kept pretty-printed copy is not one')
148
+ return { text, blank: numbers }
149
+ }
150
+
151
+ // A pretty-printed copy's text alone, as a rename diff reads it.
142
152
  export async function prettyText(copy: OpenedBlob): Promise<string> {
143
- return (await decompress(await readBytes(copy))).toString()
153
+ return (await prettyCopy(copy)).text
144
154
  }
145
155
 
146
156
  // Every file of a version that needs pretty-printing (needsPrettyPrint), read
@@ -159,10 +169,10 @@ async function npmPrintable(doc: NpmVersionDocument): Promise<Array<PrettyReques
159
169
  return files.toSorted((a, b) => pathOrder(a.path, b.path))
160
170
  }
161
171
 
162
- // A file's line (PrettyLine): the text of its copy, as `copy` has it, or why
163
- // it has none.
172
+ // A file's line (PrettyLine): the text of its copy and its layout blank lines,
173
+ // as `copy` has them, or why it has none.
164
174
  async function prettyLine({ path, hash }: PrettyRequest, copy: () => Promise<OpenedBlob>): Promise<PrettyLine> {
165
- try { return { path, source: hash, text: await prettyText(await copy()) } }
175
+ try { return { path, source: hash, ...await prettyCopy(await copy()) } }
166
176
  catch (err) {
167
177
  if (!(err instanceof PrettyError)) throw err
168
178
  return { path, source: hash, error: err.message, status: err.status }
@@ -233,12 +243,12 @@ function sharing<T>(): (key: string, make: () => Promise<T>) => Promise<T> {
233
243
  }
234
244
  }
235
245
 
236
- async function keep(put: () => Promise<void>) {
246
+ export async function keep(put: () => Promise<void>) {
237
247
  try { await put() } catch (err) { console.warn('managed: pretty-print cache write failed:', err) }
238
248
  }
239
249
 
240
250
  // A copy kept, or null where none is.
241
- async function kept(open: () => Promise<OpenedBlob>): Promise<OpenedBlob | null> {
251
+ export async function kept(open: () => Promise<OpenedBlob>): Promise<OpenedBlob | null> {
242
252
  try { return await open() }
243
253
  catch (err) { if (err instanceof CacheMissError) return null; throw err }
244
254
  }
@@ -197,7 +197,7 @@ async function serveBundle(res: ServerResponse, deps: ManagedHttpDeps, bundle: M
197
197
  if (!prettyCache) { json(res, 503, { error: 'unavailable' }); return }
198
198
  const pretty = await prettyOr(res, () => prettyCache.bundle(bundle, file))
199
199
  if (pretty === undefined) return
200
- await stream(pretty, 'br', 'text/plain; charset=utf-8'); return
200
+ await stream(pretty, 'br', 'application/json'); return
201
201
  }
202
202
  if (!deps.bundleCache) { json(res, 503, { error: 'unavailable' }); return }
203
203
  if (part === 'advisories') {
@@ -106,28 +106,39 @@ export function reviewsOf(pkg: LockedPackage, reviews: ReviewsOf): PublishedRevi
106
106
  export const comparedWith = (review: PublishedReview, pkg: LockedPackage, bases: string[] | 'any') => review.basePackage === null
107
107
  || review.basePackage !== pkg.name || bases === 'any' || bases.includes(review.baseVersion ?? '')
108
108
 
109
- const ACCEPTING = new Set(['accept', 'accept-with-nits'])
109
+ // The latest of `reviews`, some, the first of those as late.
110
+ export const latestOf = (reviews: PublishedReview[]) => reviews.reduce((one, other) => other.time > one.time ? other : one)
111
+ // What a review compares its version with, as one continued from it in a new
112
+ // review does too: none (whole), or a package's version; one a pull
113
+ // request's comments make, which revises no review here, of its comments.
114
+ const comparisonOf = (review: PublishedReview) => review.comment ? '\0comment' : review.basePackage === null ? '' : `${review.basePackage}@${review.baseVersion ?? ''}`
115
+
116
+ export const ACCEPTING = new Set(['accept', 'accept-with-nits'])
110
117
  // Where `pkg`'s reviewers stand on it among `reviews`, those of its version,
111
118
  // of an integrity it is locked with, and of its patch: each one's latest
112
- // telling it, any rejecting it holding it unreviewed (`rejected`); those
113
- // accepting it passing it (`passing`), each once, by a full, accepting
114
- // review of theirs, whole, compared with another package's version, or
115
- // compared with one of `bases` of its own (any, for `any`); and how many review it in its pull request's comments
116
- // alone (`commented`), which count for it alone: whose reviews passing it,
117
- // else whose latest, are its comments'. Each reviewer too (`reviewers`), by
118
- // their login, their latest verdict, whether they pass it, and whether that
119
- // verdict is its comments' (`comment`), which counts for it alone.
119
+ // telling it, whatever it was compared with, any rejecting it holding it
120
+ // unreviewed (`rejected`), any not accepting it holding each of theirs
121
+ // before it; those accepting it passing it (`passing`), each once, by a
122
+ // full, accepting review of theirs, the latest of its comparison, which
123
+ // revises those of it before, whole, compared with another package's
124
+ // version, or compared with one of `bases` of its own (any, for `any`); and
125
+ // how many review it in its pull request's comments alone (`commented`),
126
+ // which count for it alone: whose reviews passing it, else whose latest, are
127
+ // its comments'. Each reviewer too (`reviewers`), by their login, their
128
+ // latest verdict, whether they pass it, and whether that verdict is its
129
+ // comments' (`comment`), which counts for it alone.
120
130
  function stancesOf(pkg: LockedPackage, reviews: ReviewsOf, bases: string[] | 'any') {
121
131
  const by = Map.groupBy(reviewsOf(pkg, reviews), review => review.reviewer)
122
132
  const qualifies = (review: PublishedReview) => review.full && ACCEPTING.has(review.status) && comparedWith(review, pkg, bases)
123
133
  let commented = 0, passing = 0, rejected = false
124
134
  const reviewers: Reviewer[] = []
125
135
  for (const theirs of by.values()) {
126
- const latest = theirs.reduce((one, other) => other.time > one.time ? other : one)
127
- const passes = ACCEPTING.has(latest.status) && theirs.some(qualifies)
136
+ const latest = latestOf(theirs)
137
+ const current = [...Map.groupBy(theirs, comparisonOf).values()].map(latestOf).filter(qualifies)
138
+ const passes = ACCEPTING.has(latest.status) && current.length > 0
128
139
  // In its comments alone where those telling where they stand are: those
129
140
  // passing it, where they pass it, else their latest.
130
- const comment = (passes ? theirs.filter(qualifies) : [latest]).every(review => review.comment)
141
+ const comment = (passes ? current : [latest]).every(review => review.comment)
131
142
  if (comment) commented++
132
143
  if (latest.status === 'reject') rejected = true
133
144
  else if (passes) passing++
@@ -1 +0,0 @@
1
- function e(e,t,a){let o=new Map;for(let t of e.onlyBase)o.set(t.pkg,{base:t.bytes,other:null});for(let t of e.onlyOther)o.set(t.pkg,{base:null,other:t.bytes});for(let t of e.changed)o.set(t.pkg,{base:t.baseBytes,other:t.otherBytes});for(let t of e.unchanged??[])o.set(t.pkg,{base:t.bytes,other:t.bytes});let s=(e,t)=>[...e.get(t)??[]].toSorted(n),c=new Set([...e.onlyBase,...e.onlyOther,...e.changed].map(e=>e.pkg));for(let e of new Set([...t.keys(),...a.keys()]))r(s(t,e),s(a,e))||c.add(e);let l=[],u=[],d=[];for(let e of c){let n=o.get(e)??{base:null,other:null},c=s(a,e),f=s(t,e),p=n.base!==null||f.length>0,m=n.other!==null||c.length>0;p&&!m?d.push({pkg:e,bytes:n.base,versions:f}):m&&!p?l.push({pkg:e,bytes:n.other,versions:c}):u.push({pkg:e,baseBytes:n.base,otherBytes:n.other,delta:n.base!==null&&n.other!==null?n.other-n.base:null,baseVersions:f,otherVersions:c,direction:f.length>0&&c.length>0&&!r(f,c)?i(f,c):null})}let f=(e,t)=>e.pkg.localeCompare(t.pkg);return{removed:d.toSorted(f),added:l.toSorted(f),changed:u.toSorted(f)}}function t(e){let t=String(e).trim().split(`+`,1)[0],n=t.indexOf(`-`);return n===-1?{core:t,pre:``}:{core:t.slice(0,n),pre:t.slice(n+1)}}function n(e,n){if(e===n)return 0;let r=t(e),i=t(n),a=r.core.split(`.`),o=i.core.split(`.`);for(let t=0;t<Math.max(a.length,o.length);t++){let r=Number(a[t]??0),i=Number(o[t]??0);if(Number.isNaN(r)||Number.isNaN(i))return e<n?-1:1;if(r!==i)return r<i?-1:1}if(r.pre===i.pre)return 0;if(!r.pre)return 1;if(!i.pre)return-1;let s=r.pre.split(`.`),c=i.pre.split(`.`);for(let e=0;e<Math.max(s.length,c.length);e++){let t=s[e],n=c[e];if(t===void 0)return-1;if(n===void 0)return 1;if(t===n)continue;let r=/^\d+$/u.test(t),i=/^\d+$/u.test(n);return r&&i?Number(t)<Number(n)?-1:1:r===i?t<n?-1:1:r?-1:1}return 0}function r(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function i(e,t){let r=e.filter(e=>!t.includes(e)).toSorted(n),i=t.filter(t=>!e.includes(t)).toSorted(n),a=e.filter(e=>t.includes(e)).toSorted(n),[o,s]=r.length>0&&i.length>0?[r,i]:r.length>0?[r,a]:[a,i];if(o.length===0||s.length===0)return`changed`;let c=(e,t)=>n(e.at(-1),t[0])<0;return c(o,s)?`up`:c(s,o)?`down`:`changed`}function a(e,t){let a=[],o=[],s=[],c=new Set([...e.keys(),...t.keys()]);for(let l of c){let c=e.get(l),u=t.get(l);if(c&&u){let e=[...c].toSorted(n),t=[...u].toSorted(n);if(r(e,t))continue;a.push({pkg:l,baseVersions:e,otherVersions:t,direction:i(e,t)})}else c?s.push({pkg:l,versions:[...c].toSorted(n)}):o.push({pkg:l,versions:[...u].toSorted(n)})}let l=(e,t)=>e.pkg.localeCompare(t.pkg);return a.sort(l),o.sort(l),s.sort(l),{updated:a,added:o,removed:s,totals:{baseDeps:e.size,otherDeps:t.size}}}export{e as a,n as b,a as c};