Skip to content

Commit

Permalink
feat: add buffer stringify support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 1, 2019
1 parent 895d35a commit 67757a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
20 changes: 7 additions & 13 deletions index.js
Expand Up @@ -2,26 +2,20 @@

const { promisify } = require('util')
const zlib = require('zlib')
const JSONB = require('buffer-json')

const hasNativeAPI = Boolean(zlib.brotliCompress)

const defaultSerialize = val => Buffer.from(JSON.stringify(val))
const defaultDeserialize = JSON.parse

const noop = {
deserialize: defaultDeserialize,
serialize: defaultSerialize,
decompress: data => data,
compress: data => data
}
const identity = val => val

const createCompress = ({
enable = true,
serialize = defaultSerialize,
deserialize = defaultDeserialize,
serialize = JSONB.stringify,
deserialize = JSONB.parse,
iltorb = () => require('iltorb')
} = {}) => {
if (!enable) return noop
if (!enable) {
return { serialize, deserialize, decompress: identity, compress: identity }
}

const compress = hasNativeAPI
? promisify(zlib.brotliCompress)
Expand Down
29 changes: 15 additions & 14 deletions package.json
Expand Up @@ -81,19 +81,20 @@
}
},
"lint-staged": {
"linters": {
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
],
"*.md": [
"standard-markdown",
"git add"
]
}
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
],
"*.md": [
"standard-markdown",
"git add"
]
},
"dependencies": {
"buffer-json": "~2.0.0"
}
}

0 comments on commit 67757a8

Please sign in to comment.