buffer-peek-stream
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -16,5 +16,14 @@ var stream = require('stream'); | ||
peek.BufferPeekStream = BufferPeekStream; | ||
peek.promise = promise; | ||
module.exports = peek; | ||
function promise(source, bytes) { | ||
return new Promise((resolve, reject) => { | ||
peek(source, bytes, (err, buffer, dest) => { | ||
if (err) return reject(err); | ||
resolve([buffer, dest]); | ||
}) | ||
}) | ||
} | ||
@@ -80,2 +89,2 @@ function BufferPeekStream(opts) { | ||
callback(); | ||
} | ||
} |
# buffer-peek-stream changelog | ||
## 1.1.0 (2020/02/02) | ||
- add promise interface | ||
- drop tests for node 4 & 6 | ||
## 1.0.1 (2015/06/09) | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "buffer-peek-stream", | ||
"version": "1.0.1", | ||
"description": "Transform stream that lets you inspect the start of a ReadStream before deciding what to do with it", | ||
"version": "1.1.0", | ||
"description": "Transform stream that lets you inspect the start of a readable stream before deciding what to do with it", | ||
"main": "buffer-peek-stream.js", | ||
@@ -28,6 +28,6 @@ "scripts": { | ||
"devDependencies": { | ||
"chai": "^1.10.0", | ||
"chai": "^4.2.0", | ||
"concat-stream": "^1.4.6", | ||
"dev-null-stream": "0.0.1", | ||
"mocha": "^2.0.1", | ||
"mocha": "^6.2.1", | ||
"randstream": "^0.3.2", | ||
@@ -34,0 +34,0 @@ "truncate-stream": "^1.0.1" |
@@ -5,5 +5,5 @@ # node-buffer-peek-stream | ||
A Transform stream which lets you take a peek at the first bytes before unpiping itself and | ||
unshifting the buffer back onto the upstream stream leaving the original stream ready to be | ||
piped again onto its final destination. | ||
Take a peek at the start of a stream and get back a new stream rewound from the start without | ||
buffering the entire stream. Useful when you need to inspect the start of the stream before | ||
deciding what to do with the stream. | ||
@@ -21,4 +21,15 @@ ``` | ||
## Usage | ||
As a function... | ||
As a promise (with await)... | ||
``` | ||
const peek = require('buffer-peek-stream').promise; | ||
const readstream = fs.createReadStream('package.json'); | ||
const [data, outputStream] = await peek(readstream, 65536); | ||
// outputStream is ready to be piped somewhere else | ||
outputStream.pipe(somewhere_else); | ||
``` | ||
As a callback... | ||
``` | ||
var peek = require('buffer-peek-stream'); | ||
@@ -42,4 +53,4 @@ var readstream = fs.createReadStream('package.json'); | ||
// peek will only emit the data event once | ||
peek.once('data', function (buf) { | ||
// peek will only emit the peek event once | ||
peek.once('peek', function (buf) { | ||
@@ -46,0 +57,0 @@ // readstream is ready to be piped somewhere else |
39
test.js
@@ -7,2 +7,3 @@ var fs = require('fs'); | ||
var concat = require('concat-stream'); | ||
var http = require('http'); | ||
@@ -96,5 +97,39 @@ var peek = require('./buffer-peek-stream'); | ||
describe('promise', () => { | ||
it('should return a promise which resolves a tuple with buffer & forward stream', (done) => { | ||
const promise = peek.promise(make(50000), 1000); | ||
promise.then(([buffer, stream]) => { | ||
expect(buffer).to.be.an.instanceof(Buffer); | ||
expect(buffer).to.have.lengthOf(1000); | ||
stream.pipe(concat((data) => { | ||
expect(data).to.have.lengthOf(50000); | ||
done(); | ||
})); | ||
}); | ||
}); | ||
}); | ||
//TODO: peeking inside gzip data (transform) | ||
//TODO: peeking a http response | ||
}); | ||
this.timeout(10000); | ||
it('should peek an IncomingMessage stream', (done) => { | ||
http.get('http://nodejs.org/dist/index.json', (res) => { | ||
const statusCode = res.statusCode; | ||
if (statusCode !== 200) { | ||
return done(new Error(`Request Failed [${statusCode}]`)); | ||
} | ||
peek(res, 1000, function (err, data, stream) { | ||
if (err) return done(err); | ||
const index_json = data.toString(); | ||
expect(index_json).to.have.string('"version"'); | ||
stream.resume(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Network access
Supply chain riskThis module accesses the network.
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
10360
175
66
7
1