-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (56 loc) · 1.3 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
57
58
59
60
61
62
63
64
65
import express from 'express'
import path from 'path'
import {
Socket
} from 'socket.io'
import {
toBuffer
} from 'qrcode'
import fetch from 'node-fetch'
import fs from 'fs';
import wakeDyno from 'woke-dyno';
function connect(conn, PORT) {
let app = global.app = express()
// app.use(express.static(path.join(__dirname, 'views')))
let _qr = 'invalid'
conn.ev.on('connection.update', function appQR({
qr
}) {
if (qr) _qr = qr
})
app.use(async (req, res) => {
res.setHeader('content-type', 'image/png')
res.end(await toBuffer(_qr))
})
let server = app.listen(PORT, () => {
console.log('App listened on port', PORT)
if (opts['keepalive']) keepAlive()
})
let io = Socket(server)
io.on('connection', socket => {
let {
unpipeEmit
} = pipeEmit(conn, socket, 'conn-')
socket.on('disconnect', unpipeEmit)
})
}
function pipeEmit(event, event2, prefix = '') {
let old = event.emit
event.emit = function(event, ...args) {
old.emit(event, ...args)
event2.emit(prefix + event, ...args)
}
return {
unpipeEmit() {
event.emit = old
}
}
}
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 * 1000 * 60)
}
export default connect