Skip to content

Commit 3af54d9

Browse files
committed
Add support for keep alive connections
1 parent 81daa16 commit 3af54d9

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

packages/push/src/client.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
import http from 'http';
21
import https from 'https';
32

43
import Config from '../src/config';
54

6-
function performRequest(method, url, headers, payload) {
7-
let isTLS = url.protocol === 'https:';
8-
let client = isTLS ? https : http;
9-
10-
let options = {
11-
method,
5+
function performRequest({ url, payload, ...options }) {
6+
options = {
127
hostname: url.host,
13-
port: url.port !== '' ? url.port : isTLS ? 443 : 80,
8+
port: url.port !== '' ? url.port : 443,
149
path: url.pathname,
15-
headers: headers,
10+
...options,
1611
};
1712

1813
return new Promise(function (resolve, reject) {
19-
let request = client.request(options, (response) => {
14+
let request = https.request(options, (response) => {
2015
resolve(response);
2116
});
2217

@@ -37,6 +32,8 @@ function performRequest(method, url, headers, payload) {
3732
export default class Client {
3833
constructor(config = {}) {
3934
this.config = new Config(config);
35+
36+
this.agent = new https.Agent({ keepAlive: true });
4037
}
4138

4239
get(endpoint) {
@@ -57,6 +54,7 @@ export default class Client {
5754

5855
_request(method, endpoint, payload) {
5956
let { pushEndpoint, pushKey } = this.config;
57+
let { agent } = this;
6058

6159
let url = new URL(endpoint, pushEndpoint);
6260
let headers = {
@@ -65,6 +63,6 @@ export default class Client {
6563
Authorization: `Push ${pushKey}`,
6664
};
6765

68-
return performRequest(method, url, headers, payload);
66+
return performRequest({ method, url, headers, payload, agent });
6967
}
7068
}

0 commit comments

Comments
 (0)