webstreams-converter
TypeScript icon, indicating that this package has built-in type declarations

0.1.5 • Public • Published

webstreams-converter

This package has two functions for web streams.

  • webStreamsFrom(iterable) is stream.Readable.from implementation for web streams.

  • webStreams2AsyncIterator(readableStream) is Async Iteration implementation for web streams in browser. web streams spec has this, but no browserimplementation this yet.

Zero dependency! And package working in node.js (> 18) and browser.

streams sepc: https://streams.spec.whatwg.org/

MDN doc: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API

API

webStreamsFrom(iterable)

Parameter iterable is any value meet iterable

strings or TypedArray will only in one chunk.

import { webStreamsFrom, webStreams2AsyncIterator } from 'webstreams-converter';

function* testGen() {
  yield 'a';
  yield 'b';
  yield 'c';
  return 'ddd';
}

let result = '';
const read = webStreamsFrom(testGen());

webStreams2AsyncIterator(readableStream)

Parameter readableStream is instance of ReadableStream

strings or TypedArray will only in one chunk.

import { webStreamsFrom, webStreams2AsyncIterator } from 'webstreams-converter';

const read = webStreamsFrom(['a', 'b', 'c']);
for await (const val of webStreams2AsyncIterator(read)) {
  console.log(val);
}

Install

npm

$ npm i webstreams-converter

jsdelivr

ESM

<script type="module">
  import {
    webStreamsFrom,
    webStreams2AsyncIterator,
  } from 'https://cdn.jsdelivr.net/npm/webstreams-converter@latest';
  const read = webStreamsFrom(['a', 'b', 'c']);
  for await (const val of webStreams2AsyncIterator(read)) {
    console.log(val);
  }
</script>

UMD

<script src="https://cdn.jsdelivr.net/npm/webstreams-converter@latest/dist/webstreams-converter.umd.js"></script>
<script>
  const webStreamsFrom = window['webstreams-converter'].webStreamsFrom;
  const webStreams2AsyncIterator =
    window['webstreams-converter'].webStreams2AsyncIterator;

  const read = webStreamsFrom(['a', 'b', 'c']);
  (async () => {
    for await (const val of webStreams2AsyncIterator(read)) {
      console.log(val);
    }
  })();
</script>

Package Sidebar

Install

npm i webstreams-converter

Weekly Downloads

0

Version

0.1.5

License

MIT

Unpacked Size

13.5 kB

Total Files

22

Last publish

Collaborators

  • zizifn