-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
169 lines (142 loc) · 3.25 KB
/
utils.js
File metadata and controls
169 lines (142 loc) · 3.25 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
157
158
159
160
161
162
163
164
165
166
167
168
169
const fs = require('fs');
const path = require('path');
const url = require('url');
const querystring = require('querystring');
function parseToken(req) {
const tokenName = 'access_token';
const cookies = parseCookies(req.headers.cookie);
if (cookies[tokenName]) {
return cookies[tokenName];
}
const u = url.parse(req.url);
const params = querystring.parse(u.query);
if (params[tokenName]) {
return params[tokenName];
}
if (req.headers[tokenName]) {
return req.headers[tokenName];
}
if (req.body){
const body = JSON.parse(req.body);
if (body.params && body.params[tokenName]) {
return body.params[tokenName];
}
}
return null;
}
// taken from https://stackoverflow.com/a/31645958/943814
function parseCookies(cookie) {
let rx = /([^;=\s]*)=([^;]*)/g;
let obj = { };
for ( let m ; m = rx.exec(cookie) ; )
obj[ m[1] ] = decodeURIComponent( m[2] );
return obj;
}
function parsePath(pathStr) {
if (pathStr === '/') {
return [];
}
return pathStr.split('/').slice(1);
}
// TODO: keep an eye on the new version above and make sure we haven't
// broken anything
//function parsePath(path) {
// if (path.endsWith('/')) {
// path = path.slice(0, path.length - 1);
// }
//
// if (path === '' || path === '/') {
// return [];
// }
//
// return path.split('/');
//}
function encodePath(parts) {
return '/' + parts.join('/');
}
async function buildRemfsDir(fsPath) {
let filenames;
try {
filenames = await fs.promises.readdir(fsPath);
}
catch (e) {
res.statusCode = 404;
res.write("Not Found");
res.end();
return;
}
const remfs = {
type: 'dir',
children: {},
};
const localRemfs = await readLocalRemfs(fsPath);
Object.assign(remfs, localRemfs);
let totalSize = 0;
for (const filename of filenames) {
const childFsPath = path.join(fsPath, filename);
let stats;
try {
stats = await fs.promises.stat(childFsPath);
}
catch (e) {
console.error("This one shouldn't happen");
console.error(e);
continue;
}
totalSize += stats.size;
if (stats.isDirectory()) {
remfs.children[filename] = {
type: 'dir',
size: stats.size,
};
//remfs.children[filename] = await buildRemfsDir(childFsPath);
}
else {
remfs.children[filename] = {
type: 'file',
size: stats.size,
};
}
}
remfs.size = totalSize;
return remfs;
}
async function readLocalRemfs(fsPath) {
const localRemfsPath = path.join(fsPath, 'remfs.json');
try {
const localRemfsDataText = await fs.promises.readFile(localRemfsPath, {
encoding: 'utf8',
});
const localRemfsData = JSON.parse(localRemfsDataText);
return localRemfsData;
}
catch (e) {
//console.log("no remfs in", fsPath);
}
}
function getMime(ext) {
switch (ext) {
case '.js':
return 'application/javascript';
case '.json':
return 'application/json';
case '.html':
return 'text/html';
case '.css':
return 'text/css';
case '.jpeg':
case '.jpg':
case '.JPEG':
case '.JPG':
return 'image/jpeg';
case '.svg':
return 'image/svg+xml';
}
}
module.exports = {
parseToken,
parsePath,
encodePath,
buildRemfsDir,
getMime,
};