@celljs/ta-relay 4.18.0 → 4.20.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.
Files changed (52) hide show
  1. package/esm/common/index.d.ts +3 -0
  2. package/esm/common/index.js +3 -0
  3. package/esm/common/relay-account-client.d.ts +138 -0
  4. package/esm/common/relay-account-client.js +338 -0
  5. package/esm/common/relay-account.d.ts +137 -0
  6. package/esm/common/relay-account.js +122 -0
  7. package/esm/common/relay-accounts.d.ts +107 -0
  8. package/esm/common/relay-accounts.js +393 -0
  9. package/esm/common/relay-hubs-connection.d.ts +2 -0
  10. package/esm/common/relay-hubs-connection.js +3 -0
  11. package/esm/common/relay-hubs-identity.d.ts +24 -2
  12. package/esm/common/relay-hubs-identity.js +81 -0
  13. package/esm/common/relay-identity.d.ts +7 -0
  14. package/esm/common/relay-identity.js +8 -0
  15. package/esm/common/relay-protocol.d.ts +7 -0
  16. package/esm/common/relay-protocol.js +7 -0
  17. package/esm/node/index.d.ts +1 -0
  18. package/esm/node/index.js +1 -0
  19. package/esm/node/relay-server-accounts.d.ts +16 -0
  20. package/esm/node/relay-server-accounts.js +54 -0
  21. package/esm/node/relay-server.d.ts +5 -0
  22. package/esm/node/relay-server.js +7 -0
  23. package/esm/node/relay-store.d.ts +3 -1
  24. package/esm/node/relay-store.js +1 -0
  25. package/lib/common/index.d.ts +3 -0
  26. package/lib/common/index.js +3 -0
  27. package/lib/common/relay-account-client.d.ts +139 -0
  28. package/lib/common/relay-account-client.js +343 -0
  29. package/lib/common/relay-account.d.ts +138 -0
  30. package/lib/common/relay-account.js +125 -0
  31. package/lib/common/relay-accounts.d.ts +108 -0
  32. package/lib/common/relay-accounts.js +401 -0
  33. package/lib/common/relay-hubs-connection.d.ts +2 -0
  34. package/lib/common/relay-hubs-connection.js +3 -0
  35. package/lib/common/relay-hubs-identity.d.ts +24 -2
  36. package/lib/common/relay-hubs-identity.js +81 -0
  37. package/lib/common/relay-identity.d.ts +7 -0
  38. package/lib/common/relay-identity.js +8 -0
  39. package/lib/common/relay-protocol.d.ts +7 -0
  40. package/lib/common/relay-protocol.js +8 -1
  41. package/lib/node/index.d.ts +1 -0
  42. package/lib/node/index.js +1 -0
  43. package/lib/node/relay-server-accounts.d.ts +17 -0
  44. package/lib/node/relay-server-accounts.js +58 -0
  45. package/lib/node/relay-server.d.ts +5 -0
  46. package/lib/node/relay-server.js +7 -0
  47. package/lib/node/relay-store.d.ts +3 -1
  48. package/lib/node/relay-store.js +1 -0
  49. package/package.json +3 -3
  50. package/viewer/join.html +6 -6
  51. package/viewer/page.html +12 -12
  52. package/viewer/room.html +13 -13
@@ -1,9 +1,13 @@
1
- import type { RelayFriends } from './relay-friends.js';
2
- import { RelayHubsConnection } from './relay-hubs-connection.js';
1
+ import { type RelayFriends } from './relay-friends.js';
2
+ import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
3
3
  import type { RelayIdentityBundle, RelayIdentityImport, RelayRoomRecord } from './relay-hubs-protocol.js';
4
4
  import { type RelayIdentity, type RelayPublicIdentity } from './relay-identity.js';
5
5
  export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
6
6
  protected abstract readonly book: RelayFriends;
7
+ abstract setState(roomId: string, key: string, value: unknown, version?: number): Promise<{
8
+ ok: boolean;
9
+ version: number;
10
+ }>;
7
11
  /** The name others see; the owners of rooms this Ta is in hear it. */
8
12
  setName(name: string): Promise<RelayPublicIdentity>;
9
13
  /**
@@ -11,6 +15,24 @@ export declare abstract class RelayHubsIdentity extends RelayHubsConnection {
11
15
  * look that is none. The owners of rooms this Ta is in hear it, as a name.
12
16
  */
13
17
  setLook(look: string | undefined): Promise<RelayPublicIdentity>;
18
+ /**
19
+ * The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
20
+ * in the rooms this Ta is in now, and in those it connects to later.
21
+ */
22
+ setPicture(picture: string | null): Promise<RelayIdentity>;
23
+ /**
24
+ * Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
25
+ * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
26
+ */
27
+ protected keepPicture(live: Live): Promise<void>;
28
+ /**
29
+ * The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
30
+ * what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
31
+ */
32
+ pictures(): Promise<{
33
+ self: string;
34
+ pictures: Record<string, string>;
35
+ }>;
14
36
  /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
15
37
  protected changeSelf(identity: RelayIdentity): Promise<RelayPublicIdentity>;
16
38
  /** What makes this Ta who it is in rooms, private keys and all (RelayIdentityBundle): for the user's own computers alone. */
@@ -1,6 +1,8 @@
1
+ import { RelayFriendsUtil } from './relay-friends.js';
1
2
  import { Live, RelayHubsConnection } from './relay-hubs-connection.js';
2
3
  import { RelayHubsUtil } from './relay-hubs-util.js';
3
4
  import { RelayIdentityUtil } from './relay-identity.js';
5
+ import { RELAY_PICTURE_KEY } from './relay-protocol.js';
4
6
  export class RelayHubsIdentity extends RelayHubsConnection {
5
7
  /** The name others see; the owners of rooms this Ta is in hear it. */
6
8
  async setName(name) {
@@ -19,6 +21,85 @@ export class RelayHubsIdentity extends RelayHubsConnection {
19
21
  const next = RelayIdentityUtil.withLook(identity, look);
20
22
  return next.look === identity.look ? RelayIdentityUtil.publicOf(identity) : this.changeSelf(next);
21
23
  }
24
+ /**
25
+ * The picture others see of the user (RelayIdentity.picture), or with null none, the dinosaur of the look again: put
26
+ * in the rooms this Ta is in now, and in those it connects to later.
27
+ */
28
+ async setPicture(picture) {
29
+ var _a;
30
+ if (picture !== null && !RelayIdentityUtil.isPicture(picture)) {
31
+ throw new Error('A picture is a data URL of a PNG, JPEG, WebP or SVG, of 16 KB at most.');
32
+ }
33
+ const identity = { ...(await this.identity()) };
34
+ if (((_a = identity.picture) !== null && _a !== void 0 ? _a : null) !== picture) {
35
+ if (picture === null) {
36
+ delete identity.picture;
37
+ }
38
+ else {
39
+ identity.picture = picture;
40
+ }
41
+ this.options.store.write('identity', identity);
42
+ this.own = identity;
43
+ // The pages draw the user by it, and read it again.
44
+ this.emit({ type: 'rooms' });
45
+ }
46
+ for (const live of this.live.values()) {
47
+ void this.keepPicture(live).catch(() => undefined);
48
+ }
49
+ return identity;
50
+ }
51
+ /**
52
+ * Puts this Ta's picture (RelayIdentity.picture) in the state of a room it is in, or clears it there, once the room is
53
+ * ready: in a room of friends only its own, where its friends read it; not in a room joined with another identity.
54
+ */
55
+ async keepPicture(live) {
56
+ var _a, _b, _c;
57
+ const { record } = live;
58
+ if (record.status !== 'member' || record.identity || (RelayFriendsUtil.isKind(record.kind) && !record.own)) {
59
+ return;
60
+ }
61
+ await this.ready(live);
62
+ const picture = (_a = (await this.identity()).picture) !== null && _a !== void 0 ? _a : null;
63
+ const key = RELAY_PICTURE_KEY + record.member;
64
+ const kept = live.state.get(key);
65
+ if (live.record.status !== 'member' || ((_b = kept === null || kept === void 0 ? void 0 : kept.value) !== null && _b !== void 0 ? _b : null) === picture) {
66
+ return;
67
+ }
68
+ await this.setState(record.id, key, picture, (_c = kept === null || kept === void 0 ? void 0 : kept.version) !== null && _c !== void 0 ? _c : 0);
69
+ }
70
+ /**
71
+ * The pictures of the Ta this one knows from its rooms, by the id of each (RelayIdentity.picture), its own among them:
72
+ * what each member put in a room under its member id, the one a Ta put in a room it owns (its room of friends) first.
73
+ */
74
+ async pictures() {
75
+ const own = await this.identity();
76
+ const pictures = {};
77
+ const owned = new Set();
78
+ for (const live of this.live.values()) {
79
+ if (live.record.status !== 'member' || !live.roster) {
80
+ continue;
81
+ }
82
+ for (const member of live.roster.members) {
83
+ const kept = live.state.get(RELAY_PICTURE_KEY + member.member);
84
+ if (member.guest || (kept === null || kept === void 0 ? void 0 : kept.by) !== member.member || !RelayIdentityUtil.isPicture(kept.value) || owned.has(member.id)) {
85
+ continue;
86
+ }
87
+ if (!(member.id in pictures) || member.owner) {
88
+ pictures[member.id] = kept.value;
89
+ }
90
+ if (member.owner) {
91
+ owned.add(member.id);
92
+ }
93
+ }
94
+ }
95
+ if (own.picture) {
96
+ pictures[own.id] = own.picture;
97
+ }
98
+ else {
99
+ delete pictures[own.id];
100
+ }
101
+ return { self: own.id, pictures };
102
+ }
22
103
  /** Keeps the identity with a new name or look, and tells the rooms this Ta is in. */
23
104
  async changeSelf(identity) {
24
105
  var _a;
@@ -20,6 +20,11 @@ export interface RelayIdentity extends RelayPublicIdentity {
20
20
  createdAt: number;
21
21
  /** The user's other computers are this Ta too: who answers in a room is agreed first (docs/design/ta-sync.md 9). */
22
22
  shared?: true;
23
+ /**
24
+ * The picture others see of the user (RelayIdentityUtil.isPicture), instead of the dinosaur of the look: kept in the
25
+ * state of the rooms this Ta is in (RELAY_PICTURE_KEY), never in what others are given of the identity.
26
+ */
27
+ picture?: string;
23
28
  }
24
29
  /** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
25
30
  export declare namespace RelayKeys {
@@ -44,6 +49,8 @@ export declare namespace RelayIdentityUtil {
44
49
  function publicOf(identity: RelayPublicIdentity): RelayPublicIdentity;
45
50
  /** Whether it is a look as a Ta gives it: a short word of letters, digits and dashes. */
46
51
  function isLook(look: unknown): look is string;
52
+ /** Whether it is a picture as a Ta keeps it: a data URL of a PNG, JPEG, WebP or SVG, small. */
53
+ function isPicture(picture: unknown): picture is string;
47
54
  /** `value` with `look`, or without any for one that is none. */
48
55
  function withLook<T extends {
49
56
  look?: string;
@@ -1,4 +1,5 @@
1
1
  import { RelayBytes } from './relay-bytes.js';
2
+ import { RELAY_PICTURE_MAX_CHARS } from './relay-protocol.js';
2
3
  const encoder = new TextEncoder();
3
4
  const decoder = new TextDecoder();
4
5
  const subtle = () => globalThis.crypto.subtle;
@@ -96,6 +97,13 @@ export var RelayIdentityUtil;
96
97
  return typeof look === 'string' && /^[a-z][a-z0-9-]{0,31}$/.test(look);
97
98
  }
98
99
  RelayIdentityUtil.isLook = isLook;
100
+ /** Whether it is a picture as a Ta keeps it: a data URL of a PNG, JPEG, WebP or SVG, small. */
101
+ function isPicture(picture) {
102
+ return (typeof picture === 'string' &&
103
+ picture.length <= RELAY_PICTURE_MAX_CHARS &&
104
+ /^data:image\/(png|jpeg|webp|svg\+xml);base64,[A-Za-z0-9+/]+={0,2}$/.test(picture));
105
+ }
106
+ RelayIdentityUtil.isPicture = isPicture;
99
107
  /** `value` with `look`, or without any for one that is none. */
100
108
  function withLook(value, look) {
101
109
  const { look: _, ...rest } = value;
@@ -188,6 +188,13 @@ export declare const RELAY_HUB_MAX_KEYS = 4096;
188
188
  export declare const RELAY_HUB_MAX_STATE: number;
189
189
  /** Asks to join waiting for the owner, and for how long. */
190
190
  export declare const RELAY_HUB_MAX_ASKS = 20;
191
+ /**
192
+ * The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
193
+ * member its own, in the rooms of friends the owner alone (RelayHubs.keepPicture).
194
+ */
195
+ export declare const RELAY_PICTURE_KEY = "picture:";
196
+ /** Characters of a picture at most, as a data URL: a small square, a few of them in a room. */
197
+ export declare const RELAY_PICTURE_MAX_CHARS: number;
191
198
  export declare const RELAY_HUB_ASK_DAYS = 7;
192
199
  /** A room nobody used for so long is deleted. */
193
200
  export declare const RELAY_HUB_IDLE_DAYS = 90;
@@ -121,6 +121,13 @@ export const RELAY_HUB_MAX_KEYS = 4096;
121
121
  export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
122
122
  /** Asks to join waiting for the owner, and for how long. */
123
123
  export const RELAY_HUB_MAX_ASKS = 20;
124
+ /**
125
+ * The key of the state of a room a member keeps its picture under (RelayIdentity.picture), its member id after it: each
126
+ * member its own, in the rooms of friends the owner alone (RelayHubs.keepPicture).
127
+ */
128
+ export const RELAY_PICTURE_KEY = 'picture:';
129
+ /** Characters of a picture at most, as a data URL: a small square, a few of them in a room. */
130
+ export const RELAY_PICTURE_MAX_CHARS = 16 * 1024;
124
131
  export const RELAY_HUB_ASK_DAYS = 7;
125
132
  /** A room nobody used for so long is deleted. */
126
133
  export const RELAY_HUB_IDLE_DAYS = 90;
@@ -5,6 +5,7 @@ export * from './relay-host.js';
5
5
  export * from './relay-hub-files.js';
6
6
  export * from './relay-memory-storage.js';
7
7
  export * from './relay-server.js';
8
+ export * from './relay-server-accounts.js';
8
9
  export * from './relay-store.js';
9
10
  export * from './relay-viewer.js';
10
11
  export * from './relays-file.js';
package/esm/node/index.js CHANGED
@@ -5,6 +5,7 @@ export * from './relay-host.js';
5
5
  export * from './relay-hub-files.js';
6
6
  export * from './relay-memory-storage.js';
7
7
  export * from './relay-server.js';
8
+ export * from './relay-server-accounts.js';
8
9
  export * from './relay-store.js';
9
10
  export * from './relay-viewer.js';
10
11
  export * from './relays-file.js';
@@ -0,0 +1,16 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import { type RelayAccountProvider, RelayAccounts, type RelayAccountsOptions } from '../common/relay-accounts.js';
3
+ /**
4
+ * The account service of `ta relay` (docs/design/ta-account.md), as the Worker answers it: the book of each account in
5
+ * a directory of its own under `data`, or in memory without one.
6
+ */
7
+ export declare class RelayServerAccounts {
8
+ protected readonly accounts: RelayAccounts;
9
+ /** `authenticate`: a gateway's, whose sign-in gives the tokens (RelayAccountsOptions.authenticate). */
10
+ constructor(data: string | undefined, options?: Pick<RelayAccountsOptions, 'authenticate'> & {
11
+ providers?: RelayAccountProvider[];
12
+ });
13
+ /** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
14
+ serve(request: IncomingMessage, response: ServerResponse): void;
15
+ protected answer(request: IncomingMessage, response: ServerResponse): Promise<void>;
16
+ }
@@ -0,0 +1,54 @@
1
+ import { join } from 'node:path';
2
+ import { RelayAccountBooks, RelayAccounts, RelayGitHubAccounts } from '../common/relay-accounts.js';
3
+ import { RelayBytes } from '../common/relay-bytes.js';
4
+ import { FileRelayStorage } from './relay-file-storage.js';
5
+ import { MemoryHubStorage } from './relay-memory-storage.js';
6
+ /** What a call of the account service may hold. */
7
+ const MAX_BYTES = 64 * 1024;
8
+ /**
9
+ * The account service of `ta relay` (docs/design/ta-account.md), as the Worker answers it: the book of each account in
10
+ * a directory of its own under `data`, or in memory without one.
11
+ */
12
+ export class RelayServerAccounts {
13
+ /** `authenticate`: a gateway's, whose sign-in gives the tokens (RelayAccountsOptions.authenticate). */
14
+ constructor(data, options = {}) {
15
+ var _a;
16
+ const books = new RelayAccountBooks(account => data ? new FileRelayStorage(join(data, 'accounts', RelayBytes.toBase64(new TextEncoder().encode(account)))) : new MemoryHubStorage());
17
+ const providers = (_a = options.providers) !== null && _a !== void 0 ? _a : (options.authenticate ? [] : [new RelayGitHubAccounts()]);
18
+ this.accounts = new RelayAccounts({ book: account => books.book(account), providers, authenticate: options.authenticate });
19
+ }
20
+ /** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
21
+ serve(request, response) {
22
+ this.answer(request, response).catch(() => {
23
+ if (!response.headersSent) {
24
+ response.writeHead(400);
25
+ }
26
+ response.end();
27
+ });
28
+ }
29
+ async answer(request, response) {
30
+ var _a;
31
+ const chunks = [];
32
+ let size = 0;
33
+ for await (const chunk of request) {
34
+ size += chunk.length;
35
+ if (size > MAX_BYTES) {
36
+ response.writeHead(413, { connection: 'close' }).end('Too large.\n');
37
+ return;
38
+ }
39
+ chunks.push(chunk);
40
+ }
41
+ const headers = new Headers();
42
+ for (const [name, value] of Object.entries(request.headers)) {
43
+ if (typeof value === 'string') {
44
+ headers.set(name, value);
45
+ }
46
+ }
47
+ const method = (_a = request.method) !== null && _a !== void 0 ? _a : 'GET';
48
+ const body = chunks.length && method !== 'GET' && method !== 'HEAD' ? Buffer.concat(chunks) : undefined;
49
+ const answer = await this.accounts.handle(new Request(`http://relay${request.url}`, { method, headers, body }));
50
+ response.writeHead(answer.status, Object.fromEntries(answer.headers));
51
+ response.end(Buffer.from(await answer.arrayBuffer()));
52
+ }
53
+ }
54
+ //# sourceMappingURL=relay-server-accounts.js.map
@@ -2,12 +2,14 @@ import { type IncomingMessage, type Server, type ServerResponse } from 'node:htt
2
2
  import type { Duplex } from 'node:stream';
3
3
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
4
4
  import { type TaDeploymentEnv } from '@celljs/ta-protocol/lib/common/deployment-protocol';
5
+ import type { RelayAccountProvider } from '../common/relay-accounts.js';
5
6
  import { RelayBlobs } from '../common/relay-blobs.js';
6
7
  import { RelayHub, type RelayHubWho } from '../common/relay-hub.js';
7
8
  import { RelayRoom } from '../common/relay-room.js';
8
9
  import { RelaySpot, type RelaySpotSocket, type RelaySpotWho } from '../common/relay-spot.js';
9
10
  import { FileRelayStorage } from './relay-file-storage.js';
10
11
  import { MemoryBlobStorage, MemoryHubStorage } from './relay-memory-storage.js';
12
+ import { RelayServerAccounts } from './relay-server-accounts.js';
11
13
  export interface RelayServerOptions {
12
14
  host?: string;
13
15
  port?: number;
@@ -29,6 +31,8 @@ export interface RelayServerOptions {
29
31
  TA_PUBLIC_URL?: string;
30
32
  };
31
33
  version?: string;
34
+ /** The ways to sign in to accounts (docs/design/ta-account.md): GitHub with Ta's own app, unless given. */
35
+ accounts?: RelayAccountProvider[];
32
36
  }
33
37
  interface Room {
34
38
  secretHash: string;
@@ -71,6 +75,7 @@ export declare class RelayServer {
71
75
  /** The address each connection to a spot came from. */
72
76
  protected readonly spotAddresses: WeakMap<WebSocketConnection, string>;
73
77
  protected server?: Server;
78
+ protected readonly accounts: RelayServerAccounts;
74
79
  constructor(options?: RelayServerOptions);
75
80
  /** What it is part of, and whether it runs and can keep what it holds: for anyone, and the probes of a platform. */
76
81
  protected describe(path: string, request: IncomingMessage, response: ServerResponse): void;
@@ -4,6 +4,7 @@ import { createServer } from 'node:http';
4
4
  import { join } from 'node:path';
5
5
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
6
6
  import { TA_WELL_KNOWN_PATH, TaDeploymentUtil } from '@celljs/ta-protocol/lib/common/deployment-protocol';
7
+ import { RELAY_ACCOUNT_PATH } from '../common/relay-account.js';
7
8
  import { RelayBlobs } from '../common/relay-blobs.js';
8
9
  import { RelayHub } from '../common/relay-hub.js';
9
10
  import { RELAY_INFO_PATH, RelayInfoUtil } from '../common/relay-info.js';
@@ -14,6 +15,7 @@ import { RELAY_JOIN_PATH, RELAY_WEB_PATH } from '../common/relay-team.js';
14
15
  import { RelayWebUtil } from '../common/relay-web.js';
15
16
  import { FileRelayStorage } from './relay-file-storage.js';
16
17
  import { MemoryBlobStorage, MemoryHubStorage } from './relay-memory-storage.js';
18
+ import { RelayServerAccounts } from './relay-server-accounts.js';
17
19
  import { RelayViewer } from './relay-viewer.js';
18
20
  const WEBSOCKET = { maxPayload: 32 * 1024 * 1024, heartbeat: 30000 };
19
21
  const peer = (connection) => ({ send: text => connection.send(text), close: (code, reason) => connection.close(code, reason) });
@@ -35,6 +37,7 @@ export class RelayServer {
35
37
  this.spotTries = new Map();
36
38
  /** The address each connection to a spot came from. */
37
39
  this.spotAddresses = new WeakMap();
40
+ this.accounts = new RelayServerAccounts(options.data, { providers: options.accounts });
38
41
  if (options.data) {
39
42
  mkdirSync(options.data, { recursive: true, mode: 0o700 });
40
43
  try {
@@ -95,6 +98,10 @@ export class RelayServer {
95
98
  response.end(JSON.stringify(info));
96
99
  return;
97
100
  }
101
+ if (path.startsWith(RELAY_ACCOUNT_PATH)) {
102
+ this.accounts.serve(request, response);
103
+ return;
104
+ }
98
105
  if (path.startsWith(RELAY_BLOB_PATH)) {
99
106
  this.blob(path.slice(RELAY_BLOB_PATH.length), request, response).catch(() => {
100
107
  // A request aborted or that failed: an unhandled rejection would end the relay.
@@ -12,6 +12,8 @@ export interface RelayDevice {
12
12
  createdAt: number;
13
13
  /** One let in by the user, not the user's own device: the principal its calls are made as (docs/design/ta-team.md). */
14
14
  member?: string;
15
+ /** A device of the user's account (docs/design/ta-account.md), by its id there: paired by the account, unpaired with it. */
16
+ account?: string;
15
17
  /** No longer reaches this ta serve from then on: a link of invitation. */
16
18
  expiresAt?: number;
17
19
  }
@@ -152,7 +154,7 @@ export declare class RelayStore {
152
154
  * each relay that has one (docs/design/ta-relays.md). `member`: a device of one the user let in, not their own;
153
155
  * `expiresAt`: it reaches this ta serve until then. Devices expired are dropped as a new one is added.
154
156
  */
155
- pair(relay: string, deviceName: string, options?: Pick<RelayDevice, 'member' | 'expiresAt'>): RelayPairing;
157
+ pair(relay: string, deviceName: string, options?: Pick<RelayDevice, 'member' | 'expiresAt' | 'account'>): RelayPairing;
156
158
  /** Whether ta serve keeps the computer awake for the devices (RelayConfig.keepAwake). */
157
159
  setKeepAwake(on: boolean): void;
158
160
  /** False when there was no such device. */
@@ -218,6 +218,7 @@ export class RelayStore {
218
218
  key: randomBytes(32).toString('base64url'),
219
219
  createdAt: now,
220
220
  ...(options.member ? { member: options.member } : {}),
221
+ ...(options.account ? { account: options.account } : {}),
221
222
  ...(options.expiresAt !== undefined ? { expiresAt: options.expiresAt } : {}),
222
223
  };
223
224
  config.devices = [...config.devices.filter(one => one.expiresAt === undefined || one.expiresAt > now), device];
@@ -1,4 +1,7 @@
1
1
  export * from './qr-code';
2
+ export * from './relay-account';
3
+ export * from './relay-account-client';
4
+ export * from './relay-accounts';
2
5
  export * from './relay-blobs';
3
6
  export * from './relay-bytes';
4
7
  export * from './relay-card';
@@ -15,6 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./qr-code"), exports);
18
+ __exportStar(require("./relay-account"), exports);
19
+ __exportStar(require("./relay-account-client"), exports);
20
+ __exportStar(require("./relay-accounts"), exports);
18
21
  __exportStar(require("./relay-blobs"), exports);
19
22
  __exportStar(require("./relay-bytes"), exports);
20
23
  __exportStar(require("./relay-card"), exports);
@@ -0,0 +1,139 @@
1
+ import { type AccountCalls, type AccountCircleState, type AccountDevice, type AccountDeviceKind, type AccountName, type AccountRoster, type AccountSignInPoll, type AccountSignInStart } from './relay-account';
2
+ import { type RelayIdentity } from './relay-identity';
3
+ /** The service said no: `status` 401 is a token no longer good, 403 this device removed. */
4
+ export declare class AccountCallError extends Error {
5
+ readonly status: number;
6
+ constructor(status: number, message: string);
7
+ }
8
+ /** The account service of a relay or a gateway, over fetch: the same in ta serve and a page. */
9
+ export declare namespace AccountService {
10
+ /** How long the service is waited for: one out of reach (a company's network, away from it) answers nothing at all. */
11
+ const TIMEOUT_MS = 8000;
12
+ function base(service: string): string;
13
+ function providers(service: string): Promise<{
14
+ id: string;
15
+ name: string;
16
+ }[]>;
17
+ function start(service: string, provider: string): Promise<AccountSignInStart>;
18
+ function poll(service: string, provider: string, handle: string): Promise<AccountSignInPoll>;
19
+ /**
20
+ * The ways to sign in to an account: those of the relay (GitHub), and those of the gateways of deployments (a
21
+ * company's accounts), whose own sign-in gives the token; a gateway without the account service is left out.
22
+ */
23
+ function ways(relay: string, gateways?: string[]): Promise<AccountWay[]>;
24
+ /** Begins signing in by `way`: the code the user enters on the page of `url`. */
25
+ function begin(way: AccountWay, deviceName: string): Promise<AccountSignInStart>;
26
+ /** One look at a sign-in begun by `way`. */
27
+ function check(way: AccountWay, handle: string): Promise<AccountSignInPoll>;
28
+ /** The calls of an account, with a token of the service (or of the gateway it is part of). */
29
+ function calls(service: string, token: string): AccountCalls;
30
+ }
31
+ /** A way to sign in to an account: a provider of a relay, or of a gateway (`gateway`), whose token is the account's. */
32
+ export interface AccountWay {
33
+ id: string;
34
+ name: string;
35
+ service: string;
36
+ gateway?: true;
37
+ }
38
+ /** What a device keeps of its account (`account.json` of ta serve, localStorage of a page): its keys and whom it trusts. */
39
+ export interface AccountKept {
40
+ /** The relay or the gateway the account is on. */
41
+ service: string;
42
+ token: string;
43
+ account: AccountName;
44
+ kind: AccountDeviceKind;
45
+ /** This device's own keys, made for the account: not the identity others know the user by. */
46
+ self: RelayIdentity;
47
+ circle: AccountCircleState;
48
+ /** The computers that sent this device a way in: their account device id, the pairing link. */
49
+ pairings: Record<string, string>;
50
+ }
51
+ /** A device of the account as a device sees it. */
52
+ export interface AccountDeviceView {
53
+ id: string;
54
+ kind: AccountDeviceKind;
55
+ name: string;
56
+ self: boolean;
57
+ /** Let in, and trusted by this device. */
58
+ trusted: boolean;
59
+ /** Waiting to be let in: `code` is what it shows, for the user to compare. */
60
+ waiting: boolean;
61
+ code?: string;
62
+ online: boolean;
63
+ seenAt: number;
64
+ addedAt: number;
65
+ }
66
+ export interface AccountView {
67
+ account: AccountName;
68
+ service: string;
69
+ self: string;
70
+ /** This device waits to be let in: `code` is what the device that lets it in shows too. */
71
+ waiting: boolean;
72
+ code: string;
73
+ devices: AccountDeviceView[];
74
+ }
75
+ /** What a device of an account does on its side, besides keeping `AccountKept`. */
76
+ export interface AccountHooks {
77
+ save(kept: AccountKept): void | Promise<void>;
78
+ name(): string;
79
+ /** A computer: where it hangs on a relay, so that devices reach it. */
80
+ host?(): {
81
+ relay: string;
82
+ id: string;
83
+ } | undefined;
84
+ /** A computer: a way in for a trusted device of the account (a pairing link), made once. */
85
+ pair?(device: AccountDevice): Promise<string>;
86
+ /** A computer: the devices of the account it made a way in for. */
87
+ paired?(): string[];
88
+ /** A computer: no way in any more for this device of the account. */
89
+ unpair?(device: string): void | Promise<void>;
90
+ /** A way into a computer of the account came: `link` pairs with it. */
91
+ pairing?(from: AccountDevice, link: string): void | Promise<void>;
92
+ /** A computer that sent a way in is no longer trusted, or removed. */
93
+ unpaired?(from: string): void | Promise<void>;
94
+ /** What a package on another device of the account said (AccountAgent.tell), new or changed. */
95
+ told?(from: AccountDevice, topic: string, value: unknown): void | Promise<void>;
96
+ /** This device was removed from the account, or its token no longer works: it is signed out. */
97
+ signedOut?(reason: 'removed' | 'token'): void | Promise<void>;
98
+ }
99
+ /**
100
+ * A device of an account (docs/design/ta-account.md 4): says it is there, keeps whom it trusts, lets others in, makes and
101
+ * takes ways into computers through sealed mail. The same in ta serve and a page; what is particular comes in hooks.
102
+ */
103
+ export declare class AccountAgent {
104
+ protected kept: AccountKept;
105
+ protected readonly hooks: AccountHooks;
106
+ protected roster?: AccountRoster;
107
+ protected readonly calls: AccountCalls;
108
+ /** What was told last, by `<from>|<topic>`: a mail is kept until replaced, told once each time it changes. */
109
+ protected heard: Map<string, string>;
110
+ constructor(kept: AccountKept, hooks: AccountHooks, calls?: AccountCalls);
111
+ /** A new device of `account`: its own keys, trusting itself alone. */
112
+ static create(service: string, token: string, account: AccountName, kind: AccountDeviceKind, name: string): Promise<AccountKept>;
113
+ get state(): AccountKept;
114
+ protected save(next: AccountKept): Promise<void>;
115
+ protected guard<T>(run: () => Promise<T>): Promise<T>;
116
+ /** One round: be in the roster, trust whom to, let the trusted in to this computer, take ways into others. */
117
+ sync(): Promise<AccountView>;
118
+ /** Let in by `me.endorsement.by`: whom it trusts came sealed, signed by it; the device it let in trusts them too. */
119
+ protected joinCircle(roster: AccountRoster, me: AccountDevice): Promise<void>;
120
+ /**
121
+ * The trusted devices in the roster, with the keys this device trusts them by: the service may list other keys under
122
+ * a trusted id, and mail is sealed to and checked against what was trusted, never what the service says now.
123
+ */
124
+ protected trusted(roster: AccountRoster): AccountDevice[];
125
+ /** Ways in: made for the trusted (a computer), dropped for the rest; taken from trusted computers. */
126
+ protected ways(roster: AccountRoster): Promise<void>;
127
+ view(now?: number): Promise<AccountView>;
128
+ /** The devices of the account this one trusts, itself aside, as the service last listed them. */
129
+ devices(): AccountDevice[];
130
+ /** Says `value` to a trusted device of the account, sealed for it: a package on it hears it by `topic`. */
131
+ tell(to: string, topic: string, value: unknown): Promise<void>;
132
+ /** Lets a waiting device in: signs it, and sends it whom this device trusts. */
133
+ approve(id: string): Promise<AccountView>;
134
+ /** Removes a device (one waiting that is not the user's, or one lost): every device drops it. */
135
+ remove(id: string): Promise<AccountView>;
136
+ /** Leaves the account: removed from it, its token dropped. */
137
+ signOut(): Promise<void>;
138
+ }
139
+ //# sourceMappingURL=relay-account-client.d.ts.map