~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
93 lines · 3.0 KB Raw History
import { describe, expect, it } from 'vitest';
import { hostnameToSite, isValidSiteName } from '../src/hostname.js';

const DOMAIN = 'example.com';
const APEX = 'home';

describe('hostnameToSite', () => {
  it('maps the apex to the apex site name', () => {
    expect(hostnameToSite('example.com', DOMAIN, APEX)).toEqual({
      site: 'home',
    });
  });

  it('maps a one-label subdomain to that label', () => {
    expect(hostnameToSite('blog.example.com', DOMAIN, APEX)).toEqual({
      site: 'blog',
    });
  });

  it('case-folds hostnames and the configured domain', () => {
    expect(hostnameToSite('Blog.Example.COM', DOMAIN, APEX)).toEqual({
      site: 'blog',
    });
    expect(hostnameToSite('blog.example.com', 'Example.COM', APEX)).toEqual({
      site: 'blog',
    });
  });

  it('ignores a trailing dot (FQDN form)', () => {
    expect(hostnameToSite('blog.example.com.', DOMAIN, APEX)).toEqual({
      site: 'blog',
    });
  });

  it('returns null for hostnames outside the site domain', () => {
    expect(hostnameToSite('pds.chad-53c.workers.dev', DOMAIN, APEX)).toBeNull();
    expect(hostnameToSite('example.org', DOMAIN, APEX)).toBeNull();
    expect(hostnameToSite('localhost', DOMAIN, APEX)).toBeNull();
  });

  it('returns null for a suffix that is not a subdomain boundary', () => {
    expect(hostnameToSite('badexample.com', DOMAIN, APEX)).toBeNull();
  });

  it('marks multi-label subdomains not-found rather than serving a site', () => {
    expect(hostnameToSite('a.b.example.com', DOMAIN, APEX)).toEqual({
      notFound: true,
    });
  });

  it('marks malformed labels not-found', () => {
    expect(hostnameToSite('-bad.example.com', DOMAIN, APEX)).toEqual({
      notFound: true,
    });
    expect(hostnameToSite('bad-.example.com', DOMAIN, APEX)).toEqual({
      notFound: true,
    });
    expect(hostnameToSite('under_score.example.com', DOMAIN, APEX)).toEqual({
      notFound: true,
    });
  });

  it('accepts labels at the 63-char bound and rejects 64', () => {
    const label63 = 'a'.repeat(63);
    const label64 = 'a'.repeat(64);
    expect(hostnameToSite(`${label63}.example.com`, DOMAIN, APEX)).toEqual({
      site: label63,
    });
    expect(hostnameToSite(`${label64}.example.com`, DOMAIN, APEX)).toEqual({
      notFound: true,
    });
  });

  it('accepts single-character labels', () => {
    expect(hostnameToSite('a.example.com', DOMAIN, APEX)).toEqual({
      site: 'a',
    });
  });
});

describe('isValidSiteName', () => {
  it('accepts DNS labels and rejects everything else', () => {
    expect(isValidSiteName('home')).toBe(true);
    expect(isValidSiteName('my-app2')).toBe(true);
    expect(isValidSiteName('a')).toBe(true);
    expect(isValidSiteName('')).toBe(false);
    expect(isValidSiteName('Has-Caps')).toBe(false);
    expect(isValidSiteName('-lead')).toBe(false);
    expect(isValidSiteName('trail-')).toBe(false);
    expect(isValidSiteName('dot.name')).toBe(false);
    expect(isValidSiteName('a'.repeat(64))).toBe(false);
  });
});