( out: string[], directive: string, value?: number | true | string[], )
| 360 | } |
| 361 | |
| 362 | function append( |
| 363 | out: string[], |
| 364 | directive: string, |
| 365 | value?: number | true | string[], |
| 366 | ): void { |
| 367 | if (value === undefined) return; |
| 368 | if (value === true) { |
| 369 | out.push(directive); |
| 370 | return; |
| 371 | } |
| 372 | if (typeof value === "number") { |
| 373 | if (!Number.isInteger(value) || value < 0) { |
| 374 | throw new RangeError( |
| 375 | `Cache-Control: ${directive} must be a non-negative integer, got ${value}`, |
| 376 | ); |
| 377 | } |
| 378 | // Clamp to MAX_DELTA_SECONDS to match parser behavior (RFC 9111 §1.2.2). |
| 379 | out.push(`${directive}=${Math.min(value, MAX_DELTA_SECONDS)}`); |
| 380 | return; |
| 381 | } |
| 382 | if (value.length === 0) { |
| 383 | out.push(directive); |
| 384 | return; |
| 385 | } |
| 386 | for (const name of value) { |
| 387 | if (!TCHAR_REGEXP.test(name)) { |
| 388 | throw new TypeError( |
| 389 | `Cache-Control: invalid field name in ${directive}: "${name}"`, |
| 390 | ); |
| 391 | } |
| 392 | } |
| 393 | out.push(`${directive}="${value.join(", ")}"`); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Serializes a Cache-Control object to a header value string. Output is |
no test coverage detected