Skip to content

Commit

Permalink
Protect multiple RSA requesters from each other
Browse files Browse the repository at this point in the history
If two ReactServerAgent requests are made to a given endpoint only one
upstream http request is actually issued, and the result is provided to both
requesters.  Previously this result was passed by reference, so mutations
by one requester interfered with the data for others.

This patch provides a fresh deep copy to each requester.

This has the unfortunate side effect of introducing a deep copy in the browser
where we previously thought we could get away without one.  It's a minor perf
hit, but it's important for data integrity.
  • Loading branch information
gigabo committed Feb 1, 2017
1 parent fef3417 commit 02ef5be
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions packages/react-server/core/ReactServerAgent/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,16 @@ class CacheEntry {
this.res = res;
this.loaded = true;

if (SERVER_SIDE){

// Deep copy.
//
// Leave ourselves with a clean copy of the original
// response regardless of what mutation might happen
// once stores get ahold of it.
//
// This is important to ensure that we provide the same
// data from the cache when we wake up in the browser
// as we initially provide on the server.
//
res = JSON.parse(JSON.stringify(res));
}

this.dfd.resolve(res);
// Resolve with a serialized copy. We'll unserialize for each
// requester. This way we provide a fresh copy each time so mutations
// don't leak.
//
// This also leaves _us_ with a clean copy of the original response.
// This is important to ensure that we provide the same data from the
// cache when we wake up in the browser as we initially provide on the
// server.
//
this.dfd.resolve(JSON.stringify(res));
}

setError (err) {
Expand Down Expand Up @@ -223,7 +217,7 @@ class CacheEntry {
// server-side, we increment the number of requesters
// we expect to retrieve the data on the frontend
this.requesters += 1;
return this.dfd.promise;
return this.dfd.promise.then(val => JSON.parse(val));
} else {
// client-side, whenever someone retrieves data from the cache,
// we decrement the number of retrievals expected, and when we
Expand Down

0 comments on commit 02ef5be

Please sign in to comment.