Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Thumbs.db
.nyc_output
coverage
bin
packages/*/dist
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
*.log
.DS_Store
Thumbs.db
.vscode
.nyc_output
coverage
bin
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ node_js:
- 'lts/*'
- 'node'

script: npm run lint && npm run coverage
script: npm run lint && npm run check && npm run coverage
after_success: coveralls-lerna
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"start": "NODE_ENV=development lerna run --parallel start",
"clean": "lerna run --parallel clean",
"lint": "lerna run --parallel lint",
"check": "lerna run --parallel check",
"publish": "lerna publish"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions packages/blob-to-it/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

const browserReadableStreamToIt = require('browser-readablestream-to-it')

/**
* @param {Blob} blob
* @returns {AsyncIterable<Uint8Array>}
*/
function blobToIt (blob) {
if (typeof blob.stream === 'function') {
return browserReadableStreamToIt(blob.stream())
}

// firefox < 69 does not support blob.stream()
// @ts-ignore - response.body is optional, but in practice it's a stream.
return browserReadableStreamToIt(new Response(blob).body)
}

Expand Down
17 changes: 15 additions & 2 deletions packages/blob-to-it/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"scripts": {
"test": "playwright-test",
"lint": "standard",
"coverage": "playwright-test"
"coverage": "playwright-test",
"clean": "rm -rf dist",
"check": "npm run build:dep:types && tsc --noEmit",
"build": "npm run build:types",
"build:dep:types": "cd node_modules/browser-readablestream-to-it && npm run build:types",
"build:types": "npm run build:dep:types && tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
Expand All @@ -22,5 +27,13 @@
"mocha": "8.0.1",
"playwright-test": "^0.7.1",
"standard": "^14.3.1"
},
"typesVersions": {
"*": {
"*": [
".",
"dist/*"
]
}
}
}
}
6 changes: 6 additions & 0 deletions packages/blob-to-it/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../typescript.json",
"include": [
"."
]
}
14 changes: 13 additions & 1 deletion packages/browser-readablestream-to-it/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"scripts": {
"test": "playwright-test",
"lint": "standard",
"coverage": "playwright-test"
"coverage": "playwright-test",
"check": "tsc --noEmit",
"clean": "rm -rf dist",
"build": "npm run build:types",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
Expand All @@ -19,5 +23,13 @@
"mocha": "8.0.1",
"playwright-test": "^0.7.1",
"standard": "^14.3.1"
},
"typesVersions": {
"*": {
"*": [
".",
"dist/*"
]
}
}
}
6 changes: 6 additions & 0 deletions packages/browser-readablestream-to-it/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../typescript.json",
"include": [
"."
]
}
2 changes: 1 addition & 1 deletion packages/it-all/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build status](https://travis-ci.org/achingbrain/it.svg?branch=master)](https://travis-ci.org/achingbrain/it?branch=master) [![Coverage Status](https://coveralls.io/repos/github/achingbrain/it/badge.svg?branch=master)](https://coveralls.io/github/achingbrain/it?branch=master) [![Dependencies Status](https://david-dm.org/achingbrain/it/status.svg?path=packages/it-all)](https://david-dm.org/achingbrain/it?path=packages/it-all)

> Collects all values from an async iterator and returns them as an array
> Collects all values from an (async) iterable into an array and returns it.

For when you need a one-liner to collect iterable values.

Expand Down
11 changes: 9 additions & 2 deletions packages/it-all/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict'

const all = async (iterator) => {
/**
* Collects all values from an (async) iterable into an array and returns it.
*
* @template T
* @param {AsyncIterable<T>|Iterable<T>} source
* @returns {Promise<T[]>}
*/
const all = async (source) => {
const arr = []

for await (const entry of iterator) {
for await (const entry of source) {
arr.push(entry)
}

Expand Down
18 changes: 15 additions & 3 deletions packages/it-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@
"test": "ava",
"lint": "standard",
"coverage": "nyc --reporter html --reporter lcov ava",
"clean": "rm -rf .nyc_output coverage"
"clean": "rm -rf .nyc_output coverage dist",
"check": "tsc --noEmit",
"build": "npm run build:types",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
"devDependencies": {
"ava": "^2.4.0",
"nyc": "^14.0.0",
"standard": "^14.3.1"
"standard": "^14.3.1",
"typescript": "^3.9.7"
},
"typesVersions": {
"*": {
"*": [
".",
"dist/*"
]
}
}
}
}
6 changes: 6 additions & 0 deletions packages/it-all/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../typescript.json",
"include": [
"."
]
}
2 changes: 1 addition & 1 deletion packages/it-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build status](https://travis-ci.org/achingbrain/it.svg?branch=master)](https://travis-ci.org/achingbrain/it?branch=master) [![Coverage Status](https://coveralls.io/repos/github/achingbrain/it/badge.svg?branch=master)](https://coveralls.io/github/achingbrain/it?branch=master) [![Dependencies Status](https://david-dm.org/achingbrain/it/status.svg?path=packages/it-batch)](https://david-dm.org/achingbrain/it?path=packages/it-batch)

> Takes an async iterator that emits things and emits them as fixed-size batches
> Takes an (async) iterable that emits things and returns an async iterable that emits those things in fixed-sized batches.

The final batch may be smaller than the max.

Expand Down
11 changes: 11 additions & 0 deletions packages/it-batch/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
'use strict'

/**
* Takes an (async) iterable that emits things and returns an async iterable that
* emits those things in fixed-sized batches.
*
* @template T
* @param {AsyncIterable<T>|Iterable<T>} source
* @param {number|string} [size=1]
* @returns {AsyncIterable<T[]>}
*/
async function * batch (source, size) {
// @ts-ignore - parseInt expects string
size = parseInt(size)

if (isNaN(size) || size < 1) {
size = 1
}

/** @type {T[]} */
let things = []

for await (const thing of source) {
Expand Down
18 changes: 15 additions & 3 deletions packages/it-batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@
"test": "ava",
"lint": "standard",
"coverage": "nyc --reporter html --reporter lcov ava",
"clean": "rm -rf .nyc_output coverage"
"clean": "rm -rf .nyc_output coverage dist",
"check": "tsc --noEmit",
"build": "npm run build:types",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
"devDependencies": {
"ava": "^2.4.0",
"it-all": "^1.0.2",
"nyc": "^14.0.0",
"standard": "^14.3.1"
"standard": "^14.3.1",
"typescript": "^3.9.7"
},
"typesVersions": {
"*": {
"*": [
"./",
"dist/*"
]
}
}
}
}
6 changes: 6 additions & 0 deletions packages/it-batch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../typescript.json",
"include": [
"."
]
}
2 changes: 1 addition & 1 deletion packages/it-buffer-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build status](https://travis-ci.org/achingbrain/it.svg?branch=master)](https://travis-ci.org/achingbrain/it?branch=master) [![Coverage Status](https://coveralls.io/repos/github/achingbrain/it/badge.svg?branch=master)](https://coveralls.io/github/achingbrain/it?branch=master) [![Dependencies Status](https://david-dm.org/achingbrain/it/status.svg?path=packages/it-buffer-stream)](https://david-dm.org/achingbrain/it?path=packages/it-buffer-stream)

> An async iterator that emits buffers containing bytes up to a certain length
> An async iterable that emits buffers containing bytes up to a certain length

## Install

Expand Down
16 changes: 16 additions & 0 deletions packages/it-buffer-stream/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
'use strict'

// @ts-ignore - untyped dependency
const randomBytes = require('iso-random-stream/src/random')

/**
* @typedef {Object} Options
* @property {number} [chunkSize]
* @property {function(Buffer):void} [collector]
* @property {function(number):Promise<Buffer>|Buffer} [generator]
*/

/** @type {Options} */
const defaultOptions = {
chunkSize: 4096,
collector: () => {},
generator: (size) => Promise.resolve(randomBytes(size))
}

/**
* An async iterable that emits buffers containing bytes up to a certain length.
*
* @param {number} limit
* @param {Options} [options]
* @returns {AsyncIterable<Buffer>}
*/
async function * bufferStream (limit, options = {}) {
options = Object.assign({}, defaultOptions, options)
let emitted = 0
Expand Down
18 changes: 15 additions & 3 deletions packages/it-buffer-stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@
"test": "ava",
"lint": "standard",
"coverage": "nyc --reporter html --reporter lcov ava",
"clean": "rm -rf .nyc_output coverage"
"clean": "rm -rf .nyc_output coverage dist",
"check": "tsc --noEmit",
"build": "npm run build:types",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
"devDependencies": {
"ava": "^2.4.0",
"buffer": "^5.5.0",
"nyc": "^14.0.0",
"standard": "^14.3.1"
"standard": "^14.3.1",
"typescript": "^3.9.7"
},
"dependencies": {
"iso-random-stream": "^1.1.1"
},
"typesVersions": {
"*": {
"*": [
".",
"dist/*"
]
}
}
}
}
6 changes: 6 additions & 0 deletions packages/it-buffer-stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../typescript.json",
"include": [
"."
]
}
2 changes: 1 addition & 1 deletion packages/it-drain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build status](https://travis-ci.org/achingbrain/it.svg?branch=master)](https://travis-ci.org/achingbrain/it?branch=master) [![Coverage Status](https://coveralls.io/repos/github/achingbrain/it/badge.svg?branch=master)](https://coveralls.io/github/achingbrain/it?branch=master) [![Dependencies Status](https://david-dm.org/achingbrain/it/status.svg?path=packages/it-drain)](https://david-dm.org/achingbrain/it?path=packages/it-drain)

> Drains an async iterator without returning anything
> Drains an (async) iterable discarding its content and does not return anything.

Mostly useful for tests or when you want to be explicit about consuming an iterable without doing anything with any yielded values.

Expand Down
12 changes: 10 additions & 2 deletions packages/it-drain/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
'use strict'

const drain = async (iterator) => {
for await (const _ of iterator) { } // eslint-disable-line no-unused-vars
/**
* Drains an (async) iterable discarding its' content and does not return
* anything.
*
* @template T
* @param {AsyncIterable<T>|Iterable<T>} source
* @returns {Promise<void>}
*/
const drain = async (source) => {
for await (const _ of source) { } // eslint-disable-line no-unused-vars
}

module.exports = drain
18 changes: 15 additions & 3 deletions packages/it-drain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@
"test": "ava",
"lint": "standard",
"coverage": "nyc --reporter html --reporter lcov ava",
"clean": "rm -rf .nyc_output coverage"
"clean": "rm -rf .nyc_output coverage dist",
"check": "tsc --noEmit",
"build": "npm run build:types",
"build:types": "tsc --emitDeclarationOnly --declarationDir dist"
},
"author": "Alex Potsides <alex@achingbrain.net>",
"license": "ISC",
"devDependencies": {
"ava": "^2.4.0",
"nyc": "^14.0.0",
"standard": "^14.3.1"
"standard": "^14.3.1",
"typescript": "^3.9.7"
},
"typesVersions": {
"*": {
"*": [
".",
"dist/*"
]
}
}
}
}
Loading