File tree Expand file tree Collapse file tree 3 files changed +21
-6
lines changed Expand file tree Collapse file tree 3 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -231,12 +231,9 @@ class EventSource extends EventTarget {
231
231
232
232
// 14. Let processEventSourceEndOfBody given response res be the following step: if res is not a network error, then reestablish the connection.
233
233
const processEventSourceEndOfBody = ( response ) => {
234
- if ( isNetworkError ( response ) ) {
235
- this . dispatchEvent ( new Event ( 'error' ) )
236
- this . close ( )
234
+ if ( ! isNetworkError ( response ) ) {
235
+ return this . #reconnect( )
237
236
}
238
-
239
- this . #reconnect( )
240
237
}
241
238
242
239
// 15. Fetch request, with processResponseEndOfBody set to processEventSourceEndOfBody...
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ function isASCIINumber (value) {
26
26
// https://github.com/nodejs/undici/issues/2664
27
27
function delay ( ms ) {
28
28
return new Promise ( ( resolve ) => {
29
- setTimeout ( resolve , ms ) . unref ( )
29
+ setTimeout ( resolve , ms )
30
30
} )
31
31
}
32
32
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ const events = require('node:events')
5
5
const http = require ( 'node:http' )
6
6
const { test, describe } = require ( 'node:test' )
7
7
const { EventSource } = require ( '../../lib/web/eventsource/eventsource' )
8
+ const { randomInt } = require ( 'node:crypto' )
8
9
9
10
describe ( 'EventSource - sending correct request headers' , ( ) => {
10
11
test ( 'should send request with connection keep-alive' , async ( ) => {
@@ -181,4 +182,21 @@ describe('EventSource - received response must have content-type to be text/even
181
182
server . close ( )
182
183
}
183
184
} )
185
+
186
+ test ( 'should try to connect again if server is unreachable' , async ( ) => {
187
+ const domain = 'bad.n' + randomInt ( 1e10 ) . toString ( 36 ) + '.proxy'
188
+
189
+ const eventSourceInstance = new EventSource ( `http://${ domain } ` )
190
+
191
+ const onerrorCalls = [ ]
192
+ eventSourceInstance . onerror = ( error ) => {
193
+ onerrorCalls . push ( error )
194
+ }
195
+
196
+ await new Promise ( resolve => setTimeout ( resolve , 8000 ) )
197
+
198
+ eventSourceInstance . close ( )
199
+
200
+ assert . strictEqual ( onerrorCalls . length , 3 )
201
+ } )
184
202
} )
You can’t perform that action at this time.
0 commit comments