-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·154 lines (136 loc) · 4.41 KB
/
index.js
File metadata and controls
executable file
·154 lines (136 loc) · 4.41 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
#!/usr/bin/env node
"use strict";
function error (message, exitCode = 255) {
process.stderr.write(message + "\n");
process.exit(exitCode);
}
function readAll (readStream) {
return new Promise((resolve, reject) => {
let onError, onData, onEnd;
const finalize = () => {
readStream.removeListener("data", onData);
readStream.removeListener("end", onEnd);
readStream.removeListener("error", onError);
};
let chunks = [];
readStream.on("data", onData = (chunk) => chunks.push(chunk));
readStream.on("error", onError = (err) => {
finalize();
reject(err);
});
readStream.on("end", onEnd = () => {
finalize();
resolve(Buffer.concat(chunks));
});
readStream.resume();
});
}
function writeAll (writeStream, data) {
return new Promise((resolve, reject) => {
let onError, onDrain;
const finalize = () => {
writeStream.removeListener("error", onError);
writeStream.removeListener("drain", onDrain);
};
writeStream.on("error", onError = (err) => {
finalize();
reject(err);
});
writeStream.on("drain", onDrain = () => {
finalize();
resolve();
});
if (!writeStream.write(data, null)) {
finalize();
process.nextTick(resolve);
}
});
}
const STRING_CHARSET = "utf-8";
const argv = process.argv;
let readStream = () => process.stdin,
writeStream = () => process.stdout,
decode = false;
for (let i = 2, len = argv.length; i < len; i++) {
let param = argv[i];
switch (param) {
case "--help":
case "-h":
case "-?": {
process.stdout.write(`Usage: ${argv[1]} [-hD] [-i in_file] [-o out_file]
-h, --help display this message
-D, --decode decodes input
-i, --input input file (default: "-" for stdin)
-o, --output output file (default: "-" for stdout)
`);
process.exit(0);
break;
}
case "--decode":
case "-D": {
decode = true;
break;
}
case "--input":
case "-i": {
const val = argv[++i];
if (!(val && typeof val === "string")) error(`Value of "${param}" must be non-empty string`);
if (val === "-") {
readStream = () => process.stdin; // jshint ignore:line
} else {
readStream = () => require("fs").createReadStream(val, { autoClose: true, flags: "r", encoding: null }); // jshint ignore:line
}
break;
}
case "--output":
case "-o": {
const val = argv[++i];
if (!(val && typeof val === "string")) error(`Value of "${param}" must be non-empty string`);
if (val === "-") {
writeStream = () => process.stdout; // jshint ignore:line
} else {
writeStream = () => require("fs").createWriteStream(val, { autoClose: true, flags: "w", encoding: null, mode: 0o644 }); // jshint ignore:line
}
break;
}
default: {
error(`Invalid argument "${param}"`);
}
}
}
readAll(readStream()).then(
(buffer) => {
const data = buffer.toString(STRING_CHARSET);
if (decode) {
return require("punycode").toUnicode(data);
} else {
return require("punycode").toASCII(data);
}
}
).then(
(processed) => {
const ws = writeStream();
return writeAll(ws, Buffer.from(processed, STRING_CHARSET)).then(
() => {
if (ws !== process.stdout) return new Promise((resolve, reject) => {
let onFinish, onError;
const finalize = () => {
ws.removeListener("error", onError);
ws.removeListener("finish", onFinish);
};
ws.on("error", onError = (err) => {
finalize();
reject(err);
});
ws.on("finish", onFinish = () => {
finalize();
resolve();
});
ws.end();
});
}
);
}
).catch(
(err) => error(err.stack || err.message, 1)
);