es-arraybuffer-base64
Version:
An ES-spec-compliant shim/polyfill/replacement for ArrayBuffer base64 methods that works as far down as ES3
69 lines (51 loc) • 1.9 kB
JavaScript
;
var GetIntrinsic = require('get-intrinsic');
var $parseInt = GetIntrinsic('%parseInt%');
var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');
var modulo = require('es-abstract/2024/modulo');
var substring = require('es-abstract/2024/substring');
var isInteger = require('math-intrinsics/isInteger');
var MAX_SAFE_INTEGER = require('math-intrinsics/constants/maxSafeInteger');
var callBound = require('call-bound');
var safeRegexTest = require('safe-regex-test');
var isHexDigit = safeRegexTest(/^[0-9a-fA-F]+$/);
var $push = callBound('Array.prototype.push');
// https://tc39.es/proposal-arraybuffer-base64/spec/#sec-fromhex
module.exports = function FromHex(string) {
if (typeof string !== 'string') {
throw new $TypeError('Assertion failed: `string` must be a string');
}
var maxLength = arguments.length > 1 ? arguments[1] : MAX_SAFE_INTEGER; // step 1
if (arguments.length > 1 && (!isInteger(maxLength) || maxLength < 0)) {
throw new $TypeError('Assertion failed: `maxLength` must be a non-negative integer: ' + maxLength);
}
var length = string.length; // step 2
var bytes = []; // step 3
var read = 0; // step 4
if (modulo(length, 2) !== 0) { // step 5
return {
'[[Read]]': read,
'[[Bytes]]': bytes,
'[[Error]]': new $SyntaxError('string should be an even number of characters')
};
}
while (read < length && bytes.length < maxLength) { // step 6
var hexits = substring(string, read, read + 2); // step 6.a
if (!isHexDigit(hexits)) { // step 6.b
return {
'[[Read]]': read,
'[[Bytes]]': bytes,
'[[Error]]': new $SyntaxError('string should only contain hex characters')
};
}
read += 2; // step 6.c
var byte = $parseInt(hexits, 16); // step 6.d
$push(bytes, byte); // step 6.e
}
return {
'[[Read]]': read,
'[[Bytes]]': bytes,
'[[Error]]': null
}; // step 7
};