Skip to content

Commit

Permalink
uses base64 instead of hex encoding, inspects encode param, updates d…
Browse files Browse the repository at this point in the history
…iscover v2 & explore v2
  • Loading branch information
AlexandrosGounis committed Jan 11, 2024
1 parent adfa901 commit 45bfc45
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/discover/handle-discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function handleDiscover(request: Request, env: Env, _ctx: Execution
if (version === VERSIONS.V1) {
responsePayload = JSON.stringify(discoverData)
} else {
responsePayload = JSON.stringify(makeResponsePayload(JSON.stringify(discoverData), version, env))
responsePayload = JSON.stringify(makeResponsePayload(JSON.stringify(discoverData), version, env, request.url))
}

return new Response(responsePayload, { headers: HEADERS })
Expand Down
17 changes: 3 additions & 14 deletions src/explore/handle-explore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Env } from '../../worker-configuration'
import { getParams } from '../utils/param-utils'
import { VERSIONS, makeResponsePayload } from '../utils/versioning'
import { Dataset } from "./storage/dataset"

Expand All @@ -23,9 +24,9 @@ export async function handleExpore(request: Request, env: Env, _ctx: ExecutionCo
const version = request.url.includes(VERSIONS.V2) ? VERSIONS.V2 : VERSIONS.V1
const dataset = new Dataset({ version })

delete params.encode
const paramKeys = Object.keys(params)


for (const rawKey of paramKeys) {
if (rawKey === SHOULD_DISPLAY_LINK_KEY) {
dataset.addFilter(o => String(!!o.discover) === params[rawKey])
Expand All @@ -48,7 +49,7 @@ export async function handleExpore(request: Request, env: Env, _ctx: ExecutionCo
if (version === VERSIONS.V1) {
responsePayload = snapshot
} else {
responsePayload = JSON.stringify(makeResponsePayload(snapshot, version, env))
responsePayload = JSON.stringify(makeResponsePayload(snapshot, version, env, request.url))
}

return new Response(responsePayload, { status: 200, headers: HEADERS })
Expand All @@ -58,15 +59,3 @@ export async function handleExpore(request: Request, env: Env, _ctx: ExecutionCo
}
}

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
}
12 changes: 12 additions & 0 deletions src/utils/param-utils.ts
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
}
13 changes: 4 additions & 9 deletions src/utils/text-utils.ts
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')
}
24 changes: 18 additions & 6 deletions src/utils/versioning.ts
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,
}
}

0 comments on commit 45bfc45

Please sign in to comment.