File tree 4 files changed +44
-4
lines changed
4 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -50,7 +50,17 @@ export function decodeContent() {
50
50
transform ( value , controller ) {
51
51
// Base64 decode the `content` field and send it as a chunk.
52
52
if ( value . content ?. length ) {
53
- controller . enqueue ( atob ( value . content ) ) ;
53
+ // The `atob` function does not work properly if the decoded content contains any byte
54
+ // values over 0x7f, because "binary" in JS means that each byte gets represented as a
55
+ // UTF-16 code unit, which happens to be <= 0xff. I wish I was making this up:
56
+ // https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
57
+ const binary = atob ( value . content ) ;
58
+ const bytes = new Uint8Array ( binary . length ) ;
59
+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
60
+ bytes [ i ] = binary . charCodeAt ( i ) ;
61
+ }
62
+ const text = new TextDecoder ( ) . decode ( bytes ) ;
63
+ controller . enqueue ( text ) ;
54
64
}
55
65
} ,
56
66
} ) ;
Original file line number Diff line number Diff line change @@ -56,7 +56,17 @@ function decodeContent() {
56
56
transform ( value , controller ) {
57
57
// Base64 decode the `content` field and send it as a chunk.
58
58
if ( value . content ?. length ) {
59
- controller . enqueue ( atob ( value . content ) ) ;
59
+ // The `atob` function does not work properly if the decoded content contains any byte
60
+ // values over 0x7f, because "binary" in JS means that each byte gets represented as a
61
+ // UTF-16 code unit, which happens to be <= 0xff. I wish I was making this up:
62
+ // https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
63
+ const binary = atob ( value . content ) ;
64
+ const bytes = new Uint8Array ( binary . length ) ;
65
+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
66
+ bytes [ i ] = binary . charCodeAt ( i ) ;
67
+ }
68
+ const text = new TextDecoder ( ) . decode ( bytes ) ;
69
+ controller . enqueue ( text ) ;
60
70
}
61
71
} ,
62
72
} ) ;
Original file line number Diff line number Diff line change @@ -54,7 +54,17 @@ export function decodeContent() {
54
54
transform ( value , controller ) {
55
55
// Base64 decode the `content` field and send it as a chunk.
56
56
if ( value . content ?. length ) {
57
- controller . enqueue ( atob ( value . content ) ) ;
57
+ // The `atob` function does not work properly if the decoded content contains any byte
58
+ // values over 0x7f, because "binary" in JS means that each byte gets represented as a
59
+ // UTF-16 code unit, which happens to be <= 0xff. I wish I was making this up:
60
+ // https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
61
+ const binary = atob ( value . content ) ;
62
+ const bytes = new Uint8Array ( binary . length ) ;
63
+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
64
+ bytes [ i ] = binary . charCodeAt ( i ) ;
65
+ }
66
+ const text = new TextDecoder ( ) . decode ( bytes ) ;
67
+ controller . enqueue ( text ) ;
58
68
}
59
69
} ,
60
70
} ) ;
Original file line number Diff line number Diff line change @@ -54,7 +54,17 @@ export function decodeContent() {
54
54
transform ( value , controller ) {
55
55
// Base64 decode the `content` field and send it as a chunk.
56
56
if ( value . content ?. length ) {
57
- controller . enqueue ( atob ( value . content ) ) ;
57
+ // The `atob` function does not work properly if the decoded content contains any byte
58
+ // values over 0x7f, because "binary" in JS means that each byte gets represented as a
59
+ // UTF-16 code unit, which happens to be <= 0xff. I wish I was making this up:
60
+ // https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
61
+ const binary = atob ( value . content ) ;
62
+ const bytes = new Uint8Array ( binary . length ) ;
63
+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
64
+ bytes [ i ] = binary . charCodeAt ( i ) ;
65
+ }
66
+ const text = new TextDecoder ( ) . decode ( bytes ) ;
67
+ controller . enqueue ( text ) ;
58
68
}
59
69
} ,
60
70
} ) ;
You can’t perform that action at this time.
0 commit comments