Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
15a226d30d | |||
16c5c89662 | |||
851a96c014 | |||
4ea42cb9fb | |||
41eed6423d | |||
0e067004a4 | |||
9fe222b500 | |||
05e9067a34 | |||
2aff46eb0e | |||
6aa4b86598 | |||
af30268551 | |||
e562e8f099 | |||
01f4a53b5b | |||
f42b77986f | |||
b7ef295757 | |||
2818420ee9 | |||
d759e2a562 | |||
65a97c9ee0 | |||
b036bfcb92 | |||
c0304d1d10 | |||
26880f9b71 | |||
aa7c30a096 | |||
083673f02c | |||
93bcc03e72 | |||
f8b79710be | |||
addf20995a | |||
5401e5e008 | |||
144295f7c0 | |||
1e0557b722 | |||
9b67507acd | |||
c7b3f2a228 | |||
084a47096d | |||
224b39f0a6 | |||
b24b564495 |
20
.gitignore
vendored
20
.gitignore
vendored
@ -1,4 +1,20 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
coverage/
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
156
.gitlab-ci.yml
156
.gitlab-ci.yml
@ -1,42 +1,140 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
# gitzone ci_default
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
|
cache:
|
||||||
|
paths:
|
||||||
|
- .npmci_cache/
|
||||||
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- security
|
||||||
- release
|
- test
|
||||||
- page
|
- release
|
||||||
testLEGACY:
|
- metadata
|
||||||
stage: test
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
|
mirror:
|
||||||
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci test legacy
|
- npmci git mirror
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
testLTS:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci test lts
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci test stable
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
release:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- npmci publish
|
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
pages:
|
- notpriv
|
||||||
image: hosttoday/ht-docker-node:npmpage
|
|
||||||
stage: page
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npmpage --host gitlab
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
|
|
||||||
|
testStable:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
|
release:
|
||||||
|
stage: release
|
||||||
|
script:
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm publish
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# metadata stage
|
||||||
|
# ====================
|
||||||
|
codequality:
|
||||||
|
stage: metadata
|
||||||
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
script:
|
||||||
|
- npmci command npm install -g typescript
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
trigger:
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci trigger
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
|
pages:
|
||||||
|
stage: metadata
|
||||||
|
script:
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci command npm install -g @git.zone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command tsdoc
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- notpriv
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"command": "npm test",
|
||||||
|
"name": "Run npm test",
|
||||||
|
"request": "launch",
|
||||||
|
"type": "node-terminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
36
README.md
36
README.md
@ -1,36 +0,0 @@
|
|||||||
# smartstream
|
|
||||||
simplifies access to node streams, TypeScript ready!
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
[](https://www.npmjs.com/package/smartstream)
|
|
||||||
[](https://gitlab.com/pushrocks/smartstream)
|
|
||||||
[](https://github.com/pushrocks/smartstream)
|
|
||||||
[](https://pushrocks.gitlab.io/smartstream/gitbook)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
[](https://gitlab.com/pushrocks/smartstream/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartstream/commits/master)
|
|
||||||
[](https://david-dm.org/pushrocks/smartstream)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartstream/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/smartstream)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
We recommend the use of TypeScript for best in class intellisense support.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Smartstream } from 'smartstream'
|
|
||||||
import * as gUglify from 'gulp-uglify'
|
|
||||||
|
|
||||||
let mySmartstream = new Smartstream([
|
|
||||||
gulp.src(['./file1.js','./file2.js']),
|
|
||||||
gUglify(),
|
|
||||||
gulp.dest('./some/output/path')
|
|
||||||
])
|
|
||||||
|
|
||||||
mySmartstream.onError((err) => {/* handle error */}) // handles all errors in stream
|
|
||||||
mySmartstream.run().then(() => {/* do something when stream is finished */})
|
|
||||||
```
|
|
||||||
|
|
38
dist/index.d.ts
vendored
38
dist/index.d.ts
vendored
@ -1,38 +0,0 @@
|
|||||||
/// <reference types="q" />
|
|
||||||
import * as plugins from './smartstream.plugins';
|
|
||||||
export interface IErrorFunction {
|
|
||||||
(err: any): any;
|
|
||||||
}
|
|
||||||
export interface IStreamStartFunction {
|
|
||||||
(stream: any): any;
|
|
||||||
}
|
|
||||||
export interface ICustomEventFunction {
|
|
||||||
(): any;
|
|
||||||
}
|
|
||||||
export interface ICustomEventObject {
|
|
||||||
eventName: string;
|
|
||||||
eventFunction: ICustomEventFunction;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* class Smartstream handles
|
|
||||||
*/
|
|
||||||
export declare class Smartstream {
|
|
||||||
streamArray: any[];
|
|
||||||
errorFunction: IErrorFunction;
|
|
||||||
streamStartFunction: IStreamStartFunction;
|
|
||||||
customEventObjectArray: ICustomEventObject[];
|
|
||||||
constructor(streamArrayArg: any[]);
|
|
||||||
/**
|
|
||||||
* attach an error handler to the stream to prevent throwing
|
|
||||||
*/
|
|
||||||
onError(errorFunctionArg: IErrorFunction): void;
|
|
||||||
/**
|
|
||||||
* make something with the stream itself
|
|
||||||
*/
|
|
||||||
onStreamStart(): plugins.q.Promise<any>;
|
|
||||||
/**
|
|
||||||
* run the stream
|
|
||||||
* @returns Promise
|
|
||||||
*/
|
|
||||||
run(): plugins.q.Promise<void>;
|
|
||||||
}
|
|
67
dist/index.js
vendored
67
dist/index.js
vendored
@ -1,67 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
const plugins = require("./smartstream.plugins");
|
|
||||||
/**
|
|
||||||
* class Smartstream handles
|
|
||||||
*/
|
|
||||||
class Smartstream {
|
|
||||||
constructor(streamArrayArg) {
|
|
||||||
this.streamArray = [];
|
|
||||||
this.errorFunction = null;
|
|
||||||
this.streamStartFunction = null;
|
|
||||||
this.customEventObjectArray = [];
|
|
||||||
this.streamArray = streamArrayArg;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* attach an error handler to the stream to prevent throwing
|
|
||||||
*/
|
|
||||||
onError(errorFunctionArg) {
|
|
||||||
this.errorFunction = errorFunctionArg;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* make something with the stream itself
|
|
||||||
*/
|
|
||||||
onStreamStart() {
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* run the stream
|
|
||||||
* @returns Promise
|
|
||||||
*/
|
|
||||||
run() {
|
|
||||||
let done = plugins.q.defer();
|
|
||||||
// clone Array
|
|
||||||
let streamExecutionArray = [];
|
|
||||||
for (let streamItem of this.streamArray) {
|
|
||||||
streamExecutionArray.push(streamItem);
|
|
||||||
}
|
|
||||||
// combine the stream
|
|
||||||
let finalStream = null;
|
|
||||||
let firstIteration = true;
|
|
||||||
for (let stream of streamExecutionArray) {
|
|
||||||
if (firstIteration === true) {
|
|
||||||
finalStream = stream;
|
|
||||||
}
|
|
||||||
if (this.errorFunction !== null) {
|
|
||||||
stream.on('error', this.errorFunction);
|
|
||||||
}
|
|
||||||
for (let customEventObject of this.customEventObjectArray) {
|
|
||||||
stream.on(customEventObject.eventName, customEventObject.eventFunction);
|
|
||||||
}
|
|
||||||
if (!firstIteration) {
|
|
||||||
finalStream = finalStream.pipe(stream);
|
|
||||||
}
|
|
||||||
firstIteration = false;
|
|
||||||
}
|
|
||||||
finalStream.on('end', function () {
|
|
||||||
done.resolve();
|
|
||||||
});
|
|
||||||
finalStream.on('close', function () {
|
|
||||||
done.resolve();
|
|
||||||
});
|
|
||||||
finalStream.on('finish', function () {
|
|
||||||
done.resolve();
|
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.Smartstream = Smartstream;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsaURBQWdEO0FBbUJoRDs7R0FFRztBQUNIO0lBS0ksWUFBWSxjQUFxQjtRQUpqQyxnQkFBVyxHQUFHLEVBQUUsQ0FBQTtRQUNoQixrQkFBYSxHQUFtQixJQUFJLENBQUE7UUFDcEMsd0JBQW1CLEdBQXlCLElBQUksQ0FBQTtRQUNoRCwyQkFBc0IsR0FBeUIsRUFBRSxDQUFBO1FBRTdDLElBQUksQ0FBQyxXQUFXLEdBQUcsY0FBYyxDQUFBO0lBQ3JDLENBQUM7SUFFRDs7T0FFRztJQUNILE9BQU8sQ0FBQyxnQkFBZ0M7UUFDcEMsSUFBSSxDQUFDLGFBQWEsR0FBRyxnQkFBZ0IsQ0FBQTtJQUN6QyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxhQUFhO0lBRWIsQ0FBQztJQUVEOzs7T0FHRztJQUNILEdBQUc7UUFDQyxJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBUSxDQUFBO1FBRWxDLGNBQWM7UUFDZCxJQUFJLG9CQUFvQixHQUFHLEVBQUUsQ0FBQTtRQUM3QixHQUFHLENBQUMsQ0FBQyxJQUFJLFVBQVUsSUFBSSxJQUFJLENBQUMsV0FBVyxDQUFDLENBQUMsQ0FBQztZQUFDLG9CQUFvQixDQUFDLElBQUksQ0FBQyxVQUFVLENBQUMsQ0FBQTtRQUFDLENBQUM7UUFFbEYscUJBQXFCO1FBQ3JCLElBQUksV0FBVyxHQUFHLElBQUksQ0FBQTtRQUN0QixJQUFJLGNBQWMsR0FBWSxJQUFJLENBQUE7UUFDbEMsR0FBRyxDQUFDLENBQUMsSUFBSSxNQUFNLElBQUksb0JBQW9CLENBQUMsQ0FBQyxDQUFDO1lBQ3RDLEVBQUUsQ0FBQyxDQUFDLGNBQWMsS0FBSyxJQUFJLENBQUMsQ0FBQyxDQUFDO2dCQUMxQixXQUFXLEdBQUcsTUFBTSxDQUFBO1lBQ3hCLENBQUM7WUFDRCxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsYUFBYSxLQUFLLElBQUksQ0FBQyxDQUFDLENBQUM7Z0JBQzlCLE1BQU0sQ0FBQyxFQUFFLENBQUMsT0FBTyxFQUFFLElBQUksQ0FBQyxhQUFhLENBQUMsQ0FBQTtZQUMxQyxDQUFDO1lBQ0QsR0FBRyxDQUFDLENBQUMsSUFBSSxpQkFBaUIsSUFBSSxJQUFJLENBQUMsc0JBQXNCLENBQUMsQ0FBQyxDQUFDO2dCQUN4RCxNQUFNLENBQUMsRUFBRSxDQUFDLGlCQUFpQixDQUFDLFNBQVMsRUFBRSxpQkFBaUIsQ0FBQyxhQUFhLENBQUMsQ0FBQTtZQUMzRSxDQUFDO1lBQ0QsRUFBRSxDQUFDLENBQUMsQ0FBQyxjQUFjLENBQUMsQ0FBQyxDQUFDO2dCQUNsQixXQUFXLEdBQUcsV0FBVyxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsQ0FBQTtZQUMxQyxDQUFDO1lBQ0QsY0FBYyxHQUFHLEtBQUssQ0FBQTtRQUMxQixDQUFDO1FBQ0QsV0FBVyxDQUFDLEVBQUUsQ0FBQyxLQUFLLEVBQUM7WUFDakIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsV0FBVyxDQUFDLEVBQUUsQ0FBQyxPQUFPLEVBQUM7WUFDbkIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsV0FBVyxDQUFDLEVBQUUsQ0FBQyxRQUFRLEVBQUM7WUFDcEIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ2xCLENBQUMsQ0FBQyxDQUFBO1FBQ0YsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztDQUNKO0FBL0RELGtDQStEQyJ9
|
|
2
dist/smartstream.plugins.d.ts
vendored
2
dist/smartstream.plugins.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
import 'typings-global';
|
|
||||||
export import q = require('q');
|
|
4
dist/smartstream.plugins.js
vendored
4
dist/smartstream.plugins.js
vendored
@ -1,4 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
exports.q = require("q");
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0ucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c3RyZWFtLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUN2Qix5QkFBOEIifQ==
|
|
@ -1 +1,17 @@
|
|||||||
{}
|
{
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "push.rocks",
|
||||||
|
"gitrepo": "smartstream",
|
||||||
|
"description": "simplifies access to node streams",
|
||||||
|
"npmPackagename": "@push.rocks/smartstream",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
package.json
50
package.json
@ -1,11 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "smartstream",
|
"name": "@push.rocks/smartstream",
|
||||||
"version": "1.0.3",
|
"version": "2.0.5",
|
||||||
"description": "simplifies access to node streams, TypeScript ready!",
|
"private": false,
|
||||||
"main": "dist/index.js",
|
"description": "simplifies access to node streams",
|
||||||
"typings": "dist/index.d.ts",
|
"main": "dist_ts/index.js",
|
||||||
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)"
|
"test": "(tstest test/)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -18,14 +21,33 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartstream#README",
|
"homepage": "https://gitlab.com/pushrocks/smartstream#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/should": "^8.1.30",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"npmts-g": "^5.2.8",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"should": "^11.1.0",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"typings-test": "^1.0.3"
|
"@push.rocks/smartfile": "^10.0.33",
|
||||||
|
"@push.rocks/tapbundle": "^5.0.15"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/q": "0.x.x",
|
"@push.rocks/smartpromise": "^4.0.3",
|
||||||
"q": "^1.4.1",
|
"@push.rocks/smartrx": "^3.0.7",
|
||||||
"typings-global": "^1.0.14"
|
"@types/from2": "^2.3.4",
|
||||||
}
|
"@types/through2": "^2.0.40",
|
||||||
|
"from2": "^2.3.0",
|
||||||
|
"through2": "^4.0.2"
|
||||||
|
},
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
5705
pnpm-lock.yaml
generated
Normal file
5705
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
54
readme.md
Normal file
54
readme.md
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# @pushrocks/smartstream
|
||||||
|
simplifies access to node streams
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartstream)
|
||||||
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartstream)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartstream)
|
||||||
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartstream/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
|
|
||||||
|
Status Category | Status Badge
|
||||||
|
-- | --
|
||||||
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
|
npm | [](https://lossless.cloud)
|
||||||
|
Snyk | [](https://lossless.cloud)
|
||||||
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Smartstream } from 'smartstream'
|
||||||
|
import * as gUglify from 'gulp-uglify'
|
||||||
|
|
||||||
|
let mySmartstream = new Smartstream([
|
||||||
|
gulp.src(['./file1.js','./file2.js']),
|
||||||
|
gUglify(),
|
||||||
|
gulp.dest('./some/output/path')
|
||||||
|
])
|
||||||
|
|
||||||
|
mySmartstream.onError((err) => { /* handle error */ }) // handles all errors in stream
|
||||||
|
myStream.onCustomEvent('myeventname', (args...) => { /* Do something */ }) // emit an custom event anywhere in your stream
|
||||||
|
mySmartstream.run().then(() => {/* do something when stream is finished */})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contribution
|
||||||
|
|
||||||
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
6210
test/assets/readabletext.txt
Normal file
6210
test/assets/readabletext.txt
Normal file
File diff suppressed because it is too large
Load Diff
51
test/assets/writabletext.txt
Normal file
51
test/assets/writabletext.txt
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
hi+wow
|
||||||
|
noice
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -1 +0,0 @@
|
|||||||
import 'typings-test';
|
|
18
test/test.js
18
test/test.js
@ -1,18 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
const fs = require("fs");
|
|
||||||
const smartstream = require("../dist/index");
|
|
||||||
let testSmartstream;
|
|
||||||
describe('smartstream', function () {
|
|
||||||
it('should combine a stream', function (done) {
|
|
||||||
this.timeout(5000);
|
|
||||||
testSmartstream = new smartstream.Smartstream([
|
|
||||||
fs.createReadStream('./test/assets/test.md'),
|
|
||||||
fs.createWriteStream('./test/assets/testCopy.md')
|
|
||||||
]);
|
|
||||||
testSmartstream.run().then(() => {
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQix5QkFBeUI7QUFHekIsNkNBQTRDO0FBRTVDLElBQUksZUFBd0MsQ0FBQTtBQUU1QyxRQUFRLENBQUMsYUFBYSxFQUFFO0lBQ3BCLEVBQUUsQ0FBQyx5QkFBeUIsRUFBRSxVQUFTLElBQUk7UUFDdkMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUNsQixlQUFlLEdBQUcsSUFBSSxXQUFXLENBQUMsV0FBVyxDQUFDO1lBQzFDLEVBQUUsQ0FBQyxnQkFBZ0IsQ0FBQyx1QkFBdUIsQ0FBQztZQUM1QyxFQUFFLENBQUMsaUJBQWlCLENBQUMsMkJBQTJCLENBQUM7U0FDcEQsQ0FBQyxDQUFBO1FBQ0YsZUFBZSxDQUFDLEdBQUcsRUFBRSxDQUFDLElBQUksQ0FBQztZQUN2QixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9
|
|
44
test/test.smartstream.ts
Normal file
44
test/test.smartstream.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
|
import { SmartStream } from '../ts/smartstream.classes.smartstream.js'; // Adjust the import to your file structure
|
||||||
|
import * as smartrx from '@push.rocks/smartrx';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
|
tap.test('should create a SmartStream from a Buffer', async () => {
|
||||||
|
const bufferData = Buffer.from('This is a test buffer');
|
||||||
|
const smartStream = SmartStream.fromBuffer(bufferData);
|
||||||
|
|
||||||
|
let receivedData = Buffer.alloc(0);
|
||||||
|
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
smartStream.on('data', (chunk: Buffer) => {
|
||||||
|
receivedData = Buffer.concat([receivedData, chunk]);
|
||||||
|
});
|
||||||
|
|
||||||
|
smartStream.on('end', () => {
|
||||||
|
expect(receivedData.toString()).toEqual(bufferData.toString());
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a SmartStream from an Observable', async () => {
|
||||||
|
const observableData = 'Observable test data';
|
||||||
|
const testObservable = smartrx.rxjs.of(Buffer.from(observableData));
|
||||||
|
|
||||||
|
const smartStream = SmartStream.fromObservable(testObservable);
|
||||||
|
|
||||||
|
let receivedData = Buffer.alloc(0);
|
||||||
|
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
smartStream.on('data', (chunk: Buffer) => {
|
||||||
|
receivedData = Buffer.concat([receivedData, chunk]);
|
||||||
|
});
|
||||||
|
|
||||||
|
smartStream.on('end', () => {
|
||||||
|
expect(receivedData.toString()).toEqual(observableData);
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
68
test/test.streamfunction.ts
Normal file
68
test/test.streamfunction.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
|
|
||||||
|
import * as smartstream from '../ts/index.js';
|
||||||
|
|
||||||
|
let testIntake: smartstream.StreamIntake<string>;
|
||||||
|
|
||||||
|
tap.test('should handle a read stream', async (tools) => {
|
||||||
|
const counter = 0;
|
||||||
|
const testSmartstream = new smartstream.StreamWrapper([
|
||||||
|
smartfile.fsStream.createReadStream('./test/assets/readabletext.txt'),
|
||||||
|
smartstream.createDuplexStream<Buffer, Buffer>(
|
||||||
|
async (chunkStringArg: Buffer, streamTools) => {
|
||||||
|
// do something with the stream here
|
||||||
|
const result = chunkStringArg.toString().substr(0, 100);
|
||||||
|
streamTools.pipeMore('wow =========== \n');
|
||||||
|
return Buffer.from(result);
|
||||||
|
},
|
||||||
|
async (tools) => {
|
||||||
|
// tools.pipeMore('hey, this is the end')
|
||||||
|
return Buffer.from('this is the end');
|
||||||
|
},
|
||||||
|
{ objectMode: false }
|
||||||
|
),
|
||||||
|
smartstream.createDuplexStream<Buffer, string>(async (chunkStringArg) => {
|
||||||
|
console.log(chunkStringArg.toString());
|
||||||
|
return null;
|
||||||
|
}),
|
||||||
|
smartstream.cleanPipe(),
|
||||||
|
]);
|
||||||
|
await testSmartstream.run();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a valid Intake', async (tools) => {
|
||||||
|
testIntake = new smartstream.StreamIntake<string>();
|
||||||
|
testIntake
|
||||||
|
.getReadable()
|
||||||
|
.pipe(
|
||||||
|
smartstream.createDuplexStream<string, string>(
|
||||||
|
async (chunkString) => {
|
||||||
|
await tools.delayFor(100);
|
||||||
|
console.log(chunkString);
|
||||||
|
return chunkString;
|
||||||
|
},
|
||||||
|
async () => {
|
||||||
|
return 'noice';
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.pipe(smartfile.fsStream.createWriteStream('./test/assets/writabletext.txt'));
|
||||||
|
const testFinished = tools.defer();
|
||||||
|
let counter = 0;
|
||||||
|
testIntake.pushNextObservable.subscribe(() => {
|
||||||
|
if (counter < 50) {
|
||||||
|
counter++;
|
||||||
|
testIntake.pushData('hi');
|
||||||
|
testIntake.pushData('+wow');
|
||||||
|
testIntake.pushData('\n');
|
||||||
|
} else {
|
||||||
|
testIntake.signalEnd();
|
||||||
|
testFinished.resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await testFinished.promise;
|
||||||
|
testIntake.signalEnd();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
29
test/test.ts
29
test/test.ts
@ -1,20 +1,15 @@
|
|||||||
import 'typings-test'
|
import * as smartfile from '@push.rocks/smartfile';
|
||||||
import fs = require('fs')
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
import * as should from 'should'
|
|
||||||
|
|
||||||
import * as smartstream from '../dist/index'
|
import * as smartstream from '../ts/smartstream.classes.streamwrapper.js';
|
||||||
|
|
||||||
let testSmartstream: smartstream.Smartstream
|
let testSmartstream: smartstream.StreamWrapper;
|
||||||
|
tap.test('should combine a stream', async () => {
|
||||||
|
testSmartstream = new smartstream.StreamWrapper([
|
||||||
|
smartfile.fsStream.createReadStream('./test/assets/test.md'),
|
||||||
|
smartfile.fsStream.createWriteStream('./test/assets/testCopy.md'),
|
||||||
|
]);
|
||||||
|
await testSmartstream.run();
|
||||||
|
});
|
||||||
|
|
||||||
describe('smartstream', function() {
|
tap.start();
|
||||||
it('should combine a stream', function(done){
|
|
||||||
this.timeout(5000)
|
|
||||||
testSmartstream = new smartstream.Smartstream([
|
|
||||||
fs.createReadStream('./test/assets/test.md'),
|
|
||||||
fs.createWriteStream('./test/assets/testCopy.md')
|
|
||||||
])
|
|
||||||
testSmartstream.run().then(() => {
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@push.rocks/smartstream',
|
||||||
|
version: '2.0.5',
|
||||||
|
description: 'simplifies access to node streams'
|
||||||
|
}
|
90
ts/index.ts
90
ts/index.ts
@ -1,86 +1,4 @@
|
|||||||
import * as plugins from './smartstream.plugins'
|
export * from './smartstream.classes.smartstream.js';
|
||||||
|
export * from './smartstream.classes.streamwrapper.js';
|
||||||
export interface IErrorFunction {
|
export * from './smartstream.classes.streamintake.js';
|
||||||
(err): any
|
export * from './smartstream.duplex.js';
|
||||||
}
|
|
||||||
|
|
||||||
export interface IStreamStartFunction {
|
|
||||||
(stream): any
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICustomEventFunction {
|
|
||||||
(): any
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICustomEventObject {
|
|
||||||
eventName: string
|
|
||||||
eventFunction: ICustomEventFunction
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class Smartstream handles
|
|
||||||
*/
|
|
||||||
export class Smartstream {
|
|
||||||
streamArray = []
|
|
||||||
errorFunction: IErrorFunction = null
|
|
||||||
streamStartFunction: IStreamStartFunction = null
|
|
||||||
customEventObjectArray: ICustomEventObject[] = []
|
|
||||||
constructor(streamArrayArg: any[]) {
|
|
||||||
this.streamArray = streamArrayArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* attach an error handler to the stream to prevent throwing
|
|
||||||
*/
|
|
||||||
onError(errorFunctionArg: IErrorFunction) {
|
|
||||||
this.errorFunction = errorFunctionArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* make something with the stream itself
|
|
||||||
*/
|
|
||||||
onStreamStart(): plugins.q.Promise<any> {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* run the stream
|
|
||||||
* @returns Promise
|
|
||||||
*/
|
|
||||||
run(): plugins.q.Promise<void> {
|
|
||||||
let done = plugins.q.defer<void>()
|
|
||||||
|
|
||||||
// clone Array
|
|
||||||
let streamExecutionArray = []
|
|
||||||
for (let streamItem of this.streamArray) { streamExecutionArray.push(streamItem) }
|
|
||||||
|
|
||||||
// combine the stream
|
|
||||||
let finalStream = null
|
|
||||||
let firstIteration: boolean = true
|
|
||||||
for (let stream of streamExecutionArray) {
|
|
||||||
if (firstIteration === true) {
|
|
||||||
finalStream = stream
|
|
||||||
}
|
|
||||||
if (this.errorFunction !== null) {
|
|
||||||
stream.on('error', this.errorFunction)
|
|
||||||
}
|
|
||||||
for (let customEventObject of this.customEventObjectArray) {
|
|
||||||
stream.on(customEventObject.eventName, customEventObject.eventFunction)
|
|
||||||
}
|
|
||||||
if (!firstIteration) {
|
|
||||||
finalStream = finalStream.pipe(stream)
|
|
||||||
}
|
|
||||||
firstIteration = false
|
|
||||||
}
|
|
||||||
finalStream.on('end',function(){
|
|
||||||
done.resolve()
|
|
||||||
})
|
|
||||||
finalStream.on('close',function(){
|
|
||||||
done.resolve()
|
|
||||||
})
|
|
||||||
finalStream.on('finish',function(){
|
|
||||||
done.resolve()
|
|
||||||
})
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
}
|
|
55
ts/smartstream.classes.smartstream.ts
Normal file
55
ts/smartstream.classes.smartstream.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
import * as plugins from './smartstream.plugins.js';
|
||||||
|
import { Duplex, type DuplexOptions } from 'stream';
|
||||||
|
|
||||||
|
export class SmartStream extends Duplex {
|
||||||
|
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
|
||||||
|
|
||||||
|
constructor(options?: DuplexOptions) {
|
||||||
|
super(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
_read(size: number) {
|
||||||
|
// Implement if you need custom behavior, otherwise leave it empty
|
||||||
|
}
|
||||||
|
|
||||||
|
_write(chunk: any, encoding: string, callback: (error?: Error | null) => void) {
|
||||||
|
// Implement if you need custom behavior
|
||||||
|
callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromBuffer(buffer: Buffer, options?: DuplexOptions): SmartStream {
|
||||||
|
const smartStream = new SmartStream(options);
|
||||||
|
process.nextTick(() => {
|
||||||
|
smartStream.push(buffer);
|
||||||
|
smartStream.push(null); // Signal the end of the data
|
||||||
|
});
|
||||||
|
return smartStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromObservable(observable: plugins.smartrx.rxjs.Observable<any>, options?: DuplexOptions): SmartStream {
|
||||||
|
const smartStream = new SmartStream(options);
|
||||||
|
smartStream.observableSubscription = observable.subscribe({
|
||||||
|
next: (data) => {
|
||||||
|
if (!smartStream.push(data)) {
|
||||||
|
// Pause the observable if the stream buffer is full
|
||||||
|
smartStream.observableSubscription?.unsubscribe();
|
||||||
|
smartStream.once('drain', () => {
|
||||||
|
// Resume the observable when the stream buffer is drained
|
||||||
|
smartStream.observableSubscription?.unsubscribe();
|
||||||
|
smartStream.observableSubscription = observable.subscribe(data => {
|
||||||
|
smartStream.push(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: (err) => {
|
||||||
|
smartStream.emit('error', err);
|
||||||
|
},
|
||||||
|
complete: () => {
|
||||||
|
smartStream.push(null); // Signal the end of the data
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return smartStream;
|
||||||
|
}
|
||||||
|
}
|
66
ts/smartstream.classes.streamintake.ts
Normal file
66
ts/smartstream.classes.streamintake.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import * as plugins from './smartstream.plugins.js';
|
||||||
|
|
||||||
|
export class StreamIntake<T> {
|
||||||
|
private signalEndBoolean = false;
|
||||||
|
private chunkStore: T[] = [];
|
||||||
|
|
||||||
|
public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
|
||||||
|
|
||||||
|
private pushedNextDeferred = plugins.smartpromise.defer();
|
||||||
|
|
||||||
|
private readableStream = plugins.from2.obj(async (size, next) => {
|
||||||
|
// console.log('get next');
|
||||||
|
// execute without backpressure
|
||||||
|
while (this.chunkStore.length > 0) {
|
||||||
|
next(null, this.chunkStore.shift());
|
||||||
|
}
|
||||||
|
if (this.signalEndBoolean) {
|
||||||
|
next(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets trigger backpressure handling
|
||||||
|
this.pushNextObservable.push('please push next');
|
||||||
|
await this.pushedNextDeferred.promise;
|
||||||
|
this.pushedNextDeferred = plugins.smartpromise.defer();
|
||||||
|
|
||||||
|
// execute with backpressure
|
||||||
|
while (this.chunkStore.length > 0) {
|
||||||
|
next(null, this.chunkStore.shift());
|
||||||
|
}
|
||||||
|
if (this.signalEndBoolean) {
|
||||||
|
next(null, null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.pushNextObservable.push('please push next');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns a new style readble stream
|
||||||
|
*/
|
||||||
|
public getReadable() {
|
||||||
|
const readable = new plugins.stream.Readable({
|
||||||
|
objectMode: true,
|
||||||
|
});
|
||||||
|
return readable.wrap(this.readableStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns an oldstyle readble stream
|
||||||
|
*/
|
||||||
|
public getReadableStream() {
|
||||||
|
return this.readableStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public pushData(chunkData: T) {
|
||||||
|
this.chunkStore.push(chunkData);
|
||||||
|
this.pushedNextDeferred.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
public signalEnd() {
|
||||||
|
this.signalEndBoolean = true;
|
||||||
|
this.pushedNextDeferred.resolve();
|
||||||
|
this.pushNextObservable.signalComplete();
|
||||||
|
}
|
||||||
|
}
|
107
ts/smartstream.classes.streamwrapper.ts
Normal file
107
ts/smartstream.classes.streamwrapper.ts
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import * as plugins from './smartstream.plugins.js';
|
||||||
|
|
||||||
|
// interfaces
|
||||||
|
import { Transform } from 'stream';
|
||||||
|
|
||||||
|
export interface IErrorFunction {
|
||||||
|
(err: Error): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICustomEventFunction {
|
||||||
|
(): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICustomEventObject {
|
||||||
|
eventName: string;
|
||||||
|
eventFunction: ICustomEventFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class Smartstream handles
|
||||||
|
*/
|
||||||
|
export class StreamWrapper {
|
||||||
|
private streamArray: Array<plugins.stream.Duplex> = [];
|
||||||
|
private customEventObjectArray: ICustomEventObject[] = [];
|
||||||
|
private streamStartedDeferred = plugins.smartpromise.defer();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constructor
|
||||||
|
*/
|
||||||
|
constructor(streamArrayArg: any[]) {
|
||||||
|
this.streamArray = streamArrayArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* make something with the stream itself
|
||||||
|
*/
|
||||||
|
streamStarted(): Promise<any> {
|
||||||
|
return this.streamStartedDeferred.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* attach listener to custom event
|
||||||
|
*/
|
||||||
|
onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) {
|
||||||
|
this.customEventObjectArray.push({
|
||||||
|
eventName: eventNameArg,
|
||||||
|
eventFunction: eventFunctionArg,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* run the stream
|
||||||
|
* @returns Promise
|
||||||
|
*/
|
||||||
|
run(): Promise<void> {
|
||||||
|
const done = plugins.smartpromise.defer<void>();
|
||||||
|
|
||||||
|
// clone Array
|
||||||
|
const streamExecutionArray: Array<plugins.stream.Duplex> = [];
|
||||||
|
for (const streamItem of this.streamArray) {
|
||||||
|
streamExecutionArray.push(streamItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// combine the stream
|
||||||
|
let finalStream = null;
|
||||||
|
let firstIteration: boolean = true;
|
||||||
|
for (const stream of streamExecutionArray) {
|
||||||
|
if (firstIteration === true) {
|
||||||
|
finalStream = stream;
|
||||||
|
}
|
||||||
|
stream.on('error', (err) => {
|
||||||
|
done.reject(err);
|
||||||
|
});
|
||||||
|
for (const customEventObject of this.customEventObjectArray) {
|
||||||
|
stream.on(customEventObject.eventName, customEventObject.eventFunction);
|
||||||
|
}
|
||||||
|
if (!firstIteration) {
|
||||||
|
finalStream = finalStream.pipe(stream);
|
||||||
|
}
|
||||||
|
firstIteration = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.streamStartedDeferred.resolve();
|
||||||
|
|
||||||
|
finalStream.on('end', () => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
finalStream.on('close', () => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
finalStream.on('finish', () => {
|
||||||
|
done.resolve();
|
||||||
|
});
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export let cleanPipe = () => {
|
||||||
|
return plugins.through2.obj(
|
||||||
|
(file, enc, cb) => {
|
||||||
|
cb();
|
||||||
|
},
|
||||||
|
(cb) => {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
83
ts/smartstream.duplex.ts
Normal file
83
ts/smartstream.duplex.ts
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import * as plugins from './smartstream.plugins.js';
|
||||||
|
|
||||||
|
export interface ITruncateFunc {
|
||||||
|
(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IPipeMoreFunc {
|
||||||
|
(pipeObject: any): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IStreamTools {
|
||||||
|
truncate: ITruncateFunc;
|
||||||
|
pipeMore: IPipeMoreFunc;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IStreamFunction<T, rT> {
|
||||||
|
(chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IStreamEndFunction<rT> {
|
||||||
|
(toolsArg: IStreamTools): Promise<rT>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IStreamOptions {
|
||||||
|
objectMode?: boolean;
|
||||||
|
readableObjectMode?: boolean;
|
||||||
|
writableObjectMode?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export let createDuplexStream = <T, rT>(
|
||||||
|
funcArg: IStreamFunction<T, rT>,
|
||||||
|
endFuncArg?: IStreamEndFunction<rT>,
|
||||||
|
optionsArg: IStreamOptions = {
|
||||||
|
objectMode: false,
|
||||||
|
readableObjectMode: true,
|
||||||
|
writableObjectMode: true,
|
||||||
|
}
|
||||||
|
) => {
|
||||||
|
return plugins.through2(
|
||||||
|
optionsArg,
|
||||||
|
function (chunk, enc, cb) {
|
||||||
|
let truncated = false;
|
||||||
|
const tools: IStreamTools = {
|
||||||
|
truncate: () => {
|
||||||
|
truncated = true;
|
||||||
|
cb(null, null);
|
||||||
|
},
|
||||||
|
pipeMore: (pipeObject) => {
|
||||||
|
this.push(pipeObject);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const asyncWrapper = async () => {
|
||||||
|
const resultChunk: rT = await funcArg(chunk, tools);
|
||||||
|
if (!truncated) {
|
||||||
|
cb(null, resultChunk);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
asyncWrapper().catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function (cb) {
|
||||||
|
const tools: IStreamTools = {
|
||||||
|
truncate: () => {
|
||||||
|
cb();
|
||||||
|
},
|
||||||
|
pipeMore: (pushArg) => {
|
||||||
|
this.push(pushArg);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const asyncWrapper = async () => {
|
||||||
|
if (endFuncArg) {
|
||||||
|
const result = await endFuncArg(tools);
|
||||||
|
this.push(result);
|
||||||
|
}
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
asyncWrapper().catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
@ -1,2 +1,16 @@
|
|||||||
import 'typings-global'
|
// node native
|
||||||
export import q = require('q')
|
import * as stream from 'stream';
|
||||||
|
|
||||||
|
export { stream };
|
||||||
|
|
||||||
|
// pushrocks scope
|
||||||
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
|
import * as smartrx from '@push.rocks/smartrx';
|
||||||
|
|
||||||
|
export { smartpromise, smartrx };
|
||||||
|
|
||||||
|
// thirdparty
|
||||||
|
import from2 from 'from2';
|
||||||
|
import through2 from 'through2';
|
||||||
|
|
||||||
|
export { from2, through2 };
|
||||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "tslint-config-standard"
|
|
||||||
}
|
|
Reference in New Issue
Block a user