-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
712 lines (544 loc) · 20.2 KB
/
functions.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
'use strict'
//----------------
// Includes: Self
//----------------
const color = require('./color.js')
const memory = require('./memory.js')
const host = require('./host.js')
const shared = require('./shared.js')
//----------
// Includes
//----------
const fs = require('fs')
const http = require('http')
const https = require('https')
const path = require('path')
const stream = require('stream')
const url = require('url')
const util = require('util')
//-----------
// Promisify
//-----------
const fsReadFile = util.promisify(fs.readFile)
const fsStat = util.promisify(fs.stat)
//-----------
// Variables
//-----------
const functions = {
// cacheControlPrivate
// cloneObj
// fileExtAcceptRanges
// fileExtSupportCompress
// fileExtToMime
// fileStat
// fileStream
// flowThrow
// hash
// initTome
// log
// logAlways
// logCompress
// logError
// memoryCacheClean
// memoryCacheEnsure
// request
// responseTakenCareOf
// serverListenError
// validRange
// wait
} // functions
//-----------
// Functions
//-----------
functions.cacheControlPrivate = function cacheControlPrivate(fileExt, option) {
/*
Figure out if a file extension should have its "Cache-Control" header set to private.
@param {String} fileExt File extension like "html".
@param {Object} option Host option object.
@return {Boolean}
*/
return (option.cacheControlPrivate.indexOf(fileExt) >= 0) ? true : false
} // cacheControlPrivate
functions.cloneObj = function cloneObj(object) {
/*
Clone an object recursively so the return is not a reference to the original object.
@param {Object} obj Object like { number: 1, bool: true, array: [], subObject: {} }
@return {Object}
*/
if (object === null || typeof object !== 'object') {
// return early for boolean, function, null, number, string, symbol, undefined
return object
} // if
const objectConstructor = object.constructor()
for (const key in object) {
// call self recursively
objectConstructor[key] = functions.cloneObj(object[key])
} // for
return objectConstructor
} // cloneObj
functions.fileExtAcceptRanges = function fileExtAcceptRanges(fileExt, option) {
/*
Figure out if a file extension supports ranges.
@param {String} fileExt File extension like "mp4".
@param {Object} option Host option object.
@return {Boolean}
*/
return (option.fileExtAcceptRanges.indexOf(fileExt) >= 0) ? true : false
} // fileExtAcceptRanges
functions.fileExtSupportCompress = function fileExtSupportCompress(fileExt, option) {
/*
Figure out if a file extension is intended to be served as a "br" or "gzip" compressed file.
@param {String} fileExt File extension like "css".
@param {Object} option Host option object.
@return {Boolean}
*/
return (option.fileExtSupportCompress.indexOf(fileExt) >= 0) ? true : false
} // fileExtSupportCompress
functions.fileExtToMime = function fileExtToMime(fileExt, option) {
/*
Return the mime type for a file extension.
@param {String} fileExt File extension like "wav".
@param {Object} option Host option object.
@return {String} Mime type like "audio/wav".
*/
return option.mimeType[fileExt] || 'application/octet-stream'
} // fileExtToMime
functions.fileStat = async function fileStat(filePath, option) {
/*
Get statistics about a file.
Statistics can come from memory or disk.
@param {String} filePath File path like "/path/to/file.txt".
@param {Object} option Host option object.
@return {Object} Object like { modified: 123, size: 456 }
*/
const stats = {
// modified
// size
}
if (option.memoryCache) {
await functions.memoryCacheEnsure(filePath, option)
const memCache = memory[option.domains[0]]
// stats from memory
stats.modified = memCache[filePath].modified,
stats.size = memCache[filePath].size
} else {
// stats from disk
const statsFromDisk = await fsStat(filePath)
stats.modified = new Date(statsFromDisk.mtime).getTime(),
stats.size = statsFromDisk.size
} // if
return stats
} // fileStat
functions.fileStream = async function fileStream(filePath, option, fileOptions = {}) {
/*
Return a readable file stream.
Readable streams can be from memory or disk.
@param {String} filePath File path like "/web/video.mp4".
@param {Object} option Host option object.
@param {Object} [fileOptions] Optional. Object like { start: 0, end: 1024 }
@return {Object} A readable file stream.
*/
let theStream
let serveFromMemory = false // will be set to true if memory cache is being used and the filePath in question is cached in memory
if (option.memoryCache) {
const memCache = memory[option.domains[0]]
if ((memCache[filePath] || {}).content !== undefined) {
// serve from memory
serveFromMemory = true
log('functions.fileStream -> serve from memory')
theStream = stream.Readable()
const chunkSize = 65536
let pos = 0
let end = memCache[filePath].size
if (fileOptions.start !== undefined) {
pos = fileOptions.start
} // if
if (fileOptions.end !== undefined) {
end = fileOptions.end
} // if
theStream._read = function() {
if (pos >= end) {
theStream.push(null)
return
}
const chunk = memCache[filePath].content.slice(pos, pos + chunkSize)
theStream.push(chunk)
pos += chunkSize
}
} // if
} // if
if (serveFromMemory === false) {
// serve from disk
log('functions.fileStream -> serve from disk')
theStream = fs.createReadStream(filePath, fileOptions)
} // if
return theStream
} // fileStream
functions.flowThrow = function flowThrow(statusCode = 500, content = 'Server error.') {
/*
Throw an error inside a flow function to release control back to the main "flow.control" function.
@param {Number} [statusCode] Optional. Defaults to the number "500".
@param {String} [content] Optional. Defaults to "Server error."
*/
throw({
'statusCode': statusCode,
'content': content
})
} // flowThrow
functions.hash = function hash(str) {
/*
Create a unique hash based on the input string.
Used for setting "ETag" headers.
@param {String} str String to hash like "/file.txt-123".
@return {String} Hashed string like "1139087527".
*/
// from https://github.com/darkskyapp/string-hash/blob/master/index.js
let hash = 5381
let i = str.length
while(i) {
hash = (hash * 33) ^ str.charCodeAt(--i)
} // while
/*
JavaScript does bitwise operations (like XOR, above) on 32-bit signed
integers. Since we want the results to be always positive, convert the
signed int to an unsigned by doing an unsigned bitshift.
*/
return (hash >>> 0).toString()
} // hash
functions.initTome = function initTome() {
/*
If needed, create a tome config file in the current directory.
*/
const destPath = path.join(shared.path.pwd, 'tome.js')
const destPathAlt = path.join(shared.path.pwd, 'tome-config.js')
const statSyncOptions = {
throwIfNoEntry: false
}
const destPathExists = fs.statSync(destPath, statSyncOptions)
const destPathAltExists = fs.statSync(destPathAlt, statSyncOptions)
if (destPathExists === undefined && destPathAltExists === undefined) {
// no tome config files found
try {
const sourcePath = path.join(shared.path.self, 'template', 'tome-config.js')
fs.copyFileSync(sourcePath, destPathAlt, 1) // 1 means fail if the destination file already exists
logAlways('Created a "tome-config.js" file.\n')
} catch (error) {
logAlways('Tome halted due to errors.\n')
logError(error)
process.exit()
} // catch
} else {
logAlways('Tome halted due to errors.\n')
const fileName = (destPathExists) ? 'tome.js' : 'tome-config.js'
logAlways('A "' + fileName + '" file already exists in the current directory.\n')
process.exit()
} // if
} // initTome
functions.log = function log(info = '') {
/*
If shared.debug is true, log information to the console.
@param {*} [info] Optional. Anything that can be logged to the console. Defaults to an empty string.
*/
if (shared.debug === true) {
logAlways(info)
} // if
} // log
functions.logAlways = function logAlways(info = '') {
/*
Log information to the console.
@param {*} [info] Optional. Anything that can be logged to the console. Defaults to an empty string.
*/
if (typeof(info) === 'string') {
if (info.indexOf(`\u001B`) < 0) { // unicode escape
info = color.gray(info)
} // if
} // if
console.log(info)
} // logAlways
functions.logCompress = function logCompress(obj) {
/*
Compress a log object before it is written to disk.
@param {Object} obj Log object made by "flow.control".
@return {String}
*/
const compressObj = {
request: {
method: obj.request.method,
port: obj.request.port,
url: obj.request.url
},
response: {
statusCode: obj.response.statusCode
},
stats: { // stats
bytesSent: obj.stats.bytes.sent,
timeBegin: obj.stats.time.begin,
timeTotal: obj.stats.time.total
}
} // compressObj
//-----------------
// Request Headers
//-----------------
if (obj.request.headers.referer) {
compressObj.request.referer = obj.request.headers.referer
} // if
//------------------
// Response Headers
//------------------
if (obj.response.headers['cache-control']) {
compressObj.response.cacheControl = obj.response.headers['cache-control'].replace(', ', ',')
} // if
if (obj.response.headers['content-encoding']) {
compressObj.response.contentEncoding = obj.response.headers['content-encoding']
} // if
if (obj.response.headers['content-length']) {
compressObj.response.contentLength = obj.response.headers['content-length']
} // if
if (obj.response.headers['content-type']) {
compressObj.response.contentType = obj.response.headers['content-type'].replace('; ', ';')
} // if
if (obj.response.headers.etag) {
compressObj.response.etag = obj.response.headers.etag
} // if
if (obj.response.headers.vary) {
compressObj.response.vary = obj.response.headers.vary
} // if
return JSON.stringify(compressObj)
} // logCompress
functions.logError = function logError(error) {
/*
Log an error to the console.
@param {String,Object} error String or object to log to the console.
*/
if (typeof(error) === 'object' && error instanceof Error === false) {
logAlways(color.red(JSON.stringify(error, null, 2))) // indent 2 spaces
} else {
logAlways(color.red('Error ->'))
logAlways(error)
} // if
} // logError
functions.memoryCacheClean = async function memoryCacheClean() {
/*
Clean up files in memory that are no longer fresh or no longer on disk.
@return {Promise}
*/
const domains = Object.keys(memory)
const now = Date.now()
domains.map(function(domain) {
if (host[domain].option.memoryCache) {
// this host is using a memoryCache
const files = Object.keys(memory[domain])
const filesCheckDisk = []
const expireTime = now - ((host[domain].option.memoryCacheSeconds + 10) * 1000)
files.forEach(function(filePath) {
if (memory[domain][filePath].checked > expireTime) {
// file still fresh
filesCheckDisk.push(filePath)
} else {
// file is no longer fresh
delete memory[domain][filePath]
} // if
}) // forEach
if (filesCheckDisk.length) {
filesCheckDisk.forEach(async function(filePath) {
try {
await fsStat(filePath)
} catch (error) {
// file no longer exists
delete memory[domain][filePath]
} // catch
}) // forEach
} // if
} // if
}) // map
} // memoryCacheClean
functions.memoryCacheEnsure = async function memoryCacheEnsure(filePath, option) {
/*
Ensure a cached file is fresh and in memory. Load or reload the file from disk if needed.
@param {String} filePath File path like "/path/to/file.txt".
@param {Object} option Host option object.
@return {Promise}
*/
const now = Date.now()
let loadFromDisk = true
let stats
if (typeof memory[option.domains[0]] === 'undefined') {
memory[option.domains[0]] = {}
} // if
const memCache = memory[option.domains[0]]
if (typeof(memCache[filePath]) === 'object') {
// file in memory
if ((memCache[filePath].checked + (option.memoryCacheSeconds * 1000)) > now) {
// cache entry is still fresh since we last checked
loadFromDisk = false
} else {
// cache entry is not too fresh
// check if we need to update our cache from disk
try {
stats = await fsStat(filePath)
const modified = new Date(stats.mtime).getTime()
if (modified === memCache[filePath].modified) {
// cache is still fresh
memCache[filePath].checked = now
loadFromDisk = false
} else {
// cache is out of date so re-read the file into memory
} // if
} catch(error) {
delete memCache[filePath]
} // catch
} // if
} // if
if (loadFromDisk) {
try {
if (typeof(stats) !== 'object') {
stats = await fsStat(filePath)
} // if
memCache[filePath] = {
checked: now,
content: null, // will only contain data if this file type is allowed to be cached
modified: new Date(stats.mtime).getTime(),
size: stats.size
}
const fileExt = path.extname(filePath).replace('.', '').toLowerCase()
if (option.memoryDoNotCacheFileExt.indexOf(fileExt) < 0) {
memCache[filePath].content = await fsReadFile(filePath)
} // if
} catch(error) {
if (error.code === 'ENOENT') {
log('functions.memoryCacheEnsure -> file not found')
} else {
logError('functions.memoryCacheEnsure -> error loading from disk')
} // if
throw(error)
} // catch
} // if
} // memoryCache
functions.request = async function request(link, options) {
/*
Request a URL using GET, POST, HTTP, or HTTPS.
@param {String} link String like "https://local.test/folder?a=1".
@param {Object} options Optional. Object like { method: 'POST', body: '...' }
@return {Promise}
*/
link = link || ''
if (link.toLowerCase().indexOf('http') !== 0) {
link = 'http://' + link
} // if
link = url.parse(link)
// Besides the optional BODY property, the option object below is the same as -> https://nodejs.org/api/http.html#http_http_request_url_options_callback
options = options || {}
options.body = options.body || null
options.headers = options.headers || {}
options.hostname = link.hostname || 'local.test'
options.method = options.method || 'GET'
options.path = link.path || '/'
options.port = link.port || (link.protocol === 'https:' ? 443 : 80)
if (options.method === 'POST') {
if (options.headers['Content-Type'] === undefined) {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded'
} // if
if (options.body) {
options.headers['Content-Length'] = Buffer.byteLength(options.body)
} // if
} // if
return new Promise(function(resolve, reject) {
const library = (link.protocol === 'https:') ? https : http
const clientRequest = library.request(options, function(incomingMessage) {
const response = {
statusCode: incomingMessage.statusCode,
headers: incomingMessage.headers,
body: []
}
incomingMessage.on('data', function(chunk) {
// accumulate body data
response.body.push(chunk)
})
incomingMessage.on('end', function() {
response.body = response.body.join('')
resolve(response)
})
})
clientRequest.on('error', function(error) {
reject(error)
})
if (options.body && options.method === 'POST') {
clientRequest.write(options.body)
} // if
clientRequest.end()
}) // promise
} // request
functions.responseTakenCareOf = function responseTakenCareOf(response) {
/*
Figure out if a node HTTP response like "obj.response" has been taken care of by any previous flow functions.
@param {Object} response Node HTTP response object within a reusable object created by "flow.control".
@return {Boolean}
*/
if (response.statusCode === 200 ||
response.statusCode === 206 ||
response.statusCode === 301 ||
response.statusCode === 304 ||
response.statusCode === 403
) {
return true
} else {
return false
} // if
} // responseTakenCareOf
functions.serverListenError = function serverListenError(error) {
/*
Listener function for http and "https.createServer" error events.
@param {Object} error Error object.
*/
if (error.port === shared.http.port) {
logAlways(color.red('Error -> httpServer.listen(' + shared.http.port + ')'))
} else {
logAlways(color.red('Error -> httpsServer.listen(' + shared.https.port + ')'))
} // if
if (error.code === 'EACCES') {
logAlways(color.red('Error -> Could not bind to port.'))
logError(error)
logAlways('Ports lower than 1024 may require additional privileges.')
} else {
logError(error)
} // if
logAlways()
} // serverListenError
functions.validRange = function validRange(range, fileSize) {
/*
Used by the range file flow function to determine if a requested range is satisfiable or not.
@param {String} range Range request string like "bytes=0-1023".
@param {Number} fileSize File size in bytes.
@return {Boolean,Object} Boolean false or a valid range array like [0, 1023]
*/
range = range.replace('bytes=', '').split('-')
if (range.length !== 2) {
return false
} // if
const rangeStart = parseInt(range[0], 10) || 0
const rangeEnd = parseInt(range[1], 10) || fileSize - 1
if (rangeStart >= fileSize ||
rangeEnd >= fileSize ||
rangeStart > rangeEnd) {
return false
} // if
return [rangeStart, rangeEnd]
} // validRange
functions.wait = function wait(ms) {
/*
Promise that is useful for injecting delays and testing scenarios.
@param {Number} ms Number of milliseconds to wait before returning.
@return {Promise}
*/
return new Promise(resolve => setTimeout(resolve, ms))
} // wait
//---------
// Aliases
//---------
const log = functions.log
const logAlways = functions.logAlways
const logError = functions.logError
//---------
// Exports
//---------
module.exports = functions