-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
184 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
|
||
### 1.0.3 (2021-07-25) | ||
|
||
### 1.0.2 (2021-07-19) | ||
|
||
### 1.0.1 (2021-07-19) | ||
|
||
## [1.0.0](https://github.com/Kikobeats/keyv-offline/compare/v0.0.1...v1.0.0) (2021-07-16) | ||
|
||
### 0.0.1 (2021-07-16) |
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,52 @@ | ||
# @keyvhq/compress [<img width="100" align="right" src="https://keyv.js.org/media/logo-sunset.svg" alt="keyv">](https://github.com/microlinkhq/keyv) | ||
|
||
> Adding offline capabilities for your keyv instance. | ||
## Install | ||
|
||
```bash | ||
$ npm install @keyvhq/compress --save | ||
``` | ||
|
||
## Usage | ||
|
||
All you need to do is to wrap your [keyv](https://keyv.js.org) instance: | ||
|
||
```js | ||
|
||
const KeyvRedis = require('@keyvhq/redis') | ||
|
||
const keyv = new KeyvRedis({ | ||
uri: 'redis://user:pass@localhost:6379', | ||
maxRetriesPerRequest: 1, | ||
emitErrors: false | ||
}) | ||
``` | ||
|
||
Using `@keyvhq/compress` at the top level: | ||
|
||
```js | ||
const KeyvRedis = require('@keyvhq/redis') | ||
const keyvOffline = require('@keyvhq/compress') | ||
|
||
const keyv = keyvOffline(new KeyvRedis({ | ||
uri: 'redis://user:pass@localhost:6379', | ||
maxRetriesPerRequest: 1, | ||
emitErrors: false | ||
})) | ||
``` | ||
|
||
That's all! | ||
|
||
In the next database downtime, your keyv set/get petitions will be temporarily bypassed, preventing your application to crash for that, being more resilient than the default keyv behavior. | ||
|
||
As soon as the connection is re-established it will be work back as expected. | ||
|
||
In case you need, you can see omitted errors enabling debug doing `DEBUG=@keyvhq/compress*` | ||
|
||
## License | ||
|
||
**@keyvhq/memoize** © [Kiko Beats](https://kikobeats.com), Released under the [MIT](https://github.com/microlinkhq/keyv/blob/master/LICENSE.md) License.<br/> | ||
Maintained by [Microlink](https://microlink.io) with help from [contributors](https://github.com/microlinkhq/keyv/contributors). | ||
|
||
> [microlink.io](https://microlink.io) · GitHub [@MicrolinkHQ](https://github.com/microlinkhq) · Twitter [@microlinkhq](https://twitter.com/microlinkhq) |
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,56 @@ | ||
{ | ||
"name": "@keyvhq/compress", | ||
"description": "Adds compression bindings for your Keyv instance, saving as much space as you can.", | ||
"homepage": "https://keyv.js.org", | ||
"version": "1.0.3", | ||
"main": "src/index.js", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "microlink.io", | ||
"url": "https://microlink.io" | ||
}, | ||
"repository": { | ||
"directory": "packages/offline", | ||
"type": "git", | ||
"url": "git+https://github.com/microlinkhq/keyv.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/microlinkhq/keyv/issues" | ||
}, | ||
"keywords": [ | ||
"brotli", | ||
"cache", | ||
"compress", | ||
"compression", | ||
"decompress", | ||
"deserialize", | ||
"key", | ||
"keyv", | ||
"serialize", | ||
"store", | ||
"ttl", | ||
"value" | ||
], | ||
"dependencies": { | ||
"compress-brotli": "~1.3.2" | ||
}, | ||
"devDependencies": { | ||
"@keyvhq/core": "latest", | ||
"ava": "latest", | ||
"delay": "latest", | ||
"nyc": "latest" | ||
}, | ||
"engines": { | ||
"node": ">= 12" | ||
}, | ||
"files": [ | ||
"src" | ||
], | ||
"scripts": { | ||
"test": "nyc ava" | ||
}, | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,22 @@ | ||
'use strict' | ||
|
||
const compressBrotli = require('compress-brotli') | ||
|
||
function KeyvCompress (keyv, opts) { | ||
if (!(this instanceof KeyvCompress)) return new KeyvCompress(keyv, opts) | ||
|
||
const brotli = compressBrotli(opts) | ||
|
||
keyv.serialize = async ({ value, expires }) => { | ||
return brotli.serialize({ value: await brotli.compress(value), expires }) | ||
} | ||
|
||
keyv.deserialize = async data => { | ||
const { value, expires } = brotli.deserialize(data) | ||
return { value: await brotli.decompress(value), expires } | ||
} | ||
|
||
return keyv | ||
} | ||
|
||
module.exports = KeyvCompress |
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,34 @@ | ||
'use strict' | ||
|
||
const compressBrotli = require('compress-brotli') | ||
const JSONB = require('json-buffer') | ||
const Keyv = require('@keyvhq/core') | ||
const test = require('ava') | ||
|
||
const KeyvCompress = require('..') | ||
|
||
test('pass compress options', async t => { | ||
const store = new Map() | ||
const keyv = KeyvCompress(new Keyv({ store, namespace: null }), { | ||
enable: false | ||
}) | ||
|
||
await keyv.set('foo', 'bar') | ||
t.is(store.get('foo'), JSONB.stringify({ value: 'bar', expires: null })) | ||
}) | ||
|
||
test('enable compression', async t => { | ||
const brotli = compressBrotli() | ||
const store = new Map() | ||
const keyv = KeyvCompress(new Keyv({ store, namespace: null })) | ||
await keyv.set('foo', 'bar') | ||
const compressed = await brotli.compress('bar') | ||
|
||
t.is( | ||
store.get('foo'), | ||
JSONB.stringify({ | ||
value: compressed, | ||
expires: null | ||
}) | ||
) | ||
}) |