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

obj-codec

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

obj-codec

Encodes and decodes objects to binary, supports nested references

  • 0.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

简体中文 | English

obj-codec

test

Encodes and decodes objects to binary, supports nested references.

Dev

npm i
npm run compile
npm run test

Clean

  1. Remove the following directories and files:
    • .rollup.cache
    • cache
    • dist
  2. Run npm run compile

Features TODO List

  • Readable(Node) Style API
  • ReadableStream(Web) Style API
  • Unique Pointer: Points to built-in objects, classes, functions, etc

Document

Encode

import { ObjCodec } from 'obj-codec';
import { createWriteStream } from 'fs';

const DATA = {};

// Using globally registered custom codecs
function useGlobalCodoc() {
	const encoder = ObjCodec.encode(DATA);
	const writeStream = createWriteStream('path/to/file');

	for (const data of encoder.encode()) {
			writeStream.write(data);
	}

	writeStream.end();
}

//  Use custom codecs that are instance-only
function useInstanceCodoc() {
	const codec = new ObjCodec();
	//  Call `codec.registerCodecs` to register custom codecs limited to the instance `codec
	const encoder = codec.encode(DATA);
	const writeStream = createWriteStream('path/to/file');
	for (const data of encoder.encode()) {
			writeStream.write(data);
	}
	writeStream.end();
}

Decode

import { ObjCodec } from 'obj-codec';
import { createReadStream } from 'fs';

// Using globally registered custom codecs
function useGlobalCodoc() {
	const decoder = ObjCodec.decode();
	const readStream = createReadStream('path/to/file');

	readStream.on('data', (chunk) => {
		decoder.write(new Uint8Array(chunk));
	});

	readStream.on('end', () => {
		encoder.end().then(console.log);
	});
}

//  Use custom codecs that are instance-only
function useInstanceCodoc() {
	const codec = new ObjCodec();
	//  Call `codec.registerCodecs` to register custom codecs limited to the instance `codec
	const decoder = codec.decode();
	const readStream = createReadStream('path/to/file');

	readStream.on('data', (chunk) => {
		decoder.write(new Uint8Array(chunk));
	});

	readStream.on('end', () => {
		encoder.end().then(console.log);
	});
}

Encoded Data Structure

DescriptonType
Versionu8
Custom Type Mapping Table LengthFlexable Unsigned Integer
Custom Type Mapping TableCustom Type Mapping Table
Data Object[]Data Object

Custom Type Mapping Table

  1. Structure: Repeat according to Length of customized type mapping table.
    1. String length: Flexable Unsigned Integer
    2. String
  2. Mapping
    • All strings must be unique
    • The Data Object type ID is the index of the mapping table if it is greater than 16, minus 17.
    • The mapping table should be created at the time of coding
    • Decoding should start by decoding the mapping table and creating an array of Custom Type Codecs based on the mapping table

Data Object

  1. Root object: The first object is the root object
  2. Structure

Reference: Codec

Codec

IDNameLength (Bytes)Weights (the smaller the first)Referencable*Comment
0PointerFlexable*N/AN/AImplicit type, cannot be used directly. Created automatically by the main codecs (ObjEncoder, ObjDecoder)
1Binary2X
2Number80
3BigInt0
4String1X
5false00No data area
6true00No data area
7null00No data area
8undefined00No data area
9Object5XFallback type
10Array5XFallback type
11Set4X
12Map4X
13Date84X
14RegExp4X
15Symbol1X
16Unique PointerNot Implemented
17+Custom TypeN/A3X

Comments:

  • "Flexable" Length:reference Flexable Unsigned Integer
  • Referenceable: if the type is contained within a container type, it is not encoded as raw data, but as a pointer

Flexable Unsigned Integer

Used to represent unsigned integers, supports dynamic encoding of lengths.

  1. Representation range: 0 ~ 2^n - n (n is the number of bits required for encoding)
  2. Structure
    • The high bit (bit 8) of each byte is used to indicate the existence of subsequent bytes:
    • If the high bit is 1, it indicates that there are more bytes to follow.
    • If the high bit is 1, more bytes follow. If the high bit is 0, this is the last byte.
    • The remaining 7 bits are used to store the actual data portion.
  3. Encoding example
    • 0 -> `0b0000_0000
    • 127 -> 0b0111_1111
    • 128 -> 0b1000_0000 and `0b0000_0001
    • 129 -> 0b1000_0001 and 0b0000_0001.

Custom Codec

Define
interface ICodec<
	Data,
	DecodeMiddle = Data,
	EncodeResult extends BasicType,
> {
	encode(data: Data): EncodeResult;
	decode(encoded: EncodeResult): DecodeMiddle;
	dereference?(data: DecodeMiddle): any;
}

The encode method can return data of any type belonging to BasicType. If a non-BasicType type is returned, it will be encoded directly as a BasicType, and custom codecs will be ignored.

The decode method needs to return Data type. Parameter encoded may contain a pointer, which cannot be dereferenced and should be kept in the return value.

The dereference method is used to dereference the return value of the decode method. The method return value is ignored.

Register
registerCodecs(id: string, codec: AnyCodec, constructor: IClass): void;

FAQs

Package last updated on 06 Nov 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

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