/
console.js
330 lines (327 loc) · 12.7 KB
/
console.js
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
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// <define:process>
var init_define_process = __esm({
"<define:process>"() {
}
});
// ../format/dist/index.js
var require_dist = __commonJS({
"../format/dist/index.js"(exports) {
"use strict";
init_define_process();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFormat = void 0;
function createFormat2(opts = {}) {
if (opts.customInspectSymbol === void 0) {
opts.customInspectSymbol = Symbol.for("edge-runtime.inspect.custom");
}
if (opts.formatError === void 0) {
opts.formatError = (error2) => `[${Error.prototype.toString.call(error2)}]`;
}
const { formatError, customInspectSymbol } = opts;
function format2(...args) {
const [firstArg] = args;
if (!kind(firstArg, "string")) {
if (hasCustomSymbol(firstArg, customInspectSymbol)) {
return format2(firstArg[customInspectSymbol]());
} else {
return args.map((item) => inspect(item, { customInspectSymbol })).join(" ");
}
}
let index = 1;
let str = String(firstArg).replace(/%[sjdOoif%]/g, (token) => {
if (token === "%%")
return "%";
if (index >= args.length)
return token;
switch (token) {
case "%s": {
const arg = args[index++];
return hasCustomSymbol(arg, customInspectSymbol) ? format2(arg[customInspectSymbol]()) : String(arg);
}
case "%j":
return safeStringify(args[index++]);
case "%d":
return String(Number(args[index++]));
case "%O":
return inspect(args[index++], { customInspectSymbol });
case "%o":
return inspect(args[index++], {
customInspectSymbol,
showHidden: true,
depth: 4
});
case "%i":
return String(parseInt(args[index++], 10));
case "%f":
return String(parseFloat(args[index++]));
default:
return token;
}
});
for (let arg = args[index]; index < args.length; arg = args[++index]) {
if (arg === null || !kind(arg, "object")) {
str += " " + arg;
} else {
str += " " + inspect(arg);
}
}
return str;
}
__name(format2, "format");
function formatValue(ctx, value, recurseTimes) {
if (hasCustomSymbol(value, customInspectSymbol)) {
return format2(value[customInspectSymbol]());
}
const formattedPrimitive = formatPrimitive(value);
if (formattedPrimitive !== void 0) {
return formattedPrimitive;
}
const symbols = Object.getOwnPropertySymbols(value);
if (symbols.length > 0) {
symbols.forEach((symbol) => {
const obj = value;
const symbolKey = `[${symbol.toString()}]`;
obj[symbolKey] = obj[symbol];
delete obj[symbol];
});
}
const keys = ctx.showHidden ? Object.getOwnPropertyNames(value) : Object.keys(value);
const visibleKeys = /* @__PURE__ */ new Set();
keys.forEach((key) => visibleKeys.add(key));
if (keys.length === 0) {
if (kind(value, "function")) {
return `[Function${value.name ? ": " + value.name : ""}]`;
} else if (isRegExp(value)) {
return RegExp.prototype.toString.call(value);
} else if (isDate(value)) {
return Date.prototype.toString.call(value);
} else if (isError(value)) {
return formatError(value);
} else if (hasCustomSymbol(value, ctx.customInspectSymbol)) {
return format2(value[ctx.customInspectSymbol]());
}
}
const isValueFunction = kind(value, "function");
const isValueArray = Array.isArray(value);
let base = "";
if (isValueFunction) {
base = `[Function${value.name ? ": " + value.name : ""}]`;
} else if (isRegExp(value)) {
base = " " + RegExp.prototype.toString.call(value);
} else if (isDate(value)) {
base = " " + Date.prototype.toUTCString.call(value);
} else if (isError(value)) {
base = " " + formatError(value);
} else if (hasCustomSymbol(value, ctx.customInspectSymbol)) {
base = " " + value[ctx.customInspectSymbol]();
}
const braces = isValueArray ? ["[", "]"] : isValueFunction ? ["", ""] : ["{", "}"];
if (keys.length === 0 && (!isValueArray || value.length === 0)) {
return braces[0] + base + braces[1];
}
if (recurseTimes && recurseTimes < 0) {
return isRegExp(value) ? RegExp.prototype.toString.call(value) : "[Object]";
}
ctx.seen.push(value);
let output = isValueArray ? formatArray(ctx, value, recurseTimes, visibleKeys, keys) : keys.map((key) => formatProperty(ctx, value, recurseTimes, visibleKeys, key, false));
ctx.seen.pop();
return reduceToSingleString(output, base, braces, isValueFunction);
}
__name(formatValue, "formatValue");
function inspect(value, opts2) {
opts2 = Object.assign({ seen: [], depth: 2 }, opts2);
return formatValue(opts2, value, opts2.depth);
}
__name(inspect, "inspect");
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, isArray) {
let name;
let str;
const desc = Object.getOwnPropertyDescriptor(value, key) || {
value: value[key]
};
if (desc.get) {
str = desc.set ? "[Getter/Setter]" : "[Getter]";
} else if (desc.set) {
str = "[Setter]";
}
if (!visibleKeys.has(key)) {
name = "[" + key + "]";
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
str = formatValue(ctx, desc.value, recurseTimes === null || recurseTimes === void 0 ? null : recurseTimes - 1);
if (str.indexOf("\n") > -1) {
if (isArray) {
str = str.split("\n").map((line) => ` ${line}`).join("\n").slice(2);
} else {
str = "\n" + str.split("\n").map((line) => ` ${line}`).join("\n");
}
}
} else {
str = "[Circular]";
}
}
if (name === void 0) {
if (isArray && key.match(/^\d+$/)) {
return str;
}
}
return `${key}: ${str}`;
}
__name(formatProperty, "formatProperty");
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
const output = [];
for (let index = 0; index < value.length; ++index) {
if (Object.prototype.hasOwnProperty.call(value, String(index))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, String(index), true));
} else {
output.push("");
}
}
keys.forEach((key) => {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, key, true));
}
});
return output;
}
__name(formatArray, "formatArray");
return format2;
}
__name(createFormat2, "createFormat");
exports.createFormat = createFormat2;
function formatPrimitive(value) {
if (value === null)
return "null";
if (value === void 0)
return "undefined";
if (kind(value, "string")) {
return `'${JSON.stringify(value).replace(/^"|"$/g, "").replace(/'/g, "\\'").replace(/\\"/g, '"')}'`;
}
if (kind(value, "boolean"))
return "" + value;
if (kind(value, "number"))
return "" + value;
if (kind(value, "bigint"))
return "" + value;
if (kind(value, "symbol"))
return value.toString();
}
__name(formatPrimitive, "formatPrimitive");
function hasCustomSymbol(value, customInspectSymbol) {
return value !== null && kind(value, "object") && customInspectSymbol in value && kind(value[customInspectSymbol], "function");
}
__name(hasCustomSymbol, "hasCustomSymbol");
function kind(value, type) {
return typeof value === type;
}
__name(kind, "kind");
function isRegExp(value) {
return kind(value, "object") && Object.prototype.toString.call(value) === "[object RegExp]";
}
__name(isRegExp, "isRegExp");
function isDate(value) {
return kind(value, "object") && Object.prototype.toString.call(value) === "[object Date]";
}
__name(isDate, "isDate");
function isError(value) {
return kind(value, "object") && (Object.prototype.toString.call(value) === "[object Error]" || value instanceof Error);
}
__name(isError, "isError");
function reduceToSingleString(output, base, braces, isValueFunction) {
const length = output.reduce((prev, cur) => {
return prev + cur.replace(/\u001b\[\d\d?m/g, "").length + 1;
}, 0);
if (length > 60) {
const prefix2 = isValueFunction ? " {" : "";
const suffix2 = isValueFunction ? "\n}" : " ";
return braces[0] + (base === "" ? "" : base + prefix2 + "\n ") + ` ${output.join(",\n ")}` + suffix2 + braces[1];
}
const prefix = isValueFunction ? " { " : " ";
const suffix = isValueFunction ? " } " : " ";
return (braces[0] + base + prefix + output.join(", ") + suffix + braces[1]).trim();
}
__name(reduceToSingleString, "reduceToSingleString");
function safeStringify(object) {
if (Array.isArray(object)) {
object = object.map((element) => JSON.parse(JSON.stringify(element, makeCircularReplacer())));
}
return JSON.stringify(object, makeCircularReplacer());
}
__name(safeStringify, "safeStringify");
function makeCircularReplacer() {
const seen = /* @__PURE__ */ new WeakSet();
return (key, value) => {
if (value !== null && kind(value, "object")) {
if (seen.has(value))
return "[Circular]";
seen.add(value);
}
return value;
};
}
__name(makeCircularReplacer, "makeCircularReplacer");
}
});
// src/primitives/console.js
var console_exports = {};
__export(console_exports, {
console: () => konsole
});
module.exports = __toCommonJS(console_exports);
init_define_process();
var import_format = __toESM(require_dist());
var format = (0, import_format.createFormat)();
var bareError = console.error.bind(console);
var bareLog = console.log.bind(console);
var assert = console.assert.bind(console);
var time = console.time.bind(console);
var timeEnd = console.timeEnd.bind(console);
var timeLog = console.timeLog.bind(console);
var trace = console.trace.bind(console);
var error = /* @__PURE__ */ __name((...args) => bareError(format(...args)), "error");
var log = /* @__PURE__ */ __name((...args) => bareLog(format(...args)), "log");
var konsole = {
assert: (assertion, ...args) => assert(assertion, format(...args)),
count: console.count.bind(console),
dir: console.dir.bind(console),
error,
info: log,
log,
time: (...args) => time(format(...args)),
timeEnd: (...args) => timeEnd(format(...args)),
timeLog,
trace,
warn: error
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
console
});