Skip to content

Commit 6e19619

Browse files
committedApr 5, 2021
Require Node.js 12 and move to ESM
1 parent d78436d commit 6e19619

File tree

8 files changed

+48
-35
lines changed

8 files changed

+48
-35
lines changed
 

‎.github/workflows/main.yml

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
16-
- 8
1715
steps:
1816
- uses: actions/checkout@v2
1917
- uses: actions/setup-node@v1

‎index.d.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ interface TypeOption {
4040
4141
cryptoRandomString({length: 10, type: 'ascii-printable'});
4242
//=> '`#Rt8$IK>B'
43-
43+
4444
cryptoRandomString({length: 10, type: 'alphanumeric'});
4545
//=> 'DMuKL8YtE7'
4646
```
@@ -66,9 +66,7 @@ interface CharactersOption {
6666
characters?: string;
6767
}
6868

69-
declare namespace cryptoRandomString {
70-
type Options = BaseOptions & MergeExclusive<TypeOption, CharactersOption>;
71-
}
69+
export type Options = BaseOptions & MergeExclusive<TypeOption, CharactersOption>;
7270

7371
declare const cryptoRandomString: {
7472
/**
@@ -78,28 +76,34 @@ declare const cryptoRandomString: {
7876
7977
@example
8078
```
81-
import cryptoRandomString = require('crypto-random-string');
79+
import cryptoRandomString from 'crypto-random-string';
8280
8381
cryptoRandomString({length: 10});
8482
//=> '2cf05d94db'
8583
```
8684
*/
87-
(options: cryptoRandomString.Options): string;
85+
(options: Options): string;
8886

8987
/**
9088
Asynchronously generate a [cryptographically strong](https://en.wikipedia.org/wiki/Strong_cryptography) random string.
9189
90+
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
91+
92+
> The `crypto.randomBytes()` method will not complete until there is sufficient entropy available. This should normally never take longer than a few milliseconds. The only time when generating the random bytes may conceivably block for a longer period of time is right after boot, when the whole system is still low on entropy.
93+
94+
In general, anything async comes with some overhead on it's own.
95+
9296
@returns A promise which resolves to a randomized string.
9397
9498
@example
9599
```
96-
import cryptoRandomString = require('crypto-random-string');
100+
import cryptoRandomString from 'crypto-random-string';
97101
98102
await cryptoRandomString.async({length: 10});
99103
//=> '2cf05d94db'
100104
```
101105
*/
102-
async(options: cryptoRandomString.Options): Promise<string>;
103-
}
106+
async(options: Options): Promise<string>;
107+
};
104108

105-
export = cryptoRandomString;
109+
export default cryptoRandomString;

‎index.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict';
2-
const {promisify} = require('util');
3-
const crypto = require('crypto');
1+
import {promisify} from 'util';
2+
import crypto from 'crypto';
43

54
const randomBytesAsync = promisify(crypto.randomBytes);
65

@@ -71,7 +70,7 @@ const generateRandomBytesAsync = async (byteLength, type, length) => {
7170
return buffer.toString(type).slice(0, length);
7271
};
7372

74-
const allowedTypes = [
73+
const allowedTypes = new Set([
7574
undefined,
7675
'hex',
7776
'base64',
@@ -80,7 +79,7 @@ const allowedTypes = [
8079
'distinguishable',
8180
'ascii-printable',
8281
'alphanumeric'
83-
];
82+
]);
8483

8584
const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({length, type, characters}) => {
8685
if (!(length >= 0 && Number.isFinite(length))) {
@@ -95,7 +94,7 @@ const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({
9594
throw new TypeError('Expected `characters` to be string');
9695
}
9796

98-
if (!allowedTypes.includes(type)) {
97+
if (!allowedTypes.has(type)) {
9998
throw new TypeError(`Unknown type: ${type}`);
10099
}
101100

@@ -142,5 +141,8 @@ const createGenerator = (generateForCustomCharacters, generateRandomBytes) => ({
142141
return generateForCustomCharacters(length, characters.split(''));
143142
};
144143

145-
module.exports = createGenerator(generateForCustomCharacters, generateRandomBytes);
146-
module.exports.async = createGenerator(generateForCustomCharactersAsync, generateRandomBytesAsync);
144+
const cryptoRandomString = createGenerator(generateForCustomCharacters, generateRandomBytes);
145+
146+
cryptoRandomString.async = createGenerator(generateForCustomCharactersAsync, generateRandomBytesAsync);
147+
148+
export default cryptoRandomString;

‎index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType, expectError} from 'tsd';
2-
import cryptoRandomString = require('.');
2+
import cryptoRandomString from './index.js';
33

44
expectType<string>(cryptoRandomString({length: 10}));
55
expectType<string>(cryptoRandomString({length: 10, type: 'url-safe'}));

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

‎package.json

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Generate a cryptographically strong random string",
55
"license": "MIT",
66
"repository": "sindresorhus/crypto-random-string",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=12"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -36,11 +39,11 @@
3639
"protect"
3740
],
3841
"dependencies": {
39-
"type-fest": "^0.8.1"
42+
"type-fest": "^1.0.1"
4043
},
4144
"devDependencies": {
42-
"ava": "^2.1.0",
43-
"tsd": "^0.11.0",
44-
"xo": "^0.25.3"
45+
"ava": "^3.15.0",
46+
"tsd": "^0.14.0",
47+
"xo": "^0.38.2"
4548
}
4649
}

‎readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $ npm install crypto-random-string
1313
## Usage
1414

1515
```js
16-
const cryptoRandomString = require('crypto-random-string');
16+
import cryptoRandomString from 'crypto-random-string';
1717

1818
cryptoRandomString({length: 10});
1919
//=> '2cf05d94db'
@@ -50,6 +50,12 @@ Returns a randomized string. [Hex](https://en.wikipedia.org/wiki/Hexadecimal) by
5050

5151
Returns a promise which resolves to a randomized string. [Hex](https://en.wikipedia.org/wiki/Hexadecimal) by default.
5252

53+
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
54+
55+
> The `crypto.randomBytes()` method will not complete until there is sufficient entropy available. This should normally never take longer than a few milliseconds. The only time when generating the random bytes may conceivably block for a longer period of time is right after boot, when the whole system is still low on entropy.
56+
57+
In general, anything async comes with some overhead on it's own.
58+
5359
#### options
5460

5561
Type: `object`

‎test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
2-
import cryptoRandomString from '.';
2+
import cryptoRandomString from './index.js';
33

4-
// Probabilistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656
4+
// Probabilistic, result is always less than or equal to actual set size, chance it is less is below 1e-256 for sizes up to 32656.
55
const generatedCharacterSetSize = (options, targetSize) => {
66
const set = new Set();
77
const length = targetSize * 640;
@@ -49,15 +49,15 @@ test('url-safe', t => {
4949
t.is(cryptoRandomString({length: 0, type: 'url-safe'}).length, 0);
5050
t.is(cryptoRandomString({length: 10, type: 'url-safe'}).length, 10);
5151
t.is(cryptoRandomString({length: 100, type: 'url-safe'}).length, 100);
52-
t.regex(cryptoRandomString({length: 100, type: 'url-safe'}), /^[a-zA-Z\d._~-]*$/); // Sanity check, probabilistic
52+
t.regex(cryptoRandomString({length: 100, type: 'url-safe'}), /^[\w.~-]*$/); // Sanity check, probabilistic
5353
t.is(generatedCharacterSetSize({type: 'url-safe'}, 66), 66);
5454
});
5555

5656
test('numeric', t => {
5757
t.is(cryptoRandomString({length: 0, type: 'numeric'}).length, 0);
5858
t.is(cryptoRandomString({length: 10, type: 'numeric'}).length, 10);
5959
t.is(cryptoRandomString({length: 100, type: 'numeric'}).length, 100);
60-
t.regex(cryptoRandomString({length: 100, type: 'numeric'}), /^[\d]*$/); // Sanity check, probabilistic
60+
t.regex(cryptoRandomString({length: 100, type: 'numeric'}), /^\d*$/); // Sanity check, probabilistic
6161
t.is(generatedCharacterSetSize({type: 'numeric'}, 10), 10);
6262
});
6363

@@ -73,7 +73,7 @@ test('ascii-printable', t => {
7373
t.is(cryptoRandomString({length: 0, type: 'ascii-printable'}).length, 0);
7474
t.is(cryptoRandomString({length: 10, type: 'ascii-printable'}).length, 10);
7575
t.is(cryptoRandomString({length: 100, type: 'ascii-printable'}).length, 100);
76-
t.regex(cryptoRandomString({length: 100, type: 'ascii-printable'}), /^[!"#$%&'()*+,-./\d:;<=>?@A-Z[\\\]^_`a-z{|}~]*$/); // Sanity check, probabilistic
76+
t.regex(cryptoRandomString({length: 100, type: 'ascii-printable'}), /^[!"#$%&'()*+,-./\w:;<=>?@[\\\]^`{|}~]*$/); // Sanity check, probabilistic
7777
});
7878

7979
test('alphanumeric', t => {
@@ -95,7 +95,7 @@ test('characters', t => {
9595

9696
test('argument errors', t => {
9797
t.throws(() => {
98-
cryptoRandomString({length: Infinity});
98+
cryptoRandomString({length: Number.POSITIVE_INFINITY});
9999
});
100100

101101
t.throws(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.