Skip to content

Commit

Permalink
feat: convert to typescript (#16)
Browse files Browse the repository at this point in the history
- Only named exports
- No more CJS, only ESM
- Adds auto-publish
- Adds dependabot
- Adds tests for abortable sink/source/duplex
- Removes support for multiple abort signals - just use `any-signal`

BREAKING CHANGE: switch to named exports, ESM only
  • Loading branch information
achingbrain committed Jan 12, 2022
1 parent 8fd144b commit 7a36b81
Show file tree
Hide file tree
Showing 17 changed files with 680 additions and 11,569 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "11:00"
open-pull-requests-limit: 10
152 changes: 152 additions & 0 deletions .github/workflows/js-test-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
name: test & maybe release
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:

check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present lint
- run: npm run --if-present dep-check

test-node:
needs: check
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node: [16]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:node
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: node

test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:chrome
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: chrome

test-chrome-webworker:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:chrome-webworker
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: chrome-webworker

test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:firefox
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: firefox

test-firefox-webworker:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:firefox-webworker
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: firefox-webworker

test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npx xvfb-maybe npm run --if-present test:electron-main
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: electron-main

test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npx xvfb-maybe npm run --if-present test:electron-renderer
- uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
with:
directory: ./.nyc_output
flags: electron-renderer

release:
needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer]
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v2.4.0
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- uses: ipfs/aegir/actions/docker-login@master
with:
docker-token: ${{ secrets.DOCKER_TOKEN }}
docker-username: ${{ secrets.DOCKER_USERNAME }}
- run: npm run --if-present release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
.nyc_output
coverage
.nyc_output
package-lock.json
dist
4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

7 changes: 0 additions & 7 deletions AbortError.js

This file was deleted.

74 changes: 20 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# abortable-iterator

[![Build Status](https://travis-ci.org/alanshaw/abortable-iterator.svg?branch=master)](https://travis-ci.org/alanshaw/abortable-iterator) [![dependencies Status](https://david-dm.org/alanshaw/abortable-iterator/status.svg)](https://david-dm.org/alanshaw/abortable-iterator) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Build Status](https://github.com/alanshaw/abortable-iterator/actions/workflows/js-test-and-release.yml/badge.svg?branch=master)](https://github.com/alanshaw/abortable-iterator/actions/workflows/js-test-and-release.yml)
[![Dependencies Status](https://status.david-dm.org/gh/alanshaw/abortable-iterator.svg)](https://david-dm.org/alanshaw/abortable-iterator)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

> Make any iterator or iterable abortable via an AbortSignal
Expand All @@ -15,8 +17,7 @@ npm install abortable-iterator
## Usage

```js
const abortable = require('abortable-iterator')
const AbortController = require('abort-controller')
import { abortableSource } from 'abortable-iterator'

// An example function that creates an async iterator that yields an increasing
// number every x milliseconds and NEVER ENDS!
Expand All @@ -32,7 +33,7 @@ const everySecond = asyncCounter(0, 1000)

// Make everySecond abortable!
const controller = new AbortController()
const abortableEverySecond = abortable(everySecond, controller.signal)
const abortableEverySecond = abortableSource(everySecond, controller.signal)

// Abort after 5 seconds
setTimeout(() => controller.abort(), 5000)
Expand All @@ -54,21 +55,20 @@ try {
## API

```js
const abortable = require('abortable-iterator')
import {
abortableSource,
abortableSink,
abortableTransform,
abortableDuplex
} from 'abortable-iterator'
```

* [`abortable(source, signal, [options])`](#abortablesource-signal-options)
* [`abortable(source, signals)`](#abortablesource-signals)
* [`abortable.source(source, signal, [options])`](#abortablesource-signal-options)
* [`abortable.source(source, signals)`](#abortablesource-signals)
* [`abortable.sink(sink, signal, [options])`](#abortablesinksink-signal-options)
* [`abortable.sink(sink, signals)`](#abortablesinksink-signals)
* [`abortable.transform(transform, signal, [options])`](#abortabletransformtransform-signal-options)
* [`abortable.transform(transform, signals)`](#abortabletransformtransform-signals)
* [`abortable.duplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options)
* [`abortable.duplex(duplex, signals)`](#abortableduplex-signals)

### `abortable(source, signal, [options])`
* [`abortableSource(source, signal, [options])`](#abortablesource-signal-options)
* [`abortableSink(sink, signal, [options])`](#abortablesinksink-signal-options)
* [`abortableTransform(transform, signal, [options])`](#abortabletransformtransform-signal-options)
* [`abortableDuplex(duplex, signal, [options])`](#abortableduplexduplex-signal-options)

### `abortableSource(source, signal, [options])`
**(alias for `abortable.source(source, signal, [options])`)**

Make any iterator or iterable abortable via an `AbortSignal`.
Expand All @@ -94,54 +94,20 @@ Make any iterator or iterable abortable via an `AbortSignal`.

The returned iterator will `throw` an `AbortError` when it is aborted that has a `type` with the value `aborted` and `code` property with the value `ABORT_ERR` by default.

### `abortable(source, signals)`
**(alias for `abortable.source(source, signals)`)**

Make any iterator or iterable abortable via any one of the passed `AbortSignal`'s.

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| source | [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol)\|[`Iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | The iterator or iterable object to make abortable |
| signals | `Array<{ signal, [options] }>` | An array of objects with `signal` and optional `options` properties. See above docs for expected values for these two properties. |

#### Returns

| Type | Description |
|------|-------------|
| [`Iterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) | An iterator that wraps the passed `source` parameter that makes it abortable via the passed `signal` parameters. |

The returned iterator will `throw` an `AbortError` when it is aborted on _any_ one of the passed abort signals. The error object has a `type` with the value `aborted` and `code` property with the value `ABORT_ERR` by default.

### `abortable.sink(sink, signal, [options])`
### `abortableSink(sink, signal, [options])`

The same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`sink`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#sink-it) abortable. Returns a new sink that wraps the passed `sink` and makes it abortable via the passed `signal` parameter.

### `abortable.sink(sink, signals)`

The same as [`abortable.source`](#abortablesource-signals) except this makes the passed [`sink`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#sink-it) abortable via any one of the passed `AbortSignal`'s. Returns a new sink that wraps the passed `sink` and makes it abortable via the passed `signal` parameters.

### `abortable.transform(transform, signal, [options])`
### `abortableTransform(transform, signal, [options])`

The same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`transform`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) abortable. Returns a new transform that wraps the passed `transform` and makes it abortable via the passed `signal` parameter.

### `abortable.transform(transform, signals)`

The same as [`abortable.source`](#abortablesource-signals) except this makes the passed [`transform`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) abortable via any one of the passed `AbortSignal`'s. Returns a new transform that wraps the passed `transform` and makes it abortable via the passed `signal` parameters.

### `abortable.duplex(duplex, signal, [options])`
### `abortableDuplex(duplex, signal, [options])`

The same as [`abortable.source`](#abortablesource-signal-options) except this makes the passed [`duplex`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) abortable. Returns a new duplex that wraps the passed `duplex` and makes it abortable via the passed `signal` parameter.

Note that this will abort _both_ sides of the duplex. Use `duplex.sink = abortable.sink(duplex.sink)` or `duplex.source = abortable.source(duplex.source)` to abort just the sink or the source.

### `abortable.duplex(duplex, signals)`

The same as [`abortable.source`](#abortablesource-signals) except this makes the passed [`duplex`](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) abortable via any one of the passed `AbortSignal`'s. Returns a new duplex that wraps the passed `duplex` and makes it abortable via the passed `signal` parameters.

Note that this will abort _both_ sides of the duplex. Use `duplex.sink = abortable.sink(duplex.sink)` or `duplex.source = abortable.source(duplex.source)` to abort just the sink or the source.

## Related

* [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to "pipe" async iterables together
Expand Down
5 changes: 2 additions & 3 deletions example.js → examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const abortable = require('./')
const AbortController = require('abort-controller')
import { abortableSource } from 'abortable-iterator'

async function main () {
// An example function that creates an async iterator that yields an increasing
Expand All @@ -16,7 +15,7 @@ async function main () {

// Make everySecond abortable!
const controller = new AbortController()
const abortableEverySecond = abortable(everySecond, controller.signal)
const abortableEverySecond = abortableSource(everySecond, controller.signal)

// Abort after 5 seconds
setTimeout(() => controller.abort(), 5000)
Expand Down
70 changes: 0 additions & 70 deletions index.d.ts

This file was deleted.

Loading

0 comments on commit 7a36b81

Please sign in to comment.