From 71b5ce7de2c1d7fb3796275b01eb929809bd10d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Tue, 11 Oct 2022 16:47:42 +0200 Subject: [PATCH] Remove unused polyfills MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rūdolfs Ošiņš --- src/polyfills/enc-utils.js | 101 -------------------------- src/polyfills/typedarray-to-buffer.js | 18 ----- vite.config.ts | 6 -- 3 files changed, 125 deletions(-) delete mode 100644 src/polyfills/enc-utils.js delete mode 100644 src/polyfills/typedarray-to-buffer.js diff --git a/src/polyfills/enc-utils.js b/src/polyfills/enc-utils.js deleted file mode 100644 index 1b559af63..000000000 --- a/src/polyfills/enc-utils.js +++ /dev/null @@ -1,101 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -/* eslint-disable @typescript-eslint/no-var-requires */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ - -"use strict"; - -import { Buffer } from "buffer/"; -import typedarrayToBuffer from "typedarray-to-buffer"; - -const ENC_UTF8 = "utf8"; -const ENC_HEX = "hex"; -const STRING_ZERO = "0"; - -export function bufferToArray(buf) { - return new Uint8Array(buf); -} - -export function bufferToUtf8(buf) { - return buf.toString(ENC_UTF8); -} - -export function bufferToHex(buf, prefixed = false) { - const hex = buf.toString(ENC_HEX); - return prefixed ? addHexPrefix(hex) : hex; -} - -export function utf8ToBuffer(utf8) { - return Buffer.from(utf8, ENC_UTF8); -} - -export function utf8ToArray(utf8) { - return bufferToArray(utf8ToBuffer(utf8)); -} - -export function hexToArray(hex) { - return bufferToArray(hexToBuffer(hex)); -} - -export function hexToBuffer(hex) { - return Buffer.from(removeHexPrefix(hex), ENC_HEX); -} - -export function removeHexPrefix(hex) { - return hex.replace(/^0x/, ""); -} - -export function addHexPrefix(hex) { - return hex.startsWith("0x") ? hex : `0x${hex}`; -} - -export function arrayToBuffer(arr) { - return typedarrayToBuffer(arr); -} - -export function arrayToHex(arr, prefixed = false) { - return bufferToHex(arrayToBuffer(arr), prefixed); -} - -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]); -} - -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 deleted file mode 100644 index 0fd867140..000000000 --- a/src/polyfills/typedarray-to-buffer.js +++ /dev/null @@ -1,18 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -/* eslint-disable @typescript-eslint/no-var-requires */ -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ - -import isTypedArray from "is-typedarray"; -import { Buffer } from "buffer/"; - -export default function typedarrayToBuffer(arr) { - if (isTypedArray.strict(arr)) { - let buf = Buffer.from(arr.buffer); - if (arr.byteLength !== arr.buffer.byteLength) { - buf = buf.slice(arr.byteOffset, arr.byteOffset + arr.byteLength); - } - return buf; - } else { - return Buffer.from(arr); - } -} diff --git a/vite.config.ts b/vite.config.ts index a3dc5b593..7783f6408 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -36,12 +36,6 @@ const config: UserConfig = { alias: { "@public": path.resolve("./public"), "@app": path.resolve("./src"), - "typedarray-to-buffer": path.resolve( - "./src/polyfills/typedarray-to-buffer.js", - ), - // "Buffer" is not defined in the published package.. - "@walletconnect/encoding": path.resolve("./src/polyfills/enc-utils.js"), - "enc-utils": path.resolve("./src/polyfills/enc-utils.js"), }, }, define: {