Skip to content

Commit

Permalink
feat!: convert to typescript (alanshaw#21)
Browse files Browse the repository at this point in the history
* feat!: convert to typescript

- Converts to typescript
- Only named exports
- No more CJS, only ESM
- Uses aegir for building/testing/publishing
- Publishes with provenance
- Publishes API docs
- Switches travis for GitHub CI
- Updates deps

Similar in scope to alanshaw/it-ws#6 etc

* chore: fix examples

* chore: simpler config

* chore: refactor benchmark
  • Loading branch information
achingbrain authored Mar 7, 2024
1 parent 5975f01 commit afedf8b
Show file tree
Hide file tree
Showing 36 changed files with 1,193 additions and 11,791 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "10:00"
open-pull-requests-limit: 20
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
11 changes: 11 additions & 0 deletions .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# File managed by web3-bot. DO NOT EDIT.
# See https://github.com/protocol/.github/ for details.

name: Automerge
on: [ pull_request ]

jobs:
automerge:
uses: protocol/.github/.github/workflows/automerge.yml@master
with:
job: 'automerge'
27 changes: 27 additions & 0 deletions .github/workflows/js-test-and-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test & maybe release

on:
push:
branches:
- master
pull_request:
workflow_dispatch:

permissions:
contents: write
id-token: write
packages: write
pull-requests: write

concurrency:
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}
cancel-in-progress: true

jobs:
js-test-and-release:
uses: pl-strflt/uci/.github/workflows/js-test-and-release.yml@v0.0
secrets:
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
UCI_GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/semantic-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Semantic PR

on:
pull_request_target:
types:
- opened
- edited
- synchronize

jobs:
main:
uses: pl-strflt/.github/.github/workflows/reusable-semantic-pull-request.yml@v0.3
13 changes: 13 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Close and mark stale issue

on:
schedule:
- cron: '0 0 * * *'

permissions:
issues: write
pull-requests: write

jobs:
stale:
uses: pl-strflt/.github/.github/workflows/reusable-stale-issue.yml@v0.3
11 changes: 8 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
node_modules
.nyc_output
coverage
.clinic
build
dist
.docs
.coverage
node_modules
package-lock.json
yarn.lock
.vscode
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

23 changes: 3 additions & 20 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
MIT License
This project is dual licensed under MIT and Apache-2.0.

Copyright (c) 2019 Alan Shaw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
107 changes: 59 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
# stream-to-it

[![Build Status](https://travis-ci.org/alanshaw/stream-to-it.svg?branch=master)](https://travis-ci.org/alanshaw/stream-to-it)
[![dependencies Status](https://status.david-dm.org/gh/alanshaw/stream-to-it.svg)](https://david-dm.org/alanshaw/stream-to-it)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![codecov](https://img.shields.io/codecov/c/github/alanshaw/stream-to-it.svg?style=flat-square)](https://codecov.io/gh/alanshaw/stream-to-it)
[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/stream-to-it/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/alanshaw/stream-to-it/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)

> Convert Node.js streams to streaming iterables
## Install
# About

```sh
npm i stream-to-it
```
<!--
## Usage
!IMPORTANT!
```js
const toIterable = require('stream-to-it')
```
Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.
To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts
To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.
-->

### Convert readable stream to source iterable
Seamlessly use Node.js streams with `it-pipe` and friends.

## Example - Convert readable stream to source iterable

```TypeScript
import fs from 'node:fs'
import * as toIterable from 'stream-to-it'

```js
const readable = fs.createReadStream('/path/to/file')
// Node.js streams are already async iterable so this is just s => s
const source = toIterable.source(readable)
const source = toIterable.source<Buffer>(readable)

for await (const chunk of source) {
console.log(chunk.toString())
Expand All @@ -32,29 +41,40 @@ for await (const chunk of source) {

Also works with browser [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream):

```js
const res = fetch('http://example.org/file.jpg')
```TypeScript
import * as toIterable from 'stream-to-it'

const res = await fetch('http://example.org/file.jpg')

if (res.body == null) {
throw new Error('Body was not set')
}

for await (const chunk of toIterable.source(res.body)) {
console.log(chunk.toString())
}
```

### Convert writable stream to sink iterable
## Example - Convert writable stream to sink iterable

```js
const pipe = require('it-pipe')
```TypeScript
import fs from 'node:fs'
import { pipe } from 'it-pipe'
import * as toIterable from 'stream-to-it'

const source = [Buffer.from('Hello '), Buffer.from('World!')]
const sink = toIterable.sink(fs.createWriteStream('/path/to/file'))

await pipe(source, sink)
```

### Convert transform stream to transform iterable
## Example - Convert transform stream to transform iterable

```js
const { Transform } = require('stream')
```TypeScript
import fs from 'node:fs'
import { Transform } from 'node:stream'
import { pipe } from 'it-pipe'
import * as toIterable from 'stream-to-it'

const output = await pipe(
[true, false, true, true],
Expand All @@ -64,47 +84,38 @@ const output = await pipe(
}
})),
// Collect and return the chunks
source => {
async source => {
const chunks = []
for await (chunk of source) chunks.push(chunk)
for await (const chunk of source) chunks.push(chunk)
return chunks
}
)

console.log(output) // [ false, true, false, false ]
```

## API

```js
const toIterable = require('stream-to-it')
```

### `toIterable.source(readable): Function`

Convert a [`Readable`](https://nodejs.org/dist/latest/docs/api/stream.html#stream_readable_streams) stream or a browser [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) to a [source](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#source-it) iterable.

### `toIterable.sink(writable): Function`

Convert a [`Writable`](https://nodejs.org/dist/latest/docs/api/stream.html#stream_writable_streams) stream to a [sink](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#sink-it) iterable.
## Related

### `toIterable.transform(transform): Function`
- [`it-to-stream`](https://www.npmjs.com/package/it-to-stream) Convert streaming iterables to Node.js streams
- [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to "pipe" async iterables together

Convert a [`Transform`](https://nodejs.org/dist/latest/docs/api/stream.html#stream_duplex_and_transform_streams) stream to a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) iterable.
# Install

### `toIterable.duplex(duplex): { sink: Function, source: Function }`
```console
$ npm i stream-to-it
```

Convert a [`Duplex`](https://nodejs.org/dist/latest/docs/api/stream.html#stream_duplex_and_transform_streams) stream to a [duplex](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#duplex-it) iterable.
# API Docs

## Related
- <https://github.saobby.my.eu.org.github.io/alanshaw>

* [`it-to-stream`](https://www.npmjs.com/package/it-to-stream) Convert streaming iterables to Node.js streams
* [`it-pipe`](https://www.npmjs.com/package/it-pipe) Utility to "pipe" async iterables together
# License

## Contribute
Licensed under either of

Feel free to dive in! [Open an issue](https://github.com/alanshaw/stream-to-it/issues/new) or submit PRs.
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## License
# Contribution

[MIT](LICENSE) © Alan Shaw
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
12 changes: 6 additions & 6 deletions benchmark/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* 2. From project root: `clinic doctor -- node ./benchmark/memory.js`
*/

const fs = require('fs')
const os = require('os')
const toIterable = require('../')
const pipe = require('it-pipe')
import fs from 'node:fs'
import os from 'node:os'
import { pipe } from 'it-pipe'
import { sink } from '../dist/src/index.js'

async function * run () {
for (let i = 0; i < 170000; i++) {
Expand All @@ -21,7 +21,7 @@ async function * run () {
}

(async () => {
const sink = toIterable.sink(fs.createWriteStream(`${os.tmpdir()}/memcheck.txt`))
await pipe(run(), sink)
const s = sink(fs.createWriteStream(`${os.tmpdir()}/memcheck.txt`))
await pipe(run(), s)
process.exit(1)
})()
4 changes: 0 additions & 4 deletions duplex.js

This file was deleted.

5 changes: 0 additions & 5 deletions index.js

This file was deleted.

Loading

0 comments on commit afedf8b

Please sign in to comment.