Skip to content

Commit

Permalink
feat(most-node-streams): initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed Nov 27, 2016
0 parents commit 81c3747
Show file tree
Hide file tree
Showing 21 changed files with 510 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root = true

[*]
indent_style = space
indent_size = 2
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
insert_final_newline = false
16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Do you need help or have a question? Please ensure your question is thorough.
Found a bug? Please fill out the sections below 👍.
Show some empathy to the person reading your issue. They are volunteers.
-->

**Code to reproduce the issue:**


**Expected behavior:**


**Actual behavior:**


**Versions of packages used:**
34 changes: 34 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!--
Thank you for your contribution!
To help speed up the process of merging your code, check the following:
-->

- [ ] I added new tests for the issue I fixed or the feature I built
- [ ] I ran `npm test` for the package I'm modifying
- [ ] I used `npm run commit` instead of `git commit`

<!--
Please Continue to describe your fix / change citing an issue if possible.
If there are BREAKING CHANGES, please use one of the following to
communicate the changes that have occured.
This change absolutely will affect users:
![yes](https://img.shields.io/badge/will%20it%20affect%20me%3F-yes-red.svg)
High probability of affecting users:
![probably will](https://img.shields.io/badge/will%20it%20affect%20me%3F-probably%20will-orange.svg)
Will affect more users than not:
![maybe will](https://img.shields.io/badge/will%20it%20affect%20me%3F-maybe%20will-yellow.svg)
Will *not* affect more users than not:
![maybe won't](https://img.shields.io/badge/will%20it%20affect%20me%3F-maybe%20won't-yellowgreen.svg)
Will only affect a few people
![probably won't](https://img.shields.io/badge/will%20it%20affect%20me%3F-probably%20won't-green.svg)
Though breaking, it will almost definitely *not* affect users:
![no](https://img.shields.io/badge/will%20it%20affect%20me%3F-no-brightgreen.svg)
-->
43 changes: 43 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# generated files
lib
.tmp
test/test.txt
Empty file added .npmignore
Empty file.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 6
- 7
19 changes: 19 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"editor.fontFamily": "'Fira Code Medium', 'Courier New', monospace, 'Droid Sans Fallback'",

"editor.fontLigatures": true,

"editor.rulers": [80, 160],

"editor.wrappingColumn": 120,

"editor.wrappingIndent": "indent",

"editor.tabSize": 2,

"editor.insertSpaces": true,

"editor.formatOnType": true,

"typescript.tsdk": "node_modules/typescript/lib"
}
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Contributing

First of all, thank you so much, we need your help.

## Contributing a fix or feature

1. Fork the repository
2. Switch to a new branch `git checkout -b [branchName]`
3. Produce your fix or feature
4. Use `npm run commit` instead of `git commit` PLEASE!
5. Submit a pull request for review
20 changes: 20 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 Tylor Steinberger

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.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# most-node-streams

> Convert Node.js Streams to Most.js Streams
<!-- Write a short summary about your library here -->

## Let me have it!
```sh
npm install --save most-node-streams
```

## API

<!-- Describe your API here -->
64 changes: 64 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "most-node-streams",
"version": "0.0.0",
"description": "Convert Node.js streams to Most.js Streams",
"main": "lib/commonjs",
"scripts": {
"test:lint": "tslint src/**/*.ts src/*.ts",
"test:unit": "TS_NODE_PROJECT=test/tsconfig.json mocha -r ts-node/register test/*.ts",
"test": "npm run test:lint && npm run test:unit",
"commit": "git-cz",
"changelog": "conventional-changelog --infile CHANGELOG.md --same-file --release-count 0 --preset angular",
"postchangelog": "git add CHANGELOG.md && git commit -m 'docs(CHANGELOG): append to changelog'",
"build:es2015": "tsc -P tsconfig.json",
"build:commonjs": "tsc -P tsconfig.commonjs.json",
"build": "npm run build:es2015 && npm run build:commonjs",
"preversion": "npm run build",
"postversion": "npm run changelog && git push origin master --tags && npm publish",
"release:minor": "npm version minor -m 'chore(package): v%s'",
"release:major": "npm version major -m 'chore(package): v%s'"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TylorS/most-node-streams.git"
},
"keywords": [
"most",
"node",
"streams",
"convert"
],
"author": "Tylor Steinberger <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/TylorS/most-node-streams/issues"
},
"homepage": "https://github.com/TylorS/most-node-streams#readme",
"config": {
"ghooks": {
"commit-msg": "node ./node_modules/.bin/validate-commit-msg"
}
},
"jsnext:main": "lib/es2015/index.js",
"module": "lib/es2015/index.js",
"typings": "lib/es2015/index.d.ts",
"devDependencies": {
"@motorcycle/tslint": "^1.2.0",
"@types/mocha": "^2.2.33",
"@types/node": "^6.0.51",
"commitizen": "^2.8.6",
"conventional-changelog-cli": "^1.2.0",
"cz-conventional-changelog": "^1.2.0",
"ghooks": "^1.3.2",
"mocha": "^3.2.0",
"stdio-mock": "^1.0.1",
"ts-node": "^1.7.0",
"tslint": "^4.0.2",
"typescript": "^2.2.0-dev.20161127",
"validate-commit-msg": "^2.8.2"
},
"dependencies": {
"@most/multicast": "^1.2.4",
"most": "^1.1.0"
}
}
39 changes: 39 additions & 0 deletions src/FromStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { never, Disposable, Sink, Scheduler } from 'most';
import { MulticastSource } from '@most/multicast';
import { FromStreamDisposable } from './FromStreamDisposable';
import { addListeners } from './addListeners';
import { NodeStream } from './types';

export class FromStream<T> extends MulticastSource<T> {
private stream: NodeStream;
private endEventName: string;
private dataEventName: string;

// From MulticastSource
private _disposable: Disposable<T>;

constructor(stream: NodeStream, endEventName: string, dataEventName: string) {
super(never().source);

this.stream = stream;
this.endEventName = endEventName;
this.dataEventName = dataEventName;
}

public run(sink: Sink<T>, scheduler: Scheduler) {
const n = this.add(sink);

if (n === 1) {
const stream = this.stream;

this._disposable =
addListeners(stream, this.endEventName, this.dataEventName, sink, scheduler);

if (typeof (stream as NodeJS.ReadableStream).resume === 'function') {
(stream as NodeJS.ReadableStream).resume();
}
}

return new FromStreamDisposable(this, sink);
}
}
22 changes: 22 additions & 0 deletions src/FromStreamDisposable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Sink, Disposable } from 'most';
import { FromStream } from './FromStream';

export class FromStreamDisposable implements Disposable<any> {
private source: FromStream<any>;
private sink: Sink<any>;
private disposed: boolean;

constructor(source: FromStream<any>, sink: Sink<any>) {
this.source = source;
this.sink = sink;
this.disposed = false;
}

public dispose() {
if (this.disposed) return;
this.disposed = true;
const remaining = this.source.remove(this.sink);

return remaining === 0 && (this.source as any)._dispose();
}
}
26 changes: 26 additions & 0 deletions src/addListeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Sink, Scheduler, Disposable, PropagateTask } from 'most';
import { NodeStream } from './types';

export function addListeners(
stream: NodeStream,
endEventName: string,
dataEventName: string,
sink: Sink<any>,
scheduler: Scheduler): Disposable<any>
{
const event = (value: any) => scheduler.asap(PropagateTask.event(value, sink));
const error = (err: Error) => scheduler.asap(PropagateTask.error(err, sink));
const end = (value: any) => scheduler.asap(PropagateTask.end(value, sink));

stream.addListener(dataEventName, event);
stream.addListener(endEventName, end);
stream.addListener('error', error);

return {
dispose() {
stream.removeListener(dataEventName, event);
stream.removeListener(endEventName, end);
stream.removeListener('error', error);
},
};
}
51 changes: 51 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Stream, Subscription } from 'most';
import { FromStream } from './FromStream';
import { NodeStream } from './types';

export interface FromStreamOptions {
endEventName?: string;
dataEventName?: string;
}

export function fromStream(
nodeStream: NodeStream,
options: FromStreamOptions = {})
{
const { endEventName = 'end', dataEventName = 'data' } = options;

if (typeof (nodeStream as NodeJS.ReadableStream).pause === 'function')
(nodeStream as NodeJS.ReadableStream).pause();

return new Stream<any>(new FromStream<any>(nodeStream, endEventName, dataEventName));
}

export function fromReadable(
nodeStream: NodeJS.ReadableStream,
dataEventName: string,
): Stream<Buffer> {
return fromStream(nodeStream, { dataEventName, endEventName: 'end' });
}

export function fromWritable(nodeStream: NodeJS.WritableStream) {
return fromStream(nodeStream, { endEventName: 'finish' });
}

export function toWritable(
nodeStream: NodeJS.WritableStream,
stream: Stream<string | Buffer>): Subscription<any>
{
return stream.subscribe({
next (x: string | Buffer) {
nodeStream.write(x);
},
error (e: Error) {
nodeStream.emit('error', e);
},
complete() {
// process.stdout && process.stderr are not closable
if (!(nodeStream as any).isStdio) {
nodeStream.end();
}
},
});
}
Loading

0 comments on commit 81c3747

Please sign in to comment.