Skip to content

Commit 7e84230

Browse files
committed
update constructor and add test assertions
1 parent 187d1c4 commit 7e84230

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/bson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface Document {
112112
const MAXSIZE = 1024 * 1024 * 17;
113113

114114
// Current Internal Temporary Serialization Buffer
115-
let buffer = Buffer.alloc(MAXSIZE);
115+
let buffer = Buffer.allocUnsafe(MAXSIZE);
116116

117117
/**
118118
* Sets the size of the internal serialization buffer.

src/decimal128.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Buffer } from 'buffer';
22
import { BSONTypeError } from './error';
33
import { Long } from './long';
4+
import { isUint8Array } from './parser/utils';
45

56
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
67
const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -132,8 +133,14 @@ export class Decimal128 {
132133

133134
if (typeof bytes === 'string') {
134135
this.bytes = Decimal128.fromString(bytes).bytes;
136+
} else if (isUint8Array(bytes)) {
137+
if (bytes.byteLength === 16) {
138+
this.bytes = bytes;
139+
} else {
140+
throw new BSONTypeError('Decimal128 must take a Buffer of 16 bytes');
141+
}
135142
} else {
136-
this.bytes = bytes;
143+
throw new BSONTypeError('Decimal128 must take a Buffer or string');
137144
}
138145
}
139146

test/node/decimal128_tests.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
// const { BSONTypeError } = require('../register-bson');
34
const BSON = require('../register-bson');
45
const Decimal128 = BSON.Decimal128;
56

@@ -1205,7 +1206,7 @@ describe('Decimal128', function () {
12051206
done();
12061207
});
12071208

1208-
it('accepts strings in the constructor', function (done) {
1209+
it('accepts strings in the constructor', () => {
12091210
expect(new Decimal128('0').toString()).to.equal('0');
12101211
expect(new Decimal128('00').toString()).to.equal('0');
12111212
expect(new Decimal128('0.5').toString()).to.equal('0.5');
@@ -1216,6 +1217,24 @@ describe('Decimal128', function () {
12161217
expect(new Decimal128('0.0011').toString()).to.equal('0.0011');
12171218
expect(new Decimal128('0.00110').toString()).to.equal('0.00110');
12181219
expect(new Decimal128('-1e400').toString()).to.equal('-1E+400');
1219-
done();
1220+
});
1221+
1222+
it.only('throws appropriate error for invalid constructor arguments', () => {
1223+
const byteLengthErrMsg = 'Decimal128 must take a Buffer of 16 bytes';
1224+
const constructorArgErrMsg = 'Decimal128 must take a Buffer or string';
1225+
1226+
expect(() => new Decimal128(-0)).to.throw(constructorArgErrMsg);
1227+
expect(() => new Decimal128(-1)).to.throw(constructorArgErrMsg);
1228+
expect(() => new Decimal128(10)).to.throw(constructorArgErrMsg);
1229+
expect(() => new Decimal128(1111111111111111)).to.throw(constructorArgErrMsg);
1230+
1231+
expect(() => new Decimal128(new Uint8Array(0))).to.throw(byteLengthErrMsg);
1232+
expect(() => new Decimal128(Buffer.alloc(0))).to.throw(byteLengthErrMsg);
1233+
expect(() => new Decimal128(new Uint8Array(3))).to.throw(byteLengthErrMsg);
1234+
expect(() => new Decimal128(Buffer.alloc(3))).to.throw(byteLengthErrMsg);
1235+
expect(() => new Decimal128(new Uint8Array(17))).to.throw(byteLengthErrMsg);
1236+
expect(() => new Decimal128(Buffer.alloc(17))).to.throw(byteLengthErrMsg);
1237+
new Decimal128(Buffer.alloc(16));
1238+
new Decimal128(new Uint8Array(16));
12201239
});
12211240
});

0 commit comments

Comments
 (0)