1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @emails react-core
    
  8.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. // Polyfills for test environment
    
  13. global.ReadableStream =
    
  14.   require('web-streams-polyfill/ponyfill/es6').ReadableStream;
    
  15. global.TextEncoder = require('util').TextEncoder;
    
  16. global.TextDecoder = require('util').TextDecoder;
    
  17. 
    
  18. // Don't wait before processing work on the server.
    
  19. // TODO: we can replace this with FlightServer.act().
    
  20. global.setTimeout = cb => cb();
    
  21. 
    
  22. let clientExports;
    
  23. let webpackMap;
    
  24. let webpackModules;
    
  25. let webpackModuleLoading;
    
  26. let React;
    
  27. let ReactDOMServer;
    
  28. let ReactServerDOMServer;
    
  29. let ReactServerDOMClient;
    
  30. let use;
    
  31. 
    
  32. describe('ReactFlightDOMEdge', () => {
    
  33.   beforeEach(() => {
    
  34.     jest.resetModules();
    
  35. 
    
  36.     // Simulate the condition resolution
    
  37.     jest.mock('react', () => require('react/react.shared-subset'));
    
  38.     jest.mock('react-server-dom-webpack/server', () =>
    
  39.       require('react-server-dom-webpack/server.edge'),
    
  40.     );
    
  41. 
    
  42.     const WebpackMock = require('./utils/WebpackMock');
    
  43. 
    
  44.     clientExports = WebpackMock.clientExports;
    
  45.     webpackMap = WebpackMock.webpackMap;
    
  46.     webpackModules = WebpackMock.webpackModules;
    
  47.     webpackModuleLoading = WebpackMock.moduleLoading;
    
  48. 
    
  49.     ReactServerDOMServer = require('react-server-dom-webpack/server');
    
  50. 
    
  51.     jest.resetModules();
    
  52.     __unmockReact();
    
  53.     jest.unmock('react-server-dom-webpack/server');
    
  54.     jest.mock('react-server-dom-webpack/client', () =>
    
  55.       require('react-server-dom-webpack/client.edge'),
    
  56.     );
    
  57.     React = require('react');
    
  58.     ReactDOMServer = require('react-dom/server.edge');
    
  59.     ReactServerDOMClient = require('react-server-dom-webpack/client');
    
  60.     use = React.use;
    
  61.   });
    
  62. 
    
  63.   function passThrough(stream) {
    
  64.     // Simulate more realistic network by splitting up and rejoining some chunks.
    
  65.     // This lets us test that we don't accidentally rely on particular bounds of the chunks.
    
  66.     return new ReadableStream({
    
  67.       async start(controller) {
    
  68.         const reader = stream.getReader();
    
  69.         let prevChunk = new Uint8Array(0);
    
  70.         function push() {
    
  71.           reader.read().then(({done, value}) => {
    
  72.             if (done) {
    
  73.               controller.enqueue(prevChunk);
    
  74.               controller.close();
    
  75.               return;
    
  76.             }
    
  77.             const chunk = new Uint8Array(prevChunk.length + value.length);
    
  78.             chunk.set(prevChunk, 0);
    
  79.             chunk.set(value, prevChunk.length);
    
  80.             if (chunk.length > 50) {
    
  81.               controller.enqueue(chunk.subarray(0, chunk.length - 50));
    
  82.               prevChunk = chunk.subarray(chunk.length - 50);
    
  83.             } else {
    
  84.               prevChunk = chunk;
    
  85.             }
    
  86.             push();
    
  87.           });
    
  88.         }
    
  89.         push();
    
  90.       },
    
  91.     });
    
  92.   }
    
  93. 
    
  94.   async function readResult(stream) {
    
  95.     const reader = stream.getReader();
    
  96.     let result = '';
    
  97.     while (true) {
    
  98.       const {done, value} = await reader.read();
    
  99.       if (done) {
    
  100.         return result;
    
  101.       }
    
  102.       result += Buffer.from(value).toString('utf8');
    
  103.     }
    
  104.   }
    
  105. 
    
  106.   it('should allow an alternative module mapping to be used for SSR', async () => {
    
  107.     function ClientComponent() {
    
  108.       return <span>Client Component</span>;
    
  109.     }
    
  110.     // The Client build may not have the same IDs as the Server bundles for the same
    
  111.     // component.
    
  112.     const ClientComponentOnTheClient = clientExports(ClientComponent);
    
  113.     const ClientComponentOnTheServer = clientExports(ClientComponent);
    
  114. 
    
  115.     // In the SSR bundle this module won't exist. We simulate this by deleting it.
    
  116.     const clientId = webpackMap[ClientComponentOnTheClient.$$id].id;
    
  117.     delete webpackModules[clientId];
    
  118. 
    
  119.     // Instead, we have to provide a translation from the client meta data to the SSR
    
  120.     // meta data.
    
  121.     const ssrMetadata = webpackMap[ClientComponentOnTheServer.$$id];
    
  122.     const translationMap = {
    
  123.       [clientId]: {
    
  124.         '*': ssrMetadata,
    
  125.       },
    
  126.     };
    
  127. 
    
  128.     function App() {
    
  129.       return <ClientComponentOnTheClient />;
    
  130.     }
    
  131. 
    
  132.     const stream = ReactServerDOMServer.renderToReadableStream(
    
  133.       <App />,
    
  134.       webpackMap,
    
  135.     );
    
  136.     const response = ReactServerDOMClient.createFromReadableStream(stream, {
    
  137.       ssrManifest: {
    
  138.         moduleMap: translationMap,
    
  139.         moduleLoading: webpackModuleLoading,
    
  140.       },
    
  141.     });
    
  142. 
    
  143.     function ClientRoot() {
    
  144.       return use(response);
    
  145.     }
    
  146. 
    
  147.     const ssrStream = await ReactDOMServer.renderToReadableStream(
    
  148.       <ClientRoot />,
    
  149.     );
    
  150.     const result = await readResult(ssrStream);
    
  151.     expect(result).toEqual('<span>Client Component</span>');
    
  152.   });
    
  153. 
    
  154.   it('should encode long string in a compact format', async () => {
    
  155.     const testString = '"\n\t'.repeat(500) + '🙃';
    
  156.     const testString2 = 'hello'.repeat(400);
    
  157. 
    
  158.     const stream = ReactServerDOMServer.renderToReadableStream({
    
  159.       text: testString,
    
  160.       text2: testString2,
    
  161.     });
    
  162.     const [stream1, stream2] = passThrough(stream).tee();
    
  163. 
    
  164.     const serializedContent = await readResult(stream1);
    
  165.     // The content should be compact an unescaped
    
  166.     expect(serializedContent.length).toBeLessThan(4000);
    
  167.     expect(serializedContent).not.toContain('\\n');
    
  168.     expect(serializedContent).not.toContain('\\t');
    
  169.     expect(serializedContent).not.toContain('\\"');
    
  170.     expect(serializedContent).toContain('\t');
    
  171. 
    
  172.     const result = await ReactServerDOMClient.createFromReadableStream(
    
  173.       stream2,
    
  174.       {
    
  175.         ssrManifest: {
    
  176.           moduleMap: null,
    
  177.           moduleLoading: null,
    
  178.         },
    
  179.       },
    
  180.     );
    
  181.     // Should still match the result when parsed
    
  182.     expect(result.text).toBe(testString);
    
  183.     expect(result.text2).toBe(testString2);
    
  184.   });
    
  185. 
    
  186.   it('should encode repeated objects in a compact format by deduping', async () => {
    
  187.     const obj = {
    
  188.       this: {is: 'a large objected'},
    
  189.       with: {many: 'properties in it'},
    
  190.     };
    
  191.     const props = {
    
  192.       items: new Array(30).fill(obj),
    
  193.     };
    
  194.     const stream = ReactServerDOMServer.renderToReadableStream(props);
    
  195.     const [stream1, stream2] = passThrough(stream).tee();
    
  196. 
    
  197.     const serializedContent = await readResult(stream1);
    
  198.     expect(serializedContent.length).toBeLessThan(400);
    
  199. 
    
  200.     const result = await ReactServerDOMClient.createFromReadableStream(
    
  201.       stream2,
    
  202.       {
    
  203.         ssrManifest: {
    
  204.           moduleMap: null,
    
  205.           moduleLoading: null,
    
  206.         },
    
  207.       },
    
  208.     );
    
  209.     // Should still match the result when parsed
    
  210.     expect(result).toEqual(props);
    
  211.     expect(result.items[5]).toBe(result.items[10]); // two random items are the same instance
    
  212.     // TODO: items[0] is not the same as the others in this case
    
  213.   });
    
  214. 
    
  215.   it('should execute repeated server components only once', async () => {
    
  216.     const str = 'this is a long return value';
    
  217.     let timesRendered = 0;
    
  218.     function ServerComponent() {
    
  219.       timesRendered++;
    
  220.       return str;
    
  221.     }
    
  222.     const element = <ServerComponent />;
    
  223.     const children = new Array(30).fill(element);
    
  224.     const resolvedChildren = new Array(30).fill(str);
    
  225.     const stream = ReactServerDOMServer.renderToReadableStream(children);
    
  226.     const [stream1, stream2] = passThrough(stream).tee();
    
  227. 
    
  228.     const serializedContent = await readResult(stream1);
    
  229.     expect(serializedContent.length).toBeLessThan(400);
    
  230.     expect(timesRendered).toBeLessThan(5);
    
  231. 
    
  232.     const result = await ReactServerDOMClient.createFromReadableStream(
    
  233.       stream2,
    
  234.       {
    
  235.         ssrManifest: {
    
  236.           moduleMap: null,
    
  237.           moduleLoading: null,
    
  238.         },
    
  239.       },
    
  240.     );
    
  241.     // Should still match the result when parsed
    
  242.     expect(result).toEqual(resolvedChildren);
    
  243.   });
    
  244. 
    
  245.   // @gate enableBinaryFlight
    
  246.   it('should be able to serialize any kind of typed array', async () => {
    
  247.     const buffer = new Uint8Array([
    
  248.       123, 4, 10, 5, 100, 255, 244, 45, 56, 67, 43, 124, 67, 89, 100, 20,
    
  249.     ]).buffer;
    
  250.     const buffers = [
    
  251.       buffer,
    
  252.       new Int8Array(buffer, 1),
    
  253.       new Uint8Array(buffer, 2),
    
  254.       new Uint8ClampedArray(buffer, 2),
    
  255.       new Int16Array(buffer, 2),
    
  256.       new Uint16Array(buffer, 2),
    
  257.       new Int32Array(buffer, 4),
    
  258.       new Uint32Array(buffer, 4),
    
  259.       new Float32Array(buffer, 4),
    
  260.       new Float64Array(buffer, 0),
    
  261.       new BigInt64Array(buffer, 0),
    
  262.       new BigUint64Array(buffer, 0),
    
  263.       new DataView(buffer, 3),
    
  264.     ];
    
  265.     const stream = passThrough(
    
  266.       ReactServerDOMServer.renderToReadableStream(buffers),
    
  267.     );
    
  268.     const result = await ReactServerDOMClient.createFromReadableStream(stream, {
    
  269.       ssrManifest: {
    
  270.         moduleMap: null,
    
  271.         moduleLoading: null,
    
  272.       },
    
  273.     });
    
  274.     expect(result).toEqual(buffers);
    
  275.   });
    
  276. });