MCPcopy Create free account
hub / github.com/arktypeio/arktype / createSnapshotCheck

Function createSnapshotCheck

ark/aot/cache.ts:262–326  ·  view source on GitHub ↗
(
	cache: AnswerCache,
	host: PrecompileHost,
	resolveInBundler?: (specifier: string, importer: string) => string | undefined
)

Source from the content-addressed store, hash-verified

260// same, judged once each until a file it read changes or the build resets,
261// and the build gave each module it names as given what it was given last
262export const createSnapshotCheck = (
263 cache: AnswerCache,
264 host: PrecompileHost,
265 resolveInBundler?: (specifier: string, importer: string) => string | undefined
266): SnapshotCheck => {
267 const holding: Record<string, Snapshot["given"] | false> = {}
268 const mtimes: Record<string, number> = {}
269 const hashes: Record<string, string | null> = {}
270 const hashOfFile = (file: string) => {
271 mtimes[file] = mtimeOf(file)
272 const source = readFile(file)
273 return source === undefined ? null : hashOf(source)
274 }
275 const targets: Record<string, string | null> = {}
276 const tsconfigs: Record<string, string | null> = {}
277 const answers: Record<string, string | null> = {}
278 const holdsFrom = (id: string): Snapshot["given"] | false => {
279 const asked: Snapshot["asked"] = []
280 // read from the cache, so no record's module is a key every object has
281 const given: Record<string, string> = Object.create(null)
282 for (let next: string | undefined = id; next !== undefined; ) {
283 const snapshot = cache.snapshot(next)
284 if (!snapshot) return false
285 for (const [file, hash] of snapshot.reads)
286 if ((hashes[file] ??= hashOfFile(file)) !== hash) return false
287 for (const [importer, specifier, target] of snapshot.resolutions) {
288 if (
289 (targets[`${importer}\0${specifier}`] ??=
290 host.resolve(specifier, importer) ?? null) !== target
291 )
292 return false
293 }
294 for (const [importer, tsconfig] of snapshot.tsconfigs) {
295 if (
296 (tsconfigs[importer] ??= host.tsconfigOf?.(importer) ?? null) !==
297 tsconfig
298 )
299 return false
300 }
301 asked.push(...snapshot.asked)
302 for (const [module, text] of snapshot.given) given[module] ??= text
303 next = snapshot.parent
304 }
305 // last, as each question waits on the bundler's thread
306 return (
307 asked.every(
308 ([importer, specifier, target]) =>
309 (answers[`${importer}\0${specifier}`] ??=
310 host.resolve(specifier, importer) ??
311 resolveInBundler?.(specifier, importer) ??
312 null) === target
313 ) && Object.entries(given)
314 )
315 }
316 return Object.assign(
317 (id: string, given: Record<string, string>) => {
318 const stated = (holding[id] ??= holdsFrom(id))
319 return (

Callers 2

scannedFunction · 0.90
cachedAnswerOfFunction · 0.90

Calls 1

holdsFromFunction · 0.85

Tested by

no test coverage detected