1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import {
validateObject,
validateOneOf,
validateString,
} from "ext:deno_node/internal/validators.mjs";
import { codes } from "ext:deno_node/internal/error_codes.ts";
import {
colors,
createStylizeWithColor,
formatBigInt,
formatNumber,
formatValue,
styles,
} from "ext:deno_console/01_console.js";
// Set Graphics Rendition https://en.wikipedia.org/wiki/ANSI_escape_code#graphics
// Each color consists of an array with the color code as first entry and the
// reset code as second entry.
const defaultFG = 39;
const defaultBG = 49;
inspect.colors = {
reset: [0, 0],
bold: [1, 22],
dim: [2, 22], // Alias: faint
italic: [3, 23],
underline: [4, 24],
blink: [5, 25],
// Swap foreground and background colors
inverse: [7, 27], // Alias: swapcolors, swapColors
hidden: [8, 28], // Alias: conceal
strikethrough: [9, 29], // Alias: strikeThrough, crossedout, crossedOut
doubleunderline: [21, 24], // Alias: doubleUnderline
black: [30, defaultFG],
red: [31, defaultFG],
green: [32, defaultFG],
yellow: [33, defaultFG],
blue: [34, defaultFG],
magenta: [35, defaultFG],
cyan: [36, defaultFG],
white: [37, defaultFG],
bgBlack: [40, defaultBG],
bgRed: [41, defaultBG],
bgGreen: [42, defaultBG],
bgYellow: [43, defaultBG],
bgBlue: [44, defaultBG],
bgMagenta: [45, defaultBG],
bgCyan: [46, defaultBG],
bgWhite: [47, defaultBG],
framed: [51, 54],
overlined: [53, 55],
gray: [90, defaultFG], // Alias: grey, blackBright
redBright: [91, defaultFG],
greenBright: [92, defaultFG],
yellowBright: [93, defaultFG],
blueBright: [94, defaultFG],
magentaBright: [95, defaultFG],
cyanBright: [96, defaultFG],
whiteBright: [97, defaultFG],
bgGray: [100, defaultBG], // Alias: bgGrey, bgBlackBright
bgRedBright: [101, defaultBG],
bgGreenBright: [102, defaultBG],
bgYellowBright: [103, defaultBG],
bgBlueBright: [104, defaultBG],
bgMagentaBright: [105, defaultBG],
bgCyanBright: [106, defaultBG],
bgWhiteBright: [107, defaultBG],
};
function defineColorAlias(target, alias) {
Object.defineProperty(inspect.colors, alias, {
get() {
return this[target];
},
set(value) {
this[target] = value;
},
configurable: true,
enumerable: false,
});
}
defineColorAlias("gray", "grey");
defineColorAlias("gray", "blackBright");
defineColorAlias("bgGray", "bgGrey");
defineColorAlias("bgGray", "bgBlackBright");
defineColorAlias("dim", "faint");
defineColorAlias("strikethrough", "crossedout");
defineColorAlias("strikethrough", "strikeThrough");
defineColorAlias("strikethrough", "crossedOut");
defineColorAlias("hidden", "conceal");
defineColorAlias("inverse", "swapColors");
defineColorAlias("inverse", "swapcolors");
defineColorAlias("doubleunderline", "doubleUnderline");
// TODO(BridgeAR): Add function style support for more complex styles.
// Don't use 'blue' not visible on cmd.exe
inspect.styles = Object.assign(Object.create(null), {
special: "cyan",
number: "yellow",
bigint: "yellow",
boolean: "yellow",
undefined: "grey",
null: "bold",
string: "green",
symbol: "green",
date: "magenta",
// "name": intentionally not styling
// TODO(BridgeAR): Highlight regular expressions properly.
regexp: "red",
module: "underline",
});
const inspectDefaultOptions = {
indentationLvl: 0,
currentDepth: 0,
stylize: stylizeNoColor,
showHidden: false,
depth: 2,
colors: false,
showProxy: false,
breakLength: 80,
escapeSequences: true,
compact: 3,
sorted: false,
getters: false,
// node only
maxArrayLength: 100,
maxStringLength: 10000, // deno: strAbbreviateSize: 100
customInspect: true,
// deno only
/** You can override the quotes preference in inspectString.
* Used by util.inspect() */
// TODO(kt3k): Consider using symbol as a key to hide this from the public
// API.
quotes: ["'", '"', "`"],
iterableLimit: Infinity, // similar to node's maxArrayLength, but doesn't only apply to arrays
trailingComma: false,
inspect,
// TODO(@crowlKats): merge into indentationLvl
indentLevel: 0,
};
/**
* Echos the value of any input. Tries to print the value out
* in the best way possible given the different types.
*/
/* Legacy: value, showHidden, depth, colors */
export function inspect(value, opts) {
// Default options
const ctx = {
budget: {},
seen: [],
...inspectDefaultOptions,
};
if (arguments.length > 1) {
// Legacy...
if (arguments.length > 2) {
if (arguments[2] !== undefined) {
ctx.depth = arguments[2];
}
if (arguments.length > 3 && arguments[3] !== undefined) {
ctx.colors = arguments[3];
}
}
// Set user-specified options
if (typeof opts === "boolean") {
ctx.showHidden = opts;
} else if (opts) {
const optKeys = Object.keys(opts);
for (let i = 0; i < optKeys.length; ++i) {
const key = optKeys[i];
// TODO(BridgeAR): Find a solution what to do about stylize. Either make
// this function public or add a new API with a similar or better
// functionality.
if (
// deno-lint-ignore no-prototype-builtins
inspectDefaultOptions.hasOwnProperty(key) ||
key === "stylize"
) {
ctx[key] = opts[key];
} else if (ctx.userOptions === undefined) {
// This is required to pass through the actual user input.
ctx.userOptions = opts;
}
}
}
}
if (ctx.colors) {
ctx.stylize = createStylizeWithColor(inspect.styles, inspect.colors);
}
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
return formatValue(ctx, value, 0);
}
const customInspectSymbol = Symbol.for("nodejs.util.inspect.custom");
inspect.custom = customInspectSymbol;
Object.defineProperty(inspect, "defaultOptions", {
get() {
return inspectDefaultOptions;
},
set(options) {
validateObject(options, "options");
return Object.assign(inspectDefaultOptions, options);
},
});
function stylizeNoColor(str) {
return str;
}
const builtInObjects = new Set(
Object.getOwnPropertyNames(globalThis).filter((e) =>
/^[A-Z][a-zA-Z0-9]+$/.test(e)
),
);
// Regex used for ansi escape code splitting
// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
// Matches all ansi escape code sequences in a string
const ansiPattern = "[\\u001B\\u009B][[\\]()#;?]*" +
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*" +
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" +
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))";
const ansi = new RegExp(ansiPattern, "g");
/**
* Returns the number of columns required to display the given string.
*/
export function getStringWidth(str, removeControlChars = true) {
let width = 0;
if (removeControlChars) {
str = stripVTControlCharacters(str);
}
str = str.normalize("NFC");
for (const char of str[Symbol.iterator]()) {
const code = char.codePointAt(0);
if (isFullWidthCodePoint(code)) {
width += 2;
} else if (!isZeroWidthCodePoint(code)) {
width++;
}
}
return width;
}
/**
* Returns true if the character represented by a given
* Unicode code point is full-width. Otherwise returns false.
*/
const isFullWidthCodePoint = (code) => {
// Code points are partially derived from:
// https://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
return code >= 0x1100 && (
code <= 0x115f || // Hangul Jamo
code === 0x2329 || // LEFT-POINTING ANGLE BRACKET
code === 0x232a || // RIGHT-POINTING ANGLE BRACKET
// CJK Radicals Supplement .. Enclosed CJK Letters and Months
(code >= 0x2e80 && code <= 0x3247 && code !== 0x303f) ||
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
(code >= 0x3250 && code <= 0x4dbf) ||
// CJK Unified Ideographs .. Yi Radicals
(code >= 0x4e00 && code <= 0xa4c6) ||
// Hangul Jamo Extended-A
(code >= 0xa960 && code <= 0xa97c) ||
// Hangul Syllables
(code >= 0xac00 && code <= 0xd7a3) ||
// CJK Compatibility Ideographs
(code >= 0xf900 && code <= 0xfaff) ||
// Vertical Forms
(code >= 0xfe10 && code <= 0xfe19) ||
// CJK Compatibility Forms .. Small Form Variants
(code >= 0xfe30 && code <= 0xfe6b) ||
// Halfwidth and Fullwidth Forms
(code >= 0xff01 && code <= 0xff60) ||
(code >= 0xffe0 && code <= 0xffe6) ||
// Kana Supplement
(code >= 0x1b000 && code <= 0x1b001) ||
// Enclosed Ideographic Supplement
(code >= 0x1f200 && code <= 0x1f251) ||
// Miscellaneous Symbols and Pictographs 0x1f300 - 0x1f5ff
// Emoticons 0x1f600 - 0x1f64f
(code >= 0x1f300 && code <= 0x1f64f) ||
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
(code >= 0x20000 && code <= 0x3fffd)
);
};
const isZeroWidthCodePoint = (code) => {
return code <= 0x1F || // C0 control codes
(code >= 0x7F && code <= 0x9F) || // C1 control codes
(code >= 0x300 && code <= 0x36F) || // Combining Diacritical Marks
(code >= 0x200B && code <= 0x200F) || // Modifying Invisible Characters
// Combining Diacritical Marks for Symbols
(code >= 0x20D0 && code <= 0x20FF) ||
(code >= 0xFE00 && code <= 0xFE0F) || // Variation Selectors
(code >= 0xFE20 && code <= 0xFE2F) || // Combining Half Marks
(code >= 0xE0100 && code <= 0xE01EF); // Variation Selectors
};
function hasBuiltInToString(value) {
// TODO(wafuwafu13): Implement
// // Prevent triggering proxy traps.
// const getFullProxy = false;
// const proxyTarget = getProxyDetails(value, getFullProxy);
const proxyTarget = undefined;
if (proxyTarget !== undefined) {
value = proxyTarget;
}
// Count objects that have no `toString` function as built-in.
if (typeof value.toString !== "function") {
return true;
}
// The object has a own `toString` property. Thus it's not a built-in one.
if (Object.prototype.hasOwnProperty.call(value, "toString")) {
return false;
}
// Find the object that has the `toString` property as own property in the
// prototype chain.
let pointer = value;
do {
pointer = Object.getPrototypeOf(pointer);
} while (!Object.prototype.hasOwnProperty.call(pointer, "toString"));
// Check closer if the object is a built-in.
const descriptor = Object.getOwnPropertyDescriptor(pointer, "constructor");
return descriptor !== undefined &&
typeof descriptor.value === "function" &&
builtInObjects.has(descriptor.value.name);
}
const firstErrorLine = (error) => error.message.split("\n", 1)[0];
let CIRCULAR_ERROR_MESSAGE;
function tryStringify(arg) {
try {
return JSON.stringify(arg);
} catch (err) {
// Populate the circular error message lazily
if (!CIRCULAR_ERROR_MESSAGE) {
try {
const a = {};
a.a = a;
JSON.stringify(a);
} catch (circularError) {
CIRCULAR_ERROR_MESSAGE = firstErrorLine(circularError);
}
}
if (
err.name === "TypeError" &&
firstErrorLine(err) === CIRCULAR_ERROR_MESSAGE
) {
return "[Circular]";
}
throw err;
}
}
export function format(...args) {
return formatWithOptionsInternal(undefined, args);
}
export function formatWithOptions(inspectOptions, ...args) {
if (typeof inspectOptions !== "object" || inspectOptions === null) {
throw new codes.ERR_INVALID_ARG_TYPE(
"inspectOptions",
"object",
inspectOptions,
);
}
return formatWithOptionsInternal(inspectOptions, args);
}
function formatNumberNoColor(number, options) {
return formatNumber(
stylizeNoColor,
number,
options?.numericSeparator ?? inspectDefaultOptions.numericSeparator,
);
}
function formatBigIntNoColor(bigint, options) {
return formatBigInt(
stylizeNoColor,
bigint,
options?.numericSeparator ?? inspectDefaultOptions.numericSeparator,
);
}
function formatWithOptionsInternal(inspectOptions, args) {
const first = args[0];
let a = 0;
let str = "";
let join = "";
if (typeof first === "string") {
if (args.length === 1) {
return first;
}
let tempStr;
let lastPos = 0;
for (let i = 0; i < first.length - 1; i++) {
if (first.charCodeAt(i) === 37) { // '%'
const nextChar = first.charCodeAt(++i);
if (a + 1 !== args.length) {
switch (nextChar) {
// deno-lint-ignore no-case-declarations
case 115: // 's'
const tempArg = args[++a];
if (typeof tempArg === "number") {
tempStr = formatNumberNoColor(tempArg, inspectOptions);
} else if (typeof tempArg === "bigint") {
tempStr = formatBigIntNoColor(tempArg, inspectOptions);
} else if (
typeof tempArg !== "object" ||
tempArg === null ||
!hasBuiltInToString(tempArg)
) {
tempStr = String(tempArg);
} else {
tempStr = inspect(tempArg, {
...inspectOptions,
compact: 3,
colors: false,
depth: 0,
});
}
break;
case 106: // 'j'
tempStr = tryStringify(args[++a]);
break;
// deno-lint-ignore no-case-declarations
case 100: // 'd'
const tempNum = args[++a];
if (typeof tempNum === "bigint") {
tempStr = formatBigIntNoColor(tempNum, inspectOptions);
} else if (typeof tempNum === "symbol") {
tempStr = "NaN";
} else {
tempStr = formatNumberNoColor(Number(tempNum), inspectOptions);
}
break;
case 79: // 'O'
tempStr = inspect(args[++a], inspectOptions);
break;
case 111: // 'o'
tempStr = inspect(args[++a], {
...inspectOptions,
showHidden: true,
showProxy: true,
depth: 4,
});
break;
// deno-lint-ignore no-case-declarations
case 105: // 'i'
const tempInteger = args[++a];
if (typeof tempInteger === "bigint") {
tempStr = formatBigIntNoColor(tempInteger, inspectOptions);
} else if (typeof tempInteger === "symbol") {
tempStr = "NaN";
} else {
tempStr = formatNumberNoColor(
Number.parseInt(tempInteger),
inspectOptions,
);
}
break;
// deno-lint-ignore no-case-declarations
case 102: // 'f'
const tempFloat = args[++a];
if (typeof tempFloat === "symbol") {
tempStr = "NaN";
} else {
tempStr = formatNumberNoColor(
Number.parseFloat(tempFloat),
inspectOptions,
);
}
break;
case 99: // 'c'
a += 1;
tempStr = "";
break;
case 37: // '%'
str += first.slice(lastPos, i);
lastPos = i + 1;
continue;
default: // Any other character is not a correct placeholder
continue;
}
if (lastPos !== i - 1) {
str += first.slice(lastPos, i - 1);
}
str += tempStr;
lastPos = i + 1;
} else if (nextChar === 37) {
str += first.slice(lastPos, i);
lastPos = i + 1;
}
}
}
if (lastPos !== 0) {
a++;
join = " ";
if (lastPos < first.length) {
str += first.slice(lastPos);
}
}
}
while (a < args.length) {
const value = args[a];
str += join;
str += typeof value !== "string" ? inspect(value, inspectOptions) : value;
join = " ";
a++;
}
return str;
}
/**
* Remove all VT control characters. Use to estimate displayed string width.
*/
export function stripVTControlCharacters(str) {
validateString(str, "str");
return str.replace(ansi, "");
}
export function styleText(format, text) {
validateString(text, "text");
if (Array.isArray(format)) {
for (let i = 0; i < format.length; i++) {
const item = format[i];
const formatCodes = inspect.colors[item];
if (formatCodes == null) {
validateOneOf(item, "format", Object.keys(inspect.colors));
}
text = `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
}
return text;
}
const formatCodes = inspect.colors[format];
if (formatCodes == null) {
validateOneOf(format, "format", Object.keys(inspect.colors));
}
return `\u001b[${formatCodes[0]}m${text}\u001b[${formatCodes[1]}m`;
}
export default {
format,
getStringWidth,
inspect,
stripVTControlCharacters,
formatWithOptions,
styleText,
};
|