lifecycleion 0.0.1

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.
Files changed (177) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +125 -0
  3. package/dist/index.cjs +7 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +2 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +5 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/lib/arrays.cjs +95 -0
  10. package/dist/lib/arrays.cjs.map +1 -0
  11. package/dist/lib/arrays.d.cts +15 -0
  12. package/dist/lib/arrays.d.ts +15 -0
  13. package/dist/lib/arrays.js +63 -0
  14. package/dist/lib/arrays.js.map +1 -0
  15. package/dist/lib/ascii-tables/index.cjs +642 -0
  16. package/dist/lib/ascii-tables/index.cjs.map +1 -0
  17. package/dist/lib/ascii-tables/index.d.cts +66 -0
  18. package/dist/lib/ascii-tables/index.d.ts +66 -0
  19. package/dist/lib/ascii-tables/index.js +603 -0
  20. package/dist/lib/ascii-tables/index.js.map +1 -0
  21. package/dist/lib/clamp.cjs +41 -0
  22. package/dist/lib/clamp.cjs.map +1 -0
  23. package/dist/lib/clamp.d.cts +26 -0
  24. package/dist/lib/clamp.d.ts +26 -0
  25. package/dist/lib/clamp.js +15 -0
  26. package/dist/lib/clamp.js.map +1 -0
  27. package/dist/lib/constants.cjs +73 -0
  28. package/dist/lib/constants.cjs.map +1 -0
  29. package/dist/lib/constants.d.cts +17 -0
  30. package/dist/lib/constants.d.ts +17 -0
  31. package/dist/lib/constants.js +34 -0
  32. package/dist/lib/constants.js.map +1 -0
  33. package/dist/lib/curly-brackets.cjs +77 -0
  34. package/dist/lib/curly-brackets.cjs.map +1 -0
  35. package/dist/lib/curly-brackets.d.cts +17 -0
  36. package/dist/lib/curly-brackets.d.ts +17 -0
  37. package/dist/lib/curly-brackets.js +52 -0
  38. package/dist/lib/curly-brackets.js.map +1 -0
  39. package/dist/lib/deep-clone.cjs +87 -0
  40. package/dist/lib/deep-clone.cjs.map +1 -0
  41. package/dist/lib/deep-clone.d.cts +19 -0
  42. package/dist/lib/deep-clone.d.ts +19 -0
  43. package/dist/lib/deep-clone.js +62 -0
  44. package/dist/lib/deep-clone.js.map +1 -0
  45. package/dist/lib/error-to-string.cjs +743 -0
  46. package/dist/lib/error-to-string.cjs.map +1 -0
  47. package/dist/lib/error-to-string.d.cts +3 -0
  48. package/dist/lib/error-to-string.d.ts +3 -0
  49. package/dist/lib/error-to-string.js +706 -0
  50. package/dist/lib/error-to-string.js.map +1 -0
  51. package/dist/lib/event-emitter.cjs +899 -0
  52. package/dist/lib/event-emitter.cjs.map +1 -0
  53. package/dist/lib/event-emitter.d.cts +78 -0
  54. package/dist/lib/event-emitter.d.ts +78 -0
  55. package/dist/lib/event-emitter.js +861 -0
  56. package/dist/lib/event-emitter.js.map +1 -0
  57. package/dist/lib/id-helpers.cjs +205 -0
  58. package/dist/lib/id-helpers.cjs.map +1 -0
  59. package/dist/lib/id-helpers.d.cts +198 -0
  60. package/dist/lib/id-helpers.d.ts +198 -0
  61. package/dist/lib/id-helpers.js +170 -0
  62. package/dist/lib/id-helpers.js.map +1 -0
  63. package/dist/lib/is-boolean.cjs +33 -0
  64. package/dist/lib/is-boolean.cjs.map +1 -0
  65. package/dist/lib/is-boolean.d.cts +19 -0
  66. package/dist/lib/is-boolean.d.ts +19 -0
  67. package/dist/lib/is-boolean.js +8 -0
  68. package/dist/lib/is-boolean.js.map +1 -0
  69. package/dist/lib/is-function.cjs +33 -0
  70. package/dist/lib/is-function.cjs.map +1 -0
  71. package/dist/lib/is-function.d.cts +3 -0
  72. package/dist/lib/is-function.d.ts +3 -0
  73. package/dist/lib/is-function.js +8 -0
  74. package/dist/lib/is-function.js.map +1 -0
  75. package/dist/lib/is-number.cjs +38 -0
  76. package/dist/lib/is-number.cjs.map +1 -0
  77. package/dist/lib/is-number.d.cts +38 -0
  78. package/dist/lib/is-number.d.ts +38 -0
  79. package/dist/lib/is-number.js +12 -0
  80. package/dist/lib/is-number.js.map +1 -0
  81. package/dist/lib/is-plain-object.cjs +33 -0
  82. package/dist/lib/is-plain-object.cjs.map +1 -0
  83. package/dist/lib/is-plain-object.d.cts +20 -0
  84. package/dist/lib/is-plain-object.d.ts +20 -0
  85. package/dist/lib/is-plain-object.js +8 -0
  86. package/dist/lib/is-plain-object.js.map +1 -0
  87. package/dist/lib/is-promise.cjs +34 -0
  88. package/dist/lib/is-promise.cjs.map +1 -0
  89. package/dist/lib/is-promise.d.cts +3 -0
  90. package/dist/lib/is-promise.d.ts +3 -0
  91. package/dist/lib/is-promise.js +9 -0
  92. package/dist/lib/is-promise.js.map +1 -0
  93. package/dist/lib/json-helpers.cjs +49 -0
  94. package/dist/lib/json-helpers.cjs.map +1 -0
  95. package/dist/lib/json-helpers.d.cts +10 -0
  96. package/dist/lib/json-helpers.d.ts +10 -0
  97. package/dist/lib/json-helpers.js +22 -0
  98. package/dist/lib/json-helpers.js.map +1 -0
  99. package/dist/lib/lifecycle-manager/index.cjs +5594 -0
  100. package/dist/lib/lifecycle-manager/index.cjs.map +1 -0
  101. package/dist/lib/lifecycle-manager/index.d.cts +2044 -0
  102. package/dist/lib/lifecycle-manager/index.d.ts +2044 -0
  103. package/dist/lib/lifecycle-manager/index.js +5543 -0
  104. package/dist/lib/lifecycle-manager/index.js.map +1 -0
  105. package/dist/lib/logger/index.cjs +2514 -0
  106. package/dist/lib/logger/index.cjs.map +1 -0
  107. package/dist/lib/logger/index.d.cts +630 -0
  108. package/dist/lib/logger/index.d.ts +630 -0
  109. package/dist/lib/logger/index.js +2470 -0
  110. package/dist/lib/logger/index.js.map +1 -0
  111. package/dist/lib/padding-utils.cjs +77 -0
  112. package/dist/lib/padding-utils.cjs.map +1 -0
  113. package/dist/lib/padding-utils.d.cts +44 -0
  114. package/dist/lib/padding-utils.d.ts +44 -0
  115. package/dist/lib/padding-utils.js +46 -0
  116. package/dist/lib/padding-utils.js.map +1 -0
  117. package/dist/lib/process-signal-manager.cjs +1306 -0
  118. package/dist/lib/process-signal-manager.cjs.map +1 -0
  119. package/dist/lib/process-signal-manager.d.cts +305 -0
  120. package/dist/lib/process-signal-manager.d.ts +305 -0
  121. package/dist/lib/process-signal-manager.js +1269 -0
  122. package/dist/lib/process-signal-manager.js.map +1 -0
  123. package/dist/lib/promise-protected-resolver.cjs +828 -0
  124. package/dist/lib/promise-protected-resolver.cjs.map +1 -0
  125. package/dist/lib/promise-protected-resolver.d.cts +17 -0
  126. package/dist/lib/promise-protected-resolver.d.ts +17 -0
  127. package/dist/lib/promise-protected-resolver.js +791 -0
  128. package/dist/lib/promise-protected-resolver.js.map +1 -0
  129. package/dist/lib/retry-utils/index.cjs +2183 -0
  130. package/dist/lib/retry-utils/index.cjs.map +1 -0
  131. package/dist/lib/retry-utils/index.d.cts +321 -0
  132. package/dist/lib/retry-utils/index.d.ts +321 -0
  133. package/dist/lib/retry-utils/index.js +2133 -0
  134. package/dist/lib/retry-utils/index.js.map +1 -0
  135. package/dist/lib/safe-handle-callback.cjs +818 -0
  136. package/dist/lib/safe-handle-callback.cjs.map +1 -0
  137. package/dist/lib/safe-handle-callback.d.cts +43 -0
  138. package/dist/lib/safe-handle-callback.d.ts +43 -0
  139. package/dist/lib/safe-handle-callback.js +780 -0
  140. package/dist/lib/safe-handle-callback.js.map +1 -0
  141. package/dist/lib/serialize-error/index.cjs +93 -0
  142. package/dist/lib/serialize-error/index.cjs.map +1 -0
  143. package/dist/lib/serialize-error/index.d.cts +26 -0
  144. package/dist/lib/serialize-error/index.d.ts +26 -0
  145. package/dist/lib/serialize-error/index.js +64 -0
  146. package/dist/lib/serialize-error/index.js.map +1 -0
  147. package/dist/lib/single-event-observer.cjs +841 -0
  148. package/dist/lib/single-event-observer.cjs.map +1 -0
  149. package/dist/lib/single-event-observer.d.cts +54 -0
  150. package/dist/lib/single-event-observer.d.ts +54 -0
  151. package/dist/lib/single-event-observer.js +803 -0
  152. package/dist/lib/single-event-observer.js.map +1 -0
  153. package/dist/lib/sleep.cjs +37 -0
  154. package/dist/lib/sleep.cjs.map +1 -0
  155. package/dist/lib/sleep.d.cts +11 -0
  156. package/dist/lib/sleep.d.ts +11 -0
  157. package/dist/lib/sleep.js +12 -0
  158. package/dist/lib/sleep.js.map +1 -0
  159. package/dist/lib/strings.cjs +186 -0
  160. package/dist/lib/strings.cjs.map +1 -0
  161. package/dist/lib/strings.d.cts +107 -0
  162. package/dist/lib/strings.d.ts +107 -0
  163. package/dist/lib/strings.js +149 -0
  164. package/dist/lib/strings.js.map +1 -0
  165. package/dist/lib/tmp-dir.cjs +254 -0
  166. package/dist/lib/tmp-dir.cjs.map +1 -0
  167. package/dist/lib/tmp-dir.d.cts +63 -0
  168. package/dist/lib/tmp-dir.d.ts +63 -0
  169. package/dist/lib/tmp-dir.js +211 -0
  170. package/dist/lib/tmp-dir.js.map +1 -0
  171. package/dist/lib/unix-time-helpers.cjs +53 -0
  172. package/dist/lib/unix-time-helpers.cjs.map +1 -0
  173. package/dist/lib/unix-time-helpers.d.cts +56 -0
  174. package/dist/lib/unix-time-helpers.d.ts +56 -0
  175. package/dist/lib/unix-time-helpers.js +24 -0
  176. package/dist/lib/unix-time-helpers.js.map +1 -0
  177. package/package.json +220 -0
@@ -0,0 +1,818 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
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
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/lib/safe-handle-callback.ts
31
+ var safe_handle_callback_exports = {};
32
+ __export(safe_handle_callback_exports, {
33
+ safeHandleCallback: () => safeHandleCallback,
34
+ safeHandleCallbackAndWait: () => safeHandleCallbackAndWait
35
+ });
36
+ module.exports = __toCommonJS(safe_handle_callback_exports);
37
+
38
+ // src/lib/strings.ts
39
+ function isString(value) {
40
+ return typeof value === "string";
41
+ }
42
+ function splitGraphemes(text) {
43
+ const graphemes = [];
44
+ let grapheme = "";
45
+ let zwjSequence = "";
46
+ for (let i = 0; i < text.length; i++) {
47
+ const char = text[i];
48
+ const nextChar = text[i + 1] || "";
49
+ const code = char.charCodeAt(0);
50
+ if (code >= 768 && code <= 879 || // Combining Diacritical Marks
51
+ code >= 6832 && code <= 6911 || // Combining Diacritical Marks Extended
52
+ code >= 7616 && code <= 7679 || // Combining Diacritical Marks Supplement
53
+ code >= 65056 && code <= 65071 || // Combining Half Marks
54
+ code >= 3633 && code <= 3642 || // Thai combining marks
55
+ code >= 3655 && code <= 3662) {
56
+ grapheme += char;
57
+ } else if (char === "\u200D") {
58
+ zwjSequence += grapheme + char;
59
+ grapheme = "";
60
+ } else {
61
+ if (grapheme) {
62
+ if (zwjSequence) {
63
+ graphemes.push(zwjSequence + grapheme);
64
+ zwjSequence = "";
65
+ } else {
66
+ graphemes.push(grapheme);
67
+ }
68
+ }
69
+ grapheme = char;
70
+ if (char >= "\uD800" && char <= "\uDBFF" && nextChar >= "\uDC00" && nextChar <= "\uDFFF") {
71
+ grapheme += nextChar;
72
+ i++;
73
+ }
74
+ }
75
+ }
76
+ if (grapheme) {
77
+ if (zwjSequence) {
78
+ graphemes.push(zwjSequence + grapheme);
79
+ } else {
80
+ graphemes.push(grapheme);
81
+ }
82
+ }
83
+ return graphemes;
84
+ }
85
+
86
+ // src/lib/constants.ts
87
+ var BLANK_SPACE = " ";
88
+ var EOL = "\n";
89
+ var DOUBLE_EOL = EOL + EOL;
90
+ var INDENT = " ".repeat(4);
91
+ var DOUBLE_INDENT = INDENT + INDENT;
92
+ var ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
93
+ var ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
94
+ var ASCII_LETTERS = ASCII_LOWERCASE + ASCII_UPPERCASE;
95
+ var DIGITS = "0123456789";
96
+ var HEX_DIGITS = DIGITS + "abcdefABCDEF";
97
+ var PUNCTUATION = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
98
+ var WHITESPACE = " \n\r\v\f";
99
+ var PRINTABLE = DIGITS + ASCII_LETTERS + PUNCTUATION + WHITESPACE;
100
+
101
+ // src/lib/padding-utils.ts
102
+ function padLeft(str, length, padStr = BLANK_SPACE) {
103
+ return str.padStart(length, padStr);
104
+ }
105
+ function padRight(str, length, padStr = BLANK_SPACE) {
106
+ return str.padEnd(length, padStr);
107
+ }
108
+ function padCenter(str, length, prefer = "left", padStr = BLANK_SPACE) {
109
+ const midStrLength = length - str.length;
110
+ if (midStrLength > 0) {
111
+ const padLeftAmount = prefer === "left" ? Math.ceil(midStrLength / 2) : Math.floor(midStrLength / 2);
112
+ const padRightAmount = prefer === "left" ? Math.floor(midStrLength / 2) : Math.ceil(midStrLength / 2);
113
+ return padLeft("", padLeftAmount, padStr) + str + padRight("", padRightAmount, padStr);
114
+ } else {
115
+ return str;
116
+ }
117
+ }
118
+ function padCenterPreferRight(str, length, padStr = BLANK_SPACE) {
119
+ return padCenter(str, length, "right", padStr);
120
+ }
121
+
122
+ // src/lib/ascii-tables/ascii-table-utils.ts
123
+ var import_string_width = __toESM(require("string-width"), 1);
124
+ var ASCIITableUtils = class _ASCIITableUtils {
125
+ static centerText(text, width) {
126
+ return padCenterPreferRight(text, width, " ");
127
+ }
128
+ static createSeparator(columnWidths, character = "=") {
129
+ const totalWidth = columnWidths.reduce((sum, width) => sum + width + 3, 0) - 1;
130
+ return `+${padRight("", totalWidth, character)}+`;
131
+ }
132
+ static wrapText(text, maxLength) {
133
+ const words = text.split(" ");
134
+ const lines = [];
135
+ let currentLine = "";
136
+ for (const word of words) {
137
+ if ((0, import_string_width.default)(currentLine) + (0, import_string_width.default)(word) + 1 <= maxLength) {
138
+ currentLine += (currentLine ? " " : "") + word;
139
+ } else {
140
+ if (currentLine) {
141
+ lines.push(currentLine);
142
+ }
143
+ if ((0, import_string_width.default)(word) <= maxLength) {
144
+ currentLine = word;
145
+ } else {
146
+ const subWords = _ASCIITableUtils.splitWord(word, maxLength);
147
+ lines.push(...subWords.slice(0, -1));
148
+ currentLine = subWords[subWords.length - 1];
149
+ }
150
+ }
151
+ }
152
+ if (currentLine) {
153
+ lines.push(currentLine);
154
+ }
155
+ return lines;
156
+ }
157
+ static splitWord(word, maxLength) {
158
+ const graphemes = splitGraphemes(word);
159
+ const subWords = [];
160
+ let currentSubWord = "";
161
+ for (const grapheme of graphemes) {
162
+ if ((0, import_string_width.default)(currentSubWord + grapheme) <= maxLength) {
163
+ currentSubWord += grapheme;
164
+ } else {
165
+ subWords.push(currentSubWord);
166
+ currentSubWord = grapheme;
167
+ }
168
+ }
169
+ if (currentSubWord) {
170
+ subWords.push(currentSubWord);
171
+ }
172
+ return subWords;
173
+ }
174
+ };
175
+
176
+ // src/lib/ascii-tables/multi-column-ascii-table.ts
177
+ var import_string_width2 = __toESM(require("string-width"), 1);
178
+ var MultiColumnASCIITable = class {
179
+ headers;
180
+ rows;
181
+ tableWidth;
182
+ emptyMessage;
183
+ widthMode;
184
+ constructor(headers, options = {}) {
185
+ this.headers = headers;
186
+ this.rows = [];
187
+ this.tableWidth = options.tableWidth || 80;
188
+ this.emptyMessage = options.emptyMessage || "";
189
+ this.widthMode = options.widthMode || "flex";
190
+ const minTableWidth = this.getMinimumWidth();
191
+ if (this.tableWidth < minTableWidth) {
192
+ throw new Error(
193
+ `Table width must be at least ${minTableWidth} to accommodate the headers.`
194
+ );
195
+ }
196
+ }
197
+ getMinimumWidth() {
198
+ return this.headers.length * 4 + 1;
199
+ }
200
+ addRow(row) {
201
+ if (row.length !== this.headers.length) {
202
+ throw new Error(
203
+ `Number of values in the row (${row.length}) must match the number of headers (${this.headers.length}).`
204
+ );
205
+ }
206
+ this.rows.push(row);
207
+ }
208
+ toString(options = {}) {
209
+ const tableWidth = options.tableWidth || this.tableWidth;
210
+ const emptyMessage = options.emptyMessage || this.emptyMessage;
211
+ if (this.rows.length === 0) {
212
+ const emptyTableWidth = Math.min(tableWidth, 40);
213
+ const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
214
+ const emptyMessageLines = ASCIITableUtils.wrapText(
215
+ emptyMessage,
216
+ emptyTableWidth - 4
217
+ );
218
+ const emptyRows = emptyMessageLines.map((line) => {
219
+ const paddingLeft = " ".repeat(
220
+ Math.floor((emptyTableWidth - (0, import_string_width2.default)(line) - 4) / 2)
221
+ );
222
+ const paddingRight = " ".repeat(
223
+ Math.ceil((emptyTableWidth - (0, import_string_width2.default)(line) - 4) / 2)
224
+ );
225
+ return `| ${paddingLeft}${line}${paddingRight} |`;
226
+ });
227
+ if (emptyRows.length === 0) {
228
+ emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
229
+ }
230
+ return [separator, ...emptyRows, separator].join("\n");
231
+ }
232
+ const columnWidths = this.calculateColumnWidths(options);
233
+ const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
234
+ const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
235
+ let tableString = headerSeparator + "\n";
236
+ const header = this.renderRow(this.headers, columnWidths);
237
+ tableString += header + "\n" + rowSeparator + "\n";
238
+ const rows = this.rows.map((row) => {
239
+ return this.renderRow(row, columnWidths);
240
+ });
241
+ tableString += rows.join("\n" + rowSeparator + "\n");
242
+ tableString += "\n" + headerSeparator;
243
+ return tableString;
244
+ }
245
+ calculateColumnWidths(options = {}) {
246
+ const tableWidth = options.tableWidth || this.tableWidth;
247
+ const widthMode = options.widthMode || this.widthMode;
248
+ const numColumns = this.headers.length;
249
+ if (widthMode === "fixed") {
250
+ const availableWidth2 = tableWidth - (numColumns + 1) * 3 + 1;
251
+ const columnWidth = Math.floor(availableWidth2 / numColumns);
252
+ const extraWidth = availableWidth2 % numColumns;
253
+ const columnWidths = new Array(numColumns).fill(columnWidth);
254
+ for (let i = 0; i < extraWidth; i++) {
255
+ columnWidths[i] += 1;
256
+ }
257
+ return columnWidths;
258
+ }
259
+ const availableWidth = tableWidth - (numColumns + 1) * 3 + 1;
260
+ const maxColumnWidth = Math.floor(availableWidth / numColumns);
261
+ const totalContentWidth = this.headers.reduce(
262
+ (sum, header) => sum + (0, import_string_width2.default)(header),
263
+ 0
264
+ );
265
+ if (availableWidth >= totalContentWidth) {
266
+ const remainingWidth = availableWidth - totalContentWidth;
267
+ const extraCharWidth = Math.floor(remainingWidth / numColumns);
268
+ const extraCharRemainder = remainingWidth % numColumns;
269
+ const columnWidths = this.headers.map((header, index) => {
270
+ const extraWidth = index < extraCharRemainder ? 1 : 0;
271
+ return (0, import_string_width2.default)(header) + extraCharWidth + extraWidth;
272
+ });
273
+ return columnWidths;
274
+ } else {
275
+ const columnWidths = this.headers.map(() => maxColumnWidth);
276
+ return columnWidths;
277
+ }
278
+ }
279
+ renderRow(row, columnWidths) {
280
+ const wrappedCells = row.map((value, index) => {
281
+ const wrappedLines = ASCIITableUtils.wrapText(value, columnWidths[index]);
282
+ return wrappedLines.map((line) => line.padEnd(columnWidths[index])).join("\n");
283
+ });
284
+ const maxLines = Math.max(
285
+ ...wrappedCells.map((cell) => cell.split("\n").length)
286
+ );
287
+ const paddedRows = [];
288
+ for (let i = 0; i < maxLines; i++) {
289
+ const rowLine = wrappedCells.map((cell, index) => {
290
+ const cellLines = cell.split("\n");
291
+ const cellLine = cellLines[i] || "";
292
+ const padding = " ".repeat(columnWidths[index] - (0, import_string_width2.default)(cellLine));
293
+ return " " + cellLine + padding + " ";
294
+ });
295
+ paddedRows.push("|" + rowLine.join("|") + "|");
296
+ }
297
+ return paddedRows.join("\n");
298
+ }
299
+ };
300
+
301
+ // src/lib/ascii-tables/key-value-ascii-table.ts
302
+ var import_string_width3 = __toESM(require("string-width"), 1);
303
+
304
+ // src/lib/clamp.ts
305
+ function clamp(value, min, max) {
306
+ return Math.max(min, Math.min(value, max));
307
+ }
308
+
309
+ // src/lib/ascii-tables/key-value-ascii-table.ts
310
+ var KeyValueASCIITable = class _KeyValueASCIITable {
311
+ tableWidth;
312
+ emptyMessage;
313
+ autoAdjustWidthWhenPossible = true;
314
+ rows = [];
315
+ constructor(options = {}) {
316
+ const minTableWidth = this.getMinimumWidth();
317
+ if (options.tableWidth && options.tableWidth < minTableWidth) {
318
+ throw new Error(
319
+ `Table width must be at least ${minTableWidth} to accommodate the table structure.`
320
+ );
321
+ }
322
+ this.tableWidth = options.tableWidth || 80;
323
+ this.autoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? true;
324
+ this.emptyMessage = options.emptyMessage || "";
325
+ }
326
+ getMinimumWidth() {
327
+ return 9;
328
+ }
329
+ /**
330
+ * Adds key and value to the table but placing the value on its own row.
331
+ *
332
+ * @param key
333
+ * @param value
334
+ */
335
+ addValueOnSeparateRow(key, value) {
336
+ const row = { kind: "own", key, value };
337
+ this.rows.push(row);
338
+ }
339
+ /**
340
+ * Adds key and value to the table.
341
+ *
342
+ * If provided value is an instance of ASCIITable or MultiColumnASCIITable, it will be rendered as a nested table on its own row for readability.
343
+ *
344
+ * @param key
345
+ * @param value
346
+ */
347
+ addRow(key, value) {
348
+ const row = { kind: "regular", key, value };
349
+ this.rows.push(row);
350
+ }
351
+ toString(options = {}) {
352
+ const tableWidth = options.tableWidth || this.tableWidth;
353
+ const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
354
+ const emptyMessage = options.emptyMessage || this.emptyMessage;
355
+ if (this.rows.length === 0) {
356
+ const emptyTableWidth = Math.min(tableWidth, 40);
357
+ const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
358
+ const emptyMessageLines = ASCIITableUtils.wrapText(
359
+ emptyMessage,
360
+ emptyTableWidth - 4
361
+ );
362
+ const emptyRows = emptyMessageLines.map((line) => {
363
+ const paddingLeft = padRight(
364
+ "",
365
+ Math.floor((emptyTableWidth - (0, import_string_width3.default)(line) - 4) / 2),
366
+ " "
367
+ );
368
+ const paddingRight = padRight(
369
+ "",
370
+ Math.ceil((emptyTableWidth - (0, import_string_width3.default)(line) - 4) / 2),
371
+ " "
372
+ );
373
+ return `| ${paddingLeft}${line}${paddingRight} |`;
374
+ });
375
+ if (emptyRows.length === 0) {
376
+ emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
377
+ }
378
+ return [separator, ...emptyRows, separator].join("\n");
379
+ }
380
+ const columnWidths = this.calculateColumnWidths(options);
381
+ const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
382
+ const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
383
+ let tableString = headerSeparator + "\n";
384
+ for (const [rowIndex, row] of this.rows.entries()) {
385
+ const { kind, key, value } = row;
386
+ if (kind === "own" || value instanceof _KeyValueASCIITable || value instanceof MultiColumnASCIITable || Array.isArray(value)) {
387
+ const keyString = ASCIITableUtils.centerText(
388
+ key,
389
+ columnWidths[0] + columnWidths[1] + 3
390
+ );
391
+ tableString += `| ${keyString} |
392
+ `;
393
+ tableString += rowSeparator + "\n";
394
+ let valueString = "";
395
+ if (value instanceof _KeyValueASCIITable) {
396
+ valueString = this.formatValue(
397
+ value,
398
+ tableWidth - 4,
399
+ canAutoAdjustWidthWhenPossible,
400
+ ""
401
+ );
402
+ } else if (value instanceof MultiColumnASCIITable) {
403
+ valueString = this.formatValue(
404
+ value,
405
+ tableWidth - 4,
406
+ canAutoAdjustWidthWhenPossible,
407
+ ""
408
+ );
409
+ } else if (Array.isArray(value)) {
410
+ valueString = this.formatValue(
411
+ value,
412
+ tableWidth - 4,
413
+ canAutoAdjustWidthWhenPossible,
414
+ ""
415
+ );
416
+ } else if (row.kind === "own") {
417
+ valueString = this.formatTableRowOnOwnRow(
418
+ value,
419
+ tableWidth - 4,
420
+ tableWidth
421
+ );
422
+ }
423
+ const valueLines = valueString.split("\n");
424
+ const paddedValueLines = valueLines.map((line) => {
425
+ const padding = padRight("", tableWidth - (0, import_string_width3.default)(line) - 4, " ");
426
+ return `| ${line}${padding} |`;
427
+ });
428
+ tableString += paddedValueLines.join("\n") + "\n";
429
+ tableString += headerSeparator + "\n";
430
+ } else {
431
+ const keyLines = ASCIITableUtils.wrapText(key, columnWidths[0]);
432
+ const valueLines = ASCIITableUtils.wrapText(
433
+ this.formatValue(
434
+ value,
435
+ columnWidths[1],
436
+ canAutoAdjustWidthWhenPossible,
437
+ ""
438
+ ),
439
+ columnWidths[1]
440
+ );
441
+ const maxLines = Math.max(keyLines.length, valueLines.length);
442
+ for (let i = 0; i < maxLines; i++) {
443
+ const keyLine = keyLines[i] || "";
444
+ const valueLine = valueLines[i] || "";
445
+ const keyPadding = " ".repeat(columnWidths[0] - (0, import_string_width3.default)(keyLine));
446
+ const valuePadding = " ".repeat(
447
+ columnWidths[1] - (0, import_string_width3.default)(valueLine)
448
+ );
449
+ tableString += `| ${keyLine}${keyPadding} | ${valueLine}${valuePadding} |
450
+ `;
451
+ if (i === maxLines - 1) {
452
+ if (rowIndex === this.rows.length - 1) {
453
+ tableString += headerSeparator + "\n";
454
+ } else {
455
+ tableString += rowSeparator + "\n";
456
+ }
457
+ }
458
+ }
459
+ }
460
+ }
461
+ return tableString.trim();
462
+ }
463
+ calculateColumnWidths(options = {}) {
464
+ const tableWidth = options.tableWidth || this.tableWidth;
465
+ const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
466
+ const columnWidths = [0, 0];
467
+ for (const row of this.rows) {
468
+ const { key, value } = row;
469
+ const keyWidth = (0, import_string_width3.default)(key);
470
+ const maxKeyWidth = Math.floor((tableWidth - 7) / 2);
471
+ if (keyWidth > columnWidths[0]) {
472
+ columnWidths[0] = Math.min(keyWidth, maxKeyWidth);
473
+ columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
474
+ }
475
+ if (typeof value === "string") {
476
+ const valueWidth = Math.max(
477
+ ...value.split("\n").map((line) => (0, import_string_width3.default)(line))
478
+ );
479
+ if (valueWidth > columnWidths[1]) {
480
+ columnWidths[1] = Math.min(
481
+ valueWidth,
482
+ tableWidth - columnWidths[0] - 7
483
+ );
484
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
485
+ }
486
+ } else if (row.kind === "own") {
487
+ let valueWidth = 0;
488
+ if (isString(value)) {
489
+ valueWidth = Math.max(
490
+ ...value.split("\n").map((line) => (0, import_string_width3.default)(line))
491
+ );
492
+ }
493
+ if (valueWidth > columnWidths[1]) {
494
+ columnWidths[1] = Math.min(
495
+ valueWidth,
496
+ tableWidth - columnWidths[0] - 7
497
+ );
498
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
499
+ }
500
+ } else if (value instanceof _KeyValueASCIITable) {
501
+ let nestedTableColumnWidths = [];
502
+ if (canAutoAdjustWidthWhenPossible) {
503
+ const minWidth = value.getMinimumWidth();
504
+ const availableWidth2 = tableWidth - columnWidths[0] - 7;
505
+ const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
506
+ nestedTableColumnWidths = value.calculateColumnWidths({
507
+ tableWidth: adjustedWidth
508
+ });
509
+ } else {
510
+ nestedTableColumnWidths = value.calculateColumnWidths();
511
+ }
512
+ const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
513
+ const availableWidth = tableWidth - columnWidths[0] - 7;
514
+ if (nestedTableWidth > availableWidth) {
515
+ columnWidths[1] = availableWidth;
516
+ } else {
517
+ columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
518
+ }
519
+ } else if (value instanceof MultiColumnASCIITable) {
520
+ let nestedTableColumnWidths = [];
521
+ if (canAutoAdjustWidthWhenPossible) {
522
+ const minWidth = value.getMinimumWidth();
523
+ const availableWidth2 = tableWidth - columnWidths[0] - 7;
524
+ const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
525
+ nestedTableColumnWidths = value.calculateColumnWidths({
526
+ tableWidth: adjustedWidth
527
+ });
528
+ } else {
529
+ nestedTableColumnWidths = value.calculateColumnWidths();
530
+ }
531
+ const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
532
+ const availableWidth = tableWidth - columnWidths[0] - 7;
533
+ if (nestedTableWidth > availableWidth) {
534
+ columnWidths[1] = availableWidth;
535
+ } else {
536
+ columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
537
+ }
538
+ } else if (Array.isArray(value)) {
539
+ for (const nestedCell of value) {
540
+ const nestedKeyWidth = (0, import_string_width3.default)(nestedCell.key);
541
+ const maxNestedKeyWidth = Math.floor((tableWidth - 7) / 2);
542
+ if (nestedKeyWidth > columnWidths[0]) {
543
+ columnWidths[0] = Math.min(nestedKeyWidth, maxNestedKeyWidth);
544
+ columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
545
+ }
546
+ if (typeof nestedCell.value === "string") {
547
+ const nestedValueWidth = Math.max(
548
+ ...nestedCell.value.split("\n").map((line) => (0, import_string_width3.default)(line))
549
+ );
550
+ if (nestedValueWidth > columnWidths[1]) {
551
+ columnWidths[1] = Math.min(
552
+ nestedValueWidth,
553
+ tableWidth - columnWidths[0] - 7
554
+ );
555
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
556
+ }
557
+ }
558
+ }
559
+ }
560
+ }
561
+ return columnWidths;
562
+ }
563
+ formatValue(value, cellWidth, canAutoAdjustWidthWhenPossible, indent = "") {
564
+ if (typeof value === "string") {
565
+ return value;
566
+ } else if (typeof value === "number") {
567
+ return String(value);
568
+ } else if (typeof value === "boolean") {
569
+ return String(value);
570
+ } else if (value === null) {
571
+ return "null";
572
+ } else if (value === void 0) {
573
+ return "undefined";
574
+ } else if (value instanceof _KeyValueASCIITable) {
575
+ let nestedTableLines;
576
+ if (canAutoAdjustWidthWhenPossible) {
577
+ const minWidth = value.getMinimumWidth();
578
+ const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
579
+ nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
580
+ } else {
581
+ nestedTableLines = value.toString().split("\n");
582
+ }
583
+ const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
584
+ return indentedLines.join("\n");
585
+ } else if (value instanceof MultiColumnASCIITable) {
586
+ let nestedTableLines;
587
+ if (canAutoAdjustWidthWhenPossible) {
588
+ const minWidth = value.getMinimumWidth();
589
+ const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
590
+ nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
591
+ } else {
592
+ nestedTableLines = value.toString().split("\n");
593
+ }
594
+ const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
595
+ return indentedLines.join("\n");
596
+ } else if (Array.isArray(value)) {
597
+ const nestedValueLines = [];
598
+ for (const { key, value: nestedValue } of value) {
599
+ const formattedKey = `${indent}${key}:`;
600
+ const formattedValue = this.formatValue(
601
+ nestedValue,
602
+ cellWidth - indent.length - (0, import_string_width3.default)(key) - 2,
603
+ canAutoAdjustWidthWhenPossible,
604
+ `${indent}`
605
+ );
606
+ const wrappedSpacer = padRight("", 4, " ");
607
+ const wrappedValue = formattedValue.split("\n").map((line) => `${indent}${wrappedSpacer}${line}`);
608
+ nestedValueLines.push(formattedKey);
609
+ nestedValueLines.push(...wrappedValue);
610
+ nestedValueLines.push("");
611
+ }
612
+ return nestedValueLines.slice(0, -1).join("\n");
613
+ } else {
614
+ throw new TypeError("Invalid value type provided");
615
+ }
616
+ }
617
+ formatTableRowOnOwnRow(value, width, maxRowLength) {
618
+ const lines = value.split("\n");
619
+ const paddedLines = lines.map((line) => {
620
+ const wrappedLines = ASCIITableUtils.wrapText(line, maxRowLength - 4);
621
+ return wrappedLines.map((wrappedLine) => {
622
+ const padding = padRight(
623
+ "",
624
+ width - (0, import_string_width3.default)(wrappedLine) - 2,
625
+ " "
626
+ );
627
+ return `${wrappedLine}${padding}`;
628
+ }).join("\n");
629
+ });
630
+ return paddedLines.join("\n");
631
+ }
632
+ };
633
+
634
+ // src/lib/error-to-string.ts
635
+ function safeStringify(value) {
636
+ if (value === null || value === void 0) {
637
+ return String(value);
638
+ }
639
+ switch (typeof value) {
640
+ case "string":
641
+ return value;
642
+ case "number":
643
+ case "boolean":
644
+ case "bigint":
645
+ return String(value);
646
+ case "object":
647
+ return JSON.stringify(value);
648
+ case "function":
649
+ return "[Function]";
650
+ case "symbol":
651
+ return value.toString();
652
+ default:
653
+ return String(value);
654
+ }
655
+ }
656
+ function errorToString(error, maxRowLength = 80) {
657
+ const table = errorToASCIITable(error, maxRowLength);
658
+ return table.toString();
659
+ }
660
+ function errorToASCIITable(error, maxRowLength) {
661
+ const table = new KeyValueASCIITable({
662
+ tableWidth: maxRowLength,
663
+ autoAdjustWidthWhenPossible: true
664
+ });
665
+ if (error && typeof error === "object") {
666
+ const err = error;
667
+ table.addRow("Key", "Value");
668
+ if (err["message"]) {
669
+ table.addRow("Message", safeStringify(err["message"]));
670
+ }
671
+ if (err["name"]) {
672
+ table.addRow("Name", safeStringify(err["name"]));
673
+ }
674
+ if (err["code"]) {
675
+ table.addRow("Code", safeStringify(err["code"]));
676
+ }
677
+ if (err["errno"]) {
678
+ table.addRow("Errno", safeStringify(err["errno"]));
679
+ }
680
+ if (err["errPrefix"]) {
681
+ table.addRow("Prefix", safeStringify(err["errPrefix"]));
682
+ }
683
+ if (err["errType"]) {
684
+ table.addRow("errType", safeStringify(err["errType"]));
685
+ }
686
+ if (err["errCode"]) {
687
+ table.addRow("errCode", safeStringify(err["errCode"]));
688
+ }
689
+ if (err["additionalInfo"]) {
690
+ const additionalInfo = err["additionalInfo"];
691
+ const sensitiveFieldNames = err["sensitiveFieldNames"] || [];
692
+ for (const key in additionalInfo) {
693
+ if (sensitiveFieldNames.includes(key)) {
694
+ table.addRow(`AdditionalInfo.${key}`, "***");
695
+ } else {
696
+ const value = additionalInfo[key];
697
+ table.addRow(
698
+ `AdditionalInfo.${key}`,
699
+ stringifyValue(value, table, maxRowLength)
700
+ );
701
+ }
702
+ }
703
+ }
704
+ if (err["stack"]) {
705
+ table.addValueOnSeparateRow("Stack", safeStringify(err["stack"]));
706
+ }
707
+ }
708
+ return table;
709
+ }
710
+ function stringifyValue(value, table, maxRowLength) {
711
+ if (typeof value === "string") {
712
+ return value;
713
+ } else if (Array.isArray(value)) {
714
+ return value.map((item) => {
715
+ const result = stringifyValue(item, table, maxRowLength);
716
+ if (typeof result === "string") {
717
+ return result;
718
+ } else if (result instanceof KeyValueASCIITable) {
719
+ return result.toString();
720
+ } else {
721
+ return JSON.stringify(result);
722
+ }
723
+ }).join(", ");
724
+ } else if (typeof value === "object" && value !== null) {
725
+ if (value instanceof Error) {
726
+ return errorToASCIITable(value, maxRowLength - 4);
727
+ } else {
728
+ const entries = Object.entries(value).map(
729
+ ([key, val]) => ({
730
+ key,
731
+ value: stringifyValue(val, table, maxRowLength - 4)
732
+ })
733
+ );
734
+ return entries;
735
+ }
736
+ } else {
737
+ return String(value);
738
+ }
739
+ }
740
+
741
+ // src/lib/is-promise.ts
742
+ function isPromise(obj) {
743
+ return !!obj && (typeof obj === "object" || typeof obj === "function") && // @ts-expect-error - obj is checked to be object/function, then property access works at runtime
744
+ typeof obj["then"] === "function";
745
+ }
746
+
747
+ // src/lib/is-function.ts
748
+ function isFunction(value) {
749
+ return typeof value === "function" || value instanceof Function;
750
+ }
751
+
752
+ // src/lib/safe-handle-callback.ts
753
+ function safeHandleCallback(callbackName, callback, ...args) {
754
+ const handleError = (error) => {
755
+ if (typeof globalThis.dispatchEvent === "function") {
756
+ globalThis.dispatchEvent(
757
+ new ErrorEvent("reportError", {
758
+ error: new Error(
759
+ `Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
760
+ )
761
+ })
762
+ );
763
+ }
764
+ };
765
+ if (isFunction(callback)) {
766
+ try {
767
+ const result = callback(...args);
768
+ if (isPromise(result)) {
769
+ result.catch((error) => {
770
+ handleError(error);
771
+ });
772
+ }
773
+ } catch (error) {
774
+ handleError(error);
775
+ }
776
+ } else {
777
+ handleError(
778
+ new Error(`Callback provided for ${callbackName} is not a function`)
779
+ );
780
+ }
781
+ }
782
+ async function safeHandleCallbackAndWait(callbackName, callback, ...args) {
783
+ const handleError = (error) => {
784
+ if (typeof globalThis.dispatchEvent === "function") {
785
+ globalThis.dispatchEvent(
786
+ new ErrorEvent("reportError", {
787
+ error: new Error(
788
+ `Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
789
+ )
790
+ })
791
+ );
792
+ }
793
+ return { success: false, error };
794
+ };
795
+ if (isFunction(callback)) {
796
+ try {
797
+ const result = callback(...args);
798
+ if (isPromise(result)) {
799
+ const value = await result;
800
+ return { success: true, value };
801
+ } else {
802
+ return { success: true, value: result };
803
+ }
804
+ } catch (error) {
805
+ return handleError(error);
806
+ }
807
+ } else {
808
+ return handleError(
809
+ new Error(`Callback provided for ${callbackName} is not a function`)
810
+ );
811
+ }
812
+ }
813
+ // Annotate the CommonJS export names for ESM import in node:
814
+ 0 && (module.exports = {
815
+ safeHandleCallback,
816
+ safeHandleCallbackAndWait
817
+ });
818
+ //# sourceMappingURL=safe-handle-callback.cjs.map