Replies: 10 comments
-
You mean like getting json from a post or something? |
Beta Was this translation helpful? Give feedback.
-
hello, thanks for project. Can anybody say how to check empty or no empty data? |
Beta Was this translation helpful? Give feedback.
-
I guess onData has to be called with isLast set immediately if there was no data otherwise we hang the connection forever |
Beta Was this translation helpful? Give feedback.
-
@alexhultman Yes, that is why I was need to check empty data, or there is another way to check and immediately close the connection. thanks |
Beta Was this translation helpful? Give feedback.
-
Yeah it is a bug - onData needs to return false/true or immediately call the callback with isLast. Something like that. I guess the most automatically compatible way is to call with isLast immediately |
Beta Was this translation helpful? Give feedback.
-
Okay this is fixed now. You can see the JsonPost example. It tells you when there is no data and when there is |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
-
You mean like getting json from a post or something? Yes - json body parsing. graphql body parsing to be exact. |
Beta Was this translation helpful? Give feedback.
-
type TPromiseObjectNull = Promise<{ [key: string]: any } | null>;
export default (res): TPromiseObjectNull => new Promise((resolve, reject) => {
let buffer;
res.onData((ab, isLast) => {
const chunk = Buffer.from(ab);
if (isLast) {
const toParse = buffer
? Buffer.concat([buffer, chunk])
: chunk;
const resolveValue = JSON.parse(toParse as unknown as string);
resolve(resolveValue);
}
else {
const concatValue = buffer
? [buffer, chunk]
: [chunk];
buffer = Buffer.concat(concatValue);
}
});
res.onAborted(_ => reject(null));
}); |
Beta Was this translation helpful? Give feedback.
-
I edited @yamiteru response. this is working for me. const parseBody = (res: uWS.HttpResponse): Promise<{ [key: string]: any } | null> => {
return new Promise((resolve, reject) => {
let buffer: Buffer = Buffer.alloc(0);
let totalSize = 0;
res.onData((ab, isLast) => {
try {
if (res.aborted) {
reject(new Error('Request aborted'));
return;
}
if(ab.byteLength > 0) { // I found some non-last onData with 0 byte length
const copy = copyArrayBuffer(ab); // Immediately copy the ArrayBuffer into a Buffer, every return of onData neuters the ArrayBuffer
totalSize += copy.byteLength;
buffer = Buffer.concat([buffer, Buffer.from(copy)]);
}
if (totalSize > MAX_BODY_SIZE) { // define your allowed max size if it applies to you
reject(new Error('Request body too large: max 4MB allowed'));
return;
}
if (isLast) {
// If this is the last chunk, process the final buffer
// Convert the buffer to a string and parse it as JSON
// this will fail if the buffer doesn't contain a valid JSON (e.g. length = 0)
const resolveValue = JSON.parse(buffer.toString());
resolve(resolveValue);
}
} catch (err: any) {
reject(new Error(`Failed to parse JSON: ${err.message}`));
}
});
});
}; |
Beta Was this translation helpful? Give feedback.
-
This library performs better than turbo-http under high load, congrats.
Now I am a little confused about the api, how would you implement a body parser?
I am trying to stitch it to an Apollo GraphQL instance
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions