diff --git a/packages/cbor/lib/encoder.js b/packages/cbor/lib/encoder.js index 63279595..dc85ea59 100644 --- a/packages/cbor/lib/encoder.js +++ b/packages/cbor/lib/encoder.js @@ -116,6 +116,7 @@ function parseDateType(str) { * @property {boolean} [omitUndefinedProperties=false] When encoding * objects or Maps, do not include a key if its corresponding value is * `undefined`. + * @property {boolean} [useFloat32bit=false] Force float to use 32bit encoding. */ /** @@ -140,6 +141,7 @@ class Encoder extends stream.Transform { detectLoops = false, omitUndefinedProperties = false, genTypes = [], + useFloat32bit, ...superOpts } = options @@ -154,6 +156,7 @@ class Encoder extends stream.Transform { this.disallowUndefinedKeys = disallowUndefinedKeys this.dateType = parseDateType(dateType) this.collapseBigIntegers = this.canonical ? true : collapseBigIntegers + this.useFloat32bit = useFloat32bit /** @type {WeakSet?} */ this.detectLoops = undefined @@ -294,10 +297,13 @@ class Encoder extends stream.Transform { return this._pushUInt8(HALF) && this.push(b2) } } + + if (this.useFloat32bit) { + return this._pushUInt8(FLOAT) && this._pushFloatBE(obj) + } if (Math.fround(obj) === obj) { return this._pushUInt8(FLOAT) && this._pushFloatBE(obj) } - return this._pushUInt8(DOUBLE) && this._pushDoubleBE(obj) } diff --git a/packages/cbor/package.json b/packages/cbor/package.json index 33359f91..74a9729e 100644 --- a/packages/cbor/package.json +++ b/packages/cbor/package.json @@ -42,7 +42,8 @@ "Denis Lapaev (http://lapaev.me/)", "Ruben Bridgewater ", "Burt Harris ", - "Jakub Arbet (https://jakubarbet.me/)" + "Jakub Arbet (https://jakubarbet.me/)", + "John Paul Rabanal " ], "types": "./types/lib/cbor.d.ts", "dependencies": { diff --git a/packages/cbor/types/lib/encoder.d.ts b/packages/cbor/types/lib/encoder.d.ts index 09840ff0..3a79364b 100644 --- a/packages/cbor/types/lib/encoder.d.ts +++ b/packages/cbor/types/lib/encoder.d.ts @@ -38,6 +38,8 @@ export = Encoder; * @property {boolean} [omitUndefinedProperties=false] When encoding * objects or Maps, do not include a key if its corresponding value is * `undefined`. + * + * @property {boolean} [useFloat32bit=false] Force float to use 32bit encoding. */ /** * Transform JavaScript values into CBOR bytes. The `Writable` side of @@ -454,6 +456,12 @@ type EncodingOptions = { * `undefined`. */ omitUndefinedProperties?: boolean; + + /** + * Force 32bit encoding for floats + */ + + useFloat32bit?: boolean }; /** * A mapping from tag number to a tag decoding function.