Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose utility functions for custom script builder usage #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 53 additions & 38 deletions server/meta.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /^.+@.+$/;
Expand Down Expand Up @@ -239,10 +246,6 @@ function validateSDKUrl(sdkUrl: string) {
}
}

type SDKAttributes = {|
[string]: string | boolean,
|};

const getDefaultSDKAttributes = (): SDKAttributes => {
// $FlowFixMe
return {};
Expand Down Expand Up @@ -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 (<script {...allAttrs} />).render(html());
}

return (
<script
nonce={nonce}
innerHTML={`
function getSDKInlineScript(baseURL: string): string {
return `
(function() {
function loadScript(url, attributes) {
var scriptTag = '<scr' + 'ipt src="' + url + '" ' + (attributes || '') + '></scr' + 'ipt>';
Expand Down Expand Up @@ -395,12 +368,54 @@ export function unpackSDKMeta(sdkMeta?: string): SDKMeta {
return;
}
})();
`}
/>
`;
}

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 (<script {...allAttrs} />).render(html());
}

return (
<script nonce={nonce} innerHTML={getSDKInlineScript(baseURL)} />
).render(html());
};

const hasInlineScript = !url;

return {
getSDKLoader,
getSDKScriptAttributes: () => {
return hasInlineScript ? undefined : getSDKScriptAttributes(url, attrs);
},
getSDKScriptUrl: () => {
return hasInlineScript ? undefined : sanitizeSDKUrl(url);
},
getSDKInlineScript: (baseURL = DEFAULT_LEGACY_SDK_BASE_URL) => {
return hasInlineScript ? getSDKInlineScript(baseURL) : undefined;
},
};
}
66 changes: 65 additions & 1 deletion server/meta.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint max-lines: off */

import cheerio from "cheerio";
import { test, afterEach } from "vitest";
import { describe, expect, test, afterEach } from "vitest";

import { unpackSDKMeta } from ".";

Expand Down Expand Up @@ -1399,3 +1399,67 @@ test("should error when the origin parameter is not just the origin", () => {
throw new Error("Expected error to be thrown");
}
});

describe("Utilities", () => {
Copy link
Author

@andrewatwood andrewatwood Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than checking every utility every time, I figured we could just check that they're equal and go from there. But it might not hurt to add specific assertions for each utility to each test case.

I started there, but bailed when it ended up being so many cases that it seemed redundant.

test("getSDKScriptUrl() matches sdkScript src", () => {
const sdkUrl = "https://www.paypal.com/sdk/js?client-id=foo";

const { getSDKLoader, getSDKScriptUrl } = unpackSDKMeta(
Buffer.from(
JSON.stringify({
url: sdkUrl,
})
).toString("base64")
);

const $ = cheerio.load(getSDKLoader());
const src = $("script").attr("src");

const sdkScriptUrl = getSDKScriptUrl();

expect(sdkScriptUrl).toBe(sdkUrl);
expect(sdkScriptUrl).toBe(src);
});

test("getSDKScriptAttributes() matches sdkScript data-attributes", () => {
const sdkUrl = "https://www.paypal.com/sdk/js?client-id=foo";

const attrs = {
"data-uid": "uid",
"data-popups-disabled": "true",
"data-dummy-id": "begone",
};

const { getSDKLoader, getSDKScriptAttributes } = unpackSDKMeta(
Buffer.from(
JSON.stringify({
url: sdkUrl,
attrs,
})
).toString("base64")
);

const $ = cheerio.load(getSDKLoader());
const scriptTag = $("script");

const result = getSDKScriptAttributes();

expect(result).toBeDefined();
expect(result).not.toHaveProperty("data-dummy-id");
expect(attrs).toMatchObject(result);

for (const [name, value] of Object.entries(result || {})) {
expect(scriptTag.attr(name)).toBe(value);
}
});

test("getSDKInlineScript() matches loader inline script", () => {
const { getSDKLoader, getSDKInlineScript } = unpackSDKMeta();

const $ = cheerio.load(getSDKLoader());
const script = $("script").html();

const result = getSDKInlineScript();
expect(script).toContain(result);
});
});
Loading