Socket
Socket
Sign inDemoInstall

workbox-streams

Package Overview
Dependencies
Maintainers
4
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-streams - npm Package Compare versions

Comparing version 5.0.0-alpha.0 to 5.0.0-alpha.1

_types.d.ts

564

build/workbox-streams.dev.js
this.workbox = this.workbox || {};
this.workbox.streams = (function (exports, logger_mjs, assert_mjs) {
'use strict';
this.workbox.streams = (function (exports, logger_js, assert_js, Deferred_js) {
'use strict';
try {
self['workbox:streams:5.0.0-alpha.0'] && _();
} catch (e) {} // eslint-disable-line
// @ts-ignore
try {
self['workbox:streams:5.0.0-alpha.1'] && _();
} catch (e) {}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
/**
* Takes either a Response, a ReadableStream, or a
* [BodyInit](https://fetch.spec.whatwg.org/#bodyinit) and returns the
* ReadableStreamReader object associated with it.
*
* @param {workbox.streams.StreamSource} source
* @return {ReadableStreamReader}
* @private
*/
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.
*/
/**
* Takes either a Response, a ReadableStream, or a
* [BodyInit](https://fetch.spec.whatwg.org/#bodyinit) and returns the
* ReadableStreamReader object associated with it.
*
* @param {workbox.streams.StreamSource} source
* @return {ReadableStreamReader}
* @private
*/
function _getReaderFromSource(source) {
if (source.body && source.body.getReader) {
return source.body.getReader();
}
function _getReaderFromSource(source) {
if (source instanceof Response) {
return source.body.getReader();
}
if (source.getReader) {
return source.getReader();
} // TODO: This should be possible to do by constructing a ReadableStream, but
// I can't get it to work. As a hack, construct a new Response, and use the
// reader associated with its body.
if (source instanceof ReadableStream) {
return source.getReader();
}
return new Response(source).body.getReader();
}
/**
* Takes multiple source Promises, each of which could resolve to a Response, a
* ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit).
*
* Returns an object exposing a ReadableStream with each individual stream's
* data returned in sequence, along with a Promise which signals when the
* stream is finished (useful for passing to a FetchEvent's waitUntil()).
*
* @param {Array<Promise<workbox.streams.StreamSource>>} sourcePromises
* @return {Object<{done: Promise, stream: ReadableStream}>}
*
* @memberof workbox.streams
*/
return new Response(source).body.getReader();
}
/**
* Takes multiple source Promises, each of which could resolve to a Response, a
* ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit).
*
* Returns an object exposing a ReadableStream with each individual stream's
* data returned in sequence, along with a Promise which signals when the
* stream is finished (useful for passing to a FetchEvent's waitUntil()).
*
* @param {Array<Promise<workbox.streams.StreamSource>>} sourcePromises
* @return {Object<{done: Promise, stream: ReadableStream}>}
*
* @memberof workbox.streams
*/
function concatenate(sourcePromises) {
{
assert_js.assert.isArray(sourcePromises, {
moduleName: 'workbox-streams',
funcName: 'concatenate',
paramName: 'sourcePromises'
});
}
function concatenate(sourcePromises) {
{
assert_mjs.assert.isArray(sourcePromises, {
moduleName: 'workbox-streams',
funcName: 'concatenate',
paramName: 'sourcePromises'
const readerPromises = sourcePromises.map(sourcePromise => {
return Promise.resolve(sourcePromise).then(source => {
return _getReaderFromSource(source);
});
});
}
const streamDeferred = new Deferred_js.Deferred();
let i = 0;
const logMessages = [];
const stream = new ReadableStream({
pull(controller) {
return readerPromises[i].then(reader => reader.read()).then(result => {
if (result.done) {
{
logMessages.push(['Reached the end of source:', sourcePromises[i]]);
}
const readerPromises = sourcePromises.map(sourcePromise => {
return Promise.resolve(sourcePromise).then(source => {
return _getReaderFromSource(source);
});
});
let fullyStreamedResolve;
let fullyStreamedReject;
const done = new Promise((resolve, reject) => {
fullyStreamedResolve = resolve;
fullyStreamedReject = reject;
});
let i = 0;
const logMessages = [];
const stream = new ReadableStream({
pull(controller) {
return readerPromises[i].then(reader => reader.read()).then(result => {
if (result.done) {
{
logMessages.push(['Reached the end of source:', sourcePromises[i]]);
}
i++;
i++;
if (i >= readerPromises.length) {
// Log all the messages in the group at once in a single group.
{
logger_js.logger.groupCollapsed(`Concatenating ${readerPromises.length} sources.`);
if (i >= readerPromises.length) {
// Log all the messages in the group at once in a single group.
{
logger_mjs.logger.groupCollapsed(`Concatenating ${readerPromises.length} sources.`);
for (const message of logMessages) {
if (Array.isArray(message)) {
logger_js.logger.log(...message);
} else {
logger_js.logger.log(message);
}
}
for (const message of logMessages) {
if (Array.isArray(message)) {
logger_mjs.logger.log(...message);
} else {
logger_mjs.logger.log(message);
}
logger_js.logger.log('Finished reading all sources.');
logger_js.logger.groupEnd();
}
logger_mjs.logger.log('Finished reading all sources.');
logger_mjs.logger.groupEnd();
}
controller.close();
streamDeferred.resolve();
return;
} // The `pull` method is defined because we're inside it.
controller.close();
fullyStreamedResolve();
return;
return this.pull(controller);
} else {
controller.enqueue(result.value);
}
}).catch(error => {
{
logger_js.logger.error('An error occurred:', error);
}
return this.pull(controller);
} else {
controller.enqueue(result.value);
}
}).catch(error => {
streamDeferred.reject(error);
throw error;
});
},
cancel() {
{
logger_mjs.logger.error('An error occurred:', error);
logger_js.logger.warn('The ReadableStream was cancelled.');
}
fullyStreamedReject(error);
throw error;
});
},
cancel() {
{
logger_mjs.logger.warn('The ReadableStream was cancelled.');
streamDeferred.resolve();
}
fullyStreamedResolve();
}
});
return {
done: streamDeferred.promise,
stream
};
}
});
return {
done,
stream
};
}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
/**
* This is a utility method that determines whether the current browser supports
* the features required to create streamed responses. Currently, it checks if
* [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
* is available.
*
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {boolean} `true`, if the current browser meets the requirements for
* streaming responses, and `false` otherwise.
*
* @memberof workbox.streams
*/
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.
*/
/**
* This is a utility method that determines whether the current browser supports
* the features required to create streamed responses. Currently, it checks if
* [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
* is available.
*
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {boolean} `true`, if the current browser meets the requirements for
* streaming responses, and `false` otherwise.
*
* @memberof workbox.streams
*/
function createHeaders(headersInit = {}) {
// See https://github.com/GoogleChrome/workbox/issues/1461
const headers = new Headers(headersInit);
function createHeaders(headersInit = {}) {
// See https://github.com/GoogleChrome/workbox/issues/1461
const headers = new Headers(headersInit);
if (!headers.has('content-type')) {
headers.set('content-type', 'text/html');
}
if (!headers.has('content-type')) {
headers.set('content-type', 'text/html');
return headers;
}
return headers;
}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
/**
* Takes multiple source Promises, each of which could resolve to a Response, a
* ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit),
* along with a
* [HeadersInit](https://fetch.spec.whatwg.org/#typedefdef-headersinit).
*
* Returns an object exposing a Response whose body consists of each individual
* stream's data returned in sequence, along with a Promise which signals when
* the stream is finished (useful for passing to a FetchEvent's waitUntil()).
*
* @param {Array<Promise<workbox.streams.StreamSource>>} sourcePromises
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {Object<{done: Promise, response: Response}>}
*
* @memberof workbox.streams
*/
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.
*/
/**
* Takes multiple source Promises, each of which could resolve to a Response, a
* ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit),
* along with a
* [HeadersInit](https://fetch.spec.whatwg.org/#typedefdef-headersinit).
*
* Returns an object exposing a Response whose body consists of each individual
* stream's data returned in sequence, along with a Promise which signals when
* the stream is finished (useful for passing to a FetchEvent's waitUntil()).
*
* @param {Array<Promise<workbox.streams.StreamSource>>} sourcePromises
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {Object<{done: Promise, response: Response}>}
*
* @memberof workbox.streams
*/
function concatenateToResponse(sourcePromises, headersInit) {
const {
done,
stream
} = concatenate(sourcePromises);
const headers = createHeaders(headersInit);
const response = new Response(stream, {
headers
});
return {
done,
response
};
}
function concatenateToResponse(sourcePromises, headersInit) {
const {
done,
stream
} = concatenate(sourcePromises);
const headers = createHeaders(headersInit);
const response = new Response(stream, {
headers
});
return {
done,
response
};
}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
let cachedIsSupported;
/**
* This is a utility method that determines whether the current browser supports
* the features required to create streamed responses. Currently, it checks if
* [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
* can be created.
*
* @return {boolean} `true`, if the current browser meets the requirements for
* streaming responses, and `false` otherwise.
*
* @memberof workbox.streams
*/
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.
*/
let cachedIsSupported = undefined;
/**
* This is a utility method that determines whether the current browser supports
* the features required to create streamed responses. Currently, it checks if
* [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
* can be created.
*
* @return {boolean} `true`, if the current browser meets the requirements for
* streaming responses, and `false` otherwise.
*
* @memberof workbox.streams
*/
function isSupported() {
if (cachedIsSupported === undefined) {
// See https://github.com/GoogleChrome/workbox/issues/1473
try {
new ReadableStream({
start() {}
function isSupported() {
if (cachedIsSupported === undefined) {
// See https://github.com/GoogleChrome/workbox/issues/1473
try {
new ReadableStream({
start() {}
});
cachedIsSupported = true;
} catch (error) {
cachedIsSupported = false;
}
}
});
cachedIsSupported = true;
} catch (error) {
cachedIsSupported = false;
}
return cachedIsSupported;
}
return cachedIsSupported;
}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
/**
* A shortcut to create a strategy that could be dropped-in to Workbox's router.
*
* On browsers that do not support constructing new `ReadableStream`s, this
* strategy will automatically wait for all the `sourceFunctions` to complete,
* and create a final response that concatenates their values together.
*
* @param {Array<function({event, request, url, params})>} sourceFunctions
* An array of functions similar to {@link workbox.routing.Route~handlerCallback}
* but that instead return a {@link workbox.streams.StreamSource} (or a
* Promise which resolves to one).
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {workbox.routing.Route~handlerCallback}
*
* @memberof workbox.streams
*/
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.
*/
/**
* A shortcut to create a strategy that could be dropped-in to Workbox's router.
*
* On browsers that do not support constructing new `ReadableStream`s, this
* strategy will automatically wait for all the `sourceFunctions` to complete,
* and create a final response that concatenates their values together.
*
* @param {
* Array<function(workbox.routing.Route~handlerCallback)>} sourceFunctions
* Each function should return a {@link workbox.streams.StreamSource} (or a
* Promise which resolves to one).
* @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
* `'text/html'` will be used by default.
* @return {workbox.routing.Route~handlerCallback}
*
* @memberof workbox.streams
*/
function strategy(sourceFunctions, headersInit) {
return async ({
event,
request,
url,
params
}) => {
const sourcePromises = sourceFunctions.map(fn => {
// Ensure the return value of the function is always a promise.
return Promise.resolve(fn({
event,
request,
url,
params
}));
});
function strategy(sourceFunctions, headersInit) {
return async ({
event,
url,
params
}) => {
if (isSupported()) {
const {
done,
response
} = concatenateToResponse(sourceFunctions.map(fn => fn({
event,
url,
params
})), headersInit);
event.waitUntil(done);
return response;
}
if (isSupported()) {
const {
done,
response
} = concatenateToResponse(sourcePromises, headersInit);
{
logger_mjs.logger.log(`The current browser doesn't support creating response ` + `streams. Falling back to non-streaming response instead.`);
} // Fallback to waiting for everything to finish, and concatenating the
// responses.
if (event) {
event.waitUntil(done);
}
return response;
}
const parts = await Promise.all(sourceFunctions.map(sourceFunction => sourceFunction({
event,
url,
params
})).map(async responsePromise => {
const response = await responsePromise;
{
logger_js.logger.log(`The current browser doesn't support creating response ` + `streams. Falling back to non-streaming response instead.`);
} // Fallback to waiting for everything to finish, and concatenating the
// responses.
if (response instanceof Response) {
return response.blob();
} // Otherwise, assume it's something like a string which can be used
// as-is when constructing the final composite blob.
const blobPartsPromises = sourcePromises.map(async sourcePromise => {
const source = await sourcePromise;
return response;
}));
const headers = createHeaders(headersInit); // Constructing a new Response from a Blob source is well-supported.
// So is constructing a new Blob from multiple source Blobs or strings.
if (source instanceof Response) {
return source.blob();
} else {
// Technically, a `StreamSource` object can include any valid
// `BodyInit` type, including `FormData` and `URLSearchParams`, which
// cannot be passed to the Blob constructor directly, so we have to
// convert them to actual Blobs first.
return new Response(source).blob();
}
});
const blobParts = await Promise.all(blobPartsPromises);
const headers = createHeaders(headersInit); // Constructing a new Response from a Blob source is well-supported.
// So is constructing a new Blob from multiple source Blobs or strings.
return new Response(new Blob(parts), {
headers
});
};
}
return new Response(new Blob(blobParts), {
headers
});
};
}
/*
Copyright 2018 Google LLC
/*
Copyright 2018 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.
*/
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.
*/
exports.concatenate = concatenate;
exports.concatenateToResponse = concatenateToResponse;
exports.isSupported = isSupported;
exports.strategy = strategy;
exports.concatenate = concatenate;
exports.concatenateToResponse = concatenateToResponse;
exports.isSupported = isSupported;
exports.strategy = strategy;
return exports;
return exports;
}({}, workbox.core._private, workbox.core._private));
}({}, workbox.core._private, workbox.core._private, workbox.core._private));
//# sourceMappingURL=workbox-streams.dev.js.map

@@ -1,2 +0,2 @@

this.workbox=this.workbox||{},this.workbox.streams=function(e){"use strict";try{self["workbox:streams:5.0.0-alpha.0"]&&_()}catch(e){}function n(e){const n=e.map(e=>Promise.resolve(e).then(e=>(function(e){return e.body&&e.body.getReader?e.body.getReader():e.getReader?e.getReader():new Response(e).body.getReader()})(e)));let t,r;const s=new Promise((e,n)=>{t=e,r=n});let o=0;return{done:s,stream:new ReadableStream({pull(e){return n[o].then(e=>e.read()).then(r=>{if(r.done)return++o>=n.length?(e.close(),void t()):this.pull(e);e.enqueue(r.value)}).catch(e=>{throw r(e),e})},cancel(){t()}})}}function t(e={}){const n=new Headers(e);return n.has("content-type")||n.set("content-type","text/html"),n}function r(e,r){const{done:s,stream:o}=n(e),a=t(r);return{done:s,response:new Response(o,{headers:a})}}let s=void 0;function o(){if(void 0===s)try{new ReadableStream({start(){}}),s=!0}catch(e){s=!1}return s}return e.concatenate=n,e.concatenateToResponse=r,e.isSupported=o,e.strategy=function(e,n){return async({event:s,url:a,params:c})=>{if(o()){const{done:t,response:o}=r(e.map(e=>e({event:s,url:a,params:c})),n);return s.waitUntil(t),o}const i=await Promise.all(e.map(e=>e({event:s,url:a,params:c})).map(async e=>{const n=await e;return n instanceof Response?n.blob():n})),u=t(n);return new Response(new Blob(i),{headers:u})}},e}({});
this.workbox=this.workbox||{},this.workbox.streams=function(e,n){"use strict";try{self["workbox:streams:5.0.0-alpha.1"]&&_()}catch(e){}function t(e){const t=e.map(e=>Promise.resolve(e).then(e=>(function(e){return e instanceof Response?e.body.getReader():e instanceof ReadableStream?e.getReader():new Response(e).body.getReader()})(e))),r=new n.Deferred;let s=0;const o=new ReadableStream({pull(e){return t[s].then(e=>e.read()).then(n=>{if(n.done)return++s>=t.length?(e.close(),void r.resolve()):this.pull(e);e.enqueue(n.value)}).catch(e=>{throw r.reject(e),e})},cancel(){r.resolve()}});return{done:r.promise,stream:o}}function r(e={}){const n=new Headers(e);return n.has("content-type")||n.set("content-type","text/html"),n}function s(e,n){const{done:s,stream:o}=t(e),a=r(n);return{done:s,response:new Response(o,{headers:a})}}let o;function a(){if(void 0===o)try{new ReadableStream({start(){}}),o=!0}catch(e){o=!1}return o}return e.concatenate=t,e.concatenateToResponse=s,e.isSupported=a,e.strategy=function(e,n){return async({event:t,request:o,url:c,params:i})=>{const u=e.map(e=>Promise.resolve(e({event:t,request:o,url:c,params:i})));if(a()){const{done:e,response:r}=s(u,n);return t&&t.waitUntil(e),r}const f=u.map(async e=>{const n=await e;return n instanceof Response?n.blob():new Response(n).blob()}),l=await Promise.all(f),p=r(n);return new Response(new Blob(l),{headers:p})}},e}({},workbox.core._private);
//# sourceMappingURL=workbox-streams.prod.js.map
{
"name": "workbox-streams",
"version": "5.0.0-alpha.0",
"version": "5.0.0-alpha.1",
"license": "MIT",

@@ -27,8 +27,10 @@ "author": "Google's Web DevRel Team",

},
"main": "build/workbox-streams.prod.js",
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"dependencies": {
"workbox-core": "^5.0.0-alpha.0"
"workbox-core": "^5.0.0-alpha.1",
"workbox-routing": "^5.0.0-alpha.1"
},
"gitHead": "7f231c04023669bc42d5a939d1359b0867e2efda"
"gitHead": "20d2110ddace710a46af06addd4977cae08f5942"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc