forked from jordicenzano/webserver-chunked-growingfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunkedGrowingWebserver.js
196 lines (148 loc) · 6.25 KB
/
chunkedGrowingWebserver.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const uuidv4 = require('uuid/v4');
const readGrowingFileStream = require('./readGrowingFileStream.js');
"use strict";
const GHOST_SUFFIX = ".tmp";
// Webserver
class chunkedGrowingWebserver {
constructor(base_path, port, host, headers_by_ext, cors_default, public_fallback_dir) {
this.base_path = base_path;
this.port = port;
this.host = null;
this.headers_by_ext = null;
this.cors_default = null;
this.public_fallback = null;
this.server = null;
this.endCallback = null;
if ((typeof (host) === 'string') && (host != null)) {
this.host = host;
console.log("Set bind host addr: " + host);
}
if ((typeof (headers_by_ext) === 'object') && (headers_by_ext != null)) {
this.headers_by_ext = headers_by_ext;
console.log("Loaded HEADERS: " + JSON.stringify(this.headers_by_ext));
}
if ((typeof (cors_default) === 'object') && (cors_default != null)) {
this.cors_default = cors_default;
console.log("Loaded CORS: " + JSON.stringify(this.cors_default));
}
if ((typeof (public_fallback_dir) === 'string') && (public_fallback_dir != null)) {
if (fs.existsSync(public_fallback_dir)) {
this.public_fallback = public_fallback_dir;
console.log("Set fallback dir to: " + public_fallback_dir);
}
}
}
start() {
if (this.server != null)
return;
const that = this;
//Create simple static webserver
this.server = http.createServer(function (req, res) {
//Create a UUID per session
let uuid = uuidv4();
//Get file path name
const filepath_name = that._cleanUrlPath(url.parse(req.url).pathname);
//Find file in base dir
let local_path_filename = path.resolve(that.base_path, filepath_name);
//Create ghost file path
let local_path_filename_ghost = path.join(path.dirname(local_path_filename), path.basename(local_path_filename) + GHOST_SUFFIX);
if (!fs.existsSync(local_path_filename)) {
//Find in config/public (default)
if (that.public_fallback != null) {
local_path_filename = path.resolve(that.public_fallback, filepath_name);
if (!fs.existsSync(local_path_filename) && !fs.existsSync(local_path_filename_ghost))
local_path_filename = null;
}
}
if (local_path_filename === null) {
console.log(uuid + "-404: " + filepath_name + ". Not found: " + local_path_filename);
that._sendError(uuid, filepath_name, res, 404, 'Page Was Not Found');
}
else {
console.log(uuid + "-(" + filepath_name + ") Local: " + local_path_filename);
let readFileStream = null;
let writtenData = 0;
if (!fs.existsSync(local_path_filename_ghost)) {
//Use regular file reader
console.log(uuid + "-(" + filepath_name + ") Regular read detected");
readFileStream = new fs.createReadStream(local_path_filename);
}
else {
//Use growing file reader
console.log(uuid + "-(" + filepath_name + ") Growing file read detected");
readFileStream = new readGrowingFileStream.readGrowingFileStream(local_path_filename, local_path_filename_ghost);
}
let headers = that._getHeaderBasedOnFileType(that.headers_by_ext, that.cors_default, path.extname(filepath_name).toLocaleLowerCase());
res.writeHead(200, headers);
readFileStream.on("data", function(data) {
res.write(data);
writtenData = writtenData + data.length;
console.log(uuid + "-(" + filepath_name + ") Write length: " + writtenData.toString());
});
readFileStream.on("error", function(err) {
console.log(uuid + "-(" + filepath_name + ") Error: " + err);
res.end();
});
readFileStream.on("end", function() {
console.log(uuid + "-(" + filepath_name + ") Write closed! Written length: " + writtenData.toString());
res.end();
});
}
});
if (this.host != null)
this.server.listen(this.port, this.host);
else
this.server.listen(this.port);
this.server.that = this;
console.log("Server listening on port: " + this.port.toString());
}
stop(callback) {
if (this.server != null) {
this.endCallback = callback;
this.server.close(this._endClose);
}
else {
callback();
}
}
_endClose() {
if (this.that.endCallback != null) {
this.that.endCallback();
this.that.endCallback = null;
}
this.that.server = null;
}
_sendError(uuid, filename, res, status, message) {
console.log(uuid + "- 500: " + filename + ". Message:" + message);
res.writeHead(status, {'Content-type':'text/plain'});
res.write(message);
res.end();
}
//Add response headers based on requested file extension
_getHeaderBasedOnFileType (headers_by_ext, cors_default, extension) {
let ret = {};
if (headers_by_ext != null) {
ret = headers_by_ext["default"];
if (extension in headers_by_ext)
ret = headers_by_ext[extension];
}
//Add CORS
if (cors_default != null)
ret = Object.assign(ret, cors_default);
return ret;
}
_cleanUrlPath(path) {
let ret = path;
while ((ret.length > 0) && (ret[0] === '/'))
ret = ret.substr(1);
return ret;
}
}
//Export class
module.exports.chunkedGrowingWebserver = chunkedGrowingWebserver;
module.exports.GHOST_SUFFIX = GHOST_SUFFIX;