|
| 1 | +import { Readable, PassThrough } from 'stream'; |
| 2 | +import injectRSCPayload from '../src/injectRSCPayload'; |
| 3 | + |
| 4 | +describe('injectRSCPayload', () => { |
| 5 | + const createMockStream = (chunks) => { |
| 6 | + if (Array.isArray(chunks)) { |
| 7 | + return Readable.from(chunks.map(chunk => |
| 8 | + typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk |
| 9 | + )); |
| 10 | + } |
| 11 | + const passThrough = new PassThrough(); |
| 12 | + const entries = Object.entries(chunks); |
| 13 | + const keysLength = entries.length; |
| 14 | + entries.forEach(([delay, value], index) => { |
| 15 | + setTimeout(() => { |
| 16 | + const chunksArray = Array.isArray(value) ? value : [value]; |
| 17 | + chunksArray.forEach(chunk => { |
| 18 | + passThrough.push(new TextEncoder().encode(chunk)); |
| 19 | + }); |
| 20 | + if (index === keysLength - 1) { |
| 21 | + passThrough.push(null); |
| 22 | + } |
| 23 | + }, +delay); |
| 24 | + }); |
| 25 | + return passThrough; |
| 26 | + } |
| 27 | + |
| 28 | + const collectStreamData = async (stream) => { |
| 29 | + const chunks = []; |
| 30 | + for await (const chunk of stream) { |
| 31 | + chunks.push(new TextDecoder().decode(chunk)); |
| 32 | + } |
| 33 | + return chunks.join(''); |
| 34 | + }; |
| 35 | + |
| 36 | + it('should inject RSC payload as script tags', async () => { |
| 37 | + const mockRSC = createMockStream(['{"test": "data"}']); |
| 38 | + const mockHTML = createMockStream(['<html><body><div>Hello, world!</div></body></html>']); |
| 39 | + const result = injectRSCPayload(mockHTML, mockRSC); |
| 40 | + const resultStr = await collectStreamData(result); |
| 41 | + |
| 42 | + expect(resultStr).toContain( |
| 43 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data\\"}")</script>' |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle multiple RSC payloads', async () => { |
| 48 | + const mockRSC = createMockStream(['{"test": "data"}', '{"test": "data2"}']); |
| 49 | + const mockHTML = createMockStream(['<html><body><div>Hello, world!</div></body></html>']); |
| 50 | + const result = injectRSCPayload(mockHTML, mockRSC); |
| 51 | + const resultStr = await collectStreamData(result); |
| 52 | + |
| 53 | + expect(resultStr).toContain( |
| 54 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data\\"}")</script>' |
| 55 | + ); |
| 56 | + expect(resultStr).toContain( |
| 57 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data2\\"}")</script>' |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should add all ready html chunks before adding RSC payloads', async () => { |
| 62 | + const mockRSC = createMockStream(['{"test": "data"}', '{"test": "data2"}']); |
| 63 | + const mockHTML = createMockStream([ |
| 64 | + '<html><body><div>Hello, world!</div></body></html>', |
| 65 | + '<div>Next chunk</div>', |
| 66 | + ]); |
| 67 | + const result = injectRSCPayload(mockHTML, mockRSC); |
| 68 | + const resultStr = await collectStreamData(result); |
| 69 | + |
| 70 | + expect(resultStr).toEqual( |
| 71 | + '<html><body><div>Hello, world!</div></body></html>' + |
| 72 | + '<div>Next chunk</div>' + |
| 73 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data\\"}")</script>' + |
| 74 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data2\\"}")</script>' |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('adds delayed html chunks after RSC payloads', async () => { |
| 79 | + const mockRSC = createMockStream(['{"test": "data"}', '{"test": "data2"}']); |
| 80 | + const mockHTML = createMockStream({ |
| 81 | + 0: '<html><body><div>Hello, world!</div></body></html>', |
| 82 | + 100: '<div>Next chunk</div>' |
| 83 | + }); |
| 84 | + const result = injectRSCPayload(mockHTML, mockRSC); |
| 85 | + const resultStr = await collectStreamData(result); |
| 86 | + |
| 87 | + expect(resultStr).toEqual( |
| 88 | + '<html><body><div>Hello, world!</div></body></html>' + |
| 89 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data\\"}")</script>' + |
| 90 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data2\\"}")</script>' + |
| 91 | + '<div>Next chunk</div>' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles the case when html is delayed', async () => { |
| 96 | + const mockRSC = createMockStream({ |
| 97 | + 0: '{"test": "data"}', |
| 98 | + 150: '{"test": "data2"}', |
| 99 | + }); |
| 100 | + const mockHTML = createMockStream({ |
| 101 | + 100: [ |
| 102 | + '<html><body><div>Hello, world!</div></body></html>', |
| 103 | + '<div>Next chunk</div>' |
| 104 | + ], |
| 105 | + 200: '<div>Third chunk</div>' |
| 106 | + }); |
| 107 | + const result = injectRSCPayload(mockHTML, mockRSC); |
| 108 | + const resultStr = await collectStreamData(result); |
| 109 | + |
| 110 | + expect(resultStr).toEqual( |
| 111 | + '<html><body><div>Hello, world!</div></body></html>' + |
| 112 | + '<div>Next chunk</div>' + |
| 113 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data\\"}")</script>' + |
| 114 | + '<script>(self.__FLIGHT_DATA||=[]).push("{\\"test\\": \\"data2\\"}")</script>' + |
| 115 | + '<div>Third chunk</div>' |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
0 commit comments