Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 16, 2021
1 parent 4f481fb commit 1bf0852
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 61 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
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
41 changes: 18 additions & 23 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
/// <reference types="node"/>
import {Readable as ReadableStream} from 'stream';

declare namespace intoStream {
type Input =
| Buffer
| NodeJS.TypedArray
| ArrayBuffer
| string
| Iterable<Buffer | string>
| AsyncIterable<Buffer | string>;

/* eslint-disable @typescript-eslint/ban-types */
type InputObject =
| object
| Iterable<object>
| AsyncIterable<object>;
/* eslint-enable @typescript-eslint/ban-types */
}
export type Input =
| Buffer
| NodeJS.TypedArray
| ArrayBuffer
| string
| Iterable<Buffer | string>
| AsyncIterable<Buffer | string>;

/* eslint-disable @typescript-eslint/ban-types */
export type ObjectInput =
| object
| Iterable<object>
| AsyncIterable<object>;
/* eslint-enable @typescript-eslint/ban-types */

declare const intoStream: {
/**
Expand All @@ -25,9 +22,7 @@ declare const intoStream: {
@param input - The object input to convert to a stream.
@returns A [readable object stream](https://nodejs.org/api/stream.html#stream_object_mode).
*/
object: (
input: intoStream.InputObject | Promise<intoStream.InputObject>
) => ReadableStream;
object: (input: ObjectInput | Promise<ObjectInput>) => ReadableStream;

/**
Convert `input` into a stream. Adheres to the requested chunk size, except for `array` where each element will be a chunk.
Expand All @@ -37,13 +32,13 @@ declare const intoStream: {
@example
```
import intoStream = require('into-stream');
import intoStream from 'into-stream';
intoStream('unicorn').pipe(process.stdout);
//=> 'unicorn'
```
*/
(input: intoStream.Input | Promise<intoStream.Input>): ReadableStream;
(input: Input | Promise<Input>): ReadableStream;
};

export = intoStream;
export default intoStream;
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const from = require('from2');
const pIsPromise = require('p-is-promise');
import from2 from 'from2';
import isPromise from 'p-is-promise';

const intoStream = input => {
export default function intoStream(input) {
if (Array.isArray(input)) {
input = input.slice();
}
Expand All @@ -23,7 +22,7 @@ const intoStream = input => {
input = Buffer.from(input);
}

promise = pIsPromise(input) ? input : null;
promise = isPromise(input) ? input : null;

// We don't iterate on strings and buffers since slicing them is ~7x faster
const shouldIterate = !promise && input[Symbol.iterator] && typeof input !== 'string' && !Buffer.isBuffer(input);
Expand All @@ -33,7 +32,7 @@ const intoStream = input => {
asyncIterator = shouldAsyncIterate ? input[Symbol.asyncIterator]() : null;
}

return from(function reader(size, callback) {
return from2(function reader(size, callback) {
if (promise) {
(async () => {
try {
Expand Down Expand Up @@ -76,11 +75,9 @@ const intoStream = input => {

setImmediate(callback, null, chunk);
});
};

module.exports = intoStream;
}

module.exports.object = input => {
intoStream.object = input => {
if (Array.isArray(input)) {
input = input.slice();
}
Expand All @@ -93,12 +90,12 @@ module.exports.object = input => {

function prepare(value) {
input = value;
promise = pIsPromise(input) ? input : null;
promise = isPromise(input) ? input : null;
iterator = !promise && input[Symbol.iterator] ? input[Symbol.iterator]() : null;
asyncIterator = !promise && input[Symbol.asyncIterator] ? input[Symbol.asyncIterator]() : null;
}

return from.obj(function reader(size, callback) {
return from2.obj(function reader(size, callback) {
if (promise) {
(async () => {
try {
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import intoStream = require('.');
import intoStream from './index.js';

const unicornArray = 'unicorn'.split('');

Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -46,11 +48,11 @@
"p-is-promise": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"get-stream": "^6.0.0",
"ava": "^3.15.0",
"get-stream": "^6.0.1",
"p-event": "^4.2.0",
"p-immediate": "^3.1.0",
"tsd": "^0.13.1",
"xo": "^0.33.0"
"p-immediate": "^4.0.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ npm install into-stream
## Usage

```js
const intoStream = require('into-stream');
import intoStream from 'into-stream';

intoStream('unicorn').pipe(process.stdout);
//=> 'unicorn'
Expand Down
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import test from 'ava';
import getStream from 'get-stream';
import pEvent from 'p-event';
import pImmediate from 'p-immediate';
import intoStream from '.';
import intoStream from './index.js';

const fixture = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.';

function generatorFrom(array) {
return function * () {
let i = 0;
while (i < array.length) {
yield array[i++];
let index = 0;
while (index < array.length) {
yield array[index++];
}
};
}
Expand All @@ -23,11 +23,11 @@ function iterableFrom(array) {

function asyncGeneratorFrom(array) {
return async function * () {
let i = 0;
while (i < array.length) {
let index = 0;
while (index < array.length) {
// eslint-disable-next-line no-await-in-loop
await pImmediate();
yield array[i++];
yield array[index++];
}
};
}
Expand All @@ -50,8 +50,8 @@ test('buffer', async t => {
test('ArrayBuffer', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (const [i, element] of f.entries()) {
view[i] = element;
for (const [index, element] of f.entries()) {
view[index] = element;
}

t.true((await getStream.buffer(intoStream(view.buffer))).equals(f));
Expand All @@ -60,8 +60,8 @@ test('ArrayBuffer', async t => {
test('ArrayBuffer view', async t => {
const f = Buffer.from(fixture);
const view = new Uint8Array(f.length);
for (const [i, element] of f.entries()) {
view[i] = element;
for (const [index, element] of f.entries()) {
view[index] = element;
}

t.true((await getStream.buffer(intoStream(view))).equals(f));
Expand Down Expand Up @@ -116,7 +116,7 @@ test('stream errors when promise rejects', async t => {
const promise = new Promise((resolve, reject) => {
setImmediate(reject.bind(null, new Error('test error')));
});
await t.throwsAsync(getStream(intoStream(promise)), 'test error');
await t.throwsAsync(getStream(intoStream(promise)), {message: 'test error'});
});

test('object mode', async t => {
Expand Down Expand Up @@ -173,7 +173,7 @@ test('object mode errors when promise rejects', async t => {
const promise = new Promise((resolve, reject) => {
setImmediate(reject.bind(null, new Error('test error')));
});
await t.throwsAsync(getStream.array(intoStream.object(promise)), 'test error');
await t.throwsAsync(getStream.array(intoStream.object(promise)), {message: 'test error'});
});

test('pushes chunk on next tick', async t => {
Expand Down

0 comments on commit 1bf0852

Please # to comment.