File
Blob: samples/nodejs-compat-streams-split2/split2.js
| 1 | // https://www.npmjs.com/package/split2 |
| 2 | |
| 3 | /* |
| 4 | Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com> |
| 5 | |
| 6 | Permission to use, copy, modify, and/or distribute this software for any |
| 7 | purpose with or without fee is hereby granted, provided that the above |
| 8 | copyright notice and this permission notice appear in all copies. |
| 9 | |
| 10 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR |
| 16 | IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | 'use strict' |
| 20 | |
| 21 | const { Transform } = require('stream') |
| 22 | const { StringDecoder } = require('string_decoder') |
| 23 | const kLast = Symbol('last') |
| 24 | const kDecoder = Symbol('decoder') |
| 25 | |
| 26 | function transform (chunk, enc, cb) { |
| 27 | let list |
| 28 | if (this.overflow) { // Line buffer is full. Skip to start of next line. |
| 29 | const buf = this[kDecoder].write(chunk) |
| 30 | list = buf.split(this.matcher) |
| 31 | |
| 32 | if (list.length === 1) return cb() // Line ending not found. Discard entire chunk. |
| 33 | |
| 34 | // Line ending found. Discard trailing fragment of previous line and reset overflow state. |
| 35 | list.shift() |
| 36 | this.overflow = false |
| 37 | } else { |
| 38 | this[kLast] += this[kDecoder].write(chunk) |
| 39 | list = this[kLast].split(this.matcher) |
| 40 | } |
| 41 | |
| 42 | this[kLast] = list.pop() |
| 43 | |
| 44 | for (let i = 0; i < list.length; i++) { |
| 45 | try { |
| 46 | push(this, this.mapper(list[i])) |
| 47 | } catch (error) { |
| 48 | return cb(error) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | this.overflow = this[kLast].length > this.maxLength |
| 53 | if (this.overflow && !this.skipOverflow) { |
| 54 | cb(new Error('maximum buffer reached')) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | cb() |
| 59 | } |
| 60 | |
| 61 | function flush (cb) { |
| 62 | // forward any gibberish left in there |
| 63 | this[kLast] += this[kDecoder].end() |
| 64 | |
| 65 | if (this[kLast]) { |
| 66 | try { |
| 67 | push(this, this.mapper(this[kLast])) |
| 68 | } catch (error) { |
| 69 | return cb(error) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | cb() |
| 74 | } |
| 75 | |
| 76 | function push (self, val) { |
| 77 | if (val !== undefined) { |
| 78 | self.push(val) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function noop (incoming) { |
| 83 | return incoming |
| 84 | } |
| 85 | |
| 86 | function split (matcher, mapper, options) { |
| 87 | // Set defaults for any arguments not supplied. |
| 88 | matcher = matcher || /\r?\n/ |
| 89 | mapper = mapper || noop |
| 90 | options = options || {} |
| 91 | |
| 92 | // Test arguments explicitly. |
| 93 | switch (arguments.length) { |
| 94 | case 1: |
| 95 | // If mapper is only argument. |
| 96 | if (typeof matcher === 'function') { |
| 97 | mapper = matcher |
| 98 | matcher = /\r?\n/ |
| 99 | // If options is only argument. |
| 100 | } else if (typeof matcher === 'object' && !(matcher instanceof RegExp) && !matcher[Symbol.split]) { |
| 101 | options = matcher |
| 102 | matcher = /\r?\n/ |
| 103 | } |
| 104 | break |
| 105 | |
| 106 | case 2: |
| 107 | // If mapper and options are arguments. |
| 108 | if (typeof matcher === 'function') { |
| 109 | options = mapper |
| 110 | mapper = matcher |
| 111 | matcher = /\r?\n/ |
| 112 | // If matcher and options are arguments. |
| 113 | } else if (typeof mapper === 'object') { |
| 114 | options = mapper |
| 115 | mapper = noop |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | options = Object.assign({}, options) |
| 120 | options.autoDestroy = true |
| 121 | options.transform = transform |
| 122 | options.flush = flush |
| 123 | options.readableObjectMode = true |
| 124 | |
| 125 | const stream = new Transform(options) |
| 126 | |
| 127 | stream[kLast] = '' |
| 128 | stream[kDecoder] = new StringDecoder('utf8') |
| 129 | stream.matcher = matcher |
| 130 | stream.mapper = mapper |
| 131 | stream.maxLength = options.maxLength |
| 132 | stream.skipOverflow = options.skipOverflow || false |
| 133 | stream.overflow = false |
| 134 | stream._destroy = function (err, cb) { |
| 135 | // Weird Node v12 bug that we need to work around |
| 136 | this._writableState.errorEmitted = false |
| 137 | cb(err) |
| 138 | } |
| 139 | |
| 140 | return stream |
| 141 | } |
| 142 | |
| 143 | module.exports = split |