Comparing version 1.0.17 to 1.1.0
1060
build/utils.js
@@ -1,777 +0,359 @@ | ||
"use strict"; | ||
/*! | ||
Copyright (c) Peculiar Ventures, LLC | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getUTCDate = getUTCDate; | ||
exports.getParametersValue = getParametersValue; | ||
exports.bufferToHexCodes = bufferToHexCodes; | ||
exports.checkBufferParams = checkBufferParams; | ||
exports.utilFromBase = utilFromBase; | ||
exports.utilToBase = utilToBase; | ||
exports.utilConcatBuf = utilConcatBuf; | ||
exports.utilConcatView = utilConcatView; | ||
exports.utilDecodeTC = utilDecodeTC; | ||
exports.utilEncodeTC = utilEncodeTC; | ||
exports.isEqualBuffer = isEqualBuffer; | ||
exports.padNumber = padNumber; | ||
exports.toBase64 = toBase64; | ||
exports.fromBase64 = fromBase64; | ||
exports.arrayBufferToString = arrayBufferToString; | ||
exports.stringToArrayBuffer = stringToArrayBuffer; | ||
exports.nearestPowerOf2 = nearestPowerOf2; | ||
exports.clearProps = clearProps; | ||
//************************************************************************************** | ||
/** | ||
* Making UTC date from local date | ||
* @param {Date} date Date to convert from | ||
* @returns {Date} | ||
*/ | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function getUTCDate(date) { | ||
// noinspection NestedFunctionCallJS, MagicNumberJS | ||
return new Date(date.getTime() + date.getTimezoneOffset() * 60000); | ||
return new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Get value for input parameters, or set a default value | ||
* @param {Object} parameters | ||
* @param {string} name | ||
* @param defaultValue | ||
*/ | ||
function getParametersValue(parameters, name, defaultValue) { | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS | ||
if (parameters instanceof Object === false) return defaultValue; | ||
// noinspection NonBlockStatementBodyJS | ||
if (name in parameters) return parameters[name]; | ||
return defaultValue; | ||
if ((parameters instanceof Object) === false) { | ||
return defaultValue; | ||
} | ||
return parameters[name] ?? defaultValue; | ||
} | ||
//************************************************************************************** | ||
/** | ||
* Converts "ArrayBuffer" into a hexdecimal string | ||
* @param {ArrayBuffer} inputBuffer | ||
* @param {number} [inputOffset=0] | ||
* @param {number} [inputLength=inputBuffer.byteLength] | ||
* @param {boolean} [insertSpace=false] | ||
* @returns {string} | ||
*/ | ||
function bufferToHexCodes(inputBuffer, inputOffset = 0, inputLength = inputBuffer.byteLength - inputOffset, insertSpace = false) { | ||
let result = ""; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = new Uint8Array(inputBuffer, inputOffset, inputLength)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const item = _step.value; | ||
// noinspection ChainedFunctionCallJS | ||
const str = item.toString(16).toUpperCase(); | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS | ||
if (str.length === 1) result += "0"; | ||
result += str; | ||
// noinspection NonBlockStatementBodyJS | ||
if (insertSpace) result += " "; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return result.trim(); | ||
function bufferToHexCodes(inputBuffer, inputOffset = 0, inputLength = (inputBuffer.byteLength - inputOffset), insertSpace = false) { | ||
let result = ""; | ||
for (const item of (new Uint8Array(inputBuffer, inputOffset, inputLength))) { | ||
const str = item.toString(16).toUpperCase(); | ||
if (str.length === 1) { | ||
result += "0"; | ||
} | ||
result += str; | ||
if (insertSpace) { | ||
result += " "; | ||
} | ||
} | ||
return result.trim(); | ||
} | ||
//************************************************************************************** | ||
// noinspection JSValidateJSDoc, FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Check input "ArrayBuffer" for common functions | ||
* @param {LocalBaseBlock} baseBlock | ||
* @param {ArrayBuffer} inputBuffer | ||
* @param {number} inputOffset | ||
* @param {number} inputLength | ||
* @returns {boolean} | ||
*/ | ||
function checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength) { | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (inputBuffer instanceof ArrayBuffer === false) { | ||
// noinspection JSUndefinedPropertyAssignment | ||
baseBlock.error = "Wrong parameter: inputBuffer must be \"ArrayBuffer\""; | ||
return false; | ||
} | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (inputBuffer.byteLength === 0) { | ||
// noinspection JSUndefinedPropertyAssignment | ||
baseBlock.error = "Wrong parameter: inputBuffer has zero length"; | ||
return false; | ||
} | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (inputOffset < 0) { | ||
// noinspection JSUndefinedPropertyAssignment | ||
baseBlock.error = "Wrong parameter: inputOffset less than zero"; | ||
return false; | ||
} | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (inputLength < 0) { | ||
// noinspection JSUndefinedPropertyAssignment | ||
baseBlock.error = "Wrong parameter: inputLength less than zero"; | ||
return false; | ||
} | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (inputBuffer.byteLength - inputOffset - inputLength < 0) { | ||
// noinspection JSUndefinedPropertyAssignment | ||
baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)"; | ||
return false; | ||
} | ||
return true; | ||
if (!(inputBuffer instanceof ArrayBuffer)) { | ||
baseBlock.error = "Wrong parameter: inputBuffer must be \"ArrayBuffer\""; | ||
return false; | ||
} | ||
if (!inputBuffer.byteLength) { | ||
baseBlock.error = "Wrong parameter: inputBuffer has zero length"; | ||
return false; | ||
} | ||
if (inputOffset < 0) { | ||
baseBlock.error = "Wrong parameter: inputOffset less than zero"; | ||
return false; | ||
} | ||
if (inputLength < 0) { | ||
baseBlock.error = "Wrong parameter: inputLength less than zero"; | ||
return false; | ||
} | ||
if ((inputBuffer.byteLength - inputOffset - inputLength) < 0) { | ||
baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Convert number from 2^base to 2^10 | ||
* @param {Uint8Array} inputBuffer | ||
* @param {number} inputBase | ||
* @returns {number} | ||
*/ | ||
function utilFromBase(inputBuffer, inputBase) { | ||
let result = 0; | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS | ||
if (inputBuffer.length === 1) return inputBuffer[0]; | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS | ||
for (let i = inputBuffer.length - 1; i >= 0; i--) result += inputBuffer[inputBuffer.length - 1 - i] * Math.pow(2, inputBase * i); | ||
return result; | ||
let result = 0; | ||
if (inputBuffer.length === 1) { | ||
return inputBuffer[0]; | ||
} | ||
for (let i = (inputBuffer.length - 1); i >= 0; i--) { | ||
result += inputBuffer[(inputBuffer.length - 1) - i] * Math.pow(2, inputBase * i); | ||
} | ||
return result; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS, FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Convert number from 2^10 to 2^base | ||
* @param {!number} value The number to convert | ||
* @param {!number} base The base for 2^base | ||
* @param {number} [reserved=0] Pre-defined number of bytes in output array (-1 = limited by function itself) | ||
* @returns {ArrayBuffer} | ||
*/ | ||
function utilToBase(value, base, reserved = -1) { | ||
const internalReserved = reserved; | ||
let internalValue = value; | ||
let result = 0; | ||
let biggest = Math.pow(2, base); | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
for (let i = 1; i < 8; i++) { | ||
if (value < biggest) { | ||
let retBuf; | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (internalReserved < 0) { | ||
retBuf = new ArrayBuffer(i); | ||
result = i; | ||
} else { | ||
// noinspection NonBlockStatementBodyJS | ||
if (internalReserved < i) return new ArrayBuffer(0); | ||
retBuf = new ArrayBuffer(internalReserved); | ||
result = internalReserved; | ||
} | ||
const retView = new Uint8Array(retBuf); | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
for (let j = i - 1; j >= 0; j--) { | ||
const basis = Math.pow(2, j * base); | ||
retView[result - j - 1] = Math.floor(internalValue / basis); | ||
internalValue -= retView[result - j - 1] * basis; | ||
} | ||
return retBuf; | ||
} | ||
biggest *= Math.pow(2, base); | ||
} | ||
return new ArrayBuffer(0); | ||
function utilToBase(value, base, reserved = (-1)) { | ||
const internalReserved = reserved; | ||
let internalValue = value; | ||
let result = 0; | ||
let biggest = Math.pow(2, base); | ||
for (let i = 1; i < 8; i++) { | ||
if (value < biggest) { | ||
let retBuf; | ||
if (internalReserved < 0) { | ||
retBuf = new ArrayBuffer(i); | ||
result = i; | ||
} | ||
else { | ||
if (internalReserved < i) { | ||
return (new ArrayBuffer(0)); | ||
} | ||
retBuf = new ArrayBuffer(internalReserved); | ||
result = internalReserved; | ||
} | ||
const retView = new Uint8Array(retBuf); | ||
for (let j = (i - 1); j >= 0; j--) { | ||
const basis = Math.pow(2, j * base); | ||
retView[result - j - 1] = Math.floor(internalValue / basis); | ||
internalValue -= (retView[result - j - 1]) * basis; | ||
} | ||
return retBuf; | ||
} | ||
biggest *= Math.pow(2, base); | ||
} | ||
return new ArrayBuffer(0); | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS | ||
/** | ||
* Concatenate two ArrayBuffers | ||
* @param {...ArrayBuffer} buffers Set of ArrayBuffer | ||
*/ | ||
function utilConcatBuf(...buffers) { | ||
//region Initial variables | ||
let outputLength = 0; | ||
let prevLength = 0; | ||
//endregion | ||
//region Calculate output length | ||
// noinspection NonBlockStatementBodyJS | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = buffers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
const buffer = _step2.value; | ||
outputLength += buffer.byteLength; | ||
} //endregion | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
const retBuf = new ArrayBuffer(outputLength); | ||
const retView = new Uint8Array(retBuf); | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = buffers[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
const buffer = _step3.value; | ||
// noinspection NestedFunctionCallJS | ||
retView.set(new Uint8Array(buffer), prevLength); | ||
prevLength += buffer.byteLength; | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return retBuf; | ||
let outputLength = 0; | ||
let prevLength = 0; | ||
for (const buffer of buffers) { | ||
outputLength += buffer.byteLength; | ||
} | ||
const retBuf = new ArrayBuffer(outputLength); | ||
const retView = new Uint8Array(retBuf); | ||
for (const buffer of buffers) { | ||
retView.set(new Uint8Array(buffer), prevLength); | ||
prevLength += buffer.byteLength; | ||
} | ||
return retBuf; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS | ||
/** | ||
* Concatenate two Uint8Array | ||
* @param {...Uint8Array} views Set of Uint8Array | ||
*/ | ||
function utilConcatView(...views) { | ||
//region Initial variables | ||
let outputLength = 0; | ||
let prevLength = 0; | ||
//endregion | ||
//region Calculate output length | ||
// noinspection NonBlockStatementBodyJS | ||
var _iteratorNormalCompletion4 = true; | ||
var _didIteratorError4 = false; | ||
var _iteratorError4 = undefined; | ||
try { | ||
for (var _iterator4 = views[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
const view = _step4.value; | ||
outputLength += view.length; | ||
} //endregion | ||
} catch (err) { | ||
_didIteratorError4 = true; | ||
_iteratorError4 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError4) { | ||
throw _iteratorError4; | ||
} | ||
} | ||
} | ||
const retBuf = new ArrayBuffer(outputLength); | ||
const retView = new Uint8Array(retBuf); | ||
var _iteratorNormalCompletion5 = true; | ||
var _didIteratorError5 = false; | ||
var _iteratorError5 = undefined; | ||
try { | ||
for (var _iterator5 = views[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
const view = _step5.value; | ||
retView.set(view, prevLength); | ||
prevLength += view.length; | ||
} | ||
} catch (err) { | ||
_didIteratorError5 = true; | ||
_iteratorError5 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion5 && _iterator5.return) { | ||
_iterator5.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError5) { | ||
throw _iteratorError5; | ||
} | ||
} | ||
} | ||
return retView; | ||
let outputLength = 0; | ||
let prevLength = 0; | ||
for (const view of views) { | ||
outputLength += view.length; | ||
} | ||
const retBuf = new ArrayBuffer(outputLength); | ||
const retView = new Uint8Array(retBuf); | ||
for (const view of views) { | ||
retView.set(view, prevLength); | ||
prevLength += view.length; | ||
} | ||
return retView; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS | ||
/** | ||
* Decoding of "two complement" values | ||
* The function must be called in scope of instance of "hexBlock" class ("valueHex" and "warnings" properties must be present) | ||
* @returns {number} | ||
*/ | ||
function utilDecodeTC() { | ||
const buf = new Uint8Array(this.valueHex); | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (this.valueHex.byteLength >= 2) { | ||
//noinspection JSBitwiseOperatorUsage, ConstantOnRightSideOfComparisonJS, LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
const condition1 = buf[0] === 0xFF && buf[1] & 0x80; | ||
// noinspection ConstantOnRightSideOfComparisonJS, LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
const condition2 = buf[0] === 0x00 && (buf[1] & 0x80) === 0x00; | ||
// noinspection NonBlockStatementBodyJS | ||
if (condition1 || condition2) this.warnings.push("Needlessly long format"); | ||
} | ||
//region Create big part of the integer | ||
const bigIntBuffer = new ArrayBuffer(this.valueHex.byteLength); | ||
const bigIntView = new Uint8Array(bigIntBuffer); | ||
// noinspection NonBlockStatementBodyJS | ||
for (let i = 0; i < this.valueHex.byteLength; i++) bigIntView[i] = 0; | ||
// noinspection MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
bigIntView[0] = buf[0] & 0x80; // mask only the biggest bit | ||
const bigInt = utilFromBase(bigIntView, 8); | ||
//endregion | ||
//region Create small part of the integer | ||
const smallIntBuffer = new ArrayBuffer(this.valueHex.byteLength); | ||
const smallIntView = new Uint8Array(smallIntBuffer); | ||
// noinspection NonBlockStatementBodyJS | ||
for (let j = 0; j < this.valueHex.byteLength; j++) smallIntView[j] = buf[j]; | ||
// noinspection MagicNumberJS | ||
smallIntView[0] &= 0x7F; // mask biggest bit | ||
const smallInt = utilFromBase(smallIntView, 8); | ||
//endregion | ||
return smallInt - bigInt; | ||
const buf = new Uint8Array(this.valueHex); | ||
if (this.valueHex.byteLength >= 2) { | ||
const condition1 = (buf[0] === 0xFF) && (buf[1] & 0x80); | ||
const condition2 = (buf[0] === 0x00) && ((buf[1] & 0x80) === 0x00); | ||
if (condition1 || condition2) { | ||
this.warnings.push("Needlessly long format"); | ||
} | ||
} | ||
const bigIntBuffer = new ArrayBuffer(this.valueHex.byteLength); | ||
const bigIntView = new Uint8Array(bigIntBuffer); | ||
for (let i = 0; i < this.valueHex.byteLength; i++) { | ||
bigIntView[i] = 0; | ||
} | ||
bigIntView[0] = (buf[0] & 0x80); | ||
const bigInt = utilFromBase(bigIntView, 8); | ||
const smallIntBuffer = new ArrayBuffer(this.valueHex.byteLength); | ||
const smallIntView = new Uint8Array(smallIntBuffer); | ||
for (let j = 0; j < this.valueHex.byteLength; j++) { | ||
smallIntView[j] = buf[j]; | ||
} | ||
smallIntView[0] &= 0x7F; | ||
const smallInt = utilFromBase(smallIntView, 8); | ||
return (smallInt - bigInt); | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS, FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Encode integer value to "two complement" format | ||
* @param {number} value Value to encode | ||
* @returns {ArrayBuffer} | ||
*/ | ||
function utilEncodeTC(value) { | ||
// noinspection ConstantOnRightSideOfComparisonJS, ConditionalExpressionJS | ||
const modValue = value < 0 ? value * -1 : value; | ||
let bigInt = 128; | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
for (let i = 1; i < 8; i++) { | ||
if (modValue <= bigInt) { | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (value < 0) { | ||
const smallInt = bigInt - modValue; | ||
const retBuf = utilToBase(smallInt, 8, i); | ||
const retView = new Uint8Array(retBuf); | ||
// noinspection MagicNumberJS | ||
retView[0] |= 0x80; | ||
return retBuf; | ||
} | ||
let retBuf = utilToBase(modValue, 8, i); | ||
let retView = new Uint8Array(retBuf); | ||
//noinspection JSBitwiseOperatorUsage, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
if (retView[0] & 0x80) { | ||
//noinspection JSCheckFunctionSignatures | ||
const tempBuf = retBuf.slice(0); | ||
const tempView = new Uint8Array(tempBuf); | ||
retBuf = new ArrayBuffer(retBuf.byteLength + 1); | ||
// noinspection ReuseOfLocalVariableJS | ||
retView = new Uint8Array(retBuf); | ||
// noinspection NonBlockStatementBodyJS | ||
for (let k = 0; k < tempBuf.byteLength; k++) retView[k + 1] = tempView[k]; | ||
// noinspection MagicNumberJS | ||
retView[0] = 0x00; | ||
} | ||
return retBuf; | ||
} | ||
bigInt *= Math.pow(2, 8); | ||
} | ||
return new ArrayBuffer(0); | ||
const modValue = (value < 0) ? (value * (-1)) : value; | ||
let bigInt = 128; | ||
for (let i = 1; i < 8; i++) { | ||
if (modValue <= bigInt) { | ||
if (value < 0) { | ||
const smallInt = bigInt - modValue; | ||
const retBuf = utilToBase(smallInt, 8, i); | ||
const retView = new Uint8Array(retBuf); | ||
retView[0] |= 0x80; | ||
return retBuf; | ||
} | ||
let retBuf = utilToBase(modValue, 8, i); | ||
let retView = new Uint8Array(retBuf); | ||
if (retView[0] & 0x80) { | ||
const tempBuf = retBuf.slice(0); | ||
const tempView = new Uint8Array(tempBuf); | ||
retBuf = new ArrayBuffer(retBuf.byteLength + 1); | ||
retView = new Uint8Array(retBuf); | ||
for (let k = 0; k < tempBuf.byteLength; k++) { | ||
retView[k + 1] = tempView[k]; | ||
} | ||
retView[0] = 0x00; | ||
} | ||
return retBuf; | ||
} | ||
bigInt *= Math.pow(2, 8); | ||
} | ||
return (new ArrayBuffer(0)); | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleReturnPointsJS, ParameterNamingConventionJS | ||
/** | ||
* Compare two array buffers | ||
* @param {!ArrayBuffer} inputBuffer1 | ||
* @param {!ArrayBuffer} inputBuffer2 | ||
* @returns {boolean} | ||
*/ | ||
function isEqualBuffer(inputBuffer1, inputBuffer2) { | ||
// noinspection NonBlockStatementBodyJS | ||
if (inputBuffer1.byteLength !== inputBuffer2.byteLength) return false; | ||
// noinspection LocalVariableNamingConventionJS | ||
const view1 = new Uint8Array(inputBuffer1); | ||
// noinspection LocalVariableNamingConventionJS | ||
const view2 = new Uint8Array(inputBuffer2); | ||
for (let i = 0; i < view1.length; i++) { | ||
// noinspection NonBlockStatementBodyJS | ||
if (view1[i] !== view2[i]) return false; | ||
} | ||
return true; | ||
if (inputBuffer1.byteLength !== inputBuffer2.byteLength) { | ||
return false; | ||
} | ||
const view1 = new Uint8Array(inputBuffer1); | ||
const view2 = new Uint8Array(inputBuffer2); | ||
for (let i = 0; i < view1.length; i++) { | ||
if (view1[i] !== view2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleReturnPointsJS | ||
/** | ||
* Pad input number with leade "0" if needed | ||
* @returns {string} | ||
* @param {number} inputNumber | ||
* @param {number} fullLength | ||
*/ | ||
function padNumber(inputNumber, fullLength) { | ||
const str = inputNumber.toString(10); | ||
// noinspection NonBlockStatementBodyJS | ||
if (fullLength < str.length) return ""; | ||
const dif = fullLength - str.length; | ||
const padding = new Array(dif); | ||
// noinspection NonBlockStatementBodyJS | ||
for (let i = 0; i < dif; i++) padding[i] = "0"; | ||
const paddingString = padding.join(""); | ||
return paddingString.concat(str); | ||
const str = inputNumber.toString(10); | ||
if (fullLength < str.length) { | ||
return ""; | ||
} | ||
const dif = fullLength - str.length; | ||
const padding = new Array(dif); | ||
for (let i = 0; i < dif; i++) { | ||
padding[i] = "0"; | ||
} | ||
const paddingString = padding.join(""); | ||
return paddingString.concat(str); | ||
} | ||
//************************************************************************************** | ||
const base64Template = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | ||
const base64UrlTemplate = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="; | ||
//************************************************************************************** | ||
// noinspection FunctionWithMultipleLoopsJS, OverlyComplexFunctionJS, FunctionTooLongJS, FunctionNamingConventionJS | ||
/** | ||
* Encode string into BASE64 (or "base64url") | ||
* @param {string} input | ||
* @param {boolean} useUrlTemplate If "true" then output would be encoded using "base64url" | ||
* @param {boolean} skipPadding Skip BASE-64 padding or not | ||
* @param {boolean} skipLeadingZeros Skip leading zeros in input data or not | ||
* @returns {string} | ||
*/ | ||
function toBase64(input, useUrlTemplate = false, skipPadding = false, skipLeadingZeros = false) { | ||
let i = 0; | ||
// noinspection LocalVariableNamingConventionJS | ||
let flag1 = 0; | ||
// noinspection LocalVariableNamingConventionJS | ||
let flag2 = 0; | ||
let output = ""; | ||
// noinspection ConditionalExpressionJS | ||
const template = useUrlTemplate ? base64UrlTemplate : base64Template; | ||
if (skipLeadingZeros) { | ||
let nonZeroPosition = 0; | ||
for (let i = 0; i < input.length; i++) { | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (input.charCodeAt(i) !== 0) { | ||
nonZeroPosition = i; | ||
// noinspection BreakStatementJS | ||
break; | ||
} | ||
} | ||
// noinspection AssignmentToFunctionParameterJS | ||
input = input.slice(nonZeroPosition); | ||
} | ||
while (i < input.length) { | ||
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS | ||
const chr1 = input.charCodeAt(i++); | ||
// noinspection NonBlockStatementBodyJS | ||
if (i >= input.length) flag1 = 1; | ||
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS | ||
const chr2 = input.charCodeAt(i++); | ||
// noinspection NonBlockStatementBodyJS | ||
if (i >= input.length) flag2 = 1; | ||
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS | ||
const chr3 = input.charCodeAt(i++); | ||
// noinspection LocalVariableNamingConventionJS | ||
const enc1 = chr1 >> 2; | ||
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
const enc2 = (chr1 & 0x03) << 4 | chr2 >> 4; | ||
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
let enc3 = (chr2 & 0x0F) << 2 | chr3 >> 6; | ||
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
let enc4 = chr3 & 0x3F; | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (flag1 === 1) { | ||
// noinspection NestedAssignmentJS, AssignmentResultUsedJS, MagicNumberJS | ||
enc3 = enc4 = 64; | ||
} else { | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (flag2 === 1) { | ||
// noinspection MagicNumberJS | ||
enc4 = 64; | ||
} | ||
} | ||
// noinspection NonBlockStatementBodyJS | ||
if (skipPadding) { | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS | ||
if (enc3 === 64) output += `${template.charAt(enc1)}${template.charAt(enc2)}`;else { | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS | ||
if (enc4 === 64) output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}`;else output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`; | ||
} | ||
} else output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`; | ||
} | ||
return output; | ||
let i = 0; | ||
let flag1 = 0; | ||
let flag2 = 0; | ||
let output = ""; | ||
const template = (useUrlTemplate) ? base64UrlTemplate : base64Template; | ||
if (skipLeadingZeros) { | ||
let nonZeroPosition = 0; | ||
for (let i = 0; i < input.length; i++) { | ||
if (input.charCodeAt(i) !== 0) { | ||
nonZeroPosition = i; | ||
break; | ||
} | ||
} | ||
input = input.slice(nonZeroPosition); | ||
} | ||
while (i < input.length) { | ||
const chr1 = input.charCodeAt(i++); | ||
if (i >= input.length) { | ||
flag1 = 1; | ||
} | ||
const chr2 = input.charCodeAt(i++); | ||
if (i >= input.length) { | ||
flag2 = 1; | ||
} | ||
const chr3 = input.charCodeAt(i++); | ||
const enc1 = chr1 >> 2; | ||
const enc2 = ((chr1 & 0x03) << 4) | (chr2 >> 4); | ||
let enc3 = ((chr2 & 0x0F) << 2) | (chr3 >> 6); | ||
let enc4 = chr3 & 0x3F; | ||
if (flag1 === 1) { | ||
enc3 = enc4 = 64; | ||
} | ||
else { | ||
if (flag2 === 1) { | ||
enc4 = 64; | ||
} | ||
} | ||
if (skipPadding) { | ||
if (enc3 === 64) { | ||
output += `${template.charAt(enc1)}${template.charAt(enc2)}`; | ||
} | ||
else { | ||
if (enc4 === 64) { | ||
output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}`; | ||
} | ||
else { | ||
output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`; | ||
} | ||
} | ||
} | ||
else { | ||
output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`; | ||
} | ||
} | ||
return output; | ||
} | ||
//************************************************************************************** | ||
// noinspection FunctionWithMoreThanThreeNegationsJS, FunctionWithMultipleLoopsJS, OverlyComplexFunctionJS, FunctionNamingConventionJS | ||
/** | ||
* Decode string from BASE64 (or "base64url") | ||
* @param {string} input | ||
* @param {boolean} [useUrlTemplate=false] If "true" then output would be encoded using "base64url" | ||
* @param {boolean} [cutTailZeros=false] If "true" then cut tailing zeroz from function result | ||
* @returns {string} | ||
*/ | ||
function fromBase64(input, useUrlTemplate = false, cutTailZeros = false) { | ||
// noinspection ConditionalExpressionJS | ||
const template = useUrlTemplate ? base64UrlTemplate : base64Template; | ||
//region Aux functions | ||
// noinspection FunctionWithMultipleReturnPointsJS, NestedFunctionJS | ||
function indexof(toSearch) { | ||
// noinspection ConstantOnRightSideOfComparisonJS, MagicNumberJS | ||
for (let i = 0; i < 64; i++) { | ||
// noinspection NonBlockStatementBodyJS | ||
if (template.charAt(i) === toSearch) return i; | ||
} | ||
// noinspection MagicNumberJS | ||
return 64; | ||
} | ||
// noinspection NestedFunctionJS | ||
function test(incoming) { | ||
// noinspection ConstantOnRightSideOfComparisonJS, ConditionalExpressionJS, MagicNumberJS | ||
return incoming === 64 ? 0x00 : incoming; | ||
} | ||
//endregion | ||
let i = 0; | ||
let output = ""; | ||
while (i < input.length) { | ||
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS | ||
const enc1 = indexof(input.charAt(i++)); | ||
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS | ||
const enc2 = i >= input.length ? 0x00 : indexof(input.charAt(i++)); | ||
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS | ||
const enc3 = i >= input.length ? 0x00 : indexof(input.charAt(i++)); | ||
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS | ||
const enc4 = i >= input.length ? 0x00 : indexof(input.charAt(i++)); | ||
// noinspection LocalVariableNamingConventionJS, NonShortCircuitBooleanExpressionJS | ||
const chr1 = test(enc1) << 2 | test(enc2) >> 4; | ||
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
const chr2 = (test(enc2) & 0x0F) << 4 | test(enc3) >> 2; | ||
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS | ||
const chr3 = (test(enc3) & 0x03) << 6 | test(enc4); | ||
output += String.fromCharCode(chr1); | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS | ||
if (enc3 !== 64) output += String.fromCharCode(chr2); | ||
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS | ||
if (enc4 !== 64) output += String.fromCharCode(chr3); | ||
} | ||
if (cutTailZeros) { | ||
const outputLength = output.length; | ||
let nonZeroStart = -1; | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
for (let i = outputLength - 1; i >= 0; i--) { | ||
// noinspection ConstantOnRightSideOfComparisonJS | ||
if (output.charCodeAt(i) !== 0) { | ||
nonZeroStart = i; | ||
// noinspection BreakStatementJS | ||
break; | ||
} | ||
} | ||
// noinspection NonBlockStatementBodyJS, NegatedIfStatementJS | ||
if (nonZeroStart !== -1) output = output.slice(0, nonZeroStart + 1);else output = ""; | ||
} | ||
return output; | ||
const template = (useUrlTemplate) ? base64UrlTemplate : base64Template; | ||
function indexOf(toSearch) { | ||
for (let i = 0; i < 64; i++) { | ||
if (template.charAt(i) === toSearch) | ||
return i; | ||
} | ||
return 64; | ||
} | ||
function test(incoming) { | ||
return ((incoming === 64) ? 0x00 : incoming); | ||
} | ||
let i = 0; | ||
let output = ""; | ||
while (i < input.length) { | ||
const enc1 = indexOf(input.charAt(i++)); | ||
const enc2 = (i >= input.length) ? 0x00 : indexOf(input.charAt(i++)); | ||
const enc3 = (i >= input.length) ? 0x00 : indexOf(input.charAt(i++)); | ||
const enc4 = (i >= input.length) ? 0x00 : indexOf(input.charAt(i++)); | ||
const chr1 = (test(enc1) << 2) | (test(enc2) >> 4); | ||
const chr2 = ((test(enc2) & 0x0F) << 4) | (test(enc3) >> 2); | ||
const chr3 = ((test(enc3) & 0x03) << 6) | test(enc4); | ||
output += String.fromCharCode(chr1); | ||
if (enc3 !== 64) { | ||
output += String.fromCharCode(chr2); | ||
} | ||
if (enc4 !== 64) { | ||
output += String.fromCharCode(chr3); | ||
} | ||
} | ||
if (cutTailZeros) { | ||
const outputLength = output.length; | ||
let nonZeroStart = (-1); | ||
for (let i = (outputLength - 1); i >= 0; i--) { | ||
if (output.charCodeAt(i) !== 0) { | ||
nonZeroStart = i; | ||
break; | ||
} | ||
} | ||
if (nonZeroStart !== (-1)) { | ||
output = output.slice(0, nonZeroStart + 1); | ||
} | ||
else { | ||
output = ""; | ||
} | ||
} | ||
return output; | ||
} | ||
//************************************************************************************** | ||
function arrayBufferToString(buffer) { | ||
let resultString = ""; | ||
const view = new Uint8Array(buffer); | ||
// noinspection NonBlockStatementBodyJS | ||
var _iteratorNormalCompletion6 = true; | ||
var _didIteratorError6 = false; | ||
var _iteratorError6 = undefined; | ||
try { | ||
for (var _iterator6 = view[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) { | ||
const element = _step6.value; | ||
resultString += String.fromCharCode(element); | ||
} | ||
} catch (err) { | ||
_didIteratorError6 = true; | ||
_iteratorError6 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion6 && _iterator6.return) { | ||
_iterator6.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError6) { | ||
throw _iteratorError6; | ||
} | ||
} | ||
} | ||
return resultString; | ||
let resultString = ""; | ||
const view = new Uint8Array(buffer); | ||
for (const element of view) { | ||
resultString += String.fromCharCode(element); | ||
} | ||
return resultString; | ||
} | ||
//************************************************************************************** | ||
function stringToArrayBuffer(str) { | ||
const stringLength = str.length; | ||
const resultBuffer = new ArrayBuffer(stringLength); | ||
const resultView = new Uint8Array(resultBuffer); | ||
// noinspection NonBlockStatementBodyJS | ||
for (let i = 0; i < stringLength; i++) resultView[i] = str.charCodeAt(i); | ||
return resultBuffer; | ||
const stringLength = str.length; | ||
const resultBuffer = new ArrayBuffer(stringLength); | ||
const resultView = new Uint8Array(resultBuffer); | ||
for (let i = 0; i < stringLength; i++) { | ||
resultView[i] = str.charCodeAt(i); | ||
} | ||
return resultBuffer; | ||
} | ||
//************************************************************************************** | ||
const log2 = Math.log(2); | ||
//************************************************************************************** | ||
// noinspection FunctionNamingConventionJS | ||
/** | ||
* Get nearest to input length power of 2 | ||
* @param {number} length Current length of existing array | ||
* @returns {number} | ||
*/ | ||
function nearestPowerOf2(length) { | ||
const base = Math.log(length) / log2; | ||
const floor = Math.floor(base); | ||
const round = Math.round(base); | ||
// noinspection ConditionalExpressionJS | ||
return floor === round ? floor : round; | ||
const base = (Math.log(length) / log2); | ||
const floor = Math.floor(base); | ||
const round = Math.round(base); | ||
return ((floor === round) ? floor : round); | ||
} | ||
//************************************************************************************** | ||
/** | ||
* Delete properties by name from specified object | ||
* @param {Object} object Object to delete properties from | ||
* @param {Array.<string>} propsArray Array of properties names | ||
*/ | ||
function clearProps(object, propsArray) { | ||
var _iteratorNormalCompletion7 = true; | ||
var _didIteratorError7 = false; | ||
var _iteratorError7 = undefined; | ||
for (const prop of propsArray) { | ||
delete object[prop]; | ||
} | ||
} | ||
try { | ||
for (var _iterator7 = propsArray[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) { | ||
const prop = _step7.value; | ||
delete object[prop]; | ||
} | ||
} catch (err) { | ||
_didIteratorError7 = true; | ||
_iteratorError7 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion7 && _iterator7.return) { | ||
_iterator7.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError7) { | ||
throw _iteratorError7; | ||
} | ||
} | ||
} | ||
} | ||
//************************************************************************************** | ||
//# sourceMappingURL=utils.js.map | ||
exports.arrayBufferToString = arrayBufferToString; | ||
exports.bufferToHexCodes = bufferToHexCodes; | ||
exports.checkBufferParams = checkBufferParams; | ||
exports.clearProps = clearProps; | ||
exports.fromBase64 = fromBase64; | ||
exports.getParametersValue = getParametersValue; | ||
exports.getUTCDate = getUTCDate; | ||
exports.isEqualBuffer = isEqualBuffer; | ||
exports.nearestPowerOf2 = nearestPowerOf2; | ||
exports.padNumber = padNumber; | ||
exports.stringToArrayBuffer = stringToArrayBuffer; | ||
exports.toBase64 = toBase64; | ||
exports.utilConcatBuf = utilConcatBuf; | ||
exports.utilConcatView = utilConcatView; | ||
exports.utilDecodeTC = utilDecodeTC; | ||
exports.utilEncodeTC = utilEncodeTC; | ||
exports.utilFromBase = utilFromBase; | ||
exports.utilToBase = utilToBase; |
@@ -10,2 +10,6 @@ { | ||
"name": "Ryan Hurst" | ||
}, | ||
{ | ||
"email": "microshine@mail.ru", | ||
"name": "Miroshin Stepan" | ||
} | ||
@@ -22,23 +26,36 @@ ], | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
"build": "babel src --compact=false --out-dir build --source-maps", | ||
"watch": "babel src --compact=false --out-dir build --source-maps --watch", | ||
"test": "cross-env NODE_ENV=test nyc mocha --require babel-register --require babel-polyfill" | ||
"prepare": "npm run build", | ||
"test": "mocha", | ||
"lint": "eslint . --ext .ts", | ||
"lint:fix": "eslint --fix . --ext .ts", | ||
"build": "rollup -c", | ||
"coverage": "nyc npm test", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"jsnext:main": "./src/utils.js", | ||
"module": "./src/utils.js", | ||
"files": [ | ||
"build", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"module": "./build/utils.es.js", | ||
"main": "./build/utils.js", | ||
"types": "./build/index.d.ts", | ||
"devDependencies": { | ||
"babel-cli": "latest", | ||
"mocha": "^3.0.2", | ||
"nyc": "^11.6.0", | ||
"cross-env": "^5.1.4", | ||
"babel-plugin-istanbul": "^4.1.5", | ||
"babel-preset-env": "latest", | ||
"babel-polyfill": "^6.26.0", | ||
"eslint": "^4.19.1" | ||
"@types/mocha": "^9.1.0", | ||
"@types/node": "^17.0.19", | ||
"@typescript-eslint/eslint-plugin": "^5.12.1", | ||
"@typescript-eslint/parser": "^5.12.1", | ||
"eslint": "^8.9.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"mocha": "^9.2.1", | ||
"nyc": "^15.1.0", | ||
"rollup": "2.68.0", | ||
"rollup-plugin-dts": "^4.1.0", | ||
"rollup-plugin-typescript2": "^0.31.2", | ||
"ts-node": "^10.5.0", | ||
"typescript": "^4.5.5" | ||
}, | ||
"name": "pvutils", | ||
"version": "1.0.17", | ||
"version": "1.1.0", | ||
"license": "MIT" | ||
} |
# pvutils | ||
[![CircleCI](https://circleci.com/gh/PeculiarVentures/pvutils.svg?style=svg)](https://circleci.com/gh/PeculiarVentures/pvutils) [![Coverage Status](https://coveralls.io/repos/github/PeculiarVentures/pvutils/badge.svg?branch=master)](https://coveralls.io/github/PeculiarVentures/pvutils?branch=master) | ||
[![test](https://github.com/PeculiarVentures/pvutils/actions/workflows/test.yml/badge.svg)](https://github.com/PeculiarVentures/pvutils/actions/workflows/test.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/PeculiarVentures/pvutils/badge.svg?branch=master)](https://coveralls.io/github/PeculiarVentures/pvutils?branch=master) | ||
`pvutils` is a set of common utility functions used in various Peculiar Ventures Javascript based projects. | ||
`pvutils` is a set of common utility functions used in various Peculiar Ventures Javascript based projects. | ||
@@ -7,0 +8,0 @@ Some example capabilities included in `pvutils` include: |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15
31994
13
806
2