Skip to content

Commit

Permalink
Optimizations and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Aug 1, 2019
1 parent d2c4d2d commit c0f8339
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
22 changes: 8 additions & 14 deletions source/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ const nameKeys = [
'maxReservedRemoteStreams',
'maxSendHeaderBlockLength',
'paddingStrategy',
'peerMaxConcurrentStreams',
'settings',

// `tls.connect()` options
'localAddress',
'family',
'path',
'rejectUnauthorized',
'minDHSize',
Expand Down Expand Up @@ -80,7 +77,7 @@ class Agent extends EventEmitter {
};
}

getName(authority, options = {}) {
getName(authority, options) {
if (typeof authority === 'string') {
authority = new URL(authority);
}
Expand All @@ -90,12 +87,9 @@ class Agent extends EventEmitter {

let name = `${host}:${port}`;

// TODO: this should ignore defaults too
for (const key of nameKeys) {
if (Reflect.has(options, key)) {
if (typeof options[key] === 'object') {
name += `:${JSON.stringify(options[key])}`;
} else {
if (options) {
for (const key of nameKeys) {
if (Reflect.has(options, key)) {
name += `:${options[key]}`;
}
}
Expand Down Expand Up @@ -283,13 +277,13 @@ class Agent extends EventEmitter {
static connect(authority, options) {
options.ALPNProtocols = ['h2'];

if (typeof options.servername === 'undefined') {
options.servername = authority.host;
}

const port = authority.port || 443;
const host = authority.hostname || authority.host;

if (typeof options.servername === 'undefined') {
options.servername = host;
}

return tls.connect(port, host, options);
}

Expand Down
37 changes: 20 additions & 17 deletions source/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,30 @@ const prepareRequest = async options => {
if (options.protocol === 'https:') {
const host = options.hostname || options.host || 'localhost';
const port = options.port || 443;
const ALPNProtocols = options.ALPNProtocols || ['h2', 'http/1.1'];
const name = `${host}:${port}:${ALPNProtocols.sort()}`;

let alpnProtocol = cache.get(name);
if (isCompatible) {
const ALPNProtocols = options.ALPNProtocols || ['h2', 'http/1.1'];
const name = `${host}:${port}:${ALPNProtocols.sort()}`;

if (typeof alpnProtocol === 'undefined') {
alpnProtocol = (await httpResolveALPN(options)).alpnProtocol;
cache.set(name, alpnProtocol);
}
let alpnProtocol = cache.get(name);

if (alpnProtocol === 'h2' && isCompatible) {
return (options, callback) => {
if (options.agent && options.agent.http2) {
options = {
...options,
agent: options.agent.http2
};
}
if (typeof alpnProtocol === 'undefined') {
alpnProtocol = (await httpResolveALPN(options)).alpnProtocol;
cache.set(name, alpnProtocol);
}

return Http2ClientRequest.request(options, callback);
};
if (alpnProtocol === 'h2') {
return (options, callback) => {
if (options.agent && options.agent.http2) {
options = {
...options,
agent: options.agent.http2
};
}

return Http2ClientRequest.request(options, callback);
};
}
}

return (options, callback) => {
Expand Down
4 changes: 1 addition & 3 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ if (isCompatible) {

const firstSession = await agent.getSession(server.url);
const secondSession = await agent.getSession(server.url, {
settings: {
peerMaxConcurrentStreams: 1
}
maxSessionMemory: 1
});

t.not(firstSession, secondSession);
Expand Down

0 comments on commit c0f8339

Please sign in to comment.