@@ -11,110 +11,111 @@ function strictEqual (a, b) {
11
11
addEventListener ( "fetch" , async ( event ) => {
12
12
let cnt = 0 ;
13
13
const stream = new ReadableStream ( {
14
- // type: 'bytes',
15
- pull ( controller ) {
14
+ type : 'bytes' ,
15
+ autoAllocateChunkSize : 1024 ,
16
+ async pull ( controller ) {
17
+ if ( cnt === 0 ) {
18
+ {
19
+ let cnt = 0 ;
20
+ const stream = new ReadableStream ( {
21
+ type : 'bytes' ,
22
+ autoAllocateChunkSize : 1024 ,
23
+ start ( controller ) {
24
+ } ,
25
+ pull ( controller ) {
26
+ cnt ++ ;
27
+ const view = controller . byobRequest ?. view ;
28
+ if ( cnt < 3 ) {
29
+ view [ 0 ] = 1 ;
30
+ view [ 1 ] = 2 ;
31
+ view [ 2 ] = 3 ;
32
+ controller . byobRequest . respond ( 3 ) ;
33
+ } else if ( cnt == 3 ) {
34
+ controller . enqueue ( new Uint8Array ( [ 1 , 2 , 4 ] ) ) ;
35
+ }
36
+ if ( cnt === 3 )
37
+ return controller . close ( ) ;
38
+ }
39
+ } ) ;
40
+
41
+ const byobReader = stream . getReader ( ) ;
42
+ let buf ;
43
+ let result ;
44
+ let offset = 0 ;
45
+ while ( true ) {
46
+ if ( buf && cnt < 3 ) {
47
+ const output = new Uint8Array ( buf ) ;
48
+ strictEqual ( output [ 0 ] , 1 ) ;
49
+ strictEqual ( output [ 1 ] , 2 ) ;
50
+ strictEqual ( output [ 2 ] , 3 ) ;
51
+ strictEqual ( output [ 3 ] , 0 ) ;
52
+ strictEqual ( output . byteLength , 1024 ) ;
53
+ }
54
+ result = await byobReader . read ( ) ;
55
+ if ( result . done )
56
+ break ;
57
+ offset += result . value . byteLength ;
58
+ buf = result . value . buffer ;
59
+ }
60
+
61
+ strictEqual ( cnt , 3 ) ;
62
+ const output = new Uint8Array ( buf ) ;
63
+ strictEqual ( output [ 0 ] , 1 ) ;
64
+ strictEqual ( output [ 1 ] , 2 ) ;
65
+ strictEqual ( output [ 2 ] , 4 ) ;
66
+ strictEqual ( output [ 3 ] , undefined ) ;
67
+ strictEqual ( output . byteLength , 3 ) ;
68
+ console . log ( 'Passed 1' ) ;
69
+ }
70
+
71
+ {
72
+ let cnt = 0 ;
73
+ const stream = new ReadableStream ( {
74
+ type : 'bytes' ,
75
+ start ( controller ) {
76
+ } ,
77
+ pull ( controller ) {
78
+ cnt ++ ;
79
+ const view = controller . byobRequest ?. view ;
80
+ if ( view ) {
81
+ view [ 0 ] = 1 ;
82
+ view [ 1 ] = 2 ;
83
+ view [ 2 ] = 3 ;
84
+ controller . byobRequest . respond ( 3 ) ;
85
+ }
86
+ if ( cnt === 3 )
87
+ return controller . close ( ) ;
88
+ }
89
+ } ) ;
90
+
91
+ const byobReader = stream . getReader ( { mode : 'byob' } ) ;
92
+ let buf = new ArrayBuffer ( 10 ) ;
93
+ let result ;
94
+ let offset = 0 ;
95
+ do {
96
+ result = await byobReader . read ( new Uint8Array ( buf , offset ) ) ;
97
+ offset += result . value . byteLength ;
98
+ buf = result . value . buffer ;
99
+ } while ( ! result . done ) ;
100
+
101
+ const out = new Uint8Array ( buf ) ;
102
+ if ( out [ 0 ] === 1 && out [ 1 ] === 2 && out [ 2 ] === 3 && out [ 3 ] === 1 && out [ 4 ] === 2 && out [ 5 ] === 3 && out [ 6 ] === 1 && out [ 7 ] === 2 && out [ 8 ] === 3 ) {
103
+ console . log ( 'Passed 2' ) ;
104
+ }
105
+ }
106
+ }
107
+
16
108
cnt ++ ;
17
- const view = controller . byobRequest ? .view ;
109
+ const view = controller . byobRequest . view ;
18
110
if ( view ) {
19
111
view [ 0 ] = 104 ;
20
112
view [ 1 ] = 101 ;
21
113
view [ 2 ] = 121 ;
22
114
controller . byobRequest . respond ( 3 ) ;
23
- } else {
24
- controller . enqueue ( new Uint8Array ( [ 104 , 101 , 106 ] ) ) ;
25
115
}
26
116
if ( cnt === 3 )
27
117
return controller . close ( ) ;
28
118
}
29
119
} ) ;
30
120
event . respondWith ( new Response ( stream ) ) ;
31
-
32
- {
33
- let cnt = 0 ;
34
- const stream = new ReadableStream ( {
35
- type : 'bytes' ,
36
- autoAllocateChunkSize : 1024 ,
37
- start ( controller ) {
38
- } ,
39
- pull ( controller ) {
40
- cnt ++ ;
41
- const view = controller . byobRequest ?. view ;
42
- if ( cnt < 3 ) {
43
- view [ 0 ] = 1 ;
44
- view [ 1 ] = 2 ;
45
- view [ 2 ] = 3 ;
46
- controller . byobRequest . respond ( 3 ) ;
47
- } else if ( cnt == 3 ) {
48
- controller . enqueue ( new Uint8Array ( [ 1 , 2 , 4 ] ) ) ;
49
- }
50
- if ( cnt === 3 )
51
- return controller . close ( ) ;
52
- }
53
- } ) ;
54
-
55
- const byobReader = stream . getReader ( ) ;
56
- let buf ;
57
- let result ;
58
- let offset = 0 ;
59
- while ( true ) {
60
- if ( buf && cnt < 3 ) {
61
- const output = new Uint8Array ( buf ) ;
62
- strictEqual ( output [ 0 ] , 1 ) ;
63
- strictEqual ( output [ 1 ] , 2 ) ;
64
- strictEqual ( output [ 2 ] , 3 ) ;
65
- strictEqual ( output [ 3 ] , 0 ) ;
66
- strictEqual ( output . byteLength , 1024 ) ;
67
- }
68
- result = await byobReader . read ( ) ;
69
- if ( result . done )
70
- break ;
71
- offset += result . value . byteLength ;
72
- buf = result . value . buffer ;
73
- }
74
-
75
- strictEqual ( cnt , 3 ) ;
76
- const output = new Uint8Array ( buf ) ;
77
- strictEqual ( output [ 0 ] , 1 ) ;
78
- strictEqual ( output [ 1 ] , 2 ) ;
79
- strictEqual ( output [ 2 ] , 4 ) ;
80
- strictEqual ( output [ 3 ] , undefined ) ;
81
- strictEqual ( output . byteLength , 3 ) ;
82
- console . log ( 'Passed 1' ) ;
83
- }
84
-
85
- {
86
- let cnt = 0 ;
87
- const stream = new ReadableStream ( {
88
- type : 'bytes' ,
89
- start ( controller ) {
90
- } ,
91
- pull ( controller ) {
92
- cnt ++ ;
93
- const view = controller . byobRequest ?. view ;
94
- if ( view ) {
95
- view [ 0 ] = 1 ;
96
- view [ 1 ] = 2 ;
97
- view [ 2 ] = 3 ;
98
- controller . byobRequest . respond ( 3 ) ;
99
- }
100
- if ( cnt === 3 )
101
- return controller . close ( ) ;
102
- }
103
- } ) ;
104
-
105
- const byobReader = stream . getReader ( { mode : 'byob' } ) ;
106
- let buf = new ArrayBuffer ( 10 ) ;
107
- let result ;
108
- let offset = 0 ;
109
- do {
110
- result = await byobReader . read ( new Uint8Array ( buf , offset ) ) ;
111
- offset += result . value . byteLength ;
112
- buf = result . value . buffer ;
113
- } while ( ! result . done ) ;
114
-
115
- const out = new Uint8Array ( buf ) ;
116
- if ( out [ 0 ] === 1 && out [ 1 ] === 2 && out [ 2 ] === 3 && out [ 3 ] === 1 && out [ 4 ] === 2 && out [ 5 ] === 3 && out [ 6 ] === 1 && out [ 7 ] === 2 && out [ 8 ] === 3 ) {
117
- console . log ( 'Passed 2' ) ;
118
- }
119
- }
120
121
} ) ;
0 commit comments