source: frontend/node_modules/workbox-range-requests/src/createPartialResponse.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: 3.6 KB
RevLine 
[9af201e]1/*
2 Copyright 2018 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 {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
10import {assert} from 'workbox-core/_private/assert.js';
11import {logger} from 'workbox-core/_private/logger.js';
12import {calculateEffectiveBoundaries} from './utils/calculateEffectiveBoundaries.js';
13import {parseRangeHeader} from './utils/parseRangeHeader.js';
14import './_version.js';
15
16/**
17 * Given a `Request` and `Response` objects as input, this will return a
18 * promise for a new `Response`.
19 *
20 * If the original `Response` already contains partial content (i.e. it has
21 * a status of 206), then this assumes it already fulfills the `Range:`
22 * requirements, and will return it as-is.
23 *
24 * @param {Request} request A request, which should contain a Range:
25 * header.
26 * @param {Response} originalResponse A response.
27 * @return {Promise<Response>} Either a `206 Partial Content` response, with
28 * the response body set to the slice of content specified by the request's
29 * `Range:` header, or a `416 Range Not Satisfiable` response if the
30 * conditions of the `Range:` header can't be met.
31 *
32 * @memberof workbox-range-requests
33 */
34async function createPartialResponse(
35 request: Request,
36 originalResponse: Response,
37): Promise<Response> {
38 try {
39 if (process.env.NODE_ENV !== 'production') {
40 assert!.isInstance(request, Request, {
41 moduleName: 'workbox-range-requests',
42 funcName: 'createPartialResponse',
43 paramName: 'request',
44 });
45
46 assert!.isInstance(originalResponse, Response, {
47 moduleName: 'workbox-range-requests',
48 funcName: 'createPartialResponse',
49 paramName: 'originalResponse',
50 });
51 }
52
53 if (originalResponse.status === 206) {
54 // If we already have a 206, then just pass it through as-is;
55 // see https://github.com/GoogleChrome/workbox/issues/1720
56 return originalResponse;
57 }
58
59 const rangeHeader = request.headers.get('range');
60 if (!rangeHeader) {
61 throw new WorkboxError('no-range-header');
62 }
63
64 const boundaries = parseRangeHeader(rangeHeader);
65 const originalBlob = await originalResponse.blob();
66
67 const effectiveBoundaries = calculateEffectiveBoundaries(
68 originalBlob,
69 boundaries.start,
70 boundaries.end,
71 );
72
73 const slicedBlob = originalBlob.slice(
74 effectiveBoundaries.start,
75 effectiveBoundaries.end,
76 );
77 const slicedBlobSize = slicedBlob.size;
78
79 const slicedResponse = new Response(slicedBlob, {
80 // Status code 206 is for a Partial Content response.
81 // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
82 status: 206,
83 statusText: 'Partial Content',
84 headers: originalResponse.headers,
85 });
86
87 slicedResponse.headers.set('Content-Length', String(slicedBlobSize));
88 slicedResponse.headers.set(
89 'Content-Range',
90 `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` +
91 `${originalBlob.size}`,
92 );
93
94 return slicedResponse;
95 } catch (error) {
96 if (process.env.NODE_ENV !== 'production') {
97 logger.warn(
98 `Unable to construct a partial response; returning a ` +
99 `416 Range Not Satisfiable response instead.`,
100 );
101 logger.groupCollapsed(`View details here.`);
102 logger.log(error);
103 logger.log(request);
104 logger.log(originalResponse);
105 logger.groupEnd();
106 }
107
108 return new Response('', {
109 status: 416,
110 statusText: 'Range Not Satisfiable',
111 });
112 }
113}
114
115export {createPartialResponse};
Note: See TracBrowser for help on using the repository browser.