@nhtio/lucid-resourceful/encoding
Portable cross-environment encoding and decoding of structured data.
Decoding is treated as untrusted input handling: function rehydration (fns:d records) is off by default and rejected with E_UNDECODABLE_VALUE. Decoding is also bounded by default to 16 MiB of inflated input, depth 32, and arrays of 1,048,576 entries. Raise a limit for a trusted, expected payload with decode(payload, { limits: { maxDepth: 64 } }). Unsafe object keys are rejected unless allowUnsafeKeys is explicitly true. Pass { allowFunctions: true } to decode() only for payloads you produced yourself or otherwise fully trust — rehydrated functions are built with the Function constructor from the payload's own text, so enabling it for untrusted input allows arbitrary code execution.
Classes
| Class | Description |
|---|---|
| E_CIRCULAR_REFERENCE | Exception thrown when a circular reference is detected during encoding. Circular references cannot be encoded and must be avoided. |
| E_INCOMPATIBLE_VERSION | Exception thrown when attempting to decode data encoded with an incompatible version. This typically occurs when the encoded data is from a newer version than the decoder supports. |
| E_INVALID_VERSION | Exception thrown when an invalid version string is encountered. |
| E_NOT_AN_ENCODED_VALUE | Exception thrown when attempting to decode a value that is not a valid encoded value. This indicates the input string is not in the expected encoded format. |
| E_UNDECODABLE_VALUE | Exception thrown when a value with an unknown or unsupported type cannot be decoded. |
| E_UNENCODABLE_VALUE | Exception thrown when a value cannot be encoded. This indicates that the value type is not supported by the encoder. |
| FunctionSerializer | A portable function serializer that can dehydrate and rehydrate functions from strings, with support for named bindings and various function types. |
Interfaces
| Interface | Description |
|---|---|
| BaseException | Base exception class that extends the native Error class. Provides a foundation for all custom exceptions with additional metadata like error codes, status codes, and help descriptions. |
| DecodeBudget | An opaque, shared inflation budget created by createDecodeBudget(). Limits and byte usage are read-only; depth remains per decode() call and array-length limits remain per value. |
| DecodeLimits | Resource limits for decoding. Positive numbers or Infinity (explicit opt-out). |
| DecodeOptions | Options that control how a value is decoded. |
Type Aliases
| Type Alias | Description |
|---|---|
| DehydratedFunctionValue | Type definition for a dehydrated function value |
| Encodable | Defines all the types which can be be encoded |
Variables
| Variable | Description |
|---|---|
| createDecodeBudget | Creates an opaque budget sharing total inflated bytes across decode() calls. Limits are merged with DEFAULT_DECODE_LIMITS and must be positive finite numbers or Infinity; invalid values throw TypeError. Depth remains per call and array-length caps remain per value. Diagnostics are read-only. |
| decode | Decodes a compressed base64 string back into a value |
| DEFAULT_DECODE_LIMITS | Safe defaults for decoding untrusted input. |
| encode | Encodes a value into a compressed base64 string |
| version | The current version of the package. |