index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2014-2016, 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. var through = require('through2')
  16. var StringDecoder = require('string_decoder').StringDecoder
  17. function transform (chunk, enc, cb) {
  18. this._last += this._decoder.write(chunk)
  19. if (this._last.length > this.maxLength) {
  20. return cb(new Error('maximum buffer reached'))
  21. }
  22. var list = this._last.split(this.matcher)
  23. this._last = list.pop()
  24. for (var i = 0; i < list.length; i++) {
  25. push(this, this.mapper(list[i]))
  26. }
  27. cb()
  28. }
  29. function flush (cb) {
  30. // forward any gibberish left in there
  31. this._last += this._decoder.end()
  32. if (this._last) {
  33. push(this, this.mapper(this._last))
  34. }
  35. cb()
  36. }
  37. function push (self, val) {
  38. if (val !== undefined) {
  39. self.push(val)
  40. }
  41. }
  42. function noop (incoming) {
  43. return incoming
  44. }
  45. function split (matcher, mapper, options) {
  46. // Set defaults for any arguments not supplied.
  47. matcher = matcher || /\r?\n/
  48. mapper = mapper || noop
  49. options = options || {}
  50. // Test arguments explicitly.
  51. switch (arguments.length) {
  52. case 1:
  53. // If mapper is only argument.
  54. if (typeof matcher === 'function') {
  55. mapper = matcher
  56. matcher = /\r?\n/
  57. // If options is only argument.
  58. } else if (typeof matcher === 'object' && !(matcher instanceof RegExp)) {
  59. options = matcher
  60. matcher = /\r?\n/
  61. }
  62. break
  63. case 2:
  64. // If mapper and options are arguments.
  65. if (typeof matcher === 'function') {
  66. options = mapper
  67. mapper = matcher
  68. matcher = /\r?\n/
  69. // If matcher and options are arguments.
  70. } else if (typeof mapper === 'object') {
  71. options = mapper
  72. mapper = noop
  73. }
  74. }
  75. var stream = through(options, transform, flush)
  76. // this stream is in objectMode only in the readable part
  77. stream._readableState.objectMode = true
  78. // objectMode default hwm is 16 and not 16384
  79. if (stream._readableState.highWaterMark && !options.highWaterMark) {
  80. stream._readableState.highWaterMark = 16
  81. }
  82. stream._last = ''
  83. stream._decoder = new StringDecoder('utf8')
  84. stream.matcher = matcher
  85. stream.mapper = mapper
  86. stream.maxLength = options.maxLength
  87. return stream
  88. }
  89. module.exports = split