Skip to content

passport90/memory-stream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memory-stream

Simple and lightweight implementation of Node.js Readable and Writable stream API where the data are stored in memory.

Installation

npm install @passport90/memory-stream

Usage Example

Read stream

This snippet would print 'This is a content.' to the screen.

import { MemoryReadStream } from '@passport90/memory-stream'

const content = 'This is a content.'

const stream = new MemoryReadStream(Buffer.from(content, 'utf8'))

let readString = ''
stream.on('error', console.error)
stream.on('end', () => console.log(readString)) 
stream.on('readable', () => {
  let chunk
  while (null !== (chunk = stream.read())) {
    readString += chunk.toString()
  }
})

Write stream

This snippet would print 'This is a content.' to the screen.

import { MemoryWriteStream } from '@passport90/memory-stream'

const content = 'This is a content.'
const buffer = Buffer.alloc(content.length)

const stream = new MemoryWriteStream(buffer)

stream.on('error', console.error)
stream.on('finish', () => console.log(buffer.toString()))
stream.end(content, 'utf8')

About

Simple implementation of Node.js Readable and Writable stream API where the data are stored in memory.

Resources

License

Stars

Watchers

Forks