bson 4.6.2 → 4.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bower.json +1 -1
- package/bson.d.ts +1235 -1118
- package/dist/bson.browser.esm.js +113 -145
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +113 -145
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +113 -145
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +110 -145
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +14 -6
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +19 -6
- package/lib/bson.js.map +1 -1
- package/lib/code.js +2 -1
- package/lib/code.js.map +1 -1
- package/lib/db_ref.js +3 -2
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +14 -13
- package/lib/decimal128.js.map +1 -1
- package/lib/double.js +3 -2
- package/lib/double.js.map +1 -1
- package/lib/ensure_buffer.js +1 -1
- package/lib/ensure_buffer.js.map +1 -1
- package/lib/extended_json.js +24 -12
- package/lib/extended_json.js.map +1 -1
- package/lib/int_32.js +2 -1
- package/lib/int_32.js.map +1 -1
- package/lib/long.js +4 -3
- package/lib/long.js.map +1 -1
- package/lib/map.js +1 -1
- package/lib/map.js.map +1 -1
- package/lib/max_key.js +1 -0
- package/lib/max_key.js.map +1 -1
- package/lib/min_key.js +1 -0
- package/lib/min_key.js.map +1 -1
- package/lib/objectid.js +10 -8
- package/lib/objectid.js.map +1 -1
- package/lib/parser/calculate_size.js +10 -9
- package/lib/parser/calculate_size.js.map +1 -1
- package/lib/parser/deserializer.js +13 -10
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +25 -22
- package/lib/parser/serializer.js.map +1 -1
- package/lib/parser/utils.js +23 -19
- package/lib/parser/utils.js.map +1 -1
- package/lib/regexp.js +5 -4
- package/lib/regexp.js.map +1 -1
- package/lib/symbol.js +2 -1
- package/lib/symbol.js.map +1 -1
- package/lib/timestamp.js +7 -4
- package/lib/timestamp.js.map +1 -1
- package/lib/utils/global.js +1 -1
- package/lib/utils/global.js.map +1 -1
- package/lib/uuid.js +9 -9
- package/lib/uuid.js.map +1 -1
- package/lib/uuid_utils.js +1 -1
- package/lib/uuid_utils.js.map +1 -1
- package/package.json +16 -16
- package/src/binary.ts +8 -0
- package/src/code.ts +2 -1
- package/src/db_ref.ts +2 -1
- package/src/decimal128.ts +4 -3
- package/src/double.ts +1 -0
- package/src/extended_json.ts +13 -2
- package/src/int_32.ts +1 -0
- package/src/long.ts +33 -7
- package/src/max_key.ts +1 -0
- package/src/min_key.ts +1 -0
- package/src/objectid.ts +4 -2
- package/src/parser/calculate_size.ts +4 -3
- package/src/parser/deserializer.ts +6 -3
- package/src/parser/serializer.ts +14 -7
- package/src/parser/utils.ts +24 -20
- package/src/regexp.ts +1 -0
- package/src/symbol.ts +1 -0
- package/src/timestamp.ts +5 -2
- package/src/utils/global.ts +1 -1
- package/src/uuid.ts +1 -1
- package/lib/float_parser.js +0 -137
- package/lib/float_parser.js.map +0 -1
- package/src/float_parser.ts +0 -152
package/src/objectid.ts
CHANGED
|
@@ -26,6 +26,7 @@ const kId = Symbol('id');
|
|
|
26
26
|
/**
|
|
27
27
|
* A class representation of the BSON ObjectId type.
|
|
28
28
|
* @public
|
|
29
|
+
* @category BSONType
|
|
29
30
|
*/
|
|
30
31
|
export class ObjectId {
|
|
31
32
|
_bsontype!: 'ObjectID';
|
|
@@ -36,7 +37,7 @@ export class ObjectId {
|
|
|
36
37
|
static cacheHexString: boolean;
|
|
37
38
|
|
|
38
39
|
/** ObjectId Bytes @internal */
|
|
39
|
-
private [kId]
|
|
40
|
+
private [kId]!: Buffer;
|
|
40
41
|
/** ObjectId hexString cache @internal */
|
|
41
42
|
private __id?: string;
|
|
42
43
|
|
|
@@ -71,7 +72,8 @@ export class ObjectId {
|
|
|
71
72
|
// Generate a new id
|
|
72
73
|
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
|
|
73
74
|
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
|
74
|
-
|
|
75
|
+
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
|
|
76
|
+
this[kId] = workingId instanceof Buffer ? workingId : ensureBuffer(workingId);
|
|
75
77
|
} else if (typeof workingId === 'string') {
|
|
76
78
|
if (workingId.length === 12) {
|
|
77
79
|
const bytes = Buffer.from(workingId);
|
|
@@ -121,15 +121,16 @@ function calculateElement(
|
|
|
121
121
|
);
|
|
122
122
|
}
|
|
123
123
|
} else if (value['_bsontype'] === 'Binary') {
|
|
124
|
+
const binary: Binary = value;
|
|
124
125
|
// Check what kind of subtype we have
|
|
125
|
-
if (
|
|
126
|
+
if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
|
|
126
127
|
return (
|
|
127
128
|
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
|
|
128
|
-
(
|
|
129
|
+
(binary.position + 1 + 4 + 1 + 4)
|
|
129
130
|
);
|
|
130
131
|
} else {
|
|
131
132
|
return (
|
|
132
|
-
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (
|
|
133
|
+
(name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1)
|
|
133
134
|
);
|
|
134
135
|
}
|
|
135
136
|
} else if (value['_bsontype'] === 'Symbol') {
|
|
@@ -197,6 +197,7 @@ function deserializeObject(
|
|
|
197
197
|
let isPossibleDBRef = isArray ? false : null;
|
|
198
198
|
|
|
199
199
|
// While we have more left data left keep parsing
|
|
200
|
+
const dataview = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
200
201
|
while (!done) {
|
|
201
202
|
// Read the type
|
|
202
203
|
const elementType = buffer[index++];
|
|
@@ -263,10 +264,10 @@ function deserializeObject(
|
|
|
263
264
|
(buffer[index++] << 16) |
|
|
264
265
|
(buffer[index++] << 24);
|
|
265
266
|
} else if (elementType === constants.BSON_DATA_NUMBER && promoteValues === false) {
|
|
266
|
-
value = new Double(
|
|
267
|
+
value = new Double(dataview.getFloat64(index, true));
|
|
267
268
|
index = index + 8;
|
|
268
269
|
} else if (elementType === constants.BSON_DATA_NUMBER) {
|
|
269
|
-
value =
|
|
270
|
+
value = dataview.getFloat64(index, true);
|
|
270
271
|
index = index + 8;
|
|
271
272
|
} else if (elementType === constants.BSON_DATA_DATE) {
|
|
272
273
|
const lowBits =
|
|
@@ -697,7 +698,7 @@ function deserializeObject(
|
|
|
697
698
|
value = new DBRef(namespace, oid);
|
|
698
699
|
} else {
|
|
699
700
|
throw new BSONError(
|
|
700
|
-
|
|
701
|
+
`Detected unknown BSON type ${elementType.toString(16)} for fieldname "${name}"`
|
|
701
702
|
);
|
|
702
703
|
}
|
|
703
704
|
if (name === '__proto__') {
|
|
@@ -742,9 +743,11 @@ function isolateEval(
|
|
|
742
743
|
functionCache?: { [hash: string]: Function },
|
|
743
744
|
object?: Document
|
|
744
745
|
) {
|
|
746
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
745
747
|
if (!functionCache) return new Function(functionString);
|
|
746
748
|
// Check for cache hit, eval if missing and return cached function
|
|
747
749
|
if (functionCache[functionString] == null) {
|
|
750
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
748
751
|
functionCache[functionString] = new Function(functionString);
|
|
749
752
|
}
|
|
750
753
|
|
package/src/parser/serializer.ts
CHANGED
|
@@ -9,7 +9,6 @@ import type { Double } from '../double';
|
|
|
9
9
|
import { ensureBuffer } from '../ensure_buffer';
|
|
10
10
|
import { BSONError, BSONTypeError } from '../error';
|
|
11
11
|
import { isBSONType } from '../extended_json';
|
|
12
|
-
import { writeIEEE754 } from '../float_parser';
|
|
13
12
|
import type { Int32 } from '../int_32';
|
|
14
13
|
import { Long } from '../long';
|
|
15
14
|
import { Map } from '../map';
|
|
@@ -79,6 +78,12 @@ function serializeString(
|
|
|
79
78
|
return index;
|
|
80
79
|
}
|
|
81
80
|
|
|
81
|
+
const SPACE_FOR_FLOAT64 = new Uint8Array(8);
|
|
82
|
+
const DV_FOR_FLOAT64 = new DataView(
|
|
83
|
+
SPACE_FOR_FLOAT64.buffer,
|
|
84
|
+
SPACE_FOR_FLOAT64.byteOffset,
|
|
85
|
+
SPACE_FOR_FLOAT64.byteLength
|
|
86
|
+
);
|
|
82
87
|
function serializeNumber(
|
|
83
88
|
buffer: Buffer,
|
|
84
89
|
key: string,
|
|
@@ -119,7 +124,8 @@ function serializeNumber(
|
|
|
119
124
|
index = index + numberOfWrittenBytes;
|
|
120
125
|
buffer[index++] = 0;
|
|
121
126
|
// Write float
|
|
122
|
-
|
|
127
|
+
DV_FOR_FLOAT64.setFloat64(0, value, true);
|
|
128
|
+
buffer.set(SPACE_FOR_FLOAT64, index);
|
|
123
129
|
// Adjust index
|
|
124
130
|
index = index + 8;
|
|
125
131
|
}
|
|
@@ -487,7 +493,8 @@ function serializeDouble(
|
|
|
487
493
|
buffer[index++] = 0;
|
|
488
494
|
|
|
489
495
|
// Write float
|
|
490
|
-
|
|
496
|
+
DV_FOR_FLOAT64.setFloat64(0, value.value, true);
|
|
497
|
+
buffer.set(SPACE_FOR_FLOAT64, index);
|
|
491
498
|
|
|
492
499
|
// Adjust index
|
|
493
500
|
index = index + 8;
|
|
@@ -763,7 +770,7 @@ export function serializeInto(
|
|
|
763
770
|
if (Array.isArray(object)) {
|
|
764
771
|
// Get object keys
|
|
765
772
|
for (let i = 0; i < object.length; i++) {
|
|
766
|
-
const key =
|
|
773
|
+
const key = `${i}`;
|
|
767
774
|
let value = object[i];
|
|
768
775
|
|
|
769
776
|
// Is there an override value
|
|
@@ -841,7 +848,7 @@ export function serializeInto(
|
|
|
841
848
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
842
849
|
index = serializeMinMax(buffer, key, value, index, true);
|
|
843
850
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
844
|
-
throw new BSONTypeError(
|
|
851
|
+
throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value['_bsontype'])}`);
|
|
845
852
|
}
|
|
846
853
|
}
|
|
847
854
|
} else if (object instanceof Map || isMap(object)) {
|
|
@@ -942,7 +949,7 @@ export function serializeInto(
|
|
|
942
949
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
943
950
|
index = serializeMinMax(buffer, key, value, index);
|
|
944
951
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
945
|
-
throw new BSONTypeError(
|
|
952
|
+
throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value['_bsontype'])}`);
|
|
946
953
|
}
|
|
947
954
|
}
|
|
948
955
|
} else {
|
|
@@ -1047,7 +1054,7 @@ export function serializeInto(
|
|
|
1047
1054
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
1048
1055
|
index = serializeMinMax(buffer, key, value, index);
|
|
1049
1056
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
1050
|
-
throw new BSONTypeError(
|
|
1057
|
+
throw new BSONTypeError(`Unrecognized or invalid _bsontype: ${String(value['_bsontype'])}`);
|
|
1051
1058
|
}
|
|
1052
1059
|
}
|
|
1053
1060
|
}
|
package/src/parser/utils.ts
CHANGED
|
@@ -33,33 +33,37 @@ declare let window: any;
|
|
|
33
33
|
declare let require: Function;
|
|
34
34
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
35
|
declare let global: any;
|
|
36
|
-
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
declare let process: any; // Used by @rollup/plugin-replace
|
|
37
38
|
|
|
38
39
|
const detectRandomBytes = (): RandomBytesFunction => {
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
if (process.browser) {
|
|
41
|
+
if (typeof window !== 'undefined') {
|
|
42
|
+
// browser crypto implementation(s)
|
|
43
|
+
const target = window.crypto || window.msCrypto; // allow for IE11
|
|
44
|
+
if (target && target.getRandomValues) {
|
|
45
|
+
return size => target.getRandomValues(Buffer.alloc(size));
|
|
46
|
+
}
|
|
44
47
|
}
|
|
45
|
-
}
|
|
46
48
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
|
|
50
|
+
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
|
|
51
|
+
return size => global.crypto.getRandomValues(Buffer.alloc(size));
|
|
52
|
+
}
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
return insecureRandomBytes;
|
|
55
|
+
} else {
|
|
56
|
+
let requiredRandomBytes: RandomBytesFunction | null | undefined;
|
|
57
|
+
try {
|
|
58
|
+
requiredRandomBytes = require('crypto').randomBytes;
|
|
59
|
+
} catch (e) {
|
|
60
|
+
// keep the fallback
|
|
61
|
+
}
|
|
59
62
|
|
|
60
|
-
|
|
63
|
+
// NOTE: in transpiled cases the above require might return null/undefined
|
|
61
64
|
|
|
62
|
-
|
|
65
|
+
return requiredRandomBytes || insecureRandomBytes;
|
|
66
|
+
}
|
|
63
67
|
};
|
|
64
68
|
|
|
65
69
|
export const randomBytes = detectRandomBytes();
|
package/src/regexp.ts
CHANGED
package/src/symbol.ts
CHANGED
package/src/timestamp.ts
CHANGED
|
@@ -19,7 +19,10 @@ export interface TimestampExtended {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
* @category BSONType
|
|
25
|
+
* */
|
|
23
26
|
export class Timestamp extends LongWithoutOverridesClass {
|
|
24
27
|
_bsontype!: 'Timestamp';
|
|
25
28
|
|
|
@@ -41,7 +44,7 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
41
44
|
constructor(low: number, high: number);
|
|
42
45
|
constructor(low: number | Long | { t: number; i: number }, high?: number) {
|
|
43
46
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
44
|
-
|
|
47
|
+
// @ts-expect-error
|
|
45
48
|
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
|
|
46
49
|
|
|
47
50
|
if (Long.isLong(low)) {
|
package/src/utils/global.ts
CHANGED
|
@@ -11,12 +11,12 @@ function checkForMath(potentialGlobal: any) {
|
|
|
11
11
|
|
|
12
12
|
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
|
|
13
13
|
export function getGlobal<T = Record<string, unknown>>(): T {
|
|
14
|
-
// eslint-disable-next-line no-undef
|
|
15
14
|
return (
|
|
16
15
|
checkForMath(typeof globalThis === 'object' && globalThis) ||
|
|
17
16
|
checkForMath(typeof window === 'object' && window) ||
|
|
18
17
|
checkForMath(typeof self === 'object' && self) ||
|
|
19
18
|
checkForMath(typeof global === 'object' && global) ||
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
20
20
|
Function('return this')()
|
|
21
21
|
);
|
|
22
22
|
}
|
package/src/uuid.ts
CHANGED
package/lib/float_parser.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright (c) 2008, Fair Oaks Labs, Inc.
|
|
3
|
-
// All rights reserved.
|
|
4
|
-
//
|
|
5
|
-
// Redistribution and use in source and binary forms, with or without
|
|
6
|
-
// modification, are permitted provided that the following conditions are met:
|
|
7
|
-
//
|
|
8
|
-
// * Redistributions of source code must retain the above copyright notice,
|
|
9
|
-
// this list of conditions and the following disclaimer.
|
|
10
|
-
//
|
|
11
|
-
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
-
// this list of conditions and the following disclaimer in the documentation
|
|
13
|
-
// and/or other materials provided with the distribution.
|
|
14
|
-
//
|
|
15
|
-
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
|
|
16
|
-
// may be used to endorse or promote products derived from this software
|
|
17
|
-
// without specific prior written permission.
|
|
18
|
-
//
|
|
19
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
-
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
-
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
22
|
-
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
23
|
-
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
24
|
-
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
25
|
-
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
26
|
-
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
27
|
-
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
28
|
-
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
29
|
-
// POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
|
|
33
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
-
exports.writeIEEE754 = exports.readIEEE754 = void 0;
|
|
35
|
-
function readIEEE754(buffer, offset, endian, mLen, nBytes) {
|
|
36
|
-
var e;
|
|
37
|
-
var m;
|
|
38
|
-
var bBE = endian === 'big';
|
|
39
|
-
var eLen = nBytes * 8 - mLen - 1;
|
|
40
|
-
var eMax = (1 << eLen) - 1;
|
|
41
|
-
var eBias = eMax >> 1;
|
|
42
|
-
var nBits = -7;
|
|
43
|
-
var i = bBE ? 0 : nBytes - 1;
|
|
44
|
-
var d = bBE ? 1 : -1;
|
|
45
|
-
var s = buffer[offset + i];
|
|
46
|
-
i += d;
|
|
47
|
-
e = s & ((1 << -nBits) - 1);
|
|
48
|
-
s >>= -nBits;
|
|
49
|
-
nBits += eLen;
|
|
50
|
-
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8)
|
|
51
|
-
;
|
|
52
|
-
m = e & ((1 << -nBits) - 1);
|
|
53
|
-
e >>= -nBits;
|
|
54
|
-
nBits += mLen;
|
|
55
|
-
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8)
|
|
56
|
-
;
|
|
57
|
-
if (e === 0) {
|
|
58
|
-
e = 1 - eBias;
|
|
59
|
-
}
|
|
60
|
-
else if (e === eMax) {
|
|
61
|
-
return m ? NaN : (s ? -1 : 1) * Infinity;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
m = m + Math.pow(2, mLen);
|
|
65
|
-
e = e - eBias;
|
|
66
|
-
}
|
|
67
|
-
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
68
|
-
}
|
|
69
|
-
exports.readIEEE754 = readIEEE754;
|
|
70
|
-
function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
|
|
71
|
-
var e;
|
|
72
|
-
var m;
|
|
73
|
-
var c;
|
|
74
|
-
var bBE = endian === 'big';
|
|
75
|
-
var eLen = nBytes * 8 - mLen - 1;
|
|
76
|
-
var eMax = (1 << eLen) - 1;
|
|
77
|
-
var eBias = eMax >> 1;
|
|
78
|
-
var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
|
|
79
|
-
var i = bBE ? nBytes - 1 : 0;
|
|
80
|
-
var d = bBE ? -1 : 1;
|
|
81
|
-
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
82
|
-
value = Math.abs(value);
|
|
83
|
-
if (isNaN(value) || value === Infinity) {
|
|
84
|
-
m = isNaN(value) ? 1 : 0;
|
|
85
|
-
e = eMax;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
e = Math.floor(Math.log(value) / Math.LN2);
|
|
89
|
-
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
90
|
-
e--;
|
|
91
|
-
c *= 2;
|
|
92
|
-
}
|
|
93
|
-
if (e + eBias >= 1) {
|
|
94
|
-
value += rt / c;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
value += rt * Math.pow(2, 1 - eBias);
|
|
98
|
-
}
|
|
99
|
-
if (value * c >= 2) {
|
|
100
|
-
e++;
|
|
101
|
-
c /= 2;
|
|
102
|
-
}
|
|
103
|
-
if (e + eBias >= eMax) {
|
|
104
|
-
m = 0;
|
|
105
|
-
e = eMax;
|
|
106
|
-
}
|
|
107
|
-
else if (e + eBias >= 1) {
|
|
108
|
-
m = (value * c - 1) * Math.pow(2, mLen);
|
|
109
|
-
e = e + eBias;
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
113
|
-
e = 0;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (isNaN(value))
|
|
117
|
-
m = 0;
|
|
118
|
-
while (mLen >= 8) {
|
|
119
|
-
buffer[offset + i] = m & 0xff;
|
|
120
|
-
i += d;
|
|
121
|
-
m /= 256;
|
|
122
|
-
mLen -= 8;
|
|
123
|
-
}
|
|
124
|
-
e = (e << mLen) | m;
|
|
125
|
-
if (isNaN(value))
|
|
126
|
-
e += 8;
|
|
127
|
-
eLen += mLen;
|
|
128
|
-
while (eLen > 0) {
|
|
129
|
-
buffer[offset + i] = e & 0xff;
|
|
130
|
-
i += d;
|
|
131
|
-
e /= 256;
|
|
132
|
-
eLen -= 8;
|
|
133
|
-
}
|
|
134
|
-
buffer[offset + i - d] |= s * 128;
|
|
135
|
-
}
|
|
136
|
-
exports.writeIEEE754 = writeIEEE754;
|
|
137
|
-
//# sourceMappingURL=float_parser.js.map
|
package/lib/float_parser.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"float_parser.js","sourceRoot":"","sources":["../src/float_parser.ts"],"names":[],"mappings":";AAAA,2CAA2C;AAC3C,uBAAuB;AACvB,EAAE;AACF,qEAAqE;AACrE,8EAA8E;AAC9E,EAAE;AACF,4EAA4E;AAC5E,2DAA2D;AAC3D,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,4DAA4D;AAC5D,EAAE;AACF,gFAAgF;AAChF,2EAA2E;AAC3E,gDAAgD;AAChD,EAAE;AACF,8EAA8E;AAC9E,4EAA4E;AAC5E,6EAA6E;AAC7E,4EAA4E;AAC5E,sEAAsE;AACtE,uEAAuE;AACvE,2EAA2E;AAC3E,0EAA0E;AAC1E,0EAA0E;AAC1E,6EAA6E;AAC7E,8BAA8B;AAC9B,EAAE;AACF,EAAE;AACF,+EAA+E;;;AAI/E,SAAgB,WAAW,CACzB,MAAyB,EACzB,MAAc,EACd,MAAwB,EACxB,IAAY,EACZ,MAAc;IAEd,IAAI,CAAS,CAAC;IACd,IAAI,CAAS,CAAC;IACd,IAAM,GAAG,GAAG,MAAM,KAAK,KAAK,CAAC;IAC7B,IAAM,IAAI,GAAG,MAAM,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC;IACnC,IAAM,IAAI,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAM,KAAK,GAAG,IAAI,IAAI,CAAC,CAAC;IACxB,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC;IACf,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAC7B,IAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CA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AC,CAAC;YACvD,CAAC,GAAG,CAAC,CAAC;SACP;KACF;IAED,IAAI,KAAK,CAAC,KAAK,CAAC;QAAE,CAAC,GAAG,CAAC,CAAC;IAExB,OAAO,IAAI,IAAI,CAAC,EAAE;QAChB,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;QAC9B,CAAC,IAAI,CAAC,CAAC;QACP,CAAC,IAAI,GAAG,CAAC;QACT,IAAI,IAAI,CAAC,CAAC;KACX;IAED,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IAEpB,IAAI,KAAK,CAAC,KAAK,CAAC;QAAE,CAAC,IAAI,CAAC,CAAC;IAEzB,IAAI,IAAI,IAAI,CAAC;IAEb,OAAO,IAAI,GAAG,CAAC,EAAE;QACf,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;QAC9B,CAAC,IAAI,CAAC,CAAC;QACP,CAAC,IAAI,GAAG,CAAC;QACT,IAAI,IAAI,CAAC,CAAC;KACX;IAED,MAAM,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;AACpC,CAAC;AA5ED,oCA4EC"}
|
package/src/float_parser.ts
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2008, Fair Oaks Labs, Inc.
|
|
2
|
-
// All rights reserved.
|
|
3
|
-
//
|
|
4
|
-
// Redistribution and use in source and binary forms, with or without
|
|
5
|
-
// modification, are permitted provided that the following conditions are met:
|
|
6
|
-
//
|
|
7
|
-
// * Redistributions of source code must retain the above copyright notice,
|
|
8
|
-
// this list of conditions and the following disclaimer.
|
|
9
|
-
//
|
|
10
|
-
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
11
|
-
// this list of conditions and the following disclaimer in the documentation
|
|
12
|
-
// and/or other materials provided with the distribution.
|
|
13
|
-
//
|
|
14
|
-
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
|
|
15
|
-
// may be used to endorse or promote products derived from this software
|
|
16
|
-
// without specific prior written permission.
|
|
17
|
-
//
|
|
18
|
-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
-
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
-
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
-
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
22
|
-
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
23
|
-
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
24
|
-
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
25
|
-
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
26
|
-
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
27
|
-
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
28
|
-
// POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
|
|
32
|
-
|
|
33
|
-
type NumericalSequence = { [index: number]: number };
|
|
34
|
-
|
|
35
|
-
export function readIEEE754(
|
|
36
|
-
buffer: NumericalSequence,
|
|
37
|
-
offset: number,
|
|
38
|
-
endian: 'big' | 'little',
|
|
39
|
-
mLen: number,
|
|
40
|
-
nBytes: number
|
|
41
|
-
): number {
|
|
42
|
-
let e: number;
|
|
43
|
-
let m: number;
|
|
44
|
-
const bBE = endian === 'big';
|
|
45
|
-
const eLen = nBytes * 8 - mLen - 1;
|
|
46
|
-
const eMax = (1 << eLen) - 1;
|
|
47
|
-
const eBias = eMax >> 1;
|
|
48
|
-
let nBits = -7;
|
|
49
|
-
let i = bBE ? 0 : nBytes - 1;
|
|
50
|
-
const d = bBE ? 1 : -1;
|
|
51
|
-
let s = buffer[offset + i];
|
|
52
|
-
|
|
53
|
-
i += d;
|
|
54
|
-
|
|
55
|
-
e = s & ((1 << -nBits) - 1);
|
|
56
|
-
s >>= -nBits;
|
|
57
|
-
nBits += eLen;
|
|
58
|
-
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
59
|
-
|
|
60
|
-
m = e & ((1 << -nBits) - 1);
|
|
61
|
-
e >>= -nBits;
|
|
62
|
-
nBits += mLen;
|
|
63
|
-
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
64
|
-
|
|
65
|
-
if (e === 0) {
|
|
66
|
-
e = 1 - eBias;
|
|
67
|
-
} else if (e === eMax) {
|
|
68
|
-
return m ? NaN : (s ? -1 : 1) * Infinity;
|
|
69
|
-
} else {
|
|
70
|
-
m = m + Math.pow(2, mLen);
|
|
71
|
-
e = e - eBias;
|
|
72
|
-
}
|
|
73
|
-
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function writeIEEE754(
|
|
77
|
-
buffer: NumericalSequence,
|
|
78
|
-
value: number,
|
|
79
|
-
offset: number,
|
|
80
|
-
endian: 'big' | 'little',
|
|
81
|
-
mLen: number,
|
|
82
|
-
nBytes: number
|
|
83
|
-
): void {
|
|
84
|
-
let e: number;
|
|
85
|
-
let m: number;
|
|
86
|
-
let c: number;
|
|
87
|
-
const bBE = endian === 'big';
|
|
88
|
-
let eLen = nBytes * 8 - mLen - 1;
|
|
89
|
-
const eMax = (1 << eLen) - 1;
|
|
90
|
-
const eBias = eMax >> 1;
|
|
91
|
-
const rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
|
|
92
|
-
let i = bBE ? nBytes - 1 : 0;
|
|
93
|
-
const d = bBE ? -1 : 1;
|
|
94
|
-
const s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
95
|
-
|
|
96
|
-
value = Math.abs(value);
|
|
97
|
-
|
|
98
|
-
if (isNaN(value) || value === Infinity) {
|
|
99
|
-
m = isNaN(value) ? 1 : 0;
|
|
100
|
-
e = eMax;
|
|
101
|
-
} else {
|
|
102
|
-
e = Math.floor(Math.log(value) / Math.LN2);
|
|
103
|
-
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
104
|
-
e--;
|
|
105
|
-
c *= 2;
|
|
106
|
-
}
|
|
107
|
-
if (e + eBias >= 1) {
|
|
108
|
-
value += rt / c;
|
|
109
|
-
} else {
|
|
110
|
-
value += rt * Math.pow(2, 1 - eBias);
|
|
111
|
-
}
|
|
112
|
-
if (value * c >= 2) {
|
|
113
|
-
e++;
|
|
114
|
-
c /= 2;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (e + eBias >= eMax) {
|
|
118
|
-
m = 0;
|
|
119
|
-
e = eMax;
|
|
120
|
-
} else if (e + eBias >= 1) {
|
|
121
|
-
m = (value * c - 1) * Math.pow(2, mLen);
|
|
122
|
-
e = e + eBias;
|
|
123
|
-
} else {
|
|
124
|
-
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
125
|
-
e = 0;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (isNaN(value)) m = 0;
|
|
130
|
-
|
|
131
|
-
while (mLen >= 8) {
|
|
132
|
-
buffer[offset + i] = m & 0xff;
|
|
133
|
-
i += d;
|
|
134
|
-
m /= 256;
|
|
135
|
-
mLen -= 8;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
e = (e << mLen) | m;
|
|
139
|
-
|
|
140
|
-
if (isNaN(value)) e += 8;
|
|
141
|
-
|
|
142
|
-
eLen += mLen;
|
|
143
|
-
|
|
144
|
-
while (eLen > 0) {
|
|
145
|
-
buffer[offset + i] = e & 0xff;
|
|
146
|
-
i += d;
|
|
147
|
-
e /= 256;
|
|
148
|
-
eLen -= 8;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
buffer[offset + i - d] |= s * 128;
|
|
152
|
-
}
|