-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
86 lines (70 loc) · 2.64 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
var uuid = require('uuid').v4;
if (
(
/**
* Edge 14's fetch implementation is a buggy mess
* https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8653298/
*/
global.navigator &&
global.navigator.userAgent &&
global.navigator.userAgent.indexOf('Edge/14') > -1
) || (
/**
* Some browsers have a fetch implementation but only a partial Headers implementation
* https://developer.mozilla.org/en-US/docs/Web/API/Headers#Browser_compatibility
*/
!global.Headers ||
!global.Headers.prototype.entries
)
) {
// we have to delete fetch because unfetch has a bug that won't let it act like
// a ponyfill: https://github.com/developit/unfetch/issues/77
delete global.fetch;
}
function getAppName() {
if (typeof process == 'undefined' && global.location)
return (global.location && global.location.hostname) || '';
if (typeof process == 'undefined')
return 'non-node';
if (process && process.env && process.env['WORKER_NAME'])
return process.env['WORKER_NAME'];
if (process && process.env && process.env['APP_NAME'])
return process.env['APP_NAME'];
if (process && process.env && process.env['npm_package_name'])
return process.env['npm_package_name'];
if (process && typeof process.cwd == 'function') {
try {
var pkg = require(process.cwd() + '/package.json');
if (pkg && pkg.name)
return pkg.name;
}
catch (e) { /* package not found */ }
}
return 'unknown-node';
}
// Based on `unfetch` polyfill
// https://github.com/developit/unfetch/blob/master/packages/isomorphic-unfetch/index.js
function getImplementation() {
var impl = global.fetch;
var appName = getAppName();
var appIdentifier = appName
.replace(/[^a-z0-9]+/ig, '-')
.toLowerCase()
.replace(/(^-)|(-$)/ig, '');
var headers = {
'user-agent': 'Ambassify ' + appName + ' - [email protected]',
'x-request-id': appIdentifier + '+' + uuid(),
};
if (!impl && typeof process == 'undefined')
impl = require('unfetch').default || require('unfetch');
if (!impl && typeof process != 'undefined')
impl = require('node-fetch');
return function fetch(url, options) {
options = Object.assign({}, options || {});
options.headers = Object.assign({}, headers, options.headers || {});
if (options.body && typeof options.duplex === 'undefined')
options.duplex = 'half';
return impl(url, options);
};
}
module.exports = getImplementation();