Skip to content

Commit 7801408

Browse files
committedMar 2, 2023
feat: migrate to es6 class instead of util.inherit in http2wrapper
1 parent 73c7efb commit 7801408

File tree

1 file changed

+137
-138
lines changed

1 file changed

+137
-138
lines changed
 

‎src/node/http2wrapper.js

+137-138
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Stream = require('stream');
2-
const util = require('util');
32
const net = require('net');
43
const tls = require('tls');
54
// eslint-disable-next-line node/no-deprecated-api
@@ -31,169 +30,169 @@ function setProtocol(protocol) {
3130
};
3231
}
3332

34-
function Request(protocol, options) {
35-
Stream.call(this);
36-
const defaultPort = protocol === 'https:' ? 443 : 80;
37-
const defaultHost = 'localhost';
38-
const port = options.port || defaultPort;
39-
const host = options.host || defaultHost;
33+
class Request extends Stream {
34+
constructor(protocol, options) {
35+
super();
36+
const defaultPort = protocol === 'https:' ? 443 : 80;
37+
const defaultHost = 'localhost';
38+
const port = options.port || defaultPort;
39+
const host = options.host || defaultHost;
40+
41+
delete options.port;
42+
delete options.host;
43+
44+
this.method = options.method;
45+
this.path = options.path;
46+
this.protocol = protocol;
47+
this.host = host;
48+
49+
delete options.method;
50+
delete options.path;
51+
52+
const sessionOptions = { ...options };
53+
if (options.socketPath) {
54+
sessionOptions.socketPath = options.socketPath;
55+
sessionOptions.createConnection = this.createUnixConnection.bind(this);
56+
}
4057

41-
delete options.port;
42-
delete options.host;
58+
this._headers = {};
4359

44-
this.method = options.method;
45-
this.path = options.path;
46-
this.protocol = protocol;
47-
this.host = host;
60+
const session = http2.connect(
61+
`${protocol}//${host}:${port}`,
62+
sessionOptions
63+
);
64+
this.setHeader('host', `${host}:${port}`);
4865

49-
delete options.method;
50-
delete options.path;
66+
session.on('error', (error) => this.emit('error', error));
5167

52-
const sessionOptions = { ...options };
53-
if (options.socketPath) {
54-
sessionOptions.socketPath = options.socketPath;
55-
sessionOptions.createConnection = this.createUnixConnection.bind(this);
68+
this.session = session;
5669
}
5770

58-
this._headers = {};
71+
createUnixConnection(authority, options) {
72+
switch (this.protocol) {
73+
case 'http:':
74+
return net.connect(options.socketPath);
75+
case 'https:':
76+
options.ALPNProtocols = ['h2'];
77+
options.servername = this.host;
78+
options.allowHalfOpen = true;
79+
return tls.connect(options.socketPath, options);
80+
default:
81+
throw new Error('Unsupported protocol', this.protocol);
82+
}
83+
}
5984

60-
const session = http2.connect(`${protocol}//${host}:${port}`, sessionOptions);
61-
this.setHeader('host', `${host}:${port}`);
85+
setNoDelay(bool) {
86+
// We can not use setNoDelay with HTTP/2.
87+
// Node 10 limits http2session.socket methods to ones safe to use with HTTP/2.
88+
// See also https://nodejs.org/api/http2.html#http2_http2session_socket
89+
}
6290

63-
session.on('error', (error) => this.emit('error', error));
91+
getFrame() {
92+
if (this.frame) {
93+
return this.frame;
94+
}
6495

65-
this.session = session;
66-
}
96+
const method = {
97+
[HTTP2_HEADER_PATH]: this.path,
98+
[HTTP2_HEADER_METHOD]: this.method
99+
};
67100

68-
/**
69-
* Inherit from `Stream` (which inherits from `EventEmitter`).
70-
*/
71-
util.inherits(Request, Stream);
72-
73-
Request.prototype.createUnixConnection = function (authority, options) {
74-
switch (this.protocol) {
75-
case 'http:':
76-
return net.connect(options.socketPath);
77-
case 'https:':
78-
options.ALPNProtocols = ['h2'];
79-
options.servername = this.host;
80-
options.allowHalfOpen = true;
81-
return tls.connect(options.socketPath, options);
82-
default:
83-
throw new Error('Unsupported protocol', this.protocol);
84-
}
85-
};
101+
let headers = this.mapToHttp2Header(this._headers);
86102

87-
Request.prototype.setNoDelay = function (bool) {
88-
// We can not use setNoDelay with HTTP/2.
89-
// Node 10 limits http2session.socket methods to ones safe to use with HTTP/2.
90-
// See also https://nodejs.org/api/http2.html#http2_http2session_socket
91-
};
103+
headers = Object.assign(headers, method);
92104

93-
Request.prototype.getFrame = function () {
94-
if (this.frame) {
95-
return this.frame;
96-
}
105+
const frame = this.session.request(headers);
97106

98-
const method = {
99-
[HTTP2_HEADER_PATH]: this.path,
100-
[HTTP2_HEADER_METHOD]: this.method
101-
};
107+
frame.once('response', (headers, flags) => {
108+
headers = this.mapToHttpHeader(headers);
109+
frame.headers = headers;
110+
frame.statusCode = headers[HTTP2_HEADER_STATUS];
111+
frame.status = frame.statusCode;
112+
this.emit('response', frame);
113+
});
102114

103-
let headers = this.mapToHttp2Header(this._headers);
115+
this._headerSent = true;
104116

105-
headers = Object.assign(headers, method);
117+
frame.once('drain', () => this.emit('drain'));
118+
frame.on('error', (error) => this.emit('error', error));
119+
frame.on('close', () => this.session.close());
106120

107-
const frame = this.session.request(headers);
121+
this.frame = frame;
122+
return frame;
123+
}
108124

109-
frame.once('response', (headers, flags) => {
110-
headers = this.mapToHttpHeader(headers);
111-
frame.headers = headers;
112-
frame.statusCode = headers[HTTP2_HEADER_STATUS];
113-
frame.status = frame.statusCode;
114-
this.emit('response', frame);
115-
});
125+
mapToHttpHeader(headers) {
126+
const keys = Object.keys(headers);
127+
const http2Headers = {};
128+
for (let key of keys) {
129+
let value = headers[key];
130+
key = key.toLowerCase();
131+
switch (key) {
132+
case HTTP2_HEADER_SET_COOKIE:
133+
value = Array.isArray(value) ? value : [value];
134+
break;
135+
default:
136+
break;
137+
}
138+
139+
http2Headers[key] = value;
140+
}
116141

117-
this._headerSent = true;
142+
return http2Headers;
143+
}
118144

119-
frame.once('drain', () => this.emit('drain'));
120-
frame.on('error', (error) => this.emit('error', error));
121-
frame.on('close', () => this.session.close());
145+
mapToHttp2Header(headers) {
146+
const keys = Object.keys(headers);
147+
const http2Headers = {};
148+
for (let key of keys) {
149+
let value = headers[key];
150+
key = key.toLowerCase();
151+
switch (key) {
152+
case HTTP2_HEADER_HOST:
153+
key = HTTP2_HEADER_AUTHORITY;
154+
value = /^http:\/\/|^https:\/\//.test(value)
155+
? parse(value).host
156+
: value;
157+
break;
158+
default:
159+
break;
160+
}
161+
162+
http2Headers[key] = value;
163+
}
122164

123-
this.frame = frame;
124-
return frame;
125-
};
165+
return http2Headers;
166+
}
126167

127-
Request.prototype.mapToHttpHeader = function (headers) {
128-
const keys = Object.keys(headers);
129-
const http2Headers = {};
130-
for (let key of keys) {
131-
let value = headers[key];
132-
key = key.toLowerCase();
133-
switch (key) {
134-
case HTTP2_HEADER_SET_COOKIE:
135-
value = Array.isArray(value) ? value : [value];
136-
break;
137-
default:
138-
break;
139-
}
168+
setHeader(name, value) {
169+
this._headers[name.toLowerCase()] = value;
170+
}
140171

141-
http2Headers[key] = value;
172+
getHeader(name) {
173+
return this._headers[name.toLowerCase()];
142174
}
143175

144-
return http2Headers;
145-
};
146-
147-
Request.prototype.mapToHttp2Header = function (headers) {
148-
const keys = Object.keys(headers);
149-
const http2Headers = {};
150-
for (let key of keys) {
151-
let value = headers[key];
152-
key = key.toLowerCase();
153-
switch (key) {
154-
case HTTP2_HEADER_HOST:
155-
key = HTTP2_HEADER_AUTHORITY;
156-
value = /^http:\/\/|^https:\/\//.test(value)
157-
? parse(value).host
158-
: value;
159-
break;
160-
default:
161-
break;
162-
}
176+
write(data, encoding) {
177+
const frame = this.getFrame();
178+
return frame.write(data, encoding);
179+
}
180+
181+
pipe(stream, options) {
182+
const frame = this.getFrame();
183+
return frame.pipe(stream, options);
184+
}
163185

164-
http2Headers[key] = value;
186+
end(data) {
187+
const frame = this.getFrame();
188+
frame.end(data);
165189
}
166190

167-
return http2Headers;
168-
};
169-
170-
Request.prototype.setHeader = function (name, value) {
171-
this._headers[name.toLowerCase()] = value;
172-
};
173-
174-
Request.prototype.getHeader = function (name) {
175-
return this._headers[name.toLowerCase()];
176-
};
177-
178-
Request.prototype.write = function (data, encoding) {
179-
const frame = this.getFrame();
180-
return frame.write(data, encoding);
181-
};
182-
183-
Request.prototype.pipe = function (stream, options) {
184-
const frame = this.getFrame();
185-
return frame.pipe(stream, options);
186-
};
187-
188-
Request.prototype.end = function (data) {
189-
const frame = this.getFrame();
190-
frame.end(data);
191-
};
192-
193-
Request.prototype.abort = function (data) {
194-
const frame = this.getFrame();
195-
frame.close(NGHTTP2_CANCEL);
196-
this.session.destroy();
197-
};
191+
abort(data) {
192+
const frame = this.getFrame();
193+
frame.close(NGHTTP2_CANCEL);
194+
this.session.destroy();
195+
}
196+
}
198197

199198
exports.setProtocol = setProtocol;

0 commit comments

Comments
 (0)
Please sign in to comment.