source: frontend/node_modules/workbox-range-requests/createPartialResponse.js

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

Fix frontend appearance

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