Skip to content

Commit a01125e

Browse files
authored
Merge pull request #20 from Kikobeats/footprint
feat: reduce library footprint
2 parents 7538f2c + 16aad9d commit a01125e

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ If you don't provide one, this be used as fallback for avoid keep things into ca
149149
Type: `boolean`<br>
150150
Default: `false`
151151

152-
Enable compress/decmpress data using brotli compression format.
152+
Enable compress/decompress data using brotli compression format.
153+
154+
If you enable it, you need to an additional `iltorb` package:
155+
156+
```bash
157+
npm install iltorb
158+
```
153159

154160
##### revalidate
155161

Diff for: index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const createCompress = require('compress-brotli')
34
const { resolve: urlResolve } = require('url')
45
const normalizeUrl = require('normalize-url')
56
const { parse } = require('querystring')
@@ -9,9 +10,7 @@ const assert = require('assert')
910
const { URL } = require('url')
1011
const Keyv = require('keyv')
1112

12-
const { decompress: brotliDecompress, compress: brotliCompress } = require('iltorb')
13-
14-
const getEtag = data => computeEtag(typeof data === 'string' ? data : JSON.stringify(data))
13+
const getEtag = data => computeEtag(JSON.stringify(data))
1514

1615
const getKey = url => {
1716
const { origin } = new URL(url)
@@ -45,7 +44,7 @@ const createSetHeaders = ({ revalidate }) => {
4544

4645
module.exports = ({
4746
cache = new Keyv({ namespace: 'ssr' }),
48-
compress = false,
47+
compress: enableCompression = false,
4948
get,
5049
send,
5150
revalidate = ttl => ttl / 24,
@@ -58,6 +57,8 @@ module.exports = ({
5857
revalidate: typeof revalidate === 'function' ? revalidate : () => revalidate
5958
})
6059

60+
const { compress, decompress } = createCompress({ enable: enableCompression })
61+
6162
return async ({ req, res, ...opts }) => {
6263
const hasForce = Boolean(req.query ? req.query.force : parse(req.url.split('?')[1]).force)
6364
const url = urlResolve('http://localhost', req.url)
@@ -67,8 +68,7 @@ module.exports = ({
6768
const hasData = cachedData !== undefined
6869
const isHit = !hasForce && hasData
6970

70-
const cachedResult =
71-
compress && hasData ? JSON.parse(await brotliDecompress(cachedData)) : cachedData
71+
const cachedResult = await decompress(cachedData)
7272

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

8888
if (!isHit) {
8989
const payload = { etag, createdAt, ttl, data, ...props }
90-
const value = compress ? await brotliCompress(Buffer.from(JSON.stringify(payload))) : payload
90+
const value = await compress(payload)
9191
await cache.set(key, value, ttl)
9292
}
9393

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"ssr"
2929
],
3030
"dependencies": {
31+
"compress-brotli": "~1.0.1",
3132
"etag": "~1.8.1",
32-
"iltorb": "~2.4.2",
3333
"keyv": "~3.1.0",
3434
"normalize-url": "~4.3.0",
3535
"pretty-ms": "~4.0.0"
@@ -46,6 +46,7 @@
4646
"git-dirty": "latest",
4747
"got": "latest",
4848
"husky": "latest",
49+
"iltorb": "latest",
4950
"lint-staged": "latest",
5051
"micro": "latest",
5152
"npm-check-updates": "latest",

0 commit comments

Comments
 (0)