Skip to content

Commit af146b5

Browse files
committed
Optimize sessions and cookies
PR-URL: #105
1 parent 7d01ed8 commit af146b5

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

lib/auth.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ module.exports = application => {
7979
};
8080

8181
class Session {
82-
constructor(token, cookie, sandbox, contextData = { token }) {
82+
constructor(token, sandbox, contextData = { token }) {
8383
const contextHandler = {
8484
set: (data, key, value) => {
8585
const res = Reflect.set(data, key, value);
@@ -88,30 +88,31 @@ module.exports = application => {
8888
}
8989
};
9090
this.token = token;
91-
this.cookie = cookie;
9291
this.sandbox = sandbox;
9392
this.data = contextData;
9493
this.context = new Proxy(contextData, contextHandler);
9594
}
9695
}
9796

98-
const start = (req, ip, userId) => {
97+
const start = (client, userId) => {
9998
const token = generateToken();
100-
const host = parseHost(req.headers.host);
99+
const host = parseHost(client.req.headers.host);
100+
const ip = client.req.connection.remoteAddress;
101101
const cookie = `${TOKEN}=${token}; ${COOKIE_HOST}=${host}; HttpOnly`;
102102
const sandbox = getSandbox();
103-
const session = new Session(token, cookie, sandbox);
103+
const session = new Session(token, sandbox);
104104
sessions.set(token, session);
105-
cache.set(req, session);
105+
cache.set(client.req, session);
106106
const data = JSON.stringify(session.data);
107107
db.insert('Session', { userId, token, ip, data });
108+
if (client.res) client.res.setHeader('Set-Cookie', cookie);
108109
return session;
109110
};
110111

111-
const restore = async req => {
112-
const cachedSession = cache.get(req);
112+
const restore = async client => {
113+
const cachedSession = cache.get(client.req);
113114
if (cachedSession) return cachedSession;
114-
const { cookie } = req.headers;
115+
const { cookie } = client.req.headers;
115116
if (!cookie) return null;
116117
const cookies = parseCookies(cookie);
117118
const token = cookies.token;
@@ -122,18 +123,18 @@ module.exports = application => {
122123
if (record.data) {
123124
const data = JSON.parse(record.data);
124125
const sandbox = getSandbox();
125-
session = new Session(token, cookie, sandbox, data);
126+
session = new Session(token, sandbox, data);
126127
sessions.set(token, session);
127128
}
128129
}
129130
if (!session) return null;
130-
cache.set(req, session);
131+
cache.set(client.req, session);
131132
return session;
132133
};
133134

134-
const remove = (req, res, token) => {
135-
const host = parseHost(req.headers.host);
136-
res.setHeader('Set-Cookie', COOKIE_DELETE + host);
135+
const remove = (client, token) => {
136+
const host = parseHost(client.req.headers.host);
137+
client.res.setHeader('Set-Cookie', COOKIE_DELETE + host);
137138
sessions.delete(token);
138139
db.delete('Session', { token });
139140
};

lib/server.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ const closeClients = () => {
4646
};
4747

4848
class Client {
49-
constructor(req, res, application) {
49+
constructor(req, res, application, connection) {
5050
this.req = req;
5151
this.res = res;
5252
this.application = application;
53+
this.connection = connection;
5354
}
5455

5556
static() {
@@ -79,9 +80,8 @@ class Client {
7980
}
8081
const { req, res } = this;
8182
const { url } = req;
82-
const ip = req.connection.remoteAddress;
8383
const name = url.substring(METHOD_OFFSET);
84-
const session = await this.application.auth.restore(req);
84+
const session = await this.application.auth.restore(this);
8585
const args = await receiveArgs(req);
8686
const sandbox = session ? session.sandbox : undefined;
8787
const context = session ? session.context : {};
@@ -100,8 +100,7 @@ class Client {
100100
return;
101101
}
102102
if (!session && access === 'public') {
103-
const session = this.application.auth.start(req, ip, result.userId);
104-
res.setHeader('Set-Cookie', session.cookie);
103+
this.application.auth.start(this, result.userId);
105104
}
106105
res.end(JSON.stringify(result));
107106
} catch (err) {
@@ -141,7 +140,8 @@ const listener = application => (req, res) => {
141140
}
142141
};
143142

144-
const apiws = (application, connection, req) => async message => {
143+
const apiws = async (client, message) => {
144+
const { connection, application } = client;
145145
const { semaphore } = application.server;
146146
const send = obj => connection.send(JSON.stringify(obj));
147147
try {
@@ -151,9 +151,8 @@ const apiws = (application, connection, req) => async message => {
151151
return;
152152
}
153153
try {
154-
const ip = req.connection.remoteAddress;
155154
const { method: name, args } = JSON.parse(message);
156-
const session = await application.auth.restore(req);
155+
const session = await application.auth.restore(client);
157156
const sandbox = session ? session.sandbox : undefined;
158157
const context = session ? session.context : {};
159158
const exp = application.runScript(name, sandbox);
@@ -166,8 +165,8 @@ const apiws = (application, connection, req) => async message => {
166165
}
167166
const result = await method(args);
168167
if (!session && access === 'public') {
169-
const session = application.auth.start(req, ip, result.userId);
170-
result.token = session.cookie;
168+
const session = application.auth.start(client, result.userId);
169+
result.token = session.token;
171170
}
172171
send(result);
173172
} catch (err) {
@@ -191,7 +190,10 @@ class Server {
191190
if (transport.startsWith('ws')) {
192191
this.ws = new WebSocket.Server({ server: this.instance });
193192
this.ws.on('connection', (connection, req) => {
194-
connection.on('message', apiws(application, connection, req));
193+
const client = new Client(req, null, application, connection);
194+
connection.on('message', message => {
195+
apiws(client, message);
196+
});
195197
});
196198
}
197199
this.instance.listen(port, host);

0 commit comments

Comments
 (0)