From 0cbfbd9f47d4c579fc30844bc74ced4b84988a96 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Tue, 3 Aug 2021 17:09:29 +0200 Subject: [PATCH] Fix some of the missing polyfills --- src/polyfills/enc-utils.js | 85 +++++++++++++++++++-------- src/polyfills/typedarray-to-buffer.js | 11 ++-- vite.config.ts | 1 + 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/polyfills/enc-utils.js b/src/polyfills/enc-utils.js index 5dc07f517..6f2ebcffb 100644 --- a/src/polyfills/enc-utils.js +++ b/src/polyfills/enc-utils.js @@ -1,73 +1,106 @@ /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ "use strict"; -var Buffer = require("buffer/").Buffer; -var typedarrayToBuffer = require("typedarray-to-buffer"); +import { Buffer } from 'buffer/'; +import typedarrayToBuffer from 'typedarray-to-buffer'; const ENC_UTF8 = 'utf8'; const ENC_HEX = 'hex'; +const STRING_ZERO = '0'; -function bufferToArray(buf) { +export function bufferToArray(buf) { return new Uint8Array(buf); } -exports.bufferToArray = bufferToArray; -function bufferToUtf8(buf) { +export function bufferToUtf8(buf) { return buf.toString(ENC_UTF8); } -exports.bufferToUtf8 = bufferToUtf8; -function bufferToHex(buf, prefixed = false) { +export function bufferToHex(buf, prefixed = false) { const hex = buf.toString(ENC_HEX); return prefixed ? addHexPrefix(hex) : hex; } -exports.bufferToHex = bufferToHex; -function utf8ToBuffer(utf8) { +export function utf8ToBuffer(utf8) { return Buffer.from(utf8, ENC_UTF8); } -exports.utf8ToBuffer = utf8ToBuffer; -function utf8ToArray(utf8) { +export function utf8ToArray(utf8) { return bufferToArray(utf8ToBuffer(utf8)); } -exports.utf8ToArray = utf8ToArray; -function hexToArray(hex) { +export function hexToArray(hex) { return bufferToArray(hexToBuffer(hex)); } -exports.hexToArray = hexToArray; -function hexToBuffer(hex) { +export function hexToBuffer(hex) { return Buffer.from(removeHexPrefix(hex), ENC_HEX); } -exports.hexToBuffer = hexToBuffer; -function removeHexPrefix(hex) { +export function removeHexPrefix(hex) { return hex.replace(/^0x/, ''); } -exports.removeHexPrefix = removeHexPrefix; -function addHexPrefix(hex) { +export function addHexPrefix(hex) { return hex.startsWith('0x') ? hex : `0x${hex}`; } -exports.addHexPrefix = addHexPrefix; -function arrayToBuffer(arr) { +export function arrayToBuffer(arr) { return typedarrayToBuffer(arr); } -exports.arrayToBuffer = arrayToBuffer; -function arrayToHex(arr, prefixed = false) { +export function arrayToHex(arr, prefixed = false) { return bufferToHex(arrayToBuffer(arr), prefixed); } -exports.arrayToHex = arrayToHex; -function concatArrays(...args) { +export function arrayToUtf8(arr) { + return bufferToUtf8(arrayToBuffer(arr)); +} + +export function sanitizeHex(hex) { + hex = removeHexPrefix(hex); + hex = sanitizeBytes(hex, 2); + if (hex) { + hex = addHexPrefix(hex); + } + return hex; +} + +export function sanitizeBytes(str, byteSize = 8, padding = STRING_ZERO) { + return padLeft(str, calcByteLength(str.length, byteSize), padding); +} + +export function padLeft(str, length, padding = STRING_ZERO) { + return padString(str, length, true, padding); +} + +export function calcByteLength(length, byteSize = 8) { + const remainder = length % byteSize; + return remainder + ? ((length - remainder) / byteSize) * byteSize + byteSize + : length; +} + +export function concatArrays(...args) { let result = []; args.forEach(arg => (result = result.concat(Array.from(arg)))); return new Uint8Array([...result]); } -exports.concatArrays = concatArrays; + +export function padString( + str, + length, + left, + padding = STRING_ZERO +) { + const diff = length - str.length; + let result = str; + if (diff > 0) { + const pad = padding.repeat(diff); + result = left ? pad + str : str + pad; + } + return result; +} diff --git a/src/polyfills/typedarray-to-buffer.js b/src/polyfills/typedarray-to-buffer.js index 0f076d775..1ff9cdbf9 100644 --- a/src/polyfills/typedarray-to-buffer.js +++ b/src/polyfills/typedarray-to-buffer.js @@ -1,11 +1,12 @@ /* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -var isTypedArray = require('is-typedarray').strict; -var Buffer = require('buffer/').Buffer; +import isTypedArray from 'is-typedarray'; +import { Buffer } from 'buffer/'; -module.exports = function typedarrayToBuffer(arr) { - if (isTypedArray(arr)) { +export default function typedarrayToBuffer(arr) { + if (isTypedArray.strict(arr)) { var buf = Buffer.from(arr.buffer); if (arr.byteLength !== arr.buffer.byteLength) { buf = buf.slice(arr.byteOffset, arr.byteOffset + arr.byteLength); @@ -14,4 +15,4 @@ module.exports = function typedarrayToBuffer(arr) { } else { return Buffer.from(arr); } -}; +} diff --git a/vite.config.ts b/vite.config.ts index e6d8200e6..b5f0e6122 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,6 +14,7 @@ const config: UserConfig = { // Polyfill for Node.js 'stream' library. 'stream': path.resolve('./src/polyfills/stream.ts'), 'typedarray-to-buffer': path.resolve('./src/polyfills/typedarray-to-buffer.js'), + // "Buffer" is not defined in the published package.. 'enc-utils': path.resolve('./src/polyfills/enc-utils.js'), }, },