Skip to content

Commit

Permalink
Added option for SN to accept a custom json parser #8 (#4)
Browse files Browse the repository at this point in the history
* feat(encoding): added option for SN to accept a custom json parser

* fix(json): using optional param in funtion
  • Loading branch information
arhamj authored Jun 4, 2024
1 parent 138ac4c commit 608ec0b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type SnOpts = {
sendHeaderVersion: number
}
customStringifier?: (val) => string
customJsonParser?: (value: string) => any

Check warning on line 58 in src/types.ts

View workflow job for this annotation

GitHub Actions / ci-main / merge-checks

Unexpected any. Specify a different type
crypto: {
hashKey: string
signingSecretKeyHex: string
Expand Down
28 changes: 4 additions & 24 deletions src/util/Encoding.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(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 = <T>(data: T, customStringifier?: (data: T) => string): string => {
return customStringifier ? customStringifier(data) : JSON.stringify(data)
export const jsonParse = <T>(data: string, customParser?: (data: string) => T): T => {
return customParser ? customParser(data) : JSON.parse(data)
}

0 comments on commit 608ec0b

Please sign in to comment.