From 608ec0baed927e511040ae42b6ae4e2654c6a5b5 Mon Sep 17 00:00:00 2001 From: Arham <86415475+arhamj@users.noreply.github.com> Date: Tue, 4 Jun 2024 15:41:24 +0530 Subject: [PATCH] Added option for SN to accept a custom json parser #8 (#4) * feat(encoding): added option for SN to accept a custom json parser * fix(json): using optional param in funtion --- src/index.ts | 8 ++++---- src/types.ts | 1 + src/util/Encoding.ts | 28 ++++------------------------ 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/index.ts b/src/index.ts index a37b0e1..94eaacd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ import { TimeoutCallback, validateSnOpts, } from './types' -import { base64BufferReviver, stringifyData } from './util/Encoding' +import { jsonParse, jsonStringify } from './util/Encoding' import { NewNumberHistogram } from './util/Histogram' import { logMessageInfo } from './util/Log' import { TTLMap } from './util/TTLMap' @@ -141,9 +141,9 @@ export const Sn = (opts: SnOpts) => { }, awaitProcessing: boolean = true ) => { - const stringifiedData = stringifyData(augData, opts.customStringifier) + const stringifiedData = jsonStringify(augData, opts.customStringifier) const stringifiedHeader = optionalHeader - ? stringifyData(optionalHeader.headerData, opts.customStringifier) + ? jsonStringify(optionalHeader.headerData, opts.customStringifier) : null /* prettier-ignore */ if(logFlags.net_verbose) logMessageInfo(augData, stringifiedData) @@ -364,7 +364,7 @@ export const Sn = (opts: SnOpts) => { sign?: Sign ) => { // [TODO] Secure this with validation - let augData: AugmentedData = JSON.parse(augDataStr, base64BufferReviver) + let augData: AugmentedData = jsonParse(augDataStr, opts.customJsonParser) //here we will log the received message. note we exploit an aspect of augData //that the data part is the first value and will be close enough to the start ot the string diff --git a/src/types.ts b/src/types.ts index b995221..273844a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,6 +55,7 @@ export type SnOpts = { sendHeaderVersion: number } customStringifier?: (val) => string + customJsonParser?: (value: string) => any crypto: { hashKey: string signingSecretKeyHex: string diff --git a/src/util/Encoding.ts b/src/util/Encoding.ts index 0044c55..8d458b5 100644 --- a/src/util/Encoding.ts +++ b/src/util/Encoding.ts @@ -1,27 +1,7 @@ -export const isObject = (val) => { - if (val === null) { - return false - } - if (Array.isArray(val)) { - return false - } - return typeof val === 'function' || typeof val === 'object' +export const jsonStringify = (data: T, customStringify?: (data: T) => string): string => { + return customStringify ? customStringify(data) : JSON.stringify(data) } -export function base64BufferReviver(key: string, value: any) { - const originalObject = value - if ( - isObject(originalObject) && - originalObject.hasOwnProperty('dataType') && - originalObject.dataType && - originalObject.dataType == 'bh' - ) { - return Buffer.from(originalObject.data, 'base64') - } else { - return value - } -} - -export const stringifyData = (data: T, customStringifier?: (data: T) => string): string => { - return customStringifier ? customStringifier(data) : JSON.stringify(data) +export const jsonParse = (data: string, customParser?: (data: string) => T): T => { + return customParser ? customParser(data) : JSON.parse(data) }