@cardonjs/realtime-do 0.2.3 → 0.3.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/README.md +254 -8
- package/dist/client.d.ts +9 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +54 -2
- package/dist/client.js.map +1 -1
- package/dist/connect.d.ts +2 -0
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +3 -2
- package/dist/connect.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +22 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +22 -2
- package/dist/protocol.js.map +1 -1
- package/dist/publisher.d.ts +4 -4
- package/dist/room.d.ts.map +1 -1
- package/dist/room.js +209 -5
- package/dist/room.js.map +1 -1
- package/dist/ticket.d.ts +64 -0
- package/dist/ticket.d.ts.map +1 -0
- package/dist/ticket.js +178 -0
- package/dist/ticket.js.map +1 -0
- package/dist/validation.d.ts +7 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +7 -0
- package/dist/validation.js.map +1 -1
- package/llms.txt +52 -8
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -13,6 +13,9 @@ reconnects, or that missed events, reloads its state through the API.
|
|
|
13
13
|
binding with internal `fetch` requests.
|
|
14
14
|
- `connectToRoom(namespace, room, request, options)` opens a socket from a route, after the
|
|
15
15
|
route authenticated the caller and authorized the room. Channels are chosen by the server.
|
|
16
|
+
- `issueRoomTicket(namespace, room, options)` and `connectToRoomWithTicket(namespace, room,
|
|
17
|
+
request, options)`: the same for a client without a same-site cookie (an access token
|
|
18
|
+
holder), through a one-time ticket (see [Clients without a cookie](#clients-without-a-cookie-tickets)).
|
|
16
19
|
- `realtime(namespace)` returns a `RealtimePublisher` (`publish`, `count`, `disconnect`,
|
|
17
20
|
`presence`) for the adapters factory.
|
|
18
21
|
- `@cardonjs/realtime-do/client`: `createRealtimeClient`, zero dependencies, for browsers.
|
|
@@ -27,8 +30,10 @@ For coding agents: `llms.txt`, shipped next to this README in the package, is a
|
|
|
27
30
|
## Wrangler configuration
|
|
28
31
|
|
|
29
32
|
Bind the room class and create it with a migration (`new_sqlite_classes`: the room stores
|
|
30
|
-
only short-lived revocation markers, and new classes should use the SQLite
|
|
31
|
-
one on the free plan):
|
|
33
|
+
only short-lived revocation markers and pending tickets, and new classes should use the SQLite
|
|
34
|
+
backend, the only one on the free plan). Tickets (below) need it: they use its synchronous
|
|
35
|
+
storage, and on a room class created with `new_classes` `issueRoomTicket` and
|
|
36
|
+
`connectToRoomWithTicket` throw an `Error` saying so. Everything else works on either backend.
|
|
32
37
|
|
|
33
38
|
```toml
|
|
34
39
|
[[durable_objects.bindings]]
|
|
@@ -299,7 +304,8 @@ namespace. Business logic depends on the `RealtimePublisher` type, not on the bi
|
|
|
299
304
|
- `count(room)` resolves with the open connections (expired ones are closed first and not
|
|
300
305
|
counted).
|
|
301
306
|
- `disconnect(room, { id, kind })` closes that identity's open connections in the room with
|
|
302
|
-
4001, refuses its connects there for 60 seconds, and
|
|
307
|
+
4001, drops its pending tickets, refuses its connects (and tickets) there for 60 seconds, and
|
|
308
|
+
resolves with `{ closed }`.
|
|
303
309
|
- `presence(room, options?)` lists who is connected (see [Presence](#presence)).
|
|
304
310
|
|
|
305
311
|
Names and events:
|
|
@@ -502,14 +508,256 @@ client.close();
|
|
|
502
508
|
in); otherwise the client keeps retrying with backoff.
|
|
503
509
|
- **No tokens in the URL.** Do not put a session token or API key in the socket URL's query
|
|
504
510
|
string: URLs end up in logs and history. The upgrade is authenticated by the same-site
|
|
505
|
-
session cookie.
|
|
511
|
+
session cookie, or by a one-time ticket offered as a subprotocol (next section).
|
|
512
|
+
|
|
513
|
+
## Clients without a cookie: tickets
|
|
514
|
+
|
|
515
|
+
A browser cannot set `Authorization` on a WebSocket upgrade, and a credential never goes in a
|
|
516
|
+
URL. A caller whose credential travels in a header (an access token of
|
|
517
|
+
`@cardonjs/access-tokens`, a bearer of another site) exchanges it for a one-time ticket, then
|
|
518
|
+
opens the socket offering the ticket as a WebSocket subprotocol, the only header a page can set
|
|
519
|
+
on an upgrade.
|
|
520
|
+
|
|
521
|
+
The example: a guest who placed an order holds an access token for it (see
|
|
522
|
+
`@cardonjs/access-tokens`) and follows the order live on a shop page of another site. The room
|
|
523
|
+
is per order (`order:<id>`).
|
|
524
|
+
|
|
525
|
+
```ts
|
|
526
|
+
import { requireAccessToken } from "@cardonjs/access-tokens";
|
|
527
|
+
import { requireAuth } from "@cardonjs/auth";
|
|
528
|
+
import {
|
|
529
|
+
type CardonEnv,
|
|
530
|
+
createApp,
|
|
531
|
+
defineModule,
|
|
532
|
+
errorEnvelopeSchema,
|
|
533
|
+
idSchema,
|
|
534
|
+
ok,
|
|
535
|
+
rateLimit,
|
|
536
|
+
rateLimitKeys,
|
|
537
|
+
successEnvelope,
|
|
538
|
+
UnauthorizedError,
|
|
539
|
+
workersRateLimiter,
|
|
540
|
+
} from "@cardonjs/core";
|
|
541
|
+
import {
|
|
542
|
+
connectToRoomWithTicket,
|
|
543
|
+
defineRoom,
|
|
544
|
+
issueRoomTicket,
|
|
545
|
+
realtime,
|
|
546
|
+
} from "@cardonjs/realtime-do";
|
|
547
|
+
import { createRoute, z } from "@hono/zod-openapi";
|
|
548
|
+
|
|
549
|
+
const SHOP_ORIGIN = "https://shop.example.com"; // the page that opens the socket
|
|
550
|
+
|
|
551
|
+
type Bindings = { ORDER_ROOMS: DurableObjectNamespace; LIVE_LIMITER: RateLimit; DB: D1Database };
|
|
552
|
+
const adapters = (env: Bindings) => ({
|
|
553
|
+
auth: sessionAuth(env), // your Authenticator, for the routes that need a login
|
|
554
|
+
tokens: orderTokens(env.DB), // accessTokens({ store, prefix: "ord", ... })
|
|
555
|
+
live: realtime(env.ORDER_ROOMS),
|
|
556
|
+
limiter: workersRateLimiter(env.LIVE_LIMITER, { periodSeconds: 60 }),
|
|
557
|
+
});
|
|
558
|
+
type AppEnv = CardonEnv<Bindings, ReturnType<typeof adapters>>;
|
|
559
|
+
|
|
560
|
+
const errors = (description: string) => ({
|
|
561
|
+
description,
|
|
562
|
+
content: { "application/json": { schema: errorEnvelopeSchema } },
|
|
563
|
+
});
|
|
564
|
+
const perIp = rateLimit<AppEnv>({
|
|
565
|
+
name: "order-live",
|
|
566
|
+
limiter: (c) => c.var.adapters.limiter,
|
|
567
|
+
key: rateLimitKeys.ip,
|
|
568
|
+
});
|
|
569
|
+
const orderParams = z.object({ id: idSchema });
|
|
570
|
+
|
|
571
|
+
// 1. Exchange: the access token (in `Authorization`) for a ticket to this order's room.
|
|
572
|
+
const ticketRoute = createRoute({
|
|
573
|
+
method: "post",
|
|
574
|
+
path: "/{id}/live-ticket",
|
|
575
|
+
tags: ["Orders"],
|
|
576
|
+
summary: "Get a one-time ticket for the order's live updates",
|
|
577
|
+
middleware: [
|
|
578
|
+
perIp,
|
|
579
|
+
requireAccessToken<AppEnv>((c) => c.var.adapters.tokens, {
|
|
580
|
+
resource: { type: "order", param: "id" },
|
|
581
|
+
scope: "order:read",
|
|
582
|
+
}),
|
|
583
|
+
] as const,
|
|
584
|
+
request: { params: orderParams },
|
|
585
|
+
responses: {
|
|
586
|
+
200: {
|
|
587
|
+
description: "The ticket, usable once within `expiresAt`",
|
|
588
|
+
content: {
|
|
589
|
+
"application/json": {
|
|
590
|
+
schema: successEnvelope(z.object({ ticket: z.string(), expiresAt: z.string() })),
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
},
|
|
594
|
+
429: errors("Too many requests, or too many pending tickets"),
|
|
595
|
+
},
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
// 2. Connect: the ticket is the authorization (public in `requireAuth`).
|
|
599
|
+
const liveRoute = createRoute({
|
|
600
|
+
method: "get",
|
|
601
|
+
path: "/{id}/live",
|
|
602
|
+
tags: ["Orders"],
|
|
603
|
+
summary: "Open the order's live updates with a ticket (WebSocket upgrade)",
|
|
604
|
+
middleware: perIp,
|
|
605
|
+
request: { params: orderParams },
|
|
606
|
+
responses: {
|
|
607
|
+
101: { description: "Switching to the WebSocket protocol" },
|
|
608
|
+
400: errors("Not a WebSocket upgrade"),
|
|
609
|
+
401: errors("No valid ticket for this order"),
|
|
610
|
+
403: errors("Origin not allowed"),
|
|
611
|
+
429: errors("Too many connects, or too many connections"),
|
|
612
|
+
},
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
const ordersLive = defineModule<AppEnv>({
|
|
616
|
+
name: "orders-live",
|
|
617
|
+
prefix: "/v1/orders",
|
|
618
|
+
routes: (app) => {
|
|
619
|
+
app.openapi(ticketRoute, async (c) => {
|
|
620
|
+
const grant = c.var.accessToken;
|
|
621
|
+
if (grant === undefined) throw new UnauthorizedError(); // fail closed
|
|
622
|
+
const { ticket, expiresAt } = await issueRoomTicket(
|
|
623
|
+
c.env.ORDER_ROOMS,
|
|
624
|
+
`order:${grant.resourceId}`, // from the grant: the token is for this order
|
|
625
|
+
{
|
|
626
|
+
identity: { id: grant.id, kind: "guest" }, // the token's record id
|
|
627
|
+
channels: ["order"], // chosen by the server
|
|
628
|
+
connectionExpiresAt: grant.expiresAt, // the socket does not outlive the token
|
|
629
|
+
},
|
|
630
|
+
);
|
|
631
|
+
c.header("Cache-Control", "no-store"); // a credential: never cached
|
|
632
|
+
return ok(c, { ticket, expiresAt: expiresAt.toISOString() });
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
app.openapi(liveRoute, async (c) => {
|
|
636
|
+
const { id } = c.req.valid("param");
|
|
637
|
+
return connectToRoomWithTicket(c.env.ORDER_ROOMS, `order:${id}`, c.req.raw, {
|
|
638
|
+
allowedOrigins: [SHOP_ORIGIN],
|
|
639
|
+
});
|
|
640
|
+
});
|
|
641
|
+
},
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
const app = createApp(adapters, {
|
|
645
|
+
cors: { origins: [SHOP_ORIGIN] }, // the exchange is a cross-origin fetch from the shop page
|
|
646
|
+
guard: requireAuth<AppEnv>({
|
|
647
|
+
authenticator: (c) => c.var.adapters.auth,
|
|
648
|
+
public: ["POST /v1/orders/:id/live-ticket", "GET /v1/orders/:id/live"],
|
|
649
|
+
}),
|
|
650
|
+
}).register(ordersLive);
|
|
651
|
+
|
|
652
|
+
export const OrderRoom = defineRoom<Bindings>();
|
|
653
|
+
|
|
654
|
+
export default app;
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
On the shop page, the client asks for a new ticket before every connection attempt:
|
|
658
|
+
|
|
659
|
+
```ts
|
|
660
|
+
import { createRealtimeClient } from "@cardonjs/realtime-do/client";
|
|
661
|
+
|
|
662
|
+
const client = createRealtimeClient({
|
|
663
|
+
url: `https://api.example.com/v1/orders/${orderId}/live`,
|
|
664
|
+
ticket: async () => {
|
|
665
|
+
const res = await fetch(`https://api.example.com/v1/orders/${orderId}/live-ticket`, {
|
|
666
|
+
method: "POST",
|
|
667
|
+
headers: { Authorization: `Bearer ${orderToken}` },
|
|
668
|
+
});
|
|
669
|
+
if (res.status === 401) client.close(); // the token expired or was revoked: stop
|
|
670
|
+
if (!res.ok) throw new Error(`live ticket: ${res.status}`); // a failed attempt: backoff
|
|
671
|
+
return ((await res.json()) as { data: { ticket: string } }).data.ticket;
|
|
672
|
+
},
|
|
673
|
+
onEvent: () => void reloadOrder(orderId),
|
|
674
|
+
onResync: () => reloadOrder(orderId),
|
|
675
|
+
});
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
The `ticket` function is first called while `createRealtimeClient` runs, before `client` is
|
|
679
|
+
assigned: it must not use `client` before its first `await` (the `close()` above runs after one).
|
|
680
|
+
An attempt whose ticket does not come within 10 seconds has failed: the client backs off and
|
|
681
|
+
tries again with a new call, and a late answer opens nothing.
|
|
682
|
+
|
|
683
|
+
How it works:
|
|
684
|
+
|
|
685
|
+
- **The room keeps the tickets.** `issueRoomTicket` asks the room (through the binding) for a
|
|
686
|
+
ticket: `crt_` and 43 characters (32 random bytes). The room stores only its SHA-256, with the
|
|
687
|
+
identity, the channels and the expiry, and answers the ticket once. No secret, table or new
|
|
688
|
+
binding. A ticket exists only in the room that issued it, so it opens that room and no other.
|
|
689
|
+
- **Single use.** `connectToRoomWithTicket` sends the ticket to the room, which reads and deletes
|
|
690
|
+
its entry in one synchronous step: of two connects with one ticket, exactly one finds it. The
|
|
691
|
+
room then connects exactly as `connectToRoom` does, with the stored identity and channels
|
|
692
|
+
(same caps, revocation window and hooks). The ticket is used up even when that connect is
|
|
693
|
+
refused (429): the client gets a new one for its next attempt.
|
|
694
|
+
- **On the wire.** The client offers `Sec-WebSocket-Protocol: cardon.ticket.v1, <ticket>`,
|
|
695
|
+
exactly these two entries in this order, and the 101 selects `cardon.ticket.v1` (a browser
|
|
696
|
+
fails a connection that offered subprotocols and got none). Never in the query string.
|
|
697
|
+
- **Lifetimes.** `ttlSeconds` (default 30, 5 to 120) is how long the ticket can be used.
|
|
698
|
+
`connectionExpiresAt` (optional, a `Date`) is when the credential that was exchanged expires:
|
|
699
|
+
the ticket never outlives it, and the socket is closed with 4000 at the room's first sweep after
|
|
700
|
+
it (connect, publish, count or presence), if that is earlier than its age from
|
|
701
|
+
`maxConnectionAgeSeconds`; it never extends that age. The client then asks for a new ticket,
|
|
702
|
+
which the exchange route refuses once the token expired.
|
|
703
|
+
- **Caps.** At most 1000 pending tickets per room and 5 per identity; past that, issuing answers
|
|
704
|
+
`RateLimitedError` (429). Used tickets free their slot at once, expired ones when the room next
|
|
705
|
+
issues a ticket (they are deleted then; there is no alarm). In a room many identities share,
|
|
706
|
+
the room-wide cap can be filled by many credentials together (each within its own 5), which
|
|
707
|
+
blocks issuing for everyone there until tickets are used or expire: rate limit the exchange
|
|
708
|
+
route per credential as well as per IP (for example a second `rateLimit` after
|
|
709
|
+
`requireAccessToken`, keyed on `c.var.accessToken?.id`). The caps are not configurable.
|
|
710
|
+
- **Revocation.** `disconnect(room, { id, kind: "guest" })` closes the identity's sockets, drops
|
|
711
|
+
its pending tickets and, for 60 seconds, refuses its tickets and connects (429). A socket that
|
|
712
|
+
is already open otherwise stays open until `connectionExpiresAt`, its maximum age or
|
|
713
|
+
`disconnect`, so an app that revokes a token also calls `disconnect`:
|
|
714
|
+
|
|
715
|
+
```ts
|
|
716
|
+
await tokens.revoke(record.id);
|
|
717
|
+
await live.disconnect(`order:${orderId}`, { id: record.id, kind: "guest" });
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
`issueRoomTicket` throws a `TypeError` for invalid arguments (room, identity, channels,
|
|
721
|
+
`ttlSeconds`, a `connectionExpiresAt` that is not a `Date` later than now, binding), before
|
|
722
|
+
calling the room, and `RateLimitedError` over the caps or during a revocation window. `connectToRoomWithTicket`
|
|
723
|
+
throws the core errors:
|
|
724
|
+
|
|
725
|
+
| Case | Error |
|
|
726
|
+
| --- | --- |
|
|
727
|
+
| Not a `GET` with `Upgrade: websocket` | `ValidationError` (400) |
|
|
728
|
+
| `Origin` missing or not exactly in `allowedOrigins` | `ForbiddenError` (403) |
|
|
729
|
+
| No ticket, malformed, unknown, used, expired, for another room, or other subprotocol entries | `UnauthorizedError` (401), one message for all |
|
|
730
|
+
| Room full, the identity has too many connections, or it was disconnected less than 60 s ago | `RateLimitedError` (429) |
|
|
731
|
+
|
|
732
|
+
What stays the app's:
|
|
733
|
+
|
|
734
|
+
- The exchange route's authentication and its authorization of the room (above:
|
|
735
|
+
`requireAccessToken` for that order, with the room built from the grant), its rate limit, and
|
|
736
|
+
`Cache-Control: no-store` on its answer.
|
|
737
|
+
- **A rate limit on the connect route, per IP: required.** The route is public and takes the
|
|
738
|
+
room name from the path, so without a credential anyone can make it wake a Durable Object for
|
|
739
|
+
any room name. The per-IP `rateLimit` above bounds that; the route may also check that the room
|
|
740
|
+
is one the app has (the order exists) before calling `connectToRoomWithTicket`.
|
|
741
|
+
- The `Origin` check: `allowedOrigins` holds the pages allowed to open the socket.
|
|
742
|
+
- A room class created with `new_sqlite_classes` (above). On another backend a ticket connect is
|
|
743
|
+
an `Error`, so the client gets the app's generic 500, and the log says why.
|
|
744
|
+
|
|
745
|
+
The package's own code never puts the ticket in a URL, a log line, an error message, a socket
|
|
746
|
+
tag or storage (only its hash is stored). The ticket does travel in the `Sec-WebSocket-Protocol`
|
|
747
|
+
request header of the connect route: an app that records request headers (its own logging
|
|
748
|
+
middleware, a Tail Worker, a log push) must redact that header, and keep the exchange route's
|
|
749
|
+
answer out of its logs.
|
|
750
|
+
|
|
751
|
+
`"guest"` is a `RoomIdentity.kind` for a caller without an account; with an access token, `id`
|
|
752
|
+
is the token's record id (`grant.id`), so `disconnect` with it closes that token's sockets.
|
|
753
|
+
Presence lists guests like any identity: filter by `kind` when it matters.
|
|
506
754
|
|
|
507
755
|
## Limits and close codes
|
|
508
756
|
|
|
509
757
|
| Close code | Meaning | Client |
|
|
510
758
|
| --- | --- | --- |
|
|
511
759
|
| 1008 | Protocol violation: the client sent something other than `"ping"` | stops (`"closed"`) |
|
|
512
|
-
| 4000 | Connection past its age (see `maxConnectionAgeSeconds`): authenticate again | reconnects after a random delay below `minBackoffMs` |
|
|
760
|
+
| 4000 | Connection past its age (see `maxConnectionAgeSeconds`), or past the `connectionExpiresAt` of its ticket: authenticate again | reconnects after a random delay below `minBackoffMs` |
|
|
513
761
|
| 4001 | Disconnected by the server (`disconnect`) | reconnects with backoff; the route decides |
|
|
514
762
|
|
|
515
763
|
Connection age is checked lazily (there is no alarm): an expired socket is closed the next time
|
|
@@ -550,9 +798,7 @@ so fake timers apply. Test-only.
|
|
|
550
798
|
|
|
551
799
|
## Not covered
|
|
552
800
|
|
|
553
|
-
-
|
|
554
|
-
no tokens in URLs (do not add any to the query string). A one-time ticket for clients on another site is added when a project needs
|
|
555
|
-
it.
|
|
801
|
+
- A ticket in the query string, for clients that cannot offer subprotocols.
|
|
556
802
|
- Client → server messages (other than the keep-alive ping), message history or replay,
|
|
557
803
|
subscriptions after connect, binary frames, alarms.
|
|
558
804
|
- Presence broadcast by the framework, presence across rooms in one query, custom
|
package/dist/client.d.ts
CHANGED
|
@@ -16,6 +16,15 @@ export interface RealtimeClientOptions {
|
|
|
16
16
|
onResync(): void | Promise<void>;
|
|
17
17
|
/** Called on every status change (not for the initial `"connecting"`). */
|
|
18
18
|
onStatus?(status: ConnectionStatus): void;
|
|
19
|
+
/**
|
|
20
|
+
* For a client without a same-site cookie: called before every connection attempt (the first
|
|
21
|
+
* and each reconnect), usually a `fetch` to the app's exchange route; the ticket it resolves
|
|
22
|
+
* with is offered with the upgrade (`new WebSocket(url, ["cardon.ticket.v1", ticket])`). A
|
|
23
|
+
* rejection, a value that is not a ticket, or no answer within 10 seconds counts as a failed
|
|
24
|
+
* attempt. Without it, the socket is opened with the URL alone. It is first called while
|
|
25
|
+
* `createRealtimeClient` runs: do not use the returned client before its first `await`.
|
|
26
|
+
*/
|
|
27
|
+
readonly ticket?: () => string | Promise<string>;
|
|
19
28
|
/** Default 30 000; 0 disables fallback polling. */
|
|
20
29
|
readonly pollIntervalMs?: number;
|
|
21
30
|
/** Default 25 000. */
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAGL,KAAK,aAAa,EAGnB,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhE,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG,MAAM,GAAG,cAAc,GAAG,QAAQ,CAAC;AAEjF,MAAM,WAAW,qBAAqB;IACpC,8EAA8E;IAC9E,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAC;IACpC,4FAA4F;IAC5F,QAAQ,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,0EAA0E;IAC1E,QAAQ,CAAC,CAAC,MAAM,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAC1C;;;;;;;OAOG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,mDAAmD;IACnD,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,sBAAsB;IACtB,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC,sBAAsB;IACtB,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,oBAAoB;IACpB,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,sBAAsB;IACtB,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,SAAS,CAAC;IACtC,oDAAoD;IACpD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC;;;OAGG;IACH,KAAK,IAAI,IAAI,CAAC;CACf;AA8GD;;;;;;;;;;;;GAYG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAkOnF"}
|
package/dist/client.js
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* The socket only signals "something changed": events are hints, and `onResync` reloads the
|
|
6
6
|
* state from the API after every reconnect and on each fallback poll while disconnected.
|
|
7
7
|
*/
|
|
8
|
-
import { EVENT_TYPE_PATTERN, NAME_PATTERN } from "./validation.js";
|
|
8
|
+
import { EVENT_TYPE_PATTERN, NAME_PATTERN, TICKET_PATTERN, TICKET_PROTOCOL, } from "./validation.js";
|
|
9
9
|
/** A connection that stays open this long resets the backoff. */
|
|
10
10
|
const STABLE_AFTER_MS = 10_000;
|
|
11
|
+
/** How long one attempt waits for the `ticket` function; past it the attempt failed. */
|
|
12
|
+
const TICKET_TIMEOUT_MS = 10_000;
|
|
11
13
|
const CLOSE_PROTOCOL = 1008;
|
|
12
14
|
const CLOSE_REAUTHENTICATE = 4000;
|
|
13
15
|
function isLocalHost(hostname) {
|
|
@@ -116,6 +118,10 @@ export function createRealtimeClient(options) {
|
|
|
116
118
|
if (options.onStatus !== undefined && typeof options.onStatus !== "function") {
|
|
117
119
|
throw new TypeError("createRealtimeClient: onStatus must be a function.");
|
|
118
120
|
}
|
|
121
|
+
if (options.ticket !== undefined && typeof options.ticket !== "function") {
|
|
122
|
+
throw new TypeError("createRealtimeClient: ticket must be a function.");
|
|
123
|
+
}
|
|
124
|
+
const ticketSource = options.ticket;
|
|
119
125
|
const url = resolveUrl(options.url);
|
|
120
126
|
const pollIntervalMs = integer("pollIntervalMs", options.pollIntervalMs, 30_000, 0);
|
|
121
127
|
const pingIntervalMs = integer("pingIntervalMs", options.pingIntervalMs, 25_000, 1);
|
|
@@ -141,6 +147,7 @@ export function createRealtimeClient(options) {
|
|
|
141
147
|
let pongTimer;
|
|
142
148
|
let pingTimer;
|
|
143
149
|
let pollTimer;
|
|
150
|
+
let ticketTimer;
|
|
144
151
|
const setStatus = (next) => {
|
|
145
152
|
if (status === next)
|
|
146
153
|
return;
|
|
@@ -194,9 +201,52 @@ export function createRealtimeClient(options) {
|
|
|
194
201
|
reconnectTimer = undefined;
|
|
195
202
|
if (stopped)
|
|
196
203
|
return;
|
|
204
|
+
if (ticketSource === undefined) {
|
|
205
|
+
open(undefined);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
// A new ticket for every attempt: a ticket is single-use and short-lived.
|
|
209
|
+
let pending;
|
|
210
|
+
try {
|
|
211
|
+
pending = Promise.resolve(ticketSource());
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
scheduleReconnect(false);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// Settled once: by the answer or by the timeout, whichever comes first. A late answer
|
|
218
|
+
// (after the timeout, or after close()) opens nothing.
|
|
219
|
+
let settled = false;
|
|
220
|
+
const settle = () => {
|
|
221
|
+
if (settled)
|
|
222
|
+
return false;
|
|
223
|
+
settled = true;
|
|
224
|
+
stopTimeout(ticketTimer);
|
|
225
|
+
ticketTimer = undefined;
|
|
226
|
+
return !stopped;
|
|
227
|
+
};
|
|
228
|
+
ticketTimer = setTimeout(() => {
|
|
229
|
+
if (settle())
|
|
230
|
+
scheduleReconnect(false);
|
|
231
|
+
}, TICKET_TIMEOUT_MS);
|
|
232
|
+
pending.then((ticket) => {
|
|
233
|
+
if (!settle())
|
|
234
|
+
return; // close() while the ticket was awaited, or too late: no socket.
|
|
235
|
+
if (typeof ticket !== "string" || !TICKET_PATTERN.test(ticket)) {
|
|
236
|
+
scheduleReconnect(false);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
open(ticket);
|
|
240
|
+
}, () => {
|
|
241
|
+
if (settle())
|
|
242
|
+
scheduleReconnect(false);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function open(ticket) {
|
|
197
246
|
let ws;
|
|
198
247
|
try {
|
|
199
|
-
|
|
248
|
+
// Without a ticket, exactly as before: the URL alone.
|
|
249
|
+
ws = ticket === undefined ? new Socket(url) : new Socket(url, [TICKET_PROTOCOL, ticket]);
|
|
200
250
|
}
|
|
201
251
|
catch {
|
|
202
252
|
scheduleReconnect(false);
|
|
@@ -262,6 +312,8 @@ export function createRealtimeClient(options) {
|
|
|
262
312
|
stopped = true;
|
|
263
313
|
stopTimeout(reconnectTimer);
|
|
264
314
|
reconnectTimer = undefined;
|
|
315
|
+
stopTimeout(ticketTimer);
|
|
316
|
+
ticketTimer = undefined;
|
|
265
317
|
stopPolling();
|
|
266
318
|
const current = release();
|
|
267
319
|
try {
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,kBAAkB,EAClB,YAAY,EAEZ,cAAc,EACd,eAAe,GAChB,MAAM,iBAAiB,CAAC;AAiEzB,iEAAiE;AACjE,MAAM,eAAe,GAAG,MAAM,CAAC;AAC/B,wFAAwF;AACxF,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACjC,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAElC,SAAS,WAAW,CAAC,QAAgB;IACnC,OAAO,CACL,QAAQ,KAAK,WAAW;QACxB,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC;QAC/B,QAAQ,KAAK,WAAW;QACxB,QAAQ,KAAK,OAAO,CACrB,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CAAC,GAAY;IAC9B,MAAM,OAAO,GAAG,GAAG,EAAE,CACnB,IAAI,SAAS,CACX,oIAAoI,CACrI,CAAC;IACJ,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,EAAE;QAAE,MAAM,OAAO,EAAE,CAAC;IAC3D,MAAM,QAAQ,GAAI,UAA0C,CAAC,QAAQ,CAAC;IACtE,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC5E,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,OAAO,EAAE,CAAC;IAClB,CAAC;IACD,IAA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oBAAoB,CAAC,CAAC;QACzD,CAAC,CAAC;QAEF,gDAAgD;QAChD,EAAE,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,SAAS,CAAC;IAC/B,CAAC;IAED,SAAS,IAAI;QACX,OAAO,GAAG,IAAI,CAAC;QACf,WAAW,CAAC,cAAc,CAAC,CAAC;QAC5B,cAAc,GAAG,SAAS,CAAC;QAC3B,WAAW,CAAC,WAAW,CAAC,CAAC;QACzB,WAAW,GAAG,SAAS,CAAC;QACxB,WAAW,EAAE,CAAC;QACd,MAAM,OAAO,GAAG,OAAO,EAAE,CAAC;QAC1B,IAAI,CAAC;YACH,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACjC,CAAC;QAAC,MAAM,CAAC;YACP,mBAAmB;QACrB,CAAC;QACD,SAAS,CAAC,QAAQ,CAAC,CAAC;IACtB,CAAC;IAED,OAAO,EAAE,CAAC;IAEV,OAAO;QACL,IAAI,MAAM;YACR,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,KAAK;YACH,IAAI,CAAC,OAAO;gBAAE,IAAI,EAAE,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/connect.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface ConnectOptions {
|
|
|
6
6
|
/** Exact origins allowed to open the socket, e.g. ["https://app.example.com"]. Required. */
|
|
7
7
|
readonly allowedOrigins: readonly string[];
|
|
8
8
|
}
|
|
9
|
+
/** `allowedOrigins`, checked; `caller` names the function in the `TypeError`. */
|
|
10
|
+
export declare function validOrigins(value: unknown, caller?: string): readonly string[];
|
|
9
11
|
/**
|
|
10
12
|
* Checks the upgrade and the `Origin`, then asks the room for a socket. Resolves with the 101
|
|
11
13
|
* response to return from the route.
|
package/dist/connect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAKA,OAAO,EAML,KAAK,YAAY,EAGlB,MAAM,eAAe,CAAC;AAGvB,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,uFAAuF;IACvF,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,4FAA4F;IAC5F,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;CAC5C;
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAKA,OAAO,EAML,KAAK,YAAY,EAGlB,MAAM,eAAe,CAAC;AAGvB,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,uFAAuF;IACvF,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,4FAA4F;IAC5F,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;CAC5C;AAYD,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,SAAkB,GAAG,SAAS,MAAM,EAAE,CAgBxF;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,aAAa,CACjC,SAAS,EAAE,sBAAsB,EACjC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,QAAQ,CAAC,CA4BnB"}
|
package/dist/connect.js
CHANGED
|
@@ -12,8 +12,9 @@ function isLocalHost(hostname) {
|
|
|
12
12
|
hostname === "127.0.0.1" ||
|
|
13
13
|
hostname === "[::1]");
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
15
|
+
/** `allowedOrigins`, checked; `caller` names the function in the `TypeError`. */
|
|
16
|
+
export function validOrigins(value, caller = "connectToRoom") {
|
|
17
|
+
const message = `${caller}: allowedOrigins must be a non-empty array of exact origins (https://host[:port]; http: only for localhost, *.localhost, 127.0.0.1 or [::1]), without wildcards.`;
|
|
17
18
|
if (!Array.isArray(value) || value.length === 0)
|
|
18
19
|
throw new TypeError(message);
|
|
19
20
|
for (const origin of value) {
|
package/dist/connect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,EACL,SAAS,EACT,eAAe,EACf,cAAc,EACd,eAAe,EACf,IAAI,EAEJ,QAAQ,EACR,aAAa,GACd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAUvE,2EAA2E;AAC3E,SAAS,WAAW,CAAC,QAAgB;IACnC,OAAO,CACL,QAAQ,KAAK,WAAW;QACxB,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC;QAC/B,QAAQ,KAAK,WAAW;QACxB,QAAQ,KAAK,OAAO,CACrB,CAAC;AACJ,CAAC;AAED,
|
|
1
|
+
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACnF,OAAO,EACL,SAAS,EACT,eAAe,EACf,cAAc,EACd,eAAe,EACf,IAAI,EAEJ,QAAQ,EACR,aAAa,GACd,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAUvE,2EAA2E;AAC3E,SAAS,WAAW,CAAC,QAAgB;IACnC,OAAO,CACL,QAAQ,KAAK,WAAW;QACxB,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC;QAC/B,QAAQ,KAAK,WAAW;QACxB,QAAQ,KAAK,O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AAC,MAAM,IAAI,CAAC,CAAC;AACjG,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ export type { Presence, PresenceConnection, PresenceOptions } from "./presence.j
|
|
|
3
3
|
export type { RoomIdentity } from "./protocol.js";
|
|
4
4
|
export { type RealtimePublisher, realtime } from "./publisher.js";
|
|
5
5
|
export { defineRoom, type RoomClass, type RoomHookContext, type RoomOptions, } from "./room.js";
|
|
6
|
+
export { connectToRoomWithTicket, issueRoomTicket, type RoomTicket, type RoomTicketOptions, type TicketConnectOptions, } from "./ticket.js";
|
|
6
7
|
export { assertValidChannel, assertValidChannels, assertValidRoom, type JsonValue, type RealtimeEvent, validEvent, } from "./validation.js";
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClE,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACnF,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,KAAK,iBAAiB,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EACL,UAAU,EACV,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,WAAW,GACjB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,UAAU,GACX,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClE,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACnF,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,KAAK,iBAAiB,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EACL,UAAU,EACV,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,WAAW,GACjB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,GAC1B,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EACf,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,UAAU,GACX,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { connectToRoom } from "./connect.js";
|
|
2
2
|
export { realtime } from "./publisher.js";
|
|
3
3
|
export { defineRoom, } from "./room.js";
|
|
4
|
+
export { connectToRoomWithTicket, issueRoomTicket, } from "./ticket.js";
|
|
4
5
|
export { assertValidChannel, assertValidChannels, assertValidRoom, validEvent, } from "./validation.js";
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,aAAa,EAAE,MAAM,cAAc,CAAC;AAGlE,OAAO,EAA0B,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EACL,UAAU,GAIX,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EAGf,UAAU,GACX,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,aAAa,EAAE,MAAM,cAAc,CAAC;AAGlE,OAAO,EAA0B,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EACL,UAAU,GAIX,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,uBAAuB,EACvB,eAAe,GAIhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,kBAAkB,EAClB,mBAAmB,EACnB,eAAe,EAGf,UAAU,GACX,MAAM,iBAAiB,CAAC"}
|
package/dist/protocol.d.ts
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
* room. Requests travel only through the Durable Object binding, so the room trusts what they
|
|
4
4
|
* carry; it still validates it, because a malformed value must never reach a socket tag.
|
|
5
5
|
*/
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Who holds a connection. Roles and email are not stored: they would go stale. `"guest"` is a
|
|
8
|
+
* caller without an account, e.g. the holder of an access token (`id`: the token's record id).
|
|
9
|
+
*/
|
|
7
10
|
export interface RoomIdentity {
|
|
8
11
|
readonly id: string;
|
|
9
|
-
readonly kind: "user" | "service";
|
|
12
|
+
readonly kind: "user" | "service" | "guest";
|
|
10
13
|
}
|
|
11
14
|
/** Base URL of the internal requests. Never leaves the binding. */
|
|
12
15
|
export declare const INTERNAL_ORIGIN = "https://realtime-do.internal";
|
|
@@ -17,6 +20,16 @@ export declare const PATH: {
|
|
|
17
20
|
readonly disconnect: "/disconnect";
|
|
18
21
|
/** POST `{ query }` (see `validPresenceQuery`); answers `{ connections, total }`, times in ms. */
|
|
19
22
|
readonly presence: "/presence";
|
|
23
|
+
/**
|
|
24
|
+
* POST `{ room, identity, channels, ttlSeconds, connectionExpiresAt? }` (epoch ms); answers
|
|
25
|
+
* `{ ticket, expiresAt }` once (epoch ms), 429 over the caps or during a revocation window.
|
|
26
|
+
*/
|
|
27
|
+
readonly ticket: "/ticket";
|
|
28
|
+
/**
|
|
29
|
+
* GET upgrade with `{ room, ticket }` in `CONNECT_HEADER`; consumes the ticket, then connects
|
|
30
|
+
* like `connect` with the stored identity and channels. 401 for any ticket failure.
|
|
31
|
+
*/
|
|
32
|
+
readonly ticketConnect: "/ticket-connect";
|
|
20
33
|
};
|
|
21
34
|
/**
|
|
22
35
|
* Carries the connect parameters: JSON with everything outside printable ASCII escaped
|
|
@@ -36,6 +49,13 @@ export declare function asciiJson(value: unknown): string;
|
|
|
36
49
|
* identities. Tags starting with `#` cannot collide with channels (letter or digit first).
|
|
37
50
|
*/
|
|
38
51
|
export declare function identityTag(identity: RoomIdentity): Promise<string>;
|
|
52
|
+
/** The start of every ticket's storage key (see `ticketKey`). */
|
|
53
|
+
export declare const TICKET_KEY_PREFIX = "#ticket:";
|
|
54
|
+
/**
|
|
55
|
+
* The storage key of a pending ticket: `#ticket:` + hex SHA-256 of the ticket. The ticket itself
|
|
56
|
+
* is never stored; with 256 random bits a plain hash cannot be reversed or guessed.
|
|
57
|
+
*/
|
|
58
|
+
export declare function ticketKey(ticket: string): Promise<string>;
|
|
39
59
|
/** The start of every identity tag (see `identityTag`). */
|
|
40
60
|
export declare const IDENTITY_TAG_PREFIX = "#identity:";
|
|
41
61
|
export declare function assertValidIdentityId(id: unknown): asserts id is string;
|
package/dist/protocol.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../src/protocol.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC;CAC7C;AAED,mEAAmE;AACnE,eAAO,MAAM,eAAe,iCAAiC,CAAC;AAC9D,eAAO,MAAM,IAAI;aACf,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
|
disconnect: "/disconnect",
|
|
13
13
|
/** POST `{ query }` (see `validPresenceQuery`); answers `{ connections, total }`, times in ms. */
|
|
14
14
|
presence: "/presence",
|
|
15
|
+
/**
|
|
16
|
+
* POST `{ room, identity, channels, ttlSeconds, connectionExpiresAt? }` (epoch ms); answers
|
|
17
|
+
* `{ ticket, expiresAt }` once (epoch ms), 429 over the caps or during a revocation window.
|
|
18
|
+
*/
|
|
19
|
+
ticket: "/ticket",
|
|
20
|
+
/**
|
|
21
|
+
* GET upgrade with `{ room, ticket }` in `CONNECT_HEADER`; consumes the ticket, then connects
|
|
22
|
+
* like `connect` with the stored identity and channels. 401 for any ticket failure.
|
|
23
|
+
*/
|
|
24
|
+
ticketConnect: "/ticket-connect",
|
|
15
25
|
};
|
|
16
26
|
/**
|
|
17
27
|
* Carries the connect parameters: JSON with everything outside printable ASCII escaped
|
|
@@ -44,6 +54,16 @@ export async function identityTag(identity) {
|
|
|
44
54
|
const input = new TextEncoder().encode(`${identity.kind}:${identity.id}`);
|
|
45
55
|
return `${IDENTITY_TAG_PREFIX}${hex(await crypto.subtle.digest("SHA-256", input))}`;
|
|
46
56
|
}
|
|
57
|
+
/** The start of every ticket's storage key (see `ticketKey`). */
|
|
58
|
+
export const TICKET_KEY_PREFIX = "#ticket:";
|
|
59
|
+
/**
|
|
60
|
+
* The storage key of a pending ticket: `#ticket:` + hex SHA-256 of the ticket. The ticket itself
|
|
61
|
+
* is never stored; with 256 random bits a plain hash cannot be reversed or guessed.
|
|
62
|
+
*/
|
|
63
|
+
export async function ticketKey(ticket) {
|
|
64
|
+
const input = new TextEncoder().encode(ticket);
|
|
65
|
+
return `${TICKET_KEY_PREFIX}${hex(await crypto.subtle.digest("SHA-256", input))}`;
|
|
66
|
+
}
|
|
47
67
|
/** The start of every identity tag (see `identityTag`). */
|
|
48
68
|
export const IDENTITY_TAG_PREFIX = "#identity:";
|
|
49
69
|
export function assertValidIdentityId(id) {
|
|
@@ -62,8 +82,8 @@ export function validIdentity(identity) {
|
|
|
62
82
|
}
|
|
63
83
|
const { id, kind } = identity;
|
|
64
84
|
assertValidIdentityId(id);
|
|
65
|
-
if (kind !== "user" && kind !== "service") {
|
|
66
|
-
throw new TypeError('realtime: identity.kind must be "user" or "
|
|
85
|
+
if (kind !== "user" && kind !== "service" && kind !== "guest") {
|
|
86
|
+
throw new TypeError('realtime: identity.kind must be "user", "service" or "guest".');
|
|
67
87
|
}
|
|
68
88
|
return Object.freeze({ id, kind });
|
|
69
89
|
}
|