-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
138 lines (110 loc) · 3.6 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
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
const RefParser = require('@cgamesplay/referer-parser');
const useragent = require('useragent');
const defaults = {
cookieName: 'ref64',
ttl: 30 * 86400000, // 30 days
domains: [],
ignoredPaths: [],
decorationName: 'getOriginalReferrer',
verbose: false,
accept: ''
};
const register = (server, options) => {
// update the useragent's library of browsers so it is always current:
options = Object.assign({}, defaults, options);
server.event('referrer');
if (!options.ignoredPaths.includes('favicon.ico')) {
options.ignoredPaths.push('favicon.ico');
}
server.ext('onPreHandler', (request, h) => {
if (request.method !== 'get') {
return h.continue;
}
if (options.accept) {
if (!request.headers || !request.headers.accept || request.headers.accept.indexOf(options.accept) === -1) {
return h.continue;
}
}
const reqUri = `${request.headers['x-forwarded-proto'] || request.server.info.protocol}://${request.info.host}${request.path}`;
let currentCookie = '';
/* $lab:coverage:off$ */
// hapi issue? can't reproduce in tests
if (request.state) {
currentCookie = request.state[options.cookieName] || '';
}
/* $lab:coverage:on$ */
if (currentCookie.length) {
server.events.emit('referrer', { request, refInfo: request[options.decorationName]() });
return h.continue;
}
const blacklistedDomain = options.domains.find(item => request.info.referrer.indexOf(item) !== -1);
const blacklistedPath = options.ignoredPaths.find(item => request.path.indexOf(item) !== -1);
if (blacklistedDomain || blacklistedPath) {
return h.continue;
}
const data = new RefParser(request.info.referrer, reqUri);
if (data.medium === 'internal') {
return h.continue;
}
const refString = [];
if (data.medium !== 'unknown') {
refString.push(data.medium);
}
if (data.referer) {
refString.push(data.referer);
}
// if unknown - Check if direct or linked
if (data.medium === 'unknown' && !data.referer) {
if (request.info.referrer.length) {
refString.push('link');
} else {
refString.push('direct');
}
}
const ts = Date.now();
const cookieValue = `${encodeURIComponent(refString.join(' - '))}||${ts}||${encodeURIComponent(request.info.referrer)}||${encodeURIComponent(reqUri)}`;
const refInfo = {
medium: refString.join(' - '),
referrer: request.info.referrer,
uri: reqUri
};
const agent = useragent.parse(request.headers['user-agent']);
if (options.verbose) {
server.log(['hapi-referrer', 'set-cookie', 'info'], {
refInfo,
ua: agent.source,
browser: `${agent.family} ${agent.major}.${agent.minor}.${agent.patch}`
});
}
h.state(options.cookieName, cookieValue, {
path: '/',
ttl: options.ttl,
clearInvalid: true,
ignoreErrors: true,
encoding: 'base64'
});
server.events.emit('referrer', {
request,
refInfo
});
return h.continue;
});
function getOriginalReferrer() {
const currentCookie = Buffer.from(this.state[options.cookieName] || '', 'base64').toString();
if (!currentCookie) {
return {};
}
const [medium, timestamp, referrer, uri] = currentCookie.split('||');
return {
medium: decodeURIComponent(medium),
timestamp,
referrer: decodeURIComponent(referrer),
uri: decodeURIComponent(uri) };
}
server.decorate('request', options.decorationName, getOriginalReferrer);
};
exports.plugin = {
once: true,
pkg: require('./package.json'),
register
};