internal.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Export Node.js internal encodings.
  2. var utf16lebom = new Buffer([0xFF, 0xFE]);
  3. module.exports = {
  4. // Encodings
  5. utf8: { type: "_internal", enc: "utf8" },
  6. cesu8: { type: "_internal", enc: "utf8" },
  7. unicode11utf8: { type: "_internal", enc: "utf8" },
  8. ucs2: { type: "_internal", enc: "ucs2", bom: utf16lebom },
  9. utf16le:{ type: "_internal", enc: "ucs2", bom: utf16lebom },
  10. binary: { type: "_internal", enc: "binary" },
  11. base64: { type: "_internal", enc: "base64" },
  12. hex: { type: "_internal", enc: "hex" },
  13. // Codec.
  14. _internal: function(options) {
  15. if (!options || !options.enc)
  16. throw new Error("Internal codec is called without encoding type.")
  17. return {
  18. encoder: encoderInternal,
  19. decoder: decoderInternal,
  20. enc: options.enc,
  21. bom: options.bom,
  22. };
  23. },
  24. };
  25. // We use node.js internal decoder. It's signature is the same as ours.
  26. var StringDecoder = require('string_decoder').StringDecoder;
  27. if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.
  28. StringDecoder.prototype.end = function() {};
  29. function decoderInternal() {
  30. return new StringDecoder(this.enc);
  31. }
  32. function encoderInternal() {
  33. return {
  34. write: encodeInternal,
  35. end: function() {},
  36. enc: this.enc,
  37. }
  38. }
  39. function encodeInternal(str) {
  40. return new Buffer(str, this.enc);
  41. }