This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread-datacenter.js
69 lines (60 loc) · 1.89 KB
/
read-datacenter.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
var fs = require('fs');
var Result = require('raynos-rust-result');
var errors = require('./errors.js');
module.exports = readDatacenter;
function readDatacenter(opts) {
var env = opts.env || process.env;
var NODE_ENV = env.NODE_ENV;
// specifying a datacenter is optional in dev but required
// in production.
if (NODE_ENV === 'production' && !opts.dc && !opts.dcValue) {
throw errors.DatacenterRequired({
strOpts: JSON.stringify(opts)
});
}
var result;
if (opts.dcValue) {
result = Result.Ok({
'datacenter': opts.dcValue.replace(/\s/g, '')
});
} else if (opts.dc) {
var fileResult = readFileOrError(opts.dc);
if (Result.isErr(fileResult)) {
var err = Result.Err(fileResult);
// create error synchronously for correct stack trace
if (NODE_ENV === 'production') {
result = Result.Err(errors.DatacenterFileRequired({
path: err.path,
errno: err.errno,
code: err.code,
syscall: err.syscall
}));
} else {
result = Result.Err(errors.MissingDatacenter({
path: err.path,
errno: err.errno,
code: err.code,
syscall: err.syscall
}));
}
} else {
result = Result.Ok({
'datacenter': Result.Ok(fileResult)
.replace(/\s/g, '')
});
}
} else {
result = Result.Ok(null);
}
return result;
}
// break try catch into small function to avoid v8 de-optimization
function readFileOrError(uri) {
var content;
try {
content = fs.readFileSync(uri, 'utf8');
} catch (err) {
return Result.Err(err);
}
return Result.Ok(content);
}