-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (123 loc) · 4.03 KB
/
server.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
const http = require('http');
const sharp = require("sharp");
var fs = require('fs');
const port = process.env.PORT || 8089;
function gtMarkerMatrix(id) {
var ids = [16, 23, 9, 14];
var index, val, x, y;
var marker = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]];
for (y = 0; y < 5; y++) {
index = (id >> 2 * (4 - y)) & 3;
val = ids[index];
for (x = 0; x < 5; x++) {
if ((val >> (4 - x)) & 1) {
marker[x][y] = 1;
} else {
marker[x][y] = 0;
}
}
}
return marker;
}
function getImage(size, id) {
var x, y;
var marker = gtMarkerMatrix(id);
var image;
if (size) {
size = 'height="' + size + '" width="' + size + '"';
} else {
size = '';
}
image = '<svg ' + size + ' viewBox="0 0 7 7" version="1.1" xmlns="http://www.w3.org/2000/svg">\n' +
' <rect x="0" y="0" width="7" height="7" fill="black"/>\n';
for (y = 0; y < 5; y++) {
for (x = 0; x < 5; x++) {
if (marker[x][y] === 1) {
image += ' <rect x="' + (x + 1) + '" y="' + (y + 1) +
'" width="1" height="1" fill="white" ' +
// Slight stroke to get around aliasing issues with adjacent rectangles
'stroke="white" stroke-width="0.01" />\n';
}
}
}
image += '</svg>';
return image;
}
const server = http.createServer((req, res) => {
if (req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
let data = JSON.parse(body);
let image = getImage(data.size || 150, data.id);
if (data.type === "svg") {
res.statusCode = 200;
res.setHeader('Content-Type', 'image/svg+xml');
res.write(image);
res.end();
} else if (data.type === "png") {
sharp(Buffer.from(image))
.png()
.toBuffer()
.then(data => {
res.statusCode = 200;
res.setHeader('Content-Type', 'image/png');
res.write(data);
res.end();
})
.catch(err => {
res.statusCode = 500;
res.end(err);
});
} else if (data.type === "jpeg") {
sharp(Buffer.from(image))
.jpeg()
.toBuffer()
.then(data => {
res.statusCode = 200;
res.setHeader('Content-Type', 'image/jpeg');
res.write(data);
res.end();
})
.catch(err => {
res.statusCode = 500;
res.end(err);
});
} else if (data.type === "base64") {
sharp(Buffer.from(image))
.png()
.toBuffer()
.then(data => {
res.statusCode = 200;
res.write(data);
res.end();
})
.catch(err => {
res.statusCode = 500;
res.end(err);
});
} else {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end("Please mention the format e.g. svg or image");
}
});
}
else {
fs.readFile('public/index.html', function (err, data) {
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write(data);
res.end();
});
}
});
server.listen(port, () => {
console.log(`Server listening on port :${port}`);
});