@rbxts/netweave 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +149 -2
- package/index.d.ts +1226 -0
- package/macros.d.ts +87 -0
- package/macros.luau +27 -0
- package/package.json +34 -5
- package/src/api/Audience.luau +156 -0
- package/src/api/Channel.luau +1288 -0
- package/src/api/Config.luau +884 -0
- package/src/api/Context.luau +385 -0
- package/src/api/Namespace.luau +540 -0
- package/src/api/Observer.luau +493 -0
- package/src/api/Policy.luau +457 -0
- package/src/api/Protocol.luau +593 -0
- package/src/api/Transport.luau +135 -0
- package/src/api/Trust.luau +393 -0
- package/src/api/View.luau +503 -0
- package/src/codec/Buffer.luau +1272 -0
- package/src/codec/Ir.luau +1353 -0
- package/src/codec/Serdes.luau +2694 -0
- package/src/netweave.luau +490 -0
- package/src/replication/Baseline.luau +314 -0
- package/src/replication/Delta.luau +613 -0
- package/src/replication/Store.luau +229 -0
- package/src/replication/Tick.luau +597 -0
- package/src/transport/Batch.luau +970 -0
- package/src/transport/Budget.luau +249 -0
- package/src/transport/Driver.luau +305 -0
- package/src/transport/Inbound.luau +1508 -0
- package/src/transport/Link.luau +162 -0
- package/src/transport/Outbound.luau +523 -0
- package/src/transport/Pacer.luau +120 -0
- package/src/transport/Query.luau +535 -0
- package/src/transport/Recipients.luau +507 -0
- package/src/types/init.luau +2064 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hawakiki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,150 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rbxts/netweave
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[netweave](https://github.com/Hawakiki/netweave) for roblox-ts: a networking library for Roblox in
|
|
4
|
+
which a channel is declared once, with its payload, its rate and who may send it, and both sides
|
|
5
|
+
are typed from that declaration.
|
|
6
|
+
|
|
7
|
+
This package is the Luau of netweave itself, at commit
|
|
8
|
+
[`d9f6185`](https://github.com/Hawakiki/netweave/tree/d9f6185) with two patches, the fixes for
|
|
9
|
+
[netweave#10](https://github.com/Hawakiki/netweave/issues/10) and
|
|
10
|
+
[netweave#15](https://github.com/Hawakiki/netweave/issues/15), and TypeScript types for it. Its companion `rbxts-transformer-netweave` runs in the roblox-ts build: it lets a
|
|
11
|
+
payload be written as a type and a handler as a method, loads the declarations before anything
|
|
12
|
+
else in every script, and reports at build time what would otherwise fail in a running game.
|
|
13
|
+
|
|
14
|
+
## Why
|
|
15
|
+
|
|
16
|
+
What netweave does that a game would otherwise write into each RemoteEvent handler:
|
|
17
|
+
|
|
18
|
+
- On the server, a packet reaches the handler of a `command` only once it fits the channel's
|
|
19
|
+
schema, came within the sender's rate and was allowed by the policy, and the handler receives it
|
|
20
|
+
as `nw.Trusted<T>`. A `command` declared without `rate` or `authorize` is a type error.
|
|
21
|
+
- The client's `send` and the server's handler are typed from the same declaration.
|
|
22
|
+
- A refused packet raises nothing. It becomes a record of its channel, player, stage and reason,
|
|
23
|
+
which a function given to `nw.observe` receives and `nw.diagnostics()` counts.
|
|
24
|
+
- The build stops on a namespace declared where one side cannot load it, on an unreliable channel
|
|
25
|
+
that can be larger than a packet and on two handlers for one channel, and warns of a channel the
|
|
26
|
+
program does not use from both ends.
|
|
27
|
+
|
|
28
|
+
What it asks in return is exact versions of roblox-ts and TypeScript, a transformer in the build and
|
|
29
|
+
`"incremental"` turned off, as
|
|
30
|
+
[Step 1 of the tutorial](https://github.com/Hawakiki/netweave-ts/blob/main/docs/tutorial/step1-install.md)
|
|
31
|
+
and the [known limitations](https://github.com/Hawakiki/netweave-ts/blob/main/docs/reference/limitations.md)
|
|
32
|
+
say.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
<!-- docs:excerpt docs/tutorial/step1-install.md -->
|
|
37
|
+
```sh
|
|
38
|
+
npm install --save-dev --save-exact roblox-ts@3.0.0 @rbxts/compiler-types@3.0.0-types.0 @rbxts/types@1.0.963
|
|
39
|
+
npm install @rbxts/netweave
|
|
40
|
+
npm install --save-dev rbxts-transformer-netweave
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
and in `compilerOptions` of `tsconfig.json`:
|
|
44
|
+
|
|
45
|
+
<!-- docs:excerpt examples/tutorial/tsconfig.json -->
|
|
46
|
+
```json
|
|
47
|
+
"experimentalDecorators": true,
|
|
48
|
+
"plugins": [{ "transform": "rbxts-transformer-netweave" }]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The versions are exact. The transformer runs on roblox-ts 3.0.0 and the TypeScript 5.5.3 it brings,
|
|
52
|
+
and stops the build on any other; the two type packages are the versions the repository is checked
|
|
53
|
+
with. Install `@rbxts/netweave` and the transformer of the same minor version, 0.1.x with 0.1.x.
|
|
54
|
+
`"incremental"` has to be off, and a project made with `npm create roblox-ts` has it on, so take it out.
|
|
55
|
+
[Step 1 of the tutorial](https://github.com/Hawakiki/netweave-ts/blob/main/docs/tutorial/step1-install.md)
|
|
56
|
+
has the whole setup.
|
|
57
|
+
|
|
58
|
+
## A declaration, a server and a client
|
|
59
|
+
|
|
60
|
+
One module, which both sides load, declares the channels. `nw` is the library. `schema` and
|
|
61
|
+
`listen` come from `@rbxts/netweave/macros`, and the transformer replaces them. `mayEquip` is a
|
|
62
|
+
policy, written in
|
|
63
|
+
[Step 3 of the tutorial](https://github.com/Hawakiki/netweave-ts/blob/main/docs/tutorial/step3-policies.md).
|
|
64
|
+
|
|
65
|
+
<!-- docs:excerpt examples/tutorial/src/shared/net.ts -->
|
|
66
|
+
```ts
|
|
67
|
+
import nw from "@rbxts/netweave";
|
|
68
|
+
import { schema } from "@rbxts/netweave/macros";
|
|
69
|
+
import { mayEquip } from "shared/policies";
|
|
70
|
+
|
|
71
|
+
// What each channel carries, written as a type. `nw.u8<0, 9>` is a whole number from 0 to 9, sent as one byte.
|
|
72
|
+
export interface Equip {
|
|
73
|
+
slot: nw.u8<0, 9>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface Loadout {
|
|
77
|
+
primary: nw.u16;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// A namespace holds channels. The server and the clients both load this module, so both declare the same ones.
|
|
81
|
+
export const combat = nw.namespace("combat", {
|
|
82
|
+
// A command goes from a client to the server and changes what the server holds, so it needs a rate and a policy.
|
|
83
|
+
equip: nw.command({
|
|
84
|
+
data: schema<Equip>(), // the build turns the type into a schema
|
|
85
|
+
rate: 5, // one player may send it five times a second
|
|
86
|
+
authorize: mayEquip, // who may send it
|
|
87
|
+
}),
|
|
88
|
+
|
|
89
|
+
// An event goes from the server to clients. `owner` sends it only to the player it is published for.
|
|
90
|
+
loadout: nw.event({
|
|
91
|
+
data: schema<Loadout>(),
|
|
92
|
+
audience: nw.audience.owner,
|
|
93
|
+
}),
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The server handles the command in a method, and answers:
|
|
98
|
+
|
|
99
|
+
<!-- docs:excerpt examples/tutorial/src/server/loadouts.ts -->
|
|
100
|
+
```ts
|
|
101
|
+
import nw from "@rbxts/netweave";
|
|
102
|
+
import { listen } from "@rbxts/netweave/macros";
|
|
103
|
+
import { combat, Equip } from "shared/net";
|
|
104
|
+
|
|
105
|
+
export class Loadouts {
|
|
106
|
+
// The slot each player has equipped. Only the handler below writes to it.
|
|
107
|
+
private readonly slots = new Map<Player, number>();
|
|
108
|
+
|
|
109
|
+
// @listen makes this method the handler of `equip`. It starts receiving when the game does `new Loadouts()`.
|
|
110
|
+
@listen(combat.server.equip)
|
|
111
|
+
private equip(ctx: nw.Ctx, chosen: nw.Trusted<Equip>) {
|
|
112
|
+
// `ctx.player` is who sent it. `chosen` is Trusted: it fits the schema, came inside the rate, and the policy allowed it.
|
|
113
|
+
this.slots.set(ctx.player, chosen.slot);
|
|
114
|
+
print(`${ctx.player.Name} equipped slot ${chosen.slot}`);
|
|
115
|
+
|
|
116
|
+
// The answer. `loadout` goes to the owner of what it is published for, so this reaches the sender only.
|
|
117
|
+
combat.server.loadout.publish(ctx.player, { primary: 100 + chosen.slot });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The client sends, and hears the answer:
|
|
123
|
+
|
|
124
|
+
<!-- docs:excerpt examples/tutorial/src/client/main.client.ts -->
|
|
125
|
+
```ts
|
|
126
|
+
import { combat } from "shared/net";
|
|
127
|
+
|
|
128
|
+
// What the server answers an `equip` with arrives here.
|
|
129
|
+
combat.client.loadout.listen(loadout => {
|
|
130
|
+
print(`equipped primary ${loadout.primary}`);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Slot 1 is inside the schema's range and the policy allows it, so the server answers.
|
|
134
|
+
combat.client.equip.send({ slot: 1 });
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The whole project is [`examples/tutorial`](https://github.com/Hawakiki/netweave-ts/tree/main/examples/tutorial) in the repository.
|
|
138
|
+
|
|
139
|
+
## Documentation
|
|
140
|
+
|
|
141
|
+
- [Tutorial](https://github.com/Hawakiki/netweave-ts/blob/main/docs/tutorial/README.md), from an empty folder
|
|
142
|
+
- [Reference](https://github.com/Hawakiki/netweave-ts/blob/main/docs/reference/README.md): the type vocabulary, build errors and warnings,
|
|
143
|
+
settings and versions, known limitations
|
|
144
|
+
- [Coming from Luau](https://github.com/Hawakiki/netweave-ts/blob/main/docs/from-luau.md)
|
|
145
|
+
|
|
146
|
+
Every public name has a comment that the editor shows on hover.
|
|
147
|
+
|
|
148
|
+
## Licence
|
|
149
|
+
|
|
150
|
+
MIT. The Luau under `src` is netweave's, under the same licence.
|