Skip to content

Commit 9977354

Browse files
committed
http: set async_id_symbol to any socket set
OutgoingMessage needs a async-hooks enabled socket to work, but we support also basic streams. This PR init the async-hooks bits for the passed stream if it is needed. PR-URL: #14389 Fixes: #14386 Fixes: #14381
1 parent aa496f4 commit 9977354

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

lib/_http_outgoing.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ const checkIsHttpToken = common._checkIsHttpToken;
3232
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
3333
const outHeadersKey = require('internal/http').outHeadersKey;
3434
const async_id_symbol = process.binding('async_wrap').async_id_symbol;
35+
const async_hooks = require('async_hooks');
3536
const nextTick = require('internal/process/next_tick').nextTick;
3637
const errors = require('internal/errors');
38+
const kSocket = Symbol('socket');
3739

3840
const CRLF = common.CRLF;
3941
const debug = common.debug;
@@ -116,7 +118,7 @@ function OutgoingMessage() {
116118
this.finished = false;
117119
this._headerSent = false;
118120

119-
this.socket = null;
121+
this[kSocket] = null;
120122
this.connection = null;
121123
this._header = null;
122124
this[outHeadersKey] = null;
@@ -125,6 +127,19 @@ function OutgoingMessage() {
125127
}
126128
util.inherits(OutgoingMessage, Stream);
127129

130+
Object.defineProperty(OutgoingMessage.prototype, 'socket', {
131+
get: function() {
132+
return this[kSocket];
133+
},
134+
set: function(socket) {
135+
this[kSocket] = socket;
136+
if (socket && socket[async_id_symbol] === undefined) {
137+
socket[async_id_symbol] = async_hooks.newUid();
138+
async_hooks.emitInit(socket[async_id_symbol], 'not-a-socket',
139+
async_hooks.initTriggerId(), this);
140+
}
141+
}
142+
});
128143

129144
Object.defineProperty(OutgoingMessage.prototype, '_headers', {
130145
get: function() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { OutgoingMessage } = require('http');
5+
const { Writable } = require('stream');
6+
const assert = require('assert');
7+
8+
// check that OutgoingMessage can be used without a proper Socket
9+
// Fixes: https://github.com/nodejs/node/issues/14386
10+
// Fixes: https://github.com/nodejs/node/issues/14381
11+
12+
class Response extends OutgoingMessage {
13+
constructor() {
14+
super({ method: 'GET', httpVersionMajor: 1, httpVersionMinor: 1 });
15+
}
16+
17+
_implicitHeader() {}
18+
}
19+
20+
const res = new Response();
21+
const ws = new Writable({
22+
write: common.mustCall((chunk, encoding, callback) => {
23+
assert(chunk.toString().match(/hello world/));
24+
setImmediate(callback);
25+
})
26+
});
27+
28+
res.socket = ws;
29+
ws._httpMessage = res;
30+
res.connection = ws;
31+
32+
res.end('hello world');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { ServerResponse } = require('http');
5+
const { Writable } = require('stream');
6+
const assert = require('assert');
7+
8+
// check that ServerResponse can be used without a proper Socket
9+
// Fixes: https://github.com/nodejs/node/issues/14386
10+
// Fixes: https://github.com/nodejs/node/issues/14381
11+
12+
const res = new ServerResponse({
13+
method: 'GET',
14+
httpVersionMajor: 1,
15+
httpVersionMinor: 1
16+
});
17+
18+
const ws = new Writable({
19+
write: common.mustCall((chunk, encoding, callback) => {
20+
assert(chunk.toString().match(/hello world/));
21+
setImmediate(callback);
22+
})
23+
});
24+
25+
res.assignSocket(ws);
26+
27+
res.end('hello world');

0 commit comments

Comments
 (0)