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

Fix Subdomain Computation of Util function #1629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions aws-ts-static-website/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,35 @@ const distributionArgs: aws.cloudfront.DistributionArgs = {
const cdn = new aws.cloudfront.Distribution("cdn", distributionArgs);

// Split a domain name into its subdomain and parent domain names.
// e.g "example.com" => "", "example.com".
// e.g. "www.example.com" => "www", "example.com".
function getDomainAndSubdomain(domain: string): { subdomain: string, parentDomain: string } {
const parts = domain.split(".");
if (parts.length < 2) {
throw new Error(`No TLD found on ${domain}`);
}
// No subdomain, e.g. awesome-website.com.
if (parts.length === 2) {
return { subdomain: "", parentDomain: domain };
}

const subdomain = parts[0];
parts.shift(); // Drop first element.
return {
subdomain,
// Trailing "." to canonicalize domain.
parentDomain: parts.join(".") + ".",
};
// e.g. "subdomain.subdomain.example.com" => "subdomain.subdomain", "example.com".
function getDomainAndSubdomain(domain: string): {
subdomain: string;
parentDomain: string;
} {
const parts = domain.split(".");
if (parts.length < 2) {
throw new Error(`No TLD found on ${domain}`);
}
// No subdomain, e.g. example.com.
if (parts.length === 2) {
return { subdomain: "", parentDomain: domain };
}

let subdomain = "";
for (let i = 0; i < parts.length - 2; i++) {
subdomain += parts[i] + ".";
}
subdomain = subdomain.slice(0, -1); // Remove trailing dot

const parentDomain = parts.slice(-2).join(".");

return {
subdomain,
// Add trailing "." to canonicalize domain.
parentDomain: parentDomain + ".",
};
}

// Creates a new Route53 DNS record pointing the domain to the CloudFront distribution.
Expand Down
Loading