-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
server.js
56 lines (48 loc) · 1.57 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
import express from 'express';
import { createServer } from 'http';
import path from 'path';
import { Server } from 'socket.io';
import { toBuffer } from 'qrcode';
import fetch from 'node-fetch';
function connect(conn, PORT, opts = {}) {
const app = express();
const server = createServer(app);
let _qr = 'invalid';
conn.ev.on('connection.update', ({ qr }) => {
if (qr) _qr = qr;
});
app.get('/', async (req, res) => {
res.setHeader('Content-Type', 'image/png');
res.end(await toBuffer(_qr));
});
// Uncomment the following block to enable socket.io functionality
// const io = new Server(server);
// io.on('connection', socket => {
// const { unpipeEmit } = pipeEmit(conn, socket, 'conn-');
// socket.on('disconnect', unpipeEmit);
// });
server.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
if (opts.keepalive) keepAlive();
});
}
function pipeEmit(event, event2, prefix = '') {
const originalEmit = event.emit;
event.emit = function (eventName, ...args) {
originalEmit.call(event, eventName, ...args);
event2.emit(`${prefix}${eventName}`, ...args);
};
return {
unpipeEmit() {
event.emit = originalEmit;
}
};
}
function keepAlive() {
const url = `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`;
if (/(\/\/|\.)undefined\./.test(url)) return;
setInterval(() => {
fetch(url).catch(console.error);
}, 5 * 60 * 1000); // 5 minutes interval
}
export default connect;