-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
409 lines (383 loc) · 11.8 KB
/
app.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
var port = process.env.PORT || 3000;
var config = require("./src/main/node/config.js");
var fieldTypes = require("./src/main/node/fieldTypes.js");
var PROTOS = require('./src/main/node/PrototypeExpander.js');
var X3DJSONLD = require('./src/main/node/X3DJSONLD.js');
var { DOMParser, XMLSerializer } = require('@xmldom/xmldom');
var path = require('path');
var express = require('express');
var fs = require('fs');
var https = require('https');
const { glob, globSync } = require('glob');
var path = require('path');
var fileURLToPath = require('url');
var DOMParser = require('@xmldom/xmldom');
var DOM2JSONSerializer = require("./src/main/node/DOM2JSONSerializer.js");
var mapToMethod = require("./src/main/node/mapToMethod.js");
// var { runAndSend } = require( './src/main/node/runAndSend';
var www = config.x3dcode();
var app = express();
app.use(express.static('/'));
app.use(express.static('dist'));
app.use(express.static('src/main/data'));
app.use(express.static('src/main/personal'));
app.use(express.static('src/main/node'));
app.use(express.static('src/main/orig'));
app.use(express.static('src/main/out'));
app.use(express.static('src/main/schema'));
app.use(express.static('src/main/resources'));
app.use(express.static('src/main/shaders'));
app.use(express.static('src/main/nashorn'));
app.use(express.static('src/main/lib'));
app.use(express.static('src/main/html'));
app.use(express.static('src/main'));
async function convertX3dToJson(res, infile, outfile, next) {
try {
var data = await fs.promises.readFile(infile);
var doc = null;
var domParser = new DOMParser();
doc = domParser.parseFromString (data.toString(), 'application/xml');
var element = doc.documentElement;
console.error("Calling converter "+serializer+" on "+infile);
var serializer = new DOM2JSONSerializer();
var str = serializer.serializeToString(null, element, outfile, mapToMethod, fieldTypes);
var json = JSON.parse(str);
// json = PROTOS.externalPrototypeExpander(outfile, json);
send(res, json, "text/json", next);
} catch (e) {
next();
console.error(e, "Problems converting", infile, "to", outfile);
}
/*
runAndSend(['---overwrite', '---', infile], function(json) {
// json = PROTOS.externalPrototypeExpander(outfile, json);
// console.error("Json", json);
send(res, json, "text/json", next);
});
*/
}
function send(res, data, type, next) {
sendNoNext(res, data, type);
next();
}
function sendNoNext(res, data, type) {
// console.error("Type", type);
try {
if (!type.startsWith("image/")) {
res.header("Content-Type", type);
}
res.header("Content-Type-Options", "nosniff");
} catch (e) {
console.error(e);
}
res.send(data);
}
var filecount = 0;
/*
app.post("/convert", function(req, res, next) {
var buf = '';
req.on('data', function(chunk){ buf += chunk; });
req.on('end', function(){
var infile = __dirname+"/"+ (filecount)+".x3d";
var outfile = __dirname+"/"+ (filecount)+".json";
filecount++;
console.error("converting ", buf);
fs.writeFile(infile, buf, function(err) {
if (err) throw err;
convertX3dToJson(res, infile, outfile, function() {
console.error("Unlinking", infile);
fs.unlink(infile);
console.error("Unlinking", outfile);
fs.unlink(outfile);
next();
});
});
});
});
*/
/*
app.get("/X3dGraphics.com/*.x3d", function(req, res, next) {
var url = req._parsedUrl.pathname;
var hash = url.indexOf("#");
var infile = url;
if (hash > 0) {
infile = url.substring(0, hash);
}
infile = "https:/" + infile;
console.error("Proxy", infile);
console.error("=========== converting == ", infile);
var outfile = infile.substr(0, infile.lastIndexOf("."))+".json";
convertX3dToJson(res, infile, outfile, next);
});
app.get("/www.web3d.org/*.x3d", function(req, res, next) {
var url = req._parsedUrl.pathname;
var hash = url.indexOf("#");
var infile = url;
if (hash > 0) {
infile = url.substring(0, hash);
}
infile = www + "/" + infile;
console.error("Proxy", infile);
console.error("=========== converting == ", infile);
var outfile = infile.substr(0, infile.lastIndexOf("."))+".json";
convertX3dToJson(res, infile, outfile, next);
});
*/
app.get("/www.web3d.org/*.wrl", async function(req, res, next) {
var url = req._parsedUrl.pathname;
var hash = url.indexOf("#");
var infile = url;
if (hash > 0) {
infile = url.substring(0, hash);
}
infile = www + "/" + infile;
var data = await fs.promises.readFile(infile);
sendNoNext(res, data.toString(), "model/vrml");
});
/*
app.get("/data/*.x3d*", function(req, res, next) {
var url = req._parsedUrl.pathname;
var hash = url.indexOf("#");
var infile = url;
if (hash > 0) {
infile = url.substring(0, hash);
}
console.error("=========== converting == ", infile);
var outfile = infile.substr(0, infile.lastIndexOf("."))+".json";
convertX3dToJson(res, infile, outfile, next);
});
*/
app.get("/files", function(req, res, next) {
var test = req._parsedUrl.query;
// var test = req._parsedUrl.pathname;
while (test.startsWith("/")) {
test = test.substr(1);
}
console.log("here's the file search query", test);
var json = [];
// globSync(www+'/**/*.wrl', function( err, files ) {
// if (err) return;
// files.forEach(function(file) {
// if (new RegExp(test).test(file)) {
// json.push(file.substr(www.length, file.length-www.length));
// console.error(file);
// }
// });
// });
// globSync(www+'/**/*.x3d', function( err, files ) {
// if (err) return;
// files.forEach(function(file) {
// if (new RegExp(test).test(file)) {
// json.push(file.substr(www.length, file.length-www.length));
// console.error(file);
// }
// });
// });
// globSync(www+'/**/*.json', function( err, files ) {
// if (err) return;
// files.forEach(function(file) {
// if (new RegExp(test).test(file)) {
// json.push(file.substr(www.length, file.length-www.length));
// console.error(file);
// }
// });
// });
let dirs = ['src/main/Library/**', 'src/main/data/**','src/main/personal/**', 'src/main/wrl/**', 'src/main/ply/**', 'src/main/stl/**'];
for (let dir in dirs) {
let files = globSync(dirs[dir]);
console.log("searching", dirs[dir]);
files.forEach(function(file) {
// console.log(file, "matching", test);
if (new RegExp(test).test(file)) {
console.log(file, "matches", test);
file = file.replace(/src[\/\\]main/, '..');
file = file.replace(/\\/g, '/');
json.push(file);
console.log("returning", file);
} else {
// console.log(file, "does not match", test);
}
});
}
console.log("Sending ", json.length, "files");
send(res, json, "text/json", next);
});
function magic(path, type) {
query = path.indexOf("?");
if (query >= 0) {
path = path.substr(query);
}
var hash = path.indexOf("#");
if (hash > 0) {
path = path.substr(hash);
}
app.get(path, async function(req, res, next) {
var url = req._parsedUrl.pathname;
try {
while (url.startsWith("/")) {
url = url.substr(1);
}
console.error(req.ip+": Requested", url);
var wind = url.indexOf("www.web3d.org");
if (wind >= 0) {
url = url.substring(wind);
var cwind = config.examples().indexOf("www.web3d.org");
url = config.examples().substr(0, cwind) + url;
} else {
url = __dirname+"/"+ url;
}
if (fs.existsSync(url)) {
console.error("Reading", url);
var data = await fs.promises.readFile(url);
if (type.startsWith("image") || type.startsWith("audio") || type.startsWith("video")) {
sendNoNext(res, data, type);
} else {
sendNoNext(res, data.toString(), type);
}
} else {
console.error("File does not exist", url);
}
} catch (e) {
console.error(e, "Couldn't read", url);
next();
}
});
}
/*
function processX3d(req, res, next) {
var url = req._parsedUrl.pathname.substr(1);
console.error("X3D url", url);
var hash = url.indexOf("#");
var infile = url;
if (hash > 0) {
infile = url.substring(0, hash);
}
infile = www + "/" + infile;
console.error("=========== converting == ", infile);
var outfile = infile.substr(0, infile.lastIndexOf("."))+".json";
convertX3dToJson(res, infile, outfile, next);
}
app.get("*.x3d#*", processX3d);
app.get("*.x3d", processX3d);
*/
magic("*.gif", "image/gif");
magic("*.jpg", "image/jpeg");
magic("*.JPG", "image/jpeg");
magic("*.jpeg", "image/jpeg");
magic("*.png", "image/png");
magic("*.mpg", "video/mpeg");
magic("*.mp4", "video/mp4");
magic("*.ogv", "video/ogg");
magic("*.wav", "audio/wav");
magic("*.mp3", "audio/mpeg3");
magic("*.ply", "application/octet-stream");
magic("*.stl", "application/octet-stream");
magic("*.vs", "x-shader/x-vertex");
magic("*.fs", "x-shader/x-fragment");
// magic("*.vs", "text/plain");//"x-shader/x-vertex");
// magic("*.fs", "text/plain");//"x-shader/x-fragment");
magic("*.js", "text/javascript");
magic("*.py", "text/python");
magic("/dist/*.mjs", "text/javascript");
magic("/src/main/node/*.mjs", "text/javascript");
magic("*.js.map", "application/json");
magic("*.csv", "text/csv");
magic("/*.xhtml", "application/xhtml+xml");
magic("/*.xsd", "application/xml");
magic("/*.html", "text/html");
magic("*.xslt", "text/xsl");
magic("*.css", "text/css");
magic("*.swf", "application/x-shockwave-flash");
magic("/**/schema/*.json", "text/json");
magic("*.x3d", "model/x3d+xml");
magic("*.x3dv", "model/x3d+vrml");
magic("*.wrl", "model/vrml");
magic("*.gltf", "text/json");
magic("*.glb", "application/octet-stream");
magic("*.bin", "application/octet-stream");
/*
magic("*.xml", "text/xml");
*/
app.get("*.json", async function(req, res, next) {
var url = req._parsedUrl.pathname.substr(1);
console.error(req.ip+": Requested JSON File", url);
var file = url;
var hash = url.indexOf("#");
if (hash > 0) {
file = url.substring(0, hash);
}
var json = {};
var outfile = __dirname+"/"+ file;
if (outfile.indexOf("www.web3d.org") >= 0) {
outfile = www +"/"+file.substr(file.indexOf("www.web3d.org"));
}
try {
/*
if (fs.existsSync(outfile)) {
*/
var data = await fs.promises.readFile(outfile);
// console.error("Data", data.toString());
var json = JSON.parse(data.toString());
// console.error(JSON.stringify(json));
// console.error("Calling extern proto expander");
// json = PROTOS.externalPrototypeExpander(outfile, json);
// console.error(JSON.stringify(json));
send(res, json, "text/json", next);
/*
} else {
var infile = file.substr(0, file.lastIndexOf("."))+".x3d";
infile = www + "/" + infile;
convertX3dToJson(res, infile, outfile, next);
}
*/
} catch (e) {
console.error("Couldn't read JSON. Consider creating a JSON file", url);
next();
}
});
app.get("*.x3dj", async function(req, res, next) {
var url = req._parsedUrl.pathname.substr(1);
console.error(req.ip+": Requested JSON File", url);
var file = url;
var hash = url.indexOf("#");
if (hash > 0) {
file = url.substring(0, hash);
}
var json = {};
var outfile = __dirname+"/"+ file;
if (outfile.indexOf("www.web3d.org") >= 0) {
outfile = www +"/"+file.substr(file.indexOf("www.web3d.org"));
}
try {
/*
if (fs.existsSync(outfile)) {
*/
var data = await fs.promises.readFile(outfile);
// console.error("Data", data.toString());
var json = JSON.parse(data.toString());
// console.error(JSON.stringify(json));
// console.error("Calling extern proto expander");
// json = PROTOS.externalPrototypeExpander(outfile, json);
// console.error(JSON.stringify(json));
send(res, json, "text/json", next);
/*
} else {
var infile = file.substr(0, file.lastIndexOf("."))+".x3d";
infile = www + "/" + infile;
convertX3dToJson(res, infile, outfile, next);
}
*/
} catch (e) {
console.error("Couldn't read JSON. Consider creating a JSON file", url);
next();
}
});
https.createServer({
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
requestCert: false,
rejectUnauthorized: false
}, app)
.listen(port, '127.0.0.1', function () {
console.log('Example app listening on port', port, "! Go to https://localhost:"+port+"/");
});