@@ -172,6 +172,9 @@ const __PROTO__ = '__proto__';
172172const ObjectPrototype = Object . prototype ;
173173const ArrayPrototype = Array . prototype ;
174174
175+ type RESPONSE_SYMBOL_TYPE = 'RESPONSE_SYMBOL' ; // Fake symbol type.
176+ const RESPONSE_SYMBOL : RESPONSE_SYMBOL_TYPE = Symbol ( ) as any ;
177+
175178type PendingChunk < T > = {
176179 status : 'pending' ,
177180 value : null | Array < InitializationReference | ( T => mixed ) > ,
@@ -203,7 +206,7 @@ type BlockedChunk<T> = {
203206type ResolvedModelChunk < T > = {
204207 status : 'resolved_model' ,
205208 value : UninitializedModel ,
206- reason : Response ,
209+ reason : { [ RESPONSE_SYMBOL_TYPE ] : Response } ,
207210 _children : Array < SomeChunk < any >> | ProfilingResult , // Profiling-only
208211 _debugChunk : null | SomeChunk < ReactDebugInfoEntry > , // DEV-only
209212 _debugInfo : ReactDebugInfo , // DEV-only
@@ -865,7 +868,7 @@ function createResolvedModelChunk<T>(
865868 value: UninitializedModel,
866869): ResolvedModelChunk< T > {
867870 // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
868- return new ReactPromise ( RESOLVED_MODEL , value , response ) ;
871+ return new ReactPromise ( RESOLVED_MODEL , value , { [ RESPONSE_SYMBOL ] : response } ) ;
869872}
870873
871874function createResolvedModuleChunk< T > (
@@ -929,7 +932,9 @@ function createResolvedIteratorResultChunk<T>(
929932 const iteratorResultJSON =
930933 ( done ? '{"done":true,"value":' : '{"done":false,"value":' ) + value + '}' ;
931934 // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
932- return new ReactPromise ( RESOLVED_MODEL , iteratorResultJSON , response ) ;
935+ return new ReactPromise ( RESOLVED_MODEL , iteratorResultJSON , {
936+ [ RESPONSE_SYMBOL ] : response ,
937+ } ) ;
933938}
934939
935940function resolveIteratorResultChunk< T > (
@@ -963,7 +968,7 @@ function resolveModelChunk<T>(
963968 const resolvedChunk : ResolvedModelChunk < T > = chunk as any ;
964969 resolvedChunk . status = RESOLVED_MODEL ;
965970 resolvedChunk . value = value ;
966- resolvedChunk . reason = response ;
971+ resolvedChunk . reason = { [ RESPONSE_SYMBOL ] : response } ;
967972 if ( resolveListeners !== null ) {
968973 // This is unfortunate that we're reading this eagerly if
969974 // we already have listeners attached since they might no
@@ -1134,7 +1139,7 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
11341139 initializingHandler = null ;
11351140
11361141 const resolvedModel = chunk . value ;
1137- const response = chunk . reason ;
1142+ const response = chunk . reason [ RESPONSE_SYMBOL ] ;
11381143
11391144 // We go to the BLOCKED state until we've fully resolved this.
11401145 // We do this before parsing in case we try to initialize the same chunk
@@ -2070,6 +2075,15 @@ function loadServerReference<A: Iterable<any>, T>(
20702075 parentObject: Object,
20712076 key: string,
20722077): (...A) => Promise < T > {
2078+ if ( key === 'then' ) {
2079+ // This should never happen because we always serialize objects with then-functions
2080+ // as "thenable" which reduces to a chunk with no other fields.
2081+ return null as any ;
2082+ }
2083+ const id = metaData . id ;
2084+ if ( typeof id !== 'string' ) {
2085+ return null as any ;
2086+ }
20732087 if ( ! response . _serverReferenceConfig ) {
20742088 // In the normal case, we can't load this Server Reference in the current environment and
20752089 // we just return a proxy to it.
@@ -2082,27 +2096,27 @@ function loadServerReference<A: Iterable<any>, T>(
20822096 }
20832097 // If we have a module mapping we can load the real version of this Server Reference.
20842098 const serverReference : ClientReference < T > =
2085- resolveServerReference < $FlowFixMe > (
2086- response . _serverReferenceConfig ,
2087- metaData . id ,
2088- ) ;
2099+ resolveServerReference < $FlowFixMe > ( response . _serverReferenceConfig , id ) ;
20892100
2101+ const bound = metaData . bound ;
20902102 let promise : null | Thenable < any > = preloadModule ( serverReference ) ;
20912103 if ( ! promise ) {
2092- if ( ! metaData . bound ) {
2104+ if ( bound instanceof ReactPromise ) {
2105+ promise = Promise . resolve ( bound ) ;
2106+ } else {
2107+ // We only await the bound arguments when they are one of our own chunks.
2108+ // Anything else could be a foreign thenable whose `then` we must not call.
20932109 const resolvedValue = requireModule ( serverReference ) as any ;
20942110 registerBoundServerReference (
20952111 resolvedValue ,
2096- metaData . id ,
2097- metaData . bound ,
2112+ id ,
2113+ bound ,
20982114 response . _encodeFormAction ,
20992115 ) ;
21002116 return resolvedValue ;
2101- } else {
2102- promise = Promise . resolve ( metaData . bound ) ;
21032117 }
2104- } else if ( metaData . bound ) {
2105- promise = Promise . all ( [ promise , metaData . bound ] ) ;
2118+ } else if ( bound instanceof ReactPromise ) {
2119+ promise = Promise . all ( [ promise , bound ] ) ;
21062120 }
21072121
21082122 let handler : InitializationHandler ;
@@ -2123,17 +2137,20 @@ function loadServerReference<A: Iterable<any>, T>(
21232137 function fulfill ( ) : void {
21242138 let resolvedValue = requireModule ( serverReference ) as any ;
21252139
2126- if ( metaData . bound ) {
2127- // This promise is coming from us and should have initilialized by now.
2128- const boundArgs : Array < any > = ( metaData . bound as any ) . value . slice ( 0 ) ;
2140+ if ( bound ) {
2141+ // This promise is coming from us and should have initialized by now.
2142+ const promiseValue = ( bound as any ) . value ;
2143+ const boundArgs : Array < any > = isArray ( promiseValue )
2144+ ? promiseValue . slice ( 0 )
2145+ : [ ] ;
21292146 boundArgs . unshift ( null ) ; // this
21302147 resolvedValue = resolvedValue . bind . apply ( resolvedValue , boundArgs ) ;
21312148 }
21322149
21332150 registerBoundServerReference (
21342151 resolvedValue ,
2135- metaData . id ,
2136- metaData . bound ,
2152+ id ,
2153+ bound ,
21372154 response . _encodeFormAction ,
21382155 ) ;
21392156
@@ -2584,7 +2601,17 @@ function extractIterator(response: Response, model: Array<any>): Iterator<any> {
25842601 return model [ Symbol . iterator ] ( ) ;
25852602}
25862603
2587- function createModel ( response : Response , model : any ) : any {
2604+ function createModel (
2605+ response : Response ,
2606+ model : any ,
2607+ parentObject : Object ,
2608+ key : string ,
2609+ ) : any {
2610+ if ( key === 'then ' && typeof model === 'function ') {
2611+ // This should never happen because we always serialize objects with then-functions
2612+ // as "thenable" which reduces to a chunk with no other fields.
2613+ return null ;
2614+ }
25882615 return model ;
25892616}
25902617
0 commit comments