Skip to content

Commit c7ba2b8

Browse files
acdliteAndyPengc12
authored andcommitted
useFormState: Hash the component key path for more compact output (facebook#27397)
To support MPA-style form submissions, useFormState sends down a key that represents the identity of the hook on the page. It's based on the key path of the component within the React tree; for deeply nested hooks, this keypath can become very long. We can hash the key to make it shorter. Adds a method called createFastHash to the Stream Config interface. We're not using this for security or obfuscation, only to generate a more compact key without sacrificing too much collision resistance. - In Node.js builds, createFastHash uses the built-in crypto module. - In Bun builds, createFastHash uses Bun.hash. See: https://bun.sh/docs/api/hashing#bun-hash I have not yet implemented createFastHash in the Edge, Browser, or FB (Hermes) stream configs because those environments do not have a built-in hashing function that meets our requirements. (We can't use the web standard `crypto` API because those methods are async, and yielding to the main thread is too costly to be worth it for this particular use case.) We'll likely use a pure JS implementation in those environments; for now, they just return the original string without hashing it. I'll address this in separate PRs.
1 parent 18f443f commit c7ba2b8

File tree

14 files changed

+58
-12
lines changed

14 files changed

+58
-12
lines changed

.eslintrc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,7 @@ module.exports = {
415415
},
416416
},
417417
{
418-
files: [
419-
'packages/react-native-renderer/**/*.js',
420-
],
418+
files: ['packages/react-native-renderer/**/*.js'],
421419
globals: {
422420
nativeFabricUIManager: 'readonly',
423421
},

packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
7676
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
7777
destination.destroy(error);
7878
}
79+
80+
export function createFastHash(input: string): string | number {
81+
return input;
82+
}

packages/react-server-dom-fb/src/ReactServerStreamConfigFB.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
8383
destination.fatal = true;
8484
destination.error = error;
8585
}
86+
87+
export function createFastHash(input: string): string | number {
88+
return input;
89+
}

packages/react-server/src/ReactFizzHooks.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {getTreeId} from './ReactFizzTreeContext';
2727
import {createThenableState, trackUsedThenable} from './ReactFizzThenable';
2828

2929
import {makeId, NotPendingTransition} from './ReactFizzConfig';
30+
import {createFastHash} from './ReactServerStreamConfig';
3031

3132
import {
3233
enableCache,
@@ -592,11 +593,16 @@ function createPostbackFormStateKey(
592593
hookIndex: number,
593594
): string {
594595
if (permalink !== undefined) {
596+
// Don't bother to hash a permalink-based key since it's already short.
595597
return 'p' + permalink;
596598
} else {
597599
// Append a node to the key path that represents the form state hook.
598600
const keyPath: KeyNode = [componentKeyPath, null, hookIndex];
599-
return 'k' + JSON.stringify(keyPath);
601+
// Key paths are hashed to reduce the size. It does not need to be secure,
602+
// and it's more important that it's fast than that it's completely
603+
// collision-free.
604+
const keyPathHash = createFastHash(JSON.stringify(keyPath));
605+
return 'k' + keyPathHash;
600606
}
601607
}
602608

packages/react-server/src/ReactServerStreamConfigBrowser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
182182
destination.close();
183183
}
184184
}
185+
186+
export function createFastHash(input: string): string | number {
187+
return input;
188+
}

packages/react-server/src/ReactServerStreamConfigBun.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @flow
88
*/
99

10+
/* global Bun */
11+
1012
type BunReadableStreamController = ReadableStreamController & {
1113
end(): mixed,
1214
write(data: Chunk | BinaryChunk): void,
@@ -96,3 +98,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
9698
destination.close();
9799
}
98100
}
101+
102+
export function createFastHash(input: string): string | number {
103+
return Bun.hash(input);
104+
}

packages/react-server/src/ReactServerStreamConfigEdge.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,7 @@ export function closeWithError(destination: Destination, error: mixed): void {
182182
destination.close();
183183
}
184184
}
185+
186+
export function createFastHash(input: string): string | number {
187+
return input;
188+
}

packages/react-server/src/ReactServerStreamConfigNode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import type {Writable} from 'stream';
1111

1212
import {TextEncoder} from 'util';
13+
import {createHash} from 'crypto';
1314

1415
interface MightBeFlushable {
1516
flush?: () => void;
@@ -243,3 +244,9 @@ export function closeWithError(destination: Destination, error: mixed): void {
243244
// $FlowFixMe[incompatible-call]: This is an Error object or the destination accepts other types.
244245
destination.destroy(error);
245246
}
247+
248+
export function createFastHash(input: string): string | number {
249+
const hash = createHash('md5');
250+
hash.update(input);
251+
return hash.digest('hex');
252+
}

packages/react-server/src/forks/ReactServerStreamConfig.custom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ export const typedArrayToBinaryChunk = $$$config.typedArrayToBinaryChunk;
4444
export const clonePrecomputedChunk = $$$config.clonePrecomputedChunk;
4545
export const byteLengthOfChunk = $$$config.byteLengthOfChunk;
4646
export const byteLengthOfBinaryChunk = $$$config.byteLengthOfBinaryChunk;
47+
export const createFastHash = $$$config.createFastHash;

scripts/flow/environment.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,9 @@ declare module 'node:worker_threads' {
281281
port2: MessagePort;
282282
}
283283
}
284+
285+
declare var Bun: {
286+
hash(
287+
input: string | $TypedArray | DataView | ArrayBuffer | SharedArrayBuffer,
288+
): number,
289+
};

0 commit comments

Comments
 (0)