Skip to content

Commit 832cc03

Browse files
committed
Delete dependency on git repo
1 parent b4f3652 commit 832cc03

6 files changed

Lines changed: 224 additions & 7 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
coverage
22
node_modules
3+
/test/support/supertest/http2wrapper.js

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ before_install:
2424
# Setup Node.js version-specific dependencies
2525
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
2626
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)"
27-
- "test -z $(echo $HTTP2_TEST) || npm install --only=dev https://github.com/sogaani/supertest.git#http2"
2827

2928
# Update Node.js modules
3029
- "test ! -d node_modules || npm prune"
@@ -38,7 +37,5 @@ after_script:
3837
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
3938
matrix:
4039
include:
41-
- node_js: "8.11"
42-
env: HTTP2_TEST=1
43-
- node_js: "10.7"
40+
- node_js: "10"
4441
env: HTTP2_TEST=1

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"eslint-plugin-standard": "3.0.1",
1919
"istanbul": "0.4.5",
2020
"mocha": "2.5.3",
21-
"supertest": "1.1.0"
21+
"supertest": "2.0"
2222
},
2323
"files": [
2424
"LICENSE",
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
'use strict';
2+
3+
const http2 = require('http2');
4+
const Stream = require('stream');
5+
const util = require('util');
6+
const net = require('net');
7+
const tls = require('tls');
8+
const parse = require('url').parse;
9+
10+
const {
11+
HTTP2_HEADER_PATH,
12+
HTTP2_HEADER_STATUS,
13+
HTTP2_HEADER_METHOD,
14+
HTTP2_HEADER_AUTHORITY,
15+
HTTP2_HEADER_HOST,
16+
HTTP2_HEADER_SET_COOKIE,
17+
NGHTTP2_CANCEL,
18+
} = http2.constants;
19+
20+
21+
function setProtocol(protocol) {
22+
return {
23+
request: function (options) {
24+
return new Request(protocol, options);
25+
}
26+
}
27+
}
28+
29+
function Request(protocol, options) {
30+
Stream.call(this);
31+
const defaultPort = protocol === 'https:' ? 443 : 80;
32+
const defaultHost = 'localhost'
33+
const port = options.port || defaultPort;
34+
const host = options.host || defaultHost;
35+
36+
delete options.port
37+
delete options.host
38+
39+
this.method = options.method.toUpperCase();
40+
this.path = options.path;
41+
this.protocol = protocol;
42+
this.host = host;
43+
44+
delete options.method
45+
delete options.path
46+
47+
const sessionOptions = Object.assign({}, options);
48+
if (options.socketPath) {
49+
sessionOptions.socketPath = options.socketPath;
50+
sessionOptions.createConnection = this.createUnixConnection.bind(this);
51+
}
52+
53+
this._headers = {};
54+
55+
const session = http2.connect(`${protocol}//${host}:${port}`, sessionOptions);
56+
this.setHeader('host', `${host}:${port}`)
57+
58+
session.on('error', (err) => this.emit('error', err));
59+
60+
this.session = session;
61+
}
62+
63+
/**
64+
* Inherit from `Stream` (which inherits from `EventEmitter`).
65+
*/
66+
util.inherits(Request, Stream);
67+
68+
Request.prototype.createUnixConnection = function (authority, options) {
69+
switch (this.protocol) {
70+
case 'http:':
71+
return net.connect(options.socketPath);
72+
case 'https:':
73+
options.ALPNProtocols = ['h2'];
74+
options.servername = this.host;
75+
options.allowHalfOpen = true;
76+
return tls.connect(options.socketPath, options);
77+
default:
78+
throw new Error('Unsupported protocol', this.protocol);
79+
}
80+
}
81+
82+
Request.prototype.setNoDelay = function (bool) {
83+
// We can not use setNoDelay with HTTP/2.
84+
// Node 10 limits http2session.socket methods to ones safe to use with HTTP/2.
85+
// See also https://nodejs.org/api/http2.html#http2_http2session_socket
86+
}
87+
88+
Request.prototype.getFrame = function () {
89+
if (this.frame) {
90+
return this.frame;
91+
}
92+
93+
const method = {
94+
[HTTP2_HEADER_PATH]: this.path,
95+
[HTTP2_HEADER_METHOD]: this.method,
96+
}
97+
98+
let headers = this.mapToHttp2Header(this._headers);
99+
100+
headers = Object.assign(headers, method);
101+
102+
const frame = this.session.request(headers);
103+
frame.once('response', (headers, flags) => {
104+
headers = this.mapToHttpHeader(headers);
105+
frame.headers = headers;
106+
frame.status = frame.statusCode = headers[HTTP2_HEADER_STATUS];
107+
this.emit('response', frame);
108+
});
109+
110+
this._headerSent = true;
111+
112+
frame.once('drain', () => this.emit('drain'));
113+
frame.on('error', (err) => this.emit('error', err));
114+
frame.on('close', () => this.session.close());
115+
116+
this.frame = frame;
117+
return frame;
118+
}
119+
120+
Request.prototype.mapToHttpHeader = function (headers) {
121+
const keys = Object.keys(headers);
122+
const http2Headers = {};
123+
for (var i = 0; i < keys.length; i++) {
124+
let key = keys[i];
125+
let value = headers[key];
126+
key = key.toLowerCase();
127+
switch (key) {
128+
case HTTP2_HEADER_SET_COOKIE:
129+
value = Array.isArray(value) ? value : [value];
130+
break;
131+
default:
132+
break;
133+
}
134+
http2Headers[key] = value;
135+
}
136+
return http2Headers;
137+
}
138+
139+
Request.prototype.mapToHttp2Header = function (headers) {
140+
const keys = Object.keys(headers);
141+
const http2Headers = {};
142+
for (var i = 0; i < keys.length; i++) {
143+
let key = keys[i];
144+
let value = headers[key];
145+
key = key.toLowerCase();
146+
switch (key) {
147+
case HTTP2_HEADER_HOST:
148+
key = HTTP2_HEADER_AUTHORITY;
149+
value = /^http\:\/\/|^https\:\/\//.test(value) ? parse(value).host : value;
150+
break;
151+
default:
152+
break;
153+
}
154+
http2Headers[key] = value;
155+
}
156+
return http2Headers;
157+
}
158+
159+
Request.prototype.setHeader = function (name, value) {
160+
this._headers[name.toLowerCase()] = value;
161+
}
162+
163+
Request.prototype.getHeader = function (name) {
164+
return this._headers[name.toLowerCase()];
165+
}
166+
167+
Request.prototype.write = function (data, encoding) {
168+
const frame = this.getFrame();
169+
return frame.write(data, encoding);
170+
};
171+
172+
Request.prototype.pipe = function (stream, options) {
173+
const frame = this.getFrame();
174+
return frame.pipe(stream, options);
175+
}
176+
177+
Request.prototype.end = function (data) {
178+
const frame = this.getFrame();
179+
frame.end(data);
180+
}
181+
182+
Request.prototype.abort = function (data) {
183+
const frame = this.getFrame();
184+
frame.close(NGHTTP2_CANCEL);
185+
this.session.destroy();
186+
}
187+
188+
exports.setProtocol = setProtocol;

test/support/supertest/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var request = require('supertest')
2+
3+
if (process.env.HTTP2_TEST) {
4+
var http2 = require('http2')
5+
var http2wrapper = require('./http2wrapper')
6+
var agent = require('superagent')
7+
var tls = require('tls')
8+
agent.protocols = {
9+
'http:': http2wrapper.setProtocol('http:'),
10+
'https:': http2wrapper.setProtocol('https:')
11+
}
12+
request.Test.prototype.serverAddress = function (app, path, host) {
13+
var addr = app.address()
14+
var port
15+
var protocol
16+
17+
if (!addr) this._server = app.listen(0)
18+
port = app.address().port
19+
20+
protocol = app instanceof tls.Server ? 'https' : 'http'
21+
return protocol + '://' + (host || '127.0.0.1') + ':' + port + path
22+
}
23+
var originalRequest = request
24+
request = function (app) {
25+
if (typeof app === 'function') {
26+
app = http2.createServer(app)
27+
}
28+
return originalRequest(app)
29+
}
30+
}
31+
32+
module.exports = request

test/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11

22
var assert = require('assert')
33
var http = require('http')
4-
var request = require('supertest')
4+
var request = require('./support/supertest')
55
var vhost = require('..')
66

77
if (process.env.HTTP2_TEST) {
8-
request.http2 = true
98
http = require('http2')
109
}
1110

0 commit comments

Comments
 (0)