-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (68 loc) · 1.88 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
const Hoek = require('hoek');
const Path = require('path');
const userAgentLib = require('useragent');
const defaults = {
endpoint: '/api/browser-log',
serveScript: false,
tags: ['hapi-browser-log'],
routeConfig: {}
};
const register = function(server, options) {
let settings = Hoek.clone(options);
settings = Hoek.applyToDefaults(defaults, settings);
let filter = () => true;
if (settings.ignore &&
Array.isArray(settings.ignore) &&
settings.ignore.length) {
filter = data => {
let isLoggable = true;
const checkIgnore = (ignore, value) =>
Object.keys(ignore).some(key => {
const regexp = new RegExp(ignore[key]);
return regexp.exec(value[key]) !== null;
});
for (let i = 0, len = settings.ignore.length; i < len && isLoggable; i++) {
isLoggable = !checkIgnore(settings.ignore[i], data);
}
return isLoggable;
};
}
server.route({
method: 'POST',
path: settings.endpoint,
config: settings.routeConfig,
handler(request) {
if (!request.payload || !request.payload.data) {
return { success: false };
}
const data = request.payload.data;
const payloadTags = request.payload.tags;
const tags = settings.tags.concat(payloadTags);
if (data.userAgent) {
data.browser = userAgentLib.parse(data.userAgent).toString();
}
if (filter(data)) {
request.server.log(tags, {
data
});
}
return { success: true };
}
});
if (settings.serveScript) {
server.dependency(['inert']);
server.route({
method: 'GET',
path: '/hapi-browser-log.js',
handler(request, h) {
return h.file(Path.join(__dirname, '/lib/browser/script.js'));
}
});
}
};
exports.plugin = {
name: 'hapi-browser-log',
register,
once: true,
pkg: require('./package.json')
};