Gathering detailed insights and metrics for encoding-down
Gathering detailed insights and metrics for encoding-down
Gathering detailed insights and metrics for encoding-down
Gathering detailed insights and metrics for encoding-down
@types/encoding-down
TypeScript definitions for encoding-down
level-packager
levelup package helper for distributing with an abstract-leveldown store
@smithy/util-hex-encoding
Converts binary buffers to and from lowercase hexadecimal encoding
html-encoding-sniffer
Sniff the encoding from a HTML byte stream
An abstract-leveldown implementation that wraps another store to encode keys and values.
npm install encoding-down
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
41 Stars
184 Commits
8 Forks
10 Watching
3 Branches
12 Contributors
Updated on 17 Oct 2023
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
31.4%
262,018
Compared to previous day
Last week
14.2%
1,234,085
Compared to previous week
Last month
11.1%
4,391,832
Compared to previous month
Last year
25.2%
24,178,323
Compared to previous year
4
An abstract-leveldown
implementation that wraps another store to encode keys and values.
:pushpin: This module will soon be deprecated, because its functionality is included in
abstract-level
.
Stores like leveldown
can only store strings and Buffers. Other types, though accepted, are serialized before storage, which is an irreversible type conversion. For a richer set of data types you can wrap such a store with encoding-down
. It allows you to specify an encoding to use for keys and values independently. This not only widens the range of input types, but also limits the range of output types. The encoding is applied to all read and write operations: it encodes writes and decodes reads.
Many encodings are builtin courtesy of level-codec
. The default encoding is utf8
which ensures you'll always get back a string. You can also provide a custom encoding like bytewise
- or your own!
Without any options, encoding-down
defaults to the utf8
encoding.
1const levelup = require('levelup') 2const leveldown = require('leveldown') 3const encode = require('encoding-down') 4 5const db = levelup(encode(leveldown('./db1'))) 6 7db.put('example', Buffer.from('encoding-down'), function (err) { 8 db.get('example', function (err, value) { 9 console.log(typeof value, value) // 'string encoding-down' 10 }) 11})
Can we store objects? Yes!
1const db = levelup(encode(leveldown('./db2'), { valueEncoding: 'json' })) 2 3db.put('example', { awesome: true }, function (err) { 4 db.get('example', function (err, value) { 5 console.log(value) // { awesome: true } 6 console.log(typeof value) // 'object' 7 }) 8})
How about storing Buffers, but getting back a hex-encoded string?
1const db = levelup(encode(leveldown('./db3'), { valueEncoding: 'hex' })) 2 3db.put('example', Buffer.from([0, 255]), function (err) { 4 db.get('example', function (err, value) { 5 console.log(typeof value, value) // 'string 00ff' 6 }) 7})
What if we previously stored binary data?
1const db = levelup(encode(leveldown('./db4'), { valueEncoding: 'binary' })) 2 3db.put('example', Buffer.from([0, 255]), function (err) { 4 db.get('example', function (err, value) { 5 console.log(typeof value, value) // 'object <Buffer 00 ff>' 6 }) 7 8 // Override the encoding for this operation 9 db.get('example', { valueEncoding: 'base64' }, function (err, value) { 10 console.log(typeof value, value) // 'string AP8=' 11 }) 12})
And what about keys?
1const db = levelup(encode(leveldown('./db5'), { keyEncoding: 'json' })) 2 3db.put({ awesome: true }, 'example', function (err) { 4 db.get({ awesome: true }, function (err, value) { 5 console.log(value) // 'example' 6 }) 7})
1const db = levelup(encode(leveldown('./db6'), { keyEncoding: 'binary' })) 2 3db.put(Buffer.from([0, 255]), 'example', function (err) { 4 db.get('00ff', { keyEncoding: 'hex' }, function (err, value) { 5 console.log(value) // 'example' 6 }) 7})
level
The level
module conveniently bundles encoding-down
and passes its options
to encoding-down
. This means you can simply do:
1const level = require('level') 2const db = level('./db7', { valueEncoding: 'json' }) 3 4db.put('example', 42, function (err) { 5 db.get('example', function (err, value) { 6 console.log(value) // 42 7 console.log(typeof value) // 'number' 8 }) 9})
db = require('encoding-down')(db[, options])
db
must be an abstract-leveldown
compliant storeoptions
are passed to level-codec
:
keyEncoding
: encoding to use for keysvalueEncoding
: encoding to use for valuesBoth encodings default to 'utf8'
. They can be a string (builtin level-codec
encoding) or an object (custom encoding).
Please refer to level-codec
documentation for a precise description of the format. Here's a quick example with level
and async/await
just for fun:
1const level = require('level') 2const lexint = require('lexicographic-integer') 3 4async function main () { 5 const db = level('./db8', { 6 keyEncoding: { 7 type: 'lexicographic-integer', 8 encode: (n) => lexint.pack(n, 'hex'), 9 decode: lexint.unpack, 10 buffer: false 11 } 12 }) 13 14 await db.put(2, 'example') 15 await db.put(10, 'example') 16 17 // Without our encoding, the keys would sort as 10, 2. 18 db.createKeyStream().on('data', console.log) // 2, 10 19} 20 21main()
With an npm-installed encoding (modularity ftw!) we can reduce the above to:
1const level = require('level') 2const lexint = require('lexicographic-integer-encoding')('hex') 3 4const db = level('./db8', { 5 keyEncoding: lexint 6})
Level/encoding-down
is an OPEN Open Source Project. This means that:
Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project.
See the Contribution Guide for more details.
Support us with a monthly donation on Open Collective and help us continue our work.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/22 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More