-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uses base64 instead of hex encoding, inspects encode param, updates d…
…iscover v2 & explore v2
- Loading branch information
1 parent
adfa901
commit 45bfc45
Showing
5 changed files
with
38 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function getParams(requestUrl: string) { | ||
const url = new URL(requestUrl) | ||
|
||
const urlParams = new URLSearchParams(url.searchParams) | ||
const params: Record<string, string> = {} | ||
|
||
for (const [key, value] of urlParams) { | ||
params[key] = value | ||
} | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
export function stringToHex(input: string) { | ||
let hex | ||
let result = "" | ||
// @ts-nocheck | ||
import { Buffer } from 'node:buffer' | ||
|
||
for (let i = 0; i < input.length; i++) { | ||
hex = input.charCodeAt(i).toString(16) | ||
result += ("000" + hex).slice(-4) | ||
} | ||
|
||
return result | ||
export function stringToBase64(input: string) { | ||
return Buffer.from(input).toString('base64') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import { Env } from "../../worker-configuration" | ||
import { signData } from "./encrypt" | ||
import { stringToHex } from "./text-utils" | ||
import { getParams } from "./param-utils" | ||
import { stringToBase64 } from "./text-utils" | ||
|
||
export enum VERSIONS { | ||
V1 = "/v1/", | ||
V2 = "/v2/" | ||
} | ||
|
||
export function makeResponsePayload(payload: any, version: VERSIONS, env: Env) { | ||
const ENCODE_ENABLED_OPTIONS = new Set(["", "true"]) | ||
|
||
export function makeResponsePayload(payload: any, version: VERSIONS, env: Env, requestUrl: string) { | ||
if (version === VERSIONS.V1) { | ||
return payload | ||
} | ||
|
||
const hexData = stringToHex(payload) | ||
const { encode } = getParams(requestUrl) | ||
const shouldEncode = ENCODE_ENABLED_OPTIONS.has(encode) | ||
|
||
let encodedData = payload | ||
let displayData = JSON.parse(payload) | ||
|
||
if (shouldEncode) { | ||
encodedData = stringToBase64(encodedData) | ||
console.log('after encoding::', encodedData) | ||
displayData = encodedData | ||
} | ||
|
||
const { signature } = signData(hexData, env) | ||
const { signature } = signData(encodedData, env) | ||
|
||
return { | ||
data: JSON.parse(payload), | ||
hexData, | ||
data: displayData, | ||
signature, | ||
} | ||
} |