Repository navigation
Expand file tree
/
Copy pathobservationSources.ts
More file actions
153 lines (141 loc) · 5.95 KB
/
Copy pathobservationSources.ts
File metadata and controls
153 lines (141 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { mathDigest } from '../../src/math/mathRuntimeIo';
import type { WebNativeTeam, WebObservation, WebTaskSource, WebTeamProof } from './collaboration';
type SourceReference = { sha256: string };
type TaskSourceWithText<Text> = Omit<WebTaskSource, 'source' | 'dependencySources'> & {
source: Text;
dependencySources?: { file: string; source: Text }[];
};
type ObservationWithSource<Text> = Omit<WebObservation, 'sources'> & { sources: TaskSourceWithText<Text>[] };
export type CompactWebTaskSource = TaskSourceWithText<SourceReference>;
export type CompactWebObservation = ObservationWithSource<SourceReference>;
type TeamProofWithSource<Text> = Omit<WebTeamProof, 'sources' | 'declarations'> & {
sources: { file: string; source: Text }[];
declarations: (Omit<WebTeamProof['declarations'][number], 'checkedSource'> & { checkedSource?: Text })[];
};
type NativeTeamWithSource<Text> = Omit<WebNativeTeam, 'tasks' | 'deliveries' | 'adoptions'> & {
tasks: (Omit<WebNativeTeam['tasks'][number], 'rejection'> & { rejection?: TeamProofWithSource<Text> })[];
deliveries: (Omit<WebNativeTeam['deliveries'][number], 'proof'> & { proof?: TeamProofWithSource<Text> })[];
adoptions: (Omit<WebNativeTeam['adoptions'][number], 'proof'> & { proof?: TeamProofWithSource<Text> })[];
};
/** Older v2 archives kept native team source inline. */
export type ArchivedWebObservation = Omit<CompactWebObservation, 'inspection'> & {
inspection: Omit<WebObservation['inspection'], 'nativeTeam'> & {
nativeTeam?: NativeTeamWithSource<string | SourceReference>;
};
};
export type WorkerObservationFrame = {
type: 'observation';
observation: CompactWebObservation;
sourceBlobs: Record<string, string>;
};
export const maxWorkerObservationBytes = 15 * 1024 * 1024;
/** A standalone frame includes every referenced blob, unlike a frame on an open stream. */
export function standaloneObservationFrame(observation: WebObservation): WorkerObservationFrame {
const sourceBlobs: Record<string, string> = {};
const compact = compactObservation(observation, (digest, source) => {
sourceBlobs[digest] = source;
});
return { type: 'observation', observation: compact, sourceBlobs };
}
function mapSources<From, To>(
observation: ObservationWithSource<From>,
map: (source: From) => To,
): ObservationWithSource<To> {
return {
...observation,
sources: observation.sources.map(({ source, dependencySources, ...metadata }) => ({
...metadata,
source: map(source),
...(dependencySources === undefined
? {}
: {
dependencySources: dependencySources.map(dependency => ({ ...dependency, source: map(dependency.source) })),
}),
})),
};
}
function sourceCompactor(save: (digest: string, text: string) => void) {
const digests = new Map<string, string>();
return (source: string): SourceReference => {
let sha256 = digests.get(source);
if (!sha256) {
sha256 = mathDigest(source);
digests.set(source, sha256);
save(sha256, source);
}
return { sha256 };
};
}
export function compactObservation(
observation: WebObservation,
save: (digest: string, text: string) => void,
): CompactWebObservation {
return mapSources(observation, sourceCompactor(save));
}
function sourceHydrator(read: (digest: string) => string | undefined) {
return (source: string | SourceReference): string => {
if (typeof source === 'string') return source;
if (typeof source?.sha256 !== 'string' || !/^[a-f0-9]{64}$/u.test(source.sha256))
throw new Error('web_observation_source_reference_invalid');
const text = read(source.sha256);
if (text === undefined) throw new Error('web_observation_source_blob_missing');
return text;
};
}
/** Legacy records inline their sources. References must all resolve before handing the observation to a consumer. */
export function hydrateObservation(
observation: WebObservation | CompactWebObservation,
read: (digest: string) => string | undefined,
): WebObservation {
return mapSources(observation, sourceHydrator(read));
}
function mapTeamSources<From, To>(
team: NativeTeamWithSource<From>,
map: (source: From) => To,
): NativeTeamWithSource<To> {
const mapProof = ({ sources, declarations, ...proof }: TeamProofWithSource<From>): TeamProofWithSource<To> => ({
...proof,
sources: sources.map(({ source, ...file }) => ({ ...file, source: map(source) })),
declarations: declarations.map(({ checkedSource, ...declaration }) => ({
...declaration,
...(checkedSource === undefined ? {} : { checkedSource: map(checkedSource) }),
})),
});
return {
...team,
tasks: team.tasks.map(({ rejection, ...task }) => ({
...task,
...(rejection ? { rejection: mapProof(rejection) } : {}),
})),
deliveries: team.deliveries.map(({ proof, ...delivery }) => ({
...delivery,
...(proof ? { proof: mapProof(proof) } : {}),
})),
adoptions: team.adoptions.map(({ proof, ...adoption }) => ({
...adoption,
...(proof ? { proof: mapProof(proof) } : {}),
})),
};
}
/** Share source references across archive frames, including native team proof evidence. */
export function archiveObservationCompactor(save: (digest: string, text: string) => void) {
const reference = sourceCompactor(save);
return (observation: WebObservation): ArchivedWebObservation => {
const { nativeTeam, ...inspection } = observation.inspection;
return {
...mapSources(observation, reference),
inspection: { ...inspection, ...(nativeTeam ? { nativeTeam: mapTeamSources(nativeTeam, reference) } : {}) },
};
};
}
export function hydrateArchiveObservation(
observation: ArchivedWebObservation,
read: (digest: string) => string | undefined,
): WebObservation {
const resolve = sourceHydrator(read);
const { nativeTeam, ...inspection } = observation.inspection;
return {
...mapSources({ ...observation, inspection }, resolve),
inspection: { ...inspection, ...(nativeTeam ? { nativeTeam: mapTeamSources(nativeTeam, resolve) } : {}) },
};
}