You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Node Transform stream that lets you inspect the start of a ReadStream before deciding what to do with it
3
+
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
4
+
piped again onto its final destination.
5
+
6
+
```
7
+
npm install buffer-peek-stream
8
+
```
9
+
10
+
Useful if you want to inspect the start of a stream before deciding what to do with it.
11
+
12
+
This works with buffers and does no string decoding. If you know you have a string and already
13
+
know its encoding then checkout [peek-stream](https://github.com/mafintosh/peek-stream).
14
+
15
+
16
+
## Usage
17
+
As a function...
18
+
```
19
+
var peek = require('./buffer-peek-stream');
20
+
var readstream = fs.createReadStream('package.json');
21
+
22
+
peek(readstream, 65536, function (err, data) {
23
+
if (err) throw err;
24
+
25
+
// readstream is ready to be piped somewhere else
26
+
readstream.pipe(somewhere_else);
27
+
});
28
+
```
29
+
30
+
As a stream...
31
+
```
32
+
var PeekStream = require('./buffer-peek-stream').Constructor;
33
+
34
+
var peek = new PeekStream(65536);
35
+
var readstream = fs.createReadStream('package.json');
36
+
37
+
// peek will only emit the data event once
38
+
peek.once('data', function (buf) {
39
+
40
+
// readstream is ready to be piped somewhere else
41
+
readstream.pipe(somewhere_else);
42
+
});
43
+
44
+
stream.pipe(peek);
45
+
```
46
+
47
+
48
+
## NOTICE
49
+
This hasn't been run in production yet and hasn't gone through substantial testing. The approach
50
+
taken to unshift data back onto the origin stream in particular could turn out to be a bad idea when
51
+
you try to peek at more bytes than are in the stream.
0 commit comments