final-stream
Advanced tools
Comparing version 1.0.2 to 1.1.0
28
index.js
@@ -1,7 +0,7 @@ | ||
const ErrorWithObject = require('error-with-object'); | ||
const ErrorWithObject = require('error-with-object') | ||
function parseBody (stream, mutation, callback) { | ||
if (!callback) { | ||
callback = mutation; | ||
mutation = undefined; | ||
callback = mutation | ||
mutation = undefined | ||
} | ||
@@ -13,28 +13,28 @@ | ||
message: 'You did not set a stream.' | ||
}); | ||
}) | ||
} | ||
const body = []; | ||
const body = [] | ||
stream | ||
.on('data', function (chunk) { | ||
body.push(chunk); | ||
body.push(chunk) | ||
}) | ||
.on('end', function () { | ||
const bodyString = Buffer.concat(body).toString(); | ||
const bodyString = Buffer.concat(body).toString() | ||
let finalBody; | ||
let finalBody | ||
try { | ||
finalBody = bodyString && mutation ? mutation(bodyString) : bodyString; | ||
callback(null, finalBody); | ||
finalBody = mutation ? mutation(bodyString) : bodyString | ||
callback(null, finalBody) | ||
} catch (error) { | ||
callback(error); | ||
callback(error) | ||
} | ||
}) | ||
.on('error', function (error) { | ||
callback(error); | ||
}); | ||
callback(error) | ||
}) | ||
} | ||
module.exports = parseBody; | ||
module.exports = parseBody |
{ | ||
"name": "final-stream", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,3 @@ # Final Stream | ||
const finalStream = require('final-stream'); | ||
finalStream(response, function (error, result) { | ||
finalStream(request, function (error, result) { | ||
console.log({error, result}); | ||
@@ -23,3 +23,3 @@ }); | ||
const finalStream = require('final-stream'); | ||
finalStream(response, JSON.parse, function (error, result) { | ||
finalStream(request, JSON.parse, function (error, result) { | ||
console.log({error, result}); | ||
@@ -26,0 +26,0 @@ }); |
@@ -1,66 +0,76 @@ | ||
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); | ||
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); | ||
}); | ||
}); | ||
if (error) { console.log(error) } | ||
t.equal(result, testFileContent) | ||
}) | ||
}) | ||
test('basic stream to json', t => { | ||
t.plan(1); | ||
t.plan(1) | ||
const testFileContent = require('./testJsonFileToStream.json'); | ||
const stream = fs.createReadStream('./test/testJsonFileToStream.json', { highWaterMark: 32 }); | ||
const testFileContent = require('./testJsonFileToStream.json') | ||
const stream = fs.createReadStream('./test/testJsonFileToStream.json', { highWaterMark: 32 }) | ||
finalStream(stream, JSON.parse, function (error, result) { | ||
if (error) { console.log(error); } | ||
t.deepEqual(result, testFileContent); | ||
}); | ||
}); | ||
if (error) { console.log(error) } | ||
t.deepEqual(result, testFileContent) | ||
}) | ||
}) | ||
test('basic stream with invalid json', t => { | ||
t.plan(1); | ||
t.plan(1) | ||
const stream = fs.createReadStream('./test/testInvalidJsonFileToStream.json', { highWaterMark: 32 }); | ||
const stream = fs.createReadStream('./test/testInvalidJsonFileToStream.json', { highWaterMark: 32 }) | ||
finalStream(stream, JSON.parse, function (error, result) { | ||
t.ok(error.toString().includes('SyntaxError: Unexpected token } in JSON')); | ||
}); | ||
}); | ||
t.ok(error.toString().includes('SyntaxError: Unexpected token } in JSON')) | ||
}) | ||
}) | ||
test('basic stream with empty string', t => { | ||
t.plan(1) | ||
const stream = fs.createReadStream('./test/testEmptyFileToStream.json', { highWaterMark: 32 }) | ||
finalStream(stream, JSON.parse, function (error, result) { | ||
t.ok(error.toString().includes('Unexpected end of JSON input')) | ||
}) | ||
}) | ||
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')) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13421
10
108