| 231 | } |
| 232 | |
| 233 | function normalizeFetchOptions(options?: IsolatedFetchOptions): SecureFetchOptions { |
| 234 | if (!options) return { maxResponseBytes: MAX_FETCH_RESPONSE_BYTES } |
| 235 | |
| 236 | const normalized: SecureFetchOptions = { |
| 237 | maxResponseBytes: MAX_FETCH_RESPONSE_BYTES, |
| 238 | } |
| 239 | |
| 240 | if (typeof options.method === 'string' && options.method.length > 0) { |
| 241 | normalized.method = options.method |
| 242 | } |
| 243 | |
| 244 | if ( |
| 245 | typeof options.timeout === 'number' && |
| 246 | Number.isFinite(options.timeout) && |
| 247 | options.timeout > 0 |
| 248 | ) { |
| 249 | normalized.timeout = Math.floor(options.timeout) |
| 250 | } |
| 251 | |
| 252 | if ( |
| 253 | typeof options.maxRedirects === 'number' && |
| 254 | Number.isFinite(options.maxRedirects) && |
| 255 | options.maxRedirects >= 0 |
| 256 | ) { |
| 257 | normalized.maxRedirects = Math.floor(options.maxRedirects) |
| 258 | } |
| 259 | |
| 260 | if (options.headers) { |
| 261 | const headers: Record<string, string> = {} |
| 262 | if (options.headers instanceof Headers) { |
| 263 | options.headers.forEach((value, key) => { |
| 264 | headers[key] = value |
| 265 | }) |
| 266 | } else if (Array.isArray(options.headers)) { |
| 267 | for (const [key, value] of options.headers) { |
| 268 | headers[String(key)] = String(value) |
| 269 | } |
| 270 | } else { |
| 271 | for (const [key, value] of Object.entries(options.headers)) { |
| 272 | headers[key] = String(value) |
| 273 | } |
| 274 | } |
| 275 | normalized.headers = headers |
| 276 | } |
| 277 | |
| 278 | if ( |
| 279 | typeof options.body === 'string' || |
| 280 | options.body instanceof Buffer || |
| 281 | options.body instanceof Uint8Array |
| 282 | ) { |
| 283 | normalized.body = options.body |
| 284 | } else if (options.body !== undefined && options.body !== null) { |
| 285 | normalized.body = String(options.body) |
| 286 | } |
| 287 | |
| 288 | return normalized |
| 289 | } |
| 290 | |