Gathering detailed insights and metrics for binary-data
Gathering detailed insights and metrics for binary-data
Gathering detailed insights and metrics for binary-data
Gathering detailed insights and metrics for binary-data
has-binary-data
A function that takes anything in javascript and returns true if its argument contains binary data.
functional-red-black-tree
A fully persistent balanced binary search tree
has-binary
A function that takes anything in javascript and returns true if its argument contains binary data.
concat-stream
writable stream that concatenates strings or binary data and calls a callback with the result
npm install binary-data
Typescript
Module System
Min. Node Version
Node Version
NPM Version
84.4
Supply Chain
99.6
Quality
75
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
12 Stars
112 Commits
11 Forks
1 Watchers
15 Branches
2 Contributors
Updated on Mar 30, 2025
Latest Version
0.6.0
Package Id
binary-data@0.6.0
Size
210.56 kB
NPM Version
6.4.1
Node Version
10.14.1
Published on
Dec 10, 2018
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
Declarative binary data encoder / decoder. This module works almost like as binary
or restructure
but provided modern and clean api. It inspired by abstract-encoding interface.
1const { decode, createDecode, types: { uint8, array, string } } = require('binary-data') 2 3// 1. Define your own schema as plain object 4const protocol = { 5 type: uint8, 6 value: array(string(null), uint8) 7} 8 9const message = Buffer.from([1, 2, 3, 4, 5, 6, 0]); 10 11// Just decode message 12const packet = decode(message, protocol) 13 14// 2 Also you may decode messages from streams 15const net = require('net'); 16 17const socket = net.createConnection({ port: 8124 }); 18const istream = createDecode(protocol); 19 20socket.pipe(istream).on('data', packet => { 21 console.log(packet.type, packet.value); 22});
1const { encode, createEncode, types: { uint8, string } } = require('binary-data') 2 3const protocol = { 4 type: uint8, 5 value: string(uint8) 6} 7 8const hello = { 9 type: 12, 10 value: 'my random data' 11} 12 13// Just encode message 14const ostream = encode(hello, protocol); 15const packet = ostream.slice(); 16 17// Or you may encode messages into a stream 18const net = require('net'); 19 20const ostream = createEncode(protocol); 21const socket = net.createConnection({ port: 8124 }, () => { 22 ostream.write(hello); 23}); 24 25ostream.pipe(socket); 26 27// You may combine multiple schemes into one stream 28const ostream = createEncode(); 29 30encode(obj1, ostream, protocol1); 31encode(obj2, ostream, protocol2); 32encode(obj3, ostream, protocol3); 33 34const packet = ostream.slice(); 35
See stun or dtls module for complete example.
Decoding DTLS ClientHello packet, nodejs 10.14.1 / Ubuntu 16.04 x64
name | time |
---|---|
binary data | 637.900ms |
binary | 2229.218ms |
encode(obj: any, [target: BinaryStream], type: Object): BinaryStream
decode(source: BinaryStream|Buffer, type: Object): any
encodingLength(item: any, type: Object): Number
createEncodeStream([type: Object]): BinaryStream
createDecodeStream([type: Object|Buffer]): BinaryStream
createEncode([type: Object]): BinaryStream
createDecode([type: Object|Buffer]): BinaryStream
decode(source: BinaryStream|Buffer, type: Object): any
Reads any data from stream rstream
using data type type
. See examples above.
encode(obj: any, [target: BinaryStream], type: Object): BinaryStream
Writes any data obj
to stream target
using data type type
. See examples above.
encodingLength(item: any, type: Object): Number
Return the amount of bytes needed to encode item
using type
.
createEncodeStream([type: Object]): BinaryStream
createEncode([type: Object]): BinaryStream
Create instance of BinaryStream.
createDecodeStream([type: Object|Buffer]): BinaryStream
createDecode([type: Object|Buffer]): BinaryStream
Create instance of BinaryStream.
types: Object
Contains all primitive data types.
(u)int(8, 16, 24, 32, 40, 48)(be, le)
Low-level integer types.
1const schema = { 2 type: int8 3} 4 5// define int64 as buffer and use your loved library for big numbers 6const int64 = buffer(8)
(double, float)(be, le)
Low-level floating-point types.
1const schema = { 2 size: doublele 3}
array(type: Object, length: number|Object|Function, lengthType: string)
Array of low-level or user defined types. Argument type
should be primitive type or user defined scheme. Argument length
should be a number, number type or function. Argument lengthType
should be bytes
or count
(default).
1array(uint8, 3) // 3 x uint8 2 3const schema = { 4 length: uint8, 5 type: uint32be, 6 items: array(uint16, ({node}) => node.length, 'bytes') 7} 8 9// difference between `bytes` or `count` 10 11array(uint16, uint8) 12// | 0x2 | 0x0 0x1 | 0x0 0x2 | 13// | length | item 1 | item 2 | 14// bytes = 1 + 4, length = 2 15 16array(uint16, uint8, 'bytes') 17// | 0x4 | 0x0 0x1 | 0x0 0x2 | 18// | length | item 1 | item 2 | 19// bytes = 1 + 4, length = 4
string(length)
Low-level string type. Argument length
can be number, null for C - strings, type for size-prefixed data or function.
bool(type: any)
Convert provided type to / from boolean. Argument type
should be an number type.
1const schema = bool(uint8) 2const rstream = createDecodeStream(buf) // 0x01 0 3 4decode(rstream, schema) // return true 5decode(rstream, schema) // return false
buffer(length: Object|null|number)
Low-level buffer type. Argument length
can be number, number type for size-prefixed data, function or null.
1buffer(5) // buffer should be 5 bytes 2 3buffer(uint8) // length prefixed buffer 4// | 0x3 | 0xa 0xb 0xc 5// | length | data 6 7const packet = { 8 header: { 9 length: uint16be 10 }, 11 data: buffer(({node}) => node.header.length % 2) // function should return actual length 12}
reserved(type, count)
Special type to skip any data. Argument count
should be a number, number type or function.
1const packet = { 2 type: uint8, 3 _padding: reserved(uint8, 3) 4} 5 6decode(rstream, packet) // return { type }
when(fn: function(context): boolean, type)
Special type for conditions. Argument fn
should be a function and should return boolean value. The type
argument will be evaluated when the first one returns positive value.
1const schema = { 2 type: uint8, 3 bytes: when(({ node }) => node.type === 1, string(uint16be)), 4 list: when(({ node }) => node.type === 2, array(uint32be, uint8)) 5}
select(when, ..., defaultType)
The second type for conditions. The same as switch
operator in js. Argument defaultType
may be any known
type excluding user schemas.
1const schema = { 2 id: uint8, 3 payload: select(when(({ node }) => node.id === 1, string(uint16be)), buffer(uint16be)) 4}
MIT, 2017 (c) Dmitriy Tsvettsikh
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
65 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More