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

Protect multiple RSA requesters from each other #866

Merged
merged 1 commit into from
Mar 10, 2017
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
38 changes: 18 additions & 20 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));
Copy link
Member

Choose a reason for hiding this comment

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

I apparently can't leave comments willy-nilly in this file, but should we do the same thing for error responses (line ~209)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's annoying that you can click to see those lines but can't comment on them. 👿

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, didn't realize those were handled separately. 👍

}

setError (err) {
Expand All @@ -218,17 +212,21 @@ class CacheEntry {
this.dfd.reject(err);
}

_parsePromise(dfd) {
return dfd.promise.then(val => JSON.parse(val));
}

whenDataReady () {
if (SERVER_SIDE) {
// 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._parsePromise(this.dfd);
} else {
// client-side, whenever someone retrieves data from the cache,
// we decrement the number of retrievals expected, and when we
// hit zero, remove the cache entry.
return this._requesterDecrementingPromise(this.dfd.promise);
return this._requesterDecrementingPromise(this.dfd);
}
}

Expand All @@ -250,11 +248,11 @@ class CacheEntry {
* Chain a promise with another promise that decrements
* the number of expected requesters.
*/
_requesterDecrementingPromise (promise) {
_requesterDecrementingPromise (dfd) {
// regardless of whether we're resolved with a 'res' or 'err',
// we want to decrement requests. the appropriate 'success' or 'error'
// callback will be executed on whatever is chained after this method
return promise.fin( resOrErr => {
return this._parsePromise(dfd).fin( resOrErr => {
this.decrementRequesters();
return resOrErr;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,10 @@ describe("ReactServerAgent", () => {
]).then(results => {
var [res1, res2] = results;

expect(res1).toBe(res2);
expect(res1).toEqual(res2);

// Must be a deep copy, not a reference.
expect(res1).not.toBe(res2);

var cache = ReactServerAgent.cache();
var dehydrated = cache.dehydrate();
Expand Down Expand Up @@ -566,7 +569,10 @@ describe("ReactServerAgent", () => {
]).then(results => {
var [res1, res2] = results;

expect(res1).toBe(res2);
expect(res1).toEqual(res2);

// Must be a deep copy, not a reference.
expect(res1).not.toBe(res2);

var cache = ReactServerAgent.cache();
var dehydrated = cache.dehydrate();
Expand Down