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 - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

LICENSE

8

index.js

@@ -1,4 +0,2 @@

/**
* Entry point
*/
/** Entry point */

@@ -9,6 +7,6 @@ 'use strict';

const Compactr = require('./src');
const schema = require('./src/schema');
/* Exports -------------------------------------------------------------------*/
module.exports = Compactr;
module.exports = { schema };
{
"name": "compactr",
"version": "1.0.2",
"description": "A compression library for the modern web",
"version": "2.0.0",
"description": "Schema based serialization made easy",
"main": "index.js",
"scripts": {
"test": "mocha tests/index.js",
"build": "webpack -p -d -j --define process.env.NODE_ENV='\"browser\"' --config ./webpack.config"
"bench": "node benchmarks/array.js && node benchmarks/boolean.js && node benchmarks/double.js && node benchmarks/integer.js && node benchmarks/object.js && node benchmarks/string.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fed135/compactr.git"
"url": "git+https://github.com/compactr/compactr-js.git"
},

@@ -30,14 +30,12 @@ "keywords": [

"author": "frederic charette <fredericcharette@gmail.com>",
"license": "GPL-3.0",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/fed135/compactr/issues"
"url": "https://github.com/compactr/compactr.js/issues"
},
"homepage": "https://github.com/fed135/compactr#readme",
"homepage": "https://github.com/compactr/compactr.js#readme",
"devDependencies": {
"chai": "3.5.x",
"mocha": "2.5.x"
},
"dependencies": {
"ieee754": "1.1.x"
"benchmark": "^2.1.4",
"chai": "^3.5.0",
"mocha": "^3.1.0"
}
}

@@ -1,9 +0,18 @@

# Compactr
*A compression library for the modern web*
<h1 align="center">
<a title="Schema based serialization made easy" href="http://compactr.js.org">
<img alt="Compactr" width="320px" src="http://res.cloudinary.com/kalm/image/upload/v1494589244/compactr_header_rev1.png" />
<br/><br/>
</a>
Compactr
</h1>
<h3 align="center">
Schema based serialization made easy
<br/><br/><br/>
</h3>
<br/>
[![Compactr](https://img.shields.io/npm/v/compactr.svg)](https://www.npmjs.com/package/compactr)
[![Build Status](https://travis-ci.org/fed135/compactr.svg?branch=master)](https://travis-ci.org/fed135/compactr)
[![Dependencies Status](https://david-dm.org/fed135/compactr.svg)](https://www.npmjs.com/package/compactr)
[![Code Climate](https://codeclimate.com/github/fed135/compactr/badges/gpa.svg)](https://codeclimate.com/github/fed135/compactr)
[![Gitter](https://img.shields.io/gitter/room/fed135/compactr.svg)](https://gitter.im/fed135/compactr)
[![Node](https://img.shields.io/badge/node->%3D4.0-blue.svg)](https://nodejs.org)
[![Build Status](https://travis-ci.org/compactr/compactr.js.svg?branch=master)](https://travis-ci.org/compactr/compactr.js)
[![Gitter](https://img.shields.io/gitter/room/compactr/compactr.svg)](https://gitter.im/compactr/compactr)

@@ -14,94 +23,99 @@ ---

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.
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 build schemas for your data directly in your scripting language. For example, if you have a DB schema for a model, you could use that directly as a schema for Compactr.
## Aren't there any other libraries out there that do this?
## Install
Yes, yes there are. Like [msgpack](http://msgpack.org/), [snappy](https://google.github.io/snappy/) and [protocol-buffers](https://developers.google.com/protocol-buffers/).
```
npm install compactr
```
## Then why make another one, isn't Protobuf like... the best thing?
## Implementation
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.
```node
The first thing that comes to mind is the painful management of `.proto` files.
const Compactr = require('compactr');
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](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself))
// Defining a schema
const userSchema = Compactr.schema({
id: { type: 'number' },
name: { type: 'string' }
});
Furthermore, Protobuf variable types don't mean a lot in Javascript.
## So what's your solution?
// Encoding
userSchema.write({ id: 123, name: 'John' });
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.
// Get the schema header bytes (for partial loads)
const header = userSchema.headerBytes();
// Get the partial load bytes
const partial = userSchema.contentBytes();
## Examples, please.
// Get the full header + content bytes
const buffer = userSchema.bytes();
For example, if you have a DB schema for users, you can use that directly as a schema for Compactr.
| **Waterline** | **Mongoose** |
| --- | --- | --- |
| `{` <br> ` id: {` <br> ` type: 'integer',` <br> ` required: true` <br> ` },` <br> ` name: 'string'` <br> `}` | `{` <br> ` id: {` <br> ` type: Number,` <br> ` required: true` <br> ` },` <br> ` name: String` <br> `}` |
// Decoding (full)
const content = userSchema.decode(buffer);
// Decoding (partial)
const content = userSchema.decode(header, partial);
```
/* 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 */);
## Performances
```
[Array] JSON x 84.04 ops/sec ±0.77% (64 runs sampled)
[Array] Compactr x 88.12 ops/sec ±0.99% (66 runs sampled)
```
/* Decoding the User data */
[Boolean] JSON x 99.29 ops/sec ±0.88% (64 runs sampled)
[Boolean] Compactr x 190 ops/sec ±1.17% (75 runs sampled)
let user = Compactr.decode(User, deflated);
[Float] JSON x 66.76 ops/sec ±1.10% (61 runs sampled)
[Float] Compactr x 112 ops/sec ±1.67% (70 runs sampled)
```
No need to create additional models for serialization!
Note that you can also use plain Objects as Schemas
[Integer] JSON x 103 ops/sec ±1.41% (66 runs sampled)
[Integer] Compactr x 202 ops/sec ±1.74% (74 runs sampled)
## Can that be used for Websockets too?
[Integer (negative)] JSON x 109 ops/sec ±1.24% (69 runs sampled)
[Integer (negative)] Compactr x 203 ops/sec ±1.55% (74 runs sampled)
Oh yes, via webpack!
[Object] JSON x 61.91 ops/sec ±1.32% (58 runs sampled)
[Object] Compactr x 44.78 ops/sec ±2.19% (54 runs sampled)
`npm run build`
[String] JSON x 68.89 ops/sec ±1.48% (63 runs sampled)
[String] Compactr x 79.16 ops/sec ±2.04% (61 runs sampled)
Will generate browser-ready code!
[String (special characters)] JSON x 71.65 ops/sec ±1.15% (65 runs sampled)
[String (special characters)] Compactr x 144 ops/sec ±1.37% (71 runs sampled)
```
## What about Node compatibility
## Size comparison
You need Node 6.0.0 and up
JSON: `{"id":123,"name":"John"}`: 24 bytes
Compactr (full): `<Buffer 02 00 01 01 04 7b 4a 6f 68 6e>`: 10 bytes
## What about performances?
Compactr (partial): `<Buffer 7b 4a 6f 68 6e>`: 5 bytes
**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!
## Protocol details
## Alright, what about features?
[Compactr Protocol](https://github.com/compactr/protocol)
Right now, Compactr allows you to
- [x] Use Waterline schemas
- [x] Use Mongoose schemas
- [x] Synchronously encode/decode
- [x] Encode nested objects/Arrays
## Want to help ?
And in the near future
You are awesome! Open an issue on this project, identifying the feature that you want to tackle and we'll take the discussion there!
- [ ] Run validation checks on payloads
- [ ] Allow multiple levels of encoding
## License
## 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.
[Apache 2.0](LICENSE) (c) 2017 Frederic Charette

Sorry, the diff of this file is not supported yet

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