Skip to content

Commit

Permalink
Merge pull request #1263 from moleculerjs/replOptions
Browse files Browse the repository at this point in the history
[0.15] New "replOptions" broker option
  • Loading branch information
icebob authored Nov 25, 2023
2 parents 7cfc1be + a1d7ca5 commit 759460a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 73 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,45 @@ module.exports = {
};
```

## New REPL options

In order to the REPL options can be more extensible, a new `replOptions` broker option is introduces. You can use it instead of the old `replCommands` and `replDelimiter` broker options.

**Old REPL options**
```js
// moleculer.config.js
module.exports = {
replDelimiter: "mol # ",
replCommands: [
{
command: "hello <name>",
action(broker, args) {
// ...
}
}
]
}
```

**New REPL options**
```js
// moleculer.config.js
module.exports = {
replOptions: {
delimiter: "mol # ",
customCommands: [
{
command: "hello <name>",
action(broker, args) {
// ...
}
}
]
}
}
```
> Please note, you should rename the `replCommands` property to `customCommands`, as well.
## Action streaming

The built-in `Stream` sending has been rewritten. Now it accepts `params` besides the `Stream` instance.
Expand Down
50 changes: 26 additions & 24 deletions dev/dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
*/

function myMiddleware() {
return handler => async ctx => {
const myMiddleware = {
localAction: handler => async ctx => {
ctx.broker.logger.warn(">> MW1-before (from config)");
const res = await handler(ctx);
ctx.broker.logger.warn("<< MW1-after (from config)");
return res;
};
}
}
};

module.exports = {
namespace: "config-test",
transporter: "TCP",
logger: true,
logLevel: "debug",

middlewares: [myMiddleware()],
middlewares: [myMiddleware],

created(broker) {
broker.logger.warn("--- Broker created (from config)!");
Expand All @@ -50,24 +50,26 @@ module.exports = {
stopped(broker) {
return broker.Promise.delay(2000).then(() => broker.logger.warn("--- Broker stopped"));
},
replCommands: [
{
command: "hello <name>",
description: "Call the greeter.hello service with name",
alias: "hi",
options: [{ option: "-u, --uppercase", description: "Uppercase the name" }],
types: {
string: ["name"],
boolean: ["u", "uppercase"]
},
//parse(command, args) {},
//validate(args) {},
//help(args) {},
allowUnknownOptions: true,
action(broker, args) {
const name = args.options.uppercase ? args.name.toUpperCase() : args.name;
return broker.call("greeter.hello", { name }).then(console.log);
replOptions: {
customCommands: [
{
command: "hello <name>",
description: "Call the greeter.hello service with name",
alias: "hi",
options: [{ option: "-u, --uppercase", description: "Uppercase the name" }],
types: {
string: ["name"],
boolean: ["u", "uppercase"]
},
//parse(command, args) {},
//validate(args) {},
//help(args) {},
allowUnknownOptions: true,
action(broker, args) {
const name = args.options.uppercase ? args.name.toUpperCase() : args.name;
return broker.call("greeter.hello", { name }).then(console.log);
}
}
}
]
]
}
};
39 changes: 39 additions & 0 deletions docs/MIGRATION_GUIDE_0.15.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,45 @@ module.exports = {
};
```

## New REPL options

In order to the REPL options can be more extensible, a new `replOptions` broker option is introduces. You can use it instead of the old `replCommands` and `replDelimiter` broker options.

**Old REPL options**
```js
// moleculer.config.js
module.exports = {
replDelimiter: "mol # ",
replCommands: [
{
command: "hello <name>",
action(broker, args) {
// ...
}
}
]
}
```

**New REPL options**
```js
// moleculer.config.js
module.exports = {
replOptions: {
delimiter: "mol # ",
customCommands: [
{
command: "hello <name>",
action(broker, args) {
// ...
}
}
]
}
}
```
> Please note, you should rename the `replCommands` property to `customCommands`, as well.
## New action streaming

The built-in `Stream` sending has been rewritten. Now it accepts `params` besides the `Stream` instance.
Expand Down
42 changes: 22 additions & 20 deletions examples/multi-nodes/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,29 @@ const broker = new ServiceBroker({
}
},
//heartbeatInterval: 10,
replCommands: [
{
command: "scale <count>",
alias: "s",
description: "Scaling up/down nodes",
options: [
{ option: "-k, --kill", description: "Kill nodes" }
//{ option: "--nodeID <nodeID>", description: "NodeID" }
],
types: {
//number: ["service"]
},
action(broker, args) {
console.log(args);
return broker.call("nodes.scale", {
count: Number(args.count != null ? args.count : 0),
kill: args.kill
});
replOptions: {
customCommands: [
{
command: "scale <count>",
alias: "s",
description: "Scaling up/down nodes",
options: [
{ option: "-k, --kill", description: "Kill nodes" }
//{ option: "--nodeID <nodeID>", description: "NodeID" }
],
types: {
//number: ["service"]
},
action(broker, args) {
console.log(args);
return broker.call("nodes.scale", {
count: Number(args.count != null ? args.count : 0),
kill: args.kill
});
}
}
}
]
]
}
});

broker.loadService(path.join(__dirname, "node-controller.service.js"));
Expand Down
9 changes: 7 additions & 2 deletions src/service-broker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type EventEndpoint = require("./registry/endpoint-event");
import type Service = require("./service");
import type { ServiceDependency } from "./service";
import type { Stream } from "stream";
import { ReplOptions } from "repl";

declare namespace ServiceBroker {
type BrokerSyncLifecycleHandler = (broker: ServiceBroker) => void;
Expand Down Expand Up @@ -87,8 +88,7 @@ declare namespace ServiceBroker {

middlewares?: (Middleware | string)[];

replCommands?: Record<string, any>[] | null;
replDelimiter?: string;
replOptions?: ReplOptions | null;

metadata?: Record<string, any>;

Expand All @@ -109,6 +109,11 @@ declare namespace ServiceBroker {
maxSafeObjectSize?: number;
}

export interface ReplOptions {
customCommands?: Record<string, any>[] | null;
delimiter?: string;
}

export interface BrokerRegistryOptions {
strategy?: Function | string;
strategyOptions?: Record<string, any>;
Expand Down
26 changes: 5 additions & 21 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,9 @@ const { Tracer } = require("./tracing");
const C = require("./constants");

/**
* @typedef {Object} CallingOptions Calling options
* @property {number?} timeout
* @property {number?} retries
* @property {Function?} fallbackResponse
* @property {string?} nodeID
* @property {object?} meta
* @property {object?} parentSpan
* @property {Context?} parentCtx
* @property {string?} requestID
* @property {boolean?} tracking
* @property {boolean?} paramsCloning
* @property {string?} caller
* @property {Stream?} stream
* Import types
*
* @typedef {import("./service-broker").CallingOptions} CallingOptions Calling options
*/

/**
Expand Down Expand Up @@ -134,8 +124,7 @@ const defaultOptions = {

middlewares: null,

replCommands: null,
replDelimiter: null,
replOptions: null,

metadata: {},

Expand Down Expand Up @@ -633,12 +622,7 @@ class ServiceBroker {
}

if (repl) {
let opts = null;
const delimiter = this.options.replDelimiter;
const customCommands = this.options.replCommands;
delimiter && (opts = { delimiter });
customCommands && (opts = { ...opts, customCommands });
return repl(this, opts);
return repl(this, this.options.replOptions);
}
}

Expand Down
15 changes: 9 additions & 6 deletions test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ describe("Test ServiceBroker constructor", () => {
dependencyTimeout: 0,
hotReload: true,
middlewares: null,
replCommands: null,
replDelimiter: null,
replOptions: null,
metadata: {
region: "eu-west1"
},
Expand Down Expand Up @@ -975,23 +974,27 @@ describe("Test broker.repl", () => {
repl.mockClear();
let broker = new ServiceBroker({
logger: false,
replCommands: []
replOptions: {
customCommands: []
}
});
broker.repl();

expect(repl).toHaveBeenCalledTimes(1);
expect(repl).toHaveBeenCalledWith(broker, { customCommands: broker.options.replCommands });
expect(repl).toHaveBeenCalledWith(broker, { customCommands: [] });
});
it("should switch to repl mode with delimiter", () => {
repl.mockClear();
let broker = new ServiceBroker({
logger: false,
replDelimiter: "mol # "
replOptions: {
delimiter: "mol # "
}
});
broker.repl();

expect(repl).toHaveBeenCalledTimes(1);
expect(repl).toHaveBeenCalledWith(broker, { delimiter: broker.options.replDelimiter });
expect(repl).toHaveBeenCalledWith(broker, { delimiter: "mol # " });
});
});

Expand Down

0 comments on commit 759460a

Please sign in to comment.