summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/npm/node_modules/@fastify/busboy/deps
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/Provider/npm/node_modules/@fastify/busboy/deps')
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/LICENSE19
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js207
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js100
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js13
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/dicer.d.ts164
-rw-r--r--g4f/Provider/npm/node_modules/@fastify/busboy/deps/streamsearch/sbmh.js228
6 files changed, 0 insertions, 731 deletions
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/LICENSE b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/LICENSE
deleted file mode 100644
index 290762e9..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/LICENSE
+++ /dev/null
@@ -1,19 +0,0 @@
-Copyright Brian White. All rights reserved.
-
-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. \ No newline at end of file
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js
deleted file mode 100644
index 79da160c..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/Dicer.js
+++ /dev/null
@@ -1,207 +0,0 @@
-'use strict'
-
-const WritableStream = require('node:stream').Writable
-const inherits = require('node:util').inherits
-
-const StreamSearch = require('../../streamsearch/sbmh')
-
-const PartStream = require('./PartStream')
-const HeaderParser = require('./HeaderParser')
-
-const DASH = 45
-const B_ONEDASH = Buffer.from('-')
-const B_CRLF = Buffer.from('\r\n')
-const EMPTY_FN = function () {}
-
-function Dicer (cfg) {
- if (!(this instanceof Dicer)) { return new Dicer(cfg) }
- WritableStream.call(this, cfg)
-
- if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string')) { throw new TypeError('Boundary required') }
-
- if (typeof cfg.boundary === 'string') { this.setBoundary(cfg.boundary) } else { this._bparser = undefined }
-
- this._headerFirst = cfg.headerFirst
-
- this._dashes = 0
- this._parts = 0
- this._finished = false
- this._realFinish = false
- this._isPreamble = true
- this._justMatched = false
- this._firstWrite = true
- this._inHeader = true
- this._part = undefined
- this._cb = undefined
- this._ignoreData = false
- this._partOpts = { highWaterMark: cfg.partHwm }
- this._pause = false
-
- const self = this
- this._hparser = new HeaderParser(cfg)
- this._hparser.on('header', function (header) {
- self._inHeader = false
- self._part.emit('header', header)
- })
-}
-inherits(Dicer, WritableStream)
-
-Dicer.prototype.emit = function (ev) {
- if (ev === 'finish' && !this._realFinish) {
- if (!this._finished) {
- const self = this
- process.nextTick(function () {
- self.emit('error', new Error('Unexpected end of multipart data'))
- if (self._part && !self._ignoreData) {
- const type = (self._isPreamble ? 'Preamble' : 'Part')
- self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'))
- self._part.push(null)
- process.nextTick(function () {
- self._realFinish = true
- self.emit('finish')
- self._realFinish = false
- })
- return
- }
- self._realFinish = true
- self.emit('finish')
- self._realFinish = false
- })
- }
- } else { WritableStream.prototype.emit.apply(this, arguments) }
-}
-
-Dicer.prototype._write = function (data, encoding, cb) {
- // ignore unexpected data (e.g. extra trailer data after finished)
- if (!this._hparser && !this._bparser) { return cb() }
-
- if (this._headerFirst && this._isPreamble) {
- if (!this._part) {
- this._part = new PartStream(this._partOpts)
- if (this._events.preamble) { this.emit('preamble', this._part) } else { this._ignore() }
- }
- const r = this._hparser.push(data)
- if (!this._inHeader && r !== undefined && r < data.length) { data = data.slice(r) } else { return cb() }
- }
-
- // allows for "easier" testing
- if (this._firstWrite) {
- this._bparser.push(B_CRLF)
- this._firstWrite = false
- }
-
- this._bparser.push(data)
-
- if (this._pause) { this._cb = cb } else { cb() }
-}
-
-Dicer.prototype.reset = function () {
- this._part = undefined
- this._bparser = undefined
- this._hparser = undefined
-}
-
-Dicer.prototype.setBoundary = function (boundary) {
- const self = this
- this._bparser = new StreamSearch('\r\n--' + boundary)
- this._bparser.on('info', function (isMatch, data, start, end) {
- self._oninfo(isMatch, data, start, end)
- })
-}
-
-Dicer.prototype._ignore = function () {
- if (this._part && !this._ignoreData) {
- this._ignoreData = true
- this._part.on('error', EMPTY_FN)
- // we must perform some kind of read on the stream even though we are
- // ignoring the data, otherwise node's Readable stream will not emit 'end'
- // after pushing null to the stream
- this._part.resume()
- }
-}
-
-Dicer.prototype._oninfo = function (isMatch, data, start, end) {
- let buf; const self = this; let i = 0; let r; let shouldWriteMore = true
-
- if (!this._part && this._justMatched && data) {
- while (this._dashes < 2 && (start + i) < end) {
- if (data[start + i] === DASH) {
- ++i
- ++this._dashes
- } else {
- if (this._dashes) { buf = B_ONEDASH }
- this._dashes = 0
- break
- }
- }
- if (this._dashes === 2) {
- if ((start + i) < end && this._events.trailer) { this.emit('trailer', data.slice(start + i, end)) }
- this.reset()
- this._finished = true
- // no more parts will be added
- if (self._parts === 0) {
- self._realFinish = true
- self.emit('finish')
- self._realFinish = false
- }
- }
- if (this._dashes) { return }
- }
- if (this._justMatched) { this._justMatched = false }
- if (!this._part) {
- this._part = new PartStream(this._partOpts)
- this._part._read = function (n) {
- self._unpause()
- }
- if (this._isPreamble && this._events.preamble) { this.emit('preamble', this._part) } else if (this._isPreamble !== true && this._events.part) { this.emit('part', this._part) } else { this._ignore() }
- if (!this._isPreamble) { this._inHeader = true }
- }
- if (data && start < end && !this._ignoreData) {
- if (this._isPreamble || !this._inHeader) {
- if (buf) { shouldWriteMore = this._part.push(buf) }
- shouldWriteMore = this._part.push(data.slice(start, end))
- if (!shouldWriteMore) { this._pause = true }
- } else if (!this._isPreamble && this._inHeader) {
- if (buf) { this._hparser.push(buf) }
- r = this._hparser.push(data.slice(start, end))
- if (!this._inHeader && r !== undefined && r < end) { this._oninfo(false, data, start + r, end) }
- }
- }
- if (isMatch) {
- this._hparser.reset()
- if (this._isPreamble) { this._isPreamble = false } else {
- if (start !== end) {
- ++this._parts
- this._part.on('end', function () {
- if (--self._parts === 0) {
- if (self._finished) {
- self._realFinish = true
- self.emit('finish')
- self._realFinish = false
- } else {
- self._unpause()
- }
- }
- })
- }
- }
- this._part.push(null)
- this._part = undefined
- this._ignoreData = false
- this._justMatched = true
- this._dashes = 0
- }
-}
-
-Dicer.prototype._unpause = function () {
- if (!this._pause) { return }
-
- this._pause = false
- if (this._cb) {
- const cb = this._cb
- this._cb = undefined
- cb()
- }
-}
-
-module.exports = Dicer
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js
deleted file mode 100644
index 65f667b5..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/HeaderParser.js
+++ /dev/null
@@ -1,100 +0,0 @@
-'use strict'
-
-const EventEmitter = require('node:events').EventEmitter
-const inherits = require('node:util').inherits
-const getLimit = require('../../../lib/utils/getLimit')
-
-const StreamSearch = require('../../streamsearch/sbmh')
-
-const B_DCRLF = Buffer.from('\r\n\r\n')
-const RE_CRLF = /\r\n/g
-const RE_HDR = /^([^:]+):[ \t]?([\x00-\xFF]+)?$/ // eslint-disable-line no-control-regex
-
-function HeaderParser (cfg) {
- EventEmitter.call(this)
-
- cfg = cfg || {}
- const self = this
- this.nread = 0
- this.maxed = false
- this.npairs = 0
- this.maxHeaderPairs = getLimit(cfg, 'maxHeaderPairs', 2000)
- this.maxHeaderSize = getLimit(cfg, 'maxHeaderSize', 80 * 1024)
- this.buffer = ''
- this.header = {}
- this.finished = false
- this.ss = new StreamSearch(B_DCRLF)
- this.ss.on('info', function (isMatch, data, start, end) {
- if (data && !self.maxed) {
- if (self.nread + end - start >= self.maxHeaderSize) {
- end = self.maxHeaderSize - self.nread + start
- self.nread = self.maxHeaderSize
- self.maxed = true
- } else { self.nread += (end - start) }
-
- self.buffer += data.toString('binary', start, end)
- }
- if (isMatch) { self._finish() }
- })
-}
-inherits(HeaderParser, EventEmitter)
-
-HeaderParser.prototype.push = function (data) {
- const r = this.ss.push(data)
- if (this.finished) { return r }
-}
-
-HeaderParser.prototype.reset = function () {
- this.finished = false
- this.buffer = ''
- this.header = {}
- this.ss.reset()
-}
-
-HeaderParser.prototype._finish = function () {
- if (this.buffer) { this._parseHeader() }
- this.ss.matches = this.ss.maxMatches
- const header = this.header
- this.header = {}
- this.buffer = ''
- this.finished = true
- this.nread = this.npairs = 0
- this.maxed = false
- this.emit('header', header)
-}
-
-HeaderParser.prototype._parseHeader = function () {
- if (this.npairs === this.maxHeaderPairs) { return }
-
- const lines = this.buffer.split(RE_CRLF)
- const len = lines.length
- let m, h
-
- for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
- if (lines[i].length === 0) { continue }
- if (lines[i][0] === '\t' || lines[i][0] === ' ') {
- // folded header content
- // RFC2822 says to just remove the CRLF and not the whitespace following
- // it, so we follow the RFC and include the leading whitespace ...
- if (h) {
- this.header[h][this.header[h].length - 1] += lines[i]
- continue
- }
- }
-
- const posColon = lines[i].indexOf(':')
- if (
- posColon === -1 ||
- posColon === 0
- ) {
- return
- }
- m = RE_HDR.exec(lines[i])
- h = m[1].toLowerCase()
- this.header[h] = this.header[h] || []
- this.header[h].push((m[2] || ''))
- if (++this.npairs === this.maxHeaderPairs) { break }
- }
-}
-
-module.exports = HeaderParser
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js
deleted file mode 100644
index c91da1c4..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/PartStream.js
+++ /dev/null
@@ -1,13 +0,0 @@
-'use strict'
-
-const inherits = require('node:util').inherits
-const ReadableStream = require('node:stream').Readable
-
-function PartStream (opts) {
- ReadableStream.call(this, opts)
-}
-inherits(PartStream, ReadableStream)
-
-PartStream.prototype._read = function (n) {}
-
-module.exports = PartStream
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/dicer.d.ts b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/dicer.d.ts
deleted file mode 100644
index 3c5b8962..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/dicer/lib/dicer.d.ts
+++ /dev/null
@@ -1,164 +0,0 @@
-// Type definitions for dicer 0.2
-// Project: https://github.com/mscdex/dicer
-// Definitions by: BendingBender <https://github.com/BendingBender>
-// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
-// TypeScript Version: 2.2
-/// <reference types="node" />
-
-import stream = require("stream");
-
-// tslint:disable:unified-signatures
-
-/**
- * A very fast streaming multipart parser for node.js.
- * Dicer is a WritableStream
- *
- * Dicer (special) events:
- * - on('finish', ()) - Emitted when all parts have been parsed and the Dicer instance has been ended.
- * - on('part', (stream: PartStream)) - Emitted when a new part has been found.
- * - on('preamble', (stream: PartStream)) - Emitted for preamble if you should happen to need it (can usually be ignored).
- * - on('trailer', (data: Buffer)) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too).
- */
-export class Dicer extends stream.Writable {
- /**
- * Creates and returns a new Dicer instance with the following valid config settings:
- *
- * @param config The configuration to use
- */
- constructor(config: Dicer.Config);
- /**
- * Sets the boundary to use for parsing and performs some initialization needed for parsing.
- * You should only need to use this if you set headerFirst to true in the constructor and are parsing the boundary from the preamble header.
- *
- * @param boundary The boundary to use
- */
- setBoundary(boundary: string): void;
- addListener(event: "finish", listener: () => void): this;
- addListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- addListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- addListener(event: "trailer", listener: (data: Buffer) => void): this;
- addListener(event: "close", listener: () => void): this;
- addListener(event: "drain", listener: () => void): this;
- addListener(event: "error", listener: (err: Error) => void): this;
- addListener(event: "pipe", listener: (src: stream.Readable) => void): this;
- addListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
- addListener(event: string, listener: (...args: any[]) => void): this;
- on(event: "finish", listener: () => void): this;
- on(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- on(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- on(event: "trailer", listener: (data: Buffer) => void): this;
- on(event: "close", listener: () => void): this;
- on(event: "drain", listener: () => void): this;
- on(event: "error", listener: (err: Error) => void): this;
- on(event: "pipe", listener: (src: stream.Readable) => void): this;
- on(event: "unpipe", listener: (src: stream.Readable) => void): this;
- on(event: string, listener: (...args: any[]) => void): this;
- once(event: "finish", listener: () => void): this;
- once(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- once(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- once(event: "trailer", listener: (data: Buffer) => void): this;
- once(event: "close", listener: () => void): this;
- once(event: "drain", listener: () => void): this;
- once(event: "error", listener: (err: Error) => void): this;
- once(event: "pipe", listener: (src: stream.Readable) => void): this;
- once(event: "unpipe", listener: (src: stream.Readable) => void): this;
- once(event: string, listener: (...args: any[]) => void): this;
- prependListener(event: "finish", listener: () => void): this;
- prependListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- prependListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- prependListener(event: "trailer", listener: (data: Buffer) => void): this;
- prependListener(event: "close", listener: () => void): this;
- prependListener(event: "drain", listener: () => void): this;
- prependListener(event: "error", listener: (err: Error) => void): this;
- prependListener(event: "pipe", listener: (src: stream.Readable) => void): this;
- prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
- prependListener(event: string, listener: (...args: any[]) => void): this;
- prependOnceListener(event: "finish", listener: () => void): this;
- prependOnceListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- prependOnceListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- prependOnceListener(event: "trailer", listener: (data: Buffer) => void): this;
- prependOnceListener(event: "close", listener: () => void): this;
- prependOnceListener(event: "drain", listener: () => void): this;
- prependOnceListener(event: "error", listener: (err: Error) => void): this;
- prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this;
- prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
- prependOnceListener(event: string, listener: (...args: any[]) => void): this;
- removeListener(event: "finish", listener: () => void): this;
- removeListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
- removeListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
- removeListener(event: "trailer", listener: (data: Buffer) => void): this;
- removeListener(event: "close", listener: () => void): this;
- removeListener(event: "drain", listener: () => void): this;
- removeListener(event: "error", listener: (err: Error) => void): this;
- removeListener(event: "pipe", listener: (src: stream.Readable) => void): this;
- removeListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
- removeListener(event: string, listener: (...args: any[]) => void): this;
-}
-
-declare namespace Dicer {
- interface Config {
- /**
- * This is the boundary used to detect the beginning of a new part.
- */
- boundary?: string | undefined;
- /**
- * If true, preamble header parsing will be performed first.
- */
- headerFirst?: boolean | undefined;
- /**
- * The maximum number of header key=>value pairs to parse Default: 2000 (same as node's http).
- */
- maxHeaderPairs?: number | undefined;
- }
-
- /**
- * PartStream is a _ReadableStream_
- *
- * PartStream (special) events:
- * - on('header', (header: object)) - An object containing the header for this particular part. Each property value is an array of one or more string values.
- */
- interface PartStream extends stream.Readable {
- addListener(event: "header", listener: (header: object) => void): this;
- addListener(event: "close", listener: () => void): this;
- addListener(event: "data", listener: (chunk: Buffer | string) => void): this;
- addListener(event: "end", listener: () => void): this;
- addListener(event: "readable", listener: () => void): this;
- addListener(event: "error", listener: (err: Error) => void): this;
- addListener(event: string, listener: (...args: any[]) => void): this;
- on(event: "header", listener: (header: object) => void): this;
- on(event: "close", listener: () => void): this;
- on(event: "data", listener: (chunk: Buffer | string) => void): this;
- on(event: "end", listener: () => void): this;
- on(event: "readable", listener: () => void): this;
- on(event: "error", listener: (err: Error) => void): this;
- on(event: string, listener: (...args: any[]) => void): this;
- once(event: "header", listener: (header: object) => void): this;
- once(event: "close", listener: () => void): this;
- once(event: "data", listener: (chunk: Buffer | string) => void): this;
- once(event: "end", listener: () => void): this;
- once(event: "readable", listener: () => void): this;
- once(event: "error", listener: (err: Error) => void): this;
- once(event: string, listener: (...args: any[]) => void): this;
- prependListener(event: "header", listener: (header: object) => void): this;
- prependListener(event: "close", listener: () => void): this;
- prependListener(event: "data", listener: (chunk: Buffer | string) => void): this;
- prependListener(event: "end", listener: () => void): this;
- prependListener(event: "readable", listener: () => void): this;
- prependListener(event: "error", listener: (err: Error) => void): this;
- prependListener(event: string, listener: (...args: any[]) => void): this;
- prependOnceListener(event: "header", listener: (header: object) => void): this;
- prependOnceListener(event: "close", listener: () => void): this;
- prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this;
- prependOnceListener(event: "end", listener: () => void): this;
- prependOnceListener(event: "readable", listener: () => void): this;
- prependOnceListener(event: "error", listener: (err: Error) => void): this;
- prependOnceListener(event: string, listener: (...args: any[]) => void): this;
- removeListener(event: "header", listener: (header: object) => void): this;
- removeListener(event: "close", listener: () => void): this;
- removeListener(event: "data", listener: (chunk: Buffer | string) => void): this;
- removeListener(event: "end", listener: () => void): this;
- removeListener(event: "readable", listener: () => void): this;
- removeListener(event: "error", listener: (err: Error) => void): this;
- removeListener(event: string, listener: (...args: any[]) => void): this;
- }
-} \ No newline at end of file
diff --git a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/streamsearch/sbmh.js b/g4f/Provider/npm/node_modules/@fastify/busboy/deps/streamsearch/sbmh.js
deleted file mode 100644
index b90c0e86..00000000
--- a/g4f/Provider/npm/node_modules/@fastify/busboy/deps/streamsearch/sbmh.js
+++ /dev/null
@@ -1,228 +0,0 @@
-'use strict'
-
-/**
- * Copyright Brian White. All rights reserved.
- *
- * @see https://github.com/mscdex/streamsearch
- *
- * 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.
- *
- * Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
- * by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
- */
-const EventEmitter = require('node:events').EventEmitter
-const inherits = require('node:util').inherits
-
-function SBMH (needle) {
- if (typeof needle === 'string') {
- needle = Buffer.from(needle)
- }
-
- if (!Buffer.isBuffer(needle)) {
- throw new TypeError('The needle has to be a String or a Buffer.')
- }
-
- const needleLength = needle.length
-
- if (needleLength === 0) {
- throw new Error('The needle cannot be an empty String/Buffer.')
- }
-
- if (needleLength > 256) {
- throw new Error('The needle cannot have a length bigger than 256.')
- }
-
- this.maxMatches = Infinity
- this.matches = 0
-
- this._occ = new Array(256)
- .fill(needleLength) // Initialize occurrence table.
- this._lookbehind_size = 0
- this._needle = needle
- this._bufpos = 0
-
- this._lookbehind = Buffer.alloc(needleLength)
-
- // Populate occurrence table with analysis of the needle,
- // ignoring last letter.
- for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
- this._occ[needle[i]] = needleLength - 1 - i
- }
-}
-inherits(SBMH, EventEmitter)
-
-SBMH.prototype.reset = function () {
- this._lookbehind_size = 0
- this.matches = 0
- this._bufpos = 0
-}
-
-SBMH.prototype.push = function (chunk, pos) {
- if (!Buffer.isBuffer(chunk)) {
- chunk = Buffer.from(chunk, 'binary')
- }
- const chlen = chunk.length
- this._bufpos = pos || 0
- let r
- while (r !== chlen && this.matches < this.maxMatches) { r = this._sbmh_feed(chunk) }
- return r
-}
-
-SBMH.prototype._sbmh_feed = function (data) {
- const len = data.length
- const needle = this._needle
- const needleLength = needle.length
- const lastNeedleChar = needle[needleLength - 1]
-
- // Positive: points to a position in `data`
- // pos == 3 points to data[3]
- // Negative: points to a position in the lookbehind buffer
- // pos == -2 points to lookbehind[lookbehind_size - 2]
- let pos = -this._lookbehind_size
- let ch
-
- if (pos < 0) {
- // Lookbehind buffer is not empty. Perform Boyer-Moore-Horspool
- // search with character lookup code that considers both the
- // lookbehind buffer and the current round's haystack data.
- //
- // Loop until
- // there is a match.
- // or until
- // we've moved past the position that requires the
- // lookbehind buffer. In this case we switch to the
- // optimized loop.
- // or until
- // the character to look at lies outside the haystack.
- while (pos < 0 && pos <= len - needleLength) {
- ch = this._sbmh_lookup_char(data, pos + needleLength - 1)
-
- if (
- ch === lastNeedleChar &&
- this._sbmh_memcmp(data, pos, needleLength - 1)
- ) {
- this._lookbehind_size = 0
- ++this.matches
- this.emit('info', true)
-
- return (this._bufpos = pos + needleLength)
- }
- pos += this._occ[ch]
- }
-
- // No match.
-
- if (pos < 0) {
- // There's too few data for Boyer-Moore-Horspool to run,
- // so let's use a different algorithm to skip as much as
- // we can.
- // Forward pos until
- // the trailing part of lookbehind + data
- // looks like the beginning of the needle
- // or until
- // pos == 0
- while (pos < 0 && !this._sbmh_memcmp(data, pos, len - pos)) { ++pos }
- }
-
- if (pos >= 0) {
- // Discard lookbehind buffer.
- this.emit('info', false, this._lookbehind, 0, this._lookbehind_size)
- this._lookbehind_size = 0
- } else {
- // Cut off part of the lookbehind buffer that has
- // been processed and append the entire haystack
- // into it.
- const bytesToCutOff = this._lookbehind_size + pos
- if (bytesToCutOff > 0) {
- // The cut off data is guaranteed not to contain the needle.
- this.emit('info', false, this._lookbehind, 0, bytesToCutOff)
- }
-
- this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff,
- this._lookbehind_size - bytesToCutOff)
- this._lookbehind_size -= bytesToCutOff
-
- data.copy(this._lookbehind, this._lookbehind_size)
- this._lookbehind_size += len
-
- this._bufpos = len
- return len
- }
- }
-
- pos += (pos >= 0) * this._bufpos
-
- // Lookbehind buffer is now empty. We only need to check if the
- // needle is in the haystack.
- if (data.indexOf(needle, pos) !== -1) {
- pos = data.indexOf(needle, pos)
- ++this.matches
- if (pos > 0) { this.emit('info', true, data, this._bufpos, pos) } else { this.emit('info', true) }
-
- return (this._bufpos = pos + needleLength)
- } else {
- pos = len - needleLength
- }
-
- // There was no match. If there's trailing haystack data that we cannot
- // match yet using the Boyer-Moore-Horspool algorithm (because the trailing
- // data is less than the needle size) then match using a modified
- // algorithm that starts matching from the beginning instead of the end.
- // Whatever trailing data is left after running this algorithm is added to
- // the lookbehind buffer.
- while (
- pos < len &&
- (
- data[pos] !== needle[0] ||
- (
- (Buffer.compare(
- data.subarray(pos, pos + len - pos),
- needle.subarray(0, len - pos)
- ) !== 0)
- )
- )
- ) {
- ++pos
- }
- if (pos < len) {
- data.copy(this._lookbehind, 0, pos, pos + (len - pos))
- this._lookbehind_size = len - pos
- }
-
- // Everything until pos is guaranteed not to contain needle data.
- if (pos > 0) { this.emit('info', false, data, this._bufpos, pos < len ? pos : len) }
-
- this._bufpos = len
- return len
-}
-
-SBMH.prototype._sbmh_lookup_char = function (data, pos) {
- return (pos < 0)
- ? this._lookbehind[this._lookbehind_size + pos]
- : data[pos]
-}
-
-SBMH.prototype._sbmh_memcmp = function (data, pos, len) {
- for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
- if (this._sbmh_lookup_char(data, pos + i) !== this._needle[i]) { return false }
- }
- return true
-}
-
-module.exports = SBMH