screw-bugs 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +906 -2
- package/bin/screw-bugs.js +29 -0
- package/dist/browser-playwright/annotate.d.ts +9 -0
- package/dist/browser-playwright/annotate.js +86 -0
- package/dist/browser-playwright/capture.d.ts +232 -0
- package/dist/browser-playwright/capture.js +1049 -0
- package/dist/browser-playwright/frames.d.ts +77 -0
- package/dist/browser-playwright/frames.js +191 -0
- package/dist/browser-playwright/index.d.ts +2 -0
- package/dist/browser-playwright/index.js +2 -0
- package/dist/browser-playwright/input.d.ts +44 -0
- package/dist/browser-playwright/input.js +116 -0
- package/dist/browser-playwright/launch.d.ts +21 -0
- package/dist/browser-playwright/launch.js +86 -0
- package/dist/browser-playwright/page.d.ts +387 -0
- package/dist/browser-playwright/page.js +1930 -0
- package/dist/build-info.json +5 -0
- package/dist/cli/app.d.ts +19 -0
- package/dist/cli/app.js +70 -0
- package/dist/cli/build-info.d.ts +52 -0
- package/dist/cli/build-info.js +82 -0
- package/dist/cli/commands.d.ts +14 -0
- package/dist/cli/commands.js +308 -0
- package/dist/cli/config.d.ts +16 -0
- package/dist/cli/config.js +71 -0
- package/dist/cli/doctor.d.ts +19 -0
- package/dist/cli/doctor.js +113 -0
- package/dist/cli/environment.d.ts +20 -0
- package/dist/cli/environment.js +34 -0
- package/dist/cli/exit-codes.d.ts +29 -0
- package/dist/cli/exit-codes.js +28 -0
- package/dist/cli/failure.d.ts +20 -0
- package/dist/cli/failure.js +15 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.js +6 -0
- package/dist/cli/install-method.d.ts +96 -0
- package/dist/cli/install-method.js +148 -0
- package/dist/cli/main.d.ts +1 -0
- package/dist/cli/main.js +10 -0
- package/dist/cli/mcp.d.ts +30 -0
- package/dist/cli/mcp.js +309 -0
- package/dist/cli/node-version.d.ts +3 -0
- package/dist/cli/node-version.js +22 -0
- package/dist/cli/operations.d.ts +127 -0
- package/dist/cli/operations.js +337 -0
- package/dist/cli/package-info.d.ts +22 -0
- package/dist/cli/package-info.js +25 -0
- package/dist/cli/report-server.d.ts +170 -0
- package/dist/cli/report-server.js +387 -0
- package/dist/cli/signals.d.ts +37 -0
- package/dist/cli/signals.js +61 -0
- package/dist/cli/stamp-build.d.ts +1 -0
- package/dist/cli/stamp-build.js +9 -0
- package/dist/cli/update-check.d.ts +140 -0
- package/dist/cli/update-check.js +316 -0
- package/dist/cli/uploads.d.ts +16 -0
- package/dist/cli/uploads.js +80 -0
- package/dist/core/css.d.ts +95 -0
- package/dist/core/css.js +436 -0
- package/dist/core/engine.d.ts +65 -0
- package/dist/core/engine.js +436 -0
- package/dist/core/errors.d.ts +66 -0
- package/dist/core/errors.js +30 -0
- package/dist/core/evidence.d.ts +14 -0
- package/dist/core/evidence.js +127 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/index.js +12 -0
- package/dist/core/labels.d.ts +47 -0
- package/dist/core/labels.js +199 -0
- package/dist/core/liveness.d.ts +45 -0
- package/dist/core/liveness.js +64 -0
- package/dist/core/marks.d.ts +63 -0
- package/dist/core/marks.js +410 -0
- package/dist/core/services.d.ts +444 -0
- package/dist/core/services.js +22 -0
- package/dist/core/steps.d.ts +96 -0
- package/dist/core/steps.js +656 -0
- package/dist/core/uploads.d.ts +49 -0
- package/dist/core/uploads.js +133 -0
- package/dist/core/video.d.ts +16 -0
- package/dist/core/video.js +101 -0
- package/dist/core/viewport.d.ts +28 -0
- package/dist/core/viewport.js +50 -0
- package/dist/report/html.d.ts +37 -0
- package/dist/report/html.js +762 -0
- package/dist/report/index.d.ts +5 -0
- package/dist/report/index.js +5 -0
- package/dist/report/markup.d.ts +27 -0
- package/dist/report/markup.js +75 -0
- package/dist/report/pr-summary.d.ts +11 -0
- package/dist/report/pr-summary.js +110 -0
- package/dist/report/sha256.d.ts +9 -0
- package/dist/report/sha256.js +97 -0
- package/dist/report/styles.d.ts +1 -0
- package/dist/report/styles.js +328 -0
- package/dist/report/view.d.ts +141 -0
- package/dist/report/view.js +368 -0
- package/dist/report/walkthrough.d.ts +75 -0
- package/dist/report/walkthrough.js +458 -0
- package/dist/schema/config.d.ts +7 -0
- package/dist/schema/config.js +6 -0
- package/dist/schema/doctor.d.ts +46 -0
- package/dist/schema/doctor.js +23 -0
- package/dist/schema/evidence.d.ts +994 -0
- package/dist/schema/evidence.js +521 -0
- package/dist/schema/findings.d.ts +159 -0
- package/dist/schema/findings.js +80 -0
- package/dist/schema/index.d.ts +7 -0
- package/dist/schema/index.js +7 -0
- package/dist/schema/job.d.ts +1724 -0
- package/dist/schema/job.js +505 -0
- package/dist/schema/ledger.d.ts +951 -0
- package/dist/schema/ledger.js +193 -0
- package/dist/schema/status.d.ts +7 -0
- package/dist/schema/status.js +11 -0
- package/dist/store-local/accounts.d.ts +7 -0
- package/dist/store-local/accounts.js +9 -0
- package/dist/store-local/index.d.ts +3 -0
- package/dist/store-local/index.js +3 -0
- package/dist/store-local/paths.d.ts +10 -0
- package/dist/store-local/paths.js +17 -0
- package/dist/store-local/storage.d.ts +20 -0
- package/dist/store-local/storage.js +170 -0
- package/package.json +73 -4
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CaseStatus } from '../schema/index.js';
|
|
2
|
+
import { type TextPart } from './view.js';
|
|
3
|
+
/** Markup that is already safe. Everything else that goes into the page is escaped. */
|
|
4
|
+
export declare class Markup {
|
|
5
|
+
readonly value: string;
|
|
6
|
+
constructor(value: string);
|
|
7
|
+
toString(): string;
|
|
8
|
+
}
|
|
9
|
+
export declare const escapeHtml: (text: string) => string;
|
|
10
|
+
export type Value = Markup | string | number | boolean | null | undefined | ReadonlyArray<Value>;
|
|
11
|
+
export declare const render: (value: Value) => string;
|
|
12
|
+
/** A template whose interpolations are escaped unless they are markup themselves. */
|
|
13
|
+
export declare const html: (strings: TemplateStringsArray, ...values: ReadonlyArray<Value>) => Markup;
|
|
14
|
+
/**
|
|
15
|
+
* Paths from the ledger should look like `evidence/01/02-name.png` or `video/session-1.webm`.
|
|
16
|
+
* Anything else (absolute paths, `..`, URL schemes) is not linked, so a damaged ledger cannot
|
|
17
|
+
* point the page elsewhere.
|
|
18
|
+
*/
|
|
19
|
+
export declare const safeRelative: (path: string | undefined) => string | undefined;
|
|
20
|
+
export declare const icons: Record<CaseStatus | 'done' | 'skipped' | 'pen' | 'chip' | 'play', Markup>;
|
|
21
|
+
export declare const icon: (kind: keyof typeof icons, extraClass?: string) => Markup;
|
|
22
|
+
export declare const badge: (status: CaseStatus) => Markup;
|
|
23
|
+
/** Who wrote a part of the page: the agent's write-up, or code that ran the browser. */
|
|
24
|
+
export declare const byAgent: Markup;
|
|
25
|
+
export declare const byCode: Markup;
|
|
26
|
+
export declare const textParts: (parts: ReadonlyArray<TextPart>) => Markup[];
|
|
27
|
+
export declare const themeSwitch: Markup;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { statusLabel } from './view.js';
|
|
2
|
+
/*
|
|
3
|
+
* The pieces both HTML pages (the report and the walkthrough) are built from: escaped templates,
|
|
4
|
+
* safe relative links, status icons and badges, and the theme switch.
|
|
5
|
+
*/
|
|
6
|
+
/** Markup that is already safe. Everything else that goes into the page is escaped. */
|
|
7
|
+
export class Markup {
|
|
8
|
+
value;
|
|
9
|
+
constructor(value) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
toString() {
|
|
13
|
+
return this.value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export const escapeHtml = (text) => text
|
|
17
|
+
.replaceAll('&', '&')
|
|
18
|
+
.replaceAll('<', '<')
|
|
19
|
+
.replaceAll('>', '>')
|
|
20
|
+
.replaceAll('"', '"')
|
|
21
|
+
.replaceAll("'", ''');
|
|
22
|
+
export const render = (value) => {
|
|
23
|
+
if (value instanceof Markup)
|
|
24
|
+
return value.value;
|
|
25
|
+
if (Array.isArray(value))
|
|
26
|
+
return value.map(render).join('');
|
|
27
|
+
if (value === undefined || value === null || value === false)
|
|
28
|
+
return '';
|
|
29
|
+
return escapeHtml(String(value));
|
|
30
|
+
};
|
|
31
|
+
/** A template whose interpolations are escaped unless they are markup themselves. */
|
|
32
|
+
export const html = (strings, ...values) => new Markup(strings.reduce((out, string, i) => out + render(values[i - 1]) + string));
|
|
33
|
+
/**
|
|
34
|
+
* Paths from the ledger should look like `evidence/01/02-name.png` or `video/session-1.webm`.
|
|
35
|
+
* Anything else (absolute paths, `..`, URL schemes) is not linked, so a damaged ledger cannot
|
|
36
|
+
* point the page elsewhere.
|
|
37
|
+
*/
|
|
38
|
+
export const safeRelative = (path) => path !== undefined &&
|
|
39
|
+
/^[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*$/.test(path) &&
|
|
40
|
+
!path.split('/').includes('..')
|
|
41
|
+
? path
|
|
42
|
+
: undefined;
|
|
43
|
+
/* One distinct shape per status, so a status never depends on color alone. */
|
|
44
|
+
export const icons = {
|
|
45
|
+
passed: html `<path d="M4.5 8.5l2.5 2.5 5-6" />`,
|
|
46
|
+
failed: html `<path d="M5 5l6 6M11 5l-6 6" />`,
|
|
47
|
+
blocked: html `<path d="M4 8h8" />`,
|
|
48
|
+
not_verified: html `<circle cx="8" cy="8" r="4.5" stroke-dasharray="2 2.2" />`,
|
|
49
|
+
done: html `<path d="M4.5 8.5l2.5 2.5 5-6" />`,
|
|
50
|
+
skipped: html ``,
|
|
51
|
+
pen: html `<path d="M3 13l1-3.5 6.5-6.5 2.5 2.5-6.5 6.5zM9.5 4l2.5 2.5" />`,
|
|
52
|
+
chip: html `<rect x="3.5" y="3.5" width="9" height="9" rx="1.5" /><path d="M6 6.5h4M6 9.5h4" />`,
|
|
53
|
+
play: html `<path d="M5 3.5l7.5 4.5-7.5 4.5z" />`,
|
|
54
|
+
};
|
|
55
|
+
export const icon = (kind, extraClass = '') => html `<svg class="icon ${extraClass}" viewBox="0 0 16 16" aria-hidden="true">${icons[kind]}</svg>`;
|
|
56
|
+
export const badge = (status) => html `<span class="badge s-${status}">${icon(status)}<span>${statusLabel[status]}</span></span>`;
|
|
57
|
+
/** Who wrote a part of the page: the agent's write-up, or code that ran the browser. */
|
|
58
|
+
export const byAgent = html `<span class="by by-agent">${icon('pen')}Agent's write-up</span>`;
|
|
59
|
+
export const byCode = html `<span class="by by-code">${icon('chip')}Decided by code</span>`;
|
|
60
|
+
export const textParts = (parts) => parts.map((part) => part.kind === 'text'
|
|
61
|
+
? html `${part.text}`
|
|
62
|
+
: part.kind === 'value'
|
|
63
|
+
? html `<q class="value">${part.text}</q>`
|
|
64
|
+
: html `<code>${part.text}</code>`);
|
|
65
|
+
export const themeSwitch = html `<div
|
|
66
|
+
class="theme"
|
|
67
|
+
id="theme"
|
|
68
|
+
role="group"
|
|
69
|
+
aria-label="Color theme"
|
|
70
|
+
hidden
|
|
71
|
+
>
|
|
72
|
+
<button type="button" data-theme-set="light" aria-pressed="false">Light</button
|
|
73
|
+
><button type="button" data-theme-set="dark" aria-pressed="false">Dark</button
|
|
74
|
+
><button type="button" data-theme-set="system" aria-pressed="true">System</button>
|
|
75
|
+
</div>`;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ReportBundle } from '../schema/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* A short GitHub-flavored Markdown summary of a run, to paste into (or post on) a pull request:
|
|
4
|
+
* the headline (the agent's, when it wrote one up, else the code's verdict), the counts, what to
|
|
5
|
+
* fix, the cases that did not pass with where their failure is in the video, the video and the
|
|
6
|
+
* screenshots with the agent's highlights drawn in, and where the full report is. Paths are
|
|
7
|
+
* relative to the project root; `reportPath` defaults to the report's.
|
|
8
|
+
*/
|
|
9
|
+
export declare const renderPrSummary: (bundle: ReportBundle, options?: {
|
|
10
|
+
readonly reportPath?: string;
|
|
11
|
+
}) => string;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { countsOf, fixesOf, formatTimecode, notTestedCount, severityLabel, statuses, statusLabel, videoOf, } from './view.js';
|
|
2
|
+
const mark = {
|
|
3
|
+
passed: '✅',
|
|
4
|
+
failed: '❌',
|
|
5
|
+
blocked: '⚠️',
|
|
6
|
+
not_verified: '➖',
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Free text as inert Markdown on one line: no tables broken by `|`, no HTML, links, emphasis or
|
|
10
|
+
* headings from page-derived or user-written text.
|
|
11
|
+
*/
|
|
12
|
+
const inline = (text) => text
|
|
13
|
+
.replace(/\s+/g, ' ')
|
|
14
|
+
.trim()
|
|
15
|
+
.replace(/[\\`*_[\]<>|#~!]/g, (ch) => `\\${ch}`)
|
|
16
|
+
.replaceAll('&', '&');
|
|
17
|
+
/** A path as inline code; backticks cannot occur in run paths, but are dropped to be sure. */
|
|
18
|
+
const code = (path) => `\`${path.replaceAll('`', '')}\``;
|
|
19
|
+
/**
|
|
20
|
+
* A short GitHub-flavored Markdown summary of a run, to paste into (or post on) a pull request:
|
|
21
|
+
* the headline (the agent's, when it wrote one up, else the code's verdict), the counts, what to
|
|
22
|
+
* fix, the cases that did not pass with where their failure is in the video, the video and the
|
|
23
|
+
* screenshots with the agent's highlights drawn in, and where the full report is. Paths are
|
|
24
|
+
* relative to the project root; `reportPath` defaults to the report's.
|
|
25
|
+
*/
|
|
26
|
+
export const renderPrSummary = (bundle, options = {}) => {
|
|
27
|
+
const { ledger, findings } = bundle;
|
|
28
|
+
const counts = countsOf(ledger.cases);
|
|
29
|
+
const total = ledger.cases.length;
|
|
30
|
+
const ended = ledger.state === 'finished' || ledger.state === 'errored';
|
|
31
|
+
const allPassed = ledger.state === 'finished' && counts.passed === total;
|
|
32
|
+
const mood = !ended ? '🕒' : allPassed ? mark.passed : mark.failed;
|
|
33
|
+
const gaps = notTestedCount(bundle);
|
|
34
|
+
const verdict = !ended
|
|
35
|
+
? `still running, ${counts.passed} of ${total} passed so far`
|
|
36
|
+
: allPassed && gaps === undefined
|
|
37
|
+
? `${total === 1 ? 'the case' : `all ${total} cases`} passed`
|
|
38
|
+
: `${counts.passed} of ${total} ${total === 1 ? 'case' : 'cases'} passed${gaps ? ` · ${gaps}` : ''}`;
|
|
39
|
+
const notTested = findings?.notTested ?? [];
|
|
40
|
+
const headline = findings ? inline(findings.headline) : verdict;
|
|
41
|
+
const notPassing = ledger.cases.filter((c) => c.status !== 'passed');
|
|
42
|
+
const runDir = `.screw-bugs/runs/${bundle.jobId}`;
|
|
43
|
+
const reportPath = options.reportPath ?? `${runDir}/${bundle.files.html ?? 'report.html'}`;
|
|
44
|
+
const sessions = ledger.sessions ?? [];
|
|
45
|
+
const fixes = fixesOf(bundle);
|
|
46
|
+
const annotated = Object.entries(bundle.annotated ?? {});
|
|
47
|
+
const caseOfShot = new Map(ledger.cases.flatMap((c) => (c.shots ?? []).map((shot) => [shot.id, c.id])));
|
|
48
|
+
/** Where a case was decided in its session's video. */
|
|
49
|
+
const at = (c) => {
|
|
50
|
+
const video = videoOf(bundle, c);
|
|
51
|
+
if (video?.path === undefined)
|
|
52
|
+
return '—';
|
|
53
|
+
const decided = c.steps?.find((s) => s.index === c.step)?.startMs ?? c.startMs;
|
|
54
|
+
return decided === undefined ? '—' : formatTimecode(decided);
|
|
55
|
+
};
|
|
56
|
+
return [
|
|
57
|
+
`### ${mood} screw-bugs: ${headline}`,
|
|
58
|
+
'',
|
|
59
|
+
...(findings
|
|
60
|
+
? [
|
|
61
|
+
`**Code decided:** ${verdict}. _The ${notTested.length > 0 ? 'headline, fixes and what was not tested' : 'headline and fixes'} are the agent's write-up._`,
|
|
62
|
+
'',
|
|
63
|
+
]
|
|
64
|
+
: []),
|
|
65
|
+
`${inline(bundle.summary.text)}${bundle.summary.derived ? ' _(summarized from case titles)_' : ''}`,
|
|
66
|
+
'',
|
|
67
|
+
statuses.map((s) => `${mark[s]} ${counts[s]} ${statusLabel[s].toLowerCase()}`).join(' · '),
|
|
68
|
+
...(ledger.error ? ['', `**The run stopped early:** ${inline(ledger.error)}`] : []),
|
|
69
|
+
...(notTested.length > 0
|
|
70
|
+
? [
|
|
71
|
+
'',
|
|
72
|
+
"#### Not tested _(agent's write-up)_",
|
|
73
|
+
'',
|
|
74
|
+
...notTested.map((item) => `- **${inline(item.what)}:** ${inline(item.why)}`),
|
|
75
|
+
]
|
|
76
|
+
: []),
|
|
77
|
+
...(fixes.length > 0
|
|
78
|
+
? [
|
|
79
|
+
'',
|
|
80
|
+
'#### What to fix',
|
|
81
|
+
'',
|
|
82
|
+
...fixes.map((fix, i) => `${i + 1}. **${severityLabel[fix.severity]}: ${inline(fix.title)}** ${inline(fix.why)} ` +
|
|
83
|
+
`(${fix.caseIds.map((id) => code(id)).join(', ')})`),
|
|
84
|
+
]
|
|
85
|
+
: []),
|
|
86
|
+
...(notPassing.length > 0
|
|
87
|
+
? [
|
|
88
|
+
'',
|
|
89
|
+
'| Case | Status | Reason | Video at |',
|
|
90
|
+
'| --- | --- | --- | --- |',
|
|
91
|
+
...notPassing.map((c) => `| ${inline(c.title)} | ${mark[c.status]} ${statusLabel[c.status]} | ${c.reason ? inline(c.reason) : '—'} | ${at(c)} |`),
|
|
92
|
+
]
|
|
93
|
+
: []),
|
|
94
|
+
'',
|
|
95
|
+
...sessions.map((session) => session.video !== undefined && session.videoProblem === undefined
|
|
96
|
+
? `Video: ${code(`${runDir}/${session.video}`)}`
|
|
97
|
+
: `Video missing or incomplete${session.video ? ` (${code(`${runDir}/${session.video}`)})` : ''}: ${inline(session.videoProblem ?? 'the video file was not saved.')}`),
|
|
98
|
+
...(sessions.length > 0 ? [''] : []),
|
|
99
|
+
...(annotated.length > 0
|
|
100
|
+
? [
|
|
101
|
+
'Screenshots with highlights drawn in:',
|
|
102
|
+
...annotated.map(([id, path]) => `- ${code(`${runDir}/${path}`)} (case ${code(caseOfShot.get(id) ?? '?')})`),
|
|
103
|
+
'',
|
|
104
|
+
]
|
|
105
|
+
: []),
|
|
106
|
+
`Target: ${inline(bundle.spec.url)} (${inline(bundle.device.label)}) · Job \`${bundle.jobId}\``,
|
|
107
|
+
`Full report with video, steps and screenshots: ${code(reportPath)}`,
|
|
108
|
+
'',
|
|
109
|
+
].join('\n');
|
|
110
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The SHA-256 digest of `bytes`. */
|
|
2
|
+
export declare const sha256: (bytes: Uint8Array) => Uint8Array;
|
|
3
|
+
/** HMAC-SHA256 (RFC 2104) of `message` under `key`. */
|
|
4
|
+
export declare const hmacSha256: (key: Uint8Array, message: Uint8Array) => Uint8Array;
|
|
5
|
+
/** Bytes as lowercase hex. */
|
|
6
|
+
export declare const hex: (bytes: Uint8Array) => string;
|
|
7
|
+
export declare const base64: (bytes: Uint8Array) => string;
|
|
8
|
+
/** A CSP source that allows exactly this inline script: `'sha256-…'`. */
|
|
9
|
+
export declare const cspHash: (script: string) => string;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SHA-256 (and HMAC-SHA256) in plain code, for the hash of the report's one inline script in its
|
|
3
|
+
* Content Security Policy, and the report server's identity proof. The report module stays free
|
|
4
|
+
* of Node, and the renderer is synchronous, so neither `node:crypto` nor Web Crypto (which is
|
|
5
|
+
* async) fits.
|
|
6
|
+
*/
|
|
7
|
+
const K = Uint32Array.from([
|
|
8
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
9
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
10
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
11
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
12
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
13
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
14
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
15
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
16
|
+
]);
|
|
17
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
18
|
+
/** The SHA-256 digest of `bytes`. */
|
|
19
|
+
export const sha256 = (bytes) => {
|
|
20
|
+
const length = bytes.length;
|
|
21
|
+
const padded = new Uint8Array(Math.ceil((length + 9) / 64) * 64);
|
|
22
|
+
padded.set(bytes);
|
|
23
|
+
padded[length] = 0x80;
|
|
24
|
+
const view = new DataView(padded.buffer);
|
|
25
|
+
view.setUint32(padded.length - 8, Math.floor((length * 8) / 2 ** 32));
|
|
26
|
+
view.setUint32(padded.length - 4, (length * 8) >>> 0);
|
|
27
|
+
const h = Uint32Array.from([
|
|
28
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
29
|
+
]);
|
|
30
|
+
const w = new Uint32Array(64);
|
|
31
|
+
for (let block = 0; block < padded.length; block += 64) {
|
|
32
|
+
for (let i = 0; i < 16; i++)
|
|
33
|
+
w[i] = view.getUint32(block + i * 4);
|
|
34
|
+
for (let i = 16; i < 64; i++) {
|
|
35
|
+
const a = w[i - 15];
|
|
36
|
+
const b = w[i - 2];
|
|
37
|
+
const s0 = rotr(a, 7) ^ rotr(a, 18) ^ (a >>> 3);
|
|
38
|
+
const s1 = rotr(b, 17) ^ rotr(b, 19) ^ (b >>> 10);
|
|
39
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
40
|
+
}
|
|
41
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
42
|
+
for (let i = 0; i < 64; i++) {
|
|
43
|
+
const t1 = (hh + (rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]) >>>
|
|
44
|
+
0;
|
|
45
|
+
const t2 = ((rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
46
|
+
hh = g;
|
|
47
|
+
g = f;
|
|
48
|
+
f = e;
|
|
49
|
+
e = (d + t1) >>> 0;
|
|
50
|
+
d = c;
|
|
51
|
+
c = b;
|
|
52
|
+
b = a;
|
|
53
|
+
a = (t1 + t2) >>> 0;
|
|
54
|
+
}
|
|
55
|
+
h[0] = (h[0] + a) >>> 0;
|
|
56
|
+
h[1] = (h[1] + b) >>> 0;
|
|
57
|
+
h[2] = (h[2] + c) >>> 0;
|
|
58
|
+
h[3] = (h[3] + d) >>> 0;
|
|
59
|
+
h[4] = (h[4] + e) >>> 0;
|
|
60
|
+
h[5] = (h[5] + f) >>> 0;
|
|
61
|
+
h[6] = (h[6] + g) >>> 0;
|
|
62
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
63
|
+
}
|
|
64
|
+
const out = new Uint8Array(32);
|
|
65
|
+
const outView = new DataView(out.buffer);
|
|
66
|
+
h.forEach((word, i) => outView.setUint32(i * 4, word));
|
|
67
|
+
return out;
|
|
68
|
+
};
|
|
69
|
+
/** HMAC-SHA256 (RFC 2104) of `message` under `key`. */
|
|
70
|
+
export const hmacSha256 = (key, message) => {
|
|
71
|
+
const block = new Uint8Array(64);
|
|
72
|
+
block.set(key.length > 64 ? sha256(key) : key);
|
|
73
|
+
const pad = (byte) => block.map((b) => b ^ byte);
|
|
74
|
+
const inner = new Uint8Array(64 + message.length);
|
|
75
|
+
inner.set(pad(0x36));
|
|
76
|
+
inner.set(message, 64);
|
|
77
|
+
const outer = new Uint8Array(64 + 32);
|
|
78
|
+
outer.set(pad(0x5c));
|
|
79
|
+
outer.set(sha256(inner), 64);
|
|
80
|
+
return sha256(outer);
|
|
81
|
+
};
|
|
82
|
+
/** Bytes as lowercase hex. */
|
|
83
|
+
export const hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
84
|
+
const BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
85
|
+
export const base64 = (bytes) => {
|
|
86
|
+
let out = '';
|
|
87
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
88
|
+
const [a = 0, b = 0, c = 0] = [bytes[i], bytes[i + 1], bytes[i + 2]];
|
|
89
|
+
const n = (a << 16) | (b << 8) | c;
|
|
90
|
+
out += BASE64[(n >> 18) & 63] + BASE64[(n >> 12) & 63];
|
|
91
|
+
out += i + 1 < bytes.length ? BASE64[(n >> 6) & 63] : '=';
|
|
92
|
+
out += i + 2 < bytes.length ? BASE64[n & 63] : '=';
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
};
|
|
96
|
+
/** A CSP source that allows exactly this inline script: `'sha256-…'`. */
|
|
97
|
+
export const cspHash = (script) => `'sha256-${base64(sha256(new TextEncoder().encode(script)))}'`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const styles: string;
|