@arcadible/server-sdk 0.0.0-stage → 0.5.0-next.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 +176 -0
- package/README.md +120 -2
- package/dist/server/api.d.ts +11 -0
- package/dist/server/api.js +75 -0
- package/dist/server/index.d.ts +27 -0
- package/dist/server/index.js +35 -0
- package/dist/server/issuer.d.ts +17 -0
- package/dist/server/issuer.js +46 -0
- package/dist/server/keys.d.ts +11 -0
- package/dist/server/keys.js +87 -0
- package/dist/server/players.d.ts +33 -0
- package/dist/server/players.js +33 -0
- package/dist/server/tickets.d.ts +59 -0
- package/dist/server/tickets.js +178 -0
- package/dist/shared/api.d.ts +16 -0
- package/dist/shared/api.js +75 -0
- package/dist/shared/error.d.ts +17 -0
- package/dist/shared/error.js +38 -0
- package/dist/shared/players.d.ts +23 -0
- package/dist/shared/players.js +69 -0
- package/dist/shared/values.d.ts +100 -0
- package/dist/shared/values.js +208 -0
- package/package.json +30 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -1,3 +1,121 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://arcadible.com">
|
|
3
|
+
<img src="https://adn-arcadible.imgix.net/images/i18n/core/www/appicon-512.png?w=256" alt="Arcadible" height="96">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
<h1 align="center">Arcadible Server SDK</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
How your own server works with Arcadible.
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<br>
|
|
14
|
+
|
|
15
|
+
A game on [Arcadible](https://arcadible.com) can have a server of its own. The Arcadible Server SDK is for that server: it checks the tickets your game sends, so the server knows a request comes from your game and who's playing, and it asks Arcadible about your players, with your game's key.
|
|
16
|
+
|
|
17
|
+
**A prerelease.** It may change before it's released.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @arcadible/server-sdk@next
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
It runs wherever `fetch` and Web Crypto do: Node 22 and later, Cloudflare Workers, Deno, and Bun. On Cloudflare Workers, turn on the `nodejs_compat` flag, with a compatibility date from 2025-04-01, so your Worker's `ARCADIBLE_KEY` and `ARCADIBLE_SERVE` reach it; checking a ticket needs neither. It's for a server of your own; a game uses [`@arcadible/sdk`](https://www.npmjs.com/package/@arcadible/sdk), and a game's function, which Arcadible runs, [`@arcadible/function-sdk`](https://www.npmjs.com/package/@arcadible/function-sdk).
|
|
26
|
+
|
|
27
|
+
## Tickets
|
|
28
|
+
|
|
29
|
+
Your game asks Arcadible for a ticket and sends it once, as it connects. Your server checks it, then keeps its own session.
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { tickets } from "@arcadible/server-sdk";
|
|
33
|
+
|
|
34
|
+
const ticket = await tickets.check(bearer, {
|
|
35
|
+
server: "https://api.example.com",
|
|
36
|
+
game: "8f36cdfb-adad-4f37-b543-63cf1b401455",
|
|
37
|
+
channel: "production",
|
|
38
|
+
});
|
|
39
|
+
// { id, game, site, version, channel, player, expiresAt }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- **`server`** is your server's own address, as your game's `arcadible.toml` lists it. A ticket for another server is refused.
|
|
43
|
+
- **`game`** is your game's id. A ticket for another game is refused.
|
|
44
|
+
- **`channel`** is the channel your server takes tickets from: `"production"`, `"staging"`, or both as a list. A production server passes `"production"`, so it never takes a staging build's tickets.
|
|
45
|
+
- **`player`** is the player, as `{ id }`: their id in your game, the same every time. It's `null` for a guest. Pass it as it is to `players.read` and to a value's `scope`.
|
|
46
|
+
- **`id`** is unique to the ticket. Keep it until `expiresAt` to refuse a ticket sent twice.
|
|
47
|
+
|
|
48
|
+
It rejects with an `ArcadibleError`, whose `code` says what to do:
|
|
49
|
+
|
|
50
|
+
| Code | What happened | Do |
|
|
51
|
+
| ------------- | ----------------------------------------------------------- | ----------------------------- |
|
|
52
|
+
| `invalid` | Not a ticket, or not one for this server, game, and channel | Refuse the request |
|
|
53
|
+
| `expired` | It was good, and isn't any more | Ask the game for a new ticket |
|
|
54
|
+
| `unavailable` | Arcadible's keys couldn't be read | Try again |
|
|
55
|
+
|
|
56
|
+
## Players
|
|
57
|
+
|
|
58
|
+
A ticket gives your server a player, by their id in your game. With one of your game's keys, it can ask Arcadible for their name, and who has deleted their account.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { players } from "@arcadible/server-sdk";
|
|
62
|
+
|
|
63
|
+
const [sam, kim] = await players.read([ticket.player, { id: stored }]);
|
|
64
|
+
// { id, name }, or null for a player who's gone, or a guest
|
|
65
|
+
|
|
66
|
+
const gone = await players.deleted({ since: lastChecked });
|
|
67
|
+
// [{ id, deletedAt }]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- **The key** is read from `ARCADIBLE_KEY` in your server's environment, never passed in code. Make one in Arcadible Studio, or with `arc server key create`. It's for one game and one channel, and it's shown once.
|
|
71
|
+
- **A player is an object,** in every call: `players.read` takes up to 50, each anything with a player's `id`, such as a ticket's `player`, an entry of `players.deleted`, or a `{ id }` your server kept. An id alone isn't taken. A missing player, `null`, as a guest's ticket has, comes back `null`.
|
|
72
|
+
- **A player may gain properties.** Arcadible may add to a player later, and every call that takes one reads only its `id`. Don't depend on a player's exact shape, such as by comparing whole objects or expecting the same properties back: compare and keep the `id`.
|
|
73
|
+
- **A name** is the player's own, as they've set it on Arcadible; show it as text.
|
|
74
|
+
- **`players.deleted`** lists who deleted their account in the last 30 days. Read it on a schedule, and delete what your server keeps for each.
|
|
75
|
+
|
|
76
|
+
Both reject with an `ArcadibleError`: `invalid` (no key, one Arcadible doesn't take, or an argument that isn't a list of players or a time), `too_many` (more than 50 players), `busy` (too many requests: slow down), or `unavailable` (try again).
|
|
77
|
+
|
|
78
|
+
## Values
|
|
79
|
+
|
|
80
|
+
Your game's data, kept on Arcadible: JSON under a key, in a scope. With your game's key, your server reads the developer's values, and reads and writes the game's (which every player reads), its own (`"server"`, which no player reads), and any player's.
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import { values } from "@arcadible/server-sdk";
|
|
84
|
+
|
|
85
|
+
// one call across scopes, one snapshot
|
|
86
|
+
const [board, seats] = await values.read([
|
|
87
|
+
{ scope: "game", key: "board" },
|
|
88
|
+
{ scope: "server", key: "seats" },
|
|
89
|
+
]);
|
|
90
|
+
// each { scope, key, value, revision, updatedAt }, or null
|
|
91
|
+
|
|
92
|
+
// written all or none, each only if it's still the one read
|
|
93
|
+
await values.write([
|
|
94
|
+
{ scope: "game", key: "board", value: next, revision: board?.revision ?? 0 },
|
|
95
|
+
{ scope: "server", key: "seats", value: taken, revision: seats?.revision ?? 0 },
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
// a logged-in player's own
|
|
99
|
+
await values.write([{ scope: ticket.player, key: "progress", value: { level: 3 } }]);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- **`read`, `write`, and `remove`** take items, each naming its scope and key, up to 32, as one request across scopes: a read is one snapshot, and a write or a remove is all made or none. With no items, nothing is asked.
|
|
103
|
+
- **A `revision`** makes a write, or a remove, only if the value is still that one (`0`: only if there's none); otherwise the whole call rejects with `conflict`, and your server reads again.
|
|
104
|
+
- **`since`** on a read item, a revision you have: if the value is still at it, it comes back `{ scope, key, revision, unchanged: true }`, without its value.
|
|
105
|
+
- **`list`** gives a scope's keys, with their sizes and times and no values, 50 at a time.
|
|
106
|
+
|
|
107
|
+
They reject with an `ArcadibleError`: `invalid`, `denied` (not allowed to a server), `conflict`, `too_large`, `too_many`, `full` (the scope's storage is used up), `busy`, or `unavailable`.
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
`arc serve` makes tickets on your own machine, and prints the variable that points your server at it:
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
ARCADIBLE_SERVE=http://localhost:4100 node server.js
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
With it set, your server takes `arc serve`'s tickets (their channel is `local`), and none of Arcadible's, and `players` and `values` ask `arc serve`, with no key. It differs from Arcadible in a few ways: `players.read` knows only the players its tickets named in this run, `players.deleted` is always empty, and the limits on how often you call aren't enforced. Set it only in development: it must name your own machine, and any other value makes every check fail.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Api } from "../shared/api.js";
|
|
2
|
+
export declare const KEY = "ARCADIBLE_KEY";
|
|
3
|
+
export type ApiOptions = {
|
|
4
|
+
env?: Record<string, string | undefined>;
|
|
5
|
+
fetch?: (url: string, init: RequestInit) => Promise<Response>;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The server API for one process. Its issuer is read from the environment
|
|
9
|
+
* as it's made, at a server's first call, and its key at each request.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createApi(options?: ApiOptions): Api;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Arcadible's server API, for a server of the developer's own: one request,
|
|
2
|
+
// with the game's key from the process's environment, answered with its
|
|
3
|
+
// data or a code. Against `arc serve` there's none: its API answers this
|
|
4
|
+
// machine alone.
|
|
5
|
+
// [import]
|
|
6
|
+
import { dataOf, initOf, TIMEOUT, unreachable, } from "../shared/api.js";
|
|
7
|
+
import { ArcadibleError, Codes } from "../shared/error.js";
|
|
8
|
+
import { issuerOf } from "./issuer.js";
|
|
9
|
+
// [declare]
|
|
10
|
+
// The variable a server keeps its key in. It's the environment's and never
|
|
11
|
+
// an argument, so a key is never in code.
|
|
12
|
+
export const KEY = "ARCADIBLE_KEY";
|
|
13
|
+
const KEY_SHAPE = /^arcadible_key_[ps]_[0-9A-Za-z]{43}$/u;
|
|
14
|
+
// What a Worker needs for its variables to reach the SDK: Cloudflare puts a
|
|
15
|
+
// Worker's own in `process.env` only with `nodejs_compat`, from this
|
|
16
|
+
// compatibility date on.
|
|
17
|
+
const WORKERS = "On Cloudflare Workers, turn on the nodejs_compat flag, with a compatibility date from 2025-04-01, so ARCADIBLE_KEY reaches the SDK.";
|
|
18
|
+
// [function]
|
|
19
|
+
// whether the code runs on Cloudflare Workers, which say so.
|
|
20
|
+
function onWorkers() {
|
|
21
|
+
return (globalThis.navigator
|
|
22
|
+
?.userAgent === "Cloudflare-Workers");
|
|
23
|
+
}
|
|
24
|
+
// [function]
|
|
25
|
+
// the key a request carries: the environment's. Without one, or with what
|
|
26
|
+
// isn't a key, nothing is sent. With no environment at all, as on a Worker
|
|
27
|
+
// without nodejs_compat, it says how to give it one.
|
|
28
|
+
function keyOf(env) {
|
|
29
|
+
if (env === null) {
|
|
30
|
+
throw new ArcadibleError(Codes.Invalid, `No environment to read ${KEY} from. ${WORKERS}`);
|
|
31
|
+
}
|
|
32
|
+
const key = env[KEY];
|
|
33
|
+
if (key === undefined || key === "") {
|
|
34
|
+
throw new ArcadibleError(Codes.Invalid, `No key: set ${KEY} to one of the game's keys, from Arcadible Studio or \`arc server key create\`.${onWorkers() ? ` ${WORKERS}` : ""}`);
|
|
35
|
+
}
|
|
36
|
+
if (!KEY_SHAPE.test(key)) {
|
|
37
|
+
throw new ArcadibleError(Codes.Invalid, `${KEY} isn't a key.`);
|
|
38
|
+
}
|
|
39
|
+
return key;
|
|
40
|
+
}
|
|
41
|
+
// [function]
|
|
42
|
+
/**
|
|
43
|
+
* The server API for one process. Its issuer is read from the environment
|
|
44
|
+
* as it's made, at a server's first call, and its key at each request.
|
|
45
|
+
*/
|
|
46
|
+
export function createApi(options = {}) {
|
|
47
|
+
// null with no environment at all, as on a Worker without nodejs_compat
|
|
48
|
+
const env = options.env ??
|
|
49
|
+
globalThis
|
|
50
|
+
.process?.env ??
|
|
51
|
+
null;
|
|
52
|
+
const send = options.fetch ?? ((url, init) => fetch(url, init));
|
|
53
|
+
const issuer = issuerOf(env ?? {});
|
|
54
|
+
return {
|
|
55
|
+
async request(path, body) {
|
|
56
|
+
const key = issuer.local ? null : keyOf(env);
|
|
57
|
+
const init = initOf(body);
|
|
58
|
+
let res;
|
|
59
|
+
try {
|
|
60
|
+
res = await send(`${issuer.api}${path}`, {
|
|
61
|
+
...init,
|
|
62
|
+
headers: {
|
|
63
|
+
...init.headers,
|
|
64
|
+
...(key === null ? {} : { authorization: `Bearer ${key}` }),
|
|
65
|
+
},
|
|
66
|
+
signal: AbortSignal.timeout(TIMEOUT),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw unreachable();
|
|
71
|
+
}
|
|
72
|
+
return dataOf(res, `Arcadible didn't take the key in ${KEY}: it's revoked, or it isn't one.`);
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Values } from "../shared/values.js";
|
|
2
|
+
import { type Players } from "./players.js";
|
|
3
|
+
import { type Tickets } from "./tickets.js";
|
|
4
|
+
/**
|
|
5
|
+
* Tickets a game sends this server. Made at its first use, when the
|
|
6
|
+
* process's environment is read: `ARCADIBLE_SERVE`, for development only.
|
|
7
|
+
* With a value that isn't allowed, every check rejects, saying so.
|
|
8
|
+
*/
|
|
9
|
+
export declare const tickets: Tickets;
|
|
10
|
+
/**
|
|
11
|
+
* The game's players, as its tickets name them: their names, and who
|
|
12
|
+
* deleted their account, asked of Arcadible with the game's key, from
|
|
13
|
+
* `ARCADIBLE_KEY` in the process's environment.
|
|
14
|
+
*/
|
|
15
|
+
export declare const players: Players;
|
|
16
|
+
/**
|
|
17
|
+
* The game's values, kept on Arcadible: JSON under a key, in a scope. A
|
|
18
|
+
* server reads the developer's, and reads and writes the game's, its own,
|
|
19
|
+
* and any player's of the game.
|
|
20
|
+
*/
|
|
21
|
+
export declare const values: Values;
|
|
22
|
+
export { ArcadibleError, Codes } from "../shared/error.js";
|
|
23
|
+
export type { Code } from "../shared/error.js";
|
|
24
|
+
export type { Player } from "../shared/players.js";
|
|
25
|
+
export type { Entry, Listed, ListOptions, ReadItem, RemoveItem, Scope, Unchanged, Value, WritableScope, WriteItem, Written, } from "../shared/values.js";
|
|
26
|
+
export type { DeletedPlayer } from "./players.js";
|
|
27
|
+
export type { Channel, CheckOptions, Ticket } from "./tickets.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// `@arcadible/server-sdk`: what a server of the developer's own uses with
|
|
2
|
+
// Arcadible. It runs wherever `fetch` and Web Crypto do: Node 22 and later,
|
|
3
|
+
// Workers, Deno, and Bun. A game's function uses `@arcadible/function-sdk`.
|
|
4
|
+
import { createValues } from "../shared/values.js";
|
|
5
|
+
import { createApi } from "./api.js";
|
|
6
|
+
import { createPlayers } from "./players.js";
|
|
7
|
+
import { createTickets } from "./tickets.js";
|
|
8
|
+
// [declare]
|
|
9
|
+
let api = null;
|
|
10
|
+
let made = null;
|
|
11
|
+
// [export]
|
|
12
|
+
/**
|
|
13
|
+
* Tickets a game sends this server. Made at its first use, when the
|
|
14
|
+
* process's environment is read: `ARCADIBLE_SERVE`, for development only.
|
|
15
|
+
* With a value that isn't allowed, every check rejects, saying so.
|
|
16
|
+
*/
|
|
17
|
+
export const tickets = {
|
|
18
|
+
async check(ticket, options) {
|
|
19
|
+
made ??= createTickets();
|
|
20
|
+
return made.check(ticket, options);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The game's players, as its tickets name them: their names, and who
|
|
25
|
+
* deleted their account, asked of Arcadible with the game's key, from
|
|
26
|
+
* `ARCADIBLE_KEY` in the process's environment.
|
|
27
|
+
*/
|
|
28
|
+
export const players = createPlayers(() => (api ??= createApi()));
|
|
29
|
+
/**
|
|
30
|
+
* The game's values, kept on Arcadible: JSON under a key, in a scope. A
|
|
31
|
+
* server reads the developer's, and reads and writes the game's, its own,
|
|
32
|
+
* and any player's of the game.
|
|
33
|
+
*/
|
|
34
|
+
export const values = createValues(() => (api ??= createApi()));
|
|
35
|
+
export { ArcadibleError, Codes } from "../shared/error.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Where tickets come from: who signs them, their keys, and their channels. */
|
|
2
|
+
export type Issuer = {
|
|
3
|
+
name: string;
|
|
4
|
+
keys: string;
|
|
5
|
+
channels: readonly string[];
|
|
6
|
+
api: string;
|
|
7
|
+
local: boolean;
|
|
8
|
+
};
|
|
9
|
+
export declare const ARCADIBLE: Issuer;
|
|
10
|
+
export declare const SERVE = "ARCADIBLE_SERVE";
|
|
11
|
+
/**
|
|
12
|
+
* The issuer a process's environment names: Arcadible, or, with
|
|
13
|
+
* `ARCADIBLE_SERVE` set, the `arc serve` at that address, which must be on
|
|
14
|
+
* this machine. Any other value throws, so a server misconfigured in
|
|
15
|
+
* production stops, and never takes tickets from somewhere else.
|
|
16
|
+
*/
|
|
17
|
+
export declare function issuerOf(env: Record<string, string | undefined>): Issuer;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Who signs the tickets a server takes: Arcadible, unless the server's
|
|
2
|
+
// process was started for development, with `ARCADIBLE_SERVE` naming an
|
|
3
|
+
// `arc serve` on the developer's own machine. It's the environment's to say
|
|
4
|
+
// and never a ticket's or a call's, so no request can talk a server into
|
|
5
|
+
// trusting another signer.
|
|
6
|
+
export const ARCADIBLE = {
|
|
7
|
+
api: "https://api.arcadible.com/v1/server",
|
|
8
|
+
channels: ["production", "staging"],
|
|
9
|
+
keys: "https://arcadible.com/.well-known/jwks.json",
|
|
10
|
+
local: false,
|
|
11
|
+
name: "https://arcadible.com",
|
|
12
|
+
};
|
|
13
|
+
// The variable `arc serve` prints, and the hosts that are the developer's
|
|
14
|
+
// own machine.
|
|
15
|
+
export const SERVE = "ARCADIBLE_SERVE";
|
|
16
|
+
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
17
|
+
// [function]
|
|
18
|
+
/**
|
|
19
|
+
* The issuer a process's environment names: Arcadible, or, with
|
|
20
|
+
* `ARCADIBLE_SERVE` set, the `arc serve` at that address, which must be on
|
|
21
|
+
* this machine. Any other value throws, so a server misconfigured in
|
|
22
|
+
* production stops, and never takes tickets from somewhere else.
|
|
23
|
+
*/
|
|
24
|
+
export function issuerOf(env) {
|
|
25
|
+
const serve = env[SERVE];
|
|
26
|
+
if (serve === undefined || serve === "")
|
|
27
|
+
return ARCADIBLE;
|
|
28
|
+
let url = null;
|
|
29
|
+
try {
|
|
30
|
+
url = new URL(serve);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
url = null;
|
|
34
|
+
}
|
|
35
|
+
const web = url?.protocol === "http:" || url?.protocol === "https:";
|
|
36
|
+
if (!url || !web || !LOOPBACK.has(url.hostname)) {
|
|
37
|
+
throw new Error(`${SERVE} must be an arc serve on this machine, such as http://localhost:4100. Unset it everywhere but development.`);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
api: `${url.origin}/_arcadible/v1/server`,
|
|
41
|
+
channels: ["local"],
|
|
42
|
+
keys: `${url.origin}/.well-known/jwks.json`,
|
|
43
|
+
local: true,
|
|
44
|
+
name: url.origin,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type KeysOptions = {
|
|
2
|
+
url: string;
|
|
3
|
+
fetch: (url: string, init: {
|
|
4
|
+
signal: AbortSignal;
|
|
5
|
+
}) => Promise<Response>;
|
|
6
|
+
now: () => number;
|
|
7
|
+
};
|
|
8
|
+
export type Keys = {
|
|
9
|
+
find(kid: string): Promise<CryptoKey | null>;
|
|
10
|
+
};
|
|
11
|
+
export declare function createKeys(options: KeysOptions): Keys;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// An issuer's published keys, fetched and kept: a ticket names its key by
|
|
2
|
+
// id, and a server checks the signature with the public half.
|
|
3
|
+
// [import]
|
|
4
|
+
import { ArcadibleError, Codes } from "../shared/error.js";
|
|
5
|
+
// [declare]
|
|
6
|
+
// Keys are kept this long before they're read again, and an id that isn't
|
|
7
|
+
// among them is looked for at most this often, so a flood of tickets with
|
|
8
|
+
// made-up ids costs one fetch a minute.
|
|
9
|
+
const KEPT = 60 * 60 * 1000;
|
|
10
|
+
const RETRY = 60 * 1000;
|
|
11
|
+
const TIMEOUT = 10 * 1000;
|
|
12
|
+
// [function]
|
|
13
|
+
function isObject(value) {
|
|
14
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
// [function]
|
|
17
|
+
// the published keys that sign tickets, by id: Ed25519 ones only.
|
|
18
|
+
async function parse(body) {
|
|
19
|
+
const keys = new Map();
|
|
20
|
+
const list = isObject(body) && Array.isArray(body["keys"]) ? body["keys"] : [];
|
|
21
|
+
for (const entry of list) {
|
|
22
|
+
if (isObject(entry) &&
|
|
23
|
+
entry.kty === "OKP" &&
|
|
24
|
+
entry.crv === "Ed25519" &&
|
|
25
|
+
typeof entry.kid === "string" &&
|
|
26
|
+
typeof entry.x === "string") {
|
|
27
|
+
try {
|
|
28
|
+
keys.set(entry.kid, await crypto.subtle.importKey("jwk", { crv: "Ed25519", kty: "OKP", x: entry.x }, { name: "Ed25519" }, false, ["verify"]));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// a key that isn't one is left out; the rest still check tickets.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return keys;
|
|
36
|
+
}
|
|
37
|
+
// [function]
|
|
38
|
+
export function createKeys(options) {
|
|
39
|
+
let kept = null;
|
|
40
|
+
let readAt = -Infinity;
|
|
41
|
+
let reading = null;
|
|
42
|
+
const read = function () {
|
|
43
|
+
reading ??= (async () => {
|
|
44
|
+
const at = options.now();
|
|
45
|
+
try {
|
|
46
|
+
const res = await options.fetch(options.url, {
|
|
47
|
+
signal: AbortSignal.timeout(TIMEOUT),
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok)
|
|
50
|
+
throw new Error(`The keys answered ${res.status}.`);
|
|
51
|
+
const keys = await parse(await res.json());
|
|
52
|
+
if (keys.size === 0)
|
|
53
|
+
throw new Error("The keys hold none.");
|
|
54
|
+
kept = keys;
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
// a failed read counts too, so an outage isn't asked every time.
|
|
58
|
+
readAt = at;
|
|
59
|
+
reading = null;
|
|
60
|
+
}
|
|
61
|
+
})();
|
|
62
|
+
return reading;
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
async find(kid) {
|
|
66
|
+
const age = options.now() - readAt;
|
|
67
|
+
const known = kept?.get(kid) ?? null;
|
|
68
|
+
// read when there are none, when they're old, or when a ticket names
|
|
69
|
+
// a key that's new since the last read (a rotation), not too often.
|
|
70
|
+
const due = kept === null ? age >= RETRY : age >= KEPT;
|
|
71
|
+
if (due || (kept !== null && known === null && age >= RETRY)) {
|
|
72
|
+
try {
|
|
73
|
+
await read();
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// the keys held still check tickets through an outage.
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// with none, nothing can be checked: not the ticket's fault.
|
|
80
|
+
const keys = kept;
|
|
81
|
+
if (keys === null) {
|
|
82
|
+
throw new ArcadibleError(Codes.Unavailable, "The keys couldn't be read to check the ticket. Try again.");
|
|
83
|
+
}
|
|
84
|
+
return keys.get(kid) ?? null;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Api } from "../shared/api.js";
|
|
2
|
+
import { type Player } from "../shared/players.js";
|
|
3
|
+
/** A player who deleted their account. */
|
|
4
|
+
export type DeletedPlayer = {
|
|
5
|
+
/** Their id in this game. */
|
|
6
|
+
id: string;
|
|
7
|
+
/** When they deleted their account, as an ISO time. */
|
|
8
|
+
deletedAt: string;
|
|
9
|
+
};
|
|
10
|
+
export interface Players {
|
|
11
|
+
/**
|
|
12
|
+
* Each player, in the order given: their id and name, or `null` for one
|
|
13
|
+
* who's gone, never was, or is a guest. Up to 50 players, each anything
|
|
14
|
+
* with a player's `id`: a ticket's `player`, a `deleted` entry, or a
|
|
15
|
+
* `{ id }` the server kept. An id alone isn't taken. A missing player,
|
|
16
|
+
* `null`, as a guest's ticket has, is `null`, and isn't asked for.
|
|
17
|
+
* Rejects with `invalid` for what isn't a list of players, and
|
|
18
|
+
* `too_many` past 50.
|
|
19
|
+
*/
|
|
20
|
+
read(players: readonly ({
|
|
21
|
+
readonly id: string;
|
|
22
|
+
} | null | undefined)[]): Promise<(Player | null)[]>;
|
|
23
|
+
/**
|
|
24
|
+
* The players who deleted their accounts in the last 30 days, oldest
|
|
25
|
+
* first, or those after `since`, an ISO time. Read it on a schedule, and
|
|
26
|
+
* clear what the server holds against each. Rejects with `invalid` for
|
|
27
|
+
* a `since` that isn't a time.
|
|
28
|
+
*/
|
|
29
|
+
deleted(options?: {
|
|
30
|
+
since?: string;
|
|
31
|
+
}): Promise<DeletedPlayer[]>;
|
|
32
|
+
}
|
|
33
|
+
export declare function createPlayers(api: () => Api): Players;
|