feat(NODE-7314): export byteUtils & add missing methods#852
feat(NODE-7314): export byteUtils & add missing methods#852baileympearson merged 6 commits intomainfrom
Conversation
2379a1e to
b5db818
Compare
nbbeeken
left a comment
There was a problem hiding this comment.
Looks great, one concern:
baileympearson
left a comment
There was a problem hiding this comment.
From the AC in the ticket:
.isBuffer
Does not exist; but we'll either want to remove usages of this from the driver (we'll need to evaluate on a case-by-case basis) or useisUint8Array, which exists in BSON. Maybe it makes sense to export isUint8Array from BSON too.
and
.from
The driver uses Buffer.from to convert the following to buffers:
- utf8 strings
- number arrays
- buffers
- base64 strings
Byte utils supports converting base64 strings and number arrays to Uint8Arrays already. converting buffers to buffers can be done usingisUint8Arrayand conditionally converting the string argument to a buffer if needed. We will need to add conversion of utf8 strings -> buffers to byte utils.
Have you given these APIs thought / are they not needed to remove dependence on the global Buffer object in the driver?
|
@baileympearson thanks for you comment. I have added missing methods: |
Description
Summary of Changes
Added a
compare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1byte utility that performs a lexicographical comparison of two byte arrays.Added a
concat(list: Uint8Array[]): Uint8Arrayutility.And mark
ByteUtilsas public and experimental (similar as forNumberUtils).Notes for Reviewers
compareare intended to match typical lexicographical ordering used for binary data (byte-by-byte from index 0, with shorter equal-prefix arrays considered smaller).concatis implemented using .length on Uint8Array (number of elements), which matches the number of bytes for this typed array and keeps the implementation simple and explicit.What is the motivation for this change?
Release Highlight
ByteUtilsadded as a binary utillityByteUtilsare now public and provide set of platform-agnostic tools to manipulate binary data (using Buffer in nodejs-compatible environments and fallback to Uint8Array).Note
This feature is experimental and may change at any time
Double check the following
npm run check:lint)type(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript