@ndn/util
Version:
NDNts: general utilities
26 lines (25 loc) • 800 B
JavaScript
import assert from "tiny-invariant";
export function concatBuffers(list, totalLength) {
totalLength ??= list.reduce((l, { byteLength }) => l + byteLength, 0);
const c = new Uint8Array(totalLength);
let offset = 0;
for (const part of list) {
c.set(part, offset);
offset += part.byteLength;
}
assert(offset === totalLength);
return c;
}
export const console = globalThis.console;
export function delay(after, value) {
return new Promise((resolve) => setTimeout(resolve, after, value));
}
export function timingSafeEqual(a, b) {
// length has been checked by caller
// https://codahale.com/a-lesson-in-timing-attacks/
let result = 0;
for (let i = 0; i < a.byteLength; ++i) {
result |= a[i] ^ b[i];
}
return result === 0;
}