-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
53 lines (43 loc) · 1.31 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
const asc = require("assemblyscript/cli/asc");
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
express.static.mime.define({
'application/wasm': ['wasm']
});
app.use(express.static('./'));
app.post(`/makeWorkletBinary/:id`, function (req, res) {
const id = req.params.id;
const {
ts
} = req.body;
const tsSource = `export function processor(input_ptr: i32, output_ptr: i32, length: i32): void {
${ts}
}`;
fs.writeFileSync(`./files/${id}.ts`, tsSource, 'utf8');
asc.main([
`./files/${id}.ts`,
"--binaryFile", `./files/${id}.wasm`,
"--importMemory"
], function (err) {
if (err) {
res.status(500).send({
error: err
});
} else {
const b64 = fs.readFileSync(`./files/${id}.wasm`).toString('base64');
res.send(b64);
fs.unlinkSync(`./files/${id}.wasm`);
fs.unlinkSync(`./files/${id}.ts`);
}
});
});
app.listen(8092, (err) => {
if (err) console.error(err);
else console.info(`Started server on port [8092]`);
});