whistle 2.10.5 → 2.10.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/util.js CHANGED
@@ -185,15 +185,15 @@ exports.getDefaultPort = function () {
185
185
  };
186
186
 
187
187
  exports.getBody = function (res, callback) {
188
- var resBody;
188
+ var resBody = [];
189
189
  res.on('data', function(data) {
190
- resBody = resBody ? Buffer.concat([resBody, data]) : data;
190
+ resBody.push(data);
191
191
  });
192
192
  res.on('end', function() {
193
193
  if (res.statusCode != 200) {
194
194
  callback('Bad response (' + res.statusCode + ')');
195
195
  } else {
196
- callback(null, JSON.parse(resBody + ''));
196
+ callback(null, JSON.parse(Buffer.concat(resBody).toString()));
197
197
  }
198
198
  });
199
199
  };
@@ -13,7 +13,8 @@ module.exports = function(req, res) {
13
13
  var reqList = parseArray(req.query.reqList);
14
14
  var resList = parseArray(req.query.resList);
15
15
  var result = {};
16
- reqList.concat(resList).forEach(function(id) {
16
+ var isReq;
17
+ var appendResult = function(id) {
17
18
  if (result[id] != null) {
18
19
  return;
19
20
  }
@@ -22,9 +23,12 @@ module.exports = function(req, res) {
22
23
  result[id] = 0;
23
24
  return;
24
25
  }
25
- if ((item.requestTime && reqList.indexOf(id) !== -1) || item.endTime) {
26
+ if ((item.requestTime && isReq) || item.endTime) {
26
27
  result[id] = item;
27
28
  }
28
- });
29
+ };
30
+ resList.forEach(appendResult);
31
+ isReq = true;
32
+ reqList.forEach(appendResult);
29
33
  res.json(result);
30
34
  };
@@ -71,47 +71,58 @@ exports.getServerInfo = function(req) {
71
71
  ipv6: [],
72
72
  mac: req.ip + (config.storage ? '\n' + config.storage : '')
73
73
  };
74
- var ifaces = util.networkInterfaces();
75
- Object.keys(ifaces).forEach(function(ifname) {
76
- ifaces[ifname].forEach(function (iface) {
77
- if (iface.internal) {
78
- return;
79
- }
80
- info[iface.family == 'IPv4' || iface.family === 4 ? 'ipv4' : 'ipv6'].push(iface.address);
81
- });
82
- });
83
-
74
+ var serverInfo = util.getServerInfo();
75
+ info.ipv4 = serverInfo.ipv4;
76
+ info.ipv6 = serverInfo.ipv6;
84
77
  return info;
85
78
  };
86
79
 
87
80
  var DATA_RE = /[\r\n]\s*(\{[\s\S]*\})[\r\n]/;
88
81
  var REPLACE_RE = /[\r\n]1[\r\n]/;
82
+ var EXCEED_SIZE_ERR = new Error('The file size can not exceed 6MB');
83
+ var INVALID_CONTENT_ERR = new Error('The file content is not a JSON object');
84
+
89
85
  exports.getReqData = function(req, callback) {
90
- var result = '';
86
+ var result = [];
87
+ var len = 0;
88
+ var done;
89
+ var handleCb = function(err, data) {
90
+ if (!done) {
91
+ done = true;
92
+ callback(err, data);
93
+ result = null;
94
+ }
95
+ };
91
96
  req.on('data', function(chunk) {
92
- result = result ? Buffer.concat([result, chunk]) : chunk;
93
- if (result.length > MAX_OBJECT_SIZE) {
94
- req.removeAllListeners('data');
95
- callback(new Error('The file size can not exceed 6MB'));
97
+ if (done) {
98
+ return;
99
+ }
100
+ len += chunk.length;
101
+ result.push(chunk);
102
+ if (len > MAX_OBJECT_SIZE) {
103
+ handleCb(EXCEED_SIZE_ERR);
96
104
  }
97
105
  });
98
- req.on('error', callback);
106
+ req.on('error', handleCb);
99
107
  req.on('end', function() {
100
- result += '';
108
+ if (done) {
109
+ return;
110
+ }
101
111
  var data;
112
+ result = Buffer.concat(result).toString();
102
113
  result = result.replace(DATA_RE, function(all, match) {
103
114
  data = match;
104
115
  return '';
105
116
  });
106
117
  if (!data) {
107
- return callback(new Error('The file content is not a JSON object'));
118
+ return handleCb(INVALID_CONTENT_ERR);
108
119
  }
109
120
  try {
110
121
  data = JSON.parse(data);
111
122
  } catch(err) {
112
- return callback(err);
123
+ return handleCb(err);
113
124
  }
114
- callback(null, {
125
+ handleCb(null, {
115
126
  data: data,
116
127
  replace: REPLACE_RE.test(result)
117
128
  });
@@ -8,6 +8,6 @@
8
8
  </head>
9
9
  <body>
10
10
  <div id="container" class="main"></div>
11
- <script src="js/index.js?v=2.10.5"></script>
11
+ <script src="js/index.js?v=2.10.6"></script>
12
12
  </body>
13
13
  </html>