Skip to content

Commit

Permalink
Remove "partial" things
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 30, 2023
1 parent 183231c commit 9687555
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 562 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ If `Uint8Array` is not present, the `shim` functions and `auto` entrypoints will
## Supported things

- [`Uint8Array.prototype.toBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64)
- [`Uint8Array.prototype.toPartialBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.topartialbase64)
- [`Uint8Array.prototype.toHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tohex)
- [`Uint8Array.fromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64)
- [`Uint8Array.fromPartialBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frompartialbase64)
- [`Uint8Array.fromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.fromhex)

## Getting started
Expand Down
108 changes: 95 additions & 13 deletions Uint8Array.fromBase64/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,32 @@ var $SyntaxError = GetIntrinsic('%SyntaxError%');
var $TypeError = GetIntrinsic('%TypeError%');
var $Uint8Array = GetIntrinsic('%Uint8Array%', true);

var callBound = require('call-bind/callBound');

var $replace = callBound('String.prototype.replace');
var $strSlice = callBound('String.prototype.slice');

var Get = require('es-abstract/2023/Get');
var GetOptionsObject = require('../aos/GetOptionsObject');
var GetStringForBinaryEncoding = require('../aos/GetStringForBinaryEncoding');
var ToString = require('es-abstract/2023/ToString');
var ToBoolean = require('es-abstract/2023/ToBoolean');
var StringToCodePoints = require('es-abstract/2023/StringToCodePoints');

var mod = require('es-abstract/helpers/mod');
var fromBase64Helper = require('../aos/helpers/fromBase64');

module.exports = function fromBase64(value) {
var safeRegexTest = require('safe-regex-test');

var containsPlusOrSolidus = safeRegexTest(/[+/]/);
var lastIsEquals = safeRegexTest(/[=]$/);

module.exports = function fromBase64(string) {
if (!$Uint8Array) {
throw new $SyntaxError('This environment does not support Uint8Array'); // step 1
}

var string = GetStringForBinaryEncoding(value); // step 1
if (typeof string !== 'string') {
throw new $TypeError('`value` is not a string: ' + string); // step 1
}

var options = GetOptionsObject(arguments.length > 1 ? arguments[1] : void undefined); // step 2

Expand All @@ -28,25 +41,94 @@ module.exports = function fromBase64(value) {
alphabet = 'base64'; // step 4
}

alphabet = ToString(alphabet); // step 5
if (typeof alphabet !== 'string') {
throw new $TypeError('`alphabet` is not a string: ' + string); // step 5
}

if (alphabet !== 'base64' && alphabet !== 'base64url') {
throw new $TypeError('Invalid alphabet'); // step 6
}

if (alphabet === 'base64') { // step 11
// a. If characters cannot result from applying the base64 encoding specified in section 4 of RFC 4648 to some sequence of bytes, throw a SyntaxError exception.
var strict = ToBoolean(Get(options, 'strict')); // step 7

// b. Let bytes be the unique sequence of bytes such that applying the base64 encoding specified in section 4 of RFC 4648 to that sequence would produce characters.
} else { // step 12
// a. Assert: alphabet is "base64url".
var input = StringToCodePoints(string); // step 8

if (alphabet === 'base64url') { // step 9
if (containsPlusOrSolidus(input)) {
throw new $SyntaxError('Invalid character: there is a plus sign or solidus in the input'); // step 9.a
}
input = $replace(input, /\u002D/g, '\u002B'); // step 9.b
input = $replace(input, /\u005F/g, '\u002F'); // step 9.c
}

// b. If characters cannot result from applying the base64url encoding specified in section 5 of RFC 4648 to some sequence of bytes, throw a SyntaxError exception.
if (!strict) { // step 10
input = $replace(/[\u0009\u000A\u000C\u000D\u0020]/g, ''); // step 10.a
}

var inputLength = input.length; // step 11

// eslint-disable-next-line no-negated-condition
if (mod(inputLength, 4) !== 0) { // step 12
if (input !== '' && lastIsEquals(input)) { // step 12.a
input = $strSlice(input, 0, -1); // step 12.a.i
inputLength -= 1; // step 12.a.ii
if (input !== '' && lastIsEquals(input)) { // step 12.a.iii
input = $strSlice(input, 0, -1); // step 12.a.iii.a
inputLength -= 1; // step 12.a.iii.b
}
}
} else { // step 13
if (strict) {
throw new $SyntaxError('Invalid character: input length is not a multiple of four'); // step 13.a
}
}

// If _input_ contains any elements which are not also elements of the standard base64 alphabet, throw a *SyntaxError* exception.

var lastChunkSize = mod(inputLength, 4); // step 15

if (lastChunkSize === 1) { // step 16
throw new $SyntaxError('Invalid character: the length of the final sequence of bytes is 1'); // step 16.a
} else if (lastChunkSize === 2) { // step 17
input += '\u0041\u0041'; // step 17.a
} else if (lastChunkSize === 3) { // step 18
input += '\u0041'; // step 18.a
}

// c. Let bytes be the unique sequence of bytes such that applying the base64url encoding specified in section 5 of RFC 4648 to that sequence would produce characters.
/*
*Let _bytes_ be the unique (possibly empty) sequence of bytes resulting from decoding _input_ as base64 (such that applying the base64 encoding specified in section 4 of <a href="https://datatracker.ietf.org/doc/html/rfc4648">RFC 4648</a> to _bytes_ would produce _input_).
* 1. Let _byteLength_ be the length of _bytes_.
* 1. If _lastChunkSize_ is 2, then
* 1. If _strict_ is *true* and _bytes_[_byteLength_ - 2] is not 0, throw a *SyntaxError* exception.
* 1. Remove the final 2 elements of _bytes_.
* 1. Set _byteLength_ to _byteLength_ - 2.
* 1. Else if _lastChunkSize_ is 3, then
* 1. If _strict_ is *true* and _bytes_[_byteLength_ - 1] is not 0, throw a *SyntaxError* exception.
* 1. Remove the final element of _bytes_.
* 1. Set _byteLength_ to _byteLength_ - 1.
* 1. Let _result_ be ? AllocateTypedArray(*"Uint8Array"*, %Uint8Array%, %Uint8Array.prototype%, _byteLength_).
* 1. Set the value at each index of _result_.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding index of _bytes_.
* 1. Return _result_.
*/
var bytes = [];

var byteLength = bytes.length;

if (lastChunkSize === 2) {
if (strict && bytes[byteLength - 2] !== 0) {
throw new $SyntaxError('Invalid character: the length of the final sequence of bytes is 2');
}
bytes.length -= 2;
byteLength -= 2;
} else if (lastChunkSize === 3) {
if (strict && bytes[byteLength - 1] !== 0) {
throw new $SyntaxError('Invalid character: the length of the final sequence of bytes is 3');
}
bytes.length -= 1;
byteLength -= 1;
}

// var resultLength = bytes.length; // step 13
// var result = AllocateTypedArray('Uint8Array', $Uint8Array, $Uint8Array.prototype, byteLength);

return fromBase64Helper(string, alphabet).result;
};
8 changes: 5 additions & 3 deletions Uint8Array.fromHex/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
var GetIntrinsic = require('get-intrinsic');

var $SyntaxError = GetIntrinsic('%SyntaxError%');
var $TypeError = GetIntrinsic('%TypeError%');
var $Uint8Array = GetIntrinsic('%Uint8Array%', true);

var substring = require('es-abstract/2023/substring');
var GetStringForBinaryEncoding = require('../aos/GetStringForBinaryEncoding');

var regexTest = require('safe-regex-test');

var nonHexChar = regexTest(/[^a-f0-9]/i);

module.exports = function fromHex(value) {
module.exports = function fromHex(string) {
if (!$Uint8Array) {
throw new $SyntaxError('This environment does not support Uint8Array'); // step 1
}

var string = GetStringForBinaryEncoding(value); // step 1
if (typeof string !== 'string') {
throw new $TypeError('`value` is not a string: ' + string); // step 1
}

var stringLen = string.length; // step 3

Expand Down
3 changes: 0 additions & 3 deletions Uint8Array.fromPartialBase64/auto.js

This file was deleted.

80 changes: 0 additions & 80 deletions Uint8Array.fromPartialBase64/implementation.js

This file was deleted.

18 changes: 0 additions & 18 deletions Uint8Array.fromPartialBase64/index.js

This file was deleted.

7 changes: 0 additions & 7 deletions Uint8Array.fromPartialBase64/polyfill.js

This file was deleted.

18 changes: 0 additions & 18 deletions Uint8Array.fromPartialBase64/shim.js

This file was deleted.

5 changes: 3 additions & 2 deletions Uint8Array.prototype.toBase64/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var $TypeError = GetIntrinsic('%TypeError%');
var Get = require('es-abstract/2023/Get');
var GetUint8ArrayBytes = require('../aos/GetUint8ArrayBytes');
var GetOptionsObject = require('../aos/GetOptionsObject');
var ToString = require('es-abstract/2023/ToString');

var toBase64Helper = require('../aos/helpers/toBase64');

Expand All @@ -25,7 +24,9 @@ module.exports = function toBase64() {
alphabet = 'base64'; // step 5
}

alphabet = ToString(alphabet); // step 6
if (typeof alphabet !== 'string') {
throw new $TypeError('`alphabet` is not a string: ' + alphabet); // step 6
}

if (alphabet !== 'base64' && alphabet !== 'base64url') {
throw new $TypeError('Invalid alphabet'); // step 7
Expand Down
3 changes: 0 additions & 3 deletions Uint8Array.prototype.toPartialBase64/auto.js

This file was deleted.

Loading

0 comments on commit 9687555

Please sign in to comment.