lazycrate

Lazily serialize and deserialize variables

Usage no npm install needed!

<script type="module">
  import lazycrate from 'https://cdn.skypack.dev/lazycrate';
</script>

README

Lazycrate

Build Status Coverage Node LTS Node v15+

Lazily and easily serialize and deserialize variables

const lazycrate = require('laycrate').sync; // Use the synchronous version

function stringifiedClass() {

  // Test class
  class TestClass {
    constructor() {
      this.creationTime = new Date();
    }
    getCreationTime() {
      return this.creationTime;
    }
  }

  const test = new TestClass();

  return lazycrate.box(test); // Convert our object into a string!
}

const stringClass = stringifiedClass(); // Our class is now converted into a string

console.log(stringClass);

const revivedClass = lazycrate.unbox(stringClass);  // Convert the string back into the class!

console.log(revivedClass.getCreationTime().toLocaleDateString()); // This works!

Installation

npm install lazycrate

Features

  • Serialize and deserialize variables
  • Accepts primitives and objects
  • Compress serialized objects
  • Synchronous and asynchronous boxing/unboxing

Full List of Supported Primitives & Objects

Synchronous

lazycrate.sync.box(object)

  • object: <Variable> Variable to be serialized
  • Returns: <String> Serialized version of the variable. Can be compressed.

lazycrate.sync.unbox(string)

  • string: <String> | <Buffer> Serialized content
  • Returns: <Variable> Deserialized variable created from boxing

Example:

const { unbox, box } = require('lazycrate').sync;
let unserialized = {
  content: 'Hello World'
}
let serialized = box(unserialized);
console.log(unbox(serialized));
/*
  {
    content: 'Hello World'
  }
*/

Asynchronous

For the most part, the asynchronous code is available due to the usage of Brotli compression.

lazycrate.box(object)

  • object: <Variable> Variable to be serialized
  • Returns: <String> Serialized version of the variable. Can be compressed.

lazycrate.unbox(string)

  • string: <String> | <Buffer> Serialized content
  • Returns: <Variable> Deserialized variable created from boxing

Example:

const { unbox, box } = require('lazycrate');
let unserialized = {
  content: 'Hello World'
}
(async function(){
  let serialized = await box(unserialized);
  console.log(await unbox(serialized));
  /*
    {
      content: 'Hello World'
    }
  */
})();

Supported Primitives & Objects

  • Object
    • Object
    • Date
    • Map
      • Map
      • WeakMap
    • Set
      • Set
      • WeakSet
    • RegExp
    • Boolean
    • Number
    • String
    • Error
      • Error
      • AggregateError
      • EvalError
      • RangeError
      • ReferenceError
      • SyntaxError
      • TypeError
      • URIError
      • InternalError
    • DataView
    • Array
      • ArrayBuffer
      • SharedArrayBuffer
      • BigInt64Array
      • BigUint64Array
      • Float32Array
      • Float64Array
      • Int8Array
      • Int16Array
      • Int32Array
      • Uint8Array
      • Uint16Array
      • Uint32Array
      • Uint8ClampedArray
    • Buffer
    • Generator
    • Proxy
    • FinalizationRegistry
    • Promise
    • WeakRef
  • undefined
  • Boolean
  • Number
  • BigInt
  • String
  • Symbol
  • Function
    • Function
    • AsyncFunction
    • AsyncGeneratorFunction
    • GeneratorFunction