Fix test-http-chunk-problem on macOS

This commit is contained in:
Jarred Sumner
2025-01-26 05:17:01 -08:00
parent 7410da9c71
commit 71fec1f541
3 changed files with 46 additions and 71 deletions

View File

@@ -55,12 +55,7 @@ export function asyncIterator(this: Console) {
}
while (true) {
const firstResult = reader.readMany();
if ($isPromise(firstResult)) {
({ done, value } = await firstResult);
} else {
({ done, value } = firstResult);
}
({ done, value } = await reader.read());
if (done) {
if (pendingChunk) {
@@ -69,33 +64,30 @@ export function asyncIterator(this: Console) {
return;
}
// we assume it was given line-by-line
for (idx = 0, value_len = value.length; idx < value_len; idx++) {
actualChunk = value[idx];
if (pendingChunk) {
actualChunk = Buffer.concat([pendingChunk, actualChunk]);
pendingChunk = undefined;
}
last = 0;
// TODO: "\r", 0x4048, 0x4049, 0x404A, 0x404B, 0x404C, 0x404D, 0x404E, 0x404F
i = indexOf(actualChunk, last);
while (i !== -1) {
// This yield may end the function, in that case we need to be able to recover state
// if the iterator was fired up again.
yield decoder.decode(
actualChunk.subarray(
last,
process.platform === "win32" ? (actualChunk[i - 1] === 0x0d /* \r */ ? i - 1 : i) : i,
),
);
last = i + 1;
i = indexOf(actualChunk, last);
}
i = -1;
pendingChunk = actualChunk.subarray(last);
actualChunk = value as unknown as Uint8Array;
if (pendingChunk) {
actualChunk = Buffer.concat([pendingChunk, actualChunk]);
pendingChunk = undefined;
}
last = 0;
// TODO: "\r", 0x4048, 0x4049, 0x404A, 0x404B, 0x404C, 0x404D, 0x404E, 0x404F
i = indexOf(actualChunk, last);
while (i !== -1) {
// This yield may end the function, in that case we need to be able to recover state
// if the iterator was fired up again.
yield decoder.decode(
actualChunk.subarray(
last,
process.platform === "win32" ? (actualChunk[i - 1] === 0x0d /* \r */ ? i - 1 : i) : i,
),
);
last = i + 1;
i = indexOf(actualChunk, last);
}
i = -1;
pendingChunk = actualChunk.subarray(last);
actualChunk = undefined!;
}
} catch (e) {

View File

@@ -101,19 +101,11 @@ class ReadableFromWeb extends Readable {
var deferredError;
try {
do {
var done = false,
value;
const firstResult = reader.readMany();
var { done, value } = await reader.read();
if ($isPromise(firstResult)) {
({ done, value } = await firstResult);
if (this.#closed) {
this.#pendingChunks.push(...value);
return;
}
} else {
({ done, value } = firstResult);
if (this.#closed) {
this.#pendingChunks.push(value);
return;
}
if (done) {
@@ -121,17 +113,9 @@ class ReadableFromWeb extends Readable {
return;
}
if (!this.push(value[0])) {
this.#pendingChunks = value.slice(1);
if (!this.push(value)) {
return;
}
for (let i = 1, count = value.length; i < count; i++) {
if (!this.push(value[i])) {
this.#pendingChunks = value.slice(i + 1);
return;
}
}
} while (!this.#closed);
} catch (e) {
deferredError = e;

View File

@@ -857,7 +857,7 @@ IncomingMessage.prototype = {
this.complete = true;
this.push(null);
} else if (this[bodyStreamSymbol] == null) {
const reader = this[reqSymbol].body?.getReader() as ReadableStreamDefaultReader;
const reader = this[reqSymbol]?.body?.getReader() as ReadableStreamDefaultReader;
if (!reader) {
this.complete = true;
this.push(null);
@@ -968,27 +968,19 @@ $setPrototypeDirect.$call(IncomingMessage.prototype, Readable.prototype);
$setPrototypeDirect.$call(IncomingMessage, Readable);
async function consumeStream(self, reader: ReadableStreamDefaultReader) {
var done = false,
value,
aborted = false;
var aborted = false;
try {
while (true) {
const result = reader.readMany();
if ($isPromise(result)) {
({ done, value } = await result);
} else {
({ done, value } = result);
var { done, value } = await reader.read();
if (done) {
break;
}
if (self.destroyed || (aborted = self[abortedSymbol])) {
break;
}
for (var v of value) {
self.push(v);
}
if (self.destroyed || (aborted = self[abortedSymbol]) || done) {
break;
}
self.push(value);
}
} catch (err) {
if (aborted || self.destroyed) return;
@@ -1546,12 +1538,19 @@ class ClientRequest extends OutgoingMessage {
_write(chunk, encoding, callback) {
if (this.#controller) {
let promise;
if (typeof chunk === "string") {
this.#controller.write(Buffer.from(chunk, encoding));
promise = this.#controller.write(Buffer.from(chunk, encoding));
} else {
this.#controller.write(chunk);
promise = this.#controller.write(chunk);
}
if ($isPromise(promise)) {
promise.then(() => {
callback(null);
});
} else {
callback(null);
}
process.nextTick(callback);
return;
}
if (!this.#bodyChunks) {