-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathchange_hc_version.js
More file actions
79 lines (66 loc) · 2.42 KB
/
change_hc_version.js
File metadata and controls
79 lines (66 loc) · 2.42 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
/*******************************************************************************
Highcharts Export Server
Copyright (c) 2016-2024, Highsoft
Licenced under the MIT licence.
Additionally a valid Highcharts license is required for use.
See LICENSE file in root for details.
*******************************************************************************/
import { updateVersion, version } from '../../cache.js';
import { envs } from '../../envs.js';
import HttpError from '../../errors/HttpError.js';
/**
* Adds the POST /change_hc_version/:newVersion route that can be utilized to modify
* the Highcharts version on the server.
*
* TODO: Add auth token and connect to API
*/
export default (app) =>
!app
? false
: app.post(
'/version/change/:newVersion',
async (request, response, next) => {
try {
const adminToken = envs.HIGHCHARTS_ADMIN_TOKEN;
// Check the existence of the token
if (!adminToken || !adminToken.length) {
throw new HttpError(
'The server is not configured to perform run-time version changes: HIGHCHARTS_ADMIN_TOKEN is not set.',
401
);
}
// Check if the hc-auth header contain a correct token
const token = request.get('hc-auth');
if (!token || token !== adminToken) {
throw new HttpError(
'Invalid or missing token: Set the token in the hc-auth header.',
401
);
}
// Compare versions
const newVersion = request.params.newVersion;
if (newVersion) {
try {
// eslint-disable-next-line import/no-named-as-default-member
await updateVersion(newVersion);
} catch (error) {
throw new HttpError(
`Version change: ${error.message}`,
400
).setError(error);
}
// Success
response.status(200).send({
statusCode: 200,
version: version(),
message: `Successfully updated Highcharts to version: ${newVersion}.`
});
} else {
// No version specified
throw new HttpError('No new version supplied.', 400);
}
} catch (error) {
next(error);
}
}
);