Skip to content

Commit 8ccdc4b

Browse files
committed
Remove finishedSymbol and de-getterify the finished property
1 parent 514d447 commit 8ccdc4b

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/js/node/http.ts

+21-29
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const runSymbol = Symbol("run");
3939
const deferredSymbol = Symbol("deferred");
4040
const eofInProgress = Symbol("eofInProgress");
4141
const fakeSocketSymbol = Symbol("fakeSocket");
42-
const finishedSymbol = Symbol("finished");
4342
const firstWriteSymbol = Symbol("firstWrite");
4443
const headersSymbol = Symbol("headers");
4544
const isTlsSymbol = Symbol("is_tls");
@@ -677,14 +676,14 @@ Object.defineProperty(Server, "name", { value: "Server" });
677676
function onRequestEvent(event) {
678677
const [server, http_res, req] = this.socket[kInternalSocketData];
679678

680-
if (!http_res[finishedSymbol]) {
679+
if (!http_res.finished) {
681680
switch (event) {
682681
case NodeHTTPResponseAbortEvent.timeout:
683682
this.emit("timeout");
684683
server.emit("timeout", req.socket);
685684
break;
686685
case NodeHTTPResponseAbortEvent.abort:
687-
http_res[finishedSymbol] = true;
686+
http_res.finished = true;
688687
this.destroy();
689688
break;
690689
}
@@ -1542,7 +1541,7 @@ function OutgoingMessage(options) {
15421541
Stream.$call(this, options);
15431542

15441543
this.sendDate = true;
1545-
this[finishedSymbol] = false;
1544+
this.finished = false;
15461545
this[headerStateSymbol] = NodeHTTPHeaderState.none;
15471546
this[kAbortController] = null;
15481547

@@ -1714,15 +1713,15 @@ const OutgoingMessagePrototype = {
17141713
},
17151714

17161715
get writableNeedDrain() {
1717-
return !this.destroyed && !this[finishedSymbol] && this[kBodyChunks] && this[kBodyChunks].length > 0;
1716+
return !this.destroyed && !this.finished && this[kBodyChunks] && this[kBodyChunks].length > 0;
17181717
},
17191718

17201719
get writableEnded() {
1721-
return this[finishedSymbol];
1720+
return this.finished;
17221721
},
17231722

17241723
get writableFinished() {
1725-
return this[finishedSymbol] && !!(this[kEmitState] & (1 << ClientRequestEmitState.finish));
1724+
return this.finished && !!(this[kEmitState] & (1 << ClientRequestEmitState.finish));
17261725
},
17271726

17281727
_send(data, encoding, callback, byteLength) {
@@ -1850,7 +1849,7 @@ function ServerResponse(req, options) {
18501849
this._sent100 = false;
18511850
this[headerStateSymbol] = NodeHTTPHeaderState.none;
18521851
this[kPendingCallbacks] = [];
1853-
this[finishedSymbol] = false;
1852+
this.finished = false;
18541853

18551854
// this is matching node's behaviour
18561855
// https://github.com/nodejs/node/blob/cf8c6994e0f764af02da4fa70bc5962142181bf3/lib/_http_server.js#L192
@@ -1887,13 +1886,6 @@ const ServerResponsePrototype = {
18871886
this[headerStateSymbol] = value ? NodeHTTPHeaderState.sent : NodeHTTPHeaderState.none;
18881887
},
18891888

1890-
get finished() {
1891-
return this[finishedSymbol];
1892-
},
1893-
set finished(value) {
1894-
this[finishedSymbol] = value;
1895-
},
1896-
18971889
// This end method is actually on the OutgoingMessage prototype in Node.js
18981890
// But we don't want it for the fetch() response version.
18991891
end(chunk, encoding, callback) {
@@ -2074,11 +2066,11 @@ const ServerResponsePrototype = {
20742066
},
20752067

20762068
get writableNeedDrain() {
2077-
return !this.destroyed && !this[finishedSymbol] && (this[kHandle]?.bufferedAmount ?? 1) !== 0;
2069+
return !this.destroyed && !this.finished && (this[kHandle]?.bufferedAmount ?? 1) !== 0;
20782070
},
20792071

20802072
get writableFinished() {
2081-
return !!(this[finishedSymbol] && (!this[kHandle] || this[kHandle].finished));
2073+
return !!(this.finished && (!this[kHandle] || this[kHandle].finished));
20822074
},
20832075

20842076
get writableLength() {
@@ -2227,7 +2219,7 @@ function ensureReadableStreamController(run) {
22272219
for (let run of this[runSymbol]) {
22282220
run(controller);
22292221
}
2230-
if (!this[finishedSymbol]) {
2222+
if (!this.finished) {
22312223
const { promise, resolve } = $newPromiseCapability(GlobalPromise);
22322224
this[deferredSymbol] = resolve;
22332225
return promise;
@@ -2280,7 +2272,7 @@ function ServerResponse_finalDeprecated(chunk, encoding, callback) {
22802272
chunk = Buffer.from(chunk, encoding);
22812273
}
22822274
const req = this.req;
2283-
const shouldEmitClose = req && req.emit && !this[finishedSymbol];
2275+
const shouldEmitClose = req && req.emit && !this.finished;
22842276
if (!this.headersSent) {
22852277
let data = this[firstWriteSymbol];
22862278
if (chunk) {
@@ -2300,7 +2292,7 @@ function ServerResponse_finalDeprecated(chunk, encoding, callback) {
23002292
}
23012293

23022294
this[firstWriteSymbol] = undefined;
2303-
this[finishedSymbol] = true;
2295+
this.finished = true;
23042296
this.headersSent = true; // https://github.com/oven-sh/bun/issues/3458
23052297
drainHeadersIfObservable.$call(this);
23062298
this[kDeprecatedReplySymbol](
@@ -2318,7 +2310,7 @@ function ServerResponse_finalDeprecated(chunk, encoding, callback) {
23182310
return;
23192311
}
23202312

2321-
this[finishedSymbol] = true;
2313+
this.finished = true;
23222314
ensureReadableStreamController.$call(this, controller => {
23232315
if (chunk && encoding) {
23242316
chunk = Buffer.from(chunk, encoding);
@@ -2464,13 +2456,13 @@ function ClientRequest(input, options, cb) {
24642456
}
24652457

24662458
if (chunk) {
2467-
if (this[finishedSymbol]) {
2459+
if (this.finished) {
24682460
emitErrorNextTickIfErrorListenerNT(this, $ERR_STREAM_WRITE_AFTER_END("Cannot write after end"), callback);
24692461
return this;
24702462
}
24712463

24722464
write_(chunk, encoding, null);
2473-
} else if (this[finishedSymbol]) {
2465+
} else if (this.finished) {
24742466
if (callback) {
24752467
if (!this.writableFinished) {
24762468
this.on("finish", callback);
@@ -2484,7 +2476,7 @@ function ClientRequest(input, options, cb) {
24842476
this.once("finish", callback);
24852477
}
24862478

2487-
if (!this[finishedSymbol]) {
2479+
if (!this.finished) {
24882480
send();
24892481
resolveNextChunk?.(true);
24902482
}
@@ -2503,7 +2495,7 @@ function ClientRequest(input, options, cb) {
25032495
res._dump();
25042496
}
25052497

2506-
this[finishedSymbol] = true;
2498+
this.finished = true;
25072499

25082500
// If request is destroyed we abort the current response
25092501
this[kAbortController]?.abort?.();
@@ -2614,7 +2606,7 @@ function ClientRequest(input, options, cb) {
26142606
self.emit("drain");
26152607
}
26162608

2617-
while (!self[finishedSymbol]) {
2609+
while (!self.finished) {
26182610
yield await new Promise(resolve => {
26192611
resolveNextChunk = end => {
26202612
resolveNextChunk = undefined;
@@ -2725,7 +2717,7 @@ function ClientRequest(input, options, cb) {
27252717
let handleResponse = () => {};
27262718

27272719
const send = () => {
2728-
this[finishedSymbol] = true;
2720+
this.finished = true;
27292721
const controller = new AbortController();
27302722
this[kAbortController] = controller;
27312723
controller.signal.addEventListener("abort", onAbort, { once: true });
@@ -2978,7 +2970,7 @@ function ClientRequest(input, options, cb) {
29782970
// this.useChunkedEncodingByDefault = true;
29792971
// }
29802972

2981-
this[finishedSymbol] = false;
2973+
this.finished = false;
29822974
this[kRes] = null;
29832975
this[kUpgradeOrConnect] = false;
29842976
this[kParser] = null;
@@ -3148,7 +3140,7 @@ const ClientRequestPrototype = {
31483140
},
31493141

31503142
get writable() {
3151-
return !this[finishedSymbol];
3143+
return !this.finished;
31523144
},
31533145
};
31543146

0 commit comments

Comments
 (0)