-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (114 loc) · 4.13 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const express = require('express');
const http = require('http');
const hbs = require('hbs');
const { parse } = require('cookie');
const uuid = require('uuid').v4;
const dayjs = require('dayjs');
const app = express();
const server = http.createServer(app);
const { Server } = require('socket.io');
const io = new Server(server);
const db = require('./db/db');
const { colors } = require('./node_utils/constants');
const { getRoomIdFromURL } = require('./node_utils/get-room-id');
const { methods } = require('./node_utils/middlewares/methods');
process.title = 'Shared Canvas';
const port = process.env.port || 3000;
app.use(methods);
app.set('view engine', 'html');
app.engine('html', hbs.__express);
const indexHandler = async (req, res) => {
const url = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
const cookies = parse(req.headers.cookie || '');
const roomUuid = getRoomIdFromURL(url);
const userUuid = cookies?.userUuid || uuid();
if (!cookies?.userUuid) {
res.cookie('userUuid', userUuid, {
secure: true,
httpOnly: true,
expires: dayjs().add(30, 'days').toDate(),
});
}
await db('users').insert({ uuid: userUuid }).onConflict('uuid').ignore();
await db('drawing_rooms').insert({ uuid: roomUuid }).onConflict('uuid').ignore();
res.render('index', { colors });
};
app.set('views', `${__dirname}/dist`);
app.get('/', indexHandler);
app.get('/status', (req, res) => {
res.sendStatus(200);
});
app.use(express.static('dist'));
app.get('/new', async (req, res) => {
res.redirect(`/room/${uuid()}`);
});
app.get('/room/:roomId', indexHandler);
const getAllArt = async ({ userUuid, roomUuid }) => {
let results = await db('canvas_entries')
.select(['image', 'user_uuid'])
.where({ room_uuid: roomUuid });
let myResult = null;
if (userUuid) {
myResult = results.find((r) => r.user_uuid === userUuid);
results = results
.filter((r) => r.user_uuid !== userUuid)
.map((res) => ({
id: res.user_uuid,
image: res.image,
}));
}
return [results, myResult?.image];
};
io.on('connection', async (socket) => {
const ip = socket.conn.remoteAddress;
const roomUuid = getRoomIdFromURL(socket.handshake.headers.referer);
socket.join(roomUuid);
const cookies = parse(socket.handshake.headers.cookie);
const userUuid = cookies?.userUuid;
const dbQueryParams = { user_uuid: userUuid, room_uuid: roomUuid };
const dbQueryFields = ['user_uuid', 'room_uuid'];
const log = (...messages) => {
// eslint-disable-next-line no-console
console.info(`[IP: ${ip}]: `, dbQueryParams, ...messages);
};
log('A user connected.', roomUuid);
socket.emit('client-connection', io.engine.clientsCount);
socket.broadcast.emit('client-connection', io.engine.clientsCount);
// Upsert a new entry for current user, and get the uuid back
await db('canvas_entries')
.insert(dbQueryParams)
.onConflict(dbQueryFields)
.ignore();
// eslint-disable-next-line no-console
console.info(dbQueryParams);
const [allArt, myArt] = await getAllArt({ userUuid, roomUuid });
socket.emit('image', allArt, myArt, userUuid);
socket.on('clear-image', async (cb) => {
log('A user cleared image.');
await db('canvas_entries')
.where(dbQueryParams)
.update({ image: Buffer.from([]) });
socket.emit('image-clear', userUuid);
socket.to(roomUuid).emit('image-clear', userUuid);
cb({ status: 'ok' });
});
socket.on('image', async (points, cb) => {
let { image } = await db('canvas_entries')
.select('image')
.where(dbQueryParams)
.first();
if (!image) image = Buffer.from([]);
const pointsBuf = Buffer.concat([image, points]);
await db('canvas_entries').where(dbQueryParams).update({ image: pointsBuf });
socket.to(roomUuid).emit('image-update', userUuid, points);
cb({ status: 'ok' });
});
socket.on('disconnect', () => {
log('A user disconnected.');
socket.broadcast.emit('client-connection', io.engine.clientsCount);
});
});
server.listen(port, '0.0.0.0', () => {
// eslint-disable-next-line no-console
console.info(`[Shared Canvas] App listening at http://localhost:${port}`);
});