-
Notifications
You must be signed in to change notification settings - Fork 0
/
auntie-do-snap-event-example.js
57 lines (46 loc) · 1.5 KB
/
auntie-do-snap-event-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Auntie example for snap event, it loads a file containing 8192
* long english words, separated by CRLF '\r\n' pattern.
* For "messing" things up, the chunk size is reduced to k bytes.
*/
const log = console.log
, fs = require( 'fs' )
, Auntie = require( '../' )
, stdout = process.stdout
, dpath = __dirname + '/data/some-english-words-crlf.txt'
, pattern = '\r\n'
// default pattern is '\r\n'
, untie = Auntie( pattern )
, rstream = fs.createReadStream( dpath )
;
log();
log( '- Auntie example, loading english long words from file:\n "%s"', dpath );
log( '\n- original stream highwatermark value: %d bytes', rstream._readableState.highWaterMark );
// I voluntarily reduce the chunk buffer size to k bytes
rstream._readableState.highWaterMark = 1;
log( '- new stream highwatermark value: %d bytes', rstream._readableState.highWaterMark );
var m = 0
, t = 0
, c = 0
;
untie.on( 'snap', function( data ) {
log(' !snap (%d) -> (%d) %s', ++m, data.length, data );
} );
log( '\n - read stream..' );
rstream.on( 'data', function ( chunk ) {
log( ' -> data chunk: %d', ++c );
t += chunk.length;
untie.do( chunk, false );
} );
rstream.on( 'end', function () {
log( '\n !end' );
} );
rstream.on( 'close', function () {
log( ' !close' );
log();
log( '- total data chunks: %d ', c );
log( '- total data length: %d bytes', t );
log( '- current pattern (%d):', pattern.length, untie.seq );
log( '- total matches: %d', m );
log();
} );