Skip to content

Commit

Permalink
Fixed #27
Browse files Browse the repository at this point in the history
  • Loading branch information
calebsander committed Aug 17, 2016
1 parent b80a085 commit 2b566af
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ const tribeType = new sb.StructType({
members: new sb.SetType(personType),
money: new sb.MapType(personType, new sb.FloatType)
});

console.log(tribeType.toBuffer());
let typeBuffer = tribeType.toBuffer(); //ArrayBuffer { byteLength: 49 }
console.log(Buffer.from(typeBuffer));
//<Buffer 51 03 06 6c 65 61 64 65 72 51 03 03 64 6f 62 15 02 69 64 12 04 6e 61 6d 65 41 07 6d 65 6d 62 65 72 73 53 ff 00 1b 05 6d 6f 6e 65 79 54 ff 00 25 20>

let louis = {
Expand All @@ -110,16 +110,17 @@ let value = {
members: new Set().add(louis).add(garfield),
money: new Map().set(louis, 23.05).set(garfield, -10.07)
};
console.log(tribeType.valueBuffer(value));
let valueBuffer = tribeType.valueBuffer(value); //ArrayBuffer { byteLength: 100 }
console.log(Buffer.from(valueBuffer));
//<Buffer 00 00 01 4e b7 2d 6c 20 00 0a 4a 6f 65 00 00 00 00 02 00 00 01 4e b7 2d 6c 21 00 09 4c 6f 75 69 73 00 00 00 01 4e b7 2d 6c 22 00 11 47 61 72 66 69 65 ... >
````
### Reading from type and value `Buffer`s
````javascript
const sb = require('structure-bytes');

//Buffer obtained somehow
let tribeBuffer = Buffer.from([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x15, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x00, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x00, 0x25, 0x20]);
let type = sb.r.type(tribeBuffer);
let tribeBuffer = new Uint8Array([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x15, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x00, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x00, 0x25, 0x20]);
let type = sb.r.type(tribeBuffer.buffer);
console.log(type);
/*
StructType {
Expand All @@ -130,8 +131,8 @@ StructType {
*/

//Buffer obtained somehow
let buffer = Buffer.from([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
console.log(sb.r.value({type, buffer}));
let valueBuffer = new Uint8Array([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
console.log(sb.r.value({type, buffer: valueBuffer.buffer}));
/*
{ leader: { dob: 2015-07-22T19:11:24.192Z, id: 10, name: 'Joe' },
members:
Expand Down Expand Up @@ -161,17 +162,22 @@ let type = new sb.EnumType({
sb.writeType({
type,
outStream: fs.createWriteStream('./type')
}, (err) => {
}, err => {
sb.readType(fs.createReadStream('./type'), (err, readType) => {
console.log(type.equals(readType)); //true
console.log(readType);
/*
EnumType {
type: StringType {},
values: [ 'ON_TIME', 'LATE', 'CANCELLED', 'UNKNOWN' ] }
*/
});
});
let value = 'CANCELLED';
sb.writeValue({
type,
value,
outStream: fs.createWriteStream('./value')
}, (err) => {
}, err => {
sb.readValue({
type,
inStream: fs.createReadStream('./value')
Expand All @@ -183,7 +189,7 @@ sb.writeTypeAndValue({
type,
value,
outStream: fs.createWriteStream('./type-value')
}, (err) => {
}, err => {
sb.readTypeAndValue(fs.createReadStream('./type-value'), (err, type, value) => {
console.log(value); //'CANCELLED'
});
Expand All @@ -197,7 +203,7 @@ const sb = require('structure-bytes');

let type = new sb.DateType();
http.createServer((req, res) => {
sb.httpRespond({req, res, type, value: new Date()}, (err) => {
sb.httpRespond({req, res, type, value: new Date}, err => {
console.log('Responded');
});
}).listen(80);
Expand Down Expand Up @@ -231,8 +237,8 @@ http.createServer((req, res) => {
````
Client-side:
````html
<script src = './node_modules/structure-bytes/compiled/jquery.js'></script>
<script src = './node_modules/structure-bytes/compiled/upload.js'></script>
<script src = '/structure-bytes/compiled/jquery.js'></script>
<script src = '/structure-bytes/compiled/upload.js'></script>
<button>Click me</button>
<script>
var clickCount = 0;
Expand Down Expand Up @@ -261,6 +267,6 @@ Versions will be of the form `x.y.z`.

## Testing
To test the NodeJS code, run `npm test`.
To test the HTTP transaction code, run `node client-test/server.js` and open `localhost:8080` in your browser. Open each link in a new page and each page should alert `Success`.
To test the HTTP transaction code, run `node client-test/server.js` and open `localhost:8080` in your browser. Open each link in a new page. `Upload` and `Download` should each alert `Success`, while `Upload & Download` should alert `Upload: Success` and `Download: Success`.

_Caleb Sander, 2016_
4 changes: 2 additions & 2 deletions client-side/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
* sb.download('people', {
* url: '/download-people',
* type: 'GET',
* success: function(response, textStatus, jqXHR) {
* console.log(response); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
* success: function(value) {
* console.log(value); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
* }
* });
*/
Expand Down
4 changes: 2 additions & 2 deletions doc/client-side_download.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ <h1 class="page-title">Source: client-side/download.js</h1>
* sb.download('people', {
* url: '/download-people',
* type: 'GET',
* success: function(response, textStatus, jqXHR) {
* console.log(response); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
* success: function(value) {
* console.log(value); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
* }
* });
*/
Expand Down
4 changes: 2 additions & 2 deletions doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ <h5>Example</h5>
<pre class="prettyprint"><code>sb.download('people', {
url: '/download-people',
type: 'GET',
success: function(response, textStatus, jqXHR) {
console.log(response); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
success: function(value) {
console.log(value); //e.g. [{name: 'John', id: 2}, {name: 'Jane', id: 10}]
}
});</code></pre>

Expand Down
36 changes: 21 additions & 15 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
members: new sb.SetType(personType),
money: new sb.MapType(personType, new sb.FloatType)
});

console.log(tribeType.toBuffer());
let typeBuffer = tribeType.toBuffer(); //ArrayBuffer { byteLength: 49 }
console.log(Buffer.from(typeBuffer));
//&lt;Buffer 51 03 06 6c 65 61 64 65 72 51 03 03 64 6f 62 15 02 69 64 12 04 6e 61 6d 65 41 07 6d 65 6d 62 65 72 73 53 ff 00 1b 05 6d 6f 6e 65 79 54 ff 00 25 20>

let louis = {
Expand All @@ -146,12 +146,13 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
members: new Set().add(louis).add(garfield),
money: new Map().set(louis, 23.05).set(garfield, -10.07)
};
console.log(tribeType.valueBuffer(value));
let valueBuffer = tribeType.valueBuffer(value); //ArrayBuffer { byteLength: 100 }
console.log(Buffer.from(valueBuffer));
//&lt;Buffer 00 00 01 4e b7 2d 6c 20 00 0a 4a 6f 65 00 00 00 00 02 00 00 01 4e b7 2d 6c 21 00 09 4c 6f 75 69 73 00 00 00 01 4e b7 2d 6c 22 00 11 47 61 72 66 69 65 ... ></code></pre><h3>Reading from type and value <code>Buffer</code>s</h3><pre class="prettyprint source lang-javascript"><code>const sb = require('structure-bytes');

//Buffer obtained somehow
let tribeBuffer = Buffer.from([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x15, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x00, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x00, 0x25, 0x20]);
let type = sb.r.type(tribeBuffer);
let tribeBuffer = new Uint8Array([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x15, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x00, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x00, 0x25, 0x20]);
let type = sb.r.type(tribeBuffer.buffer);
console.log(type);
/*
StructType {
Expand All @@ -162,8 +163,8 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
*/

//Buffer obtained somehow
let buffer = Buffer.from([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
console.log(sb.r.value({type, buffer}));
let valueBuffer = new Uint8Array([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
console.log(sb.r.value({type, buffer: valueBuffer.buffer}));
/*
{ leader: { dob: 2015-07-22T19:11:24.192Z, id: 10, name: 'Joe' },
members:
Expand All @@ -189,17 +190,22 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
sb.writeType({
type,
outStream: fs.createWriteStream('./type')
}, (err) => {
}, err => {
sb.readType(fs.createReadStream('./type'), (err, readType) => {
console.log(type.equals(readType)); //true
console.log(readType);
/*
EnumType {
type: StringType {},
values: [ 'ON_TIME', 'LATE', 'CANCELLED', 'UNKNOWN' ] }
*/
});
});
let value = 'CANCELLED';
sb.writeValue({
type,
value,
outStream: fs.createWriteStream('./value')
}, (err) => {
}, err => {
sb.readValue({
type,
inStream: fs.createReadStream('./value')
Expand All @@ -211,7 +217,7 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
type,
value,
outStream: fs.createWriteStream('./type-value')
}, (err) => {
}, err => {
sb.readTypeAndValue(fs.createReadStream('./type-value'), (err, type, value) => {
console.log(value); //'CANCELLED'
});
Expand All @@ -221,7 +227,7 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas

let type = new sb.DateType();
http.createServer((req, res) => {
sb.httpRespond({req, res, type, value: new Date()}, (err) => {
sb.httpRespond({req, res, type, value: new Date}, err => {
console.log('Responded');
});
}).listen(80);</code></pre><p>Client-side:</p>
Expand All @@ -245,8 +251,8 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
res.end(String(value));
});
}).listen(80);</code></pre><p>Client-side:</p>
<pre class="prettyprint source lang-html"><code>&lt;script src = './node_modules/structure-bytes/compiled/jquery.js'>&lt;/script>
&lt;script src = './node_modules/structure-bytes/compiled/upload.js'>&lt;/script>
<pre class="prettyprint source lang-html"><code>&lt;script src = '/structure-bytes/compiled/jquery.js'>&lt;/script>
&lt;script src = '/structure-bytes/compiled/upload.js'>&lt;/script>
&lt;button>Click me&lt;/button>
&lt;script>
var clickCount = 0;
Expand All @@ -269,7 +275,7 @@ <h2>Examples</h2><h3>Type creation</h3><pre class="prettyprint source lang-javas
<code>y</code> is the minor release; changes to it represent bug-fixing, non-breaking releases.
<code>z</code> is the version of the type and value specification, which is independent of the API version. It should match the version set in <code>config.js</code>.</p>
<h2>Testing</h2><p>To test the NodeJS code, run <code>npm test</code>.
To test the HTTP transaction code, run <code>node client-test/server.js</code> and open <code>localhost:8080</code> in your browser. Open each link in a new page and each page should alert <code>Success</code>.</p>
To test the HTTP transaction code, run <code>node client-test/server.js</code> and open <code>localhost:8080</code> in your browser. Open each link in a new page. <code>Upload</code> and <code>Download</code> should each alert <code>Success</code>, while <code>Upload &amp; Download</code> should alert <code>Upload: Success</code> and <code>Download: Success</code>.</p>
<p><em>Caleb Sander, 2016</em></p></article>
</section>

Expand Down

0 comments on commit 2b566af

Please sign in to comment.