-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathutils.js
58 lines (49 loc) · 1.64 KB
/
utils.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
"use strict";
const path = require('path');
const os = require('os');
const crypto = require('crypto');
module.exports = {
get: function(scope, prop, defaultValue){
let parts = prop.split(".");
let current = scope;
for (let i = 0; i < parts.length; i++){
if (current[parts[i]] !== undefined && i < parts.length - 1){
current = current[parts[i]];
}else if (current[parts[i]] !== undefined && i < parts.length){
return current[parts[i]];
}else{
return defaultValue;
}
}
return defaultValue;
},
sanitize: function(filePath){
filePath = filePath.replace(/[^\w.-]/g, "_");
return filePath;
},
parseUnsafePathsList: function(paths){
// Parse a list (or a JSON encoded string representing a list)
// of paths and remove all traversals (., ..) and guarantee
// that the paths are relative
if (typeof paths === "string"){
try{
paths = JSON.parse(paths);
}catch(e){
return [];
}
}
if (!Array.isArray(paths)){
return [];
}
return paths.map(p => {
const safeSuffix = path.normalize(p).replace(/^(\.\.(\/|\\|$))+/, '');
return path.join('./', safeSuffix);
});
},
clone: function(json){
return JSON.parse(JSON.stringify(json));
},
tmpPath: function(extension = ".txt"){
return path.join(os.tmpdir(), `nodeodm_${crypto.randomBytes(6).readUIntLE(0,6).toString(36)}${extension}`);
}
};