@terrafirmagreg-team/data 1.2.1 → 1.2.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrafirmagreg-team/data",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "type": "module",
5
5
  "description": "Storage drivers and shared helpers",
6
6
  "license": "LGPL-3.0-only",
@@ -1,16 +1,17 @@
1
+ import { createPrivateKey, sign } from 'node:crypto';
1
2
  import { env } from './env';
2
3
  import { githubFetch } from './github';
3
4
 
4
5
  const TOKEN_TTL_MS = 50 * 60 * 1000;
5
6
  const cache = new Map<string, { token: Promise<string>; exp: number }>();
6
7
 
7
- function pemToDer(pem: string): ArrayBuffer {
8
- const b64 = pem.replace(/\\n/g, '\n').replace(/-----(?:BEGIN|END)[^\n-]*-----|\s/g, '');
9
- const bin = Buffer.from(b64, 'base64');
10
- return bin.buffer.slice(
11
- bin.byteOffset,
12
- bin.byteOffset + bin.byteLength,
13
- ) as ArrayBuffer;
8
+ function privateKey(pem: string) {
9
+ const normalized = pem.replace(/\\n/g, '\n');
10
+ if (normalized.includes('-----BEGIN')) return createPrivateKey(normalized);
11
+ return createPrivateKey({
12
+ key: Buffer.from(normalized.trim(), 'base64'),
13
+ format: 'der',
14
+ });
14
15
  }
15
16
 
16
17
  async function appJwt(): Promise<string> {
@@ -22,19 +23,8 @@ async function appJwt(): Promise<string> {
22
23
  const pack = (value: object) =>
23
24
  Buffer.from(JSON.stringify(value)).toString('base64url');
24
25
  const payload = `${pack({ alg: 'RS256', typ: 'JWT' })}.${pack({ iat: now - 60, exp: now + 540, iss: appId })}`;
25
- const key = await crypto.subtle.importKey(
26
- 'pkcs8',
27
- pemToDer(pem),
28
- { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
29
- false,
30
- ['sign'],
31
- );
32
- const signature = await crypto.subtle.sign(
33
- 'RSASSA-PKCS1-v1_5',
34
- key,
35
- new TextEncoder().encode(payload),
36
- );
37
- return `${payload}.${Buffer.from(signature).toString('base64url')}`;
26
+ const signature = sign('sha256', Buffer.from(payload), privateKey(pem));
27
+ return `${payload}.${signature.toString('base64url')}`;
38
28
  }
39
29
 
40
30
  async function mintInstallationToken(repo: string): Promise<string> {
@@ -0,0 +1,91 @@
1
+ export type DumpDatasetState = {
2
+ status: 'ok' | 'failed';
3
+ fails: number;
4
+ lastFail: number;
5
+ };
6
+
7
+ export type DumpStatusSnapshot = {
8
+ statuses: Record<string, DumpDatasetState>;
9
+ retryNonce: number;
10
+ dismissed: boolean;
11
+ };
12
+
13
+ type Store = {
14
+ state: Map<string, DumpDatasetState>;
15
+ listeners: Set<() => void>;
16
+ retryNonce: number;
17
+ dismissedSignature: string;
18
+ snapshot: DumpStatusSnapshot;
19
+ };
20
+
21
+ const store = ((globalThis as Record<symbol, unknown>)[
22
+ Symbol.for('terrafirmagreg.dump-status')
23
+ ] ??= {
24
+ state: new Map(),
25
+ listeners: new Set(),
26
+ retryNonce: 0,
27
+ dismissedSignature: '',
28
+ snapshot: { statuses: {}, retryNonce: 0, dismissed: false },
29
+ }) as Store;
30
+
31
+ function notify() {
32
+ store.snapshot = {
33
+ statuses: Object.fromEntries(store.state),
34
+ retryNonce: store.retryNonce,
35
+ dismissed: dumpFailuresDismissed(),
36
+ };
37
+ for (const listener of store.listeners) listener();
38
+ }
39
+
40
+ export function markDumpResult(id: string, ok: boolean) {
41
+ const prev = store.state.get(id);
42
+ if (ok) {
43
+ if (!prev || prev.status === 'failed') {
44
+ store.state.set(id, { status: 'ok', fails: 0, lastFail: 0 });
45
+ if (prev?.status === 'failed') store.retryNonce += 1;
46
+ notify();
47
+ }
48
+ return;
49
+ }
50
+ const fails = (prev?.fails ?? 0) + 1;
51
+ store.state.set(id, { status: 'failed', fails, lastFail: Date.now() });
52
+ notify();
53
+ }
54
+
55
+ export function dumpStatusSnapshot(): DumpStatusSnapshot {
56
+ return store.snapshot;
57
+ }
58
+
59
+ export function subscribeDumpStatus(listener: () => void): () => void {
60
+ store.listeners.add(listener);
61
+ return () => store.listeners.delete(listener);
62
+ }
63
+
64
+ export function requestDumpRetry() {
65
+ store.retryNonce += 1;
66
+ notify();
67
+ }
68
+
69
+ export function dumpRetryDelay(fails: number): number {
70
+ return fails <= 1 ? 10_000 : fails === 2 ? 30_000 : 60_000;
71
+ }
72
+
73
+ export function dumpFailureSignature(): string {
74
+ const failed = [...store.state.entries()].filter(
75
+ ([, state]) => state.status === 'failed',
76
+ );
77
+ if (!failed.length) return '';
78
+ const ids = failed.map(([id]) => id).sort().join(',');
79
+ const fails = failed.reduce((sum, [, state]) => sum + state.fails, 0);
80
+ return `${ids}#${fails}`;
81
+ }
82
+
83
+ export function dismissDumpFailures() {
84
+ store.dismissedSignature = dumpFailureSignature();
85
+ notify();
86
+ }
87
+
88
+ export function dumpFailuresDismissed(): boolean {
89
+ const signature = dumpFailureSignature();
90
+ return signature !== '' && signature === store.dismissedSignature;
91
+ }