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

bin-protocol

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bin-protocol - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

2

package.json

@@ -9,3 +9,3 @@ {

},
"version": "1.0.3",
"version": "1.0.4",
"main": "./lib/index.js",

@@ -12,0 +12,0 @@ "keywords": ["buffer"],

@@ -66,3 +66,3 @@ # bin-protocol

this.Int8('char');
return String.fromCharCode(this.context.char);
return String.fromCharCode(this.context.char); // convert from char code to character
}

@@ -75,4 +75,4 @@ });

.Int8('length')
.loop('items', this.char, this.context.length);
return this.context.items;
.loop('items', this.char, this.context.length); // read 'length' characters with above defined 'char' method
return this.context.items; // return just items, without 'length' property
}

@@ -83,4 +83,3 @@ });

reader
.array('chars');
reader.array('chars');

@@ -104,2 +103,3 @@ console.log(reader.result); // => { chars: [ 'a', 'b', 'c', 'd', 'e' ] }

Define an array data type which first writes data array length as a single byte
```javascript

@@ -110,3 +110,3 @@ protocol.define('array', {

.Int8(values.length)
.loop(values, this.Int8);
.loop(values, this.Int8); // write all values with Int8 method
}

@@ -150,2 +150,52 @@ });

### Loops (arrays)
All next 3 examples are essentialy identical:
Read data with your own code:
```javascript
protocol.define('customArray', {
read: function () {
var i = 0;
this.Int32BE('length');
for(i = 0; i<this.context.length; i++){
this.Int32BE('items[' + i + ']'); // yes, this works
}
return this.context.items;
}
});
```
Read with `.loop()` method by providing the length (loop count):
```javascript
protocol.define('loopArray', {
read: function () {
this
.Int32BE('length')
.loop('items', this.Int32BE, this.context.length);
return this.context.items;
}
});
```
Read with `.loop()` method until the `end()` is called:
```javascript
protocol.define('loopArrayEnd', {
read: function () {
var len;
this.Int32BE('length');
len = this.context.length;
this.loop('items', function (end) {
this.Int32BE();
if((len -= 1) === 0){
end(); // call end() to break from loop
}
});
return this.context.items;
}
});
```
See [Kafka protocol](https://github.com/oleksiyk/kafka/tree/master/lib/protocol) for more examples.

@@ -152,0 +202,0 @@

@@ -123,8 +123,8 @@ "use strict";

read: function () {
var i = 0;
this
.Int32BE('length')
.loop('items', function (end) {
var len;
this.Int32BE('length');
len = this.context.length;
this.loop('items', function (end) {
this.Int32BE();
if(i++ === 2){
if((len -= 1) === 0){
end();

@@ -131,0 +131,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