Socket
Socket
Sign inDemoInstall

bop

Package Overview
Dependencies
0
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bop

Bop, an ultra fast Boyer-Moore parser/matcher optimized for string and buffer patterns (<= 255 bytes), then it is ideal for parsing multipart/form-data streams, that have a pattern / boundary length < ~70 bytes.


Version published
Weekly downloads
3
decreased by-76.92%
Maintainers
1
Install size
40.8 kB
Created
Weekly downloads
 

Readme

Source

Bop

NPM VERSION CODACY BADGE CODECLIMATE-TEST-COVERAGE LICENSE

NODE VERSION TRAVIS CI BUILD BUILD STATUS DEVDEPENDENCY STATUS

NPM MONTHLY NPM YEARLY

NPM GRAPH

  • Bop is a very fast Boyer-Moore parser for string or buffer patterns.
  • It is optimized for using with pattern strings/buffers <= 255 bytes.
  • It is ideal, for example, to parse streams like multipart/form-data ones, in which pattern/boundary length < ~70 bytes).

Main features

Given a m-length pattern and n-length data, and σ-length alphabet ( σ = 256 ):

  • it performs the comparisons from right to left.
  • preprocessing phase in O(m+σ) time and space complexity.
  • searching phase in O(m*n) time complexity.
  • 3*n text character comparisons in the worst case when searching for a non periodic pattern.
  • O(n/m) best performance.

See Lecroq for reference and also Qap, a QuickSearch parser.

Install

$ npm install bop [-g]

require:

var Bop = require( 'bop' );

Run Tests

$cd bop/
$npm test

to execute a single test file simply do:

 $ node test/file-name.js

Run Benchmarks

$ cd bop/
$ npm run bench

Constructor

Create an instance, using a pattern.

Bop( Buffer pattern | String pattern )
// or
new Bop( Buffer pattern | String pattern )

Methods

Arguments within [] are optional.

// Change the current pattern to search.
Bop#set( Buffer pattern | String pattern ) : Buffer

/*
 * List all pattern occurrences into a String or Buffer data.
 * It returns a new array of indexes, or populates an array
 * passed as the last argument.
 *
 * NOTE: it is faster using Buffers.
 *
 */
Bop#parse( String data | Buffer data [, Number startFromIndex [, Number limitResultsTo [, Array array ] ] ] ) : Array

/*
 * Strict parse, it's the same as parse, but it parses data
 * without collecting overlapping sequences.
 *
 * Example with CRLF sequence:
 *
 * - bop pattern is set to: "\r\n\r\n"                 (CR LF CR LF)
 * - data to parse is:      "\r\n\r\n\r\n\r\n\r\n\r\n" (CR LF CR LF CR LF CR LF )
 * 
 * - with Bop.parse( data ) we get 3 indexes as results: [0, 2, 4]
 *
 *       0  1  2  3  4  5  6  7
 *      -----------------------
 *   p: CR LF CR LF
 *   d: CR LF CR LF CR LF CR LF
 *
 *   p: ----> CR LF CR LF
 *   d: CR LF CR LF CR LF CR LF
 *
 *   p: ----------> CR LF CR LF
 *   d: CR LF CR LF CR LF CR LF
 *
 * - with Bop.sparse( data ) we get only 2 results: [0, 4]
 *
 *       0  1  2  3  4  5  6  7
 *      -----------------------
 *   p: CR LF CR LF
 *   d: CR LF CR LF CR LF CR LF
 *
 *   p: ----------> CR LF CR LF
 *   d: CR LF CR LF CR LF CR LF
 *
 */
Bop#sparse( String data | Buffer data [, Number startFromIndex [, Number limitResultsTo [, Array array ] ] ] ) : Array

Usage Example

var Bop = require( 'bop' )
    , pattern = 'hellofolks\r\n'
    , somedata = 'hehehe' + pattern +'eheheh' + pattern
    , bop = Bop( pattern )
    // parse data from beginning
    , results = bop.parse( somedata )
    ;

Benchmark for a short pattern ( length <= 255 bytes )

Parser uses 3 Buffers 256-bytes long to build shifting tables, then:

  • Pattern parsing / table creation space and time complexity is O(σ).
  • Very low memory footprint.
  • Ultra fast to preprocess pattern ( = tables creation ).
  $ node bench/small-pattern-data-rate

for default it:

  • uses a pattern string of 57 bytes/chars.
  • builds a data buffer of 700 MB in memory.
  • uses a redundancy/distance factor for pattern strings equal to 2. The bigger the value, the lesser are occurrences of pattern string into the text buffer.

Custom Usage:

  # with [testBufferSizeInMB] [distanceFactor] [aStringPattern]
  $ node bench/small-pattern-data-rate.js 700 4 "that'sallfolks"
Benchmark for a big pattern ( length > 255 bytes )

Parser uses 3 arrays to build shifting tables for big patterns, then:

  • there will be an high memory consumption, due to the use of arrays.
  • it will take a long time to preprocess pattern ( = tables creation ).
  $ node bench/big-pattern-data-rate
  • it uses a very big pattern ( 20 MBytes ).
  • it builds a data buffer of 300 MBytes, copying the same pattern 12 times.

See bench dir.

MIT License

Copyright (c) 2013-present < Guglielmo Ferri : 44gatti@gmail.com >

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Last updated on 13 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc