script.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const crypto_1 = require("crypto");
  4. const promiseContainer_1 = require("./promiseContainer");
  5. const command_1 = require("./command");
  6. const standard_as_callback_1 = require("standard-as-callback");
  7. class Script {
  8. constructor(lua, numberOfKeys = null, keyPrefix = "") {
  9. this.lua = lua;
  10. this.numberOfKeys = numberOfKeys;
  11. this.keyPrefix = keyPrefix;
  12. this.sha = crypto_1.createHash("sha1")
  13. .update(lua)
  14. .digest("hex");
  15. }
  16. execute(container, args, options, callback) {
  17. if (typeof this.numberOfKeys === "number") {
  18. args.unshift(this.numberOfKeys);
  19. }
  20. if (this.keyPrefix) {
  21. options.keyPrefix = this.keyPrefix;
  22. }
  23. const evalsha = new command_1.default("evalsha", [this.sha].concat(args), options);
  24. evalsha.isCustomCommand = true;
  25. const result = container.sendCommand(evalsha);
  26. if (promiseContainer_1.isPromise(result)) {
  27. return standard_as_callback_1.default(result.catch(err => {
  28. if (err.toString().indexOf("NOSCRIPT") === -1) {
  29. throw err;
  30. }
  31. return container.sendCommand(new command_1.default("eval", [this.lua].concat(args), options));
  32. }), callback);
  33. }
  34. // result is not a Promise--probably returned from a pipeline chain; however,
  35. // we still need the callback to fire when the script is evaluated
  36. standard_as_callback_1.default(evalsha.promise, callback);
  37. return result;
  38. }
  39. }
  40. exports.default = Script;