From 8bb3a1208c8ae89ff84b8e4e5345cc6294092ba1 Mon Sep 17 00:00:00 2001 From: John Paul Rabanal Date: Fri, 19 Aug 2022 13:44:57 +0800 Subject: [PATCH 1/2] Added encoding option to use 32bit encoding --- packages/cbor/lib/encoder.js | 8 +++++++- packages/cbor/types/lib/encoder.d.ts | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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/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. From 4828cb685a327ea7fe2544b5884495cc60fd6fbd Mon Sep 17 00:00:00 2001 From: John Paul Rabanal Date: Mon, 22 Aug 2022 14:28:40 +0800 Subject: [PATCH 2/2] Updated contributors --- packages/cbor/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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": {