readable-file-writer
TypeScript icon, indicating that this package has built-in type declarations

0.10.0 • Public • Published

readable-file-writer

A file writer that can also multicast data to multiple readers (Streams v2)

Build Status Coverage Status

Usage

import { ReadableFileWriter } from 'readable-file-writer'
import request = require('request')
import express = require('express')
 
const app = express()
app.get('/', (req, res) => {
    // Stream a file from an HTTP server to disk
    const writer = new ReadableFileWriter('movie.mp4')
    request('http://foo.com/movie.mp4').pipe(writer)
 
    // Stream the file to the client at the same time
    writer.createReadStream().pipe(res)
})
app.listen(3000)

API

export interface Options
{
    bufferSize: number;
}
 
export interface ReadStreamOptions
{
    highWaterMark: number;
}
 
class ReadableFileWriter extends stream.Writable
{
    constructor(
        path: string,
        options?: Options
    );
 
    readonly path: string;
 
    createReadStream(
        options?: ReadStreamOptions
    ): NodeJS.ReadableStream;
}

Options

bufferSize - total maximum amount of memory (in bytes) that can be used for buffering transferred data. Default value is 1MB.

The memory buffer contains last chunks of data that went through the writing pipeline.

Readers always try to read from the memory buffer first. If the memory buffer is big enough or the reading pipeline is fast enough the streaming is done completely from the memory buffer.

ReadableFileWriter's events

  • open
  • close
  • error

ReadableFileWriter.createReadStream

Creates a readable stream. Readable streams can be created and used at any time, even if the writing pipeline is finished and the instance of ReadableFileWriter is destroyed.

Any number of readable streams can be created.

highWaterMark - this value is passed to all calls to fs.createReadStream that ReadableFileWriter makes under the hood during the streaming. Default value is 128KB.

Error handling

Clients should listen to error events on the instance of ReadableFileWriter and listen to error events on all created readable streams.

ReadableFileWriter propagates errors from the writing pipeline to all reading pipelines. It allows clients to gracefuly stop reading pipelines without leaking resources.

Installation

npm install readable-file-writer

or

yarn add readable-file-writer

An additional installation step is required if your project is written in Typescript. Typings for the readable-stream module are not installed automatically. You have to install them manually using either option 1 or 2:

  • Option 1: install typings using npm or yarn. There is no standard @types/readable-stream right now but it may be added in the future;

  • Option 2: manually add typings from this module to your tsconfig.json:

    "include"[
        "./node_modules/readable-file-writer/types/readable-stream.d.ts"
    ]

Copyright © 2018 Sergei Kogan. Licensed under The MIT license.

Package Sidebar

Install

npm i readable-file-writer

Weekly Downloads

1

Version

0.10.0

License

MIT

Unpacked Size

206 kB

Total Files

54

Last publish

Collaborators

  • sekogan