Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 9, 2021
1 parent 7e25deb commit 08ceb85
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
.DS_Store
*.log
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,31 @@
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"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,
Expand All @@ -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"
}
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.

Expand Down
52 changes: 25 additions & 27 deletions test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -25,39 +23,39 @@ 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()
},
/^Error: Did not expect `write` after `end`$/,
'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')
})
.end(Buffer.from([0x62, 0x72, 0xc3, 0xa1, 0x76, 0x6f]), 'ascii')

phase = 0

createStream(proc)
stream(proc)
.on('data', function () {
st.equal(phase, 1, 'should trigger data after callback')
phase++
Expand All @@ -69,7 +67,7 @@ test('createStream', function (t) {

exception = new Error('alpha')

createStream(
stream(
proc().use(function () {
return transformer
function transformer() {
Expand All @@ -89,7 +87,7 @@ test('createStream', function (t) {
)
.end()

createStream(
stream(
proc().use(function () {
return transformer
function transformer(tree, file) {
Expand Down Expand Up @@ -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)
Expand All @@ -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 () {
Expand All @@ -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),
Expand Down

0 comments on commit 08ceb85

Please sign in to comment.