- Q-1 one entry per host (iapl:mail-relay:site:<host>); a read never deletes or writes; another host's
entry is neither used nor touched (a developer's process on the live database, www and apex, a copy)
- a database that holds another host's connection and none of this site's does not enrol by itself:
"Connect again" does, once; mail.relay.otherHosts ('wait' | 'enrol' | the site's own other hosts);
panel text and doctor finding; a site with an empty store enrols by itself as before
- Q-2 the file limit answers from what is stored and never asks the service
- Q-3 a file whose mail was not confirmed is not reported as a wrong file, and no notice mail follows
- Q-4 its own text when the access-key field is off
- D-1 docs for open enrolment; staging, copies and several hosts
Co-Authored-By: Claude Fable 5.2 <[email protected]>
70 lines
3.8 KiB
TypeScript
70 lines
3.8 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import { readdirSync, readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { vi } from 'vitest'
|
|
|
|
import type { FakeRelayOptions } from '../../src/modules/email/relay/fake.js'
|
|
import type { RelayOptions, ResolvedRelayOptions } from '../../src/modules/email/relay/options.js'
|
|
|
|
import { relayConfirmEndpoint } from '../../src/modules/email/relay/confirm.js'
|
|
import { createFakeRelay } from '../../src/modules/email/relay/fake.js'
|
|
import { resolveRelayOptions } from '../../src/modules/email/relay/options.js'
|
|
|
|
export const CONTRACT_DIR = path.resolve(import.meta.dirname, '../../contracts/mail-relay')
|
|
|
|
export type ContractExample = {
|
|
client?: { reEnrol?: boolean; retry?: boolean; status?: null | string }
|
|
direction?: string
|
|
given?: Record<string, boolean | string>
|
|
id: string
|
|
request: { body?: unknown; headers?: Record<string, string>; method: string; path: string; schema?: string }
|
|
response: { body?: Record<string, unknown>; schema?: string; status: number }
|
|
then?: { mail?: Record<string, unknown>; mails?: number }
|
|
}
|
|
|
|
export const contractExamples = (): ContractExample[] =>
|
|
readdirSync(path.join(CONTRACT_DIR, 'examples'))
|
|
.filter((f) => f.endsWith('.json'))
|
|
.sort()
|
|
.map((f) => JSON.parse(readFileSync(path.join(CONTRACT_DIR, 'examples', f), 'utf8')) as ContractExample)
|
|
|
|
/** The slice of Payload the relay client touches: the key-value store, the config, the logger, the two globals. */
|
|
export function relayPayload({ custom, endpoints = [relayConfirmEndpoint()], integrations = {}, serverURL = 'https://example.com', siteName = '' }: { custom?: object; endpoints?: unknown[]; integrations?: Record<string, unknown>; serverURL?: string; siteName?: string } = {}) {
|
|
const store = new Map<string, unknown>()
|
|
const logger = { error: vi.fn(), info: vi.fn(), warn: vi.fn() }
|
|
const settings = { ...integrations }
|
|
const payload = {
|
|
config: { custom: custom ?? {}, endpoints, globals: [{ slug: 'site-integrations' }, { slug: 'site-settings' }], routes: { api: '/api' }, serverURL },
|
|
findGlobal: vi.fn(async ({ slug }: { slug: string }) => (slug === 'site-settings' ? { siteName } : settings)),
|
|
kv: {
|
|
delete: async (key: string) => void store.delete(key),
|
|
get: async (key: string) => (store.has(key) ? structuredClone(store.get(key)) : null),
|
|
keys: async () => [...store.keys()],
|
|
set: async (key: string, value: object) => void store.set(key, structuredClone(value)),
|
|
},
|
|
logger,
|
|
updateGlobal: vi.fn(async ({ data }: { data: Record<string, unknown> }) => Object.assign(settings, data)),
|
|
}
|
|
return { logger, payload: payload as unknown as BasePayload, settings, store }
|
|
}
|
|
|
|
/** How the fake service reaches this site's confirm route: straight into the endpoint's handler. */
|
|
export function callConfirmRoute(payload: BasePayload) {
|
|
const endpoint = relayConfirmEndpoint()
|
|
return async (url: string, init: { body: string; headers: Record<string, string> }): Promise<Response> => {
|
|
const target = new URL(url)
|
|
if (target.pathname !== '/api/iapl/mail-relay/confirm') {return new Response(null, { status: 404 })}
|
|
const req = { headers: new Headers({ ...init.headers, 'x-forwarded-for': '198.51.100.7' }), payload, text: async () => init.body, url }
|
|
return (await endpoint.handler(req as never))
|
|
}
|
|
}
|
|
|
|
/** A site (the payload double) and a fake service that can call it back. */
|
|
export function relaySite({ fake: fakeOptions, options: own, ...site }: { fake?: FakeRelayOptions; options?: RelayOptions } & Parameters<typeof relayPayload>[0] = {}) {
|
|
const made = relayPayload(site)
|
|
const fake = createFakeRelay({ callSite: callConfirmRoute(made.payload), ...fakeOptions })
|
|
const options = resolveRelayOptions(made.payload, { fetchImpl: fake.fetch, retryBackoffMs: 1, url: fake.url, ...own }) as ResolvedRelayOptions
|
|
return { ...made, fake, options }
|
|
}
|