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
10 changes: 8 additions & 2 deletions cloud-function/src/internal/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,19 @@ export function renderHtml(state = null) {
const consoleProxy = new Proxy(console, {
get(target, prop) {
if (typeof target[prop] === "function") {
const uuid =
new URLSearchParams(location.search).get("uuid") || undefined;
return (...args) => {
try {
window.parent.postMessage(
{ typ: "console", prop, args },
{ uuid, typ: "console", prop, args },
"*"
);
} catch {
try {
window.parent.postMessage(
{
uuid,
typ: "console",
prop,
args: args.map((x) => {
Expand All @@ -470,6 +473,7 @@ export function renderHtml(state = null) {
} catch {
window.parent.postMessage(
{
uuid,
typ: "console",
prop: "warn",
args: [
Expand Down Expand Up @@ -537,7 +541,9 @@ export function renderHtml(state = null) {
</script>
<script>
try {
window.parent.postMessage({ typ: "ready" }, "*");
const uuid =
new URLSearchParams(location.search).get("uuid") || undefined;
window.parent.postMessage({ uuid, typ: "ready" }, "*");
} catch (e) {
console.error("[Playground] Failed to post ready message", e);
}
Expand Down
34 changes: 34 additions & 0 deletions cloud-function/src/internal/play/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { renderHtml } from "./index.js";

describe("renderHtml", () => {
const output = renderHtml();

it("reads the uuid from the runner URL", () => {
assert.match(
output,
/new URLSearchParams\(location\.search\)\.get\("uuid"\) \|\| undefined/
);
});

const cases = [
{ name: "ready", pattern: /postMessage\(\{ uuid, typ: "ready" \}, "\*"\)/ },
{ name: "console", pattern: /\{ uuid, typ: "console", prop, args \}/ },
{
name: "console fallback",
pattern: /\{\s+uuid,\s+typ: "console",\s+prop,\s+args: args\.map/,
},
{
name: "console warn",
pattern: /\{\s+uuid,\s+typ: "console",\s+prop: "warn",/,
},
];

for (const { name, pattern } of cases) {
it(`tags the ${name} message with the uuid`, () => {
assert.match(output, pattern);
});
}
});
Loading