Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/interpreter/src/js/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[4905932134995611186, 10317260808282072637, 14897950097980380376, 5444526391971481782, 16137391248650656019, 5052021921702764563, 10728655726121042447, 11339769846046015954]
[4905932134995611186, 10317260808282072637, 14897950097980380376, 5444526391971481782, 16137391248650656019, 13191714558229096046, 12344782490540418386, 10728655726121042447, 11339769846046015954]
2 changes: 1 addition & 1 deletion packages/interpreter/src/js/patch_console.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions packages/interpreter/src/ts/patch_console.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Run with: node --experimental-strip-types --test patch_console.spec.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { monkeyPatchConsole } from "./patch_console.ts";

function patched() {
const sent: string[] = [];
const fakeConsole: any = {};
for (const level of ["log", "info", "warn", "error", "debug"]) {
fakeConsole[level] = () => {};
}
(globalThis as any).WebSocket = { OPEN: 1 };
(globalThis as any).window = { console: fakeConsole };
monkeyPatchConsole({ readyState: 1, send: (s: string) => sent.push(s) } as any);
return { fakeConsole, sent };
}

test("every console level sends only string messages", () => {
const { fakeConsole, sent } = patched();
for (const level of ["log", "info", "warn", "error", "debug"]) {
fakeConsole[level]("Wee", { some: "object" }, 42, [1, 2]);
}
assert.equal(sent.length, 5);
for (const raw of sent) {
const { Log } = JSON.parse(raw);
assert.deepEqual(Log.messages, ["Wee", '{"some":"object"}', "42", "[1,2]"]);
}
});

test("values JSON.stringify cannot handle fall back to strings", () => {
const { fakeConsole, sent } = patched();
const circular: any = {};
circular.self = circular;
fakeConsole.debug(circular, 10n, undefined, () => {});
const { Log } = JSON.parse(sent[0]);
assert.equal(Log.messages.length, 4);
for (const m of Log.messages) assert.equal(typeof m, "string");
});
22 changes: 17 additions & 5 deletions packages/interpreter/src/ts/patch_console.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function stringify(args: any[]): string[] {
return args.map((a) => {
if (typeof a === "string") return a;
try {
const s = JSON.stringify(a);
return s === undefined ? String(a) : s;
} catch {
return String(a);
}
});
}


export function monkeyPatchConsole(ws: WebSocket) {
const console = window.console;
Expand All @@ -10,7 +22,7 @@ export function monkeyPatchConsole(ws: WebSocket) {
console.log = function (...args: any[]) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
"Log": { level: "log", messages: args }
"Log": { level: "log", messages: stringify(args) }
}));
}
log.apply(console, args);
Expand All @@ -19,7 +31,7 @@ export function monkeyPatchConsole(ws: WebSocket) {
console.info = function (...args: any[]) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
"Log": { level: "info", messages: args }
"Log": { level: "info", messages: stringify(args) }
}));
}
info.apply(console, args);
Expand All @@ -28,7 +40,7 @@ export function monkeyPatchConsole(ws: WebSocket) {
console.warn = function (...args: any[]) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
"Log": { level: "warn", messages: args }
"Log": { level: "warn", messages: stringify(args) }
}));
}
warn.apply(console, args);
Expand All @@ -37,7 +49,7 @@ export function monkeyPatchConsole(ws: WebSocket) {
console.error = function (...args: any[]) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
"Log": { level: "error", messages: args }
"Log": { level: "error", messages: stringify(args) }
}));
}
error.apply(console, args);
Expand All @@ -46,7 +58,7 @@ export function monkeyPatchConsole(ws: WebSocket) {
console.debug = function (...args: any[]) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
"Log": { level: "debug", messages: args }
"Log": { level: "debug", messages: stringify(args) }
}));
}
debug.apply(console, args);
Expand Down
Loading