|
| 1 | +import { setTimeout } from 'timers/promises' |
| 2 | + |
| 3 | +const stash = new Map() |
| 4 | + |
| 5 | +/** |
| 6 | + * @see https://github.com/web-platform-tests/wpt/blob/master/fetch/connection-pool/resources/network-partition-key.py |
| 7 | + * @param {Parameters<import('http').RequestListener>[0]} req |
| 8 | + * @param {Parameters<import('http').RequestListener>[1]} res |
| 9 | + * @param {URL} url |
| 10 | + */ |
| 11 | +export async function route (req, res, { searchParams }) { |
| 12 | + let stashedData = { count: 0, preflight: 0 } |
| 13 | + let status = 302 |
| 14 | + res.setHeader('Content-Type', 'text/plain') |
| 15 | + res.setHeader('Cache-Control', 'no-cache') |
| 16 | + res.setHeader('Pragma', 'no-cache') |
| 17 | + |
| 18 | + if (Object.hasOwn(req.headers, 'origin')) { |
| 19 | + res.setHeader('Access-Control-Allow-Origin', req.headers.origin ?? '') |
| 20 | + res.setHeader('Access-Control-Allow-Credentials', 'true') |
| 21 | + } else { |
| 22 | + res.setHeader('Access-Control-Allow-Origin', '*') |
| 23 | + } |
| 24 | + |
| 25 | + let token = null |
| 26 | + if (searchParams.has('token')) { |
| 27 | + token = searchParams.get('token') |
| 28 | + const data = stash.get(token) |
| 29 | + stash.delete(token) |
| 30 | + if (data) { |
| 31 | + stashedData = data |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if (req.method === 'OPTIONS') { |
| 36 | + if (searchParams.has('allow_headers')) { |
| 37 | + res.setHeader('Access-Control-Allow-Headers', searchParams.get('allow_headers')) |
| 38 | + } |
| 39 | + |
| 40 | + stashedData.preflight = '1' |
| 41 | + |
| 42 | + if (!searchParams.has('redirect_preflight')) { |
| 43 | + if (token) { |
| 44 | + stash.set(searchParams.get('token'), stashedData) |
| 45 | + } |
| 46 | + |
| 47 | + res.statusCode = 200 |
| 48 | + res.end('') |
| 49 | + return |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (searchParams.has('redirect_status')) { |
| 54 | + status = parseInt(searchParams.get('redirect_status')) |
| 55 | + } |
| 56 | + |
| 57 | + stashedData.count += 1 |
| 58 | + |
| 59 | + if (searchParams.has('location')) { |
| 60 | + let url = decodeURIComponent(searchParams.get('location')) |
| 61 | + |
| 62 | + if (!searchParams.has('simple')) { |
| 63 | + const scheme = new URL(url).protocol |
| 64 | + |
| 65 | + if (scheme === 'http:' || scheme === 'https:') { |
| 66 | + url += url.includes('?') ? '&' : '?' |
| 67 | + |
| 68 | + for (const [key, value] of searchParams) { |
| 69 | + url += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(value) |
| 70 | + } |
| 71 | + |
| 72 | + url += '&count=' + stashedData.count |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + res.setHeader('location', url) |
| 77 | + } |
| 78 | + |
| 79 | + if (searchParams.has('redirect_referrerpolicy')) { |
| 80 | + res.setHeader('Referrer-Policy', searchParams.get('redirect_referrerpolicy')) |
| 81 | + } |
| 82 | + |
| 83 | + if (searchParams.has('delay')) { |
| 84 | + await setTimeout(parseFloat(searchParams.get('delay') ?? 0)) |
| 85 | + } |
| 86 | + |
| 87 | + if (token) { |
| 88 | + stash.set(searchParams.get('token'), stashedData) |
| 89 | + |
| 90 | + if (searchParams.has('max_count')) { |
| 91 | + const maxCount = parseInt(searchParams.get('max_count')) |
| 92 | + |
| 93 | + if (stashedData.count > maxCount) { |
| 94 | + res.end((stashedData.count - 1).toString()) |
| 95 | + return |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + res.statusCode = status |
| 101 | + res.end('') |
| 102 | +} |
0 commit comments