Skip to content

Streaming Examples #58

@BeBoRE

Description

@BeBoRE

I find this to be a pretty interesting and promising project. Especially the streaming aspect of this. I am however failing to understand how to turn the result of toCrossJSONStream, back into an object on the other end. I already tried giving them separate refs and stuff. Maybe it would be handy if a few example projects are given to show-case how to make use of Seroval and it's streaming capabilities.

My current code:

import { toCrossJSONStream, fromCrossJSON } from "seroval";

const delay = <T>(delay : number, value : T) => new Promise<T>((resolve) => {
  setTimeout(() => resolve(value), delay)
})

const bla = {
  a: 'Hello',
  b: delay(1000, "World"),
  c: delay(2000, "Bla"),
  d: delay(3000, {
    e: 2500
  })
}

toCrossJSONStream(bla, {
  onParse(node, initial) {
      try {
        console.log(fromCrossJSON(node, {}))
      } catch (err : unknown) {
        if(err instanceof Error){
          console.log(err.cause)
        }
      }
  }
});

I get the errors:

$ node --experimental-strip-types index.ts
(node:37028) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{
  a: 'Hello',
  b: Promise { <pending> },
  c: Promise { <pending> },
  d: Promise { <pending> }
}
P [Error]: Missing "Promise" instance.
    at te.deserializePromiseResolve (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:12678)    
    at te.deserialize (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:14510)
    at ho (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:35797)
    at G.onParse [as onParseCallback] (file:///C:/Users/AfstOntw2/seroval/index.ts:21:21)
    at G.onParseInternal (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:31913)
    at G.onParse (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32098)
    at G.handlePromiseSuccess (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32993)
P [Error]: Missing "Promise" instance.
    at te.deserializePromiseResolve (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:12678)    
    at te.deserialize (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:14510)
    at ho (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:35797)
    at G.onParse [as onParseCallback] (file:///C:/Users/AfstOntw2/seroval/index.ts:21:21)
    at G.onParseInternal (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:31913)
    at G.onParse (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32098)
    at G.handlePromiseSuccess (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32993)
P [Error]: Missing "Promise" instance.
    at te.deserializePromiseResolve (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:12678)    
    at te.deserialize (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:14510)
    at ho (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:35797)
    at G.onParse [as onParseCallback] (file:///C:/Users/AfstOntw2/seroval/index.ts:21:21)
    at G.onParseInternal (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:31913)
    at G.onParse (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32098)
    at G.handlePromiseSuccess (file:///C:/Users/AfstOntw2/seroval/node_modules/.pnpm/seroval@1.3.1/node_modules/seroval/dist/esm/production/index.mjs:17:32993)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions