MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1const { inspect } = require('node:util');
2
3function assert(cond, msg) {
4 if (!cond) throw new Error(msg);
5}
6
7assert(typeof Symbol.inspect === 'symbol', 'expected Symbol.inspect to exist');
8assert(Symbol.inspect !== Symbol.for('ant.inspect'), 'expected Symbol.inspect to stay separate from the global registry');
9assert(Symbol.keyFor(Symbol.inspect) === undefined, 'expected Symbol.inspect to not be registry-backed');
10
11class Connection {
12 constructor(host, port, state) {
13 this.host = host;
14 this.port = port;
15 this.state = state;
16 }
17
18 [Symbol.inspect]() {
19 return `Connection { ${this.host}:${this.port} (${this.state}) }`;
20 }
21}
22
23class FallbackConnection {
24 constructor(host) {
25 this.host = host;
26 }
27
28 [Symbol.inspect]() {
29 throw new Error('boom');
30 }
31}
32
33const blob = new Blob(['hi'], { type: 'text/plain' });
34const file = new File(['hi'], 'note.txt', { type: 'text/plain', lastModified: 42 });
35const headers = new Headers({ 'content-type': 'text/plain' });
36const request = new Request('https://google.com');
37const response = new Response('ok', { headers });
38const timeout = setTimeout(() => {}, 1);
39const interval = setInterval(() => {}, 5);
40const headersInspect = inspect(headers);
41const timeoutInspect = inspect(timeout);
42const intervalInspect = inspect(interval);
43const requestInspect = inspect(request);
44const responseInspect = inspect(response);
45
46assert(typeof Headers.prototype[Symbol.inspect] === 'function', 'expected Headers.prototype[Symbol.inspect] to exist');
47assert(typeof Request.prototype[Symbol.inspect] === 'function', 'expected Request.prototype[Symbol.inspect] to exist');
48assert(typeof Response.prototype[Symbol.inspect] === 'function', 'expected Response.prototype[Symbol.inspect] to exist');
49
50assert(inspect(new Connection('localhost', 3000, 'open')) === 'Connection { localhost:3000 (open) }', 'expected custom inspect result');
51assert(inspect(blob) === "Blob { size: 2, type: 'text/plain' }", 'expected Blob custom inspect output');
52assert(inspect(file) === "File { size: 2, type: 'text/plain', name: 'note.txt', lastModified: 42 }", 'expected File custom inspect output');
53assert(
54 Headers.prototype[Symbol.inspect].call(headers) === headersInspect,
55 `expected Headers Symbol.inspect output, got: ${Headers.prototype[Symbol.inspect].call(headers)}`
56);
57assert(
58 requestInspect === "Request {\n method: 'GET',\n url: 'https://google.com/',\n headers: Headers {},\n destination: '',\n referrer: 'about:client',\n referrerPolicy: '',\n mode: 'cors',\n credentials: 'same-origin',\n cache: 'default',\n redirect: 'follow',\n integrity: '',\n keepalive: false,\n isReloadNavigation: false,\n isHistoryNavigation: false,\n signal: AbortSignal { aborted: false }\n}",
59 `expected Request inspect output, got: ${requestInspect}`
60);
61assert(
62 Request.prototype[Symbol.inspect].call(request) === requestInspect,
63 `expected Request Symbol.inspect output, got: ${Request.prototype[Symbol.inspect].call(request)}`
64);
65assert(
66 Response.prototype[Symbol.inspect].call(response) === responseInspect,
67 `expected Response Symbol.inspect output, got: ${Response.prototype[Symbol.inspect].call(response)}`
68);
69assert(
70 timeoutInspect === 'Timeout (1) {\n delay: 1,\n repeat: null,\n [Symbol(Symbol.toPrimitive)]: [native code]\n}',
71 `expected legacy Timeout inspect output, got: ${timeoutInspect}`
72);
73assert(
74 intervalInspect === 'Interval (2) {\n delay: 5,\n repeat: 5,\n [Symbol(Symbol.toPrimitive)]: [native code]\n}',
75 `expected legacy Interval inspect output, got: ${intervalInspect}`
76);
77
78clearTimeout(timeout);
79clearInterval(interval);
80
81const fallback = inspect(new FallbackConnection('db.internal'));
82assert(
83 fallback === "FallbackConnection { host: 'db.internal' }",
84 `expected fallback formatting after inspect throw, got: ${fallback}`
85);
86
87console.log('PASS');