Skip to content

Commit

Permalink
deepLinkCreate and deepLinkParse now base64 encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Campbell authored and Cameron Campbell committed Aug 19, 2024
1 parent 261a1ca commit 376c1d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "openblox",
"description": "Roblox API Wrapper For Both Classic And OpenCloud APIs.",
"type": "commonjs",
"version": "1.0.42",
"version": "1.0.43",
"license": "MIT",
"bugs": {
"url": "https://github.com/MightyPart/openblox/issues"
Expand Down
28 changes: 13 additions & 15 deletions src/helpers/deepLinkHelpers/createDeepLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,42 @@ type DeepLinkMethods<
//////////////////////////////////////////////////////////////////////////////////


// [ Variables ] /////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////


// [ Private Functions ] /////////////////////////////////////////////////////////
const toAppsFlyerFormat = (placeId: Identifier, launchDataStr: string | undefined) => (
const toAppsFlyerFormat = (placeId: Identifier, launchDataStr: string | null) => (
new DeepLinkString(
`ro.blox.com/Ebh5?af_dp=roblox%3A%2F%2FplaceId%3D${placeId}${launchDataStr ? `%26launchData%3D${launchDataStr}` : ""}&af_web_dp=https%3A%2F%2Fwww.roblox.com%2Fgames%2Fstart%3FplaceId%3D${placeId}${launchDataStr ? `%26launchData%3D${launchDataStr}` : ""}`,
placeId, launchDataStr, toAppsFlyerFormat
)
)

const toRobloxProtocolFormat = (placeId: Identifier, launchDataStr: string | undefined) => (
const toRobloxProtocolFormat = (placeId: Identifier, launchDataStr: string | null) => (
new DeepLinkString(`roblox://placeId=${placeId}${launchDataStr ? `&launchData=${launchDataStr}` : ""}`, placeId, launchDataStr, toRobloxProtocolFormat)
)

const toRobloxUrlFormat = (placeId: Identifier, launchDataStr: string | undefined) => (
const toRobloxUrlFormat = (placeId: Identifier, launchDataStr: string | null) => (
new DeepLinkString(
`https://www.roblox.com/games/start?placeId=${placeId}${launchDataStr ? `&launchData=${launchDataStr}` : ""}`,
placeId, launchDataStr, toRobloxUrlFormat
)
)

const stringifyLaunchData = (launchData: string | Record<any, any> | undefined) => (
typeof launchData === "string" ? launchData.length == 0 ? undefined : encodeURIComponent(launchData) :
launchData?.constructor === Object ? encodeURIComponent(JSON.stringify(launchData)) :
undefined
) as string | undefined
const stringifyLaunchData = (launchData: string | Record<any, any> | undefined | null): string | null => {
if (!launchData) return null

if (typeof launchData === "string") return launchData.length == 0 ? null : encodeURIComponent(btoa(launchData))
if (launchData?.constructor === Object) return encodeURIComponent(btoa(JSON.stringify(launchData)))
return null
}
//////////////////////////////////////////////////////////////////////////////////


class DeepLinkString extends String {
str: string
placeId: Identifier
launchData: string | undefined
reFormat: (placeId: Identifier, launchData: string | undefined) => any
launchData: string | null
reFormat: (placeId: Identifier, launchData: string | null) => any

constructor(str: string, placeId: Identifier, launchData: string | undefined, reFormat: (placeId: Identifier, launchData: string | undefined) => any) {
constructor(str: string, placeId: Identifier, launchData: string | null, reFormat: (placeId: Identifier, launchData: string | null) => any) {
super(str);
this.str = str;

Expand Down
12 changes: 9 additions & 3 deletions src/helpers/deepLinkHelpers/parseDeepLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ type DeepLinkParseReturns<DeepLink extends any, PlaceId, LaunchData extends bool


// [ Variables ] /////////////////////////////////////////////////////////////////
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/

const placeIdsRegex = /placeId=([0-9]+)(?:.*placeId=([0-9]+))?/
const launchDataRegex = /launchData=([^&]+)(?:.*launchData=([^&]+))?/
//////////////////////////////////////////////////////////////////////////////////


// [ Private Functions ] /////////////////////////////////////////////////////////
const parseLaunchData = (launchData: string | null | undefined) => {
const parseLaunchData = (launchData: string | null | undefined, decodeBase64?: boolean) => {
if (!launchData) return null
launchData = decodeURIComponent(launchData)

if (decodeBase64 === undefined || decodeBase64 === true) {
if (base64Regex.test(launchData)) launchData = atob(launchData)
}

try { return JSON.parse(launchData) }
catch { return launchData }
}
Expand All @@ -67,7 +73,7 @@ export const deepLinkParse = <
> : never),
PlaceIds extends (ExtractedData extends [ infer PlaceIds, boolean[] ] ? PlaceIds : never),
LaunchData extends (ExtractedData extends [ string[], infer HasLaunchData ] ? HasLaunchData : never)
>(deepLink: DeepLink): DeepLinkParseReturns<DeepLink, PlaceIds, LaunchData> => {
>(deepLink: DeepLink, decodeBase64?: boolean): DeepLinkParseReturns<DeepLink, PlaceIds, LaunchData> => {
const decodedDeepLink = deepLink.replaceAll("%3D", "=")

const placeIdsExec = placeIdsRegex.exec(decodedDeepLink)
Expand All @@ -78,5 +84,5 @@ export const deepLinkParse = <
const firstLaunchData = launchDataExec?.[1], secondLaunchData = launchDataExec?.[2]
const launchData = [ typeof firstLaunchData === "string" ? firstLaunchData : null, typeof secondLaunchData === "string" ? secondLaunchData : null ]

return placeIds.map((placeId, idx) => ({ placeId, launchData: parseLaunchData(launchData[idx]) })) as DeepLinkParseReturns<DeepLink, PlaceIds, LaunchData>
return placeIds.map((placeId, idx) => ({ placeId, launchData: parseLaunchData(launchData[idx], decodeBase64) })) as DeepLinkParseReturns<DeepLink, PlaceIds, LaunchData>
}

0 comments on commit 376c1d7

Please sign in to comment.