forked from webaverse/preview-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.js
52 lines (36 loc) · 1.09 KB
/
encoder.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
'use strict';
/*
Encode functions adapted from:
Version 1.0 12/25/99 Copyright (C) 1999 Masanao Izumo <[email protected]>
http://www.onicos.com/staff/iz/amuse/javascript/expert/base64.txt
*/
const Stream = require('stream');
const internals = {};
exports.encode = function (buffer) {
return Buffer.from(buffer.toString('base64'));
};
exports.Encoder = class Encoder extends Stream.Transform {
constructor() {
super();
this._reminder = null;
}
_transform(chunk, encoding, callback) {
let part = this._reminder ? Buffer.concat([this._reminder, chunk]) : chunk;
const remaining = part.length % 3;
if (remaining) {
this._reminder = part.slice(part.length - remaining);
part = part.slice(0, part.length - remaining);
}
else {
this._reminder = null;
}
this.push(exports.encode(part));
return callback();
}
_flush(callback) {
if (this._reminder) {
this.push(exports.encode(this._reminder));
}
return callback();
}
};