Skip to content

Commit

Permalink
Merge b973eef into 7538f2c
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 6, 2019
2 parents 7538f2c + b973eef commit 56438ad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -149,7 +149,13 @@ If you don't provide one, this be used as fallback for avoid keep things into ca
Type: `boolean`<br>
Default: `false`

Enable compress/decmpress data using brotli compression format.
Enable compress/decompress data using brotli compression format.

If you enable it, you need to an additional `iltorb` package:

```bash
npm install iltorb
```

##### revalidate

Expand Down
16 changes: 8 additions & 8 deletions index.js
@@ -1,5 +1,6 @@
'use strict'

const createCompress = require('compress-brotli')
const { resolve: urlResolve } = require('url')
const normalizeUrl = require('normalize-url')
const { parse } = require('querystring')
Expand All @@ -9,9 +10,7 @@ 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 getEtag = data => computeEtag(JSON.stringify(data))

const getKey = url => {
const { origin } = new URL(url)
Expand Down Expand Up @@ -44,8 +43,8 @@ const createSetHeaders = ({ revalidate }) => {
}

module.exports = ({
cache = new Keyv({ namespace: 'ssr' }),
compress = false,
cache = new Keyv(),
compress: enableCompression = false,
get,
send,
revalidate = ttl => ttl / 24,
Expand All @@ -58,6 +57,8 @@ module.exports = ({
revalidate: typeof revalidate === 'function' ? revalidate : () => revalidate
})

const { compress, decompress } = createCompress({ enable: enableCompression })

return async ({ req, res, ...opts }) => {
const hasForce = Boolean(req.query ? req.query.force : parse(req.url.split('?')[1]).force)
const url = urlResolve('http://localhost', req.url)
Expand All @@ -67,8 +68,7 @@ module.exports = ({
const hasData = cachedData !== undefined
const isHit = !hasForce && hasData

const cachedResult =
compress && hasData ? JSON.parse(await brotliDecompress(cachedData)) : cachedData
const cachedResult = await decompress(cachedData)

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

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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -28,8 +28,8 @@
"ssr"
],
"dependencies": {
"compress-brotli": "~1.0.1",
"etag": "~1.8.1",
"iltorb": "~2.4.2",
"keyv": "~3.1.0",
"normalize-url": "~4.3.0",
"pretty-ms": "~4.0.0"
Expand All @@ -46,6 +46,7 @@
"git-dirty": "latest",
"got": "latest",
"husky": "latest",
"iltorb": "latest",
"lint-staged": "latest",
"micro": "latest",
"npm-check-updates": "latest",
Expand Down

0 comments on commit 56438ad

Please sign in to comment.