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

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.
@@ -1,2 +1,2 @@
1
- export function findingValueIndex(): { values: unknown[]; index<T>(data: T): T }
2
- export function expandFindingValues<T>(data: T, values: unknown): T
1
+ export function packTeamReports<T extends { repo?: unknown; data: unknown }>(reports: readonly T[]): { reports: T[]; values: Record<string, unknown[]> | null; keys: string[][] | null }
2
+ export function unpackTeamReport<T>(report: T, sent?: { values?: unknown; keys?: unknown }): T | null
@@ -1,44 +1,138 @@
1
- // A finding's `package` and `repo`: the same few across a team's many
2
- // findings. A team's reports are sent with each once, in the response's
3
- // `values`, and each finding's as its index there
4
- // (server-managed/team-reports.ts), put back as they are read
5
- // (client/managed/session.js).
6
- const FIELDS = ['package', 'repo']
1
+ // A team's reports tell the same few values over and over across their many
2
+ // findings: their severity, model, type and effort, the files they are in,
3
+ // their packages, repositories and bundles; and each finding tells the same
4
+ // few field names. A team response (server-managed/team-reports.ts) sends
5
+ // each once, read back as it is read (client/managed/session.js):
6
+ //
7
+ // - `values`: by field, the values told of it, the most told first, so that
8
+ // the commonest are a single digit, and each place one is told as its index
9
+ // there. A field is pooled where that sends fewer characters than its
10
+ // values in place, and then every value it is told but null is an index: a
11
+ // report's own `repo`, its data's own fields (but its entries and links)
12
+ // and its findings'.
13
+ // - `keys`: the field names of its findings, by shape, the most told first,
14
+ // and each finding as an array, its shape's index there and then its
15
+ // values in that shape's order. An entry of a report's data that is an
16
+ // array is a group of them; anything in a group that is not a finding is
17
+ // sent as `[-1, it]`.
7
18
 
19
+ // A report's data's fields holding its entries and links, never pooled.
20
+ const STRUCTURE = new Set(['findings', 'groups', 'links'])
21
+ const NOT_A_FINDING = -1
22
+
23
+ const isObject = value => value !== null && typeof value === 'object' && !Array.isArray(value)
8
24
  const entriesKey = data => Array.isArray(data?.findings) ? 'findings' : Array.isArray(data?.groups) ? 'groups' : null
25
+ const findingsOf = data => { const key = entriesKey(data); return key ? data[key].flat().filter(isObject) : [] }
26
+ // A finding's fields as JSON writes them: those holding a value.
27
+ const fieldsOf = finding => Object.keys(finding).filter(field => finding[field] !== undefined)
9
28
 
10
- // `values`, and `index(data)`: a report's data with its findings' values as
11
- // their index in it, one value at one index across every report it is given.
12
- export function findingValueIndex() {
13
- const indexes = new Map(), values = []
14
- const indexOf = value => {
15
- const key = JSON.stringify(value)
16
- if (!indexes.has(key)) indexes.set(key, values.push(value) - 1)
17
- return indexes.get(key)
18
- }
19
- const indexed = finding => {
20
- if (!finding || typeof finding !== 'object' || !FIELDS.some(field => finding[field] != null)) return finding
21
- const copy = { ...finding }
22
- for (const field of FIELDS) if (finding[field] != null) copy[field] = indexOf(finding[field])
23
- return copy
29
+ // Values as they are counted, each kept by itself: a string as it is, and
30
+ // anything else by its JSON. The most told first, then the first told.
31
+ function counter() {
32
+ const others = new Map(), strings = new Map()
33
+ let seen = 0
34
+ const entryOf = (value, create) => {
35
+ const map = typeof value === 'string' ? strings : others
36
+ const key = typeof value === 'string' ? value : JSON.stringify(value)
37
+ let entry = map.get(key)
38
+ if (!entry && create) map.set(key, entry = { value, count: 0, first: seen++, size: typeof value === 'string' ? JSON.stringify(value).length : key.length, index: -1 })
39
+ return entry
24
40
  }
25
41
  return {
26
- values,
27
- index(data) {
28
- const key = entriesKey(data)
29
- return key ? { ...data, [key]: data[key].map(entry => Array.isArray(entry) ? entry.map(indexed) : indexed(entry)) } : data
42
+ count(value) { entryOf(value, true).count++ },
43
+ index: value => entryOf(value, false).index,
44
+ ranked() {
45
+ const entries = [...strings.values(), ...others.values()].toSorted((a, b) => b.count - a.count || a.first - b.first)
46
+ for (const [index, entry] of entries.entries()) entry.index = index
47
+ return entries
30
48
  },
31
49
  }
32
50
  }
33
51
 
34
- // A report's data, as read, its findings' values put in place of their index
35
- // in a response's `values`; one sent without any as it is.
36
- export function expandFindingValues(data, values) {
37
- const key = entriesKey(data)
38
- if (!key || !Array.isArray(values)) return data
39
- for (const finding of data[key].flat()) {
40
- if (!finding || typeof finding !== 'object') continue
41
- for (const field of FIELDS) if (Number.isInteger(finding[field])) finding[field] = values[finding[field]]
42
- }
43
- return data
52
+ // A field set as JSON.parse sets it: an own field, whatever its name.
53
+ function put(record, field, value) {
54
+ if (field === '__proto__') Object.defineProperty(record, field, { value, enumerable: true, writable: true, configurable: true })
55
+ else record[field] = value
56
+ }
57
+
58
+ // `reports` as a team response sends them, `{ id, filename, repo, data, ... }`
59
+ // each: copies, their values pooled in `values` (null where none is) and their
60
+ // findings shaped by `keys` (null where there are none), the reports
61
+ // themselves left as they are.
62
+ export function packTeamReports(reports) {
63
+ const fields = new Map(), shapes = counter()
64
+ const count = (field, value) => {
65
+ if (value == null) return
66
+ let values = fields.get(field)
67
+ if (!values) fields.set(field, values = counter())
68
+ values.count(value)
69
+ }
70
+ for (const report of reports) {
71
+ count('repo', report.repo)
72
+ if (!isObject(report.data)) continue
73
+ for (const field in report.data) if (!STRUCTURE.has(field)) count(field, report.data[field])
74
+ for (const finding of findingsOf(report.data)) {
75
+ const named = fieldsOf(finding)
76
+ shapes.count(named)
77
+ for (const field of named) count(field, finding[field])
78
+ }
79
+ }
80
+ const values = {}
81
+ for (const [field, counted] of fields) {
82
+ const entries = counted.ranked()
83
+ let inline = 0, pooled = JSON.stringify(field).length + 3 + entries.length
84
+ for (const [index, entry] of entries.entries()) { inline += entry.count * entry.size; pooled += entry.size + entry.count * String(index).length }
85
+ if (pooled < inline) put(values, field, entries.map(entry => entry.value))
86
+ else fields.delete(field)
87
+ }
88
+ const keys = shapes.ranked().map(entry => entry.value)
89
+ const sent = (field, value) => value != null && fields.has(field) ? fields.get(field).index(value) : value
90
+ const finding = value => {
91
+ const named = fieldsOf(value)
92
+ return [shapes.index(named), ...named.map(field => sent(field, value[field]))]
93
+ }
94
+ const entry = value => Array.isArray(value) ? value.map(inGroup => isObject(inGroup) ? finding(inGroup) : [NOT_A_FINDING, inGroup])
95
+ : isObject(value) ? finding(value) : value
96
+ return {
97
+ values: fields.size > 0 ? values : null,
98
+ keys: keys.length > 0 ? keys : null,
99
+ reports: reports.map(report => {
100
+ const copy = { ...report }
101
+ if (report.repo != null) copy.repo = sent('repo', report.repo)
102
+ if (!isObject(report.data)) return copy
103
+ copy.data = { ...report.data }
104
+ for (const field in report.data) if (!STRUCTURE.has(field) && fields.has(field)) put(copy.data, field, sent(field, report.data[field]))
105
+ const key = entriesKey(report.data)
106
+ if (key) copy.data[key] = report.data[key].map(entry)
107
+ return copy
108
+ }),
109
+ }
110
+ }
111
+
112
+ // A report of a team response, as read, given the response's `values` and
113
+ // `keys`: its findings objects again, their values in place of their index;
114
+ // one sent without either as it is. Null where its findings cannot be read.
115
+ export function unpackTeamReport(report, { values = null, keys = null } = {}) {
116
+ if (!isObject(report)) return report
117
+ const pools = new Map(isObject(values) ? Object.entries(values).filter(([, list]) => Array.isArray(list)) : [])
118
+ const shaped = Array.isArray(keys)
119
+ let readable = true
120
+ const value = (field, sent) => Number.isInteger(sent) && pools.has(field) ? pools.get(field)[sent] : sent
121
+ const expanded = record => { for (const [field, list] of pools) if (Number.isInteger(record[field])) put(record, field, list[record[field]]); return record }
122
+ const tuple = sent => {
123
+ const named = keys[sent[0]]
124
+ if (!Array.isArray(named) || named.length !== sent.length - 1) { readable = false; return null }
125
+ const read = {}
126
+ for (const [index, field] of named.entries()) put(read, field, value(field, sent[index + 1]))
127
+ return read
128
+ }
129
+ const isTuple = sent => shaped && Array.isArray(sent) && Number.isInteger(sent[0])
130
+ const inGroup = sent => isTuple(sent) ? sent[0] === NOT_A_FINDING ? sent[1] : tuple(sent) : isObject(sent) ? expanded(sent) : sent
131
+ const entry = sent => Array.isArray(sent) && !isTuple(sent) ? sent.map(inGroup) : inGroup(sent)
132
+ if (report.repo != null) report.repo = value('repo', report.repo)
133
+ if (!isObject(report.data)) return report
134
+ for (const field in report.data) if (!STRUCTURE.has(field) && pools.has(field)) put(report.data, field, value(field, report.data[field]))
135
+ const key = entriesKey(report.data)
136
+ if (key) report.data[key] = report.data[key].map(entry)
137
+ return readable ? report : null
44
138
  }