-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathstatic.js
132 lines (120 loc) · 4.11 KB
/
static.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
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
'use strict';
const { node, metarhia } = require('./deps.js');
const { Place } = require('./place.js');
const { join } = node.path.posix;
const WIN = process.platform === 'win32';
const MAX_FILE_SIZE = '10 mb';
const STATUS_CACHE = new Map();
const status = (code) => {
let file = STATUS_CACHE.get(code);
if (file) return file;
const status = node.http.STATUS_CODES[code] || 'Unknown error';
const data = Buffer.from(`<!DOCTYPE html>
<html><head><title>${code} ${status}</title></head>
<body><h1>${code} ${status}</h1></body></html>`);
file = { data, stat: null, code };
STATUS_CACHE.set(code, file);
return file;
};
class Static extends Place {
constructor(name, application, options = {}) {
super(name, application);
this.files = new Map();
this.ext = options.ext;
this.maxFileSize = -1;
}
get(key) {
return this.files.get(key);
}
getKey(filePath) {
const key = filePath.substring(this.path.length);
if (WIN) return metarhia.metautil.replace(key, node.path.sep, '/');
return key;
}
delete(filePath) {
const key = this.getKey(filePath);
this.files.delete(key);
}
async change(filePath) {
if (this.maxFileSize === -1) {
const maxFileSize = this.application.config?.cache?.maxFileSize;
const size = maxFileSize || MAX_FILE_SIZE;
this.maxFileSize = metarhia.metautil.sizeToBytes(size);
}
const ext = metarhia.metautil.fileExt(filePath);
if (this.ext && !this.ext.includes(ext)) return;
try {
const stat = await node.fsp.stat(filePath);
const key = this.getKey(filePath);
if (stat.size > this.maxFileSize) {
this.files.set(key, { data: null, stat });
} else {
const data = await node.fsp.readFile(filePath);
this.files.set(key, { data, stat });
}
} catch {
this.delete(filePath);
}
}
find(path, code, parent = false) {
let filePath = path;
const root = path === '/';
if (code) {
const fileName = `.${code}.html`;
filePath = join(filePath, fileName);
const file = this.get(filePath);
if (file) return { data: file.data, stat: null, code };
if (root) return status(code);
} else {
const folder = path.endsWith('/');
if (folder && !parent) {
filePath = join(path, 'index.html');
}
let file = this.get(filePath);
if (file) return { ...file, code: 200 };
filePath = join(path, '.virtual.html');
file = this.get(filePath);
if (file) return { ...file, code: -1 };
if (root) return this.find(filePath, 404, true);
}
filePath = node.path.dirname(path);
if (filePath !== '/') filePath += '/';
return this.find(filePath, code, true);
}
async serve(url, transport) {
const [filePath] = metarhia.metautil.split(url, '?');
const fileExt = metarhia.metautil.fileExt(filePath);
let file = this.find(filePath);
if (file.data && file.stat) {
if (file.code === -1) return void transport.write(file.data, 200, 'html');
return void transport.write(file.data, file.code, fileExt);
}
const absPath = join(this.path, url);
if (absPath.startsWith(this.path)) {
let { stat } = file;
if (!stat) stat = await node.fsp.stat(absPath).catch(() => null);
if (stat && stat.isFile()) {
const { size } = stat;
const options = { size };
let code = 200;
const { headers } = transport.req;
if (headers.range) {
const range = metarhia.metautil.parseRange(headers.range);
const { start, end = size - 1 } = range;
if (start >= end || start >= size || end >= size) {
file = this.find(filePath, 416);
return void transport.write(file.data, 416, fileExt);
}
options.start = start;
options.end = end;
code = 206;
}
const readable = node.fs.createReadStream(absPath, options);
return void transport.write(readable, code, fileExt, options);
}
}
if (file.code === -1) return void transport.write(file.data, 200, 'html');
return void transport.write(file.data, 404);
}
}
module.exports = { Static };