1
+ /*<replacement>*/
2
+ var bufferShim = require ( 'safe-buffer' ) . Buffer ;
3
+ /*</replacement>*/
4
+
5
+ var common = require ( '../common' ) ;
6
+ var assert = require ( 'assert/' ) ;
7
+ var stream = require ( '../../' ) ;
8
+
9
+ // ensure consistency between the finish event when using cork()
10
+ // and writev and when not using them
11
+
12
+ {
13
+ var writable = new stream . Writable ( ) ;
14
+
15
+ writable . _write = function ( chunks , encoding , cb ) {
16
+ cb ( new Error ( 'write test error' ) ) ;
17
+ } ;
18
+
19
+ var firstError = false ;
20
+ writable . on ( 'finish' , common . mustCall ( function ( ) {
21
+ assert . strictEqual ( firstError , true ) ;
22
+ } ) ) ;
23
+
24
+ writable . on ( 'prefinish' , common . mustCall ( ) ) ;
25
+
26
+ writable . on ( 'error' , common . mustCall ( function ( er ) {
27
+ assert . strictEqual ( er . message , 'write test error' ) ;
28
+ firstError = true ;
29
+ } ) ) ;
30
+
31
+ writable . end ( 'test' ) ;
32
+ }
33
+
34
+ {
35
+ var _writable = new stream . Writable ( ) ;
36
+
37
+ _writable . _write = function ( chunks , encoding , cb ) {
38
+ setImmediate ( cb , new Error ( 'write test error' ) ) ;
39
+ } ;
40
+
41
+ var _firstError = false ;
42
+ _writable . on ( 'finish' , common . mustCall ( function ( ) {
43
+ assert . strictEqual ( _firstError , true ) ;
44
+ } ) ) ;
45
+
46
+ _writable . on ( 'prefinish' , common . mustCall ( ) ) ;
47
+
48
+ _writable . on ( 'error' , common . mustCall ( function ( er ) {
49
+ assert . strictEqual ( er . message , 'write test error' ) ;
50
+ _firstError = true ;
51
+ } ) ) ;
52
+
53
+ _writable . end ( 'test' ) ;
54
+ }
55
+
56
+ {
57
+ var _writable2 = new stream . Writable ( ) ;
58
+
59
+ _writable2 . _write = function ( chunks , encoding , cb ) {
60
+ cb ( new Error ( 'write test error' ) ) ;
61
+ } ;
62
+
63
+ _writable2 . _writev = function ( chunks , cb ) {
64
+ cb ( new Error ( 'writev test error' ) ) ;
65
+ } ;
66
+
67
+ var _firstError2 = false ;
68
+ _writable2 . on ( 'finish' , common . mustCall ( function ( ) {
69
+ assert . strictEqual ( _firstError2 , true ) ;
70
+ } ) ) ;
71
+
72
+ _writable2 . on ( 'prefinish' , common . mustCall ( ) ) ;
73
+
74
+ _writable2 . on ( 'error' , common . mustCall ( function ( er ) {
75
+ assert . strictEqual ( er . message , 'writev test error' ) ;
76
+ _firstError2 = true ;
77
+ } ) ) ;
78
+
79
+ _writable2 . cork ( ) ;
80
+ _writable2 . write ( 'test' ) ;
81
+
82
+ setImmediate ( function ( ) {
83
+ _writable2 . end ( 'test' ) ;
84
+ } ) ;
85
+ }
86
+
87
+ {
88
+ var _writable3 = new stream . Writable ( ) ;
89
+
90
+ _writable3 . _write = function ( chunks , encoding , cb ) {
91
+ setImmediate ( cb , new Error ( 'write test error' ) ) ;
92
+ } ;
93
+
94
+ _writable3 . _writev = function ( chunks , cb ) {
95
+ setImmediate ( cb , new Error ( 'writev test error' ) ) ;
96
+ } ;
97
+
98
+ var _firstError3 = false ;
99
+ _writable3 . on ( 'finish' , common . mustCall ( function ( ) {
100
+ assert . strictEqual ( _firstError3 , true ) ;
101
+ } ) ) ;
102
+
103
+ _writable3 . on ( 'prefinish' , common . mustCall ( ) ) ;
104
+
105
+ _writable3 . on ( 'error' , common . mustCall ( function ( er ) {
106
+ assert . strictEqual ( er . message , 'writev test error' ) ;
107
+ _firstError3 = true ;
108
+ } ) ) ;
109
+
110
+ _writable3 . cork ( ) ;
111
+ _writable3 . write ( 'test' ) ;
112
+
113
+ setImmediate ( function ( ) {
114
+ _writable3 . end ( 'test' ) ;
115
+ } ) ;
116
+ }
117
+
118
+ // Regression test for
119
+ // https://github.com/nodejs/node/issues/13812
120
+
121
+ {
122
+ var rs = new stream . Readable ( ) ;
123
+ rs . push ( 'ok' ) ;
124
+ rs . push ( null ) ;
125
+ rs . _read = function ( ) { } ;
126
+
127
+ var ws = new stream . Writable ( ) ;
128
+ var _firstError4 = false ;
129
+
130
+ ws . on ( 'finish' , common . mustCall ( function ( ) {
131
+ assert . strictEqual ( _firstError4 , true ) ;
132
+ } ) ) ;
133
+ ws . on ( 'error' , common . mustCall ( function ( ) {
134
+ _firstError4 = true ;
135
+ } ) ) ;
136
+
137
+ ws . _write = function ( chunk , encoding , done ) {
138
+ setImmediate ( done , new Error ( ) ) ;
139
+ } ;
140
+ rs . pipe ( ws ) ;
141
+ }
142
+
143
+ {
144
+ var _rs = new stream . Readable ( ) ;
145
+ _rs . push ( 'ok' ) ;
146
+ _rs . push ( null ) ;
147
+ _rs . _read = function ( ) { } ;
148
+
149
+ var _ws = new stream . Writable ( ) ;
150
+
151
+ _ws . on ( 'finish' , common . mustNotCall ( ) ) ;
152
+ _ws . on ( 'error' , common . mustCall ( ) ) ;
153
+
154
+ _ws . _write = function ( chunk , encoding , done ) {
155
+ done ( new Error ( ) ) ;
156
+ } ;
157
+ _rs . pipe ( _ws ) ;
158
+ }
0 commit comments