From d3e164b6576e01775282b8002bbd1c34afa9e9b7 Mon Sep 17 00:00:00 2001 From: Richard Rodger Date: Sun, 5 Nov 2023 22:10:24 +0000 Subject: [PATCH] v0.8.0 --- dist/gateway.d.ts | 20 ++++---- dist/gateway.js | 81 ++++++++++++++++++++++++++------ dist/gateway.js.map | 2 +- package.json | 14 +++--- src/gateway.ts | 112 ++++++++++++++++++++++++++++++++++++-------- 5 files changed, 175 insertions(+), 54 deletions(-) diff --git a/dist/gateway.d.ts b/dist/gateway.d.ts index 976bd70..9c4598f 100644 --- a/dist/gateway.d.ts +++ b/dist/gateway.d.ts @@ -16,11 +16,18 @@ type GatewayOptions = { allow: any; custom: any; fixed: any; + timeout: { + client: boolean; + max: number; + }; error: { message: boolean; details: boolean; }; - debug: boolean; + debug: { + response: boolean; + log: boolean; + }; }; declare function gateway(this: any, options: GatewayOptions): { exports: { @@ -29,16 +36,7 @@ declare function gateway(this: any, options: GatewayOptions): { }; }; declare namespace gateway { - var defaults: { - allow: any; - custom: any; - fixed: any; - error: { - message: boolean; - details: boolean; - }; - debug: boolean; - }; + var defaults: GatewayOptions; } export type { GatewayOptions, GatewayResult, }; export default gateway; diff --git a/dist/gateway.js b/dist/gateway.js index a3914f1..c7b5e53 100644 --- a/dist/gateway.js +++ b/dist/gateway.js @@ -1,9 +1,11 @@ "use strict"; -/* Copyright © 2021-2022 Richard Rodger, MIT License. */ +/* Copyright © 2021-2023 Richard Rodger, MIT License. */ +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", { value: true }); -const Seneca = require('seneca'); -// import { Open, Skip } from 'gubu' -const { Open, Skip } = Seneca.valid; +const seneca_1 = __importDefault(require("seneca")); +const { Open, Skip } = seneca_1.default.valid; function gateway(options) { let seneca = this; const root = seneca.root; @@ -12,7 +14,6 @@ function gateway(options) { const Jsonic = seneca.util.Jsonic; const allowed = new Patrun({ gex: true }); const checkAllowed = null != options.allow; - // console.log('CA', checkAllowed, options) if (checkAllowed) { for (let patStr in options.allow) { let pat = Jsonic(patStr); @@ -59,9 +60,21 @@ function gateway(options) { }); // Handle inbound JSON, converting it into a message, and submitting to Seneca. async function handler(json, ctx) { + if (options.debug.log) { + root.log.debug('gateway-handler-json', { json }); + } const seneca = await prepare(json, ctx); const rawmsg = tu.internalize_msg(seneca, json); const msg = seneca.util.clean(rawmsg); + // Clients can set a custom timeout, up to a maximum. + if (options.timeout.client && null != rawmsg.timeout$) { + let clientTimeout = +rawmsg.timeout$; + let maxTimeout = options.timeout.max; + maxTimeout = 0 < maxTimeout ? maxTimeout : seneca.options().timeout; + if (clientTimeout <= maxTimeout) { + msg.timeout$ = clientTimeout; + } + } return await new Promise(async (resolve) => { if (checkAllowed) { let allowMsg = false; @@ -74,17 +87,30 @@ function gateway(options) { // will still work, which is not what we want! allowMsg = !!allowed.find(msgdef.msgcanon); } + else { + seneca.log.debug('msg-not-found', { msg }); + } if (!allowMsg) { + let errdesc = { + name: 'Error', + code: 'not-allowed', + message: 'Message not allowed', + details: undefined, + pattern: undefined, + allowed: undefined, + }; + if (options.debug.response) { + errdesc.pattern = msgdef ? msgdef.pattern : undefined; + errdesc.allowed = msgdef ? allowMsg : undefined; + } + if (options.debug.log) { + seneca.log.debug('handler-not-allowed', { allowMsg, errdesc, msgdef, msg }); + } return resolve({ error: true, out: { meta$: { id: rawmsg.id$ }, - error$: nundef({ - name: 'Error', - code: 'not-allowed', - message: 'Message not allowed', - details: undefined, - }) + error$: nundef(errdesc) } }); } @@ -93,19 +119,25 @@ function gateway(options) { for (var i = 0; i < hooks.action.length; i++) { out = await hooks.action[i].call(seneca, msg, ctx); if (out) { + if (options.debug.log) { + seneca.log.debug('handler-hook-action', { out, msg }); + } return resolve(out); } } + if (options.debug.log) { + seneca.log.debug('handler-act', { msg }); + } seneca.act(msg, async function (err, out, meta) { for (var i = 0; i < hooks.result.length; i++) { await hooks.result[i].call(seneca, out, msg, err, meta, ctx); } - if (err && !options.debug) { + if (err && !options.debug.response) { err.stack = null; } out = tu.externalize_reply(this, err, out, meta); // Don't expose internal activity unless debugging - if (!options.debug) { + if (!options.debug.response) { if (out.meta$) { out.meta$ = { id: out.meta$.id @@ -158,6 +190,9 @@ function gateway(options) { await hookaction(fixed, json, ctx); } } + if (options.debug.log) { + root.log.debug('gateway-delegate-params', { fixed, custom }); + } // NOTE: a new delegate is created for each request to ensure isolation. const delegate = root.delegate(fixed, { custom: custom }); for (i = 0; i < hooks.delegate.length; i++) { @@ -197,20 +232,36 @@ function nundef(o) { } // Default options. gateway.defaults = { + // Keys are pattern strings. allow: Skip(Open({})), + // Add custom meta data values. custom: Open({ // Assume gateway is used to handle external messages. safe: false }), + // Set request delegate fixed values. fixed: Open({}), + // Allow clients to set a custom timeout (using the timeout$ directive). + timeout: { + // Clients can set a custom timeout. + client: false, + // Maximum value of client-set timeout. + // Default is same as Seneca delegate. + max: -1 + }, error: { // Include exception object message property in response. message: false, // Include exception object details property in response. details: false, }, - // When true, errors will include stack trace. - debug: false + // Control debug output. + debug: { + // When true, errors will include stack trace and other meta data. + response: false, + // Produce detailed debug logging. + log: false, + } }; exports.default = gateway; if ('undefined' !== typeof (module)) { diff --git a/dist/gateway.js.map b/dist/gateway.js.map index e58ddde..13f3a2d 100644 --- a/dist/gateway.js.map +++ b/dist/gateway.js.map @@ -1 +1 @@ -{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":";AAAA,wDAAwD;;AAExD,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;AAChC,oCAAoC;AAEpC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC,KAAK,CAAA;AA4CnC,SAAS,OAAO,CAAY,OAAuB;IACjD,IAAI,MAAM,GAAQ,IAAI,CAAA;IACtB,MAAM,IAAI,GAAQ,MAAM,CAAC,IAAI,CAAA;IAC7B,MAAM,EAAE,GAAQ,MAAM,CAAC,MA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AE;IACnC,MAAM,CAAC,OAAO,GAAG,OAAO,CAAA;CACzB"} \ No newline at end of file diff --git a/package.json b/package.json index 910ff6a..b6aacc6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@seneca/gateway", - "version": "0.7.0", + "version": "0.8.0", "main": "dist/gateway.js", "type": "commonjs", "types": "dist/gateway.d.ts", @@ -37,14 +37,14 @@ "node": ">=14" }, "devDependencies": { - "@seneca/maintain": "^0.0.30", - "@types/jest": "^29.5.2", - "@types/node": "^20.3.2", - "esbuild": "^0.18.10", + "@seneca/maintain": "^0.1.0", + "@types/jest": "^29.5.7", + "@types/node": "^20.8.10", + "esbuild": "^0.19.5", "esbuild-jest": "^0.5.0", - "jest": "^29.5.0", + "jest": "^29.7.0", "seneca-msg-test": "^3.4.0", - "typescript": "^5.1.6" + "typescript": "^5.2.2" }, "peerDependencies": { "seneca": ">=3", diff --git a/src/gateway.ts b/src/gateway.ts index c845d6e..f191248 100644 --- a/src/gateway.ts +++ b/src/gateway.ts @@ -1,12 +1,14 @@ -/* Copyright © 2021-2022 Richard Rodger, MIT License. */ +/* Copyright © 2021-2023 Richard Rodger, MIT License. */ -const Seneca = require('seneca') -// import { Open, Skip } from 'gubu' + +import Seneca from 'seneca' const { Open, Skip } = Seneca.valid +// Seneca action result. type GatewayResult = { + // The action result (transport externalized). // OR: a description of the error. out: any | { @@ -35,15 +37,23 @@ type GatewayResult = { } +// See defaults below for behaviour. type GatewayOptions = { - allow: any, + allow: any custom: any - fixed: any, + fixed: any + timeout: { + client: boolean + max: number + } error: { message: boolean details: boolean } - debug: boolean + debug: { + response: boolean + log: boolean + } } @@ -57,7 +67,6 @@ function gateway(this: any, options: GatewayOptions) { const allowed = new Patrun({ gex: true }) const checkAllowed = null != options.allow - // console.log('CA', checkAllowed, options) if (checkAllowed) { for (let patStr in options.allow) { @@ -121,17 +130,31 @@ function gateway(this: any, options: GatewayOptions) { // Handle inbound JSON, converting it into a message, and submitting to Seneca. async function handler(json: any, ctx: any) { + if (options.debug.log) { + root.log.debug('gateway-handler-json', { json }) + } + const seneca = await prepare(json, ctx) const rawmsg = tu.internalize_msg(seneca, json) - const msg = seneca.util.clean(rawmsg) + // Clients can set a custom timeout, up to a maximum. + if (options.timeout.client && null != rawmsg.timeout$) { + let clientTimeout = +rawmsg.timeout$ + let maxTimeout = options.timeout.max + maxTimeout = 0 < maxTimeout ? maxTimeout : seneca.options().timeout + if (clientTimeout <= maxTimeout) { + msg.timeout$ = clientTimeout + } + } + return await new Promise(async (resolve) => { if (checkAllowed) { let allowMsg = false // First, find msg that will be called let msgdef = seneca.find(msg) + if (msgdef) { // Second, check found msg matches allowed patterns // NOTE: just doing allowed.find(msg) will enable separate messages @@ -139,18 +162,34 @@ function gateway(this: any, options: GatewayOptions) { // will still work, which is not what we want! allowMsg = !!allowed.find(msgdef.msgcanon) } + else { + seneca.log.debug('msg-not-found', { msg }) + } if (!allowMsg) { + let errdesc: any = { + name: 'Error', + code: 'not-allowed', + message: 'Message not allowed', + details: undefined, + pattern: undefined, + allowed: undefined, + } + + if (options.debug.response) { + errdesc.pattern = msgdef ? msgdef.pattern : undefined + errdesc.allowed = msgdef ? allowMsg : undefined + } + + if (options.debug.log) { + seneca.log.debug('handler-not-allowed', { allowMsg, errdesc, msgdef, msg }) + } + return resolve({ error: true, out: { meta$: { id: rawmsg.id$ }, - error$: nundef({ - name: 'Error', - code: 'not-allowed', - message: 'Message not allowed', - details: undefined, - }) + error$: nundef(errdesc) } }) } @@ -160,23 +199,30 @@ function gateway(this: any, options: GatewayOptions) { for (var i = 0; i < hooks.action.length; i++) { out = await hooks.action[i].call(seneca, msg, ctx) if (out) { + if (options.debug.log) { + seneca.log.debug('handler-hook-action', { out, msg }) + } return resolve(out) } } + if (options.debug.log) { + seneca.log.debug('handler-act', { msg }) + } + seneca.act(msg, async function(this: any, err: any, out: any, meta: any) { for (var i = 0; i < hooks.result.length; i++) { await hooks.result[i].call(seneca, out, msg, err, meta, ctx) } - if (err && !options.debug) { + if (err && !options.debug.response) { err.stack = null } out = tu.externalize_reply(this, err, out, meta) // Don't expose internal activity unless debugging - if (!options.debug) { + if (!options.debug.response) { if (out.meta$) { out.meta$ = { id: out.meta$.id @@ -239,6 +285,10 @@ function gateway(this: any, options: GatewayOptions) { } } + if (options.debug.log) { + root.log.debug('gateway-delegate-params', { fixed, custom }) + } + // NOTE: a new delegate is created for each request to ensure isolation. const delegate = root.delegate(fixed, { custom: custom }) @@ -286,18 +336,32 @@ function nundef(o: any) { // Default options. -gateway.defaults = { +gateway.defaults = ({ + // Keys are pattern strings. allow: Skip(Open({})), + // Add custom meta data values. custom: Open({ // Assume gateway is used to handle external messages. safe: false }), + // Set request delegate fixed values. fixed: Open({}), + // Allow clients to set a custom timeout (using the timeout$ directive). + timeout: { + + // Clients can set a custom timeout. + client: false, + + // Maximum value of client-set timeout. + // Default is same as Seneca delegate. + max: -1 + }, + error: { // Include exception object message property in response. message: false, @@ -306,9 +370,17 @@ gateway.defaults = { details: false, }, - // When true, errors will include stack trace. - debug: false -} + + // Control debug output. + debug: { + + // When true, errors will include stack trace and other meta data. + response: false, + + // Produce detailed debug logging. + log: false, + } +} as GatewayOptions) export type {