@@ -7,6 +7,8 @@ import { createReadStream, readFileSync, existsSync } from 'node:fs'
7
7
import { setTimeout as sleep } from 'node:timers/promises'
8
8
import { route as networkPartitionRoute } from './routes/network-partition-key.mjs'
9
9
import { route as redirectRoute } from './routes/redirect.mjs'
10
+ import { Pipeline } from './util.mjs'
11
+ import { symbols } from './constants.mjs'
10
12
11
13
const tests = fileURLToPath ( join ( import . meta. url , '../../tests' ) )
12
14
@@ -31,6 +33,11 @@ const stash = new Stash()
31
33
const server = createServer ( async ( req , res ) => {
32
34
const fullUrl = new URL ( req . url , `http://localhost:${ server . address ( ) . port } ` )
33
35
36
+ if ( fullUrl . searchParams . has ( 'pipe' ) ) {
37
+ const pipe = new Pipeline ( fullUrl . searchParams . get ( 'pipe' ) )
38
+ res = await pipe . call ( req , res )
39
+ }
40
+
34
41
switch ( fullUrl . pathname ) {
35
42
case '/service-workers/cache-storage/resources/blank.html' : {
36
43
res . setHeader ( 'content-type' , 'text/html' )
@@ -55,7 +62,8 @@ const server = createServer(async (req, res) => {
55
62
case '/fetch/data-urls/resources/base64.json' :
56
63
case '/fetch/data-urls/resources/data-urls.json' :
57
64
case '/fetch/api/resources/empty.txt' :
58
- case '/fetch/api/resources/data.json' : {
65
+ case '/fetch/api/resources/data.json' :
66
+ case '/common/text-plain.txt' : {
59
67
// If this specific resources requires custom headers
60
68
const customHeadersPath = join ( tests , fullUrl . pathname + '.headers' )
61
69
if ( existsSync ( customHeadersPath ) ) {
@@ -74,9 +82,11 @@ const server = createServer(async (req, res) => {
74
82
}
75
83
76
84
// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/data.json
77
- return createReadStream ( join ( tests , fullUrl . pathname ) )
85
+ createReadStream ( join ( tests , fullUrl . pathname ) )
78
86
. on ( 'end' , ( ) => res . end ( ) )
79
87
. pipe ( res )
88
+
89
+ break
80
90
}
81
91
case '/fetch/api/resources/trickle.py' : {
82
92
// Note: python's time.sleep(...) takes seconds, while setTimeout
@@ -133,7 +143,7 @@ const server = createServer(async (req, res) => {
133
143
}
134
144
135
145
res . end ( )
136
- return
146
+ break
137
147
}
138
148
case '/fetch/api/resources/stash-take.py' : {
139
149
// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/stash-take.py
@@ -144,7 +154,8 @@ const server = createServer(async (req, res) => {
144
154
const took = stash . take ( key , fullUrl . pathname ) ?? null
145
155
146
156
res . write ( JSON . stringify ( took ) )
147
- return res . end ( )
157
+ res . end ( )
158
+ break
148
159
}
149
160
case '/fetch/api/resources/echo-content.py' : {
150
161
res . setHeader ( 'X-Request-Method' , req . method )
@@ -268,15 +279,19 @@ const server = createServer(async (req, res) => {
268
279
break
269
280
}
270
281
case '/fetch/connection-pool/resources/network-partition-key.py' : {
271
- return networkPartitionRoute ( req , res , fullUrl )
282
+ networkPartitionRoute ( req , res , fullUrl )
283
+ break
272
284
}
273
285
case '/resources/top.txt' : {
274
- return createReadStream ( join ( tests , 'fetch/api/' , fullUrl . pathname ) )
286
+ createReadStream ( join ( tests , 'fetch/api/' , fullUrl . pathname ) )
275
287
. on ( 'end' , ( ) => res . end ( ) )
276
288
. pipe ( res )
289
+
290
+ break
277
291
}
278
292
case '/fetch/api/resources/redirect.py' : {
279
- return redirectRoute ( req , res , fullUrl )
293
+ redirectRoute ( req , res , fullUrl )
294
+ break
280
295
}
281
296
case '/fetch/api/resources/method.py' : {
282
297
if ( fullUrl . searchParams . has ( 'cors' ) ) {
@@ -299,7 +314,7 @@ const server = createServer(async (req, res) => {
299
314
}
300
315
301
316
res . end ( )
302
- return
317
+ break
303
318
}
304
319
case '/fetch/api/resources/clean-stash.py' : {
305
320
const token = fullUrl . searchParams . get ( 'token' )
@@ -334,7 +349,7 @@ const server = createServer(async (req, res) => {
334
349
335
350
if ( req . headers . authorization ) {
336
351
res . end ( req . headers . authorization )
337
- return
352
+ break
338
353
}
339
354
340
355
res . end ( 'none' )
@@ -363,48 +378,48 @@ const server = createServer(async (req, res) => {
363
378
364
379
if ( user === 'user' && password === 'password' ) {
365
380
res . end ( 'Authentication done' )
366
- return
381
+ break
367
382
}
368
383
369
384
const realm = fullUrl . searchParams . get ( 'realm' ) ?? 'test'
370
385
371
386
res . statusCode = 401
372
387
res . setHeader ( 'WWW-Authenticate' , `Basic realm="${ realm } "` )
373
388
res . end ( 'Please login with credentials \'user\' and \'password\'' )
374
- return
389
+ break
375
390
}
376
391
case '/fetch/api/resources/redirect-empty-location.py' : {
377
392
res . setHeader ( 'location' , '' )
378
393
res . statusCode = 302
379
394
res . end ( '' )
380
- return
395
+ break
381
396
}
382
397
case '/service-workers/cache-storage/resources/fetch-status.py' : {
383
398
const status = Number ( fullUrl . searchParams . get ( 'status' ) )
384
399
385
400
res . statusCode = status
386
401
res . end ( )
387
- return
402
+ break
388
403
}
389
404
case '/service-workers/cache-storage/this-resource-should-not-exist' :
390
405
case '/service-workers/cache-storage/this-does-not-exist-please-dont-create-it' : {
391
406
res . statusCode = 404
392
407
res . end ( )
393
- return
408
+ break
394
409
}
395
410
case '/service-workers/cache-storage/resources/vary.py' : {
396
411
if ( fullUrl . searchParams . has ( 'clear-vary-value-override-cookie' ) ) {
397
412
res . setHeader ( 'cookie' , '' )
398
413
res . end ( 'vary cookie cleared' )
399
- return
414
+ break
400
415
}
401
416
402
417
const setCookieVary = fullUrl . searchParams . get ( 'set-vary-value-override-cookie' ) ?? ''
403
418
404
419
if ( setCookieVary ) {
405
420
res . setHeader ( 'set-cookie' , `vary-value-override=${ setCookieVary } ` )
406
421
res . end ( 'vary cookie set' )
407
- return
422
+ break
408
423
}
409
424
410
425
const cookieVary = req . headers . cookie ?. split ( ';' ) . find ( ( c ) => c . includes ( 'vary-value-override=' ) )
@@ -420,7 +435,7 @@ const server = createServer(async (req, res) => {
420
435
}
421
436
422
437
res . end ( 'vary response' )
423
- return
438
+ break
424
439
}
425
440
case '/eventsource/resources/message.py' : {
426
441
const mime = fullUrl . searchParams . get ( 'mime' ) ?? 'text/event-stream'
@@ -435,7 +450,7 @@ const server = createServer(async (req, res) => {
435
450
res . end ( )
436
451
} , sleep )
437
452
438
- return
453
+ break
439
454
}
440
455
case '/eventsource/resources/last-event-id.py' : {
441
456
const lastEventId = req . headers [ 'Last-Event-ID' ] ?? ''
@@ -451,13 +466,17 @@ const server = createServer(async (req, res) => {
451
466
res . end ( )
452
467
}
453
468
454
- return
469
+ break
455
470
}
456
471
default : {
457
472
res . statusCode = 200
458
473
res . end ( fullUrl . toString ( ) )
459
474
}
460
475
}
476
+
477
+ if ( res [ symbols . kContent ] ) {
478
+ res . write ( res [ symbols . kContent ] )
479
+ }
461
480
} ) . listen ( 0 )
462
481
463
482
await once ( server , 'listening' )
0 commit comments