|
| 1 | +import { BSON } from '../register-bson'; |
| 2 | +import { bufferFromHexArray } from './tools/utils'; |
| 3 | +import { BSON_DATA_LONG } from '../../src/constants'; |
| 4 | +import { BSONDataView } from '../../src/utils/byte_utils'; |
| 5 | + |
| 6 | +describe('BSON BigInt serialization Support', function () { |
| 7 | + // Index for the data type byte of a BSON document with a |
| 8 | + // NOTE: These offsets only apply for documents with the shape {a : <n>} |
| 9 | + // where n is a BigInt |
| 10 | + type SerializedDocParts = { |
| 11 | + dataType: number; |
| 12 | + key: string; |
| 13 | + value: bigint; |
| 14 | + }; |
| 15 | + /** |
| 16 | + * NOTE: this function operates on serialized BSON documents with the shape { <key> : <n> } |
| 17 | + * where n is some int64. This function assumes that keys are properly encoded |
| 18 | + * with the necessary null byte at the end and only at the end of the key string |
| 19 | + */ |
| 20 | + function getSerializedDocParts(serializedDoc: Uint8Array): SerializedDocParts { |
| 21 | + const DATA_TYPE_OFFSET = 4; |
| 22 | + const KEY_OFFSET = 5; |
| 23 | + |
| 24 | + const dataView = BSONDataView.fromUint8Array(serializedDoc); |
| 25 | + const keySlice = serializedDoc.slice(KEY_OFFSET); |
| 26 | + |
| 27 | + let keyLength = 0; |
| 28 | + while (keySlice[keyLength++] !== 0); |
| 29 | + |
| 30 | + const valueOffset = KEY_OFFSET + keyLength; |
| 31 | + const key = Buffer.from(serializedDoc.slice(KEY_OFFSET, KEY_OFFSET + keyLength)).toString( |
| 32 | + 'utf8' |
| 33 | + ); |
| 34 | + |
| 35 | + return { |
| 36 | + dataType: dataView.getInt8(DATA_TYPE_OFFSET), |
| 37 | + key: key.slice(0, keyLength - 1), |
| 38 | + value: dataView.getBigInt64(valueOffset, true) |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + it('serializes bigints with the correct BSON type', function () { |
| 43 | + const testDoc = { a: 0n }; |
| 44 | + const serializedDoc = getSerializedDocParts(BSON.serialize(testDoc)); |
| 45 | + expect(serializedDoc.dataType).to.equal(BSON_DATA_LONG); |
| 46 | + }); |
| 47 | + |
| 48 | + it('serializes bigints into little-endian byte order', function () { |
| 49 | + const testDoc = { a: 0x1234567812345678n }; |
| 50 | + const serializedDoc = getSerializedDocParts(BSON.serialize(testDoc)); |
| 51 | + const expectedResult = getSerializedDocParts( |
| 52 | + bufferFromHexArray([ |
| 53 | + '12', // int64 type |
| 54 | + '6100', // 'a' key with null terminator |
| 55 | + '7856341278563412' |
| 56 | + ]) |
| 57 | + ); |
| 58 | + |
| 59 | + expect(expectedResult.value).to.equal(serializedDoc.value); |
| 60 | + }); |
| 61 | + |
| 62 | + it('serializes a BigInt that can be safely represented as a Number', function () { |
| 63 | + const testDoc = { a: 0x23n }; |
| 64 | + const serializedDoc = getSerializedDocParts(BSON.serialize(testDoc)); |
| 65 | + const expectedResult = getSerializedDocParts( |
| 66 | + bufferFromHexArray([ |
| 67 | + '12', // int64 type |
| 68 | + '6100', // 'a' key with null terminator |
| 69 | + '2300000000000000' // little endian int64 |
| 70 | + ]) |
| 71 | + ); |
| 72 | + expect(serializedDoc).to.deep.equal(expectedResult); |
| 73 | + }); |
| 74 | + |
| 75 | + it('serializes a BigInt in the valid range [-2^63, 2^63 - 1]', function () { |
| 76 | + const testDoc = { a: 0xfffffffffffffff1n }; |
| 77 | + const serializedDoc = getSerializedDocParts(BSON.serialize(testDoc)); |
| 78 | + const expectedResult = getSerializedDocParts( |
| 79 | + bufferFromHexArray([ |
| 80 | + '12', // int64 |
| 81 | + '6100', // 'a' key with null terminator |
| 82 | + 'f1ffffffffffffff' |
| 83 | + ]) |
| 84 | + ); |
| 85 | + expect(serializedDoc).to.deep.equal(expectedResult); |
| 86 | + }); |
| 87 | + |
| 88 | + it('wraps to negative on a BigInt that is larger than (2^63 -1)', function () { |
| 89 | + const maxIntPlusOne = { a: 2n ** 63n }; |
| 90 | + const serializedMaxIntPlusOne = getSerializedDocParts(BSON.serialize(maxIntPlusOne)); |
| 91 | + const expectedResultForMaxIntPlusOne = getSerializedDocParts( |
| 92 | + bufferFromHexArray([ |
| 93 | + '12', // int64 |
| 94 | + '6100', // 'a' key with null terminator |
| 95 | + '0000000000000080' |
| 96 | + ]) |
| 97 | + ); |
| 98 | + expect(serializedMaxIntPlusOne).to.deep.equal(expectedResultForMaxIntPlusOne); |
| 99 | + }); |
| 100 | + |
| 101 | + it('serializes BigInts at the edges of the valid range [-2^63, 2^63 - 1]', function () { |
| 102 | + const maxPositiveInt64 = { a: 2n ** 63n - 1n }; |
| 103 | + const serializedMaxPositiveInt64 = getSerializedDocParts(BSON.serialize(maxPositiveInt64)); |
| 104 | + const expectedSerializationForMaxPositiveInt64 = getSerializedDocParts( |
| 105 | + bufferFromHexArray([ |
| 106 | + '12', // int64 |
| 107 | + '6100', // 'a' key with null terminator |
| 108 | + 'ffffffffffffff7f' |
| 109 | + ]) |
| 110 | + ); |
| 111 | + expect(serializedMaxPositiveInt64).to.deep.equal(expectedSerializationForMaxPositiveInt64); |
| 112 | + |
| 113 | + const minPositiveInt64 = { a: -(2n ** 63n) }; |
| 114 | + const serializedMinPositiveInt64 = getSerializedDocParts(BSON.serialize(minPositiveInt64)); |
| 115 | + const expectedSerializationForMinPositiveInt64 = getSerializedDocParts( |
| 116 | + bufferFromHexArray([ |
| 117 | + '12', // int64 |
| 118 | + '6100', // 'a' key with null terminator |
| 119 | + '0000000000000080' |
| 120 | + ]) |
| 121 | + ); |
| 122 | + expect(serializedMinPositiveInt64).to.deep.equal(expectedSerializationForMinPositiveInt64); |
| 123 | + }); |
| 124 | + |
| 125 | + it('truncates a BigInt that is larger than a 64-bit int', function () { |
| 126 | + const testDoc = { a: 2n ** 64n + 1n }; |
| 127 | + const serializedDoc = getSerializedDocParts(BSON.serialize(testDoc)); |
| 128 | + const expectedSerialization = getSerializedDocParts( |
| 129 | + bufferFromHexArray([ |
| 130 | + '12', //int64 |
| 131 | + '6100', // 'a' key with null terminator |
| 132 | + '0100000000000000' |
| 133 | + ]) |
| 134 | + ); |
| 135 | + expect(serializedDoc).to.deep.equal(expectedSerialization); |
| 136 | + }); |
| 137 | + |
| 138 | + it('serializes array of BigInts', function () { |
| 139 | + const testArr = { a: [1n] }; |
| 140 | + const serializedArr = BSON.serialize(testArr); |
| 141 | + const expectedSerialization = bufferFromHexArray([ |
| 142 | + '04', // array |
| 143 | + '6100', // 'a' key with null terminator |
| 144 | + bufferFromHexArray([ |
| 145 | + '12', // int64 |
| 146 | + '3000', // '0' key with null terminator |
| 147 | + '0100000000000000' // 1n (little-endian) |
| 148 | + ]).toString('hex') |
| 149 | + ]); |
| 150 | + expect(serializedArr).to.deep.equal(expectedSerialization); |
| 151 | + }); |
| 152 | + |
| 153 | + it('serializes Map with BigInt values', function () { |
| 154 | + const testMap = new Map(); |
| 155 | + testMap.set('a', 1n); |
| 156 | + const serializedMap = getSerializedDocParts(BSON.serialize(testMap)); |
| 157 | + const expectedSerialization = getSerializedDocParts( |
| 158 | + bufferFromHexArray([ |
| 159 | + '12', // int64 |
| 160 | + '6100', // 'a' key with null terminator |
| 161 | + '0100000000000000' |
| 162 | + ]) |
| 163 | + ); |
| 164 | + expect(serializedMap).to.deep.equal(expectedSerialization); |
| 165 | + }); |
| 166 | +}); |
0 commit comments