You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.2 KiB

exports.utf8 = exports['utf-8'] = {
encode: function(data){
return isBinary(data)
? data
: String(data);
},
decode: function(data){
return typeof data === 'string'
? data
: String(data)
},
buffer: false,
type: 'utf8'
};
exports.json = {
encode: JSON.stringify,
decode: JSON.parse,
buffer: false,
type: 'json'
};
exports.binary = {
encode: function(data){
return isBinary(data)
? data
: new Buffer(data);
},
decode: identity,
buffer: true,
type: 'binary'
};
exports.none = {
encode: identity,
decode: identity,
buffer: false,
type: 'id'
};
exports.id = exports.none;
var bufferEncodings = [
'hex',
'ascii',
'base64',
'ucs2',
'ucs-2',
'utf16le',
'utf-16le'
];
bufferEncodings.forEach(function(type){
exports[type] = {
encode: function(data){
return isBinary(data)
? data
: new Buffer(data, type);
},
decode: function(buffer){
return buffer.toString(type);
},
buffer: true,
type: type
};
});
function identity(value){
return value;
}
function isBinary(data){
return data === undefined
|| data === null
|| Buffer.isBuffer(data);
}