(source: string)
| 67 | } |
| 68 | |
| 69 | async function loadPolicyText(source: string): Promise<string> { |
| 70 | if (!isRemotePolicySource(source)) { |
| 71 | try { |
| 72 | return await readFile(source, "utf8"); |
| 73 | } catch (error) { |
| 74 | const message = getErrorMessage(error); |
| 75 | throw new Error(`Failed to read policy file: ${message}`); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | const abortController = new AbortController(); |
| 80 | const timeout = setTimeout(() => abortController.abort(), POLICY_FETCH_TIMEOUT_MS); |
| 81 | |
| 82 | try { |
| 83 | const response = await fetch(source, { |
| 84 | signal: abortController.signal, |
| 85 | headers: { |
| 86 | accept: "application/json", |
| 87 | }, |
| 88 | }); |
| 89 | |
| 90 | if (!response.ok) { |
| 91 | throw new Error(`HTTP ${response.status}`); |
| 92 | } |
| 93 | |
| 94 | const text = await response.text(); |
| 95 | const bytes = Buffer.byteLength(text, "utf8"); |
| 96 | if (bytes > POLICY_MAX_BYTES) { |
| 97 | throw new Error(`Response too large (${bytes} bytes)`); |
| 98 | } |
| 99 | |
| 100 | return text; |
| 101 | } catch (error) { |
| 102 | const message = getErrorMessage(error); |
| 103 | throw new Error(`Failed to fetch policy URL (${formatPolicySourceForLog(source)}): ${message}`); |
| 104 | } finally { |
| 105 | clearTimeout(timeout); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | async function loadGovernorPolicyText(input: { |
| 110 | governorOrigin: string; |
no test coverage detected