Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
039b4a31ac Add test-http-server-response-standalone and fix ServerResponse.end 2025-05-29 15:46:01 -07:00
2 changed files with 85 additions and 2 deletions

View File

@@ -231,9 +231,54 @@ const ServerResponsePrototype = {
}
if (!handle) {
if ($isCallable(callback)) {
process.nextTick(callback);
if (!this.socket) {
if ($isCallable(callback)) {
process.nextTick(callback);
}
return this;
}
this._implicitHeader();
if (chunk) {
OutgoingMessagePrototype._send.$call(this, chunk, encoding, null);
} else {
OutgoingMessagePrototype._send.$call(this, kEmptyBuffer, encoding, null);
}
const socket = this.socket;
this.detachSocket(socket);
this.finished = true;
process.nextTick(self => {
self._ended = true;
}, this);
this.emit("prefinish");
this._callPendingCallbacks();
if (callback) {
process.nextTick(
function (callback, self) {
self.emit("finish");
try {
callback();
} catch (err) {
self.emit("error", err);
}
process.nextTick(emitCloseNT, self);
},
callback,
this,
);
} else {
process.nextTick(function (self) {
self.emit("finish");
process.nextTick(emitCloseNT, self);
}, this);
}
socket.end(Buffer.alloc(0));
return this;
}

View File

@@ -0,0 +1,38 @@
const common = require('../common');
const { ServerResponse } = require('http');
const { Writable } = require('stream');
const assert = require('assert');
// Check that ServerResponse can be used without a proper Socket
// Refs: https://github.com/nodejs/node/issues/14386
// Refs: https://github.com/nodejs/node/issues/14381
const res = new ServerResponse({
method: 'GET',
httpVersionMajor: 1,
httpVersionMinor: 1
});
let firstChunk = true;
const ws = new Writable({
write: common.mustCall((chunk, encoding, callback) => {
if (firstChunk) {
assert(chunk.toString().endsWith('hello world'));
firstChunk = false;
} else {
assert.strictEqual(chunk.length, 0);
}
setImmediate(callback);
}, 2)
});
res.assignSocket(ws);
assert.throws(function() {
res.assignSocket(ws);
}, {
code: 'ERR_HTTP_SOCKET_ASSIGNED'
});
res.end('hello world');