|
| 1 | +const stash = new Map() |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/web-platform-tests/wpt/blob/master/fetch/connection-pool/resources/network-partition-key.py |
| 5 | + * @param {Parameters<import('http').RequestListener>[0]} req |
| 6 | + * @param {Parameters<import('http').RequestListener>[1]} res |
| 7 | + * @param {URL} url |
| 8 | + */ |
| 9 | +export function route (req, res, { searchParams, port }) { |
| 10 | + res.setHeader('Cache-Control', 'no-store') |
| 11 | + |
| 12 | + const dispatch = searchParams.get('dispatch') |
| 13 | + const uuid = searchParams.get('uuid') |
| 14 | + const partitionId = searchParams.get('partition_id') |
| 15 | + |
| 16 | + if (!uuid || !dispatch || !partitionId) { |
| 17 | + res.statusCode = 404 |
| 18 | + res.end('Invalid query parameters') |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + let testFailed = false |
| 23 | + let requestCount = 0 |
| 24 | + let connectionCount = 0 |
| 25 | + |
| 26 | + if (searchParams.get('nocheck_partition') !== 'True') { |
| 27 | + const addressKey = `${req.socket.localAddress}|${port}` |
| 28 | + const serverState = stash.get(uuid) ?? { |
| 29 | + testFailed: false, |
| 30 | + requestCount: 0, |
| 31 | + connectionCount: 0 |
| 32 | + } |
| 33 | + |
| 34 | + stash.delete(uuid) |
| 35 | + requestCount = serverState.requestCount + 1 |
| 36 | + serverState.requestCount = requestCount |
| 37 | + |
| 38 | + if (Object.hasOwn(serverState, addressKey)) { |
| 39 | + if (serverState[addressKey] !== partitionId) { |
| 40 | + serverState.testFailed = true |
| 41 | + } |
| 42 | + } else { |
| 43 | + connectionCount = serverState.connectionCount + 1 |
| 44 | + serverState.connectionCount = connectionCount |
| 45 | + } |
| 46 | + |
| 47 | + serverState[addressKey] = partitionId |
| 48 | + testFailed = serverState.testFailed |
| 49 | + stash.set(uuid, serverState) |
| 50 | + } |
| 51 | + |
| 52 | + const origin = req.headers.origin |
| 53 | + if (origin) { |
| 54 | + res.setHeader('Access-Control-Allow-Origin', origin) |
| 55 | + res.setHeader('Access-Control-Allow-Credentials', 'true') |
| 56 | + } |
| 57 | + |
| 58 | + if (req.method === 'OPTIONS') { |
| 59 | + return handlePreflight(req, res) |
| 60 | + } |
| 61 | + |
| 62 | + if (dispatch === 'fetch_file') { |
| 63 | + res.end() |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if (dispatch === 'check_partition') { |
| 68 | + const status = searchParams.get('status') ?? 200 |
| 69 | + |
| 70 | + if (testFailed) { |
| 71 | + res.statusCode = status |
| 72 | + res.end('Multiple partition IDs used on a socket') |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + let body = 'ok' |
| 77 | + if (searchParams.get('addcounter')) { |
| 78 | + body += `. Request was sent ${requestCount} times. ${connectionCount} connections were created.` |
| 79 | + res.statusCode = status |
| 80 | + res.end(body) |
| 81 | + return |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (dispatch === 'clean_up') { |
| 86 | + stash.delete(uuid) |
| 87 | + res.statusCode = 200 |
| 88 | + if (testFailed) { |
| 89 | + res.end('Test failed, but cleanup completed.') |
| 90 | + } else { |
| 91 | + res.end('cleanup complete') |
| 92 | + } |
| 93 | + |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + res.statusCode = 404 |
| 98 | + res.end('Unrecognized dispatch parameter: ' + dispatch) |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @param {Parameters<import('http').RequestListener>[0]} req |
| 103 | + * @param {Parameters<import('http').RequestListener>[1]} res |
| 104 | + */ |
| 105 | +function handlePreflight (req, res) { |
| 106 | + res.statusCode = 200 |
| 107 | + res.setHeader('Access-Control-Allow-Methods', 'GET') |
| 108 | + res.setHeader('Access-Control-Allow-Headers', 'header-to-force-cors') |
| 109 | + res.setHeader('Access-Control-Max-Age', '86400') |
| 110 | + res.end('Preflight request') |
| 111 | +} |
0 commit comments