Comparing version 1.0.4 to 1.0.5
@@ -24,2 +24,4 @@ "use strict"; | ||
var isArray = _interopRequire(require("lodash/isArray")); | ||
var includeIoMixin = _interopRequire(require("./io-mixin")); | ||
@@ -47,5 +49,10 @@ | ||
slicePadding(io, padding); | ||
return result.buffer().toString("utf8"); | ||
return result.buffer(); | ||
} | ||
}, | ||
readString: { | ||
value: function readString(io) { | ||
return this.read(io).toString("utf8"); | ||
} | ||
}, | ||
write: { | ||
@@ -57,6 +64,8 @@ value: function write(value, io) { | ||
if (!isString(value)) { | ||
throw new Error("XDR Write Error: " + value + " is not a string,"); | ||
var buffer = undefined; | ||
if (isString(value)) { | ||
buffer = Buffer.from(value, "utf8"); | ||
} else { | ||
buffer = Buffer.from(value); | ||
} | ||
var buffer = Buffer.from(value, "utf8"); | ||
@@ -69,6 +78,10 @@ Int.write(buffer.length, io); | ||
value: function isValid(value) { | ||
if (!isString(value)) { | ||
var buffer = undefined; | ||
if (isString(value)) { | ||
buffer = Buffer.from(value, "utf8"); | ||
} else if (isArray(value) || Buffer.isBuffer(value)) { | ||
buffer = Buffer.from(value); | ||
} else { | ||
return false; | ||
} | ||
var buffer = Buffer.from(value, "utf8"); | ||
return buffer.length <= this._maxLength; | ||
@@ -75,0 +88,0 @@ } |
{ | ||
"name": "js-xdr", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Read/write XDR encoded data structures (RFC 4506)", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,3 @@ import { Int } from "./int"; | ||
import isString from "lodash/isString"; | ||
import isArray from "lodash/isArray"; | ||
import includeIoMixin from './io-mixin'; | ||
@@ -25,5 +26,9 @@ | ||
slicePadding(io, padding); | ||
return result.buffer().toString('utf8'); | ||
return result.buffer(); | ||
} | ||
readString(io) { | ||
return this.read(io).toString('utf8'); | ||
} | ||
write(value, io) { | ||
@@ -36,6 +41,8 @@ if(value.length > this._maxLength) { | ||
if(!isString(value)) { | ||
throw new Error(`XDR Write Error: ${value} is not a string,`); | ||
let buffer; | ||
if (isString(value)) { | ||
buffer = Buffer.from(value, 'utf8'); | ||
} else { | ||
buffer = Buffer.from(value); | ||
} | ||
let buffer = Buffer.from(value, 'utf8'); | ||
@@ -47,6 +54,10 @@ Int.write(buffer.length, io); | ||
isValid(value) { | ||
if (!isString(value)) { | ||
let buffer; | ||
if (isString(value)) { | ||
buffer = Buffer.from(value, 'utf8'); | ||
} else if (isArray(value) || Buffer.isBuffer(value)) { | ||
buffer = Buffer.from(value); | ||
} else { | ||
return false; | ||
} | ||
let buffer = Buffer.from(value, 'utf8'); | ||
return buffer.length <= this._maxLength; | ||
@@ -53,0 +64,0 @@ } |
@@ -9,8 +9,20 @@ import { Cursor } from "../../src/cursor"; | ||
it('decodes correctly', function() { | ||
expect(read([0x00,0x00,0x00,0x00])).to.eql(""); | ||
expect(read([0x00,0x00,0x00,0x01,0x41,0x00,0x00,0x00])).to.eql("A"); | ||
expect(read([0x00,0x00,0x00,0x03,0xe4,0xb8,0x89,0x00])).to.eql("三"); | ||
expect(read([0x00,0x00,0x00,0x02,0x41,0x41,0x00,0x00])).to.eql("AA"); | ||
expect(read([0x00,0x00,0x00,0x00]).toString('utf8')).to.eql(""); | ||
expect(read([0x00,0x00,0x00,0x01,0x41,0x00,0x00,0x00]).toString('utf8')).to.eql("A"); | ||
expect(read([0x00,0x00,0x00,0x03,0xe4,0xb8,0x89,0x00]).toString('utf8')).to.eql("三"); | ||
expect(read([0x00,0x00,0x00,0x02,0x41,0x41,0x00,0x00]).toString('utf8')).to.eql("AA"); | ||
}); | ||
it('decodes correctly to string', function() { | ||
expect(readString([0x00,0x00,0x00,0x00])).to.eql(""); | ||
expect(readString([0x00,0x00,0x00,0x01,0x41,0x00,0x00,0x00])).to.eql("A"); | ||
expect(readString([0x00,0x00,0x00,0x03,0xe4,0xb8,0x89,0x00])).to.eql("三"); | ||
expect(readString([0x00,0x00,0x00,0x02,0x41,0x41,0x00,0x00])).to.eql("AA"); | ||
}); | ||
it('decodes non-utf-8 correctly', function() { | ||
let val = read([0x00,0x00,0x00,0x01,0xd1,0x00,0x00,0x00]) | ||
expect(val[0]).to.eql(0xd1); | ||
}) | ||
it("throws a read error when the encoded length is greater than the allowed max", function() { | ||
@@ -30,2 +42,7 @@ expect(() => read([0x00,0x00,0x00,0x05,0x41,0x41,0x41,0x41,0x41])).to.throw(/read error/i); | ||
} | ||
function readString(bytes) { | ||
let io = new Cursor(bytes); | ||
return subject.readString(io); | ||
} | ||
}); | ||
@@ -35,3 +52,3 @@ | ||
it('encodes correctly', function() { | ||
it('encodes string correctly', function() { | ||
expect(write("")).to.eql([0x00,0x00,0x00,0x00]); | ||
@@ -43,2 +60,10 @@ expect(write("三")).to.eql([0x00,0x00,0x00,0x03,0xe4,0xb8,0x89,0x00]); | ||
it('encodes non-utf-8 correctly', function() { | ||
expect(write([0xd1])).to.eql([0x00,0x00,0x00,0x01,0xd1,0x00,0x00,0x00]); | ||
}); | ||
it('encodes non-utf-8 correctly (buffer)', function() { | ||
expect(write(Buffer.from([0xd1]))).to.eql([0x00,0x00,0x00,0x01,0xd1,0x00,0x00,0x00]); | ||
}); | ||
function write(value) { | ||
@@ -58,2 +83,10 @@ let io = new Cursor(8); | ||
it('returns true for arrays of the correct length', function() { | ||
expect(subject.isValid([0x01])).to.be.true; | ||
}); | ||
it('returns true for buffers of the correct length', function() { | ||
expect(subject.isValid(Buffer.from([0x01]))).to.be.true; | ||
}); | ||
it('returns false for strings that are too large', function() { | ||
@@ -63,7 +96,14 @@ expect(subject.isValid("aaaaa")).to.be.false; | ||
it('returns false for non string', function() { | ||
it('returns false for arrays that are too large', function() { | ||
expect(subject.isValid([0x01, 0x01, 0x01, 0x01, 0x01])).to.be.false; | ||
}); | ||
it('returns false for buffers that are too large', function() { | ||
expect(subject.isValid(Buffer.from([0x01, 0x01, 0x01, 0x01, 0x01]))).to.be.false; | ||
}); | ||
it('returns false for non string/array/buffer', function() { | ||
expect(subject.isValid(true)).to.be.false; | ||
expect(subject.isValid(null)).to.be.false; | ||
expect(subject.isValid(3)).to.be.false; | ||
expect(subject.isValid([0])).to.be.false; | ||
}); | ||
@@ -70,0 +110,0 @@ }); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
707062
16481