File tree 2 files changed +105
-0
lines changed
2 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const pipeline = require ( 'internal/streams/pipeline' ) ;
4
+ const Duplex = require ( 'internal/streams/duplex' ) ;
5
+ const { destroyer } = require ( 'internal/streams/destroy' ) ;
6
+
7
+ module . exports = function pipe ( ...streams ) {
8
+ let ondrain ;
9
+ let onfinish ;
10
+ let onreadable ;
11
+ let onclose ;
12
+ let ret ;
13
+
14
+ const r = pipeline ( streams , function ( err ) {
15
+ if ( onclose ) {
16
+ const cb = onclose ;
17
+ onclose = null ;
18
+ cb ( err ) ;
19
+ } else {
20
+ ret . destroy ( err ) ;
21
+ }
22
+ } ) ;
23
+ const w = streams [ 0 ] ;
24
+
25
+ const writable = w . writable ;
26
+ const readable = r . readable ;
27
+ const objectMode = w . readableObjectMode ;
28
+
29
+ ret = new Duplex ( {
30
+ writable,
31
+ readable,
32
+ objectMode,
33
+ highWaterMark : 1
34
+ } ) ;
35
+
36
+ if ( writable ) {
37
+ // TODO (ronag): Can we avoid double buffering?
38
+
39
+ ret . _write = function ( chunk , encoding , callback ) {
40
+ if ( w . write ( chunk , encoding ) ) {
41
+ callback ( ) ;
42
+ } else {
43
+ ondrain = callback ;
44
+ }
45
+ } ;
46
+
47
+ ret . _final = function ( callback ) {
48
+ w . end ( ) ;
49
+ onfinish = callback ;
50
+ } ;
51
+
52
+ ret . on ( 'drain' , function ( ) {
53
+ if ( ondrain ) {
54
+ const cb = ondrain ;
55
+ ondrain = null ;
56
+ cb ( ) ;
57
+ }
58
+ } ) ;
59
+
60
+ ret . on ( 'finish' , function ( ) {
61
+ if ( onfinish ) {
62
+ const cb = onfinish ;
63
+ onfinish = null ;
64
+ cb ( ) ;
65
+ }
66
+ } ) ;
67
+ }
68
+
69
+ if ( readable ) {
70
+ // TODO (ronag): Can we avoid double buffering?
71
+
72
+ r . on ( 'readable' , function ( ) {
73
+ if ( onreadable ) {
74
+ const cb = onreadable ;
75
+ onreadable = null ;
76
+ cb ( ) ;
77
+ }
78
+ } ) ;
79
+
80
+ ret . _read = function ( ) {
81
+ while ( true ) {
82
+ const buf = r . read ( ) ;
83
+
84
+ if ( buf === null ) {
85
+ onreadable = ret . _read ;
86
+ return ;
87
+ }
88
+
89
+ if ( ! ret . push ( buf ) ) {
90
+ return ;
91
+ }
92
+ }
93
+ } ;
94
+ }
95
+
96
+ ret . _destroy = function ( err , callback ) {
97
+ onclose = callback ;
98
+ onreadable = null ;
99
+ ondrain = null ;
100
+ onfinish = null ;
101
+ destroyer ( r , err ) ;
102
+ } ;
103
+ } ;
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ const {
30
30
} = require ( 'internal/util' ) ;
31
31
32
32
const pipeline = require ( 'internal/streams/pipeline' ) ;
33
+ const pipelinify = require ( 'internal/streams/pipelinify' ) ;
33
34
const eos = require ( 'internal/streams/end-of-stream' ) ;
34
35
const internalBuffer = require ( 'internal/buffer' ) ;
35
36
@@ -42,6 +43,7 @@ Stream.Duplex = require('internal/streams/duplex');
42
43
Stream . Transform = require ( 'internal/streams/transform' ) ;
43
44
Stream . PassThrough = require ( 'internal/streams/passthrough' ) ;
44
45
Stream . pipeline = pipeline ;
46
+ Stream . pipelinify = pipelinify ;
45
47
const { addAbortSignal } = require ( 'internal/streams/add-abort-signal' ) ;
46
48
Stream . addAbortSignal = addAbortSignal ;
47
49
Stream . finished = eos ;
You can’t perform that action at this time.
0 commit comments