You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importhttpfrom'node:http';importnetfrom'node:net';importassertfrom'node:assert';// Create an HTTP serverconstserver=http.createServer((req,res)=>{// Flag to check if close event is emittedletcloseEventEmitted=false;// Listen for the 'close' event on the responseres.on('close',()=>{console.log('Response close event emitted');closeEventEmitted=true;});// Stream responseres.writeHead(200,{'Content-Type': 'text/plain'});constinterval=setInterval(()=>{res.write('Hello, world!\n');},100);req.on('end',()=>{clearInterval(interval);res.end();});req.on('error',(err)=>{console.error('Request error:',err);clearInterval(interval);res.end();});// Verify if close event was emitted when the server is closedserver.on('close',()=>{assert.strictEqual(closeEventEmitted,true,'Close event was not emitted on the response');console.log('Server closed and close event was verified');});});// Start the serverserver.listen(3000,()=>{console.log('Server is listening on port 3000');// Create a client connection to the serverconstclient=net.createConnection({port: 3000},()=>{console.log('Client connected to server');// Write data to the serverclient.write('GET / HTTP/1.1\r\n');client.write('Host: localhost\r\n');client.write('Connection: close\r\n');client.write('\r\n');// End the client connection prematurely while reading dataclient.on('data',(data)=>{console.log('Client received data:',data.toString());client.end();console.log('Client connection ended prematurely');});});// Set a timeout to give the server time to handle the premature connection endsetTimeout(()=>{// Close the server after some timeserver.close(()=>{console.log('Server closed');});},2000);});
Expected output:
➜ node close_event_on_server_node_test.mjs
Server is listening on port 3000
Client connected to server
Client received data: HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 24 Jun 2024 15:58:23 GMT
Connection: close
Transfer-Encoding: chunked
e
Hello, world!
Client connection ended prematurely
Response close event emitted
Request error: Error: aborted
at abortIncoming (node:_http_server:806:17)
at socketOnClose (node:_http_server:800:3)
at Socket.emit (node:events:532:35)
at TCP.<anonymous> (node:net:338:12) {
code: 'ECONNRESET'
}
^C
Current output:
➜ deno run -A close_event_on_server_node_test.mjs
Server is listening on port 3000
Client connected to server
Client received data: HTTP/1.1 200 OK
content-type: text/plain
vary: Accept-Encoding
transfer-encoding: chunked
date: Mon, 24 Jun 2024 15:58:31 GMT
1C
Hello, world!
Hello, world!
Client connection ended prematurely
error: Uncaught (in promise) AssertionError: Close event was not emitted on the response
at new AssertionError (ext:deno_node/assertion_error.ts:536:11)
at toNode (node:assert:71:15)
at Function.strictEqual (node:assert:321:3)
at ServerImpl.<anonymous> (file:///Users/sr/c/denoland/deno/close_event_on_server_node_test.mjs:36:12)
at ServerImpl.emit (ext:deno_node/_events.mjs:398:35)
at node:http:1674:49
at eventLoopTick (ext:core/01_core.js:168:7)
The text was updated successfully, but these errors were encountered:
Example:
Expected output:
Current output:
The text was updated successfully, but these errors were encountered: