index.js 3.7 KB

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