@whatwg-node/server 0.9.48 → 0.9.49
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.
|
@@ -47,6 +47,7 @@ function useContentEncoding() {
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
onResponse({ request, response, setResponse, fetchAPI, serverContext }) {
|
|
50
|
+
const waitUntil = serverContext.waitUntil?.bind(serverContext) || (() => { });
|
|
50
51
|
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed
|
|
51
52
|
if (response['bodyInit'] || response.body) {
|
|
52
53
|
const encodings = encodingMap.get(request);
|
|
@@ -60,21 +61,15 @@ function useContentEncoding() {
|
|
|
60
61
|
const bufOfRes = response._buffer;
|
|
61
62
|
if (bufOfRes) {
|
|
62
63
|
const writer = compressionStream.writable.getWriter();
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
else if (value) {
|
|
75
|
-
chunks.push(...value);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
64
|
+
waitUntil(writer.write(bufOfRes));
|
|
65
|
+
waitUntil(writer.close());
|
|
66
|
+
const uint8Arrays$ = (0, utils_js_1.isReadable)(compressionStream.readable['readable'])
|
|
67
|
+
? collectReadableValues(compressionStream.readable['readable'])
|
|
68
|
+
: (0, utils_js_1.isAsyncIterable)(compressionStream.readable)
|
|
69
|
+
? collectAsyncIterableValues(compressionStream.readable)
|
|
70
|
+
: collectReadableStreamValues(compressionStream.readable);
|
|
71
|
+
return uint8Arrays$.then(uint8Arrays => {
|
|
72
|
+
const chunks = uint8Arrays.flatMap(uint8Array => [...uint8Array]);
|
|
78
73
|
const uint8Array = new Uint8Array(chunks);
|
|
79
74
|
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
80
75
|
newHeaders.set('content-encoding', supportedEncoding);
|
|
@@ -85,7 +80,7 @@ function useContentEncoding() {
|
|
|
85
80
|
});
|
|
86
81
|
utils_js_1.decompressedResponseMap.set(compressedResponse, response);
|
|
87
82
|
setResponse(compressedResponse);
|
|
88
|
-
|
|
83
|
+
waitUntil(compressionStream.writable.close());
|
|
89
84
|
});
|
|
90
85
|
}
|
|
91
86
|
}
|
|
@@ -106,3 +101,33 @@ function useContentEncoding() {
|
|
|
106
101
|
},
|
|
107
102
|
};
|
|
108
103
|
}
|
|
104
|
+
function collectReadableValues(readable) {
|
|
105
|
+
const values = [];
|
|
106
|
+
readable.on('data', value => values.push(value));
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
readable.once('end', () => resolve(values));
|
|
109
|
+
readable.once('error', reject);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async function collectAsyncIterableValues(asyncIterable) {
|
|
113
|
+
const values = [];
|
|
114
|
+
for await (const value of asyncIterable) {
|
|
115
|
+
values.push(value);
|
|
116
|
+
}
|
|
117
|
+
return values;
|
|
118
|
+
}
|
|
119
|
+
async function collectReadableStreamValues(readableStream) {
|
|
120
|
+
const reader = readableStream.getReader();
|
|
121
|
+
const values = [];
|
|
122
|
+
while (true) {
|
|
123
|
+
const { done, value } = await reader.read();
|
|
124
|
+
if (done) {
|
|
125
|
+
reader.releaseLock();
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
else if (value) {
|
|
129
|
+
values.push(value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return values;
|
|
133
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { decompressedResponseMap, getSupportedEncodings } from '../utils.js';
|
|
1
|
+
import { decompressedResponseMap, getSupportedEncodings, isAsyncIterable, isReadable, } from '../utils.js';
|
|
2
2
|
export function useContentEncoding() {
|
|
3
3
|
const encodingMap = new WeakMap();
|
|
4
4
|
return {
|
|
@@ -44,6 +44,7 @@ export function useContentEncoding() {
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
onResponse({ request, response, setResponse, fetchAPI, serverContext }) {
|
|
47
|
+
const waitUntil = serverContext.waitUntil?.bind(serverContext) || (() => { });
|
|
47
48
|
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed
|
|
48
49
|
if (response['bodyInit'] || response.body) {
|
|
49
50
|
const encodings = encodingMap.get(request);
|
|
@@ -57,21 +58,15 @@ export function useContentEncoding() {
|
|
|
57
58
|
const bufOfRes = response._buffer;
|
|
58
59
|
if (bufOfRes) {
|
|
59
60
|
const writer = compressionStream.writable.getWriter();
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
else if (value) {
|
|
72
|
-
chunks.push(...value);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
61
|
+
waitUntil(writer.write(bufOfRes));
|
|
62
|
+
waitUntil(writer.close());
|
|
63
|
+
const uint8Arrays$ = isReadable(compressionStream.readable['readable'])
|
|
64
|
+
? collectReadableValues(compressionStream.readable['readable'])
|
|
65
|
+
: isAsyncIterable(compressionStream.readable)
|
|
66
|
+
? collectAsyncIterableValues(compressionStream.readable)
|
|
67
|
+
: collectReadableStreamValues(compressionStream.readable);
|
|
68
|
+
return uint8Arrays$.then(uint8Arrays => {
|
|
69
|
+
const chunks = uint8Arrays.flatMap(uint8Array => [...uint8Array]);
|
|
75
70
|
const uint8Array = new Uint8Array(chunks);
|
|
76
71
|
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
77
72
|
newHeaders.set('content-encoding', supportedEncoding);
|
|
@@ -82,7 +77,7 @@ export function useContentEncoding() {
|
|
|
82
77
|
});
|
|
83
78
|
decompressedResponseMap.set(compressedResponse, response);
|
|
84
79
|
setResponse(compressedResponse);
|
|
85
|
-
|
|
80
|
+
waitUntil(compressionStream.writable.close());
|
|
86
81
|
});
|
|
87
82
|
}
|
|
88
83
|
}
|
|
@@ -103,3 +98,33 @@ export function useContentEncoding() {
|
|
|
103
98
|
},
|
|
104
99
|
};
|
|
105
100
|
}
|
|
101
|
+
function collectReadableValues(readable) {
|
|
102
|
+
const values = [];
|
|
103
|
+
readable.on('data', value => values.push(value));
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
readable.once('end', () => resolve(values));
|
|
106
|
+
readable.once('error', reject);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
async function collectAsyncIterableValues(asyncIterable) {
|
|
110
|
+
const values = [];
|
|
111
|
+
for await (const value of asyncIterable) {
|
|
112
|
+
values.push(value);
|
|
113
|
+
}
|
|
114
|
+
return values;
|
|
115
|
+
}
|
|
116
|
+
async function collectReadableStreamValues(readableStream) {
|
|
117
|
+
const reader = readableStream.getReader();
|
|
118
|
+
const values = [];
|
|
119
|
+
while (true) {
|
|
120
|
+
const { done, value } = await reader.read();
|
|
121
|
+
if (done) {
|
|
122
|
+
reader.releaseLock();
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
else if (value) {
|
|
126
|
+
values.push(value);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return values;
|
|
130
|
+
}
|