0.0.10 • Published 3 years ago

json-stream-wrapper v0.0.10

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

node-json-stream-wrapper

A stream wrapper which wraps a stream into a JSON object

Build Status

Example Usage

import jsonStreamWrapper, {Base64Stream} from 'json-stream-wrapper';

import {createReadStream} from 'fs';
import {post} from 'request';

createReadStream('<example-file>')
  .pipe(new Base64Stream())
  .pipe(jsonStreamWrapper({example: 1}, 'file'))
  .pipe(post({
      url: 'http://target',
      json: true
    }));
    
// body of the http-request of the target-server:
// {
//    "example" : 1,
//    "file" : "<FILE_CONTENT_IN_BASE64>"
// }

or:

import jsonStreamWrapper, {Base64Stream} from 'json-stream-wrapper';

import {createReadStream} from 'fs';

let fstream = createReadStream('<example-file>');

const foo = fstream
  .pipe(new Base64Stream())
  .pipe(jsonStreamWrapper({example: 1}, 'file'));

streamToObject(foo, (obj) => {
  console.log(obj);
  // output:
  // {
  //    "example" : 1,
  //    "file" : "<FILE_CONTENT_IN_BASE64>"
  // }

});



function streamToObject(stream, cb) {
  const chunks = [];
  stream.on('data', (chunk) => {
    chunks.push(chunk);
  });
  stream.on('end', () => {
    cb(JSON.parse(chunks.join('')));
  });
}
0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago