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

server$ consistent return between client and server #9

Open
arbassett opened this issue Mar 8, 2023 · 2 comments
Open

server$ consistent return between client and server #9

arbassett opened this issue Mar 8, 2023 · 2 comments

Comments

@arbassett
Copy link

arbassett commented Mar 8, 2023

Describe the bug

server$ server call is not being serialized on the sever to match the network boundary which can result in runtime errors if passing un-serializable objects like Date/Map/Set

on the server the function in called directly

/**
*  currently in the context of the server render you can basically think this as const fn = () => new Date()
*/
const $$server_module0 = fetch$.createHandler(async function $$serverHandler0() {
  return new Date();
}, "/_m/43dde1d673/getNow", undefined);
fetch$.registerHandler("/_m/43dde1d673/getNow", $$server_module0);

and on the client its a fetch to the endpoint

/** 
* again you can think of this as const fn = () => fetch('/_m/0dbe216f23/clientDate')
*/
const $$server_module0 = fetch$.createFetcher('/_m/7d3e2194d0/clientDate', void 0),

This also effects solid-start solidjs/solid-start#792. Bling currently does try to parse client and server the same but only for Reponse objects

another thing to note is when this is fixed server$ return type will not reflect what is really returned.. for simple values like number object and string it is correct.. but when returning something that is not serializable like Date Map or Set. the return types don't match what is being parsed on the wire. ex Date -> string, Map -> {}.

TRPC had this same issue trpc/trpc#3261 i prototypes fixing the return types for solid-start by copying what TRPC did but this only handles using JSON.parse & stringify. with bling supporting custom serializer & deserializer types should refelect this somehow

Your Example Website or App

https://stackblitz.com/edit/github-uqyvwn?file=examples%2Fastro-solid%2Fpackage.json,examples%2Fastro-solid%2Fsrc%2Fapp%2Froot.tsx

Steps to Reproduce the Bug or Issue

  • build bling dev
  • Run astro-solid example
  • go to /
  • server console says response is date while browser console reports string

Expected behavior

The return type should reflect the network boundary and serialize the response

Screenshots or Videos

No response

Platform

  • OS: [Linux]
  • Browser: [Chrome, Safari, Firefox]
  • Version: [commit 98a28d8]

Additional context

No response

@arbassett
Copy link
Author

arbassett commented Mar 8, 2023

Something to also note this happens for parameter's as well if you have a function that expects a Date the server render will succeed and pass a date object and on client render it will get serialized to a string causing the server to crash.

const clientDate = server$(async (date: Date) => {
  console.log('datePassedToServer', 'type', typeof date, 'value', date)
  // the server render will get a Date but when the client calls it will be a string
  console.log('toLocaleDateString()', date.toLocaleDateString()) 
})

clientDate(new Date()); // this causes the server to crash when the client calls it

i updated the example to show this as well

@antl3x
Copy link

antl3x commented Sep 18, 2023

What about support eJson like meteor ddp?

https://github.com/primus/ejson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants