|
| 1 | +import { ensure0xPrefix } from "./validation.js"; |
| 2 | + |
1 | 3 | export type Done = (error?: Error) => void; |
2 | 4 |
|
3 | 5 | export type ArtilleryEvents = Readonly<{ |
@@ -84,3 +86,49 @@ export function getPersistedVar( |
84 | 86 | if (Object.prototype.hasOwnProperty.call(vars, key)) return vars[key]; |
85 | 87 | return undefined; |
86 | 88 | } |
| 89 | + |
| 90 | +/** |
| 91 | + * Read a required var from the VU-scoped Artillery context. |
| 92 | + * |
| 93 | + * Throws if the key does not exist on `context.vars`. |
| 94 | + * |
| 95 | + * Note: this does NOT validate the value type; callers should validate/cast as needed. |
| 96 | + */ |
| 97 | +export function requirePersistedVar( |
| 98 | + context: ArtilleryContext, |
| 99 | + key: string |
| 100 | +): unknown { |
| 101 | + const vars = ensureVars(context); |
| 102 | + if (Object.prototype.hasOwnProperty.call(vars, key)) return vars[key]; |
| 103 | + throw new Error(`Missing persisted var: ${key}`); |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Read a required persisted string var. |
| 108 | + * |
| 109 | + * Throws if missing, not a string, or blank. |
| 110 | + */ |
| 111 | +export function requirePersistedVarString( |
| 112 | + context: ArtilleryContext, |
| 113 | + key: string |
| 114 | +): `0x${string}` { |
| 115 | + const vars = ensureVars(context); |
| 116 | + return ensure0xPrefix(requireVarString(vars, key)); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Read a required persisted number var. |
| 121 | + * |
| 122 | + * Throws if missing or not a number. (No range checks.) |
| 123 | + */ |
| 124 | +export function requirePersistedVarNumber( |
| 125 | + context: ArtilleryContext, |
| 126 | + key: string |
| 127 | +): number { |
| 128 | + const vars = ensureVars(context); |
| 129 | + const v = vars[key]; |
| 130 | + if (typeof v !== "number") { |
| 131 | + throw new Error(`Missing or invalid var: ${key}`); |
| 132 | + } |
| 133 | + return v; |
| 134 | +} |
0 commit comments