/
index.js
177 lines (157 loc) · 6.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const CsvParser = require('./lib/CsvParser')
const CSVSniffer = require('csv-sniffer')
// const Stream = require('readable-stream')
const { Readable } = require('readable-stream')
// quote.charAt(0); quote.slice(-1); quote.charAt(quote.length - 1)
//* Parser of the input stream (JS string chunks that have UTF16 encoding in ES6)
class Parser { // ValidatingParser
constructor ({
relaxColumnCount, skipLinesWithError,
delimiter, quotes, // Inferred if not specified
newLine // Optionally used only when quotes or delimiter are inferred
} = {}
) {
this.relaxColumnCount = relaxColumnCount
this.skipLinesWithError = skipLinesWithError
this.delimiter = delimiter
this.quotes = quotes
this.newLine = newLine
// this.logger = logger
}
import (input, {
relaxColumnCount = this.relaxColumnCount,
skipLinesWithError = this.skipLinesWithError
} = {}) {
// input.setEncoding('utf8')
// console.log(`Parser.import(), input encoding: ${input.readableEncoding}`)
const output = new CsvParser({
delimiter: this.delimiter, // ? this.delimiter : ',', // parsedMetadata.delimiter, // this.delimiter = ','
// ATTENTION: the spread operator ([...symb]) is required in case of a unicode quote that is represented with more than a single code point
// TODO: consider distinct opening and closing unicode quotes
quoteChar: this.quotes, // ? [...this.quotes][0] : '"', // parsedMetadata.quoteChar, // this.quoteChar = '"'
newlineStr: this.newLine,
relaxColumnCount,
skipLinesWithError
})
if (!input.readable && input.readableEnded) {
output.emit('end')
return output
}
// Process the remained input data
input.on('error', (err) => {
output.emit('error', err)
console.warn('input.error()')
})
// .on('end', () => {
// if (!output.readable) {
// output.emit('end')
// }
// })
// Identify delimiter and quotes, given the CSV stream
if (!this.delimiter || !this.quotes) {
let inpPart = '' // Partial input string
// const inpDataStream = new Stream.Readable({ read () {} })
//* Processing of the partial input string
const inpPartProc = () => {
// Sniff the fetch data
// const csvDelimiters = [',', ';', '\t']
const sniffer = new (CSVSniffer())() // (CsvSniffer())(csvDelimiters)
// TODO: consider unicode delimiter and quotes
const csvMeta = sniffer.sniff(inpPart,
{
delimiter: (this.delimiter || '')[0],
quoteChar: (this.quotes || '')[0],
newlineStr: this.newLine
})
const updOpts = {}
if (!this.delimiter && csvMeta.delimiter) {
console.info(`inpPartProc()> Inferred delimiter: ${csvMeta.delimiter}`)
updOpts.delimiter = this.delimiter = csvMeta.delimiter
// console.info(`inpPartProc()> after sniffing: delimiter: ${output.parser.options.delimiter}, quote: ${output.parser.options.quote}`)
// output.setDelimiter(this.delimiter) // || ','
}
if (!this.quotes && csvMeta.quoteChar) {
console.info(`inpPartProc()> Inferred quoteChar: ${csvMeta.quoteChar}`)
updOpts.quoteChar = this.quotes = csvMeta.quoteChar // || ''
// output.parser.setQuote([...this.quotes][0]) // || '"'
}
if (!this.newLine && csvMeta.newlineStr) {
let cpoints = ''
for (const cp of csvMeta.newlineStr) { cpoints += ' 0x' + cp.codePointAt(0).toString(16).toUpperCase() }
console.info(`inpPartProc()> Inferred newLine (codepoints): ${cpoints}`)
updOpts.newlineStr = this.newLine = csvMeta.newlineStr // || ''
}
if (Object.keys(updOpts).length !== 0) { output.updateOptions(updOpts) }
// Process the sniffed data
// console.info(`inpPartProc() after sniffing: delimiter: ${output.parser.options.delimiter}, quote: ${output.parser.options.quote}`)
// ATTENTION: Readable.from is not available in the browser
// const rdinp = Readable.from([inpPart])
const rdinp = new Readable()
rdinp.push(inpPart)
rdinp.push(null)
rdinp.on('error', (err) => {
output.emit('error', err)
}).on('end', () => {
console.debug(`rdinp.end()> input is {readable: ${input.readable}, ended: ${input.readableEnded}}`)
if (!input.readable && input.readableEnded) {
output.emit('end')
return
}
input.on('end', () => {
console.debug('input.end()')
// if (!output.readable) {
// output.emit('end')
// }
})
// .on('finish', () => {
// console.debug('input.finish()')
// // if (!output.readable) {
// // output.emit('end')
// // }
// })
input.pipe(output)
}).pipe(output, { end: false })
// console.debug(`inpPartProc() outputting, nlf: ${nlf}, ncr: ${ncr}, inpPart [${inpPart.length}]`)
// input.pipe(output)
}
const linesMin = 10 // Min number of lines to read for the automatic inference
const lengthMax = 512 * 1024 // Soft max number of codepoints to be read for sniffing
const clf = '\n'.codePointAt(0)
const ccr = '\r'.codePointAt(0)
let nlf = 0 // Linefeed counter
let ncr = 0 // Carriage return counter
const onReadable = () => {
let chunk
// Use a loop to make sure we read all currently available data
while ((chunk = input.read()) !== null) {
// Count the number of newline symbols to identify the number of lines to process
for (const c of chunk) {
if (c === clf) { // codePointAt(0)
++nlf
} else if (c === ccr) {
++ncr
} // else console.log(c)
}
// inpDataStream.push(chunk)
inpPart += chunk
// console.debug(`onReadable(), nlf: ${nlf}, ncr: ${ncr}, inpPart [${inpPart.length}]`)
if (Math.max(nlf, ncr) >= linesMin || inpPart.length >= lengthMax) {
input.removeListener('readable', onReadable)
inpPartProc()
return
}
}
if (chunk === null && inpPart) {
input.removeListener('readable', onReadable)
inpPartProc()
}
}
input.on('readable', onReadable)
} else input.pipe(output)
return output
}
static import (input, options) {
return (new Parser(options)).import(input)
}
}
module.exports = Parser