UNPKG

ioredis-cache

Version:

A promise-based cache package for Nodejs using IORedis

517 lines (516 loc) 29.7 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LuaAcquire = void 0; var Acquire_1 = require("./Acquire"); var DEFAULT_PREFIX = ""; var FLOAT_FLAG = "1"; var INTEGER_FLAG = "0"; var HAS_LIMIT = "1"; var NO_LIMIT = "0"; var ACQUIRE_MANY_COMMAND = "ioredisCacheAcquireMany"; var HASH_ACQUIRE_MANY_COMMAND = "ioredisCacheHashAcquireMany"; var RELEASE_MANY_COMMAND = "ioredisCacheReleaseMany"; var HASH_RELEASE_MANY_COMMAND = "ioredisCacheHashReleaseMany"; var ACQUIRE_FAILED_MESSAGE = "Failed to acquire requested amounts"; var RELEASE_FAILED_MESSAGE = "Failed to release acquired amounts"; var HASH_RELEASE_FAILED_MESSAGE = "Failed to release acquired hash amounts"; var RELEASE_AFTER_CALLBACK_FAILED_MESSAGE = "Failed to release acquired amounts after acquire callback failed"; var HASH_RELEASE_AFTER_CALLBACK_FAILED_MESSAGE = "Failed to release acquired hash amounts after acquire callback failed"; var INTEGER_RADIX = 10; var NON_FINITE_AMOUNT_MESSAGE = "Acquire amounts must be finite numbers"; var ACQUIRE_MANY_SCRIPT = "\nlocal isFloat = ARGV[1] == \"1\"\nlocal offset = 2\n\nfor index = 1, #KEYS do\n local value = redis.call(\"GET\", KEYS[index])\n local desire = ARGV[offset + 1]\n local hasLimit = ARGV[offset + 2] == \"1\"\n local limit = ARGV[offset + 3]\n\n if isFloat then\n if tonumber(desire) == nil then\n return redis.error_reply(\"ERR desire amount is not a valid float\")\n end\n elseif string.match(desire, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR desire amount is not an integer\")\n end\n\n if hasLimit then\n if isFloat then\n if tonumber(limit) == nil then\n return redis.error_reply(\"ERR limit amount is not a valid float\")\n end\n elseif string.match(limit, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR limit amount is not an integer\")\n end\n end\n\n if value ~= false then\n if isFloat then\n if tonumber(value) == nil then\n return redis.error_reply(\"ERR value is not a valid float\")\n end\n elseif string.match(value, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR value is not an integer or out of range\")\n end\n end\n\n offset = offset + 4\nend\n\nlocal results = {}\noffset = 2\n\nfor index = 1, #KEYS do\n local logicalKey = ARGV[offset]\n local desire = tonumber(ARGV[offset + 1])\n local hasLimit = ARGV[offset + 2] == \"1\"\n local limit = tonumber(ARGV[offset + 3])\n local current = tonumber(redis.call(\"GET\", KEYS[index]) or \"0\")\n local acquired = desire\n local lacking = 0\n\n if hasLimit then\n local available = math.max(0, limit - current)\n acquired = math.min(desire, available)\n lacking = desire - acquired\n end\n\n if isFloat then\n redis.call(\"INCRBYFLOAT\", KEYS[index], acquired)\n else\n redis.call(\"INCRBY\", KEYS[index], acquired)\n end\n\n results[index] = {\n logicalKey,\n tostring(current + acquired),\n tostring(acquired),\n tostring(lacking)\n }\n offset = offset + 4\nend\n\nreturn results\n"; var HASH_ACQUIRE_MANY_SCRIPT = "\nlocal isFloat = ARGV[1] == \"1\"\nlocal offset = 2\nlocal hashKey = KEYS[1]\nlocal itemCount = tonumber(ARGV[offset])\noffset = offset + 1\n\nfor index = 1, itemCount do\n local field = ARGV[offset]\n local desire = ARGV[offset + 1]\n local hasLimit = ARGV[offset + 2] == \"1\"\n local limit = ARGV[offset + 3]\n local value = redis.call(\"HGET\", hashKey, field)\n\n if isFloat then\n if tonumber(desire) == nil then\n return redis.error_reply(\"ERR desire amount is not a valid float\")\n end\n elseif string.match(desire, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR desire amount is not an integer\")\n end\n\n if hasLimit then\n if isFloat then\n if tonumber(limit) == nil then\n return redis.error_reply(\"ERR limit amount is not a valid float\")\n end\n elseif string.match(limit, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR limit amount is not an integer\")\n end\n end\n\n if value ~= false then\n if isFloat then\n if tonumber(value) == nil then\n return redis.error_reply(\"ERR hash value is not a valid float\")\n end\n elseif string.match(value, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR hash value is not an integer or out of range\")\n end\n end\n\n offset = offset + 4\nend\n\nlocal results = {}\noffset = 3\n\nfor index = 1, itemCount do\n local field = ARGV[offset]\n local desire = tonumber(ARGV[offset + 1])\n local hasLimit = ARGV[offset + 2] == \"1\"\n local limit = tonumber(ARGV[offset + 3])\n local current = tonumber(redis.call(\"HGET\", hashKey, field) or \"0\")\n local acquired = desire\n local lacking = 0\n\n if hasLimit then\n local available = math.max(0, limit - current)\n acquired = math.min(desire, available)\n lacking = desire - acquired\n end\n\n if isFloat then\n redis.call(\"HINCRBYFLOAT\", hashKey, field, acquired)\n else\n redis.call(\"HINCRBY\", hashKey, field, acquired)\n end\n\n results[index] = {\n field,\n tostring(current + acquired),\n tostring(acquired),\n tostring(lacking)\n }\n offset = offset + 4\nend\n\nreturn results\n"; var RELEASE_MANY_SCRIPT = "\nlocal isFloat = ARGV[1] == \"1\"\nlocal offset = 2\n\nfor index = 1, #KEYS do\n local amount = ARGV[offset + 1]\n if not isFloat and string.match(amount, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR release amount is not an integer\")\n end\n offset = offset + 2\nend\n\noffset = 2\n\nfor index = 1, #KEYS do\n local amount = tonumber(ARGV[offset + 1])\n if amount ~= 0 then\n if isFloat then\n redis.call(\"INCRBYFLOAT\", KEYS[index], -amount)\n else\n redis.call(\"DECRBY\", KEYS[index], amount)\n end\n end\n offset = offset + 2\nend\n\nreturn 1\n"; var HASH_RELEASE_MANY_SCRIPT = "\nlocal isFloat = ARGV[1] == \"1\"\nlocal hashKey = KEYS[1]\nlocal itemCount = tonumber(ARGV[2])\nlocal offset = 3\n\nfor index = 1, itemCount do\n local amount = ARGV[offset + 1]\n if not isFloat and string.match(amount, \"^-?%d+$\") == nil then\n return redis.error_reply(\"ERR release amount is not an integer\")\n end\n offset = offset + 2\nend\n\noffset = 3\n\nfor index = 1, itemCount do\n local field = ARGV[offset]\n local amount = tonumber(ARGV[offset + 1])\n if amount ~= 0 then\n if isFloat then\n redis.call(\"HINCRBYFLOAT\", hashKey, field, -amount)\n else\n redis.call(\"HINCRBY\", hashKey, field, -amount)\n end\n end\n offset = offset + 2\nend\n\nreturn 1\n"; var LuaAcquire = /** @class */ (function () { function LuaAcquire(redis) { this.redis = redis; this.defineCommands(); } LuaAcquire.prototype.acquire = function (key, amount, fn, options) { return __awaiter(this, void 0, void 0, function () { var normalizedOptions; return __generator(this, function (_a) { normalizedOptions = normalizeAcquireOptions(options); assertFiniteAmount(amount); assertFiniteLimit(normalizedOptions.limit); if (normalizedOptions.limit === undefined || normalizedOptions.limit === null) { return [2 /*return*/, this.acquireSingle(key, amount, fn, normalizedOptions)]; } return [2 /*return*/, this.acquireMany([ { key: String(key), desireAmount: amount, limitAmount: normalizedOptions.limit, }, ], function (_a) { var result = _a[0]; return fn(result.currentAmount, result.acquiredAmount, result.lackingAmount); }, normalizedOptions)]; }); }); }; LuaAcquire.prototype.hashAcquire = function (hashKey, id, amount, fn, options) { return __awaiter(this, void 0, void 0, function () { var normalizedOptions; return __generator(this, function (_a) { normalizedOptions = normalizeAcquireOptions(options); assertFiniteAmount(amount); assertFiniteLimit(normalizedOptions.limit); if (normalizedOptions.limit === undefined || normalizedOptions.limit === null) { return [2 /*return*/, this.hashAcquireSingle(hashKey, id, amount, fn, normalizedOptions)]; } return [2 /*return*/, this.hashAcquireMany(String(hashKey), [ { key: String(id), desireAmount: amount, limitAmount: normalizedOptions.limit, }, ], function (_a) { var result = _a[0]; return fn(result.currentAmount, result.acquiredAmount, result.lackingAmount); }, normalizedOptions)]; }); }); }; LuaAcquire.prototype.acquireMany = function (items, fn, options) { return __awaiter(this, void 0, void 0, function () { var acquiredResults; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: assertFiniteItems(items); if (!!items.length) return [3 /*break*/, 2]; return [4 /*yield*/, fn([])]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [4 /*yield*/, this.runAcquireManyScript(items, options)]; case 3: acquiredResults = _a.sent(); return [2 /*return*/, this.runAcquireCallback(fn, acquiredResults, function (releaseItems) { return _this.releaseMany(releaseItems, options); }, RELEASE_AFTER_CALLBACK_FAILED_MESSAGE)]; } }); }); }; LuaAcquire.prototype.hashAcquireMany = function (hashKey, items, fn, options) { return __awaiter(this, void 0, void 0, function () { var acquiredResults; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: assertFiniteItems(items); if (!!items.length) return [3 /*break*/, 2]; return [4 /*yield*/, fn([])]; case 1: return [2 /*return*/, _a.sent()]; case 2: return [4 /*yield*/, this.runHashAcquireManyScript(hashKey, items, options)]; case 3: acquiredResults = _a.sent(); return [2 /*return*/, this.runAcquireCallback(fn, acquiredResults, function (releaseItems) { return _this.hashReleaseMany(hashKey, releaseItems, options); }, HASH_RELEASE_AFTER_CALLBACK_FAILED_MESSAGE)]; } }); }); }; LuaAcquire.prototype.runAcquireManyScript = function (items, options) { var _a; return __awaiter(this, void 0, void 0, function () { var prefix, keys, args, result, error_1; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: prefix = (_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : DEFAULT_PREFIX; keys = items.map(function (item) { return "" + prefix + item.key; }); args = this.buildAcquireArgs(items, options); _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, (_b = this.luaRedis).ioredisCacheAcquireMany.apply(_b, __spreadArrays([keys.length], keys, args))]; case 2: result = _c.sent(); return [2 /*return*/, this.parseAcquiredRows(items, result)]; case 3: error_1 = _c.sent(); throw new Acquire_1.AcquireFailedError(ACQUIRE_FAILED_MESSAGE, error_1); case 4: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.acquireSingle = function (key, amount, fn, options) { return __awaiter(this, void 0, void 0, function () { var float, change, parse, currentAmount, _a, error_2; return __generator(this, function (_b) { switch (_b.label) { case 0: float = (options === null || options === void 0 ? void 0 : options.float) === true; change = this.getChangeCommand(float); parse = float ? parseFloatValue : parseIntegerValue; _b.label = 1; case 1: _b.trys.push([1, 4, , 6]); _a = parse; return [4 /*yield*/, change(key, amount)]; case 2: currentAmount = _a.apply(void 0, [_b.sent()]); return [4 /*yield*/, fn(currentAmount, amount, 0)]; case 3: return [2 /*return*/, _b.sent()]; case 4: error_2 = _b.sent(); return [4 /*yield*/, change(key, -amount)]; case 5: _b.sent(); throw error_2; case 6: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.hashAcquireSingle = function (hashKey, id, amount, fn, options) { return __awaiter(this, void 0, void 0, function () { var float, change, parse, currentAmount, _a, error_3; return __generator(this, function (_b) { switch (_b.label) { case 0: float = (options === null || options === void 0 ? void 0 : options.float) === true; change = this.getHashChangeCommand(float); parse = float ? parseFloatValue : parseIntegerValue; _b.label = 1; case 1: _b.trys.push([1, 4, , 6]); _a = parse; return [4 /*yield*/, change(hashKey, id, amount)]; case 2: currentAmount = _a.apply(void 0, [_b.sent()]); return [4 /*yield*/, fn(currentAmount, amount, 0)]; case 3: return [2 /*return*/, _b.sent()]; case 4: error_3 = _b.sent(); return [4 /*yield*/, change(hashKey, id, -amount)]; case 5: _b.sent(); throw error_3; case 6: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.getChangeCommand = function (float) { var _this = this; if (float) { return function (key, amount) { return _this.redis.incrbyfloat(key, amount); }; } return function (key, amount) { return _this.redis.incrby(key, amount); }; }; LuaAcquire.prototype.getHashChangeCommand = function (float) { var _this = this; if (float) { return function (key, id, amount) { return _this.redis.hincrbyfloat(key, id, amount); }; } return function (key, id, amount) { return _this.redis.hincrby(key, id, amount); }; }; LuaAcquire.prototype.runHashAcquireManyScript = function (hashKey, items, options) { var _a; return __awaiter(this, void 0, void 0, function () { var redisKey, args, result, error_4; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: redisKey = "" + ((_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : DEFAULT_PREFIX) + hashKey; args = this.buildHashAcquireArgs(items, options); _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, (_b = this.luaRedis).ioredisCacheHashAcquireMany.apply(_b, __spreadArrays([redisKey], args))]; case 2: result = _c.sent(); return [2 /*return*/, this.parseAcquiredRows(items, result)]; case 3: error_4 = _c.sent(); throw new Acquire_1.AcquireFailedError(ACQUIRE_FAILED_MESSAGE, error_4); case 4: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.runAcquireCallback = function (fn, acquiredResults, releaseAcquired, releaseFailureMessage) { return __awaiter(this, void 0, void 0, function () { var error_5, releaseError_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 7]); return [4 /*yield*/, fn(acquiredResults)]; case 1: return [2 /*return*/, _a.sent()]; case 2: error_5 = _a.sent(); _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); return [4 /*yield*/, releaseAcquired(this.getAcquiredReleaseItems(acquiredResults))]; case 4: _a.sent(); return [3 /*break*/, 6]; case 5: releaseError_1 = _a.sent(); throw new Acquire_1.ReleaseFailedError(releaseFailureMessage, { acquireError: error_5, releaseError: releaseError_1, }); case 6: throw error_5; case 7: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.releaseMany = function (items, options) { var _a; return __awaiter(this, void 0, void 0, function () { var prefix, keys, args; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!items.length) { return [2 /*return*/]; } prefix = (_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : DEFAULT_PREFIX; keys = items.map(function (item) { return "" + prefix + item.key; }); args = this.buildReleaseArgs(items, options); return [4 /*yield*/, this.runDynamicKeyReleaseScript(RELEASE_MANY_COMMAND, keys.length, __spreadArrays(keys, args), RELEASE_FAILED_MESSAGE)]; case 1: _b.sent(); return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.hashReleaseMany = function (hashKey, items, options) { var _a; return __awaiter(this, void 0, void 0, function () { var redisKey, args; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!items.length) { return [2 /*return*/]; } redisKey = "" + ((_a = options === null || options === void 0 ? void 0 : options.prefix) !== null && _a !== void 0 ? _a : DEFAULT_PREFIX) + hashKey; args = this.buildHashReleaseArgs(items, options); return [4 /*yield*/, this.runFixedKeyReleaseScript(HASH_RELEASE_MANY_COMMAND, __spreadArrays([redisKey], args), HASH_RELEASE_FAILED_MESSAGE)]; case 1: _b.sent(); return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.runDynamicKeyReleaseScript = function (command, keyCount, params, failureMessage) { return __awaiter(this, void 0, void 0, function () { var error_6; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, (_a = this.luaRedis)[command].apply(_a, __spreadArrays([keyCount], params))]; case 1: _b.sent(); return [3 /*break*/, 3]; case 2: error_6 = _b.sent(); throw new Acquire_1.ReleaseFailedError(failureMessage, error_6); case 3: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.runFixedKeyReleaseScript = function (command, params, failureMessage) { return __awaiter(this, void 0, void 0, function () { var error_7; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 2, , 3]); return [4 /*yield*/, (_a = this.luaRedis)[command].apply(_a, params)]; case 1: _b.sent(); return [3 /*break*/, 3]; case 2: error_7 = _b.sent(); throw new Acquire_1.ReleaseFailedError(failureMessage, error_7); case 3: return [2 /*return*/]; } }); }); }; LuaAcquire.prototype.defineCommands = function () { this.redis.defineCommand(ACQUIRE_MANY_COMMAND, { lua: ACQUIRE_MANY_SCRIPT, }); this.redis.defineCommand(HASH_ACQUIRE_MANY_COMMAND, { numberOfKeys: 1, lua: HASH_ACQUIRE_MANY_SCRIPT, }); this.redis.defineCommand(RELEASE_MANY_COMMAND, { lua: RELEASE_MANY_SCRIPT, }); this.redis.defineCommand(HASH_RELEASE_MANY_COMMAND, { numberOfKeys: 1, lua: HASH_RELEASE_MANY_SCRIPT, }); }; Object.defineProperty(LuaAcquire.prototype, "luaRedis", { get: function () { return this.redis; }, enumerable: false, configurable: true }); LuaAcquire.prototype.buildAcquireArgs = function (items, options) { var _a; var args = [this.getFloatFlag(options)]; for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { var item = items_1[_i]; args.push(String(item.key), String(item.desireAmount), item.limitAmount === undefined || item.limitAmount === null ? NO_LIMIT : HAS_LIMIT, String((_a = item.limitAmount) !== null && _a !== void 0 ? _a : 0)); } return args; }; LuaAcquire.prototype.buildHashAcquireArgs = function (items, options) { return __spreadArrays([ this.getFloatFlag(options), String(items.length) ], this.buildAcquireArgs(items).slice(1)); }; LuaAcquire.prototype.buildReleaseArgs = function (items, options) { var args = [this.getFloatFlag(options)]; for (var _i = 0, items_2 = items; _i < items_2.length; _i++) { var item = items_2[_i]; args.push(String(item.key), String(item.amount)); } return args; }; LuaAcquire.prototype.buildHashReleaseArgs = function (items, options) { return __spreadArrays([ this.getFloatFlag(options), String(items.length) ], this.buildReleaseArgs(items).slice(1)); }; LuaAcquire.prototype.getFloatFlag = function (options) { return (options === null || options === void 0 ? void 0 : options.float) === true ? FLOAT_FLAG : INTEGER_FLAG; }; LuaAcquire.prototype.parseAcquiredRows = function (items, result) { var _this = this; if (!Array.isArray(result) || result.length !== items.length) { throw new Acquire_1.AcquireFailedError(ACQUIRE_FAILED_MESSAGE); } return result.map(function (row, index) { if (!_this.isLuaRow(row)) { throw new Acquire_1.AcquireFailedError(ACQUIRE_FAILED_MESSAGE); } return { key: items[index].key, currentAmount: Number(row[1]), acquiredAmount: Number(row[2]), lackingAmount: Number(row[3]), }; }); }; LuaAcquire.prototype.isLuaRow = function (row) { return Array.isArray(row) && row.length === 4; }; LuaAcquire.prototype.getAcquiredReleaseItems = function (items) { return items .filter(function (item) { return item.acquiredAmount > 0; }) .map(function (item) { return ({ key: item.key, amount: item.acquiredAmount }); }); }; return LuaAcquire; }()); exports.LuaAcquire = LuaAcquire; var parseIntegerValue = function (value) { if (typeof value === "number") { return value; } return parseInt(value, INTEGER_RADIX); }; var parseFloatValue = function (value) { if (typeof value === "number") { return value; } return parseFloat(value); }; var normalizeAcquireOptions = function (options) { if (typeof options === "boolean") { return { float: options }; } return options !== null && options !== void 0 ? options : {}; }; var assertFiniteItems = function (items) { for (var _i = 0, items_3 = items; _i < items_3.length; _i++) { var item = items_3[_i]; assertFiniteAmount(item.desireAmount); assertFiniteLimit(item.limitAmount); } }; var assertFiniteAmount = function (amount) { if (!Number.isFinite(amount)) { throw new Acquire_1.AcquireFailedError(NON_FINITE_AMOUNT_MESSAGE); } }; var assertFiniteLimit = function (limit) { if (limit !== undefined && !Number.isFinite(limit)) { throw new Acquire_1.AcquireFailedError(NON_FINITE_AMOUNT_MESSAGE); } };