Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copyResponse and more granular stream detects #2193

Merged
merged 2 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/workbox-core/src/_private/canConstructReadableStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2019 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

import '../_version.js';


let supportStatus: boolean | undefined;

/**
* A utility function that determines whether the current browser supports
* constructing a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
* object.
*
* @return {boolean} `true`, if the current browser can successfully
* construct a `ReadableStream`, `false` otherwise.
*/
function canConstructReadableStream(): boolean {
if (supportStatus === undefined) {
// See https://github.com/GoogleChrome/workbox/issues/1473
try {
new ReadableStream({start() {}});
supportStatus = true;
} catch (error) {
supportStatus = false;
}
}

return supportStatus;
}

export {canConstructReadableStream};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2019 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

import '../_version.js';


let supportStatus: boolean | undefined;

/**
* A utility function that determines whether the current browser supports
* constructing a new `Response` from a `response.body` stream.
*
* @return {boolean} `true`, if the current browser can successfully
* construct a `Response` from a `response.body` stream, `false` otherwise.
*/
function canConstructResponseFromBodyStream(): boolean {
if (supportStatus === undefined) {
const testResponse = new Response('');

if ('body' in testResponse) {
try {
new Response(testResponse.body);
supportStatus = true;
} catch (error) {
supportStatus = false;
}
}
supportStatus = false;
}

return supportStatus;
}

export {canConstructResponseFromBodyStream};
54 changes: 54 additions & 0 deletions packages/workbox-core/src/copyResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2019 Google LLC

Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

import {canConstructResponseFromBodyStream} from './_private/canConstructResponseFromBodyStream.js';
import './_version.js';


/**
* Allows developers to copy a response and modify its `headers`, `status`,
* or `statusText` values (the values settable via a
* [`ResponseInit`]{@link https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#Syntax}
* object in the constructor).
* To modify these values, pass a function as the second argument. That
* function will be invoked with a single object with the response properties
* `{headers, status, statusText}`. The return value of this function will
* be used as the `ResponseInit` for the new `Response`. To change the values
* either modify the passed parameter(s) and return it, or return a totally
* new object.
*
* @param {Response} response
* @param {Function} modifier
* @alias workbox.core.copyResponse
*/
async function copyResponse(
response: Response,
modifier?: (responseInit: ResponseInit) => ResponseInit
) {
const clonedResponse = response.clone();

// Create a fresh `ResponseInit` object by cloning the headers.
const responseInit: ResponseInit = {
headers: new Headers(clonedResponse.headers),
status: clonedResponse.status,
statusText: clonedResponse.statusText,
}

// Apply any user modifications.
const modifiedResponseInit = modifier ? modifier(responseInit) : responseInit;

// Create the new response from the body stream and `ResponseInit`
// modifications. Note: not all browsers support the Response.body stream,
// so fall back to reading the entire body into memory as a blob.
const body = canConstructResponseFromBodyStream() ?
clonedResponse.body : await clonedResponse.blob();

return new Response(body, modifiedResponseInit);
};

export {copyResponse}
10 changes: 7 additions & 3 deletions packages/workbox-precaching/src/PrecacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {cacheNames} from 'workbox-core/_private/cacheNames.js';
import {cacheWrapper} from 'workbox-core/_private/cacheWrapper.js';
import {fetchWrapper} from 'workbox-core/_private/fetchWrapper.js';
import {logger} from 'workbox-core/_private/logger.js';
import {RouteHandlerCallback} from 'workbox-core/types.js';
import {WorkboxError} from 'workbox-core/_private/WorkboxError.js';
import {copyResponse} from 'workbox-core/copyResponse.js';
import {RouteHandlerCallback} from 'workbox-core/types.js';
import {WorkboxPlugin} from 'workbox-core/types.js';

import {PrecacheEntry} from './_types.js';
import {cleanRedirect} from './utils/cleanRedirect.js';
import {createCacheKey} from './utils/createCacheKey.js';
import {printCleanupDetails} from './utils/printCleanupDetails.js';
import {printInstallDetails} from './utils/printInstallDetails.js';
Expand Down Expand Up @@ -239,8 +239,12 @@ class PrecacheController {
});
}

// Redirected responses cannot be used to satisfy a navigation request, so
// any redirected response must be "copied" rather than cloned, so the new
// response doesn't contain the `redirected` flag. See:
// https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
if (response.redirected) {
response = await cleanRedirect(response);
response = await copyResponse(response);
}

await cacheWrapper.put({
Expand Down
35 changes: 0 additions & 35 deletions packages/workbox-precaching/src/utils/cleanRedirect.ts

This file was deleted.

20 changes: 4 additions & 16 deletions packages/workbox-streams/src/isSupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
https://opensource.org/licenses/MIT.
*/

import {canConstructReadableStream} from 'workbox-core/_private/canConstructReadableStream.js';
import './_version.js';

let cachedIsSupported: boolean | undefined;

/**
* This is a utility method that determines whether the current browser supports
Expand All @@ -18,21 +18,9 @@ let cachedIsSupported: boolean | undefined;
*
* @return {boolean} `true`, if the current browser meets the requirements for
* streaming responses, and `false` otherwise.
*
* @memberof workbox.streams
*/
function isSupported(): boolean {
if (cachedIsSupported === undefined) {
// See https://github.com/GoogleChrome/workbox/issues/1473
try {
new ReadableStream({start() {}});
cachedIsSupported = true;
} catch (error) {
cachedIsSupported = false;
}
}

return cachedIsSupported;
function isSupported() {
return canConstructReadableStream();
}

export {isSupported};
export {isSupported}
Loading