New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

compresjon

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compresjon

A lightweight package designed for storing JSON data in a compressed and serialized format.

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

CompreSJON

npm

compresjon is a lightweight package designed for storing JSON data in a compressed and serialized format.

It is particularly useful for:

  • Long-running processes that require infrequent access to data, such as cold storage during interval downtimes.
  • Sending back binary data instead of JSON over API requests

CompreSJON uses msgpack in combination with Brotli compression to ensure low memory utilization or bandwidth usage.

Installation

npm install compresjon

Usage

Converts a JavaScript Object Notation (JSON) into its compressed counterpart.

import CompreSJON from 'compresjon';

const json = new CompreSJON({ hello: 'world' });

/**
 * CompreSJON can create an instance from a `Buffer` created by
 * another instance. Eg. if your API sends back a `Buffer` over
 * its API request, the client can create a `CompreSJON` from that `Buffer`.
 */
const json = new CompreSJON<{ hello: 'world' }>(buffer);

Updating Data

Override the internal compressed data with a new updated dataset.

const json = new CompreSJON({ hello: 'world' });
json.update({ hello: 'universe' });
console.log(CompreSJON.parse(json)); // { hello: 'universe' }

Serializing and Deserializing

You can stringify a CompreSJON instance using the static stringify method:

const json = new CompreSJON({ hello: 'world' });
console.log(CompreSJON.stringify(json)); // '{"hello":"world"}'

You can convert a CompreSJON instance back to JSON using the static parse method:

const json = new CompreSJON({ hello: 'world' });
console.log(CompreSJON.parse(json)); // { hello: 'world' }

Keep in mind that when using parse that there will be two instances of the JSON data in memory during the runtime. Both the internal binary representation and the parsed JSON. So depending on your use case you can look into dump:

const json = new CompreSJON({ hello: 'world' });
console.log(CompreSJON.dump(json)); // { hello: 'world' }
console.log(json.buffer.length); // 0

Dumping the data will return the parsed JSON while also clearing the internal binary reference. This means that the only instance available, during the runtime after dump, is the parsed JSON. Just don't forget to update with the updated data once it's ready to be compressed again.

CompreSJON also has a built-in toJSON() method allowing it to be sent back through an API directly to the client, exposing the internal Buffer.

Keywords

json

FAQs

Package last updated on 15 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts