45 lines
2.4 KiB
TypeScript
45 lines
2.4 KiB
TypeScript
/**
|
|
* Development addresses, known by pattern — no environment variable needed.
|
|
*
|
|
* A site whose own host matches one of these patterns is a development, preview or staging address: not the
|
|
* public site. The plugin uses that for one decision today: such a site, when its database holds the mail-service
|
|
* connection of ANOTHER host, is a copy of that site's data and does not mail real people by itself
|
|
* (docs/email.md "Staging, copies and several hosts").
|
|
*
|
|
* Why a name is built in: `*.dev.netscale.pl` is Intecion's own development domain. Every site the team builds
|
|
* starts there (`<name>.dev.netscale.pl`) and moves to its real domain at launch, and nothing in those sites'
|
|
* environments marks them as development copies. With the pattern in the plugin they are protected without an
|
|
* edit or an env step. It is a default like any other: `iapl({ developmentHosts: [...] })` replaces the list,
|
|
* `[]` empties it, and another agency names its own pattern there.
|
|
*/
|
|
export const DEFAULT_DEVELOPMENT_HOSTS: readonly string[] = ['*.dev.netscale.pl']
|
|
|
|
/** Lowercase host of a URL or a bare host (`https://Acme.dev.example.pl/x` → `acme.dev.example.pl`); '' when unreadable. Keeps the port. */
|
|
export function hostOf(value: null | string | undefined): string {
|
|
const raw = (value ?? '').trim()
|
|
if (!raw) {return ''}
|
|
try {
|
|
return new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`).host.toLowerCase()
|
|
} catch {
|
|
return ''
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The pattern that makes `host` a development address, or null. `*` stands for any characters (dots included);
|
|
* a pattern without `*` must equal the host. The whole host must match; a port is not part of it.
|
|
*/
|
|
export function matchDevelopmentHost(host: null | string | undefined, patterns: readonly string[] = DEFAULT_DEVELOPMENT_HOSTS): null | string {
|
|
const name = hostOf(host).replace(/:\d+$/, '')
|
|
if (!name) {return null}
|
|
for (const pattern of patterns) {
|
|
const source = pattern.trim().toLowerCase().split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*')
|
|
if (source && new RegExp(`^${source}$`).test(name)) {return pattern}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** Is `host` a development address (`matchDevelopmentHost` as a yes or no)? */
|
|
export const isDevelopmentHost = (host: null | string | undefined, patterns: readonly string[] = DEFAULT_DEVELOPMENT_HOSTS): boolean =>
|
|
matchDevelopmentHost(host, patterns) !== null
|