-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasynclinesreader.js
More file actions
173 lines (137 loc) · 3.65 KB
/
asynclinesreader.js
File metadata and controls
173 lines (137 loc) · 3.65 KB
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
'use strict'
/*
Command line:
node --no-warnings asynclinesreader.js input.txt output.txt
*/
const fs = require('fs')
function pb (edge = 0) {
const rl = require('readline')
const DEFAULT_FREQ = 500
const HUNDRED_PERCENT = 100
const PB_LENGTH = 50
const PB_SCALE = HUNDRED_PERCENT / PB_LENGTH
const NANOSECONDS_PER_SECOND = 1e9
const hrStart = process.hrtime()
function clearLine () {
rl.cursorTo(process.stdout, 0)
rl.clearLine(process.stdout, 0)
}
function getTimePast () {
const hrEnd = process.hrtime(hrStart)
return `${(
(hrEnd[0] * NANOSECONDS_PER_SECOND + hrEnd[1]) /
NANOSECONDS_PER_SECOND
).toFixed(1)} s`
}
return {
edge,
stat: 0,
start (freq = DEFAULT_FREQ) {
this.updater = setInterval(() => {
this.update()
}, freq)
},
update (stat = this.stat) {
const statPercent =
stat === this.edge || stat > this.edge
? HUNDRED_PERCENT
: stat / this.edge * HUNDRED_PERCENT
const barsNumber = Math.floor(statPercent / PB_SCALE)
const padsNumber = PB_LENGTH - barsNumber
clearLine()
process.stdout.write(
`${'█'.repeat(barsNumber)}${'░'.repeat(
padsNumber
)} ${statPercent.toFixed(
1
)}% ${getTimePast()} (${stat.toLocaleString()} of ${this.edge.toLocaleString()})`
)
},
end () {
clearInterval(this.updater)
this.stat = this.edge
this.update()
console.log('\n')
},
clear () {
clearInterval(this.updater)
clearLine()
}
}
}
class LinesReader {
constructor (inputFilePath, options) {
options = options || {}
if (!options.highWaterMark) options.highWaterMark = 64 * 1024
if (!options.encoding || options.encoding !== 'utf16le') {
options.encoding = 'utf8'
}
this.options = options
this.chunksAsync = fs.createReadStream(inputFilePath, {
encoding: this.options.encoding,
highWaterMark: this.options.highWaterMark
})
}
async * chunksToLines () {
let previous = ''
for await (const chunk of this.chunksAsync) {
const lines = (previous === '' ? chunk : `${previous}${chunk}`).split(
/(?<=\r?\n|\r(?!\n))/u
)
previous = lines[lines.length - 1].endsWith('\n') ? '' : lines.pop()
yield lines
}
if (previous !== '') {
yield [previous]
}
}
}
function guessEncoding (path) {
const BOM_0 = 0xff
const BOM_1 = 0xfe
try {
const fd = fs.openSync(path, 'r')
const bf = Buffer.alloc(2)
fs.readSync(fd, bf, 0, 2, 0)
fs.closeSync(fd)
return bf[0] === BOM_0 && bf[1] === BOM_1 ? 'utf16le' : 'utf8'
} catch (e) {
console.error(`Error: ${e.message}.`)
return null
}
}
function fileExists (filePath) {
try {
return fs.statSync(filePath).isFile()
} catch (err) {
return false
}
}
function processLine (line) {
return line
}
async function main () {
try {
const input_encoding = guessEncoding(process.argv[2])
const reader = new LinesReader(process.argv[2], {
encoding: input_encoding
})
const output = fs.openSync(process.argv[3], 'w')
const pbAsync = pb(fs.statSync(process.argv[2])['size'])
pbAsync.start(1000)
for await (let lines of reader.chunksToLines()) {
const chunkToWrite = lines.map(processLine).join('')
fs.writeSync(output, chunkToWrite, null, input_encoding)
pbAsync.stat += Buffer.byteLength(chunkToWrite, input_encoding)
}
pbAsync.end()
} catch (err) {
console.log(err)
}
}
if (process.argv.length === 4 && fileExists(process.argv[2])) {
main()
} else {
console.log('Invalid command line.')
process.exit()
}