Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.9 KiB

  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