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

500 error on client-side render: devalue.stringify fails to generate valid JSON in utils.js:serialize_data_node() #9371

Open
ncoder opened this issue Mar 9, 2023 · 3 comments

Comments

@ncoder
Copy link

ncoder commented Mar 9, 2023

Describe the bug

a load() handler returns lots of strings, some of which contain extended characters.

in serialize_data_node(), svelte uses devalue to turn it into a json string.

	const stringified = devalue.stringify(node.data);

later, on the client, response.json() gets called and fails because the json is invalid.

Reproduction

I have a 30k line payload causing the trouble... so its' a bit hard to pinpoint it, but you can get an idea of what can cause the problem with this toy snippet, which reproduces the basic problem:

> JSON.parse(devalue.stringify(decoder.write(Buffer.from([0x00,0x11]))))

> const devalue = await import('devalue');
> const { StringDecoder } = require('node:string_decoder');
> const decoder = new StringDecoder('utf8');

// intentionally bad input string:
> devalue.stringify(decoder.write(Buffer.from([0x00,0x11])))
'["\\u0000\x11"]'
> JSON.parse(devalue.stringify(decoder.write(Buffer.from([0x00,0x11]))))
Uncaught SyntaxError: Unexpected token  in JSON at position 8

// expected it would roundtrip fine like built-in:
> JSON.parse(JSON.stringify(decoder.write(Buffer.from([0x00,0x11]))))
'\x00\x11'

Logs

I get a variety of errors on the client, depending on which character is bad

Bad control character in string literal in JSON at position 18230
Uncaught SyntaxError SyntaxError: Unexpected token � in JSON at position 18168

System Info

node: 'v16.19.0'
devalue: "4.2.0",
svelte: "3.55.0",

Severity

serious, but I can work around it

Additional Information

No response

@ncoder
Copy link
Author

ncoder commented Mar 9, 2023

Rich-Harris/devalue#6

@Conduitry
Copy link
Member

Are you serving your app in a way that it is not getting the appropriate charset=utf-8 part of the Content-Type header? That was the situation I was trying to deal with in that devalue issue.

@gtm-nayan
Copy link
Contributor

This should've been fixed by Rich-Harris/devalue#57, at least as far as devalue won't generate JSON with invalid characters,

but your HTML of course needs to have the correct encoding set for the embedding HTML document to parse correctly as well

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

3 participants