@banou/social 0.0.0-stage → 0.0.1
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 +21 -0
- package/README.md +382 -2
- package/build/index.js +429 -0
- package/dist/index.d.mts +336 -0
- package/dist/index.mjs +284 -0
- package/dist/names.d.mts +14 -0
- package/dist/names.mjs +55 -0
- package/package.json +66 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
//#region src/lib/errors.d.ts
|
|
2
|
+
/** Why social cannot answer. Callers branch on the code, never on the message. */
|
|
3
|
+
type Availability = 'ready' |
|
|
4
|
+
/** no FKN transport on this page, or the package would not boot */
|
|
5
|
+
'unavailable' |
|
|
6
|
+
/** this library is older than the package still serves */
|
|
7
|
+
'outdated' |
|
|
8
|
+
/** the HOST APP has no FKN connection, so no bearer can be minted for the package */
|
|
9
|
+
'app-not-connected' | 'signed-out' |
|
|
10
|
+
/** the account's encryption key is not held in this storage partition */
|
|
11
|
+
'locked' |
|
|
12
|
+
/** the account never enrolled, or was reset and has not re-enrolled */
|
|
13
|
+
'no-keys' |
|
|
14
|
+
/** sealed objects will not open: a key reset happened */
|
|
15
|
+
'reset' |
|
|
16
|
+
/** signed in, but no social identity has been set up yet */
|
|
17
|
+
'no-identity';
|
|
18
|
+
type SocialErrorCode = Availability | 'declined' | 'offline' | 'quota' | 'denied' | 'timeout' | 'full' | 'invalid' | 'limit' |
|
|
19
|
+
/** this platform cannot do it yet, for example handle search */
|
|
20
|
+
'unsupported';
|
|
21
|
+
type SocialError = Error & {
|
|
22
|
+
code: SocialErrorCode;
|
|
23
|
+
};
|
|
24
|
+
export declare const isSocialError: (value: unknown) => value is SocialError;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/lib/protocol.d.ts
|
|
27
|
+
/** What the package answers before anything else, so the library knows what it may call. */
|
|
28
|
+
type Hello = {
|
|
29
|
+
/** the level the package speaks */
|
|
30
|
+
protocol: number;
|
|
31
|
+
/** the oldest library level it still serves */
|
|
32
|
+
min: number;
|
|
33
|
+
};
|
|
34
|
+
/** What `openInvite` answers. `not-here`: only the site may stage an invite. */
|
|
35
|
+
type OpenInviteAnswer = {
|
|
36
|
+
ok: true;
|
|
37
|
+
} | {
|
|
38
|
+
ok: false;
|
|
39
|
+
reason: 'malformed' | 'not-here';
|
|
40
|
+
};
|
|
41
|
+
/** What `openAdd` answers. `malformed`: the fragment is not a name. `not-here`: only the site may stage one. */
|
|
42
|
+
type OpenAddAnswer = {
|
|
43
|
+
ok: true;
|
|
44
|
+
} | {
|
|
45
|
+
ok: false;
|
|
46
|
+
reason: 'malformed' | 'not-here';
|
|
47
|
+
};
|
|
48
|
+
/** What `openEdit` answers. `not-here`: only the site may open the editor. */
|
|
49
|
+
type OpenEditAnswer = {
|
|
50
|
+
ok: true;
|
|
51
|
+
} | {
|
|
52
|
+
ok: false;
|
|
53
|
+
reason: 'not-here';
|
|
54
|
+
};
|
|
55
|
+
/** A name's record as the site read it: the name, room id and owner, message text, and core `<name>` proof when `text` came from `<page>`. */
|
|
56
|
+
type SiteRecord = {
|
|
57
|
+
name: string;
|
|
58
|
+
roomId: string;
|
|
59
|
+
owner: string;
|
|
60
|
+
text: string;
|
|
61
|
+
core?: {
|
|
62
|
+
roomId: string;
|
|
63
|
+
owner: string;
|
|
64
|
+
text: string;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
/** A thing the person picked for their page's Favourites: its title, where it is, and its picture. */
|
|
68
|
+
type Favourite = {
|
|
69
|
+
title: string;
|
|
70
|
+
link: string;
|
|
71
|
+
image?: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* What `openFriends` answers: the Friends part of the record, opened. `none`: there is none, or the
|
|
75
|
+
* person signed in is neither the holder nor one of the holder's friends, or their key is locked.
|
|
76
|
+
* `not-here`: only the site may ask.
|
|
77
|
+
*/
|
|
78
|
+
type OpenFriendsAnswer = {
|
|
79
|
+
ok: true;
|
|
80
|
+
profile: {
|
|
81
|
+
displayName: string;
|
|
82
|
+
bio: string;
|
|
83
|
+
links: string[];
|
|
84
|
+
avatar?: {
|
|
85
|
+
url: string;
|
|
86
|
+
key: string;
|
|
87
|
+
} | null;
|
|
88
|
+
background?: {
|
|
89
|
+
url: string;
|
|
90
|
+
key: string;
|
|
91
|
+
} | null;
|
|
92
|
+
} | null;
|
|
93
|
+
sections: string[];
|
|
94
|
+
feed: unknown[];
|
|
95
|
+
favourites?: unknown[];
|
|
96
|
+
comments?: unknown[];
|
|
97
|
+
panels?: unknown[];
|
|
98
|
+
panelOrder?: string[];
|
|
99
|
+
} | {
|
|
100
|
+
ok: false;
|
|
101
|
+
reason: 'not-here' | 'none';
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* What an app posts to the person's page: the kind of thing that happened, the title of what it
|
|
105
|
+
* happened to, a short line after it ("Episode 5"), where the thing is (https, and on the app's own
|
|
106
|
+
* origin when the app is one), its artwork, and the app's own stable id for it, so entries about one
|
|
107
|
+
* thing draw together. The package normalises the text to NFC, checks every field, and stamps the
|
|
108
|
+
* entry with an id, the app and the time.
|
|
109
|
+
*/
|
|
110
|
+
type Post = {
|
|
111
|
+
kind: 'started' | 'watched' | 'completed' | 'rated';
|
|
112
|
+
title: string;
|
|
113
|
+
detail?: string;
|
|
114
|
+
link: string;
|
|
115
|
+
image?: string;
|
|
116
|
+
subject?: string;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Why a posting call did nothing. `not-here`: the connection is not one fkn.app's broker made, so it
|
|
120
|
+
* names no app. `no-name`: no FKN Social name yet. `locked`: the account's key is locked here, so
|
|
121
|
+
* nothing was read and no card raised. `unavailable`: anything else that stopped it. `outdated` and
|
|
122
|
+
* `unsupported`: this library is older than the package serves, or the package older than this library.
|
|
123
|
+
*/
|
|
124
|
+
type PublishingRefusal = 'not-here' | 'no-name' | 'locked' | 'unavailable' | 'outdated' | 'unsupported';
|
|
125
|
+
/**
|
|
126
|
+
* Where an app stands with the person. `allowed`: its posts land, of these kinds. `asked`: it asked and
|
|
127
|
+
* the person has not answered on social.fkn.app. `off`: it never asked, or the person said Not now or
|
|
128
|
+
* Stop, so it may ask again.
|
|
129
|
+
*/
|
|
130
|
+
type PublishingState = {
|
|
131
|
+
state: 'allowed';
|
|
132
|
+
kinds: string[];
|
|
133
|
+
} | {
|
|
134
|
+
state: 'asked';
|
|
135
|
+
} | {
|
|
136
|
+
state: 'off';
|
|
137
|
+
} | {
|
|
138
|
+
state: PublishingRefusal;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* What `publish` answers. `not-allowed`: not Allowed, the kind not allowed, or the person's Activity is
|
|
142
|
+
* Off. `invalid`: a field the package refuses. `too-many`: 10 entries from this app in the last hour.
|
|
143
|
+
* A refused post is dropped.
|
|
144
|
+
*/
|
|
145
|
+
type PublishAnswer = {
|
|
146
|
+
ok: true;
|
|
147
|
+
id: string;
|
|
148
|
+
} | {
|
|
149
|
+
ok: false;
|
|
150
|
+
reason: 'not-allowed' | 'invalid' | 'too-many' | PublishingRefusal;
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* What an edit of the page from the site answers. `not-here`: only the site may ask. `none`: the name is
|
|
154
|
+
* not the one the identity holds. `full`: at most 8 favourites. `unavailable`: anything else that
|
|
155
|
+
* stopped it, nothing changed.
|
|
156
|
+
*/
|
|
157
|
+
type EditAnswer = {
|
|
158
|
+
ok: true;
|
|
159
|
+
} | {
|
|
160
|
+
ok: false;
|
|
161
|
+
reason: 'not-here' | 'none' | 'full' | 'unavailable';
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* What a comment left on a name's page from the site answers. `not-friend`: the page is not held by one
|
|
165
|
+
* of the person's friends. `invalid`: empty, or past 500 characters. `closed`: their inbox keeps nothing
|
|
166
|
+
* now. `full`: this account has as much waiting there as the inbox takes from one. `none`: the person has
|
|
167
|
+
* no name yet. `unavailable`: anything else that stopped it, nothing sent.
|
|
168
|
+
*/
|
|
169
|
+
type CommentAnswer = {
|
|
170
|
+
ok: true;
|
|
171
|
+
} | {
|
|
172
|
+
ok: false;
|
|
173
|
+
reason: 'not-here' | 'not-friend' | 'invalid' | 'closed' | 'full' | 'none' | 'unavailable';
|
|
174
|
+
};
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/lib/client.d.ts
|
|
177
|
+
/** One room participant, as an app renders it. */
|
|
178
|
+
type Participant = {
|
|
179
|
+
memberId: string;
|
|
180
|
+
status: 'silent' | 'unverified' | 'verified' | 'friend' | 'rekeyed';
|
|
181
|
+
/** null while nothing verified; an app then shows `defaultName(memberId)`, from the `./names` entry */
|
|
182
|
+
identity: {
|
|
183
|
+
handle: string;
|
|
184
|
+
name: string;
|
|
185
|
+
displayName: string;
|
|
186
|
+
avatar: string | null;
|
|
187
|
+
} | null;
|
|
188
|
+
self: boolean;
|
|
189
|
+
};
|
|
190
|
+
/** What the app hands social, so social never needs the app's own `Room` object. */
|
|
191
|
+
type RoomBinding = {
|
|
192
|
+
roomId: string;
|
|
193
|
+
selfMemberId: string;
|
|
194
|
+
/** how social gets one opaque frame onto the wire; the app wraps it in its own protocol */
|
|
195
|
+
send: (frame: string) => void | Promise<void>;
|
|
196
|
+
/**
|
|
197
|
+
* Optional and strongly preferred. With the room's invite the package joins the room itself, lands
|
|
198
|
+
* on the same member id, and reads the server-stamped sender rather than the one this app reports.
|
|
199
|
+
*/
|
|
200
|
+
observe?: string;
|
|
201
|
+
};
|
|
202
|
+
type Presence = {
|
|
203
|
+
participants: () => Participant[];
|
|
204
|
+
of: (memberId: string) => Participant;
|
|
205
|
+
on: (listener: (participants: Participant[]) => void) => () => void;
|
|
206
|
+
/** hand social a frame the app received; true when social claimed it. Never throws. */
|
|
207
|
+
ingest: (from: string, frame: string, at: number, seq: number) => boolean;
|
|
208
|
+
memberJoined: (memberId: string) => void;
|
|
209
|
+
memberLeft: (memberId: string) => void;
|
|
210
|
+
announce: () => Promise<void>;
|
|
211
|
+
detach: () => Promise<void>;
|
|
212
|
+
};
|
|
213
|
+
/** What the package exposes across the origin hop. Injected, so the library is testable without one. */
|
|
214
|
+
type Hub = {
|
|
215
|
+
hello: () => Promise<Hello>;
|
|
216
|
+
/** stages an invite fragment (`location.hash.slice(1)` on `/i`) in the manager; the site's own call */
|
|
217
|
+
openInvite: (fragment: string) => Promise<OpenInviteAnswer>;
|
|
218
|
+
/** stages the name a page's Request friend names (`location.hash.slice(1)` on `/a`) in the manager; the site's own call */
|
|
219
|
+
openAdd: (fragment: string) => Promise<OpenAddAnswer>;
|
|
220
|
+
/** opens Page layout in the manager, which a name's Edit (`/e`) asks for; the site's own call */
|
|
221
|
+
openEdit: () => Promise<OpenEditAnswer>;
|
|
222
|
+
/** the signed-in person's own sign key, null with none; the site's own call, answered null for any other */
|
|
223
|
+
ownKey: () => Promise<Uint8Array | null>;
|
|
224
|
+
/** the Friends part of a name's record, opened for its holder or one of the holder's friends; the site's own call */
|
|
225
|
+
openFriends: (record: SiteRecord) => Promise<OpenFriendsAnswer>;
|
|
226
|
+
/** deletes entries from the holder's own page, by id; the site's own call */
|
|
227
|
+
deleteEntries: (edit: {
|
|
228
|
+
name: string;
|
|
229
|
+
ids: string[];
|
|
230
|
+
}) => Promise<EditAnswer>;
|
|
231
|
+
/** marks a favourite on the holder's own page, or takes it off; the site's own call */
|
|
232
|
+
setFavourite: (edit: {
|
|
233
|
+
name: string;
|
|
234
|
+
favourite: Favourite;
|
|
235
|
+
on: boolean;
|
|
236
|
+
}) => Promise<EditAnswer>;
|
|
237
|
+
/** deletes approved comments from the holder's own page, by id; the site's own call */
|
|
238
|
+
deleteComments: (edit: {
|
|
239
|
+
name: string;
|
|
240
|
+
ids: string[];
|
|
241
|
+
}) => Promise<EditAnswer>;
|
|
242
|
+
/** leaves a comment for a friend's page, which waits for them to approve it; the site's own call */
|
|
243
|
+
sendComment: (edit: {
|
|
244
|
+
name: string;
|
|
245
|
+
text: string;
|
|
246
|
+
}) => Promise<CommentAnswer>;
|
|
247
|
+
state: () => Promise<Availability>;
|
|
248
|
+
attach: (binding: Omit<RoomBinding, 'send'>) => Promise<{
|
|
249
|
+
participants: () => Promise<Participant[]>;
|
|
250
|
+
ingest: (from: string, frame: string, at: number, seq: number) => Promise<boolean>;
|
|
251
|
+
memberJoined: (memberId: string) => Promise<void>;
|
|
252
|
+
memberLeft: (memberId: string) => Promise<void>;
|
|
253
|
+
announce: () => Promise<void>;
|
|
254
|
+
detach: () => Promise<void>;
|
|
255
|
+
}>;
|
|
256
|
+
};
|
|
257
|
+
/** How the library reaches the package. In production this is `packages.connect`. */
|
|
258
|
+
type Connect = () => Promise<Hub>;
|
|
259
|
+
/**
|
|
260
|
+
* The library, over a transport.
|
|
261
|
+
*
|
|
262
|
+
* Every session member ANSWERS rather than rejecting, following `rooms.available()`'s own precedent
|
|
263
|
+
* in `@fkn/lib`: an app must be able to render a roster without branching on an exception.
|
|
264
|
+
*/
|
|
265
|
+
export declare const createSocial: (connect: Connect) => {
|
|
266
|
+
available: () => Promise<Availability>;
|
|
267
|
+
attach: (binding: RoomBinding) => Promise<Presence>;
|
|
268
|
+
findByHandle: () => Promise<never>;
|
|
269
|
+
};
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/lib/publishing.d.ts
|
|
272
|
+
/** The package's answers to an app's posting calls, over the connection `packages.connect` makes. */
|
|
273
|
+
type PublishingHub = {
|
|
274
|
+
hello: () => Promise<Hello>;
|
|
275
|
+
publishing: () => Promise<PublishingState>;
|
|
276
|
+
allowPublishing: (ask: {
|
|
277
|
+
kinds: string[];
|
|
278
|
+
}) => Promise<PublishingState>;
|
|
279
|
+
publish: (post: Post) => Promise<PublishAnswer>;
|
|
280
|
+
stopPublishing: () => Promise<PublishingState>;
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* How the calls reach the package: `connect` answers null when it is not installed for this app, and
|
|
284
|
+
* `install` whether it is installed now. Throws are read as `unavailable`.
|
|
285
|
+
*/
|
|
286
|
+
type Transport = {
|
|
287
|
+
connect: () => Promise<PublishingHub | null>;
|
|
288
|
+
install: () => Promise<boolean>;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* What an app calls to post to the person's FKN Social page. Each call answers and never throws. Only
|
|
292
|
+
* `allowPublishing`, which an app calls from the person's own press, installs the package, so fkn.app's
|
|
293
|
+
* notice shows then and never otherwise; with nothing installed the others answer `off` and
|
|
294
|
+
* `not-allowed`. One connection serves the page.
|
|
295
|
+
*/
|
|
296
|
+
export declare const createPublishing: ({ connect, install }: Transport) => {
|
|
297
|
+
/** where this app stands: allowed with its kinds, asked, off, or why it cannot tell */
|
|
298
|
+
publishing: () => Promise<PublishingState>;
|
|
299
|
+
/** records the ask, which the person answers on social.fkn.app; installs the package first when it is not */
|
|
300
|
+
allowPublishing: (ask: {
|
|
301
|
+
kinds: Post['kind'][];
|
|
302
|
+
}) => Promise<PublishingState>;
|
|
303
|
+
publish: (post: Post) => Promise<PublishAnswer>;
|
|
304
|
+
/** ends this app's posts at once; what it posted stays until the person deletes it */
|
|
305
|
+
stopPublishing: () => Promise<PublishingState>;
|
|
306
|
+
};
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region src/lib/index.d.ts
|
|
309
|
+
/** The social package, addressed by the origin that serves it, so a deploy of that site ships it. */
|
|
310
|
+
export declare const SOCIAL_PACKAGE = "https:social.fkn.app";
|
|
311
|
+
/** The contract tag the package's `onConnect` is handed. */
|
|
312
|
+
export declare const SOCIAL_PROTOCOL = "social@1";
|
|
313
|
+
/**
|
|
314
|
+
* The production transport: installs the package for this app with a notice rather than a prompt,
|
|
315
|
+
* then connects, exposing `payload` to the package as its `remote`. Throws only when FKN refuses;
|
|
316
|
+
* `createSocial` turns that into `unavailable`.
|
|
317
|
+
*/
|
|
318
|
+
export declare const connectPackage: (uri?: string, payload?: unknown) => Connect;
|
|
319
|
+
/**
|
|
320
|
+
* The posting calls' transport: connects without installing, answering null when the package is not
|
|
321
|
+
* installed for this app, and installs with a notice rather than a prompt.
|
|
322
|
+
*/
|
|
323
|
+
export declare const packageTransport: (uri?: string) => Transport;
|
|
324
|
+
export declare const social: {
|
|
325
|
+
available: () => Promise<Availability>;
|
|
326
|
+
attach: (binding: RoomBinding) => Promise<Presence>;
|
|
327
|
+
findByHandle: () => Promise<never>;
|
|
328
|
+
publishing: () => Promise<PublishingState>;
|
|
329
|
+
allowPublishing: (ask: {
|
|
330
|
+
kinds: Post['kind'][];
|
|
331
|
+
}) => Promise<PublishingState>;
|
|
332
|
+
publish: (post: Post) => Promise<PublishAnswer>;
|
|
333
|
+
stopPublishing: () => Promise<PublishingState>;
|
|
334
|
+
};
|
|
335
|
+
//#endregion
|
|
336
|
+
export type { Availability, Connect, Hub, Participant, Post, Presence, PublishAnswer, PublishingHub, PublishingRefusal, PublishingState, RoomBinding, SocialError, SocialErrorCode, Transport };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { connect, install } from "@fkn/lib/packages";
|
|
2
|
+
//#region src/lib/errors.ts
|
|
3
|
+
/**
|
|
4
|
+
* Mints a coded error.
|
|
5
|
+
*
|
|
6
|
+
* The code is the contract and the message is for a person reading a log. osra keeps only name,
|
|
7
|
+
* message, stack and cause across an origin hop, so a code carried as a property would not survive
|
|
8
|
+
* one: it is re-minted on this side from the refusal that crossed, the way `@fkn/lib/rooms` does it.
|
|
9
|
+
*/
|
|
10
|
+
const socialError = (code, message) => Object.assign(new Error(message), { code });
|
|
11
|
+
const isSocialError = (value) => value instanceof Error && typeof value.code === "string";
|
|
12
|
+
/**
|
|
13
|
+
* Decides what this library may call against the package that answered.
|
|
14
|
+
*
|
|
15
|
+
* The normal direction is the package being NEWER, and that must always work: the package only adds,
|
|
16
|
+
* and never removes a call at a level it still serves. The reverse happens too, when a consumer
|
|
17
|
+
* updates the library before the site has redeployed, and there the library must confine itself to
|
|
18
|
+
* the calls the package's own level defines.
|
|
19
|
+
*/
|
|
20
|
+
const negotiate = (hello) => {
|
|
21
|
+
if (2 < hello.min) return {
|
|
22
|
+
usable: false,
|
|
23
|
+
reason: "outdated"
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
usable: true,
|
|
27
|
+
level: Math.min(2, hello.protocol)
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/** Whether a call introduced at `since` may be made at the negotiated level. */
|
|
31
|
+
const supports = (negotiation, since) => negotiation.usable && negotiation.level >= since;
|
|
32
|
+
/** The level each call was introduced at. A call absent here is level 1. */
|
|
33
|
+
const SINCE = {
|
|
34
|
+
available: 1,
|
|
35
|
+
me: 1,
|
|
36
|
+
attach: 1,
|
|
37
|
+
friends: 1,
|
|
38
|
+
openInvite: 1,
|
|
39
|
+
openAdd: 1,
|
|
40
|
+
publishing: 2,
|
|
41
|
+
allowPublishing: 2,
|
|
42
|
+
publish: 2,
|
|
43
|
+
stopPublishing: 2,
|
|
44
|
+
/** needs a lookup the platform does not offer, so it answers `unsupported` */
|
|
45
|
+
findByHandle: 3,
|
|
46
|
+
beFindable: 3
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/lib/client.ts
|
|
50
|
+
/**
|
|
51
|
+
* Whether a frame is one of ours.
|
|
52
|
+
*
|
|
53
|
+
* The library forwards ONLY frames whose decoded `t` names a social message, so a host app cannot
|
|
54
|
+
* push arbitrary bytes across the origin hop into the verifier. Anything else is somebody else's
|
|
55
|
+
* traffic and is left alone.
|
|
56
|
+
*/
|
|
57
|
+
const isSocialFrame = (frame) => {
|
|
58
|
+
try {
|
|
59
|
+
const parsed = JSON.parse(frame);
|
|
60
|
+
if (typeof parsed !== "object" || parsed === null) return false;
|
|
61
|
+
const tag = parsed.t;
|
|
62
|
+
return typeof tag === "string" && tag.startsWith("social.");
|
|
63
|
+
} catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const silentParticipant = (memberId, selfMemberId) => ({
|
|
68
|
+
memberId,
|
|
69
|
+
status: "silent",
|
|
70
|
+
identity: null,
|
|
71
|
+
self: memberId === selfMemberId
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* A Presence that answers for a room social could not reach.
|
|
75
|
+
*
|
|
76
|
+
* THE GUARANTEED FLOOR. Every member reads `silent` and carries no identity, which is exactly the
|
|
77
|
+
* state an app renders when nobody has announced, so a host app that never hears from social draws
|
|
78
|
+
* precisely what it drew before it imported this library. `attach` therefore resolves rather than
|
|
79
|
+
* rejecting, and no app should ever need a try/catch to render a roster.
|
|
80
|
+
*/
|
|
81
|
+
const inertPresence = (selfMemberId) => {
|
|
82
|
+
const members = /* @__PURE__ */ new Set([selfMemberId]);
|
|
83
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
84
|
+
const list = () => [...members].sort().map((id) => silentParticipant(id, selfMemberId));
|
|
85
|
+
return {
|
|
86
|
+
participants: list,
|
|
87
|
+
of: (memberId) => silentParticipant(memberId, selfMemberId),
|
|
88
|
+
on: (listener) => {
|
|
89
|
+
listeners.add(listener);
|
|
90
|
+
return () => listeners.delete(listener);
|
|
91
|
+
},
|
|
92
|
+
ingest: () => false,
|
|
93
|
+
memberJoined: (memberId) => {
|
|
94
|
+
members.add(memberId);
|
|
95
|
+
},
|
|
96
|
+
memberLeft: (memberId) => {
|
|
97
|
+
members.delete(memberId);
|
|
98
|
+
},
|
|
99
|
+
announce: async () => {},
|
|
100
|
+
detach: async () => {}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The library, over a transport.
|
|
105
|
+
*
|
|
106
|
+
* Every session member ANSWERS rather than rejecting, following `rooms.available()`'s own precedent
|
|
107
|
+
* in `@fkn/lib`: an app must be able to render a roster without branching on an exception.
|
|
108
|
+
*/
|
|
109
|
+
const createSocial = (connect) => {
|
|
110
|
+
let hub = null;
|
|
111
|
+
let negotiated = null;
|
|
112
|
+
let reach = null;
|
|
113
|
+
const ensure = async () => {
|
|
114
|
+
if (reach !== null && reach !== "ready") return reach;
|
|
115
|
+
if (hub !== null && negotiated?.usable === true) return reach ?? "ready";
|
|
116
|
+
try {
|
|
117
|
+
const connected = await connect();
|
|
118
|
+
const negotiation = negotiate(await connected.hello());
|
|
119
|
+
if (!negotiation.usable) {
|
|
120
|
+
reach = "outdated";
|
|
121
|
+
return reach;
|
|
122
|
+
}
|
|
123
|
+
hub = connected;
|
|
124
|
+
negotiated = negotiation;
|
|
125
|
+
reach = await connected.state();
|
|
126
|
+
return reach;
|
|
127
|
+
} catch {
|
|
128
|
+
reach = "unavailable";
|
|
129
|
+
return reach;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const available = async () => ensure();
|
|
133
|
+
const attach = async (binding) => {
|
|
134
|
+
if (await ensure() !== "ready" || hub === null || negotiated === null || !supports(negotiated, SINCE.attach)) return inertPresence(binding.selfMemberId);
|
|
135
|
+
try {
|
|
136
|
+
const remote = await hub.attach({
|
|
137
|
+
roomId: binding.roomId,
|
|
138
|
+
selfMemberId: binding.selfMemberId,
|
|
139
|
+
observe: binding.observe
|
|
140
|
+
});
|
|
141
|
+
let cached = [];
|
|
142
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
143
|
+
const refresh = async () => {
|
|
144
|
+
cached = await remote.participants();
|
|
145
|
+
for (const listener of listeners) listener(cached);
|
|
146
|
+
};
|
|
147
|
+
refresh();
|
|
148
|
+
return {
|
|
149
|
+
participants: () => cached,
|
|
150
|
+
of: (memberId) => cached.find((p) => p.memberId === memberId) ?? silentParticipant(memberId, binding.selfMemberId),
|
|
151
|
+
on: (listener) => {
|
|
152
|
+
listeners.add(listener);
|
|
153
|
+
return () => listeners.delete(listener);
|
|
154
|
+
},
|
|
155
|
+
ingest: (from, frame, at, seq) => {
|
|
156
|
+
if (!isSocialFrame(frame)) return false;
|
|
157
|
+
remote.ingest(from, frame, at, seq).then(refresh, () => {});
|
|
158
|
+
return true;
|
|
159
|
+
},
|
|
160
|
+
memberJoined: (memberId) => {
|
|
161
|
+
remote.memberJoined(memberId).then(refresh, () => {});
|
|
162
|
+
},
|
|
163
|
+
memberLeft: (memberId) => {
|
|
164
|
+
remote.memberLeft(memberId).then(refresh, () => {});
|
|
165
|
+
},
|
|
166
|
+
announce: () => remote.announce(),
|
|
167
|
+
detach: () => remote.detach()
|
|
168
|
+
};
|
|
169
|
+
} catch {
|
|
170
|
+
return inertPresence(binding.selfMemberId);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
/** Calls above the negotiated level refuse with a code rather than throwing something untyped. */
|
|
174
|
+
const unsupported = (what) => socialError("unsupported", `social: ${what} needs a newer social package than this one`);
|
|
175
|
+
const findByHandle = async () => {
|
|
176
|
+
await ensure();
|
|
177
|
+
throw unsupported("handle search");
|
|
178
|
+
};
|
|
179
|
+
return {
|
|
180
|
+
available,
|
|
181
|
+
attach,
|
|
182
|
+
findByHandle
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
//#endregion
|
|
186
|
+
//#region src/lib/publishing.ts
|
|
187
|
+
/**
|
|
188
|
+
* What an app calls to post to the person's FKN Social page. Each call answers and never throws. Only
|
|
189
|
+
* `allowPublishing`, which an app calls from the person's own press, installs the package, so fkn.app's
|
|
190
|
+
* notice shows then and never otherwise; with nothing installed the others answer `off` and
|
|
191
|
+
* `not-allowed`. One connection serves the page.
|
|
192
|
+
*/
|
|
193
|
+
const createPublishing = ({ connect, install }) => {
|
|
194
|
+
let reached = null;
|
|
195
|
+
const reach = () => {
|
|
196
|
+
const attempt = reached ??= (async () => {
|
|
197
|
+
const hub = await connect();
|
|
198
|
+
if (hub === null) return "none";
|
|
199
|
+
const negotiated = negotiate(await hub.hello());
|
|
200
|
+
if (!negotiated.usable) return { refused: "outdated" };
|
|
201
|
+
return supports(negotiated, SINCE.publish) ? { hub } : { refused: "unsupported" };
|
|
202
|
+
})();
|
|
203
|
+
attempt.then((answer) => {
|
|
204
|
+
if (answer === "none" && reached === attempt) reached = null;
|
|
205
|
+
}, () => {
|
|
206
|
+
if (reached === attempt) reached = null;
|
|
207
|
+
});
|
|
208
|
+
return attempt;
|
|
209
|
+
};
|
|
210
|
+
const call = async (nothing, refused, run, installing = false) => {
|
|
211
|
+
try {
|
|
212
|
+
let answer = await reach();
|
|
213
|
+
if (answer === "none" && installing) {
|
|
214
|
+
if (!await install()) return refused("unavailable");
|
|
215
|
+
answer = await reach();
|
|
216
|
+
}
|
|
217
|
+
if (answer === "none") return installing ? refused("unavailable") : nothing;
|
|
218
|
+
if ("refused" in answer) return refused(answer.refused);
|
|
219
|
+
return await run(answer.hub);
|
|
220
|
+
} catch {
|
|
221
|
+
reached = null;
|
|
222
|
+
return refused("unavailable");
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const OFF = { state: "off" };
|
|
226
|
+
const state = (reason) => ({ state: reason });
|
|
227
|
+
return {
|
|
228
|
+
/** where this app stands: allowed with its kinds, asked, off, or why it cannot tell */
|
|
229
|
+
publishing: () => call(OFF, state, (hub) => hub.publishing()),
|
|
230
|
+
/** records the ask, which the person answers on social.fkn.app; installs the package first when it is not */
|
|
231
|
+
allowPublishing: (ask) => call(OFF, state, (hub) => hub.allowPublishing(ask), true),
|
|
232
|
+
publish: (post) => call({
|
|
233
|
+
ok: false,
|
|
234
|
+
reason: "not-allowed"
|
|
235
|
+
}, (reason) => ({
|
|
236
|
+
ok: false,
|
|
237
|
+
reason
|
|
238
|
+
}), (hub) => hub.publish(post)),
|
|
239
|
+
/** ends this app's posts at once; what it posted stays until the person deletes it */
|
|
240
|
+
stopPublishing: () => call(OFF, state, (hub) => hub.stopPublishing())
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
//#endregion
|
|
244
|
+
//#region src/lib/index.ts
|
|
245
|
+
/** The social package, addressed by the origin that serves it, so a deploy of that site ships it. */
|
|
246
|
+
const SOCIAL_PACKAGE = "https:social.fkn.app";
|
|
247
|
+
/** The contract tag the package's `onConnect` is handed. */
|
|
248
|
+
const SOCIAL_PROTOCOL = "social@1";
|
|
249
|
+
/**
|
|
250
|
+
* The production transport: installs the package for this app with a notice rather than a prompt,
|
|
251
|
+
* then connects, exposing `payload` to the package as its `remote`. Throws only when FKN refuses;
|
|
252
|
+
* `createSocial` turns that into `unavailable`.
|
|
253
|
+
*/
|
|
254
|
+
const connectPackage = (uri = SOCIAL_PACKAGE, payload) => async () => {
|
|
255
|
+
const installed = await install(uri, { noConfirm: true });
|
|
256
|
+
if (installed === null) throw socialError("unavailable", "social: the package was not installed");
|
|
257
|
+
const { remote } = await connect(installed.uri, {
|
|
258
|
+
protocol: SOCIAL_PROTOCOL,
|
|
259
|
+
payload
|
|
260
|
+
});
|
|
261
|
+
return remote;
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* The posting calls' transport: connects without installing, answering null when the package is not
|
|
265
|
+
* installed for this app, and installs with a notice rather than a prompt.
|
|
266
|
+
*/
|
|
267
|
+
const packageTransport = (uri = SOCIAL_PACKAGE) => ({
|
|
268
|
+
connect: async () => {
|
|
269
|
+
try {
|
|
270
|
+
const { remote } = await connect(uri, { protocol: SOCIAL_PROTOCOL });
|
|
271
|
+
return remote;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (error?.code === "not-installed") return null;
|
|
274
|
+
throw error;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
install: async () => await install(uri, { noConfirm: true }) !== null
|
|
278
|
+
});
|
|
279
|
+
const social = {
|
|
280
|
+
...createSocial(connectPackage()),
|
|
281
|
+
...createPublishing(packageTransport())
|
|
282
|
+
};
|
|
283
|
+
//#endregion
|
|
284
|
+
export { SOCIAL_PACKAGE, SOCIAL_PROTOCOL, connectPackage, createPublishing, createSocial, isSocialError, packageTransport, social };
|
package/dist/names.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/lib/names.d.ts
|
|
2
|
+
/** A default name's first word. */
|
|
3
|
+
export declare const ADJECTIVES: readonly string[];
|
|
4
|
+
/** A default name's second word. */
|
|
5
|
+
export declare const ANIMALS: readonly string[];
|
|
6
|
+
/**
|
|
7
|
+
* The name shown for someone who claimed none on FKN Social: two capitalised words, an adjective then an
|
|
8
|
+
* animal ("Brave Otter"), worked out from the id alone, so every app and every member of a room shows the
|
|
9
|
+
* same name for the same id with nothing stored or sent. Pass the room member id. A default name never
|
|
10
|
+
* reads as a claimed one, which is lowercase with no space. The lists and the hash are part of this
|
|
11
|
+
* promise: changing either renames everyone.
|
|
12
|
+
*/
|
|
13
|
+
export declare const defaultName: (memberId: string) => string;
|
|
14
|
+
//#endregion
|
package/dist/names.mjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/lib/names.ts
|
|
2
|
+
/** A default name's first word. */
|
|
3
|
+
const ADJECTIVES = `
|
|
4
|
+
Able Active Adept Agile Airy Alert Amiable Amused Apt Arctic Ardent Astral Astute Atomic Avid Awake Aware
|
|
5
|
+
Beaming Bold Brainy Brave Breezy Bright Brisk Bubbly Buoyant Busy Calm Candid Canny Capable Careful Caring
|
|
6
|
+
Cheery Chipper Chirpy Chummy Civil Classic Classy Clear Clever Comfy Comic Cool Cordial Cosmic Cozy Crafty
|
|
7
|
+
Crisp Crystal Curious Dandy Dapper Daring Dashing Dear Decent Deft Devoted Dewy Direct Dreamy Driven Dutiful
|
|
8
|
+
Dynamic Eager Early Earnest Elated Elegant Eminent Endless Epic Exact Expert Fabled Famed Famous Fancy Fervent
|
|
9
|
+
Festive Fiery Fine Firm Floral Fluent Fond Frank Free Fresh Frosty Frugal Funny Gallant Genial Gentle Genuine
|
|
10
|
+
Giddy Gifted Glad Gleeful Global Glowing Good Graceful Great Groovy Gusty Gutsy Handy Happy Hardy Hearty Helpful
|
|
11
|
+
Heroic Hidden Honest Hopeful Humble Hushed Icy Ideal Jaunty Jazzy Jolly Jovial Joyful Just Keen Kind Kindly
|
|
12
|
+
Kinetic Lavish Leafy Lively Lofty Lovely Loyal Lucent Lucid Lucky Lyrical Magic Major Mellow Merry Mighty Mild
|
|
13
|
+
Mindful Minty Misty Modern Modest Mossy Mystic Mythic Natural Neat Nifty Nimble Noble Nomadic Noted Novel
|
|
14
|
+
Oceanic Open Orbital Orderly Organic Patient Pensive Peppy Placid Playful Plucky Plush Poetic Poised Polar
|
|
15
|
+
Polite Precise Prime Proper Proud Prudent Quaint Quantum Quick Quiet Quirky Radiant Rapid Rare Ready Regal
|
|
16
|
+
Relaxed Roaming Robust Rocky Royal Rugged Rustic Safe Sage Savvy Scenic Secret Serene Sharp Shiny Silent
|
|
17
|
+
Sincere Skilled Sleek Sleepy Smart Smiley Smooth Snappy Snazzy Snowy Snug Social Solar Solid Sonic Sound
|
|
18
|
+
Sparkly Speedy Sporty Spry Stable Starry Stately Steady Stellar Stoic Stormy Sturdy Suave Sublime Sunny Super
|
|
19
|
+
Superb Sure Swanky Sweet Sylvan Tactful Thrifty Tidal Tidy Timely Toasty Trendy Tropic True Trusty Tuneful
|
|
20
|
+
Unique Upbeat Upright Valiant Valid Velvet Vernal Vibrant Vintage Vital Vivid Vocal Warm Wary Wintry Wise
|
|
21
|
+
Wistful Witty Worldly Worthy Zany Zealous Zesty Zippy
|
|
22
|
+
`.trim().split(/\s+/);
|
|
23
|
+
/** A default name's second word. */
|
|
24
|
+
const ANIMALS = `
|
|
25
|
+
Aardvark Alpaca Anole Anteater Antelope Avocet Axolotl Badger Basilisk Beagle Bear Bee Beetle Beluga Betta
|
|
26
|
+
Bilby Bison Bittern Bobcat Budgie Buffalo Bunting Caiman Canary Capybara Caracal Caribou Centaur Chipmunk
|
|
27
|
+
Cicada Civet Coati Colugo Condor Corgi Crane Cricket Curlew Cygnet Deer Dingo Dipper Dolphin Dormouse Dove
|
|
28
|
+
Dragon Dryad Duck Dugong Dunlin Eagle Echidna Eel Egret Eider Eland Elk Emu Ermine Falcon Fawn Fennec Ferret
|
|
29
|
+
Finch Firefly Flamingo Foal Fossa Fox Gazelle Gecko Genet Giraffe Gnu Gopher Goshawk Griffin Guppy Hamster
|
|
30
|
+
Hare Harrier Hawk Hedgehog Heron Herring Hoopoe Horse Hound Hydra Hyrax Ibex Ibis Iguana Impala Jacana Jaguar
|
|
31
|
+
Jay Jerboa Junco Kakapo Kangaroo Katydid Kea Kestrel Kinkajou Kirin Kite Kitsune Koala Koi Kraken Krill Kudu
|
|
32
|
+
Ladybug Lamb Lapwing Lark Lemur Leopard Linnet Lion Lionfish Llama Lobster Loris Lynx Macaw Magpie Mallard
|
|
33
|
+
Manta Mantis Margay Marlin Marmot Marten Meerkat Merlin Mink Moose Moth Mouse Mustang Narwhal Nautilus Newt
|
|
34
|
+
Numbat Nyala Ocelot Octopus Okapi Orca Oriole Oryx Osprey Ostrich Otter Owl Panda Pangolin Panther Parrot
|
|
35
|
+
Pegasus Pelican Penguin Petrel Pheasant Phoenix Pika Pike Pipit Pixie Platypus Plover Pony Poodle Puffin Puma
|
|
36
|
+
Quokka Quoll Rabbit Raven Reindeer Rhea Robin Saiga Salmon Scarab Seahorse Seal Selkie Serval Shrike Siskin
|
|
37
|
+
Skink Skua Skylark Snipe Spaniel Sparrow Sphinx Sprite Squid Squirrel Starfish Stilt Stingray Stoat Stork
|
|
38
|
+
Sunbird Swan Takin Tanager Tapir Tarsier Tenrec Tern Tetra Tiger Tortoise Toucan Towhee Triton Tuatara Tuna
|
|
39
|
+
Turaco Turtle Unicorn Vicuna Vole Wallaby Wapiti Warbler Waxwing Weaver Wombat Wren Wyvern Yak Zebra Zebu
|
|
40
|
+
`.trim().split(/\s+/);
|
|
41
|
+
/**
|
|
42
|
+
* The name shown for someone who claimed none on FKN Social: two capitalised words, an adjective then an
|
|
43
|
+
* animal ("Brave Otter"), worked out from the id alone, so every app and every member of a room shows the
|
|
44
|
+
* same name for the same id with nothing stored or sent. Pass the room member id. A default name never
|
|
45
|
+
* reads as a claimed one, which is lowercase with no space. The lists and the hash are part of this
|
|
46
|
+
* promise: changing either renames everyone.
|
|
47
|
+
*/
|
|
48
|
+
const defaultName = (memberId) => {
|
|
49
|
+
let hash = 2166136261;
|
|
50
|
+
for (let i = 0; i < memberId.length; i++) hash = Math.imul(hash ^ memberId.charCodeAt(i), 16777619);
|
|
51
|
+
hash >>>= 0;
|
|
52
|
+
return `${ADJECTIVES[hash % ADJECTIVES.length]} ${ANIMALS[Math.floor(hash / ADJECTIVES.length) % ANIMALS.length]}`;
|
|
53
|
+
};
|
|
54
|
+
//#endregion
|
|
55
|
+
export { ADJECTIVES, ANIMALS, defaultName };
|