From 10dbce12524f0e87adf0b5b8681d97fab62b787f Mon Sep 17 00:00:00 2001 From: Mathias Date: Sun, 6 Aug 2023 15:24:27 +0200 Subject: [PATCH 01/12] Update peer dependency for mqtt to 5.0.2 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6b1826b72..6eac7fb46 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "lockfile-lint": "^4.10.6", "log4js": "^6.9.1", "moleculer-repl": "^0.7.3", - "mqtt": "^4.3.7", + "mqtt": "^5.0.2", "msgpack5": "^6.0.2", "nats": "^2.7.1", "node-nats-streaming": "^0.3.2", @@ -145,7 +145,7 @@ "jaeger-client": "^3.0.0", "kafka-node": "^5.0.0", "log4js": "^6.0.0", - "mqtt": "^4.0.0", + "mqtt": "^5.0.2", "msgpack5": "^5.0.0 || ^6.0.0", "nats": "^1.0.0 || ^2.0.0", "node-nats-streaming": "^0.0.51 || ^0.2.0 || ^0.3.0", From 96b668da9402f0da460ef5c5819a18eacc5c9110 Mon Sep 17 00:00:00 2001 From: Icebob Date: Sun, 6 Aug 2023 16:13:53 +0200 Subject: [PATCH 02/12] revert and lock the etcd3 version due to https://github.com/microsoft/etcd3/issues/190 --- package-lock.json | 82 +++++++++++++++++++++++++++++++++-------------- package.json | 2 +- 2 files changed, 59 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index f65877ad4..089df37aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -594,21 +594,21 @@ "requires": { "@grpc/proto-loader": "^0.7.0", "@types/node": ">=12.12.47" - } - }, - "@grpc/proto-loader": { - "version": "0.7.8", - "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.7.8.tgz", - "integrity": "sha512-GU12e2c8dmdXb7XUlOgYWZ2o2i+z9/VeACkxTA/zzAe2IjclC5PnVL0lpgjhrqfpDYHzM8B1TF6pqWegMYAzlA==", - "dev": true, - "requires": { - "@types/long": "^4.0.1", - "lodash.camelcase": "^4.3.0", - "long": "^4.0.0", - "protobufjs": "^7.2.4", - "yargs": "^17.7.2" }, "dependencies": { + "@grpc/proto-loader": { + "version": "0.7.8", + "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.7.8.tgz", + "integrity": "sha512-GU12e2c8dmdXb7XUlOgYWZ2o2i+z9/VeACkxTA/zzAe2IjclC5PnVL0lpgjhrqfpDYHzM8B1TF6pqWegMYAzlA==", + "dev": true, + "requires": { + "@types/long": "^4.0.1", + "lodash.camelcase": "^4.3.0", + "long": "^4.0.0", + "protobufjs": "^7.2.4", + "yargs": "^17.7.2" + } + }, "cliui": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", @@ -643,6 +643,39 @@ } } }, + "@grpc/proto-loader": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.5.6.tgz", + "integrity": "sha512-DT14xgw3PSzPxwS13auTEwxhMMOoz33DPUKNtmYK/QYbBSpLXJy78FGGs5yVoxVobEqPm4iW9MOIoz0A3bLTRQ==", + "dev": true, + "requires": { + "lodash.camelcase": "^4.3.0", + "protobufjs": "^6.8.6" + }, + "dependencies": { + "protobufjs": { + "version": "6.11.3", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.3.tgz", + "integrity": "sha512-xL96WDdCZYdU7Slin569tFX712BxsxslWwAfAhCYjQKGTq7dAU91Lomy6nLLhh/dyGhk/YH4TwTSRxTzhuHyZg==", + "dev": true, + "requires": { + "@protobufjs/aspromise": "^1.1.2", + "@protobufjs/base64": "^1.1.2", + "@protobufjs/codegen": "^2.0.4", + "@protobufjs/eventemitter": "^1.1.0", + "@protobufjs/fetch": "^1.1.0", + "@protobufjs/float": "^1.0.2", + "@protobufjs/inquire": "^1.1.0", + "@protobufjs/path": "^1.1.2", + "@protobufjs/pool": "^1.1.0", + "@protobufjs/utf8": "^1.1.0", + "@types/long": "^4.0.1", + "@types/node": ">=13.7.0", + "long": "^4.0.0" + } + } + } + }, "@hapi/hoek": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", @@ -2845,9 +2878,9 @@ "dev": true }, "cockatiel": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/cockatiel/-/cockatiel-3.1.1.tgz", - "integrity": "sha512-zHMqBGvkZLfMKkBMD+0U8X1nW8zYwMtymgJ8CTknWOmTDpvjEwygtFN4QR9A1iFQDwCbg8g8+B/zVBoxvj1feQ==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cockatiel/-/cockatiel-1.1.1.tgz", + "integrity": "sha512-YO02ZhTcK2HOZodksWfg4tS1GYYt1j7R6U1unAkwcQf1uoIYKSBqPrfqXlLNbsyMvkDXMwa2nuwZDHbUkB1VbQ==", "dev": true }, "code-point-at": { @@ -4244,15 +4277,15 @@ "dev": true }, "etcd3": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/etcd3/-/etcd3-1.1.2.tgz", - "integrity": "sha512-YIampCz1/OmrVo/tR3QltAVUtYCQQOSFoqmHKKeoHbalm+WdXe3l4rhLIylklu8EzR/I3PBiOF4dC847dDskKg==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/etcd3/-/etcd3-1.1.0.tgz", + "integrity": "sha512-9SnJvaPyW5IYdJHJWX91CYo1QZCAev2B7PxrQWIe2tGbutZOmsXHfjNDKwEltFWoG5h++K3/JfWPjJdOGX90hg==", "dev": true, "requires": { - "@grpc/grpc-js": "^1.8.20", - "@grpc/proto-loader": "^0.7.8", - "bignumber.js": "^9.1.1", - "cockatiel": "^3.1.1" + "@grpc/grpc-js": "^1.1.7", + "@grpc/proto-loader": "^0.5.5", + "bignumber.js": "^9.0.0", + "cockatiel": "^1.1.1" } }, "event-emitter": { @@ -11547,7 +11580,8 @@ }, "word-wrap": { "version": "1.2.3", - "resolved": "", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", + "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", "dev": true }, "wrap-ansi": { diff --git a/package.json b/package.json index b9d4b137d..c14d00fc3 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "eslint-plugin-prettier": "^4.2.1", "eslint-plugin-promise": "^6.1.1", "eslint-plugin-security": "^1.7.1", - "etcd3": "^1.1.2", + "etcd3": "1.1.0", "event-loop-stats": "^1.4.1", "fakerator": "^0.3.6", "gc-stats": "^1.4.0", From 7006e9c47321422a86ca3663e16249db366f6bea Mon Sep 17 00:00:00 2001 From: Mathias Date: Tue, 8 Aug 2023 14:13:56 +0000 Subject: [PATCH 03/12] - allow usage of mqtt 4 and 5 in peer dependencies - use 4.3.7 in dev dependencies to stay compatible with older Node versions --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6eac7fb46..e425b89b6 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "lockfile-lint": "^4.10.6", "log4js": "^6.9.1", "moleculer-repl": "^0.7.3", - "mqtt": "^5.0.2", + "mqtt": "^4.3.7", "msgpack5": "^6.0.2", "nats": "^2.7.1", "node-nats-streaming": "^0.3.2", @@ -145,7 +145,7 @@ "jaeger-client": "^3.0.0", "kafka-node": "^5.0.0", "log4js": "^6.0.0", - "mqtt": "^5.0.2", + "mqtt": "^4.0.0 || ^5.0.0", "msgpack5": "^5.0.0 || ^6.0.0", "nats": "^1.0.0 || ^2.0.0", "node-nats-streaming": "^0.0.51 || ^0.2.0 || ^0.3.0", From a0cec5fa78d97e3d9d87a660124ed4169251e7c4 Mon Sep 17 00:00:00 2001 From: ghostiee Date: Sun, 10 Sep 2023 20:39:32 +0800 Subject: [PATCH 04/12] fix memory leak Resume stream after all chunks are published to prevent memory leak --- src/transit.js | 24 ++++++++++++------------ test/unit/transit.spec.js | 28 ++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/transit.js b/src/transit.js index c7dd87a07..a0723db33 100644 --- a/src/transit.js +++ b/src/transit.js @@ -895,7 +895,7 @@ class Transit { } else { chunks.push(chunk); } - for (const ch of chunks) { + const publishMap = chunks.map(ch => { const copy = Object.assign({}, payload); copy.seq = ++payload.seq; copy.stream = true; @@ -905,12 +905,13 @@ class Transit { `=> Send stream chunk ${requestID}to ${nodeName} node. Seq: ${copy.seq}` ); - this.publish(new Packet(P.PACKET_REQUEST, ctx.nodeID, copy)).catch( - publishCatch - ); - } - stream.resume(); - return; + return this.publish(new Packet(P.PACKET_REQUEST, ctx.nodeID, copy)); + }); + + return this.Promise.all(publishMap).then( + () => stream.resume(), + publishCatch + ); }); stream.on("end", () => { @@ -1126,7 +1127,7 @@ class Transit { } else { chunks.push(chunk); } - for (const ch of chunks) { + const publishMap = chunks.map(ch => { const copy = Object.assign({}, payload); copy.seq = ++payload.seq; copy.stream = true; @@ -1134,10 +1135,9 @@ class Transit { this.logger.debug(`=> Send stream chunk to ${nodeID} node. Seq: ${copy.seq}`); - this.publish(new Packet(P.PACKET_RESPONSE, nodeID, copy)).catch(publishCatch); - } - stream.resume(); - return; + return this.publish(new Packet(P.PACKET_RESPONSE, nodeID, copy)); + }); + return this.Promise.all(publishMap).then(() => stream.resume(), publishCatch); }); stream.on("end", () => { diff --git a/test/unit/transit.spec.js b/test/unit/transit.spec.js index d8bfc3d8c..00eadd923 100644 --- a/test/unit/transit.spec.js +++ b/test/unit/transit.spec.js @@ -1931,6 +1931,10 @@ describe("Test Transit._sendRequest", () => { const resolve = jest.fn(); const reject = jest.fn(); + beforeEach(() => { + transit.publish = jest.fn(() => Promise.resolve().delay(40)); + }); + it("should send stream chunks", () => { transit.publish.mockClear(); @@ -2070,8 +2074,11 @@ describe("Test Transit._sendRequest", () => { transit.publish.mockClear(); stream.push(randomData); }) - .delay(100) + .delay(20) + .then(() => expect(stream.isPaused()).toBeTruthy()) + .delay(80) .then(() => { + expect(stream.isPaused()).toBeFalsy(); expect(transit.publish).toHaveBeenCalledTimes( Math.ceil(randomData.length / transit.opts.maxChunkSize) ); @@ -2163,8 +2170,11 @@ describe("Test Transit._sendRequest", () => { }); transit.publish.mockClear(); }) - .delay(100) + .delay(20) + .then(() => expect(stream.isPaused()).toBeTruthy()) + .delay(80) .then(() => { + expect(stream.isPaused()).toBeFalsy(); expect(transit.publish).toHaveBeenCalledTimes( Math.ceil(randomData.length / transit.opts.maxChunkSize) + 1 ); @@ -2715,6 +2725,10 @@ describe("Test Transit.sendResponse", () => { }); describe("with Stream", () => { + beforeEach(() => { + transit.publish = jest.fn(() => Promise.resolve().delay(40)); + }); + it("should send stream chunks", () => { transit.publish.mockClear(); @@ -2819,8 +2833,11 @@ describe("Test Transit.sendResponse", () => { transit.publish.mockClear(); stream.push("first chunk"); }) - .delay(100) + .delay(20) + .then(() => expect(stream.isPaused()).toBeTruthy()) + .delay(80) .then(() => { + expect(stream.isPaused()).toBeFalsy(); expect(transit.publish).toHaveBeenCalledTimes(1); expect(transit.publish).toHaveBeenCalledWith({ payload: { @@ -2889,8 +2906,11 @@ describe("Test Transit.sendResponse", () => { transit.publish.mockClear(); stream.push(randomData); }) - .delay(100) + .delay(20) + .then(() => expect(stream.isPaused()).toBeTruthy()) + .delay(80) .then(() => { + expect(stream.isPaused()).toBeFalsy(); expect(transit.publish).toHaveBeenCalledTimes( Math.ceil(randomData.length / transit.opts.maxChunkSize) ); From 68c4a3ff041ca0ce06502f600a8b1dd6f2df932e Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Wed, 20 Sep 2023 19:23:48 +0700 Subject: [PATCH 05/12] add d.ts for class Runner (MoleculerRunner) --- index.d.ts | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/index.d.ts b/index.d.ts index ffe250769..4a396a433 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1834,6 +1834,131 @@ declare namespace Moleculer { function makeDirs(path: string): void; function parseByteString(value: string): number; } + + interface RunnerFlags { + + config?: string; + + repl?: boolean; + + hot?: boolean; + + silent?: boolean; + + env?: boolean; + + envfile?: string; + + instances?: number; + + mask?: string; + + } + + /** + * Moleculer Runner + */ + class Runner { + worker: NodeJS.ClusterWorker | null; + broker: ServiceBroker | null; + + /** + * Watch folders for hot reload + */ + watchFolders: string[]; + + /** + * Parsed CLI flags + */ + flags: RunnerFlags | null; + + /** + * Loaded configuration file + */ + configFile: Partial; + + /** + * Merged configuration + */ + config: Partial; + + /** + * Process command line arguments + */ + processFlags(args: string[]): void; + + /** + * Load environment variables from '.env' file + */ + loadEnvFile(): void; + + /** + * Load configuration file + * + * Try to load a configuration file in order to: + * + * - load file defined in MOLECULER_CONFIG env var + * - try to load file which is defined in CLI option with --config + * - try to load the `moleculer.config.js` file if exist in the cwd + * - try to load the `moleculer.config.json` file if exist in the cwd + */ + loadConfigFile(): Promise; + + /** + * Normalize a value from env variable + */ + normalizeEnvValue(value: string): string | number | boolean; + + /** + * Overwrite config values from environment variables + */ + overwriteFromEnv(obj: any, prefix?: string): any; + + /** + * Merge broker options from config file & env variables + */ + mergeOptions(): void; + + /** + * Check if a path is a directory + */ + isDirectory(path: string): boolean; + + /** + * Check if a path is a service file + */ + isServiceFile(path: string): boolean; + + /** + * Load services from files or directories + */ + loadServices(): void; + + /** + * Start cluster workers + */ + startWorkers(instances: number): void; + + /** + * Load service from NPM module + */ + loadNpmModule(name: string): ServiceBroker.Service; + + /** + * Start Moleculer broker + */ + startBroker(): Promise; + + /** + * Restart broker + */ + restartBroker(): Promise; + + /** + * Start runner + */ + start(args: string[]): Promise; + } } export = Moleculer; From 8de4abcdbbd890ec6e4946f93a9b0368f37e1ef1 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Thu, 21 Sep 2023 08:19:27 +0700 Subject: [PATCH 06/12] fix & improve --- index.d.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 4a396a433..f84df9477 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,5 @@ import type { EventEmitter2 } from "eventemitter2"; +import type { Worker } from "cluster"; declare namespace Moleculer { /** @@ -1835,22 +1836,49 @@ declare namespace Moleculer { function parseByteString(value: string): number; } + /** + * Parsed CLI flags + */ interface RunnerFlags { + /** + * Path to load configuration from a file + */ config?: string; + /** + * Start REPL mode + */ repl?: boolean; + /** + * Enable hot reload mode + */ hot?: boolean; + /** + * Silent mode. No logger + */ silent?: boolean; + /** + * Load .env file from current directory + */ env?: boolean; + /** + * Load .env files by glob pattern + */ envfile?: string; + /** + * Number of node instances to start in cluster mode + */ instances?: number; + /** + * File mask for loading services + */ mask?: string; } @@ -1859,7 +1887,7 @@ declare namespace Moleculer { * Moleculer Runner */ class Runner { - worker: NodeJS.ClusterWorker | null; + worker: Worker | null; broker: ServiceBroker | null; /** From 2fbdd2401d47b3c47816b64aefbbe8dfcb89ae7c Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Thu, 21 Sep 2023 08:55:00 +0700 Subject: [PATCH 07/12] fix & improve --- index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index f84df9477..ab378243d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1903,12 +1903,12 @@ declare namespace Moleculer { /** * Loaded configuration file */ - configFile: Partial; + configFile: Partial; /** * Merged configuration */ - config: Partial; + config: Partial; /** * Process command line arguments @@ -1970,7 +1970,7 @@ declare namespace Moleculer { /** * Load service from NPM module */ - loadNpmModule(name: string): ServiceBroker.Service; + loadNpmModule(name: string): Service; /** * Start Moleculer broker From 6fb089d706ad0941bf314fa05a05f2afc3bfa7ac Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Fri, 22 Sep 2023 18:14:18 +0700 Subject: [PATCH 08/12] imp(index.d.ts): add Middlewares declaration --- index.d.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/index.d.ts b/index.d.ts index ffe250769..13deaa34f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,5 @@ import type { EventEmitter2 } from "eventemitter2"; +import type { BinaryLike, CipherCCMTypes, CipherGCMTypes, CipherKey, CipherOCBTypes } from 'crypto' declare namespace Moleculer { /** @@ -166,18 +167,18 @@ declare namespace Moleculer { type TracingActionTags = | TracingActionTagsFuncType | { - params?: boolean | string[]; - meta?: boolean | string[]; - response?: boolean | string[]; - }; + params?: boolean | string[]; + meta?: boolean | string[]; + response?: boolean | string[]; + }; type TracingEventTagsFuncType = (ctx: Context) => GenericObject; type TracingEventTags = | TracingEventTagsFuncType | { - params?: boolean | string[]; - meta?: boolean | string[]; - }; + params?: boolean | string[]; + meta?: boolean | string[]; + }; type TracingSpanNameOption = string | ((ctx: Context) => string); @@ -1016,8 +1017,8 @@ declare namespace Moleculer { internalServices?: | boolean | { - [key: string]: Partial; - }; + [key: string]: Partial; + }; internalMiddlewares?: boolean; dependencyInterval?: number; @@ -1834,6 +1835,55 @@ declare namespace Moleculer { function makeDirs(path: string): void; function parseByteString(value: string): number; } + + const Middlewares = { + Transmit: { + /** + * Encrypts the Transporter payload + * @param key The key to use for encryption + * @param [algorithm] The algorithm to use for encryption. Default is aes-256-cbc + * @param [iv] The initialization vector to use for encryption. Optional + * @example // moleculer.config.js + * const crypto = require("crypto"); + * const { Middlewares } = require("moleculer"); + * const initVector = crypto.randomBytes(16); + * + * module.exports = { + * middlewares: [ + * Middlewares.Transmit.Encryption("secret-password", "aes-256-cbc", initVector) // "aes-256-cbc" is the default + * ] + * }; + */ + Encryption: (key: CipherKey, algorithm?: CipherCCMTypes|CipherOCBTypes|CipherGCMTypes|string, iv?: BinaryLike | null)=> Middleware, + Compression: (opts?: { + /** + * @default deflate + */ + method?: 'gzip' | 'deflate' | 'deflateRaw' + /** + * Compression middleware reduces the size of the messages that go through the transporter module. + * This middleware uses built-in Node zlib lib. + * Threshold should be a number of bytes or a string like 100kb, 4mb, etc. Accepted units are: + * - kb, for kilobytes + * - mb, for megabytes + * - gb, for gigabytes + * - tb, for terabytes + * - pb, for petabytes + * @default 1kb + * @example // moleculer.config.js + * const { Middlewares } = require("moleculer"); + * + * // Create broker + * module.exports = { + * middlewares: [ + * Middlewares.Transmit.Compression("deflate") // or "deflateRaw" or "gzip" + * ] + * }; + */ + threshold?: number | string + }) => Middleware, + }, + } } export = Moleculer; From a30bb3a6c56c5dd4103fc047a4b2ff64fc51eb04 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Fri, 22 Sep 2023 19:50:51 +0700 Subject: [PATCH 09/12] fix test:ts --- index.d.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 13deaa34f..a02511462 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1836,7 +1836,8 @@ declare namespace Moleculer { function parseByteString(value: string): number; } - const Middlewares = { + /* @private */ + interface MoleculerMiddlewares { Transmit: { /** * Encrypts the Transporter payload @@ -1882,8 +1883,9 @@ declare namespace Moleculer { */ threshold?: number | string }) => Middleware, - }, + } } + const Middlewares: MoleculerMiddlewares } export = Moleculer; From 0fd010b542e0626bc816d97cea28cb8cceba1090 Mon Sep 17 00:00:00 2001 From: Icebob Date: Sun, 24 Sep 2023 15:07:29 +0200 Subject: [PATCH 10/12] issue dev example --- dev/issue-1241.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 dev/issue-1241.js diff --git a/dev/issue-1241.js b/dev/issue-1241.js new file mode 100644 index 000000000..0954331ac --- /dev/null +++ b/dev/issue-1241.js @@ -0,0 +1,54 @@ +const ServiceBroker = require("../src/service-broker"); + +const broker = new ServiceBroker({ + middlewares: [ + { + call(next) { + return (actionName, params, opts) => { + const p = next(actionName, params, opts); + + const pp = p.then(res => { + return res; + }); + + pp.ctx = p.ctx; + + return pp; + }; + } + } + ] +}); + +broker.createService({ + name: "statusCodeTest", + + actions: { + testNotFound: { + rest: "GET /testNotFound", + handler(ctx) { + ctx.meta.$statusCode = 404; + } + } + } +}); + +broker.createService({ + name: "test", + actions: { + hello: { + async handler(ctx) { + await ctx.call("statusCodeTest.testNotFound"); + this.logger.info("Context meta", ctx.meta); + } + } + } +}); + +broker.start().then(() => { + broker.repl(); + + broker.call("test.hello").then(res => { + console.log("Result:", res); + }); +}); From 1524218755eb40a3d4b9d2b4b997fc06850fb7e2 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Tue, 26 Sep 2023 00:43:43 +0700 Subject: [PATCH 11/12] imp(index.d.ts): middleware serviceCreating params --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index 606b51ffc..c6dbcf354 100644 --- a/index.d.ts +++ b/index.d.ts @@ -667,6 +667,7 @@ declare namespace Moleculer { | ((handler: ActionHandler, event: ServiceEvent) => any) | ((handler: ActionHandler) => any) | ((service: Service) => any) + | ((service: Service, serviceSchema: ServiceSchema) => any) | ((broker: ServiceBroker) => any) | ((handler: CallMiddlewareHandler) => CallMiddlewareHandler); }; From cad067d5939a68220bb49218b049a1570f146083 Mon Sep 17 00:00:00 2001 From: Icebob Date: Sun, 1 Oct 2023 11:23:04 +0200 Subject: [PATCH 12/12] change code style --- src/transit.js | 56 ++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/transit.js b/src/transit.js index a0723db33..2f8e4f593 100644 --- a/src/transit.js +++ b/src/transit.js @@ -895,23 +895,23 @@ class Transit { } else { chunks.push(chunk); } - const publishMap = chunks.map(ch => { - const copy = Object.assign({}, payload); - copy.seq = ++payload.seq; - copy.stream = true; - copy.params = ch; - this.logger.debug( - `=> Send stream chunk ${requestID}to ${nodeName} node. Seq: ${copy.seq}` - ); - - return this.publish(new Packet(P.PACKET_REQUEST, ctx.nodeID, copy)); - }); - - return this.Promise.all(publishMap).then( - () => stream.resume(), - publishCatch - ); + return this.Promise.all( + chunks.map(ch => { + const copy = Object.assign({}, payload); + copy.seq = ++payload.seq; + copy.stream = true; + copy.params = ch; + + this.logger.debug( + `=> Send stream chunk ${requestID}to ${nodeName} node. Seq: ${copy.seq}` + ); + + return this.publish(new Packet(P.PACKET_REQUEST, ctx.nodeID, copy)); + }) + ) + .then(() => stream.resume()) + .catch(publishCatch); }); stream.on("end", () => { @@ -1127,17 +1127,23 @@ class Transit { } else { chunks.push(chunk); } - const publishMap = chunks.map(ch => { - const copy = Object.assign({}, payload); - copy.seq = ++payload.seq; - copy.stream = true; - copy.data = ch; - this.logger.debug(`=> Send stream chunk to ${nodeID} node. Seq: ${copy.seq}`); + return this.Promise.all( + chunks.map(ch => { + const copy = Object.assign({}, payload); + copy.seq = ++payload.seq; + copy.stream = true; + copy.data = ch; - return this.publish(new Packet(P.PACKET_RESPONSE, nodeID, copy)); - }); - return this.Promise.all(publishMap).then(() => stream.resume(), publishCatch); + this.logger.debug( + `=> Send stream chunk to ${nodeID} node. Seq: ${copy.seq}` + ); + + return this.publish(new Packet(P.PACKET_RESPONSE, nodeID, copy)); + }) + ) + .then(() => stream.resume()) + .catch(publishCatch); }); stream.on("end", () => {