Add missing matcher support

parent b9c74083
...@@ -25,6 +25,7 @@ export interface MiddlewareMatcher { ...@@ -25,6 +25,7 @@ export interface MiddlewareMatcher {
regexp: string regexp: string
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
export interface PageStaticInfo { export interface PageStaticInfo {
......
...@@ -157,8 +157,13 @@ export function getUtils({ ...@@ -157,8 +157,13 @@ export function getUtils({
) )
let params = matcher(parsedUrl.pathname) let params = matcher(parsedUrl.pathname)
if (rewrite.has && params) { if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas(req, rewrite.has, parsedUrl.query) const hasParams = matchHas(
req,
parsedUrl.query,
rewrite.has,
rewrite.missing
)
if (hasParams) { if (hasParams) {
Object.assign(params, hasParams) Object.assign(params, hasParams)
......
...@@ -24,6 +24,7 @@ export type Rewrite = { ...@@ -24,6 +24,7 @@ export type Rewrite = {
basePath?: false basePath?: false
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
export type Header = { export type Header = {
...@@ -32,6 +33,7 @@ export type Header = { ...@@ -32,6 +33,7 @@ export type Header = {
locale?: false locale?: false
headers: Array<{ key: string; value: string }> headers: Array<{ key: string; value: string }>
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
// internal type used for validation (not user facing) // internal type used for validation (not user facing)
...@@ -41,6 +43,7 @@ export type Redirect = { ...@@ -41,6 +43,7 @@ export type Redirect = {
basePath?: false basePath?: false
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} & ( } & (
| { | {
statusCode?: never statusCode?: never
...@@ -56,6 +59,7 @@ export type Middleware = { ...@@ -56,6 +59,7 @@ export type Middleware = {
source: string source: string
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host']) const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host'])
...@@ -132,8 +136,9 @@ export function checkCustomRoutes( ...@@ -132,8 +136,9 @@ export function checkCustomRoutes(
let numInvalidRoutes = 0 let numInvalidRoutes = 0
let hadInvalidStatus = false let hadInvalidStatus = false
let hadInvalidHas = 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') { if (type === 'rewrite') {
allowedKeys.add('basePath') allowedKeys.add('basePath')
...@@ -198,48 +203,65 @@ export function checkCustomRoutes( ...@@ -198,48 +203,65 @@ export function checkCustomRoutes(
invalidParts.push('`locale` must be undefined or false') invalidParts.push('`locale` must be undefined or false')
} }
if (typeof route.has !== 'undefined' && !Array.isArray(route.has)) { const checkInvalidHasMissing = (
invalidParts.push('`has` must be undefined or valid has object') items: any,
hadInvalidHas = true fieldName: 'has' | 'missing'
} else if (route.has) { ) => {
const invalidHasItems = [] let hadInvalidItem = false
for (const hasItem of route.has) { if (typeof items !== 'undefined' && !Array.isArray(items)) {
let invalidHasParts = [] invalidParts.push(
`\`${fieldName}\` must be undefined or valid has object`
)
hadInvalidItem = true
} else if (items) {
const invalidHasItems = []
if (!allowedHasTypes.has(hasItem.type)) { for (const hasItem of items) {
invalidHasParts.push(`invalid type "${hasItem.type}"`) let invalidHasParts = []
}
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) { if (!allowedHasTypes.has(hasItem.type)) {
invalidHasItems.push( invalidHasParts.push(`invalid type "${hasItem.type}"`)
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}` }
) 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) { if (invalidHasItems.length > 0) {
hadInvalidHas = true hadInvalidItem = true
const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}` const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}`
console.error( console.error(
`Invalid \`has\` ${itemStr}:\n` + invalidHasItems.join('\n') `Invalid \`${fieldName}\` ${itemStr}:\n` +
) invalidHasItems.join('\n')
console.error() )
invalidParts.push(`invalid \`has\` ${itemStr} found`) 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) { if (!route.source) {
...@@ -421,6 +443,19 @@ export function checkCustomRoutes( ...@@ -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()
console.error( console.error(
`Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found` `Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found`
......
...@@ -30,6 +30,7 @@ type RouteResult = { ...@@ -30,6 +30,7 @@ type RouteResult = {
export type Route = { export type Route = {
match: RouteMatch match: RouteMatch
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
type: string type: string
check?: boolean check?: boolean
statusCode?: number statusCode?: number
...@@ -416,8 +417,13 @@ export default class Router { ...@@ -416,8 +417,13 @@ export default class Router {
}) })
let params = route.match(matchPathname) let params = route.match(matchPathname)
if (route.has && params) { if ((route.has || route.missing) && params) {
const hasParams = matchHas(req, route.has, parsedUrlUpdated.query) const hasParams = matchHas(
req,
parsedUrlUpdated.query,
route.has,
route.missing
)
if (hasParams) { if (hasParams) {
Object.assign(params, hasParams) Object.assign(params, hasParams)
} else { } else {
......
...@@ -25,8 +25,8 @@ export function getMiddlewareRouteMatcher( ...@@ -25,8 +25,8 @@ export function getMiddlewareRouteMatcher(
continue continue
} }
if (matcher.has) { if (matcher.has || matcher.missing) {
const hasParams = matchHas(req, matcher.has, query) const hasParams = matchHas(req, query, matcher.has, matcher.missing)
if (!hasParams) { if (!hasParams) {
continue continue
} }
......
...@@ -42,12 +42,13 @@ function unescapeSegments(str: string) { ...@@ -42,12 +42,13 @@ function unescapeSegments(str: string) {
export function matchHas( export function matchHas(
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
has: RouteHas[], query: Params,
query: Params has: RouteHas[] = [],
missing: RouteHas[] = []
): false | Params { ): false | Params {
const params: Params = {} const params: Params = {}
const allMatch = has.every((hasItem) => { const hasMatch = (hasItem: RouteHas) => {
let value: undefined | string let value: undefined | string
let key = hasItem.key let key = hasItem.key
...@@ -100,7 +101,11 @@ export function matchHas( ...@@ -100,7 +101,11 @@ export function matchHas(
} }
} }
return false return false
}) }
const allMatch =
has.every((item) => hasMatch(item)) &&
!missing.some((item) => hasMatch(item))
if (allMatch) { if (allMatch) {
return params return params
......
...@@ -44,7 +44,7 @@ export default function resolveRewrites( ...@@ -44,7 +44,7 @@ export default function resolveRewrites(
let params = matcher(parsedAs.pathname) let params = matcher(parsedAs.pathname)
if (rewrite.has && params) { if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas( const hasParams = matchHas(
{ {
headers: { headers: {
...@@ -58,8 +58,9 @@ export default function resolveRewrites( ...@@ -58,8 +58,9 @@ export default function resolveRewrites(
return acc return acc
}, {}), }, {}),
} as any, } as any,
parsedAs.query,
rewrite.has, rewrite.has,
parsedAs.query rewrite.missing
) )
if (hasParams) { if (hasParams) {
......
...@@ -57,5 +57,25 @@ export const config = { ...@@ -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', () => { ...@@ -21,6 +21,28 @@ describe('Middleware custom matchers', () => {
afterAll(() => next.destroy()) afterAll(() => next.destroy())
const runTests = () => { 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 () => { it('should match source path', async () => {
const res = await fetchViaHTTP(next.url, '/source-match') const res = await fetchViaHTTP(next.url, '/source-match')
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
module.exports = { module.exports = {
// target: 'serverless',
async rewrites() { async rewrites() {
// no-rewrites comment // no-rewrites comment
return { return {
...@@ -205,6 +204,38 @@ module.exports = { ...@@ -205,6 +204,38 @@ module.exports = {
], ],
destination: '/blog-catchall/:post', 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', source: '/blog/about',
destination: '/hello', destination: '/hello',
......
...@@ -20,7 +20,6 @@ import { ...@@ -20,7 +20,6 @@ import {
getBrowserBodyText, getBrowserBodyText,
waitFor, waitFor,
normalizeRegEx, normalizeRegEx,
initNextServerScript,
nextExport, nextExport,
hasRedbox, hasRedbox,
check, check,
...@@ -914,6 +913,52 @@ const runTests = (isDev = false) => { ...@@ -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 () => { it('should match has header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, { const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, {
headers: { headers: {
...@@ -2080,6 +2125,41 @@ const runTests = (isDev = false) => { ...@@ -2080,6 +2125,41 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/has-rewrite-8(?:\\/)?$'), regex: normalizeRegEx('^\\/has-rewrite-8(?:\\/)?$'),
source: '/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', destination: '/hello',
regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'), regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'),
...@@ -2426,96 +2506,6 @@ describe('Custom routes', () => { ...@@ -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', () => { describe('should load custom routes when only one type is used', () => {
const runSoloTests = (isDev) => { const runSoloTests = (isDev) => {
const buildAndStart = async () => { 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