-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
113 lines (91 loc) · 3.18 KB
/
server.js
File metadata and controls
113 lines (91 loc) · 3.18 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
const http2 = require('http2');
const { readFileSync } = require('fs');
const { readdir, lstat, open } = require('fs').promises;
const mimeType = require('mime-types');
const argv = require('yargs').argv;
const TYPE_DIRECTORY = 'DIRECTORY';
const TYPE_FILE = 'FILE';
const TYPE_OTHER = 'OTHER';
const {
HTTP2_HEADER_STATUS,
HTTP2_HEADER_CONTENT_TYPE,
HTTP2_HEADER_PATH,
HTTP2_HEADER_METHOD,
HTTP2_METHOD_GET
} = http2.constants;
process.env.UV_THREADPOOL_SIZE = 1024;
process.on('uncaughtException', err => console.error('Got uncaught exception', err));
process.on('unhandledRejection', event => console.error('Got unhandled rejection', event));
main();
async function main() {
// deploy server
const server = initServer(argv.port);
server.on('error', err => {
console.error(`Server has encountered an error: ${err}.\nClosing server...`);
server.close();
process.exit(1);
});
server.on('sessionError', err => {
console.error(`Server had a session error:`, err);
});
server.on('stream', async (stream, headers) => {
try {
await dispatch(stream, headers);
} catch (err) {
console.error(`dispatch failed: `, err);
stream.respond({[HTTP2_HEADER_STATUS]: 500});
stream.end(err.toString());
}
});
}
function initServer(port){
const server = http2.createSecureServer({
peerMaxConcurrentStreams: 500,
key: readFileSync('./cert/key.pem'),
cert: readFileSync('./cert/certificate.pem'),
MaxSessionMemory: argv.maxMem
});
server.listen(port);
console.log(`Server deployed. Listening on port number: ${port}`);
return server;
}
async function dispatch(stream, headers) {
const method = headers[HTTP2_HEADER_METHOD];
if (method === HTTP2_METHOD_GET) {
// get path type
const {[HTTP2_HEADER_PATH]: path} = headers;
const stats = await lstat(path);
const type = getType(stats);
switch (type) {
case TYPE_DIRECTORY: { // if type is directory, list entries and send back
const entries = await readdir(path);
stream.respond({
[HTTP2_HEADER_STATUS]: 200
});
stream.end(JSON.stringify(entries));
break;
}
case TYPE_FILE: { // if type is file, download it.
const contentType = mimeType.lookup(path) || 'application/octet-stream';
const fileHandle = await open(path, 'r');
stream.respondWithFD(fileHandle.fd, {
[HTTP2_HEADER_CONTENT_TYPE]: contentType,
[HTTP2_HEADER_STATUS]: 200
});
stream.on('close', async () => {
await fileHandle.close();
});
break;
}
}
}
}
function getType(stats){
let type = TYPE_OTHER;
if (stats.isDirectory()) {
type = TYPE_DIRECTORY;
} else if (stats.isFile()) {
type = TYPE_FILE;
}
return type;
}