PHP WebShell
Текущая директория: /opt/BitGoJS/node_modules/@dfinity/agent/lib/esm/utils
Просмотр файла: buffer.js
/**
* Concatenate multiple array buffers.
* @param buffers The buffers to concatenate.
*/
export function concat(...buffers) {
const result = new Uint8Array(buffers.reduce((acc, curr) => acc + curr.byteLength, 0));
let index = 0;
for (const b of buffers) {
result.set(new Uint8Array(b), index);
index += b.byteLength;
}
return result.buffer;
}
/**
* Transforms a buffer to an hexadecimal string. This will use the buffer as an Uint8Array.
* @param buffer The buffer to return the hexadecimal string of.
*/
export function toHex(buffer) {
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('');
}
const hexRe = new RegExp(/^[0-9a-fA-F]+$/);
/**
* Transforms a hexadecimal string into an array buffer.
* @param hex The hexadecimal string to use.
*/
export function fromHex(hex) {
if (!hexRe.test(hex)) {
throw new Error('Invalid hexadecimal string.');
}
const buffer = [...hex]
.reduce((acc, curr, i) => {
acc[(i / 2) | 0] = (acc[(i / 2) | 0] || '') + curr;
return acc;
}, [])
.map(x => Number.parseInt(x, 16));
return new Uint8Array(buffer).buffer;
}
/**
*
* @param b1 array buffer 1
* @param b2 array buffer 2
* @returns number - negative if b1 < b2, positive if b1 > b2, 0 if b1 === b2
*/
export function compare(b1, b2) {
if (b1.byteLength !== b2.byteLength) {
return b1.byteLength - b2.byteLength;
}
const u1 = new Uint8Array(b1);
const u2 = new Uint8Array(b2);
for (let i = 0; i < u1.length; i++) {
if (u1[i] !== u2[i]) {
return u1[i] - u2[i];
}
}
return 0;
}
/**
* Checks two array buffers for equality.
* @param b1 array buffer 1
* @param b2 array buffer 2
* @returns boolean
*/
export function bufEquals(b1, b2) {
return compare(b1, b2) === 0;
}
/**
* Returns a true ArrayBuffer from a Uint8Array, as Uint8Array.buffer is unsafe.
* @param {Uint8Array} arr Uint8Array to convert
* @returns ArrayBuffer
*/
export function uint8ToBuf(arr) {
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength).buffer;
}
/**
* Returns a true ArrayBuffer from an ArrayBufferLike object.
* @param bufLike a buffer-like object
* @returns ArrayBuffer
*/
export function bufFromBufLike(bufLike) {
if (bufLike instanceof Uint8Array) {
return uint8ToBuf(bufLike);
}
if (bufLike instanceof ArrayBuffer) {
return bufLike;
}
if (Array.isArray(bufLike)) {
return uint8ToBuf(new Uint8Array(bufLike));
}
if ('buffer' in bufLike) {
return bufFromBufLike(bufLike.buffer);
}
return uint8ToBuf(new Uint8Array(bufLike));
}
//# sourceMappingURL=buffer.js.mapВыполнить команду
Для локальной разработки. Не используйте в интернете!