Skip to content

Commit

Permalink
feat: 🎸 Encode long numbers as strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
atoulme committed Feb 3, 2021
1 parent 0ebc842 commit 7ea1d2f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -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]';

Expand All @@ -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)) {
Expand Down Expand Up @@ -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';
}
5 changes: 5 additions & 0 deletions test/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 7ea1d2f

Please sign in to comment.