Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compactr

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compactr

A compression library for the modern web

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Compactr

A compression library for the modern web

Compactr Build Status Dependencies Status Code Climate Gitter


What is this and why does it matter?

Compactr is a library to compress and decompress Javascript objects before sending them over the web. It's immensely useful for web applications that use sockets a lot. Smaller payloads equals faster throughput and less bandwidth costs.

Aren't there any other libraries out there that do this?

Yes, yes there are. Like msgpack, snappy and protocol-buffers.

Then why make another one, isn't Protobuf like... the best thing?

Why yes, Protocol Buffer is by far the better performing protocol out there, but there's a few things about it I don't like - as a Node developer.

The first thing that comes to mind is the painful management of .proto files.

Not only are they overly complex, they are also written in a different markup, which makes dynamic generation or property checking a bit of a hassle. Not to mention that you have to maintain parity across services of these messages that are more often than not a copy of your data Models. (See DRY)

Furthermore, Protobuf variable types don't mean a lot in Javascript.

So what's your solution?

Protocol Buffers are awesome. Having schemas to deflate and inflate data while maintaining some kind of validation is a great concept. Compactr's goal is to build on that to better suit Node development and reduce repetition by allowing you to re-use your current Model schemas.

Examples, please.

For example, if you have a DB schema for users, you can use that directly as a schema for Compactr.

| Waterline | Mongoose | | --- | --- | --- | | {
id: {
type: 'integer',
required: true
},
name: 'string'
} | {
id: {
type: Number,
required: true
},
name: String
} |

/* User compessing in a controller */

const Compactr = require('compactr');

User.create({ id: 0, name: 'Bruce' })
  .then(user => Compactr.encode(User, user))
  .then(deflated => /* Send encoded User */);

/* Decoding the User data */

let user = Compactr.decode(User, deflated);

No need to create additional models for serialization! Note that you can also use plain Objects as Schemas

Can that be used for Websockets too?

Oh yes, via webpack!

npm run build

Will generate browser-ready code!

What about Node compatibility

You need Node 6.0.0 and up

What about performances?

TODO

I'm still working on graphs and proper test scenarios, but I can say that it performs as fast, and sometimes faster than JSON encoding/decoding and outputs a buffer that is more or less half the size!

Alright, what about features?

Right now, Compactr allows you to

  • Use Waterline schemas
  • Use Mongoose schemas
  • Synchronously encode/decode
  • Encode nested objects/Arrays

And in the near future

  • Run validation checks on payloads
  • Allow multiple levels of encoding

Alright, I'm convinced! How can I help?

Just open an issue, identifying it as a feature that you want to tackle. Ex: STORY - [...] And we'll take the discussion there.

Keywords

FAQs

Package last updated on 15 Sep 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc