Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
123456789101112131415161718192021222324const { StringDecoder } = require("node:string_decoder");const show = (label, parts) => console.log(label, JSON.stringify(parts));
const euro = Buffer.from("€✓😀");const u8 = new StringDecoder("utf8");show("utf8", [...euro].map((b) => u8.write(Buffer.from([b]))).concat(u8.end()));const cut = new StringDecoder("utf-8");show("utf8 end mid-char", [cut.write(euro.subarray(0, 2)), cut.end()]);show("utf8 invalid", [new StringDecoder().write(Buffer.from([0xff, 0x41]))]);
const wide = Buffer.from("a😀b", "utf16le");const u16 = new StringDecoder("ucs2");show("utf16le", [u16.write(wide.subarray(0, 1)), u16.write(wide.subarray(1, 4)), u16.write(wide.subarray(4)), u16.end()]);const u16cut = new StringDecoder("utf16le");show("utf16le end", [u16cut.write(wide.subarray(0, 4)), u16cut.end(Buffer.from([0x41]))]);
const b64 = new StringDecoder("base64");show("base64", [b64.write(Buffer.from("he")), b64.write(Buffer.from("llo")), b64.write(Buffer.from("!")), b64.end()]);const b64url = new StringDecoder("base64url");show("base64url", [b64url.write(Buffer.from([0xfb, 0xff])), b64url.end()]);show("hex latin1", [new StringDecoder("hex").write(Buffer.from("hi")), new StringDecoder("latin1").write(Buffer.from([0xe9]))]);console.log(new StringDecoder("UTF8").encoding, new StringDecoder("binary").encoding, new StringDecoder().write("str"));try { new StringDecoder("nope"); } catch (e) { console.log(e.code, e.message); }