Skip to the content

Capsid menu

app/lib/bytes.mjs

42 lines and 6 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.

Functions

The functions of bytes.mjs
FunctionLinesComplexityCoveredCRAPMutation
toHex8 to 101100%OkCRAP 1No data: no mutants in this code
<anonymous:9>9 to 91100%OkCRAP 1No data: no mutants in this code
fnv1a3218 to 252100%OkCRAP 2No data: no mutants in this code
base64ToBytes34 to 36150%OkCRAP 1.1No data: no mutants in this code
<anonymous:35>35 to 3510%OkCRAP 2No data: no mutants in this code
bytesToBase6442 to 462100%OkCRAP 2No data: no mutants in this code

Imports

It imports no other module of this repo. Imported by app/lib/bearer.server.ts, app/lib/content/hashes.mjs, app/lib/content/og-card-text.mjs, app/lib/cv/pdf-html.mjs, app/lib/editor/github.server.ts, app/lib/media/classify.mjs, app/lib/media/core.server.ts, app/lib/operator/upload-media.server.ts, app/lib/preview-token.mjs, app/lib/search/ask-guard.server.ts.

Source

// Byte and hash encodings shared by the Worker, the build scripts and node tests. No imports and no
// Buffer: Buffer does not exist in the Worker runtime unless nodejs_compat is on.

/**
 * @param {ArrayBuffer | Uint8Array} bytes
 * @returns {string} lowercase hex, two digits a byte
 */
export function toHex(bytes) {
  return [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, "0")).join("");
}

/**
 * FNV-1a, 32 bits, over UTF-16 code units: a short stable fingerprint, not a security boundary.
 *
 * @param {string} input
 * @returns {string} eight lowercase hex digits
 */
export function fnv1a32(input) {
  let hash = 0x811c9dc5;
  for (let i = 0; i < input.length; i += 1) {
    hash ^= input.charCodeAt(i);
    hash = Math.imul(hash, 0x01000193) >>> 0;
  }
  return hash.toString(16).padStart(8, "0");
}

/**
 * Whitespace is dropped first: GitHub wraps base64 content in lines, chats and YAML add more, and
 * `atob` throws on it. Anything else that is not base64 throws.
 *
 * @param {string} value
 * @returns {Uint8Array}
 */
export function base64ToBytes(value) {
  return Uint8Array.from(atob(value.replace(/\s+/g, "")), (c) => c.charCodeAt(0));
}

/**
 * @param {Uint8Array} bytes
 * @returns {string} standard base64, padded
 */
export function bytesToBase64(bytes) {
  let binary = "";
  for (const byte of bytes) binary += String.fromCharCode(byte);
  return btoa(binary);
}