Skip to content

Commit 6938f73

Browse files
committed
Decompose Channel.prototype.rpc
PR-URL: #146
1 parent 057e073 commit 6938f73

2 files changed

Lines changed: 41 additions & 36 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased][unreleased]
44

55
- Move Semaphore and timeout to metautil
6+
- Decompose Channel.prototype.rpc
67

78
## [1.4.0][] - 2021-02-17
89

lib/channel.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -184,51 +184,55 @@ class Channel {
184184
}
185185

186186
async rpc(callId, interfaceName, methodName, args) {
187-
const { res, connection, application, session, client } = this;
188-
const { semaphore } = application.server;
187+
const { application, session, client } = this;
188+
const [iname, ver = '*'] = interfaceName.split('.');
189+
const proc = application.getMethod(iname, ver, methodName);
190+
if (!proc) {
191+
this.error(404, null, callId);
192+
return;
193+
}
189194
try {
190-
await semaphore.enter();
195+
await proc.enter();
191196
} catch {
192197
this.error(504, null, callId);
193198
return;
194199
}
195-
const [iname, ver = '*'] = interfaceName.split('.');
200+
const context = session ? session.context : { client };
201+
const publicMethod = proc.access === 'public';
202+
if (!this.session && !publicMethod) {
203+
this.error(403, null, callId);
204+
return;
205+
}
206+
let result = null;
196207
try {
197-
const context = session ? session.context : { client };
198-
const proc = application.getMethod(iname, ver, methodName);
199-
if (!proc) {
200-
this.error(404, null, callId);
201-
return;
202-
}
203-
if (!this.session && proc.access !== 'public') {
204-
this.error(403, null, callId);
205-
return;
206-
}
207-
const result = await application.invoke(context, proc, args);
208-
if (result instanceof Error) {
209-
this.error(result.code, result, callId);
210-
return;
211-
}
212-
const id = result ? result.systemUserId : 0;
213-
if (!this.session && id && proc.access === 'public') {
214-
this.session = application.auth.startSession(this, id);
215-
result.token = this.token;
216-
}
217-
const data = JSON.stringify({ callback: callId, result });
218-
if (connection) {
219-
connection.send(data);
220-
} else {
221-
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS });
222-
res.end(data);
223-
}
224-
const { ip, token } = this;
225-
const who = id > 0 ? id : token;
226-
const record = `${ip}\t${who}\t${interfaceName}/${methodName}`;
227-
application.console.log(record);
208+
result = await proc.invoke(context, args);
228209
} catch (err) {
229210
this.error(500, err, callId);
230211
} finally {
231-
semaphore.leave();
212+
proc.leave();
213+
}
214+
this.reply(callId, result, publicMethod);
215+
const record = `${this.ip}\t${interfaceName}/${methodName}`;
216+
application.console.log(record);
217+
}
218+
219+
reply(callId, result, publicMethod) {
220+
const { res, connection, application } = this;
221+
if (result instanceof Error) {
222+
this.error(result.code, result, callId);
223+
return;
224+
}
225+
const id = result ? result.systemUserId : 0;
226+
if (!this.session && id && publicMethod) {
227+
this.session = application.auth.startSession(this, id);
228+
result.token = this.token;
229+
}
230+
const data = JSON.stringify({ callback: callId, result });
231+
if (connection) {
232+
connection.send(data);
233+
} else {
234+
res.writeHead(200, { 'Content-Type': MIME_TYPES.json, ...HEADERS });
235+
res.end(data);
232236
}
233237
}
234238

0 commit comments

Comments
 (0)