-
Notifications
You must be signed in to change notification settings - Fork 66
/
server.js
35 lines (32 loc) · 1004 Bytes
/
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
import express from "express";
import {
createServer
} from "http";
import fetch from "node-fetch";
function connect(conn, PORT, opts) {
const app = express(),
server = createServer(app);
let _qr = "invalid";
conn.ev.on("connection.update", ({
qr
}) => qr && (_qr = qr)), app.use(async (req, res) => {
res.setHeader("content-type", "image/png"), res.end(await toBuffer(_qr));
}), server.listen(PORT, () => {
console.log("App listened on port", PORT), opts.keepalive && keepAlive();
});
}
function pipeEmit(event, event2, prefix = "") {
const oldEmit = event.emit;
return event.emit = (event, ...args) => {
oldEmit(event, ...args), event2.emit(prefix + event, ...args);
}, {
unpipeEmit() {
event.emit = oldEmit;
}
};
}
function keepAlive() {
const url = `https://${process.env.REPL_SLUG}.${process.env.REPL_OWNER}.repl.co`;
/(\/\/|\.)undefined\./.test(url) || setInterval(() => fetch(url).catch(console.error), 3e5);
}
export default connect;