Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
fix(memoryLeak): disable cache for transitjs (#993)
Browse files Browse the repository at this point in the history
* fix(memoryLeak): disable cache for transitjs

* chore(pr): pr comments addressed

---------

Co-authored-by: Jonny Adshead <JAdshead@users.noreply.github.com>
  • Loading branch information
Matthew-Mallimo and JAdshead committed May 16, 2023
1 parent 7d75d90 commit bb83c07
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/universal/utils/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
// the browser or on the server
/* eslint-disable global-require */

import transit from 'transit-immutable-js';
import instance, { handlers } from 'transit-immutable-js';
import transit from 'transit-js';
import { serializeError } from 'serialize-error';

const concealOrigin = (href) => href && href.replace(/\/\/[^/]+/g, '//***');
Expand All @@ -27,15 +28,14 @@ export function writeError(value) {
const error = serializeError(value);
delete error.stack;
error.message = concealOrigin(error.message);

if (error.response) {
error.response.url = concealOrigin(error.response.url);
}

return error;
}

export default transit.withExtraHandlers([
const extraHandlers = [
{
tag: 'error',
class: Error,
Expand All @@ -57,4 +57,33 @@ export default transit.withExtraHandlers([
: require('url').parse(value)
),
},
]);
];

const modifiedHandlers = handlers.withExtraHandlers(extraHandlers);

const reader = transit.reader('json', {
handlers: modifiedHandlers.read,
cache: false,
// Taken from transit-immutable-js. Without this, Error fails to read.
// https://github.com/glenjamin/transit-immutable-js/blob/cb0ac0799d730080ea2403dba4061cf9c9d7b9bd/index.js#L6
mapBuilder: {
init() {
return {};
},
add(m, k, v) {
// eslint-disable-next-line no-param-reassign -- Keeping what is done by transit-immutable-js
m[k] = v;
return m;
},
finalize(m) {
return m;
},
},
});
const writer = transit.writer('json', { handlers: modifiedHandlers.write, cache: false });

export default {
...instance.withExtraHandlers(extraHandlers), // added for test cases
toJSON: (data) => writer.write(data),
fromJSON: (json) => reader.read(json),
};

0 comments on commit bb83c07

Please sign in to comment.