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.
| src | fix!: make whence not optional | |
| test | initial commit | |
| .editorconfig | initial commit | |
| .gitignore | initial commit | |
| .npmrc | initial commit | |
| jsconfig.json | initial commit | |
| LICENSE | initial commit | |
| package.json | v0.2.0 | |
| README.md | docs: use spaces on readme | |
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();
},
};
}