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

Function register

ark/aot/node.ts:48–209  ·  view source on GitHub ↗
(opts?: PrecompilePluginOptions)

Source from the content-addressed store, hash-verified

46import { createSyncPrecompiler, isPrecompilerWorker } from "./worker.ts"
47
48export const register = (opts?: PrecompilePluginOptions): void => {
49 // added in Node 22.15
50 if (!module.registerHooks)
51 throwError(writeNodeVersionMessage("@ark/aot/node", "22.15"))
52 refuseDeclare(opts, "Node")
53 // a worker still runs what NODE_OPTIONS preloads, which may register these hooks
54 if (isPrecompilerWorker) return
55 const set = withSettingsFile(opts)
56 refusePrune(set, "Node")
57 opts = resolvePrecompileOptions(set)
58 const isStandalone = opts.runtime !== "arktype"
59 const variant = resolvedVariantOf(opts)
60 const request = createSyncPrecompiler({
61 precompilerOptions: driverOptionsOf(
62 set,
63 // `node --test` marks each file's process, or runs the files in its own
64 process.env.NODE_TEST_CONTEXT !== undefined ||
65 process.execArgv.includes("--test")
66 )
67 })
68 const filter = transformedFilterOf(
69 optedInPackagesOf(opts?.cwd ?? process.cwd(), opts.dependencies)
70 )
71 const isTransformed = (id: string) =>
72 filter.include.test(id) && !filter.exclude.test(id)
73 // as a development build resolves it, so Node prints errors as their issues
74 const runtimeConditions = isProductionEnv() ? [] : ["development"]
75 const resolveRuntime: module.ResolveHookSync = (
76 specifier,
77 context,
78 nextResolve
79 ) =>
80 specifier === "arktype/runtime" ?
81 nextResolve(specifier, {
82 ...context,
83 conditions: [...context.conditions, ...runtimeConditions]
84 })
85 : nextResolve(specifier, context)
86 const modules: Record<string, string> = {}
87 const importersByUrl: Record<string, string> = {}
88 const packageNamesByDir: PackageNamesByDir = {}
89 // modules whose text these hooks were given
90 const givenUrls = new Set<string>()
91 // allowUnsafe is offered only where building anyway may be what's wanted
92 const reportUnsafe = (problem: string, offersAllowUnsafe = true) => {
93 if (opts?.allowUnsafe) process.emitWarning(problem)
94 else throwError(offersAllowUnsafe ? withAllowUnsafe(problem) : problem)
95 }
96 const reportedPrecompiled = new Set<string>()
97 const checkPrecompiled = (specifier: string, importer: string) => {
98 const name = precompiledDependencyOf(specifier, importer)
99 if (!name || reportedPrecompiled.has(name)) return
100 reportedPrecompiled.add(name)
101 reportUnsafe(writePrecompiledDependencyMessage(name))
102 }
103 // e.g. a module another hook loads, code given to node --eval, or a file without an extension
104 const untransformedProblemOf = (specifier: string, importer: string) => {
105 if (!importer.startsWith("file:"))

Callers 2

drivers.test.tsFile · 0.90
start.tsFile · 0.90

Calls 15

throwErrorFunction · 0.90
refuseDeclareFunction · 0.90
withSettingsFileFunction · 0.90
refusePruneFunction · 0.90
resolvePrecompileOptionsFunction · 0.90
resolvedVariantOfFunction · 0.90
createSyncPrecompilerFunction · 0.90
driverOptionsOfFunction · 0.90
transformedFilterOfFunction · 0.90
optedInPackagesOfFunction · 0.90
isProductionEnvFunction · 0.90
variantSpecifierOfFunction · 0.90

Tested by

no test coverage detected