Skip to content

Commit

Permalink
Merge 3ce5fd7 into 65353f7
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 28, 2019
2 parents 65353f7 + 3ce5fd7 commit ed6a64a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -144,6 +144,13 @@ This value can be specified as well providing it as part of [`.get`](#get) outpu

If you don't provide one, this be used as fallback for avoid keep things into cache forever.

##### compress

Type: `boolean`<br>
Default: `false`

Enable compress/decmpress data using brotli compression format.

##### revalidate

Type: `function`|`number`<br>
Expand Down
15 changes: 12 additions & 3 deletions index.js
Expand Up @@ -9,6 +9,8 @@ const assert = require('assert')
const { URL } = require('url')
const Keyv = require('keyv')

const { decompress: brotliDecompress, compress: brotliCompress } = require('iltorb')

const getEtag = data => computeEtag(typeof data === 'string' ? data : JSON.stringify(data))

const getKey = url => {
Expand Down Expand Up @@ -43,6 +45,7 @@ const createSetHeaders = ({ revalidate }) => {

module.exports = ({
cache = new Keyv({ namespace: 'ssr' }),
compress = false,
get,
send,
revalidate = ttl => ttl / 24,
Expand All @@ -59,8 +62,12 @@ module.exports = ({
const hasForce = Boolean(req.query ? req.query.force : parse(req.url.split('?')[1]).force)
const url = urlResolve('http://localhost', req.url)
const key = getKey(url)
const cachedResult = await cache.get(key)
const isHit = !hasForce && cachedResult !== undefined

const cachedData = await cache.get(key)
const hasData = cachedData !== undefined
const cachedResult =
compress && hasData ? JSON.parse(await brotliDecompress(cachedData)) : cachedData
const isHit = !hasForce && hasData

const { etag: cachedEtag, ttl = defaultTtl, createdAt = Date.now(), data, ...props } = isHit
? cachedResult || {}
Expand All @@ -78,7 +85,9 @@ module.exports = ({
})

if (!isHit) {
await cache.set(key, { etag, createdAt, ttl, data, ...props }, ttl)
const payload = { etag, createdAt, ttl, data, ...props }
const value = compress ? await brotliCompress(Buffer.from(JSON.stringify(payload))) : payload
await cache.set(key, value, ttl)
}

send({ data, res, req, ...props })
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
],
"dependencies": {
"etag": "~1.8.1",
"iltorb": "~2.4.2",
"keyv": "~3.1.0",
"normalize-url": "~4.2.0",
"pretty-ms": "~4.0.0"
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Expand Up @@ -187,3 +187,25 @@ test('etag is present', async t => {
t.is(headersTwo['x-cache-status'], 'HIT')
t.is(headersOne.etag, headersTwo.etag)
})

test('compress support', async t => {
const url = await createServer({
compress: true,
get: ({ req, res }) => {
return {
data: { foo: 'bar' },
ttl: 30000,
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')
t.is(headersOne.etag, headersTwo.etag)
})

0 comments on commit ed6a64a

Please sign in to comment.