Skip to content

Commit dd319af

Browse files
committed
[Fix] fromBase64, setFromBase64: Safari 18.5 fails to throw on odd length strings
1 parent 4ae2fb5 commit dd319af

11 files changed

Lines changed: 48 additions & 19 deletions

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ If `Uint8Array` is not present, the `shim` functions and `auto` entrypoints will
2626
- [`Uint8Array.prototype.setFromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfrombase64)
2727
- [`Uint8Array.prototype.setFromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfromhex)
2828

29+
## When you need this package
30+
31+
- When you are using an engine that does not support any of the above methods
32+
- When using Safari 18.5 or earlier, which fails to throw on odd-length hex strings
33+
2934
## Getting started
3035

3136
```sh

Uint8Array.fromBase64/polyfill.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
var implementation = require('./implementation');
44

55
module.exports = function getPolyfill() {
6-
return typeof Uint8Array === 'function' && typeof Uint8Array.fromBase64 === 'function' ? Uint8Array.fromBase64 : implementation;
6+
if (typeof Uint8Array === 'function' && typeof Uint8Array.fromBase64 === 'function') {
7+
try {
8+
Uint8Array.fromBase64('a');
9+
} catch (e) {
10+
return Uint8Array.fromBase64;
11+
}
12+
}
13+
return implementation;
714
};

Uint8Array.prototype.setFromBase64/polyfill.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
var implementation = require('./implementation');
44

55
module.exports = function getPolyfill() {
6-
return typeof Uint8Array === 'function' && typeof Uint8Array.prototype.setFromBase64 === 'function' ? Uint8Array.prototype.setFromBase64 : implementation;
6+
if (typeof Uint8Array === 'function' && typeof Uint8Array.setFromBase64 === 'function') {
7+
try {
8+
Uint8Array.setFromBase64('a');
9+
} catch (e) {
10+
return Uint8Array.setFromBase64;
11+
}
12+
}
13+
return implementation;
714
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,8 @@
124124
"ignore": [
125125
".github/workflows"
126126
]
127+
},
128+
"testling": {
129+
"files": "test/shimmed.js"
127130
}
128131
}

test/Uint8Array.fromBase64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var shimName = 'Uint8Array.fromBase64';
1717

1818
module.exports = {
1919
tests: function (t, method) {
20-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
20+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2121
st['throws'](
2222
function () { return method(''); },
2323
SyntaxError,

test/Uint8Array.fromHex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var shimName = 'Uint8Array.fromHex';
1616

1717
module.exports = {
1818
tests: function (t, method) {
19-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
19+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2020
st['throws'](
2121
function () { return method(''); },
2222
SyntaxError,

test/Uint8Array.prototype.setFromBase64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var shimName = 'Uint8Array.prototype.setFromBase64';
2121

2222
module.exports = {
2323
tests: function (t, method) {
24-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
24+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2525
st['throws'](
2626
function () { return method(''); },
2727
SyntaxError,

test/Uint8Array.prototype.setFromHex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var shimName = 'Uint8Array.prototype.setFromHex';
2121

2222
module.exports = {
2323
tests: function (t, method) {
24-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
24+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2525
st['throws'](
2626
function () { return method(''); },
2727
SyntaxError,

test/Uint8Array.prototype.toBase64.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var shimName = 'Uint8Array.prototype.' + methodName;
2323

2424
module.exports = {
2525
tests: function (t, method) {
26-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
26+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2727
st['throws'](
2828
function () { return method(); },
2929
SyntaxError,

test/Uint8Array.prototype.toHex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var shimName = 'Uint8Array.prototype.' + methodName;
2424

2525
module.exports = {
2626
tests: function (t, method) {
27-
t.test('Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
27+
t.test('when Uint8Arrays not supported', { skip: typeof Uint8Array === 'function' }, function (st) {
2828
st['throws'](
2929
function () { return method(); },
3030
SyntaxError,

0 commit comments

Comments
 (0)