Skip to content

Commit cbbd400

Browse files
committed
more
1 parent b16c9ff commit cbbd400

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

src/js/node/_http_outgoing.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,16 @@ const OutgoingMessagePrototype = {
492492
end(_chunk, _encoding, _callback) {
493493
return this;
494494
},
495+
get writableCorked() {
496+
return this.socket.writableCorked;
497+
},
498+
set writableCorked(value) {},
499+
cork() {
500+
this.socket.cork();
501+
},
502+
uncork() {
503+
this.socket.uncork();
504+
},
495505
destroy(_err?: Error) {
496506
if (this.destroyed) return this;
497507
const handle = this[kHandle];

src/js/node/_http_server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ const ServerResponsePrototype = {
510510
handle.abort();
511511
}
512512
this?.socket?.destroy();
513+
this.emit("close");
513514
return this;
514515
},
515516

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
const common = require('../common');
24+
const http = require('http');
25+
const assert = require('assert');
26+
27+
{
28+
const server = http.createServer(
29+
common.mustCall((req, res) => {
30+
res.writeHead(200);
31+
res.write('a');
32+
})
33+
);
34+
server.listen(
35+
0,
36+
common.mustCall(() => {
37+
http.get(
38+
{ port: server.address().port },
39+
common.mustCall((res) => {
40+
res.on('data', common.mustCall(() => {
41+
res.destroy();
42+
}));
43+
assert.strictEqual(res.destroyed, false);
44+
res.on('close', common.mustCall(() => {
45+
assert.strictEqual(res.destroyed, true);
46+
server.closeAllConnections();
47+
}));
48+
})
49+
);
50+
})
51+
);
52+
}
53+
54+
{
55+
const server = http.createServer(
56+
common.mustCall((req, res) => {
57+
res.writeHead(200);
58+
res.end('a');
59+
})
60+
);
61+
server.listen(
62+
0,
63+
common.mustCall(() => {
64+
http.get(
65+
{ port: server.address().port },
66+
common.mustCall((res) => {
67+
assert.strictEqual(res.destroyed, false);
68+
res.on('end', common.mustCall(() => {
69+
assert.strictEqual(res.destroyed, false);
70+
}));
71+
res.on('close', common.mustCall(() => {
72+
assert.strictEqual(res.destroyed, true);
73+
server.close();
74+
}));
75+
res.resume();
76+
})
77+
);
78+
})
79+
);
80+
}
81+
82+
{
83+
const server = http.createServer(
84+
common.mustCall((req, res) => {
85+
res.on('close', common.mustCall());
86+
res.destroy();
87+
})
88+
);
89+
90+
server.listen(
91+
0,
92+
common.mustCall(() => {
93+
http.get(
94+
{ port: server.address().port },
95+
common.mustNotCall()
96+
)
97+
.on('error', common.mustCall(() => {
98+
server.close();
99+
}));
100+
})
101+
);
102+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const common = require('../common');
3+
const http = require('http');
4+
const assert = require('assert');
5+
6+
const server = http.createServer((req, res) => {
7+
let corked = false;
8+
const originalWrite = res.socket.write;
9+
// This calls are not visible neither in the same quantity than node.js implementation
10+
// res.socket.write = common.mustCall((...args) => {
11+
// assert.strictEqual(corked, false);
12+
// return originalWrite.call(res.socket, ...args);
13+
// }, 5);
14+
corked = true;
15+
res.cork();
16+
assert.strictEqual(res.writableCorked, res.socket.writableCorked);
17+
res.cork();
18+
assert.strictEqual(res.writableCorked, res.socket.writableCorked);
19+
res.writeHead(200, { 'a-header': 'a-header-value' });
20+
res.uncork();
21+
assert.strictEqual(res.writableCorked, res.socket.writableCorked);
22+
corked = false;
23+
res.end('asd');
24+
assert.strictEqual(res.writableCorked, res.socket.writableCorked);
25+
});
26+
27+
server.listen(0, () => {
28+
http.get({ port: server.address().port }, (res) => {
29+
res.on('data', common.mustCall());
30+
res.on('end', common.mustCall(() => {
31+
server.close();
32+
}));
33+
});
34+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const { createServer } = require('http');
6+
const { connect } = require('net');
7+
8+
let connections = 0;
9+
10+
const server = createServer(common.mustCall(function(req, res) {
11+
res.writeHead(200, { Connection: 'keep-alive' });
12+
res.end();
13+
}, 2), {
14+
headersTimeout: 0,
15+
keepAliveTimeout: 0,
16+
requestTimeout: common.platformTimeout(60000),
17+
});
18+
19+
server.on('connection', function() {
20+
connections++;
21+
});
22+
23+
server.listen(0, function() {
24+
const port = server.address().port;
25+
26+
// Create a first request but never finish it
27+
const client1 = connect(port);
28+
29+
client1.on('connect', common.mustCall(() => {
30+
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive
31+
const client2 = connect(port);
32+
let response = '';
33+
34+
client2.setEncoding('utf8');
35+
36+
client2.on('data', common.mustCall((chunk) => {
37+
response += chunk;
38+
39+
if (response.endsWith('0\r\n\r\n')) {
40+
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive'));
41+
assert.strictEqual(connections, 2);
42+
43+
server.closeAllConnections();
44+
server.close(common.mustCall());
45+
46+
// This timer should never go off as the server.close should shut everything down
47+
setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref();
48+
}
49+
}));
50+
51+
client2.on('close', common.mustCall());
52+
53+
client2.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n');
54+
}));
55+
56+
client1.on('close', common.mustCall());
57+
58+
client1.on('error', () => {});
59+
60+
client1.write('GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'); // Bun only reports connect after headers are received
61+
});

0 commit comments

Comments
 (0)