Skip to content

Commit d7569f6

Browse files
authored
v1.5.0 (#3)
1 parent 42896c8 commit d7569f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+4379
-1194
lines changed

.eslintrc

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"root": true,
3+
"rules": {
4+
"newline-before-return": 0,
5+
"prefer-const": 0,
6+
"no-fallthrough": 0,
7+
"@typescript-eslint/no-duplicate-enum-values": 0,
8+
"@typescript-eslint/no-inferrable-types": 0,
9+
"@typescript-eslint/no-explicit-any": 0,
10+
"@typescript-eslint/no-empty-interface": 0,
11+
"@typescript-eslint/no-non-null-assertion": 0,
12+
"@typescript-eslint/no-empty-function": 0,
13+
"@typescript-eslint/indent": 0,
14+
"@typescript-eslint/no-extra-semi": 0,
15+
"jsdoc/require-jsdoc": 0,
16+
"jsdoc/require-param": 0,
17+
"jsdoc/require-returns": 0,
18+
"jsdoc/require-param-type": 0,
19+
"jsdoc/require-param-description": 0,
20+
"jsdoc/require-returns-type": 0,
21+
"jsdoc/require-returns-description": 0,
22+
"jsdoc/tag-lines": 0,
23+
"jsdoc/multiline-blocks": 0,
24+
"@typescript-eslint/explicit-member-accessibility": 1,
25+
"jsdoc/check-line-alignment": 1,
26+
"jsdoc/check-syntax": 1,
27+
"jsdoc/no-blank-block-descriptions": 1,
28+
"jsdoc/no-blank-blocks": 1,
29+
"jsdoc/informative-docs": 1,
30+
"jsdoc/no-defaults": 1,
31+
"jsdoc/no-types": 1,
32+
"no-trailing-spaces": "warn",
33+
"@typescript-eslint/ban-types": "warn",
34+
"disable-autofix/jsdoc/require-jsdoc": [
35+
"warn",
36+
{
37+
"checkConstructors": false,
38+
"publicOnly": true,
39+
"require": {
40+
"MethodDefinition": true
41+
}
42+
}
43+
],
44+
"max-len": [
45+
"warn",
46+
200
47+
],
48+
"brace-style": [
49+
"warn",
50+
"stroustrup"
51+
],
52+
"curly": "warn",
53+
"semi": [
54+
"warn",
55+
"always"
56+
],
57+
"import-newlines/enforce": [
58+
"warn",
59+
{
60+
"items": 3,
61+
"max-len": 80,
62+
"semi": true
63+
}
64+
],
65+
"indent": [
66+
"warn",
67+
2,
68+
{
69+
"SwitchCase": 1
70+
}
71+
],
72+
"@typescript-eslint/explicit-function-return-type": [
73+
"error",
74+
{
75+
"allowExpressions": true
76+
}
77+
],
78+
"@typescript-eslint/no-unused-vars": [
79+
"warn",
80+
{
81+
"args": "none"
82+
}
83+
]
84+
},
85+
"extends": [
86+
"eslint:recommended",
87+
"plugin:@typescript-eslint/eslint-recommended",
88+
"plugin:@typescript-eslint/recommended",
89+
"plugin:@typescript-eslint/strict",
90+
"plugin:jsdoc/recommended"
91+
],
92+
"parser": "@typescript-eslint/parser",
93+
"parserOptions": {
94+
"ecmaVersion": 2020,
95+
"sourceType": "module"
96+
},
97+
"plugins": [
98+
"@typescript-eslint",
99+
"import-newlines",
100+
"jsdoc",
101+
"eslint-plugin-disable-autofix"
102+
]
103+
}

.github/workflows/lint.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: "lint"
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
paths:
7+
- "**/*.ts"
8+
pull_request:
9+
branches: ["main"]
10+
paths:
11+
- "**/*.ts"
12+
13+
jobs:
14+
lint:
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [16.x]
20+
21+
steps:
22+
- uses: actions/checkout@v3
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- run: npm install
28+
- run: npm run lint

.github/workflows/test.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "test"
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
paths:
7+
- "**/*.js"
8+
- "**/*.ts"
9+
pull_request:
10+
branches: ["*"]
11+
paths:
12+
- "**/*.js"
13+
- "**/*.ts"
14+
15+
jobs:
16+
test:
17+
runs-on: ubuntu-latest
18+
19+
strategy:
20+
matrix:
21+
node-version: [16.x]
22+
23+
steps:
24+
- uses: actions/checkout@v3
25+
- name: Use Node.js ${{ matrix.node-version }}
26+
uses: actions/setup-node@v3
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
- run: npm install
30+
- run: npm run test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.DS_Store
3+
coverage/

.vscode/settings.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"editor.rulers": [
3+
120
4+
],
5+
"editor.tabSize": 2,
6+
"editor.formatOnSave": true,
7+
"editor.codeActionsOnSave": {
8+
"source.fixAll.eslint": "explicit"
9+
},
10+
"eslint.format.enable": true,
11+
"files.eol": "\n",
12+
"search.exclude": {
13+
"**/node_modules": true,
14+
"**/dist": true,
15+
"**/package-lock.json": true,
16+
},
17+
"typescript.preferences.importModuleSpecifier": "relative",
18+
"workbench.tree.indent": 20,
19+
}

README.md

+49-45
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
Powerful, lightweight binary formats in TypeScript.
88

99
[![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)
1012

1113
</div>
1214

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).
1618

1719
## Install
1820

@@ -25,26 +27,28 @@ Powerful, lightweight binary formats in TypeScript.
2527
Define a `BinaryCode` like so:
2628

2729
```js
28-
import { BinaryCodec, Type, Optional } from 'typescript-binary';
30+
import { BinaryCoder, Type, Optional } from "typescript-binary";
2931

3032
// Define
31-
const GameWorldData = new BinaryCodec({
33+
const GameWorldData = new BinaryCoder({
3234
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+
],
4246
});
4347

4448
// Encode
4549
const binary = GameWorldData.encode(gameWorld.getState());
4650

47-
binary.byteLength
51+
binary.byteLength;
4852
// 20
4953

5054
// Decode
@@ -65,9 +69,9 @@ const data = GameWorldData.decode(binary);
6569

6670
### Handling multiple binary formats
6771

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`).
6973

70-
Read the identifer with the static function `BinaryCodec.peekId(...)`.
74+
Read the identifer with the static function `BinaryCoder.peekId(...)`.
7175

7276
#### BinaryFormatHandler
7377

@@ -86,34 +90,34 @@ binaryHandler.processBuffer(binary);
8690

8791
Here are all the ready-to-use types:
8892

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>&nbsp;+&nbsp;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>&nbsp;+&nbsp;n | [JSON format](http://json.org/) data, encoded as a UTF-8 string. |
109-
| `Type.Binary` | `ArrayBuffer` | 1<sup>†</sup>&nbsp;+&nbsp;n | JavaScript `ArrayBuffer` data. |
110-
| `Type.RegExp` | `RegExp` | 1<sup>†</sup>&nbsp;+&nbsp;n&nbsp;+&nbsp;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>&nbsp;+&nbsp;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>&nbsp;+&nbsp;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>&nbsp;+&nbsp;n | [JSON format](http://json.org/) data, encoded as a UTF-8 string. |
113+
| `Type.Binary` | `ArrayBuffer` | 1<sup>†</sup>&nbsp;+&nbsp;n | JavaScript `ArrayBuffer` data. |
114+
| `Type.RegExp` | `RegExp` | 1<sup>†</sup>&nbsp;+&nbsp;n&nbsp;+&nbsp;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>&nbsp;+&nbsp;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.
117121

118122
<sup>#</sup>`UInt` encodes <128 = 1 byte, <16,384 = 2 bytes, <536,870,912 = 4 bytes, otherwise = 8 bytes.
119123

dist/core/BinaryCodec.d.ts.map

-1
This file was deleted.

0 commit comments

Comments
 (0)