source: trip-planner-front/node_modules/minipass-collect/index.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1const Minipass = require('minipass')
2const _data = Symbol('_data')
3const _length = Symbol('_length')
4class Collect extends Minipass {
5 constructor (options) {
6 super(options)
7 this[_data] = []
8 this[_length] = 0
9 }
10 write (chunk, encoding, cb) {
11 if (typeof encoding === 'function')
12 cb = encoding, encoding = 'utf8'
13
14 if (!encoding)
15 encoding = 'utf8'
16
17 const c = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding)
18 this[_data].push(c)
19 this[_length] += c.length
20 if (cb)
21 cb()
22 return true
23 }
24 end (chunk, encoding, cb) {
25 if (typeof chunk === 'function')
26 cb = chunk, chunk = null
27 if (typeof encoding === 'function')
28 cb = encoding, encoding = 'utf8'
29 if (chunk)
30 this.write(chunk, encoding)
31 const result = Buffer.concat(this[_data], this[_length])
32 super.write(result)
33 return super.end(cb)
34 }
35}
36module.exports = Collect
37
38// it would be possible to DRY this a bit by doing something like
39// this.collector = new Collect() and listening on its data event,
40// but it's not much code, and we may as well save the extra obj
41class CollectPassThrough extends Minipass {
42 constructor (options) {
43 super(options)
44 this[_data] = []
45 this[_length] = 0
46 }
47 write (chunk, encoding, cb) {
48 if (typeof encoding === 'function')
49 cb = encoding, encoding = 'utf8'
50
51 if (!encoding)
52 encoding = 'utf8'
53
54 const c = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding)
55 this[_data].push(c)
56 this[_length] += c.length
57 return super.write(chunk, encoding, cb)
58 }
59 end (chunk, encoding, cb) {
60 if (typeof chunk === 'function')
61 cb = chunk, chunk = null
62 if (typeof encoding === 'function')
63 cb = encoding, encoding = 'utf8'
64 if (chunk)
65 this.write(chunk, encoding)
66 const result = Buffer.concat(this[_data], this[_length])
67 this.emit('collect', result)
68 return super.end(cb)
69 }
70}
71module.exports.PassThrough = CollectPassThrough
Note: See TracBrowser for help on using the repository browser.