buffer-codec
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -60,2 +60,7 @@ (function() { | ||
aCodecName = null; | ||
} else if (aCodecName instanceof Codec) { | ||
if (aBufferSize > 0) { | ||
aCodecName.init(aBufferSize); | ||
} | ||
return aCodecName; | ||
} | ||
@@ -111,2 +116,12 @@ if (!(this instanceof Codec)) { | ||
Codec.prototype.byteLength = function(value) { | ||
if (this._encodeBuffer) { | ||
return this.encodeBuffer(value); | ||
} else if (this._encodeString) { | ||
return this._encodeString(value).length; | ||
} else { | ||
throw new NotImplementedError(); | ||
} | ||
}; | ||
Codec.prototype.encodeString = function(value) { | ||
@@ -118,3 +133,3 @@ var len; | ||
len = this._encodeBuffer(value, this.buffer); | ||
return this.buffer.toString(void 0, 0, len); | ||
return this.buffer.toString(this.bufferEncoding, 0, len); | ||
} else { | ||
@@ -121,0 +136,0 @@ throw new NotImplementedError(); |
126
lib/codec.js
(function() { | ||
var BinaryCodec, Codec, JsonCodec, TextCodec, aliases, isBuffer, isString, register, util; | ||
var BinaryCodec, Codec, Errors, HexCodec, InvalidFormatError, JsonCodec, TextCodec, aliases, isBuffer, isString, register, util; | ||
util = require("abstract-object/lib/util"); | ||
isString = util.isString; | ||
Errors = require('abstract-object/Error'); | ||
Codec = module.exports = require('./abstract-codec'); | ||
isString = util.isString; | ||
register = Codec.register; | ||
@@ -16,2 +18,4 @@ | ||
InvalidFormatError = Errors.InvalidFormatError; | ||
TextCodec = (function() { | ||
@@ -36,2 +40,10 @@ function TextCodec() {} | ||
TextCodec.prototype.byteLength = function(data) { | ||
if (data != null) { | ||
return data.length || String(data).length; | ||
} else { | ||
return 0; | ||
} | ||
}; | ||
return TextCodec; | ||
@@ -50,2 +62,6 @@ | ||
JsonCodec.prototype.byteLength = function(data) { | ||
return this._encodeString(data).length; | ||
}; | ||
return JsonCodec; | ||
@@ -55,2 +71,96 @@ | ||
HexCodec = (function() { | ||
function HexCodec() {} | ||
register(HexCodec, TextCodec); | ||
HexCodec.prototype.bufferEncoding = 'hex'; | ||
HexCodec.prototype.byteToHex = function(byte) { | ||
if (byte < 16) { | ||
return '0' + byte.toString(16); | ||
} else { | ||
return byte.toString(16); | ||
} | ||
}; | ||
HexCodec.prototype._encodeString = function(data) { | ||
var byte, i, length, result; | ||
length = data.length; | ||
if (length % 2 !== 0) { | ||
throw new InvalidFormatError('invalid hex string.'); | ||
} | ||
length = length >> 1; | ||
result = ""; | ||
i = 0; | ||
while (i < length) { | ||
byte = parseInt(data.substr(i * 2, 2), 16); | ||
result += String.fromCharCode(byte); | ||
i++; | ||
} | ||
return result; | ||
}; | ||
HexCodec.prototype._decodeString = function(data) { | ||
var i, result; | ||
result = ""; | ||
i = 0; | ||
while (i < data.length) { | ||
result += this.byteToHex(data.charCodeAt(i)); | ||
i++; | ||
} | ||
return result; | ||
}; | ||
HexCodec.prototype._encodeBuffer = function(data, destBuffer, offset, encoding) { | ||
var byte, i, length; | ||
offset = Number(offset) || 0; | ||
length = data.length; | ||
if (length % 2 !== 0) { | ||
throw new InvalidFormatError('invalid hex string.'); | ||
} | ||
length = length >> 1; | ||
if (isBuffer(destBuffer)) { | ||
i = destBuffer.length - offset; | ||
if (length > i) { | ||
length = i; | ||
} | ||
i = 0; | ||
while (i < length) { | ||
byte = parseInt(data.substr(i * 2, 2), 16); | ||
if (isNaN(byte)) { | ||
throw new InvalidFormatError('invalid hex string.'); | ||
} | ||
destBuffer[offset + i] = byte; | ||
i++; | ||
} | ||
} | ||
return length; | ||
}; | ||
HexCodec.prototype._decodeBuffer = function(buf, start, end) { | ||
var len, result; | ||
len = buf.length; | ||
if (!start || start < 0) { | ||
start = 0; | ||
} | ||
if (!end || end < 0 || end > len) { | ||
end = len; | ||
} | ||
result = ""; | ||
while (start < end) { | ||
result += this.byteToHex(buf[start]); | ||
start++; | ||
} | ||
return result; | ||
}; | ||
HexCodec.prototype.byteLength = function(data) { | ||
return data.length >> 1; | ||
}; | ||
return HexCodec; | ||
})(); | ||
BinaryCodec = (function() { | ||
@@ -69,2 +179,14 @@ var arraySlice; | ||
BinaryCodec.prototype.byteLength = function(data) { | ||
var arr; | ||
if (isBuffer(data)) { | ||
return data.length; | ||
} else if (isString(data)) { | ||
return Codec.getByteLen(data); | ||
} else { | ||
arr = arraySlice.call(data); | ||
return arr.length; | ||
} | ||
}; | ||
BinaryCodec.prototype._encodeBuffer = function(data, destBuffer, offset, encoding) { | ||
@@ -71,0 +193,0 @@ var arr, dataIsBuffer, i, len, v, _i, _len; |
{ | ||
"name": "buffer-codec", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "add the codec ability to abstract-nosql database.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/node-buffer-codec", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62804
580