From 08ceb85bb199f5492b12f125d33c8d0596f0e43f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 9 Jul 2021 11:06:32 +0200 Subject: [PATCH] Use ESM --- .gitignore | 5 ++--- .prettierignore | 1 - index.js | 12 ++++-------- package.json | 20 +++++++++---------- readme.md | 8 +++++++- test.js | 52 ++++++++++++++++++++++++------------------------- 6 files changed, 47 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index fdefc8c..33d4929 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ -.DS_Store -*.log -.nyc_output/ coverage/ node_modules/ +.DS_Store +*.log yarn.lock diff --git a/.prettierignore b/.prettierignore index e7939c4..cebe81f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,2 @@ coverage/ -*.json *.md diff --git a/index.js b/index.js index 485697c..97c5d22 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,7 @@ -'use strict' +import events from 'events' +import once from 'once' -module.exports = stream - -var events = require('events') -var once = require('once') - -function stream(processor) { +export function stream(processor) { var chunks = [] var emitter = new events.EventEmitter() var ended @@ -47,7 +43,7 @@ function stream(processor) { // If messages are triggered during the process, those are triggerd as // `warning`s. function end() { - write.apply(null, arguments) + write(...arguments) ended = true diff --git a/package.json b/package.json index a6aa7fb..7f25a14 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], + "sideEffects": false, + "type": "module", + "main": "index.js", "files": [ "index.js" ], @@ -24,27 +27,21 @@ "once": "^1.4.0" }, "devDependencies": { + "c8": "^7.0.0", "is-function": "^1.0.0", - "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^9.0.0", "remark-preset-wooorm": "^8.0.0", "tape": "^5.0.0", "unified": "^9.0.0", - "xo": "^0.38.0" + "xo": "^0.39.0" }, "scripts": { "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", - "test-api": "node test", - "test-coverage": "nyc --reporter lcov tape test.js", + "test-api": "node --conditions development test.js", + "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js", "test": "npm run format && npm run test-coverage" }, - "nyc": { - "check-coverage": true, - "lines": 100, - "functions": 100, - "branches": 100 - }, "prettier": { "tabWidth": 2, "useTabs": false, @@ -55,8 +52,9 @@ }, "xo": { "prettier": true, - "esnext": false, "rules": { + "no-var": "off", + "prefer-arrow-callback": "off", "unicorn/explicit-length-check": "off", "unicorn/prefer-reflect-apply": "off" } diff --git a/readme.md b/readme.md index f8860fa..e7a49c2 100644 --- a/readme.md +++ b/readme.md @@ -13,6 +13,9 @@ Note that the interface is streaming, but the code buffers. ## Install +This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): +Node 12+ is needed to use it and it must be `import`ed instead of `require`d. + [npm][]: ```sh @@ -33,7 +36,10 @@ process.stdin.pipe(stream(rehype().use(format))).pipe(process.stdout) ## API -### `createStream(processor)` +This package exports the following identifiers: `stream`. +There is no default export. + +### `stream(processor)` Create a readable/writable stream that transforms with `processor`. diff --git a/test.js b/test.js index 46020f6..581e6a7 100644 --- a/test.js +++ b/test.js @@ -1,16 +1,14 @@ -'use strict' +import nodeStream from 'stream' +import test from 'tape' +import unified from 'unified' +import func from 'is-function' +import {stream} from './index.js' -var stream = require('stream') -var test = require('tape') -var unified = require('unified') -var func = require('is-function') -var createStream = require('.') - -test('createStream', function (t) { +test('stream', function (t) { var proc = unified().use(parse).use(stringify) t.test('interface', function (st) { - var tr = createStream(proc) + var tr = stream(proc) st.equal(tr.readable, true, 'should be readable') st.equal(tr.writable, true, 'should be writable') st.ok(func(tr.write), 'should have a `write` method') @@ -25,11 +23,11 @@ test('createStream', function (t) { st.plan(10) - st.equal(createStream(proc).end(), true, 'should return true') + st.equal(stream(proc).end(), true, 'should return true') st.throws( function () { - var tr = createStream(proc) + var tr = stream(proc) tr.end() tr.end() }, @@ -37,19 +35,19 @@ test('createStream', function (t) { 'should throw on end after end' ) - createStream(proc) + stream(proc) .on('data', function (value) { st.equal(value, '', 'should emit processed `data`') }) .end() - createStream(proc) + stream(proc) .on('data', function (value) { st.equal(value, 'alpha', 'should emit given `data`') }) .end('alpha') - createStream(proc) + stream(proc) .on('data', function (value) { st.equal(value, 'brC!vo', 'should honour encoding') }) @@ -57,7 +55,7 @@ test('createStream', function (t) { phase = 0 - createStream(proc) + stream(proc) .on('data', function () { st.equal(phase, 1, 'should trigger data after callback') phase++ @@ -69,7 +67,7 @@ test('createStream', function (t) { exception = new Error('alpha') - createStream( + stream( proc().use(function () { return transformer function transformer() { @@ -89,7 +87,7 @@ test('createStream', function (t) { ) .end() - createStream( + stream( proc().use(function () { return transformer function transformer(tree, file) { @@ -118,13 +116,13 @@ test('createStream', function (t) { st.doesNotThrow(function () { // Not writable. - var tr = createStream(proc) - tr.pipe(new stream.Readable()) + var tr = stream(proc) + tr.pipe(new nodeStream.Readable()) tr.end('foo') }, 'should not throw when piping to a non-writable stream') - tr = createStream(proc) - s = new stream.PassThrough() + tr = stream(proc) + s = new nodeStream.PassThrough() s._isStdio = true tr.pipe(s) @@ -137,15 +135,15 @@ test('createStream', function (t) { s.write('delta') }, 'should not `end` stdio streams') - tr = createStream(proc).on('error', function (error) { + tr = stream(proc).on('error', function (error) { st.equal(error.message, 'Whoops!', 'should pass errors') }) - tr.pipe(new stream.PassThrough()) + tr.pipe(new nodeStream.PassThrough()) tr.emit('error', new Error('Whoops!')) - tr = createStream(proc) - tr.pipe(new stream.PassThrough()) + tr = stream(proc) + tr.pipe(new nodeStream.PassThrough()) st.throws( function () { @@ -155,9 +153,9 @@ test('createStream', function (t) { 'should throw if errors are not listened to' ) - tr = createStream(proc) + tr = stream(proc) - tr.pipe(new stream.PassThrough()) + tr.pipe(new nodeStream.PassThrough()) .on('data', function (buf) { st.equal( String(buf),