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

binary-data

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-data - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0-0

.vscode/settings.json

4

package.json
{
"name": "binary-data",
"version": "0.4.0",
"version": "0.5.0-0",
"description": "Declarative encoder/decoder of various binary data.",

@@ -37,3 +37,3 @@ "main": "src/index.js",

"dependencies": {
"bl": "^1.2.1",
"bl": "^2.0.0",
"is-plain-object": "^2.0.3"

@@ -40,0 +40,0 @@ },

@@ -9,3 +9,2 @@ # binary-data

[![Greenkeeper badge](https://badges.greenkeeper.io/reklatsmasters/binary-data.svg)](https://greenkeeper.io/)
[![Coverage Status](https://coveralls.io/repos/github/reklatsmasters/binary-data/badge.svg?branch=master)](https://coveralls.io/github/reklatsmasters/binary-data?branch=master)

@@ -32,3 +31,3 @@ Declarative encoder/decoder of various binary data. This module works almost like as [`binary`](https://www.npmjs.com/package/binary) or [`restructure`](https://www.npmjs.com/package/restructure) but provided modern and clean api.

// 2.1 also you can decode messages from streams
// 2.1 also you may decode messages from streams
const unicast = require('unicast')

@@ -39,11 +38,6 @@

// 2.2 create stream
const decodeStream = createDecodeStream()
const input = createDecodeStream(protocol)
// 2.3 connect streams
socket.pipe(decodeStream)
// 2.4. decode messages chunk by chunk or what you want
socket.on('data', () => {
const packet = decode(decodeStream, protocol)
})
socket.pipe(input).on('data', packet => { /* do stuff */ })
```

@@ -57,3 +51,3 @@

// 1. define schema
const helloPacket = {
const protocol = {
type: uint8,

@@ -70,3 +64,3 @@ data: buffer(uint8)

// 3. create encode stream
const wstream = createEncodeStream()
const wstream = createEncodeStream(protocol)

@@ -76,10 +70,13 @@ // 4. connect streams

// 5. encode all your data multiple times
encode(hello, wstream, helloPacket)
// 5.1. encode all your data
wstream.write(hello)
// or convert to a buffer
// 5.2 or use another schema
encode(anotherPacket, wstream, anotherSchema)
// 5.3 or convert to a buffer
const buf = wstream.slice()
```
See [stun](https://github.com/nodertc/stun) module for complete example.
See [stun](https://github.com/nodertc/stun) or [dtls](https://github.com/nodertc/dtls) module for complete example.

@@ -86,0 +83,0 @@ ## API

@@ -15,2 +15,4 @@ const EncodeStream = require('streams/encode')

const { encodingLength } = require('lib/encoding-length')
const Transaction = require('lib/transaction')
const NotEnoughDataError = require('lib/not-enough-data-error')

@@ -32,19 +34,78 @@ const types = {

/**
* Creates duplex stream for encode objects
* into binary data.
* Creates transform stream for encode objects
* into buffer.
* @param {object} [schema]
* @returns {EncodeStream}
*/
function createEncodeStream() {
return new EncodeStream()
function createEncodeStream(schema) {
return new EncodeStream({
schema,
transform: transformEncode,
})
}
/**
* Creates duplex stream for decode binary data.
* @param {Buffer} [buf]
* Creates transform stream for decode binary data.
* @param {Buffer|Object} [bufOrSchema]
* @returns {DecodeStream}
*/
function createDecodeStream(buf) {
return new DecodeStream(buf)
function createDecodeStream(bufOrSchema) {
let schema = null
const isBuffer = Buffer.isBuffer(bufOrSchema)
if (!isBuffer) {
schema = bufOrSchema
}
const stream = new DecodeStream({
schema,
transform: transformDecode,
})
if (isBuffer) {
stream.append(bufOrSchema)
}
return stream
}
function transformEncode(chunk, encoding, cb) {
/* eslint-disable no-invalid-this */
try {
encode(chunk, this, this._schema)
const buf = this.slice()
this.consume(buf.length)
cb(null, buf)
} catch (err) {
cb(err)
}
/* eslint-enable no-invalid-this */
}
function transformDecode(chunk, encoding, cb) {
/* eslint-disable no-invalid-this */
this.append(chunk)
try {
while (this.length > 0) {
const transaction = new Transaction(this)
const data = decode(transaction, this._schema)
transaction.commit()
this.push(data)
}
cb()
} catch (err) {
if (err instanceof NotEnoughDataError) {
cb()
} else {
cb(err)
}
}
/* eslint-enable no-invalid-this */
}
module.exports = {

@@ -64,2 +125,3 @@ /* Main api */

DecodeStream,
NotEnoughDataError,
}

@@ -1,10 +0,20 @@

const BufferList = require('bl')
const AbstractStream = require('streams/abstract-stream')
const NotEnoughDataError = require('lib/not-enough-data-error')
const temporary = Buffer.alloc(8 /* maximal size */)
class DecodeStream extends BufferList {
constructor(buf) {
super()
class DecodeStream extends AbstractStream {
constructor(options = {}) {
let buf = null
if (Buffer.isBuffer(buf)) {
if (Buffer.isBuffer(options)) {
buf = options
options = {}
}
options.readableObjectMode = true
options.writableObjectMode = false
super(options)
if (buf !== null) {
this.append(buf)

@@ -118,5 +128,3 @@ }

if (size > length) {
throw new Error(
`Not enough data: requested ${size} bytes but only ${length} available.`
)
throw new NotEnoughDataError(size, length)
}

@@ -123,0 +131,0 @@ }

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

const BufferList = require('bl')
const AbstractStream = require('streams/abstract-stream')

@@ -22,3 +22,9 @@ const writeDoubleBE = createWriteFunction('writeDoubleBE')

class EncodeStream extends BufferList {
class EncodeStream extends AbstractStream {
constructor(options = {}) {
options.readableObjectMode = false
options.writableObjectMode = true
super(options)
}
writeBuffer(chunk) {

@@ -25,0 +31,0 @@ this.append(chunk)

const symbols = require('internal/symbols')
const { decodeCommon } = require('lib/decode')
const Metadata = require('internal/meta')

@@ -12,2 +14,3 @@ module.exports = select

decode,
encode() {},
[symbols.skip]: true,

@@ -20,6 +23,7 @@ }

decode.bytes = 0
const context = Metadata.clone(this) // eslint-disable-line no-invalid-this
for (const when of whenTypes) {
// eslint-disable-next-line no-invalid-this
const probalyValue = when.decode.call(this, rstream)
const probalyValue = decodeCommon(rstream, when, context)

@@ -30,3 +34,4 @@ if (when[symbols.skip] === true) {

decode.bytes = when.decode.bytes
decode.bytes = context.bytes
Metadata.clean(context)

@@ -33,0 +38,0 @@ result[symbols.skip] = false

const { isType } = require('lib/util')
const NotEnoughDataError = require('lib/not-enough-data-error')

@@ -69,3 +70,3 @@ module.exports = string

if (bytes >= rstream.length) {
throw new RangeError('Out of bounds.')
throw new NotEnoughDataError(bytes, rstream.length)
}

@@ -72,0 +73,0 @@ }

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