-
Notifications
You must be signed in to change notification settings - Fork 64
fix: ensure connection encodes utf8 correctly #4493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GordonSmith
wants to merge
1
commit into
hpcc-systems:main
Choose a base branch
from
GordonSmith:BTOA_POLLYFIL
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| const BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
|
|
||
| function toUTF8Bytes(value: string): Uint8Array { | ||
| if (typeof TextEncoder !== "undefined") { | ||
| return new TextEncoder().encode(value); | ||
| } | ||
|
|
||
| const encoded = encodeURIComponent(value); | ||
| const bytes: number[] = []; | ||
| for (let i = 0; i < encoded.length; ++i) { | ||
| if (encoded[i] === "%") { | ||
| bytes.push(parseInt(encoded.substring(i + 1, i + 3), 16)); | ||
| i += 2; | ||
| } else { | ||
| bytes.push(encoded.charCodeAt(i)); | ||
| } | ||
| } | ||
| return Uint8Array.from(bytes); | ||
| } | ||
|
|
||
| function bytesToBase64(bytes: Uint8Array): string { | ||
| let output = ""; | ||
| for (let i = 0; i < bytes.length; i += 3) { | ||
| const byte1 = bytes[i]; | ||
| const hasByte2 = i + 1 < bytes.length; | ||
| const hasByte3 = i + 2 < bytes.length; | ||
| const byte2 = hasByte2 ? bytes[i + 1] : 0; | ||
| const byte3 = hasByte3 ? bytes[i + 2] : 0; | ||
|
|
||
| output += BASE64_ALPHABET[byte1 >> 2]; | ||
| output += BASE64_ALPHABET[((byte1 & 0x03) << 4) | (byte2 >> 4)]; | ||
| output += hasByte2 ? BASE64_ALPHABET[((byte2 & 0x0f) << 2) | (byte3 >> 6)] : "="; | ||
| output += hasByte3 ? BASE64_ALPHABET[byte3 & 0x3f] : "="; | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| export function utf8ToBase64(value: string = ""): string { | ||
| const normalized = value == null ? "" : String(value); | ||
|
|
||
| const maybeBuffer = (globalThis as any)?.Buffer; | ||
| if (maybeBuffer?.from) { | ||
| return maybeBuffer.from(normalized, "utf8").toString("base64"); | ||
| } | ||
|
|
||
| return bytesToBase64(toUTF8Bytes(normalized)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { utf8ToBase64 } from "@hpcc-js/util"; | ||
|
|
||
| describe("utf8ToBase64", () => { | ||
| it("encodes multi-byte characters", () => { | ||
| const sample = "Привет, 世界 👋"; | ||
| const expected = Buffer.from(sample, "utf8").toString("base64"); | ||
| expect(utf8ToBase64(sample)).toEqual(expected); | ||
| }); | ||
|
|
||
| it("falls back when Buffer is unavailable", () => { | ||
| const sample = "mañana ☀️"; | ||
| const expected = Buffer.from(sample, "utf8").toString("base64"); | ||
| const original = (globalThis as any).Buffer; | ||
| (globalThis as any).Buffer = undefined; | ||
| try { | ||
| expect(utf8ToBase64(sample)).toEqual(expected); | ||
| } finally { | ||
| (globalThis as any).Buffer = original; | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { utf8ToBase64 } from "@hpcc-js/util"; | ||
|
|
||
| describe("utf8ToBase64", () => { | ||
| it("encodes ASCII input", () => { | ||
| expect(utf8ToBase64("hello world")) | ||
| .toEqual("aGVsbG8gd29ybGQ="); | ||
| }); | ||
|
|
||
| it("treats nullish values as empty string", () => { | ||
| expect(utf8ToBase64(undefined as unknown as string)).toEqual(""); | ||
| expect(utf8ToBase64(null as unknown as string)).toEqual(""); | ||
| }); | ||
|
|
||
| it("check frequest characters", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo? "frequest" |
||
| expect(utf8ToBase64("hello world")).toEqual(btoa("hello world")); | ||
| expect(utf8ToBase64("g@s*!")).toEqual(btoa("g@s*!")); | ||
| expect(utf8ToBase64("~@:!$%^&*()_-;'#***")).toEqual(btoa("~@:!$%^&*()_-;'#***")); | ||
| expect(utf8ToBase64("!$%^&*()_-;'#:@~")).toEqual(btoa("!$%^&*()_-;'#:@~")); | ||
| // expect(utf8ToBase64("¬!£$%^&*()_-;'#:@~")).toEqual(btoa("¬!£$%^&*()_-;'#:@~")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a commented out test case here |
||
| }); | ||
|
|
||
| describe("incremental ASCII coverage", () => { | ||
| const printableAscii = Array.from({ length: 95 }, (_, i) => String.fromCharCode(32 + i)).join(""); | ||
| const checkpoints = [1, 5, 10, 20, 32, 48, 64, 80, 95]; | ||
| const cases = checkpoints.map((len) => [len, printableAscii.slice(0, len)] as const); | ||
|
|
||
| it.each(cases)("encodes first %i printable ASCII chars", (_length, sample) => { | ||
| expect(utf8ToBase64(sample)).toEqual(btoa(sample)); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
several console.logs left behind in here?