@serwist/streams 9.0.0-preview.0 → 9.0.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +6 -85
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,16 +1,7 @@
1
1
  import { assert, Deferred, logger, SerwistError, canConstructReadableStream } from '@serwist/core/internal';
2
2
 
3
- /**
4
- * Takes either a Response, a ReadableStream, or a
5
- * [BodyInit](https://fetch.spec.whatwg.org/#bodyinit) and returns the
6
- * ReadableStreamReader object associated with it.
7
- *
8
- * @param source
9
- * @returns
10
- * @private
11
- */ function _getReaderFromSource(source) {
3
+ function _getReaderFromSource(source) {
12
4
  if (source instanceof Response) {
13
- // See https://github.com/GoogleChrome/workbox/issues/2998
14
5
  if (source.body) {
15
6
  return source.body.getReader();
16
7
  }
@@ -23,17 +14,7 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
23
14
  }
24
15
  return new Response(source).body.getReader();
25
16
  }
26
- /**
27
- * Takes multiple source Promises, each of which could resolve to a Response, a
28
- * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit).
29
- *
30
- * Returns an object exposing a ReadableStream with each individual stream's
31
- * data returned in sequence, along with a Promise which signals when the
32
- * stream is finished (useful for passing to a FetchEvent's waitUntil()).
33
- *
34
- * @param sourcePromises
35
- * @returns
36
- */ function concatenate(sourcePromises) {
17
+ function concatenate(sourcePromises) {
37
18
  if (process.env.NODE_ENV !== "production") {
38
19
  assert.isArray(sourcePromises, {
39
20
  moduleName: "@serwist/streams",
@@ -66,7 +47,6 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
66
47
  }
67
48
  i++;
68
49
  if (i >= readerPromises.length) {
69
- // Log all the messages in the group at once in a single group.
70
50
  if (process.env.NODE_ENV !== "production") {
71
51
  logger.groupCollapsed(`Concatenating ${readerPromises.length} sources.`);
72
52
  for (const message of logMessages){
@@ -83,7 +63,6 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
83
63
  streamDeferred.resolve();
84
64
  return;
85
65
  }
86
- // The `pull` method is defined because we're inside it.
87
66
  return this.pull(controller);
88
67
  }
89
68
  controller.enqueue(result?.value);
@@ -108,24 +87,7 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
108
87
  };
109
88
  }
110
89
 
111
- /*
112
- Copyright 2018 Google LLC
113
-
114
- Use of this source code is governed by an MIT-style
115
- license that can be found in the LICENSE file or at
116
- https://opensource.org/licenses/MIT.
117
- */ /**
118
- * This is a utility method that determines whether the current browser supports
119
- * the features required to create streamed responses. Currently, it checks if
120
- * [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
121
- * is available.
122
- *
123
- * @private
124
- * @param headersInit If there's no `Content-Type` specified, `'text/html'` will be used by default.
125
- * @returns `true`, if the current browser meets the requirements for
126
- * streaming responses, and `false` otherwise.
127
- */ function createHeaders(headersInit = {}) {
128
- // See https://github.com/GoogleChrome/workbox/issues/1461
90
+ function createHeaders(headersInit = {}) {
129
91
  const headers = new Headers(headersInit);
130
92
  if (!headers.has("content-type")) {
131
93
  headers.set("content-type", "text/html");
@@ -133,20 +95,7 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
133
95
  return headers;
134
96
  }
135
97
 
136
- /**
137
- * Takes multiple source Promises, each of which could resolve to a Response, a
138
- * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit),
139
- * along with a
140
- * [HeadersInit](https://fetch.spec.whatwg.org/#typedefdef-headersinit).
141
- *
142
- * Returns an object exposing a Response whose body consists of each individual
143
- * stream's data returned in sequence, along with a Promise which signals when
144
- * the stream is finished (useful for passing to a FetchEvent's waitUntil()).
145
- *
146
- * @param sourcePromises
147
- * @param headersInit If there's no `Content-Type` specified,`'text/html'` will be used by default.
148
- * @returns
149
- */ function concatenateToResponse(sourcePromises, headersInit) {
98
+ function concatenateToResponse(sourcePromises, headersInit) {
150
99
  const { done, stream } = concatenate(sourcePromises);
151
100
  const headers = createHeaders(headersInit);
152
101
  const response = new Response(stream, {
@@ -158,33 +107,13 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
158
107
  };
159
108
  }
160
109
 
161
- /**
162
- * This is a utility method that determines whether the current browser supports
163
- * the features required to create streamed responses. Currently, it checks if
164
- * [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream)
165
- * can be created.
166
- *
167
- * @returns `true`, if the current browser meets the requirements for
168
- * streaming responses, and `false` otherwise.
169
- */ function isSupported() {
110
+ function isSupported() {
170
111
  return canConstructReadableStream();
171
112
  }
172
113
 
173
- /**
174
- * A shortcut to create a strategy that could be dropped-in to Serwist's router.
175
- *
176
- * On browsers that do not support constructing new `ReadableStream`s, this
177
- * strategy will automatically wait for all the `sourceFunctions` to complete,
178
- * and create a final response that concatenates their values together.
179
- *
180
- * @param sourceFunctions An array of functions similar to `@serwist/routing.handlerCallback`
181
- * but that instead return a `@serwist/streams.StreamSource` (or a Promise which resolves to one).
182
- * @param headersInit If there's no `Content-Type` specified, `'text/html'` will be used by default.
183
- * @returns
184
- */ function strategy(sourceFunctions, headersInit) {
114
+ function strategy(sourceFunctions, headersInit) {
185
115
  return async ({ event, request, url, params })=>{
186
116
  const sourcePromises = sourceFunctions.map((fn)=>{
187
- // Ensure the return value of the function is always a promise.
188
117
  return Promise.resolve(fn({
189
118
  event,
190
119
  request,
@@ -202,23 +131,15 @@ import { assert, Deferred, logger, SerwistError, canConstructReadableStream } fr
202
131
  if (process.env.NODE_ENV !== "production") {
203
132
  logger.log(`The current browser doesn't support creating response ` + "streams. Falling back to non-streaming response instead.");
204
133
  }
205
- // Fallback to waiting for everything to finish, and concatenating the
206
- // responses.
207
134
  const blobPartsPromises = sourcePromises.map(async (sourcePromise)=>{
208
135
  const source = await sourcePromise;
209
136
  if (source instanceof Response) {
210
137
  return source.blob();
211
138
  }
212
- // Technically, a `StreamSource` object can include any valid
213
- // `BodyInit` type, including `FormData` and `URLSearchParams`, which
214
- // cannot be passed to the Blob constructor directly, so we have to
215
- // convert them to actual Blobs first.
216
139
  return new Response(source).blob();
217
140
  });
218
141
  const blobParts = await Promise.all(blobPartsPromises);
219
142
  const headers = createHeaders(headersInit);
220
- // Constructing a new Response from a Blob source is well-supported.
221
- // So is constructing a new Blob from multiple source Blobs or strings.
222
143
  return new Response(new Blob(blobParts), {
223
144
  headers
224
145
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/streams",
3
- "version": "9.0.0-preview.0",
3
+ "version": "9.0.0-preview.1",
4
4
  "type": "module",
5
5
  "description": "A library that makes it easier to work with Streams in the browser.",
6
6
  "files": [
@@ -30,13 +30,13 @@
30
30
  "./package.json": "./package.json"
31
31
  },
32
32
  "dependencies": {
33
- "@serwist/core": "9.0.0-preview.0",
34
- "@serwist/routing": "9.0.0-preview.0"
33
+ "@serwist/core": "9.0.0-preview.1",
34
+ "@serwist/routing": "9.0.0-preview.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "rollup": "4.9.6",
38
38
  "typescript": "5.4.0-dev.20240203",
39
- "@serwist/constants": "9.0.0-preview.0"
39
+ "@serwist/constants": "9.0.0-preview.1"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "typescript": ">=5.0.0"