source: node_modules/split2/index.js@ 33517cc

finki-main main
Last change on this file since 33517cc was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2Copyright (c) 2014-2021, Matteo Collina <hello@matteocollina.com>
3
4Permission to use, copy, modify, and/or distribute this software for any
5purpose with or without fee is hereby granted, provided that the above
6copyright notice and this permission notice appear in all copies.
7
8THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
14IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17'use strict'
18
19const { Transform } = require('stream')
20const { StringDecoder } = require('string_decoder')
21const kLast = Symbol('last')
22const kDecoder = Symbol('decoder')
23
24function transform (chunk, enc, cb) {
25 let list
26 if (this.overflow) { // Line buffer is full. Skip to start of next line.
27 const buf = this[kDecoder].write(chunk)
28 list = buf.split(this.matcher)
29
30 if (list.length === 1) return cb() // Line ending not found. Discard entire chunk.
31
32 // Line ending found. Discard trailing fragment of previous line and reset overflow state.
33 list.shift()
34 this.overflow = false
35 } else {
36 this[kLast] += this[kDecoder].write(chunk)
37 list = this[kLast].split(this.matcher)
38 }
39
40 this[kLast] = list.pop()
41
42 for (let i = 0; i < list.length; i++) {
43 try {
44 push(this, this.mapper(list[i]))
45 } catch (error) {
46 return cb(error)
47 }
48 }
49
50 this.overflow = this[kLast].length > this.maxLength
51 if (this.overflow && !this.skipOverflow) {
52 cb(new Error('maximum buffer reached'))
53 return
54 }
55
56 cb()
57}
58
59function flush (cb) {
60 // forward any gibberish left in there
61 this[kLast] += this[kDecoder].end()
62
63 if (this[kLast]) {
64 try {
65 push(this, this.mapper(this[kLast]))
66 } catch (error) {
67 return cb(error)
68 }
69 }
70
71 cb()
72}
73
74function push (self, val) {
75 if (val !== undefined) {
76 self.push(val)
77 }
78}
79
80function noop (incoming) {
81 return incoming
82}
83
84function split (matcher, mapper, options) {
85 // Set defaults for any arguments not supplied.
86 matcher = matcher || /\r?\n/
87 mapper = mapper || noop
88 options = options || {}
89
90 // Test arguments explicitly.
91 switch (arguments.length) {
92 case 1:
93 // If mapper is only argument.
94 if (typeof matcher === 'function') {
95 mapper = matcher
96 matcher = /\r?\n/
97 // If options is only argument.
98 } else if (typeof matcher === 'object' && !(matcher instanceof RegExp) && !matcher[Symbol.split]) {
99 options = matcher
100 matcher = /\r?\n/
101 }
102 break
103
104 case 2:
105 // If mapper and options are arguments.
106 if (typeof matcher === 'function') {
107 options = mapper
108 mapper = matcher
109 matcher = /\r?\n/
110 // If matcher and options are arguments.
111 } else if (typeof mapper === 'object') {
112 options = mapper
113 mapper = noop
114 }
115 }
116
117 options = Object.assign({}, options)
118 options.autoDestroy = true
119 options.transform = transform
120 options.flush = flush
121 options.readableObjectMode = true
122
123 const stream = new Transform(options)
124
125 stream[kLast] = ''
126 stream[kDecoder] = new StringDecoder('utf8')
127 stream.matcher = matcher
128 stream.mapper = mapper
129 stream.maxLength = options.maxLength
130 stream.skipOverflow = options.skipOverflow || false
131 stream.overflow = false
132 stream._destroy = function (err, cb) {
133 // Weird Node v12 bug that we need to work around
134 this._writableState.errorEmitted = false
135 cb(err)
136 }
137
138 return stream
139}
140
141module.exports = split
Note: See TracBrowser for help on using the repository browser.