-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
227 lines (205 loc) · 6.45 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import fs from 'fs'
import YAML from 'yaml';
import http from 'http';
import polka from 'polka';
import WebSocket from 'ws';
import nunjucks from 'nunjucks';
import spotifyWebApi from './spotify-web-api';
import cors from 'cors';
import compress from 'compression';
import storage from 'node-persist';
const templating = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
const render = (res) => (err, out) => {
if (err) {
console.error(err);
res.writeHead(500);
return res.end();
}
res.end(out);
};
const log = (...messages) => console.log(new Date().toISOString(), ...messages);
const { PORT = 3000 } = process.env;
let config;
const configs = [ 'client_id', 'client_secret', 'scope', 'redirect_uri' ];
try {
const file = fs.readFileSync('./config.yaml', 'utf8');
config = YAML.parse(file);
} catch (err) {
config = {};
}
for (const name of configs) {
if (name.toUpperCase() in process.env) {
config[name] = process.env[name.toUpperCase()];
if (name === 'scope') {
config[name] = YAML.parse(`[${String(config[name]).replace(/^\[|\]$/g, '')}]`);
}
} else if (!(name in config)) {
throw new Error(`Config option "${name}" or environment variable "${name.toUpperCase()}" not defined specified.`);
}
}
for (const name of [ 'persist' ]) {
if (name.toUpperCase() in process.env) {
config[name] = process.env[name.toUpperCase()];
}
}
let { persist, ...rest } = config;
config = rest;
const spotify = spotifyWebApi(config);
const sessions = {};
const deleteSession = (id) => {
persist && storage.removeItem(id);
delete sessions[id];
};
const app = polka();
const server = http.createServer(app.handler);
const wss = new WebSocket.Server({ server });
let clients = [];
const send = (message, client = null) => {
message = JSON.stringify(message);
if (client && client.readyState === WebSocket.OPEN) {
client.send(message);
} else {
clients.filter(client => client.readyState === WebSocket.OPEN).forEach(client => client.send(message));
}
};
const updateClients = () => {
clients = [ ...wss.clients ].filter(client => client.readyState === WebSocket.OPEN);
};
let delay = 1000;
let tries = 0;
const interval = setInterval(() => {
if (!clients.length) {
return;
}
Object.entries(sessions).forEach(async ([ id, session ]) => {
delay = 1000;
try {
let { body, ...rest } = await spotify.api(session).player.player();
tries > 0 && log({ 'ok': true });
tries = 0;
send({ id, ...body });
} catch (err) {
if (err.code === 429) {
console.log(err.message);
delay = err.value * 1000;
} else {
if (tries > 5) {
console.error(new Date().toISOString(), { id }, err);
delay = 60000;
return;
}
log({ id }, err);
delay = 2000;
tries += 1;
}
}
});
}, delay);
wss.on('connection', async ws => {
updateClients();
log({ event: 'client_connected' });
ws.on('message', data => {
data = JSON.parse(data);
const { id, name, query, body, request_id } = data;
if (id && id in sessions) {
const parts = name.split('.');
if (parts[0] === 'player') {
spotify.api(sessions[id]).player[parts[1]]({ query, body })
.then(({ res, body, ...rest }) => {
send({ id, request_id, request: parts[1], ...body, ...rest }, ws);
})
.catch(err => console.error(new Date().toISOString(), { event: `player.${parts[1]}` }, err));
}
}
log({ event: 'message' }, data);
});
});
(async function () {
let states = [];
const login = (req, res) => {
log({ http: '/login' });
let { header, state } = spotify.init();
states.push(state);
res.writeHead(303, header);
send({ event: 'init_authentication' });
res.end();
};
const refresh = (id) => {
log({ id, event: 'refresh_trigger', in: sessions[id].expires_in });
sessions[id].timeout = setTimeout(() => {
spotify.auth.refresh(sessions[id].refresh_token).then(async ({ access_token, expires_in }) => {
send({ id, event: 'token_refresh'});
sessions[id] = {
...sessions[id],
access_token,
expires_in,
timestamp: new Date().toISOString()
};
persist && await storage.setItem(id, sessions[id]);
refresh(id);
}).catch(err => {
deleteSession(id);
console.error(new Date().toISOString(), err);
});
}, (sessions[id].expires_in - 360) * 1000);
}
if (persist) {
await storage.init({ dir: persist });
storage.forEach(async function({ key, value }) {
log({ id: key, event: 'persistence loaded' });
sessions[key] = {
...value,
expires_in: 0
};
refresh(key);
});
}
const redirect = ({ query: { code, state } = {}, ...req }, res) => {
log({ http: '/redirect' });
if (! states.includes(state)) {
console.error(new Date().toISOString(), 'Auth state not matched.');
res.writeHead(403);
return res.end('Forbidden');
}
states = states.filter(item => item !== state);
spotify.auth.token(code)
.then(auth => {
spotify.api(auth).user()
.then(async ({ body: { display_name, uri: id } }) => {
sessions[id] = { ...auth, timestamp: new Date().toISOString() };
await storage.setItem(id, sessions[id]);
send({ id, event: 'authenticated' });
refresh(id);
res.writeHead(303, { Location: `/?b=${Date.now()}` });
res.end();
}).catch(err => console.error(new Date().toISOString(), { event: 'user_profile' }, err));
}).catch(err => {
console.error(new Date().toISOString(), { event: 'auth_token' }, err);
res.writeHead(403);
return res.end(err.message);
});
};
app
.use(cors({ origin: true }))
.get('/', async (req, res) => {
log({ http: '/' });
const promises = [];
const results = await Promise.all(
Object.keys(sessions).map(id => {
return { id, promise: spotify.api(sessions[id]).user() };
}).map(({ id, promise }) => promise.catch(e => {
console.error(e);
deleteSession(id);
return e;
}))
);
let values = results.filter(result => !(result instanceof Error));
values = values.map(({ body }) => body);
templating.render('index.html', { users: values }, render(res));
})
.get('/login', login)
.get('/redirect', redirect);
})();
server.listen(PORT, err => {
log({ event: 'listening 1', port: PORT });
});