Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

oracle-config.ts

44 lines1.2 KB

1type OracleEnvironment = Record<string, string | undefined>
2​
3type OfflineOracleConfigOptions = {
4 prefix: `OFFLINE_ORACLE` | `TANSTACK_DB_OFFLINE_ORACLE`
5 defaultRuns: number
6 defaultSeed?: number
7 environment?: OracleEnvironment
8}
9​
10export function readOfflineOracleConfig({
11 prefix,
12 defaultRuns,
13 defaultSeed,
14 environment = process.env,
15}: OfflineOracleConfigOptions): {
16 runs: number
17 seed: number | undefined
18 path: string | undefined
19} {
20 const runsText = environment[`${prefix}_RUNS`] ?? String(defaultRuns)
21 const runs = Number(runsText)
22 if (runsText.trim() === `` || !Number.isSafeInteger(runs) || runs < 1) {
23 throw new Error(`Invalid ${prefix}_RUNS`)
24 }
25​
26 const seedText = environment[`${prefix}_SEED`]
27 const seed = seedText === undefined ? defaultSeed : Number(seedText)
28 if (
29 seedText !== undefined &&
30 (seedText.trim() === `` || !Number.isSafeInteger(seed))
31 ) {
32 throw new Error(`Invalid ${prefix}_SEED`)
33 }
34​
35 const path = environment[`${prefix}_PATH`]
36 if (
37 path !== undefined &&
38 (seed === undefined || !/^\d+(?::\d+)*$/.test(path))
39 ) {
40 throw new Error(`${prefix}_PATH requires a seed and numeric shrink path`)
41 }
42​
43 return { runs, seed, path }