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