- iapl-skills: --dest is compared with the project root after resolving links; a folder that is the root or holds it (.., /) is refused - starter: collectBundle refuses symbolic links (their target would ship in the published bundle); writeFiles refuses absolute or escaping paths - form files: a program inside a DOCX/XLSX is found with trailing dots or spaces (Windows drops them) - proxy matcher: (?!(?:api|admin|_next)(?:/|$)|...) so /administracja, /apis get the proxy; doctor rule proxy-matcher reports the old literal - pnpm: @swc/core in ignoredBuiltDependencies (its postinstall only checks the native binary, already installed) Co-Authored-By: Claude Opus 5.5 <[email protected]>
146 lines
6.1 KiB
TypeScript
146 lines
6.1 KiB
TypeScript
import type { BuildCspArgs } from '../security/buildCsp.js'
|
|
import type { I18nConfig } from './types.js'
|
|
|
|
import { MARKDOWN_PREFIX, markdownHref, prefersMarkdown } from '../content/markdownNegotiation.js'
|
|
import { buildCsp, buildReportingEndpoints } from '../security/buildCsp.js'
|
|
import { createCspNonce, CSP_NONCE_HEADER } from '../security/cspNonce.js'
|
|
import { isValidLocale } from './helpers.js'
|
|
import { createLocaleMiddleware, DEFAULT_MIDDLEWARE_MATCHER } from './localeMiddleware.js'
|
|
|
|
/** Re-exported so the project's proxy.ts config uses the plugin's matcher. */
|
|
export const DEFAULT_PROXY_MATCHER = DEFAULT_MIDDLEWARE_MATCHER
|
|
|
|
/**
|
|
* Minimal structural types — the plugin does NOT import next/server (it's a peer
|
|
* dep not present at the plugin's own build time, which would cause TS2307).
|
|
* Next's real NextRequest / NextResponse satisfy these shapes at the call site.
|
|
*/
|
|
type ProxyRequest = {
|
|
cookies: { get: (name: string) => { value: string } | undefined }
|
|
// Matches localeMiddleware's MiddlewareRequest shape; a real NextRequest
|
|
// satisfies all of this structurally.
|
|
headers: Headers
|
|
nextUrl: { clone: () => URL; pathname: string; search: string }
|
|
url: string
|
|
}
|
|
|
|
type ProxyResponse = {
|
|
cookies: { set: (name: string, value: string, opts?: Record<string, unknown>) => void }
|
|
/** Needed for `csp`; Next's NextResponse has it. */
|
|
headers?: Headers
|
|
}
|
|
|
|
/**
|
|
* Next's NextResponse constructor, injected by the project (which has next/server
|
|
* in scope). The plugin stays free of a next/server import.
|
|
*/
|
|
type NextResponseLike = {
|
|
next: (init?: { request?: { headers: Headers } }) => ProxyResponse
|
|
redirect: (url: string | URL) => ProxyResponse
|
|
/** Needed for `markdown` content negotiation; Next's NextResponse has it. */
|
|
rewrite?: (url: string | URL) => ProxyResponse
|
|
}
|
|
|
|
type CreateProxyOptions = {
|
|
consentCategory?: 'functional' | 'necessary'
|
|
consentCookieName?: string
|
|
cookieName?: string
|
|
/**
|
|
* Content-Security-Policy with a per-request nonce (buildCsp args, no `nonce`).
|
|
* The proxy creates the nonce, puts the policy on the request (Next reads the
|
|
* nonce from it and adds it to its own scripts) and on the response, and
|
|
* exposes the nonce as the `x-nonce` request header for <SiteProviders nonce>.
|
|
* Nonces need dynamic rendering: every page under the matcher renders per
|
|
* request (no ISR / static HTML). Static sites keep the header-only CSP from
|
|
* buildSecurityHeaders `csp` instead. Default off.
|
|
*/
|
|
csp?: Omit<BuildCspArgs, 'nonce'>
|
|
/** Expose x-locale / x-pathname request headers to RSC. Default true. */
|
|
exposeHeaders?: boolean
|
|
/**
|
|
* GEO: wire the Markdown route (content.md). `true` = prefix '/md', a string
|
|
* sets another one. Paths under the prefix pass through untouched (no locale
|
|
* redirect — /md/pl/o-nas must not become /pl/md/pl/o-nas), and a page
|
|
* requested with `Accept: text/markdown` is rewritten to its Markdown version.
|
|
*/
|
|
markdown?: boolean | string
|
|
/** Consent policy version to require for saving the locale cookie (createLocaleMiddleware). */
|
|
policyVersion?: number
|
|
}
|
|
|
|
/**
|
|
* Ready-made Next 16 proxy (the former middleware). Wraps the plugin's locale
|
|
* logic AND the per-project boilerplate (NextResponse, x-locale / x-pathname
|
|
* headers, locale cookie, redirect). The project's proxy.ts becomes a few lines.
|
|
*
|
|
* `NextResponse` is INJECTED (the plugin never imports next/server — it's a peer
|
|
* dep absent at the plugin's build time). The project passes it:
|
|
*
|
|
* // proxy.ts (Next 16 — NOT middleware.ts)
|
|
* import { NextResponse } from 'next/server'
|
|
* import { createProxy, DEFAULT_PROXY_MATCHER } from '@intecion/ipal-kit'
|
|
* import { i18nConfig } from '@/i18n.config'
|
|
* export const proxy = createProxy(i18nConfig, { NextResponse, markdown: true })
|
|
* export const config = { matcher: ['/((?!(?:api|admin|_next)(?:/|$)|.*\\..*).*)'] } // inline literal
|
|
*/
|
|
export function createProxy(
|
|
config: I18nConfig,
|
|
deps: { NextResponse: NextResponseLike } & CreateProxyOptions,
|
|
): (request: ProxyRequest) => ProxyResponse {
|
|
const { csp, exposeHeaders = true, markdown, NextResponse, ...mwOptions } = deps
|
|
const run = createLocaleMiddleware({ config, ...mwOptions })
|
|
const mdPrefix = markdown ? (typeof markdown === 'string' ? markdown : MARKDOWN_PREFIX).replace(/\/$/, '') : null
|
|
|
|
return (request: ProxyRequest): ProxyResponse => {
|
|
const { pathname } = request.nextUrl
|
|
if (mdPrefix) {
|
|
if (pathname === mdPrefix || pathname.startsWith(`${mdPrefix}/`)) {return NextResponse.next()}
|
|
// Only locale-qualified paths (or single-locale sites) map 1:1 onto the
|
|
// Markdown route; the rest gets the locale redirect first, then negotiates.
|
|
const seg = pathname.split('/').filter(Boolean)[0] ?? ''
|
|
const routable = config.locales.length === 1 || isValidLocale(seg, config)
|
|
if (routable && NextResponse.rewrite && prefersMarkdown(request.headers)) {
|
|
return NextResponse.rewrite(new URL(markdownHref(pathname, mdPrefix), request.url))
|
|
}
|
|
}
|
|
|
|
const result = run(request)
|
|
|
|
const requestHeaders = new Headers(request.headers)
|
|
const nonce = csp ? createCspNonce() : undefined
|
|
const policy = csp ? buildCsp({ ...csp, nonce }) : null
|
|
if (policy && nonce) {
|
|
requestHeaders.set(policy.key, policy.value)
|
|
requestHeaders.set(CSP_NONCE_HEADER, nonce)
|
|
}
|
|
if (exposeHeaders) {
|
|
const seg = pathname.split('/').filter(Boolean)[0] ?? ''
|
|
const locale = isValidLocale(seg, config) ? seg : config.defaultLocale
|
|
requestHeaders.set('x-locale', locale)
|
|
requestHeaders.set('x-pathname', pathname)
|
|
}
|
|
|
|
const response =
|
|
result.type === 'redirect'
|
|
? NextResponse.redirect(new URL(result.location, request.url))
|
|
: NextResponse.next({ request: { headers: requestHeaders } })
|
|
|
|
if (policy) {
|
|
response.headers?.set(policy.key, policy.value)
|
|
if (csp?.reportUri) {
|
|
const re = buildReportingEndpoints(csp.reportUri, csp.reportGroup)
|
|
response.headers?.set(re.key, re.value)
|
|
}
|
|
}
|
|
|
|
if (result.cookie) {
|
|
response.cookies.set(result.cookie.name, result.cookie.value, {
|
|
path: '/',
|
|
sameSite: 'lax',
|
|
})
|
|
}
|
|
|
|
return response
|
|
}
|
|
}
|