diff options
Diffstat (limited to 'packages/server/src')
-rw-r--r-- | packages/server/src/helpers/decodeCbor.ts | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/packages/server/src/helpers/decodeCbor.ts b/packages/server/src/helpers/decodeCbor.ts index 37e8ab2..e2bae2c 100644 --- a/packages/server/src/helpers/decodeCbor.ts +++ b/packages/server/src/helpers/decodeCbor.ts @@ -1,24 +1,20 @@ -import cbor from 'cbor'; +/* eslint-disable @typescript-eslint/ban-ts-comment */ +import * as cborx from 'cbor-x'; -export function decodeCborFirst(input: string | Buffer | ArrayBufferView): any { - try { - // throws if there are extra bytes - return cbor.decodeFirstSync(input); - } catch (err) { - const _err = err as CborDecoderError; - // if the error was due to extra bytes, return the unpacked value - if (_err.value) { - return _err.value; - } - throw err; +export function decodeCborFirst(input: Uint8Array): any { + const decoded = cborx.decodeMultiple(input); + + if (decoded === undefined) { + throw new Error('CBOR input data was empty'); } -} -/** - * Intuited from a quick scan of `cbor.decodeFirstSync()` here: - * - * https://github.com/hildjj/node-cbor/blob/v5.1.0/lib/decoder.js#L189 - */ -class CborDecoderError extends Error { - value: any; + /** + * Typing on `decoded` is `void | []` which causes TypeScript to think that it's an empty array, + * and thus you can't destructure it. I'm ignoring that because the code works fine in JS, and + * so this should be a valid operation. + */ + // @ts-ignore 2493 + const [first] = decoded; + + return first; } |