https://github.com/hacknlove/cencode
best url friendly serializer
https://github.com/hacknlove/cencode
Last synced: over 1 year ago
JSON representation
best url friendly serializer
- Host: GitHub
- URL: https://github.com/hacknlove/cencode
- Owner: hacknlove
- Created: 2021-09-12T11:09:21.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-12T06:19:35.000Z (almost 4 years ago)
- Last Synced: 2025-01-08T20:50:59.842Z (over 1 year ago)
- Language: JavaScript
- Size: 101 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# cencode
## Quick howto
```
const { cencode, decencode } = require('cencode')
const serialized = cencode(ANYTHING)
const deserializez = decencode(serializez)
```
## Extend
It's possible to enable custom object serialization by using plugins.
Let's see how to add a plugin to serialize/deserialize Urls
```
const { cencode, pluginsEncode, decencode, pluginsDecode } = require('./')
pluginsEncode.push({ name: 'URL', match: x => x instanceof URL, values: x => [x.toString()] })
pluginsDecode.URL = x => new URL(x)
const serialized = cencode(['foo', 12, new URL('https://example.com')])
// -> '_JfooxC)JURLhKhttps://example.com/'
```
## Sign / verify
The `sign` and `verify` function accepts as its second parameter a callback function to add a signature and verify it.
```
const { sign, verify } = require('./')
const kissmyhash = require('kissmyhash')
const signit = (data) => kissmyhash([data, 'SALT'])
const verifyit = (signature, data) => kissmyhash(data, 'SALT') === signature ? data : false
const signed = sign(['foo', { bar: 42 }], signit)
const original = verify(signed, verifyit)
// -> ['foo', { bar: 42 }]
```
# cencode