Skip to content

Commit

Permalink
add newDatatypes.md and reorganize README, progress on #46
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Feb 19, 2016
1 parent 0e113d3 commit 6ec69d0
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 141 deletions.
148 changes: 7 additions & 141 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# ProtoDef
[![NPM version](https://img.shields.io/npm/v/protodef.svg)](http://npmjs.com/package/protodef)
[![Join the chat at https://gitter.im/roblabla/ProtoDef](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/roblabla/ProtoDef?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://img.shields.io/circleci/project/roblabla/ProtoDef/master.svg)]
(https://circleci.com/gh/roblabla/ProtoDef)
[![Build Status](https://img.shields.io/circleci/project/roblabla/ProtoDef/master.svg)](https://circleci.com/gh/roblabla/ProtoDef)
[![Tonic](https://img.shields.io/badge/tonic-try%20it-blue.svg)](https://tonicdev.com/npm/protodef)

This is a node.js module to simplify defining, reading and writing binary blobs,
Expand All @@ -19,9 +18,12 @@ npm install ProtoDef

See [example](example.js)

## Datatype documentation
## Documentation

[datatypes.md](doc/datatypes.md) documents the default datatypes provided by Protodef.
* [api.md](doc/datatypes.md) documents the exposed functions and classes
* [datatypes.md](doc/datatypes.md) documents the default datatypes provided by Protodef.
* [newDatatypes.md](doc/newDatatypes.md) explains how to create new datatypes for protodef
* [history.md](doc/history.md) is the releases history

## Projects Using ProtoDef

Expand All @@ -34,140 +36,4 @@ See [example](example.js)
* [node-raknet](https://github.com/mhsjlw/node-raknet) describe the raknet protocol in a protocol.json and uses ProtoDef to read it
* [minecraft-classic-protocol](https://github.com/mhsjlw/minecraft-classic-protocol) defines the classic minecraft protocol with ProtoDef
* [pocket-minecraft-protocol](https://github.com/mhsjlw/pocket-minecraft-protocol) defines the minecraft pocket edition protocol


## API

### ProtoDef()

#### ProtoDef.addType(name,functions)

Add the type `name` with the data `functions` which can be either:
* "native" : that type is already implemented by ProtoDef
* a js object defining a type based on other already defined types
* `[read,write,sizeOf]` functions

#### ProtoDef.read(buffer, cursor, _fieldInfo, rootNodes)

Read the packet defined by `_fieldInfo` in `buffer` starting from `cursor` using the context `rootNodes`.

#### ProtoDef.write(value, buffer, offset, _fieldInfo, rootNode)

Write the packet defined by `_fieldInfo` in `buffer` starting from `offset` with the value `value` and context `rootNode`

#### ProtoDef.sizeOf(value, _fieldInfo, rootNode)

Size of the packet `value` defined by `_fieldInfo` with context `rootNode`

#### ProtoDef.createPacketBuffer(type,packet)

Returns a buffer of the `packet` for `type`.

#### ProtoDef.parsePacketBuffer(type,buffer)

Returns a parsed packet of `buffer` for `type`.

### Serializer(proto,mainType)

Create a serializer of `mainType` defined in `proto`. This is a Transform stream.

#### Serializer.createPacketBuffer(packet)

Returns a buffer of the `packet`.

### Parser(proto,mainType)

Create a parser of `mainType` defined in `proto`. This is a Transform stream.

#### Parser.parsePacketBuffer(buffer)

Returns a parsed packet of `buffer`.

### types

An object mapping the default type names to the corresponding `[read,write,sizeOf]` functions.

### utils

Some functions that can be useful to build new datatypes reader and writer.

#### utils.getField(countField, context)

Get `countField` given `context`. Example: "../field" will get "field" one level above.

#### utils.getFieldInfo(fieldInfo)

Takes `fieldInfo` as :
* `"type"`
* `["type",typeArgs]`
* `{ type: "type", typeArgs: typeArgs }`

Returns `{ type: "type", typeArgs: typeArgs }`

#### utils.addErrorField(e, field)

Add `field` to error `e` and throw e.

#### utils.tryCatch(tryfn, catchfn)

A simple tryCatch function, useful for optimization.
returns what tryfn returns

#### utils.tryDoc(tryfn, field)

Try `tryfn`, it it fails, use addErrorField with `field`


## TODO
- Write tests for every datatypes, and the different \*Field behaviors.
- Rethink datatype function signature
- Datatypes should include name when creating them, instead of being provided
by the user, to ease datatype dependencies.
- Probably more...

## History

### 0.3.0
* add partial packet support

### 0.2.6
* add compareToValue (optional) option to switch

### 0.2.5

* fix small error in switch

### 0.2.4

* get back the example file as one file for simplicity and for tonic

### 0.2.3

* fix a small mistake in mapping error
* improve internal code
* improve example
* integrate with tonicdev

### 0.2.2

* Fix writeOption : the offset wasn't properly updated

### 0.2.1

* Anon fields may now be null/undefined.

### 0.2.0

* add createPacketBuffer and parsePacketBuffer to ProtoDef class
* expose utils functions
* add mapper and pstring datatypes

### 0.1.0

* add the serializer and parser
* expose the default datatypes
* add an example

### 0.0.1

* basic version, mostly contain the ProtoDef class and the datatype

80 changes: 80 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# API

## ProtoDef()

### ProtoDef.addType(name,functions)

Add the type `name` with the data `functions` which can be either:
* "native" : that type is already implemented by ProtoDef
* a js object defining a type based on other already defined types
* `[read,write,sizeOf]` functions

### ProtoDef.read(buffer, cursor, _fieldInfo, rootNodes)

Read the packet defined by `_fieldInfo` in `buffer` starting from `cursor` using the context `rootNodes`.

### ProtoDef.write(value, buffer, offset, _fieldInfo, rootNode)

Write the packet defined by `_fieldInfo` in `buffer` starting from `offset` with the value `value` and context `rootNode`

### ProtoDef.sizeOf(value, _fieldInfo, rootNode)

Size of the packet `value` defined by `_fieldInfo` with context `rootNode`

### ProtoDef.createPacketBuffer(type,packet)

Returns a buffer of the `packet` for `type`.

### ProtoDef.parsePacketBuffer(type,buffer)

Returns a parsed packet of `buffer` for `type`.

## Serializer(proto,mainType)

Create a serializer of `mainType` defined in `proto`. This is a Transform stream.

### Serializer.createPacketBuffer(packet)

Returns a buffer of the `packet`.

## Parser(proto,mainType)

Create a parser of `mainType` defined in `proto`. This is a Transform stream.

### Parser.parsePacketBuffer(buffer)

Returns a parsed packet of `buffer`.

## types

An object mapping the default type names to the corresponding `[read,write,sizeOf]` functions.

## utils

Some functions that can be useful to build new datatypes reader and writer.

### utils.getField(countField, context)

Get `countField` given `context`. Example: "../field" will get "field" one level above.

### utils.getFieldInfo(fieldInfo)

Takes `fieldInfo` as :
* `"type"`
* `["type",typeArgs]`
* `{ type: "type", typeArgs: typeArgs }`

Returns `{ type: "type", typeArgs: typeArgs }`

### utils.addErrorField(e, field)

Add `field` to error `e` and throw e.

### utils.tryCatch(tryfn, catchfn)

A simple tryCatch function, useful for optimization.
returns what tryfn returns

### utils.tryDoc(tryfn, field)

Try `tryfn`, it it fails, use addErrorField with `field`
46 changes: 46 additions & 0 deletions doc/history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# History

## 0.3.0
* add partial packet support

## 0.2.6
* add compareToValue (optional) option to switch

## 0.2.5

* fix small error in switch

## 0.2.4

* get back the example file as one file for simplicity and for tonic

## 0.2.3

* fix a small mistake in mapping error
* improve internal code
* improve example
* integrate with tonicdev

## 0.2.2

* Fix writeOption : the offset wasn't properly updated

## 0.2.1

* Anon fields may now be null/undefined.

## 0.2.0

* add createPacketBuffer and parsePacketBuffer to ProtoDef class
* expose utils functions
* add mapper and pstring datatypes

## 0.1.0

* add the serializer and parser
* expose the default datatypes
* add an example

## 0.0.1

* basic version, mostly contain the ProtoDef class and the datatype
Loading

0 comments on commit 6ec69d0

Please sign in to comment.