File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -128,13 +128,33 @@ const bufferFromHexArray = array => {
128
128
129
129
exports . bufferFromHexArray = bufferFromHexArray ;
130
130
131
- function int32ToHex ( int32 ) {
131
+ /**
132
+ * A companion helper to bufferFromHexArray to help with constructing bson bytes manually.
133
+ * When creating a BSON Binary you need a leading little endian int32 followed by a sequence of bytes
134
+ * of that length.
135
+ *
136
+ * @example
137
+ * ```js
138
+ * const binAsHex = '000000';
139
+ * const serializedUUID = bufferFromHexArray([
140
+ * '05', // binData type
141
+ * '6100', // 'a' & null
142
+ * int32ToHex(binAsHex.length / 2), // binary starts with int32 length
143
+ * '7F', // user subtype
144
+ * binAsHex // uuid bytes
145
+ * ]);
146
+ * ```
147
+ *
148
+ * @param {number | Int32 } int32 -
149
+ * @returns
150
+ */
151
+ function int32LEToHex ( int32 ) {
132
152
const buf = Buffer . alloc ( 4 ) ;
133
- buf . writeInt32LE ( int32 , 0 ) ;
153
+ buf . writeInt32LE ( + int32 , 0 ) ;
134
154
return buf . toString ( 'hex' ) ;
135
155
}
136
156
137
- exports . int32ToHex = int32ToHex ;
157
+ exports . int32LEToHex = int32LEToHex ;
138
158
139
159
/**
140
160
* A helper to calculate the byte size of a string (including null)
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { BSON, BSONError } from '../register-bson';
5
5
const BSON_DATA_BINARY = BSON . BSONType . binData ;
6
6
import { BSON_BINARY_SUBTYPE_UUID_NEW } from '../../src/constants' ;
7
7
import { expect } from 'chai' ;
8
- import { bufferFromHexArray , int32ToHex } from './tools/utils' ;
8
+ import { bufferFromHexArray , int32LEToHex } from './tools/utils' ;
9
9
10
10
// Test values
11
11
const UPPERCASE_DASH_SEPARATED_UUID_STRING = 'AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA' ;
@@ -158,7 +158,7 @@ describe('UUID', () => {
158
158
const serializedUUID = bufferFromHexArray ( [
159
159
'05' , // binData type
160
160
'6100' , // 'a' & null
161
- int32ToHex ( nullUUID . length / 2 ) , // binary starts with int32 length
161
+ int32LEToHex ( nullUUID . length / 2 ) , // binary starts with int32 length
162
162
'04' , // uuid subtype
163
163
nullUUID // uuid bytes
164
164
] ) ;
You can’t perform that action at this time.
0 commit comments