Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2021
1 parent 0a04c26 commit ae9b4c5
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 70 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
48 changes: 17 additions & 31 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
/// <reference types="node"/>
import {Readable as ReadableStream} from 'stream';
import {Readable as ReadableStream} from 'node:stream';

declare namespace randomBytesReadableStream {
interface Options {
/**
The total size to be produced by the stream in bytes.
@default Infinity
*/
readonly size?: number;
}
}

declare const randomBytesReadableStream: {
export interface Options {
/**
Creates a [readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) producing cryptographically strong pseudo-random data using [`crypto.randomBytes()`](https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback).
The total size to be produced by the stream in bytes.
@returns By default, infinite data.
@default Infinity
*/
readonly size?: number;
}

@example
```
import randomBytesReadableStream = require('random-bytes-readable-stream');
/**
Creates a [readable stream](https://nodejs.org/api/stream.html#stream_readable_streams) producing cryptographically strong pseudo-random data using [`crypto.randomBytes()`](https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback).
randomBytesReadableStream({size: 10}).pipe(process.stdout);
```
*/
(options?: randomBytesReadableStream.Options): ReadableStream;
@returns By default, infinite data.
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function randomBytesReadableStream(
// options?: randomBytesReadableStream.Options
// ): ReadableStream;
// export = randomBytesReadableStream;
default: typeof randomBytesReadableStream;
};
@example
```
import randomBytesReadableStream from 'random-bytes-readable-stream';
export = randomBytesReadableStream;
randomBytesReadableStream({size: 10}).pipe(process.stdout);
```
*/
export default function randomBytesReadableStream(options?: Options): ReadableStream;
19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';
const {Readable: ReadableStream} = require('stream');
const {randomBytes} = require('crypto');
import {Readable as ReadableStream} from 'node:stream';
import {randomBytes} from 'node:crypto';

const randomBytesReadableStream = (options = {}) => {
export default function randomBytesReadableStream({size = Number.POSITIVE_INFINITY} = {}) {
let producedSize = 0;

return new ReadableStream({
read(readSize) {
let shouldEnd = false;

if ((producedSize + readSize) >= options.size) {
readSize = options.size - producedSize;
if ((producedSize + readSize) >= size) {
readSize = size - producedSize;
shouldEnd = true;
}

Expand All @@ -27,10 +26,6 @@ const randomBytesReadableStream = (options = {}) => {
this.push(null);
}
});
}
},
});
};

module.exports = randomBytesReadableStream;
// TODO: Remove this for the next major release
module.exports.default = randomBytesReadableStream;
}
7 changes: 4 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import randomBytesReadableStream = require('.');
import process from 'node:process';
import randomBytesReadableStream from './index.js';

randomBytesReadableStream().pipe(process.stdout);
randomBytesReadableStream({size: 50}).pipe(process.stdout);
randomBytesReadableStream().pipe(process.stdout); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
randomBytesReadableStream({size: 50}).pipe(process.stdout); // eslint-disable-line @typescript-eslint/no-unsafe-member-access
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

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

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:

Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Creates a readable stream producing cryptographically strong pseudo-random data using `crypto.randomBytes()`",
"license": "MIT",
"repository": "sindresorhus/random-bytes-readable-stream",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -32,10 +35,10 @@
"urandom"
],
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"@types/node": "^16.6.0",
"ava": "^3.15.0",
"get-stream": "^5.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"tsd": "^0.17.0",
"xo": "^0.44.0"
}
}
14 changes: 3 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@
It's like a cross-platform `fs.createReadStream('/dev/urandom')`.


## Install

```
$ npm install random-bytes-readable-stream
```


## Usage

```js
const randomBytesReadableStream = require('random-bytes-readable-stream');
import randomBytesReadableStream from 'random-bytes-readable-stream';

randomBytesReadableStream({size: 10}).pipe(process.stdout);
```


## API

### randomBytesReadableStream([options])
### randomBytesReadableStream(options?)

Returns a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams).

Expand All @@ -35,12 +32,7 @@ Type: `Object`

##### size

Type: `number`<br>
Type: `number`\
Default: `Infinity`

The total size to be produced by the stream in bytes.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import getStream from 'get-stream';
import randomBytesReadableStream from '.';
import randomBytesReadableStream from './index.js';

test('main', async t => {
const stream = randomBytesReadableStream();
Expand Down

0 comments on commit ae9b4c5

Please sign in to comment.