You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+18-10
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,29 @@
2
2
3
3
bufferfish is utility library for working with binary network messages between Rust and TypeScript, such as over WebSockets. It provides a simple API for encoding and decoding data into binary arrays, as well as generating TypeScript definitions and decoding functions from your Rust code.
4
4
5
-
_This library has an unstable API and is missing a variety of functionality. I can't recommend using it in production, although I am using it for my own production project._
5
+
_This library has an unstable API and may be missing some basic functionality. I can't recommend using it in production, although I am using it for my own production project._
6
6
7
7
## Repository Overview
8
8
9
9
There are two seperate libraries in this repo: one for Rust and one for TypeScript. Neither of the libraries have any required dependencies. The Rust version optionally uses the `unicode-width` crate for formatting buffer output when `pretty-print` is enabled.
10
10
11
11
The Rust library is broken into three seperate crates:
12
12
13
-
### /bufferfish
13
+
### /rust/bufferfish
14
14
15
15
`bufferfish` is a re-export of the other crates, as well as a `generate` function for use in `build.rs` files in order to generate TypeScript definitions from your Rust packet ID type. This is what users will interact with directly. General tests also live here.
16
16
17
-
### /bufferfish-derive
17
+
### /rust/bufferfish-derive
18
18
19
19
`bufferfish_derive` is where the proc macro code for the `#[derive(Encode)]` and `#[derive(Decode)]` macros live. These annotations implement `Encodable` and `Decodable` - respectively - for the annotated type.
20
20
21
-
### /bufferfish-core
21
+
### /rust/bufferfish-core
22
22
23
23
`bufferfish_core`is the primary library implementation. Trait and type definitions, byte and cursor logic, and error handling live here.
24
24
25
-
TypeScript code lives alone within the `/typescript` directory.
25
+
### /typescript/bufferfish
26
+
27
+
The TypeScript implementation lives here. The API is generally mirrored from the Rust version.
- I strongly recommend the usage of the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on your packet ID enum. This removes a lot of boilerplate.
228
+
- I recommend using the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on enums you wish to `Encode`. This removes a lot of boilerplate.
225
229
- Enums in TypeScript are often mentioned as a "bad" feature, and this is generally true when considering typical web development use-cases. In the case of a list of "op codes" mapping to dev-friendly names, however, they are actually really useful. Modern bundlers - like `esbuild` - [can actually inline them, meaning we just get integer literals in the final output.](https://sombia.com/posts/typescript-enums).
226
230
227
231
## Security
@@ -232,6 +236,10 @@ When reading data, you will always get the correct return type - however, you ar
232
236
233
237
This kind of problem should be dealt with before operating on the buffer.
234
238
239
+
Decoding an oversized bufferfish via the `Decode` trait will just ignore / discard the additional data, as it is only going to read specific byte lengths generated by the `Encodable` impl.
240
+
241
+
Decoding an undersized bufferfish will return a `BufferfishError::FailedWrite`.
242
+
235
243
## Contributing
236
244
237
245
`bufferfish` is open to contributions, however it should be noted that the library was created for my own game projects, and I am not interested in making it widely general-purpose. If you have a feature request or bug fix that you think would be useful to others, feel free to open an issue or PR either way.
Copy file name to clipboardexpand all lines: rust/bufferfish-core/README.md
-2
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
Core types, traits, and logic implementations for [bufferfish](https://github.com/robertwayne/bufferfish).
4
4
5
-
_This project has an unstable API and is missing a variety of functionality. I can't recommend using it in production, although I am using it for my own production project._
6
-
7
5
## Usage
8
6
9
7
See the [main project repository](https://github.com/robertwayne/bufferfish) for more details.
Copy file name to clipboardexpand all lines: rust/bufferfish-derive/README.md
-2
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,6 @@
2
2
3
3
Macros for [bufferfish](https://github.com/robertwayne/bufferfish).
4
4
5
-
_This project has an unstable API and is missing a variety of functionality. I can't recommend using it in production, although I am using it for my own production project._
6
-
7
5
## Usage
8
6
9
7
See the [main project repository](https://github.com/robertwayne/bufferfish) for more details.
Copy file name to clipboardexpand all lines: rust/bufferfish/README.md
+14-8
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
`bufferfish` is utility library for working with binary network messages between Rust and TypeScript, such as over WebSockets. It provides a simple API for encoding and decoding data into binary arrays, as well as generating TypeScript definitions and decoding functions from your Rust code.
4
4
5
-
_This library has an unstable API and is missing a variety of functionality. I can't recommend using it in production, although I am using it for my own production project._
5
+
_This library has an unstable API and may be missing some basic functionality. I can't recommend using it in production, although I am using it for my own production project._
@@ -186,19 +186,25 @@ Supported Types | Decodes As
186
186
`Vec<T> where T: Encodable` | `Array<T>`
187
187
`T where T: Encodable` | `object` or primitive
188
188
189
+
_*The reverse is true for decoding._
190
+
189
191
## Notes
190
192
191
-
- I strongly recommend the usage of the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on your packet ID enum. This removes a lot of boilerplate.
192
-
- Enums in TypeScript are often mentioned as a "bad" feature, and this is generally true when considering typical web development use-cases. In the case of a list of "op codes" mapping to dev-friendly names, however, they are actually really useful. Modern bundlers - like `esbuild` - [can actually inline them, meaning we just get integer literals in the final output.](https://sombia.com/posts/typescript-enums).
193
+
- I recommend using the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on enums you wish to `Encode`. This removes a lot of boilerplate.
194
+
- Enums in TypeScript are often mentioned as a "bad" feature, and this is generally true when considering typical web development use-cases. In the case of a list of "op codes", mapping to dev-friendly names, however, they are actually really useful. Modern bundlers - like `esbuild` - [can actually inline them, meaning we just get integer literals in the final output.](https://sombia.com/posts/typescript-enums).
193
195
194
-
##Security
196
+
# Security
195
197
196
198
`bufferfish` functions ensure inputs are valid as a "best effort". Internal buffers are constructed with a maximum capacity _(default of 1024 bytes)_, and will fail to construct if an input would cause the internal buffer to cross that threshold.
197
199
198
200
When reading data, you will always get the correct return type - however, you are still subject to corrupted data if the input was incorrect but technically valid. For example, if you call `read_u8` on a buffer that contains a `u16` at the cursor position, you will get a `u8` back, as the buffer has no way to know that it was originally encoded as a `u16`. It is valid data, but will very likely be an unexpected value.
199
201
200
202
This kind of problem should be dealt with before operating on the buffer.
201
203
204
+
Decoding an oversized bufferfish via the `Decode` trait will just ignore / discard the additional data, as it is only going to read specific byte lengths generated by the `Encodable` impl.
205
+
206
+
Decoding an undersized bufferfish will return a `BufferfishError::FailedWrite`.
207
+
202
208
## Contributing
203
209
204
210
`bufferfish` is open to contributions, however it should be noted that the library was created for my own game projects, and I am not interested in making it widely general-purpose. If you have a feature request or bug fix that you think would be useful to others, feel free to open an issue or PR either way.
Copy file name to clipboardexpand all lines: typescript/README.md
+39-12
Original file line number
Diff line number
Diff line change
@@ -2,42 +2,63 @@
2
2
3
3
`bufferfish` is utility library for working with binary network messages between Rust and TypeScript, such as over WebSockets. It provides a simple API for encoding and decoding data into binary arrays, as well as generating TypeScript definitions and decoding functions from your Rust code.
4
4
5
-
_This library has an unstable API and is missing a variety of functionality. I can't recommend using it in production, although I am using it for my own production project._
6
-
7
5
See the [project repository](https://github.com/robertwayne/bufferfish) for more details, including how to interop with Rust.
8
6
7
+
_This library has an unstable API and may be missing some basic functionality. I can't recommend using it in production, although I am using it for my own production project._
These are built when defining a struct or enum in Rust with the `#[derive(Encode)]` macro after calling the `bufferfish::generate()` function.
24
49
25
50
```typescript
26
51
const ws =newWebSocket("ws://127.0.0.1:3000")
27
52
ws.binaryType="arraybuffer"
28
53
29
54
ws.onmessage= (event) => {
30
-
const bf =newBufferfish(event.data)
31
-
const packetId =bf.readUint16()
55
+
const bf =newBufferfish(event.data)
56
+
const packetId =bf.readUint16()
32
57
33
58
if (packetId===PacketId.Join) {
34
-
const id =bf.readUint32()
35
-
const username =bf.readString()
59
+
const packet =decodeJoinPacket(bf)
36
60
37
-
console.log({
38
-
id,
39
-
username,
40
-
}) // { id: 1, username: "Rob" }
61
+
console.log(packet) // { id: 1, username: "Rob" }
41
62
}
42
63
}
43
64
```
@@ -123,9 +144,11 @@ Supported Types | Decodes As
123
144
`Vec<T> where T: Encodable` | `Array<T>`
124
145
`T where T: Encodable` | `object` or primitive
125
146
147
+
_*The reverse is true for decoding._
148
+
126
149
## Notes
127
150
128
-
- I strongly recommend the usage of the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on your packet ID enum. This removes a lot of boilerplate.
151
+
- I recommend using the [num_enum](https://github.com/illicitonion/num_enum) crate for deriving `IntoPrimitive` and `FromPrimitve` on enums you wish to `Encode`. This removes a lot of boilerplate.
129
152
- Enums in TypeScript are often mentioned as a "bad" feature, and this is generally true when considering typical web development use-cases. In the case of a list of "op codes" mapping to dev-friendly names, however, they are actually really useful. Modern bundlers - like `esbuild` - [can actually inline them, meaning we just get integer literals in the final output.](https://sombia.com/posts/typescript-enums).
130
153
131
154
## Security
@@ -136,6 +159,10 @@ When reading data, you will always get the correct return type - however, you ar
136
159
137
160
This kind of problem should be dealt with before operating on the buffer.
138
161
162
+
Decoding an oversized bufferfish via the `Decode` trait will just ignore / discard the additional data, as it is only going to read specific byte lengths generated by the `Encodable` impl.
163
+
164
+
Decoding an undersized bufferfish will return a `BufferfishError::FailedWrite`.
165
+
139
166
## Contributing
140
167
141
168
`bufferfish` is open to contributions, however it should be noted that the library was created for my own game projects, and I am not interested in making it widely general-purpose. If you have a feature request or bug fix that you think would be useful to others, feel free to open an issue or PR either way.
0 commit comments