Socket
Socket
Sign inDemoInstall

final-stream

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.1 to 2.0.0

42

index.js

@@ -1,39 +0,31 @@

const ErrorWithObject = require('error-with-object')
const { promisify } = require('util');
function parseBody (stream, mutation, callback) {
if (!callback) {
callback = mutation
mutation = undefined
}
function parseBody (stream, callback) {
if (!stream) {
throw new ErrorWithObject({
code: 'NO_STREAM_OBJECT',
message: 'You did not set a stream.'
})
throw Object.assign(new Error('You did not set a stream.'), {
code: 'NO_STREAM_OBJECT'
});
}
const body = []
const body = [];
stream
.on('data', function (chunk) {
body.push(chunk)
body.push(chunk);
})
.on('end', function () {
const bodyString = Buffer.concat(body).toString()
let finalBody
try {
finalBody = mutation ? mutation(bodyString) : bodyString
callback(null, finalBody)
} catch (error) {
callback(error)
}
callback(null, Buffer.concat(body));
})
.on('error', function (error) {
callback(error)
})
callback(error);
});
}
module.exports = parseBody
module.exports = function (...args) {
if (args.length === 1) {
return promisify(parseBody)(...args);
}
return parseBody(...args);
};
{
"name": "final-stream",
"version": "1.1.1",
"version": "2.0.0",
"description": "A tool to read a full stream and callback once finished with the data",

@@ -19,5 +19,3 @@ "main": "index.js",

},
"dependencies": {
"error-with-object": "^1.1.0"
},
"dependencies": {},
"bugs": {

@@ -24,0 +22,0 @@ "url": "https://github.com/markwylde/final-stream/issues"

# Final Stream
[![Build Status](https://travis-ci.org/markwylde/final-stream.svg?branch=master)](https://travis-ci.org/markwylde/final-stream)
[![David DM](https://david-dm.org/markwylde/final-stream.svg)](https://david-dm.org/markwylde/final-stream)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/markwylde/final-stream)

@@ -11,16 +10,15 @@ [![GitHub package.json version](https://img.shields.io/github/package-json/v/markwylde/final-stream)](https://github.com/markwylde/final-stream/releases)

## Example Usage
### As a pure string
### With callbacks
```javascript
const finalStream = require('final-stream');
finalStream(request, function (error, result) {
console.log({error, result});
console.log(error, result.toString());
});
```
### With a mutator
### With promises
```javascript
const finalStream = require('final-stream');
finalStream(request, JSON.parse, function (error, result) {
console.log({error, result});
});
const result = await finalStream(request);
console.log(result.toString());
```

@@ -27,0 +25,0 @@

@@ -1,76 +0,79 @@

const test = require('tape')
const fs = require('fs')
const Readable = require('stream').Readable
const test = require('tape');
const fs = require('fs');
const Readable = require('stream').Readable;
const finalStream = require('../')
const finalStream = require('../');
test('basic stream to string', t => {
t.plan(1)
test('promise - basic stream to string', async t => {
t.plan(1);
const testFilePath = './test/testFiletoStream.txt'
const testFileContent = fs.readFileSync(testFilePath, 'utf8')
const stream = fs.createReadStream(testFilePath, { highWaterMark: 32 })
const testFilePath = './test/testFiletoStream.txt';
const testFileContent = fs.readFileSync(testFilePath, 'utf8');
const stream = fs.createReadStream(testFilePath, { highWaterMark: 32 });
finalStream(stream, function (error, result) {
if (error) { console.log(error) }
t.equal(result, testFileContent)
})
})
const result = await finalStream(stream);
t.equal(result.toString(), testFileContent);
});
test('basic stream to json', t => {
t.plan(1)
test('promise - with no stream object', async t => {
t.plan(1);
const testFileContent = require('./testJsonFileToStream.json')
const stream = fs.createReadStream('./test/testJsonFileToStream.json', { highWaterMark: 32 })
try {
await finalStream(null);
} catch (error) {
t.equal(error.code, 'NO_STREAM_OBJECT');
}
});
finalStream(stream, JSON.parse, function (error, result) {
if (error) { console.log(error) }
t.deepEqual(result, testFileContent)
})
})
test('promise - when stream errors', t => {
t.plan(1);
test('basic stream with invalid json', t => {
t.plan(1)
const stream = new Readable({
read (size) {}
});
const stream = fs.createReadStream('./test/testInvalidJsonFileToStream.json', { highWaterMark: 32 })
finalStream(stream).catch(error => {
t.equal(error.toString(), 'Error: oh no');
});
finalStream(stream, JSON.parse, function (error, result) {
t.ok(error.toString().includes('SyntaxError: Unexpected token } in JSON'))
})
})
stream.emit('error', new Error('oh no'));
});
test('basic stream with empty string', t => {
t.plan(1)
test('basic stream to string', t => {
t.plan(1);
const stream = fs.createReadStream('./test/testEmptyFileToStream.json', { highWaterMark: 32 })
const testFilePath = './test/testFiletoStream.txt';
const testFileContent = fs.readFileSync(testFilePath, 'utf8');
const stream = fs.createReadStream(testFilePath, { highWaterMark: 32 });
finalStream(stream, JSON.parse, function (error, result) {
t.ok(error.toString().includes('Unexpected end of JSON input'))
})
})
finalStream(stream, function (error, result) {
if (error) { console.log(error); }
t.equal(result.toString(), testFileContent);
});
});
test('with no stream object', t => {
t.plan(1)
t.plan(1);
try {
finalStream(null, function (error, result) {
if (error) { console.log(error) }
})
if (error) { console.log(error); }
});
} catch (error) {
t.equal(error.code, 'NO_STREAM_OBJECT')
t.equal(error.code, 'NO_STREAM_OBJECT');
}
})
});
test('when stream errors', t => {
t.plan(1)
t.plan(1);
const stream = new Readable({
read (size) {}
})
});
finalStream(stream, function (error, result) {
t.equal(error.toString(), 'Error: oh no')
})
t.equal(error.toString(), 'Error: oh no');
});
stream.emit('error', new Error('oh no'))
})
stream.emit('error', new Error('oh no'));
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc