diff --git a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop.2.minified.js b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop.2.minified.js index 27f665e22397..a3dc5cf7a489 100644 --- a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop.2.minified.js @@ -3,19 +3,20 @@ import _class_private_field_init from "@swc/helpers/src/_class_private_field_ini import _class_private_method_init from "@swc/helpers/src/_class_private_method_init.mjs"; let array = []; for(let i = 0; i < 10; ++i){ + var _myField, _method, _accessor; function get_accessor() { return 42; } function set_accessor(val) {} - array.push(class { + array.push((_myField = new WeakMap(), _method = new WeakSet(), _accessor = new WeakMap(), class { constructor(){ - _class_private_method_init(this, new WeakSet()), _class_private_field_init(this, new WeakMap(), { + _class_private_method_init(this, _method), _class_private_field_init(this, _accessor, { get: get_accessor, set: set_accessor - }), _class_private_field_init(this, new WeakMap(), { + }), _class_private_field_init(this, _myField, { writable: !0, value: "hello" }); } - }); + })); } diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2015).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2015).2.minified.js index 533c39857d14..e44fb08981f1 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2015).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2015).2.minified.js @@ -1,10 +1,10 @@ //// [typeOfThisInStaticMembers12.ts] var _Inner; -let _c; +let _c, _c1; class C { } -C.c = "foo", C.bar = (_c = C.c, (_Inner = class { +C.c = "foo", C.bar = (_c = C.c, _c1 = C.c, (_Inner = class { constructor(){ - this[C.c] = 123; + this[_c1] = 123; } })[_c] = 123, _Inner); diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2015).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2015).2.minified.js index ada84c4bf478..2e7dbae14f73 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2015).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2015).2.minified.js @@ -1,11 +1,11 @@ //// [typeOfThisInStaticMembers13.ts] var _Inner; -let _c; +let _c, _c1; import _define_property from "@swc/helpers/src/_define_property.mjs"; class C { } -_define_property(C, "c", "foo"), _define_property(C, "bar", (_c = C.c, _Inner = class { +_define_property(C, "c", "foo"), _define_property(C, "bar", (_c = C.c, _c1 = C.c, _Inner = class { constructor(){ - _define_property(this, C.c, 123); + _define_property(this, _c1, 123); } }, _define_property(_Inner, _c, 123), _Inner)); diff --git a/crates/swc_ecma_minifier/src/analyzer/mod.rs b/crates/swc_ecma_minifier/src/analyzer/mod.rs index bd6d9362c35d..46bbb1308bf6 100644 --- a/crates/swc_ecma_minifier/src/analyzer/mod.rs +++ b/crates/swc_ecma_minifier/src/analyzer/mod.rs @@ -425,6 +425,24 @@ where }) } + #[cfg_attr(feature = "debug", tracing::instrument(skip_all))] + fn visit_constructor(&mut self, n: &Constructor) { + self.with_child(n.span.ctxt, ScopeKind::Fn, |child| { + { + let ctx = Ctx { + in_pat_of_param: true, + ..child.ctx + }; + n.params.visit_with(&mut *child.with_ctx(ctx)); + } + + // Bypass visit_block_stmt + if let Some(body) = &n.body { + body.visit_with(child); + } + }) + } + #[cfg_attr(feature = "debug", tracing::instrument(skip(self, n)))] fn visit_assign_expr(&mut self, n: &AssignExpr) { let ctx = Ctx { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 7a3fa8b58e4d..77fda191dcbb 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -1308,6 +1308,10 @@ where ) }; + if b.is_class() || b.is_fn_expr() || b.is_arrow() { + return Ok(false); + } + match a { Mergable::Var(..) => {} Mergable::Expr(a) => { diff --git a/crates/swc_ecma_minifier/src/compress/util/mod.rs b/crates/swc_ecma_minifier/src/compress/util/mod.rs index 74074fd2be8a..736016b61a5c 100644 --- a/crates/swc_ecma_minifier/src/compress/util/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/util/mod.rs @@ -347,20 +347,10 @@ pub(crate) fn negate_cost( return cost(expr_ctx, last, in_bool_ctx, bin_op, is_ret_val_ignored); } - if is_ret_val_ignored { - 0 - } else { - 1 - } + isize::from(!is_ret_val_ignored) } - _ => { - if is_ret_val_ignored { - 0 - } else { - 1 - } - } + _ => isize::from(!is_ret_val_ignored), } })(); diff --git a/crates/swc_ecma_minifier/src/util/size.rs b/crates/swc_ecma_minifier/src/util/size.rs index 4f131c61ebc1..475a0f7a1374 100644 --- a/crates/swc_ecma_minifier/src/util/size.rs +++ b/crates/swc_ecma_minifier/src/util/size.rs @@ -162,7 +162,7 @@ impl SizeWithCtxt for Expr { _ => 2 + params.size(unresolved), }; let a = if *is_async { - 5 + if params.len() == 1 { 0 } else { 1 } + 5 + usize::from(params.len() != 1) } else { 0 }; diff --git a/crates/swc_ecma_minifier/tests/fixture/next/octokit/config.json b/crates/swc_ecma_minifier/tests/fixture/next/octokit/config.json new file mode 100644 index 000000000000..0804f8f8b56f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/octokit/config.json @@ -0,0 +1,4 @@ +{ + "defaults": true, + "toplevel": true +} diff --git a/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/input.js b/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/input.js new file mode 100644 index 000000000000..563c412a527b --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/input.js @@ -0,0 +1,1535 @@ +(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([[357], { +/***/ 1209: +/***/ (function (module, __unused_webpack_exports, __webpack_require__) { + + /** + * This file contains the Bottleneck library (MIT), compiled to ES2017, and without Clustering support. + * https://github.com/SGrondin/bottleneck + */ + (function (global, factory) { + true ? module.exports = factory() : + 0; + }(this, (function () { + 'use strict'; + + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof __webpack_require__.g !== 'undefined' ? __webpack_require__.g : typeof self !== 'undefined' ? self : {}; + + function getCjsExportFromNamespace(n) { + return n && n['default'] || n; + } + + var load = function (received, defaults, onto = {}) { + var k, ref, v; + for (k in defaults) { + v = defaults[k]; + onto[k] = (ref = received[k]) != null ? ref : v; + } + return onto; + }; + + var overwrite = function (received, defaults, onto = {}) { + var k, v; + for (k in received) { + v = received[k]; + if (defaults[k] !== void 0) { + onto[k] = v; + } + } + return onto; + }; + + var parser = { + load: load, + overwrite: overwrite + }; + + var DLList; + + DLList = class DLList { + constructor(incr, decr) { + this.incr = incr; + this.decr = decr; + this._first = null; + this._last = null; + this.length = 0; + } + + push(value) { + var node; + this.length++; + if (typeof this.incr === "function") { + this.incr(); + } + node = { + value, + prev: this._last, + next: null + }; + if (this._last != null) { + this._last.next = node; + this._last = node; + } else { + this._first = this._last = node; + } + return void 0; + } + + shift() { + var value; + if (this._first == null) { + return; + } else { + this.length--; + if (typeof this.decr === "function") { + this.decr(); + } + } + value = this._first.value; + if ((this._first = this._first.next) != null) { + this._first.prev = null; + } else { + this._last = null; + } + return value; + } + + first() { + if (this._first != null) { + return this._first.value; + } + } + + getArray() { + var node, ref, results; + node = this._first; + results = []; + while (node != null) { + results.push((ref = node, node = node.next, ref.value)); + } + return results; + } + + forEachShift(cb) { + var node; + node = this.shift(); + while (node != null) { + (cb(node), node = this.shift()); + } + return void 0; + } + + debug() { + var node, ref, ref1, ref2, results; + node = this._first; + results = []; + while (node != null) { + results.push((ref = node, node = node.next, { + value: ref.value, + prev: (ref1 = ref.prev) != null ? ref1.value : void 0, + next: (ref2 = ref.next) != null ? ref2.value : void 0 + })); + } + return results; + } + + }; + + var DLList_1 = DLList; + + var Events; + + Events = class Events { + constructor(instance) { + this.instance = instance; + this._events = {}; + if ((this.instance.on != null) || (this.instance.once != null) || (this.instance.removeAllListeners != null)) { + throw new Error("An Emitter already exists for this object"); + } + this.instance.on = (name, cb) => { + return this._addListener(name, "many", cb); + }; + this.instance.once = (name, cb) => { + return this._addListener(name, "once", cb); + }; + this.instance.removeAllListeners = (name = null) => { + if (name != null) { + return delete this._events[name]; + } else { + return this._events = {}; + } + }; + } + + _addListener(name, status, cb) { + var base; + if ((base = this._events)[name] == null) { + base[name] = []; + } + this._events[name].push({ cb, status }); + return this.instance; + } + + listenerCount(name) { + if (this._events[name] != null) { + return this._events[name].length; + } else { + return 0; + } + } + + async trigger(name, ...args) { + var e, promises; + try { + if (name !== "debug") { + this.trigger("debug", `Event triggered: ${name}`, args); + } + if (this._events[name] == null) { + return; + } + this._events[name] = this._events[name].filter(function (listener) { + return listener.status !== "none"; + }); + promises = this._events[name].map(async (listener) => { + var e, returned; + if (listener.status === "none") { + return; + } + if (listener.status === "once") { + listener.status = "none"; + } + try { + returned = typeof listener.cb === "function" ? listener.cb(...args) : void 0; + if (typeof (returned != null ? returned.then : void 0) === "function") { + return (await returned); + } else { + return returned; + } + } catch (error) { + e = error; + { + this.trigger("error", e); + } + return null; + } + }); + return ((await Promise.all(promises))).find(function (x) { + return x != null; + }); + } catch (error) { + e = error; + { + this.trigger("error", e); + } + return null; + } + } + + }; + + var Events_1 = Events; + + var DLList$1, Events$1, Queues; + + DLList$1 = DLList_1; + + Events$1 = Events_1; + + Queues = class Queues { + constructor(num_priorities) { + var i; + this.Events = new Events$1(this); + this._length = 0; + this._lists = (function () { + var j, ref, results; + results = []; + for (i = j = 1, ref = num_priorities; (1 <= ref ? j <= ref : j >= ref); i = 1 <= ref ? ++j : --j) { + results.push(new DLList$1((() => { + return this.incr(); + }), (() => { + return this.decr(); + }))); + } + return results; + }).call(this); + } + + incr() { + if (this._length++ === 0) { + return this.Events.trigger("leftzero"); + } + } + + decr() { + if (--this._length === 0) { + return this.Events.trigger("zero"); + } + } + + push(job) { + return this._lists[job.options.priority].push(job); + } + + queued(priority) { + if (priority != null) { + return this._lists[priority].length; + } else { + return this._length; + } + } + + shiftAll(fn) { + return this._lists.forEach(function (list) { + return list.forEachShift(fn); + }); + } + + getFirst(arr = this._lists) { + var j, len, list; + for (j = 0, len = arr.length; j < len; j++) { + list = arr[j]; + if (list.length > 0) { + return list; + } + } + return []; + } + + shiftLastFrom(priority) { + return this.getFirst(this._lists.slice(priority).reverse()).shift(); + } + + }; + + var Queues_1 = Queues; + + var BottleneckError; + + BottleneckError = class BottleneckError extends Error { }; + + var BottleneckError_1 = BottleneckError; + + var BottleneckError$1, DEFAULT_PRIORITY, Job, NUM_PRIORITIES, parser$1; + + NUM_PRIORITIES = 10; + + DEFAULT_PRIORITY = 5; + + parser$1 = parser; + + BottleneckError$1 = BottleneckError_1; + + Job = class Job { + constructor(task, args, options, jobDefaults, rejectOnDrop, Events, _states, Promise) { + this.task = task; + this.args = args; + this.rejectOnDrop = rejectOnDrop; + this.Events = Events; + this._states = _states; + this.Promise = Promise; + this.options = parser$1.load(options, jobDefaults); + this.options.priority = this._sanitizePriority(this.options.priority); + if (this.options.id === jobDefaults.id) { + this.options.id = `${this.options.id}-${this._randomIndex()}`; + } + this.promise = new this.Promise((_resolve, _reject) => { + this._resolve = _resolve; + this._reject = _reject; + }); + this.retryCount = 0; + } + + _sanitizePriority(priority) { + var sProperty; + sProperty = ~~priority !== priority ? DEFAULT_PRIORITY : priority; + if (sProperty < 0) { + return 0; + } else if (sProperty > NUM_PRIORITIES - 1) { + return NUM_PRIORITIES - 1; + } else { + return sProperty; + } + } + + _randomIndex() { + return Math.random().toString(36).slice(2); + } + + doDrop({ error, message = "This job has been dropped by Bottleneck" } = {}) { + if (this._states.remove(this.options.id)) { + if (this.rejectOnDrop) { + this._reject(error != null ? error : new BottleneckError$1(message)); + } + this.Events.trigger("dropped", { args: this.args, options: this.options, task: this.task, promise: this.promise }); + return true; + } else { + return false; + } + } + + _assertStatus(expected) { + var status; + status = this._states.jobStatus(this.options.id); + if (!(status === expected || (expected === "DONE" && status === null))) { + throw new BottleneckError$1(`Invalid job status ${status}, expected ${expected}. Please open an issue at https://github.com/SGrondin/bottleneck/issues`); + } + } + + doReceive() { + this._states.start(this.options.id); + return this.Events.trigger("received", { args: this.args, options: this.options }); + } + + doQueue(reachedHWM, blocked) { + this._assertStatus("RECEIVED"); + this._states.next(this.options.id); + return this.Events.trigger("queued", { args: this.args, options: this.options, reachedHWM, blocked }); + } + + doRun() { + if (this.retryCount === 0) { + this._assertStatus("QUEUED"); + this._states.next(this.options.id); + } else { + this._assertStatus("EXECUTING"); + } + return this.Events.trigger("scheduled", { args: this.args, options: this.options }); + } + + async doExecute(chained, clearGlobalState, run, free) { + var error, eventInfo, passed; + if (this.retryCount === 0) { + this._assertStatus("RUNNING"); + this._states.next(this.options.id); + } else { + this._assertStatus("EXECUTING"); + } + eventInfo = { args: this.args, options: this.options, retryCount: this.retryCount }; + this.Events.trigger("executing", eventInfo); + try { + passed = (await (chained != null ? chained.schedule(this.options, this.task, ...this.args) : this.task(...this.args))); + if (clearGlobalState()) { + this.doDone(eventInfo); + await free(this.options, eventInfo); + this._assertStatus("DONE"); + return this._resolve(passed); + } + } catch (error1) { + error = error1; + return this._onFailure(error, eventInfo, clearGlobalState, run, free); + } + } + + doExpire(clearGlobalState, run, free) { + var error, eventInfo; + if (this._states.jobStatus(this.options.id === "RUNNING")) { + this._states.next(this.options.id); + } + this._assertStatus("EXECUTING"); + eventInfo = { args: this.args, options: this.options, retryCount: this.retryCount }; + error = new BottleneckError$1(`This job timed out after ${this.options.expiration} ms.`); + return this._onFailure(error, eventInfo, clearGlobalState, run, free); + } + + async _onFailure(error, eventInfo, clearGlobalState, run, free) { + var retry, retryAfter; + if (clearGlobalState()) { + retry = (await this.Events.trigger("failed", error, eventInfo)); + if (retry != null) { + retryAfter = ~~retry; + this.Events.trigger("retry", `Retrying ${this.options.id} after ${retryAfter} ms`, eventInfo); + this.retryCount++; + return run(retryAfter); + } else { + this.doDone(eventInfo); + await free(this.options, eventInfo); + this._assertStatus("DONE"); + return this._reject(error); + } + } + } + + doDone(eventInfo) { + this._assertStatus("EXECUTING"); + this._states.next(this.options.id); + return this.Events.trigger("done", eventInfo); + } + + }; + + var Job_1 = Job; + + var BottleneckError$2, LocalDatastore, parser$2; + + parser$2 = parser; + + BottleneckError$2 = BottleneckError_1; + + LocalDatastore = class LocalDatastore { + constructor(instance, storeOptions, storeInstanceOptions) { + this.instance = instance; + this.storeOptions = storeOptions; + this.clientId = this.instance._randomIndex(); + parser$2.load(storeInstanceOptions, storeInstanceOptions, this); + this._nextRequest = this._lastReservoirRefresh = this._lastReservoirIncrease = Date.now(); + this._running = 0; + this._done = 0; + this._unblockTime = 0; + this.ready = this.Promise.resolve(); + this.clients = {}; + this._startHeartbeat(); + } + + _startHeartbeat() { + var base; + if ((this.heartbeat == null) && (((this.storeOptions.reservoirRefreshInterval != null) && (this.storeOptions.reservoirRefreshAmount != null)) || ((this.storeOptions.reservoirIncreaseInterval != null) && (this.storeOptions.reservoirIncreaseAmount != null)))) { + return typeof (base = (this.heartbeat = setInterval(() => { + var amount, incr, maximum, now, reservoir; + now = Date.now(); + if ((this.storeOptions.reservoirRefreshInterval != null) && now >= this._lastReservoirRefresh + this.storeOptions.reservoirRefreshInterval) { + this._lastReservoirRefresh = now; + this.storeOptions.reservoir = this.storeOptions.reservoirRefreshAmount; + this.instance._drainAll(this.computeCapacity()); + } + if ((this.storeOptions.reservoirIncreaseInterval != null) && now >= this._lastReservoirIncrease + this.storeOptions.reservoirIncreaseInterval) { + ({ + reservoirIncreaseAmount: amount, + reservoirIncreaseMaximum: maximum, + reservoir + } = this.storeOptions); + this._lastReservoirIncrease = now; + incr = maximum != null ? Math.min(amount, maximum - reservoir) : amount; + if (incr > 0) { + this.storeOptions.reservoir += incr; + return this.instance._drainAll(this.computeCapacity()); + } + } + }, this.heartbeatInterval))).unref === "function" ? base.unref() : void 0; + } else { + return clearInterval(this.heartbeat); + } + } + + async __publish__(message) { + await this.yieldLoop(); + return this.instance.Events.trigger("message", message.toString()); + } + + async __disconnect__(flush) { + await this.yieldLoop(); + clearInterval(this.heartbeat); + return this.Promise.resolve(); + } + + yieldLoop(t = 0) { + return new this.Promise(function (resolve, reject) { + return setTimeout(resolve, t); + }); + } + + computePenalty() { + var ref; + return (ref = this.storeOptions.penalty) != null ? ref : (15 * this.storeOptions.minTime) || 5000; + } + + async __updateSettings__(options) { + await this.yieldLoop(); + parser$2.overwrite(options, options, this.storeOptions); + this._startHeartbeat(); + this.instance._drainAll(this.computeCapacity()); + return true; + } + + async __running__() { + await this.yieldLoop(); + return this._running; + } + + async __queued__() { + await this.yieldLoop(); + return this.instance.queued(); + } + + async __done__() { + await this.yieldLoop(); + return this._done; + } + + async __groupCheck__(time) { + await this.yieldLoop(); + return (this._nextRequest + this.timeout) < time; + } + + computeCapacity() { + var maxConcurrent, reservoir; + ({ maxConcurrent, reservoir } = this.storeOptions); + if ((maxConcurrent != null) && (reservoir != null)) { + return Math.min(maxConcurrent - this._running, reservoir); + } else if (maxConcurrent != null) { + return maxConcurrent - this._running; + } else if (reservoir != null) { + return reservoir; + } else { + return null; + } + } + + conditionsCheck(weight) { + var capacity; + capacity = this.computeCapacity(); + return (capacity == null) || weight <= capacity; + } + + async __incrementReservoir__(incr) { + var reservoir; + await this.yieldLoop(); + reservoir = this.storeOptions.reservoir += incr; + this.instance._drainAll(this.computeCapacity()); + return reservoir; + } + + async __currentReservoir__() { + await this.yieldLoop(); + return this.storeOptions.reservoir; + } + + isBlocked(now) { + return this._unblockTime >= now; + } + + check(weight, now) { + return this.conditionsCheck(weight) && (this._nextRequest - now) <= 0; + } + + async __check__(weight) { + var now; + await this.yieldLoop(); + now = Date.now(); + return this.check(weight, now); + } + + async __register__(index, weight, expiration) { + var now, wait; + await this.yieldLoop(); + now = Date.now(); + if (this.conditionsCheck(weight)) { + this._running += weight; + if (this.storeOptions.reservoir != null) { + this.storeOptions.reservoir -= weight; + } + wait = Math.max(this._nextRequest - now, 0); + this._nextRequest = now + wait + this.storeOptions.minTime; + return { + success: true, + wait, + reservoir: this.storeOptions.reservoir + }; + } else { + return { + success: false + }; + } + } + + strategyIsBlock() { + return this.storeOptions.strategy === 3; + } + + async __submit__(queueLength, weight) { + var blocked, now, reachedHWM; + await this.yieldLoop(); + if ((this.storeOptions.maxConcurrent != null) && weight > this.storeOptions.maxConcurrent) { + throw new BottleneckError$2(`Impossible to add a job having a weight of ${weight} to a limiter having a maxConcurrent setting of ${this.storeOptions.maxConcurrent}`); + } + now = Date.now(); + reachedHWM = (this.storeOptions.highWater != null) && queueLength === this.storeOptions.highWater && !this.check(weight, now); + blocked = this.strategyIsBlock() && (reachedHWM || this.isBlocked(now)); + if (blocked) { + this._unblockTime = now + this.computePenalty(); + this._nextRequest = this._unblockTime + this.storeOptions.minTime; + this.instance._dropAllQueued(); + } + return { + reachedHWM, + blocked, + strategy: this.storeOptions.strategy + }; + } + + async __free__(index, weight) { + await this.yieldLoop(); + this._running -= weight; + this._done += weight; + this.instance._drainAll(this.computeCapacity()); + return { + running: this._running + }; + } + + }; + + var LocalDatastore_1 = LocalDatastore; + + var BottleneckError$3, States; + + BottleneckError$3 = BottleneckError_1; + + States = class States { + constructor(status1) { + this.status = status1; + this._jobs = {}; + this.counts = this.status.map(function () { + return 0; + }); + } + + next(id) { + var current, next; + current = this._jobs[id]; + next = current + 1; + if ((current != null) && next < this.status.length) { + this.counts[current]--; + this.counts[next]++; + return this._jobs[id]++; + } else if (current != null) { + this.counts[current]--; + return delete this._jobs[id]; + } + } + + start(id) { + var initial; + initial = 0; + this._jobs[id] = initial; + return this.counts[initial]++; + } + + remove(id) { + var current; + current = this._jobs[id]; + if (current != null) { + this.counts[current]--; + delete this._jobs[id]; + } + return current != null; + } + + jobStatus(id) { + var ref; + return (ref = this.status[this._jobs[id]]) != null ? ref : null; + } + + statusJobs(status) { + var k, pos, ref, results, v; + if (status != null) { + pos = this.status.indexOf(status); + if (pos < 0) { + throw new BottleneckError$3(`status must be one of ${this.status.join(', ')}`); + } + ref = this._jobs; + results = []; + for (k in ref) { + v = ref[k]; + if (v === pos) { + results.push(k); + } + } + return results; + } else { + return Object.keys(this._jobs); + } + } + + statusCounts() { + return this.counts.reduce(((acc, v, i) => { + acc[this.status[i]] = v; + return acc; + }), {}); + } + + }; + + var States_1 = States; + + var DLList$2, Sync; + + DLList$2 = DLList_1; + + Sync = class Sync { + constructor(name, Promise) { + this.schedule = this.schedule.bind(this); + this.name = name; + this.Promise = Promise; + this._running = 0; + this._queue = new DLList$2(); + } + + isEmpty() { + return this._queue.length === 0; + } + + async _tryToRun() { + var args, cb, error, reject, resolve, returned, task; + if ((this._running < 1) && this._queue.length > 0) { + this._running++; + ({ task, args, resolve, reject } = this._queue.shift()); + cb = (await (async function () { + try { + returned = (await task(...args)); + return function () { + return resolve(returned); + }; + } catch (error1) { + error = error1; + return function () { + return reject(error); + }; + } + })()); + this._running--; + this._tryToRun(); + return cb(); + } + } + + schedule(task, ...args) { + var promise, reject, resolve; + resolve = reject = null; + promise = new this.Promise(function (_resolve, _reject) { + resolve = _resolve; + return reject = _reject; + }); + this._queue.push({ task, args, resolve, reject }); + this._tryToRun(); + return promise; + } + + }; + + var Sync_1 = Sync; + + var version = "2.19.5"; + var version$1 = { + version: version + }; + + var version$2 = /*#__PURE__*/Object.freeze({ + version: version, + default: version$1 + }); + + var require$$2 = () => console.log('You must import the full version of Bottleneck in order to use this feature.'); + + var require$$3 = () => console.log('You must import the full version of Bottleneck in order to use this feature.'); + + var require$$4 = () => console.log('You must import the full version of Bottleneck in order to use this feature.'); + + var Events$2, Group, IORedisConnection$1, RedisConnection$1, Scripts$1, parser$3; + + parser$3 = parser; + + Events$2 = Events_1; + + RedisConnection$1 = require$$2; + + IORedisConnection$1 = require$$3; + + Scripts$1 = require$$4; + + Group = (function () { + class Group { + constructor(limiterOptions = {}) { + this.deleteKey = this.deleteKey.bind(this); + this.limiterOptions = limiterOptions; + parser$3.load(this.limiterOptions, this.defaults, this); + this.Events = new Events$2(this); + this.instances = {}; + this.Bottleneck = Bottleneck_1; + this._startAutoCleanup(); + this.sharedConnection = this.connection != null; + if (this.connection == null) { + if (this.limiterOptions.datastore === "redis") { + this.connection = new RedisConnection$1(Object.assign({}, this.limiterOptions, { Events: this.Events })); + } else if (this.limiterOptions.datastore === "ioredis") { + this.connection = new IORedisConnection$1(Object.assign({}, this.limiterOptions, { Events: this.Events })); + } + } + } + + key(key = "") { + var ref; + return (ref = this.instances[key]) != null ? ref : (() => { + var limiter; + limiter = this.instances[key] = new this.Bottleneck(Object.assign(this.limiterOptions, { + id: `${this.id}-${key}`, + timeout: this.timeout, + connection: this.connection + })); + this.Events.trigger("created", limiter, key); + return limiter; + })(); + } + + async deleteKey(key = "") { + var deleted, instance; + instance = this.instances[key]; + if (this.connection) { + deleted = (await this.connection.__runCommand__(['del', ...Scripts$1.allKeys(`${this.id}-${key}`)])); + } + if (instance != null) { + delete this.instances[key]; + await instance.disconnect(); + } + return (instance != null) || deleted > 0; + } + + limiters() { + var k, ref, results, v; + ref = this.instances; + results = []; + for (k in ref) { + v = ref[k]; + results.push({ + key: k, + limiter: v + }); + } + return results; + } + + keys() { + return Object.keys(this.instances); + } + + async clusterKeys() { + var cursor, end, found, i, k, keys, len, next, start; + if (this.connection == null) { + return this.Promise.resolve(this.keys()); + } + keys = []; + cursor = null; + start = `b_${this.id}-`.length; + end = "_settings".length; + while (cursor !== 0) { + [next, found] = (await this.connection.__runCommand__(["scan", cursor != null ? cursor : 0, "match", `b_${this.id}-*_settings`, "count", 10000])); + cursor = ~~next; + for (i = 0, len = found.length; i < len; i++) { + k = found[i]; + keys.push(k.slice(start, -end)); + } + } + return keys; + } + + _startAutoCleanup() { + var base; + clearInterval(this.interval); + return typeof (base = (this.interval = setInterval(async () => { + var e, k, ref, results, time, v; + time = Date.now(); + ref = this.instances; + results = []; + for (k in ref) { + v = ref[k]; + try { + if ((await v._store.__groupCheck__(time))) { + results.push(this.deleteKey(k)); + } else { + results.push(void 0); + } + } catch (error) { + e = error; + results.push(v.Events.trigger("error", e)); + } + } + return results; + }, this.timeout / 2))).unref === "function" ? base.unref() : void 0; + } + + updateSettings(options = {}) { + parser$3.overwrite(options, this.defaults, this); + parser$3.overwrite(options, options, this.limiterOptions); + if (options.timeout != null) { + return this._startAutoCleanup(); + } + } + + disconnect(flush = true) { + var ref; + if (!this.sharedConnection) { + return (ref = this.connection) != null ? ref.disconnect(flush) : void 0; + } + } + + } + Group.prototype.defaults = { + timeout: 1000 * 60 * 5, + connection: null, + Promise: Promise, + id: "group-key" + }; + + return Group; + + }).call(commonjsGlobal); + + var Group_1 = Group; + + var Batcher, Events$3, parser$4; + + parser$4 = parser; + + Events$3 = Events_1; + + Batcher = (function () { + class Batcher { + constructor(options = {}) { + this.options = options; + parser$4.load(this.options, this.defaults, this); + this.Events = new Events$3(this); + this._arr = []; + this._resetPromise(); + this._lastFlush = Date.now(); + } + + _resetPromise() { + return this._promise = new this.Promise((res, rej) => { + return this._resolve = res; + }); + } + + _flush() { + clearTimeout(this._timeout); + this._lastFlush = Date.now(); + this._resolve(); + this.Events.trigger("batch", this._arr); + this._arr = []; + return this._resetPromise(); + } + + add(data) { + var ret; + this._arr.push(data); + ret = this._promise; + if (this._arr.length === this.maxSize) { + this._flush(); + } else if ((this.maxTime != null) && this._arr.length === 1) { + this._timeout = setTimeout(() => { + return this._flush(); + }, this.maxTime); + } + return ret; + } + + } + Batcher.prototype.defaults = { + maxTime: null, + maxSize: null, + Promise: Promise + }; + + return Batcher; + + }).call(commonjsGlobal); + + var Batcher_1 = Batcher; + + var require$$4$1 = () => console.log('You must import the full version of Bottleneck in order to use this feature.'); + + var require$$8 = getCjsExportFromNamespace(version$2); + + var Bottleneck, DEFAULT_PRIORITY$1, Events$4, Job$1, LocalDatastore$1, NUM_PRIORITIES$1, Queues$1, RedisDatastore$1, States$1, Sync$1, parser$5, + splice = [].splice; + + NUM_PRIORITIES$1 = 10; + + DEFAULT_PRIORITY$1 = 5; + + parser$5 = parser; + + Queues$1 = Queues_1; + + Job$1 = Job_1; + + LocalDatastore$1 = LocalDatastore_1; + + RedisDatastore$1 = require$$4$1; + + Events$4 = Events_1; + + States$1 = States_1; + + Sync$1 = Sync_1; + + Bottleneck = (function () { + class Bottleneck { + constructor(options = {}, ...invalid) { + var storeInstanceOptions, storeOptions; + this._addToQueue = this._addToQueue.bind(this); + this._validateOptions(options, invalid); + parser$5.load(options, this.instanceDefaults, this); + this._queues = new Queues$1(NUM_PRIORITIES$1); + this._scheduled = {}; + this._states = new States$1(["RECEIVED", "QUEUED", "RUNNING", "EXECUTING"].concat(this.trackDoneStatus ? ["DONE"] : [])); + this._limiter = null; + this.Events = new Events$4(this); + this._submitLock = new Sync$1("submit", this.Promise); + this._registerLock = new Sync$1("register", this.Promise); + storeOptions = parser$5.load(options, this.storeDefaults, {}); + this._store = (function () { + if (this.datastore === "redis" || this.datastore === "ioredis" || (this.connection != null)) { + storeInstanceOptions = parser$5.load(options, this.redisStoreDefaults, {}); + return new RedisDatastore$1(this, storeOptions, storeInstanceOptions); + } else if (this.datastore === "local") { + storeInstanceOptions = parser$5.load(options, this.localStoreDefaults, {}); + return new LocalDatastore$1(this, storeOptions, storeInstanceOptions); + } else { + throw new Bottleneck.prototype.BottleneckError(`Invalid datastore type: ${this.datastore}`); + } + }).call(this); + this._queues.on("leftzero", () => { + var ref; + return (ref = this._store.heartbeat) != null ? typeof ref.ref === "function" ? ref.ref() : void 0 : void 0; + }); + this._queues.on("zero", () => { + var ref; + return (ref = this._store.heartbeat) != null ? typeof ref.unref === "function" ? ref.unref() : void 0 : void 0; + }); + } + + _validateOptions(options, invalid) { + if (!((options != null) && typeof options === "object" && invalid.length === 0)) { + throw new Bottleneck.prototype.BottleneckError("Bottleneck v2 takes a single object argument. Refer to https://github.com/SGrondin/bottleneck#upgrading-to-v2 if you're upgrading from Bottleneck v1."); + } + } + + ready() { + return this._store.ready; + } + + clients() { + return this._store.clients; + } + + channel() { + return `b_${this.id}`; + } + + channel_client() { + return `b_${this.id}_${this._store.clientId}`; + } + + publish(message) { + return this._store.__publish__(message); + } + + disconnect(flush = true) { + return this._store.__disconnect__(flush); + } + + chain(_limiter) { + this._limiter = _limiter; + return this; + } + + queued(priority) { + return this._queues.queued(priority); + } + + clusterQueued() { + return this._store.__queued__(); + } + + empty() { + return this.queued() === 0 && this._submitLock.isEmpty(); + } + + running() { + return this._store.__running__(); + } + + done() { + return this._store.__done__(); + } + + jobStatus(id) { + return this._states.jobStatus(id); + } + + jobs(status) { + return this._states.statusJobs(status); + } + + counts() { + return this._states.statusCounts(); + } + + _randomIndex() { + return Math.random().toString(36).slice(2); + } + + check(weight = 1) { + return this._store.__check__(weight); + } + + _clearGlobalState(index) { + if (this._scheduled[index] != null) { + clearTimeout(this._scheduled[index].expiration); + delete this._scheduled[index]; + return true; + } else { + return false; + } + } + + async _free(index, job, options, eventInfo) { + var e, running; + try { + ({ running } = (await this._store.__free__(index, options.weight))); + this.Events.trigger("debug", `Freed ${options.id}`, eventInfo); + if (running === 0 && this.empty()) { + return this.Events.trigger("idle"); + } + } catch (error1) { + e = error1; + return this.Events.trigger("error", e); + } + } + + _run(index, job, wait) { + var clearGlobalState, free, run; + job.doRun(); + clearGlobalState = this._clearGlobalState.bind(this, index); + run = this._run.bind(this, index, job); + free = this._free.bind(this, index, job); + return this._scheduled[index] = { + timeout: setTimeout(() => { + return job.doExecute(this._limiter, clearGlobalState, run, free); + }, wait), + expiration: job.options.expiration != null ? setTimeout(function () { + return job.doExpire(clearGlobalState, run, free); + }, wait + job.options.expiration) : void 0, + job: job + }; + } + + _drainOne(capacity) { + return this._registerLock.schedule(() => { + var args, index, next, options, queue; + if (this.queued() === 0) { + return this.Promise.resolve(null); + } + queue = this._queues.getFirst(); + ({ options, args } = next = queue.first()); + if ((capacity != null) && options.weight > capacity) { + return this.Promise.resolve(null); + } + this.Events.trigger("debug", `Draining ${options.id}`, { args, options }); + index = this._randomIndex(); + return this._store.__register__(index, options.weight, options.expiration).then(({ success, wait, reservoir }) => { + var empty; + this.Events.trigger("debug", `Drained ${options.id}`, { success, args, options }); + if (success) { + queue.shift(); + empty = this.empty(); + if (empty) { + this.Events.trigger("empty"); + } + if (reservoir === 0) { + this.Events.trigger("depleted", empty); + } + this._run(index, next, wait); + return this.Promise.resolve(options.weight); + } else { + return this.Promise.resolve(null); + } + }); + }); + } + + _drainAll(capacity, total = 0) { + return this._drainOne(capacity).then((drained) => { + var newCapacity; + if (drained != null) { + newCapacity = capacity != null ? capacity - drained : capacity; + return this._drainAll(newCapacity, total + drained); + } else { + return this.Promise.resolve(total); + } + }).catch((e) => { + return this.Events.trigger("error", e); + }); + } + + _dropAllQueued(message) { + return this._queues.shiftAll(function (job) { + return job.doDrop({ message }); + }); + } + + stop(options = {}) { + var done, waitForExecuting; + options = parser$5.load(options, this.stopDefaults); + waitForExecuting = (at) => { + var finished; + finished = () => { + var counts; + counts = this._states.counts; + return (counts[0] + counts[1] + counts[2] + counts[3]) === at; + }; + return new this.Promise((resolve, reject) => { + if (finished()) { + return resolve(); + } else { + return this.on("done", () => { + if (finished()) { + this.removeAllListeners("done"); + return resolve(); + } + }); + } + }); + }; + done = options.dropWaitingJobs ? (this._run = function (index, next) { + return next.doDrop({ + message: options.dropErrorMessage + }); + }, this._drainOne = () => { + return this.Promise.resolve(null); + }, this._registerLock.schedule(() => { + return this._submitLock.schedule(() => { + var k, ref, v; + ref = this._scheduled; + for (k in ref) { + v = ref[k]; + if (this.jobStatus(v.job.options.id) === "RUNNING") { + clearTimeout(v.timeout); + clearTimeout(v.expiration); + v.job.doDrop({ + message: options.dropErrorMessage + }); + } + } + this._dropAllQueued(options.dropErrorMessage); + return waitForExecuting(0); + }); + })) : this.schedule({ + priority: NUM_PRIORITIES$1 - 1, + weight: 0 + }, () => { + return waitForExecuting(1); + }); + this._receive = function (job) { + return job._reject(new Bottleneck.prototype.BottleneckError(options.enqueueErrorMessage)); + }; + this.stop = () => { + return this.Promise.reject(new Bottleneck.prototype.BottleneckError("stop() has already been called")); + }; + return done; + } + + async _addToQueue(job) { + var args, blocked, error, options, reachedHWM, shifted, strategy; + ({ args, options } = job); + try { + ({ reachedHWM, blocked, strategy } = (await this._store.__submit__(this.queued(), options.weight))); + } catch (error1) { + error = error1; + this.Events.trigger("debug", `Could not queue ${options.id}`, { args, options, error }); + job.doDrop({ error }); + return false; + } + if (blocked) { + job.doDrop(); + return true; + } else if (reachedHWM) { + shifted = strategy === Bottleneck.prototype.strategy.LEAK ? this._queues.shiftLastFrom(options.priority) : strategy === Bottleneck.prototype.strategy.OVERFLOW_PRIORITY ? this._queues.shiftLastFrom(options.priority + 1) : strategy === Bottleneck.prototype.strategy.OVERFLOW ? job : void 0; + if (shifted != null) { + shifted.doDrop(); + } + if ((shifted == null) || strategy === Bottleneck.prototype.strategy.OVERFLOW) { + if (shifted == null) { + job.doDrop(); + } + return reachedHWM; + } + } + job.doQueue(reachedHWM, blocked); + this._queues.push(job); + await this._drainAll(); + return reachedHWM; + } + + _receive(job) { + if (this._states.jobStatus(job.options.id) != null) { + job._reject(new Bottleneck.prototype.BottleneckError(`A job with the same id already exists (id=${job.options.id})`)); + return false; + } else { + job.doReceive(); + return this._submitLock.schedule(this._addToQueue, job); + } + } + + submit(...args) { + var cb, fn, job, options, ref, ref1, task; + if (typeof args[0] === "function") { + ref = args, [fn, ...args] = ref, [cb] = splice.call(args, -1); + options = parser$5.load({}, this.jobDefaults); + } else { + ref1 = args, [options, fn, ...args] = ref1, [cb] = splice.call(args, -1); + options = parser$5.load(options, this.jobDefaults); + } + task = (...args) => { + return new this.Promise(function (resolve, reject) { + return fn(...args, function (...args) { + return (args[0] != null ? reject : resolve)(args); + }); + }); + }; + job = new Job$1(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise); + job.promise.then(function (args) { + return typeof cb === "function" ? cb(...args) : void 0; + }).catch(function (args) { + if (Array.isArray(args)) { + return typeof cb === "function" ? cb(...args) : void 0; + } else { + return typeof cb === "function" ? cb(args) : void 0; + } + }); + return this._receive(job); + } + + schedule(...args) { + var job, options, task; + if (typeof args[0] === "function") { + [task, ...args] = args; + options = {}; + } else { + [options, task, ...args] = args; + } + job = new Job$1(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise); + this._receive(job); + return job.promise; + } + + wrap(fn) { + var schedule, wrapped; + schedule = this.schedule.bind(this); + wrapped = function (...args) { + return schedule(fn.bind(this), ...args); + }; + wrapped.withOptions = function (options, ...args) { + return schedule(options, fn, ...args); + }; + return wrapped; + } + + async updateSettings(options = {}) { + await this._store.__updateSettings__(parser$5.overwrite(options, this.storeDefaults)); + parser$5.overwrite(options, this.instanceDefaults, this); + return this; + } + + currentReservoir() { + return this._store.__currentReservoir__(); + } + + incrementReservoir(incr = 0) { + return this._store.__incrementReservoir__(incr); + } + + } + Bottleneck.default = Bottleneck; + + Bottleneck.Events = Events$4; + + Bottleneck.version = Bottleneck.prototype.version = require$$8.version; + + Bottleneck.strategy = Bottleneck.prototype.strategy = { + LEAK: 1, + OVERFLOW: 2, + OVERFLOW_PRIORITY: 4, + BLOCK: 3 + }; + + Bottleneck.BottleneckError = Bottleneck.prototype.BottleneckError = BottleneckError_1; + + Bottleneck.Group = Bottleneck.prototype.Group = Group_1; + + Bottleneck.RedisConnection = Bottleneck.prototype.RedisConnection = require$$2; + + Bottleneck.IORedisConnection = Bottleneck.prototype.IORedisConnection = require$$3; + + Bottleneck.Batcher = Bottleneck.prototype.Batcher = Batcher_1; + + Bottleneck.prototype.jobDefaults = { + priority: DEFAULT_PRIORITY$1, + weight: 1, + expiration: null, + id: "" + }; + + Bottleneck.prototype.storeDefaults = { + maxConcurrent: null, + minTime: 0, + highWater: null, + strategy: Bottleneck.prototype.strategy.LEAK, + penalty: null, + reservoir: null, + reservoirRefreshInterval: null, + reservoirRefreshAmount: null, + reservoirIncreaseInterval: null, + reservoirIncreaseAmount: null, + reservoirIncreaseMaximum: null + }; + + Bottleneck.prototype.localStoreDefaults = { + Promise: Promise, + timeout: null, + heartbeatInterval: 250 + }; + + Bottleneck.prototype.redisStoreDefaults = { + Promise: Promise, + timeout: null, + heartbeatInterval: 5000, + clientTimeout: 10000, + Redis: null, + clientOptions: {}, + clusterNodes: null, + clearDatastore: false, + connection: null + }; + + Bottleneck.prototype.instanceDefaults = { + datastore: "local", + connection: null, + id: "", + rejectOnDrop: true, + trackDoneStatus: false, + Promise: Promise + }; + + Bottleneck.prototype.stopDefaults = { + enqueueErrorMessage: "This limiter has been stopped and cannot accept new jobs.", + dropWaitingJobs: true, + dropErrorMessage: "This limiter has been stopped." + }; + + return Bottleneck; + + }).call(commonjsGlobal); + + var Bottleneck_1 = Bottleneck; + + var lib = Bottleneck_1; + + return lib; + + }))); + + + /***/ +}), + + +}]); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/output.js b/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/output.js new file mode 100644 index 000000000000..c4cb23ea6e5e --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/octokit/static/chunks/357-72bd409f1472b1b8/output.js @@ -0,0 +1,727 @@ +(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ + [ + 357 + ], + { + 1209: function(module, __unused_webpack_exports, __webpack_require__) { + var factory; + factory = function() { + 'use strict'; + var commonjsGlobal = 'undefined' != typeof globalThis ? globalThis : 'undefined' != typeof window ? window : void 0 !== __webpack_require__.g ? __webpack_require__.g : 'undefined' != typeof self ? self : {}, parser = { + load: function(received, defaults, onto = {}) { + var k, ref, v; + for(k in defaults)v = defaults[k], onto[k] = null != (ref = received[k]) ? ref : v; + return onto; + }, + overwrite: function(received, defaults, onto = {}) { + var k, v; + for(k in received)v = received[k], void 0 !== defaults[k] && (onto[k] = v); + return onto; + } + }; + DLList = class { + constructor(incr, decr){ + this.incr = incr, this.decr = decr, this._first = null, this._last = null, this.length = 0; + } + push(value) { + var node; + this.length++, "function" == typeof this.incr && this.incr(), node = { + value, + prev: this._last, + next: null + }, null != this._last ? (this._last.next = node, this._last = node) : this._first = this._last = node; + } + shift() { + var value; + return null == this._first ? void 0 : (this.length--, "function" == typeof this.decr && this.decr(), value = this._first.value, null != (this._first = this._first.next) ? this._first.prev = null : this._last = null, value); + } + first() { + if (null != this._first) return this._first.value; + } + getArray() { + var node, ref, results; + for(node = this._first, results = []; null != node;)results.push((ref = node, node = node.next, ref.value)); + return results; + } + forEachShift(cb) { + var node; + for(node = this.shift(); null != node;)cb(node), node = this.shift(); + } + debug() { + var node, ref, ref1, ref2, results; + for(node = this._first, results = []; null != node;)results.push((ref = node, node = node.next, { + value: ref.value, + prev: null != (ref1 = ref.prev) ? ref1.value : void 0, + next: null != (ref2 = ref.next) ? ref2.value : void 0 + })); + return results; + } + }, Events = class { + constructor(instance){ + if (this.instance = instance, this._events = {}, null != this.instance.on || null != this.instance.once || null != this.instance.removeAllListeners) throw Error("An Emitter already exists for this object"); + this.instance.on = (name, cb)=>this._addListener(name, "many", cb), this.instance.once = (name, cb)=>this._addListener(name, "once", cb), this.instance.removeAllListeners = (name = null)=>null != name ? delete this._events[name] : this._events = {}; + } + _addListener(name, status, cb) { + var base; + return null == (base = this._events)[name] && (base[name] = []), this._events[name].push({ + cb, + status + }), this.instance; + } + listenerCount(name) { + return null != this._events[name] ? this._events[name].length : 0; + } + async trigger(name, ...args) { + var e; + try { + if ("debug" !== name && this.trigger("debug", `Event triggered: ${name}`, args), null == this._events[name]) return; + return this._events[name] = this._events[name].filter(function(listener) { + return "none" !== listener.status; + }), (await Promise.all(this._events[name].map(async (listener)=>{ + var e, returned; + if ("none" !== listener.status) { + "once" === listener.status && (listener.status = "none"); + try { + if (returned = "function" == typeof listener.cb ? listener.cb(...args) : void 0, "function" == typeof (null != returned ? returned.then : void 0)) return await returned; + return returned; + } catch (error) { + return e = error, this.trigger("error", e), null; + } + } + }))).find(function(x) { + return null != x; + }); + } catch (error) { + return e = error, this.trigger("error", e), null; + } + } + }, DLList$1 = DLList, Events$1 = Events, Queues = class { + constructor(num_priorities){ + this.Events = new Events$1(this), this._length = 0, this._lists = (function() { + var j, results; + for(results = [], j = 1; 1 <= num_priorities ? j <= num_priorities : j >= num_priorities; 1 <= num_priorities ? ++j : --j)results.push(new DLList$1(()=>this.incr(), ()=>this.decr())); + return results; + }).call(this); + } + incr() { + if (0 == this._length++) return this.Events.trigger("leftzero"); + } + decr() { + if (0 == --this._length) return this.Events.trigger("zero"); + } + push(job) { + return this._lists[job.options.priority].push(job); + } + queued(priority) { + return null != priority ? this._lists[priority].length : this._length; + } + shiftAll(fn) { + return this._lists.forEach(function(list) { + return list.forEachShift(fn); + }); + } + getFirst(arr = this._lists) { + var j, len, list; + for(j = 0, len = arr.length; j < len; j++)if ((list = arr[j]).length > 0) return list; + return []; + } + shiftLastFrom(priority) { + return this.getFirst(this._lists.slice(priority).reverse()).shift(); + } + }, BottleneckError = class extends Error { + }, NUM_PRIORITIES = 10, DEFAULT_PRIORITY = 5, parser$1 = parser, BottleneckError$1 = BottleneckError, Job = class { + constructor(task, args, options, jobDefaults, rejectOnDrop, Events, _states, Promise1){ + this.task = task, this.args = args, this.rejectOnDrop = rejectOnDrop, this.Events = Events, this._states = _states, this.Promise = Promise1, this.options = parser$1.load(options, jobDefaults), this.options.priority = this._sanitizePriority(this.options.priority), this.options.id === jobDefaults.id && (this.options.id = `${this.options.id}-${this._randomIndex()}`), this.promise = new this.Promise((_resolve, _reject)=>{ + this._resolve = _resolve, this._reject = _reject; + }), this.retryCount = 0; + } + _sanitizePriority(priority) { + var sProperty; + return (sProperty = ~~priority !== priority ? DEFAULT_PRIORITY : priority) < 0 ? 0 : sProperty > NUM_PRIORITIES - 1 ? NUM_PRIORITIES - 1 : sProperty; + } + _randomIndex() { + return Math.random().toString(36).slice(2); + } + doDrop({ error , message ="This job has been dropped by Bottleneck" } = {}) { + return !!this._states.remove(this.options.id) && (this.rejectOnDrop && this._reject(null != error ? error : new BottleneckError$1(message)), this.Events.trigger("dropped", { + args: this.args, + options: this.options, + task: this.task, + promise: this.promise + }), !0); + } + _assertStatus(expected) { + var status; + if (!((status = this._states.jobStatus(this.options.id)) === expected || "DONE" === expected && null === status)) throw new BottleneckError$1(`Invalid job status ${status}, expected ${expected}. Please open an issue at https://github.com/SGrondin/bottleneck/issues`); + } + doReceive() { + return this._states.start(this.options.id), this.Events.trigger("received", { + args: this.args, + options: this.options + }); + } + doQueue(reachedHWM, blocked) { + return this._assertStatus("RECEIVED"), this._states.next(this.options.id), this.Events.trigger("queued", { + args: this.args, + options: this.options, + reachedHWM, + blocked + }); + } + doRun() { + return 0 === this.retryCount ? (this._assertStatus("QUEUED"), this._states.next(this.options.id)) : this._assertStatus("EXECUTING"), this.Events.trigger("scheduled", { + args: this.args, + options: this.options + }); + } + async doExecute(chained, clearGlobalState, run, free) { + var error, eventInfo, passed; + 0 === this.retryCount ? (this._assertStatus("RUNNING"), this._states.next(this.options.id)) : this._assertStatus("EXECUTING"), eventInfo = { + args: this.args, + options: this.options, + retryCount: this.retryCount + }, this.Events.trigger("executing", eventInfo); + try { + if (passed = await (null != chained ? chained.schedule(this.options, this.task, ...this.args) : this.task(...this.args)), clearGlobalState()) return this.doDone(eventInfo), await free(this.options, eventInfo), this._assertStatus("DONE"), this._resolve(passed); + } catch (error1) { + return error = error1, this._onFailure(error, eventInfo, clearGlobalState, run, free); + } + } + doExpire(clearGlobalState, run, free) { + var error, eventInfo; + return this._states.jobStatus("RUNNING" === this.options.id) && this._states.next(this.options.id), this._assertStatus("EXECUTING"), eventInfo = { + args: this.args, + options: this.options, + retryCount: this.retryCount + }, error = new BottleneckError$1(`This job timed out after ${this.options.expiration} ms.`), this._onFailure(error, eventInfo, clearGlobalState, run, free); + } + async _onFailure(error, eventInfo, clearGlobalState, run, free) { + var retry, retryAfter; + if (clearGlobalState()) return null != (retry = await this.Events.trigger("failed", error, eventInfo)) ? (retryAfter = ~~retry, this.Events.trigger("retry", `Retrying ${this.options.id} after ${retryAfter} ms`, eventInfo), this.retryCount++, run(retryAfter)) : (this.doDone(eventInfo), await free(this.options, eventInfo), this._assertStatus("DONE"), this._reject(error)); + } + doDone(eventInfo) { + return this._assertStatus("EXECUTING"), this._states.next(this.options.id), this.Events.trigger("done", eventInfo); + } + }, parser$2 = parser, BottleneckError$2 = BottleneckError, LocalDatastore = class { + constructor(instance, storeOptions, storeInstanceOptions){ + this.instance = instance, this.storeOptions = storeOptions, this.clientId = this.instance._randomIndex(), parser$2.load(storeInstanceOptions, storeInstanceOptions, this), this._nextRequest = this._lastReservoirRefresh = this._lastReservoirIncrease = Date.now(), this._running = 0, this._done = 0, this._unblockTime = 0, this.ready = this.Promise.resolve(), this.clients = {}, this._startHeartbeat(); + } + _startHeartbeat() { + var base; + return null == this.heartbeat && (null != this.storeOptions.reservoirRefreshInterval && null != this.storeOptions.reservoirRefreshAmount || null != this.storeOptions.reservoirIncreaseInterval && null != this.storeOptions.reservoirIncreaseAmount) ? "function" == typeof (base = this.heartbeat = setInterval(()=>{ + var amount, incr, maximum, now, reservoir; + if (now = Date.now(), null != this.storeOptions.reservoirRefreshInterval && now >= this._lastReservoirRefresh + this.storeOptions.reservoirRefreshInterval && (this._lastReservoirRefresh = now, this.storeOptions.reservoir = this.storeOptions.reservoirRefreshAmount, this.instance._drainAll(this.computeCapacity())), null != this.storeOptions.reservoirIncreaseInterval && now >= this._lastReservoirIncrease + this.storeOptions.reservoirIncreaseInterval && ({ reservoirIncreaseAmount: amount , reservoirIncreaseMaximum: maximum , reservoir } = this.storeOptions, this._lastReservoirIncrease = now, (incr = null != maximum ? Math.min(amount, maximum - reservoir) : amount) > 0)) return this.storeOptions.reservoir += incr, this.instance._drainAll(this.computeCapacity()); + }, this.heartbeatInterval)).unref ? base.unref() : void 0 : clearInterval(this.heartbeat); + } + async __publish__(message) { + return await this.yieldLoop(), this.instance.Events.trigger("message", message.toString()); + } + async __disconnect__(flush) { + return await this.yieldLoop(), clearInterval(this.heartbeat), this.Promise.resolve(); + } + yieldLoop(t = 0) { + return new this.Promise(function(resolve, reject) { + return setTimeout(resolve, t); + }); + } + computePenalty() { + var ref; + return null != (ref = this.storeOptions.penalty) ? ref : 15 * this.storeOptions.minTime || 5000; + } + async __updateSettings__(options) { + return await this.yieldLoop(), parser$2.overwrite(options, options, this.storeOptions), this._startHeartbeat(), this.instance._drainAll(this.computeCapacity()), !0; + } + async __running__() { + return await this.yieldLoop(), this._running; + } + async __queued__() { + return await this.yieldLoop(), this.instance.queued(); + } + async __done__() { + return await this.yieldLoop(), this._done; + } + async __groupCheck__(time) { + return await this.yieldLoop(), this._nextRequest + this.timeout < time; + } + computeCapacity() { + var maxConcurrent, reservoir; + return ({ maxConcurrent , reservoir } = this.storeOptions, null != maxConcurrent && null != reservoir) ? Math.min(maxConcurrent - this._running, reservoir) : null != maxConcurrent ? maxConcurrent - this._running : null != reservoir ? reservoir : null; + } + conditionsCheck(weight) { + var capacity; + return null == (capacity = this.computeCapacity()) || weight <= capacity; + } + async __incrementReservoir__(incr) { + var reservoir; + return await this.yieldLoop(), reservoir = this.storeOptions.reservoir += incr, this.instance._drainAll(this.computeCapacity()), reservoir; + } + async __currentReservoir__() { + return await this.yieldLoop(), this.storeOptions.reservoir; + } + isBlocked(now) { + return this._unblockTime >= now; + } + check(weight, now) { + return this.conditionsCheck(weight) && this._nextRequest - now <= 0; + } + async __check__(weight) { + var now; + return await this.yieldLoop(), now = Date.now(), this.check(weight, now); + } + async __register__(index, weight, expiration) { + var now, wait; + return (await this.yieldLoop(), now = Date.now(), this.conditionsCheck(weight)) ? (this._running += weight, null != this.storeOptions.reservoir && (this.storeOptions.reservoir -= weight), wait = Math.max(this._nextRequest - now, 0), this._nextRequest = now + wait + this.storeOptions.minTime, { + success: !0, + wait, + reservoir: this.storeOptions.reservoir + }) : { + success: !1 + }; + } + strategyIsBlock() { + return 3 === this.storeOptions.strategy; + } + async __submit__(queueLength, weight) { + var blocked, now, reachedHWM; + if (await this.yieldLoop(), null != this.storeOptions.maxConcurrent && weight > this.storeOptions.maxConcurrent) throw new BottleneckError$2(`Impossible to add a job having a weight of ${weight} to a limiter having a maxConcurrent setting of ${this.storeOptions.maxConcurrent}`); + return now = Date.now(), reachedHWM = null != this.storeOptions.highWater && queueLength === this.storeOptions.highWater && !this.check(weight, now), (blocked = this.strategyIsBlock() && (reachedHWM || this.isBlocked(now))) && (this._unblockTime = now + this.computePenalty(), this._nextRequest = this._unblockTime + this.storeOptions.minTime, this.instance._dropAllQueued()), { + reachedHWM, + blocked, + strategy: this.storeOptions.strategy + }; + } + async __free__(index, weight) { + return await this.yieldLoop(), this._running -= weight, this._done += weight, this.instance._drainAll(this.computeCapacity()), { + running: this._running + }; + } + }, BottleneckError$3 = BottleneckError, States = class { + constructor(status1){ + this.status = status1, this._jobs = {}, this.counts = this.status.map(function() { + return 0; + }); + } + next(id) { + var current, next; + return (next = (current = this._jobs[id]) + 1, null != current && next < this.status.length) ? (this.counts[current]--, this.counts[next]++, this._jobs[id]++) : null != current ? (this.counts[current]--, delete this._jobs[id]) : void 0; + } + start(id) { + return this._jobs[id] = 0, this.counts[0]++; + } + remove(id) { + var current; + return null != (current = this._jobs[id]) && (this.counts[current]--, delete this._jobs[id]), null != current; + } + jobStatus(id) { + var ref; + return null != (ref = this.status[this._jobs[id]]) ? ref : null; + } + statusJobs(status) { + var k, pos, ref, results; + if (null == status) return Object.keys(this._jobs); + if ((pos = this.status.indexOf(status)) < 0) throw new BottleneckError$3(`status must be one of ${this.status.join(', ')}`); + for(k in ref = this._jobs, results = [], ref)ref[k] === pos && results.push(k); + return results; + } + statusCounts() { + return this.counts.reduce((acc, v, i)=>(acc[this.status[i]] = v, acc), {}); + } + }, DLList$2 = DLList, Sync = class { + constructor(name, Promise1){ + this.schedule = this.schedule.bind(this), this.name = name, this.Promise = Promise1, this._running = 0, this._queue = new DLList$2(); + } + isEmpty() { + return 0 === this._queue.length; + } + async _tryToRun() { + var args, cb, error, reject, resolve, returned, task; + if (this._running < 1 && this._queue.length > 0) return this._running++, { task , args , resolve , reject } = this._queue.shift(), cb = await async function() { + try { + return returned = await task(...args), function() { + return resolve(returned); + }; + } catch (error1) { + return error = error1, function() { + return reject(error); + }; + } + }(), this._running--, this._tryToRun(), cb(); + } + schedule(task, ...args) { + var promise, reject, resolve; + return resolve = reject = null, promise = new this.Promise(function(_resolve, _reject) { + return resolve = _resolve, reject = _reject; + }), this._queue.push({ + task, + args, + resolve, + reject + }), this._tryToRun(), promise; + } + }; + var version = "2.19.5", version$2 = Object.freeze({ + version: version, + default: { + version: version + } + }), require$$2 = ()=>console.log('You must import the full version of Bottleneck in order to use this feature.'), require$$3 = ()=>console.log('You must import the full version of Bottleneck in order to use this feature.'), require$$4 = ()=>console.log('You must import the full version of Bottleneck in order to use this feature.'); + parser$3 = parser, Events$2 = Events, RedisConnection$1 = require$$2, IORedisConnection$1 = require$$3, Scripts$1 = require$$4, Group = (function() { + class Group { + constructor(limiterOptions = {}){ + this.deleteKey = this.deleteKey.bind(this), this.limiterOptions = limiterOptions, parser$3.load(this.limiterOptions, this.defaults, this), this.Events = new Events$2(this), this.instances = {}, this.Bottleneck = Bottleneck_1, this._startAutoCleanup(), this.sharedConnection = null != this.connection, null == this.connection && ("redis" === this.limiterOptions.datastore ? this.connection = new RedisConnection$1(Object.assign({}, this.limiterOptions, { + Events: this.Events + })) : "ioredis" === this.limiterOptions.datastore && (this.connection = new IORedisConnection$1(Object.assign({}, this.limiterOptions, { + Events: this.Events + })))); + } + key(key = "") { + var ref; + return null != (ref = this.instances[key]) ? ref : (()=>{ + var limiter; + return limiter = this.instances[key] = new this.Bottleneck(Object.assign(this.limiterOptions, { + id: `${this.id}-${key}`, + timeout: this.timeout, + connection: this.connection + })), this.Events.trigger("created", limiter, key), limiter; + })(); + } + async deleteKey(key = "") { + var deleted, instance; + return instance = this.instances[key], this.connection && (deleted = await this.connection.__runCommand__([ + 'del', + ...Scripts$1.allKeys(`${this.id}-${key}`) + ])), null != instance && (delete this.instances[key], await instance.disconnect()), null != instance || deleted > 0; + } + limiters() { + var k, ref, results, v; + for(k in ref = this.instances, results = [], ref)v = ref[k], results.push({ + key: k, + limiter: v + }); + return results; + } + keys() { + return Object.keys(this.instances); + } + async clusterKeys() { + var cursor, found, i, k, keys, len, next, start; + if (null == this.connection) return this.Promise.resolve(this.keys()); + for(keys = [], cursor = null, start = `b_${this.id}-`.length; 0 !== cursor;)for(i = 0, [next, found] = await this.connection.__runCommand__([ + "scan", + null != cursor ? cursor : 0, + "match", + `b_${this.id}-*_settings`, + "count", + 10000 + ]), cursor = ~~next, len = found.length; i < len; i++)k = found[i], keys.push(k.slice(start, -9)); + return keys; + } + _startAutoCleanup() { + var base; + return clearInterval(this.interval), "function" == typeof (base = this.interval = setInterval(async ()=>{ + var e, k, ref, results, time, v; + for(k in time = Date.now(), ref = this.instances, results = [], ref){ + v = ref[k]; + try { + await v._store.__groupCheck__(time) ? results.push(this.deleteKey(k)) : results.push(void 0); + } catch (error) { + e = error, results.push(v.Events.trigger("error", e)); + } + } + return results; + }, this.timeout / 2)).unref ? base.unref() : void 0; + } + updateSettings(options = {}) { + if (parser$3.overwrite(options, this.defaults, this), parser$3.overwrite(options, options, this.limiterOptions), null != options.timeout) return this._startAutoCleanup(); + } + disconnect(flush = !0) { + var ref; + if (!this.sharedConnection) return null != (ref = this.connection) ? ref.disconnect(flush) : void 0; + } + } + return Group.prototype.defaults = { + timeout: 300000, + connection: null, + Promise: Promise, + id: "group-key" + }, Group; + }).call(commonjsGlobal), parser$4 = parser, Events$3 = Events, Batcher = (function() { + class Batcher { + constructor(options = {}){ + this.options = options, parser$4.load(this.options, this.defaults, this), this.Events = new Events$3(this), this._arr = [], this._resetPromise(), this._lastFlush = Date.now(); + } + _resetPromise() { + return this._promise = new this.Promise((res, rej)=>this._resolve = res); + } + _flush() { + return clearTimeout(this._timeout), this._lastFlush = Date.now(), this._resolve(), this.Events.trigger("batch", this._arr), this._arr = [], this._resetPromise(); + } + add(data) { + var ret; + return this._arr.push(data), ret = this._promise, this._arr.length === this.maxSize ? this._flush() : null != this.maxTime && 1 === this._arr.length && (this._timeout = setTimeout(()=>this._flush(), this.maxTime)), ret; + } + } + return Batcher.prototype.defaults = { + maxTime: null, + maxSize: null, + Promise: Promise + }, Batcher; + }).call(commonjsGlobal); + var DLList, Events, DLList$1, Events$1, Queues, BottleneckError, BottleneckError$1, DEFAULT_PRIORITY, Job, NUM_PRIORITIES, parser$1, BottleneckError$2, LocalDatastore, parser$2, BottleneckError$3, States, DLList$2, Sync, Events$2, Group, IORedisConnection$1, RedisConnection$1, Scripts$1, parser$3, Batcher, Events$3, parser$4, require$$4$1 = ()=>console.log('You must import the full version of Bottleneck in order to use this feature.'), require$$8 = version$2 && version$2.default || version$2, splice = [].splice, Bottleneck_1 = (function() { + class Bottleneck { + constructor(options = {}, ...invalid){ + var storeInstanceOptions, storeOptions; + this._addToQueue = this._addToQueue.bind(this), this._validateOptions(options, invalid), parser.load(options, this.instanceDefaults, this), this._queues = new Queues(10), this._scheduled = {}, this._states = new States([ + "RECEIVED", + "QUEUED", + "RUNNING", + "EXECUTING" + ].concat(this.trackDoneStatus ? [ + "DONE" + ] : [])), this._limiter = null, this.Events = new Events(this), this._submitLock = new Sync("submit", this.Promise), this._registerLock = new Sync("register", this.Promise), storeOptions = parser.load(options, this.storeDefaults, {}), this._store = (function() { + if ("redis" === this.datastore || "ioredis" === this.datastore || null != this.connection) return storeInstanceOptions = parser.load(options, this.redisStoreDefaults, {}), new require$$4$1(this, storeOptions, storeInstanceOptions); + if ("local" === this.datastore) return storeInstanceOptions = parser.load(options, this.localStoreDefaults, {}), new LocalDatastore(this, storeOptions, storeInstanceOptions); + throw new Bottleneck.prototype.BottleneckError(`Invalid datastore type: ${this.datastore}`); + }).call(this), this._queues.on("leftzero", ()=>{ + var ref; + return null != (ref = this._store.heartbeat) && "function" == typeof ref.ref ? ref.ref() : void 0; + }), this._queues.on("zero", ()=>{ + var ref; + return null != (ref = this._store.heartbeat) && "function" == typeof ref.unref ? ref.unref() : void 0; + }); + } + _validateOptions(options, invalid) { + if (!(null != options && "object" == typeof options && 0 === invalid.length)) throw new Bottleneck.prototype.BottleneckError("Bottleneck v2 takes a single object argument. Refer to https://github.com/SGrondin/bottleneck#upgrading-to-v2 if you're upgrading from Bottleneck v1."); + } + ready() { + return this._store.ready; + } + clients() { + return this._store.clients; + } + channel() { + return `b_${this.id}`; + } + channel_client() { + return `b_${this.id}_${this._store.clientId}`; + } + publish(message) { + return this._store.__publish__(message); + } + disconnect(flush = !0) { + return this._store.__disconnect__(flush); + } + chain(_limiter) { + return this._limiter = _limiter, this; + } + queued(priority) { + return this._queues.queued(priority); + } + clusterQueued() { + return this._store.__queued__(); + } + empty() { + return 0 === this.queued() && this._submitLock.isEmpty(); + } + running() { + return this._store.__running__(); + } + done() { + return this._store.__done__(); + } + jobStatus(id) { + return this._states.jobStatus(id); + } + jobs(status) { + return this._states.statusJobs(status); + } + counts() { + return this._states.statusCounts(); + } + _randomIndex() { + return Math.random().toString(36).slice(2); + } + check(weight = 1) { + return this._store.__check__(weight); + } + _clearGlobalState(index) { + return null != this._scheduled[index] && (clearTimeout(this._scheduled[index].expiration), delete this._scheduled[index], !0); + } + async _free(index, job, options, eventInfo) { + var e, running; + try { + if ({ running } = await this._store.__free__(index, options.weight), this.Events.trigger("debug", `Freed ${options.id}`, eventInfo), 0 === running && this.empty()) return this.Events.trigger("idle"); + } catch (error1) { + return e = error1, this.Events.trigger("error", e); + } + } + _run(index, job, wait) { + var clearGlobalState, free, run; + return job.doRun(), clearGlobalState = this._clearGlobalState.bind(this, index), run = this._run.bind(this, index, job), free = this._free.bind(this, index, job), this._scheduled[index] = { + timeout: setTimeout(()=>job.doExecute(this._limiter, clearGlobalState, run, free), wait), + expiration: null != job.options.expiration ? setTimeout(function() { + return job.doExpire(clearGlobalState, run, free); + }, wait + job.options.expiration) : void 0, + job: job + }; + } + _drainOne(capacity) { + return this._registerLock.schedule(()=>{ + var args, index, next, options, queue; + return 0 === this.queued() ? this.Promise.resolve(null) : (queue = this._queues.getFirst(), { options , args } = next = queue.first(), null != capacity && options.weight > capacity) ? this.Promise.resolve(null) : (this.Events.trigger("debug", `Draining ${options.id}`, { + args, + options + }), index = this._randomIndex(), this._store.__register__(index, options.weight, options.expiration).then(({ success , wait , reservoir })=>{ + var empty; + return (this.Events.trigger("debug", `Drained ${options.id}`, { + success, + args, + options + }), success) ? (queue.shift(), (empty = this.empty()) && this.Events.trigger("empty"), 0 === reservoir && this.Events.trigger("depleted", empty), this._run(index, next, wait), this.Promise.resolve(options.weight)) : this.Promise.resolve(null); + })); + }); + } + _drainAll(capacity, total = 0) { + return this._drainOne(capacity).then((drained)=>null != drained ? this._drainAll(null != capacity ? capacity - drained : capacity, total + drained) : this.Promise.resolve(total)).catch((e)=>this.Events.trigger("error", e)); + } + _dropAllQueued(message) { + return this._queues.shiftAll(function(job) { + return job.doDrop({ + message + }); + }); + } + stop(options = {}) { + var done, waitForExecuting; + return options = parser.load(options, this.stopDefaults), waitForExecuting = (at)=>{ + var finished; + return finished = ()=>{ + var counts; + return (counts = this._states.counts)[0] + counts[1] + counts[2] + counts[3] === at; + }, new this.Promise((resolve, reject)=>finished() ? resolve() : this.on("done", ()=>{ + if (finished()) return this.removeAllListeners("done"), resolve(); + })); + }, done = options.dropWaitingJobs ? (this._run = function(index, next) { + return next.doDrop({ + message: options.dropErrorMessage + }); + }, this._drainOne = ()=>this.Promise.resolve(null), this._registerLock.schedule(()=>this._submitLock.schedule(()=>{ + var k, ref, v; + for(k in ref = this._scheduled)v = ref[k], "RUNNING" === this.jobStatus(v.job.options.id) && (clearTimeout(v.timeout), clearTimeout(v.expiration), v.job.doDrop({ + message: options.dropErrorMessage + })); + return this._dropAllQueued(options.dropErrorMessage), waitForExecuting(0); + }))) : this.schedule({ + priority: 9, + weight: 0 + }, ()=>waitForExecuting(1)), this._receive = function(job) { + return job._reject(new Bottleneck.prototype.BottleneckError(options.enqueueErrorMessage)); + }, this.stop = ()=>this.Promise.reject(new Bottleneck.prototype.BottleneckError("stop() has already been called")), done; + } + async _addToQueue(job) { + var args, blocked, error, options, reachedHWM, shifted, strategy; + ({ args , options } = job); + try { + ({ reachedHWM , blocked , strategy } = await this._store.__submit__(this.queued(), options.weight)); + } catch (error1) { + return error = error1, this.Events.trigger("debug", `Could not queue ${options.id}`, { + args, + options, + error + }), job.doDrop({ + error + }), !1; + } + return blocked ? (job.doDrop(), !0) : reachedHWM && (null != (shifted = strategy === Bottleneck.prototype.strategy.LEAK ? this._queues.shiftLastFrom(options.priority) : strategy === Bottleneck.prototype.strategy.OVERFLOW_PRIORITY ? this._queues.shiftLastFrom(options.priority + 1) : strategy === Bottleneck.prototype.strategy.OVERFLOW ? job : void 0) && shifted.doDrop(), null == shifted || strategy === Bottleneck.prototype.strategy.OVERFLOW) ? (null == shifted && job.doDrop(), reachedHWM) : (job.doQueue(reachedHWM, blocked), this._queues.push(job), await this._drainAll(), reachedHWM); + } + _receive(job) { + return null != this._states.jobStatus(job.options.id) ? (job._reject(new Bottleneck.prototype.BottleneckError(`A job with the same id already exists (id=${job.options.id})`)), !1) : (job.doReceive(), this._submitLock.schedule(this._addToQueue, job)); + } + submit(...args) { + var cb, fn, job, options, ref, ref1, task; + return "function" == typeof args[0] ? (ref = args, [fn, ...args] = ref, [cb] = splice.call(args, -1), options = parser.load({}, this.jobDefaults)) : (ref1 = args, [options, fn, ...args] = ref1, [cb] = splice.call(args, -1), options = parser.load(options, this.jobDefaults)), task = (...args)=>new this.Promise(function(resolve, reject) { + return fn(...args, function(...args) { + return (null != args[0] ? reject : resolve)(args); + }); + }), (job = new Job(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise)).promise.then(function(args) { + return "function" == typeof cb ? cb(...args) : void 0; + }).catch(function(args) { + return Array.isArray(args) ? "function" == typeof cb ? cb(...args) : void 0 : "function" == typeof cb ? cb(args) : void 0; + }), this._receive(job); + } + schedule(...args) { + var job, options, task; + return "function" == typeof args[0] ? ([task, ...args] = args, options = {}) : [options, task, ...args] = args, job = new Job(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise), this._receive(job), job.promise; + } + wrap(fn) { + var schedule, wrapped; + return schedule = this.schedule.bind(this), (wrapped = function(...args) { + return schedule(fn.bind(this), ...args); + }).withOptions = function(options, ...args) { + return schedule(options, fn, ...args); + }, wrapped; + } + async updateSettings(options = {}) { + return await this._store.__updateSettings__(parser.overwrite(options, this.storeDefaults)), parser.overwrite(options, this.instanceDefaults, this), this; + } + currentReservoir() { + return this._store.__currentReservoir__(); + } + incrementReservoir(incr = 0) { + return this._store.__incrementReservoir__(incr); + } + } + return Bottleneck.default = Bottleneck, Bottleneck.Events = Events, Bottleneck.version = Bottleneck.prototype.version = require$$8.version, Bottleneck.strategy = Bottleneck.prototype.strategy = { + LEAK: 1, + OVERFLOW: 2, + OVERFLOW_PRIORITY: 4, + BLOCK: 3 + }, Bottleneck.BottleneckError = Bottleneck.prototype.BottleneckError = BottleneckError, Bottleneck.Group = Bottleneck.prototype.Group = Group, Bottleneck.RedisConnection = Bottleneck.prototype.RedisConnection = require$$2, Bottleneck.IORedisConnection = Bottleneck.prototype.IORedisConnection = require$$3, Bottleneck.Batcher = Bottleneck.prototype.Batcher = Batcher, Bottleneck.prototype.jobDefaults = { + priority: 5, + weight: 1, + expiration: null, + id: "" + }, Bottleneck.prototype.storeDefaults = { + maxConcurrent: null, + minTime: 0, + highWater: null, + strategy: Bottleneck.prototype.strategy.LEAK, + penalty: null, + reservoir: null, + reservoirRefreshInterval: null, + reservoirRefreshAmount: null, + reservoirIncreaseInterval: null, + reservoirIncreaseAmount: null, + reservoirIncreaseMaximum: null + }, Bottleneck.prototype.localStoreDefaults = { + Promise: Promise, + timeout: null, + heartbeatInterval: 250 + }, Bottleneck.prototype.redisStoreDefaults = { + Promise: Promise, + timeout: null, + heartbeatInterval: 5000, + clientTimeout: 10000, + Redis: null, + clientOptions: {}, + clusterNodes: null, + clearDatastore: !1, + connection: null + }, Bottleneck.prototype.instanceDefaults = { + datastore: "local", + connection: null, + id: "", + rejectOnDrop: !0, + trackDoneStatus: !1, + Promise: Promise + }, Bottleneck.prototype.stopDefaults = { + enqueueErrorMessage: "This limiter has been stopped and cannot accept new jobs.", + dropWaitingJobs: !0, + dropErrorMessage: "This limiter has been stopped." + }, Bottleneck; + }).call(commonjsGlobal); + return Bottleneck_1; + }, module.exports = factory(); + } + } +]);