From 7ea1d2fb57ac77b6c40d160d4e6a86c96ec57dbf Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Tue, 2 Feb 2021 14:37:04 -0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Encode=20long=20numbers?= =?UTF-8?q?=20as=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Longs are encoded as objects (see https://github.com/dcodeIO/long.js). This change serializes longs to strings so they become easier to index and query. BREAKING CHANGE: 🧨 This changes the structure of the data sent to Splunk. --- package.json | 1 + src/convert.ts | 9 +++++++++ test/convert.test.ts | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/package.json b/package.json index 74f2a04..8cc5c8c 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "js-yaml": "^3.13.1", "lodash": "^4.17.15", "markdown-table": "^1.1.3", + "long": "^4.0.0", "oclif": "^1.15.2", "protobufjs": "~6.8.8", "typescript": "^3.7.3" diff --git a/src/convert.ts b/src/convert.ts index 9bed85f..4784920 100644 --- a/src/convert.ts +++ b/src/convert.ts @@ -1,3 +1,5 @@ +import * as Long from 'long'; + const isPlainObject = (obj: any): obj is Object => typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'; @@ -13,6 +15,9 @@ export function convertBuffers(obj: Buffer | Object | any | undefined, path: str return { hex: obj.toString('hex') }; } } + if (isLong(obj)) { + return new Long(obj.low, obj.high, obj.unsigned).toString(10); + } if (Array.isArray(obj)) { return obj.map((v, i) => convertBuffers(v, [...path, String(i)])); } else if (isPlainObject(obj)) { @@ -47,3 +52,7 @@ export function isLikelyText(buffer: Buffer): boolean { export function toText(buffer: Buffer): string { return buffer.toString('utf-8'); } + +function isLong(obj: any): boolean { + return typeof obj.low === 'number' && typeof obj.high === 'number'; +} diff --git a/test/convert.test.ts b/test/convert.test.ts index 896fb71..18dbd03 100644 --- a/test/convert.test.ts +++ b/test/convert.test.ts @@ -52,6 +52,11 @@ describe('convertBuffers', () => { } `); }); + + it('should convert Longs to strings', () => { + expect(convertBuffers({ low: 4, high: 0, unsigned: false })).toMatchInlineSnapshot(`"4"`); + expect(convertBuffers({ low: 4, high: 32, unsigned: false })).toMatchInlineSnapshot(`"137438953476"`); + }); }); describe('isLikelyText', () => {