-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathchannel.js
186 lines (167 loc) Β· 5.08 KB
/
channel.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
'use strict';
const http = require('http');
const metautil = require('metautil');
const EMPTY_PACKET = Buffer.from('{}');
class Session {
constructor(token, channel, data) {
this.token = token;
this.channel = channel;
this.data = data;
this.context = new Proxy(data, {
get: (data, key) => {
if (key === 'token') return token;
if (key === 'client') return channel.client;
return Reflect.get(data, key);
},
set: (data, key, value) => {
const res = Reflect.set(data, key, value);
channel.application.auth.saveSession(token, data);
return res;
},
});
}
}
const sessions = new Map(); // token: Session
const channels = new Map(); // Client: Channel
class Client {
constructor() {
this.events = { close: [] };
this.callId = 0;
}
redirect(location) {
return channels.get(this).redirect(location);
}
get ip() {
return channels.get(this).ip;
}
on(name, callback) {
if (name !== 'close') return;
this.events.close.push(callback);
}
emit(name, data) {
const packet = { event: --this.callId, [name]: data };
const channel = channels.get(this);
if (!channel.connection) {
throw new Error(`Can't send metacom events to http transport`);
}
channel.send(packet);
}
startSession(token, data = {}) {
const channel = channels.get(this);
if (channel.session) sessions.delete(channel.session.token);
const session = new Session(token, channel, data);
channel.session = session;
sessions.set(token, session);
}
restoreSession(token) {
const session = sessions.get(token);
if (!session) return false;
const channel = channels.get(this);
channel.session = session;
return true;
}
}
class Channel {
constructor(application, req, res) {
this.application = application;
this.req = req;
this.res = res;
this.ip = req.socket.remoteAddress;
this.client = new Client();
channels.set(this.client, this);
this.session = null;
this.restoreSession();
}
get token() {
if (this.session === null) return '';
return this.session.token;
}
message(data) {
if (Buffer.compare(EMPTY_PACKET, data) === 0) {
this.send('{}');
return;
}
const packet = metautil.jsonParse(data);
if (!packet) {
const error = new Error('JSON parsing error');
this.error(400, { error, pass: true });
return;
}
const [callType, target] = Object.keys(packet);
const callId = parseInt(packet[callType], 10);
const args = packet[target];
if (callId && args) {
const [interfaceName, methodName] = target.split('/');
this.rpc(callId, interfaceName, methodName, args);
return;
}
const error = new Error('Packet structure error');
this.error(400, { callId, error, pass: true });
}
async rpc(callId, interfaceName, methodName, args) {
const { application, session, client } = this;
const [iname, ver = '*'] = interfaceName.split('.');
const proc = application.getMethod(iname, ver, methodName);
if (!proc) {
this.error(404, { callId });
return;
}
try {
await proc.enter();
} catch {
this.error(503, { callId });
return;
}
const context = session ? session.context : { client };
if (!this.session && proc.access !== 'public') {
this.error(403, { callId });
return;
}
let result = null;
try {
result = await proc.invoke(context, args);
} catch (error) {
const timedout = error.message === 'Timeout reached';
const code = timedout ? 408 : 500;
this.error(code, { callId, error, pass: timedout });
return;
} finally {
proc.leave();
}
if (typeof result === 'object' && result.constructor.name === 'Error') {
this.error(result.code, { callId, error: result, pass: true });
return;
}
this.send({ callback: callId, result });
const record = `${this.ip}\t${interfaceName}/${methodName}`;
application.console.log(record);
}
error(code, { callId, error = null, pass = false } = {}) {
const { req, ip, application } = this;
const { url, method } = req;
const status = http.STATUS_CODES[code];
const message = pass ? error.message : status || 'Unknown error';
const httpCode = status ? code : 500;
const reason = `${httpCode}\t${error ? error.stack : status}`;
application.console.error(`${ip}\t${method}\t${url}\t${reason}`);
const packet = { callback: callId, error: { message, code } };
this.send(packet, httpCode);
}
async restoreSession() {
const { cookie } = this.req.headers;
if (!cookie) return null;
const cookies = metautil.parseCookies(cookie);
const { token } = cookies;
if (!token) return null;
const session = await this.application.auth.restoreSession(token);
if (!session) return null;
return session;
}
destroy() {
channels.delete(this.client);
for (const callback of this.client.events.close) callback();
if (!this.session) return;
sessions.delete(this.session.token);
}
}
module.exports = { Channel, channels };