| 1 | /*
|
|---|
| 2 | Copyright 2019 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 | import { canConstructResponseFromBodyStream } from './_private/canConstructResponseFromBodyStream.js';
|
|---|
| 9 | import { WorkboxError } from './_private/WorkboxError.js';
|
|---|
| 10 | import './_version.js';
|
|---|
| 11 | /**
|
|---|
| 12 | * Allows developers to copy a response and modify its `headers`, `status`,
|
|---|
| 13 | * or `statusText` values (the values settable via a
|
|---|
| 14 | * [`ResponseInit`]{@link https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#Syntax}
|
|---|
| 15 | * object in the constructor).
|
|---|
| 16 | * To modify these values, pass a function as the second argument. That
|
|---|
| 17 | * function will be invoked with a single object with the response properties
|
|---|
| 18 | * `{headers, status, statusText}`. The return value of this function will
|
|---|
| 19 | * be used as the `ResponseInit` for the new `Response`. To change the values
|
|---|
| 20 | * either modify the passed parameter(s) and return it, or return a totally
|
|---|
| 21 | * new object.
|
|---|
| 22 | *
|
|---|
| 23 | * This method is intentionally limited to same-origin responses, regardless of
|
|---|
| 24 | * whether CORS was used or not.
|
|---|
| 25 | *
|
|---|
| 26 | * @param {Response} response
|
|---|
| 27 | * @param {Function} modifier
|
|---|
| 28 | * @memberof workbox-core
|
|---|
| 29 | */
|
|---|
| 30 | async function copyResponse(response, modifier) {
|
|---|
| 31 | let origin = null;
|
|---|
| 32 | // If response.url isn't set, assume it's cross-origin and keep origin null.
|
|---|
| 33 | if (response.url) {
|
|---|
| 34 | const responseURL = new URL(response.url);
|
|---|
| 35 | origin = responseURL.origin;
|
|---|
| 36 | }
|
|---|
| 37 | if (origin !== self.location.origin) {
|
|---|
| 38 | throw new WorkboxError('cross-origin-copy-response', { origin });
|
|---|
| 39 | }
|
|---|
| 40 | const clonedResponse = response.clone();
|
|---|
| 41 | // Create a fresh `ResponseInit` object by cloning the headers.
|
|---|
| 42 | const responseInit = {
|
|---|
| 43 | headers: new Headers(clonedResponse.headers),
|
|---|
| 44 | status: clonedResponse.status,
|
|---|
| 45 | statusText: clonedResponse.statusText,
|
|---|
| 46 | };
|
|---|
| 47 | // Apply any user modifications.
|
|---|
| 48 | const modifiedResponseInit = modifier ? modifier(responseInit) : responseInit;
|
|---|
| 49 | // Create the new response from the body stream and `ResponseInit`
|
|---|
| 50 | // modifications. Note: not all browsers support the Response.body stream,
|
|---|
| 51 | // so fall back to reading the entire body into memory as a blob.
|
|---|
| 52 | const body = canConstructResponseFromBodyStream()
|
|---|
| 53 | ? clonedResponse.body
|
|---|
| 54 | : await clonedResponse.blob();
|
|---|
| 55 | return new Response(body, modifiedResponseInit);
|
|---|
| 56 | }
|
|---|
| 57 | export { copyResponse };
|
|---|