-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
utils.js
387 lines (316 loc) · 10.7 KB
/
utils.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'use strict';
var fs = require('fs');
var url = require('url');
var transports = require('./transports');
var path = require('path');
var lsmod = require('../vendor/node-lsmod');
var stacktrace = require('stack-trace');
var Limiter = require('async-limiter');
var stringify = require('../vendor/json-stringify-safe');
var ravenVersion = require('../package.json').version;
var protocolMap = {
http: 80,
https: 443
};
var consoleAlerts = new Set();
var fsLimiter = new Limiter({concurrency: 25);
// Default Node.js REPL depth
var MAX_SERIALIZE_EXCEPTION_DEPTH = 3;
// 50kB, as 100kB is max payload size, so half sounds reasonable
var MAX_SERIALIZE_EXCEPTION_SIZE = 50 * 1024;
var MAX_SERIALIZE_KEYS_LENGTH = 40;
function utf8Length(value) {
return ~-encodeURI(value).split(/%..|./).length;
}
function jsonSize(value) {
return utf8Length(JSON.stringify(value));
}
function isError(what) {
return (
Object.prototype.toString.call(what) === '[object Error]' || what instanceof Error
);
}
module.exports.isError = isError;
function isPlainObject(what) {
return Object.prototype.toString.call(what) === '[object Object]';
}
module.exports.isPlainObject = isPlainObject;
function serializeValue(value) {
var maxLength = 40;
if (typeof value === 'string') {
return value.length <= maxLength ? value : value.substr(0, maxLength - 1) + '\u2026';
} else if (
typeof value === 'number' ||
typeof value === 'boolean' ||
typeof value === 'undefined'
) {
return value;
}
var type = Object.prototype.toString.call(value);
// Node.js REPL notation
if (type === '[object Object]') return '[Object]';
if (type === '[object Array]') return '[Array]';
if (type === '[object Function]')
return value.name ? '[Function: ' + value.name + ']' : '[Function]';
return value;
}
function serializeObject(value, depth) {
if (depth === 0) return serializeValue(value);
if (isPlainObject(value)) {
return Object.keys(value).reduce(function(acc, key) {
acc[key] = serializeObject(value[key], depth - 1);
return acc;
}, {});
} else if (Array.isArray(value)) {
return value.map(function(val) {
return serializeObject(val, depth - 1);
});
}
return serializeValue(value);
}
function serializeException(ex, depth, maxSize) {
if (!isPlainObject(ex)) return ex;
depth = typeof depth !== 'number' ? MAX_SERIALIZE_EXCEPTION_DEPTH : depth;
maxSize = typeof depth !== 'number' ? MAX_SERIALIZE_EXCEPTION_SIZE : maxSize;
var serialized = serializeObject(ex, depth);
if (jsonSize(stringify(serialized)) > maxSize) {
return serializeException(ex, depth - 1);
}
return serialized;
}
module.exports.serializeException = serializeException;
function serializeKeysForMessage(keys, maxLength) {
if (typeof keys === 'number' || typeof keys === 'string') return keys.toString();
if (!Array.isArray(keys)) return '';
keys = keys.filter(function(key) {
return typeof key === 'string';
});
if (keys.length === 0) return '[object has no keys]';
maxLength = typeof maxLength !== 'number' ? MAX_SERIALIZE_KEYS_LENGTH : maxLength;
if (keys[0].length >= maxLength) return keys[0];
for (var usedKeys = keys.length; usedKeys > 0; usedKeys--) {
var serialized = keys.slice(0, usedKeys).join(', ');
if (serialized.length > maxLength) continue;
if (usedKeys === keys.length) return serialized;
return serialized + '\u2026';
}
return '';
}
module.exports.serializeKeysForMessage = serializeKeysForMessage;
module.exports.disableConsoleAlerts = function disableConsoleAlerts() {
consoleAlerts = false;
};
module.exports.enableConsoleAlerts = function enableConsoleAlerts() {
consoleAlerts = new Set();
};
module.exports.consoleAlert = function consoleAlert(msg) {
if (consoleAlerts) {
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
}
};
module.exports.consoleAlertOnce = function consoleAlertOnce(msg) {
if (consoleAlerts && !consoleAlerts.has(msg)) {
consoleAlerts.add(msg);
console.warn('raven@' + ravenVersion + ' alert: ' + msg);
}
};
module.exports.extend =
Object.assign ||
function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
module.exports.getAuthHeader = function getAuthHeader(timestamp, apiKey, apiSecret) {
var header = ['Sentry sentry_version=5'];
header.push('sentry_timestamp=' + timestamp);
header.push('sentry_client=raven-node/' + ravenVersion);
header.push('sentry_key=' + apiKey);
if (apiSecret) header.push('sentry_secret=' + apiSecret);
return header.join(', ');
};
module.exports.parseDSN = function parseDSN(dsn) {
if (!dsn) {
// Let a falsey value return false explicitly
return false;
}
try {
var parsed = url.parse(dsn),
response = {
protocol: parsed.protocol.slice(0, -1),
public_key: parsed.auth.split(':')[0],
host: parsed.host.split(':')[0]
};
if (parsed.auth.split(':')[1]) {
response.private_key = parsed.auth.split(':')[1];
}
if (~response.protocol.indexOf('+')) {
response.protocol = response.protocol.split('+')[1];
}
if (!transports.hasOwnProperty(response.protocol)) {
throw new Error('Invalid transport');
}
var index = parsed.pathname.lastIndexOf('/');
response.path = parsed.pathname.substr(0, index + 1);
response.project_id = parsed.pathname.substr(index + 1);
response.port = ~~parsed.port || protocolMap[response.protocol] || 443;
return response;
} catch (e) {
throw new Error('Invalid Sentry DSN: ' + dsn);
}
};
module.exports.getTransaction = function getTransaction(frame) {
if (frame.module || frame.function) {
return (frame.module || '?') + ' at ' + (frame.function || '?');
}
return '<unknown>';
};
var moduleCache;
module.exports.getModules = function getModules() {
if (!moduleCache) {
moduleCache = lsmod();
}
return moduleCache;
};
module.exports.fill = function(obj, name, replacement, track) {
var orig = obj[name];
obj[name] = replacement(orig);
if (track) {
track.push([obj, name, orig]);
}
};
var LINES_OF_CONTEXT = 7;
function getFunction(line) {
try {
return (
line.getFunctionName() ||
line.getTypeName() + '.' + (line.getMethodName() || '<anonymous>')
);
} catch (e) {
// This seems to happen sometimes when using 'use strict',
// stemming from `getTypeName`.
// [TypeError: Cannot read property 'constructor' of undefined]
return '<anonymous>';
}
}
var mainModule =
((require.main && require.main.filename && path.dirname(require.main.filename)) ||
global.process.cwd()) + '/';
function getModule(filename, base) {
if (!base) base = mainModule;
// It's specifically a module
var file = path.basename(filename, '.js');
filename = path.dirname(filename);
var n = filename.lastIndexOf('/node_modules/');
if (n > -1) {
// /node_modules/ is 14 chars
return filename.substr(n + 14).replace(/\//g, '.') + ':' + file;
}
// Let's see if it's a part of the main module
// To be a part of main module, it has to share the same base
n = (filename + '/').lastIndexOf(base, 0);
if (n === 0) {
var module = filename.substr(base.length).replace(/\//g, '.');
if (module) module += ':';
module += file;
return module;
}
return file;
}
function readSourceFiles(filenames, cb) {
// we're relying on filenames being de-duped already
if (filenames.length === 0) return setTimeout(cb, 0, {});
var sourceFiles = {};
var numFilesToRead = filenames.length;
return filenames.forEach(function(filename) {
fsLimiter.push(function (done) {
fs.readFile(filename, function(readErr, file) {
done();
if (!readErr) sourceFiles[filename] = file.toString().split('\n');
if (--numFilesToRead === 0) cb(sourceFiles);
});
});
});
}
// This is basically just `trim_line` from https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
function snipLine(line, colno) {
var ll = line.length;
if (ll <= 150) return line;
if (colno > ll) colno = ll;
var start = Math.max(colno - 60, 0);
if (start < 5) start = 0;
var end = Math.min(start + 140, ll);
if (end > ll - 5) end = ll;
if (end === ll) start = Math.max(end - 140, 0);
line = line.slice(start, end);
if (start > 0) line = '{snip} ' + line;
if (end < ll) line += ' {snip}';
return line;
}
function snipLine0(line) {
return snipLine(line, 0);
}
function parseStack(err, cb) {
if (!err) return cb([]);
var stack = stacktrace.parse(err);
if (!stack || !Array.isArray(stack) || !stack.length || !stack[0].getFileName) {
// the stack is not the useful thing we were expecting :/
return cb([]);
}
// Sentry expects the stack trace to be oldest -> newest, v8 provides newest -> oldest
stack.reverse();
var frames = [];
var filesToRead = {};
stack.forEach(function(line) {
var frame = {
filename: line.getFileName() || '',
lineno: line.getLineNumber(),
colno: line.getColumnNumber(),
function: getFunction(line)
};
var isInternal =
line.isNative() ||
(frame.filename[0] !== '/' &&
frame.filename[0] !== '.' &&
frame.filename.indexOf(':\\') !== 1);
// in_app is all that's not an internal Node function or a module within node_modules
// note that isNative appears to return true even for node core libraries
// see https://github.com/getsentry/raven-node/issues/176
frame.in_app = !isInternal && frame.filename.indexOf('node_modules/') === -1;
// Extract a module name based on the filename
if (frame.filename) {
frame.module = getModule(frame.filename);
if (!isInternal) filesToRead[frame.filename] = true;
}
frames.push(frame);
});
return readSourceFiles(Object.keys(filesToRead), function(sourceFiles) {
frames.forEach(function(frame) {
if (frame.filename && sourceFiles[frame.filename]) {
var lines = sourceFiles[frame.filename];
try {
frame.pre_context = lines
.slice(Math.max(0, frame.lineno - (LINES_OF_CONTEXT + 1)), frame.lineno - 1)
.map(snipLine0);
frame.context_line = snipLine(lines[frame.lineno - 1], frame.colno);
frame.post_context = lines
.slice(frame.lineno, frame.lineno + LINES_OF_CONTEXT)
.map(snipLine0);
} catch (e) {
// anomaly, being defensive in case
// unlikely to ever happen in practice but can definitely happen in theory
}
}
});
cb(frames);
});
}
// expose basically for testing because I don't know what I'm doing
module.exports.parseStack = parseStack;
module.exports.getModule = getModule;