forked from dodo/node-unicodetable
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.js
executable file
·203 lines (164 loc) · 5.1 KB
/
install.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env node
//
var fs = require('fs')
var path = require('path')
var https = require('https')
var readline = require('readline')
// http://www.ksu.ru/eng/departments/ktk/test/perl/lib/unicode/UCDFF301.html
var keys = [
'value',
'name',
'category',
'class',
'bidirectional_category',
'mapping',
'decimal_digit_value',
'digit_value',
'numeric_value',
'mirrored',
'unicode_name',
'comment',
'uppercase_mapping',
'lowercase_mapping',
'titlecase_mapping'
]
var systemfiles = [
'/usr/share/unicode/UnicodeData.txt', // debian
'/usr/share/unicode-data/UnicodeData.txt', // gentoo
'/usr/share/unicode/ucd/UnicodeData.txt', // redhat, unicode-ucd package
process.env.NODE_UNICODETABLE_UNICODEDATA_TXT || 'UnicodeData.txt' // manually downloaded
]
var unicodedatafile = {
scheme: 'https',
host: 'unicode.org',
path: '/Public/UNIDATA/UnicodeData.txt',
method: 'GET',
port: 443
}
var proxyServer = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
// based on https://github.com/mathiasbynens/jsesc
function escape (charValue) {
var hexadecimal = charValue.replace(/^0*/, '') // is already in hexadecimal
var longhand = hexadecimal.length > 2
return '\\' + (longhand ? 'u' : 'x') + ('0000' + hexadecimal).slice(longhand ? -4 : -2)
}
function stringify (key, value) {
return '"' + key + '"' + ':' + JSON.stringify(value)
}
function readFile (successCb, errorCb) {
var systemfile
var sysfiles = systemfiles.slice()
var tryReading = function (success, error) {
systemfile = sysfiles.shift()
if (!systemfile) {
return errorCb()
}
console.info('try to read file %s…', systemfile)
fs.access(systemfile, (fs.constants || fs).R_OK, function (err) {
if (err) {
if (err.code === 'ENOENT') {
console.error('%s not found.', systemfile)
return tryReading(successCb, errorCb)
}
throw err
}
console.info('parsing…')
var data = {}
var rl = readline.createInterface({
input: fs.createReadStream(systemfile, {
encoding: 'utf8'
})
})
rl.on('line', function (line) {
var char = {}
var values = line.toString().split(';')
for (var i = 0, length = keys.length; i < length; i++) {
char[keys[i]] = values[i]
}
char.symbol = escape(char.value)
var v = parseInt(char.value, 16)
var c = char.category
if (!data[c]) {
data[c] = fs.createWriteStream(path.join(__dirname, 'category', c + '.json'), {
encoding: 'utf8'
})
data[c].on('drain', function () {
rl.resume()
})
console.log('saving data as %s.js…', c)
if (data[c].write('{' + stringify(v, char))) {
rl.resume()
}
rl.pause()
} else if (!data[c].write(',' + stringify(v, char))) {
rl.pause()
}
})
rl.on('close', function () {
var files = Object.keys(data).length
for (var key in data) {
data[key].end('}')
data[key].once('finish', function () {
if (--files === 0) {
successCb()
}
})
}
})
})
}
tryReading(successCb, errorCb)
}
function downloadFile (callback) {
var timeouthandle = null
console.info('%s %s://%s:%d%s', unicodedatafile.method, unicodedatafile.scheme, unicodedatafile.host, unicodedatafile.port, unicodedatafile.path)
if (proxyServer) {
var proxyVars = proxyServer.match(/^([^:/]*:[/]{2})?([^:/]+)(:([0-9]+))?/i)
console.info('Proxy server detected, using proxy settings to download (%s)', proxyServer)
unicodedatafile.path = unicodedatafile.scheme + '://' + unicodedatafile.host + ':' + unicodedatafile.port + unicodedatafile.path
unicodedatafile.headers = {
Host: unicodedatafile.host
}
unicodedatafile.host = proxyVars[2]
unicodedatafile.port = proxyVars[4]
}
var dst = 'UnicodeData.txt'
https.get(unicodedatafile, function (res) {
console.log('fetching…')
// stop timeout couting
if (timeouthandle) {
clearTimeout(timeouthandle)
}
var file = fs.createWriteStream(dst)
res.setEncoding('utf8')
res.pipe(file)
file.on('finish', function () {
file.close(function () {
readFile(callback, callback)
})
})
}).on('error', function (err) {
console.error('Error while downloading %s: %s', path.basename(unicodedatafile.path), err)
console.log('Please download file manually, put it next to the install.js file and run `node install.js` again.')
fs.unlink(dst)
callback(1) // eslint-disable-line
})
timeouthandle = setTimeout(function () {
console.error('request timed out.')
callback(1) // eslint-disable-line
}, 30 * 1000)
}
// run
if (!module.parent) { // not required
readFile(process.exit, function () {
console.log('try to download…')
downloadFile(process.exit)
})
} else {
module.exports = {
escape: escape,
stringify: stringify,
read_file: readFile,
download_file: downloadFile
}
}