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

Allow ReadableStreams for parse #62

Open
plutoniumm opened this issue Feb 26, 2023 · 0 comments
Open

Allow ReadableStreams for parse #62

plutoniumm opened this issue Feb 26, 2023 · 0 comments

Comments

@plutoniumm
Copy link

I have a ServerSide JSON file that was encoded with devalue.stringify since it has Maps, doing a fetch call to it with .json doesn't work i.e consider

fetch("https://example.com/data.json")
.then(res => res.json()) // Breaks

But if I do (workaround)

fetch("https://example.com/data.json")
.then(res => res.text())
.then(data => devalue.parse(data)) // Works

So I think it would be nice if devalue.parse could accept a ReadableStream as well so that we can do something like one of the two below

fetch("https://example.com/data.json")
  .then(res => devalue.parse(res)) // This probably is the wrong way to approach it, not sure
  // similarly
  .then(res => res.devalueParse())

or alternatively

// via stream chunking
fetch("https://example.com/data.json")
  .then(res => {
    res.body.pipe( devalueParser );

    devalueParser.on( 'error', function ( error ) { } );
    devalueParser.on( 'readable', function () {
      let stream = this; // `this` is from `devalueParser`, which is a stream
      let item;

      let chunks = [];
      while ( item = stream.read() ) chunks.push( item );

      return chunks;
    } );
  })

I can see some obvious dumbness in my examples but the general idea is to be able to use a stream parser to not have to wait for the text and then parse it

@plutoniumm plutoniumm changed the title Allow ReadableStreams for parse (or maybe even stringify idk) Allow ReadableStreams for parse Feb 26, 2023
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

1 participant