still fails sometimes

This commit is contained in:
Ciro Spaciari
2024-09-06 16:20:52 -07:00
parent 6f5df574e5
commit bcff56a4b6

View File

@@ -1255,7 +1255,8 @@ function flushFirstWrite(self) {
let firstWrite = self[firstWriteSymbol];
// at this point, the user did not call end and we have not flushed the first write
// we need to flush it now and behave like chunked encoding
const { promise: controllerPromise, resolve: resolveController } = $newPromiseCapability(GlobalPromise);
self[controllerSymbol] = controllerPromise;
self._reply(
new Response(
new ReadableStream({
@@ -1264,6 +1265,8 @@ function flushFirstWrite(self) {
self[controllerSymbol] = controller;
if (firstWrite) controller.write(firstWrite);
firstWrite = undefined;
resolveController(controller);
if (!self[finishedSymbol]) {
const { promise, resolve } = $newPromiseCapability(GlobalPromise);
self[deferredSymbol] = resolve;
@@ -1286,8 +1289,7 @@ ServerResponse.prototype._write = function (chunk, encoding, callback) {
// we still wanna to flush if the user await some time before writing again
// keeping the first write is a good performance optimization
// process.nextTick(flushFirstWrite, this);
// flushFirstWrite(this);
setTimeout(flushFirstWrite, 1, this);
return;
}
@@ -1315,10 +1317,16 @@ ServerResponse.prototype._writev = function (chunks, callback) {
function ensureReadableStreamController(run) {
const thisController = this[controllerSymbol];
if (thisController) return run(thisController);
if (thisController) {
if (thisController.then) {
return thisController.then(run);
}
return run(thisController);
}
this.headersSent = true;
let firstWrite = this[firstWriteSymbol];
this[controllerSymbol] = undefined;
const { promise: controllerPromise, resolve: resolveController } = $newPromiseCapability(GlobalPromise);
this[controllerSymbol] = controllerPromise;
this._reply(
new Response(
new ReadableStream({
@@ -1328,6 +1336,8 @@ function ensureReadableStreamController(run) {
if (firstWrite) controller.write(firstWrite);
firstWrite = undefined;
run(controller);
resolveController(controller);
if (!this[finishedSymbol]) {
const { promise, resolve } = $newPromiseCapability(GlobalPromise);
this[deferredSymbol] = resolve;