Skip to content

Commit aa02aeb

Browse files
committed
move pingEndpoint to utils
1 parent ba66d76 commit aa02aeb

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

lib/contracts/blockchain.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -256,45 +256,8 @@ class Blockchain {
256256
if (!self.contractsConfig || !self.contractsConfig.deployment || !self.contractsConfig.deployment.host) {
257257
return next();
258258
}
259-
const origin = self.blockchainConfig.wsOrigins.split(',')[0];
260-
const options = {
261-
protocolVersion: 13,
262-
perMessageDeflate: true,
263-
origin: origin,
264-
host: self.contractsConfig.deployment.host,
265-
port: self.contractsConfig.deployment.port
266-
};
267-
if (self.contractsConfig.deployment.type === 'ws') {
268-
options.headers = {
269-
'Sec-WebSocket-Version': 13,
270-
Connection: 'Upgrade',
271-
Upgrade: 'websocket',
272-
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
273-
Origin: origin
274-
};
275-
}
276-
let req;
277-
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
278-
if(options.host.indexOf('/') > -1){
279-
options.host = options.host.split('/')[0];
280-
}
281-
if((self.contractsConfig.deployment.protocol || 'http') === 'https'){
282-
req = require('https').get(options);
283-
}else{
284-
req = require('http').get(options);
285-
}
286-
287-
req.on('error', (err) => {
288-
next(err);
289-
});
290-
291-
req.on('response', (_response) => {
292-
next();
293-
});
294-
295-
req.on('upgrade', (_res, _socket, _head) => {
296-
next();
297-
});
259+
const {host, port, type, protocol} = self.contractsConfig.deployment;
260+
utils.pingEndpoint(host, port, type, protocol, self.blockchainConfig.wsOrigins.split(',')[0], next);
298261
}
299262
], function (err) {
300263
if (!noLogs && err === NO_NODE_ERROR) {

lib/utils/utils.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ function httpsGetJson(url, callback) {
7373
});
7474
}
7575

76+
function pingEndpoint(host, port, type, protocol, wsOrigins, callback) {
77+
const origin = wsOrigins.split(',')[0];
78+
const options = {
79+
protocolVersion: 13,
80+
perMessageDeflate: true,
81+
origin: origin,
82+
host: host,
83+
port: port
84+
};
85+
if (type === 'ws') {
86+
options.headers = {
87+
'Sec-WebSocket-Version': 13,
88+
Connection: 'Upgrade',
89+
Upgrade: 'websocket',
90+
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
91+
Origin: origin
92+
};
93+
}
94+
let req;
95+
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
96+
if(options.host.indexOf('/') > -1){
97+
options.host = options.host.split('/')[0];
98+
}
99+
if(protocol === 'https') {
100+
req = require('https').get(options);
101+
} else {
102+
req = require('http').get(options);
103+
}
104+
105+
req.on('error', (err) => {
106+
callback(err);
107+
});
108+
109+
req.on('response', (_response) => {
110+
callback();
111+
});
112+
113+
req.on('upgrade', (_res, _socket, _head) => {
114+
callback();
115+
});
116+
}
117+
76118
function runCmd(cmd, options) {
77119
const shelljs = require('shelljs');
78120
let result = shelljs.exec(cmd, options || {silent: true});
@@ -267,6 +309,7 @@ module.exports = {
267309
httpGetJson: httpGetJson,
268310
httpsGetJson: httpsGetJson,
269311
hexToNumber: hexToNumber,
312+
pingEndpoint,
270313
decodeParams: decodeParams,
271314
runCmd: runCmd,
272315
cd: cd,

0 commit comments

Comments
 (0)