Skip to content

Commit

Permalink
fix:Buffer factory bug when node version is less than v5.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo committed Aug 3, 2017
1 parent a1857dd commit 6fc831e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
11 changes: 2 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var debug = require('debug')('node-crc16');
var crc16Native = require('./build/Release/crc16.node');
var util = require('./util/util');

var parseParam = function (input, encoding, option) {
encoding = encoding || 'hex';
Expand All @@ -13,15 +14,7 @@ var parseParam = function (input, encoding, option) {
var buf = (function () {
if (typeof input === 'string') {
try {
/*
* Buffer.from is added in v5.10.0, as the api document shows. But some node version,
* v4.2.6 for example, Buffer.from is function, however, there is an error when you call
* `Buffer.from(string, encoding)`(error some like `hex is not function`).
* */
if (typeof Buffer.from === 'function' && process.version >= "v5.10.0") {
return Buffer.from(input, encoding);
}
return new Buffer(input, encoding);
return util.bufferFactory(input, encoding);
} catch (e) {
console.trace(e);
debug(e);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions test/test.syntax.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var should = require('chai').should();
var crc16 = require('../index');
var util = require('./../util/util');

var stream = '3a16070a00000000001a0000';
var sumShouldStr = '98af';
Expand All @@ -20,7 +21,7 @@ describe('Syntax to call the apis', function(){
sum.should.eql(sumShouldArry);
})
it('Stream input can be a buffer', function(){
var sum = crc16.checkSum(Buffer.from(stream, 'hex'));
var sum = crc16.checkSum(util.bufferFactory(stream, 'hex'));
sum.should.equal(sumShouldStr);
})
})
Expand All @@ -35,7 +36,7 @@ describe('Syntax to call the apis', function(){
isValid.should.equal(true);
})
it('Stream input can be a buffer', function(){
var isValid = crc16.verifySum(Buffer.from(stream + sumShouldStr, 'hex'));
var isValid = crc16.verifySum(util.bufferFactory(stream + sumShouldStr, 'hex'));
isValid.should.equal(true);
})
})
Expand Down
14 changes: 14 additions & 0 deletions util/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports.bufferFactory = function(){
var args = Array.prototype.slice.call(arguments, 0);
/*
* Buffer.from is added in v5.10.0, as the api document shows. But some node version,
* v4.2.6 for example, Buffer.from is function, however, there is an error when you call
* `Buffer.from(string, encoding)`(error some like `hex is not function`).
* */
if (typeof Buffer.from === 'function' && process.version >= "v5.10.0") {
return Buffer.from.apply(null, args);
}

args.unshift(null);
return new (Function.prototype.bind.apply(Buffer, args));
}

0 comments on commit 6fc831e

Please sign in to comment.