@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/macros.d.ts ADDED
@@ -0,0 +1,87 @@
1
+ import nw from "./index";
2
+
3
+ /*
4
+ The macros of rbxts-transformer-netweave (PLAN-M1 D-3). The transformer replaces each call with the
5
+ code it stands for; a build without it gets macros.luau, which stops the game where it imports one.
6
+ */
7
+
8
+ /**
9
+ * The schema of `T`, written as a TypeScript type instead of with `types.*`: `schema<Equip>()` is the
10
+ * `types.struct({ ... })` that `Equip` describes, and its payload is `Equip` itself. The vocabulary is
11
+ * `nw.u8<0, 9>`, `nw.Str<1, 16>`, `nw.List<T, 3>`, `nw.Default<T, V>` and the rest of the types next to
12
+ * `nw.Schemable`.
13
+ *
14
+ * `Checked` is `T`; it is there because a type parameter cannot be constrained by a type of itself.
15
+ * Pass `T` alone.
16
+ *
17
+ * Needs `{ "transform": "rbxts-transformer-netweave" }` in `compilerOptions.plugins` of tsconfig.json.
18
+ */
19
+ export declare function schema<T extends nw.Schemable<Checked>, Checked = T>(): nw.Type<T>;
20
+
21
+ // Handlers ------------------------------------------------------------------------------------------
22
+
23
+ /*
24
+ The decorators that attach a method to a channel (PLAN-M2). Nothing below them is exported: the
25
+ `export {}` at the end of this file is what keeps a declaration file from exporting every name in it.
26
+ */
27
+
28
+ declare const refused: unique symbol;
29
+
30
+ /* A type no value has, whose only job is to put `M` in the diagnostic: index.d.ts has its own, which cannot be named from here. */
31
+ interface Refused<M extends string> {
32
+ readonly [refused]: M;
33
+ }
34
+
35
+ /* The same rule as `listen` itself: a method that returns a Promise has already returned. */
36
+ type NotAsync<R> = R extends { andThen: any }
37
+ ? Refused<"A handler must not be async. ctx is recycled the moment the handler returns; copy the fields you need and task.spawn the rest.">
38
+ : unknown;
39
+
40
+ type Listens<W> = W extends { listen(handler: never): void }
41
+ ? unknown
42
+ : Refused<"@listen goes on a view that listens: the server view of a command, an intent or a signal, or the client view of a state, an event or a replicate.">;
43
+
44
+ type Handles<W> = W extends { handle(handler: never): void }
45
+ ? unknown
46
+ : Refused<"@handle goes on the server view of a query.">;
47
+
48
+ /* What the view's own `listen` and `handle` take, so that a method is held to exactly what a function passed to them is. */
49
+ type Listened<W> = W extends { listen(handler: (...args: infer A) => unknown): void } ? A : any[];
50
+ type Handled<W> = W extends { handle(handler: (...args: infer A) => infer R): void } ? [args: A, returns: R] : [args: any[], returns: any];
51
+
52
+ /*
53
+ What TypeScript calls a method decorator with, as far as these look at it. The descriptor is spelled
54
+ out and not `TypedPropertyDescriptor`: through that one a method that is async, takes a parameter too
55
+ many, or leaves the `undefined` out of a replicated value is let through (PLAN-M2 Phase 0).
56
+ */
57
+ type Binds<A extends unknown[]> = <R>(target: object, key: string, descriptor: { value: (self: any, ...args: A) => R & NotAsync<R> }) => void;
58
+ type Answers<H extends [args: unknown[], returns: unknown]> = (target: object, key: string, descriptor: { value: (self: any, ...args: H[0]) => H[1] }) => void;
59
+
60
+ /**
61
+ * Makes the method the handler of a channel: `@listen(combat.server.fire)` above a method is
62
+ * `combat.server.fire.listen((ctx, shot) => this.fire(ctx, shot))`, which is what the build writes in its
63
+ * place. The view says which side receives: the server view of a `command`, an `intent` or a `signal`,
64
+ * the client view of a `state`, an `event` or a `replicate`.
65
+ *
66
+ * Write the parameters out, as the function `listen` takes would have them: `(ctx: nw.Ctx, shot:
67
+ * nw.Trusted<Shot>)` for a command, `nw.Untrusted<Shot>` for a signal, the payload alone on a client.
68
+ * TypeScript does not infer them from a decorator; this checks them against the channel.
69
+ *
70
+ * When it attaches: a `static` method when its class is defined, any other at the end of the class's
71
+ * constructor. A channel has one handler and nothing detaches it, so a class with such methods is
72
+ * made once; a second `new` raises. Packets that were waiting are handled as it attaches, which is
73
+ * inside `new`, after the constructor's own body.
74
+ *
75
+ * Needs `"experimentalDecorators": true` in `compilerOptions`, and
76
+ * `{ "transform": "rbxts-transformer-netweave" }` in `compilerOptions.plugins` of tsconfig.json.
77
+ */
78
+ export declare function listen<W>(view: W & Listens<W>): Binds<Listened<W>>;
79
+
80
+ /**
81
+ * Makes the method what answers a query: `@handle(shop.server.price)` above a method is
82
+ * `shop.server.price.handle((ctx, order) => this.price(ctx, order))`. The method takes `(ctx: nw.Ctx,
83
+ * args: nw.Trusted<Args>)` and returns the answer. It attaches when {@link listen} does.
84
+ */
85
+ export declare function handle<W>(view: W & Handles<W>): Answers<Handled<W>>;
86
+
87
+ export {};
package/macros.luau ADDED
@@ -0,0 +1,27 @@
1
+ --!strict
2
+
3
+ --[[
4
+ What `import { schema } from "@rbxts/netweave/macros"` finds when the build did not run
5
+ rbxts-transformer-netweave (PLAN-M1 D-3). The transformer replaces every use of a macro, so with it
6
+ nothing requires this module.
7
+
8
+ Reading anything off it raises, at the line of the game that reads it. roblox-ts reads a named import
9
+ off its module where it imports it, at the top of the file, so the game stops there and not at the
10
+ first call, which may come after other code has run or never come at all. Loading the module does
11
+ not raise: Roblox tells the requirer only that the required module failed, and so would not name
12
+ the game's line.
13
+ ]]
14
+
15
+ local TRANSFORMER = "rbxts-transformer-netweave"
16
+
17
+ local macros = setmetatable({}, {
18
+ __index = function(_: {}, key: any): never
19
+ error(
20
+ `{tostring(key)} from @rbxts/netweave/macros needs {TRANSFORMER}, which did not run on this build: `
21
+ .. `add \{ "transform": "{TRANSFORMER}" } to compilerOptions.plugins in tsconfig.json`,
22
+ 2
23
+ )
24
+ end,
25
+ })
26
+
27
+ return table.freeze(macros)
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
- "name": "@rbxts/netweave",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@rbxts/netweave",
3
+ "version": "0.1.0",
4
+ "description": "netweave for roblox-ts: typed, rate-limited and authorized networking for Roblox, declared once per channel",
5
+ "keywords": [
6
+ "roblox",
7
+ "roblox-ts",
8
+ "rbxts",
9
+ "luau",
10
+ "networking",
11
+ "remote",
12
+ "netweave"
13
+ ],
14
+ "author": "Hawakiki",
15
+ "license": "MIT",
16
+ "homepage": "https://github.com/Hawakiki/netweave-ts/blob/main/packages/netweave/README.md",
17
+ "bugs": "https://github.com/Hawakiki/netweave-ts/issues",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/Hawakiki/netweave-ts.git",
21
+ "directory": "packages/netweave"
22
+ },
23
+ "main": "src/netweave.luau",
24
+ "types": "index.d.ts",
25
+ "files": [
26
+ "src",
27
+ "index.d.ts",
28
+ "macros.luau",
29
+ "macros.d.ts",
30
+ "LICENSE"
31
+ ],
32
+ "publishConfig": {
33
+ "access": "public"
34
+ }
35
+ }
@@ -0,0 +1,156 @@
1
+ --!strict
2
+ --!optimize 2
3
+
4
+ --[=[
5
+ @class Audience
6
+
7
+ Who a server-to-client channel reaches. Guarantee G3: every `state` and `event` declares this,
8
+ and `broadcast` exists only where the audience is everyone.
9
+
10
+ ```lua
11
+ playerState = nw.state({
12
+ data = t.struct({ position = t.vector3 }),
13
+ audience = nw.audience.nearby(120),
14
+ }),
15
+ ```
16
+
17
+ Making the recipient set a *declaration* rather than a call site is the point. Positional data
18
+ leaking to players who cannot see it is how wallhacks are fed, and a call site that sends to
19
+ everyone is invisible in review — a declaration is one reviewable line per channel.
20
+ ]=]
21
+
22
+ --[[
23
+ Evaluation belongs to the transport (`src/transport/Recipients.luau`), which resolves an audience
24
+ per publish and, on a replicated channel, per subject per tick; caching a recipient set per tick
25
+ is still open (`DESIGN-API.md` §11, `PLAN-M5`). What this module fixes is the *shape*: an audience
26
+ carries a `scope` singleton
27
+ that the directional views branch on, which is what makes G3 a type error rather than a
28
+ runtime check nobody runs.
29
+ ]]
30
+
31
+ local Audience = {}
32
+
33
+ --[=[
34
+ @type Scope "everyone" | "subset"
35
+ @within Audience
36
+
37
+ Whether the audience is provably every player. Only `everyone` gets `broadcast`.
38
+ ]=]
39
+ export type Scope = "everyone" | "subset"
40
+
41
+ --[=[
42
+ @interface Audience
43
+ @within Audience
44
+ .scope Scope
45
+ .kind "everyone" | "owner" | "nearby" | "select"
46
+ .radius number? -- `nearby`
47
+ .select ((subject: any) -> { Player })? -- `select`
48
+
49
+ :::caution
50
+ `select` runs per publish, and on a replicated channel per subject per tick. An expensive
51
+ selector is charged to every one of those; nothing caches it yet (`PLAN-M5`).
52
+ :::
53
+ ]=]
54
+ export type Audience<S> = {
55
+ scope: S,
56
+ kind: "everyone" | "owner" | "nearby" | "select",
57
+ radius: number?,
58
+ select: ((subject: any) -> { Player })?,
59
+ }
60
+
61
+ --[=[
62
+ @prop everyone Audience<"everyone">
63
+ @within Audience
64
+
65
+ Every connected player. The only audience that permits `broadcast`.
66
+ ]=]
67
+ Audience.everyone = table.freeze({
68
+ scope = "everyone" :: "everyone",
69
+ kind = "everyone" :: "everyone",
70
+ }) :: Audience<"everyone">
71
+
72
+ --[=[
73
+ @prop owner Audience<"subset">
74
+ @within Audience
75
+
76
+ The player the published subject belongs to, and nobody else. The right default for anything
77
+ that is per-player state — inventory, cooldowns, currency.
78
+ ]=]
79
+ Audience.owner = table.freeze({
80
+ scope = "subset" :: "subset",
81
+ kind = "owner" :: "owner",
82
+ }) :: Audience<"subset">
83
+
84
+ --[=[
85
+ @within Audience
86
+ @function nearby
87
+ @param studs number -- must be positive and finite
88
+ @return Audience<"subset">
89
+
90
+ Players within `studs` of the subject.
91
+
92
+ ```lua
93
+ audience = nw.audience.nearby(120),
94
+ ```
95
+
96
+ :::note
97
+ A radius is a *replication* decision, not a security one on its own: a player who is inside
98
+ the radius still receives the data. It narrows what an attacker can see to what they could
99
+ plausibly see anyway.
100
+ :::
101
+ ]=]
102
+ function Audience.nearby(studs: number): Audience<"subset">
103
+ if type(studs) ~= "number" or studs ~= studs or studs <= 0 or studs == math.huge then
104
+ error(
105
+ `nw.audience.nearby takes a radius in studs, got {tostring(studs)}. Write the number the `
106
+ .. `game actually needs — nw.audience.nearby(120) — because it is also the answer to `
107
+ .. `"how far away can a wallhack read this from".`,
108
+ 2
109
+ )
110
+ end
111
+
112
+ return table.freeze({
113
+ scope = "subset" :: "subset",
114
+ kind = "nearby" :: "nearby",
115
+ radius = studs,
116
+ }) :: Audience<"subset">
117
+ end
118
+
119
+ --[=[
120
+ @within Audience
121
+ @function select
122
+ @param chooser (subject: any) -> { Player }
123
+ @return Audience<"subset">
124
+
125
+ An explicit recipient list, computed per publish. The escape hatch for an audience the built-in
126
+ kinds do not describe — a team, a party, a spectator list.
127
+
128
+ ```lua
129
+ audience = nw.audience.select(function(match)
130
+ return match.spectators
131
+ end),
132
+ ```
133
+
134
+ :::caution
135
+ Returning every player here is not the same as `everyone`: it does not unlock `broadcast`, and
136
+ it should not, because the type system cannot check that the list is complete.
137
+ :::
138
+ ]=]
139
+ function Audience.select(chooser: (subject: any) -> { Player }): Audience<"subset">
140
+ if type(chooser) ~= "function" then
141
+ error(
142
+ "nw.audience.select takes a function from the subject to a list of players, got "
143
+ .. typeof(chooser)
144
+ .. ". Write it as: nw.audience.select(function(subject) return { subject } end).",
145
+ 2
146
+ )
147
+ end
148
+
149
+ return table.freeze({
150
+ scope = "subset" :: "subset",
151
+ kind = "select" :: "select",
152
+ select = chooser,
153
+ }) :: Audience<"subset">
154
+ end
155
+
156
+ return table.freeze(Audience)