Synchronous Stream Support

I’m working on a cross-platform specification and associated reference implementations, targeting CBOR.

Although the JavaScript implementation ( GitHub - cyberphone/CBOR.js: CBOR JavaScript API and Reference Implementation ) is quite nice, it sucks a bit compared the Java and Python counterparts. In the latter, input to the decoder is a virtual stream making decoders more adaptable to different use-cases. In the JavaScript version of the decoder, input is limited a static Uint8Array. I have not found any way to use the existing stream system, but I’m not an expert on this either :face_with_raised_eyebrow:

Application note:

The corresponding solution in JavaScript is not very elegant:

thanx,
Anders

Hey, I'm working on a definition of streams that's made for your exact use case.

I've gone ahead and made my own protocol which I call a "stream iterator". Here's a slightly esoteric example that shows how you might construct a simple stream iterator.

let repeat = (value, batchSize = Infinity) => {
  let done = false, i = -1;

  return {
    next() {
      return ++i % batchSize > 0
        ? { done, value }
        : Promise.resolve({ done, value })
    }
  } 
}

Generally the benefit of doing things this way is you get something that's sync when it can be, and async when it needs to be. For transforms this has exactly the benefit you want: it lets you define a transform once and use it anywhere. You can see this with a UTF8 decoding transform that I wrote for my (very simple) filesystem access library. It's just as happy to synchronously turn an array of bytes into a string as it is decode the contents of a network stream as it comes in over the wire. It looks like this: bablr-fs-node/lib/index.js at ca5931b8243612ff77032c59a04de26a6e81b21e · bablr-lang/bablr-fs-node · GitHub

If you do choose to use this style of iterator, I recommend that you mark it as iterable like this:

let streamIterable = {
  [Symbol.for('@@iterator')]() {
    return  /* ... */
  }
}

Thank you very much. I can’t say I understand everything (synchronous streams are so much simpler), but I will give it a try!