Skip to content

Commit

Permalink
Use buffer.transfer()
Browse files Browse the repository at this point in the history
  • Loading branch information
uasan committed Feb 12, 2025
1 parent 7d65114 commit 77cd1f7
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/runtime/server/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export function readBuffer(req, res, maxLength = Server.maxByteLengthBody) {
let buffer = null;
res.context.onAborted = reject;

res.onData((chunk, done) => {
if (chunk.byteLength === length) {
resolve(new Uint8Array(chunk.transfer()));
} else if (buffer) {
res.onData((chunk, isDone) => {
if (buffer) {
buffer.set(new Uint8Array(chunk), offset);

if (done) resolve(buffer);
if (isDone) resolve(buffer);
else offset += chunk.byteLength;
} else if (isDone) {
resolve(new Uint8Array(chunk.transfer()));
} else {
offset = chunk.byteLength;
buffer = new Uint8Array(chunk.transfer(length));
Expand All @@ -38,13 +38,13 @@ export function readBuffer(req, res, maxLength = Server.maxByteLengthBody) {

export function readBufferStream(req, res) {
const reader = new BufferStreamReader(res);
const { promise, resolve, reject } = Promise.withResolvers()

return new Promise((resolve, reject) => {
reader.resolve = resolve;
reader.reject = reject;

res.context.onAborted = reject;
reader.reject = reject;
reader.resolve = resolve;

res.context.onAborted = reject;
res.onData(reader.onData);

res.onData(reader.onData);
});
return promise;
}

0 comments on commit 77cd1f7

Please sign in to comment.