MikeBild / sveltekit-adapter-aws

A SvelteKit adapter for AWS using the AWS-CDK
MIT License
135 stars 32 forks source link

FQDN not working for single level parent domains #39

Open petrosoft-fi opened 1 year ago

petrosoft-fi commented 1 year ago

Description of the bug The SvelteKit adapter for AWS does not support using top-level domains (e.g., domain.com) as FQDN values. It only works correctly with subdomains (e.g., subdomain.domain.com) and third level domains (e.g. domain.co.uk).

In the current production version, the domainName variable incorrectly receives the value "com" when using "google.com" as an FQDN. Instead, it should be assigned the correct value, which in this case is "google.com".

Steps to reproduce the behavior:

  1. Set up a project using the SvelteKit adapter for AWS.
  2. Set the FQDN value to a top-level domain (e.g., domain.com).
  3. Deploy the project using AWS CDK.
  4. Encounter the error: "[Error at /sveltekit-adapter-aws-webapp] Found zones: [] for dns:com, privateZone:undefined, vpcId:undefined, but wanted exactly 1 zone"

Expected behavior The adapter should work correctly with top-level domains, third level domains and subdomains as FQDN values.

Additional context A potential fix for this issue is to modify the adapter-stack.js file to handle both second-level top-level domains (e.g., domain.co.uk) and regular top-level domains (e.g., domain.com):

const domainParts = ((_b = process.env.FQDN) === null || _b === void 0 ? void 0 : _b.split('.')) || []; const isSecondLevelTLD = domainParts.length >= 3 && domainParts[domainParts.length - 2].length === 2; const domainName = domainParts.slice(isSecondLevelTLD ? -3 : -2).join('.');

This code works as follows:

  1. It splits the FQDN string into an array of domain parts by splitting it on the period character (.).
  2. It checks if the domain has a second-level top-level domain (TLD) by checking if there are at least 3 domain parts and if the second last part of the domain has a length of 2 characters (e.g., co in domain.co.uk).
  3. It constructs the main domain by slicing the domainParts array. If the domain has a second-level TLD, it takes the last three parts of the array; otherwise, it takes the last two parts. Finally, it joins the resulting array back into a string using the period character (.).

With this code, the main domain should be correctly determined for cases like www.domain.com, domain.com, domain.co.uk, and subdomain.domain.co.uk.

Tylermarques commented 1 year ago

Confirming that I see the same behaviour.

When attempting to deploy with a top level domain I get the error Found zones: [] for dns:com, privateZone:undefined, vpcId:undefined, but wanted exactly 1 zone which lead me to this same conclusion. replacing my FQDN with www.mydomain.com fixes the issue.