Skip to content

Commit

Permalink
feat: add some helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 committed May 13, 2024
1 parent cc8c98f commit f8c8991
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* c8 ignore start */
export { withSign, isWithSign, omitSign } from './utils'
export { withSign, isWithSign, omitSign, jsonParse, jsonStringify } from './utils'
export { SIGN } from './constants'

export { isHeartbeatRequest, getHeartbeatResponse } from './heartbeat'
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import destr from 'destr'
import { SIGN } from './constants'
import type { ResolvedWSHEConfig, ResolvedWSHEHeartbeatConfig, WSHEConfig } from './types'

Expand Down Expand Up @@ -67,3 +68,24 @@ export function isWithSign(data: string): boolean {
export function omitSign(data: string): string {
return data.slice(SIGN.length)
}

/* c8 ignore start */
export function jsonStringify(data: any): string {
try {
return JSON.stringify(data)
}
catch (e) {
return ''
}
}

export function jsonParse<T>(data: string, onError?: () => void): T {
try {
return destr<T>(data)
}
catch {
onError?.()
return {} as T
}
}
/* c8 ignore stop */

0 comments on commit f8c8991

Please sign in to comment.