swarthy/redis-semaphore

Execution environment built with Webpack throws error for `NOSCRIPT No matching script. Please use EVAL.`

Closed this issue · 4 comments

0skgc commented

execution environment

  • The system is put together with Webpack and run on a Web3 platform called Moralis.

Error contents
When I run a command that uses Lua, as shown below, it throws NOSCRIPT No matching script. Please use EVAL..

const lock = new Semaphore(redisClient, redisKey, 1, lockOptions)
await lock.acquire()

Workaround
It seems that the error is not caught in createEval.ts line 32.
We do not know the cause, but if you fix line 31 as follows, the error is caught.
Before

return (await client.evalsha(sha1, keysCount, .... . args)) as Promise<Result>

After

const result = (await client.evalsha(sha1, keysCount, .... .args)) as Promise<Result>
return result

Hi! I guess that the problem is in the webpack configuration and you should figure out why the incorrect code is being built. Otherwise, you'll have to do a PR to each of the libraries you use that has a similar code (return await <promise>).

0skgc commented

Hi, thank you for your reply.

Here is the code generated by webpack, it looks like there is no problem, but when I upload it to the Moralis service and run it, it does not work properly. I'm wondering if it could be a platform issue...

/***/ "./node_modules/redis-semaphore/lib/utils/createEval.js":
/*!**************************************************************!*\
  !*** ./node_modules/redis-semaphore/lib/utils/createEval.js ***!
  \**************************************************************/
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const crypto_1 = __webpack_require__(/*! crypto */ "crypto");
const debug_1 = __importDefault(__webpack_require__(/*! debug */ "./node_modules/debug/src/index.js"));
const index_1 = __webpack_require__(/*! ./index */ "./node_modules/redis-semaphore/lib/utils/index.js");
const debug = (0, debug_1.default)('redis-semaphore:eval');
function createSHA1(script) {
    return (0, crypto_1.createHash)('sha1').update(script, 'utf8').digest('hex');
}
function isNoScriptError(err) {
    return err.toString().indexOf('NOSCRIPT') !== -1;
}
function createEval(script, keysCount) {
    const sha1 = createSHA1(script);
    debug('creating script:', script, 'sha1:', sha1);
    return async function optimizedEval(client, args) {
        const connectionName = (0, index_1.getConnectionName)(client);
        const evalSHAArgs = [sha1, keysCount, ...args];
        debug(connectionName, sha1, 'attempt, args:', evalSHAArgs);
        try {
            return (await client.evalsha(sha1, keysCount, ...args));
        }
        catch (err) {
            if (isNoScriptError(err)) {
                const evalArgs = [script, keysCount, ...args];
                debug(connectionName, sha1, 'fallback to eval, args:', evalArgs);
                return (await client.eval(script, keysCount, ...args));
            }
            else {
                throw err;
            }
        }
    };
}
exports["default"] = createEval;
//# sourceMappingURL=createEval.js.map

/***/ }),

@0skgc did you solve the problem? Looks like it's platform issue

0skgc commented

@swarthy
The problem has not been resolved, but I have found that changing the compilerOptions.target in tsconfig seems to improve the situation.

Currently in my project, the target is es6, but I recently changed the target to ES2021 and the same problem occurred in the code in the project.
(I was aware that the execution environment is Node v16.15.1, so there is no problem with ES2021.)

In any case, I think the problem is caused by the execution environment, so I am closing this Issue.