@whatwg-node/server 0.9.38 → 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.
|
@@ -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,
|
|
@@ -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,
|