icbon
TypeScript icon, indicating that this package has built-in type declarations

1.0.6-alpha • Public • Published

icbon Logo

Internet Compressed Binary Object Notation is a compact data interchange binary serialization format developed and designed for Internet Compressed Binary Protocol. This library allows you to reduce the amount of transmitted data and to speed up client-server data transfer, especially with slow internet connection.


Support:

  • Google Chrome 7+
  • Mozilla Firefox 11+
  • Internet Explorer 10+
  • Microsoft Edge 12+
  • Opera 12.1+
  • Apple Safari 5.1+
  • Node.JS 8.10.9+

Installation:

$ npm install icbon

Documentation:

function concat(data: Uint8Array[]): Uint8Array;

Sequentially concatenates each buffer in data array into resulting buffer.

function decode(data: Uint8Array, offset: number = 0): unknown;

Deserializes data buffer to JSON RFC8259 compatible value starting from the specified offset.

function encode(data: unknown): Uint8Array;

Serializes JSON RFC8259 compatible data to resulting buffer.

class Decoder {
  new(data: Uint8Array, offset: number = 0): this;
  any(): unknown;
  number(): number;
  string(): string;
  array(): unknown[];
  hash(): Record<string, unknown>;
}

Deserializes icbon data buffer starting from the specified offset to any JSON RFC8259 compatible type listed in the class interface. Calling each class method shifts offset to the next byte after read buffer segment.

class Encoder {
  new(): this;
  any(data: unknown): Uint8Array;
  null(): Uint8Array;
  boolean(data: boolean): Uint8Array;
  number(data: number): Uint8Array;
  string(data: string): Uint8Array;
  array(data: unknown[]): Uint8Array;
  hash(data: Record<string, unknown>): Uint8Array;
}

Serializes any JSON RFC8259 compatible type listed in the class interface to icbon buffer.

class DecodeError {
  new(message?: string): this;
}

Error type which can be thrown during deserialization process.

class EncodeError {
  new(message?: string): this;
}

Error type which can be thrown during serialization process.


Usage examples:

import { encode } from 'icbon';

const response: Response = await fetch('https://random-data-api.com/api/users/random_user');
const text: string = await response.text();
const object: unknown = await response.json();
const buffer: Uint8Array = encode(object);

console.log(`text json: ${ text.length * 2 } bytes`);
console.log(`serialized icbon: ${ buffer.length } bytes`);
// client
import { encode } from 'icbon';

const response: Response = await fetch('https://random-data-api.com/api/users/random_user');
const data: unknown = await response.json();

fetch(`http://example.com/endpoint`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-icbon-encoded' },
  body: encode(data),
});

// server
import { IncomingMessage, Server, ServerResponse } from 'http';
import { decode } from 'icbon';

const server: Server = new Server();

server.on('request', (request: IncomingMessage, response: ServerResponse): void => {
  const chunks: Buffer[] = [];

  request.on('data', (chunk: Buffer): void => {
    chunks.push(chunk);
  });

  request.on('end', (): void => {
    const buffer: Buffer = Buffer.concat(chunks);
    const decoded: unknown = decode(buffer);

    console.log(decoded);

    response.writeHead(200, { 'Content-Type': 'application/json' });
    response.end(JSON.stringify(decoded));
  });
});

server.listen(8080, '0.0.0.0');
import { concat, Decoder, Encoder } from 'icbon';

// Custom packet serialization
const encoder: Encoder = new Encoder();
const signature: Uint8Array = new Uint8Array([ 0x4C, 0xAA ]);
const status: Uint8Array = encoder.number(200);
const method: Uint8Array = encoder.string('POST');
const headers: Uint8Array = encoder.hash({ 'Random-Header': 'RandomValue' });
const body: Uint8Array = encoder.any([ { foo: 'bar', array: [ 100, 200, false ] } ]);
const packet: Uint8Array = concat([ signature, status, method, headers, body ]);

// Custom packet deserialization
if (packet[0] !== 0x4C || packet[1] !== 0xAA) {
  throw new Error('Wrong signature');
}

const decoder: Decoder = new Decoder(packet, 2);

console.log(`Status: ${ decoder.number() }`);
console.log(`Method: ${ decoder.string() }`);
console.log(`Headers: ${ decoder.hash() }`);
console.log(`Body: ${ decoder.any() }`);

See also:

Package Sidebar

Install

npm i icbon

Weekly Downloads

0

Version

1.0.6-alpha

License

GPL

Unpacked Size

90.5 kB

Total Files

19

Last publish

Collaborators

  • prism-tech