@pairlobby/server-core 0.0.0-stage → 0.12.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 ADDED
@@ -0,0 +1,93 @@
1
+ Elastic License 2.0
2
+
3
+ URL: https://www.elastic.co/licensing/elastic-license
4
+
5
+ ## Acceptance
6
+
7
+ By using the software, you agree to all of the terms and conditions below.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
12
+ non-sublicensable, non-transferable license to use, copy, distribute, make
13
+ available, and prepare derivative works of the software, in each case subject to
14
+ the limitations and conditions below.
15
+
16
+ ## Limitations
17
+
18
+ You may not provide the software to third parties as a hosted or managed
19
+ service, where the service provides users with access to any substantial set of
20
+ the features or functionality of the software.
21
+
22
+ You may not move, change, disable, or circumvent the license key functionality
23
+ in the software, and you may not remove or obscure any functionality in the
24
+ software that is protected by the license key.
25
+
26
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
27
+ of the licensor in the software. Any use of the licensor’s trademarks is subject
28
+ to applicable law.
29
+
30
+ ## Patents
31
+
32
+ The licensor grants you a license, under any patent claims the licensor can
33
+ license, or becomes able to license, to make, have made, use, sell, offer for
34
+ sale, import and have imported the software, in each case subject to the
35
+ limitations and conditions in this license. This license does not cover any
36
+ patent claims that you cause to be infringed by modifications or additions to
37
+ the software. If you or your company make any written claim that the software
38
+ infringes or contributes to infringement of any patent, your patent license for
39
+ the software granted under these terms ends immediately. If your company makes
40
+ such a claim, your patent license ends immediately for work on behalf of your
41
+ company.
42
+
43
+ ## Notices
44
+
45
+ You must ensure that anyone who gets a copy of any part of the software from you
46
+ also gets a copy of these terms.
47
+
48
+ If you modify the software, you must include in any modified copies of the
49
+ software prominent notices stating that you have modified the software.
50
+
51
+ ## No Other Rights
52
+
53
+ These terms do not imply any licenses other than those expressly granted in
54
+ these terms.
55
+
56
+ ## Termination
57
+
58
+ If you use the software in violation of these terms, such use is not licensed,
59
+ and your licenses will automatically terminate. If the licensor provides you
60
+ with a notice of your violation, and you cease all violation of this license no
61
+ later than 30 days after you receive that notice, your licenses will be
62
+ reinstated retroactively. However, if you violate these terms after such
63
+ reinstatement, any additional violation of these terms will cause your licenses
64
+ to terminate automatically and permanently.
65
+
66
+ ## No Liability
67
+
68
+ *As far as the law allows, the software comes as is, without any warranty or
69
+ condition, and the licensor will not be liable to you for any damages arising
70
+ out of these terms or the use or nature of the software, under any kind of
71
+ legal claim.*
72
+
73
+ ## Definitions
74
+
75
+ The **licensor** is the entity offering these terms, and the **software** is the
76
+ software the licensor makes available under these terms, including any portion
77
+ of it.
78
+
79
+ **you** refers to the individual or entity agreeing to these terms.
80
+
81
+ **your company** is any legal entity, sole proprietorship, or other kind of
82
+ organization that you work for, plus all organizations that have control over,
83
+ are under the control of, or are under common control with that
84
+ organization. **control** means ownership of substantially all the assets of an
85
+ entity, or the power to direct its management and policies by vote, contract, or
86
+ otherwise. Control can be direct or indirect.
87
+
88
+ **your licenses** are all the licenses granted to you for the software under
89
+ these terms.
90
+
91
+ **use** means anything you do with the software requiring one of your licenses.
92
+
93
+ **trademark** means trademarks, service marks, and similar rights.
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
- # Temporary Holding Version
1
+ # @pairlobby/server-core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Part of [PairLobby](https://github.com/PairLobby/app), built from `packages/server-core` at version 0.12.0. See that repository for documentation.
4
+
5
+ Licensed under the [Elastic License 2.0](LICENSE): you may use, copy, modify and distribute it, but may not offer it to others as a hosted or managed service.
@@ -0,0 +1,33 @@
1
+ import type { HandoverRecord, InviteRecord, MessageRequest, ParticipantRecord, RequestPage, RoomEvent, RoomRecord } from '@pairlobby/protocol';
2
+ import type { Mutation, RoomView } from '@pairlobby/room-core';
3
+ import type { EventPage, IdempotencyRecord, RoomStore } from './store.js';
4
+ type Clock = () => number;
5
+ type IdempotencyKey = Parameters<RoomStore['apply']>[1];
6
+ export declare class AutoCloseStore implements RoomStore {
7
+ private readonly store;
8
+ private readonly now;
9
+ constructor(store: RoomStore, now: Clock);
10
+ createRoom(room: RoomRecord, participant: ParticipantRecord, event: RoomEvent): Promise<void>;
11
+ loadRoom(roomId: string): Promise<RoomView | null>;
12
+ apply(mutation: Mutation, idempotency: IdempotencyKey): Promise<void>;
13
+ updateTurns(room: RoomRecord, requests: MessageRequest[], expectedRevision: number): Promise<void>;
14
+ readEvents(roomId: string, after: number, limit: number): Promise<EventPage>;
15
+ eventBySeq(roomId: string, seq: number): Promise<RoomEvent | null>;
16
+ eventById(roomId: string, eventId: string): Promise<RoomEvent | null>;
17
+ idempotencyRecord(roomId: string, key: string): Promise<IdempotencyRecord | null>;
18
+ putInvite(invite: InviteRecord): Promise<void>;
19
+ inviteByDigest(digest: string): Promise<InviteRecord | null>;
20
+ reserveInvite(digest: string, attemptId: string, credentialHash: string, expectedOccupantId: string | null): Promise<InviteRecord | null>;
21
+ completeInvite(digest: string, participantId: string): Promise<void>;
22
+ participantByCredential(roomId: string, credentialHash: string): Promise<ParticipantRecord | null>;
23
+ handovers(roomId: string): Promise<HandoverRecord[]>;
24
+ messageRequest(roomId: string, eventId: string): Promise<MessageRequest | null>;
25
+ groupRequests(roomId: string, conversationId: string): Promise<MessageRequest[]>;
26
+ messageRequests(roomId: string, after: number, limit: number, recipientId?: string): Promise<RequestPage>;
27
+ setLifecycle(roomId: string, lifecycle: RoomRecord['lifecycle']): Promise<void>;
28
+ deleteRoom(roomId: string): Promise<void>;
29
+ dueAutoCloses(now: number, limit: number): Promise<string[]>;
30
+ nextAutoCloseAt(): Promise<number | null>;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=auto-close-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-close-store.d.ts","sourceRoot":"","sources":["../src/auto-close-store.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAC,cAAc,EAAE,YAAY,EAAE,cAAc,EAA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AC,IAAI,CAAC;IAIzC,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAI5D,eAAe,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAG5C"}
@@ -0,0 +1,108 @@
1
+ //! Keeps every room's auto-close deadline current and enforces it, for every
2
+ //! adapter at once. Each mutation recomputes the deadline inside the same atomic
3
+ //! write; each load first closes a room that is already past it, so no request,
4
+ //! timer or restart can act on a room after its deadline.
5
+ import { ProtocolError, newId } from '@pairlobby/protocol';
6
+ import { autoCloseDue, autoCloseRoom, refreshAutoClose } from '@pairlobby/room-core';
7
+ /** How many times a load retries closing when another write raced it. */
8
+ const CLOSE_ATTEMPTS = 3;
9
+ function membersAfter(current, upserts) {
10
+ const merged = new Map(current.map((participant) => [participant.participantId, participant]));
11
+ for (const participant of upserts)
12
+ merged.set(participant.participantId, participant);
13
+ return [...merged.values()];
14
+ }
15
+ export class AutoCloseStore {
16
+ store;
17
+ now;
18
+ constructor(store, now) {
19
+ this.store = store;
20
+ this.now = now;
21
+ }
22
+ async createRoom(room, participant, event) {
23
+ await this.store.createRoom(refreshAutoClose(room, [participant], event, this.now()), participant, event);
24
+ }
25
+ async loadRoom(roomId) {
26
+ let view = await this.store.loadRoom(roomId);
27
+ for (let attempt = 0; view && attempt < CLOSE_ATTEMPTS; attempt++) {
28
+ const reason = autoCloseDue(view, this.now());
29
+ if (!reason) {
30
+ return view;
31
+ }
32
+ try {
33
+ await this.store.apply(autoCloseRoom(view, reason, { now: this.now(), newEventId: () => newId('event') }), null);
34
+ }
35
+ catch (error) {
36
+ // Another write won the race; reload and decide again from what it left.
37
+ if (!(error instanceof ProtocolError)) {
38
+ throw error;
39
+ }
40
+ }
41
+ view = await this.store.loadRoom(roomId);
42
+ }
43
+ return view;
44
+ }
45
+ async apply(mutation, idempotency) {
46
+ const current = await this.store.loadRoom(mutation.room.roomId);
47
+ const members = membersAfter(current?.participants ?? [], mutation.upsertParticipants);
48
+ await this.store.apply({ ...mutation, room: refreshAutoClose(mutation.room, members, mutation.appendEvent, this.now()) }, idempotency);
49
+ }
50
+ async updateTurns(room, requests, expectedRevision) {
51
+ // A lease renewal writes the room without an event; keep the deadline the latest write computed.
52
+ const current = (await this.store.loadRoom(room.roomId))?.room;
53
+ const kept = current ? { lastMessageAt: current.lastMessageAt ?? null, autoCloseArmed: current.autoCloseArmed ?? false, autoCloseAt: current.autoCloseAt ?? null } : {};
54
+ await this.store.updateTurns({ ...room, ...kept }, requests, expectedRevision);
55
+ }
56
+ readEvents(roomId, after, limit) {
57
+ return this.store.readEvents(roomId, after, limit);
58
+ }
59
+ eventBySeq(roomId, seq) {
60
+ return this.store.eventBySeq(roomId, seq);
61
+ }
62
+ eventById(roomId, eventId) {
63
+ return this.store.eventById(roomId, eventId);
64
+ }
65
+ idempotencyRecord(roomId, key) {
66
+ return this.store.idempotencyRecord(roomId, key);
67
+ }
68
+ putInvite(invite) {
69
+ return this.store.putInvite(invite);
70
+ }
71
+ inviteByDigest(digest) {
72
+ return this.store.inviteByDigest(digest);
73
+ }
74
+ reserveInvite(digest, attemptId, credentialHash, expectedOccupantId) {
75
+ return this.store.reserveInvite(digest, attemptId, credentialHash, expectedOccupantId);
76
+ }
77
+ completeInvite(digest, participantId) {
78
+ return this.store.completeInvite(digest, participantId);
79
+ }
80
+ participantByCredential(roomId, credentialHash) {
81
+ return this.store.participantByCredential(roomId, credentialHash);
82
+ }
83
+ handovers(roomId) {
84
+ return this.store.handovers(roomId);
85
+ }
86
+ messageRequest(roomId, eventId) {
87
+ return this.store.messageRequest(roomId, eventId);
88
+ }
89
+ groupRequests(roomId, conversationId) {
90
+ return this.store.groupRequests(roomId, conversationId);
91
+ }
92
+ messageRequests(roomId, after, limit, recipientId) {
93
+ return this.store.messageRequests(roomId, after, limit, recipientId);
94
+ }
95
+ setLifecycle(roomId, lifecycle) {
96
+ return this.store.setLifecycle(roomId, lifecycle);
97
+ }
98
+ deleteRoom(roomId) {
99
+ return this.store.deleteRoom(roomId);
100
+ }
101
+ dueAutoCloses(now, limit) {
102
+ return this.store.dueAutoCloses(now, limit);
103
+ }
104
+ nextAutoCloseAt() {
105
+ return this.store.nextAutoCloseAt();
106
+ }
107
+ }
108
+ //# sourceMappingURL=auto-close-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-close-store.js","sourceRoot":"","sources":["../src/auto-close-store.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,iFAAiF;AACjF,gFAAgF;AAChF,0DAA0D;AAE1D,OAAO,EAAC,aAAa,EAAE,KAAK,EAAC,MAAM,qBAAqB,CAAC;AAEzD,OAAO,EAAC,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAC,MAAM,sBAAsB,CAAC;AASnF,yEAAyE;AACzE,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB,SAAS,YAAY,CAAC,OAA4B,EAAE,OAA4B;IAC5E,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,WAAW,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAC/F,KAAK,MAAM,WAAW,IAAI,OAAO;QAAE,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,aAAa,EAAE,WAAW,CAAC,CAAC;IACtF,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,OAAO,cAAc;IACM;IAAmC;IAAhE,YAA6B,KAAgB,EAAmB,GAAU;QAA7C,UAAK,GAAL,KAAK,CAAW;QAAmB,QAAG,GAAH,GAAG,CAAO;IAAG,CAAC;IAE9E,KAAK,CAAC,UAAU,CAAC,IAAgB,EAAE,WAA8B,EAAE,KAAgB;QAC/E,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,IAAI,EAAE,CAAC,WAAW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O,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC;IACzE,CAAC;IAED,YAAY,CAAC,MAAc,EAAE,SAAkC;QAC3D,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACtD,CAAC;IAED,UAAU,CAAC,MAAc;QACrB,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,aAAa,CAAC,GAAW,EAAE,KAAa;QACpC,OAAO,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC;IAED,eAAe;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,CAAC;IACxC,CAAC;CACJ"}
@@ -0,0 +1,7 @@
1
+ export * from './router.js';
2
+ export * from './service.js';
3
+ export * from './stable-json.js';
4
+ export * from './store.js';
5
+ export * from './auto-close-store.js';
6
+ export * from './request-backfill.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,uBAAuB,CAAC;AAEtC,cAAc,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from './router.js';
2
+ export * from './service.js';
3
+ export * from './stable-json.js';
4
+ export * from './store.js';
5
+ export * from './auto-close-store.js';
6
+ export * from './request-backfill.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,uBAAuB,CAAC;AAEtC,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Upgrade only retained legacy events; no synthetic acknowledgements or replies. */
2
+ export declare const REQUEST_BACKFILL_SQL = "\nINSERT OR IGNORE INTO message_requests(event_id,room_id,seq,received_at,response_event_id,requires_reply,body)\nSELECT json_extract(body,'$.eventId'),room_id,seq,NULL,NULL,\n CASE WHEN json_extract(body,'$.replyTo') IS NULL THEN 1 ELSE 0 END,\n json_object('roomId',room_id,'eventId',json_extract(body,'$.eventId'),'seq',seq,\n 'from',json_extract(body,'$.senderId'),'to',json_extract(body,'$.recipientId'),\n 'text',json_extract(body,'$.payload.text'),'at',json_extract(body,'$.at'),\n 'requiresReply',json(CASE WHEN json_extract(body,'$.replyTo') IS NULL THEN 'true' ELSE 'false' END),\n 'receivedAt',NULL,'responseEventId',NULL,'respondedAt',NULL,'progressAt',NULL)\nFROM events WHERE json_extract(body,'$.type')='message'\nAND json_extract(body,'$.senderId') IS NOT NULL AND json_extract(body,'$.recipientId') IS NOT NULL;\nUPDATE message_requests AS r SET received_at=(\n SELECT min(json_extract(e.body,'$.at')) FROM events e WHERE e.room_id=r.room_id\n AND json_extract(e.body,'$.type')='message.received'\n AND json_extract(e.body,'$.senderId')=json_extract(r.body,'$.to')\n AND json_extract(e.body,'$.payload.eventId')=r.event_id\n);\nUPDATE message_requests SET body=json_set(body,'$.receivedAt',received_at);\nUPDATE message_requests AS r SET response_event_id=(\n SELECT json_extract(e.body,'$.eventId') FROM events e WHERE e.room_id=r.room_id\n AND json_extract(e.body,'$.type')='message'\n AND json_extract(e.body,'$.replyTo')=r.event_id\n AND json_extract(e.body,'$.senderId')=json_extract(r.body,'$.to')\n AND json_extract(e.body,'$.recipientId')=json_extract(r.body,'$.from')\n AND coalesce(json_extract(e.body,'$.payload.responseStage'),'final')='final'\n ORDER BY e.seq LIMIT 1\n);\nUPDATE message_requests AS r SET body=json_set(body,'$.responseEventId',response_event_id,'$.respondedAt',(\n SELECT json_extract(e.body,'$.at') FROM events e WHERE e.room_id=r.room_id AND json_extract(e.body,'$.eventId')=r.response_event_id LIMIT 1\n));\nINSERT OR REPLACE INTO meta(key,value) VALUES('message_requests_v1','complete');\n";
3
+ //# sourceMappingURL=request-backfill.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-backfill.d.ts","sourceRoot":"","sources":["../src/request-backfill.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,eAAO,MAAM,oBAAoB,slEA+BhC,CAAC"}
@@ -0,0 +1,34 @@
1
+ /** Upgrade only retained legacy events; no synthetic acknowledgements or replies. */
2
+ export const REQUEST_BACKFILL_SQL = `
3
+ INSERT OR IGNORE INTO message_requests(event_id,room_id,seq,received_at,response_event_id,requires_reply,body)
4
+ SELECT json_extract(body,'$.eventId'),room_id,seq,NULL,NULL,
5
+ CASE WHEN json_extract(body,'$.replyTo') IS NULL THEN 1 ELSE 0 END,
6
+ json_object('roomId',room_id,'eventId',json_extract(body,'$.eventId'),'seq',seq,
7
+ 'from',json_extract(body,'$.senderId'),'to',json_extract(body,'$.recipientId'),
8
+ 'text',json_extract(body,'$.payload.text'),'at',json_extract(body,'$.at'),
9
+ 'requiresReply',json(CASE WHEN json_extract(body,'$.replyTo') IS NULL THEN 'true' ELSE 'false' END),
10
+ 'receivedAt',NULL,'responseEventId',NULL,'respondedAt',NULL,'progressAt',NULL)
11
+ FROM events WHERE json_extract(body,'$.type')='message'
12
+ AND json_extract(body,'$.senderId') IS NOT NULL AND json_extract(body,'$.recipientId') IS NOT NULL;
13
+ UPDATE message_requests AS r SET received_at=(
14
+ SELECT min(json_extract(e.body,'$.at')) FROM events e WHERE e.room_id=r.room_id
15
+ AND json_extract(e.body,'$.type')='message.received'
16
+ AND json_extract(e.body,'$.senderId')=json_extract(r.body,'$.to')
17
+ AND json_extract(e.body,'$.payload.eventId')=r.event_id
18
+ );
19
+ UPDATE message_requests SET body=json_set(body,'$.receivedAt',received_at);
20
+ UPDATE message_requests AS r SET response_event_id=(
21
+ SELECT json_extract(e.body,'$.eventId') FROM events e WHERE e.room_id=r.room_id
22
+ AND json_extract(e.body,'$.type')='message'
23
+ AND json_extract(e.body,'$.replyTo')=r.event_id
24
+ AND json_extract(e.body,'$.senderId')=json_extract(r.body,'$.to')
25
+ AND json_extract(e.body,'$.recipientId')=json_extract(r.body,'$.from')
26
+ AND coalesce(json_extract(e.body,'$.payload.responseStage'),'final')='final'
27
+ ORDER BY e.seq LIMIT 1
28
+ );
29
+ UPDATE message_requests AS r SET body=json_set(body,'$.responseEventId',response_event_id,'$.respondedAt',(
30
+ SELECT json_extract(e.body,'$.at') FROM events e WHERE e.room_id=r.room_id AND json_extract(e.body,'$.eventId')=r.response_event_id LIMIT 1
31
+ ));
32
+ INSERT OR REPLACE INTO meta(key,value) VALUES('message_requests_v1','complete');
33
+ `;
34
+ //# sourceMappingURL=request-backfill.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request-backfill.js","sourceRoot":"","sources":["../src/request-backfill.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BnC,CAAC"}
@@ -0,0 +1,37 @@
1
+ import type { LocalRoom } from '@pairlobby/protocol';
2
+ import type { RoomService } from './service.js';
3
+ export interface RouterOptions {
4
+ service: RoomService;
5
+ /**
6
+ * Exact origins a browser may call from. A CLI sends no `Origin`, so an
7
+ * unexpected one means a web page is talking to us — including a malicious
8
+ * page reaching for loopback.
9
+ */
10
+ allowedOrigins?: string[];
11
+ /** `Host` values to accept, defeating DNS rebinding against a local server. */
12
+ allowedHosts?: string[] | ((host: string) => boolean);
13
+ /** Answers `GET /v1/server`; absent means the path is unknown. */
14
+ serverInfo?: () => ServerInfo;
15
+ /**
16
+ * Answers `GET /v1/invites/probe?prefix=`: whether any invite this server issued
17
+ * has a digest starting with that prefix. Lets a client on the same network find
18
+ * the relay behind a bare code; absent means the path is unknown.
19
+ */
20
+ inviteProbe?: (prefix: string) => Promise<boolean>;
21
+ /**
22
+ * Whether this request came from the relay's own device, its local network or its
23
+ * tailnet. Only a transport that sees the connection's real address can say; absent
24
+ * means never, so joining by name is refused.
25
+ */
26
+ peerIsLocal?: (request: Request) => boolean;
27
+ /** Open rooms that allow local joins, matching a name or all of them; answers `GET /v1/rooms/local[?name=]`. */
28
+ localRooms?: (name?: string) => Promise<LocalRoom[]>;
29
+ }
30
+ export interface ServerInfo {
31
+ /** Addresses other devices can use to reach this server; empty when it only listens on this device. */
32
+ shareUrls: string[];
33
+ /** True when a device on the same network or tailnet can find this server from an invite code alone. */
34
+ discoverable?: boolean;
35
+ }
36
+ export declare function createRouter(options: RouterOptions): (request: Request) => Promise<Response>;
37
+ //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAAY,SAAS,EAAC,MAAM,qBAAqB,CAAC;AAE9D,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,cAAc,CAAC;AAE9C,MAAM,WAAW,aAAa;IAC1B,OAAO,EAAE,WAAW,CAAC;IACrB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,+EAA+E;IAC/E,YAAY,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC;IACtD,kEAAkE;IAClE,UAAU,CAAC,EAAE,MAAM,UAAU,CAAC;IAC9B;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IACnD;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC;IAC5C,gHAAgH;IAChH,UAAU,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;CACxD;AAED,MAAM,WAAW,UAAU;IACvB,uGAAuG;IACvG,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,wGAAwG;IACxG,YAAY,CAAC,EAAE,OAAO,CAAC;CAC1B;AAID,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAkC5F"}