/
config.js
109 lines (92 loc) · 2.53 KB
/
config.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
"use strict";
const extend = require("./util/extend");
let EndpointAddress = {
production: "api.push.apple.com",
development: "api.development.push.apple.com"
};
module.exports = function(dependencies) {
const logger = dependencies.logger;
const prepareCertificate = dependencies.prepareCertificate;
const prepareToken = dependencies.prepareToken;
const prepareCA = dependencies.prepareCA;
function config(options) {
let config = {
token: null,
cert: "cert.pem",
key: "key.pem",
ca: null,
pfx: null,
passphrase: null,
production: (process.env.NODE_ENV === "production"),
address: null,
port: 443,
proxy: null,
rejectUnauthorized: true,
connectionRetryLimit: 10,
heartBeat: 60000,
};
validateOptions(options);
extend(config, options);
configureAddress(config);
if (config.token) {
delete config.cert;
delete config.key;
delete config.pfx;
extend(config, { token: prepareToken(config.token) });
} else {
if (config.pfx || config.pfxData) {
config.cert = options.cert;
config.key = options.key;
}
extend(config, prepareCertificate(config));
}
extend(config, prepareCA(config));
return config;
}
function validateOptions(options) {
for (var key in options) {
if (options[key] === null || options[key] === undefined) {
logger("Option [" + key + "] is " + options[key] + ". This may cause unexpected behaviour.");
}
}
if (options) {
if (options.passphrase && typeof options.passphrase !== "string") {
throw new Error("Passphrase must be a string");
}
if (options.token) {
validateToken(options.token);
}
}
}
return config;
};
function validateToken(token) {
if (!token.keyId) {
throw new Error("token.keyId is missing");
} else if(typeof token.keyId !== "string") {
throw new Error("token.keyId must be a string");
}
if (!token.teamId) {
throw new Error("token.teamId is missing");
} else if(typeof token.teamId !== "string") {
throw new Error("token.teamId must be a string");
}
}
function configureAddress(options) {
if (!options.address) {
if (options.production) {
options.address = EndpointAddress.production;
}
else {
options.address = EndpointAddress.development;
}
}
else {
if (options.address === EndpointAddress.production) {
options.production = true;
}
else {
options.production = false;
}
}
};