source: frontend/node_modules/workbox-core/src/copyResponse.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.4 KB
Line 
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
9import {canConstructResponseFromBodyStream} from './_private/canConstructResponseFromBodyStream.js';
10import {WorkboxError} from './_private/WorkboxError.js';
11
12import './_version.js';
13
14/**
15 * Allows developers to copy a response and modify its `headers`, `status`,
16 * or `statusText` values (the values settable via a
17 * [`ResponseInit`]{@link https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#Syntax}
18 * object in the constructor).
19 * To modify these values, pass a function as the second argument. That
20 * function will be invoked with a single object with the response properties
21 * `{headers, status, statusText}`. The return value of this function will
22 * be used as the `ResponseInit` for the new `Response`. To change the values
23 * either modify the passed parameter(s) and return it, or return a totally
24 * new object.
25 *
26 * This method is intentionally limited to same-origin responses, regardless of
27 * whether CORS was used or not.
28 *
29 * @param {Response} response
30 * @param {Function} modifier
31 * @memberof workbox-core
32 */
33async function copyResponse(
34 response: Response,
35 modifier?: (responseInit: ResponseInit) => ResponseInit,
36): Promise<Response> {
37 let origin = null;
38 // If response.url isn't set, assume it's cross-origin and keep origin null.
39 if (response.url) {
40 const responseURL = new URL(response.url);
41 origin = responseURL.origin;
42 }
43
44 if (origin !== self.location.origin) {
45 throw new WorkboxError('cross-origin-copy-response', {origin});
46 }
47
48 const clonedResponse = response.clone();
49
50 // Create a fresh `ResponseInit` object by cloning the headers.
51 const responseInit: ResponseInit = {
52 headers: new Headers(clonedResponse.headers),
53 status: clonedResponse.status,
54 statusText: clonedResponse.statusText,
55 };
56
57 // Apply any user modifications.
58 const modifiedResponseInit = modifier ? modifier(responseInit) : responseInit;
59
60 // Create the new response from the body stream and `ResponseInit`
61 // modifications. Note: not all browsers support the Response.body stream,
62 // so fall back to reading the entire body into memory as a blob.
63 const body = canConstructResponseFromBodyStream()
64 ? clonedResponse.body
65 : await clonedResponse.blob();
66
67 return new Response(body, modifiedResponseInit);
68}
69
70export {copyResponse};
Note: See TracBrowser for help on using the repository browser.