async function createPartialResponse()

in workbox-v4.3.1/workbox-range-requests.dev.js [149:206]


  async function createPartialResponse(request, originalResponse) {
    try {
      {
        assert_mjs.assert.isInstance(request, Request, {
          moduleName: 'workbox-range-requests',
          funcName: 'createPartialResponse',
          paramName: 'request'
        });
        assert_mjs.assert.isInstance(originalResponse, Response, {
          moduleName: 'workbox-range-requests',
          funcName: 'createPartialResponse',
          paramName: 'originalResponse'
        });
      }

      if (originalResponse.status === 206) {
        // If we already have a 206, then just pass it through as-is;
        // see https://github.com/GoogleChrome/workbox/issues/1720
        return originalResponse;
      }

      const rangeHeader = request.headers.get('range');

      if (!rangeHeader) {
        throw new WorkboxError_mjs.WorkboxError('no-range-header');
      }

      const boundaries = parseRangeHeader(rangeHeader);
      const originalBlob = await originalResponse.blob();
      const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
      const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
      const slicedBlobSize = slicedBlob.size;
      const slicedResponse = new Response(slicedBlob, {
        // Status code 206 is for a Partial Content response.
        // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
        status: 206,
        statusText: 'Partial Content',
        headers: originalResponse.headers
      });
      slicedResponse.headers.set('Content-Length', slicedBlobSize);
      slicedResponse.headers.set('Content-Range', `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + originalBlob.size);
      return slicedResponse;
    } catch (error) {
      {
        logger_mjs.logger.warn(`Unable to construct a partial response; returning a ` + `416 Range Not Satisfiable response instead.`);
        logger_mjs.logger.groupCollapsed(`View details here.`);
        logger_mjs.logger.log(error);
        logger_mjs.logger.log(request);
        logger_mjs.logger.log(originalResponse);
        logger_mjs.logger.groupEnd();
      }

      return new Response('', {
        status: 416,
        statusText: 'Range Not Satisfiable'
      });
    }
  }