-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.js
More file actions
156 lines (139 loc) · 5.3 KB
/
Copy pathconfig.js
File metadata and controls
156 lines (139 loc) · 5.3 KB
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
const port = process.env.PORT;
const onshapeApiUrl = process.env.API_URL;
const oauthCallbackUrl = process.env.OAUTH_CALLBACK_URL;
const oauthClientId = process.env.OAUTH_CLIENT_ID;
const oauthClientSecret = process.env.OAUTH_CLIENT_SECRET;
const oauthUrl = process.env.OAUTH_URL;
const redisUrl = process.env.REDIS_URL;
const redisHost = process.env.REDIS_HOST;
const redisPort = process.env.REDIS_PORT;
const sessionSecret = process.env.SESSION_SECRET;
const webhookCallbackRootUrl = process.env.WEBHOOK_CALLBACK_ROOT_URL;
/**
* Checks if the given string is a URL. A string considered a URL if it can be parsed
* as a URL (based on the WHATWG definition).
* If `protocols` is provided, this will be taken into account in the validation.
*
* For example:
*
* `isValidUrl('https://example.com', [ 'http:', 'https:' ])` would evaluate to `true`
*
* `isValidUrl('http://sub.example.com', [ 'redis:' ])` would evaluate to `false`
*
* `isValidUrl('example.com')` would evaluate to `false`
*
* @param {string} stringToTest The string to check for validity.
* @param {string|string[]} protocols The protocol(s) to include in the validity
* check. May be excluded, in which case it will not be considered in the check.
*
* @returns {boolean} `true` if the given string is a valid URL, and has one of the
* given protocols (if provided); or `false` otherwise.
*/
const isValidUrl = function (stringToTest, protocols) {
try {
const url = new URL(stringToTest);
if (!protocols) {
return true;
}
if (typeof protocols === 'string' || protocols instanceof String) {
protocols = [ protocols ];
}
return !protocols || protocols.includes(url.protocol);
} catch {
return false;
}
}
/**
* Checks if the given string is an HTTP or HTTPS URL. A string is considered if it can
* be parsed as a URL (based on the WHATWG definition).
*
* For example:
*
* `isValidHttpUrl('http://example.com')` would evaluate to `true`
*
* `isValidHttpUrl('ftp://user:pass@ftp.example.com/public/doc.txt)` would evaluate
* to `false`
*
* `isValidHttpUrl('example.com')` would evaluate to `false`
*
* @param {string} stringToTest The string to check for validity.
*/
const isValidHttpUrl = function(stringToTest) {
return isValidUrl(stringToTest, [ 'http:', 'https:' ]);
};
/**
* Checks if the given string has content, i.e. is not null and does not contain solely
* whitespace characters.
*
* @param {string} stringToTest The string to check for validity.
*/
const isValidString = function(stringToTest) {
if (!stringToTest) return false;
if (!(stringToTest.trim())) return false;
return true;
}
// We will check the entire configuration and only throw one error (if invalid).
const errors = [];
if (port && !isValidString(port)) errors.push('PORT must have content');
if (!isValidHttpUrl(onshapeApiUrl)) errors.push('API_URL is not a valid HTTP(S) URL');
if (!isValidHttpUrl(oauthCallbackUrl)) errors.push('OAUTH_CALLBACK_URL is not a valid HTTP(S) URL');
if (!isValidString(oauthClientId)) errors.push('OAUTH_CLIENT_ID must have content');
if (!isValidString(oauthClientSecret)) errors.push('OAUTH_CLIENT_SECRET must have content');
if (!isValidHttpUrl(oauthUrl)) errors.push('OAUTH_URL is not a valid HTTP(S) URL');
if (redisUrl && !isValidUrl(redisUrl, 'redis:')) errors.push('REDISTOGO_URL is not a valid Redis URL');
if (redisHost && !isValidString(redisHost)) errors.push('REDIS_HOST must have content');
if (redisPort && !isValidString(redisPort)) errors.push('REDIS_PORT must have content');
if (!isValidString(sessionSecret)) errors.push('SESSION_SECRET must have content');
if (!isValidHttpUrl(webhookCallbackRootUrl)) errors.push('WEBHOOK_CALLBACK_ROOT_URL is not a valid HTTP(S) URL');
// Halt execution if the app isn't correctly configured.
if (errors.length !== 0) {
throw new Error('Invalid configuration: ' + errors.join(', '));
}
module.exports = {
/**
* The port this application should run on. This may be `undefined`.
*/
port,
/**
* The parent URL of the Onshape API endpoints, e.g. `https://cad.onshape.com/api`.
*/
onshapeApiUrl,
/**
* The absolute URL of the OAuth callback URL. This will be the `/oauthRedirect` endpoint
* on this server, e.g. `https://your-machine.example.com/oauthRedirect`.
*/
oauthCallbackUrl,
/**
* The Client ID of this application as registered in the Onshape Dev Portal.
*/
oauthClientId,
/**
* The Client Secret of this application as registered in the Onshape Dev Portal.
*/
oauthClientSecret,
/**
* The parent URL of the Onshape OAuth endpoints, e.g. `https://oauth.onshape.com`.
*/
oauthUrl,
/**
* The URL of the Redis To Go add-on (if deployed in Heroku). This may be `undefined`.
*/
redisUrl,
/**
* The URL of the Redis host. This may be `undefined`.
*/
redisHost,
/**
* The port of the Redis host. This may be `undefined`.
*/
redisPort,
/**
* The secret for handling session data.
*/
sessionSecret,
/**
* The URL of the webhook callback URL. This will be the `/api/event` endpoint on
* this server, e.g. `https://your-machine.example.com`.
*/
webhookCallbackRootUrl
}