index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright (c) 2014-2018, 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('readable-stream')
  16. const { StringDecoder } = require('string_decoder')
  17. const kLast = Symbol('last')
  18. const kDecoder = Symbol('decoder')
  19. function transform (chunk, enc, cb) {
  20. var list
  21. if (this.overflow) { // Line buffer is full. Skip to start of next line.
  22. var 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 (var 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) return cb(new Error('maximum buffer reached'))
  42. cb()
  43. }
  44. function flush (cb) {
  45. // forward any gibberish left in there
  46. this[kLast] += this[kDecoder].end()
  47. if (this[kLast]) {
  48. try {
  49. push(this, this.mapper(this[kLast]))
  50. } catch (error) {
  51. return cb(error)
  52. }
  53. }
  54. cb()
  55. }
  56. function push (self, val) {
  57. if (val !== undefined) {
  58. self.push(val)
  59. }
  60. }
  61. function noop (incoming) {
  62. return incoming
  63. }
  64. function split (matcher, mapper, options) {
  65. // Set defaults for any arguments not supplied.
  66. matcher = matcher || /\r?\n/
  67. mapper = mapper || noop
  68. options = options || {}
  69. // Test arguments explicitly.
  70. switch (arguments.length) {
  71. case 1:
  72. // If mapper is only argument.
  73. if (typeof matcher === 'function') {
  74. mapper = matcher
  75. matcher = /\r?\n/
  76. // If options is only argument.
  77. } else if (typeof matcher === 'object' && !(matcher instanceof RegExp)) {
  78. options = matcher
  79. matcher = /\r?\n/
  80. }
  81. break
  82. case 2:
  83. // If mapper and options are arguments.
  84. if (typeof matcher === 'function') {
  85. options = mapper
  86. mapper = matcher
  87. matcher = /\r?\n/
  88. // If matcher and options are arguments.
  89. } else if (typeof mapper === 'object') {
  90. options = mapper
  91. mapper = noop
  92. }
  93. }
  94. options = Object.assign({}, options)
  95. options.transform = transform
  96. options.flush = flush
  97. options.readableObjectMode = true
  98. const stream = new Transform(options)
  99. stream[kLast] = ''
  100. stream[kDecoder] = new StringDecoder('utf8')
  101. stream.matcher = matcher
  102. stream.mapper = mapper
  103. stream.maxLength = options.maxLength
  104. stream.skipOverflow = options.skipOverflow
  105. stream.overflow = false
  106. return stream
  107. }
  108. module.exports = split