-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customize bypass query parameter (#79)
* chore: use @keyvhq/core instead of keyv * chore: add BYPASS cache state * ci: use github actions * chore: remove keyv dependency * feat: customize bypass query parameter closes #72 * docs: add bypassQueryParameter
- Loading branch information
Showing
7 changed files
with
169 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
const got = require('got') | ||
|
||
const { parseCacheControl, createServer } = require('./util') | ||
|
||
test('ttl', async t => { | ||
const url = await createServer({ | ||
get: ({ req, res }) => ({ data: { foo: 'bar' }, ttl: 86400000 }), | ||
send: ({ data, headers, res, req, ...props }) => { | ||
res.end('Welcome to Micro') | ||
} | ||
}) | ||
|
||
const { headers } = await got(`${url}/kikobeats`) | ||
const cacheControl = parseCacheControl(headers) | ||
|
||
t.true(cacheControl.public) | ||
t.true(cacheControl['must-revalidate']) | ||
t.true([86399, 86400].includes(cacheControl['max-age'])) | ||
t.true([17279, 17280].includes(cacheControl['stale-while-revalidate'])) | ||
t.true([17279, 17280].includes(cacheControl['stale-if-error'])) | ||
}) | ||
|
||
test('revalidate', async t => { | ||
const url = await createServer({ | ||
revalidate: ttl => ttl * 0.1, | ||
get: ({ req, res }) => ({ data: { foo: 'bar' }, ttl: 86400000 }), | ||
send: ({ data, headers, res, req, ...props }) => { | ||
res.end('Welcome to Micro') | ||
} | ||
}) | ||
|
||
const { headers } = await got(`${url}/kikobeats`) | ||
const cacheControl = parseCacheControl(headers) | ||
|
||
t.true(cacheControl.public) | ||
t.true(cacheControl['must-revalidate']) | ||
t.true([86399, 86400].includes(cacheControl['max-age'])) | ||
t.true([8639, 8640].includes(cacheControl['stale-while-revalidate'])) | ||
t.true([8639, 8640].includes(cacheControl['stale-if-error'])) | ||
}) | ||
|
||
test('fixed revalidate', async t => { | ||
const url = await createServer({ | ||
revalidate: 300000, | ||
get: ({ req, res }) => ({ data: { foo: 'bar' }, ttl: 86400000 }), | ||
send: ({ data, headers, res, req, ...props }) => { | ||
res.end('Welcome to Micro') | ||
} | ||
}) | ||
|
||
const { headers } = await got(`${url}/kikobeats`) | ||
const cacheControl = parseCacheControl(headers) | ||
|
||
t.true(cacheControl.public) | ||
t.true(cacheControl['must-revalidate']) | ||
t.true([86399, 86400].includes(cacheControl['max-age'])) | ||
t.true([299, 300].includes(cacheControl['stale-while-revalidate'])) | ||
t.true([299, 300].includes(cacheControl['stale-if-error'])) | ||
}) | ||
|
||
test('bypass query parameter', async t => { | ||
const url = await createServer({ | ||
bypassQueryParameter: 'bypass', | ||
get: ({ req, res }) => { | ||
return { | ||
data: { foo: 'bar' }, | ||
ttl: 86400000, | ||
createdAt: Date.now(), | ||
foo: { bar: true } | ||
} | ||
}, | ||
send: ({ data, headers, res, req, ...props }) => { | ||
res.end('Welcome to Micro') | ||
} | ||
}) | ||
|
||
const { headers: headersOne } = await got(`${url}/kikobeats`) | ||
t.is(headersOne['x-cache-status'], 'MISS') | ||
|
||
const { headers: headersTwo } = await got(`${url}/kikobeats`) | ||
t.is(headersTwo['x-cache-status'], 'HIT') | ||
|
||
const { headers: headersThree } = await got(`${url}/kikobeats?bypass=true`) | ||
t.is(headersThree['x-cache-status'], 'BYPASS') | ||
t.is(headersThree['x-cache-expired-at'], '0ms') | ||
|
||
const { headers: headersFour } = await got(`${url}/kikobeats`) | ||
t.is(headersFour['x-cache-status'], 'HIT') | ||
|
||
const { headers: headersFive } = await got(`${url}/kikobeats?force=true`) | ||
t.is(headersFive['x-cache-status'], 'MISS') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
|
||
const cacheableResponse = require('..') | ||
|
||
const { AssertionError } = require('assert') | ||
|
||
test('.get', t => { | ||
const error = t.throws(() => cacheableResponse({})) | ||
t.true(error instanceof AssertionError) | ||
t.is(error.message, '.get required') | ||
}) | ||
|
||
test('.send', t => { | ||
const error = t.throws(() => cacheableResponse({ get: true })) | ||
t.true(error instanceof AssertionError) | ||
t.is(error.message, '.send required') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const cacheableResponse = require('..') | ||
const listen = require('test-listen') | ||
const micro = require('micro') | ||
|
||
const createServer = props => { | ||
const server = cacheableResponse(props) | ||
const api = micro((req, res) => server({ req, res })) | ||
return listen(api) | ||
} | ||
|
||
const parseCacheControl = headers => { | ||
const header = headers['cache-control'] | ||
return header.split(', ').reduce((acc, rawKey) => { | ||
let value = true | ||
let key = rawKey | ||
if (rawKey.includes('=')) { | ||
const [parsedKey, parsedValue] = rawKey.split('=') | ||
key = parsedKey | ||
value = Number(parsedValue) | ||
} | ||
return { ...acc, [key]: value } | ||
}, {}) | ||
} | ||
|
||
module.exports = { | ||
parseCacheControl, | ||
createServer | ||
} |