moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.17k stars 587 forks source link

Number key in cacher breaks when maxParamsLength is set and keys are specified #1289

Closed leiyangyou closed 3 months ago

leiyangyou commented 5 months ago

Current Behavior

when cacher has maxParamsLength set, if the action also specifies a keys array say ["id"], and the id is of type number, the cacher crashes upon key generation, as hashkey will pass id to crypto.createHash, the length check fails because

1.length < maxLength always fails

the following patch does the trick for now

diff --git a/node_modules/moleculer/src/cachers/base.js b/node_modules/moleculer/src/cachers/base.js
index 3cee84a..06ece92 100644
--- a/node_modules/moleculer/src/cachers/base.js
+++ b/node_modules/moleculer/src/cachers/base.js
@@ -232,7 +232,7 @@ class Cacher {
                    return (
                        keyPrefix +
                        this._hashedKey(
-                           isObject(val) ? this._hashedKey(this._generateKeyFromObject(val)) : val
+                           isObject(val) ? this._hashedKey(this._generateKeyFromObject(val)) : String(val)
                        )
                    );
                }
@@ -248,7 +248,7 @@ class Cacher {
                                    (i ? "|" : "") +
                                    (isObject(val) || Array.isArray(val)
                                        ? this._hashedKey(this._generateKeyFromObject(val))
-                                       : val)
+                                       : String(val))
                                );
                            }, "")
                        )