@terrafirmagreg-team/data 1.2.2 → 1.2.4

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.2",
3
+ "version": "1.2.4",
4
4
  "type": "module",
5
5
  "description": "Storage drivers and shared helpers",
6
6
  "license": "LGPL-3.0-only",
@@ -4,6 +4,8 @@ export type AsyncCache<A extends unknown[], V> = ((
4
4
  peek: (...args: A) => V | undefined;
5
5
  };
6
6
 
7
+ const MISS_TTL = 60_000;
8
+
7
9
  export function createAsyncCache<A extends unknown[], V>(
8
10
  loader: (...args: A) => Promise<V>,
9
11
  keyOf: (...args: A) => string = (...args) => JSON.stringify(args),
@@ -29,7 +31,8 @@ export function createAsyncCache<A extends unknown[], V>(
29
31
  pending = loader(...args)
30
32
  .then((value) => {
31
33
  resolved.set(key, value);
32
- if (ttl !== undefined) expiry.set(key, Date.now() + ttl);
34
+ const life = value === undefined ? MISS_TTL : ttl;
35
+ if (life !== undefined) expiry.set(key, Date.now() + life);
33
36
  return value;
34
37
  })
35
38
  .catch((err) => {
@@ -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
+ }
package/src/lib/env.ts CHANGED
@@ -1,3 +1,18 @@
1
1
  export const env = (key: string): string | undefined =>
2
2
  process.env[key]?.trim() || undefined;
3
3
 
4
+ export function dumpStorageBase(): string | undefined {
5
+ const raw = process.env.NEXT_PUBLIC_DUMP_STORAGE_URL?.trim();
6
+ return raw ? raw.replace(/\/+$/, '') : undefined;
7
+ }
8
+
9
+ export function dumpStorageOrigin(): string | undefined {
10
+ const base = dumpStorageBase();
11
+ if (!base) return undefined;
12
+ try {
13
+ return new URL(base).origin;
14
+ } catch {
15
+ return undefined;
16
+ }
17
+ }
18
+