diff --git a/client.js b/client.js index 20d1d6e5..d4b18ff7 100644 --- a/client.js +++ b/client.js @@ -358,6 +358,10 @@ class InstanceConnection extends link.Link { } } + async forwardRequestToMaster(message, request) { + return await request.send(this.slave, message.data); + } + async forwardEventToInstance(message, event) { let instanceId = message.data.instance_id; let instanceConnection = this.slave.instanceConnections.get(instanceId); diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index e955503e..16d8865d 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -408,8 +408,8 @@ The following properties are recognized by the Request constructor: Target to forward request to. Can be either `'master'` to indicate a slave should forward it to the master server when receiving it from an instance or `'instance'` to indicate it should be forwarded - to the instances specified by the `instance_id` event property. - This works by using a default handler for the event at the links + to the instances specified by the `instance_id` request property. + This works by using a default handler for the request at the links that forward it. **requestProperties**: diff --git a/lib/link/messages.js b/lib/link/messages.js index edb87dc1..e651e5cb 100644 --- a/lib/link/messages.js +++ b/lib/link/messages.js @@ -20,9 +20,9 @@ class Request { * Links this request is valid on. Takes an array of strings * containing "-" specifications. * @param {?string} forwardTo - - * Optional target to forward this request to. Only 'instance' is - * currently supported and adds instance_id into the - * requestProperties. + * Optional target to forward this request to. 'instance' add + * instance_id into the requestProperties and forward to the given + * instance. 'master' forwards it to the master server. * @param {Object} requestProperties - * Mapping of property values to JSON schema specifications for the * properties that are valid in the data payload of this requst. @@ -46,7 +46,7 @@ class Request { {"instance_id": { type: "integer" }}, requestProperties ); - } else if (forwardTo !== null) { + } else if (forwardTo !== "master" && forwardTo !== null) { throw new Error(`Invalid forwardTo value ${forwardTo}`); } @@ -99,6 +99,9 @@ class Request { * If forwardTo was set to 'instance' the `forwardRequestToInstance` * method on the link is used as the default handler. * + * If forwardTo was set to 'master' the `forwardRequesToMaster` method + * on the link is used as the default handler. + * * Does nothing if the link is neither a source nor a target for this * request. * @@ -121,6 +124,8 @@ class Request { if (!handler) { if (this.forwardTo === 'instance') { handler = link.forwardRequestToInstance; + } else if (this.forwardTo === "master") { + handler = link.forwardRequestToMaster; } }