README.md
1Description
2===========
3
4streamsearch is a module for [node.js](http://nodejs.org/) that allows searching a stream using the Boyer-Moore-Horspool algorithm.
5
6This module is based heavily on the Streaming Boyer-Moore-Horspool C++ implementation by Hongli Lai [here](https://github.com/FooBarWidget/boyer-moore-horspool).
7
8
9Requirements
10============
11
12* [node.js](http://nodejs.org/) -- v10.0.0 or newer
13
14
15Installation
16============
17
18 npm install streamsearch
19
20Example
21=======
22
23```js
24 const { inspect } = require('util');
25
26 const StreamSearch = require('streamsearch');
27
28 const needle = Buffer.from('\r\n');
29 const ss = new StreamSearch(needle, (isMatch, data, start, end) => {
30 if (data)
31 console.log('data: ' + inspect(data.toString('latin1', start, end)));
32 if (isMatch)
33 console.log('match!');
34 });
35
36 const chunks = [
37 'foo',
38 ' bar',
39 '\r',
40 '\n',
41 'baz, hello\r',
42 '\n world.',
43 '\r\n Node.JS rules!!\r\n\r\n',
44 ];
45 for (const chunk of chunks)
46 ss.push(Buffer.from(chunk));
47
48 // output:
49 //
50 // data: 'foo'
51 // data: ' bar'
52 // match!
53 // data: 'baz, hello'
54 // match!
55 // data: ' world.'
56 // match!
57 // data: ' Node.JS rules!!'
58 // match!
59 // data: ''
60 // match!
61```
62
63
64API
65===
66
67Properties
68----------
69
70* **maxMatches** - < _integer_ > - The maximum number of matches. Defaults to `Infinity`.
71
72* **matches** - < _integer_ > - The current match count.
73
74
75Functions
76---------
77
78* **(constructor)**(< _mixed_ >needle, < _function_ >callback) - Creates and returns a new instance for searching for a _Buffer_ or _string_ `needle`. `callback` is called any time there is non-matching data and/or there is a needle match. `callback` will be called with the following arguments:
79
80 1. `isMatch` - _boolean_ - Indicates whether a match has been found
81
82 2. `data` - _mixed_ - If set, this contains data that did not match the needle.
83
84 3. `start` - _integer_ - The index in `data` where the non-matching data begins (inclusive).
85
86 4. `end` - _integer_ - The index in `data` where the non-matching data ends (exclusive).
87
88 5. `isSafeData` - _boolean_ - Indicates if it is safe to store a reference to `data` (e.g. as-is or via `data.slice()`) or not, as in some cases `data` may point to a Buffer whose contents change over time.
89
90* **destroy**() - _(void)_ - Emits any last remaining unmatched data that may still be buffered and then resets internal state.
91
92* **push**(< _Buffer_ >chunk) - _integer_ - Processes `chunk`, searching for a match. The return value is the last processed index in `chunk` + 1.
93
94* **reset**() - _(void)_ - Resets internal state. Useful for when you wish to start searching a new/different stream for example.
95
96