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

@buttercup/generator

v2.0.0

Published

NodeJS password generator

Downloads

124

Readme

Buttercup Password Generator

Build Status npm version

A password generator that's used in Buttercup.

Usage

You can generate a generic password by simply running the following:

const { generatePassword, getConfig } = require("@buttercup/generator");

const config = getConfig(); // Generic config
generatePassword(config).then(password => {
    // password is a string
});

You can change what types of passwords are generated by manipulating the config object.

Generation errors

Errors may occur for a variety of reasons, primarily due to user options being too restrictive (or just by random chance). Each predictable error should have a code (thrownError.code) associated with it:

  • BAD_MODE: An invalid generation mode was selected.
  • NO_CHARSETS: No character sets were enabled - so no password could be generated.
  • MAX_RETRIES: An attempt at generating a password was made, but due to the selected character sets, restrictive repeat rules (config.randomCharacters.allowRepeatingCharacters and config.randomCharacters.allowRepeatingSets) and random chance, no password could be generated within the allowed number of retries.

Configuration

After getting a configuration object by running getConfig(), you can set config.mode to one of the two following behaviours:

  • characters: Generate a password made up of random characters. Character sets are used to customise the output. This is the default mode.
  • words: Generate a password made up of random words.

Random character configuration

You can set the length of the password by changing config.randomCharacters.length. You can change the repetition behaviour of the password by changing the two following values:

  • config.randomCharacters.allowRepeatingCharacters: Whether or not to allow repeating characters in the generated password. The default is false.
  • config.randomCharacters.allowRepeatingSets: Whether or not to allow repeating character sets. The default is true.

You can set what character sets are used to generate the random-character passwords. The available character sets are available on the object config.randomCharacters.characterSets. The enabled character sets are set on config.randomCharacters.enabledCharacterSets, which is an array of the names of enabled character sets.

For example, the following allows for generation of passwords that are 20 characters long with only letters and numbers:

const config = getConfig();
config.randomCharacters.length = 20;
config.randomCharacters.enabledCharacterSets = ["UPPERCASE", "LOWERCASE", "DIGITS"];
generatePassword(config).then(password => {
    // do something with the password
});

Similarly, you could enable the use of all character sets by using:

config.randomCharacters.enabledCharacterSets = Object.keys(config.randomCharacters.characterSets);

Important note: Limiting the character sets, as well as enforcing strict non-repeat rules, may cause the password generator to throw. You should gracefully handle thrown errors in any interface this component is used.

Random word configuration

After setting config.mode to "words", you can use the config.randomWords object to configure the generation. Set the number of generated words by using config.randomWords.length to the desired count. The words are joined by the string config.randomWords.separator.

Setup for platforms other than NodeJS

An asynchronous random number generator is used so that other platforms may easily be supported. Random number generation doesn't work well in the browser (in a broad sense), and doesn't really work at all in React-Native (crypto PRNG).

You should override the random number generation on platforms where better solutions are available (when compared to this library's built in generator). The override function should take an array of objects that resemble { min, max }, where min and max are bounds for random number generation. For each item in the array, a random number should be generated between the provided range (bounds inclusive). For example:

const { setRNG } = require("@buttercup/generator");

// Overriden method must return a Promise with an array of the
// generated numbers
setRNG(items => {
    return Promise.all(items.map(item => {
        return someRandomNumberGenerator(item.min, item.max);
    }));
});

Installation

Install the library by running:

npm install @buttercup/generator --save