
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Encode objects into binary and decode binary back into objects, supporting nested references, custom object encoding/decoding, unique pointers...
Encode objects into binary and decode binary back into objects, supporting nested references, custom object encoding/decoding, unique pointers...
npm install obj-codec
# or
yarn add obj-codec
# or
pnpm add obj-codec
import { ObjCodec } from 'obj-codec';
// Using global codec
const encoder = ObjCodec.encode(target);
const decoder = ObjCodec.decode();
for (const chunk of encoder.encode()) {
decoder.decode(chunk);
}
const result = decoder.getResult();
// Using general codec
const objCodec = new ObjCodec();
const encoder = objCodec.encode(target);
const decoder = objCodec.decode();
for (const chunk of encoder.encode()) {
decoder.decode(chunk);
}
const result = decoder.getResult();
import { ObjCodec } from 'obj-codec';
// Using global codec
const encoder = ObjCodec.encode(target);
const decoder = ObjCodec.decode();
encoder.pipe(decoder).on('finish', () => {
const result = decoder.getResult();
});
// Using general codec
const objCodec = new ObjCodec();
const encoder = objCodec.encode(target);
const decoder = objCodec.decode();
encoder.pipe(decoder).on('finish', () => {
const result = decoder.getResult();
});
import { ObjCodec } from 'obj-codec/web';
// Using global codec
const encoder = ObjCodec.encode(target);
const decoder = ObjCodec.decode();
await encoder.pipeTo(decoder);
const result = decoder.getResult();
// Using general codec
const objCodec = new ObjCodec();
const encoder = objCodec.encode(target);
const decoder = objCodec.decode();
await encoder.pipeTo(decoder);
const result = decoder.getResult();
pnpm i
pnpm build
pnpm test
Description | Type |
---|---|
Version number | u8 |
Custom type map length | Flexible Unsigned Integer |
Custom type map | Custom Type Map |
Data object table | Array of Data Objects |
[string length, string][]
Data Object
is greater than 16, subtract 17 to get the index in the mapcustom type codecs
based on itReference: Codec
ID | Name | Length (bytes) | Priority (lower is higher) | Referenceable* | Notes |
---|---|---|---|---|---|
0 | Pointer | Flexible* | N/A | N/A | Implicit type, cannot be used directly. Automatically created by main codec (ObjEncoder , ObjDecoder ) |
1 | Binary | 2 | ✅ | ||
2 | Number | 8 | 0 | ||
3 | BigInt | 0 | |||
4 | String | 1 | ✅ | ||
5 | false | 0 | 0 | No data section | |
6 | true | 0 | 0 | No data section | |
7 | null | 0 | 0 | No data section | |
8 | undefined | 0 | 0 | No data section | |
9 | Object | 5 | ✅ | Fallback type | |
10 | Array | 5 | ✅ | Fallback type | |
11 | Set | 4 | ✅ | ||
12 | Map | 4 | ✅ | ||
13 | Date | 8 | 4 | ✅ | |
14 | RegExp | 4 | ✅ | ||
15 | Symbol | 1 | ✅ | ||
16 | Unique Pointer | Flexible* | |||
17+ | Custom Type | N/A | 3 | ✅ |
Notes:
Used to represent unsigned integers with dynamic encoding length.
0
-> 0b0000_0000
127
-> 0b0111_1111
128
-> 0b1000_0000
and 0b0000_0001
129
-> 0b1000_0001
and 0b0000_0001
A unique pointer is a special encoding mechanism for handling:
globalThis
, built-in Symbol
, etc.)/**
* Codec
* @template Type Data type
* @template EncodeResult Encoding type
* @template DecodeMiddle Decoding intermediate type
*/
interface ICodec<
Type extends IClass,
EncodeResult,
DecodeMiddle extends Type = Type,
> {
/**
* Codec name
* @description
* The codec is identified by its name,
* ensure the name is unique
*/
name: string;
/** Target class for encoding/decoding */
class: Type;
/**
* Parent classes of the target class
* @description
* Used to determine matching order
* @example
* [ParentClass, GrandClass, GrandGrandClass]
*/
parentClasses?: IClass[];
/**
* Encode
* @param data Data
*/
encode(data: Type): EncodeResult;
/**
* Decode
* @param encoded Encoded data
*/
decode(encoded: EncodeResult): DecodeMiddle;
/**
* Dereference
* @param data Decoding intermediate data
*/
deref?(data: DecodeMiddle): void;
}
encode
method can return any type of data, including custom types.decode
method must return a Type
. The encoded
parameter may contain pointers, which should be preserved in the return value if they cannot be dereferenced.dereference
method is used to resolve references in the return value of decode
. Its return value will be ignored.// Register global codec
ObjCodec.register(codec);
// Register codec
const objCodec = new ObjCodec();
objCodec.register(codec);
FAQs
Encode objects into binary and decode binary back into objects, supporting nested references, custom object encoding/decoding, unique pointers...
We found that obj-codec demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.