2 // Export Node.js internal encodings.
4 var utf16lebom = new Buffer([0xFF, 0xFE]);
8 utf8: { type: "_internal", enc: "utf8" },
9 cesu8: { type: "_internal", enc: "utf8" },
10 unicode11utf8: { type: "_internal", enc: "utf8" },
11 ucs2: { type: "_internal", enc: "ucs2", bom: utf16lebom },
12 utf16le:{ type: "_internal", enc: "ucs2", bom: utf16lebom },
13 binary: { type: "_internal", enc: "binary" },
14 base64: { type: "_internal", enc: "base64" },
15 hex: { type: "_internal", enc: "hex" },
18 _internal: function(options) {
19 if (!options || !options.enc)
20 throw new Error("Internal codec is called without encoding type.")
23 encoder: options.enc == "base64" ? encoderBase64 : encoderInternal,
24 decoder: decoderInternal,
32 // We use node.js internal decoder. It's signature is the same as ours.
33 var StringDecoder = require('string_decoder').StringDecoder;
35 if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
36 StringDecoder.prototype.end = function() {};
38 function decoderInternal() {
39 return new StringDecoder(this.enc);
42 // Encoder is mostly trivial
44 function encoderInternal() {
46 write: encodeInternal,
53 function encodeInternal(str) {
54 return new Buffer(str, this.enc);
58 // Except base64 encoder, which must keep its state.
60 function encoderBase64() {
62 write: encodeBase64Write,
69 function encodeBase64Write(str) {
70 str = this.prevStr + str;
71 var completeQuads = str.length - (str.length % 4);
72 this.prevStr = str.slice(completeQuads);
73 str = str.slice(0, completeQuads);
75 return new Buffer(str, "base64");
78 function encodeBase64End() {
79 return new Buffer(this.prevStr, "base64");