-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-lol-node.mjs
66 lines (58 loc) · 1.55 KB
/
base-lol-node.mjs
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
import stream from 'stream';
import util from 'util';
import fs from 'fs';
import { encode, decode } from './base-lol';
const pipeline = util.promisify(stream.pipeline);
const binaryToEmoji = new stream.Transform({
transform(chunk, encoding, callback) {
const asString = [...chunk
.values()]
.map(encode)
.join('');
callback(null, asString);
}
});
const emojiToBinary = new stream.Transform({
transform(chunk, encoding, callback) {
const init = () => {
this.emoji = {
buffer: Buffer.alloc(4),
index: 0,
};
};
if(!this.emoji) {
init();
}
// each emoji is 4 bytes
for(let i = 0; i < chunk.length; i++) {
this.emoji.buffer[this.emoji.index] = chunk[i];
this.emoji.index += 1;
if(this.emoji.index === 4) {
this.push(
Buffer.from([
decode(
this.emoji.buffer.toString('utf8').codePointAt(0)
)
])
);
init();
}
}
callback();
}
});
const main = () => {
const stream = process.argv.some(f => f === '--decode')
? emojiToBinary
: binaryToEmoji;
pipeline(
process.stdin,
stream,
fs.createWriteStream('/dev/stdout')
)
.catch(e => {
console.error(e.stack);
process.exit(1);
})
};
main();