diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index aa74748..ad06c7a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index be57998..9eeb56f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,5 @@ -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. @@ -9,7 +7,8 @@ Convert a `string`/`Buffer`/`Uint8Array` to a [readable stream](https://nodejs.o ``` 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): ReadableStream; diff --git a/index.js b/index.js index 1cdbf59..4354574 100644 --- a/index.js +++ b/index.js @@ -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(); } }); } diff --git a/index.test-d.ts b/index.test-d.ts index e704ca5..d667301 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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>(toReadableStream('🦄🌈')); +expectType>(toReadableStream(Buffer.from('🦄🌈'))); +expectType>(toReadableStream(new Uint8Array(Buffer.from('🦄🌈').buffer))); diff --git a/package.json b/package.json index 5215567..5174fcd 100644 --- a/package.json +++ b/package.json @@ -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", @@ -13,7 +13,7 @@ "type": "module", "exports": "./index.js", "engines": { - "node": ">=12" + "node": ">=18" }, "scripts": { "test": "xo && ava && tsd" @@ -41,5 +41,13 @@ "get-stream": "^6.0.1", "tsd": "^0.14.0", "xo": "^0.39.1" + }, + "xo": { + "env": "browser" + }, + "tsd": { + "compilerOptions": { + "lib": ["dom"] + } } } diff --git a/readme.md b/readme.md index d273930..cefd749 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 diff --git a/test.js b/test.js index 94c4ac2..8b07cb5 100644 --- a/test.js +++ b/test.js @@ -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';