From ac62daa1a1f048d210bdfaf5da8acf0caf28c7d7 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 19 Mar 2024 16:09:24 -0700 Subject: [PATCH] [Refactor] editorial tweaks from https://github.com/tc39/proposal-arraybuffer-base64/pull/55 --- .eslintrc | 2 +- Uint8Array.fromBase64/implementation.js | 24 ++++---- .../implementation.js | 58 +++++++++---------- .../implementation.js | 28 +++++---- .../implementation.js | 10 +--- 5 files changed, 53 insertions(+), 69 deletions(-) diff --git a/.eslintrc b/.eslintrc index 07ddf6b..07879c1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -29,7 +29,7 @@ "SetUint8ArrayBytes", "SetValueInBuffer", "StringPad", - "TypedArrayByteLength", + "TypedArrayLength", "ValidateUint8Array", ], }], diff --git a/Uint8Array.fromBase64/implementation.js b/Uint8Array.fromBase64/implementation.js index 65d0dc2..033d130 100644 --- a/Uint8Array.fromBase64/implementation.js +++ b/Uint8Array.fromBase64/implementation.js @@ -27,33 +27,29 @@ module.exports = function fromBase64(string) { alphabet = 'base64'; // step 4 } - 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 + throw new $TypeError('Invalid `alphabet` value: must be `base64` or `base64url`'); // step 5 } - var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 7 + var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 6 if (typeof lastChunkHandling === 'undefined') { - lastChunkHandling = 'loose'; // step 8 + lastChunkHandling = 'loose'; // step 7 } if (lastChunkHandling !== 'loose' && lastChunkHandling !== 'strict' && lastChunkHandling !== 'stop-before-partial') { - throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 9 + throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 8 } - var result = FromBase64(string, alphabet, lastChunkHandling); // step 10 + var result = FromBase64(string, alphabet, lastChunkHandling); // step 9 - // var resultLength = result['[[Bytes]]']; // step 11 + // var resultLength = result['[[Bytes]]']; // step 10 - // 12. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength). + // 11. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength). - // 13. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding index of result.[[Bytes]]. + // 12. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding index of result.[[Bytes]]. - // 14. Return ta. + // 13. Return ta. - return new $Uint8Array(result['[[Bytes]]']); // step 11 - 14 + return new $Uint8Array(result['[[Bytes]]']); // step 10 - 13 }; diff --git a/Uint8Array.prototype.setFromBase64/implementation.js b/Uint8Array.prototype.setFromBase64/implementation.js index ccc7c61..a1a8b07 100644 --- a/Uint8Array.prototype.setFromBase64/implementation.js +++ b/Uint8Array.prototype.setFromBase64/implementation.js @@ -13,7 +13,7 @@ var IsTypedArrayOutOfBounds = require('es-abstract/2024/IsTypedArrayOutOfBounds' var MakeTypedArrayWithBufferWitnessRecord = require('es-abstract/2024/MakeTypedArrayWithBufferWitnessRecord'); var SetUint8ArrayBytes = require('../aos/SetUint8ArrayBytes'); var SetValueInBuffer = require('es-abstract/2024/SetValueInBuffer'); -var TypedArrayByteLength = require('es-abstract/2024/TypedArrayByteLength'); +var TypedArrayLength = require('es-abstract/2024/TypedArrayLength'); var ValidateUint8Array = require('../aos/ValidateUint8Array'); var typedArrayByteOffset = require('typed-array-byte-offset'); @@ -40,70 +40,64 @@ module.exports = function setFromBase64(string) { alphabet = 'base64'; // step 6 } - if (typeof alphabet !== 'string') { - throw new $TypeError('`alphabet` is not a string: ' + typeof alphabet); // step 7 - } - if (alphabet !== 'base64' && alphabet !== 'base64url') { - throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 8 + throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 7 } - var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 9 + var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 8 if (typeof lastChunkHandling === 'undefined') { - lastChunkHandling = 'loose'; // step 10 + lastChunkHandling = 'loose'; // step 9 } if (lastChunkHandling !== 'loose' && lastChunkHandling !== 'strict' && lastChunkHandling !== 'stop-before-partial') { - throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 11 + throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 10 } - var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 12 + var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 11 if (IsTypedArrayOutOfBounds(taRecord)) { - throw new $TypeError('typed array is out of bounds'); // step 13 + throw new $TypeError('typed array is out of bounds'); // step 12 } - var byteLength = TypedArrayByteLength(taRecord); // step 14 - - var maxLength = byteLength; // step 15 + var byteLength = TypedArrayLength(taRecord); // step 13 - var result = FromBase64(string, alphabet, lastChunkHandling, maxLength); // step 16 + var result = FromBase64(string, alphabet, lastChunkHandling, byteLength); // step 14 - var bytes = result['[[Bytes]]']; // step 17 + var bytes = result['[[Bytes]]']; // step 15 - var written = bytes.length; // step 18 + var written = bytes.length; // step 16 - // 19. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. + // 17. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. if (written > byteLength) { - throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 20 + throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 18 } - SetUint8ArrayBytes(into, bytes); // step 21 + SetUint8ArrayBytes(into, bytes); // step 19 - var offset = typedArrayByteOffset(into); // step 22 + var offset = typedArrayByteOffset(into); // step 20 - var index = 0; // step 23 + var index = 0; // step 21 var intoBuffer = typedArrayBuffer(into); - while (index < written) { // step 24 - var byte = bytes[index]; // step 24.a + while (index < written) { // step 22 + var byte = bytes[index]; // step 22.a - var byteIndexInBuffer = index + offset; // step 24.b + var byteIndexInBuffer = index + offset; // step 22.b - SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'UINT8', byte, true, 'UNORDERED'); // step 24.c + SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'UINT8', byte, true, 'UNORDERED'); // step 22.c - index += 1; // step 24.d + index += 1; // step 22.d } - // 25. Let resultObject be OrdinaryObjectCreate(%Object.prototype%). - // 26. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])). - // 27. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)). - // 28. Return resultObject. + // 23. Let resultObject be OrdinaryObjectCreate(%Object.prototype%). + // 24. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])). + // 25. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)). + // 26. Return resultObject. - return { // steps 25 - 28 + return { // steps 23 - 26 read: result['[[Read]]'], written: written }; diff --git a/Uint8Array.prototype.setFromHex/implementation.js b/Uint8Array.prototype.setFromHex/implementation.js index ca4d4f1..9d81a12 100644 --- a/Uint8Array.prototype.setFromHex/implementation.js +++ b/Uint8Array.prototype.setFromHex/implementation.js @@ -9,7 +9,7 @@ var $Uint8Array = GetIntrinsic('%Uint8Array%', true); var FromHex = require('../aos/FromHex'); var IsTypedArrayOutOfBounds = require('es-abstract/2024/IsTypedArrayOutOfBounds'); var MakeTypedArrayWithBufferWitnessRecord = require('es-abstract/2024/MakeTypedArrayWithBufferWitnessRecord'); -var TypedArrayByteLength = require('es-abstract/2024/TypedArrayByteLength'); +var TypedArrayLength = require('es-abstract/2024/TypedArrayLength'); var ValidateUint8Array = require('../aos/ValidateUint8Array'); var SetUint8ArrayBytes = require('../aos/SetUint8ArrayBytes'); @@ -32,30 +32,28 @@ module.exports = function setFromHex(string) { throw new $TypeError('fromHexInto called on Typed Array backed by detached buffer'); // step 5 } - var byteLength = TypedArrayByteLength(taRecord); // step 6 + var byteLength = TypedArrayLength(taRecord); // step 6 - var maxLength = byteLength; // step 7 + var result = FromHex(string, byteLength); // step 7 - var result = FromHex(string, maxLength); // step 8 + var bytes = result['[[Bytes]]']; // step 8 - var bytes = result['[[Bytes]]']; // step 9 + var written = bytes.length; // step 9 - var written = bytes.length; // step 10 - - // 11. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. + // 10. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk. if (written > byteLength) { - throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 12 + throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 11 } - SetUint8ArrayBytes(into, bytes); // step 13 + SetUint8ArrayBytes(into, bytes); // step 12 - // var resultObject = {}; // step 14 // OrdinaryObjectCreate(%Object.prototype%) - // CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 15 - // CreateDataPropertyOrThrow(resultObject, 'written', written); // step 16 - // return resultObject; // step 17 + // var resultObject = {}; // step 13 // OrdinaryObjectCreate(%Object.prototype%) + // CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 14 + // CreateDataPropertyOrThrow(resultObject, 'written', written); // step 15 + // return resultObject; // step 16 - return { // steps 14 - 17 + return { // steps 13 - 16 read: result['[[Read]]'], written: written }; diff --git a/Uint8Array.prototype.toBase64/implementation.js b/Uint8Array.prototype.toBase64/implementation.js index 36d845b..74c0cfb 100644 --- a/Uint8Array.prototype.toBase64/implementation.js +++ b/Uint8Array.prototype.toBase64/implementation.js @@ -27,16 +27,12 @@ module.exports = function toBase64() { alphabet = 'base64'; // step 5 } - if (typeof alphabet !== 'string') { - throw new $TypeError('`alphabet` is not a string: ' + typeof alphabet); // step 6 - } - if (alphabet !== 'base64' && alphabet !== 'base64url') { - throw new $TypeError('Invalid alphabet'); // step 7 + throw new $TypeError('Invalid `alphabet` value: must be `base64` or `base64url`'); // step 6 } // eslint-disable-next-line no-unused-vars - var toEncode = GetUint8ArrayBytes(O); // step 8 + var toEncode = GetUint8ArrayBytes(O); // step 7 // if (alphabet === 'base64') { // step 8 // a. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64 encoding specified in section 4 of RFC 4648. @@ -45,7 +41,7 @@ module.exports = function toBase64() { // b. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64url encoding specified in section 5 of RFC 4648. // } - // return CodePointsToString(outAscii); // step 10 + // return CodePointsToString(outAscii); // step 9 // code adapted from https://github.com/tc39/proposal-arraybuffer-base64/blob/22228812214d5a1c2966cd626f43be3576e79290/playground/polyfill-core.mjs var lookup = alphabetFromIdentifier(alphabet);