From a0edc031ccf7ee7f9e4d3cc0a2150bd52afc5182 Mon Sep 17 00:00:00 2001 From: Andrew Atwood Date: Thu, 5 Sep 2024 13:06:40 -0500 Subject: [PATCH] Add utility functions for custom script builder --- server/meta.jsx | 91 ++++++++++++++++++++++++++------------------- server/meta.test.js | 66 +++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 39 deletions(-) diff --git a/server/meta.jsx b/server/meta.jsx index 0e4ed4e4..a8b77366 100644 --- a/server/meta.jsx +++ b/server/meta.jsx @@ -24,8 +24,15 @@ import { } from "./constants"; import { constHas, entries } from "./util"; +type SDKAttributes = {| + [string]: string | boolean, +|}; + type SDKMeta = {| getSDKLoader: (options?: {| baseURL?: string, nonce?: string |}) => string, + getSDKScriptAttributes: () => SDKAttributes | void, + getSDKScriptUrl: () => string | void, + getSDKInlineScript: (baseURL?: string) => string | void, |}; const emailRegex = /^.+@.+$/; @@ -239,10 +246,6 @@ function validateSDKUrl(sdkUrl: string) { } } -type SDKAttributes = {| - [string]: string | boolean, -|}; - const getDefaultSDKAttributes = (): SDKAttributes => { // $FlowFixMe return {}; @@ -307,38 +310,8 @@ function sanitizeSDKUrl(sdkUrl: string): string { return sdkUrl; } -export function unpackSDKMeta(sdkMeta?: string): SDKMeta { - const { url, attrs } = sdkMeta - ? JSON.parse( - Buffer.from(decodeURIComponent(sdkMeta), "base64").toString("utf8") - ) - : DEFAULT_SDK_META; - - if (url) { - validateSDKUrl(url); - } - - const getSDKLoader = ({ - baseURL = DEFAULT_LEGACY_SDK_BASE_URL, - nonce = "", - } = {}) => { - if (url) { - const validAttrs = getSDKScriptAttributes(url, attrs); - - // $FlowFixMe - const allAttrs = { - nonce, - src: sanitizeSDKUrl(url), - ...validAttrs, - }; - - return (