@wtfalch/chat 0.9.0 → 0.10.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 +17 -5
- package/dist/chat/admin/index.js +11 -2
- package/dist/chat/client.d.ts +6 -0
- package/dist/chat/client.js +112 -8
- package/dist/chat/exchange.js +10 -1
- package/dist/chat/index.d.ts +1 -1
- package/dist/chat/index.js +1 -1
- package/dist/chat/types.d.ts +5 -2
- package/package.json +3 -7
- package/dist/chat/fake/index.d.ts +0 -27
- package/dist/chat/fake/index.js +0 -48
- package/dist/chat/fake/intercept.d.ts +0 -21
- package/dist/chat/fake/intercept.js +0 -57
- package/dist/chat/fake/sample.d.ts +0 -4
- package/dist/chat/fake/sample.js +0 -6
- package/dist/chat/fake/server.d.ts +0 -400
- package/dist/chat/fake/server.js +0 -1863
- package/dist/chat/fake/xhr.d.ts +0 -3
- package/dist/chat/fake/xhr.js +0 -104
package/README.md
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Team chat, on Matrix. Mirrors [`@wtfalch/email`](https://github.com/wtfalch/email)'s
|
|
4
4
|
shape: a client the agora gives a way to get a token, a React section built on
|
|
5
|
-
it
|
|
5
|
+
it.
|
|
6
|
+
|
|
7
|
+
## Breaking in 0.10.0
|
|
8
|
+
|
|
9
|
+
- `stop()` returns `Promise<void>` instead of `void`. It settles once every store the client built is destroyed, and it never rejects. A hand-written `ChatClient` with `stop: () => {}` no longer compiles; return a resolved promise.
|
|
10
|
+
- A `start()` that `stop()` overtook rejects with `ChatStartCancelledError`, exported from the package. Attach a handler when you do not await `start()`.
|
|
11
|
+
- The `./fake` export is gone. An app that needs a stand-in for development keeps its own copy.
|
|
6
12
|
|
|
7
13
|
## Exports
|
|
8
14
|
|
|
@@ -10,7 +16,6 @@ it, and a fake of both for stories and tests.
|
|
|
10
16
|
| --- | --- |
|
|
11
17
|
| `@wtfalch/chat` | `createChatClient`: a matrix-js-sdk `MatrixClient` behind the token exchange, refreshed on its own, exposing a small typed API |
|
|
12
18
|
| `@wtfalch/chat/react` | The `Chat` component: rooms, a timeline, a composer, direct messages, threads, reactions and mentions |
|
|
13
|
-
| `@wtfalch/chat/fake` | A real `createChatClient`, wired to a fake exchange and a fake homeserver instead of the network |
|
|
14
19
|
| `@wtfalch/chat/chat.css` | Styles, on `@wtfalch/design` tokens |
|
|
15
20
|
|
|
16
21
|
## The client
|
|
@@ -29,6 +34,10 @@ await chat.start();
|
|
|
29
34
|
chat.onRoomsChanged((rooms) => render(rooms));
|
|
30
35
|
```
|
|
31
36
|
|
|
37
|
+
A `start()` that `stop()` overtakes rejects with `ChatStartCancelledError`; if you do not await it, attach a handler:
|
|
38
|
+
`void chat.start().catch((error) => { if (!(error instanceof ChatStartCancelledError)) report(error); });`
|
|
39
|
+
`stop()` returns a Promise that settles once the store is destroyed; it never rejects.
|
|
40
|
+
|
|
32
41
|
`createChatClient` takes only a way to get a current ZITADEL access token
|
|
33
42
|
(`getAccessToken`) -- it knows nothing else about the agora, and never imports
|
|
34
43
|
an Agora type. On `start()`, it `POST`s `exchangeUrl` with that token and a
|
|
@@ -325,10 +334,13 @@ entities, `svg onload`, `object`/`embed`/`form`/`base`/`meta`, every `on*`
|
|
|
325
334
|
handler, the `<scr<script>ipt>` trick, foreign image sources, and messages
|
|
326
335
|
built out of nothing but depth or siblings.
|
|
327
336
|
|
|
328
|
-
## The
|
|
337
|
+
## The test double
|
|
338
|
+
|
|
339
|
+
Not exported and not published: `src/chat/fake` is this package's own test
|
|
340
|
+
double, left out of the build.
|
|
329
341
|
|
|
330
342
|
```ts
|
|
331
|
-
import { fakeChat } from '
|
|
343
|
+
import { fakeChat } from '../fake/index.ts';
|
|
332
344
|
|
|
333
345
|
const { client, server, dispose } = fakeChat();
|
|
334
346
|
await client.start();
|
|
@@ -339,7 +351,7 @@ dispose();
|
|
|
339
351
|
Like `@wtfalch/email`'s `fakeMail`, this is a fake transport, not a fake
|
|
340
352
|
client: `createChatClient` and matrix-js-sdk run unchanged, against a fake
|
|
341
353
|
exchange and homeserver answered by intercepting `fetch`. `FakeChatServer`
|
|
342
|
-
|
|
354
|
+
gives a test finer control -- seeding rooms (with a starting
|
|
343
355
|
`notificationCount`), seeding the company space (`isSpace`) and who is in it
|
|
344
356
|
(`members`, `invited`), adding, renaming or delivering someone leaving a room
|
|
345
357
|
mid-test, sending a message into one, invalidating the current token to
|
package/dist/chat/admin/index.js
CHANGED
|
@@ -110,8 +110,17 @@ export function createChatAdmin(options) {
|
|
|
110
110
|
});
|
|
111
111
|
const data = await response.json().catch(() => null);
|
|
112
112
|
if (!response.ok) {
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
// The shared estate envelope (`@wtfalch/contracts`):
|
|
114
|
+
// `{ error: { code, message, reason? } }`. `reason`, when the
|
|
115
|
+
// service sent one, is the finer-grained value this SDK's own
|
|
116
|
+
// `code` has always carried (`user_not_found`, not just the closed
|
|
117
|
+
// nine-value `not_found`) -- preferred for that reason.
|
|
118
|
+
const errorField = record(data) ? data.error : undefined;
|
|
119
|
+
const nested = record(errorField) ? errorField : undefined;
|
|
120
|
+
const candidate = (nested && typeof nested.reason === 'string' ? nested.reason : undefined) ??
|
|
121
|
+
(nested && typeof nested.code === 'string' ? nested.code : undefined);
|
|
122
|
+
const code = typeof candidate === 'string' && /^[a-z_]{1,64}$/.test(candidate)
|
|
123
|
+
? candidate
|
|
115
124
|
: 'request_failed';
|
|
116
125
|
throw new ChatAdminError(response.status, code);
|
|
117
126
|
}
|
package/dist/chat/client.d.ts
CHANGED
|
@@ -42,4 +42,10 @@ export declare function normaliseTopic(raw: string): string;
|
|
|
42
42
|
* no-op while `clientRunning` is still true, so a plain retry would never
|
|
43
43
|
* resume syncing.
|
|
44
44
|
*/
|
|
45
|
+
/** Rejection of a `start()` that `stop()` overtook. */
|
|
46
|
+
export declare class ChatStartCancelledError extends Error {
|
|
47
|
+
constructor(options?: {
|
|
48
|
+
cause?: unknown;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
45
51
|
export declare function createChatClient(options: CreateChatClientOptions): ChatClient;
|
package/dist/chat/client.js
CHANGED
|
@@ -179,6 +179,13 @@ const SEARCH_LIMIT_DEFAULT = 50;
|
|
|
179
179
|
* no-op while `clientRunning` is still true, so a plain retry would never
|
|
180
180
|
* resume syncing.
|
|
181
181
|
*/
|
|
182
|
+
/** Rejection of a `start()` that `stop()` overtook. */
|
|
183
|
+
export class ChatStartCancelledError extends Error {
|
|
184
|
+
constructor(options) {
|
|
185
|
+
super('chat: start cancelled by stop()', options);
|
|
186
|
+
this.name = 'ChatStartCancelledError';
|
|
187
|
+
}
|
|
188
|
+
}
|
|
182
189
|
export function createChatClient(options) {
|
|
183
190
|
const { exchangeUrl, getAccessToken, deviceId, storeName, encryptionEnabled } = options;
|
|
184
191
|
let client;
|
|
@@ -208,6 +215,11 @@ export function createChatClient(options) {
|
|
|
208
215
|
/** The account `secretStorageKeys` belongs to. */
|
|
209
216
|
let secretStorageUserId;
|
|
210
217
|
let starting;
|
|
218
|
+
// Bumped by `stop()`; a `start()` compares it after each await to learn it
|
|
219
|
+
// was overtaken. `teardown` settles once the last stop's store is destroyed.
|
|
220
|
+
let generation = 0;
|
|
221
|
+
let teardown = Promise.resolve();
|
|
222
|
+
const buildsInFlight = new Set();
|
|
211
223
|
let refreshTimer;
|
|
212
224
|
let recovering;
|
|
213
225
|
let resyncing;
|
|
@@ -2184,6 +2196,7 @@ export function createChatClient(options) {
|
|
|
2184
2196
|
async function swapClient(target, creds, wipeStore) {
|
|
2185
2197
|
disposeClient(target);
|
|
2186
2198
|
cryptoClosed = true;
|
|
2199
|
+
await destroyStore(target.store);
|
|
2187
2200
|
let wiped = !wipeStore;
|
|
2188
2201
|
let created;
|
|
2189
2202
|
for (let delay = REBUILD_RETRY_MS; !created; delay = Math.min(delay * 2, REBUILD_RETRY_MAX_MS)) {
|
|
@@ -2193,7 +2206,26 @@ export function createChatClient(options) {
|
|
|
2193
2206
|
if (!wiped)
|
|
2194
2207
|
await target.store.deleteAllData();
|
|
2195
2208
|
wiped = true;
|
|
2196
|
-
|
|
2209
|
+
if (client !== target)
|
|
2210
|
+
return undefined;
|
|
2211
|
+
// Registered with `stop()`, which waits on it; a build `stop()`
|
|
2212
|
+
// overtook destroys its own store and answers `undefined`.
|
|
2213
|
+
const building = buildClient(creds).then(async (built) => {
|
|
2214
|
+
if (client !== target) {
|
|
2215
|
+
disposeClient(built);
|
|
2216
|
+
await destroyStore(built.store);
|
|
2217
|
+
return undefined;
|
|
2218
|
+
}
|
|
2219
|
+
return built;
|
|
2220
|
+
});
|
|
2221
|
+
const tracked = building.catch(() => undefined);
|
|
2222
|
+
buildsInFlight.add(tracked);
|
|
2223
|
+
try {
|
|
2224
|
+
created = await building;
|
|
2225
|
+
}
|
|
2226
|
+
finally {
|
|
2227
|
+
buildsInFlight.delete(tracked);
|
|
2228
|
+
}
|
|
2197
2229
|
}
|
|
2198
2230
|
catch {
|
|
2199
2231
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
@@ -2201,6 +2233,7 @@ export function createChatClient(options) {
|
|
|
2201
2233
|
}
|
|
2202
2234
|
if (client !== target) {
|
|
2203
2235
|
disposeClient(created);
|
|
2236
|
+
await destroyStore(created.store);
|
|
2204
2237
|
return undefined;
|
|
2205
2238
|
}
|
|
2206
2239
|
if (latestToken)
|
|
@@ -2214,6 +2247,15 @@ export function createChatClient(options) {
|
|
|
2214
2247
|
target.stopClient();
|
|
2215
2248
|
target.removeAllListeners();
|
|
2216
2249
|
}
|
|
2250
|
+
/** Never rejects: a failed destroy is reported and teardown goes on. */
|
|
2251
|
+
async function destroyStore(store) {
|
|
2252
|
+
try {
|
|
2253
|
+
await store.destroy();
|
|
2254
|
+
}
|
|
2255
|
+
catch (error) {
|
|
2256
|
+
console.error('chat: store destroy failed', error);
|
|
2257
|
+
}
|
|
2258
|
+
}
|
|
2217
2259
|
async function buildClient(creds) {
|
|
2218
2260
|
if (secretStorageUserId !== creds.userId)
|
|
2219
2261
|
secretStorageKeys.clear();
|
|
@@ -2238,9 +2280,15 @@ export function createChatClient(options) {
|
|
|
2238
2280
|
},
|
|
2239
2281
|
},
|
|
2240
2282
|
});
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2283
|
+
try {
|
|
2284
|
+
if (encryptionEnabled)
|
|
2285
|
+
await initCrypto(created);
|
|
2286
|
+
await store.startup();
|
|
2287
|
+
}
|
|
2288
|
+
catch (error) {
|
|
2289
|
+
await destroyStore(store);
|
|
2290
|
+
throw error;
|
|
2291
|
+
}
|
|
2244
2292
|
return created;
|
|
2245
2293
|
}
|
|
2246
2294
|
function attachListeners(created) {
|
|
@@ -2280,8 +2328,16 @@ export function createChatClient(options) {
|
|
|
2280
2328
|
async function start() {
|
|
2281
2329
|
if (starting)
|
|
2282
2330
|
return starting;
|
|
2331
|
+
const myGeneration = generation;
|
|
2332
|
+
const cancelled = () => generation !== myGeneration;
|
|
2283
2333
|
const attempt = (async () => {
|
|
2334
|
+
// Never open the IndexedDB while the previous one is still closing.
|
|
2335
|
+
await teardown;
|
|
2336
|
+
if (cancelled())
|
|
2337
|
+
throw new ChatStartCancelledError();
|
|
2284
2338
|
const body = await performExchange();
|
|
2339
|
+
if (cancelled())
|
|
2340
|
+
throw new ChatStartCancelledError();
|
|
2285
2341
|
// Built before anything touches the outer closure's state (`client`,
|
|
2286
2342
|
// the listeners, the refresh timer): a WASM fetch or a blocked
|
|
2287
2343
|
// IndexedDB can throw inside `buildClient`, and `created` is still a
|
|
@@ -2291,12 +2347,38 @@ export function createChatClient(options) {
|
|
|
2291
2347
|
// client exists, as `IndexedDBStore` requires (it needs
|
|
2292
2348
|
// `setUserCreator`, which the client constructor calls, before it can
|
|
2293
2349
|
// replay persisted `m.presence`).
|
|
2294
|
-
|
|
2350
|
+
// `stop()` waits on `building`, which also destroys a store whose
|
|
2351
|
+
// start was overtaken.
|
|
2352
|
+
const building = buildClient({
|
|
2295
2353
|
baseUrl: body.homeserver,
|
|
2296
2354
|
userId: body.user_id,
|
|
2297
2355
|
accessToken: body.access_token,
|
|
2298
2356
|
deviceId: body.device_id,
|
|
2357
|
+
}).then(async (built) => {
|
|
2358
|
+
if (cancelled()) {
|
|
2359
|
+
disposeClient(built);
|
|
2360
|
+
await destroyStore(built.store);
|
|
2361
|
+
}
|
|
2362
|
+
return built;
|
|
2299
2363
|
});
|
|
2364
|
+
const tracked = building.catch(() => undefined);
|
|
2365
|
+
buildsInFlight.add(tracked);
|
|
2366
|
+
let created;
|
|
2367
|
+
try {
|
|
2368
|
+
created = await building;
|
|
2369
|
+
}
|
|
2370
|
+
catch (error) {
|
|
2371
|
+
if (cancelled()) {
|
|
2372
|
+
console.error('chat: build failed after stop()', error);
|
|
2373
|
+
throw new ChatStartCancelledError({ cause: error });
|
|
2374
|
+
}
|
|
2375
|
+
throw error;
|
|
2376
|
+
}
|
|
2377
|
+
finally {
|
|
2378
|
+
buildsInFlight.delete(tracked);
|
|
2379
|
+
}
|
|
2380
|
+
if (cancelled())
|
|
2381
|
+
throw new ChatStartCancelledError();
|
|
2300
2382
|
client = created;
|
|
2301
2383
|
attachListeners(created);
|
|
2302
2384
|
scheduleRefresh(body.expires_at);
|
|
@@ -2345,8 +2427,19 @@ export function createChatClient(options) {
|
|
|
2345
2427
|
starting = undefined;
|
|
2346
2428
|
clearTimeout(refreshTimer);
|
|
2347
2429
|
refreshTimer = undefined;
|
|
2348
|
-
|
|
2349
|
-
|
|
2430
|
+
const stopped = client;
|
|
2431
|
+
if (stopped)
|
|
2432
|
+
disposeClient(stopped);
|
|
2433
|
+
generation += 1;
|
|
2434
|
+
const previous = teardown;
|
|
2435
|
+
const building = [...buildsInFlight];
|
|
2436
|
+
buildsInFlight.clear();
|
|
2437
|
+
teardown = (async () => {
|
|
2438
|
+
await previous;
|
|
2439
|
+
await Promise.all(building);
|
|
2440
|
+
if (stopped)
|
|
2441
|
+
await destroyStore(stopped.store);
|
|
2442
|
+
})();
|
|
2350
2443
|
cryptoClosed = false;
|
|
2351
2444
|
// A recovery still sleeping in `swapClient` must not make the next
|
|
2352
2445
|
// client's recovery wait on it.
|
|
@@ -2354,6 +2447,7 @@ export function createChatClient(options) {
|
|
|
2354
2447
|
resyncing = undefined;
|
|
2355
2448
|
client = undefined;
|
|
2356
2449
|
setStatus('offline');
|
|
2450
|
+
return teardown;
|
|
2357
2451
|
}
|
|
2358
2452
|
function onRoomsChanged(listener) {
|
|
2359
2453
|
listeners.add(listener);
|
|
@@ -2550,10 +2644,20 @@ export function createChatClient(options) {
|
|
|
2550
2644
|
void fetchAutomaticStatus(userId);
|
|
2551
2645
|
try {
|
|
2552
2646
|
const raw = await target.getExtendedProfileProperty(userId, STATUS_PROFILE_KEY);
|
|
2647
|
+
// A reply that lands after `stop()` or a client swap must not write;
|
|
2648
|
+
// the placeholder goes, so the next read fetches through the live client.
|
|
2649
|
+
if (client !== target) {
|
|
2650
|
+
if (chatStatusCache.get(userId) === null)
|
|
2651
|
+
chatStatusCache.delete(userId);
|
|
2652
|
+
return;
|
|
2653
|
+
}
|
|
2553
2654
|
notifyStatusChanged(userId, isChatStatus(raw) ? raw : null);
|
|
2554
2655
|
}
|
|
2555
2656
|
catch {
|
|
2556
|
-
|
|
2657
|
+
if (client === target)
|
|
2658
|
+
notifyStatusChanged(userId, null);
|
|
2659
|
+
else if (chatStatusCache.get(userId) === null)
|
|
2660
|
+
chatStatusCache.delete(userId);
|
|
2557
2661
|
}
|
|
2558
2662
|
}
|
|
2559
2663
|
/** See `ChatClient.status`. Exported as `status` on the returned client
|
package/dist/chat/exchange.js
CHANGED
|
@@ -18,8 +18,17 @@ export async function exchange(exchangeUrl, getAccessToken, deviceId) {
|
|
|
18
18
|
if (!response.ok) {
|
|
19
19
|
let reason;
|
|
20
20
|
try {
|
|
21
|
+
// The shared estate envelope (`@wtfalch/contracts`):
|
|
22
|
+
// `{ error: { code, message, reason? } }`. `reason`, when the
|
|
23
|
+
// service sent one, is the more specific of the two -- `code` is
|
|
24
|
+
// only the closed nine-value set.
|
|
21
25
|
const problem = (await response.json());
|
|
22
|
-
reason =
|
|
26
|
+
reason =
|
|
27
|
+
typeof problem.error?.reason === 'string'
|
|
28
|
+
? problem.error.reason
|
|
29
|
+
: typeof problem.error?.code === 'string'
|
|
30
|
+
? problem.error.code
|
|
31
|
+
: undefined;
|
|
23
32
|
}
|
|
24
33
|
catch {
|
|
25
34
|
// The body was not JSON, or was empty. The status is still enough to
|
package/dist/chat/index.d.ts
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
* everything it returns is this package's own small, typed surface. See
|
|
7
7
|
* `README.md` and the chat plan, section 9.
|
|
8
8
|
*/
|
|
9
|
-
export { createChatClient, MAX_ROOM_NAME_LENGTH, MAX_TOPIC_LENGTH, MAX_UPLOAD_BYTES, normaliseTopic, } from './client.ts';
|
|
9
|
+
export { ChatStartCancelledError, createChatClient, MAX_ROOM_NAME_LENGTH, MAX_TOPIC_LENGTH, MAX_UPLOAD_BYTES, normaliseTopic, } from './client.ts';
|
|
10
10
|
export type { ChatAttachment, ChatBrowsableRoom, ChatNewRoom, ChatRoomFilesPage, ChatClient, ChatConnectionStatus, ChatPerson, ChatRoomLastMessage, ChatRoomSummary, ChatSearchResults, ChatTimelineEvent, ChatUploadProgress, CreateChatClientOptions, ExchangeBody, } from './types.ts';
|
package/dist/chat/index.js
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
* everything it returns is this package's own small, typed surface. See
|
|
7
7
|
* `README.md` and the chat plan, section 9.
|
|
8
8
|
*/
|
|
9
|
-
export { createChatClient, MAX_ROOM_NAME_LENGTH, MAX_TOPIC_LENGTH, MAX_UPLOAD_BYTES, normaliseTopic, } from "./client.js";
|
|
9
|
+
export { ChatStartCancelledError, createChatClient, MAX_ROOM_NAME_LENGTH, MAX_TOPIC_LENGTH, MAX_UPLOAD_BYTES, normaliseTopic, } from "./client.js";
|
package/dist/chat/types.d.ts
CHANGED
|
@@ -367,8 +367,11 @@ export type ChatClient = {
|
|
|
367
367
|
* it again while already started is a no-op. */
|
|
368
368
|
start: () => Promise<void>;
|
|
369
369
|
/** Stops syncing and releases the underlying Matrix client. Safe to call
|
|
370
|
-
* more than once, and before `start()` ever resolves.
|
|
371
|
-
|
|
370
|
+
* more than once, and before `start()` ever resolves. The state change is
|
|
371
|
+
* immediate; the promise settles once any in-flight store startup has
|
|
372
|
+
* finished and the store is destroyed. It never rejects. A `start()` that
|
|
373
|
+
* this overtakes rejects with `ChatStartCancelledError`. */
|
|
374
|
+
stop: () => Promise<void>;
|
|
372
375
|
/** The rooms the person is currently in. Empty before `start()` resolves. */
|
|
373
376
|
rooms: () => ChatRoomSummary[];
|
|
374
377
|
/** Called with the current room list whenever it changes. Returns a
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtfalch/chat",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "A Matrix chat client behind the agora's token exchange.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,10 +21,6 @@
|
|
|
21
21
|
"types": "./dist/chat/react/index.d.ts",
|
|
22
22
|
"default": "./dist/chat/react/index.js"
|
|
23
23
|
},
|
|
24
|
-
"./fake": {
|
|
25
|
-
"types": "./dist/chat/fake/index.d.ts",
|
|
26
|
-
"default": "./dist/chat/fake/index.js"
|
|
27
|
-
},
|
|
28
24
|
"./chat.css": "./dist/chat/chat.css",
|
|
29
25
|
"./package.json": "./package.json",
|
|
30
26
|
"./admin": {
|
|
@@ -48,7 +44,7 @@
|
|
|
48
44
|
"test:acceptance": "vitest run --config vitest.acceptance.config.ts"
|
|
49
45
|
},
|
|
50
46
|
"peerDependencies": {
|
|
51
|
-
"@wtfalch/design": ">=0.
|
|
47
|
+
"@wtfalch/design": ">=0.30.0 <0.32.0",
|
|
52
48
|
"react": "^19.0.0",
|
|
53
49
|
"react-dom": "^19.0.0"
|
|
54
50
|
},
|
|
@@ -69,7 +65,7 @@
|
|
|
69
65
|
"@types/node": "22.20.2",
|
|
70
66
|
"@types/react": "19.3.0",
|
|
71
67
|
"@types/react-dom": "19.3.0",
|
|
72
|
-
"@wtfalch/design": "0.
|
|
68
|
+
"@wtfalch/design": "0.31.0",
|
|
73
69
|
"fake-indexeddb": "6.2.5",
|
|
74
70
|
"jsdom": "30.0.1",
|
|
75
71
|
"react": "19.2.4",
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { ChatClient } from '../types.ts';
|
|
2
|
-
import { FakeChatServer, type FakeChatServerOptions } from './server.ts';
|
|
3
|
-
export { FakeChatServer } from './server.ts';
|
|
4
|
-
export type { FakeChatServerOptions, FakeHierarchyChild, FakeMember, FakeMessageSeed, FakeRoomSeed, } from './server.ts';
|
|
5
|
-
export { SAMPLE_ROOMS } from './sample.ts';
|
|
6
|
-
export type FakeChatOptions = FakeChatServerOptions & {
|
|
7
|
-
storeName?: string;
|
|
8
|
-
deviceId?: string;
|
|
9
|
-
};
|
|
10
|
-
export type FakeChat = {
|
|
11
|
-
/** A real `ChatClient`. Everything this package exports works against it
|
|
12
|
-
* unchanged, through the real `createChatClient`. */
|
|
13
|
-
client: ChatClient;
|
|
14
|
-
/** The homeserver behind it, to drive more rooms or a token refusal, or
|
|
15
|
-
* to assert on how many times the exchange was called. */
|
|
16
|
-
server: FakeChatServer;
|
|
17
|
-
/** Stops the client and uninstalls the fake. Call it when a story or a
|
|
18
|
-
* test is done. */
|
|
19
|
-
dispose: () => void;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* A client and the fake exchange/homeserver behind it.
|
|
23
|
-
*
|
|
24
|
-
* Seeded with `SAMPLE_ROOMS` unless told otherwise, because a fake with no
|
|
25
|
-
* rooms demonstrates the empty state and nothing else.
|
|
26
|
-
*/
|
|
27
|
-
export declare function fakeChat(options?: FakeChatOptions): FakeChat;
|
package/dist/chat/fake/index.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `@wtfalch/chat/fake` -- a real `createChatClient`, wired to a
|
|
3
|
-
* fake exchange and a fake homeserver instead of the network, for stories
|
|
4
|
-
* and tests. See `server.ts`'s module comment for why it fakes the
|
|
5
|
-
* transport rather than the client.
|
|
6
|
-
*
|
|
7
|
-
* ```ts
|
|
8
|
-
* import { fakeChat } from '@wtfalch/chat/fake'
|
|
9
|
-
*
|
|
10
|
-
* const { client, dispose } = fakeChat()
|
|
11
|
-
* await client.start()
|
|
12
|
-
* client.rooms() // SAMPLE_ROOMS, joined
|
|
13
|
-
* dispose()
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
import { createChatClient } from "../client.js";
|
|
17
|
-
import { SAMPLE_ROOMS } from "./sample.js";
|
|
18
|
-
import { FakeChatServer } from "./server.js";
|
|
19
|
-
export { FakeChatServer } from "./server.js";
|
|
20
|
-
export { SAMPLE_ROOMS } from "./sample.js";
|
|
21
|
-
/**
|
|
22
|
-
* A client and the fake exchange/homeserver behind it.
|
|
23
|
-
*
|
|
24
|
-
* Seeded with `SAMPLE_ROOMS` unless told otherwise, because a fake with no
|
|
25
|
-
* rooms demonstrates the empty state and nothing else.
|
|
26
|
-
*/
|
|
27
|
-
export function fakeChat(options = {}) {
|
|
28
|
-
const { storeName, deviceId, ...serverOptions } = options;
|
|
29
|
-
const server = new FakeChatServer({ rooms: SAMPLE_ROOMS, ...serverOptions });
|
|
30
|
-
const client = createChatClient({
|
|
31
|
-
exchangeUrl: server.exchangeUrl,
|
|
32
|
-
getAccessToken: () => 'fake-zitadel-token',
|
|
33
|
-
deviceId: deviceId ?? 'fake-device',
|
|
34
|
-
storeName: storeName ?? 'fake',
|
|
35
|
-
});
|
|
36
|
-
let disposed = false;
|
|
37
|
-
return {
|
|
38
|
-
client,
|
|
39
|
-
server,
|
|
40
|
-
dispose: () => {
|
|
41
|
-
if (disposed)
|
|
42
|
-
return;
|
|
43
|
-
disposed = true;
|
|
44
|
-
client.stop();
|
|
45
|
-
server.dispose();
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Global `fetch` interception, shared by every `FakeChatServer`.
|
|
3
|
-
*
|
|
4
|
-
* **A fake transport, not a fake API** -- the same choice `@wtfalch/email`'s
|
|
5
|
-
* `fakeMail()` makes. `createChatClient` and matrix-js-sdk run unchanged; only
|
|
6
|
-
* the network is answered by memory instead of a real homeserver.
|
|
7
|
-
*
|
|
8
|
-
* Unlike a single captured `route` callback, this keeps a list: two fakes --
|
|
9
|
-
* two companies' homeservers -- can be installed at once, each answering only
|
|
10
|
-
* its own origin and falling through to the next. The real `fetch` is
|
|
11
|
-
* restored once the last one is disposed.
|
|
12
|
-
*/
|
|
13
|
-
export type FetchRoute = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response> | null;
|
|
14
|
-
/** Registers `route`. Returns a function that unregisters it.
|
|
15
|
-
*
|
|
16
|
-
* `globalThis.fetch` stays patched once the first route is installed, and
|
|
17
|
-
* is never put back: a request made to a disposed fake is still in flight
|
|
18
|
-
* when its route goes, and restoring the real `fetch` underneath it sends
|
|
19
|
-
* that request to DNS. Everything but a `.example.test` host goes straight
|
|
20
|
-
* through to the real `fetch` either way, so nothing else is affected. */
|
|
21
|
-
export declare function installFetchRoute(route: FetchRoute): () => void;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { installFakeUploads } from "./xhr.js";
|
|
2
|
-
let original;
|
|
3
|
-
const routes = [];
|
|
4
|
-
/** `.example.test` is reserved: no such host can ever exist, so a request
|
|
5
|
-
* to one is always meant for a fake. It only reaches the fallthrough when
|
|
6
|
-
* the fake that would have answered it has already been disposed -- a
|
|
7
|
-
* request still in flight when a test ended. Such a request is left
|
|
8
|
-
* hanging rather than answered: a client nobody is using any more has no
|
|
9
|
-
* business succeeding, and any answer at all -- a real DNS failure, or a
|
|
10
|
-
* 404 -- comes back as a rejection inside matrix-js-sdk that no test can
|
|
11
|
-
* reach and vitest reports as an unhandled error. */
|
|
12
|
-
function isFakeHost(input) {
|
|
13
|
-
try {
|
|
14
|
-
const url = input instanceof Request ? input.url : String(input);
|
|
15
|
-
return new URL(url).hostname.endsWith('.example.test');
|
|
16
|
-
}
|
|
17
|
-
catch {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function patched(input, init) {
|
|
22
|
-
for (const route of routes) {
|
|
23
|
-
const answered = route(input, init);
|
|
24
|
-
if (answered)
|
|
25
|
-
return answered;
|
|
26
|
-
}
|
|
27
|
-
if (isFakeHost(input))
|
|
28
|
-
return new Promise(() => { });
|
|
29
|
-
if (!original) {
|
|
30
|
-
throw new Error('fake chat: no route matched and the real fetch was not captured');
|
|
31
|
-
}
|
|
32
|
-
return original(input, init);
|
|
33
|
-
}
|
|
34
|
-
/** Registers `route`. Returns a function that unregisters it.
|
|
35
|
-
*
|
|
36
|
-
* `globalThis.fetch` stays patched once the first route is installed, and
|
|
37
|
-
* is never put back: a request made to a disposed fake is still in flight
|
|
38
|
-
* when its route goes, and restoring the real `fetch` underneath it sends
|
|
39
|
-
* that request to DNS. Everything but a `.example.test` host goes straight
|
|
40
|
-
* through to the real `fetch` either way, so nothing else is affected. */
|
|
41
|
-
export function installFetchRoute(route) {
|
|
42
|
-
installFakeUploads();
|
|
43
|
-
if (!original) {
|
|
44
|
-
original = globalThis.fetch;
|
|
45
|
-
globalThis.fetch = patched;
|
|
46
|
-
}
|
|
47
|
-
routes.push(route);
|
|
48
|
-
let removed = false;
|
|
49
|
-
return () => {
|
|
50
|
-
if (removed)
|
|
51
|
-
return;
|
|
52
|
-
removed = true;
|
|
53
|
-
const index = routes.indexOf(route);
|
|
54
|
-
if (index >= 0)
|
|
55
|
-
routes.splice(index, 1);
|
|
56
|
-
};
|
|
57
|
-
}
|
package/dist/chat/fake/sample.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/** A couple of rooms, for a story or a test that wants something to look
|
|
2
|
-
* at without seeding its own. */
|
|
3
|
-
export const SAMPLE_ROOMS = [
|
|
4
|
-
{ id: '!general:chat.example.test', name: 'general', isDirect: false },
|
|
5
|
-
{ id: '!ada:chat.example.test', name: 'Ada Lovelace', isDirect: true },
|
|
6
|
-
];
|