Fix some of the missing polyfills

This commit is contained in:
Alexis Sellier
2021-08-03 17:11:07 +02:00
parent 400a2df0a4
commit 0cbfbd9f47
3 changed files with 66 additions and 31 deletions
+59 -26
View File
@@ -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;
}
+6 -5
View File
@@ -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);
}
};
}
+1
View File
@@ -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'),
},
},