npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@latehours/secret

v1.0.0

Published

`secret` is a simple utility libraty for handling secrets in a typescript app

Downloads

227

Readme

secret 🤫

GitHub Actions Workflow Status Dependencies amount 0 NPM Downloads NPM Version GitHub License Made with vacuum cleaner package size

secret is a simple utility library for handling sensitive data in any typescript app. no more leaking tokens or emails into logs by accident.

is has zero dependencies and has fairly exhaustive test coverage.

motivation

secret makes revealing the secret value explicit and intentional. it prevents accidental leaking of secrets into logs, stdout, JSON.stringify calls, writes to files and so on by the developer.

keep in mind that the secret is still stored in memory unencrypted and can be read by a debugger or by inspecting the memory of the process. this is not a security library. continue to use encryption for sensitive data.

behind the scenes the secret is stored in javascript object as a bytes array, not plaintext. this is more security by obscurity than anything else.

usage

install the library using your package manager of choice:

npm install @latehours/secret
pnpm install @latehours/secret
bun add @latehours/secret
yarn add @latehours/secret

usage in your code:

import * as Secret from "@latehours/secret";

// conceal a string into a secret
const hidden = Secret.fromString("tussihovi");

console.log(hidden); // logs [REDACTED]

// convert the secret back to a string
const exposed = Secret.expose(hidden);

considerations

when wrapping a value into a secret such as in io-ts or zod schema make sure to clean up any intermediate secrets such as the unparsed input.

sending a secret over the network is not possible as the secret is not serializable by design. use encryption for that or better yet, don't handle the secret at all.

usage as a Secret class

sometimes it can be more convenient to wrap the secret into a class and pass it around in your app. this way one doesn't need to litter codebase with @latehours/secret imports.

example code:

import { fromString, expose }  from "@latehours/secret"
import type { Secret as SecretInternal } from "@latehours/secret"

class Secret {
  secret: SecretInternal

  constructor(value: string) {
    this.secret = fromString(value)
  }

  expose() {
    return expose(this.secret)
  }

  toString() {
    return this.secret.toString()
  }
}

export function createSecret(value: string) {
  return new Secret(value)
}

io-ts codec

provided for convenience. check out tests for exported io-ts decoder and encoder for usage.

you need to have fp-ts and io-ts installed in your project.

import { SecretCodec } from "@latehours/secret/io-ts";
import { pipe } from "fp-ts/function";
import { fold } from "fp-ts/Either";

pipe(
  "tussihovi",
  SecretCodec.decode,
  fold(
    () => {
      expect.unreachable("decoding should not fail");
    },
    (secret) => {
      const exposed = SecretCodec.encode(secret);
      expect(exposed).toEqual("tussihovi");
    }
  )
);

zod schema

provided for convenience.

you need to have zod installed in your project.

import * as Secret from "@latehours/secret";
import { Secret as SecretSchema } from "@latehours/secret/zod";

const secret = SecretSchema.safeParse(input);

if (secret.success) {
  expect(Secret.isSecret(secret.data)).toBe(true);

  const exposed = Secret.expose(secret.data);
  expect(exposed).toEqual(input);
}

acknowledgements

the idea for this library came from the rust cargo secrecy.

the implementation is based on the following libraries:

this improves on the above libraries by hiding the raw value of the secret (bytes array) from leaking when calling console.log or utils.inspect on the secret object. also the raw value is not retrievable by object access.

development

To install dev dependencies:

bun install

To run tests:

bun --watch test

when creating commits follow the conventional commits format.