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

@bytehide/secrets

v1.0.7

Published

A manager for secrets

Downloads

514

Readme

ByteHide Secrets

ByteHide Secrets is a lightweight library to securely manage environment secrets and tokens for your applications.

Features

  • Securely initialize the secrets management system.
  • Retrieve and set secrets with ease.
  • Compatible with multiple environments.

Installation

Install the ByteHide Secrets library using npm or yarn:

npm install @bytehide/secrets
# or
yarn add @bytehide/secrets

Usage

Initialization

Before using the library, initialize it with your project token and environment:

import { Secrets } from '@bytehide/secrets';

Secrets.unsecureInitialize('<your-project-token>', '<environment>');

Retrieving a Secret

Use the get method to retrieve a secret. The method returns a promise:

async function fetchSecret() {
    const secretKey = 'my-secret-key';
    const secret = await Secrets.get(secretKey);
    if (secret) {
        console.log(`Retrieved secret: ${secret}`);
    } else {
        console.error('Failed to retrieve the secret.');
    }
}

fetchSecret();

Setting a Secret

You can set a new secret using the set method:

async function storeSecret() {
    const secretKey = 'my-secret-key';
    const secretValue = 'my-secret-value';
    const result = await Secrets.set({[secretKey]: secretValue});
    if (result) {
        console.log('Secret successfully stored.');
    } else {
        console.error('Failed to store the secret.');
    }
}

storeSecret();

Example: Simulating a Database Connection

Here's an example of using get to simulate a database connection:

export async function setupDB() {
    Secrets.unsecureInitialize('<your-project-token>', 'production');

    let dbIsReady = false;
    let secret = '';

    while (!dbIsReady) {
        secret = await Secrets.get('db-connection-string');
        if (secret) {
            dbIsReady = true;
        }
    }

    const setDB = (db) => {
        console.log(`Database "${db}" is initialized with secret: ${secret}`);
    };

    setDB('myDatabase');
}

Setting Up Environment Variables

Node.js Projects

  1. Create a .env file in the root directory of your project.
  2. Add your environment variables in the following format:
    BYTEHIDE_SECRETS_ENVIRONMENT=production
    BYTEHIDE_SECRETS_TOKEN=your-project-token
  3. Ensure you have dotenv installed to load these variables automatically:
    npm install dotenv
  4. Your application will now have access to these variables through process.env.

Browser Projects (e.g., Vite)

For browser-based projects, you'll need to inject the environment variables during the build process.

Webpack Example

Add the following configuration to your Webpack setup:

new webpack.DefinePlugin({
    'window.env': JSON.stringify({
        BYTEHIDE_SECRETS_ENVIRONMENT: 'production',
        BYTEHIDE_SECRETS_TOKEN: 'your-project-token',
    }),
});

Vite Example

Modify your vite.config.js to define the environment variables:

export default defineConfig({
    define: {
        'window.env': {
            BYTEHIDE_SECRETS_ENVIRONMENT: 'production',
            BYTEHIDE_SECRETS_TOKEN: 'your-project-token',
        },
    },
});

API Reference

Secrets.unsecureInitialize(token: string, environment: string)

Initializes the secrets management system.

  • token: Your project token.
  • environment: The environment (e.g., development, production).

Secrets.get(key: string): Promise

Retrieves the value of a secret.

  • key: The key of the secret to retrieve.

Returns a promise resolving to the secret value.

Secrets.setSecrets(payload: Record<string, string>): Promise

Sets or updates a secret.

  • payload: An object containing key-value pairs of secrets.

Returns a promise resolving to true if successful.

Contributing

Contributions are welcome! If you have any feature requests or find issues, feel free to open an issue or submit a pull request.

License

MIT License


Happy coding but keep it safe with @bytehide/secrets! 🛡️