CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
index.js142 linesDownload Raw Back to split2
1/*2Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com>3 4Permission to use, copy, modify, and/or distribute this software for any5purpose with or without fee is hereby granted, provided that the above6copyright notice and this permission notice appear in all copies.7 8THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES9WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF10MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR11ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES12WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN13ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR14IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.15*/16 17'use strict'18 19const { Transform } = require('stream')20const { StringDecoder } = require('string_decoder')21const kLast = Symbol('last')22const kDecoder = Symbol('decoder')23 24function transform (chunk, enc, cb) {25  let list26  if (this.overflow) { // Line buffer is full. Skip to start of next line.27    const buf = this[kDecoder].write(chunk)28    list = buf.split(this.matcher)29 30    if (list.length === 1) return cb() // Line ending not found. Discard entire chunk.31 32    // Line ending found. Discard trailing fragment of previous line and reset overflow state.33    list.shift()34    this.overflow = false35  } else {36    this[kLast] += this[kDecoder].write(chunk)37    list = this[kLast].split(this.matcher)38  }39 40  this[kLast] = list.pop()41 42  for (let i = 0; i < list.length; i++) {43    try {44      push(this, this.mapper(list[i]))45    } catch (error) {46      return cb(error)47    }48  }49 50  this.overflow = this[kLast].length > this.maxLength51  if (this.overflow && !this.skipOverflow) {52    cb(new Error('maximum buffer reached'))53    return54  }55 56  cb()57}58 59function flush (cb) {60  // forward any gibberish left in there61  this[kLast] += this[kDecoder].end()62 63  if (this[kLast]) {64    try {65      push(this, this.mapper(this[kLast]))66    } catch (error) {67      return cb(error)68    }69  }70 71  cb()72}73 74function push (self, val) {75  if (val !== undefined) {76    self.push(val)77  }78}79 80function noop (incoming) {81  return incoming82}83 84function split (matcher, mapper, options) {85  // Set defaults for any arguments not supplied.86  matcher = matcher || /\r?\n/87  mapper = mapper || noop88  options = options || {}89 90  // Test arguments explicitly.91  switch (arguments.length) {92    case 1:93      // If mapper is only argument.94      if (typeof matcher === 'function') {95        mapper = matcher96        matcher = /\r?\n/97      // If options is only argument.98      } else if (typeof matcher === 'object' && !(matcher instanceof RegExp) && !matcher[Symbol.split]) {99        options = matcher100        matcher = /\r?\n/101      }102      break103 104    case 2:105      // If mapper and options are arguments.106      if (typeof matcher === 'function') {107        options = mapper108        mapper = matcher109        matcher = /\r?\n/110      // If matcher and options are arguments.111      } else if (typeof mapper === 'object') {112        options = mapper113        mapper = noop114      }115  }116 117  options = Object.assign({}, options)118  options.autoDestroy = true119  options.transform = transform120  options.flush = flush121  options.readableObjectMode = true122 123  const stream = new Transform(options)124 125  stream[kLast] = ''126  stream[kDecoder] = new StringDecoder('utf8')127  stream.matcher = matcher128  stream.mapper = mapper129  stream.maxLength = options.maxLength130  stream.skipOverflow = options.skipOverflow || false131  stream.overflow = false132  stream._destroy = function (err, cb) {133    // Weird Node v12 bug that we need to work around134    this._writableState.errorEmitted = false135    cb(err)136  }137 138  return stream139}140 141module.exports = split142