Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useFormState: Hash the component key path for more compact output #27397

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ module.exports = {
},
},
{
files: [
'packages/react-native-renderer/**/*.js',
],
files: ['packages/react-native-renderer/**/*.js'],
globals: {
nativeFabricUIManager: 'readonly',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
destination.destroy(error);
}

export function createFastHash(input: string): string | number {
return input;
}
4 changes: 4 additions & 0 deletions packages/react-server-dom-fb/src/ReactServerStreamConfigFB.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.fatal = true;
destination.error = error;
}

export function createFastHash(input: string): string | number {
return input;
}
8 changes: 7 additions & 1 deletion packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {getTreeId} from './ReactFizzTreeContext';
import {createThenableState, trackUsedThenable} from './ReactFizzThenable';

import {makeId, NotPendingTransition} from './ReactFizzConfig';
import {createFastHash} from './ReactServerStreamConfig';

import {
enableCache,
Expand Down Expand Up @@ -592,11 +593,16 @@ function createPostbackFormStateKey(
hookIndex: number,
): string {
if (permalink !== undefined) {
// Don't bother to hash a permalink-based key since it's already short.
return 'p' + permalink;
} else {
// Append a node to the key path that represents the form state hook.
const keyPath: KeyNode = [componentKeyPath, null, hookIndex];
return 'k' + JSON.stringify(keyPath);
// Key paths are hashed to reduce the size. It does not need to be secure,
// and it's more important that it's fast than that it's completely
// collision-free.
const keyPathHash = createFastHash(JSON.stringify(keyPath));
return 'k' + keyPathHash;
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.close();
}
}

export function createFastHash(input: string): string | number {
return input;
}
6 changes: 6 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigBun.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @flow
*/

/* global Bun */

type BunReadableStreamController = ReadableStreamController & {
end(): mixed,
write(data: Chunk | BinaryChunk): void,
Expand Down Expand Up @@ -96,3 +98,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.close();
}
}

export function createFastHash(input: string): string | number {
return Bun.hash(input);
}
4 changes: 4 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
destination.close();
}
}

export function createFastHash(input: string): string | number {
return input;
}
7 changes: 7 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {Writable} from 'stream';

import {TextEncoder} from 'util';
import {createHash} from 'crypto';

interface MightBeFlushable {
flush?: () => void;
Expand Down Expand Up @@ -243,3 +244,9 @@ export function closeWithError(destination: Destination, error: mixed): void {
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
destination.destroy(error);
}

export function createFastHash(input: string): string | number {
const hash = createHash('md5');
hash.update(input);
return hash.digest('hex');
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export const typedArrayToBinaryChunk = $$$config.typedArrayToBinaryChunk;
export const clonePrecomputedChunk = $$$config.clonePrecomputedChunk;
export const byteLengthOfChunk = $$$config.byteLengthOfChunk;
export const byteLengthOfBinaryChunk = $$$config.byteLengthOfBinaryChunk;
export const createFastHash = $$$config.createFastHash;
6 changes: 6 additions & 0 deletions scripts/flow/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,9 @@ declare module 'node:worker_threads' {
port2: MessagePort;
}
}

declare var Bun: {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really have separate libdefs for each environment but since they're already all mixed together in this one file already I just tacked it on here. Can improve later.

hash(
input: string | $TypedArray | DataView | ArrayBuffer | SharedArrayBuffer,
): number,
};
16 changes: 8 additions & 8 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ const bundles = [
global: 'ReactDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async_hooks', 'react-dom'],
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
},
{
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
Expand Down Expand Up @@ -339,7 +339,7 @@ const bundles = [
global: 'ReactServerDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async_hooks', 'react-dom'],
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
},
{
bundleTypes: [NODE_DEV, NODE_PROD],
Expand All @@ -348,7 +348,7 @@ const bundles = [
global: 'ReactServerDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async_hooks', 'react-dom'],
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
},
{
bundleTypes: [NODE_DEV, NODE_PROD],
Expand All @@ -357,7 +357,7 @@ const bundles = [
global: 'ReactServerDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async_hooks', 'react-dom'],
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
},

/******* React Server DOM Webpack Client *******/
Expand All @@ -377,7 +377,7 @@ const bundles = [
global: 'ReactServerDOMClient',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'react-dom', 'util'],
externals: ['react', 'react-dom', 'util', 'crypto'],
},
{
bundleTypes: [NODE_DEV, NODE_PROD],
Expand All @@ -386,7 +386,7 @@ const bundles = [
global: 'ReactServerDOMClient',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'react-dom', 'util'],
externals: ['react', 'react-dom', 'util', 'crypto'],
},
{
bundleTypes: [NODE_DEV, NODE_PROD],
Expand Down Expand Up @@ -439,7 +439,7 @@ const bundles = [
entry: 'react-server-dom-esm/server.node',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'async_hooks', 'react-dom'],
externals: ['react', 'util', 'crypto', 'async_hooks', 'react-dom'],
},

/******* React Server DOM ESM Client *******/
Expand All @@ -457,7 +457,7 @@ const bundles = [
entry: 'react-server-dom-esm/client.node',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'react-dom', 'util'],
externals: ['react', 'react-dom', 'util', 'crypto'],
},

/******* React Server DOM ESM Node.js Loader *******/
Expand Down
2 changes: 2 additions & 0 deletions scripts/rollup/validate/eslintrc.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ module.exports = {

// Native Scheduler
nativeRuntimeScheduler: 'readonly',

Bun: 'readonly',
},
parserOptions: {
ecmaVersion: 2020,
Expand Down
2 changes: 2 additions & 0 deletions scripts/rollup/validate/eslintrc.cjs2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module.exports = {

// act
IS_REACT_ACT_ENVIRONMENT: 'readonly',

Bun: 'readonly',
},
parserOptions: {
ecmaVersion: 2015,
Expand Down
2 changes: 2 additions & 0 deletions scripts/rollup/validate/eslintrc.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ module.exports = {

// act
IS_REACT_ACT_ENVIRONMENT: 'readonly',

Bun: 'readonly',
},
parserOptions: {
ecmaVersion: 2020,
Expand Down