├── .github └── workflows │ └── ci-module.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── lib └── index.js ├── package.json └── test ├── esm.js └── index.js /.github/workflows/ci-module.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | jobs: 11 | test: 12 | uses: hapijs/.github/.github/workflows/ci-module.yml@master 13 | with: 14 | min-node-version: 14 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/node_modules 2 | **/package-lock.json 3 | 4 | coverage.* 5 | 6 | **/.DS_Store 7 | **/._* 8 | 9 | **/*.pem 10 | 11 | **/.vs 12 | **/.vscode 13 | **/.idea 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014-2022, Project contributors 2 | Copyright (c) 2014-2020, Sideway Inc 3 | Copyright (c) 2014, Walmart. 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @hapi/nigel 2 | 3 | #### Boyer-Moore-Horspool algorithms 4 | 5 | **nigel** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together. 6 | 7 | ### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support 8 | 9 | ## Useful resources 10 | 11 | - [Documentation and API](https://hapi.dev/family/nigel/) 12 | - [Versions status](https://hapi.dev/resources/status/#nigel) (builds, dependencies, node versions, licenses, eol) 13 | - [Changelog](https://hapi.dev/family/nigel/changelog/) 14 | - [Project policies](https://hapi.dev/policies/) 15 | - [Free and commercial support options](https://hapi.dev/support/) 16 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Stream = require('stream'); 4 | 5 | const Hoek = require('@hapi/hoek'); 6 | const { Vise } = require('@hapi/vise'); 7 | 8 | 9 | const internals = {}; 10 | 11 | 12 | exports.compile = function (needle) { 13 | 14 | Hoek.assert(needle?.length, 'Missing needle'); 15 | Hoek.assert(Buffer.isBuffer(needle), 'Needle must be a buffer'); 16 | 17 | const profile = { 18 | value: needle, 19 | lastPos: needle.length - 1, 20 | last: needle[needle.length - 1], 21 | length: needle.length, 22 | badCharShift: Buffer.alloc(256) // Lookup table of how many characters can be skipped for each match 23 | }; 24 | 25 | for (let i = 0; i < 256; ++i) { 26 | profile.badCharShift[i] = profile.length; // Defaults to the full length of the needle 27 | } 28 | 29 | const last = profile.length - 1; 30 | for (let i = 0; i < last; ++i) { // For each character in the needle (skip last since its position is already the default) 31 | profile.badCharShift[profile.value[i]] = last - i; 32 | } 33 | 34 | return profile; 35 | }; 36 | 37 | 38 | exports.horspool = function (haystack, needle, start) { 39 | 40 | Hoek.assert(haystack, 'Missing haystack'); 41 | 42 | needle = (needle.badCharShift ? needle : exports.compile(needle)); 43 | start = start ?? 0; 44 | 45 | for (let i = start; i <= haystack.length - needle.length;) { // Has enough room to fit the entire needle 46 | const lastChar = haystack.readUInt8(i + needle.lastPos); 47 | if (lastChar === needle.last && 48 | internals.startsWith(haystack, needle, i)) { 49 | 50 | return i; 51 | } 52 | 53 | i += needle.badCharShift[lastChar]; // Jump to the next possible position based on last character location in needle 54 | } 55 | 56 | return -1; 57 | }; 58 | 59 | 60 | internals.startsWith = function (haystack, needle, pos) { 61 | 62 | if (haystack.startsWith) { 63 | return haystack.startsWith(needle.value, pos, needle.lastPos); 64 | } 65 | 66 | for (let i = 0; i < needle.lastPos; ++i) { 67 | if (needle.value[i] !== haystack.readUInt8(pos + i)) { 68 | return false; 69 | } 70 | } 71 | 72 | return true; 73 | }; 74 | 75 | 76 | exports.all = function (haystack, needle, start) { 77 | 78 | needle = exports.compile(needle); 79 | start = start ?? 0; 80 | 81 | const matches = []; 82 | for (let i = start; i !== -1 && i < haystack.length;) { 83 | 84 | i = exports.horspool(haystack, needle, i); 85 | if (i !== -1) { 86 | matches.push(i); 87 | i += needle.length; 88 | } 89 | } 90 | 91 | return matches; 92 | }; 93 | 94 | 95 | internals._indexOf = function (haystack, needle) { 96 | 97 | Hoek.assert(haystack, 'Missing haystack'); 98 | 99 | for (let i = 0; i <= haystack.length - needle.length; ++i) { // Has enough room to fit the entire needle 100 | if (haystack.startsWith(needle.value, i)) { 101 | return i; 102 | } 103 | } 104 | 105 | return -1; 106 | }; 107 | 108 | 109 | exports.Stream = class extends Stream.Writable { 110 | 111 | constructor(needle) { 112 | 113 | super(); 114 | 115 | this.needle(needle); 116 | this._haystack = new Vise(); 117 | this._indexOf = this._needle.length > 2 ? exports.horspool : internals._indexOf; 118 | 119 | this.on('finish', () => { 120 | 121 | // Flush out the remainder 122 | 123 | const chunks = this._haystack.chunks(); 124 | for (let i = 0; i < chunks.length; ++i) { 125 | this.emit('haystack', chunks[i]); 126 | } 127 | }); 128 | } 129 | 130 | needle(needle) { 131 | 132 | this._needle = exports.compile(needle); 133 | } 134 | 135 | _write(chunk, encoding, next) { 136 | 137 | this._haystack.push(chunk); 138 | 139 | let match = this._indexOf(this._haystack, this._needle); 140 | if (match === -1 && 141 | chunk.length >= this._needle.length) { 142 | 143 | this._flush(this._haystack.length - chunk.length); 144 | } 145 | 146 | while (match !== -1) { 147 | this._flush(match); 148 | this._haystack.shift(this._needle.length); 149 | this.emit('needle'); 150 | 151 | match = this._indexOf(this._haystack, this._needle); 152 | } 153 | 154 | if (this._haystack.length) { 155 | const notChecked = this._haystack.length - this._needle.length + 1; // Not enough space for Horspool 156 | let i = notChecked; 157 | for (; i < this._haystack.length; ++i) { 158 | if (this._haystack.startsWith(this._needle.value, i, this._haystack.length - i)) { 159 | break; 160 | } 161 | } 162 | 163 | this._flush(i); 164 | } 165 | 166 | return next(); 167 | } 168 | 169 | _flush(pos) { 170 | 171 | const chunks = this._haystack.shift(pos); 172 | for (let i = 0; i < chunks.length; ++i) { 173 | this.emit('haystack', chunks[i]); 174 | } 175 | } 176 | 177 | flush() { 178 | 179 | const chunks = this._haystack.shift(this._haystack.length); 180 | for (let i = 0; i < chunks.length; ++i) { 181 | this.emit('haystack', chunks[i]); 182 | } 183 | } 184 | }; 185 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@hapi/nigel", 3 | "description": "Boyer-Moore-Horspool algorithms", 4 | "version": "5.0.1", 5 | "repository": "git://github.com/hapijs/nigel", 6 | "main": "lib/index.js", 7 | "engines": { 8 | "node": ">=14.0.0" 9 | }, 10 | "files": [ 11 | "lib" 12 | ], 13 | "keywords": [ 14 | "boyer-moore-horspool", 15 | "algorithms", 16 | "lookup", 17 | "search", 18 | "stream" 19 | ], 20 | "eslintConfig": { 21 | "extends": [ 22 | "plugin:@hapi/module" 23 | ] 24 | }, 25 | "dependencies": { 26 | "@hapi/hoek": "^11.0.2", 27 | "@hapi/vise": "^5.0.1" 28 | }, 29 | "devDependencies": { 30 | "@hapi/code": "^9.0.3", 31 | "@hapi/eslint-plugin": "^6.0.0", 32 | "@hapi/lab": "^25.0.1", 33 | "@hapi/teamwork": "^6.0.0" 34 | }, 35 | "scripts": { 36 | "test": "lab -a @hapi/code -t 100 -L", 37 | "test-cov-html": "lab -a @hapi/code -r html -o coverage.html" 38 | }, 39 | "license": "BSD-3-Clause" 40 | } 41 | -------------------------------------------------------------------------------- /test/esm.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Code = require('@hapi/code'); 4 | const Lab = require('@hapi/lab'); 5 | 6 | 7 | const { before, describe, it } = exports.lab = Lab.script(); 8 | const expect = Code.expect; 9 | 10 | 11 | describe('import()', () => { 12 | 13 | let Nigel; 14 | 15 | before(async () => { 16 | 17 | Nigel = await import('../lib/index.js'); 18 | }); 19 | 20 | it('exposes all methods and classes as named imports', () => { 21 | 22 | expect(Object.keys(Nigel)).to.equal([ 23 | 'Stream', 24 | 'all', 25 | 'compile', 26 | 'default', 27 | 'horspool' 28 | ]); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Code = require('@hapi/code'); 4 | const Lab = require('@hapi/lab'); 5 | const Nigel = require('..'); 6 | const Teamwork = require('@hapi/teamwork'); 7 | const { Vise } = require('@hapi/vise'); 8 | 9 | 10 | const internals = {}; 11 | 12 | 13 | const { describe, it } = exports.lab = Lab.script(); 14 | const expect = Code.expect; 15 | 16 | 17 | describe('compile()', () => { 18 | 19 | it('processes needle', () => { 20 | 21 | const needle = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 22 | expect(Nigel.compile(needle)).to.equal({ 23 | value: needle, 24 | lastPos: 25, 25 | last: 122, 26 | length: 26, 27 | badCharShift: Buffer.from([26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26]) 28 | }); 29 | }); 30 | 31 | it('throws on empty needle', () => { 32 | 33 | expect(() => { 34 | 35 | Nigel.compile(Buffer.from('')); 36 | }).to.throw('Missing needle'); 37 | }); 38 | 39 | it('throws on empty needle', () => { 40 | 41 | expect(() => { 42 | 43 | Nigel.compile(); 44 | }).to.throw('Missing needle'); 45 | }); 46 | }); 47 | 48 | describe('horspool()', () => { 49 | 50 | it('finds needle', () => { 51 | 52 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 53 | const needle = Buffer.from('mnopq'); 54 | 55 | expect(Nigel.horspool(haystack, needle)).to.equal(12); 56 | }); 57 | 58 | it('does not find needle', () => { 59 | 60 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 61 | const needle = Buffer.from('mnxpq'); 62 | 63 | expect(Nigel.horspool(haystack, needle)).to.equal(-1); 64 | }); 65 | 66 | it('does not find needle (tail match)', () => { 67 | 68 | const haystack = Buffer.from('q2q2q2q2q'); 69 | const needle = Buffer.from('22q'); 70 | 71 | expect(Nigel.horspool(haystack, needle)).to.equal(-1); 72 | }); 73 | 74 | it('finds needle from position', () => { 75 | 76 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 77 | const needle = Buffer.from('mnopq'); 78 | 79 | expect(Nigel.horspool(haystack, needle, 11)).to.equal(12); 80 | }); 81 | 82 | it('does not find needle from position', () => { 83 | 84 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 85 | const needle = Buffer.from('mnopq'); 86 | 87 | expect(Nigel.horspool(haystack, needle, 13)).to.equal(-1); 88 | }); 89 | 90 | it('finds needle in vise haystack', () => { 91 | 92 | const haystack = new Vise([Buffer.from('abcdefghijklmn'), Buffer.from('opqrstuvwxyz')]); 93 | expect(Nigel.horspool(haystack, Buffer.from('mnopq'))).to.equal(12); 94 | }); 95 | 96 | it('finds needle in pushed vise haystack', () => { 97 | 98 | const haystack = new Vise(); 99 | haystack.push(Buffer.from('abcdefghijklmn')); 100 | haystack.push(Buffer.from('opqrstuvwxyz')); 101 | expect(Nigel.horspool(haystack, Buffer.from('mnopq'))).to.equal(12); 102 | }); 103 | }); 104 | 105 | describe('all()', () => { 106 | 107 | it('finds needle', () => { 108 | 109 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 110 | const needle = Buffer.from('mnopq'); 111 | 112 | expect(Nigel.all(haystack, needle)).to.equal([12]); 113 | }); 114 | 115 | it('does not find needle', () => { 116 | 117 | const haystack = Buffer.from('abcdefghijklmnopqrstuvwxyz'); 118 | const needle = Buffer.from('mno2pq'); 119 | 120 | expect(Nigel.all(haystack, needle)).to.equal([]); 121 | }); 122 | 123 | it('finds multiple needles', () => { 124 | 125 | const haystack = Buffer.from('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); 126 | const needle = Buffer.from('123'); 127 | 128 | expect(Nigel.all(haystack, needle)).to.equal([3, 9, 15, 21, 27, 33, 39, 45]); 129 | }); 130 | 131 | it('finds multiple needles from position', () => { 132 | 133 | const haystack = Buffer.from('abc123def123ghi123jkl123mno123pqr123stu123vwx123yz'); 134 | const needle = Buffer.from('123'); 135 | 136 | expect(Nigel.all(haystack, needle, 11)).to.equal([15, 21, 27, 33, 39, 45]); 137 | }); 138 | }); 139 | 140 | describe('Stream', () => { 141 | 142 | it('parses a stream haystack', async () => { 143 | 144 | const team = new Teamwork.Team(); 145 | const result = []; 146 | 147 | const stream = new Nigel.Stream(Buffer.from('123')); 148 | let closed = false; 149 | stream.on('close', () => { 150 | 151 | expect(result).to.equal(['abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop']); 152 | 153 | // Verify that 'close' is only emitted once. 154 | expect(closed).to.equal(false); 155 | closed = true; 156 | setImmediate(() => { 157 | 158 | team.attend(); 159 | }); 160 | }); 161 | 162 | stream.on('needle', () => { 163 | 164 | result.push(1); 165 | }); 166 | 167 | stream.on('haystack', (chunk) => { 168 | 169 | result.push(chunk.toString()); 170 | }); 171 | 172 | stream.write('abc123de'); 173 | stream.write('fg12'); 174 | stream.write('3hij11'); 175 | stream.write('23klm'); 176 | stream.write('123'); 177 | stream.write('nop'); 178 | stream.end(); 179 | 180 | await team.work; 181 | }); 182 | 183 | it('flushes data buffers when more recent one is bigger than needle', async () => { 184 | 185 | const team = new Teamwork.Team(); 186 | const result = []; 187 | 188 | const stream = new Nigel.Stream(Buffer.from('123')); 189 | stream.on('close', () => { 190 | 191 | expect(result).to.equal(['abc', null, 'de', 'fghij', 'klmnop', 'q', null, 'r', 'stuv', 'wxy', 'zabc']); 192 | team.attend(); 193 | }); 194 | 195 | stream.on('needle', () => { 196 | 197 | result.push(null); 198 | }); 199 | 200 | stream.on('haystack', (chunk, g) => { 201 | 202 | expect(stream._haystack.length).to.be.lessThan(7); 203 | result.push(chunk.toString()); 204 | }); 205 | 206 | stream.write('abc123de'); 207 | stream.write('fghij'); 208 | stream.write('klmnop'); 209 | stream.write('q123r'); 210 | stream.write('stuv'); 211 | stream.write('wxy'); 212 | stream.write('zabc'); 213 | stream.end(); 214 | 215 | await team.work; 216 | }); 217 | 218 | it('parses a stream haystack (partial needle first)', async () => { 219 | 220 | const team = new Teamwork.Team(); 221 | const result = []; 222 | 223 | const stream = new Nigel.Stream(Buffer.from('123')); 224 | stream.on('close', () => { 225 | 226 | expect(result).to.equal([1, 'abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop']); 227 | team.attend(); 228 | }); 229 | 230 | stream.on('needle', () => { 231 | 232 | result.push(1); 233 | }); 234 | 235 | stream.on('haystack', (chunk) => { 236 | 237 | result.push(chunk.toString()); 238 | }); 239 | 240 | stream.write('12'); 241 | stream.write('3abc123de'); 242 | stream.write('fg12'); 243 | stream.write('3hij11'); 244 | stream.write('23klm'); 245 | stream.write('123'); 246 | stream.write('nop'); 247 | stream.end(); 248 | 249 | await team.work; 250 | }); 251 | 252 | it('parses a stream haystack (partial needle last)', async () => { 253 | 254 | const team = new Teamwork.Team(); 255 | const result = []; 256 | 257 | const stream = new Nigel.Stream(Buffer.from('123')); 258 | stream.on('close', () => { 259 | 260 | expect(result).to.equal([1, 'abc', 1, 'de', 'fg', 1, 'hij1', 1, 'klm', 1, 'nop', '1']); 261 | team.attend(); 262 | }); 263 | 264 | stream.on('needle', () => { 265 | 266 | result.push(1); 267 | }); 268 | 269 | stream.on('haystack', (chunk) => { 270 | 271 | result.push(chunk.toString()); 272 | }); 273 | 274 | stream.write('12'); 275 | stream.write('3abc123de'); 276 | stream.write('fg12'); 277 | stream.write('3hij11'); 278 | stream.write('23klm'); 279 | stream.write('123'); 280 | stream.write('nop1'); 281 | stream.end(); 282 | 283 | await team.work; 284 | 285 | }); 286 | 287 | describe('needle()', () => { 288 | 289 | it('changes needle mid stream', async () => { 290 | 291 | const team = new Teamwork.Team(); 292 | const result = []; 293 | 294 | const stream = new Nigel.Stream(Buffer.from('123')); 295 | stream.on('close', () => { 296 | 297 | expect(result).to.equal([1, 'abc', 1, 'de', 'fg', '12', '3hi', 1, 'j11', '23klm', '123', 'no', 1, 'p1']); 298 | team.attend(); 299 | }); 300 | 301 | stream.on('needle', () => { 302 | 303 | result.push(1); 304 | }); 305 | 306 | stream.on('haystack', (chunk) => { 307 | 308 | result.push(chunk.toString()); 309 | }); 310 | 311 | stream.write('12'); 312 | stream.write('3abc123de'); 313 | stream.write('fg12'); 314 | stream.needle(Buffer.from('45')); 315 | stream.write('3hi45j11'); 316 | stream.write('23klm'); 317 | stream.write('123'); 318 | stream.write('no45p1'); 319 | stream.end(); 320 | 321 | await team.work; 322 | }); 323 | 324 | it('changes needle mid stream (on haystack)', async () => { 325 | 326 | const team = new Teamwork.Team(); 327 | const result = []; 328 | 329 | const stream = new Nigel.Stream(Buffer.from('123')); 330 | stream.on('close', () => { 331 | 332 | expect(result).to.equal([1, 'abc', 1, 'de', 'fg', /**/ '12', '3hi', 1, 'j11', '23klm', '123', 'no', 1, 'p1']); 333 | team.attend(); 334 | }); 335 | 336 | stream.on('needle', () => { 337 | 338 | result.push(1); 339 | }); 340 | 341 | stream.on('haystack', (chunk) => { 342 | 343 | result.push(chunk.toString()); 344 | if (result.length === 5) { // After getting 'fg' 345 | stream.needle(Buffer.from('45')); 346 | } 347 | }); 348 | 349 | stream.write('12'); 350 | stream.write('3abc123de'); 351 | stream.write('fg12'); 352 | stream.write('3hi45j11'); 353 | stream.write('23klm'); 354 | stream.write('123'); 355 | stream.write('no45p1'); 356 | stream.end(); 357 | 358 | await team.work; 359 | }); 360 | 361 | it('changes needle mid stream (on needle)', async () => { 362 | 363 | const team = new Teamwork.Team(); 364 | const result = []; 365 | 366 | const stream = new Nigel.Stream(Buffer.from('12')); 367 | stream.on('close', () => { 368 | 369 | expect(result).to.equal(['a', 1, /**/ '3abc', 1, 'de', 'fg', 1, 'hi45j1', 1, 'klm', 1, 'no45p', '1']); 370 | team.attend(); 371 | }); 372 | 373 | stream.on('needle', () => { 374 | 375 | result.push(1); 376 | if (result.length === 2) { // After first needle 377 | stream.needle(Buffer.from('123')); 378 | } 379 | }); 380 | 381 | stream.on('haystack', (chunk) => { 382 | 383 | result.push(chunk.toString()); 384 | }); 385 | 386 | stream.write('a12'); 387 | stream.write('3abc123de'); 388 | stream.write('fg12'); 389 | stream.write('3hi45j11'); 390 | stream.write('23klm'); 391 | stream.write('123'); 392 | stream.write('no45p1'); 393 | stream.end(); 394 | 395 | await team.work; 396 | }); 397 | 398 | it('retains partial needle before needle', async () => { 399 | 400 | const team = new Teamwork.Team(); 401 | const result = []; 402 | 403 | const stream = new Nigel.Stream(Buffer.from('\r\n')); 404 | stream.on('close', () => { 405 | 406 | expect(result).to.equal(['abc', 1, 'defg', 1, 1, 'hijk\r', 1, 'lmnop\r', 1]); 407 | team.attend(); 408 | }); 409 | 410 | stream.on('needle', () => { 411 | 412 | result.push(1); 413 | }); 414 | 415 | stream.on('haystack', (chunk) => { 416 | 417 | result.push(chunk.toString()); 418 | }); 419 | 420 | stream.write('abc\r\ndefg\r\n\r\nhijk\r\r\nlmnop\r\r\n'); 421 | stream.end(); 422 | 423 | await team.work; 424 | }); 425 | 426 | it('emits events in correct order when nesting streams', async () => { 427 | 428 | const team = new Teamwork.Team(); 429 | const test = '1x2|3|4x|5|6|x7'; 430 | let result = ''; 431 | 432 | const x = new Nigel.Stream(Buffer.from('x')); 433 | const l = new Nigel.Stream(Buffer.from('|')); 434 | 435 | x.once('close', () => { 436 | 437 | l.end(); 438 | }); 439 | 440 | l.once('close', () => { 441 | 442 | expect(result).to.equal(test.replace(/\|/g, '[').replace(/x/g, '*')); 443 | team.attend(); 444 | }); 445 | 446 | x.on('needle', () => { 447 | 448 | result = result + '*'; 449 | }); 450 | 451 | x.on('haystack', (chunk) => { 452 | 453 | l.write(chunk); 454 | }); 455 | 456 | l.on('needle', () => { 457 | 458 | result = result + '['; 459 | }); 460 | 461 | l.on('haystack', (chunk) => { 462 | 463 | result = result + chunk.toString(); 464 | }); 465 | 466 | x.write(test); 467 | x.end(); 468 | 469 | await team.work; 470 | }); 471 | }); 472 | 473 | describe('flush()', () => { 474 | 475 | it('emits events in correct order when nesting streams (partial needle)', async () => { 476 | 477 | const team = new Teamwork.Team(); 478 | const test = '7vx7vx7vx'; 479 | let result = ''; 480 | 481 | const x = new Nigel.Stream(Buffer.from('x')); 482 | const l = new Nigel.Stream(Buffer.from('v|')); 483 | 484 | x.once('close', () => { 485 | 486 | l.end(); 487 | }); 488 | 489 | l.once('close', () => { 490 | 491 | expect(result).to.equal(test.replace(/v\|/g, '[').replace(/x/g, '*')); 492 | team.attend(); 493 | }); 494 | 495 | x.on('needle', () => { 496 | 497 | l.flush(); 498 | result = result + '*'; 499 | }); 500 | 501 | x.on('haystack', (chunk) => { 502 | 503 | l.write(chunk); 504 | }); 505 | 506 | l.on('needle', () => { 507 | 508 | result = result + '['; 509 | }); 510 | 511 | l.on('haystack', (chunk) => { 512 | 513 | result = result + chunk.toString(); 514 | }); 515 | 516 | x.write(test); 517 | x.end(); 518 | 519 | await team.work; 520 | }); 521 | }); 522 | }); 523 | --------------------------------------------------------------------------------