From bcff56a4b698bb390afa471c96e2a9406abeb54c Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Fri, 6 Sep 2024 16:20:52 -0700 Subject: [PATCH] still fails sometimes --- src/js/node/http.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 1392da5ff7..308573d9dd 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -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;