@shieldaai/security-watch 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/CONTRIBUTING.md +92 -0
  3. package/LICENSE +202 -0
  4. package/README.md +394 -2
  5. package/SECURITY.md +62 -0
  6. package/dist/assessment.d.ts +16 -0
  7. package/dist/assessment.js +569 -0
  8. package/dist/cli.d.ts +2 -0
  9. package/dist/cli.js +1105 -0
  10. package/dist/config.d.ts +302 -0
  11. package/dist/config.js +381 -0
  12. package/dist/coverage.d.ts +4 -0
  13. package/dist/coverage.js +11 -0
  14. package/dist/cve-now.d.ts +35 -0
  15. package/dist/cve-now.js +1002 -0
  16. package/dist/cvss4.d.ts +3 -0
  17. package/dist/cvss4.js +39 -0
  18. package/dist/delivery.d.ts +58 -0
  19. package/dist/delivery.js +687 -0
  20. package/dist/ecosystems.d.ts +48 -0
  21. package/dist/ecosystems.js +332 -0
  22. package/dist/engine.d.ts +80 -0
  23. package/dist/engine.js +388 -0
  24. package/dist/everyday-words.d.ts +11 -0
  25. package/dist/everyday-words.js +256 -0
  26. package/dist/health.d.ts +24 -0
  27. package/dist/health.js +122 -0
  28. package/dist/heartbeat.d.ts +13 -0
  29. package/dist/heartbeat.js +74 -0
  30. package/dist/http.d.ts +15 -0
  31. package/dist/http.js +280 -0
  32. package/dist/index.d.ts +36 -0
  33. package/dist/index.js +23 -0
  34. package/dist/inventory-evidence.d.ts +10 -0
  35. package/dist/inventory-evidence.js +206 -0
  36. package/dist/inventory.d.ts +276 -0
  37. package/dist/inventory.js +336 -0
  38. package/dist/lock.d.ts +3 -0
  39. package/dist/lock.js +112 -0
  40. package/dist/maintenance.d.ts +48 -0
  41. package/dist/maintenance.js +295 -0
  42. package/dist/matching.d.ts +29 -0
  43. package/dist/matching.js +537 -0
  44. package/dist/models.d.ts +33 -0
  45. package/dist/models.js +932 -0
  46. package/dist/operations.d.ts +17 -0
  47. package/dist/operations.js +117 -0
  48. package/dist/pilot-cases.d.ts +171 -0
  49. package/dist/pilot-cases.js +330 -0
  50. package/dist/pilot-input.d.ts +16 -0
  51. package/dist/pilot-input.js +125 -0
  52. package/dist/pilot-recall.d.ts +166 -0
  53. package/dist/pilot-recall.js +231 -0
  54. package/dist/pilot-report.d.ts +113 -0
  55. package/dist/pilot-report.js +279 -0
  56. package/dist/pilot-responses.d.ts +111 -0
  57. package/dist/pilot-responses.js +159 -0
  58. package/dist/providers.d.ts +209 -0
  59. package/dist/providers.js +459 -0
  60. package/dist/python-version.d.ts +3 -0
  61. package/dist/python-version.js +30 -0
  62. package/dist/rehearsal.d.ts +440 -0
  63. package/dist/rehearsal.js +372 -0
  64. package/dist/sbom.d.ts +13 -0
  65. package/dist/sbom.js +193 -0
  66. package/dist/setup.d.ts +113 -0
  67. package/dist/setup.js +100 -0
  68. package/dist/source-error.d.ts +38 -0
  69. package/dist/source-error.js +71 -0
  70. package/dist/sources.d.ts +3 -0
  71. package/dist/sources.js +1725 -0
  72. package/dist/store.d.ts +96 -0
  73. package/dist/store.js +953 -0
  74. package/dist/types.d.ts +404 -0
  75. package/dist/types.js +1 -0
  76. package/dist/util.d.ts +29 -0
  77. package/dist/util.js +94 -0
  78. package/dist/vendor/cvss4-reference.d.ts +2 -0
  79. package/dist/vendor/cvss4-reference.js +789 -0
  80. package/dist/vendor-sources.d.ts +24 -0
  81. package/dist/vendor-sources.js +56 -0
  82. package/dist/word-filter.d.ts +3 -0
  83. package/dist/word-filter.js +3291 -0
  84. package/docs/alert-outcome-evaluation.md +150 -0
  85. package/docs/compatibility.md +35 -0
  86. package/docs/configuration.md +666 -0
  87. package/docs/cvss-v4-support.md +41 -0
  88. package/docs/offline-advisory-preview.md +114 -0
  89. package/docs/offline-rehearsal.md +44 -0
  90. package/docs/pilot-cases.md +58 -0
  91. package/docs/pilot-recall.md +124 -0
  92. package/docs/pilot-report.md +52 -0
  93. package/docs/validation/alert-outcome-evaluation-2026-11-05.json +2935 -0
  94. package/docs/validation/cvss-v4-reference-parity-2026-11-05.json +93 -0
  95. package/docs/validation/rehearsal-accepted-2026-11-05.json +175 -0
  96. package/docs/validation/rehearsal-unknown-2026-11-05.json +175 -0
  97. package/docs/vendor/cvss-v4-LICENSE.txt +22 -0
  98. package/docs/vendor/cvss-v4-provenance.json +38 -0
  99. package/examples/advisories.json +17 -0
  100. package/examples/agent-advisories-2026.json +145 -0
  101. package/examples/agent-advisory-cases-2026.json +397 -0
  102. package/examples/common-systems.yaml +243 -0
  103. package/examples/libraries.example.txt +18 -0
  104. package/examples/libraries.example.yaml +40 -0
  105. package/examples/library-watch.example.yaml +27 -0
  106. package/examples/offline-advisory-preview.mjs +42 -0
  107. package/examples/rehearsal.json +75 -0
  108. package/examples/security-watch.example.yaml +86 -0
  109. package/examples/systems.example.yaml +35 -0
  110. package/package.json +85 -4
@@ -0,0 +1,404 @@
1
+ import type { ModelProvider, OutputFormat } from './providers.js';
2
+ import type { Ecosystem } from './ecosystems.js';
3
+ export type Severity = 'unknown' | 'low' | 'medium' | 'high' | 'critical';
4
+ export type Category = 'library' | 'product' | 'news' | 'malware' | 'health';
5
+ export type Applicability = 'affected' | 'not_affected' | 'unknown' | 'conflicting' | 'relevant';
6
+ export type Channel = 'slack' | 'email';
7
+ export interface Provenance {
8
+ source: string;
9
+ reference?: string;
10
+ recorded_at?: string;
11
+ }
12
+ export interface DeploymentObservation {
13
+ observed_at: string;
14
+ method: string;
15
+ reference?: string;
16
+ }
17
+ export type ArtifactKind = 'application' | 'library' | 'mcp_server' | 'mcp_client' | 'plugin' | 'skill' | 'model' | 'container' | 'other';
18
+ /** An operator's recorded result. Security Watch does not perform cryptographic verification. */
19
+ export interface DeclaredVerification {
20
+ status: 'verified' | 'failed' | 'unknown';
21
+ reference: string;
22
+ recorded_at: string;
23
+ recorded_by?: string;
24
+ }
25
+ export interface DependencyLink {
26
+ ref: string;
27
+ component_id?: string;
28
+ }
29
+ export interface DependencyRelationship {
30
+ ref: string;
31
+ depends_on: string[];
32
+ }
33
+ /** Lifecycle evidence is never an instruction to close or downgrade a finding. */
34
+ export interface DeclaredRemediation {
35
+ event_id: string;
36
+ stage: 'proposed' | 'changed' | 'deployed';
37
+ verification: 'supported' | 'failed' | 'unknown';
38
+ reference: string;
39
+ recorded_at: string;
40
+ recorded_by?: string;
41
+ }
42
+ export interface InventoryEvidence {
43
+ origin?: string;
44
+ digest?: string;
45
+ provenance?: Provenance;
46
+ observation?: DeploymentObservation;
47
+ artifact_kind?: ArtifactKind;
48
+ purl?: string;
49
+ source_url?: string;
50
+ source_revision?: string;
51
+ sbom_ref?: string;
52
+ dependencies?: DependencyLink[];
53
+ signature_verification?: DeclaredVerification;
54
+ attestation_verification?: DeclaredVerification;
55
+ remediation_evidence?: DeclaredRemediation[];
56
+ }
57
+ export interface Component extends InventoryEvidence {
58
+ id: string;
59
+ ecosystem: Ecosystem;
60
+ name: string;
61
+ version?: string;
62
+ /** Operator's pointer to where this library is used. Shown in notifications; never sent to models or sources. */
63
+ note?: string;
64
+ }
65
+ export interface Product {
66
+ name: string;
67
+ version?: string;
68
+ keywords: string[];
69
+ /** Operator's pointer to where this product runs. Shown in notifications; never sent to models or sources. */
70
+ note?: string;
71
+ }
72
+ /** Where a reader should look to confirm a finding. Copied from the inventory; not security evidence. */
73
+ export interface FindingLocation {
74
+ system: string;
75
+ owner?: string;
76
+ system_note?: string;
77
+ component?: string;
78
+ component_note?: string;
79
+ }
80
+ export interface System extends InventoryEvidence {
81
+ id: string;
82
+ name: string;
83
+ environment: string;
84
+ owner?: string;
85
+ /** Operator's pointer to where this system lives. Shown in notifications; never sent to models or sources. */
86
+ note?: string;
87
+ configuration?: Record<string, string | boolean | number>;
88
+ sbom_dependencies?: DependencyRelationship[];
89
+ exposure: 'internet' | 'internal' | 'unknown';
90
+ criticality: Severity;
91
+ updated_at: string;
92
+ components: Component[];
93
+ products: Product[];
94
+ keywords: string[];
95
+ }
96
+ export interface Inventory {
97
+ systems: System[];
98
+ import_warnings?: string[];
99
+ /** Entries that are accepted as written but may not be what was meant. They do not degrade a scan. */
100
+ name_notices?: string[];
101
+ }
102
+ export interface PackageClaim {
103
+ ecosystem: string;
104
+ name: string;
105
+ range?: string;
106
+ versions?: string[];
107
+ matched_versions?: string[];
108
+ fixed?: string;
109
+ }
110
+ export interface CvssV4BaseScore {
111
+ /** Validated source vector; optional contextual metrics may be present. */
112
+ vector: string;
113
+ /** Exact CVSS-B vector used to derive score. CVSS is owned by FIRST and used by permission. */
114
+ base_vector: string;
115
+ score: number;
116
+ context_removed: boolean;
117
+ }
118
+ export interface Evidence {
119
+ id: string;
120
+ source: string;
121
+ kind: 'advisory' | 'news' | 'kev' | 'health';
122
+ title: string;
123
+ url: string;
124
+ description: string;
125
+ /** Whether the adapter retrieved full text, an excerpt or no usable content. */
126
+ content_status?: 'full' | 'excerpt' | 'unavailable';
127
+ aliases: string[];
128
+ severity: Severity;
129
+ /** Bounded source diagnostics. Does not establish applicability, exploitation or deployment context. */
130
+ cvss_v4?: {
131
+ base_scores: CvssV4BaseScore[];
132
+ unsupported_vectors: number;
133
+ };
134
+ modified_at: string;
135
+ fetched_at: string;
136
+ published_at?: string;
137
+ packages: PackageClaim[];
138
+ withdrawn?: boolean;
139
+ exploited?: boolean;
140
+ malware?: boolean;
141
+ }
142
+ export interface ModelSpec {
143
+ provider: ModelProvider;
144
+ /** Minimal identifiers by default. Extra deployment context requires explicit opt-in per model. */
145
+ disclosure_profile?: 'minimal' | 'deployment-context';
146
+ output_format?: OutputFormat;
147
+ model: string;
148
+ api_key_env: string;
149
+ endpoint?: string;
150
+ allow_private_endpoint: boolean;
151
+ input_usd_per_million?: number;
152
+ output_usd_per_million?: number;
153
+ }
154
+ export interface Config {
155
+ version: 1;
156
+ inventory: string;
157
+ database: string;
158
+ poll_interval_minutes: number;
159
+ news_lookback_minutes: number;
160
+ inventory_stale_days: number;
161
+ digest_interval_hours: number;
162
+ sources: {
163
+ cve_now: boolean;
164
+ hacker_news: boolean;
165
+ osv: boolean;
166
+ github: boolean;
167
+ github_token_env: string;
168
+ kev: boolean;
169
+ feeds: string[];
170
+ vendor_feeds?: ('nodejs' | 'python' | 'php')[];
171
+ };
172
+ models: {
173
+ enabled: boolean;
174
+ analysis?: ModelSpec;
175
+ verification?: ModelSpec;
176
+ daily_request_limit: number;
177
+ daily_token_limit: number;
178
+ daily_budget_usd?: number;
179
+ max_input_chars: number;
180
+ max_output_tokens: number;
181
+ timeout_seconds: number;
182
+ };
183
+ policy: {
184
+ library: Rule;
185
+ product: Rule;
186
+ news: Rule;
187
+ urgent_unknown: boolean;
188
+ exploited_override: boolean;
189
+ malware_override: boolean;
190
+ /** Review both AI-only dismissal routes by default. */
191
+ ai_dismissals?: 'review' | 'record';
192
+ /** Deprecated configuration alias; true maps to review, false to record. */
193
+ review_before_news_suppression?: boolean;
194
+ health_channels: Channel[];
195
+ };
196
+ notifications: {
197
+ slack?: {
198
+ webhook_url_env: string;
199
+ };
200
+ email?: {
201
+ smtp_url_env: string;
202
+ from: string;
203
+ to: string[];
204
+ };
205
+ max_attempts: number;
206
+ };
207
+ monitoring?: {
208
+ heartbeat_url_env: string;
209
+ interval_seconds: number;
210
+ allow_private_endpoint: boolean;
211
+ };
212
+ maintenance?: {
213
+ interval_hours: number;
214
+ retention?: {
215
+ analysis_cache_days?: number;
216
+ evidence_history_days?: number;
217
+ finding_history_days?: number;
218
+ accepted_delivery_days?: number;
219
+ inactive_findings_days?: number;
220
+ unreferenced_evidence_days?: number;
221
+ batch_size?: number;
222
+ };
223
+ backup?: {
224
+ directory: string;
225
+ keep: number;
226
+ max_bytes: number;
227
+ };
228
+ };
229
+ }
230
+ export interface Rule {
231
+ levels: Severity[];
232
+ basis: 'source_severity' | 'priority';
233
+ channels: Channel[];
234
+ }
235
+ export interface Assessment {
236
+ /** Only news evidence supports this candidate; AI impact remains a separate claim. */
237
+ news_only?: boolean;
238
+ remediation?: RemediationEvidence;
239
+ /** Inventory names and notes for the reader of a notification. Not part of alert deduplication. */
240
+ location?: FindingLocation;
241
+ /** News-only or generic product context without deterministic package applicability. */
242
+ news_candidate?: boolean;
243
+ /**
244
+ * The supported version ranges exclude the installed version, but the advisory evidence has not
245
+ * been refreshed for two poll intervals, so applicability is `unknown`. The source's own health
246
+ * finding reports the outage; this result goes to the review digest instead of an urgent alert.
247
+ */
248
+ stale_exclusion?: boolean;
249
+ active?: boolean;
250
+ id: string;
251
+ event_id: string;
252
+ system_id: string;
253
+ component_id?: string;
254
+ category: Category;
255
+ applicability: Applicability;
256
+ source_severity: Severity;
257
+ priority: Severity;
258
+ title: string;
259
+ summary: string;
260
+ reasons: string[];
261
+ evidence_ids: string[];
262
+ urls: string[];
263
+ exploited: boolean;
264
+ fix?: string;
265
+ updated_at: string;
266
+ withdrawn?: boolean;
267
+ relevance?: 'relevant' | 'irrelevant' | 'unknown';
268
+ /** Model assessment of the article, separate from version-range applicability. */
269
+ news_analysis?: NewsAnalysis;
270
+ ai?: {
271
+ model: string;
272
+ provider: string;
273
+ prompt_version: string;
274
+ status: 'complete' | 'unavailable' | 'conflicting';
275
+ };
276
+ }
277
+ export type AiReviewReason = 'ai_general_news' | 'ai_not_affected' | 'ai_irrelevant';
278
+ export interface AiReview {
279
+ finding_id: string;
280
+ revision: number;
281
+ reason: AiReviewReason;
282
+ status: 'pending' | 'investigate' | 'dismissed' | 'superseded';
283
+ created_at: number;
284
+ reviewed_at: number | null;
285
+ reviewer: string | null;
286
+ note: string | null;
287
+ evidence: {
288
+ source: string;
289
+ id: string;
290
+ content_hash: string;
291
+ }[];
292
+ assessment: Assessment;
293
+ }
294
+ export interface Decision {
295
+ /** A durable local review is required even when channels is empty. */
296
+ review?: AiReviewReason;
297
+ action: 'notify' | 'digest' | 'record';
298
+ channels: Channel[];
299
+ reason: string;
300
+ }
301
+ export interface Notification {
302
+ id: string;
303
+ finding_id: string;
304
+ /** The persisted finding revision selected when this delivery was enqueued. */
305
+ revision?: number;
306
+ channel: Channel;
307
+ payload: string;
308
+ status: 'pending' | 'sending' | 'accepted' | 'unknown' | 'failed';
309
+ attempts: number;
310
+ available_at: number;
311
+ /** Local UTC time in milliseconds when the destination acknowledged this notification. */
312
+ accepted_at?: number;
313
+ last_error?: string;
314
+ }
315
+ export interface HttpOptions {
316
+ method?: 'GET' | 'POST';
317
+ headers?: Record<string, string>;
318
+ body?: string;
319
+ maxBytes?: number;
320
+ timeoutMs?: number;
321
+ /** Minimum delay between request starts to an origin; shared by concurrent calls. */
322
+ minIntervalMs?: number;
323
+ allowPrivate?: boolean;
324
+ redirects?: number;
325
+ /** When present, every redirect must remain within one of these exact origins. */
326
+ allowedRedirectOrigins?: string[];
327
+ signal?: AbortSignal;
328
+ }
329
+ export interface HttpResult {
330
+ status: number;
331
+ headers: Record<string, string>;
332
+ body: string;
333
+ }
334
+ export type HttpClient = (url: string, options?: HttpOptions) => Promise<HttpResult>;
335
+ export interface CoverageReceipt {
336
+ source: string;
337
+ attempted_at: string;
338
+ last_success_at?: string;
339
+ fresh_until?: string;
340
+ status: 'complete' | 'partial' | 'failed';
341
+ freshness: 'fresh' | 'stale' | 'never';
342
+ evidence_count: number;
343
+ errors: string[];
344
+ scope: string;
345
+ /**
346
+ * Present on an incomplete receipt when the source still read every record for the inventory's own
347
+ * packages; what it missed are other records. Version exclusions from the source remain supported.
348
+ */
349
+ package_results?: 'complete';
350
+ }
351
+ export interface InventoryDrift {
352
+ system_id: string;
353
+ component_id?: string;
354
+ change: 'added' | 'removed' | 'changed';
355
+ fields: string[];
356
+ before_digest?: string;
357
+ after_digest?: string;
358
+ }
359
+ export interface RemediationEvidence {
360
+ status: 'review_required' | 'version_excluded' | 'observed_version_excluded';
361
+ observed_at?: string;
362
+ artifact_digest?: string;
363
+ source_urls: string[];
364
+ requirements: string[];
365
+ configuration: Record<string, string | boolean | number>;
366
+ note: string;
367
+ operator_records?: DeclaredRemediation[];
368
+ signature_verification?: DeclaredVerification;
369
+ attestation_verification?: DeclaredVerification;
370
+ }
371
+ export interface SourceResult {
372
+ evidence: Evidence[];
373
+ errors: {
374
+ source: string;
375
+ message: string;
376
+ }[];
377
+ checkpoints: Record<string, string>;
378
+ coverage: CoverageReceipt[];
379
+ }
380
+ export interface AiAnalysis {
381
+ relevance: 'relevant' | 'irrelevant' | 'unknown';
382
+ priority: Severity;
383
+ summary: string;
384
+ evidence_ids: string[];
385
+ quotes: {
386
+ evidence_id: string;
387
+ quote: string;
388
+ }[];
389
+ news_analysis?: NewsAnalysis | null;
390
+ }
391
+ export interface NewsAnalysis {
392
+ classification: 'new_vulnerability' | 'known_vulnerability_update' | 'general_news' | 'unknown';
393
+ impact: 'affected' | 'not_affected' | 'unknown' | 'not_applicable';
394
+ affected_products: string[];
395
+ /** Affected or fixed versions quoted from the source. */
396
+ affected_versions: string[];
397
+ /** Source wording describing the conditions necessary for exploitation. */
398
+ prerequisites: string[];
399
+ rationale: string;
400
+ quotes: {
401
+ evidence_id: string;
402
+ quote: string;
403
+ }[];
404
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/util.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { Severity } from './types.js';
2
+ export declare const severityRank: Record<Severity, number>;
3
+ export declare function highest(...values: Severity[]): Severity;
4
+ export declare function hash(value: unknown): string;
5
+ export declare function timestamp(now?: number): string;
6
+ export declare function cleanText(value: string, max?: number): string;
7
+ /**
8
+ * Characters that end or disturb a line: control characters (including NEL, U+0085) and the Unicode
9
+ * line and paragraph separators. Mail clients and chat applications render them as line breaks.
10
+ */
11
+ export declare const LINE_BREAKING: RegExp;
12
+ /** Fillers that show as a blank (braille blank, Hangul fillers): spaces to the reader. */
13
+ export declare const FILLERS: RegExp;
14
+ /**
15
+ * Characters that show as nothing: Unicode's default-ignorable code points (zero-width and joining
16
+ * marks, direction marks, variation selectors, tag characters, the soft hyphen), format characters,
17
+ * and the object and annotation marks.
18
+ */
19
+ export declare const INVISIBLE: RegExp;
20
+ /** The first `max` UTF-16 units of a text, without leaving half of a surrogate pair at the end. */
21
+ export declare function cut(value: string, max: number): string;
22
+ /**
23
+ * The text on one line: every run of line-breaking characters, with the spaces around it, becomes a
24
+ * single space. The work is proportional to the input and stops once `max` characters are kept, so
25
+ * a value padded with megabytes of spaces costs nothing more than reading it.
26
+ */
27
+ export declare function singleLine(input: string, max?: number): string;
28
+ /** A review binds JSON content independently of object insertion order. Array order is retained. */
29
+ export declare function canonicalHash(value: unknown): string;
package/dist/util.js ADDED
@@ -0,0 +1,94 @@
1
+ import { createHash } from 'node:crypto';
2
+ export const severityRank = { unknown: 0, low: 1, medium: 2, high: 3, critical: 4 };
3
+ export function highest(...values) {
4
+ return values.reduce((a, b) => (severityRank[a] >= severityRank[b] ? a : b), 'unknown');
5
+ }
6
+ export function hash(value) {
7
+ return createHash('sha256').update(JSON.stringify(value)).digest('hex');
8
+ }
9
+ export function timestamp(now = Date.now()) {
10
+ return new Date(now).toISOString();
11
+ }
12
+ export function cleanText(value, max = 4000) {
13
+ return cut(value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, ''), max);
14
+ }
15
+ /**
16
+ * Characters that end or disturb a line: control characters (including NEL, U+0085) and the Unicode
17
+ * line and paragraph separators. Mail clients and chat applications render them as line breaks.
18
+ */
19
+ export const LINE_BREAKING = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/u;
20
+ /** Fillers that show as a blank (braille blank, Hangul fillers): spaces to the reader. */
21
+ export const FILLERS = /[\u115f\u1160\u2800\u3164\uffa0]/g;
22
+ /**
23
+ * Characters that show as nothing: Unicode's default-ignorable code points (zero-width and joining
24
+ * marks, direction marks, variation selectors, tag characters, the soft hyphen), format characters,
25
+ * and the object and annotation marks.
26
+ */
27
+ export const INVISIBLE = /[\p{Default_Ignorable_Code_Point}\p{Cf}\ufff9-\ufffc]/gu;
28
+ /** The first `max` UTF-16 units of a text, without leaving half of a surrogate pair at the end. */
29
+ export function cut(value, max) {
30
+ if (value.length <= max)
31
+ return value;
32
+ const end = max > 0 && (value.charCodeAt(max - 1) & 0xfc00) === 0xd800 ? max - 1 : max;
33
+ return value.slice(0, end);
34
+ }
35
+ const lineBreaking = (code) => code <= 0x1f || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029;
36
+ /** Spaces that are not line-breaking characters; with those, everything `\s` matches. */
37
+ const blank = (code) => code === 0x20 ||
38
+ code === 0xa0 ||
39
+ code === 0x1680 ||
40
+ (code >= 0x2000 && code <= 0x200a) ||
41
+ code === 0x202f ||
42
+ code === 0x205f ||
43
+ code === 0x3000 ||
44
+ code === 0xfeff;
45
+ /**
46
+ * The text on one line: every run of line-breaking characters, with the spaces around it, becomes a
47
+ * single space. The work is proportional to the input and stops once `max` characters are kept, so
48
+ * a value padded with megabytes of spaces costs nothing more than reading it.
49
+ */
50
+ export function singleLine(input, max = 4000) {
51
+ // Characters that show as nothing do not count against the limit: a value padded with thousands
52
+ // of them must not be cut short of its visible end ("8.0.3" for "8.0.39").
53
+ const value = input.replace(FILLERS, ' ').replace(INVISIBLE, '');
54
+ const length = value.length;
55
+ let index = 0;
56
+ while (index < length && (blank(value.charCodeAt(index)) || lineBreaking(value.charCodeAt(index))))
57
+ index++;
58
+ let out = '';
59
+ let start = index;
60
+ for (; index < length; index++) {
61
+ const code = value.charCodeAt(index);
62
+ if (!lineBreaking(code)) {
63
+ // Enough is kept once a visible character lies past the limit. Spaces there are still read,
64
+ // because a line break after them would remove them.
65
+ if (!blank(code) && out.length + (index - start) >= max)
66
+ break;
67
+ continue;
68
+ }
69
+ let end = index;
70
+ while (end > start && blank(value.charCodeAt(end - 1)))
71
+ end--;
72
+ out += value.slice(start, end);
73
+ while (index < length && (blank(value.charCodeAt(index)) || lineBreaking(value.charCodeAt(index))))
74
+ index++;
75
+ // Nothing but separators was left: the text ends here.
76
+ if (index < length)
77
+ out += ' ';
78
+ start = index;
79
+ index--;
80
+ }
81
+ out += value.slice(start, Math.min(length, start + Math.max(0, max - out.length)));
82
+ const kept = cut(out, max);
83
+ // The copy above may stop between the two halves of a surrogate pair.
84
+ const last = kept.charCodeAt(kept.length - 1);
85
+ return ((last & 0xfc00) === 0xd800 ? kept.slice(0, -1) : kept).trimEnd();
86
+ }
87
+ /** A review binds JSON content independently of object insertion order. Array order is retained. */
88
+ export function canonicalHash(value) {
89
+ return createHash('sha256')
90
+ .update(JSON.stringify(value, (_key, item) => item && typeof item === 'object' && !Array.isArray(item)
91
+ ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b)))
92
+ : item))
93
+ .digest('hex');
94
+ }
@@ -0,0 +1,2 @@
1
+ export declare function referenceBaseScore(selected: Record<string, string>): number;
2
+ export declare const referenceMetrics: Readonly<Record<string, readonly string[]>>;