Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

shared.ts

192 lines5.3 KB

1import {
2 createDeterministicRandom,
3 randomSegment,
4 runSequentialRequestLoop,
5} from '#memory-server/bench-utils'
6import type { StartRequestHandler } from '#memory-server/bench-utils'
7​
8export type { StartRequestHandler }
9​
10type Framework = 'react' | 'solid' | 'vue'
11​
12const errorPathsIterations = 18
13const redirectSeed = 0xdecafbad
14const notFoundSeed = 0xdecafb0d
15const errorSeed = 0xdecafbed
16const unmatchedSeed = 0xdecaf00d
17const redirectStatus = 302
18const notFoundStatus = 404
19const errorStatus = 500
20// Module-level so each error-path bench keeps advancing across runner invocations.
21const redirectRandom = createDeterministicRandom(redirectSeed)
22const notFoundRandom = createDeterministicRandom(notFoundSeed)
23const errorRandom = createDeterministicRandom(errorSeed)
24const unmatchedRandom = createDeterministicRandom(unmatchedSeed)
25let redirectCounter = 0
26let notFoundCounter = 0
27let errorCounter = 0
28let unmatchedCounter = 0
29​
30const requestInit = {
31 method: 'GET',
32 headers: {
33 accept: 'text/html',
34 },
35} satisfies RequestInit
36​
37function buildRedirectRequest(random: () => number) {
38 const id = `${(redirectCounter++).toString(36)}-${randomSegment(random)}`
39​
40 return new Request(`http://localhost/from/${id}`, requestInit)
42​
43function buildNotFoundRequest(random: () => number) {
44 const id = `${(notFoundCounter++).toString(36)}-${randomSegment(random)}`
45​
46 return new Request(`http://localhost/missing/${id}`, requestInit)
48​
49function buildErrorRequest(random: () => number) {
50 const id = `${(errorCounter++).toString(36)}-${randomSegment(random)}`
51​
52 return new Request(`http://localhost/boom/${id}`, requestInit)
54​
55function buildUnmatchedRequest(random: () => number) {
56 const id = `${(unmatchedCounter++).toString(36)}-${randomSegment(random)}`
57​
58 return new Request(`http://localhost/nope/${id}`, requestInit)
60​
61function getRequestId(request: Request) {
62 const id = new URL(request.url).pathname.split('/').pop()
63​
64 if (!id) {
65 throw new Error(`Expected request id in ${request.url}`)
66 }
67​
68 return id
70​
71function validateRedirectResponse(response: Response, request: Request) {
72 if (response.status !== redirectStatus) {
73 throw new Error(
74 `Expected status ${redirectStatus} for ${request.url}, got ${response.status}`,
75 )
76 }
77​
78 const id = getRequestId(request)
79 const location = response.headers.get('location')
80​
81 if (location !== `/target/${id}`) {
82 throw new Error(`Expected redirect location /target/${id}, got ${location}`)
83 }
85​
86function validateNotFoundResponse(response: Response, request: Request) {
87 if (response.status !== notFoundStatus) {
88 throw new Error(
89 `Expected status ${notFoundStatus} for ${request.url}, got ${response.status}`,
90 )
91 }
93​
94function validateErrorResponse(response: Response, request: Request) {
95 if (response.status !== errorStatus) {
96 throw new Error(
97 `Expected status ${errorStatus} for ${request.url}, got ${response.status}`,
98 )
99 }
101​
102async function assertStatusSanity(
103 handler: StartRequestHandler,
104 request: Request,
105 validateResponse: (response: Response, request: Request) => void,
106) {
107 const response = await handler.fetch(request)
108 validateResponse(response, request)
109 await response.text()
111​
112async function assertErrorPathsSanity(handler: StartRequestHandler) {
113 await assertStatusSanity(
114 handler,
115 new Request('http://localhost/from/sanity-redirect', requestInit),
116 validateRedirectResponse,
117 )
118 await assertStatusSanity(
119 handler,
120 new Request('http://localhost/missing/sanity-missing', requestInit),
121 validateNotFoundResponse,
122 )
123 await assertStatusSanity(
124 handler,
125 new Request('http://localhost/boom/sanity-error', requestInit),
126 validateErrorResponse,
127 )
128 await assertStatusSanity(
129 handler,
130 new Request('http://localhost/nope/sanity-unmatched', requestInit),
131 validateNotFoundResponse,
132 )
134​
135export function createWorkloadGroup(
136 framework: Framework,
137 handler: StartRequestHandler,
138) {
139 const runRedirect = () =>
140 runSequentialRequestLoop(handler, {
141 random: redirectRandom,
142 iterations: errorPathsIterations,
143 buildRequest: buildRedirectRequest,
144 validateResponse: validateRedirectResponse,
145 })
146​
147 const runNotFound = () =>
148 runSequentialRequestLoop(handler, {
149 random: notFoundRandom,
150 iterations: errorPathsIterations,
151 buildRequest: buildNotFoundRequest,
152 validateResponse: validateNotFoundResponse,
153 })
154​
155 const runError = () =>
156 runSequentialRequestLoop(handler, {
157 random: errorRandom,
158 iterations: errorPathsIterations,
159 buildRequest: buildErrorRequest,
160 validateResponse: validateErrorResponse,
161 })
162​
163 const runUnmatched = () =>
164 runSequentialRequestLoop(handler, {
165 random: unmatchedRandom,
166 iterations: errorPathsIterations,
167 buildRequest: buildUnmatchedRequest,
168 validateResponse: validateNotFoundResponse,
169 })
170​
171 return {
172 sanity: () => assertErrorPathsSanity(handler),
173 workloads: [
174 {
175 name: `mem server error-paths redirect (${framework})`,
176 run: runRedirect,
177 },
178 {
179 name: `mem server error-paths not-found (${framework})`,
180 run: runNotFound,
181 },
182 {
183 name: `mem server error-paths error (${framework})`,
184 run: runError,
185 },
186 {
187 name: `mem server error-paths unmatched (${framework})`,
188 run: runUnmatched,
189 },
190 ],
191 }