|
3 | 3 | const BSON = require('../register-bson');
|
4 | 4 | const ObjectId = BSON.ObjectId;
|
5 | 5 |
|
| 6 | +const BigInt = global.BigInt; |
| 7 | + |
6 | 8 | describe('toBSON', function () {
|
7 | 9 | /**
|
8 | 10 | * @ignore
|
@@ -129,4 +131,83 @@ describe('toBSON', function () {
|
129 | 131 | expect(true).to.equal(test2);
|
130 | 132 | done();
|
131 | 133 | });
|
| 134 | + |
| 135 | + describe('when used on global existing types', () => { |
| 136 | + beforeEach(() => { |
| 137 | + Number.prototype.toBSON = () => 'hello'; |
| 138 | + String.prototype.toBSON = () => 'hello'; |
| 139 | + Boolean.prototype.toBSON = () => 'hello'; |
| 140 | + if (BigInt) BigInt.prototype.toBSON = () => 'hello'; |
| 141 | + }); |
| 142 | + |
| 143 | + afterEach(() => { |
| 144 | + // remove prototype extension intended for test |
| 145 | + delete Number.prototype.toBSON; |
| 146 | + delete String.prototype.toBSON; |
| 147 | + delete Boolean.prototype.toBSON; |
| 148 | + if (BigInt) delete BigInt.prototype.toBSON; |
| 149 | + }); |
| 150 | + |
| 151 | + const testToBSONFor = value => { |
| 152 | + it(`should use toBSON on false-y ${typeof value} ${value === '' ? "''" : value}`, () => { |
| 153 | + const serialized_data = BSON.serialize({ a: value }); |
| 154 | + expect(serialized_data.indexOf(Buffer.from('hello\0', 'utf8'))).to.be.greaterThan(0); |
| 155 | + |
| 156 | + const deserialized_doc = BSON.deserialize(serialized_data); |
| 157 | + expect(deserialized_doc).to.have.property('a', 'hello'); |
| 158 | + }); |
| 159 | + }; |
| 160 | + |
| 161 | + testToBSONFor(0); |
| 162 | + testToBSONFor(NaN); |
| 163 | + testToBSONFor(''); |
| 164 | + testToBSONFor(false); |
| 165 | + if (BigInt) { |
| 166 | + testToBSONFor(BigInt(0)); |
| 167 | + } |
| 168 | + |
| 169 | + it('should use toBSON on false-y number in calculateObjectSize', () => { |
| 170 | + // Normally is 20 bytes |
| 171 | + // int32 0x04 'a\x00' |
| 172 | + // int32 0x10 '0\x00' int32 \0 |
| 173 | + // \0 |
| 174 | + // --------- |
| 175 | + // with toBSON is 26 bytes (hello + null) |
| 176 | + // int32 0x04 'a\x00' |
| 177 | + // int32 0x02 '0\x00' int32 'hello\0' \0 |
| 178 | + // \0 |
| 179 | + const sizeNestedToBSON = BSON.calculateObjectSize({ a: [0] }); |
| 180 | + expect(sizeNestedToBSON).to.equal(26); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should use toBSON in calculateObjectSize', () => { |
| 185 | + const sizeTopLvlToBSON = BSON.calculateObjectSize({ toBSON: () => ({ a: 1 }) }); |
| 186 | + const sizeOfWhatToBSONReturns = BSON.calculateObjectSize({ a: 1 }); |
| 187 | + expect(sizeOfWhatToBSONReturns).to.equal(12); |
| 188 | + expect(sizeTopLvlToBSON).to.equal(12); |
| 189 | + |
| 190 | + const toBSONAsAKeySize = BSON.calculateObjectSize({ toBSON: { a: 1 } }); |
| 191 | + expect(toBSONAsAKeySize).to.equal(25); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should serialize to a key for non-function values', () => { |
| 195 | + // int32 0x10 'toBSON\x00' int32 \0 |
| 196 | + const size = BSON.calculateObjectSize({ toBSON: 1 }); |
| 197 | + expect(size).to.equal(17); |
| 198 | + |
| 199 | + const bytes = BSON.serialize({ toBSON: 1 }); |
| 200 | + expect(bytes.indexOf(Buffer.from('toBSON\0', 'utf8'))).to.be.greaterThan(0); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should still be omitted if serializeFunctions is true', () => { |
| 204 | + const bytes = BSON.serialize( |
| 205 | + { toBSON: () => ({ a: 1, fn: () => ({ a: 1 }) }) }, |
| 206 | + { serializeFunctions: true } |
| 207 | + ); |
| 208 | + expect(bytes.indexOf(Buffer.from('a\0', 'utf8'))).to.be.greaterThan(0); |
| 209 | + const doc = BSON.deserialize(bytes); |
| 210 | + expect(doc).to.have.property('a', 1); |
| 211 | + expect(doc).to.have.property('fn').that.is.instanceOf(BSON.Code); |
| 212 | + }); |
132 | 213 | });
|
0 commit comments