Add multiple node.js version support - Closes #561#562
Conversation
| "main": "./dist-node/index.js", | ||
| "engines": { | ||
| "node": ">=6 <=9", | ||
| "npm": "=>3 <=5" |
willclarktech
left a comment
There was a problem hiding this comment.
Tests fail on Node.js v6.0.0. I couldn't complete installation on Node.js v8.0.0.
Found some remaining Buffer.froms which should be updated in src:
▶ grep -R -n Buffer.from src
src/crypto/convert.js:37: return Buffer.from(matchedHex.slice(0, evenLength).join(''), 'hex');
...
src/transactions/utils/getTransactionBytes.js:184: ? Buffer.from(transaction.signature, 'hex')
Additionally there are Buffer.from(xxx, 'hex') cases in test. Maybe we should also be using our function there?
| export const bufferToHex = buffer => naclInstance.to_hex(buffer); | ||
|
|
||
| export const hexToBuffer = hex => Buffer.from(hex, 'hex'); | ||
| const hexRegex = /([0-9]|[a-f])/gim; |
There was a problem hiding this comment.
Do we really want multiline?
There was a problem hiding this comment.
This regex gives odd behaviour.
> Buffer.from('abhcd', 'hex')
// Node 6: throws 'Invalid hex string'
<Buffer ab> // Node 8
> hexToBuffer('abhcd')
<Buffer abcd>
There was a problem hiding this comment.
will be changed to take only first part =)
| it('should create even numbered Buffer from odd number hex string', () => { | ||
| const buffer = hexToBuffer('c3a5c3a4c3b6a'); | ||
| return buffer.should.be.eql(Buffer.from('c3a5c3a4c3b6', 'hex')); | ||
| }); |
There was a problem hiding this comment.
We need a test case where there's a non-hex character in the middle of hex characters.
| const buffer = hexToBuffer(defaultHex); | ||
| return buffer.should.be.eql(defaultBuffer); | ||
| }); | ||
| it('should create empty Buffer from non-string', () => { |
There was a problem hiding this comment.
Should it? Node 6 and 8 both throw errors if you use a number for example.
There was a problem hiding this comment.
Will be changed to throw type error if it's not string
|
I think the |
|
Until node |
|
Whoops, sorry just realised one of my |
willclarktech
left a comment
There was a problem hiding this comment.
We still need to consider npm. I get an installation failure with npm v5.0.0.
| export const bufferToHex = buffer => naclInstance.to_hex(buffer); | ||
|
|
||
| export const hexToBuffer = hex => Buffer.from(hex, 'hex'); | ||
| const hexRegex = /^([0-9]|[a-f])+/gi; |
There was a problem hiding this comment.
Let's make this regex as simple as possible. Does the following work?
const hexRegex = /^[0-9a-f]+/i;
There was a problem hiding this comment.
i think global key is not needed but ^([0-9]|[a-f]) this is more readable isn't it?
There was a problem hiding this comment.
As discussed, a capturing group which never gets used is confusing.
| "description": "JavaScript library for sending Lisk transactions from the client or server", | ||
| "main": "./dist-node/index.js", | ||
| "engines": { | ||
| "node": ">=6.3 <=9", |
There was a problem hiding this comment.
We should put an upper bound on the latest version of 9 that we've tested.
| const hexRegex = /^([0-9]|[a-f])+/gi; | ||
| export const hexToBuffer = hex => { | ||
| if (typeof hex !== 'string') { | ||
| throw new TypeError('argument must be string'); |
There was a problem hiding this comment.
Argument must be a string
| throw new TypeError('argument must be string'); | ||
| } | ||
| const matchedHex = hex.match(hexRegex) || []; | ||
| if (matchedHex.length === 0) { |
There was a problem hiding this comment.
Why not just this?
const matchedHex = hex.match(hexRegex);
if (!matchedHex) {
return Buffer.alloc(0);
}
There was a problem hiding this comment.
In the following code, it uses first element like matchedHex[0], so I think it would be better to check length here already
| it('should create even numbered Buffer from odd number hex string', () => { | ||
| const buffer = hexToBuffer('c3a5c3a4c3b6a'); | ||
| return buffer.should.be.eql(Buffer.from('c3a5c3a4c3b6', 'hex')); | ||
| }); |
There was a problem hiding this comment.
Would also be good to have a test case with a valid odd-numbered hex string followed by non-hex. Eg. 123xxx and/or 123x.
this was solved by install without cache. |
fee66fb to
11e2f25
Compare
| return buffer.should.be.eql(defaultBuffer); | ||
| }); | ||
| it('should throw TypeError with number', () => { | ||
| return (() => hexToBuffer(123)).should.throw( |
There was a problem hiding this comment.
Prefer hexToBuffer.bind(null, 123).should.throw syntax.
| const buffer = hexToBuffer(defaultHex); | ||
| return buffer.should.be.eql(defaultBuffer); | ||
| }); | ||
| it('should throw TypeError with number', () => { |
There was a problem hiding this comment.
Prefer an empty line between its.
| }); | ||
| it('should throw TypeError with number', () => { | ||
| return (() => hexToBuffer(123)).should.throw( | ||
| new TypeError('argument must be string'), |
There was a problem hiding this comment.
This test gives false positives: the message is wrong here.
| export const bufferToHex = buffer => naclInstance.to_hex(buffer); | ||
|
|
||
| export const hexToBuffer = hex => Buffer.from(hex, 'hex'); | ||
| const hexRegex = /^[0-9a-f]+/i; |
| return Buffer.alloc(0); | ||
| } | ||
| const evenLength = Math.floor(matchedHex.length / 2) * 2; | ||
| return Buffer.from(matchedHex.slice(0, evenLength), 'hex'); |
There was a problem hiding this comment.
Do you think it is better to return a sliced element here rather than throwing an error in case it is not even?
There was a problem hiding this comment.
Same applies to non-hex strings
There was a problem hiding this comment.
I think main reason not to throw an error for non-even input is because node 8 is not behaving like that.
There was a problem hiding this comment.
Also, for the non-hex string, node 8 ignores the non hex string, and create the buffer from whatever it can from the front.
> process.version
'v8.9.4'
> Buffer.from('abcde', 'hex');
<Buffer ab cd>
> Buffer.from('abcdzz123', 'hex');
<Buffer ab cd>
There was a problem hiding this comment.
But this is our opportunity to make the behaviour we actually want.
There was a problem hiding this comment.
nodejs/node#12012 (comment)
I think main thing is that parallel behavior with base64 and coherent eror handlings of various things it can happen
There was a problem hiding this comment.
Actually the sentiment seems to be like "you can do validation on your side if you want" on the node side of things: nodejs/node#8569
And in my opinion I would prefer to have an error thrown.
f919188 to
6a41d85
Compare
6a41d85 to
01f257b
Compare
Closes #561
Description
hexToBufferbehavior to mimic node version >= 8 in all versions.Buffer.fromto usehexToBufferNote
Run test against
Changes
Buffer.from(hex, 'hex')Buffer.from(hex, 'hex')should throw Type error if first argument is not stringBuffer.from(hex, 'hex')should ignore invalid hex string