Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
) => {
if (process.env.NODE_ENV !== 'production') {
if (!(firstResponse instanceof Response &&
secondResponse instanceof Response)) {
throw new WorkboxError('invalid-responses-are-same-args');
}
}
const atLeastOneHeaderAvailable = headersToCheck.some((header) => {
return firstResponse.headers.has(header) &&
secondResponse.headers.has(header);
});
if (!atLeastOneHeaderAvailable) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to determine where the response has been updated ` +
`because none of the headers that would be checked are present.`);
logger.debug(`Attempting to compare the following: `,
firstResponse, secondResponse, headersToCheck);
}
// Just return true, indicating the that responses are the same, since we
// can't determine otherwise.
return true;
}
return headersToCheck.every((header) => {
const headerStateComparison = firstResponse.headers.has(header) ===
secondResponse.headers.has(header);
const headerValueComparison = firstResponse.headers.get(header) ===
secondResponse.headers.get(header);
const cachePut = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins,
});
if (event) {
try {
// The event has been responded to so we can keep the SW alive to
// respond to the request
event.waitUntil(cachePut);
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}
}
}
return response;
}
async registerSync() {
if ('sync' in self.registration) {
try {
await self.registration.sync.register(`${TAG_PREFIX}:${this._name}`);
} catch (err) {
// This means the registration failed for some reason, possibly due to
// the user disabling it.
if (process.env.NODE_ENV !== 'production') {
logger.warn(
`Unable to register sync event for '${this._name}'.`, err);
}
}
}
}
// Keep the service worker while we put the request to the cache
const responseClone = response.clone();
const cachePutPromise = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins,
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}
}
return response;
}
}
// 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', String(slicedBlobSize));
slicedResponse.headers.set('Content-Range',
`bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` +
originalBlob.size);
return slicedResponse;
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to construct a partial response; returning a ` +
`416 Range Not Satisfiable response instead.`);
logger.groupCollapsed(`View details here.`);
logger.log(error);
logger.log(request);
logger.log(originalResponse);
logger.groupEnd();
}
return new Response('', {
status: 416,
statusText: 'Range Not Satisfiable',
});
}
}
});
const cachePutPromise = cacheWrapper.put({
cacheName: this._cacheName,
request,
response: response.clone(),
event,
plugins: this._plugins,
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
if (process.env.NODE_ENV !== 'production') {
logger.warn(`Unable to ensure service worker stays alive when ` +
`updating cache for '${getFriendlyURL(request.url)}'.`);
}
}
}
return response;
}
}
return (options: object) => {
if (process.env.NODE_ENV !== 'production') {
const strategyCtrName = strategy[0].toUpperCase() + strategy.slice(1);
logger.warn(`The 'workbox.strategies.${strategy}()' function has been ` +
`deprecated and will be removed in a future version of Workbox.\n` +
`Please use 'new workbox.strategies.${strategyCtrName}()' instead.`);
}
return new StrategyCtr(options);
};
};