Skip to content

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

Closed
@ncoder

Description

@ncoder

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions