Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 18 and return a web stream instead of a node stream #14

Merged
merged 4 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
9 changes: 4 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {Readable as ReadableStream} from 'node:stream';

/**
Convert a `string`/`Buffer`/`Uint8Array` to a [readable stream](https://nodejs.org/api/stream.html#stream_readable_streams).
Convert a value to a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).

@param value - Value to convert to a stream.

@example
```
import toReadableStream from 'to-readable-stream';

toReadableStream('🦄🌈').pipe(process.stdout);
toReadableStream('🦄🌈');
//=> ReadableStream<'🦄🌈'>
```
*/
export default function toReadableStream(value: string | Buffer | Uint8Array): ReadableStream;
export default function toReadableStream<Value>(value: Value): ReadableStream<Value>;
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {Readable as ReadableStream} from 'node:stream';

export default function toReadableStream(value) {
return new ReadableStream({
read() {
this.push(value);
this.push(null);
start(controller) {
controller.enqueue(value);
controller.close();
}
});
}
8 changes: 5 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Buffer} from 'node:buffer';
import toReadableStream from './index.js';
import {expectType} from 'tsd';

toReadableStream('🦄🌈').pipe(process.stdout);
toReadableStream(Buffer.from('🦄🌈')).pipe(process.stdout);
toReadableStream(new Uint8Array(Buffer.from('🦄🌈').buffer)).pipe(process.stdout);
expectType<ReadableStream<string>>(toReadableStream('🦄🌈'));
expectType<ReadableStream<Buffer>>(toReadableStream(Buffer.from('🦄🌈')));
expectType<ReadableStream<Uint8Array>>(toReadableStream(new Uint8Array(Buffer.from('🦄🌈').buffer)));
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "to-readable-stream",
"version": "3.0.0",
"description": "Convert a string/Buffer/Uint8Array to a readable stream",
"description": "Convert a value to a ReadableStream",
"license": "MIT",
"repository": "sindresorhus/to-readable-stream",
"funding": "https://github.com/sponsors/sindresorhus",
Expand All @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -41,5 +41,13 @@
"get-stream": "^6.0.1",
"tsd": "^0.14.0",
"xo": "^0.39.1"
},
"xo": {
"env": "browser"
},
"tsd": {
"compilerOptions": {
"lib": ["dom"]
}
}
}
11 changes: 5 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# to-readable-stream

> Convert a string/Buffer/Uint8Array to a [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable), which should not be confused with [`ReadableStream`](https://nodejs.org/api/webstreams.html#webstreams_class_readablestream)
> Convert a value to a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)

If you target Node.js 12 or later, you can use [`stream.Readable#from()`](https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options) instead.
Not to be confused with [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable), in which case, [`stream.Readable#from()`](https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options) should be used instead.

## Install

Expand All @@ -15,19 +15,18 @@ npm install to-readable-stream
```js
import toReadableStream from 'to-readable-stream';

toReadableStream('🦄🌈').pipe(process.stdout);
toReadableStream('🦄🌈');
//=> ReadableStream<'🦄🌈'>
```

## API

### toReadableStream(value)

Returns a [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
Returns a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).

#### value

Type: `string | Buffer | Uint8Array`

Value to convert to a stream.

## Related
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Readable as ReadableStream} from 'stream';
import {Buffer} from 'node:buffer';
import test from 'ava';
import getStream from 'get-stream';
import toReadableStream from './index.js';
Expand Down