Giter Site home page Giter Site logo

lambdalisue / deno-streamtools Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 63 KB

๐Ÿฆ• This is a TypeScript module that provides utilities for handling Streams API.

Home Page: https://jsr.io/@lambdalisue/streamtools

License: MIT License

TypeScript 100.00%
deno streams-api jsr

deno-streamtools's Introduction

streamtools

jsr denoland deno doc Test

This is a TypeScript module that provides utilities for handling Streams API.

Usage

pipeThroughFrom

Pipes the readable side of a TransformStream to a WritableStream. Returns the writable side of the TransformStream for further piping.

import { channel } from "./channel.ts";
import { collect } from "./collect.ts";
import { pipeThroughFrom } from "./pipe_through_from.ts";

const encoder = new TextEncoder();
const output = channel<string>();
const stream = pipeThroughFrom(output.writer, new TextDecoderStream());
const writer = stream.getWriter();

await writer.write(encoder.encode("Hello"));
await writer.write(encoder.encode("World"));
await writer.close();
writer.releaseLock();

const result = await collect(output.reader);
console.log(result); // ["Hello", "World"]

channel

channel creates a new channel, which is a pair of a readable and writable stream.

import { channel } from "./channel.ts";
import { push } from "./push.ts";
import { pop } from "./pop.ts";

const { reader, writer } = channel<number>();

await push(writer, 1);
await push(writer, 2);
await push(writer, 3);
console.log(await pop(reader)); // 1
console.log(await pop(reader)); // 2
console.log(await pop(reader)); // 3

collect/provide

collect reads all chunks from a readable stream and returns them as an array of chunks.

import { collect } from "./collect.ts";

const reader = new ReadableStream<number>({
  start(controller) {
    controller.enqueue(1);
    controller.enqueue(2);
    controller.enqueue(3);
    controller.close();
  },
});

console.log(await collect(reader)); // [1, 2, 3]

provide provides the given values to the writable stream by piping them from a readable stream created from the values. Returns a promise that resolves when all the values have been successfully written to the stream.

import { provide } from "./provide.ts";

const results: number[] = [];
const writer = new WritableStream<number>({
  write(chunk) {
    results.push(chunk);
  },
});

await provide(writer, [1, 2, 3]);
console.log(results); // [1, 2, 3]

pop/push

pop reads the next chunk from a readable stream.

import { pop } from "./pop.ts";

const reader = new ReadableStream<number>({
  start(controller) {
    controller.enqueue(1);
    controller.enqueue(2);
    controller.enqueue(3);
    controller.close();
  },
});

console.log(await pop(reader)); // 1
console.log(await pop(reader)); // 2
console.log(await pop(reader)); // 3
console.log(await pop(reader)); // null

push writes a chunk to a writable stream.

import { push } from "./push.ts";

const results: number[] = [];
const writer = new WritableStream<number>({
  write(chunk) {
    results.push(chunk);
  },
});

await push(writer, 1);
await push(writer, 2);
await push(writer, 3);
console.log(results); // [1, 2, 3]

readAll/writeAll

readAll reads all available bytes from a given ReadableStream<Uint8Array> and concatenates them into a single Uint8Array.

import { assertEquals } from "jsr:@std/assert";
import { readAll } from "./read_all.ts";

const encoder = new TextEncoder();
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(encoder.encode("Hello"));
    controller.enqueue(encoder.encode("World"));
    controller.close();
  },
});
const result = await readAll(stream);
assertEquals(result, encoder.encode("HelloWorld"));

writeAll writes all data in a Uint8Array to a writable stream in chunk-size units.

import { assertEquals } from "jsr:@std/assert";
import { writeAll } from "./write_all.ts";

const encoder = new TextEncoder();
const chunks: Uint8Array[] = [];
const stream = new WritableStream({
  write(chunk) {
    chunks.push(chunk);
  },
});
await writeAll(stream, encoder.encode("HelloWorld"), { chunkSize: 3 });
assertEquals(chunks, [
  encoder.encode("Hel"),
  encoder.encode("loW"),
  encoder.encode("orl"),
  encoder.encode("d"),
]);

License

The code is released under the MIT license, which is included in the LICENSE file. By contributing to this repository, contributors agree to follow the license for any modifications made.

deno-streamtools's People

Contributors

lambdalisue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hasundue milly

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.