| 490 | |
| 491 | // https://whatpr.org/fetch/1392.html#initialize-a-response |
| 492 | function initializeResponse (response, init, body) { |
| 493 | // 1. If init["status"] is not in the range 200 to 599, inclusive, then |
| 494 | // throw a RangeError. |
| 495 | if (init.status !== null && (init.status < 200 || init.status > 599)) { |
| 496 | throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.') |
| 497 | } |
| 498 | |
| 499 | // 2. If init["statusText"] does not match the reason-phrase token production, |
| 500 | // then throw a TypeError. |
| 501 | if ('statusText' in init && init.statusText != null) { |
| 502 | // See, https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2: |
| 503 | // reason-phrase = *( HTAB / SP / VCHAR / obs-text ) |
| 504 | if (!isValidReasonPhrase(String(init.statusText))) { |
| 505 | throw new TypeError('Invalid statusText') |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // 3. Set response’s response’s status to init["status"]. |
| 510 | if ('status' in init && init.status != null) { |
| 511 | getResponseState(response).status = init.status |
| 512 | } |
| 513 | |
| 514 | // 4. Set response’s response’s status message to init["statusText"]. |
| 515 | if ('statusText' in init && init.statusText != null) { |
| 516 | getResponseState(response).statusText = init.statusText |
| 517 | } |
| 518 | |
| 519 | // 5. If init["headers"] exists, then fill response’s headers with init["headers"]. |
| 520 | if ('headers' in init && init.headers != null) { |
| 521 | fill(getResponseHeaders(response), init.headers) |
| 522 | } |
| 523 | |
| 524 | // 6. If body was given, then: |
| 525 | if (body) { |
| 526 | // 1. If response's status is a null body status, then throw a TypeError. |
| 527 | if (nullBodyStatus.includes(response.status)) { |
| 528 | throw webidl.errors.exception({ |
| 529 | header: 'Response constructor', |
| 530 | message: `Invalid response status code ${response.status}` |
| 531 | }) |
| 532 | } |
| 533 | |
| 534 | // 2. Set response's body to body's body. |
| 535 | getResponseState(response).body = body.body |
| 536 | |
| 537 | // 3. If body's type is non-null and response's header list does not contain |
| 538 | // `Content-Type`, then append (`Content-Type`, body's type) to response's header list. |
| 539 | if (body.type != null && !getResponseState(response).headersList.contains('content-type', true)) { |
| 540 | getResponseState(response).headersList.append('content-type', body.type, true) |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @see https://fetch.spec.whatwg.org/#response-create |