Skip to content

Commit

Permalink
Fix: 'fromGlobalId' trowing on invalide UTF code points
Browse files Browse the repository at this point in the history
Fixes graphql#358
Note: it's temprorary workaround until we design a better API
  • Loading branch information
IvanGoncharov committed Dec 17, 2021
1 parent e199d64 commit 23a946d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,42 @@ function utf8ArrayToString(input: Array<number>) {
for (let i = 0; i < input.length; ) {
const a = input[i++];
if ((a & 0x80) === 0) {
result += String.fromCodePoint(a);
result += fromCodePoint(a);
continue;
}

const b = input[i++];
if ((a & 0xe0) === 0xc0) {
result += String.fromCodePoint(((a & 0x1f) << 6) | (b & 0x3f));
result += fromCodePoint(((a & 0x1f) << 6) | (b & 0x3f));
continue;
}

const c = input[i++];
if ((a & 0xf0) === 0xe0) {
result += String.fromCodePoint(
result += fromCodePoint(
((a & 0x0f) << 12) | ((b & 0x3f) << 6) | (c & 0x3f),
);
continue;
}

const d = input[i++];
result += String.fromCodePoint(
result += fromCodePoint(
((a & 0x07) << 18) | ((b & 0x3f) << 12) | ((c & 0x3f) << 6) | (d & 0x3f),
);
}

return result;
}

function fromCodePoint(code: number): string {
if (code > 0x10FFFF) {
/*
* Previously we used Node's API for parsing Base64 and following code
* Buffer.from(i, 'base64').toString('utf8')
* That silently ignored incorrect input and returned empty string instead
* Let's keep this behaviour for a time being and hopefully fix it in the future.
*/
return '';
}
return String.fromCodePoint(code);
}

0 comments on commit 23a946d

Please sign in to comment.