From 072da7de675525644e55bd3db51ecc3e55c051b2 Mon Sep 17 00:00:00 2001 From: axbuglak Date: Tue, 20 Feb 2024 00:09:53 +0100 Subject: [PATCH 1/3] reuse of code --- lib/server.js | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/lib/server.js b/lib/server.js index 46841dda..3c4d3790 100644 --- a/lib/server.js +++ b/lib/server.js @@ -248,37 +248,10 @@ class Server { } async rpc(client, packet) { - const { id, method, args } = packet; - const [unitName, methodName] = method.split('/'); + const [unitName, methodName] = packet.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}`); + return void this.handleRequest(client, packet, proc); } async stream(client, packet) { @@ -333,12 +306,12 @@ class Server { const args = { ...parameters, ...body }; const packet = { id: 0, method: unit + '/' + method, args }; const hook = application.getHook(unit); - if (hook) this.hook(client, hook, packet, verb, headers); + if (hook) this.handleRequest(client, { ...packet, verb, headers }, hook); else this.rpc(client, packet); } - async hook(client, proc, packet, verb, headers) { - const { id, method, args } = packet; + async handleRequest(client, packet, proc) { + const { id, method, args, type } = packet; if (!proc) return void client.error(404, { id }); const context = client.createContext(); try { @@ -348,10 +321,13 @@ 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 }); + if (type !== 'call') return void client.error(503, { 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(); } @@ -359,7 +335,8 @@ class Server { 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}`); } From 204311259b96461672ec2a44a2f7272b43a37e79 Mon Sep 17 00:00:00 2001 From: axbuglak Date: Thu, 22 Feb 2024 00:21:42 +0100 Subject: [PATCH 2/3] universal error handling for hook and rpc methods --- lib/server.js | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/server.js b/lib/server.js index 3c4d3790..de94d731 100644 --- a/lib/server.js +++ b/lib/server.js @@ -248,10 +248,37 @@ class Server { } async rpc(client, packet) { - const [unitName, methodName] = packet.method.split('/'); + const { id, method, args } = packet; + const [unitName, methodName] = method.split('/'); const [unit, ver = '*'] = unitName.split('.'); const proc = this.application.getMethod(unit, ver, methodName); - return void this.handleRequest(client, packet, proc); + 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}`); } async stream(client, packet) { @@ -306,12 +333,12 @@ class Server { const args = { ...parameters, ...body }; const packet = { id: 0, method: unit + '/' + method, args }; const hook = application.getHook(unit); - if (hook) this.handleRequest(client, { ...packet, verb, headers }, hook); + if (hook) this.hook(client, hook, packet, verb, headers); else this.rpc(client, packet); } - async handleRequest(client, packet, proc) { - const { id, method, args, type } = packet; + async hook(client, proc, packet, verb, headers) { + const { id, method, args } = packet; if (!proc) return void client.error(404, { id }); const context = client.createContext(); try { @@ -321,9 +348,9 @@ class Server { } let result = null; try { - result = await proc.invoke(context, args); + const par = { verb, method, args, headers }; + result = await proc.invoke(context, par); } catch (error) { - if (type !== 'call') return void client.error(503, { id, error }); let code = error.code === 'ETIMEOUT' ? 408 : 500; if (typeof error.code === 'number') code = error.code; error.httpCode = code <= 599 ? code : 500; @@ -335,8 +362,7 @@ class Server { const { code, httpCode = 200 } = result; return void client.error(code, { id, error: result, httpCode }); } - if (type === 'call') client.send({ type: 'callback', id, result }); - else client.send(result); + client.send(result); this.console.log(`${client.ip}\t${method}`); } @@ -367,3 +393,4 @@ class Server { } module.exports = { Server }; +odule.exports = { Server }; From 0cef592a1442f43a7ee99c21fc1310fd38b416d8 Mon Sep 17 00:00:00 2001 From: axbuglak Date: Thu, 22 Feb 2024 19:19:57 +0100 Subject: [PATCH 3/3] reuse code using an additional method --- lib/server.js | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/lib/server.js b/lib/server.js index de94d731..7896f20b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -248,37 +248,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) { @@ -338,7 +316,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 { @@ -348,8 +332,7 @@ 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) { let code = error.code === 'ETIMEOUT' ? 408 : 500; if (typeof error.code === 'number') code = error.code; @@ -362,7 +345,8 @@ class Server { 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}`); } @@ -393,4 +377,3 @@ class Server { } module.exports = { Server }; -odule.exports = { Server };