Repository navigation
Expand file tree
/
Copy pathmigration-verifier.ts
More file actions
198 lines (185 loc) · 6.55 KB
/
Copy pathmigration-verifier.ts
File metadata and controls
198 lines (185 loc) · 6.55 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/** Isolated verification for a staged or competing current JSONL generation. */
import { Worker } from 'node:worker_threads'
import type { WorkerOptions } from 'node:worker_threads'
import type { JsonlCompression } from './format.ts'
import type { JsonlExpectedPrefix, JsonlVerifiedGeneration } from './generation.ts'
interface VerificationRequest {
readonly path: string
readonly compression: JsonlCompression
readonly expectedId: string
readonly expectedEventCount: number
readonly expectedPrefix?: JsonlExpectedPrefix
}
type VerificationResponse =
| { readonly ok: true; readonly result: JsonlVerifiedGeneration }
| { readonly ok: false; readonly message: string; readonly stack?: string }
/** Process-wide memory bound for full-generation verification isolates. */
const MAX_CONCURRENT_VERIFIERS = 2
class VerificationScheduler {
private active = 0
private readonly waiting: Array<{ grant(): void }> = []
async run<T>(operation: () => Promise<T>, signal?: AbortSignal): Promise<T> {
const permit = this.acquire(signal)
if (permit !== undefined) await permit
try {
signal?.throwIfAborted()
return await operation()
} finally {
this.release()
}
}
private acquire(signal?: AbortSignal): Promise<void> | undefined {
signal?.throwIfAborted()
if (this.active < MAX_CONCURRENT_VERIFIERS) {
this.active += 1
return
}
return new Promise<void>((resolve, reject) => {
const waiter = {
grant: (): void => {
signal?.removeEventListener('abort', abort)
resolve()
},
}
const abort = (): void => {
const index = this.waiting.indexOf(waiter)
this.waiting.splice(index, 1)
reject(verifierAbortError(signal))
}
this.waiting.push(waiter)
signal?.addEventListener('abort', abort, { once: true })
})
}
private release(): void {
const next = this.waiting.shift()
if (next === undefined) {
this.active -= 1
return
}
next.grant()
}
}
const verificationScheduler = new VerificationScheduler()
function workerSpawn(request: VerificationRequest): { readonly entry: string | URL; readonly options: WorkerOptions } {
/* v8 ignore next 3 -- built-worker coverage owns the bundled path. */
if (!import.meta.url.endsWith('.ts')) {
return {
entry: new URL('./worker.cjs', import.meta.url),
options: { workerData: request, execArgv: [] },
}
}
const workerEntry = new URL('./worker.ts', import.meta.url)
const bootstrap = [
`import { register as registerEsm } from ${JSON.stringify(import.meta.resolve('tsx/esm/api'))}`,
`import { register as registerCjs } from ${JSON.stringify(import.meta.resolve('tsx/cjs/api'))}`,
'registerCjs()',
'registerEsm()',
`await import(${JSON.stringify(workerEntry.href)})`,
].join('\n')
return {
entry: new URL(`data:text/javascript,${encodeURIComponent(bootstrap)}`),
options: {
workerData: request,
execArgv: [],
},
}
}
/**
* Verify one current generation in a fresh Worker Thread.
* @param path - staged or competing current-generation path.
* @param compression - configured physical encoding.
* @param expectedId - Session id expected in the decoded header.
* @param expectedEventCount - exact logical event count expected after decoding.
* @param expectedPrefix - verified physical prefix; an append tail may be present and is not validated.
* @param signal - optional cancellation for scheduler wait and Worker execution.
* @returns the worker's physical identity and digest after its natural zero exit.
*/
export function verifyCurrentGenerationInWorker(
path: string,
compression: JsonlCompression,
expectedId: string,
expectedEventCount: number,
expectedPrefix?: JsonlExpectedPrefix,
signal?: AbortSignal,
): Promise<JsonlVerifiedGeneration> {
return verificationScheduler.run(() => runVerificationWorker(
path,
compression,
expectedId,
expectedEventCount,
expectedPrefix,
signal,
), signal)
}
function runVerificationWorker(
path: string,
compression: JsonlCompression,
expectedId: string,
expectedEventCount: number,
expectedPrefix?: JsonlExpectedPrefix,
signal?: AbortSignal,
): Promise<JsonlVerifiedGeneration> {
signal?.throwIfAborted()
const request: VerificationRequest = {
path, compression, expectedId, expectedEventCount,
...(expectedPrefix === undefined ? {} : { expectedPrefix }),
}
const { entry, options } = workerSpawn(request)
const worker = new Worker(entry, options)
return new Promise((resolve, reject) => {
let settled = false
let success: Extract<VerificationResponse, { ok: true }> | undefined
const cleanup = (): void => {
signal?.removeEventListener('abort', abort)
}
const fail = (error: Error): void => {
/* v8 ignore next -- a late error/exit races only after another terminal callback settled. */
if (settled) return
settled = true
cleanup()
void worker.terminate().then(
() => { reject(error) },
(cleanup: unknown) => {
reject(new AggregateError([error, cleanup], 'migration verifier termination failed'))
},
)
}
worker.once('message', (value: unknown) => {
/* v8 ignore next -- a duplicate message races only after another terminal callback settled. */
if (settled) return
if (typeof value !== 'object' || value === null || typeof (value as { ok?: unknown }).ok !== 'boolean') {
fail(new Error('migration verifier returned an invalid response'))
return
}
const response = value as VerificationResponse
if (!response.ok) {
const error = new Error(response.message)
if (response.stack !== undefined) error.stack = response.stack
fail(error)
return
}
success = response
})
worker.once('error', fail)
worker.once('exit', (code) => {
if (settled) return
settled = true
cleanup()
if (success === undefined) {
reject(new Error(`migration verifier exited before reporting a result (code ${code})`))
} else if (code !== 0) {
reject(new Error(`migration verifier exited after reporting a result (code ${code})`))
} else {
resolve(success.result)
}
})
const abort = (): void => { fail(verifierAbortError(signal)) }
signal?.addEventListener('abort', abort, { once: true })
})
}
function verifierAbortError(signal?: AbortSignal): Error {
const reason: unknown = signal?.reason
return reason instanceof Error
? reason
: new Error('migration verifier aborted', { cause: reason })
}