lifecycleion 0.0.10 → 0.0.12
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/README.md +6 -2
- package/dist/lib/event-emitter.cjs.map +1 -1
- package/dist/lib/event-emitter.js.map +1 -1
- package/dist/lib/http-client/index.cjs.map +1 -1
- package/dist/lib/http-client/index.js.map +1 -1
- package/dist/lib/lifecycle-manager/index.cjs +5 -7
- package/dist/lib/lifecycle-manager/index.cjs.map +1 -1
- package/dist/lib/lifecycle-manager/index.d.cts +2 -8
- package/dist/lib/lifecycle-manager/index.d.ts +2 -8
- package/dist/lib/lifecycle-manager/index.js +5 -7
- package/dist/lib/lifecycle-manager/index.js.map +1 -1
- package/dist/lib/logger/index.cjs +6 -5
- package/dist/lib/logger/index.cjs.map +1 -1
- package/dist/lib/logger/index.d.cts +3 -3
- package/dist/lib/logger/index.d.ts +3 -3
- package/dist/lib/logger/index.js +6 -5
- package/dist/lib/logger/index.js.map +1 -1
- package/dist/lib/lru-cache/index.cjs +896 -29
- package/dist/lib/lru-cache/index.cjs.map +1 -1
- package/dist/lib/lru-cache/index.d.cts +33 -17
- package/dist/lib/lru-cache/index.d.ts +33 -17
- package/dist/lib/lru-cache/index.js +884 -29
- package/dist/lib/lru-cache/index.js.map +1 -1
- package/dist/lib/process-signal-manager.cjs.map +1 -1
- package/dist/lib/process-signal-manager.js.map +1 -1
- package/dist/lib/promise-protected-resolver.cjs.map +1 -1
- package/dist/lib/promise-protected-resolver.js.map +1 -1
- package/dist/lib/retry-utils/index.cjs.map +1 -1
- package/dist/lib/retry-utils/index.js.map +1 -1
- package/dist/lib/safe-handle-callback.cjs.map +1 -1
- package/dist/lib/safe-handle-callback.d.cts +2 -2
- package/dist/lib/safe-handle-callback.d.ts +2 -2
- package/dist/lib/safe-handle-callback.js.map +1 -1
- package/dist/lib/single-event-observer.cjs.map +1 -1
- package/dist/lib/single-event-observer.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/lib/lru-cache/index.ts
|
|
@@ -23,6 +33,761 @@ __export(lru_cache_exports, {
|
|
|
23
33
|
LRUCache: () => LRUCache
|
|
24
34
|
});
|
|
25
35
|
module.exports = __toCommonJS(lru_cache_exports);
|
|
36
|
+
|
|
37
|
+
// src/lib/strings.ts
|
|
38
|
+
function isString(value) {
|
|
39
|
+
return typeof value === "string";
|
|
40
|
+
}
|
|
41
|
+
function splitGraphemes(text) {
|
|
42
|
+
const graphemes = [];
|
|
43
|
+
let grapheme = "";
|
|
44
|
+
let zwjSequence = "";
|
|
45
|
+
for (let i = 0; i < text.length; i++) {
|
|
46
|
+
const char = text[i];
|
|
47
|
+
const nextChar = text[i + 1] || "";
|
|
48
|
+
const code = char.charCodeAt(0);
|
|
49
|
+
if (code >= 768 && code <= 879 || // Combining Diacritical Marks
|
|
50
|
+
code >= 6832 && code <= 6911 || // Combining Diacritical Marks Extended
|
|
51
|
+
code >= 7616 && code <= 7679 || // Combining Diacritical Marks Supplement
|
|
52
|
+
code >= 65056 && code <= 65071 || // Combining Half Marks
|
|
53
|
+
code >= 3633 && code <= 3642 || // Thai combining marks
|
|
54
|
+
code >= 3655 && code <= 3662) {
|
|
55
|
+
grapheme += char;
|
|
56
|
+
} else if (char === "\u200D") {
|
|
57
|
+
zwjSequence += grapheme + char;
|
|
58
|
+
grapheme = "";
|
|
59
|
+
} else {
|
|
60
|
+
if (grapheme) {
|
|
61
|
+
if (zwjSequence) {
|
|
62
|
+
graphemes.push(zwjSequence + grapheme);
|
|
63
|
+
zwjSequence = "";
|
|
64
|
+
} else {
|
|
65
|
+
graphemes.push(grapheme);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
grapheme = char;
|
|
69
|
+
if (char >= "\uD800" && char <= "\uDBFF" && nextChar >= "\uDC00" && nextChar <= "\uDFFF") {
|
|
70
|
+
grapheme += nextChar;
|
|
71
|
+
i++;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (grapheme) {
|
|
76
|
+
if (zwjSequence) {
|
|
77
|
+
graphemes.push(zwjSequence + grapheme);
|
|
78
|
+
} else {
|
|
79
|
+
graphemes.push(grapheme);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return graphemes;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/lib/constants.ts
|
|
86
|
+
var BLANK_SPACE = " ";
|
|
87
|
+
var EOL = "\n";
|
|
88
|
+
var DOUBLE_EOL = EOL + EOL;
|
|
89
|
+
var INDENT = " ".repeat(4);
|
|
90
|
+
var DOUBLE_INDENT = INDENT + INDENT;
|
|
91
|
+
var ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
|
92
|
+
var ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
93
|
+
var ASCII_LETTERS = ASCII_LOWERCASE + ASCII_UPPERCASE;
|
|
94
|
+
var DIGITS = "0123456789";
|
|
95
|
+
var HEX_DIGITS = DIGITS + "abcdefABCDEF";
|
|
96
|
+
var PUNCTUATION = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
97
|
+
var WHITESPACE = " \n\r\v\f";
|
|
98
|
+
var PRINTABLE = DIGITS + ASCII_LETTERS + PUNCTUATION + WHITESPACE;
|
|
99
|
+
|
|
100
|
+
// src/lib/padding-utils.ts
|
|
101
|
+
function padLeft(str, length, padStr = BLANK_SPACE) {
|
|
102
|
+
return str.padStart(length, padStr);
|
|
103
|
+
}
|
|
104
|
+
function padRight(str, length, padStr = BLANK_SPACE) {
|
|
105
|
+
return str.padEnd(length, padStr);
|
|
106
|
+
}
|
|
107
|
+
function padCenter(str, length, prefer = "left", padStr = BLANK_SPACE) {
|
|
108
|
+
const midStrLength = length - str.length;
|
|
109
|
+
if (midStrLength > 0) {
|
|
110
|
+
const padLeftAmount = prefer === "left" ? Math.ceil(midStrLength / 2) : Math.floor(midStrLength / 2);
|
|
111
|
+
const padRightAmount = prefer === "left" ? Math.floor(midStrLength / 2) : Math.ceil(midStrLength / 2);
|
|
112
|
+
return padLeft("", padLeftAmount, padStr) + str + padRight("", padRightAmount, padStr);
|
|
113
|
+
} else {
|
|
114
|
+
return str;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function padCenterPreferRight(str, length, padStr = BLANK_SPACE) {
|
|
118
|
+
return padCenter(str, length, "right", padStr);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/lib/ascii-tables/ascii-table-utils.ts
|
|
122
|
+
var import_string_width = __toESM(require("string-width"), 1);
|
|
123
|
+
var ASCIITableUtils = class _ASCIITableUtils {
|
|
124
|
+
static centerText(text, width) {
|
|
125
|
+
return padCenterPreferRight(text, width, " ");
|
|
126
|
+
}
|
|
127
|
+
static createSeparator(columnWidths, character = "=") {
|
|
128
|
+
const totalWidth = columnWidths.reduce((sum, width) => sum + width + 3, 0) - 1;
|
|
129
|
+
return `+${padRight("", totalWidth, character)}+`;
|
|
130
|
+
}
|
|
131
|
+
static wrapText(text, maxLength) {
|
|
132
|
+
const words = text.split(" ");
|
|
133
|
+
const lines = [];
|
|
134
|
+
let currentLine = "";
|
|
135
|
+
for (const word of words) {
|
|
136
|
+
if ((0, import_string_width.default)(currentLine) + (0, import_string_width.default)(word) + 1 <= maxLength) {
|
|
137
|
+
currentLine += (currentLine ? " " : "") + word;
|
|
138
|
+
} else {
|
|
139
|
+
if (currentLine) {
|
|
140
|
+
lines.push(currentLine);
|
|
141
|
+
}
|
|
142
|
+
if ((0, import_string_width.default)(word) <= maxLength) {
|
|
143
|
+
currentLine = word;
|
|
144
|
+
} else {
|
|
145
|
+
const subWords = _ASCIITableUtils.splitWord(word, maxLength);
|
|
146
|
+
lines.push(...subWords.slice(0, -1));
|
|
147
|
+
currentLine = subWords[subWords.length - 1];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (currentLine) {
|
|
152
|
+
lines.push(currentLine);
|
|
153
|
+
}
|
|
154
|
+
return lines;
|
|
155
|
+
}
|
|
156
|
+
static splitWord(word, maxLength) {
|
|
157
|
+
const graphemes = splitGraphemes(word);
|
|
158
|
+
const subWords = [];
|
|
159
|
+
let currentSubWord = "";
|
|
160
|
+
for (const grapheme of graphemes) {
|
|
161
|
+
if ((0, import_string_width.default)(currentSubWord + grapheme) <= maxLength) {
|
|
162
|
+
currentSubWord += grapheme;
|
|
163
|
+
} else {
|
|
164
|
+
subWords.push(currentSubWord);
|
|
165
|
+
currentSubWord = grapheme;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (currentSubWord) {
|
|
169
|
+
subWords.push(currentSubWord);
|
|
170
|
+
}
|
|
171
|
+
return subWords;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// src/lib/ascii-tables/multi-column-ascii-table.ts
|
|
176
|
+
var import_string_width2 = __toESM(require("string-width"), 1);
|
|
177
|
+
var MultiColumnASCIITable = class {
|
|
178
|
+
headers;
|
|
179
|
+
rows;
|
|
180
|
+
tableWidth;
|
|
181
|
+
emptyMessage;
|
|
182
|
+
widthMode;
|
|
183
|
+
constructor(headers, options = {}) {
|
|
184
|
+
this.headers = headers;
|
|
185
|
+
this.rows = [];
|
|
186
|
+
this.tableWidth = options.tableWidth || 80;
|
|
187
|
+
this.emptyMessage = options.emptyMessage || "";
|
|
188
|
+
this.widthMode = options.widthMode || "flex";
|
|
189
|
+
const minTableWidth = this.getMinimumWidth();
|
|
190
|
+
if (this.tableWidth < minTableWidth) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`Table width must be at least ${minTableWidth} to accommodate the headers.`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
getMinimumWidth() {
|
|
197
|
+
return this.headers.length * 4 + 1;
|
|
198
|
+
}
|
|
199
|
+
addRow(row) {
|
|
200
|
+
if (row.length !== this.headers.length) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Number of values in the row (${row.length}) must match the number of headers (${this.headers.length}).`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
this.rows.push(row);
|
|
206
|
+
}
|
|
207
|
+
toString(options = {}) {
|
|
208
|
+
const tableWidth = options.tableWidth || this.tableWidth;
|
|
209
|
+
const emptyMessage = options.emptyMessage || this.emptyMessage;
|
|
210
|
+
if (this.rows.length === 0) {
|
|
211
|
+
const emptyTableWidth = Math.min(tableWidth, 40);
|
|
212
|
+
const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
|
|
213
|
+
const emptyMessageLines = ASCIITableUtils.wrapText(
|
|
214
|
+
emptyMessage,
|
|
215
|
+
emptyTableWidth - 4
|
|
216
|
+
);
|
|
217
|
+
const emptyRows = emptyMessageLines.map((line) => {
|
|
218
|
+
const paddingLeft = " ".repeat(
|
|
219
|
+
Math.floor((emptyTableWidth - (0, import_string_width2.default)(line) - 4) / 2)
|
|
220
|
+
);
|
|
221
|
+
const paddingRight = " ".repeat(
|
|
222
|
+
Math.ceil((emptyTableWidth - (0, import_string_width2.default)(line) - 4) / 2)
|
|
223
|
+
);
|
|
224
|
+
return `| ${paddingLeft}${line}${paddingRight} |`;
|
|
225
|
+
});
|
|
226
|
+
if (emptyRows.length === 0) {
|
|
227
|
+
emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
|
|
228
|
+
}
|
|
229
|
+
return [separator, ...emptyRows, separator].join("\n");
|
|
230
|
+
}
|
|
231
|
+
const columnWidths = this.calculateColumnWidths(options);
|
|
232
|
+
const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
|
|
233
|
+
const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
|
|
234
|
+
let tableString = headerSeparator + "\n";
|
|
235
|
+
const header = this.renderRow(this.headers, columnWidths);
|
|
236
|
+
tableString += header + "\n" + rowSeparator + "\n";
|
|
237
|
+
const rows = this.rows.map((row) => {
|
|
238
|
+
return this.renderRow(row, columnWidths);
|
|
239
|
+
});
|
|
240
|
+
tableString += rows.join("\n" + rowSeparator + "\n");
|
|
241
|
+
tableString += "\n" + headerSeparator;
|
|
242
|
+
return tableString;
|
|
243
|
+
}
|
|
244
|
+
calculateColumnWidths(options = {}) {
|
|
245
|
+
const tableWidth = options.tableWidth || this.tableWidth;
|
|
246
|
+
const widthMode = options.widthMode || this.widthMode;
|
|
247
|
+
const numColumns = this.headers.length;
|
|
248
|
+
if (widthMode === "fixed") {
|
|
249
|
+
const availableWidth2 = tableWidth - (numColumns + 1) * 3 + 1;
|
|
250
|
+
const columnWidth = Math.floor(availableWidth2 / numColumns);
|
|
251
|
+
const extraWidth = availableWidth2 % numColumns;
|
|
252
|
+
const columnWidths = new Array(numColumns).fill(columnWidth);
|
|
253
|
+
for (let i = 0; i < extraWidth; i++) {
|
|
254
|
+
columnWidths[i] += 1;
|
|
255
|
+
}
|
|
256
|
+
return columnWidths;
|
|
257
|
+
}
|
|
258
|
+
const availableWidth = tableWidth - (numColumns + 1) * 3 + 1;
|
|
259
|
+
const maxColumnWidth = Math.floor(availableWidth / numColumns);
|
|
260
|
+
const totalContentWidth = this.headers.reduce(
|
|
261
|
+
(sum, header) => sum + (0, import_string_width2.default)(header),
|
|
262
|
+
0
|
|
263
|
+
);
|
|
264
|
+
if (availableWidth >= totalContentWidth) {
|
|
265
|
+
const remainingWidth = availableWidth - totalContentWidth;
|
|
266
|
+
const extraCharWidth = Math.floor(remainingWidth / numColumns);
|
|
267
|
+
const extraCharRemainder = remainingWidth % numColumns;
|
|
268
|
+
const columnWidths = this.headers.map((header, index) => {
|
|
269
|
+
const extraWidth = index < extraCharRemainder ? 1 : 0;
|
|
270
|
+
return (0, import_string_width2.default)(header) + extraCharWidth + extraWidth;
|
|
271
|
+
});
|
|
272
|
+
return columnWidths;
|
|
273
|
+
} else {
|
|
274
|
+
const columnWidths = this.headers.map(() => maxColumnWidth);
|
|
275
|
+
return columnWidths;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
renderRow(row, columnWidths) {
|
|
279
|
+
const wrappedCells = row.map((value, index) => {
|
|
280
|
+
const wrappedLines = ASCIITableUtils.wrapText(value, columnWidths[index]);
|
|
281
|
+
return wrappedLines.map((line) => line.padEnd(columnWidths[index])).join("\n");
|
|
282
|
+
});
|
|
283
|
+
const maxLines = Math.max(
|
|
284
|
+
...wrappedCells.map((cell) => cell.split("\n").length)
|
|
285
|
+
);
|
|
286
|
+
const paddedRows = [];
|
|
287
|
+
for (let i = 0; i < maxLines; i++) {
|
|
288
|
+
const rowLine = wrappedCells.map((cell, index) => {
|
|
289
|
+
const cellLines = cell.split("\n");
|
|
290
|
+
const cellLine = cellLines[i] || "";
|
|
291
|
+
const padding = " ".repeat(columnWidths[index] - (0, import_string_width2.default)(cellLine));
|
|
292
|
+
return " " + cellLine + padding + " ";
|
|
293
|
+
});
|
|
294
|
+
paddedRows.push("|" + rowLine.join("|") + "|");
|
|
295
|
+
}
|
|
296
|
+
return paddedRows.join("\n");
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// src/lib/ascii-tables/key-value-ascii-table.ts
|
|
301
|
+
var import_string_width3 = __toESM(require("string-width"), 1);
|
|
302
|
+
|
|
303
|
+
// src/lib/clamp.ts
|
|
304
|
+
function clamp(value, min, max) {
|
|
305
|
+
return Math.max(min, Math.min(value, max));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/lib/ascii-tables/key-value-ascii-table.ts
|
|
309
|
+
var KeyValueASCIITable = class _KeyValueASCIITable {
|
|
310
|
+
tableWidth;
|
|
311
|
+
emptyMessage;
|
|
312
|
+
autoAdjustWidthWhenPossible = true;
|
|
313
|
+
rows = [];
|
|
314
|
+
constructor(options = {}) {
|
|
315
|
+
const minTableWidth = this.getMinimumWidth();
|
|
316
|
+
if (options.tableWidth && options.tableWidth < minTableWidth) {
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Table width must be at least ${minTableWidth} to accommodate the table structure.`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
this.tableWidth = options.tableWidth || 80;
|
|
322
|
+
this.autoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? true;
|
|
323
|
+
this.emptyMessage = options.emptyMessage || "";
|
|
324
|
+
}
|
|
325
|
+
getMinimumWidth() {
|
|
326
|
+
return 9;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Adds key and value to the table but placing the value on its own row.
|
|
330
|
+
*
|
|
331
|
+
* @param key
|
|
332
|
+
* @param value
|
|
333
|
+
*/
|
|
334
|
+
addValueOnSeparateRow(key, value) {
|
|
335
|
+
const row = { kind: "own", key, value };
|
|
336
|
+
this.rows.push(row);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Adds key and value to the table.
|
|
340
|
+
*
|
|
341
|
+
* If provided value is an instance of ASCIITable or MultiColumnASCIITable, it will be rendered as a nested table on its own row for readability.
|
|
342
|
+
*
|
|
343
|
+
* @param key
|
|
344
|
+
* @param value
|
|
345
|
+
*/
|
|
346
|
+
addRow(key, value) {
|
|
347
|
+
const row = { kind: "regular", key, value };
|
|
348
|
+
this.rows.push(row);
|
|
349
|
+
}
|
|
350
|
+
toString(options = {}) {
|
|
351
|
+
const tableWidth = options.tableWidth || this.tableWidth;
|
|
352
|
+
const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
|
|
353
|
+
const emptyMessage = options.emptyMessage || this.emptyMessage;
|
|
354
|
+
if (this.rows.length === 0) {
|
|
355
|
+
const emptyTableWidth = Math.min(tableWidth, 40);
|
|
356
|
+
const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
|
|
357
|
+
const emptyMessageLines = ASCIITableUtils.wrapText(
|
|
358
|
+
emptyMessage,
|
|
359
|
+
emptyTableWidth - 4
|
|
360
|
+
);
|
|
361
|
+
const emptyRows = emptyMessageLines.map((line) => {
|
|
362
|
+
const paddingLeft = padRight(
|
|
363
|
+
"",
|
|
364
|
+
Math.floor((emptyTableWidth - (0, import_string_width3.default)(line) - 4) / 2),
|
|
365
|
+
" "
|
|
366
|
+
);
|
|
367
|
+
const paddingRight = padRight(
|
|
368
|
+
"",
|
|
369
|
+
Math.ceil((emptyTableWidth - (0, import_string_width3.default)(line) - 4) / 2),
|
|
370
|
+
" "
|
|
371
|
+
);
|
|
372
|
+
return `| ${paddingLeft}${line}${paddingRight} |`;
|
|
373
|
+
});
|
|
374
|
+
if (emptyRows.length === 0) {
|
|
375
|
+
emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
|
|
376
|
+
}
|
|
377
|
+
return [separator, ...emptyRows, separator].join("\n");
|
|
378
|
+
}
|
|
379
|
+
const columnWidths = this.calculateColumnWidths(options);
|
|
380
|
+
const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
|
|
381
|
+
const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
|
|
382
|
+
let tableString = headerSeparator + "\n";
|
|
383
|
+
for (const [rowIndex, row] of this.rows.entries()) {
|
|
384
|
+
const { kind, key, value } = row;
|
|
385
|
+
if (kind === "own" || value instanceof _KeyValueASCIITable || value instanceof MultiColumnASCIITable || Array.isArray(value)) {
|
|
386
|
+
const keyString = ASCIITableUtils.centerText(
|
|
387
|
+
key,
|
|
388
|
+
columnWidths[0] + columnWidths[1] + 3
|
|
389
|
+
);
|
|
390
|
+
tableString += `| ${keyString} |
|
|
391
|
+
`;
|
|
392
|
+
tableString += rowSeparator + "\n";
|
|
393
|
+
let valueString = "";
|
|
394
|
+
if (value instanceof _KeyValueASCIITable) {
|
|
395
|
+
valueString = this.formatValue(
|
|
396
|
+
value,
|
|
397
|
+
tableWidth - 4,
|
|
398
|
+
canAutoAdjustWidthWhenPossible,
|
|
399
|
+
""
|
|
400
|
+
);
|
|
401
|
+
} else if (value instanceof MultiColumnASCIITable) {
|
|
402
|
+
valueString = this.formatValue(
|
|
403
|
+
value,
|
|
404
|
+
tableWidth - 4,
|
|
405
|
+
canAutoAdjustWidthWhenPossible,
|
|
406
|
+
""
|
|
407
|
+
);
|
|
408
|
+
} else if (Array.isArray(value)) {
|
|
409
|
+
valueString = this.formatValue(
|
|
410
|
+
value,
|
|
411
|
+
tableWidth - 4,
|
|
412
|
+
canAutoAdjustWidthWhenPossible,
|
|
413
|
+
""
|
|
414
|
+
);
|
|
415
|
+
} else if (row.kind === "own") {
|
|
416
|
+
valueString = this.formatTableRowOnOwnRow(
|
|
417
|
+
value,
|
|
418
|
+
tableWidth - 4,
|
|
419
|
+
tableWidth
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
const valueLines = valueString.split("\n");
|
|
423
|
+
const paddedValueLines = valueLines.map((line) => {
|
|
424
|
+
const padding = padRight("", tableWidth - (0, import_string_width3.default)(line) - 4, " ");
|
|
425
|
+
return `| ${line}${padding} |`;
|
|
426
|
+
});
|
|
427
|
+
tableString += paddedValueLines.join("\n") + "\n";
|
|
428
|
+
tableString += headerSeparator + "\n";
|
|
429
|
+
} else {
|
|
430
|
+
const keyLines = ASCIITableUtils.wrapText(key, columnWidths[0]);
|
|
431
|
+
const valueLines = ASCIITableUtils.wrapText(
|
|
432
|
+
this.formatValue(
|
|
433
|
+
value,
|
|
434
|
+
columnWidths[1],
|
|
435
|
+
canAutoAdjustWidthWhenPossible,
|
|
436
|
+
""
|
|
437
|
+
),
|
|
438
|
+
columnWidths[1]
|
|
439
|
+
);
|
|
440
|
+
const maxLines = Math.max(keyLines.length, valueLines.length);
|
|
441
|
+
for (let i = 0; i < maxLines; i++) {
|
|
442
|
+
const keyLine = keyLines[i] || "";
|
|
443
|
+
const valueLine = valueLines[i] || "";
|
|
444
|
+
const keyPadding = " ".repeat(columnWidths[0] - (0, import_string_width3.default)(keyLine));
|
|
445
|
+
const valuePadding = " ".repeat(
|
|
446
|
+
columnWidths[1] - (0, import_string_width3.default)(valueLine)
|
|
447
|
+
);
|
|
448
|
+
tableString += `| ${keyLine}${keyPadding} | ${valueLine}${valuePadding} |
|
|
449
|
+
`;
|
|
450
|
+
if (i === maxLines - 1) {
|
|
451
|
+
if (rowIndex === this.rows.length - 1) {
|
|
452
|
+
tableString += headerSeparator + "\n";
|
|
453
|
+
} else {
|
|
454
|
+
tableString += rowSeparator + "\n";
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return tableString.trim();
|
|
461
|
+
}
|
|
462
|
+
calculateColumnWidths(options = {}) {
|
|
463
|
+
const tableWidth = options.tableWidth || this.tableWidth;
|
|
464
|
+
const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
|
|
465
|
+
const columnWidths = [0, 0];
|
|
466
|
+
for (const row of this.rows) {
|
|
467
|
+
const { key, value } = row;
|
|
468
|
+
const keyWidth = (0, import_string_width3.default)(key);
|
|
469
|
+
const maxKeyWidth = Math.floor((tableWidth - 7) / 2);
|
|
470
|
+
if (keyWidth > columnWidths[0]) {
|
|
471
|
+
columnWidths[0] = Math.min(keyWidth, maxKeyWidth);
|
|
472
|
+
columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
|
|
473
|
+
}
|
|
474
|
+
if (typeof value === "string") {
|
|
475
|
+
const valueWidth = Math.max(
|
|
476
|
+
...value.split("\n").map((line) => (0, import_string_width3.default)(line))
|
|
477
|
+
);
|
|
478
|
+
if (valueWidth > columnWidths[1]) {
|
|
479
|
+
columnWidths[1] = Math.min(
|
|
480
|
+
valueWidth,
|
|
481
|
+
tableWidth - columnWidths[0] - 7
|
|
482
|
+
);
|
|
483
|
+
columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
|
|
484
|
+
}
|
|
485
|
+
} else if (row.kind === "own") {
|
|
486
|
+
let valueWidth = 0;
|
|
487
|
+
if (isString(value)) {
|
|
488
|
+
valueWidth = Math.max(
|
|
489
|
+
...value.split("\n").map((line) => (0, import_string_width3.default)(line))
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
if (valueWidth > columnWidths[1]) {
|
|
493
|
+
columnWidths[1] = Math.min(
|
|
494
|
+
valueWidth,
|
|
495
|
+
tableWidth - columnWidths[0] - 7
|
|
496
|
+
);
|
|
497
|
+
columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
|
|
498
|
+
}
|
|
499
|
+
} else if (value instanceof _KeyValueASCIITable) {
|
|
500
|
+
let nestedTableColumnWidths = [];
|
|
501
|
+
if (canAutoAdjustWidthWhenPossible) {
|
|
502
|
+
const minWidth = value.getMinimumWidth();
|
|
503
|
+
const availableWidth2 = tableWidth - columnWidths[0] - 7;
|
|
504
|
+
const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
|
|
505
|
+
nestedTableColumnWidths = value.calculateColumnWidths({
|
|
506
|
+
tableWidth: adjustedWidth
|
|
507
|
+
});
|
|
508
|
+
} else {
|
|
509
|
+
nestedTableColumnWidths = value.calculateColumnWidths();
|
|
510
|
+
}
|
|
511
|
+
const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
|
|
512
|
+
const availableWidth = tableWidth - columnWidths[0] - 7;
|
|
513
|
+
if (nestedTableWidth > availableWidth) {
|
|
514
|
+
columnWidths[1] = availableWidth;
|
|
515
|
+
} else {
|
|
516
|
+
columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
|
|
517
|
+
}
|
|
518
|
+
} else if (value instanceof MultiColumnASCIITable) {
|
|
519
|
+
let nestedTableColumnWidths = [];
|
|
520
|
+
if (canAutoAdjustWidthWhenPossible) {
|
|
521
|
+
const minWidth = value.getMinimumWidth();
|
|
522
|
+
const availableWidth2 = tableWidth - columnWidths[0] - 7;
|
|
523
|
+
const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
|
|
524
|
+
nestedTableColumnWidths = value.calculateColumnWidths({
|
|
525
|
+
tableWidth: adjustedWidth
|
|
526
|
+
});
|
|
527
|
+
} else {
|
|
528
|
+
nestedTableColumnWidths = value.calculateColumnWidths();
|
|
529
|
+
}
|
|
530
|
+
const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
|
|
531
|
+
const availableWidth = tableWidth - columnWidths[0] - 7;
|
|
532
|
+
if (nestedTableWidth > availableWidth) {
|
|
533
|
+
columnWidths[1] = availableWidth;
|
|
534
|
+
} else {
|
|
535
|
+
columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
|
|
536
|
+
}
|
|
537
|
+
} else if (Array.isArray(value)) {
|
|
538
|
+
for (const nestedCell of value) {
|
|
539
|
+
const nestedKeyWidth = (0, import_string_width3.default)(nestedCell.key);
|
|
540
|
+
const maxNestedKeyWidth = Math.floor((tableWidth - 7) / 2);
|
|
541
|
+
if (nestedKeyWidth > columnWidths[0]) {
|
|
542
|
+
columnWidths[0] = Math.min(nestedKeyWidth, maxNestedKeyWidth);
|
|
543
|
+
columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
|
|
544
|
+
}
|
|
545
|
+
if (typeof nestedCell.value === "string") {
|
|
546
|
+
const nestedValueWidth = Math.max(
|
|
547
|
+
...nestedCell.value.split("\n").map((line) => (0, import_string_width3.default)(line))
|
|
548
|
+
);
|
|
549
|
+
if (nestedValueWidth > columnWidths[1]) {
|
|
550
|
+
columnWidths[1] = Math.min(
|
|
551
|
+
nestedValueWidth,
|
|
552
|
+
tableWidth - columnWidths[0] - 7
|
|
553
|
+
);
|
|
554
|
+
columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return columnWidths;
|
|
561
|
+
}
|
|
562
|
+
formatValue(value, cellWidth, canAutoAdjustWidthWhenPossible, indent = "") {
|
|
563
|
+
if (typeof value === "string") {
|
|
564
|
+
return value;
|
|
565
|
+
} else if (typeof value === "number") {
|
|
566
|
+
return String(value);
|
|
567
|
+
} else if (typeof value === "boolean") {
|
|
568
|
+
return String(value);
|
|
569
|
+
} else if (value === null) {
|
|
570
|
+
return "null";
|
|
571
|
+
} else if (value === void 0) {
|
|
572
|
+
return "undefined";
|
|
573
|
+
} else if (value instanceof _KeyValueASCIITable) {
|
|
574
|
+
let nestedTableLines;
|
|
575
|
+
if (canAutoAdjustWidthWhenPossible) {
|
|
576
|
+
const minWidth = value.getMinimumWidth();
|
|
577
|
+
const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
|
|
578
|
+
nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
|
|
579
|
+
} else {
|
|
580
|
+
nestedTableLines = value.toString().split("\n");
|
|
581
|
+
}
|
|
582
|
+
const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
|
|
583
|
+
return indentedLines.join("\n");
|
|
584
|
+
} else if (value instanceof MultiColumnASCIITable) {
|
|
585
|
+
let nestedTableLines;
|
|
586
|
+
if (canAutoAdjustWidthWhenPossible) {
|
|
587
|
+
const minWidth = value.getMinimumWidth();
|
|
588
|
+
const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
|
|
589
|
+
nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
|
|
590
|
+
} else {
|
|
591
|
+
nestedTableLines = value.toString().split("\n");
|
|
592
|
+
}
|
|
593
|
+
const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
|
|
594
|
+
return indentedLines.join("\n");
|
|
595
|
+
} else if (Array.isArray(value)) {
|
|
596
|
+
const nestedValueLines = [];
|
|
597
|
+
for (const { key, value: nestedValue } of value) {
|
|
598
|
+
const formattedKey = `${indent}${key}:`;
|
|
599
|
+
const formattedValue = this.formatValue(
|
|
600
|
+
nestedValue,
|
|
601
|
+
cellWidth - indent.length - (0, import_string_width3.default)(key) - 2,
|
|
602
|
+
canAutoAdjustWidthWhenPossible,
|
|
603
|
+
`${indent}`
|
|
604
|
+
);
|
|
605
|
+
const wrappedSpacer = padRight("", 4, " ");
|
|
606
|
+
const wrappedValue = formattedValue.split("\n").map((line) => `${indent}${wrappedSpacer}${line}`);
|
|
607
|
+
nestedValueLines.push(formattedKey);
|
|
608
|
+
nestedValueLines.push(...wrappedValue);
|
|
609
|
+
nestedValueLines.push("");
|
|
610
|
+
}
|
|
611
|
+
return nestedValueLines.slice(0, -1).join("\n");
|
|
612
|
+
} else {
|
|
613
|
+
throw new TypeError("Invalid value type provided");
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
formatTableRowOnOwnRow(value, width, maxRowLength) {
|
|
617
|
+
const lines = value.split("\n");
|
|
618
|
+
const paddedLines = lines.map((line) => {
|
|
619
|
+
const wrappedLines = ASCIITableUtils.wrapText(line, maxRowLength - 4);
|
|
620
|
+
return wrappedLines.map((wrappedLine) => {
|
|
621
|
+
const padding = padRight(
|
|
622
|
+
"",
|
|
623
|
+
width - (0, import_string_width3.default)(wrappedLine) - 2,
|
|
624
|
+
" "
|
|
625
|
+
);
|
|
626
|
+
return `${wrappedLine}${padding}`;
|
|
627
|
+
}).join("\n");
|
|
628
|
+
});
|
|
629
|
+
return paddedLines.join("\n");
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
// src/lib/error-to-string.ts
|
|
634
|
+
function safeStringify(value) {
|
|
635
|
+
if (value === null || value === void 0) {
|
|
636
|
+
return String(value);
|
|
637
|
+
}
|
|
638
|
+
switch (typeof value) {
|
|
639
|
+
case "string":
|
|
640
|
+
return value;
|
|
641
|
+
case "number":
|
|
642
|
+
case "boolean":
|
|
643
|
+
case "bigint":
|
|
644
|
+
return String(value);
|
|
645
|
+
case "object":
|
|
646
|
+
return JSON.stringify(value);
|
|
647
|
+
case "function":
|
|
648
|
+
return "[Function]";
|
|
649
|
+
case "symbol":
|
|
650
|
+
return value.toString();
|
|
651
|
+
default:
|
|
652
|
+
return String(value);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function errorToString(error, maxRowLength = 80) {
|
|
656
|
+
const table = errorToASCIITable(error, maxRowLength);
|
|
657
|
+
return table.toString();
|
|
658
|
+
}
|
|
659
|
+
function errorToASCIITable(error, maxRowLength) {
|
|
660
|
+
const table = new KeyValueASCIITable({
|
|
661
|
+
tableWidth: maxRowLength,
|
|
662
|
+
autoAdjustWidthWhenPossible: true
|
|
663
|
+
});
|
|
664
|
+
if (error && typeof error === "object") {
|
|
665
|
+
const err = error;
|
|
666
|
+
table.addRow("Key", "Value");
|
|
667
|
+
if (err["message"]) {
|
|
668
|
+
table.addRow("Message", safeStringify(err["message"]));
|
|
669
|
+
}
|
|
670
|
+
if (err["name"]) {
|
|
671
|
+
table.addRow("Name", safeStringify(err["name"]));
|
|
672
|
+
}
|
|
673
|
+
if (err["code"]) {
|
|
674
|
+
table.addRow("Code", safeStringify(err["code"]));
|
|
675
|
+
}
|
|
676
|
+
if (err["errno"]) {
|
|
677
|
+
table.addRow("Errno", safeStringify(err["errno"]));
|
|
678
|
+
}
|
|
679
|
+
if (err["errPrefix"]) {
|
|
680
|
+
table.addRow("Prefix", safeStringify(err["errPrefix"]));
|
|
681
|
+
}
|
|
682
|
+
if (err["errType"]) {
|
|
683
|
+
table.addRow("errType", safeStringify(err["errType"]));
|
|
684
|
+
}
|
|
685
|
+
if (err["errCode"]) {
|
|
686
|
+
table.addRow("errCode", safeStringify(err["errCode"]));
|
|
687
|
+
}
|
|
688
|
+
if (err["additionalInfo"]) {
|
|
689
|
+
const additionalInfo = err["additionalInfo"];
|
|
690
|
+
const sensitiveFieldNames = err["sensitiveFieldNames"] || [];
|
|
691
|
+
for (const key in additionalInfo) {
|
|
692
|
+
if (sensitiveFieldNames.includes(key)) {
|
|
693
|
+
table.addRow(`AdditionalInfo.${key}`, "***");
|
|
694
|
+
} else {
|
|
695
|
+
const value = additionalInfo[key];
|
|
696
|
+
table.addRow(
|
|
697
|
+
`AdditionalInfo.${key}`,
|
|
698
|
+
stringifyValue(value, table, maxRowLength)
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (err["stack"]) {
|
|
704
|
+
table.addValueOnSeparateRow("Stack", safeStringify(err["stack"]));
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
return table;
|
|
708
|
+
}
|
|
709
|
+
function stringifyValue(value, table, maxRowLength) {
|
|
710
|
+
if (typeof value === "string") {
|
|
711
|
+
return value;
|
|
712
|
+
} else if (Array.isArray(value)) {
|
|
713
|
+
return value.map((item) => {
|
|
714
|
+
const result = stringifyValue(item, table, maxRowLength);
|
|
715
|
+
if (typeof result === "string") {
|
|
716
|
+
return result;
|
|
717
|
+
} else if (result instanceof KeyValueASCIITable) {
|
|
718
|
+
return result.toString();
|
|
719
|
+
} else {
|
|
720
|
+
return JSON.stringify(result);
|
|
721
|
+
}
|
|
722
|
+
}).join(", ");
|
|
723
|
+
} else if (typeof value === "object" && value !== null) {
|
|
724
|
+
if (value instanceof Error) {
|
|
725
|
+
return errorToASCIITable(value, maxRowLength - 4);
|
|
726
|
+
} else {
|
|
727
|
+
const entries = Object.entries(value).map(
|
|
728
|
+
([key, val]) => ({
|
|
729
|
+
key,
|
|
730
|
+
value: stringifyValue(val, table, maxRowLength - 4)
|
|
731
|
+
})
|
|
732
|
+
);
|
|
733
|
+
return entries;
|
|
734
|
+
}
|
|
735
|
+
} else {
|
|
736
|
+
return String(value);
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// src/lib/is-promise.ts
|
|
741
|
+
function isPromise(obj) {
|
|
742
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && // @ts-expect-error - obj is checked to be object/function, then property access works at runtime
|
|
743
|
+
typeof obj["then"] === "function";
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// src/lib/is-function.ts
|
|
747
|
+
function isFunction(value) {
|
|
748
|
+
return typeof value === "function" || value instanceof Function;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// src/lib/safe-handle-callback.ts
|
|
752
|
+
function safeHandleCallback(callbackName, callback, ...args) {
|
|
753
|
+
const handleError = (error) => {
|
|
754
|
+
if (typeof globalThis.dispatchEvent === "function") {
|
|
755
|
+
globalThis.dispatchEvent(
|
|
756
|
+
new ErrorEvent("reportError", {
|
|
757
|
+
error: new Error(
|
|
758
|
+
`Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
|
|
759
|
+
)
|
|
760
|
+
})
|
|
761
|
+
);
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
if (isFunction(callback)) {
|
|
765
|
+
try {
|
|
766
|
+
const result = callback(...args);
|
|
767
|
+
if (isPromise(result)) {
|
|
768
|
+
result.catch((error) => {
|
|
769
|
+
handleError(error);
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
} catch (error) {
|
|
773
|
+
handleError(error);
|
|
774
|
+
}
|
|
775
|
+
} else {
|
|
776
|
+
handleError(
|
|
777
|
+
new Error(`Callback provided for ${callbackName} is not a function`)
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// src/lib/lru-cache/index.ts
|
|
783
|
+
var VALID_CHANGE_REASONS = /* @__PURE__ */ new Set([
|
|
784
|
+
"evict",
|
|
785
|
+
"expired",
|
|
786
|
+
"delete",
|
|
787
|
+
"clear",
|
|
788
|
+
"set",
|
|
789
|
+
"skip"
|
|
790
|
+
]);
|
|
26
791
|
var LRUCache = class {
|
|
27
792
|
maxEntries;
|
|
28
793
|
maxSize;
|
|
@@ -38,6 +803,8 @@ var LRUCache = class {
|
|
|
38
803
|
// Track how many entries currently have expirations
|
|
39
804
|
sizeCalculator;
|
|
40
805
|
// Optional function to calculate item size
|
|
806
|
+
onChange;
|
|
807
|
+
onChangeReasons;
|
|
41
808
|
// Store values with their expiration time and size
|
|
42
809
|
map = /* @__PURE__ */ new Map();
|
|
43
810
|
/**
|
|
@@ -47,6 +814,8 @@ var LRUCache = class {
|
|
|
47
814
|
* @param options.defaultTtl Default time to live in milliseconds for all entries
|
|
48
815
|
* @param options.maxSize Maximum total size in bytes
|
|
49
816
|
* @param options.sizeCalculator Function to calculate the size of a value
|
|
817
|
+
* @param options.onChange Callback invoked for cache changes
|
|
818
|
+
* @param options.onChangeReasons Optional list of change reasons that should trigger onChange
|
|
50
819
|
*/
|
|
51
820
|
constructor(maxEntries, options) {
|
|
52
821
|
if (!Number.isInteger(maxEntries) || maxEntries <= 0) {
|
|
@@ -66,10 +835,25 @@ var LRUCache = class {
|
|
|
66
835
|
if (options?.sizeCalculator !== void 0 && typeof options.sizeCalculator !== "function") {
|
|
67
836
|
throw new TypeError("sizeCalculator must be a function");
|
|
68
837
|
}
|
|
838
|
+
if (options?.onChange !== void 0 && typeof options.onChange !== "function") {
|
|
839
|
+
throw new TypeError("onChange must be a function");
|
|
840
|
+
}
|
|
841
|
+
if (options?.onChangeReasons !== void 0) {
|
|
842
|
+
if (!Array.isArray(options.onChangeReasons)) {
|
|
843
|
+
throw new TypeError("onChangeReasons must be an array");
|
|
844
|
+
}
|
|
845
|
+
for (const reason of options.onChangeReasons) {
|
|
846
|
+
if (!VALID_CHANGE_REASONS.has(reason)) {
|
|
847
|
+
throw new RangeError(`Invalid onChange reason: ${String(reason)}`);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
69
851
|
this.maxEntries = maxEntries;
|
|
70
852
|
this.defaultTtl = options?.defaultTtl;
|
|
71
853
|
this.maxSize = options?.maxSize;
|
|
72
854
|
this.sizeCalculator = options?.sizeCalculator;
|
|
855
|
+
this.onChange = options?.onChange;
|
|
856
|
+
this.onChangeReasons = options?.onChangeReasons ? new Set(options.onChangeReasons) : void 0;
|
|
73
857
|
}
|
|
74
858
|
/**
|
|
75
859
|
* Get the current number of entries in the cache
|
|
@@ -92,7 +876,9 @@ var LRUCache = class {
|
|
|
92
876
|
const entry = this.map.get(key);
|
|
93
877
|
if (entry) {
|
|
94
878
|
if (entry.expires && Date.now() > entry.expires) {
|
|
95
|
-
this.
|
|
879
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
880
|
+
this.removeEntry(key, "expired", changeEvents);
|
|
881
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
96
882
|
return false;
|
|
97
883
|
}
|
|
98
884
|
return true;
|
|
@@ -103,7 +889,9 @@ var LRUCache = class {
|
|
|
103
889
|
const entry = this.map.get(key);
|
|
104
890
|
if (entry) {
|
|
105
891
|
if (entry.expires && Date.now() > entry.expires) {
|
|
106
|
-
this.
|
|
892
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
893
|
+
this.removeEntry(key, "expired", changeEvents);
|
|
894
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
107
895
|
return void 0;
|
|
108
896
|
}
|
|
109
897
|
this.map.delete(key);
|
|
@@ -121,8 +909,29 @@ var LRUCache = class {
|
|
|
121
909
|
);
|
|
122
910
|
}
|
|
123
911
|
const size = this.calculateSize(value);
|
|
124
|
-
|
|
125
|
-
|
|
912
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
913
|
+
this.cleanupExpiredInternal(changeEvents);
|
|
914
|
+
const existingEntry = this.map.get(key);
|
|
915
|
+
if (this.maxSize !== void 0 && size > this.maxSize) {
|
|
916
|
+
changeEvents.push(
|
|
917
|
+
existingEntry ? {
|
|
918
|
+
reason: "skip",
|
|
919
|
+
key,
|
|
920
|
+
currentValue: existingEntry.value,
|
|
921
|
+
newValue: value,
|
|
922
|
+
cause: "maxSize"
|
|
923
|
+
} : {
|
|
924
|
+
reason: "skip",
|
|
925
|
+
key,
|
|
926
|
+
newValue: value,
|
|
927
|
+
cause: "maxSize"
|
|
928
|
+
}
|
|
929
|
+
);
|
|
930
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
if (existingEntry) {
|
|
934
|
+
this.removeEntry(key);
|
|
126
935
|
}
|
|
127
936
|
const ttl = customTtl ?? this.defaultTtl;
|
|
128
937
|
const expires = ttl && ttl > 0 ? Date.now() + ttl : void 0;
|
|
@@ -131,51 +940,75 @@ var LRUCache = class {
|
|
|
131
940
|
if (expires !== void 0) {
|
|
132
941
|
this.expirableEntryCount++;
|
|
133
942
|
}
|
|
134
|
-
|
|
135
|
-
|
|
943
|
+
changeEvents?.push(
|
|
944
|
+
existingEntry ? {
|
|
945
|
+
reason: "set",
|
|
946
|
+
key,
|
|
947
|
+
oldValue: existingEntry.value,
|
|
948
|
+
newValue: value
|
|
949
|
+
} : {
|
|
950
|
+
reason: "set",
|
|
951
|
+
key,
|
|
952
|
+
newValue: value
|
|
953
|
+
}
|
|
954
|
+
);
|
|
955
|
+
this.evictIfNeeded(changeEvents);
|
|
956
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
136
957
|
}
|
|
137
958
|
/**
|
|
138
959
|
* Clear all entries from the cache
|
|
139
960
|
*/
|
|
140
961
|
clear() {
|
|
962
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
963
|
+
const shouldEmitClear = this.onChange !== void 0 && (this.onChangeReasons === void 0 || this.onChangeReasons.has("clear"));
|
|
964
|
+
if (shouldEmitClear) {
|
|
965
|
+
for (const [key, entry] of this.map.entries()) {
|
|
966
|
+
changeEvents.push({
|
|
967
|
+
reason: "clear",
|
|
968
|
+
key,
|
|
969
|
+
value: entry.value
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
}
|
|
141
973
|
this.map.clear();
|
|
142
974
|
this.currentSize = 0;
|
|
143
975
|
this.expirableEntryCount = 0;
|
|
144
976
|
this.lastCleanup = Date.now();
|
|
977
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Delete a specific entry from the cache
|
|
981
|
+
* @param key The key to delete
|
|
982
|
+
* @returns True if the entry was deleted, false if it didn't exist
|
|
983
|
+
*/
|
|
984
|
+
delete(key) {
|
|
985
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
986
|
+
const wasDeleted = this.removeEntry(key, "delete", changeEvents);
|
|
987
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
988
|
+
return wasDeleted;
|
|
145
989
|
}
|
|
146
990
|
/**
|
|
147
991
|
* Remove all expired entries from the cache
|
|
148
992
|
* @returns The number of entries removed
|
|
149
993
|
*/
|
|
150
994
|
cleanupExpired() {
|
|
995
|
+
const changeEvents = this.createLocalChangeQueue();
|
|
996
|
+
const removed = this.cleanupExpiredInternal(changeEvents);
|
|
997
|
+
this.flushLocalChangeQueue(changeEvents);
|
|
998
|
+
return removed;
|
|
999
|
+
}
|
|
1000
|
+
cleanupExpiredInternal(changeEvents) {
|
|
151
1001
|
let removed = 0;
|
|
152
1002
|
const now = Date.now();
|
|
153
1003
|
for (const [key, entry] of this.map.entries()) {
|
|
154
1004
|
if (entry.expires && now > entry.expires) {
|
|
155
|
-
this.
|
|
1005
|
+
this.removeEntry(key, "expired", changeEvents);
|
|
156
1006
|
removed++;
|
|
157
1007
|
}
|
|
158
1008
|
}
|
|
159
1009
|
this.lastCleanup = now;
|
|
160
1010
|
return removed;
|
|
161
1011
|
}
|
|
162
|
-
/**
|
|
163
|
-
* Delete a specific entry from the cache
|
|
164
|
-
* @param key The key to delete
|
|
165
|
-
* @returns True if the entry was deleted, false if it didn't exist
|
|
166
|
-
*/
|
|
167
|
-
delete(key) {
|
|
168
|
-
const entry = this.map.get(key);
|
|
169
|
-
if (entry) {
|
|
170
|
-
this.currentSize -= entry.size;
|
|
171
|
-
if (entry.expires !== void 0) {
|
|
172
|
-
this.expirableEntryCount--;
|
|
173
|
-
}
|
|
174
|
-
this.map.delete(key);
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
1012
|
isBufferValue(value) {
|
|
180
1013
|
const globalBuffer = globalThis.Buffer;
|
|
181
1014
|
if (globalBuffer === void 0) {
|
|
@@ -236,23 +1069,23 @@ var LRUCache = class {
|
|
|
236
1069
|
/**
|
|
237
1070
|
* Evict entries if we exceed either max entries or max size
|
|
238
1071
|
*/
|
|
239
|
-
evictIfNeeded() {
|
|
1072
|
+
evictIfNeeded(changeEvents) {
|
|
240
1073
|
if (this.map.size > this.maxEntries) {
|
|
241
|
-
this.evictOldest();
|
|
1074
|
+
this.evictOldest(changeEvents);
|
|
242
1075
|
}
|
|
243
1076
|
if (this.maxSize !== void 0 && this.currentSize > this.maxSize) {
|
|
244
1077
|
while (this.currentSize > this.maxSize && this.map.size > 0) {
|
|
245
|
-
this.evictOldest();
|
|
1078
|
+
this.evictOldest(changeEvents);
|
|
246
1079
|
}
|
|
247
1080
|
}
|
|
248
1081
|
}
|
|
249
1082
|
/**
|
|
250
1083
|
* Evict the oldest (least recently used) entry
|
|
251
1084
|
*/
|
|
252
|
-
evictOldest() {
|
|
1085
|
+
evictOldest(changeEvents) {
|
|
253
1086
|
if (this.map.size > 0) {
|
|
254
1087
|
const oldest = this.map.keys().next().value;
|
|
255
|
-
this.
|
|
1088
|
+
this.removeEntry(oldest, "evict", changeEvents);
|
|
256
1089
|
}
|
|
257
1090
|
}
|
|
258
1091
|
maybeCleanup() {
|
|
@@ -261,6 +1094,40 @@ var LRUCache = class {
|
|
|
261
1094
|
this.cleanupExpired();
|
|
262
1095
|
}
|
|
263
1096
|
}
|
|
1097
|
+
// Collect changes locally so each cache operation flushes its own events in order.
|
|
1098
|
+
createLocalChangeQueue() {
|
|
1099
|
+
return [];
|
|
1100
|
+
}
|
|
1101
|
+
flushLocalChangeQueue(changeEvents) {
|
|
1102
|
+
if (!this.onChange || changeEvents.length === 0) {
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
for (const changeEvent of changeEvents) {
|
|
1106
|
+
if (this.onChangeReasons !== void 0 && !this.onChangeReasons.has(changeEvent.reason)) {
|
|
1107
|
+
continue;
|
|
1108
|
+
}
|
|
1109
|
+
safeHandleCallback("LRUCache onChange", this.onChange, changeEvent);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
removeEntry(key, reason, changeEvents) {
|
|
1113
|
+
const entry = this.map.get(key);
|
|
1114
|
+
if (!entry) {
|
|
1115
|
+
return false;
|
|
1116
|
+
}
|
|
1117
|
+
this.currentSize -= entry.size;
|
|
1118
|
+
if (entry.expires !== void 0) {
|
|
1119
|
+
this.expirableEntryCount--;
|
|
1120
|
+
}
|
|
1121
|
+
this.map.delete(key);
|
|
1122
|
+
if (reason && changeEvents) {
|
|
1123
|
+
changeEvents.push({
|
|
1124
|
+
reason,
|
|
1125
|
+
key,
|
|
1126
|
+
value: entry.value
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
return true;
|
|
1130
|
+
}
|
|
264
1131
|
assertNonNegativeFiniteNumber(value, message) {
|
|
265
1132
|
if (!Number.isFinite(value) || value < 0) {
|
|
266
1133
|
throw new RangeError(message);
|