arcadible 1.0.32 → 1.0.34
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/dist/action/serve/tool.js +1 -0
- package/dist/action/wire/tool.js +36 -0
- package/dist/api.js +12 -0
- package/dist/command/serve.js +27 -8
- package/dist/command/wire/index.js +14 -0
- package/dist/command/wire/usage.js +43 -0
- package/dist/commands.js +7 -1
- package/dist/phrase/serve.js +4 -0
- package/dist/serve/page.js +172 -16
- package/dist/serve/runner.js +53 -1
- package/dist/serve/server.js +35 -2
- package/dist/serve/ticket.js +33 -5
- package/dist/serve/values.js +17 -12
- package/dist/serve/wires.js +231 -0
- package/dist/wires/room.js +214 -0
- package/npm-shrinkwrap.json +6 -5
- package/package.json +2 -1
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// A wire's rules (plan/SDKWIRES.md): who's on it, the one order its messages
|
|
2
|
+
// go out in, who each is from, and its limits. On Arcadible it's what each
|
|
3
|
+
// wire's Durable Object runs (io-arcadible-wires); under arc serve it's this
|
|
4
|
+
// module itself, as it's built (serve/wires.ts), so a game meets the same
|
|
5
|
+
// wire in both. It holds no connection: whatever runs it says who's
|
|
6
|
+
// connected (`list`), and it says what to send to whom (`deliver`) and whom
|
|
7
|
+
// to drop (`drop`). It imports nothing, so a runtime runs it as it is.
|
|
8
|
+
// [declare]
|
|
9
|
+
/** The protocol a connection speaks, named as it opens. */
|
|
10
|
+
export const PROTOCOL = "arcadible-wire.v1";
|
|
11
|
+
/** What a connection sends to say it's there, and what it's answered. */
|
|
12
|
+
export const PING = "ping";
|
|
13
|
+
export const PONG = "pong";
|
|
14
|
+
/**
|
|
15
|
+
* A wire's limits (LIMITS.md): a message's size as JSON, in bytes; a
|
|
16
|
+
* member's sends a second, and at once; the wire's, every member's
|
|
17
|
+
* together; its members; one member's connections; and how long a
|
|
18
|
+
* connection may go unheard, in milliseconds.
|
|
19
|
+
*/
|
|
20
|
+
export const LIMITS = {
|
|
21
|
+
connections: 8,
|
|
22
|
+
idle: 120_000,
|
|
23
|
+
members: 100,
|
|
24
|
+
message: 1024,
|
|
25
|
+
rate: 20,
|
|
26
|
+
rateBurst: 40,
|
|
27
|
+
wireRate: 200,
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The code a connection is closed with, for each reason: in the range a
|
|
31
|
+
* protocol keeps for itself, each the HTTP status that means the same.
|
|
32
|
+
*/
|
|
33
|
+
export const CLOSED = {
|
|
34
|
+
busy: 4429,
|
|
35
|
+
denied: 4403,
|
|
36
|
+
expired: 4401,
|
|
37
|
+
invalid: 4400,
|
|
38
|
+
too_large: 4413,
|
|
39
|
+
too_many: 4409,
|
|
40
|
+
unavailable: 4503,
|
|
41
|
+
};
|
|
42
|
+
/** A connection that went unheard: closed, and free to come back. */
|
|
43
|
+
export const IDLE = 4408;
|
|
44
|
+
// [function]
|
|
45
|
+
// tokens that refill at `rate` a second, up to `burst`: whether one is
|
|
46
|
+
// there to take, at `at`.
|
|
47
|
+
function bucket(rate, burst) {
|
|
48
|
+
let tokens = burst;
|
|
49
|
+
let last = 0;
|
|
50
|
+
return function take(at) {
|
|
51
|
+
tokens = Math.min(burst, tokens + ((at - last) / 1000) * rate);
|
|
52
|
+
last = at;
|
|
53
|
+
if (tokens < 1)
|
|
54
|
+
return false;
|
|
55
|
+
tokens -= 1;
|
|
56
|
+
return true;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// [function]
|
|
60
|
+
// a string's size in bytes, as UTF-8.
|
|
61
|
+
function size(text) {
|
|
62
|
+
let bytes = 0;
|
|
63
|
+
for (let at = 0; at < text.length; at += 1) {
|
|
64
|
+
const code = text.charCodeAt(at);
|
|
65
|
+
if (code < 0x80)
|
|
66
|
+
bytes += 1;
|
|
67
|
+
else if (code < 0x800)
|
|
68
|
+
bytes += 2;
|
|
69
|
+
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
70
|
+
bytes += 4;
|
|
71
|
+
at += 1;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
bytes += 3;
|
|
75
|
+
}
|
|
76
|
+
return bytes;
|
|
77
|
+
}
|
|
78
|
+
// [function]
|
|
79
|
+
/** A member as a frame carries one: only what a member is. */
|
|
80
|
+
export function memberOf(value) {
|
|
81
|
+
if (typeof value !== "object" || value === null)
|
|
82
|
+
return null;
|
|
83
|
+
const { guest, id, name } = value;
|
|
84
|
+
if (typeof id !== "string" || typeof guest !== "boolean")
|
|
85
|
+
return null;
|
|
86
|
+
return { guest, id, name: typeof name === "string" && !guest ? name : null };
|
|
87
|
+
}
|
|
88
|
+
// [function]
|
|
89
|
+
/**
|
|
90
|
+
* A wire. Frames are JSON text, one thing each: to a connection,
|
|
91
|
+
* `{ welcome: { you, members } }` as it comes on, then
|
|
92
|
+
* `{ message: { data, from, at } }`, `{ join: member }`, `{ leave: member }`,
|
|
93
|
+
* and `{ refused: code }` for a send that wasn't taken; from one,
|
|
94
|
+
* `{ send: data }`. A member is a player, however many connections they
|
|
95
|
+
* have: the rest hear `join` at their first and `leave` at their last.
|
|
96
|
+
*/
|
|
97
|
+
export function createRoom(options) {
|
|
98
|
+
const now = options.now ?? (() => Date.now());
|
|
99
|
+
// each member's sends, and the wire's, counted here: a wire that's put to
|
|
100
|
+
// sleep and woken starts them full again, which lets no one send faster
|
|
101
|
+
// for long
|
|
102
|
+
const sends = new Map();
|
|
103
|
+
const wire = bucket(LIMITS.wireRate, LIMITS.wireRate);
|
|
104
|
+
// who's on: each member once, in the order they came
|
|
105
|
+
const members = function () {
|
|
106
|
+
const seen = new Map();
|
|
107
|
+
for (const each of options.list()) {
|
|
108
|
+
if (!seen.has(each.member.id))
|
|
109
|
+
seen.set(each.member.id, each.member);
|
|
110
|
+
}
|
|
111
|
+
return [...seen.values()];
|
|
112
|
+
};
|
|
113
|
+
const all = function (frame, but) {
|
|
114
|
+
const text = JSON.stringify(frame);
|
|
115
|
+
for (const each of options.list()) {
|
|
116
|
+
if (each.key !== but)
|
|
117
|
+
options.deliver(each.key, text);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
// one message, to everyone on the wire, the sender included, stamped
|
|
121
|
+
// here: the order they're sent in is the wire's order
|
|
122
|
+
const message = function (data, from) {
|
|
123
|
+
all({ message: { at: new Date(now()).toISOString(), data, from } });
|
|
124
|
+
options.passed?.();
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
admit(member) {
|
|
128
|
+
const on = options.list();
|
|
129
|
+
const theirs = on.filter((each) => each.member.id === member.id).length;
|
|
130
|
+
if (theirs >= LIMITS.connections)
|
|
131
|
+
return "too_many";
|
|
132
|
+
if (theirs === 0 && members().length >= LIMITS.members)
|
|
133
|
+
return "too_many";
|
|
134
|
+
return null;
|
|
135
|
+
},
|
|
136
|
+
joined(key) {
|
|
137
|
+
const on = options.list();
|
|
138
|
+
const mine = on.find((each) => each.key === key);
|
|
139
|
+
if (!mine)
|
|
140
|
+
return;
|
|
141
|
+
options.deliver(key, JSON.stringify({ welcome: { members: members(), you: mine.member } }));
|
|
142
|
+
// their first connection: the rest hear they came
|
|
143
|
+
const first = !on.some((each) => each.key !== key && each.member.id === mine.member.id);
|
|
144
|
+
if (first)
|
|
145
|
+
all({ join: mine.member }, key);
|
|
146
|
+
},
|
|
147
|
+
left(member) {
|
|
148
|
+
// their last connection: the rest hear they went
|
|
149
|
+
const still = options.list().some((each) => each.member.id === member.id);
|
|
150
|
+
if (!still) {
|
|
151
|
+
sends.delete(member.id);
|
|
152
|
+
all({ leave: member });
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
said(key, text) {
|
|
156
|
+
const mine = options.list().find((each) => each.key === key);
|
|
157
|
+
if (!mine)
|
|
158
|
+
return;
|
|
159
|
+
const refuse = (code) => options.deliver(key, JSON.stringify({ refused: code }));
|
|
160
|
+
// a frame is `{"send":<data>}`: its data's size, as JSON, is the
|
|
161
|
+
// frame's less what's around it
|
|
162
|
+
if (size(text) - '{"send":}'.length > LIMITS.message) {
|
|
163
|
+
refuse("too_large");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
let frame;
|
|
167
|
+
try {
|
|
168
|
+
frame = JSON.parse(text);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
frame = null;
|
|
172
|
+
}
|
|
173
|
+
if (typeof frame !== "object" ||
|
|
174
|
+
frame === null ||
|
|
175
|
+
!Object.hasOwn(frame, "send")) {
|
|
176
|
+
refuse("invalid");
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const at = now();
|
|
180
|
+
let take = sends.get(mine.member.id);
|
|
181
|
+
if (!take) {
|
|
182
|
+
take = bucket(LIMITS.rate, LIMITS.rateBurst);
|
|
183
|
+
sends.set(mine.member.id, take);
|
|
184
|
+
}
|
|
185
|
+
// past a member's rate, or the wire's, a message is dropped, and its
|
|
186
|
+
// sender told
|
|
187
|
+
if (!take(at) || !wire(at)) {
|
|
188
|
+
refuse("busy");
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
message(frame.send, mine.member);
|
|
192
|
+
},
|
|
193
|
+
server(data) {
|
|
194
|
+
if (data === undefined)
|
|
195
|
+
return false;
|
|
196
|
+
// the server's word is never dropped for the wire's rate: what it
|
|
197
|
+
// sends is counted where it's sent from
|
|
198
|
+
message(data, null);
|
|
199
|
+
return true;
|
|
200
|
+
},
|
|
201
|
+
sweep() {
|
|
202
|
+
const at = now();
|
|
203
|
+
let stay = 0;
|
|
204
|
+
// as it stood: dropping one changes who's listed
|
|
205
|
+
for (const each of [...options.list()]) {
|
|
206
|
+
if (at - each.heard > LIMITS.idle)
|
|
207
|
+
options.drop(each.key, IDLE, "idle");
|
|
208
|
+
else
|
|
209
|
+
stay += 1;
|
|
210
|
+
}
|
|
211
|
+
return stay;
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"qrcode-terminal": "0.12.0",
|
|
29
29
|
"range-parser": "1.3.0",
|
|
30
30
|
"semver": "7.8.6",
|
|
31
|
+
"ws": "8.23.0",
|
|
31
32
|
"yargs": "17.7.3",
|
|
32
33
|
"zod": "4.6.7"
|
|
33
34
|
},
|
|
@@ -43,7 +44,7 @@
|
|
|
43
44
|
},
|
|
44
45
|
"license": "Apache-2.0",
|
|
45
46
|
"name": "arcadible",
|
|
46
|
-
"version": "1.0.
|
|
47
|
+
"version": "1.0.34"
|
|
47
48
|
},
|
|
48
49
|
"node_modules/@asamuzakjp/css-color": {
|
|
49
50
|
"dependencies": {
|
|
@@ -3147,7 +3148,7 @@
|
|
|
3147
3148
|
"engines": {
|
|
3148
3149
|
"node": ">=10.0.0"
|
|
3149
3150
|
},
|
|
3150
|
-
"integrity": "sha512
|
|
3151
|
+
"integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==",
|
|
3151
3152
|
"license": "MIT",
|
|
3152
3153
|
"peerDependencies": {
|
|
3153
3154
|
"bufferutil": "^4.0.1",
|
|
@@ -3161,8 +3162,8 @@
|
|
|
3161
3162
|
"optional": true
|
|
3162
3163
|
}
|
|
3163
3164
|
},
|
|
3164
|
-
"resolved": "https://registry.npmjs.org/ws/-/ws-8.
|
|
3165
|
-
"version": "8.
|
|
3165
|
+
"resolved": "https://registry.npmjs.org/ws/-/ws-8.23.0.tgz",
|
|
3166
|
+
"version": "8.23.0"
|
|
3166
3167
|
},
|
|
3167
3168
|
"node_modules/wsl-utils": {
|
|
3168
3169
|
"dependencies": {
|
|
@@ -3266,5 +3267,5 @@
|
|
|
3266
3267
|
}
|
|
3267
3268
|
},
|
|
3268
3269
|
"requires": true,
|
|
3269
|
-
"version": "1.0.
|
|
3270
|
+
"version": "1.0.34"
|
|
3270
3271
|
}
|
package/package.json
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"qrcode-terminal": "0.12.0",
|
|
26
26
|
"range-parser": "1.3.0",
|
|
27
27
|
"semver": "7.8.6",
|
|
28
|
+
"ws": "8.23.0",
|
|
28
29
|
"yargs": "17.7.3",
|
|
29
30
|
"zod": "4.6.7"
|
|
30
31
|
},
|
|
@@ -67,5 +68,5 @@
|
|
|
67
68
|
"test": "tsc && npm run lint && node --test \"test/**/*.test.js\" \"test/**/*.test.mjs\""
|
|
68
69
|
},
|
|
69
70
|
"type": "module",
|
|
70
|
-
"version": "1.0.
|
|
71
|
+
"version": "1.0.34"
|
|
71
72
|
}
|