7
7
Powerful, lightweight binary formats in TypeScript.
8
8
9
9
[ ![ NPM version] ( https://img.shields.io/npm/v/typescript-binary.svg?style=flat-square )] ( https://www.npmjs.com/package/typescript-binary )
10
+ [ ![ test] ( https://github.com/reececomo/typescript-binary/actions/workflows/test.yml/badge.svg )] ( https://github.com/reececomo/typescript-binary/actions/workflows/test.yml )
11
+ [ ![ test] ( https://github.com/reececomo/typescript-binary/actions/workflows/lint.yml/badge.svg )] ( https://github.com/reececomo/typescript-binary/actions/workflows/lint.yml )
10
12
11
13
</div >
12
14
13
- * Compatible with [ geckos.io] ( https://github.com/geckosio/geckos.io ) , [ socket.io] ( https://github.com/socketio/socket.io ) and [ peer.js] ( https://github.com/peers/peerjs ) .
14
- * Similar to [ FlatBuffers] ( https://github.com/google/flatbuffers ) and [ Protocol Buffers] ( https://protobuf.dev/ ) , with zero dependencies.
15
- * Hard-forked from the fantastic [ sitegui/js-binary] ( https://github.com/sitegui/js-binary ) library, written by [ Guilherme Souza] ( https://github.com/sitegui ) .
15
+ - Compatible with [ geckos.io] ( https://github.com/geckosio/geckos.io ) , [ socket.io] ( https://github.com/socketio/socket.io ) and [ peer.js] ( https://github.com/peers/peerjs ) .
16
+ - Similar to [ FlatBuffers] ( https://github.com/google/flatbuffers ) and [ Protocol Buffers] ( https://protobuf.dev/ ) , with zero dependencies.
17
+ - Hard-forked from the fantastic [ sitegui/js-binary] ( https://github.com/sitegui/js-binary ) library, written by [ Guilherme Souza] ( https://github.com/sitegui ) .
16
18
17
19
## Install
18
20
@@ -25,26 +27,28 @@ Powerful, lightweight binary formats in TypeScript.
25
27
Define a ` BinaryCode ` like so:
26
28
27
29
``` js
28
- import { BinaryCodec , Type , Optional } from ' typescript-binary' ;
30
+ import { BinaryCoder , Type , Optional } from " typescript-binary" ;
29
31
30
32
// Define
31
- const GameWorldData = new BinaryCodec ({
33
+ const GameWorldData = new BinaryCoder ({
32
34
timeRemaining: Type .UInt ,
33
- players: [{
34
- id: Type .String ,
35
- health: Type .UInt8 ,
36
- position: Optional ({
37
- x: Type .Float34 ,
38
- y: Type .Float34
39
- }),
40
- isJumping: Type .Boolean
41
- }],
35
+ players: [
36
+ {
37
+ id: Type .String ,
38
+ health: Type .UInt8 ,
39
+ position: Optional ({
40
+ x: Type .Float34 ,
41
+ y: Type .Float34 ,
42
+ }),
43
+ isJumping: Type .Boolean ,
44
+ },
45
+ ],
42
46
});
43
47
44
48
// Encode
45
49
const binary = GameWorldData .encode (gameWorld .getState ());
46
50
47
- binary .byteLength
51
+ binary .byteLength ;
48
52
// 20
49
53
50
54
// Decode
@@ -65,9 +69,9 @@ const data = GameWorldData.decode(binary);
65
69
66
70
### Handling multiple binary formats
67
71
68
- By default, each ` BinaryCodec ` includes a 2-byte ` UInt16 ` identifier. This can be disabled by setting ` Id ` as ` false ` in the ` BinaryCodec ` constructor. You can also provide your own fixed identifier instead (i.e. an ` Enum ` ).
72
+ By default, each ` BinaryCoder ` includes a 2-byte ` UInt16 ` identifier. This can be disabled by setting ` Id ` as ` false ` in the ` BinaryCoder ` constructor. You can also provide your own fixed identifier instead (i.e. an ` Enum ` ).
69
73
70
- Read the identifer with the static function ` BinaryCodec .peekId(...)` .
74
+ Read the identifer with the static function ` BinaryCoder .peekId(...)` .
71
75
72
76
#### BinaryFormatHandler
73
77
@@ -86,34 +90,34 @@ binaryHandler.processBuffer(binary);
86
90
87
91
Here are all the ready-to-use types:
88
92
89
- | ** Type** | ** JavaScript Type** | ** Bytes** | ** About** |
90
- | :---: | :---: | :---: | ---|
91
- | ` Type.Int ` | ` number ` | 1-8<sup >* </sup > | Integer up to ` ±Number.MAX_SAFE_INTEGER ` . Dynamically sized. |
92
- | ` Type.Int8 ` | ` number ` | 1 | Integer between -127 to 128. |
93
- | ` Type.Int16 ` | ` number ` | 2 | Integer between -32,767 to 32,767. |
94
- | ` Type.Int32 ` | ` number ` | 4 | Integer between -2,147,483,647 to 2,147,483,647. |
95
- | ` Type.UInt ` | ` number ` | 1-8<sup >#</sup > | Unsigned integer up to ` Number.MAX_SAFE_INTEGER ` . Dynamically sized. |
96
- | ` Type.UInt8 ` | ` number ` | 1 | Unsigned integer up to 255. |
97
- | ` Type.UInt16 ` | ` number ` | 2 | Unsigned integer up to 65,535. |
98
- | ` Type.UInt32 ` | ` number ` | 4 | Unsigned integer up to 4,294,967,295. |
99
- | ` Type.Float16 ` | ` number ` | 2 | A 16-bit "half-precision" floating point.<br />** Important Note:** Low decimal precision. Max. large values ±65,500. |
100
- | ` Type.Float32 ` | ` number ` | 4 | A 32-bit "single-precision" floating point. |
101
- | ` Type.Float64 ` | ` number ` | 8 | Default JavaScript ` number ` type. A 64-bit "double-precision" floating point. |
102
- | ` Type.String ` | ` string ` | 1<sup >†</sup >  ; +  ; n | A UTF-8 string. |
103
- | ` Type.Boolean ` | ` boolean ` | 1 | A single boolean. |
104
- | ` Type.BooleanTuple ` | ` boolean[] ` | 1<sup >¶</sup > | An array of booleans. Dynamically sized. |
105
- | ` Type.Bitmask8 ` | ` boolean[] ` | 1 | 1-8 booleans. |
106
- | ` Type.Bitmask16 ` | ` boolean[] ` | 2 | 1-16 booleans. |
107
- | ` Type.Bitmask32 ` | ` boolean[] ` | 4 | 1-32 booleans. |
108
- | ` Type.JSON ` | ` any ` | 1<sup >†</sup >  ; +  ; n | [ JSON format] ( http://json.org/ ) data, encoded as a UTF-8 string. |
109
- | ` Type.Binary ` | ` ArrayBuffer ` | 1<sup >†</sup >  ; +  ; n | JavaScript ` ArrayBuffer ` data. |
110
- | ` Type.RegExp ` | ` RegExp ` | 1<sup >†</sup >  ; +  ; n  ; +  ; 1 | JavaScript ` RegExp ` object. |
111
- | ` Type.Date ` | ` Date ` | 8 | JavaScript ` Date ` object. |
112
- | ` Optional(T) ` | ` T \| undefined ` | 1 | Any optional field. Use the ` Optional(...) ` helper. Array elements cannot be optional. |
113
- | ` [T] ` | ` Array<T> ` | 1<sup >†</sup >  ; +  ; n | Use array syntax. Any array. |
114
- | ` {} ` | ` object ` | _ none_ | Use object syntax. No overhead to using object types. Buffers are ordered, flattened structures. |
115
-
116
- <sup >* </sup >` Int ` encodes <±64 = 1 byte, <±8,192 = 2 bytes, <±268,435,456 = 4 bytes, otherwise = 8 bytes.
93
+ | ** Type** | ** JavaScript Type** | ** Bytes** | ** About** |
94
+ | :-----------------: | :-----------------: | :---------------------------------------: | ------------------------------------------------------------------------------------------------------------------- |
95
+ | ` Type.Int ` | ` number ` | 1-8<sup >\ * </sup > | Integer up to ` ±Number.MAX_SAFE_INTEGER ` . Dynamically sized. |
96
+ | ` Type.Int8 ` | ` number ` | 1 | Integer between -127 to 128. |
97
+ | ` Type.Int16 ` | ` number ` | 2 | Integer between -32,767 to 32,767. |
98
+ | ` Type.Int32 ` | ` number ` | 4 | Integer between -2,147,483,647 to 2,147,483,647. |
99
+ | ` Type.UInt ` | ` number ` | 1-8<sup >#</sup > | Unsigned integer up to ` Number.MAX_SAFE_INTEGER ` . Dynamically sized. |
100
+ | ` Type.UInt8 ` | ` number ` | 1 | Unsigned integer up to 255. |
101
+ | ` Type.UInt16 ` | ` number ` | 2 | Unsigned integer up to 65,535. |
102
+ | ` Type.UInt32 ` | ` number ` | 4 | Unsigned integer up to 4,294,967,295. |
103
+ | ` Type.Float16 ` | ` number ` | 2 | A 16-bit "half-precision" floating point.<br />** Important Note:** Low decimal precision. Max. large values ±65,500. |
104
+ | ` Type.Float32 ` | ` number ` | 4 | A 32-bit "single-precision" floating point. |
105
+ | ` Type.Float64 ` | ` number ` | 8 | Default JavaScript ` number ` type. A 64-bit "double-precision" floating point. |
106
+ | ` Type.String ` | ` string ` | 1<sup >†</sup >  ; +  ; n | A UTF-8 string. |
107
+ | ` Type.Boolean ` | ` boolean ` | 1 | A single boolean. |
108
+ | ` Type.BooleanTuple ` | ` boolean[] ` | 1<sup >¶</sup > | An array of booleans. Dynamically sized. |
109
+ | ` Type.Bitmask8 ` | ` boolean[] ` | 1 | 1-8 booleans. |
110
+ | ` Type.Bitmask16 ` | ` boolean[] ` | 2 | 1-16 booleans. |
111
+ | ` Type.Bitmask32 ` | ` boolean[] ` | 4 | 1-32 booleans. |
112
+ | ` Type.JSON ` | ` any ` | 1<sup >†</sup >  ; +  ; n | [ JSON format] ( http://json.org/ ) data, encoded as a UTF-8 string. |
113
+ | ` Type.Binary ` | ` ArrayBuffer ` | 1<sup >†</sup >  ; +  ; n | JavaScript ` ArrayBuffer ` data. |
114
+ | ` Type.RegExp ` | ` RegExp ` | 1<sup >†</sup >  ; +  ; n  ; +  ; 1 | JavaScript ` RegExp ` object. |
115
+ | ` Type.Date ` | ` Date ` | 8 | JavaScript ` Date ` object. |
116
+ | ` Optional(T) ` | ` T \| undefined ` | 1 | Any optional field. Use the ` Optional(...) ` helper. Array elements cannot be optional. |
117
+ | ` [T] ` | ` Array<T> ` | 1<sup >†</sup >  ; +  ; n | Use array syntax. Any array. |
118
+ | ` {} ` | ` object ` | _ none_ | Use object syntax. No overhead to using object types. Buffers are ordered, flattened structures. |
119
+
120
+ <sup >\ * </sup >` Int ` encodes <±64 = 1 byte, <±8,192 = 2 bytes, <±268,435,456 = 4 bytes, otherwise = 8 bytes.
117
121
118
122
<sup >#</sup >` UInt ` encodes <128 = 1 byte, <16,384 = 2 bytes, <536,870,912 = 4 bytes, otherwise = 8 bytes.
119
123
0 commit comments