Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051// Buffer as a guest sees it: the global, node:buffer, and its encodings.import { test, expect } from "@playwright/test";import { guestLines } from "./guest";
test("a CJS guest encodes and decodes with the global Buffer", async ({ page }) => { const lines = await guestLines(page, { "/app/main.cjs": [ 'console.log(Buffer.from("hi").toString("base64"));', 'console.log(Buffer.from("aGk=", "base64").toString());', 'console.log(Buffer.from("héllo").toString("hex"), Buffer.from("68c3a96c6c6f", "hex").toString("utf8"));', 'console.log(Buffer.from([0xfb, 0xff]).toString("base64url"), Buffer.from("-_8", "base64url").toString("hex"));', 'console.log(Buffer.byteLength("héllo"), Buffer.byteLength("aGk=", "base64"));', 'const joined = Buffer.concat([Buffer.from("ab"), new Uint8Array([99])]);', "console.log(joined.toString(), joined.length, Buffer.isBuffer(joined), Buffer.isBuffer(new Uint8Array(1)), joined instanceof Uint8Array);", 'const view = joined.slice(1); view[0] = 0x42; console.log(joined.toString(), view instanceof Buffer);', 'console.log(Buffer.from("\\u00ff", "latin1")[0], Buffer.from([0xe9]).toString("latin1") === "\\u00e9");', 'console.log(JSON.stringify(Buffer.from("A")), Buffer.alloc(3, "ab").toString(), Buffer.from("a").equals(Buffer.from("a")));', 'console.log(require("node:buffer").Buffer === Buffer, require("buffer").Buffer === globalThis.Buffer, global === globalThis);', ].join("\n"), }, "/app/main.cjs"); expect(lines).toEqual([ "aGk=", "hi", "68c3a96c6c6f héllo", "-_8 fbff", "6 2", "abc 3 true false true", "aBc true", "255 true", '{"type":"Buffer","data":[65]} aba true', "true true true", ]);});
test("ESM imports Buffer from node:buffer", async ({ page }) => { const lines = await guestLines(page, { "/app/main.mjs": 'import { Buffer } from "node:buffer";\nconsole.log(Buffer.from("hi").toString("base64"), Buffer === globalThis.Buffer);\n', }, "/app/main.mjs"); expect(lines).toEqual(["aGk= true"]);});
// The builtin-buffer-encodings.cjs golden covers both decode paths only while the// browser under test has the native codecs: its strict inputs take them, its lenient// inputs fall back.test("the runtime worker has Uint8Array's native base64 and hex codecs", async ({ page }) => { const lines = await guestLines(page, { "/app/main.cjs": "console.log([Uint8Array.fromBase64, Uint8Array.fromHex, Uint8Array.prototype.toBase64, Uint8Array.prototype.toHex].map((f) => typeof f).join(\" \"));\n", }, "/app/main.cjs"); expect(lines).toEqual(["function function function function"]);});