feat(read-chunk): module to read data from stream
This commit is contained in:
44
@vates/read-chunk/README.md
Normal file
44
@vates/read-chunk/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||
|
||||
# @vates/read-chunk [](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)
|
||||
13
@vates/read-chunk/USAGE.md
Normal file
13
@vates/read-chunk/USAGE.md
Normal 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
|
||||
}
|
||||
})()
|
||||
```
|
||||
27
@vates/read-chunk/index.js
Normal file
27
@vates/read-chunk/index.js
Normal 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()
|
||||
})
|
||||
29
@vates/read-chunk/package.json
Normal file
29
@vates/read-chunk/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user