Members
leveldb
The codec information for
encoding-down, which is a
codec framework for leveldb. CBOR is a particularly convenient format for
both keys and values, as it can deal with a lot of types that JSON can't
handle without losing type information.
- Source:
Example
const level = require('level')
const cbor = require('cbor')
async function putget() {
const db = level('./db', {
keyEncoding: cbor.leveldb,
valueEncoding: cbor.leveldb,
})
await db.put({a: 1}, 9857298342094820394820394820398234092834n)
const val = await db.get({a: 1})
}
Methods
reset()
Reset everything that we can predict a plugin might have altered in good
faith. For now that includes the default set of tags that decoding and
encoding will use.
- Source:
Type Definitions
BufferLike
Things that can act as inputs, from which a NoFilter can be created.
- Source:
Type:
-
string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray|DataView|stream.Readable
BufferLike
Things that can act as inputs, from which a NoFilter can be created.
- Source:
Type:
-
string|Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray|DataView|stream.Readable
commentCallback(erroropt, commentedopt) → {void}
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
error |
Error
|
<optional> |
If one was generated. |
commented |
string
|
<optional> |
The comment string. |
Returns:
- Type:
-
void
CommentOptions
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
max_depth |
number
|
<optional> |
10 | How many times to indent the dashes. |
depth |
number
|
<optional> |
1 | Initial indentation depth. |
no_summary |
boolean
|
<optional> |
false | If true, omit the summary of the full bytes read at the end. |
tags |
object
|
<optional> |
Mapping from tag number to function(v), where v is the decoded value that comes after the tag, and where the function returns the correctly-created value for that tag. | |
preferWeb |
boolean
|
<optional> |
false | If true, prefer Uint8Arrays to be generated instead of node Buffers. This might turn on some more changes in the future, so forward-compatibility is not guaranteed yet. |
encoding |
BufferEncoding
|
<optional> |
'hex' | Encoding to use for input, if it is a string. |
- Source:
decodeAllCallback(error, value)
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
error |
Error
|
If one was generated. |
value |
Array.<ExtendedResults>
|
Array.<any>
|
All of the decoded values, wrapped in an Array. |
decodeCallback(erroropt, valueopt) → {void}
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
error |
Error
|
<optional> |
If one was generated. |
value |
any
|
<optional> |
The decoded value. |
Returns:
- Type:
-
void
DecoderOptions
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
max_depth |
number
|
<optional> |
-1 | The maximum depth to parse. Use -1 for "until you run out of memory". Set this to a finite positive number for un-trusted inputs. Most standard inputs won't nest more than 100 or so levels; I've tested into the millions before running out of memory. |
tags |
Tagged.TagMap
|
<optional> |
Mapping from tag number to function(v), where v is the decoded value that comes after the tag, and where the function returns the correctly-created value for that tag. | |
preferWeb |
boolean
|
<optional> |
false | If true, prefer Uint8Arrays to be generated instead of node Buffers. This might turn on some more changes in the future, so forward-compatibility is not guaranteed yet. |
encoding |
BufferEncoding
|
<optional> |
'hex' | The encoding of the input. Ignored if input is a Buffer. |
required |
boolean
|
<optional> |
false | Should an error be thrown when no data is in the input? |
extendedResults |
boolean
|
<optional> |
false | If true, emit extended results, which will be an object with shape ExtendedResults. The value will already have been null-checked. |
preventDuplicateKeys |
boolean
|
<optional> |
false | If true, error is thrown if a map has duplicate keys. |
- Source:
diagnoseCallback(erroropt, valueopt) → {void}
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
error |
Error
|
<optional> |
If one was generated. |
value |
string
|
<optional> |
The diagnostic value. |
Returns:
- Type:
-
void
DiagnoseOptions
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
separator |
string
|
<optional> |
'\n' | Output between detected objects. |
stream_errors |
boolean
|
<optional> |
false | Put error info into the output stream. |
max_depth |
number
|
<optional> |
-1 | The maximum depth to parse. Use -1 for "until you run out of memory". Set this to a finite positive number for un-trusted inputs. Most standard inputs won't nest more than 100 or so levels; I've tested into the millions before running out of memory. |
tags |
object
|
<optional> |
Mapping from tag number to function(v), where v is the decoded value that comes after the tag, and where the function returns the correctly-created value for that tag. | |
preferWeb |
boolean
|
<optional> |
false | If true, prefer Uint8Arrays to be generated instead of node Buffers. This might turn on some more changes in the future, so forward-compatibility is not guaranteed yet. |
encoding |
BufferEncoding
|
<optional> |
'hex' | The encoding of input, ignored if input is not string. |
- Source:
EncodeFunction(enc, val) → {boolean}
Generate the CBOR for a value. If you are using this, you'll either need
to call Encoder.write with a Buffer, or look into the internals of
Encoder to reuse existing non-documented behavior.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
enc |
Encoder
|
The encoder to use. |
val |
any
|
The value to encode. |
Returns:
- Type:
-
boolean
True on success.
EncodingOptions
Properties:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
genTypes |
Array.<any>
|
object
|
<optional> |
[] | Array of pairs of `type`, `function(Encoder)` for semantic types to be encoded. Not needed for Array, Date, Buffer, Map, RegExp, Set, or URL. If an object, the keys are the constructor names for the types. |
canonical |
boolean
|
<optional> |
false | Should the output be canonicalized. |
detectLoops |
boolean
|
WeakSet
|
<optional> |
false | Should object loops be detected? This will currently add memory to track every part of the object being encoded in a WeakSet. Do not encode the same object twice on the same encoder, without calling `removeLoopDetectors` in between, which will clear the WeakSet. You may pass in your own WeakSet to be used; this is useful in some recursive scenarios. |
dateType |
"number"
|
"float"
|
"int"
|
"string"
|
<optional> |
"number" | how should dates be encoded? "number" means float or int, if no fractional seconds. |
encodeUndefined |
any
|
<optional> |
How should an "undefined" in the input be encoded. By default, just encode a CBOR undefined. If this is a buffer, use those bytes without re-encoding them. If this is a function, the function will be called (which is a good time to throw an exception, if that's what you want), and the return value will be used according to these rules. Anything else will be encoded as CBOR. | |
disallowUndefinedKeys |
boolean
|
<optional> |
false | Should "undefined" be disallowed as a key in a Map that is serialized? If this is true, encode(new Map([[undefined, 1]])) will throw an exception. Note that it is impossible to get a key of undefined in a normal JS object. |
collapseBigIntegers |
boolean
|
<optional> |
false | Should integers that come in as ECMAscript bigint's be encoded as normal CBOR integers if they fit, discarding type information? |
chunkSize |
number
|
<optional> |
4096 | Number of characters or bytes for each chunk, if obj is a string or Buffer, when indefinite encoding. |
omitUndefinedProperties |
boolean
|
<optional> |
false | When encoding objects or Maps, do not include a key if its corresponding value is `undefined`. |
- Source:
ExtendedResults
Properties:
| Name | Type | Attributes | Description |
|---|---|---|---|
value |
any
|
The value that was found. | |
length |
number
|
The number of bytes of the original input that were read. | |
bytes |
Buffer
|
The bytes of the original input that were used to produce the value. | |
unused |
Buffer
|
<optional> |
The bytes that were left over from the original
input. This property only exists if Decoder.decodeFirst or
Decoder.decodeFirstSync was called. |
- Source:
SemanticMap
A mapping from tag number to a tag decoding function.
- Source:
Type:
-
Object.<string, EncodeFunction>
TagFunction(value, tag) → {any}
Convert a tagged value to a more interesting JavaScript type. Errors
thrown in this function will be captured into the "err" property of the
original Tagged instance.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
value |
any
|
The value inside the tag. |
tag |
Tagged
|
The enclosing Tagged instance; useful if you want to modify it and return it. Also available as "this". |
Returns:
- Type:
-
any
The transformed value.
TagMap
A mapping from tag number to a tag decoding function.
- Source:
Type:
-
Object.<string, TagFunction>