| 54 | let configureIslands: ((config: ArkSchemaConfig) => unknown) | undefined |
| 55 | |
| 56 | export const configure = ( |
| 57 | config: ArkSchemaConfig, |
| 58 | fingerprint?: string |
| 59 | ): ArkSchemaConfig => { |
| 60 | // arktype's config is fixed at its first Type, and the build's is one |
| 61 | // call's. Called again, as when a test runner resets its modules and the |
| 62 | // config module runs again, the same values change nothing |
| 63 | if (Object.isFrozen(runtimeConfig) || checkConfigured) { |
| 64 | return isSameConfig(config, programConfig ?? {}) ? config : ( |
| 65 | throwParseError(reconfiguredMessage) |
| 66 | ) |
| 67 | } |
| 68 | const received = fingerprintOf(config) |
| 69 | // a loader can rewrite the config module after the build read it |
| 70 | if (fingerprint !== undefined && fingerprint !== received) |
| 71 | return throwParseError(configuredUnlikeBuildMessage) |
| 72 | configureIslands?.(config) |
| 73 | if ("onFail" in config) |
| 74 | (runtimeConfig as { onFail: unknown }).onFail = config.onFail |
| 75 | let isRechecked = false |
| 76 | checkConfigured = built => { |
| 77 | // configure()'s argument may be written after the call |
| 78 | if (!isRechecked && fingerprintOf(config) !== received) |
| 79 | throwParseError(writtenConfigMessage) |
| 80 | // only once it passes, so a validator after a caught throw checks again |
| 81 | isRechecked = true |
| 82 | // an untransformed call, e.g. a dependency's, has no fingerprint |
| 83 | if (fingerprint === undefined || built !== fingerprint) |
| 84 | throwParseError(configMismatchMessage) |
| 85 | } |
| 86 | // copied, as arktype copies configure()'s argument |
| 87 | programConfig = copyConfig(config) |
| 88 | return config |
| 89 | } |
| 90 | |
| 91 | export const configuredWriterOf = |
| 92 | (code: string, k: string) => |