UNPKG

6.13 kBJavaScriptView Raw
1"use strict";
2
3function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
4
5function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
6
7function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
8
9function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
10
11function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
12
13function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
14
15var Events, Group, IORedisConnection, RedisConnection, Scripts, parser;
16parser = require("./parser");
17Events = require("./Events");
18RedisConnection = require("./RedisConnection");
19IORedisConnection = require("./IORedisConnection");
20Scripts = require("./Scripts");
21
22Group = function () {
23 class Group {
24 constructor(limiterOptions = {}) {
25 this.deleteKey = this.deleteKey.bind(this);
26 this.limiterOptions = limiterOptions;
27 parser.load(this.limiterOptions, this.defaults, this);
28 this.Events = new Events(this);
29 this.instances = {};
30 this.Bottleneck = require("./Bottleneck");
31
32 this._startAutoCleanup();
33
34 this.sharedConnection = this.connection != null;
35
36 if (this.connection == null) {
37 if (this.limiterOptions.datastore === "redis") {
38 this.connection = new RedisConnection(Object.assign({}, this.limiterOptions, {
39 Events: this.Events
40 }));
41 } else if (this.limiterOptions.datastore === "ioredis") {
42 this.connection = new IORedisConnection(Object.assign({}, this.limiterOptions, {
43 Events: this.Events
44 }));
45 }
46 }
47 }
48
49 key(key = "") {
50 var ref;
51 return (ref = this.instances[key]) != null ? ref : (() => {
52 var limiter;
53 limiter = this.instances[key] = new this.Bottleneck(Object.assign(this.limiterOptions, {
54 id: `${this.id}-${key}`,
55 timeout: this.timeout,
56 connection: this.connection
57 }));
58 this.Events.trigger("created", limiter, key);
59 return limiter;
60 })();
61 }
62
63 deleteKey(key = "") {
64 var _this = this;
65
66 return _asyncToGenerator(function* () {
67 var deleted, instance;
68 instance = _this.instances[key];
69
70 if (_this.connection) {
71 deleted = yield _this.connection.__runCommand__(['del', ...Scripts.allKeys(`${_this.id}-${key}`)]);
72 }
73
74 if (instance != null) {
75 delete _this.instances[key];
76 yield instance.disconnect();
77 }
78
79 return instance != null || deleted > 0;
80 })();
81 }
82
83 limiters() {
84 var k, ref, results, v;
85 ref = this.instances;
86 results = [];
87
88 for (k in ref) {
89 v = ref[k];
90 results.push({
91 key: k,
92 limiter: v
93 });
94 }
95
96 return results;
97 }
98
99 keys() {
100 return Object.keys(this.instances);
101 }
102
103 clusterKeys() {
104 var _this2 = this;
105
106 return _asyncToGenerator(function* () {
107 var cursor, end, found, i, k, keys, len, next, start;
108
109 if (_this2.connection == null) {
110 return _this2.Promise.resolve(_this2.keys());
111 }
112
113 keys = [];
114 cursor = null;
115 start = `b_${_this2.id}-`.length;
116 end = "_settings".length;
117
118 while (cursor !== 0) {
119 var _ref = yield _this2.connection.__runCommand__(["scan", cursor != null ? cursor : 0, "match", `b_${_this2.id}-*_settings`, "count", 10000]);
120
121 var _ref2 = _slicedToArray(_ref, 2);
122
123 next = _ref2[0];
124 found = _ref2[1];
125 cursor = ~~next;
126
127 for (i = 0, len = found.length; i < len; i++) {
128 k = found[i];
129 keys.push(k.slice(start, -end));
130 }
131 }
132
133 return keys;
134 })();
135 }
136
137 _startAutoCleanup() {
138 var _this3 = this;
139
140 var base;
141 clearInterval(this.interval);
142 return typeof (base = this.interval = setInterval(
143 /*#__PURE__*/
144 _asyncToGenerator(function* () {
145 var e, k, ref, results, time, v;
146 time = Date.now();
147 ref = _this3.instances;
148 results = [];
149
150 for (k in ref) {
151 v = ref[k];
152
153 try {
154 if (yield v._store.__groupCheck__(time)) {
155 results.push(_this3.deleteKey(k));
156 } else {
157 results.push(void 0);
158 }
159 } catch (error) {
160 e = error;
161 results.push(v.Events.trigger("error", e));
162 }
163 }
164
165 return results;
166 }), this.timeout / 2)).unref === "function" ? base.unref() : void 0;
167 }
168
169 updateSettings(options = {}) {
170 parser.overwrite(options, this.defaults, this);
171 parser.overwrite(options, options, this.limiterOptions);
172
173 if (options.timeout != null) {
174 return this._startAutoCleanup();
175 }
176 }
177
178 disconnect(flush = true) {
179 var ref;
180
181 if (!this.sharedConnection) {
182 return (ref = this.connection) != null ? ref.disconnect(flush) : void 0;
183 }
184 }
185
186 }
187
188 ;
189 Group.prototype.defaults = {
190 timeout: 1000 * 60 * 5,
191 connection: null,
192 Promise: Promise,
193 id: "group-key"
194 };
195 return Group;
196}.call(void 0);
197
198module.exports = Group;
\No newline at end of file