test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. 'use strict'
  2. var test = require('tap').test
  3. var split = require('./')
  4. var callback = require('callback-stream')
  5. var Buffer = require('safe-buffer').Buffer
  6. var strcb = callback.bind(null, { decodeStrings: false })
  7. var objcb = callback.bind(null, { objectMode: true })
  8. test('split two lines on end', function (t) {
  9. t.plan(2)
  10. var input = split()
  11. input.pipe(strcb(function (err, list) {
  12. t.error(err)
  13. t.deepEqual(list, ['hello', 'world'])
  14. }))
  15. input.end('hello\nworld')
  16. })
  17. test('split two lines on two writes', function (t) {
  18. t.plan(2)
  19. var input = split()
  20. input.pipe(strcb(function (err, list) {
  21. t.error(err)
  22. t.deepEqual(list, ['hello', 'world'])
  23. }))
  24. input.write('hello')
  25. input.write('\nworld')
  26. input.end()
  27. })
  28. test('accumulate multiple writes', function (t) {
  29. t.plan(2)
  30. var input = split()
  31. input.pipe(strcb(function (err, list) {
  32. t.error(err)
  33. t.deepEqual(list, ['helloworld'])
  34. }))
  35. input.write('hello')
  36. input.write('world')
  37. input.end()
  38. })
  39. test('split using a custom string matcher', function (t) {
  40. t.plan(2)
  41. var input = split('~')
  42. input.pipe(strcb(function (err, list) {
  43. t.error(err)
  44. t.deepEqual(list, ['hello', 'world'])
  45. }))
  46. input.end('hello~world')
  47. })
  48. test('split using a custom regexp matcher', function (t) {
  49. t.plan(2)
  50. var input = split(/~/)
  51. input.pipe(strcb(function (err, list) {
  52. t.error(err)
  53. t.deepEqual(list, ['hello', 'world'])
  54. }))
  55. input.end('hello~world')
  56. })
  57. test('support an option argument', function (t) {
  58. t.plan(2)
  59. var input = split({ highWaterMark: 2 })
  60. input.pipe(strcb(function (err, list) {
  61. t.error(err)
  62. t.deepEqual(list, ['hello', 'world'])
  63. }))
  64. input.end('hello\nworld')
  65. })
  66. test('support a mapper function', function (t) {
  67. t.plan(2)
  68. var a = { a: '42' }
  69. var b = { b: '24' }
  70. var input = split(JSON.parse)
  71. input.pipe(objcb(function (err, list) {
  72. t.error(err)
  73. t.deepEqual(list, [a, b])
  74. }))
  75. input.write(JSON.stringify(a))
  76. input.write('\n')
  77. input.end(JSON.stringify(b))
  78. })
  79. test('split lines windows-style', function (t) {
  80. t.plan(2)
  81. var input = split()
  82. input.pipe(strcb(function (err, list) {
  83. t.error(err)
  84. t.deepEqual(list, ['hello', 'world'])
  85. }))
  86. input.end('hello\r\nworld')
  87. })
  88. test('splits a buffer', function (t) {
  89. t.plan(2)
  90. var input = split()
  91. input.pipe(strcb(function (err, list) {
  92. t.error(err)
  93. t.deepEqual(list, ['hello', 'world'])
  94. }))
  95. input.end(Buffer.from('hello\nworld'))
  96. })
  97. test('do not end on undefined', function (t) {
  98. t.plan(2)
  99. var input = split(function (line) {})
  100. input.pipe(strcb(function (err, list) {
  101. t.error(err)
  102. t.deepEqual(list, [])
  103. }))
  104. input.end(Buffer.from('hello\nworld'))
  105. })
  106. test('has destroy method', function (t) {
  107. t.plan(1)
  108. var input = split(function (line) {})
  109. input.on('close', function () {
  110. t.ok(true, 'close emitted')
  111. t.end()
  112. })
  113. input.destroy()
  114. })
  115. test('support custom matcher and mapper', function (t) {
  116. t.plan(4)
  117. var a = { a: '42' }
  118. var b = { b: '24' }
  119. var input = split('~', JSON.parse)
  120. t.equal(input.matcher, '~')
  121. t.equal(typeof input.mapper, 'function')
  122. input.pipe(objcb(function (err, list) {
  123. t.notOk(err, 'no errors')
  124. t.deepEqual(list, [a, b])
  125. }))
  126. input.write(JSON.stringify(a))
  127. input.write('~')
  128. input.end(JSON.stringify(b))
  129. })
  130. test('support custom matcher and options', function (t) {
  131. t.plan(6)
  132. var input = split('~', { highWaterMark: 1024 })
  133. t.equal(input.matcher, '~')
  134. t.equal(typeof input.mapper, 'function')
  135. t.equal(input._readableState.highWaterMark, 1024)
  136. t.equal(input._writableState.highWaterMark, 1024)
  137. input.pipe(strcb(function (err, list) {
  138. t.error(err)
  139. t.deepEqual(list, ['hello', 'world'])
  140. }))
  141. input.end('hello~world')
  142. })
  143. test('support mapper and options', function (t) {
  144. t.plan(6)
  145. var a = { a: '42' }
  146. var b = { b: '24' }
  147. var input = split(JSON.parse, { highWaterMark: 1024 })
  148. t.ok(input.matcher instanceof RegExp, 'matcher is RegExp')
  149. t.equal(typeof input.mapper, 'function')
  150. t.equal(input._readableState.highWaterMark, 1024)
  151. t.equal(input._writableState.highWaterMark, 1024)
  152. input.pipe(objcb(function (err, list) {
  153. t.error(err)
  154. t.deepEqual(list, [a, b])
  155. }))
  156. input.write(JSON.stringify(a))
  157. input.write('\n')
  158. input.end(JSON.stringify(b))
  159. })
  160. test('split utf8 chars', function (t) {
  161. t.plan(2)
  162. var input = split()
  163. input.pipe(strcb(function (err, list) {
  164. t.error(err)
  165. t.deepEqual(list, ['烫烫烫', '锟斤拷'])
  166. }))
  167. var buf = Buffer.from('烫烫烫\r\n锟斤拷', 'utf8')
  168. for (var i = 0; i < buf.length; ++i) {
  169. input.write(buf.slice(i, i + 1))
  170. }
  171. input.end()
  172. })
  173. test('split utf8 chars 2by2', function (t) {
  174. t.plan(2)
  175. var input = split()
  176. input.pipe(strcb(function (err, list) {
  177. t.error(err)
  178. t.deepEqual(list, ['烫烫烫', '烫烫烫'])
  179. }))
  180. var str = '烫烫烫\r\n烫烫烫'
  181. var buf = Buffer.from(str, 'utf8')
  182. for (var i = 0; i < buf.length; i += 2) {
  183. input.write(buf.slice(i, i + 2))
  184. }
  185. input.end()
  186. })
  187. test('split lines when the \n comes at the end of a chunk', function (t) {
  188. t.plan(2)
  189. var input = split()
  190. input.pipe(strcb(function (err, list) {
  191. t.error(err)
  192. t.deepEqual(list, ['hello', 'world'])
  193. }))
  194. input.write('hello\n')
  195. input.end('world')
  196. })
  197. test('truncated utf-8 char', function (t) {
  198. t.plan(2)
  199. var input = split()
  200. input.pipe(strcb(function (err, list) {
  201. t.error(err)
  202. t.deepEqual(list, ['烫' + Buffer.from('e7', 'hex').toString()])
  203. }))
  204. var str = '烫烫'
  205. var buf = Buffer.from(str, 'utf8')
  206. input.write(buf.slice(0, 3))
  207. input.end(buf.slice(3, 4))
  208. })
  209. test('maximum buffer limit', function (t) {
  210. t.plan(1)
  211. var input = split({ maxLength: 2 })
  212. input.pipe(strcb(function (err, list) {
  213. t.ok(err)
  214. }))
  215. input.write('hey')
  216. })
  217. test('readable highWaterMark', function (t) {
  218. var input = split()
  219. t.equal(input._readableState.highWaterMark, 16)
  220. t.end()
  221. })