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

htmlBoundaryScanner.test.ts

256 lines8.1 KB

1import { describe, expect, test } from 'vitest'
2import {
3 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
4 HYDRATION_SCRIPT_BOUNDARY_BYTES,
5 createHydrationScripts,
6} from '../src/ssr/hydrationScripts'
7import {
8 DOCUMENT_CLOSE_ANCHOR_INDEX,
9 DOCUMENT_CLOSE_BYTES,
10 SCRIPT_CLOSE,
11 SCRIPT_CLOSE_ANCHOR_INDEX,
12 SCRIPT_CLOSE_BYTES,
13 advanceByteMatcher,
14 findExactBytes,
15 getExactBytesPrefixAtEnd,
16} from '../src/ssr/htmlBoundaryScanner'
17import type { ByteMatcherState } from '../src/ssr/htmlBoundaryScanner'
18​
19const encoder = new TextEncoder()
20const decoder = new TextDecoder()
21const DOCUMENT_CLOSE = decoder.decode(DOCUMENT_CLOSE_BYTES)
22const HYDRATION_SCRIPT_BOUNDARY_SUFFIX = decoder.decode(
23 HYDRATION_SCRIPT_BOUNDARY_BYTES,
25const HYDRATION_SCRIPT_BOUNDARY_SOURCE = createHydrationScripts(
26 undefined,
27 [],
28).takeInitialHydrationScriptTags()!.boundary.children!
29​
30function chunksAtEverySplit(value: Uint8Array) {
31 return Array.from({ length: value.length + 1 }, (_, split) => [
32 value.subarray(0, split),
33 value.subarray(split),
34 ])
36​
37function matcherFor(pattern: Uint8Array, anchorIndex = 0): ByteMatcherState {
38 return { pattern, anchorIndex, matched: 0 }
40​
41describe('SSR exact byte matcher', () => {
42 test.each([
43 [
44 'router boundary',
45 HYDRATION_SCRIPT_BOUNDARY_BYTES,
46 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
47 ],
48 ['script close', SCRIPT_CLOSE_BYTES, SCRIPT_CLOSE_ANCHOR_INDEX],
49 ])('matches %s at every two-chunk split', (_, pattern, anchorIndex) => {
50 for (const chunks of chunksAtEverySplit(pattern)) {
51 const matcher = matcherFor(pattern, anchorIndex)
52 let matches = 0
53 for (const chunk of chunks) {
54 if (advanceByteMatcher(matcher, chunk) !== undefined) {
55 matches++
56 }
57 }
58 expect(matches).toBe(1)
59 }
60 })
61​
62 test.each([
63 [
64 'router boundary',
65 HYDRATION_SCRIPT_BOUNDARY_BYTES,
66 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
67 ],
68 ['script close', SCRIPT_CLOSE_BYTES, SCRIPT_CLOSE_ANCHOR_INDEX],
69 ])('matches byte-at-a-time %s input', (_, pattern, anchorIndex) => {
70 const matcher = matcherFor(pattern, anchorIndex)
71 let matches = 0
72 for (const byte of pattern) {
73 if (advanceByteMatcher(matcher, Uint8Array.of(byte)) !== undefined) {
74 matches++
75 }
76 }
77 expect(matches).toBe(1)
78 })
79​
80 test('returns the local end offset and keeps the unconsumed suffix', () => {
81 const matcher = matcherFor(SCRIPT_CLOSE_BYTES)
82 const prefix = encoder.encode('prefix')
83 const suffix = encoder.encode('<next>')
84 const value = new Uint8Array(
85 prefix.length + SCRIPT_CLOSE_BYTES.length + suffix.length,
86 )
87 value.set(prefix)
88 value.set(SCRIPT_CLOSE_BYTES, prefix.length)
89 value.set(suffix, prefix.length + SCRIPT_CLOSE_BYTES.length)
90​
91 const end = advanceByteMatcher(matcher, value)
92 expect(end).toBe(prefix.length + SCRIPT_CLOSE_BYTES.length)
93 expect(new TextDecoder().decode(value.subarray(end))).toBe('<next>')
94 })
95​
96 test('can find the last complete match without stopping at the first', () => {
97 const matcher = matcherFor(SCRIPT_CLOSE_BYTES, SCRIPT_CLOSE_ANCHOR_INDEX)
98 const value = encoder.encode('</script><div></div></script><next>')
99​
100 const end = advanceByteMatcher(matcher, value, 0, true)
101 expect(new TextDecoder().decode(value.subarray(0, end))).toBe(
102 '</script><div></div></script>',
103 )
104 })
105​
106 test.each([
107 [
108 'router boundary',
109 HYDRATION_SCRIPT_BOUNDARY_BYTES,
110 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
111 ],
112 ['script close', SCRIPT_CLOSE_BYTES, SCRIPT_CLOSE_ANCHOR_INDEX],
113 ])('%s satisfies the matcher invariants', (_, pattern, anchorIndex) => {
114 expect(pattern.length).toBeGreaterThan(0)
115 expect(pattern.indexOf(pattern[0]!, 1)).toBe(-1)
116 expect(anchorIndex).toBeGreaterThanOrEqual(0)
117 expect(anchorIndex).toBeLessThan(pattern.length)
118 })
119​
120 test.each(['one chunk', 'split after the old overlapping prefix'])(
121 'ignores marker text without the fixed semicolon in %s',
122 (shape) => {
123 const markerAndClose = HYDRATION_SCRIPT_BOUNDARY_BYTES.subarray(1)
124 const value = new Uint8Array(24 + markerAndClose.length - 1)
125 value.set(markerAndClose.subarray(0, 24))
126 value.set(markerAndClose.subarray(1), 24)
127 const chunks =
128 shape === 'one chunk'
129 ? [value]
130 : [value.subarray(0, 24), value.subarray(24)]
131 const matcher = matcherFor(
132 HYDRATION_SCRIPT_BOUNDARY_BYTES,
133 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
134 )
135​
136 let consumed = 0
137 let matchEnd: number | undefined
138 for (const chunk of chunks) {
139 const localEnd = advanceByteMatcher(matcher, chunk)
140 if (localEnd !== undefined) {
141 matchEnd = consumed + localEnd
142 }
143 consumed += chunk.length
144 }
145​
146 expect(matchEnd).toBeUndefined()
147 },
148 )
149​
150 test.each(['one chunk', 'split before the valid boundary'])(
151 'restarts at a valid boundary after a near match in %s',
152 (shape) => {
153 const nearMatch = HYDRATION_SCRIPT_BOUNDARY_BYTES.subarray(0, 12)
154 const value = new Uint8Array(
155 nearMatch.length + HYDRATION_SCRIPT_BOUNDARY_BYTES.length,
156 )
157 value.set(nearMatch)
158 value.set(HYDRATION_SCRIPT_BOUNDARY_BYTES, nearMatch.length)
159 const chunks =
160 shape === 'one chunk'
161 ? [value]
162 : [
163 value.subarray(0, nearMatch.length),
164 value.subarray(nearMatch.length),
165 ]
166 const matcher = matcherFor(
167 HYDRATION_SCRIPT_BOUNDARY_BYTES,
168 HYDRATION_SCRIPT_BOUNDARY_ANCHOR_INDEX,
169 )
170​
171 let consumed = 0
172 let matchEnd: number | undefined
173 for (const chunk of chunks) {
174 const localEnd = advanceByteMatcher(matcher, chunk)
175 if (localEnd !== undefined) {
176 matchEnd = consumed + localEnd
177 }
178 consumed += chunk.length
179 }
180​
181 expect(matchEnd).toBe(value.length)
182 },
183 )
184​
185 test('reset drops an incomplete match', () => {
186 const matcher = matcherFor(SCRIPT_CLOSE_BYTES)
187 expect(
188 advanceByteMatcher(matcher, SCRIPT_CLOSE_BYTES.subarray(0, 5)),
189 ).toBeUndefined()
190 matcher.matched = 0
191 expect(
192 advanceByteMatcher(matcher, SCRIPT_CLOSE_BYTES.subarray(5)),
193 ).toBeUndefined()
194 })
195})
196​
197describe('SSR exact byte helpers', () => {
198 test('scanner boundary is the exact end of the emitted script', () => {
199 expect(HYDRATION_SCRIPT_BOUNDARY_SUFFIX.endsWith(SCRIPT_CLOSE)).toBe(true)
200 expect(
201 HYDRATION_SCRIPT_BOUNDARY_SOURCE.endsWith(
202 HYDRATION_SCRIPT_BOUNDARY_SUFFIX.slice(0, -SCRIPT_CLOSE.length),
203 ),
204 ).toBe(true)
205 expect(HYDRATION_SCRIPT_BOUNDARY_BYTES).toEqual(
206 encoder.encode(HYDRATION_SCRIPT_BOUNDARY_SUFFIX),
207 )
208 })
209​
210 test('anchors document-close scans on the uncommon y byte', () => {
211 expect(DOCUMENT_CLOSE_ANCHOR_INDEX).toBe(DOCUMENT_CLOSE.indexOf('y'))
212 })
213​
214 test('finds a complete sequence without decoding or copying', () => {
215 const prefix = encoder.encode('abc')
216 const value = new Uint8Array(
217 prefix.length + DOCUMENT_CLOSE_BYTES.length + 1,
218 )
219 value.set(prefix)
220 value.set(DOCUMENT_CLOSE_BYTES, prefix.length)
221 value[value.length - 1] = 120
222​
223 expect(findExactBytes(value, DOCUMENT_CLOSE_BYTES)).toBe(prefix.length)
224 expect(findExactBytes(value, DOCUMENT_CLOSE_BYTES, prefix.length + 1)).toBe(
225 -1,
226 )
227 })
228​
229 test('reports every incomplete document-close suffix', () => {
230 for (let length = 1; length < DOCUMENT_CLOSE_BYTES.length; length++) {
231 const prefix = encoder.encode('application')
232 const value = new Uint8Array(prefix.length + length)
233 value.set(prefix)
234 value.set(DOCUMENT_CLOSE_BYTES.subarray(0, length), prefix.length)
235​
236 expect(getExactBytesPrefixAtEnd(value, DOCUMENT_CLOSE_BYTES)).toBe(
237 prefix.length,
238 )
239 }
240 })
241​
242 test('uses the longest suffix when the pattern prefix overlaps', () => {
243 const pattern = encoder.encode('abab')
244 const value = encoder.encode('xxaba')
245 expect(getExactBytesPrefixAtEnd(value, pattern)).toBe(2)
246 })
247​
248 test('returns no suffix for a final mismatch', () => {
249 expect(
250 getExactBytesPrefixAtEnd(
251 encoder.encode('application'),
252 SCRIPT_CLOSE_BYTES,
253 ),
254 ).toBeUndefined()
255 })
256})