import { expect, it, describe } from "bun:test"; function gcTrace() { Bun.gc(true); } const getByteLength = (str) => { // returns the byte length of an utf8 string var s = str.length; for (var i = str.length - 1; i >= 0; i--) { var code = str.charCodeAt(i); if (code > 0x7f && code <= 0x7ff) s++; else if (code > 0x7ff && code <= 0xffff) s += 2; if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate } return s; }; describe("TextDecoder", () => { it("should decode ascii text", () => { const decoder = new TextDecoder("latin1"); gcTrace(true); expect(decoder.encoding).toBe("windows-1252"); gcTrace(true); expect(decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))).toBe("ABC"); gcTrace(true); const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; gcTrace(true); expect(decoder.decode(Uint8Array.from(result))).toBe( String.fromCharCode(...result) ); gcTrace(true); }); it("should decode unicode text", () => { const decoder = new TextDecoder(); gcTrace(true); var text = `❤️ Red Heart`; const bytes = [ 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, ]; const decoded = decoder.decode(Uint8Array.from(bytes)); expect(decoder.encoding).toBe("utf-8"); gcTrace(true); for (let i = 0; i < text.length; i++) { expect(decoded.charCodeAt(i)).toBe(text.charCodeAt(i)); } expect(decoded).toHaveLength(text.length); gcTrace(true); }); it("should decode unicode text with multiple consecutive emoji", () => { const decoder = new TextDecoder(); const encoder = new TextEncoder(); gcTrace(true); var text = `❤️❤️❤️❤️❤️❤️ Red Heart`; text += ` ✨ Sparkles 🔥 Fire 😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 🥰 😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨 🧐 🤓 😎 🥸 🤩 🥳 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 🥺 😢 😭 😤 😠 😡 🤬 🤯 😳 🥵 🥶 😱 😨 😰`; gcTrace(true); expect(decoder.decode(encoder.encode(text))).toBe(text); gcTrace(true); const bytes = new Uint8Array(getByteLength(text) * 8); gcTrace(true); const amount = encoder.encodeInto(text, bytes); gcTrace(true); expect(decoder.decode(bytes.subarray(0, amount.written))).toBe(text); gcTrace(true); }); }); describe("TextEncoder", () => { it("should encode latin1 text", () => { gcTrace(true); const text = "Hello World!"; const encoder = new TextEncoder(); gcTrace(true); const encoded = encoder.encode(text); gcTrace(true); expect(encoded instanceof Uint8Array).toBe(true); expect(encoded.length).toBe(text.length); gcTrace(true); const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; for (let i = 0; i < result.length; i++) { expect(encoded[i]).toBe(result[i]); } }); it("should encode long latin1 text", async () => { const text = "Hello World!".repeat(1000); const encoder = new TextEncoder(); gcTrace(true); const encoded = encoder.encode(text); gcTrace(true); expect(encoded instanceof Uint8Array).toBe(true); expect(encoded.length).toBe(text.length); gcTrace(true); const decoded = new TextDecoder().decode(encoded); expect(decoded).toBe(text); gcTrace(); await new Promise((resolve) => setTimeout(resolve, 1)); gcTrace(); expect(decoded).toBe(text); }); it("should encode latin1 rope text", () => { var text = "Hello"; text += " "; text += "World!"; gcTrace(true); const encoder = new TextEncoder(); const encoded = encoder.encode(text); gcTrace(true); const into = new Uint8Array(100); const out = encoder.encodeInto(text, into); gcTrace(true); expect(out.read).toBe(text.length); expect(out.written).toBe(encoded.length); expect(encoded instanceof Uint8Array).toBe(true); const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; for (let i = 0; i < result.length; i++) { expect(encoded[i]).toBe(result[i]); expect(encoded[i]).toBe(into[i]); } expect(encoded.length).toBe(getByteLength(text)); }); it("should encode utf-16 text", () => { var text = `❤️ Red Heart ✨ Sparkles 🔥 Fire `; var encoder = new TextEncoder(); var decoder = new TextDecoder(); gcTrace(true); expect(decoder.decode(encoder.encode(text))).toBe(text); gcTrace(true); }); // this test is from a web platform test in WebKit describe("should use a unicode replacement character for invalid surrogate pairs", () => { var bad = [ { encoding: "utf-16le", input: [0x00, 0xd8], expected: "\uFFFD", name: "lone surrogate lead", }, { encoding: "utf-16le", input: [0x00, 0xdc], expected: "\uFFFD", name: "lone surrogate trail", }, { encoding: "utf-16le", input: [0x00, 0xd8, 0x00, 0x00], expected: "\uFFFD\u0000", name: "unmatched surrogate lead", }, { encoding: "utf-16le", input: [0x00, 0xdc, 0x00, 0x00], expected: "\uFFFD\u0000", name: "unmatched surrogate trail", }, { encoding: "utf-16le", input: [0x00, 0xdc, 0x00, 0xd8], expected: "\uFFFD\uFFFD", name: "swapped surrogate pair", }, ]; bad.forEach(function (t) { it(t.encoding + " - " + t.name, () => { gcTrace(true); expect( new TextDecoder(t.encoding).decode(new Uint8Array(t.input)) ).toBe(t.expected); expect( new TextDecoder(t.encoding).decode( new Uint16Array(new Uint8Array(t.input).buffer) ) ).toBe(t.expected); gcTrace(true); }); // test(function () { // assert_throws_js(TypeError, function () { // new TextDecoder(t.encoding, { fatal: true }).decode( // new Uint8Array(t.input) // ); // }); // }, t.encoding + " - " + t.name + " (fatal flag set)"); }); }); it("should encode utf-16 rope text", () => { gcTrace(true); var textReal = `❤️ Red Heart ✨ Sparkles 🔥 Fire`; var a = textReal.split(""); var text = ""; for (let j of a) { text += j; } var encoder = new TextEncoder(); expect(new TextDecoder().decode(encoder.encode(text))).toBe(textReal); }); }); redns/commit/.github/workflows/trivy-scan.yaml?h=v1.10.1&id=04341e0ab8ae393c5d924111dba9a60323945d54&follow=1'>build(deps): bump aquasecurity/trivy-action from 0.8.0 to 0.9.0 (#5900)Gravatar dependabot[bot] 1-1/+1 2023-01-30plugin/forward: fix broken tap plugins when dnstap plugins specified (#5890)Gravatar Gerhard Tan 5-13/+60 2023-01-30build(deps): bump k8s.io/klog/v2 from 2.80.1 to 2.90.0 (#5887)Gravatar dependabot[bot] 2-3/+3 2023-01-30build(deps): bump github.com/Azure/azure-sdk-for-go (#5885)Gravatar dependabot[bot] 2-3/+3 2023-01-30build(deps): bump github/codeql-action from 2.1.39 to 2.2.1 (#5884)Gravatar dependabot[bot] 3-5/+5 2023-01-30build(deps): bump golangci/golangci-lint-action from 3.3.1 to 3.4.0 (#5883)Gravatar dependabot[bot] 1-1/+1 2023-01-30build(deps): bump github.com/aws/aws-sdk-go from 1.44.180 to 1.44.189 (#5886)Gravatar dependabot[bot] 2-3/+3 2023-01-30build(deps): bump github.com/antonmedv/expr from 1.10.3 to 1.10.5 (#5888)Gravatar dependabot[bot] 2-3/+3 2023-01-30build(deps): bump k8s.io/client-go from 0.26.0 to 0.26.1 (#5889)Gravatar dependabot[bot] 2-9/+9 2023-01-27update notes with recent merges (#5880)Gravatar Chris O'Haver 1-1/+6 2023-01-27Add weighted-round-robin policy to loadbalance plugin (#5662)Gravatar Gabor Dozsa 8-36/+975 2023-01-27plugin/cache: add a new keepttl option (#5879)Gravatar Arthur Outhenin-Chalandre 6-1/+87 2023-01-23build(deps): bump github.com/Azure/go-autorest/autorest/azure/auth (#5876)Gravatar dependabot[bot] 2-3/+3 2023-01-23Fix links in main README (#5878)Gravatar Chris O'Haver 1-2/+2 2023-01-23build(deps): bump gopkg.in/DataDog/dd-trace-go.v1 from 1.46.0 to 1.46.1 (#5873)Gravatar dependabot[bot] 2-3/+3 2023-01-23build(deps): bump github.com/antonmedv/expr from 1.9.0 to 1.10.3 (#5872)Gravatar dependabot[bot] 2-18/+3 2023-01-23build(deps): bump google.golang.org/api from 0.107.0 to 0.108.0 (#5875)Gravatar dependabot[bot] 2-3/+3 2023-01-23build(deps): bump github/codeql-action from 2.1.38 to 2.1.39 (#5871)Gravatar dependabot[bot] 3-5/+5 2023-01-23build(deps): bump go.etcd.io/etcd/client/v3 from 3.5.6 to 3.5.7 (#5874)Gravatar dependabot[bot] 2-103/+9 2023-01-23Move flags to coremain (#5865)Gravatar David Hadas 2-6/+3 2023-01-16build(deps): bump github.com/aws/aws-sdk-go from 1.44.175 to 1.44.180 (#5856)Gravatar dependabot[bot] 2-3/+3 2023-01-16build(deps): bump github/codeql-action from 2.1.37 to 2.1.38 (#5855)Gravatar dependabot[bot] 3-5/+5 2023-01-16build(deps): bump gopkg.in/DataDog/dd-trace-go.v1 from 1.45.1 to 1.46.0 (#5857)Gravatar dependabot[bot] 2-3/+3 2023-01-16build(deps): bump google.golang.org/api from 0.106.0 to 0.107.0 (#5859)Gravatar dependabot[bot] 2-3/+3 2023-01-16build(deps): bump github.com/Azure/azure-sdk-for-go (#5858)Gravatar dependabot[bot] 2-3/+3 2023-01-16build(deps): bump google.golang.org/grpc from 1.51.0 to 1.52.0 (#5860)Gravatar dependabot[bot] 2-3/+3 2023-01-11plugin/etcd: update documentation (#5847)Gravatar Vico Chu 1-1/+1 2023-01-09Bump version to 1.10.1 (#5845)Gravatar Yong Tang 1-1/+1