Secure Value Wrapper (utils/secure-value.ts)

the-engineer · src/utils/secure-value.ts · typescript · MIT
security, tag:utility

An opaque wrapper for sensitive data that prevents accidental leakage through logging, serialization, or string coercion. Use this to handle API keys and credentials safely.

// Source: https://github.com/FarzamMohammadi/the-engineer | License: MIT | Copyright 2026 Farzam Mohammadi
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Opaque wrapper for sensitive values (tokens, keys, credentials).
 *
 * Prevents accidental leakage through toString, JSON serialization, console
 * logging, and Node.js inspect. The raw value is accessible only via the
 * explicit `unwrap()` call — a conscious decision at the exact point of use.
 *
 * Modeled after Rust's `secrecy::Secret<T>`.
 */
export class SecureValue {
  #value: string;

  constructor(value: string) {
    this.#value = value;
  }

  /** Explicitly retrieve the raw secret. Use only at the point of consumption. */
  unwrap(): string {
    return this.#value;
  }

  /** Always returns "[REDACTE

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key