Mail relay: development hosts are known by pattern, with no env - built in: *.dev.netscale.pl
The launch rule called a site non-public only when IAPL_NOINDEX was set (or the project named its hosts).
The owner's development sites set nothing, so production's database copied onto a development address
counted as a launch and would have mailed real people.
- iapl({ developmentHosts }): host patterns of development addresses. Default DEFAULT_DEVELOPMENT_HOSTS =
['*.dev.netscale.pl'], Intecion's own development domain; a project replaces the list or empties it.
mail.relay.developmentHosts overrides it for the mail service.
- a site whose host matches is non-public exactly as with IAPL_NOINDEX=1; either signal is enough
- siteNonPublicSignal, matchDevelopmentHost, isDevelopmentHost, pluginDevelopmentHosts exported
- panel state: nonPublicBy, nonPublicPattern; doctor: mail-non-public (info) names the signal
- every case of the launch table runs with no env on the built-in pattern
- docs, AGENT.md, skills, changelog draft
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
634908eb57
commit
14abf369da
@@ -15,6 +15,14 @@ const since = (iso?: null | string) => (iso ? `, since ${iso.slice(0, 16).replac
|
||||
* Intecion Notifications Webservice, the deprecated Graph transport, the reply address, and whether
|
||||
* the service can reach the site's confirm route. Ids carry no brand, like `agents-md`.
|
||||
*/
|
||||
/** "this site is non-public because …" — the signal named, for the findings that depend on it. */
|
||||
const nonPublicBecause = (mail: { nonPublicBy?: 'development-host' | 'noindex'; nonPublicPattern?: string }): string =>
|
||||
mail.nonPublicBy === 'noindex'
|
||||
? 'this site is non-public because IAPL_NOINDEX is set'
|
||||
: mail.nonPublicBy === 'development-host'
|
||||
? `this site is non-public because its host matches the development pattern "${mail.nonPublicPattern ?? '?'}" (developmentHosts)`
|
||||
: 'this site is non-public (a development host, or IAPL_NOINDEX)'
|
||||
|
||||
export const mailRules: DoctorRule[] = [
|
||||
{
|
||||
// Which transport a send takes right now, and whether the last sends worked.
|
||||
@@ -45,6 +53,25 @@ export const mailRules: DoctorRule[] = [
|
||||
group: 'mail',
|
||||
source: 'deep',
|
||||
},
|
||||
{
|
||||
// Which signal makes the site a non-public copy for the mail service — said plainly, because that signal
|
||||
// decides whether a copy of another site's data mails real people.
|
||||
id: 'mail-non-public',
|
||||
check: (ctx) => {
|
||||
const mail = ctx.deep?.mail
|
||||
if (!mail || mail.transport !== 'relay' || !mail.relay || !mail.nonPublic) {return []}
|
||||
return [{
|
||||
fix: mail.nonPublicBy === 'noindex'
|
||||
? 'right for a development, staging or test copy. On the PUBLIC site this is a mistake: remove IAPL_NOINDEX there (it also keeps the site out of search engines)'
|
||||
: 'right for a development address. If this host is the PUBLIC site, take it out of the list: iapl({ developmentHosts: [...] }) (`[]` = no development hosts)',
|
||||
message: `${nonPublicBecause(mail)}: for the Intecion mail service it is a non-public site — it sends its own e-mails, but with a copy of another site's data it sends nothing by itself`,
|
||||
severity: 'info',
|
||||
}]
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'deep',
|
||||
},
|
||||
{
|
||||
// The site's state with the Intecion mail service, when that is how it sends.
|
||||
id: 'mail-relay-state',
|
||||
@@ -61,8 +88,8 @@ export const mailRules: DoctorRule[] = [
|
||||
out.push({ fix: 'Intecion approves the host on the service (`ims approve <host>`); the same token then works', message: 'the site is enrolled with the Intecion mail service but waits for approval: e-mails are not sent yet', severity: 'warning' })
|
||||
} else if (relay.refusal === 'client-other-host') {
|
||||
out.push({
|
||||
fix: 'expected for a copy that must not mail real people. If this copy should send e-mails: Site integrations → E-mail sending → "Connect again" (once). To keep it silent for good: mail: { relay: false } or SMTP to a catcher. If this is NOT a copy but the site itself, it is wrongly marked non-public: remove IAPL_NOINDEX / the host from mail.relay.developmentHosts',
|
||||
message: `this site is non-public (IAPL_NOINDEX or a development host) and its database holds the mail-service connection of another site (${relay.reason ?? 'another host'}) — a copy of that site's data: it does not connect or send by itself, e-mails are not sent`,
|
||||
fix: 'expected for a copy that must not mail real people. If this copy should send e-mails: Site integrations → E-mail sending → "Connect again" (once). To keep it silent for good: mail: { relay: false } or SMTP to a catcher. If this is NOT a copy but the site itself, it is wrongly marked non-public: remove IAPL_NOINDEX, or take the host out of iapl({ developmentHosts })',
|
||||
message: `${nonPublicBecause(mail)} and its database holds the mail-service connection of another site (${relay.reason ?? 'another host'}) — a copy of that site's data: it does not connect or send by itself, e-mails are not sent`,
|
||||
severity: 'warning',
|
||||
})
|
||||
} else if (!relay.connected && relay.refusal) {
|
||||
|
||||
@@ -71,7 +71,7 @@ export async function collectDoctorStatus(payload: BasePayload): Promise<DoctorS
|
||||
}
|
||||
mail = {
|
||||
graphEnv: graphAvailable(),
|
||||
...(state.nonPublic ? { nonPublic: true } : {}),
|
||||
...(state.nonPublic ? { nonPublic: true, ...(state.nonPublicBy ? { nonPublicBy: state.nonPublicBy } : {}), ...(state.nonPublicPattern ? { nonPublicPattern: state.nonPublicPattern } : {}) } : {}),
|
||||
...(state.otherHostEntries ? { otherHostEntries: true } : {}),
|
||||
relay: state.relay
|
||||
? {
|
||||
|
||||
@@ -69,6 +69,9 @@ export type DoctorStatus = {
|
||||
graphEnv: boolean
|
||||
/** The site says it is not public (`IAPL_NOINDEX`, or a development host): a copy of another site's data does not mail by itself. */
|
||||
nonPublic?: boolean
|
||||
/** Which signal makes it non-public: the host matches a development pattern (`nonPublicPattern`), or `IAPL_NOINDEX`. */
|
||||
nonPublicBy?: 'development-host' | 'noindex'
|
||||
nonPublicPattern?: string
|
||||
/** The database also holds the mail-service connection of another host that this site did not name as its own. */
|
||||
otherHostEntries?: boolean
|
||||
/** null = the mail service is switched off in the project. */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import { type RelayOptions, resolveRelayOptions } from './options.js'
|
||||
import { disconnectRelay as disconnect, enrolRelay as enrol, type EnrolResult, siteIsNonPublic as nonPublic, type RelayStatus, rotateRelayToken as rotate, getRelayStatus as status } from './session.js'
|
||||
import { disconnectRelay as disconnect, enrolRelay as enrol, type EnrolResult, siteIsNonPublic as nonPublic, type NonPublicSignal, siteNonPublicSignal as nonPublicSignal, type RelayStatus, rotateRelayToken as rotate, getRelayStatus as status } from './session.js'
|
||||
|
||||
export { RELAY_SENDER_ADDRESS, relayAdapter, relayAttachmentLimit, relaySenderName } from './adapter.js'
|
||||
export type { RelayAdapterArgs } from './adapter.js'
|
||||
@@ -25,12 +25,12 @@ export { expectMailTo, MAIL_KIND, MAIL_KINDS, mailKindOf, runWithMailKind, withM
|
||||
export type { MailKind } from './kinds.js'
|
||||
export { bareAddress, bareAddresses, buildRelayMessage, cleanSubject } from './message.js'
|
||||
export type { BuildRelayMessageArgs, BuiltRelayMessage, RelaySendRequest } from './message.js'
|
||||
export { isPublicHost, relayHost, resolveRelayOptions } from './options.js'
|
||||
export { isPublicHost, pluginDevelopmentHosts, relayHost, resolveRelayOptions } from './options.js'
|
||||
export type { RelayOptions } from './options.js'
|
||||
export { sendThroughRelay } from './send.js'
|
||||
export type { RelaySendContext, RelaySendResult } from './send.js'
|
||||
export { setRelayToken } from './session.js'
|
||||
export type { EnrolResult, RelayStatus } from './session.js'
|
||||
export type { EnrolResult, NonPublicSignal, RelayStatus } from './session.js'
|
||||
|
||||
const DISABLED = 'The mail service is switched off for this project (mail: { relay: false }).'
|
||||
|
||||
@@ -67,8 +67,13 @@ export const disconnectRelay = (payload: BasePayload, options?: RelayOptions): P
|
||||
export const rotateRelayToken = (payload: BasePayload, options?: RelayOptions) => rotate(payload, resolved(payload, options), { force: true })
|
||||
|
||||
/**
|
||||
* Whether this site counts as a non-public copy for `mail.relay.otherHosts`: `IAPL_NOINDEX=1`, or its host is
|
||||
* matched by `mail.relay.developmentHosts`. Such a site neither enrols nor sends by itself while its database
|
||||
* holds another host's connection (docs/email.md "Staging, copies and several hosts").
|
||||
* Whether this site counts as a non-public copy for `mail.relay.otherHosts`: its host matches a development
|
||||
* pattern (`developmentHosts`; built in: `*.dev.netscale.pl`), or `IAPL_NOINDEX=1`. Such a site neither enrols
|
||||
* nor sends by itself while its database holds another host's connection (docs/email.md "Staging, copies and
|
||||
* several hosts").
|
||||
*/
|
||||
export const siteIsNonPublic = (payload: BasePayload, options?: RelayOptions): boolean => nonPublic(payload, resolveRelayOptions(payload, options) || (options ?? {}))
|
||||
|
||||
/** Which signal says so — `{ by: 'development-host', pattern }` or `{ by: 'noindex' }` — or null for a public site. */
|
||||
export const siteNonPublicSignal = (payload: BasePayload, options?: RelayOptions): NonPublicSignal | null =>
|
||||
nonPublicSignal(payload, resolveRelayOptions(payload, options) || (options ?? {}))
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { BasePayload, SendEmailOptions } from 'payload'
|
||||
|
||||
import type { MailKind } from './kinds.js'
|
||||
|
||||
import { DEFAULT_DEVELOPMENT_HOSTS } from '../../shared/developmentHosts.js'
|
||||
import { RELAY_BASE_URL } from './constants.js'
|
||||
|
||||
/**
|
||||
@@ -25,9 +26,11 @@ export type RelayOptions = {
|
||||
/** Calls to the public confirm route per address. Default 10 a minute. */
|
||||
confirmRateLimit?: { max?: number; windowMs?: number }
|
||||
/**
|
||||
* Host patterns of development and preview addresses (`*` = any characters), for example
|
||||
* `['*.dev.example-agency.pl']`. A site on such a host is treated as a non-public copy for `otherHosts`,
|
||||
* exactly like a site with `IAPL_NOINDEX=1`. Nothing is built in: name the agency's own pattern here.
|
||||
* Host patterns of development and preview addresses (`*` = any characters), for the mail service only.
|
||||
* Default: the plugin's list — `iapl({ developmentHosts })`, which itself defaults to
|
||||
* `DEFAULT_DEVELOPMENT_HOSTS` (`['*.dev.netscale.pl']`, Intecion's own development domain). A site on such a
|
||||
* host is treated as a non-public copy for `otherHosts`, exactly like a site with `IAPL_NOINDEX=1`; no
|
||||
* environment variable is needed. `[]` = no host is a development address.
|
||||
*/
|
||||
developmentHosts?: string[]
|
||||
/**
|
||||
@@ -50,7 +53,7 @@ export type RelayOptions = {
|
||||
* address's entry is still there), a moved domain, or a copy of another site's database.
|
||||
*
|
||||
* Default (`'auto'`): by direction. A **public** site enrols by itself — launch day and a domain move need
|
||||
* nobody. A **non-public** site (`IAPL_NOINDEX=1`, or a host in `developmentHosts`) that finds another
|
||||
* nobody. A **non-public** site (a host in `developmentHosts`, or `IAPL_NOINDEX=1`) that finds another
|
||||
* host's connection it was never told about is a copy of that site's data: it does not enrol and does not
|
||||
* send by itself (it would mail real people), until an admin presses "Connect again".
|
||||
*
|
||||
@@ -84,7 +87,7 @@ export type RelayOptions = {
|
||||
url?: string
|
||||
}
|
||||
|
||||
export type ResolvedRelayOptions = { ownUrl: boolean; url: string } & Omit<RelayOptions, 'url'>
|
||||
export type ResolvedRelayOptions = { developmentHosts: string[]; ownUrl: boolean; url: string } & Omit<RelayOptions, 'developmentHosts' | 'url'>
|
||||
|
||||
type PluginMail = { graphDeprecationWarning?: boolean; relay?: false | RelayOptions }
|
||||
|
||||
@@ -93,6 +96,15 @@ export function pluginMailOptions(payload: Pick<BasePayload, 'config'>): PluginM
|
||||
return ((payload.config?.custom as { iapl?: { mail?: PluginMail } } | undefined)?.iapl?.mail ?? {})
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin's development-host patterns: `iapl({ developmentHosts })` as left in the config
|
||||
* (`config.custom.iapl.developmentHosts`), else the built-in list.
|
||||
*/
|
||||
export function pluginDevelopmentHosts(payload: Pick<BasePayload, 'config'>): string[] {
|
||||
const own = (payload.config?.custom as { iapl?: { developmentHosts?: unknown } } | undefined)?.iapl?.developmentHosts
|
||||
return Array.isArray(own) ? own.filter((p): p is string => typeof p === 'string') : [...DEFAULT_DEVELOPMENT_HOSTS]
|
||||
}
|
||||
|
||||
/**
|
||||
* The options in effect: the adapter's own `relay` argument over the plugin option. `null` = the relay
|
||||
* is switched off.
|
||||
@@ -102,7 +114,7 @@ export function resolveRelayOptions(payload: Pick<BasePayload, 'config'>, own?:
|
||||
if (own === false || (own === undefined && fromPlugin === false)) {return null}
|
||||
const merged: RelayOptions = { ...(fromPlugin || {}), ...(own || {}) }
|
||||
const url = (merged.url ?? RELAY_BASE_URL).replace(/\/+$/, '')
|
||||
return { ...merged, ownUrl: url !== RELAY_BASE_URL, url }
|
||||
return { ...merged, developmentHosts: merged.developmentHosts ?? pluginDevelopmentHosts(payload), ownUrl: url !== RELAY_BASE_URL, url }
|
||||
}
|
||||
|
||||
const LABEL = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/
|
||||
|
||||
@@ -11,7 +11,7 @@ import { RELAY_SENDER_ADDRESS } from './adapter.js'
|
||||
import { relayRequest } from './client.js'
|
||||
import { RELAY_CONFIRM_PATH, RELAY_CONNECT_PATH, RELAY_DISCONNECT_PATH, RELAY_STATUS_PATH } from './constants.js'
|
||||
import { isPublicHost, relayHost, resolveRelayOptions } from './options.js'
|
||||
import { disconnectRelay, enrolRelay, type EnrolResult, getRelayStatus, LOCAL_CODES, otherHostsInTheWay, readSiteStore, type RelayStatus, setRelayToken, siteIsNonPublic } from './session.js'
|
||||
import { disconnectRelay, enrolRelay, type EnrolResult, getRelayStatus, LOCAL_CODES, type NonPublicSignal, otherHostsInTheWay, readSiteStore, type RelayStatus, setRelayToken, siteNonPublicSignal } from './session.js'
|
||||
import { otherRelayHosts } from './store.js'
|
||||
|
||||
type Settings = {
|
||||
@@ -29,8 +29,11 @@ export type MailPanelState = {
|
||||
accessKeyField?: boolean
|
||||
/** The address the service calls this site back at. */
|
||||
confirmUrl?: string
|
||||
/** The site says it is not public (`IAPL_NOINDEX`, or a host in `mail.relay.developmentHosts`). */
|
||||
/** The site is not public: its host is a development address (`developmentHosts`), or `IAPL_NOINDEX` is set. */
|
||||
nonPublic?: boolean
|
||||
/** Which signal says so; with `development-host`, `nonPublicPattern` is the pattern that matched. */
|
||||
nonPublicBy?: NonPublicSignal['by']
|
||||
nonPublicPattern?: string
|
||||
/** The database also holds the connection of a host this site did not name as its own, and the site is not held for it. */
|
||||
otherHostEntries?: boolean
|
||||
/** Enabled in the project (`mail.relay` is not `false`). */
|
||||
@@ -67,10 +70,10 @@ export async function mailPanelState(req: Pick<PayloadRequest, 'payload'>, own:
|
||||
}
|
||||
// …and so is a non-public copy of another site's database: it waits for an admin's "Connect again", with or
|
||||
// without a token of its own. Asked now, not read from what a past send left: the rule ends with its reason.
|
||||
let nonPublic = false
|
||||
let signal: NonPublicSignal | null = null
|
||||
let otherHostEntries = false
|
||||
if (status && options && options.token === undefined) {
|
||||
nonPublic = siteIsNonPublic(payload, options)
|
||||
signal = siteNonPublicSignal(payload, options)
|
||||
const stored = await readSiteStore(payload, options)
|
||||
const foreign = status.disconnected ? [] : await otherHostsInTheWay(payload, options, stored)
|
||||
const known = status.refusal?.code === LOCAL_CODES.otherHost
|
||||
@@ -87,7 +90,7 @@ export async function mailPanelState(req: Pick<PayloadRequest, 'payload'>, own:
|
||||
...(status?.host ? { confirmUrl: `https://${status.host}${api}${RELAY_CONFIRM_PATH}` } : {}),
|
||||
relay: status ? { ...status, senderAddress: status.senderAddress ?? RELAY_SENDER_ADDRESS } : null,
|
||||
replyAddress: typeof settings.smtpFromAddress === 'string' && settings.smtpFromAddress.trim() ? settings.smtpFromAddress.trim() : null,
|
||||
...(nonPublic ? { nonPublic: true } : {}),
|
||||
...(signal ? { nonPublic: true, nonPublicBy: signal.by, ...(signal.by === 'development-host' ? { nonPublicPattern: signal.pattern } : {}) } : {}),
|
||||
...(otherHostEntries ? { otherHostEntries: true } : {}),
|
||||
...(smtpIncomplete(settings) ? { smtpIncomplete: true } : {}),
|
||||
transport,
|
||||
|
||||
@@ -2,13 +2,14 @@ import type { BasePayload } from 'payload'
|
||||
|
||||
import { randomBytes } from 'node:crypto'
|
||||
|
||||
import type { ResolvedRelayOptions } from './options.js'
|
||||
import type { RelayOptions, ResolvedRelayOptions } from './options.js'
|
||||
|
||||
import { PLUGIN_VERSION } from '../../../version.js'
|
||||
import { isSiteNoindex } from '../../seo/siteNoindex.js'
|
||||
import { matchDevelopmentHost } from '../../shared/developmentHosts.js'
|
||||
import { type RelayAnswer, type RelayRefusal, relayRequest } from './client.js'
|
||||
import { RELAY_CONFIRM_PATH, RELAY_DEFAULTS, RELAY_PRODUCT, type RelayStatusCode, relayStatusFor } from './constants.js'
|
||||
import { isPublicHost, pluginMailOptions, relayHost } from './options.js'
|
||||
import { isPublicHost, pluginDevelopmentHosts, pluginMailOptions, relayHost } from './options.js'
|
||||
import * as store from './store.js'
|
||||
import { otherRelayHosts, type RelaySiteState, type RelayStored, releaseNonce, sha256Hex } from './store.js'
|
||||
|
||||
@@ -95,17 +96,24 @@ async function otherHostsNow(payload: BasePayload, host: string, fresh: boolean)
|
||||
|
||||
const bare = (host: string) => host.trim().toLowerCase().replace(/^www\./, '')
|
||||
|
||||
/** Why a site counts as non-public: its host matches a development pattern, or `IAPL_NOINDEX` is set. */
|
||||
export type NonPublicSignal = { by: 'development-host'; pattern: string } | { by: 'noindex' }
|
||||
|
||||
/**
|
||||
* Is this site a non-public copy — a development, preview or staging address? `IAPL_NOINDEX=1` (the switch
|
||||
* such a site already has), or a host that matches `mail.relay.developmentHosts`.
|
||||
* Which signal makes this site a non-public copy — a development, preview or staging address — or null for a
|
||||
* public site. Either is enough: its host matches a development pattern (`mail.relay.developmentHosts`, else
|
||||
* `iapl({ developmentHosts })`, else the built-in list — no environment variable needed), or `IAPL_NOINDEX=1`.
|
||||
* The host is named first: it is the signal a site has without anybody setting anything.
|
||||
*/
|
||||
export function siteIsNonPublic(payload: BasePayload, options: Pick<ResolvedRelayOptions, 'developmentHosts' | 'host'>): boolean {
|
||||
if (isSiteNoindex()) {return true}
|
||||
const host = relayHost(payload, options)
|
||||
return (options.developmentHosts ?? []).some((pattern) => {
|
||||
const source = pattern.trim().toLowerCase().split('*').map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*')
|
||||
return Boolean(source) && new RegExp(`^${source}$`).test(host)
|
||||
})
|
||||
export function siteNonPublicSignal(payload: BasePayload, options: Pick<RelayOptions, 'developmentHosts' | 'host'>): NonPublicSignal | null {
|
||||
const pattern = matchDevelopmentHost(relayHost(payload, options), options.developmentHosts ?? pluginDevelopmentHosts(payload))
|
||||
if (pattern) {return { by: 'development-host', pattern }}
|
||||
return isSiteNoindex() ? { by: 'noindex' } : null
|
||||
}
|
||||
|
||||
/** Is this site a non-public copy (`siteNonPublicSignal` as a yes or no)? */
|
||||
export function siteIsNonPublic(payload: BasePayload, options: Pick<RelayOptions, 'developmentHosts' | 'host'>): boolean {
|
||||
return siteNonPublicSignal(payload, options) !== null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,7 +143,7 @@ export async function otherHostsInTheWay(
|
||||
}
|
||||
|
||||
const otherHostMessage = (host: string, foreign: string[]) =>
|
||||
`This database holds the mail-service connection of another site (${foreign.join(', ')}), and this site ("${host}") is a non-public copy (IAPL_NOINDEX, or a development host) or set to wait: it does not connect or send by itself. Press "Connect again" in Site integrations → E-mail sending if it should send e-mails (mail.relay.otherHosts).`
|
||||
`This database holds the mail-service connection of another site (${foreign.join(', ')}), and this site ("${host}") is a non-public copy (a development host, or IAPL_NOINDEX) or set to wait: it does not connect or send by itself. Press "Connect again" in Site integrations → E-mail sending if it should send e-mails (mail.relay.otherHosts).`
|
||||
|
||||
async function enrolOnce(payload: BasePayload, options: ResolvedRelayOptions, force: boolean, deadline?: number): Promise<EnrolResult> {
|
||||
const stored = await readSiteStore(payload, options)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 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
|
||||
Reference in New Issue
Block a user