1
0
Fork
You've already forked iterable-reader
0
Convert Uint8Array iterables to Go-like Reader interface
This repository has been archived on 2024年03月02日. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
  • JavaScript 100%
Find a file
2022年12月01日 10:03:58 +07:00
src fix!: make whence not optional 2022年11月30日 07:57:59 +07:00
test initial commit 2022年11月30日 06:17:40 +07:00
.editorconfig initial commit 2022年11月30日 06:17:40 +07:00
.gitignore initial commit 2022年11月30日 06:17:40 +07:00
.npmrc initial commit 2022年11月30日 06:17:40 +07:00
jsconfig.json initial commit 2022年11月30日 06:17:40 +07:00
LICENSE initial commit 2022年11月30日 06:17:40 +07:00
package.json v0.2.0 2022年11月30日 07:58:13 +07:00
README.md docs: use spaces on readme 2022年12月01日 10:03:58 +07:00

iterable-reader

Convert Uint8Array iterables to Go-like Reader interface.

import { createReadStream } from 'node:fs';
import { createIterableReader } from '@intrnl/iterable-reader';
let stream = createReadStream('./file.txt');
let reader = createIterableReader(stream);
let chunk = new Uint8Array(512);
await reader.read(chunk);

Working with Web Streams

Unfortunately browsers hasn't implemented using ReadableStream directly as an async iterator, in the meantime, you could use this to convert them into one.

function createStreamIterator (stream) {
 // return if browser already supports async iterator in stream
 if (Symbol.asyncIterator in stream) {
 return stream[Symbol.asyncIterator]();
 }
 let reader = stream.getReader();
 return {
 [Symbol.asyncIterator] () {
 return this;
 },
 next () {
 return reader.read();
 },
 return () {
 reader.releaseLock();
 },
 throw () {
 reader.releaseLock();
 },
 };
}