@shieldaai/security-watch 0.1.0-alpha.0 → 1.0.1
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/CHANGELOG.md +178 -0
- package/CONTRIBUTING.md +4 -2
- package/README.md +173 -48
- package/SECURITY.md +24 -9
- package/dist/assessment.d.ts +12 -1
- package/dist/assessment.js +223 -52
- package/dist/cli.js +340 -27
- package/dist/config.d.ts +62 -2
- package/dist/config.js +88 -5
- package/dist/coverage.d.ts +4 -0
- package/dist/coverage.js +11 -0
- package/dist/cve-now.d.ts +32 -2
- package/dist/cve-now.js +715 -126
- package/dist/cvss4.d.ts +3 -0
- package/dist/cvss4.js +39 -0
- package/dist/delivery.d.ts +34 -2
- package/dist/delivery.js +391 -98
- package/dist/ecosystems.d.ts +23 -0
- package/dist/ecosystems.js +88 -10
- package/dist/engine.d.ts +55 -1
- package/dist/engine.js +101 -20
- package/dist/everyday-words.d.ts +11 -0
- package/dist/everyday-words.js +256 -0
- package/dist/health.d.ts +7 -0
- package/dist/health.js +55 -3
- package/dist/http.d.ts +7 -0
- package/dist/http.js +31 -2
- package/dist/index.d.ts +22 -4
- package/dist/index.js +12 -4
- package/dist/inventory-evidence.d.ts +10 -0
- package/dist/inventory-evidence.js +206 -0
- package/dist/inventory.d.ts +273 -0
- package/dist/inventory.js +210 -17
- package/dist/maintenance.d.ts +5 -0
- package/dist/maintenance.js +73 -5
- package/dist/matching.d.ts +29 -0
- package/dist/matching.js +537 -0
- package/dist/models.d.ts +17 -4
- package/dist/models.js +359 -29
- package/dist/pilot-cases.d.ts +171 -0
- package/dist/pilot-cases.js +330 -0
- package/dist/pilot-input.d.ts +16 -0
- package/dist/pilot-input.js +125 -0
- package/dist/pilot-recall.d.ts +166 -0
- package/dist/pilot-recall.js +231 -0
- package/dist/pilot-report.d.ts +113 -0
- package/dist/pilot-report.js +279 -0
- package/dist/pilot-responses.d.ts +111 -0
- package/dist/pilot-responses.js +159 -0
- package/dist/python-version.d.ts +3 -0
- package/dist/python-version.js +30 -0
- package/dist/rehearsal.d.ts +440 -0
- package/dist/rehearsal.js +372 -0
- package/dist/sbom.d.ts +13 -0
- package/dist/sbom.js +193 -0
- package/dist/source-error.d.ts +17 -1
- package/dist/source-error.js +18 -3
- package/dist/sources.d.ts +2 -2
- package/dist/sources.js +699 -45
- package/dist/store.d.ts +38 -13
- package/dist/store.js +325 -23
- package/dist/types.d.ts +159 -2
- package/dist/util.d.ts +23 -0
- package/dist/util.js +81 -1
- package/dist/vendor/cvss4-reference.d.ts +2 -0
- package/dist/vendor/cvss4-reference.js +789 -0
- package/dist/word-filter.d.ts +3 -0
- package/dist/word-filter.js +3291 -0
- package/docs/alert-outcome-evaluation.md +150 -0
- package/docs/compatibility.md +35 -0
- package/docs/configuration.md +289 -29
- package/docs/cvss-v4-support.md +41 -0
- package/docs/offline-advisory-preview.md +114 -0
- package/docs/offline-rehearsal.md +44 -0
- package/docs/pilot-cases.md +58 -0
- package/docs/pilot-recall.md +124 -0
- package/docs/pilot-report.md +52 -0
- package/docs/validation/alert-outcome-evaluation-2026-11-05.json +2935 -0
- package/docs/validation/cvss-v4-reference-parity-2026-11-05.json +93 -0
- package/docs/validation/rehearsal-accepted-2026-11-05.json +175 -0
- package/docs/validation/rehearsal-unknown-2026-11-05.json +175 -0
- package/docs/vendor/cvss-v4-LICENSE.txt +22 -0
- package/docs/vendor/cvss-v4-provenance.json +38 -0
- package/examples/agent-advisories-2026.json +145 -0
- package/examples/agent-advisory-cases-2026.json +397 -0
- package/examples/libraries.example.txt +4 -2
- package/examples/libraries.example.yaml +2 -0
- package/examples/offline-advisory-preview.mjs +42 -0
- package/examples/rehearsal.json +75 -0
- package/examples/systems.example.yaml +4 -0
- package/package.json +24 -7
- package/docs/quality-review.md +0 -104
package/dist/types.d.ts
CHANGED
|
@@ -4,22 +4,88 @@ export type Severity = 'unknown' | 'low' | 'medium' | 'high' | 'critical';
|
|
|
4
4
|
export type Category = 'library' | 'product' | 'news' | 'malware' | 'health';
|
|
5
5
|
export type Applicability = 'affected' | 'not_affected' | 'unknown' | 'conflicting' | 'relevant';
|
|
6
6
|
export type Channel = 'slack' | 'email';
|
|
7
|
-
export interface
|
|
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 {
|
|
8
58
|
id: string;
|
|
9
59
|
ecosystem: Ecosystem;
|
|
10
60
|
name: string;
|
|
11
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;
|
|
12
64
|
}
|
|
13
65
|
export interface Product {
|
|
14
66
|
name: string;
|
|
15
67
|
version?: string;
|
|
16
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;
|
|
17
79
|
}
|
|
18
|
-
export interface System {
|
|
80
|
+
export interface System extends InventoryEvidence {
|
|
19
81
|
id: string;
|
|
20
82
|
name: string;
|
|
21
83
|
environment: string;
|
|
22
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[];
|
|
23
89
|
exposure: 'internet' | 'internal' | 'unknown';
|
|
24
90
|
criticality: Severity;
|
|
25
91
|
updated_at: string;
|
|
@@ -29,6 +95,9 @@ export interface System {
|
|
|
29
95
|
}
|
|
30
96
|
export interface Inventory {
|
|
31
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[];
|
|
32
101
|
}
|
|
33
102
|
export interface PackageClaim {
|
|
34
103
|
ecosystem: string;
|
|
@@ -38,6 +107,14 @@ export interface PackageClaim {
|
|
|
38
107
|
matched_versions?: string[];
|
|
39
108
|
fixed?: string;
|
|
40
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
|
+
}
|
|
41
118
|
export interface Evidence {
|
|
42
119
|
id: string;
|
|
43
120
|
source: string;
|
|
@@ -49,6 +126,11 @@ export interface Evidence {
|
|
|
49
126
|
content_status?: 'full' | 'excerpt' | 'unavailable';
|
|
50
127
|
aliases: string[];
|
|
51
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
|
+
};
|
|
52
134
|
modified_at: string;
|
|
53
135
|
fetched_at: string;
|
|
54
136
|
published_at?: string;
|
|
@@ -59,6 +141,8 @@ export interface Evidence {
|
|
|
59
141
|
}
|
|
60
142
|
export interface ModelSpec {
|
|
61
143
|
provider: ModelProvider;
|
|
144
|
+
/** Minimal identifiers by default. Extra deployment context requires explicit opt-in per model. */
|
|
145
|
+
disclosure_profile?: 'minimal' | 'deployment-context';
|
|
62
146
|
output_format?: OutputFormat;
|
|
63
147
|
model: string;
|
|
64
148
|
api_key_env: string;
|
|
@@ -103,6 +187,10 @@ export interface Config {
|
|
|
103
187
|
urgent_unknown: boolean;
|
|
104
188
|
exploited_override: boolean;
|
|
105
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;
|
|
106
194
|
health_channels: Channel[];
|
|
107
195
|
};
|
|
108
196
|
notifications: {
|
|
@@ -147,8 +235,17 @@ export interface Rule {
|
|
|
147
235
|
export interface Assessment {
|
|
148
236
|
/** Only news evidence supports this candidate; AI impact remains a separate claim. */
|
|
149
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;
|
|
150
241
|
/** News-only or generic product context without deterministic package applicability. */
|
|
151
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;
|
|
152
249
|
active?: boolean;
|
|
153
250
|
id: string;
|
|
154
251
|
event_id: string;
|
|
@@ -177,7 +274,26 @@ export interface Assessment {
|
|
|
177
274
|
status: 'complete' | 'unavailable' | 'conflicting';
|
|
178
275
|
};
|
|
179
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
|
+
}
|
|
180
294
|
export interface Decision {
|
|
295
|
+
/** A durable local review is required even when channels is empty. */
|
|
296
|
+
review?: AiReviewReason;
|
|
181
297
|
action: 'notify' | 'digest' | 'record';
|
|
182
298
|
channels: Channel[];
|
|
183
299
|
reason: string;
|
|
@@ -185,11 +301,15 @@ export interface Decision {
|
|
|
185
301
|
export interface Notification {
|
|
186
302
|
id: string;
|
|
187
303
|
finding_id: string;
|
|
304
|
+
/** The persisted finding revision selected when this delivery was enqueued. */
|
|
305
|
+
revision?: number;
|
|
188
306
|
channel: Channel;
|
|
189
307
|
payload: string;
|
|
190
308
|
status: 'pending' | 'sending' | 'accepted' | 'unknown' | 'failed';
|
|
191
309
|
attempts: number;
|
|
192
310
|
available_at: number;
|
|
311
|
+
/** Local UTC time in milliseconds when the destination acknowledged this notification. */
|
|
312
|
+
accepted_at?: number;
|
|
193
313
|
last_error?: string;
|
|
194
314
|
}
|
|
195
315
|
export interface HttpOptions {
|
|
@@ -212,6 +332,42 @@ export interface HttpResult {
|
|
|
212
332
|
body: string;
|
|
213
333
|
}
|
|
214
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
|
+
}
|
|
215
371
|
export interface SourceResult {
|
|
216
372
|
evidence: Evidence[];
|
|
217
373
|
errors: {
|
|
@@ -219,6 +375,7 @@ export interface SourceResult {
|
|
|
219
375
|
message: string;
|
|
220
376
|
}[];
|
|
221
377
|
checkpoints: Record<string, string>;
|
|
378
|
+
coverage: CoverageReceipt[];
|
|
222
379
|
}
|
|
223
380
|
export interface AiAnalysis {
|
|
224
381
|
relevance: 'relevant' | 'irrelevant' | 'unknown';
|
package/dist/util.d.ts
CHANGED
|
@@ -4,3 +4,26 @@ export declare function highest(...values: Severity[]): Severity;
|
|
|
4
4
|
export declare function hash(value: unknown): string;
|
|
5
5
|
export declare function timestamp(now?: number): string;
|
|
6
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
CHANGED
|
@@ -10,5 +10,85 @@ export function timestamp(now = Date.now()) {
|
|
|
10
10
|
return new Date(now).toISOString();
|
|
11
11
|
}
|
|
12
12
|
export function cleanText(value, max = 4000) {
|
|
13
|
-
return value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, '')
|
|
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');
|
|
14
94
|
}
|