@whatwg-node/server 0.9.37 → 0.9.39
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.
|
@@ -8,7 +8,7 @@ function useContentEncoding() {
|
|
|
8
8
|
onRequest({ request, setRequest, fetchAPI, endResponse }) {
|
|
9
9
|
if (request.body) {
|
|
10
10
|
const contentEncodingHeader = request.headers.get('content-encoding');
|
|
11
|
-
if (contentEncodingHeader) {
|
|
11
|
+
if (contentEncodingHeader && contentEncodingHeader !== 'none') {
|
|
12
12
|
const contentEncodings = contentEncodingHeader?.split(',');
|
|
13
13
|
if (!contentEncodings.every(encoding => (0, utils_js_1.getSupportedEncodings)().includes(encoding))) {
|
|
14
14
|
endResponse(new fetchAPI.Response(`Unsupported 'Content-Encoding': ${contentEncodingHeader}`, {
|
|
@@ -34,15 +34,51 @@ function useContentEncoding() {
|
|
|
34
34
|
}
|
|
35
35
|
},
|
|
36
36
|
onResponse({ request, response, setResponse, fetchAPI }) {
|
|
37
|
-
|
|
37
|
+
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed
|
|
38
|
+
if (response['bodyInit'] || response.body) {
|
|
38
39
|
const encodings = encodingMap.get(request);
|
|
39
40
|
if (encodings) {
|
|
40
41
|
const supportedEncoding = encodings.find(encoding => (0, utils_js_1.getSupportedEncodings)().includes(encoding));
|
|
41
42
|
if (supportedEncoding) {
|
|
43
|
+
const compressionStream = new CompressionStream(supportedEncoding);
|
|
44
|
+
// To calculate final content-length
|
|
45
|
+
const contentLength = response.headers.get('content-length');
|
|
46
|
+
if (contentLength) {
|
|
47
|
+
const bufOfRes = response._buffer;
|
|
48
|
+
if (bufOfRes) {
|
|
49
|
+
const writer = compressionStream.writable.getWriter();
|
|
50
|
+
writer.write(bufOfRes);
|
|
51
|
+
writer.close();
|
|
52
|
+
const reader = compressionStream.readable.getReader();
|
|
53
|
+
return Promise.resolve().then(async () => {
|
|
54
|
+
const chunks = [];
|
|
55
|
+
while (true) {
|
|
56
|
+
const { done, value } = await reader.read();
|
|
57
|
+
if (done) {
|
|
58
|
+
reader.releaseLock();
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
else if (value) {
|
|
62
|
+
chunks.push(...value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const uint8Array = new Uint8Array(chunks);
|
|
66
|
+
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
67
|
+
newHeaders.set('content-encoding', supportedEncoding);
|
|
68
|
+
newHeaders.set('content-length', uint8Array.byteLength.toString());
|
|
69
|
+
const compressedResponse = new fetchAPI.Response(uint8Array, {
|
|
70
|
+
...response,
|
|
71
|
+
headers: newHeaders,
|
|
72
|
+
});
|
|
73
|
+
utils_js_1.decompressedResponseMap.set(compressedResponse, response);
|
|
74
|
+
setResponse(compressedResponse);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
42
78
|
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
43
79
|
newHeaders.set('content-encoding', supportedEncoding);
|
|
44
80
|
newHeaders.delete('content-length');
|
|
45
|
-
const compressedBody = response.body.pipeThrough(
|
|
81
|
+
const compressedBody = response.body.pipeThrough(compressionStream);
|
|
46
82
|
const compressedResponse = new fetchAPI.Response(compressedBody, {
|
|
47
83
|
...response,
|
|
48
84
|
headers: newHeaders,
|
package/cjs/utils.js
CHANGED
|
@@ -522,7 +522,7 @@ function getSupportedEncodings() {
|
|
|
522
522
|
}
|
|
523
523
|
function handleResponseDecompression(response, ResponseCtor) {
|
|
524
524
|
const contentEncodingHeader = response?.headers.get('content-encoding');
|
|
525
|
-
if (!contentEncodingHeader) {
|
|
525
|
+
if (!contentEncodingHeader || contentEncodingHeader === 'none') {
|
|
526
526
|
return response;
|
|
527
527
|
}
|
|
528
528
|
if (!response?.body) {
|
|
@@ -5,7 +5,7 @@ export function useContentEncoding() {
|
|
|
5
5
|
onRequest({ request, setRequest, fetchAPI, endResponse }) {
|
|
6
6
|
if (request.body) {
|
|
7
7
|
const contentEncodingHeader = request.headers.get('content-encoding');
|
|
8
|
-
if (contentEncodingHeader) {
|
|
8
|
+
if (contentEncodingHeader && contentEncodingHeader !== 'none') {
|
|
9
9
|
const contentEncodings = contentEncodingHeader?.split(',');
|
|
10
10
|
if (!contentEncodings.every(encoding => getSupportedEncodings().includes(encoding))) {
|
|
11
11
|
endResponse(new fetchAPI.Response(`Unsupported 'Content-Encoding': ${contentEncodingHeader}`, {
|
|
@@ -31,15 +31,51 @@ export function useContentEncoding() {
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
onResponse({ request, response, setResponse, fetchAPI }) {
|
|
34
|
-
|
|
34
|
+
// Hack for avoiding to create whatwg-node to create a readable stream until it's needed
|
|
35
|
+
if (response['bodyInit'] || response.body) {
|
|
35
36
|
const encodings = encodingMap.get(request);
|
|
36
37
|
if (encodings) {
|
|
37
38
|
const supportedEncoding = encodings.find(encoding => getSupportedEncodings().includes(encoding));
|
|
38
39
|
if (supportedEncoding) {
|
|
40
|
+
const compressionStream = new CompressionStream(supportedEncoding);
|
|
41
|
+
// To calculate final content-length
|
|
42
|
+
const contentLength = response.headers.get('content-length');
|
|
43
|
+
if (contentLength) {
|
|
44
|
+
const bufOfRes = response._buffer;
|
|
45
|
+
if (bufOfRes) {
|
|
46
|
+
const writer = compressionStream.writable.getWriter();
|
|
47
|
+
writer.write(bufOfRes);
|
|
48
|
+
writer.close();
|
|
49
|
+
const reader = compressionStream.readable.getReader();
|
|
50
|
+
return Promise.resolve().then(async () => {
|
|
51
|
+
const chunks = [];
|
|
52
|
+
while (true) {
|
|
53
|
+
const { done, value } = await reader.read();
|
|
54
|
+
if (done) {
|
|
55
|
+
reader.releaseLock();
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
else if (value) {
|
|
59
|
+
chunks.push(...value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const uint8Array = new Uint8Array(chunks);
|
|
63
|
+
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
64
|
+
newHeaders.set('content-encoding', supportedEncoding);
|
|
65
|
+
newHeaders.set('content-length', uint8Array.byteLength.toString());
|
|
66
|
+
const compressedResponse = new fetchAPI.Response(uint8Array, {
|
|
67
|
+
...response,
|
|
68
|
+
headers: newHeaders,
|
|
69
|
+
});
|
|
70
|
+
decompressedResponseMap.set(compressedResponse, response);
|
|
71
|
+
setResponse(compressedResponse);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
39
75
|
const newHeaders = new fetchAPI.Headers(response.headers);
|
|
40
76
|
newHeaders.set('content-encoding', supportedEncoding);
|
|
41
77
|
newHeaders.delete('content-length');
|
|
42
|
-
const compressedBody = response.body.pipeThrough(
|
|
78
|
+
const compressedBody = response.body.pipeThrough(compressionStream);
|
|
43
79
|
const compressedResponse = new fetchAPI.Response(compressedBody, {
|
|
44
80
|
...response,
|
|
45
81
|
headers: newHeaders,
|
package/esm/utils.js
CHANGED
|
@@ -500,7 +500,7 @@ export function getSupportedEncodings() {
|
|
|
500
500
|
}
|
|
501
501
|
export function handleResponseDecompression(response, ResponseCtor) {
|
|
502
502
|
const contentEncodingHeader = response?.headers.get('content-encoding');
|
|
503
|
-
if (!contentEncodingHeader) {
|
|
503
|
+
if (!contentEncodingHeader || contentEncodingHeader === 'none') {
|
|
504
504
|
return response;
|
|
505
505
|
}
|
|
506
506
|
if (!response?.body) {
|