Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code duplication in rpc and hook #493

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 16 additions & 29 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,37 +247,15 @@ class Server {
}

async rpc(client, packet) {
const { id, method, args } = packet;
const { id, method } = packet;
const [unitName, methodName] = method.split('/');
const [unit, ver = '*'] = unitName.split('.');
const proc = this.application.getMethod(unit, ver, methodName);
if (!proc) return void client.error(404, { id });
const context = client.createContext();
if (!client.session && proc.access !== 'public') {
return void client.error(403, { id });
}
try {
await proc.enter();
} catch {
return void client.error(503, { id });
}
let result = null;
try {
result = await proc.invoke(context, args);
} catch (error) {
let code = error.code === 'ETIMEOUT' ? 408 : 500;
if (typeof error.code === 'number') code = error.code;
error.httpCode = code <= 599 ? code : 500;
return void client.error(code, { id, error });
} finally {
proc.leave();
}
if (metautil.isError(result)) {
const { code, httpCode = 200 } = result;
return void client.error(code, { id, error: result, httpCode });
}
client.send({ type: 'callback', id, result });
this.console.log(`${client.ip}\t${method}`);
this.handleRequest(client, packet, proc);
}

async stream(client, packet) {
Expand Down Expand Up @@ -337,7 +315,13 @@ class Server {
}

async hook(client, proc, packet, verb, headers) {
const { id, method, args } = packet;
const { method, args, id } = packet;
const par = Object.assign(args, { method, headers, verb });
this.handleRequest(client, { method, id, args: par }, proc);
}

async handleRequest(client, packet, proc) {
const { id, method, args, type } = packet;
if (!proc) return void client.error(404, { id });
const context = client.createContext();
try {
Expand All @@ -347,18 +331,21 @@ class Server {
}
let result = null;
try {
const par = { verb, method, args, headers };
result = await proc.invoke(context, par);
result = await proc.invoke(context, args);
} catch (error) {
client.error(500, { id, error });
let code = error.code === 'ETIMEOUT' ? 408 : 500;
if (typeof error.code === 'number') code = error.code;
error.httpCode = code <= 599 ? code : 500;
return void client.error(code, { id, error });
} finally {
proc.leave();
}
if (metautil.isError(result)) {
const { code, httpCode = 200 } = result;
return void client.error(code, { id, error: result, httpCode });
}
client.send(result);
if (type === 'call') client.send({ type: 'callback', id, result });
else client.send(result);
this.console.log(`${client.ip}\t${method}`);
}

Expand Down