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

yael-autoencrypt

v1.1.0

Published

Encrypt and write a file during development, then read and decrypt it during production.

Downloads

132

Readme

yael-autoencrypt

Encrypt and write a file during development, then read and decrypt it during production.

Installation

npm install yael-autoencrypt

Use Case

Say you have a config file called 'secrets.txt' with all the secret API keys for all your server application. You need to distribute the secrets to your server via source control, possibly because you have a git 'push' based deploy mechanism, or you just have so many secrets that using separate environment variables for each API key is too much work. However, you don't and want those secrets stored in the clear, either in source control or on your server, because your server or source code repo could get hacked. Rather than add another step into your build chain, this library helps you encrypt your secrets in a file AND keep that file up-to-date.

Example

This example code lets you keep a master plaintext_file on your development box. (Be sure to add it to .gitignore so you don't ever upload it by mistake!) When your application runs, it will automatically save an encrypted version of the file as encrypted_file, which you can add to source control. On the production server or on other developer's boxes where plaintext_file is absent, it will notice that and try to use encrypted_file instead. From the code's perspective the library usage is the exact same, so the code is identical in both cases.

autoencrypt = require('yael-autoencrypt');
config = autoencrypt({
  password: process.env.CONFIG_PASSWORD,
  plaintext_file: 'secrets.txt',
  encrypted_file: 'encrypted.txt'
})
console.log config

Whenever you run your application on a computer where plaintext_file is present, it will check that the contents match the decrypted contents of encrypted_file. If the contents differ, it will overwrite encrypted_file with the updated content.

Caching and using multiple files

For convenience, the module caches the password, plaintext_file, and encrypted_file values in memory. This means that once you have called autoencrypt with the options, elsewhere in your code you can leave out the options:

autoencrypt = require('yael-autoencrypt')
config = autoencrypt()

This makes sense if you have one central encrypted config file. However this causes a problem if you need to work with multiple encrypted config files. To get around this, you can add a cache: false option:

autoencrypt = require('yael-autoencrypt');
config = autoencrypt({
  cache: false,
  password: process.env.CONFIG_PASSWORD2,
  plaintext_file: 'other-secrets.txt',
  encrypted_file: 'other-encrypted.txt'
})