wechaty-web-panel 1.5.4 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/src/lib/chatGPT.js +2 -2
- package/dist/cjs/src/lib/quick-lru.d.ts +33 -0
- package/dist/cjs/src/lib/quick-lru.js +240 -0
- package/dist/cjs/src/package-json.js +1 -1
- package/dist/esm/src/lib/chatGPT.js +1 -1
- package/dist/esm/src/lib/quick-lru.d.ts +33 -0
- package/dist/esm/src/lib/quick-lru.js +237 -0
- package/dist/esm/src/package-json.js +1 -1
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.openai = exports.ChatGPTUnofficialProxyAPI = exports.ChatGPTError = exports.ChatGPTAPI = void 0;
|
|
7
7
|
const keyv_1 = __importDefault(require("keyv"));
|
|
8
8
|
const pTimeout_js_1 = __importDefault(require("./pTimeout.js"));
|
|
9
|
-
const
|
|
9
|
+
const quick_lru_js_1 = __importDefault(require("./quick-lru.js"));
|
|
10
10
|
const uuid_1 = require("uuid");
|
|
11
11
|
const tiktoken_1 = require("@dqbd/tiktoken");
|
|
12
12
|
const eventsource_parser_1 = require("eventsource-parser");
|
|
@@ -135,7 +135,7 @@ Current date: ${currentDate}`;
|
|
|
135
135
|
}
|
|
136
136
|
else {
|
|
137
137
|
this._messageStore = new keyv_1.default({
|
|
138
|
-
store: new
|
|
138
|
+
store: new quick_lru_js_1.default({ maxSize: 1e4 })
|
|
139
139
|
});
|
|
140
140
|
}
|
|
141
141
|
if (!this._apiKey) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export default class QuickLRU extends Map<any, any> {
|
|
2
|
+
constructor(options?: {});
|
|
3
|
+
maxSize: any;
|
|
4
|
+
maxAge: any;
|
|
5
|
+
onEviction: any;
|
|
6
|
+
cache: Map<any, any>;
|
|
7
|
+
oldCache: Map<any, any>;
|
|
8
|
+
_size: number;
|
|
9
|
+
_emitEvictions(cache: any): void;
|
|
10
|
+
_deleteIfExpired(key: any, item: any): boolean;
|
|
11
|
+
_getOrDeleteIfExpired(key: any, item: any): any;
|
|
12
|
+
_getItemValue(key: any, item: any): any;
|
|
13
|
+
_peek(key: any, cache: any): any;
|
|
14
|
+
_set(key: any, value: any): void;
|
|
15
|
+
_moveToRecent(key: any, item: any): void;
|
|
16
|
+
_entriesAscending(): Generator<[any, any], void, unknown>;
|
|
17
|
+
get(key: any): any;
|
|
18
|
+
set(key: any, value: any, { maxAge }?: {
|
|
19
|
+
maxAge?: any;
|
|
20
|
+
}): void;
|
|
21
|
+
has(key: any): boolean;
|
|
22
|
+
peek(key: any): any;
|
|
23
|
+
delete(key: any): boolean;
|
|
24
|
+
resize(newSize: any): void;
|
|
25
|
+
keys(): Generator<any, void, unknown>;
|
|
26
|
+
values(): Generator<any, void, unknown>;
|
|
27
|
+
entriesDescending(): Generator<any[], void, unknown>;
|
|
28
|
+
entriesAscending(): Generator<any[], void, unknown>;
|
|
29
|
+
entries(): Generator<any[], void, unknown>;
|
|
30
|
+
forEach(callbackFunction: any, thisArgument?: QuickLRU): void;
|
|
31
|
+
[Symbol.iterator](): Generator<any[], void, unknown>;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=quick-lru.d.ts.map
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class QuickLRU extends Map {
|
|
4
|
+
constructor(options = {}) {
|
|
5
|
+
super();
|
|
6
|
+
if (!(options.maxSize && options.maxSize > 0)) {
|
|
7
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
8
|
+
}
|
|
9
|
+
if (typeof options.maxAge === 'number' && options.maxAge === 0) {
|
|
10
|
+
throw new TypeError('`maxAge` must be a number greater than 0');
|
|
11
|
+
}
|
|
12
|
+
// TODO: Use private class fields when ESLint supports them.
|
|
13
|
+
this.maxSize = options.maxSize;
|
|
14
|
+
this.maxAge = options.maxAge || Number.POSITIVE_INFINITY;
|
|
15
|
+
this.onEviction = options.onEviction;
|
|
16
|
+
this.cache = new Map();
|
|
17
|
+
this.oldCache = new Map();
|
|
18
|
+
this._size = 0;
|
|
19
|
+
}
|
|
20
|
+
// TODO: Use private class methods when targeting Node.js 16.
|
|
21
|
+
_emitEvictions(cache) {
|
|
22
|
+
if (typeof this.onEviction !== 'function') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
for (const [key, item] of cache) {
|
|
26
|
+
this.onEviction(key, item.value);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
_deleteIfExpired(key, item) {
|
|
30
|
+
if (typeof item.expiry === 'number' && item.expiry <= Date.now()) {
|
|
31
|
+
if (typeof this.onEviction === 'function') {
|
|
32
|
+
this.onEviction(key, item.value);
|
|
33
|
+
}
|
|
34
|
+
return this.delete(key);
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
_getOrDeleteIfExpired(key, item) {
|
|
39
|
+
const deleted = this._deleteIfExpired(key, item);
|
|
40
|
+
if (deleted === false) {
|
|
41
|
+
return item.value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
_getItemValue(key, item) {
|
|
45
|
+
return item.expiry ? this._getOrDeleteIfExpired(key, item) : item.value;
|
|
46
|
+
}
|
|
47
|
+
_peek(key, cache) {
|
|
48
|
+
const item = cache.get(key);
|
|
49
|
+
return this._getItemValue(key, item);
|
|
50
|
+
}
|
|
51
|
+
_set(key, value) {
|
|
52
|
+
this.cache.set(key, value);
|
|
53
|
+
this._size++;
|
|
54
|
+
if (this._size >= this.maxSize) {
|
|
55
|
+
this._size = 0;
|
|
56
|
+
this._emitEvictions(this.oldCache);
|
|
57
|
+
this.oldCache = this.cache;
|
|
58
|
+
this.cache = new Map();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
_moveToRecent(key, item) {
|
|
62
|
+
this.oldCache.delete(key);
|
|
63
|
+
this._set(key, item);
|
|
64
|
+
}
|
|
65
|
+
*_entriesAscending() {
|
|
66
|
+
for (const item of this.oldCache) {
|
|
67
|
+
const [key, value] = item;
|
|
68
|
+
if (!this.cache.has(key)) {
|
|
69
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
70
|
+
if (deleted === false) {
|
|
71
|
+
yield item;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const item of this.cache) {
|
|
76
|
+
const [key, value] = item;
|
|
77
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
78
|
+
if (deleted === false) {
|
|
79
|
+
yield item;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
get(key) {
|
|
84
|
+
if (this.cache.has(key)) {
|
|
85
|
+
const item = this.cache.get(key);
|
|
86
|
+
return this._getItemValue(key, item);
|
|
87
|
+
}
|
|
88
|
+
if (this.oldCache.has(key)) {
|
|
89
|
+
const item = this.oldCache.get(key);
|
|
90
|
+
if (this._deleteIfExpired(key, item) === false) {
|
|
91
|
+
this._moveToRecent(key, item);
|
|
92
|
+
return item.value;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
set(key, value, { maxAge = this.maxAge } = {}) {
|
|
97
|
+
const expiry = typeof maxAge === 'number' && maxAge !== Number.POSITIVE_INFINITY ?
|
|
98
|
+
Date.now() + maxAge :
|
|
99
|
+
undefined;
|
|
100
|
+
if (this.cache.has(key)) {
|
|
101
|
+
this.cache.set(key, {
|
|
102
|
+
value,
|
|
103
|
+
expiry
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this._set(key, { value, expiry });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
has(key) {
|
|
111
|
+
if (this.cache.has(key)) {
|
|
112
|
+
return !this._deleteIfExpired(key, this.cache.get(key));
|
|
113
|
+
}
|
|
114
|
+
if (this.oldCache.has(key)) {
|
|
115
|
+
return !this._deleteIfExpired(key, this.oldCache.get(key));
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
peek(key) {
|
|
120
|
+
if (this.cache.has(key)) {
|
|
121
|
+
return this._peek(key, this.cache);
|
|
122
|
+
}
|
|
123
|
+
if (this.oldCache.has(key)) {
|
|
124
|
+
return this._peek(key, this.oldCache);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
delete(key) {
|
|
128
|
+
const deleted = this.cache.delete(key);
|
|
129
|
+
if (deleted) {
|
|
130
|
+
this._size--;
|
|
131
|
+
}
|
|
132
|
+
return this.oldCache.delete(key) || deleted;
|
|
133
|
+
}
|
|
134
|
+
clear() {
|
|
135
|
+
this.cache.clear();
|
|
136
|
+
this.oldCache.clear();
|
|
137
|
+
this._size = 0;
|
|
138
|
+
}
|
|
139
|
+
resize(newSize) {
|
|
140
|
+
if (!(newSize && newSize > 0)) {
|
|
141
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
142
|
+
}
|
|
143
|
+
const items = [...this._entriesAscending()];
|
|
144
|
+
const removeCount = items.length - newSize;
|
|
145
|
+
if (removeCount < 0) {
|
|
146
|
+
this.cache = new Map(items);
|
|
147
|
+
this.oldCache = new Map();
|
|
148
|
+
this._size = items.length;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
if (removeCount > 0) {
|
|
152
|
+
this._emitEvictions(items.slice(0, removeCount));
|
|
153
|
+
}
|
|
154
|
+
this.oldCache = new Map(items.slice(removeCount));
|
|
155
|
+
this.cache = new Map();
|
|
156
|
+
this._size = 0;
|
|
157
|
+
}
|
|
158
|
+
this.maxSize = newSize;
|
|
159
|
+
}
|
|
160
|
+
*keys() {
|
|
161
|
+
for (const [key] of this) {
|
|
162
|
+
yield key;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
*values() {
|
|
166
|
+
for (const [, value] of this) {
|
|
167
|
+
yield value;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
*[Symbol.iterator]() {
|
|
171
|
+
for (const item of this.cache) {
|
|
172
|
+
const [key, value] = item;
|
|
173
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
174
|
+
if (deleted === false) {
|
|
175
|
+
yield [key, value.value];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const item of this.oldCache) {
|
|
179
|
+
const [key, value] = item;
|
|
180
|
+
if (!this.cache.has(key)) {
|
|
181
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
182
|
+
if (deleted === false) {
|
|
183
|
+
yield [key, value.value];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
*entriesDescending() {
|
|
189
|
+
let items = [...this.cache];
|
|
190
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
191
|
+
const item = items[i];
|
|
192
|
+
const [key, value] = item;
|
|
193
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
194
|
+
if (deleted === false) {
|
|
195
|
+
yield [key, value.value];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
items = [...this.oldCache];
|
|
199
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
200
|
+
const item = items[i];
|
|
201
|
+
const [key, value] = item;
|
|
202
|
+
if (!this.cache.has(key)) {
|
|
203
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
204
|
+
if (deleted === false) {
|
|
205
|
+
yield [key, value.value];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
*entriesAscending() {
|
|
211
|
+
for (const [key, value] of this._entriesAscending()) {
|
|
212
|
+
yield [key, value.value];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
get size() {
|
|
216
|
+
if (!this._size) {
|
|
217
|
+
return this.oldCache.size;
|
|
218
|
+
}
|
|
219
|
+
let oldCacheSize = 0;
|
|
220
|
+
for (const key of this.oldCache.keys()) {
|
|
221
|
+
if (!this.cache.has(key)) {
|
|
222
|
+
oldCacheSize++;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return Math.min(this._size + oldCacheSize, this.maxSize);
|
|
226
|
+
}
|
|
227
|
+
entries() {
|
|
228
|
+
return this.entriesAscending();
|
|
229
|
+
}
|
|
230
|
+
forEach(callbackFunction, thisArgument = this) {
|
|
231
|
+
for (const [key, value] of this.entriesAscending()) {
|
|
232
|
+
callbackFunction.call(thisArgument, value, key, this);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
get [Symbol.toStringTag]() {
|
|
236
|
+
return JSON.stringify([...this.entriesAscending()]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.default = QuickLRU;
|
|
240
|
+
//# sourceMappingURL=quick-lru.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Keyv from "keyv";
|
|
2
2
|
import pTimeout from "./pTimeout.js";
|
|
3
|
-
import QuickLRU from "quick-lru";
|
|
3
|
+
import QuickLRU from "./quick-lru.js";
|
|
4
4
|
import { v4 as uuidv4 } from "uuid";
|
|
5
5
|
import { get_encoding } from "@dqbd/tiktoken";
|
|
6
6
|
import { createParser } from "eventsource-parser";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export default class QuickLRU extends Map<any, any> {
|
|
2
|
+
constructor(options?: {});
|
|
3
|
+
maxSize: any;
|
|
4
|
+
maxAge: any;
|
|
5
|
+
onEviction: any;
|
|
6
|
+
cache: Map<any, any>;
|
|
7
|
+
oldCache: Map<any, any>;
|
|
8
|
+
_size: number;
|
|
9
|
+
_emitEvictions(cache: any): void;
|
|
10
|
+
_deleteIfExpired(key: any, item: any): boolean;
|
|
11
|
+
_getOrDeleteIfExpired(key: any, item: any): any;
|
|
12
|
+
_getItemValue(key: any, item: any): any;
|
|
13
|
+
_peek(key: any, cache: any): any;
|
|
14
|
+
_set(key: any, value: any): void;
|
|
15
|
+
_moveToRecent(key: any, item: any): void;
|
|
16
|
+
_entriesAscending(): Generator<[any, any], void, unknown>;
|
|
17
|
+
get(key: any): any;
|
|
18
|
+
set(key: any, value: any, { maxAge }?: {
|
|
19
|
+
maxAge?: any;
|
|
20
|
+
}): void;
|
|
21
|
+
has(key: any): boolean;
|
|
22
|
+
peek(key: any): any;
|
|
23
|
+
delete(key: any): boolean;
|
|
24
|
+
resize(newSize: any): void;
|
|
25
|
+
keys(): Generator<any, void, unknown>;
|
|
26
|
+
values(): Generator<any, void, unknown>;
|
|
27
|
+
entriesDescending(): Generator<any[], void, unknown>;
|
|
28
|
+
entriesAscending(): Generator<any[], void, unknown>;
|
|
29
|
+
entries(): Generator<any[], void, unknown>;
|
|
30
|
+
forEach(callbackFunction: any, thisArgument?: QuickLRU): void;
|
|
31
|
+
[Symbol.iterator](): Generator<any[], void, unknown>;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=quick-lru.d.ts.map
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
export default class QuickLRU extends Map {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
super();
|
|
4
|
+
if (!(options.maxSize && options.maxSize > 0)) {
|
|
5
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
6
|
+
}
|
|
7
|
+
if (typeof options.maxAge === 'number' && options.maxAge === 0) {
|
|
8
|
+
throw new TypeError('`maxAge` must be a number greater than 0');
|
|
9
|
+
}
|
|
10
|
+
// TODO: Use private class fields when ESLint supports them.
|
|
11
|
+
this.maxSize = options.maxSize;
|
|
12
|
+
this.maxAge = options.maxAge || Number.POSITIVE_INFINITY;
|
|
13
|
+
this.onEviction = options.onEviction;
|
|
14
|
+
this.cache = new Map();
|
|
15
|
+
this.oldCache = new Map();
|
|
16
|
+
this._size = 0;
|
|
17
|
+
}
|
|
18
|
+
// TODO: Use private class methods when targeting Node.js 16.
|
|
19
|
+
_emitEvictions(cache) {
|
|
20
|
+
if (typeof this.onEviction !== 'function') {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
for (const [key, item] of cache) {
|
|
24
|
+
this.onEviction(key, item.value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
_deleteIfExpired(key, item) {
|
|
28
|
+
if (typeof item.expiry === 'number' && item.expiry <= Date.now()) {
|
|
29
|
+
if (typeof this.onEviction === 'function') {
|
|
30
|
+
this.onEviction(key, item.value);
|
|
31
|
+
}
|
|
32
|
+
return this.delete(key);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
_getOrDeleteIfExpired(key, item) {
|
|
37
|
+
const deleted = this._deleteIfExpired(key, item);
|
|
38
|
+
if (deleted === false) {
|
|
39
|
+
return item.value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
_getItemValue(key, item) {
|
|
43
|
+
return item.expiry ? this._getOrDeleteIfExpired(key, item) : item.value;
|
|
44
|
+
}
|
|
45
|
+
_peek(key, cache) {
|
|
46
|
+
const item = cache.get(key);
|
|
47
|
+
return this._getItemValue(key, item);
|
|
48
|
+
}
|
|
49
|
+
_set(key, value) {
|
|
50
|
+
this.cache.set(key, value);
|
|
51
|
+
this._size++;
|
|
52
|
+
if (this._size >= this.maxSize) {
|
|
53
|
+
this._size = 0;
|
|
54
|
+
this._emitEvictions(this.oldCache);
|
|
55
|
+
this.oldCache = this.cache;
|
|
56
|
+
this.cache = new Map();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
_moveToRecent(key, item) {
|
|
60
|
+
this.oldCache.delete(key);
|
|
61
|
+
this._set(key, item);
|
|
62
|
+
}
|
|
63
|
+
*_entriesAscending() {
|
|
64
|
+
for (const item of this.oldCache) {
|
|
65
|
+
const [key, value] = item;
|
|
66
|
+
if (!this.cache.has(key)) {
|
|
67
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
68
|
+
if (deleted === false) {
|
|
69
|
+
yield item;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const item of this.cache) {
|
|
74
|
+
const [key, value] = item;
|
|
75
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
76
|
+
if (deleted === false) {
|
|
77
|
+
yield item;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
get(key) {
|
|
82
|
+
if (this.cache.has(key)) {
|
|
83
|
+
const item = this.cache.get(key);
|
|
84
|
+
return this._getItemValue(key, item);
|
|
85
|
+
}
|
|
86
|
+
if (this.oldCache.has(key)) {
|
|
87
|
+
const item = this.oldCache.get(key);
|
|
88
|
+
if (this._deleteIfExpired(key, item) === false) {
|
|
89
|
+
this._moveToRecent(key, item);
|
|
90
|
+
return item.value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
set(key, value, { maxAge = this.maxAge } = {}) {
|
|
95
|
+
const expiry = typeof maxAge === 'number' && maxAge !== Number.POSITIVE_INFINITY ?
|
|
96
|
+
Date.now() + maxAge :
|
|
97
|
+
undefined;
|
|
98
|
+
if (this.cache.has(key)) {
|
|
99
|
+
this.cache.set(key, {
|
|
100
|
+
value,
|
|
101
|
+
expiry
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
this._set(key, { value, expiry });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
has(key) {
|
|
109
|
+
if (this.cache.has(key)) {
|
|
110
|
+
return !this._deleteIfExpired(key, this.cache.get(key));
|
|
111
|
+
}
|
|
112
|
+
if (this.oldCache.has(key)) {
|
|
113
|
+
return !this._deleteIfExpired(key, this.oldCache.get(key));
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
peek(key) {
|
|
118
|
+
if (this.cache.has(key)) {
|
|
119
|
+
return this._peek(key, this.cache);
|
|
120
|
+
}
|
|
121
|
+
if (this.oldCache.has(key)) {
|
|
122
|
+
return this._peek(key, this.oldCache);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
delete(key) {
|
|
126
|
+
const deleted = this.cache.delete(key);
|
|
127
|
+
if (deleted) {
|
|
128
|
+
this._size--;
|
|
129
|
+
}
|
|
130
|
+
return this.oldCache.delete(key) || deleted;
|
|
131
|
+
}
|
|
132
|
+
clear() {
|
|
133
|
+
this.cache.clear();
|
|
134
|
+
this.oldCache.clear();
|
|
135
|
+
this._size = 0;
|
|
136
|
+
}
|
|
137
|
+
resize(newSize) {
|
|
138
|
+
if (!(newSize && newSize > 0)) {
|
|
139
|
+
throw new TypeError('`maxSize` must be a number greater than 0');
|
|
140
|
+
}
|
|
141
|
+
const items = [...this._entriesAscending()];
|
|
142
|
+
const removeCount = items.length - newSize;
|
|
143
|
+
if (removeCount < 0) {
|
|
144
|
+
this.cache = new Map(items);
|
|
145
|
+
this.oldCache = new Map();
|
|
146
|
+
this._size = items.length;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
if (removeCount > 0) {
|
|
150
|
+
this._emitEvictions(items.slice(0, removeCount));
|
|
151
|
+
}
|
|
152
|
+
this.oldCache = new Map(items.slice(removeCount));
|
|
153
|
+
this.cache = new Map();
|
|
154
|
+
this._size = 0;
|
|
155
|
+
}
|
|
156
|
+
this.maxSize = newSize;
|
|
157
|
+
}
|
|
158
|
+
*keys() {
|
|
159
|
+
for (const [key] of this) {
|
|
160
|
+
yield key;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
*values() {
|
|
164
|
+
for (const [, value] of this) {
|
|
165
|
+
yield value;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
*[Symbol.iterator]() {
|
|
169
|
+
for (const item of this.cache) {
|
|
170
|
+
const [key, value] = item;
|
|
171
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
172
|
+
if (deleted === false) {
|
|
173
|
+
yield [key, value.value];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const item of this.oldCache) {
|
|
177
|
+
const [key, value] = item;
|
|
178
|
+
if (!this.cache.has(key)) {
|
|
179
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
180
|
+
if (deleted === false) {
|
|
181
|
+
yield [key, value.value];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
*entriesDescending() {
|
|
187
|
+
let items = [...this.cache];
|
|
188
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
189
|
+
const item = items[i];
|
|
190
|
+
const [key, value] = item;
|
|
191
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
192
|
+
if (deleted === false) {
|
|
193
|
+
yield [key, value.value];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
items = [...this.oldCache];
|
|
197
|
+
for (let i = items.length - 1; i >= 0; --i) {
|
|
198
|
+
const item = items[i];
|
|
199
|
+
const [key, value] = item;
|
|
200
|
+
if (!this.cache.has(key)) {
|
|
201
|
+
const deleted = this._deleteIfExpired(key, value);
|
|
202
|
+
if (deleted === false) {
|
|
203
|
+
yield [key, value.value];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
*entriesAscending() {
|
|
209
|
+
for (const [key, value] of this._entriesAscending()) {
|
|
210
|
+
yield [key, value.value];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
get size() {
|
|
214
|
+
if (!this._size) {
|
|
215
|
+
return this.oldCache.size;
|
|
216
|
+
}
|
|
217
|
+
let oldCacheSize = 0;
|
|
218
|
+
for (const key of this.oldCache.keys()) {
|
|
219
|
+
if (!this.cache.has(key)) {
|
|
220
|
+
oldCacheSize++;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return Math.min(this._size + oldCacheSize, this.maxSize);
|
|
224
|
+
}
|
|
225
|
+
entries() {
|
|
226
|
+
return this.entriesAscending();
|
|
227
|
+
}
|
|
228
|
+
forEach(callbackFunction, thisArgument = this) {
|
|
229
|
+
for (const [key, value] of this.entriesAscending()) {
|
|
230
|
+
callbackFunction.call(thisArgument, value, key, this);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
get [Symbol.toStringTag]() {
|
|
234
|
+
return JSON.stringify([...this.entriesAscending()]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=quick-lru.js.map
|