Skip to content

Commit

Permalink
Add stream package with drain & waitUntilFinished (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanziw authored Oct 1, 2022
1 parent 57626f4 commit 3867b2f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/stream/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src/
**/__tests__/
tsconfig.json
27 changes: 27 additions & 0 deletions packages/stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @kanziw/stream

[![npm version](https://img.shields.io/npm/v/@kanziw/stream)](https://www.npmjs.com/package/@kanziw/stream)
[![license](https://img.shields.io/npm/l/@kanziw/stream)](https://www.npmjs.com/package/@kanziw/stream)
[![npm downloads](https://img.shields.io/npm/dt/@kanziw/stream)](https://www.npmjs.com/package/@kanziw/stream)


## drain

Drain stream

```ts
import { drain } from '@kanziw/stream'

const buf = await drain(someStream)
```


## waitUntilFinished

Wait until stream finished

```ts
import { waitUntilFinished } from '@kanziw/stream'

await waitUntilFinished(someStream)
```
39 changes: 39 additions & 0 deletions packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@kanziw/stream",
"version": "0.1.0",
"description": "A collection of utility libraries about stream",
"repository": {
"type": "git",
"url": "git+https://github.com/kanziw/kanziwjs.git",
"directory": "packages/stream"
},
"publishConfig": {
"access": "public"
},
"homepage": "https://github.com/kanziw/kanziwjs/tree/main/packages/stream#readme",
"keywords": [
"stream",
"kanziw",
"drain"
],
"author": {
"name": "Jiwoong Jung",
"email": "kanziwoong@gmail.com"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"packageManager": "yarn@3.2.0",
"scripts": {
"lint": "yarn workspace kanziwjs eslint packages/stream",
"clean": "rimraf lib",
"build": "yarn clean && yarn tsc",
"publish": "yarn build && yarn publish-if-not-published"
},
"devDependencies": {
"@types/node": "^17.0.25",
"publish-if-not-published": "^3.1.2",
"rimraf": "^3.0.2",
"typescript": "^4.6.3"
},
"license": "MIT"
}
10 changes: 10 additions & 0 deletions packages/stream/src/drain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Stream } from 'stream'

export const drain = async(stream: Stream): Promise<Buffer> => {
const bufs: Uint8Array[] = []

return new Promise<Buffer>(resolve => {
stream.on('data', chunk => bufs.push(chunk))
stream.on('end', () => resolve(Buffer.concat(bufs)))
})
}
2 changes: 2 additions & 0 deletions packages/stream/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './drain'
export * from './waitUntilFinished'
8 changes: 8 additions & 0 deletions packages/stream/src/waitUntilFinished.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Stream } from 'stream'

export const waitUntilFinished = (stream: Stream) => (
new Promise((resolve, reject) => {
stream.on('finish', resolve)
stream.on('error', reject)
})
)
10 changes: 10 additions & 0 deletions packages/stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
},
"include": [
"src"
]
}
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,17 @@ __metadata:
languageName: unknown
linkType: soft

"@kanziw/stream@workspace:packages/stream":
version: 0.0.0-use.local
resolution: "@kanziw/stream@workspace:packages/stream"
dependencies:
"@types/node": ^17.0.25
publish-if-not-published: ^3.1.2
rimraf: ^3.0.2
typescript: ^4.6.3
languageName: unknown
linkType: soft

"@kanziw/time@workspace:packages/time":
version: 0.0.0-use.local
resolution: "@kanziw/time@workspace:packages/time"
Expand Down

0 comments on commit 3867b2f

Please sign in to comment.