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

Use different transformers for client<->server #378

Closed
simonedelmann opened this issue May 17, 2021 · 6 comments · Fixed by #393
Closed

Use different transformers for client<->server #378

simonedelmann opened this issue May 17, 2021 · 6 comments · Fixed by #393
Labels
👉 good first issue Good for newcomers

Comments

@simonedelmann
Copy link
Contributor

First of all I want to say thank you very much for making this library! I was already looking for a library like this for quite some time.

There is one thing I would like to ask/suggest:

Superjson is great for preserving type information, but has some performance drops for example when using with lots of nested dates. There is another library, devalue, which has a much better performance, but it is not usably for client->server for security reasons.

Thats why I currently use superjson for client->server and devalue for server->client in some of my apps.

Is it currently possible to use different transformers for each direction? If not, I would like to suggest adding this.

@KATT
Copy link
Member

KATT commented May 17, 2021

Hmm.. there is no such option right now, and I'd be hesitant to build it as it feels a bit like a can of worms to differentiate this in each direction and a bit fiddly to add. If devalue can't be used for serialization from client->server you'd still have the same bottleneck when passing input args from client->server, although they are indeed usually much smaller.

Are you sure you're not doing premature optimization? Is the superjson really that slow?

I do find superjson a bit overkill myself as I only really care about dates and nothing else (and I find date objects a bit annoying as well as equality checks on two different dates - maybe write your own lib called jsonplusdates?

export type DataTransformer = {
serialize(object: any): any;
deserialize(object: any): any;
};

@simonedelmann
Copy link
Contributor Author

Thanks for your very fast answer! I totally see adding this as an option could be difficult and I absolutely agree that dates are annoying. :D

Are you sure you're not doing premature optimization? Is the superjson really that slow?

This only occurs in one specific app, where I have a huge collection (~1000) of models and every model contains another collection of models (~50). All models need to have multiple dates. Yes, I could do some optimization (strip non-necessary updatedAt field for example), but still the conversion with superjson would take >300ms.

I made a short test with the benchmark script provided by superjson:

Serializing + deserializing a nested object with 50.000 dates

Superjson took 482 ms
Devalue took 70 ms

So superjson is not that slow, but devalue still is much faster and in my case I would like to use that extra performance.

Would it take a big effort to modify the transformer interface like this? This should not break any existing implementation.

export type DataTransformer = {
    serialize(object: any, direction: 'up' | 'down'): any;
    deserialize(object: any, direction: 'up' | 'down'): any;  
}; 

PS: Sorry for submitting the comment to early...

@KATT
Copy link
Member

KATT commented May 18, 2021

I was thinking something like this to avoid breaking change

export type DataTransformer = {
    serialize(object: any): any;
    deserialize(object: any): any;  
};

export type DataTransformerOption = DataTransformer | { up: DataTransformer, down: DataTransformer }

I don't really have time to do this + the tests required, I'd be open to merging it if you could figure it out? I'd open VSCode & search for everywhere in the project where the keyword "transformer" is used and start hacking

@KATT
Copy link
Member

KATT commented May 18, 2021

Development workflow

git clone git@github.com:trpc/trpc.git
cd trpc
yarn

Hacking around with it

In one terminal, will run preconstruct watch in parallel which builds all packages/* on change:

yarn dev

In another terminal, you can for instance navigate to examples/next-hello-world and run yarn dev & it will update whenever code is changed in the packages.

Testing

cd packages/server
yarn test --watch

@simonedelmann
Copy link
Contributor Author

simonedelmann commented May 18, 2021

Thank you! I will have a look and try to implement that! Probably taking some days playing around with the library first.

Edit: See here for a draft PR (#393)

@github-actions
Copy link

github-actions bot commented Oct 5, 2022

This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
👉 good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants