function to get data in one go in v20.25.0 #908
git-matrix
started this conversation in
Show and tell
Replies: 3 comments 9 replies
-
const readStream = (req: WebRequest, res: WebResponse): Promise<Uint8Array> => new Promise((resolve, reject) => {
const unitArray = new Uint8Array(parseFloat(req.getHeader('content-length')) || 0);
if (unitArray.byteLength > 0 && unitArray.byteLength < 1024 * 1024 * 10) {
let offset: number = 0; res
.onData((data, done) => {
unitArray
.set(unitArray, offset);
offset += data.byteLength;
if (done)
resolve(unitArray);
}).onAborted(() => reject(``));
} else
resolve(unitArray);
}); Is this correct way? |
Beta Was this translation helpful? Give feedback.
4 replies
-
const getBody = (res, req, maxSize = 1e6) => new Promise((resolve, reject) => {
const contentLength = Number(req.getHeader('content-length'));
if (!(contentLength > 0)) return reject('Missing or zero content-length header');
if (contentLength > maxSize) return reject(`maxSize ${maxSize} exceeded: ${contentLength}`);
let uint8Array, offset = 0;
res.onAborted(() => {
res.ab = true;
reject('aborted');
});
res.onData((arrayBuffer, isLast) => {
if (!arrayBuffer.byteLength && !isLast) return;
const total = offset + arrayBuffer.byteLength;
if (total > contentLength) {
reject(`Body larger than content-length: ${contentLength}`);
return res.close();
}
if (isLast && total != contentLength) return reject(`Body size ${total} != content-length ${contentLength}`);
if (!uint8Array) {
if (isLast) return resolve(arrayBuffer);
uint8Array = new Uint8Array(contentLength);
}
uint8Array.set(new Uint8Array(arrayBuffer), offset);
if (isLast) return resolve(uint8Array);
offset = total;
});
}); |
Beta Was this translation helpful? Give feedback.
4 replies
-
@NexaraDev Can we go back and focus on the main issue:
Please explain how this relates to for instance the Upload.js example - what method, how do you trigger this failure? Curl example? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
At first tested this with
v20.25.0
with Alien mode, then withv20.24.0
. Both of them worked fine when I sent some Json body. But, whenever I sent a request with empty body or no content, the request get timeout.Beta Was this translation helpful? Give feedback.
All reactions