Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for SN to accept a custom json parser #8 #4

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)
}
Loading