feat(read-chunk): module to read data from stream

This commit is contained in:
Julien Fontanet
2020-05-28 14:34:20 +02:00
parent 20fb2c99bc
commit c10bfe3db2
5 changed files with 130 additions and 15 deletions

View File

@@ -0,0 +1,44 @@
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
# @vates/read-chunk [![Build Status](https://travis-ci.org/vatesfr/xen-orchestra.png?branch=master)](https://travis-ci.org/vatesfr/xen-orchestra)
> Read a chunk of a Node stream
## Install
Installation of the [npm package](https://npmjs.org/package/@vates/read-chunk):
```
> npm install --save @vates/read-chunk
```
## Usage
- returns the next available chunk of data
- like `stream.read()`, a number of bytes can be specified
- returns `null` if the stream has ended
```js
import { readChunk } from '@xen-orchestra/read-chunk'
;(async () => {
let chunk
while ((chunk = await readChunk(stream, 1024)) !== null) {
// do something with chunk
}
})()
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
you've encountered;
- fork and create a pull request.
## License
ISC © [Vates SAS](https://vates.fr)

View File

@@ -0,0 +1,13 @@
- returns the next available chunk of data
- like `stream.read()`, a number of bytes can be specified
- returns `null` if the stream has ended
```js
import { readChunk } from '@xen-orchestra/read-chunk'
;(async () => {
let chunk
while ((chunk = await readChunk(stream, 1024)) !== null) {
// do something with chunk
}
})()
```

View File

@@ -0,0 +1,27 @@
export const readChunk = (stream, size) =>
new Promise((resolve, reject) => {
function onEnd() {
resolve(null)
removeListeners()
}
function onError(error) {
reject(error)
removeListeners()
}
function onReadable() {
const data = stream.read(size)
if (data !== null) {
resolve(data)
removeListeners()
}
}
function removeListeners() {
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
stream.removeListener('readable', onReadable)
}
stream.on('end', onEnd)
stream.on('error', onError)
stream.on('readable', onReadable)
onReadable()
})

View File

@@ -0,0 +1,29 @@
{
"private": false,
"name": "@vates/read-chunk",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/read-chunk",
"description": "Read a chunk of a Node stream",
"license": "ISC",
"keywords": [
"async",
"chunk",
"data",
"node",
"promise",
"read",
"stream"
],
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@vates/read-chunk",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"version": "0.0.0",
"engines": {
"node": ">=8.10"
},
"scripts": {
"postversion": "npm publish --access public"
}
}

View File

@@ -17,21 +17,23 @@ const _getPackages = scope => {
}
exports.getPackages = (readPackageJson = false) => {
const p = Promise.all([_getPackages(), _getPackages('@xen-orchestra')]).then(
pkgs => {
pkgs = [].concat(...pkgs) // flatten
return readPackageJson
? Promise.all(
pkgs.map(pkg =>
readFile(`${pkg.dir}/package.json`).then(data => {
pkg.package = JSON.parse(data)
return pkg
}, noop)
)
).then(pkgs => pkgs.filter(pkg => pkg !== undefined))
: pkgs
}
)
const p = Promise.all([
_getPackages(),
_getPackages('@vates'),
_getPackages('@xen-orchestra'),
]).then(pkgs => {
pkgs = [].concat(...pkgs) // flatten
return readPackageJson
? Promise.all(
pkgs.map(pkg =>
readFile(`${pkg.dir}/package.json`).then(data => {
pkg.package = JSON.parse(data)
return pkg
}, noop)
)
).then(pkgs => pkgs.filter(pkg => pkg !== undefined))
: pkgs
})
p.forEach = fn => p.then(pkgs => forEach.call(pkgs, fn))
p.map = fn => p.then(pkgs => Promise.all(pkgs.map(fn))).then(noop)
return p