Ayush-003 commited on
Commit
c2948a7
·
verified ·
1 Parent(s): c61c049

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +18 -0
  2. package-lock.json +80 -0
  3. package.json +9 -0
  4. ws-server.js +87 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM node:20-bullseye
2
+
3
+
4
+ # Install TeX Live minimal packages + latexmk
5
+ RUN apt-get update && DEBIAN_FRONTEND=noninteractive \
6
+ && apt-get install -y --no-install-recommends \
7
+ perl git curl build-essential \
8
+ texlive-latex-recommended texlive-latex-extra texlive-fonts-recommended \
9
+ latexmk poppler-utils ghostscript \
10
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
11
+
12
+
13
+ WORKDIR /app
14
+ COPY package.json package-lock.json* ./
15
+ RUN npm ci --only=prod
16
+ COPY . .
17
+ EXPOSE ${PORT}
18
+ CMD ["node", "ws-server.js"]
package-lock.json ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "latex-compile-server",
3
+ "version": "1.0.0",
4
+ "lockfileVersion": 3,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "latex-compile-server",
9
+ "version": "1.0.0",
10
+ "license": "MIT",
11
+ "dependencies": {
12
+ "ws": "8.13.0"
13
+ }
14
+ },
15
+ "node_modules/bufferutil": {
16
+ "version": "4.0.9",
17
+ "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.9.tgz",
18
+ "integrity": "sha512-WDtdLmJvAuNNPzByAYpRo2rF1Mmradw6gvWsQKf63476DDXmomT9zUiGypLcG4ibIM67vhAj8jJRdbmEws2Aqw==",
19
+ "hasInstallScript": true,
20
+ "license": "MIT",
21
+ "optional": true,
22
+ "peer": true,
23
+ "dependencies": {
24
+ "node-gyp-build": "^4.3.0"
25
+ },
26
+ "engines": {
27
+ "node": ">=6.14.2"
28
+ }
29
+ },
30
+ "node_modules/node-gyp-build": {
31
+ "version": "4.8.4",
32
+ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
33
+ "integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==",
34
+ "license": "MIT",
35
+ "optional": true,
36
+ "peer": true,
37
+ "bin": {
38
+ "node-gyp-build": "bin.js",
39
+ "node-gyp-build-optional": "optional.js",
40
+ "node-gyp-build-test": "build-test.js"
41
+ }
42
+ },
43
+ "node_modules/utf-8-validate": {
44
+ "version": "6.0.5",
45
+ "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-6.0.5.tgz",
46
+ "integrity": "sha512-EYZR+OpIXp9Y1eG1iueg8KRsY8TuT8VNgnanZ0uA3STqhHQTLwbl+WX76/9X5OY12yQubymBpaBSmMPkSTQcKA==",
47
+ "hasInstallScript": true,
48
+ "license": "MIT",
49
+ "optional": true,
50
+ "peer": true,
51
+ "dependencies": {
52
+ "node-gyp-build": "^4.3.0"
53
+ },
54
+ "engines": {
55
+ "node": ">=6.14.2"
56
+ }
57
+ },
58
+ "node_modules/ws": {
59
+ "version": "8.13.0",
60
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz",
61
+ "integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==",
62
+ "license": "MIT",
63
+ "engines": {
64
+ "node": ">=10.0.0"
65
+ },
66
+ "peerDependencies": {
67
+ "bufferutil": "^4.0.1",
68
+ "utf-8-validate": ">=5.0.2"
69
+ },
70
+ "peerDependenciesMeta": {
71
+ "bufferutil": {
72
+ "optional": true
73
+ },
74
+ "utf-8-validate": {
75
+ "optional": true
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
package.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "latex-compile-server",
3
+ "version": "1.0.0",
4
+ "main": "server.js",
5
+ "license": "MIT",
6
+ "dependencies": {
7
+ "ws": "8.13.0"
8
+ }
9
+ }
ws-server.js ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // ws-server.js
2
+ const http = require('http');
3
+ const WebSocket = require('ws');
4
+ const { spawn } = require('child_process');
5
+ const fs = require('fs/promises');
6
+ const os = require('os');
7
+ const path = require('path');
8
+
9
+ const server = http.createServer();
10
+ const wss = new WebSocket.Server({ server });
11
+
12
+ wss.on('connection', (ws) => {
13
+ let currentJob = null;
14
+
15
+ ws.on('message', async (raw) => {
16
+ try {
17
+ const msg = JSON.parse(raw.toString());
18
+ if (msg.type === 'edit' && typeof msg.tex === 'string') {
19
+ if (currentJob && currentJob.child) {
20
+ try { currentJob.child.kill('SIGKILL'); } catch (e) { /* ignore */ }
21
+ }
22
+
23
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'tex-'));
24
+ await fs.writeFile(path.join(tmp, 'main.tex'), msg.tex, 'utf8');
25
+
26
+ const child = spawn('latexmk', ['-pdf', '-interaction=nonstopmode', '-halt-on-error', 'main.tex'], { cwd: tmp });
27
+ currentJob = { child, tmp };
28
+
29
+ child.stdout.on('data', d => {
30
+ try { ws.send(JSON.stringify({ type: 'progress', text: d.toString() })); } catch (e) { /* ignore send errors */ }
31
+ });
32
+ child.stderr.on('data', d => {
33
+ try { ws.send(JSON.stringify({ type: 'progress', text: d.toString() })); } catch (e) { /* ignore send errors */ }
34
+ });
35
+
36
+ const timeout = setTimeout(() => {
37
+ try { child.kill('SIGKILL'); } catch (e) {}
38
+ try { ws.send(JSON.stringify({ type: 'error', message: 'compile timeout' })); } catch (e) {}
39
+ }, 30_000);
40
+
41
+ child.on('close', async (code) => {
42
+ clearTimeout(timeout);
43
+ if (code !== 0) {
44
+ try { ws.send(JSON.stringify({ type: 'error', message: 'compile failed (exit ' + code + ')' })); } catch (e) {}
45
+ await fs.rm(tmp, { recursive: true, force: true });
46
+ currentJob = null;
47
+ return;
48
+ }
49
+ try {
50
+ const pdfPath = path.join(tmp, 'main.pdf');
51
+ const pdf = await fs.readFile(pdfPath);
52
+ try {
53
+ ws.send(JSON.stringify({ type: 'pdf', base64: pdf.toString('base64') }));
54
+ } catch (e) {
55
+
56
+ try { ws.send(JSON.stringify({ type: 'error', message: 'failed to send pdf over websocket' })); } catch (e) {}
57
+ }
58
+ } catch (e) {
59
+ try { ws.send(JSON.stringify({ type: 'error', message: 'could not read output PDF: ' + String(e) })); } catch (err) {}
60
+ } finally {
61
+ try { await fs.rm(tmp, { recursive: true, force: true }); } catch (e) { /* ignore cleanup errors */ }
62
+ currentJob = null;
63
+ }
64
+ });
65
+ }
66
+ } catch (e) {
67
+ try { ws.send(JSON.stringify({ type: 'error', message: String(e) })); } catch (err) {}
68
+ }
69
+ });
70
+
71
+ ws.on('close', () => {
72
+ if (currentJob && currentJob.child) {
73
+ try { currentJob.child.kill('SIGKILL'); } catch (e) {}
74
+ }
75
+ });
76
+
77
+ ws.on('error', (err) => {
78
+
79
+ console.error('WebSocket error:', err);
80
+ if (currentJob && currentJob.child) {
81
+ try { currentJob.child.kill('SIGKILL'); } catch (e) {}
82
+ }
83
+ });
84
+ });
85
+
86
+ const PORT = process.env.PORT || 58404;
87
+ server.listen(PORT, '0.0.0.0', () => console.log(`ws compile server listening on ${PORT}`));