-
Notifications
You must be signed in to change notification settings - Fork 12
fix: support uint8arrays in place of node buffers #23
fix: support uint8arrays in place of node buffers #23
Conversation
Allows passing Uint8Arrays in place of Node Buffers BREAKING CHANGE: takes Uint8Arrays as well as Node Buffers
|
cc @Gozala (can't add you to the reviewers list) |
Gozala
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure 🚨 isn't introducing a regression, because I'm not exactly sure.
|
|
||
| const parts = k.toString().split('/') | ||
| const kStr = utf8Decoder.decode(k) | ||
| const parts = kStr.split('/') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Seems like factoring out logic to count / char codes would be make sense here, which also could be then improved to avoid string encoding splitting etc...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to leave this in place as it's just doing what was being done before, but it's a valid point.
| const key = record.key | ||
| const parts = key.toString().split('/') | ||
| const keyString = utf8Decoder.decode(key) | ||
| const parts = keyString.split('/') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 This is the second place were / char codes are counted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to leave this in place as it's just doing what was being done before, but it's a valid point.
src/validators/public-key.js
Outdated
| } | ||
|
|
||
| const prefix = key.slice(0, 4).toString() | ||
| const keyString = utf8Decoder.decode(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Seems like const prefix = utf8Decoder.decode(key.subarray(0, 4)) would have kept same profile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made this change
src/validators/public-key.js
Outdated
| const publicKeyHash = await multihashing(publicKey, 'sha2-256') | ||
|
|
||
| if (!keyhash.equals(publicKeyHash)) { | ||
| if (!keyhash.every((val, i) => val === publicKeyHash[i])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚨 This does not take byteLength into account.
📝 Seems like equality check should be factored out into separate function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are Uint8Arrays so unless I'm missing something .length should always equal .byteLength so we should be good to use .every and friends?
That said it could be optimised to compare lengths before iterating over the values which would worth pulling into it's own function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pulled it into it's own function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are
Uint8Arrays so unless I'm missing something.lengthshould always equal.byteLengthso we should be good to use.everyand friends?
They do. I was just trying to say that keyhash and publicKeyHash could have different length which this code was not taking into account.
Pulls in the latest interface-datastore and replaces use of node Buffers with Uint8Arrays Depends on: - [ ] ipfs/interface-datastore#43 - [ ] ipfs/js-datastore-core#27 - [ ] libp2p/js-libp2p-record#23
vasco-santos
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
|
Allows passing Uint8Arrays in place of Node Buffers
BREAKING CHANGE: takes Uint8Arrays as well as Node Buffers