@anandsuresh/smart_stream

1.0.0 • Public • Published

smart-stream

A smart stream provides the abstraction of a stream of bytes. It has the following properties:

  • contentType: representing the MIME type of that the stream of bytes represent
  • contentEncoding: representing the type of encoding (gzip, deflate, identity)
  • contentLength: the length of the stream, in bytes. Not always available.

Usage

const {fromFile, fromHttpRequest} = require('smart_stream')

async function handleRequest(req, res) {
  try {
    const inStream = fromHttpRequest(req)
    if (!inStream.isDeserializable) {
      res.statusCode = 400
      return res.end()
    }

    const obj = await stream.toObject()

    // Do something with the object
    // Then respond with a file

    fromFile('/path/to/file.jpg', {contentType: 'image/jpeg'}).pipe(res)
  } catch (err) {
    res.statusCode = 500
    res.end(err.stack)
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @anandsuresh/smart_stream

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

19.4 kB

Total Files

5

Last publish

Collaborators

  • anandsuresh