-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (61 loc) · 2.3 KB
/
index.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
const bodyParser = require('body-parser');
const express = require("express");
const compression = require('compression')
const validate = require('express-jsonschema').validate;
const speedyjs = require("speedyjs-compiler");
const PORT = process.env.PORT || 3000;
const app = express();
app.use(bodyParser.json());
app.use(compression());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post("/compile", validate({ body: require("./schemas/compile-schema.json") }), function (req, res) {
const output = [];
for (let i = 0; i < req.body.files.length; ++i) {
const file = req.body.files[i];
const compiled = speedyjs.compileSourceCode(file.source, file.fileName, req.body.tsconfig);
output.push({
fileName: file.fileName,
map: compiled.sourceMapText,
js: compiled.js,
wasm: compiled.wasm,
wast: compiled.wast,
exitStatus: compiled.exitStatus,
diagnostics: compiled.diagnostics.map(diagnostic => speedyjs.formatDiagnostics([diagnostic]))
});
}
res.json(output);
});
app.get("/version", function (req, res) {
res.json({
"speedyjs-compiler": require("speedyjs-compiler/package.json").version,
"typescript": require("typescript/package.json").version,
"speedyjs-saas": require("./package.json").version
});
});
app.use(function(err, req, res, next) {
let responseData;
if (err.name === 'JsonSchemaValidation') {
// Log the error however you please
console.log(err.message);
// Set a bad request http response status or whatever you want
res.status(400);
// Format the response body however you want
responseData = {
statusText: 'Bad Request',
jsonSchemaValidation: true,
validations: err.validations // All of your validation information
};
res.json(responseData);
} else {
// pass error to next error middleware handler
next(err);
}
});
app.listen(PORT, function () {
console.log("SaaS server running on port " + PORT);
});