test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. 'use strict'
  2. const test = require('tape')
  3. const split = require('./')
  4. const callback = require('callback-stream')
  5. const strcb = callback.bind(null, { decodeStrings: false })
  6. const objcb = callback.bind(null, { objectMode: true })
  7. test('split two lines on end', function (t) {
  8. t.plan(2)
  9. const input = split()
  10. input.pipe(strcb(function (err, list) {
  11. t.error(err)
  12. t.deepEqual(list, ['hello', 'world'])
  13. }))
  14. input.end('hello\nworld')
  15. })
  16. test('split two lines on two writes', function (t) {
  17. t.plan(2)
  18. const input = split()
  19. input.pipe(strcb(function (err, list) {
  20. t.error(err)
  21. t.deepEqual(list, ['hello', 'world'])
  22. }))
  23. input.write('hello')
  24. input.write('\nworld')
  25. input.end()
  26. })
  27. test('split four lines on three writes', function (t) {
  28. t.plan(2)
  29. const input = split()
  30. input.pipe(strcb(function (err, list) {
  31. t.error(err)
  32. t.deepEqual(list, ['hello', 'world', 'bye', 'world'])
  33. }))
  34. input.write('hello\nwor')
  35. input.write('ld\nbye\nwo')
  36. input.write('rld')
  37. input.end()
  38. })
  39. test('accumulate multiple writes', function (t) {
  40. t.plan(2)
  41. const input = split()
  42. input.pipe(strcb(function (err, list) {
  43. t.error(err)
  44. t.deepEqual(list, ['helloworld'])
  45. }))
  46. input.write('hello')
  47. input.write('world')
  48. input.end()
  49. })
  50. test('split using a custom string matcher', function (t) {
  51. t.plan(2)
  52. const input = split('~')
  53. input.pipe(strcb(function (err, list) {
  54. t.error(err)
  55. t.deepEqual(list, ['hello', 'world'])
  56. }))
  57. input.end('hello~world')
  58. })
  59. test('split using a custom regexp matcher', function (t) {
  60. t.plan(2)
  61. const input = split(/~/)
  62. input.pipe(strcb(function (err, list) {
  63. t.error(err)
  64. t.deepEqual(list, ['hello', 'world'])
  65. }))
  66. input.end('hello~world')
  67. })
  68. test('support an option argument', function (t) {
  69. t.plan(2)
  70. const input = split({ highWaterMark: 2 })
  71. input.pipe(strcb(function (err, list) {
  72. t.error(err)
  73. t.deepEqual(list, ['hello', 'world'])
  74. }))
  75. input.end('hello\nworld')
  76. })
  77. test('support a mapper function', function (t) {
  78. t.plan(2)
  79. const a = { a: '42' }
  80. const b = { b: '24' }
  81. const input = split(JSON.parse)
  82. input.pipe(objcb(function (err, list) {
  83. t.error(err)
  84. t.deepEqual(list, [a, b])
  85. }))
  86. input.write(JSON.stringify(a))
  87. input.write('\n')
  88. input.end(JSON.stringify(b))
  89. })
  90. test('split lines windows-style', function (t) {
  91. t.plan(2)
  92. const input = split()
  93. input.pipe(strcb(function (err, list) {
  94. t.error(err)
  95. t.deepEqual(list, ['hello', 'world'])
  96. }))
  97. input.end('hello\r\nworld')
  98. })
  99. test('splits a buffer', function (t) {
  100. t.plan(2)
  101. const input = split()
  102. input.pipe(strcb(function (err, list) {
  103. t.error(err)
  104. t.deepEqual(list, ['hello', 'world'])
  105. }))
  106. input.end(Buffer.from('hello\nworld'))
  107. })
  108. test('do not end on undefined', function (t) {
  109. t.plan(2)
  110. const input = split(function (line) { })
  111. input.pipe(strcb(function (err, list) {
  112. t.error(err)
  113. t.deepEqual(list, [])
  114. }))
  115. input.end(Buffer.from('hello\nworld'))
  116. })
  117. test('has destroy method', function (t) {
  118. t.plan(1)
  119. const input = split(function (line) { })
  120. input.on('close', function () {
  121. t.ok(true, 'close emitted')
  122. t.end()
  123. })
  124. input.destroy()
  125. })
  126. test('support custom matcher and mapper', function (t) {
  127. t.plan(4)
  128. const a = { a: '42' }
  129. const b = { b: '24' }
  130. const input = split('~', JSON.parse)
  131. t.equal(input.matcher, '~')
  132. t.equal(typeof input.mapper, 'function')
  133. input.pipe(objcb(function (err, list) {
  134. t.notOk(err, 'no errors')
  135. t.deepEqual(list, [a, b])
  136. }))
  137. input.write(JSON.stringify(a))
  138. input.write('~')
  139. input.end(JSON.stringify(b))
  140. })
  141. test('support custom matcher and options', function (t) {
  142. t.plan(6)
  143. const input = split('~', { highWaterMark: 1024 })
  144. t.equal(input.matcher, '~')
  145. t.equal(typeof input.mapper, 'function')
  146. t.equal(input._readableState.highWaterMark, 1024)
  147. t.equal(input._writableState.highWaterMark, 1024)
  148. input.pipe(strcb(function (err, list) {
  149. t.error(err)
  150. t.deepEqual(list, ['hello', 'world'])
  151. }))
  152. input.end('hello~world')
  153. })
  154. test('support mapper and options', function (t) {
  155. t.plan(6)
  156. const a = { a: '42' }
  157. const b = { b: '24' }
  158. const input = split(JSON.parse, { highWaterMark: 1024 })
  159. t.ok(input.matcher instanceof RegExp, 'matcher is RegExp')
  160. t.equal(typeof input.mapper, 'function')
  161. t.equal(input._readableState.highWaterMark, 1024)
  162. t.equal(input._writableState.highWaterMark, 1024)
  163. input.pipe(objcb(function (err, list) {
  164. t.error(err)
  165. t.deepEqual(list, [a, b])
  166. }))
  167. input.write(JSON.stringify(a))
  168. input.write('\n')
  169. input.end(JSON.stringify(b))
  170. })
  171. test('split utf8 chars', function (t) {
  172. t.plan(2)
  173. const input = split()
  174. input.pipe(strcb(function (err, list) {
  175. t.error(err)
  176. t.deepEqual(list, ['烫烫烫', '锟斤拷'])
  177. }))
  178. const buf = Buffer.from('烫烫烫\r\n锟斤拷', 'utf8')
  179. for (let i = 0; i < buf.length; ++i) {
  180. input.write(buf.slice(i, i + 1))
  181. }
  182. input.end()
  183. })
  184. test('split utf8 chars 2by2', function (t) {
  185. t.plan(2)
  186. const input = split()
  187. input.pipe(strcb(function (err, list) {
  188. t.error(err)
  189. t.deepEqual(list, ['烫烫烫', '烫烫烫'])
  190. }))
  191. const str = '烫烫烫\r\n烫烫烫'
  192. const buf = Buffer.from(str, 'utf8')
  193. for (let i = 0; i < buf.length; i += 2) {
  194. input.write(buf.slice(i, i + 2))
  195. }
  196. input.end()
  197. })
  198. test('split lines when the \n comes at the end of a chunk', function (t) {
  199. t.plan(2)
  200. const input = split()
  201. input.pipe(strcb(function (err, list) {
  202. t.error(err)
  203. t.deepEqual(list, ['hello', 'world'])
  204. }))
  205. input.write('hello\n')
  206. input.end('world')
  207. })
  208. test('truncated utf-8 char', function (t) {
  209. t.plan(2)
  210. const input = split()
  211. input.pipe(strcb(function (err, list) {
  212. t.error(err)
  213. t.deepEqual(list, ['烫' + Buffer.from('e7', 'hex').toString()])
  214. }))
  215. const str = '烫烫'
  216. const buf = Buffer.from(str, 'utf8')
  217. input.write(buf.slice(0, 3))
  218. input.end(buf.slice(3, 4))
  219. })
  220. test('maximum buffer limit', function (t) {
  221. t.plan(1)
  222. const input = split({ maxLength: 2 })
  223. input.on('error', function (err) {
  224. t.ok(err)
  225. })
  226. input.resume()
  227. input.write('hey')
  228. })
  229. test('readable highWaterMark', function (t) {
  230. const input = split()
  231. t.equal(input._readableState.highWaterMark, 16)
  232. t.end()
  233. })
  234. test('maxLength < chunk size', function (t) {
  235. t.plan(2)
  236. const input = split({ maxLength: 2 })
  237. input.pipe(strcb(function (err, list) {
  238. t.error(err)
  239. t.deepEqual(list, ['a', 'b'])
  240. }))
  241. input.end('a\nb')
  242. })
  243. test('maximum buffer limit w/skip', function (t) {
  244. t.plan(2)
  245. const input = split({ maxLength: 2, skipOverflow: true })
  246. input.pipe(strcb(function (err, list) {
  247. t.error(err)
  248. t.deepEqual(list, ['a', 'b', 'c'])
  249. }))
  250. input.write('a\n123')
  251. input.write('456')
  252. input.write('789\nb\nc')
  253. input.end()
  254. })
  255. test("don't modify the options object", function (t) {
  256. t.plan(2)
  257. const options = {}
  258. const input = split(options)
  259. input.pipe(strcb(function (err, list) {
  260. t.error(err)
  261. t.same(options, {})
  262. }))
  263. input.end()
  264. })
  265. test('mapper throws flush', function (t) {
  266. t.plan(1)
  267. const error = new Error()
  268. const input = split(function () {
  269. throw error
  270. })
  271. input.on('error', (err, list) => {
  272. t.same(err, error)
  273. })
  274. input.end('hello')
  275. })
  276. test('mapper throws on transform', function (t) {
  277. t.plan(1)
  278. const error = new Error()
  279. const input = split(function (l) {
  280. throw error
  281. })
  282. input.on('error', (err) => {
  283. t.same(err, error)
  284. })
  285. input.write('a')
  286. input.write('\n')
  287. input.end('b')
  288. })