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

Parsing is incorrect with vercel edge runtime #242

Closed
mattiaz9 opened this issue Jun 25, 2023 · 5 comments · Fixed by #243
Closed

Parsing is incorrect with vercel edge runtime #242

mattiaz9 opened this issue Jun 25, 2023 · 5 comments · Fixed by #243

Comments

@mattiaz9
Copy link
Contributor

NextJS Example:

import SuperJSON from "superjson"

export const runtime = "edge"

export function GET() {
  const input =
    '{"json":{"page":1,"perPage":20,"orderBy":null,"sortDirection":null,"query":null},"meta":{"values":{"orderBy":["undefined"],"sortDirection":["undefined"],"query":["undefined"]}}}'

  const parsed = SuperJSON.parse<any>(input)

  return NextResponse.json(parsed)
}

In this example the fields with meta value undefined are set as null:

{
  "page": 1,
  "perPage": 20,
  "orderBy": null,
  "sortDirection": null,
  "query": null
}

Switching the runtime to nodejs results instead in a correct output:

{
  "page": 1,
  "perPage": 20
}
@mattiaz9
Copy link
Contributor Author

So.. i found the issue. In this code:

export const isPlainObject = (
  payload: any
): payload is { [key: string]: any } => {
  if (typeof payload !== 'object' || payload === null) return false;
  if (payload === Object.prototype) return false;
  if (Object.getPrototypeOf(payload) === null) return true;

  return (
    payload.constructor === Object &&
    Object.getPrototypeOf(payload) === Object.prototype
  );
};

payload.constructor === Object fails with edge runtime. Is that double check even necessary?

I tested it locally by evaluating the code with the edge-runtime npm package and by just using Object.getPrototypeOf(payload) === Object.prototype it works fine.

@Skn0tt
Copy link
Collaborator

Skn0tt commented Jun 26, 2023

Nice find! This check seems to be from the very beginnings of SuperJSON: c617bc7#diff-4c1f145257b72d7f61ed609a0604c966c68010147841b7d45f164c8ef3f8360aR15-R16

Let's try to remove the .constructor check, yes. @mattiaz9 do you want to open a PR for this? that'd be lovely :D

@mattiaz9
Copy link
Contributor Author

sure!

@Skn0tt
Copy link
Collaborator

Skn0tt commented Jun 26, 2023

@Skn0tt
Copy link
Collaborator

Skn0tt commented Jun 26, 2023

cc @KATT

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

Successfully merging a pull request may close this issue.

2 participants