-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (99 loc) · 3.57 KB
/
index.js
File metadata and controls
118 lines (99 loc) · 3.57 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
/**
* OPResource express node server.
* Run with `node index.js`
*/
'use strict';
const path = require('path');
const express = require('express');
const fs = require('fs');
const stream = require('stream');
const gzip = require('zlib');
const app = express();
const port = process.env.PORT || 1337;
const layerspath = process.env.GAMMA_LAYERS_NEW || 'lang0000/layers/GAMMA_LAYERS_NEW';
// Log on any file access
app.use(function(req, res, next) {
let agent = req.headers['user-agent'];
if (!agent || !agent.match(/Perpetuum/)) {
res.status(403).send();
}
else
{
console.log(req.originalUrl);
next();
}
});
// By default, we give resources along the request path
app.use('/lang0000', express.static('lang0000', { fallthrough: true }));
// Replace the not found images with the default.
app.use('/**/*.png', express.static('lang0000/icons/default/00000000.png'));
// Сompress the file, add its size to the beginning and send it as an answer.
function DeflateAndSeend(file, responce) {
// Check file availability.
if (!fs.existsSync(file)) {
console.log('File does not exist:', file);
return responce.status(400).send();
}
// Four bytes with the size of the source file.
let buf = Buffer.alloc(4);
buf.writeInt32LE(fs.statSync(file).size);
responce.write(buf);
// Pipeline for delivering compressed content to the client.
console.log('Deflate and send file:', file);
stream.pipeline(fs.createReadStream(file), gzip.createDeflate(), responce, (err) => {
if (err) {
console.log('Pipeline error:', err, 'in', file);
return responce.status(400).send();
}
});
}
// Prepare and deliver layer files from raw files intended for the server.
function ProcessReq(layer, request, responce)
{
// Looking for the zone number in the request.
const url = request.url.match(/\/\d{4}\//);
if (url == null) {
console.log('No zone number');
return responce.status(400).send();
}
const zone = url[0].match(/\d{4}/)[0];
// Сheck that the request matches the template.
const expect = new RegExp('\\/lang0000\\/layers\\/' + zone + '\\/' + layer + zone + '\\/\\d{8}\\.dat');
if (expect.exec(request.url) == null) {
console.log('URL format incorrect');
return responce.status(400).send();
}
// Collecting the path to the file with raw data.
const bin = path.join(__dirname, layerspath, layer + '.' + zone + '.bin');
console.log('Client hit', layer, bin);
// This should compress and transfer the file on the fly.
DeflateAndSeend(bin, responce);
}
// Thes altitude layer request.
app.get('/lang0000/layers/*/altitude*/*.dat', function (req, res) {
ProcessReq('altitude', req, res);
});
// The blocks layer request.
app.get('/lang0000/layers/*/blocks*/*.dat', function (req, res) {
ProcessReq('blocks', req, res);
});
// The altitude layer request.
app.get('/lang0000/layers/*/control*/*.dat', function (req, res) {
ProcessReq('control', req, res);
});
// The plants layer request.
app.get('/lang0000/layers/*/plants*/*.dat', function (req, res) {
ProcessReq('plants', req, res);
});
// The index
app.get('/', function(req, res, next) {
console.log('Client hit index!');
// Check file availability.
let index = path.join(__dirname, 'resource_0.dat')
if (!fs.existsSync(index)) {
console.log('File does not exist:', index);
return res.status(400).send();
}
res.sendFile(index);
});
app.listen(port, () => console.log('Local development OPResource server initialized'));