Add missing matcher support

parent b9c74083
......@@ -25,6 +25,7 @@ export interface MiddlewareMatcher {
regexp: string
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}
export interface PageStaticInfo {
......
......@@ -157,8 +157,13 @@ export function getUtils({
)
let params = matcher(parsedUrl.pathname)
if (rewrite.has && params) {
const hasParams = matchHas(req, rewrite.has, parsedUrl.query)
if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas(
req,
parsedUrl.query,
rewrite.has,
rewrite.missing
)
if (hasParams) {
Object.assign(params, hasParams)
......
......@@ -24,6 +24,7 @@ export type Rewrite = {
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}
export type Header = {
......@@ -32,6 +33,7 @@ export type Header = {
locale?: false
headers: Array<{ key: string; value: string }>
has?: RouteHas[]
missing?: RouteHas[]
}
// internal type used for validation (not user facing)
......@@ -41,6 +43,7 @@ export type Redirect = {
basePath?: false
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
} & (
| {
statusCode?: never
......@@ -56,6 +59,7 @@ export type Middleware = {
source: string
locale?: false
has?: RouteHas[]
missing?: RouteHas[]
}
const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host'])
......@@ -132,8 +136,9 @@ export function checkCustomRoutes(
let numInvalidRoutes = 0
let hadInvalidStatus = false
let hadInvalidHas = false
let hadInvalidMissing = false
const allowedKeys = new Set<string>(['source', 'locale', 'has'])
const allowedKeys = new Set<string>(['source', 'locale', 'has', 'missing'])
if (type === 'rewrite') {
allowedKeys.add('basePath')
......@@ -198,48 +203,65 @@ export function checkCustomRoutes(
invalidParts.push('`locale` must be undefined or false')
}
if (typeof route.has !== 'undefined' && !Array.isArray(route.has)) {
invalidParts.push('`has` must be undefined or valid has object')
hadInvalidHas = true
} else if (route.has) {
const invalidHasItems = []
const checkInvalidHasMissing = (
items: any,
fieldName: 'has' | 'missing'
) => {
let hadInvalidItem = false
for (const hasItem of route.has) {
let invalidHasParts = []
if (typeof items !== 'undefined' && !Array.isArray(items)) {
invalidParts.push(
`\`${fieldName}\` must be undefined or valid has object`
)
hadInvalidItem = true
} else if (items) {
const invalidHasItems = []
if (!allowedHasTypes.has(hasItem.type)) {
invalidHasParts.push(`invalid type "${hasItem.type}"`)
}
if (typeof hasItem.key !== 'string' && hasItem.type !== 'host') {
invalidHasParts.push(`invalid key "${hasItem.key}"`)
}
if (
typeof hasItem.value !== 'undefined' &&
typeof hasItem.value !== 'string'
) {
invalidHasParts.push(`invalid value "${hasItem.value}"`)
}
if (typeof hasItem.value === 'undefined' && hasItem.type === 'host') {
invalidHasParts.push(`value is required for "host" type`)
}
for (const hasItem of items) {
let invalidHasParts = []
if (invalidHasParts.length > 0) {
invalidHasItems.push(
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}`
)
if (!allowedHasTypes.has(hasItem.type)) {
invalidHasParts.push(`invalid type "${hasItem.type}"`)
}
if (typeof hasItem.key !== 'string' && hasItem.type !== 'host') {
invalidHasParts.push(`invalid key "${hasItem.key}"`)
}
if (
typeof hasItem.value !== 'undefined' &&
typeof hasItem.value !== 'string'
) {
invalidHasParts.push(`invalid value "${hasItem.value}"`)
}
if (typeof hasItem.value === 'undefined' && hasItem.type === 'host') {
invalidHasParts.push(`value is required for "host" type`)
}
if (invalidHasParts.length > 0) {
invalidHasItems.push(
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}`
)
}
}
}
if (invalidHasItems.length > 0) {
hadInvalidHas = true
const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}`
if (invalidHasItems.length > 0) {
hadInvalidItem = true
const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}`
console.error(
`Invalid \`has\` ${itemStr}:\n` + invalidHasItems.join('\n')
)
console.error()
invalidParts.push(`invalid \`has\` ${itemStr} found`)
console.error(
`Invalid \`${fieldName}\` ${itemStr}:\n` +
invalidHasItems.join('\n')
)
console.error()
invalidParts.push(`invalid \`${fieldName}\` ${itemStr} found`)
}
}
return hadInvalidItem
}
if (checkInvalidHasMissing(route.has, 'has')) {
hadInvalidHas = true
}
if (checkInvalidHasMissing(route.missing, 'missing')) {
hadInvalidMissing = true
}
if (!route.source) {
......@@ -421,6 +443,19 @@ export function checkCustomRoutes(
)}`
)
}
if (hadInvalidMissing) {
console.error(
`\nValid \`missing\` object shape is ${JSON.stringify(
{
type: [...allowedHasTypes].join(', '),
key: 'the key to check for',
value: 'undefined or a value string to match against',
},
null,
2
)}`
)
}
console.error()
console.error(
`Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found`
......
......@@ -30,6 +30,7 @@ type RouteResult = {
export type Route = {
match: RouteMatch
has?: RouteHas[]
missing?: RouteHas[]
type: string
check?: boolean
statusCode?: number
......@@ -416,8 +417,13 @@ export default class Router {
})
let params = route.match(matchPathname)
if (route.has && params) {
const hasParams = matchHas(req, route.has, parsedUrlUpdated.query)
if ((route.has || route.missing) && params) {
const hasParams = matchHas(
req,
parsedUrlUpdated.query,
route.has,
route.missing
)
if (hasParams) {
Object.assign(params, hasParams)
} else {
......
......@@ -25,8 +25,8 @@ export function getMiddlewareRouteMatcher(
continue
}
if (matcher.has) {
const hasParams = matchHas(req, matcher.has, query)
if (matcher.has || matcher.missing) {
const hasParams = matchHas(req, query, matcher.has, matcher.missing)
if (!hasParams) {
continue
}
......
......@@ -42,12 +42,13 @@ function unescapeSegments(str: string) {
export function matchHas(
req: BaseNextRequest | IncomingMessage,
has: RouteHas[],
query: Params
query: Params,
has: RouteHas[] = [],
missing: RouteHas[] = []
): false | Params {
const params: Params = {}
const allMatch = has.every((hasItem) => {
const hasMatch = (hasItem: RouteHas) => {
let value: undefined | string
let key = hasItem.key
......@@ -100,7 +101,11 @@ export function matchHas(
}
}
return false
})
}
const allMatch =
has.every((item) => hasMatch(item)) &&
!missing.some((item) => hasMatch(item))
if (allMatch) {
return params
......
......@@ -44,7 +44,7 @@ export default function resolveRewrites(
let params = matcher(parsedAs.pathname)
if (rewrite.has && params) {
if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas(
{
headers: {
......@@ -58,8 +58,9 @@ export default function resolveRewrites(
return acc
}, {}),
} as any,
parsedAs.query,
rewrite.has,
parsedAs.query
rewrite.missing
)
if (hasParams) {
......
......@@ -57,5 +57,25 @@ export const config = {
},
],
},
{
source: '/missing-match-1',
missing: [
{
type: 'header',
key: 'hello',
value: '(.*)',
},
],
},
{
source: '/missing-match-2',
missing: [
{
type: 'query',
key: 'test',
value: 'value',
},
],
},
],
}
......@@ -21,6 +21,28 @@ describe('Middleware custom matchers', () => {
afterAll(() => next.destroy())
const runTests = () => {
it('should match missing header correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-1')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-1', undefined, {
headers: {
hello: 'world',
},
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match missing query correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-2')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-2', {
test: 'value',
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match source path', async () => {
const res = await fetchViaHTTP(next.url, '/source-match')
expect(res.status).toBe(200)
......
module.exports = {
// target: 'serverless',
async rewrites() {
// no-rewrites comment
return {
......@@ -205,6 +204,38 @@ module.exports = {
],
destination: '/blog-catchall/:post',
},
{
source: '/missing-rewrite-1',
missing: [
{
type: 'header',
key: 'x-my-header',
value: '(?<myHeader>.*)',
},
],
destination: '/with-params',
},
{
source: '/missing-rewrite-2',
missing: [
{
type: 'query',
key: 'my-query',
},
],
destination: '/with-params',
},
{
source: '/missing-rewrite-3',
missing: [
{
type: 'cookie',
key: 'loggedIn',
value: '(?<loggedIn>true)',
},
],
destination: '/with-params?authorized=1',
},
{
source: '/blog/about',
destination: '/hello',
......
......@@ -20,7 +20,6 @@ import {
getBrowserBodyText,
waitFor,
normalizeRegEx,
initNextServerScript,
nextExport,
hasRedbox,
check,
......@@ -914,6 +913,52 @@ const runTests = (isDev = false) => {
)
})
it('should match missing header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-1', undefined, {
headers: {
'x-my-header': 'hello world!!',
},
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-1')
const $2 = cheerio.load(await res2.text())
expect(res2.status).toBe(200)
expect(JSON.parse($2('#query').text())).toEqual({})
})
it('should match missing query rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-2', {
'my-query': 'hellooo',
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-2')
const $2 = cheerio.load(await res2.text())
expect(res2.status).toBe(200)
expect(JSON.parse($2('#query').text())).toEqual({})
})
it('should match missing cookie rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-3', undefined, {
headers: {
cookie: 'loggedIn=true',
},
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-3')
const $2 = cheerio.load(await res2.text())
expect(JSON.parse($2('#query').text())).toEqual({
authorized: '1',
})
expect(res2.status).toBe(200)
})
it('should match has header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, {
headers: {
......@@ -2080,6 +2125,41 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/has-rewrite-8(?:\\/)?$'),
source: '/has-rewrite-8',
},
{
destination: '/with-params',
missing: [
{
key: 'x-my-header',
type: 'header',
value: '(?<myHeader>.*)',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-1(?:\\/)?$'),
source: '/missing-rewrite-1',
},
{
destination: '/with-params',
missing: [
{
key: 'my-query',
type: 'query',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-2(?:\\/)?$'),
source: '/missing-rewrite-2',
},
{
destination: '/with-params?authorized=1',
missing: [
{
key: 'loggedIn',
type: 'cookie',
value: '(?<loggedIn>true)',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-3(?:\\/)?$'),
source: '/missing-rewrite-3',
},
{
destination: '/hello',
regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'),
......@@ -2426,96 +2506,6 @@ describe('Custom routes', () => {
})
})
describe('serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/\/\/ target/, 'target'),
'utf8'
)
const { stdout: buildStdout } = await nextBuild(appDir, ['-d'], {
stdout: true,
})
stdout = buildStdout
appPort = await findPort()
app = await nextStart(appDir, appPort, {
onStdout: (msg) => {
stdout += msg
},
})
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(async () => {
await fs.writeFile(nextConfigPath, nextConfigContent, 'utf8')
await killApp(app)
})
runTests()
})
describe('raw serverless mode', () => {
beforeAll(async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/\/\/ target/, 'target'),
'utf8'
)
await nextBuild(appDir)
appPort = await findPort()
app = await initNextServerScript(join(appDir, 'server.js'), /ready on/, {
...process.env,
PORT: appPort,
})
})
afterAll(async () => {
await fs.writeFile(nextConfigPath, nextConfigContent, 'utf8')
await killApp(app)
})
it('should apply rewrites in lambda correctly for page route', async () => {
const html = await renderViaHTTP(appPort, '/query-rewrite/first/second')
const data = JSON.parse(cheerio.load(html)('p').text())
expect(data).toEqual({
first: 'first',
second: 'second',
section: 'first',
name: 'second',
})
})
it('should apply rewrites in lambda correctly for dynamic route', async () => {
const html = await renderViaHTTP(appPort, '/blog/post-1')
expect(html).toContain('post-2')
})
it('should apply rewrites in lambda correctly for API route', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/api-hello-param/first')
)
expect(data).toEqual({
query: {
name: 'first',
hello: 'first',
},
})
})
it('should apply rewrites in lambda correctly for dynamic API route', async () => {
const data = JSON.parse(
await renderViaHTTP(appPort, '/api-dynamic-param/first')
)
expect(data).toEqual({
query: {
slug: 'first',
hello: 'first',
},
})
})
})
describe('should load custom routes when only one type is used', () => {
const runSoloTests = (isDev) => {
const buildAndStart = async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment