Skip to the content

Capsid menu

src/encoding.ts

27 lines and 9 functions in DrDustinEdwards/capsid, at commit daa875b on 2026-10-24. This file on GitHub.

Functions

The functions of encoding.ts
FunctionLinesComplexityCoveredCRAPMutation
bytesToHex3 to 62100%OkCRAP 2No data: mutation testing has not run on this repo yet
<anonymous:5>5 to 51100%OkCRAP 1No data: mutation testing has not run on this repo yet
binaryFromBytes8 to 122100%OkCRAP 2No data: mutation testing has not run on this repo yet
b64urlFromBytes14 to 161100%OkCRAP 1No data: mutation testing has not run on this repo yet
b64urlEncode18 to 201100%OkCRAP 1No data: mutation testing has not run on this repo yet
b64urlDecode22 to 241100%OkCRAP 1No data: mutation testing has not run on this repo yet
base64Encode27 to 291100%OkCRAP 1No data: mutation testing has not run on this repo yet
base64Decode32 to 341100%OkCRAP 1No data: mutation testing has not run on this repo yet
<anonymous:33>33 to 331100%OkCRAP 1No data: mutation testing has not run on this repo yet

Imports

It imports no other module of this repo. Imported by src/access-login.ts, src/agents-schema.ts, src/auth.ts, src/bench-run-key.ts, src/bench.ts, src/controls-token.ts, src/github/client.ts, src/github/contents.ts, src/github/search.ts, src/jobs-schema.ts, src/maintenance-releases.ts, src/namespace-delete-token.ts, src/operator-key.ts, src/portal-auth.ts, src/resources.ts, src/routes.ts, src/shared-code.ts, src/site-key.ts, src/skills-refresh.ts.

Source

import { base64url } from "jose";

export function bytesToHex(bytes: ArrayBuffer | Uint8Array): string {
  const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
  return [...view].map((b) => b.toString(16).padStart(2, "0")).join("");
}

function binaryFromBytes(bytes: Uint8Array): string {
  let binary = "";
  for (const b of bytes) binary += String.fromCharCode(b);
  return binary;
}

export function b64urlFromBytes(bytes: Uint8Array): string {
  return base64url.encode(bytes);
}

export function b64urlEncode(text: string): string {
  return b64urlFromBytes(new TextEncoder().encode(text));
}

export function b64urlDecode(encoded: string): string {
  return new TextDecoder().decode(base64url.decode(encoded));
}

// Plain base64, not url-safe: GitHub's contents API encoding.
export function base64Encode(text: string): string {
  return btoa(binaryFromBytes(new TextEncoder().encode(text)));
}

// GitHub wraps base64 across lines; atob rejects the whitespace.
export function base64Decode(b64: string): string {
  return new TextDecoder().decode(Uint8Array.from(atob(b64.replace(/\s+/g, "")), (c) => c.charCodeAt(0)));
}