hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Improve type support #198

Closed kanongil closed 5 years ago

kanongil commented 6 years ago

The different engines are responsible for serializing the user provided value before storing it. Most use JSON.stringify() for this. Unfortunately, this fails to properly encode some common value types. Most prominently Date, Buffer, Set, and Map objects.

Currently, catbox takes a hands-off approach, where the engine is responsible, which causes the engines to support these differently. Eg. catbox-mongodb encode to BSON, while catbox-memory has an option to handle top-level Buffer objects.

This needs to be improved in order to finish #196.

I see two options:

  1. Pre-serialize the value to a string using some scheme before sending it to the engine, and de-serialize it when fetching it.
  2. Create a helper that engines can use to serialize these "fancy" types.

I plan to implement option 2 to keep it flexible. This will require most addons to update their serialization. To simplify this, a new helper addon is created.

kanongil commented 6 years ago

Requirements for a new serialization scheme:

  1. JSON compliant.
  2. Serialize as plain JSON when no fancy types are present.
  3. deepEqual(obj, parse(stringify(obj))) when using supported types.
  4. Somewhat compact.
  5. No configuration.
  6. Upgradeable with new types (eg. BigInt).
kanongil commented 6 years ago

I created a "helper" addon for this here: https://github.com/kanongil/granola. While it works 100%, it is in a pre-release state since I might still change the encoded syntax / error handling.

This is intended to replace JSON.stringify() and JSON.parse() when handling the envelope (using Granola.stringify() and Granola.parse()). It should work as a drop-in replacement with the advantage of supporting more JS types.

Let me know what you think.

@hueniverse Does it make sense to move it to the hapijs org?

hueniverse commented 6 years ago

If you want catbox to depend on it, yes, it should be moved.

kanongil commented 6 years ago

Possibly as a dev-dependency for catbox itself but probably not. Normal usage would be in engines like catbox-memory.

hueniverse commented 6 years ago

Same thing... :-)

hueniverse commented 5 years ago

Status update?

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.