Skip to content

Commit f78db1f

Browse files
committed
new server not crashed if send bad token
1 parent 7f52ee6 commit f78db1f

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

JavaScript/client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class Client {
2727

2828
static async getInstance(req, res) {
2929
const client = new Client(req, res);
30-
await Session.restore(client);
30+
try {
31+
await Session.restore(client);
32+
} catch {
33+
throw new Error("Couldn't restore session");
34+
}
3135
return client;
3236
}
3337

JavaScript/server.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const http = require('node:http');
55
const Client = require('./client.js');
66
const Session = require('./session.js');
77

8+
const INVALID_TOKEN_CODE = 498;
9+
810
const routing = {
911
'/': async () => '<h1>welcome to homepage</h1><hr>',
1012
'/start': async (client) => {
@@ -46,7 +48,13 @@ const types = {
4648
};
4749

4850
http.createServer(async (req, res) => {
49-
const client = await Client.getInstance(req, res);
51+
try {
52+
const client = await Client.getInstance(req, res);
53+
} catch {
54+
res.statusCode = INVALID_TOKEN_CODE;
55+
res.end();
56+
return;
57+
}
5058
const { method, url, headers } = req;
5159
console.log(`${method} ${url} ${headers.cookie}`);
5260
const handler = routing[url];

JavaScript/session.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class Session extends Map {
4343
if (sessionToken) {
4444
return new Promise((resolve, reject) => {
4545
storage.get(sessionToken, (err, session) => {
46-
if (err) reject(new Error('No session'));
46+
if (err) {
47+
reject(new Error('No session'));
48+
return;
49+
}
4750
Object.setPrototypeOf(session, Session.prototype);
4851
client.token = sessionToken;
4952
client.session = session;

0 commit comments

Comments
 (0)