Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81169 views
1
"use strict"
2
3
// Export Node.js internal encodings.
4
5
module.exports = {
6
// Encodings
7
utf8: { type: "_internal", bomAware: true},
8
cesu8: "utf8",
9
unicode11utf8: "utf8",
10
11
ucs2: { type: "_internal", bomAware: true},
12
utf16le: "ucs2",
13
14
binary: { type: "_internal" },
15
base64: { type: "_internal" },
16
hex: { type: "_internal" },
17
18
// Codec.
19
_internal: InternalCodec,
20
};
21
22
//------------------------------------------------------------------------------
23
24
function InternalCodec(codecOptions) {
25
this.enc = codecOptions.encodingName;
26
this.bomAware = codecOptions.bomAware;
27
28
if (this.enc === "base64")
29
this.encoder = InternalEncoderBase64;
30
}
31
32
InternalCodec.prototype.encoder = InternalEncoder;
33
InternalCodec.prototype.decoder = InternalDecoder;
34
35
//------------------------------------------------------------------------------
36
37
// We use node.js internal decoder. It's signature is the same as ours.
38
var StringDecoder = require('string_decoder').StringDecoder;
39
40
if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
41
StringDecoder.prototype.end = function() {};
42
43
44
function InternalDecoder(options, codec) {
45
StringDecoder.call(this, codec.enc);
46
}
47
48
InternalDecoder.prototype = StringDecoder.prototype;
49
50
51
//------------------------------------------------------------------------------
52
// Encoder is mostly trivial
53
54
function InternalEncoder(options, codec) {
55
this.enc = codec.enc;
56
}
57
58
InternalEncoder.prototype.write = function(str) {
59
return new Buffer(str, this.enc);
60
}
61
62
InternalEncoder.prototype.end = function() {
63
}
64
65
66
//------------------------------------------------------------------------------
67
// Except base64 encoder, which must keep its state.
68
69
function InternalEncoderBase64(options, codec) {
70
this.prevStr = '';
71
}
72
73
InternalEncoderBase64.prototype.write = function(str) {
74
str = this.prevStr + str;
75
var completeQuads = str.length - (str.length % 4);
76
this.prevStr = str.slice(completeQuads);
77
str = str.slice(0, completeQuads);
78
79
return new Buffer(str, "base64");
80
}
81
82
InternalEncoderBase64.prototype.end = function() {
83
return new Buffer(this.prevStr, "base64");
84
}
85
86
87