Skip to content

Commit

Permalink
Add Fizz Browser host config
Browse files Browse the repository at this point in the history
This lets Fizz render to WHATWG streams. E.g. for rendering in a
Service Worker.

I added react-dom/unstable-fizz.browser as the entry point for this.

Since we now have two configurations of DOM. I had to add another
inlinedHostConfigs configuration called `dom-browser`. The reconciler
treats this configuration the same as `dom`. For stream it checks
against the ReactFizzHostConfigBrowser instead of the Node one.
  • Loading branch information
sebmarkbage committed Nov 8, 2018
1 parent 81ce2c5 commit 8bd123b
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 26 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"targz": "^1.0.1",
"through2": "^2.0.0",
"tmp": "~0.0.28",
"typescript": "~1.8.10"
"typescript": "~1.8.10",
"@mattiasbuelens/web-streams-polyfill": "0.1.0"
},
"devEngines": {
"node": "8.x || 9.x || 10.x"
Expand Down
7 changes: 7 additions & 0 deletions packages/react-dom/npm/unstable-fizz.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react-dom-unstable-fizz.node.production.min.js');
} else {
module.exports = require('./cjs/react-dom-unstable-fizz.node.development.js');
}
4 changes: 2 additions & 2 deletions packages/react-dom/npm/unstable-fizz.node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

if (process.env.NODE_ENV === 'production') {
module.exports = require('./cjs/react-dom-unstable-fizz.production.min.js');
module.exports = require('./cjs/react-dom-unstable-fizz.node.production.min.js');
} else {
module.exports = require('./cjs/react-dom-unstable-fizz.development.js');
module.exports = require('./cjs/react-dom-unstable-fizz.node.development.js');
}
4 changes: 3 additions & 1 deletion packages/react-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
"server.node.js",
"test-utils.js",
"unstable-fizz.js",
"unstable-fizz.browser.js",
"unstable-fizz.node.js",
"unstable-native-dependencies.js",
"cjs/",
"umd/"
],
"browser": {
"./server.js": "./server.browser.js"
"./server.js": "./server.browser.js",
"./unstable-fizz.js": "./unstable-fizz.browser.js"
},
"browserify": {
"transform": [
Expand Down
45 changes: 45 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

// Polyfills for test environment
global.ReadableStream = require('@mattiasbuelens/web-streams-polyfill/ponyfill/es6').ReadableStream;
global.TextEncoder = require('util').TextEncoder;

let React;
let ReactDOMFizzServer;

describe('ReactDOMFizzServer', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOMFizzServer = require('react-dom/unstable-fizz.browser');
});

async function readResult(stream) {
let reader = stream.getReader();
let result = '';
while (true) {
let {done, value} = await reader.read();
if (done) {
return result;
}
result += Buffer.from(value).toString('utf8');
}
}

it('should call renderToReadableStream', async () => {
let stream = ReactDOMFizzServer.renderToReadableStream(
<div>hello world</div>,
);
let result = await readResult(stream);
expect(result).toBe('<div>hello world</div>');
});
});
34 changes: 34 additions & 0 deletions packages/react-dom/src/server/ReactDOMFizzServerBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {ReactNodeList} from 'shared/ReactTypes';

import {
createRequest,
startWork,
startFlowing,
} from 'react-stream/inline.dom-browser';

function renderToReadableStream(children: ReactNodeList): ReadableStream {
let request;
return new ReadableStream({
start(controller) {
request = createRequest(children, controller);
startWork(request);
},
pull(controller) {
startFlowing(request, controller.desiredSize);
},
cancel(reason) {},
});
}

export default {
renderToReadableStream,
};
5 changes: 4 additions & 1 deletion packages/react-dom/src/server/ReactDOMFizzServerNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ function createDrainHandler(destination, request) {
return () => startFlowing(request, 0);
}

function pipeToNodeWritable(children: ReactNodeList, destination: Writable) {
function pipeToNodeWritable(
children: ReactNodeList,
destination: Writable,
): void {
let request = createRequest(children, destination);
destination.on('drain', createDrainHandler(destination, request));
startWork(request);
Expand Down
16 changes: 16 additions & 0 deletions packages/react-dom/unstable-fizz.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const ReactDOMFizzServerBrowser = require('./src/server/ReactDOMFizzServerBrowser');

// TODO: decide on the top-level export form.
// This is hacky but makes it work with both Rollup and Jest
module.exports = ReactDOMFizzServerBrowser.default || ReactDOMFizzServerBrowser;
1 change: 1 addition & 0 deletions packages/react-noop-renderer/src/ReactNoopServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ReactNoopServer = ReactFizzStreamer({
destination.push(JSON.parse(Buffer.from((buffer: any)).toString('utf8')));
},
completeWriting(destination: Destination): void {},
close(destination: Destination): void {},
flushBuffered(destination: Destination): void {},
convertStringToBuffer(content: string): Uint8Array {
return Buffer.from(content, 'utf8');
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/inline.dom-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// This file intentionally does *not* have the Flow annotation.
// Don't add it. See `./inline-typed.js` for an explanation.

export * from './src/ReactFiberReconciler';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from 'react-dom/src/client/ReactDOMHostConfig';
11 changes: 11 additions & 0 deletions packages/react-stream/inline.dom-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// This file intentionally does *not* have the Flow annotation.
// Don't add it. See `./inline-typed.js` for an explanation.

export * from './src/ReactFizzStreamer';
37 changes: 37 additions & 0 deletions packages/react-stream/src/ReactFizzHostConfigBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export type Destination = ReadableStreamController;

export function scheduleWork(callback: () => void) {
callback();
}

export function flushBuffered(destination: Destination) {
// WHATWG Streams do not yet have a way to flush the underlying
// transform streams. https://github.com/whatwg/streams/issues/960
}

export function beginWriting(destination: Destination) {}

export function writeChunk(destination: Destination, buffer: Uint8Array) {
destination.enqueue(buffer);
}

export function completeWriting(destination: Destination) {}

export function close(destination: Destination) {
destination.close();
}

const textEncoder = new TextEncoder();

export function convertStringToBuffer(content: string): Uint8Array {
return textEncoder.encode(content);
}
4 changes: 4 additions & 0 deletions packages/react-stream/src/ReactFizzHostConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export function completeWriting(destination: Destination) {
destination.uncork();
}

export function close(destination: Destination) {
destination.end();
}

export function convertStringToBuffer(content: string): Uint8Array {
return Buffer.from(content, 'utf8');
}
6 changes: 5 additions & 1 deletion packages/react-stream/src/ReactFizzStreamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
writeChunk,
completeWriting,
flushBuffered,
close,
} from './ReactFizzHostConfig';
import {formatChunk} from './ReactFizzFormatConfig';
import {REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
Expand All @@ -36,7 +37,8 @@ export function createRequest(

function performWork(request: OpaqueRequest): void {
let element = (request.children: any);
if (element.$$typeof !== REACT_ELEMENT_TYPE) {
request.children = null;
if (element && element.$$typeof !== REACT_ELEMENT_TYPE) {
return;
}
let type = element.type;
Expand All @@ -55,6 +57,7 @@ function performWork(request: OpaqueRequest): void {
function flushCompletedChunks(request: OpaqueRequest) {
let destination = request.destination;
let chunks = request.completedChunks;
request.completedChunks = [];

beginWriting(destination);
try {
Expand All @@ -65,6 +68,7 @@ function flushCompletedChunks(request: OpaqueRequest) {
} finally {
completeWriting(destination);
}
close(destination);
}

export function startWork(request: OpaqueRequest): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from 'react-dom/src/server/ReactDOMFizzServerFormatConfig';
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export const beginWriting = $$$hostConfig.beginWriting;
export const writeChunk = $$$hostConfig.writeChunk;
export const completeWriting = $$$hostConfig.completeWriting;
export const flushBuffered = $$$hostConfig.flushBuffered;
export const close = $$$hostConfig.close;
export const convertStringToBuffer = $$$hostConfig.convertStringToBuffer;
10 changes: 10 additions & 0 deletions packages/react-stream/src/forks/ReactFizzHostConfig.dom-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export * from '../ReactFizzHostConfigBrowser';
9 changes: 8 additions & 1 deletion scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,17 @@ const bundles = [
},

/******* React DOM Fizz Server *******/
{
bundleTypes: [NODE_DEV, NODE_PROD, UMD_DEV, UMD_PROD],
moduleType: RENDERER,
entry: 'react-dom/unstable-fizz.browser',
global: 'ReactDOMFizzServer',
externals: ['react'],
},
{
bundleTypes: [NODE_DEV, NODE_PROD, FB_WWW_DEV, FB_WWW_PROD],
moduleType: RENDERER,
entry: 'react-dom/unstable-fizz',
entry: 'react-dom/unstable-fizz.node',
global: 'ReactDOMFizzServer',
externals: ['react'],
},
Expand Down
Loading

0 comments on commit 8bd123b

Please sign in to comment.