@gjsify/util 0.0.4 → 0.1.0

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/lib/esm/index.js CHANGED
@@ -1,6 +1,580 @@
1
- export * from "@gjsify/deno_std/node/util";
2
- import util from "@gjsify/deno_std/node/util";
3
- var src_default = util;
1
+ import * as types from "./types.js";
2
+ import { getSystemErrorName, getSystemErrorMap } from "./errors.js";
3
+ const kCustomInspect = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
4
+ function inspectValue(value, opts, depth) {
5
+ if (value === null) return opts.colors ? "\x1B[1mnull\x1B[22m" : "null";
6
+ if (value === void 0) return opts.colors ? "\x1B[90mundefined\x1B[39m" : "undefined";
7
+ const maxDepth = opts.depth ?? 2;
8
+ if (typeof value === "string") {
9
+ const escaped = value.replace(/\\/g, "\\\\");
10
+ if (value.includes("'") && !value.includes('"')) {
11
+ const dq = escaped.replace(/"/g, '\\"');
12
+ return opts.colors ? `\x1B[32m"${dq}"\x1B[39m` : `"${dq}"`;
13
+ }
14
+ const sq = escaped.replace(/'/g, "\\'");
15
+ return opts.colors ? `\x1B[32m'${sq}'\x1B[39m` : `'${sq}'`;
16
+ }
17
+ if (typeof value === "number") {
18
+ return opts.colors ? `\x1B[33m${value}\x1B[39m` : String(value);
19
+ }
20
+ if (typeof value === "bigint") {
21
+ return opts.colors ? `\x1B[33m${value}n\x1B[39m` : `${value}n`;
22
+ }
23
+ if (typeof value === "boolean") {
24
+ return opts.colors ? `\x1B[33m${value}\x1B[39m` : String(value);
25
+ }
26
+ if (typeof value === "symbol") {
27
+ return opts.colors ? `\x1B[32m${value.toString()}\x1B[39m` : value.toString();
28
+ }
29
+ if (typeof value === "function") {
30
+ const name = value.name ? `: ${value.name}` : "";
31
+ return opts.colors ? `\x1B[36m[Function${name}]\x1B[39m` : `[Function${name}]`;
32
+ }
33
+ if (value !== null && typeof value === "object" && kCustomInspect in value) {
34
+ const custom = value[kCustomInspect];
35
+ if (typeof custom === "function") {
36
+ const result = custom.call(value, depth, opts);
37
+ if (typeof result === "string") return result;
38
+ return inspectValue(result, opts, depth);
39
+ }
40
+ }
41
+ if (value instanceof Date) {
42
+ return value.toISOString();
43
+ }
44
+ if (value instanceof RegExp) {
45
+ return opts.colors ? `\x1B[31m${value.toString()}\x1B[39m` : value.toString();
46
+ }
47
+ if (value instanceof Error) {
48
+ return value.stack || value.toString();
49
+ }
50
+ if (depth > maxDepth) {
51
+ return Array.isArray(value) ? "[Array]" : "[Object]";
52
+ }
53
+ if (Array.isArray(value)) {
54
+ return inspectArray(value, opts, depth);
55
+ }
56
+ if (value instanceof Map) {
57
+ const entries = [...value.entries()].map(
58
+ ([k, v]) => `${inspectValue(k, opts, depth + 1)} => ${inspectValue(v, opts, depth + 1)}`
59
+ );
60
+ return `Map(${value.size}) { ${entries.join(", ")} }`;
61
+ }
62
+ if (value instanceof Set) {
63
+ const entries = [...value].map((v) => inspectValue(v, opts, depth + 1));
64
+ return `Set(${value.size}) { ${entries.join(", ")} }`;
65
+ }
66
+ if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
67
+ const name = value.constructor?.name || "TypedArray";
68
+ const arr = Array.from(value);
69
+ return `${name}(${arr.length}) [ ${arr.join(", ")} ]`;
70
+ }
71
+ return inspectObject(value, opts, depth);
72
+ }
73
+ function inspectArray(arr, opts, depth) {
74
+ const maxLen = opts.maxArrayLength ?? 100;
75
+ const len = Math.min(arr.length, maxLen);
76
+ const items = [];
77
+ for (let i = 0; i < len; i++) {
78
+ items.push(inspectValue(arr[i], opts, depth + 1));
79
+ }
80
+ if (arr.length > maxLen) {
81
+ items.push(`... ${arr.length - maxLen} more items`);
82
+ }
83
+ if (opts.showHidden) {
84
+ items.push(`[length]: ${arr.length}`);
85
+ }
86
+ const breakLength = opts.breakLength ?? 72;
87
+ const compact = opts.compact ?? 3;
88
+ if (typeof compact === "number" && compact > 0 && arr.length > compact) {
89
+ const indent = " ";
90
+ const indentLen = indent.length;
91
+ const stripAnsi = (s) => s.replace(/\x1b\[[0-9;]*m/g, "");
92
+ const maxItemLen = Math.max(...items.map((item) => stripAnsi(item).length));
93
+ const biasedMax = Math.max(maxItemLen - 2, 1);
94
+ const numItems = items.length;
95
+ const approxCharHeights = 2.5;
96
+ const columns = Math.min(
97
+ Math.round(Math.sqrt(approxCharHeights * biasedMax * numItems) / biasedMax),
98
+ Math.floor((breakLength - indentLen) / biasedMax),
99
+ Math.floor((2.5 + numItems - 1) / 2),
100
+ 15
101
+ );
102
+ if (columns > 1) {
103
+ const rows = [];
104
+ for (let i = 0; i < numItems; i += columns) {
105
+ rows.push(indent + items.slice(i, Math.min(i + columns, numItems)).join(", "));
106
+ }
107
+ return `[
108
+ ${rows.join(",\n")}
109
+ ]`;
110
+ }
111
+ }
112
+ const singleLine = `[ ${items.join(", ")} ]`;
113
+ if (singleLine.length <= breakLength) return singleLine;
114
+ return `[
115
+ ${items.map((i) => " " + i).join(",\n")}
116
+ ]`;
117
+ }
118
+ function inspectObject(obj, opts, depth) {
119
+ const keys = opts.showHidden ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
120
+ if (opts.sorted) keys.sort();
121
+ if (keys.length === 0) {
122
+ const tag = Object.prototype.toString.call(obj);
123
+ if (tag !== "[object Object]") return tag;
124
+ return "{}";
125
+ }
126
+ const items = keys.map((key) => {
127
+ const val = inspectValue(obj[key], opts, depth + 1);
128
+ return `${key}: ${val}`;
129
+ });
130
+ const breakLength = opts.breakLength ?? 72;
131
+ const singleLine = `{ ${items.join(", ")} }`;
132
+ if (singleLine.length <= breakLength) return singleLine;
133
+ return `{
134
+ ${items.map((i) => " " + i).join(",\n")}
135
+ }`;
136
+ }
137
+ function inspect(value, opts) {
138
+ const options = typeof opts === "boolean" ? { showHidden: opts } : { ...opts };
139
+ if (options.colors === void 0) options.colors = false;
140
+ return inspectValue(value, options, 0);
141
+ }
142
+ inspect.custom = kCustomInspect;
143
+ inspect.defaultOptions = {
144
+ showHidden: false,
145
+ depth: 2,
146
+ colors: false,
147
+ maxArrayLength: 100,
148
+ maxStringLength: 1e4,
149
+ breakLength: 72,
150
+ compact: 3,
151
+ sorted: false
152
+ };
153
+ inspect.colors = {
154
+ reset: [0, 0],
155
+ bold: [1, 22],
156
+ dim: [2, 22],
157
+ italic: [3, 23],
158
+ underline: [4, 24],
159
+ blink: [5, 25],
160
+ inverse: [7, 27],
161
+ hidden: [8, 28],
162
+ strikethrough: [9, 29],
163
+ doubleunderline: [21, 24],
164
+ black: [30, 39],
165
+ red: [31, 39],
166
+ green: [32, 39],
167
+ yellow: [33, 39],
168
+ blue: [34, 39],
169
+ magenta: [35, 39],
170
+ cyan: [36, 39],
171
+ white: [37, 39],
172
+ bgBlack: [40, 49],
173
+ bgRed: [41, 49],
174
+ bgGreen: [42, 49],
175
+ bgYellow: [43, 49],
176
+ bgBlue: [44, 49],
177
+ bgMagenta: [45, 49],
178
+ bgCyan: [46, 49],
179
+ bgWhite: [47, 49],
180
+ framed: [51, 54],
181
+ overlined: [53, 55],
182
+ gray: [90, 39],
183
+ grey: [90, 39],
184
+ redBright: [91, 39],
185
+ greenBright: [92, 39],
186
+ yellowBright: [93, 39],
187
+ blueBright: [94, 39],
188
+ magentaBright: [95, 39],
189
+ cyanBright: [96, 39],
190
+ whiteBright: [97, 39],
191
+ bgBlackBright: [100, 49],
192
+ bgRedBright: [101, 49],
193
+ bgGreenBright: [102, 49],
194
+ bgYellowBright: [103, 49],
195
+ bgBlueBright: [104, 49],
196
+ bgMagentaBright: [105, 49],
197
+ bgCyanBright: [106, 49],
198
+ bgWhiteBright: [107, 49]
199
+ };
200
+ inspect.styles = {
201
+ special: "cyan",
202
+ number: "yellow",
203
+ bigint: "yellow",
204
+ boolean: "yellow",
205
+ undefined: "grey",
206
+ null: "bold",
207
+ string: "green",
208
+ symbol: "green",
209
+ date: "magenta",
210
+ regexp: "red",
211
+ module: "underline"
212
+ };
213
+ function format(fmt, ...args) {
214
+ if (fmt === void 0 && args.length === 0) return "";
215
+ if (typeof fmt !== "string") {
216
+ if (args.length === 0) return inspect(fmt);
217
+ const parts = [inspect(fmt)];
218
+ for (const arg of args) parts.push(inspect(arg));
219
+ return parts.join(" ");
220
+ }
221
+ let i = 0;
222
+ let result = "";
223
+ let lastIdx = 0;
224
+ for (let p = 0; p < fmt.length - 1; p++) {
225
+ if (fmt[p] !== "%") continue;
226
+ if (p > lastIdx) result += fmt.slice(lastIdx, p);
227
+ const next = fmt[p + 1];
228
+ if (next === "%") {
229
+ result += "%";
230
+ lastIdx = p + 2;
231
+ p++;
232
+ continue;
233
+ }
234
+ if (i >= args.length) {
235
+ result += "%" + next;
236
+ lastIdx = p + 2;
237
+ p++;
238
+ continue;
239
+ }
240
+ const arg = args[i];
241
+ switch (next) {
242
+ case "s": {
243
+ if (typeof arg === "bigint") {
244
+ result += `${arg}n`;
245
+ } else if (typeof arg === "symbol") {
246
+ result += arg.toString();
247
+ } else if (typeof arg === "number" && Object.is(arg, -0)) {
248
+ result += "-0";
249
+ } else if (typeof arg === "object" && arg !== null) {
250
+ const proto = Object.getPrototypeOf(arg);
251
+ if (proto === null || typeof arg.toString === "function" && arg.toString !== Object.prototype.toString && arg.toString !== Array.prototype.toString) {
252
+ try {
253
+ const str = arg.toString();
254
+ if (typeof str === "string" && str !== "[object Object]") {
255
+ result += str;
256
+ } else {
257
+ result += inspect(arg, { depth: 0 });
258
+ }
259
+ } catch {
260
+ result += inspect(arg, { depth: 0 });
261
+ }
262
+ } else {
263
+ result += inspect(arg, { depth: 0 });
264
+ }
265
+ } else {
266
+ result += String(arg);
267
+ }
268
+ i++;
269
+ break;
270
+ }
271
+ case "d": {
272
+ if (typeof arg === "bigint") {
273
+ result += `${arg}n`;
274
+ } else if (typeof arg === "symbol") {
275
+ result += "NaN";
276
+ } else {
277
+ const n = Number(arg);
278
+ result += Object.is(n, -0) ? "-0" : String(n);
279
+ }
280
+ i++;
281
+ break;
282
+ }
283
+ case "i": {
284
+ if (typeof arg === "bigint") {
285
+ result += `${arg}n`;
286
+ } else if (typeof arg === "symbol") {
287
+ result += "NaN";
288
+ } else {
289
+ const n = Number(arg);
290
+ if (!isFinite(n)) {
291
+ result += "NaN";
292
+ } else {
293
+ const truncated = Math.trunc(n);
294
+ result += Object.is(truncated, -0) ? "-0" : String(truncated);
295
+ }
296
+ }
297
+ i++;
298
+ break;
299
+ }
300
+ case "f": {
301
+ if (typeof arg === "bigint") {
302
+ result += Number(arg).toString();
303
+ } else if (typeof arg === "symbol") {
304
+ result += "NaN";
305
+ } else {
306
+ const n = parseFloat(String(arg));
307
+ result += Object.is(n, -0) ? "-0" : String(n);
308
+ }
309
+ i++;
310
+ break;
311
+ }
312
+ case "j":
313
+ try {
314
+ result += JSON.stringify(args[i++]);
315
+ } catch {
316
+ result += "[Circular]";
317
+ }
318
+ break;
319
+ case "o":
320
+ result += inspect(args[i++], { showHidden: true, depth: 4 });
321
+ break;
322
+ case "O":
323
+ result += inspect(args[i++], { depth: 4 });
324
+ break;
325
+ default:
326
+ result += "%" + next;
327
+ break;
328
+ }
329
+ lastIdx = p + 2;
330
+ p++;
331
+ }
332
+ if (lastIdx < fmt.length) {
333
+ result += fmt.slice(lastIdx);
334
+ }
335
+ for (; i < args.length; i++) {
336
+ const arg = args[i];
337
+ if (typeof arg === "string") {
338
+ result += " " + arg;
339
+ } else {
340
+ result += " " + inspect(arg);
341
+ }
342
+ }
343
+ return result;
344
+ }
345
+ function formatWithOptions(inspectOptions, fmt, ...args) {
346
+ return format(fmt, ...args);
347
+ }
348
+ const kCustomPromisify = /* @__PURE__ */ Symbol.for("nodejs.util.promisify.custom");
349
+ function promisify(fn) {
350
+ if (typeof fn !== "function") {
351
+ throw new TypeError('The "original" argument must be of type Function');
352
+ }
353
+ const custom = fn[kCustomPromisify];
354
+ if (typeof custom === "function") return custom;
355
+ function promisified(...args) {
356
+ return new Promise((resolve, reject) => {
357
+ fn.call(this, ...args, (err, ...values) => {
358
+ if (err) {
359
+ reject(err);
360
+ } else if (values.length <= 1) {
361
+ resolve(values[0]);
362
+ } else {
363
+ resolve(values);
364
+ }
365
+ });
366
+ });
367
+ }
368
+ Object.setPrototypeOf(promisified, Object.getPrototypeOf(fn));
369
+ Object.defineProperty(promisified, kCustomPromisify, { value: promisified });
370
+ return promisified;
371
+ }
372
+ promisify.custom = kCustomPromisify;
373
+ function callbackify(fn) {
374
+ if (typeof fn !== "function") {
375
+ throw new TypeError('The "original" argument must be of type Function');
376
+ }
377
+ return function(...args) {
378
+ const callback = args.pop();
379
+ if (typeof callback !== "function") {
380
+ throw new TypeError("The last argument must be of type Function");
381
+ }
382
+ fn.apply(this, args).then(
383
+ (result) => Promise.resolve().then(() => callback(null, result)),
384
+ (err) => Promise.resolve().then(() => callback(err || new Error()))
385
+ );
386
+ };
387
+ }
388
+ function deprecate(fn, msg, code) {
389
+ let warned = false;
390
+ function deprecated(...args) {
391
+ if (!warned) {
392
+ warned = true;
393
+ const warning = code ? `[${code}] ${msg}` : msg;
394
+ console.warn(`DeprecationWarning: ${warning}`);
395
+ }
396
+ return fn.apply(this, args);
397
+ }
398
+ Object.setPrototypeOf(deprecated, fn);
399
+ return deprecated;
400
+ }
401
+ function debuglog(section) {
402
+ let debug;
403
+ return (...args) => {
404
+ if (debug === void 0) {
405
+ const nodeDebug = typeof globalThis.process?.env?.NODE_DEBUG === "string" ? globalThis.process.env.NODE_DEBUG : "";
406
+ const regex = new RegExp(`\\b${section}\\b`, "i");
407
+ if (regex.test(nodeDebug)) {
408
+ const pid = typeof globalThis.process?.pid === "number" ? globalThis.process.pid : 0;
409
+ debug = (...a) => {
410
+ console.error(`${section.toUpperCase()} ${pid}:`, ...a);
411
+ };
412
+ } else {
413
+ debug = () => {
414
+ };
415
+ }
416
+ }
417
+ debug(...args);
418
+ };
419
+ }
420
+ function inherits(ctor, superCtor) {
421
+ if (ctor === void 0 || ctor === null) {
422
+ throw new TypeError('The constructor to "inherits" must not be null or undefined');
423
+ }
424
+ if (superCtor === void 0 || superCtor === null) {
425
+ throw new TypeError('The super constructor to "inherits" must not be null or undefined');
426
+ }
427
+ if (superCtor.prototype === void 0) {
428
+ throw new TypeError('The super constructor to "inherits" must have a prototype');
429
+ }
430
+ Object.defineProperty(ctor, "super_", { value: superCtor, writable: true, configurable: true });
431
+ Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
432
+ }
433
+ function isBoolean(value) {
434
+ return typeof value === "boolean";
435
+ }
436
+ function isNull(value) {
437
+ return value === null;
438
+ }
439
+ function isNullOrUndefined(value) {
440
+ return value == null;
441
+ }
442
+ function isNumber(value) {
443
+ return typeof value === "number";
444
+ }
445
+ function isString(value) {
446
+ return typeof value === "string";
447
+ }
448
+ function isSymbol(value) {
449
+ return typeof value === "symbol";
450
+ }
451
+ function isUndefined(value) {
452
+ return value === void 0;
453
+ }
454
+ function isObject(value) {
455
+ return value !== null && typeof value === "object";
456
+ }
457
+ function isError(value) {
458
+ return value instanceof Error;
459
+ }
460
+ function isFunction(value) {
461
+ return typeof value === "function";
462
+ }
463
+ function isRegExp(value) {
464
+ return value instanceof RegExp;
465
+ }
466
+ function isArray(value) {
467
+ return Array.isArray(value);
468
+ }
469
+ function isPrimitive(value) {
470
+ return value === null || typeof value !== "object" && typeof value !== "function";
471
+ }
472
+ function isDate(value) {
473
+ return value instanceof Date;
474
+ }
475
+ function isBuffer(value) {
476
+ return value instanceof Uint8Array && value.constructor?.name === "Buffer";
477
+ }
478
+ const TextDecoder = globalThis.TextDecoder;
479
+ const TextEncoder = globalThis.TextEncoder;
480
+ function isDeepStrictEqual(a, b) {
481
+ if (Object.is(a, b)) return true;
482
+ if (typeof a !== typeof b) return false;
483
+ if (a === null || b === null) return false;
484
+ if (typeof a !== "object") return false;
485
+ const aObj = a;
486
+ const bObj = b;
487
+ if (Array.isArray(aObj) && Array.isArray(bObj)) {
488
+ if (aObj.length !== bObj.length) return false;
489
+ for (let i = 0; i < aObj.length; i++) {
490
+ if (!isDeepStrictEqual(aObj[i], bObj[i])) return false;
491
+ }
492
+ return true;
493
+ }
494
+ if (Array.isArray(aObj) !== Array.isArray(bObj)) return false;
495
+ if (aObj instanceof Date && bObj instanceof Date) {
496
+ return aObj.getTime() === bObj.getTime();
497
+ }
498
+ if (aObj instanceof RegExp && bObj instanceof RegExp) {
499
+ return aObj.source === bObj.source && aObj.flags === bObj.flags;
500
+ }
501
+ const aKeys = Object.keys(aObj);
502
+ const bKeys = Object.keys(bObj);
503
+ if (aKeys.length !== bKeys.length) return false;
504
+ for (const key of aKeys) {
505
+ if (!Object.prototype.hasOwnProperty.call(bObj, key)) return false;
506
+ if (!isDeepStrictEqual(aObj[key], bObj[key])) return false;
507
+ }
508
+ return true;
509
+ }
510
+ function toUSVString(string) {
511
+ if (typeof string.toWellFormed === "function") {
512
+ return string.toWellFormed();
513
+ }
514
+ return string.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "\uFFFD");
515
+ }
516
+ var index_default = {
517
+ format,
518
+ formatWithOptions,
519
+ inspect,
520
+ promisify,
521
+ callbackify,
522
+ deprecate,
523
+ debuglog,
524
+ inherits,
525
+ types,
526
+ isBoolean,
527
+ isNull,
528
+ isNullOrUndefined,
529
+ isNumber,
530
+ isString,
531
+ isSymbol,
532
+ isUndefined,
533
+ isObject,
534
+ isError,
535
+ isFunction,
536
+ isRegExp,
537
+ isArray,
538
+ isPrimitive,
539
+ isDate,
540
+ isBuffer,
541
+ isDeepStrictEqual,
542
+ toUSVString,
543
+ TextDecoder: globalThis.TextDecoder,
544
+ TextEncoder: globalThis.TextEncoder,
545
+ getSystemErrorName,
546
+ getSystemErrorMap
547
+ };
4
548
  export {
5
- src_default as default
549
+ TextDecoder,
550
+ TextEncoder,
551
+ callbackify,
552
+ debuglog,
553
+ index_default as default,
554
+ deprecate,
555
+ format,
556
+ formatWithOptions,
557
+ getSystemErrorMap,
558
+ getSystemErrorName,
559
+ inherits,
560
+ inspect,
561
+ isArray,
562
+ isBoolean,
563
+ isBuffer,
564
+ isDate,
565
+ isDeepStrictEqual,
566
+ isError,
567
+ isFunction,
568
+ isNull,
569
+ isNullOrUndefined,
570
+ isNumber,
571
+ isObject,
572
+ isPrimitive,
573
+ isRegExp,
574
+ isString,
575
+ isSymbol,
576
+ isUndefined,
577
+ promisify,
578
+ toUSVString,
579
+ types
6
580
  };