@push.rocks/smartstream 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * autocreated commitinfo by @pushrocks/commitinfo
3
+ */
4
+ export declare const commitinfo: {
5
+ name: string;
6
+ version: string;
7
+ description: string;
8
+ };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * autocreated commitinfo by @pushrocks/commitinfo
3
+ */
4
+ export const commitinfo = {
5
+ name: '@pushrocks/smartstream',
6
+ version: '2.0.3',
7
+ description: 'simplifies access to node streams'
8
+ };
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMDBfY29tbWl0aW5mb19kYXRhLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvMDBfY29tbWl0aW5mb19kYXRhLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHO0lBQ3hCLElBQUksRUFBRSx3QkFBd0I7SUFDOUIsT0FBTyxFQUFFLE9BQU87SUFDaEIsV0FBVyxFQUFFLG1DQUFtQztDQUNqRCxDQUFBIn0=
@@ -0,0 +1,3 @@
1
+ export * from './smartstream.classes.streamwrapper.js';
2
+ export * from './smartstream.classes.streamintake.js';
3
+ export * from './smartstream.duplex.js';
@@ -0,0 +1,4 @@
1
+ export * from './smartstream.classes.streamwrapper.js';
2
+ export * from './smartstream.classes.streamintake.js';
3
+ export * from './smartstream.duplex.js';
4
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLHdDQUF3QyxDQUFDO0FBQ3ZELGNBQWMsdUNBQXVDLENBQUM7QUFDdEQsY0FBYyx5QkFBeUIsQ0FBQyJ9
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import * as plugins from './smartstream.plugins.js';
4
+ export declare class StreamIntake<T> {
5
+ private signalEndBoolean;
6
+ private chunkStore;
7
+ pushNextObservable: plugins.smartrx.ObservableIntake<any>;
8
+ private pushedNextDeferred;
9
+ private readableStream;
10
+ constructor();
11
+ /**
12
+ * returns a new style readble stream
13
+ */
14
+ getReadable(): plugins.stream.Readable;
15
+ /**
16
+ * returns an oldstyle readble stream
17
+ */
18
+ getReadableStream(): NodeJS.ReadableStream;
19
+ pushData(chunkData: T): void;
20
+ signalEnd(): void;
21
+ }
@@ -0,0 +1,56 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+ export class StreamIntake {
3
+ constructor() {
4
+ this.signalEndBoolean = false;
5
+ this.chunkStore = [];
6
+ this.pushNextObservable = new plugins.smartrx.ObservableIntake();
7
+ this.pushedNextDeferred = plugins.smartpromise.defer();
8
+ this.readableStream = plugins.from2.obj(async (size, next) => {
9
+ // console.log('get next');
10
+ // execute without backpressure
11
+ while (this.chunkStore.length > 0) {
12
+ next(null, this.chunkStore.shift());
13
+ }
14
+ if (this.signalEndBoolean) {
15
+ next(null, null);
16
+ }
17
+ // lets trigger backpressure handling
18
+ this.pushNextObservable.push('please push next');
19
+ await this.pushedNextDeferred.promise;
20
+ this.pushedNextDeferred = plugins.smartpromise.defer();
21
+ // execute with backpressure
22
+ while (this.chunkStore.length > 0) {
23
+ next(null, this.chunkStore.shift());
24
+ }
25
+ if (this.signalEndBoolean) {
26
+ next(null, null);
27
+ }
28
+ });
29
+ this.pushNextObservable.push('please push next');
30
+ }
31
+ /**
32
+ * returns a new style readble stream
33
+ */
34
+ getReadable() {
35
+ const readable = new plugins.stream.Readable({
36
+ objectMode: true,
37
+ });
38
+ return readable.wrap(this.readableStream);
39
+ }
40
+ /**
41
+ * returns an oldstyle readble stream
42
+ */
43
+ getReadableStream() {
44
+ return this.readableStream;
45
+ }
46
+ pushData(chunkData) {
47
+ this.chunkStore.push(chunkData);
48
+ this.pushedNextDeferred.resolve();
49
+ }
50
+ signalEnd() {
51
+ this.signalEndBoolean = true;
52
+ this.pushedNextDeferred.resolve();
53
+ this.pushNextObservable.signalComplete();
54
+ }
55
+ }
56
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0uY2xhc3Nlcy5zdHJlYW1pbnRha2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHN0cmVhbS5jbGFzc2VzLnN0cmVhbWludGFrZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLDBCQUEwQixDQUFDO0FBRXBELE1BQU0sT0FBTyxZQUFZO0lBZ0N2QjtRQS9CUSxxQkFBZ0IsR0FBRyxLQUFLLENBQUM7UUFDekIsZUFBVSxHQUFRLEVBQUUsQ0FBQztRQUV0Qix1QkFBa0IsR0FBRyxJQUFJLE9BQU8sQ0FBQyxPQUFPLENBQUMsZ0JBQWdCLEVBQU8sQ0FBQztRQUVoRSx1QkFBa0IsR0FBRyxPQUFPLENBQUMsWUFBWSxDQUFDLEtBQUssRUFBRSxDQUFDO1FBRWxELG1CQUFjLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQyxHQUFHLENBQUMsS0FBSyxFQUFFLElBQUksRUFBRSxJQUFJLEVBQUUsRUFBRTtZQUM5RCwyQkFBMkI7WUFDM0IsK0JBQStCO1lBQy9CLE9BQU8sSUFBSSxDQUFDLFVBQVUsQ0FBQyxNQUFNLEdBQUcsQ0FBQyxFQUFFO2dCQUNqQyxJQUFJLENBQUMsSUFBSSxFQUFFLElBQUksQ0FBQyxVQUFVLENBQUMsS0FBSyxFQUFFLENBQUMsQ0FBQ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sQ0FBQztJQUM3QixDQUFDO0lBRU0sUUFBUSxDQUFDLFNBQVk7UUFDMUIsSUFBSSxDQUFDLFVBQVUsQ0FBQyxJQUFJLENBQUMsU0FBUyxDQUFDLENBQUM7UUFDaEMsSUFBSSxDQUFDLGtCQUFrQixDQUFDLE9BQU8sRUFBRSxDQUFDO0lBQ3BDLENBQUM7SUFFTSxTQUFTO1FBQ2QsSUFBSSxDQUFDLGdCQUFnQixHQUFHLElBQUksQ0FBQztRQUM3QixJQUFJLENBQUMsa0JBQWtCLENBQUMsT0FBTyxFQUFFLENBQUM7UUFDbEMsSUFBSSxDQUFDLGtCQUFrQixDQUFDLGNBQWMsRUFBRSxDQUFDO0lBQzNDLENBQUM7Q0FDRiJ9
@@ -0,0 +1,37 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+ export interface IErrorFunction {
3
+ (err: Error): any;
4
+ }
5
+ export interface ICustomEventFunction {
6
+ (): any;
7
+ }
8
+ export interface ICustomEventObject {
9
+ eventName: string;
10
+ eventFunction: ICustomEventFunction;
11
+ }
12
+ /**
13
+ * class Smartstream handles
14
+ */
15
+ export declare class StreamWrapper {
16
+ private streamArray;
17
+ private customEventObjectArray;
18
+ private streamStartedDeferred;
19
+ /**
20
+ * constructor
21
+ */
22
+ constructor(streamArrayArg: any[]);
23
+ /**
24
+ * make something with the stream itself
25
+ */
26
+ streamStarted(): Promise<any>;
27
+ /**
28
+ * attach listener to custom event
29
+ */
30
+ onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction): void;
31
+ /**
32
+ * run the stream
33
+ * @returns Promise
34
+ */
35
+ run(): Promise<void>;
36
+ }
37
+ export declare let cleanPipe: () => plugins.stream.Transform;
@@ -0,0 +1,81 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+ // interfaces
3
+ import 'stream';
4
+ /**
5
+ * class Smartstream handles
6
+ */
7
+ export class StreamWrapper {
8
+ /**
9
+ * constructor
10
+ */
11
+ constructor(streamArrayArg) {
12
+ this.streamArray = [];
13
+ this.customEventObjectArray = [];
14
+ this.streamStartedDeferred = plugins.smartpromise.defer();
15
+ this.streamArray = streamArrayArg;
16
+ }
17
+ /**
18
+ * make something with the stream itself
19
+ */
20
+ streamStarted() {
21
+ return this.streamStartedDeferred.promise;
22
+ }
23
+ /**
24
+ * attach listener to custom event
25
+ */
26
+ onCustomEvent(eventNameArg, eventFunctionArg) {
27
+ this.customEventObjectArray.push({
28
+ eventName: eventNameArg,
29
+ eventFunction: eventFunctionArg,
30
+ });
31
+ }
32
+ /**
33
+ * run the stream
34
+ * @returns Promise
35
+ */
36
+ run() {
37
+ const done = plugins.smartpromise.defer();
38
+ // clone Array
39
+ const streamExecutionArray = [];
40
+ for (const streamItem of this.streamArray) {
41
+ streamExecutionArray.push(streamItem);
42
+ }
43
+ // combine the stream
44
+ let finalStream = null;
45
+ let firstIteration = true;
46
+ for (const stream of streamExecutionArray) {
47
+ if (firstIteration === true) {
48
+ finalStream = stream;
49
+ }
50
+ stream.on('error', (err) => {
51
+ done.reject(err);
52
+ });
53
+ for (const customEventObject of this.customEventObjectArray) {
54
+ stream.on(customEventObject.eventName, customEventObject.eventFunction);
55
+ }
56
+ if (!firstIteration) {
57
+ finalStream = finalStream.pipe(stream);
58
+ }
59
+ firstIteration = false;
60
+ }
61
+ this.streamStartedDeferred.resolve();
62
+ finalStream.on('end', () => {
63
+ done.resolve();
64
+ });
65
+ finalStream.on('close', () => {
66
+ done.resolve();
67
+ });
68
+ finalStream.on('finish', () => {
69
+ done.resolve();
70
+ });
71
+ return done.promise;
72
+ }
73
+ }
74
+ export let cleanPipe = () => {
75
+ return plugins.through2.obj((file, enc, cb) => {
76
+ cb();
77
+ }, (cb) => {
78
+ cb();
79
+ });
80
+ };
81
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0uY2xhc3Nlcy5zdHJlYW13cmFwcGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZX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FDLENBQUM7UUFDSCxXQUFXLENBQUMsRUFBRSxDQUFDLFFBQVEsRUFBRSxHQUFHLEVBQUU7WUFDNUIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFDO1FBQ2pCLENBQUMsQ0FBQyxDQUFDO1FBQ0gsT0FBTyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3RCLENBQUM7Q0FDRjtBQUVELE1BQU0sQ0FBQyxJQUFJLFNBQVMsR0FBRyxHQUFHLEVBQUU7SUFDMUIsT0FBTyxPQUFPLENBQUMsUUFBUSxDQUFDLEdBQUcsQ0FDekIsQ0FBQyxJQUFJLEVBQUUsR0FBRyxFQUFFLEVBQUUsRUFBRSxFQUFFO1FBQ2hCLEVBQUUsRUFBRSxDQUFDO0lBQ1AsQ0FBQyxFQUNELENBQUMsRUFBRSxFQUFFLEVBQUU7UUFDTCxFQUFFLEVBQUUsQ0FBQztJQUNQLENBQUMsQ0FDRixDQUFDO0FBQ0osQ0FBQyxDQUFDIn0=
@@ -0,0 +1,23 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+ export interface ITruncateFunc {
3
+ (): void;
4
+ }
5
+ export interface IPipeMoreFunc {
6
+ (pipeObject: any): void;
7
+ }
8
+ export interface IStreamTools {
9
+ truncate: ITruncateFunc;
10
+ pipeMore: IPipeMoreFunc;
11
+ }
12
+ export interface IStreamFunction<T, rT> {
13
+ (chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
14
+ }
15
+ export interface IStreamEndFunction<rT> {
16
+ (toolsArg: IStreamTools): Promise<rT>;
17
+ }
18
+ export interface IStreamOptions {
19
+ objectMode?: boolean;
20
+ readableObjectMode?: boolean;
21
+ writableObjectMode?: boolean;
22
+ }
23
+ export declare let createDuplexStream: <T, rT>(funcArg: IStreamFunction<T, rT>, endFuncArg?: IStreamEndFunction<rT>, optionsArg?: IStreamOptions) => plugins.stream.Transform;
@@ -0,0 +1,48 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+ export let createDuplexStream = (funcArg, endFuncArg, optionsArg = {
3
+ objectMode: false,
4
+ readableObjectMode: true,
5
+ writableObjectMode: true,
6
+ }) => {
7
+ return plugins.through2(optionsArg, function (chunk, enc, cb) {
8
+ let truncated = false;
9
+ const tools = {
10
+ truncate: () => {
11
+ truncated = true;
12
+ cb(null, null);
13
+ },
14
+ pipeMore: (pipeObject) => {
15
+ this.push(pipeObject);
16
+ },
17
+ };
18
+ const asyncWrapper = async () => {
19
+ const resultChunk = await funcArg(chunk, tools);
20
+ if (!truncated) {
21
+ cb(null, resultChunk);
22
+ }
23
+ };
24
+ asyncWrapper().catch((err) => {
25
+ console.log(err);
26
+ });
27
+ }, function (cb) {
28
+ const tools = {
29
+ truncate: () => {
30
+ cb();
31
+ },
32
+ pipeMore: (pushArg) => {
33
+ this.push(pushArg);
34
+ },
35
+ };
36
+ const asyncWrapper = async () => {
37
+ if (endFuncArg) {
38
+ const result = await endFuncArg(tools);
39
+ this.push(result);
40
+ }
41
+ cb();
42
+ };
43
+ asyncWrapper().catch((err) => {
44
+ console.log(err);
45
+ });
46
+ });
47
+ };
48
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0uZHVwbGV4LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzdHJlYW0uZHVwbGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sMEJBQTBCLENBQUM7QUE2QnBELE1BQU0sQ0FBQyxJQUFJLGtCQUFrQixHQUFHLENBQzlCLE9BQStCLEVBQy9CLFVBQW1DLEVBQ25DLGFBQTZCO0lBQzNCLFVBQVUsRUFBRSxLQUFLO0lBQ2pCLGtCQUFrQixFQUFFLElBQUk7SUFDeEIsa0JBQWtCLEVBQUUsSUFBSTtDQUN6QixFQUNELEVBQUU7SUFDRixPQUFPLE9BQU8sQ0FBQyxRQU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NyQixDQUFDO1NBQ0YsQ0FBQztRQUNGLE1BQU0sWUFBWSxHQUFHLEtBQUssSUFBSSxFQUFFO1lBQzlCLElBQUksVUFBVSxFQUFFO2dCQUNkLE1BQU0sTUFBTSxHQUFHLE1BQU0sVUFBVSxDQUFDLEtBQUssQ0FBQyxDQUFDO2dCQUN2QyxJQUFJLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxDQUFDO2FBQ25CO1lBQ0QsRUFBRSxFQUFFLENBQUM7UUFDUCxDQUFDLENBQUM7UUFDRixZQUFZLEVBQUUsQ0FBQyxLQUFLLENBQUMsQ0FBQyxHQUFHLEVBQUUsRUFBRTtZQUMzQixPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFDO1FBQ25CLENBQUMsQ0FBQyxDQUFDO0lBQ0wsQ0FBQyxDQUNGLENBQUM7QUFDSixDQUFDLENBQUMifQ==
@@ -0,0 +1,8 @@
1
+ import * as stream from 'stream';
2
+ export { stream };
3
+ import * as smartpromise from '@pushrocks/smartpromise';
4
+ import * as smartrx from '@pushrocks/smartrx';
5
+ export { smartpromise, smartrx };
6
+ import from2 from 'from2';
7
+ import through2 from 'through2';
8
+ export { from2, through2 };
@@ -0,0 +1,12 @@
1
+ // node native
2
+ import * as stream from 'stream';
3
+ export { stream };
4
+ // pushrocks scope
5
+ import * as smartpromise from '@pushrocks/smartpromise';
6
+ import * as smartrx from '@pushrocks/smartrx';
7
+ export { smartpromise, smartrx };
8
+ // thirdparty
9
+ import from2 from 'from2';
10
+ import through2 from 'through2';
11
+ export { from2, through2 };
12
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzdHJlYW0ucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0c3RyZWFtLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYztBQUNkLE9BQU8sS0FBSyxNQUFNLE1BQU0sUUFBUSxDQUFDO0FBRWpDLE9BQU8sRUFBRSxNQUFNLEVBQUUsQ0FBQztBQUVsQixrQkFBa0I7QUFDbEIsT0FBTyxLQUFLLFlBQVksTUFBTSx5QkFBeUIsQ0FBQztBQUN4RCxPQUFPLEtBQUssT0FBTyxNQUFNLG9CQUFvQixDQUFDO0FBRTlDLE9BQU8sRUFBRSxZQUFZLEVBQUUsT0FBTyxFQUFFLENBQUM7QUFFakMsYUFBYTtBQUNiLE9BQU8sS0FBSyxNQUFNLE9BQU8sQ0FBQztBQUMxQixPQUFPLFFBQVEsTUFBTSxVQUFVLENBQUM7QUFFaEMsT0FBTyxFQUFFLEtBQUssRUFBRSxRQUFRLEVBQUUsQ0FBQyJ9
package/npmextra.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "npmci": {
3
+ "npmGlobalTools": [],
4
+ "npmAccessLevel": "public"
5
+ },
6
+ "gitzone": {
7
+ "projectType": "npm",
8
+ "module": {
9
+ "githost": "gitlab.com",
10
+ "gitscope": "pushrocks",
11
+ "gitrepo": "smartstream",
12
+ "description": "simplifies access to node streams",
13
+ "npmPackagename": "@pushrocks/smartstream",
14
+ "license": "MIT"
15
+ }
16
+ }
17
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@push.rocks/smartstream",
3
+ "version": "2.0.3",
4
+ "private": false,
5
+ "description": "simplifies access to node streams",
6
+ "main": "dist_ts/index.js",
7
+ "typings": "dist_ts/index.d.ts",
8
+ "type": "module",
9
+ "scripts": {
10
+ "test": "(tstest test/)",
11
+ "build": "(tsbuild)"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+ssh://git@gitlab.com/pushrocks/smartstream.git"
16
+ },
17
+ "author": "Lossless GmbH",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://gitlab.com/pushrocks/smartstream/issues"
21
+ },
22
+ "homepage": "https://gitlab.com/pushrocks/smartstream#README",
23
+ "devDependencies": {
24
+ "@gitzone/tsbuild": "^2.1.63",
25
+ "@gitzone/tstest": "^1.0.71",
26
+ "@pushrocks/smartfile": "^10.0.0",
27
+ "@pushrocks/tapbundle": "^5.0.3",
28
+ "tslint": "^6.1.3",
29
+ "tslint-config-prettier": "^1.18.0"
30
+ },
31
+ "dependencies": {
32
+ "@pushrocks/smartpromise": "^3.1.7",
33
+ "@pushrocks/smartrx": "^2.0.25",
34
+ "@types/from2": "^2.3.1",
35
+ "@types/through2": "^2.0.36",
36
+ "from2": "^2.3.0",
37
+ "through2": "^4.0.2"
38
+ },
39
+ "browserslist": [
40
+ "last 1 chrome versions"
41
+ ],
42
+ "files": [
43
+ "ts/**/*",
44
+ "ts_web/**/*",
45
+ "dist/**/*",
46
+ "dist_*/**/*",
47
+ "dist_ts/**/*",
48
+ "dist_ts_web/**/*",
49
+ "assets/**/*",
50
+ "cli.js",
51
+ "npmextra.json",
52
+ "readme.md"
53
+ ]
54
+ }
package/readme.md ADDED
@@ -0,0 +1,54 @@
1
+ # @pushrocks/smartstream
2
+ simplifies access to node streams
3
+
4
+ ## Availabililty and Links
5
+ * [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartstream)
6
+ * [gitlab.com (source)](https://gitlab.com/pushrocks/smartstream)
7
+ * [github.com (source mirror)](https://github.com/pushrocks/smartstream)
8
+ * [docs (typedoc)](https://pushrocks.gitlab.io/smartstream/)
9
+
10
+ ## Status for master
11
+
12
+ Status Category | Status Badge
13
+ -- | --
14
+ GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartstream/badges/master/pipeline.svg)](https://lossless.cloud)
15
+ GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartstream/badges/master/coverage.svg)](https://lossless.cloud)
16
+ npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartstream)](https://lossless.cloud)
17
+ Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartstream)](https://lossless.cloud)
18
+ TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
19
+ node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
20
+ Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
21
+ PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartstream)](https://lossless.cloud)
22
+ PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartstream)](https://lossless.cloud)
23
+ BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartstream)](https://lossless.cloud)
24
+ Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
25
+
26
+ ## Usage
27
+
28
+ Use TypeScript for best in class instellisense.
29
+
30
+ ```typescript
31
+ import { Smartstream } from 'smartstream'
32
+ import * as gUglify from 'gulp-uglify'
33
+
34
+ let mySmartstream = new Smartstream([
35
+ gulp.src(['./file1.js','./file2.js']),
36
+ gUglify(),
37
+ gulp.dest('./some/output/path')
38
+ ])
39
+
40
+ mySmartstream.onError((err) => { /* handle error */ }) // handles all errors in stream
41
+ myStream.onCustomEvent('myeventname', (args...) => { /* Do something */ }) // emit an custom event anywhere in your stream
42
+ mySmartstream.run().then(() => {/* do something when stream is finished */})
43
+ ```
44
+
45
+ ## Contribution
46
+
47
+ 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). :)
48
+
49
+ For further information read the linked docs at the top of this readme.
50
+
51
+ > MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
52
+ | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
53
+
54
+ [![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
@@ -0,0 +1,8 @@
1
+ /**
2
+ * autocreated commitinfo by @pushrocks/commitinfo
3
+ */
4
+ export const commitinfo = {
5
+ name: '@pushrocks/smartstream',
6
+ version: '2.0.3',
7
+ description: 'simplifies access to node streams'
8
+ }
package/ts/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './smartstream.classes.streamwrapper.js';
2
+ export * from './smartstream.classes.streamintake.js';
3
+ export * from './smartstream.duplex.js';
@@ -0,0 +1,66 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+
3
+ export class StreamIntake<T> {
4
+ private signalEndBoolean = false;
5
+ private chunkStore: T[] = [];
6
+
7
+ public pushNextObservable = new plugins.smartrx.ObservableIntake<any>();
8
+
9
+ private pushedNextDeferred = plugins.smartpromise.defer();
10
+
11
+ private readableStream = plugins.from2.obj(async (size, next) => {
12
+ // console.log('get next');
13
+ // execute without backpressure
14
+ while (this.chunkStore.length > 0) {
15
+ next(null, this.chunkStore.shift());
16
+ }
17
+ if (this.signalEndBoolean) {
18
+ next(null, null);
19
+ }
20
+
21
+ // lets trigger backpressure handling
22
+ this.pushNextObservable.push('please push next');
23
+ await this.pushedNextDeferred.promise;
24
+ this.pushedNextDeferred = plugins.smartpromise.defer();
25
+
26
+ // execute with backpressure
27
+ while (this.chunkStore.length > 0) {
28
+ next(null, this.chunkStore.shift());
29
+ }
30
+ if (this.signalEndBoolean) {
31
+ next(null, null);
32
+ }
33
+ });
34
+
35
+ constructor() {
36
+ this.pushNextObservable.push('please push next');
37
+ }
38
+
39
+ /**
40
+ * returns a new style readble stream
41
+ */
42
+ public getReadable() {
43
+ const readable = new plugins.stream.Readable({
44
+ objectMode: true,
45
+ });
46
+ return readable.wrap(this.readableStream);
47
+ }
48
+
49
+ /**
50
+ * returns an oldstyle readble stream
51
+ */
52
+ public getReadableStream() {
53
+ return this.readableStream;
54
+ }
55
+
56
+ public pushData(chunkData: T) {
57
+ this.chunkStore.push(chunkData);
58
+ this.pushedNextDeferred.resolve();
59
+ }
60
+
61
+ public signalEnd() {
62
+ this.signalEndBoolean = true;
63
+ this.pushedNextDeferred.resolve();
64
+ this.pushNextObservable.signalComplete();
65
+ }
66
+ }
@@ -0,0 +1,107 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+
3
+ // interfaces
4
+ import { Transform } from 'stream';
5
+
6
+ export interface IErrorFunction {
7
+ (err: Error): any;
8
+ }
9
+
10
+ export interface ICustomEventFunction {
11
+ (): any;
12
+ }
13
+
14
+ export interface ICustomEventObject {
15
+ eventName: string;
16
+ eventFunction: ICustomEventFunction;
17
+ }
18
+
19
+ /**
20
+ * class Smartstream handles
21
+ */
22
+ export class StreamWrapper {
23
+ private streamArray: Array<plugins.stream.Duplex> = [];
24
+ private customEventObjectArray: ICustomEventObject[] = [];
25
+ private streamStartedDeferred = plugins.smartpromise.defer();
26
+
27
+ /**
28
+ * constructor
29
+ */
30
+ constructor(streamArrayArg: any[]) {
31
+ this.streamArray = streamArrayArg;
32
+ }
33
+
34
+ /**
35
+ * make something with the stream itself
36
+ */
37
+ streamStarted(): Promise<any> {
38
+ return this.streamStartedDeferred.promise;
39
+ }
40
+
41
+ /**
42
+ * attach listener to custom event
43
+ */
44
+ onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) {
45
+ this.customEventObjectArray.push({
46
+ eventName: eventNameArg,
47
+ eventFunction: eventFunctionArg,
48
+ });
49
+ }
50
+
51
+ /**
52
+ * run the stream
53
+ * @returns Promise
54
+ */
55
+ run(): Promise<void> {
56
+ const done = plugins.smartpromise.defer<void>();
57
+
58
+ // clone Array
59
+ const streamExecutionArray: Array<plugins.stream.Duplex> = [];
60
+ for (const streamItem of this.streamArray) {
61
+ streamExecutionArray.push(streamItem);
62
+ }
63
+
64
+ // combine the stream
65
+ let finalStream = null;
66
+ let firstIteration: boolean = true;
67
+ for (const stream of streamExecutionArray) {
68
+ if (firstIteration === true) {
69
+ finalStream = stream;
70
+ }
71
+ stream.on('error', (err) => {
72
+ done.reject(err);
73
+ });
74
+ for (const customEventObject of this.customEventObjectArray) {
75
+ stream.on(customEventObject.eventName, customEventObject.eventFunction);
76
+ }
77
+ if (!firstIteration) {
78
+ finalStream = finalStream.pipe(stream);
79
+ }
80
+ firstIteration = false;
81
+ }
82
+
83
+ this.streamStartedDeferred.resolve();
84
+
85
+ finalStream.on('end', () => {
86
+ done.resolve();
87
+ });
88
+ finalStream.on('close', () => {
89
+ done.resolve();
90
+ });
91
+ finalStream.on('finish', () => {
92
+ done.resolve();
93
+ });
94
+ return done.promise;
95
+ }
96
+ }
97
+
98
+ export let cleanPipe = () => {
99
+ return plugins.through2.obj(
100
+ (file, enc, cb) => {
101
+ cb();
102
+ },
103
+ (cb) => {
104
+ cb();
105
+ }
106
+ );
107
+ };
@@ -0,0 +1,83 @@
1
+ import * as plugins from './smartstream.plugins.js';
2
+
3
+ export interface ITruncateFunc {
4
+ (): void;
5
+ }
6
+
7
+ export interface IPipeMoreFunc {
8
+ (pipeObject: any): void;
9
+ }
10
+
11
+ export interface IStreamTools {
12
+ truncate: ITruncateFunc;
13
+ pipeMore: IPipeMoreFunc;
14
+ }
15
+
16
+ export interface IStreamFunction<T, rT> {
17
+ (chunkArg: T, toolsArg: IStreamTools): Promise<rT>;
18
+ }
19
+
20
+ export interface IStreamEndFunction<rT> {
21
+ (toolsArg: IStreamTools): Promise<rT>;
22
+ }
23
+
24
+ export interface IStreamOptions {
25
+ objectMode?: boolean;
26
+ readableObjectMode?: boolean;
27
+ writableObjectMode?: boolean;
28
+ }
29
+
30
+ export let createDuplexStream = <T, rT>(
31
+ funcArg: IStreamFunction<T, rT>,
32
+ endFuncArg?: IStreamEndFunction<rT>,
33
+ optionsArg: IStreamOptions = {
34
+ objectMode: false,
35
+ readableObjectMode: true,
36
+ writableObjectMode: true,
37
+ }
38
+ ) => {
39
+ return plugins.through2(
40
+ optionsArg,
41
+ function (chunk, enc, cb) {
42
+ let truncated = false;
43
+ const tools: IStreamTools = {
44
+ truncate: () => {
45
+ truncated = true;
46
+ cb(null, null);
47
+ },
48
+ pipeMore: (pipeObject) => {
49
+ this.push(pipeObject);
50
+ },
51
+ };
52
+ const asyncWrapper = async () => {
53
+ const resultChunk: rT = await funcArg(chunk, tools);
54
+ if (!truncated) {
55
+ cb(null, resultChunk);
56
+ }
57
+ };
58
+ asyncWrapper().catch((err) => {
59
+ console.log(err);
60
+ });
61
+ },
62
+ function (cb) {
63
+ const tools: IStreamTools = {
64
+ truncate: () => {
65
+ cb();
66
+ },
67
+ pipeMore: (pushArg) => {
68
+ this.push(pushArg);
69
+ },
70
+ };
71
+ const asyncWrapper = async () => {
72
+ if (endFuncArg) {
73
+ const result = await endFuncArg(tools);
74
+ this.push(result);
75
+ }
76
+ cb();
77
+ };
78
+ asyncWrapper().catch((err) => {
79
+ console.log(err);
80
+ });
81
+ }
82
+ );
83
+ };
@@ -0,0 +1,16 @@
1
+ // node native
2
+ import * as stream from 'stream';
3
+
4
+ export { stream };
5
+
6
+ // pushrocks scope
7
+ import * as smartpromise from '@pushrocks/smartpromise';
8
+ import * as smartrx from '@pushrocks/smartrx';
9
+
10
+ export { smartpromise, smartrx };
11
+
12
+ // thirdparty
13
+ import from2 from 'from2';
14
+ import through2 from 'through2';
15
+
16
+ export { from2, through2 };