forked from avwo/whistle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (76 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require('./lib/util/patch');
var net = require('net');
var tls = require('tls');
var ver = process.version.substring(1).split('.');
var PROD_RE = /(^|\|)prod(uction)?($|\|)/;
if (ver[0] >= 7 && ver[1] >= 7) {
var connect = net.Socket.prototype.connect;
if (typeof connect === 'function') {
//fix: Node v7.7.0+引入的 `"listener" argument must be a function` 问题
net.Socket.prototype.connect = function(options, cb) {
if (options && typeof options === 'object' && typeof cb !== 'function') {
return connect.call(this, options, null);
}
return connect.apply(this, arguments);
};
}
}
//see: https://github.com/joyent/node/issues/9272
var env = process.env || '';
env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
if (typeof tls.checkServerIdentity == 'function') {
var checkServerIdentity = tls.checkServerIdentity;
tls.checkServerIdentity = function() {
try {
return checkServerIdentity.apply(this, arguments);
} catch(err) {
return err;
}
};
}
if (env.WHISTLE_PLUGIN_EXEC_PATH) {
env.PFORK_EXEC_PATH = env.WHISTLE_PLUGIN_EXEC_PATH;
}
function isPipeName(s) {
return typeof s === 'string' && toNumber(s) === false;
}
function toNumber(x) {
return (x = Number(x)) >= 0 ? x : false;
}
if (!net._normalizeConnectArgs) {
//Returns an array [options] or [options, cb]
//It is the same as the argument of Socket.prototype.connect().
net._normalizeConnectArgs = function (args) {
var options = {};
if (args[0] !== null && typeof args[0] === 'object') {
// connect(options, [cb])
options = args[0];
} else if (isPipeName(args[0])) {
// connect(path, [cb]);
options.path = args[0];
} else {
// connect(port, [host], [cb])
options.port = args[0];
if (typeof args[1] === 'string') {
options.host = args[1];
}
}
var cb = args[args.length - 1];
return typeof cb === 'function' ? [options, cb] : [options];
};
}
module.exports = function(options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
if (options && options.debugMode) {
if (PROD_RE.test(options.mode)) {
options.debugMode = false;
} else {
env.PFORK_MODE = 'bind';
}
}
require('./lib/config').extend(options);
return require('./lib')(callback);
};