20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
'use strict' ;
22
22
23
- require ( '../common' ) ;
23
+ const common = require ( '../common' ) ;
24
24
const assert = require ( 'assert' ) ;
25
25
const Duplex = require ( 'stream' ) . Duplex ;
26
+ const { ReadableStream, WritableStream } = require ( 'stream/web' ) ;
26
27
27
28
const stream = new Duplex ( { objectMode : true } ) ;
28
29
@@ -53,3 +54,80 @@ process.on('exit', () => {
53
54
assert . strictEqual ( read . val , 1 ) ;
54
55
assert . strictEqual ( written . val , 2 ) ;
55
56
} ) ;
57
+
58
+ // Duplex.fromWeb
59
+ {
60
+ const dataToRead = Buffer . from ( 'hello' ) ;
61
+ const dataToWrite = Buffer . from ( 'world' ) ;
62
+
63
+ const stream = new ReadableStream ( {
64
+ start ( controller ) {
65
+ controller . enqueue ( dataToRead ) ;
66
+ } ,
67
+ } ) ;
68
+
69
+ const writable = new WritableStream ( {
70
+ write : common . mustCall ( ( chunk ) => {
71
+ assert . strictEqual ( chunk , dataToWrite ) ;
72
+ } )
73
+ } ) ;
74
+
75
+ const pair = { readable : stream , writable : writable } ;
76
+ const duplex = Duplex . fromWeb ( pair ) ;
77
+
78
+ duplex . write ( dataToWrite ) ;
79
+ duplex . once ( 'data' , common . mustCall ( ( chunk ) => {
80
+ assert . strictEqual ( chunk , dataToRead ) ;
81
+ } ) ) ;
82
+ }
83
+
84
+ // Duplex.fromWeb - using utf8 and objectMode
85
+ {
86
+ const dataToRead = 'hello' ;
87
+ const dataToWrite = 'world' ;
88
+
89
+ const stream = new ReadableStream ( {
90
+ start ( controller ) {
91
+ controller . enqueue ( dataToRead ) ;
92
+ } ,
93
+ } ) ;
94
+
95
+ const writable = new WritableStream ( {
96
+ write : common . mustCall ( ( chunk ) => {
97
+ assert . strictEqual ( chunk , dataToWrite ) ;
98
+ } )
99
+ } ) ;
100
+
101
+ const pair = {
102
+ readable : stream ,
103
+ writable : writable
104
+ } ;
105
+ const duplex = Duplex . fromWeb ( pair , { encoding : 'utf8' , objectMode : true } ) ;
106
+
107
+ duplex . write ( dataToWrite ) ;
108
+ duplex . once ( 'data' , common . mustCall ( ( chunk ) => {
109
+ assert . strictEqual ( chunk , dataToRead ) ;
110
+ } ) ) ;
111
+ }
112
+ // Duplex.toWeb
113
+ {
114
+ const dataToRead = Buffer . from ( 'hello' ) ;
115
+ const dataToWrite = Buffer . from ( 'world' ) ;
116
+
117
+ const duplex = Duplex ( {
118
+ read ( ) {
119
+ this . push ( dataToRead )
120
+ this . push ( null )
121
+ } ,
122
+ write : common . mustCall ( ( chunk ) => {
123
+ assert . strictEqual ( chunk , dataToWrite )
124
+ } )
125
+ } )
126
+
127
+ const webDuplex = Duplex . toWeb ( duplex ) ;
128
+ webDuplex . writable . getWriter ( ) . write ( dataToWrite ) ;
129
+
130
+ webDuplex . readable . getReader ( ) . read ( ) . then ( common . mustCall ( ( result ) => {
131
+ assert . deepStrictEqual ( Buffer . from ( result . value ) , dataToRead ) ;
132
+ } ) ) ;
133
+ }
0 commit comments