@faybl/client 4.3.1 → 5.0.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 +46 -35
- package/package.json +17 -11
- package/src/auth.js +3 -2
- package/src/browser.js +11 -32
- package/src/channel.js +8 -12
- package/src/client.js +14 -18
- package/src/clientFactory.js +45 -0
- package/src/clientOptions.js +161 -22
- package/src/conf.js +15 -15
- package/src/connection.js +9 -6
- package/src/connectionMachine.js +137 -0
- package/src/dataset.js +97 -53
- package/src/datasetRecords.js +6 -6
- package/src/fetch.js +9 -9
- package/src/guards.js +9 -0
- package/src/index.js +10 -51
- package/src/mqtt.js +52 -54
- package/src/mqttCredentials.js +22 -16
- package/src/mqttUtils.js +103 -56
- package/src/oidcHandler.js +12 -10
- package/src/presence.js +6 -6
- package/src/procedure.js +68 -30
- package/src/report.js +11 -2
- package/src/session.js +11 -13
- package/src/subscribers.js +39 -0
- package/src/topicSegment.js +2 -2
- package/src/utils/either.js +0 -35
package/src/report.js
CHANGED
|
@@ -4,8 +4,8 @@ import * as R from 'ramda';
|
|
|
4
4
|
* Turns the caller's `onError` into the function every module reports through. The client writes
|
|
5
5
|
* nothing to the console; without an `onError`, what it would report is dropped.
|
|
6
6
|
*
|
|
7
|
-
* `context.source` says where the error came from: `mqtt`, `credentials`, `message`,
|
|
8
|
-
* or `procedure`. `context.topic` is set when the error concerns a topic.
|
|
7
|
+
* `context.source` says where the error came from: `mqtt`, `session`, `credentials`, `message`,
|
|
8
|
+
* `subscriber` or `procedure`. `context.topic` is set when the error concerns a topic.
|
|
9
9
|
*
|
|
10
10
|
* What a throwing onError throws is dropped too; it would otherwise escape into an MQTT.js event or
|
|
11
11
|
* a subscriber's delivery.
|
|
@@ -17,3 +17,12 @@ export const createReporter = (onError = () => undefined) =>
|
|
|
17
17
|
},
|
|
18
18
|
() => undefined,
|
|
19
19
|
);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Where a client keeps its reporter for the parts built on top of it, such as datasets. A symbol
|
|
23
|
+
* keeps it out of the client's public, enumerable API.
|
|
24
|
+
*/
|
|
25
|
+
export const REPORTER = Symbol('faybl.reporter');
|
|
26
|
+
|
|
27
|
+
// The reporter of a client, or one that drops everything for a client without one.
|
|
28
|
+
export const reporterOf = (client) => client?.[REPORTER] ?? (() => undefined);
|
package/src/session.js
CHANGED
|
@@ -1,37 +1,35 @@
|
|
|
1
|
+
import { createSubscribers } from './subscribers.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Follows whether the caller's login is still accepted. A request refused with 401 marks the
|
|
3
5
|
* session expired: it is reported to `onError` as `session` once, and subscribers get
|
|
4
6
|
* `{ expired: true }`. The next request that succeeds marks it alive again. However many calls
|
|
5
|
-
* were in flight, one expiry is one report and one change.
|
|
7
|
+
* were in flight, one expiry is one report and one change. A subscriber that throws is reported
|
|
8
|
+
* as `subscriber` and changes nothing for the request that told the session.
|
|
6
9
|
*/
|
|
7
10
|
export const createSession = (report = () => undefined) => {
|
|
8
|
-
|
|
11
|
+
const subscribers = createSubscribers(report);
|
|
12
|
+
// eslint-disable-next-line functional/no-let -- The session's state outlives the independent requests that change it; each change replaces the value.
|
|
9
13
|
let expired = false;
|
|
10
|
-
let callbacks = [];
|
|
11
|
-
/* eslint-enable functional/no-let */
|
|
12
|
-
|
|
13
|
-
const notify = () => callbacks.forEach((callback) => callback({ expired }));
|
|
14
14
|
|
|
15
15
|
const lost = (error) => {
|
|
16
16
|
if (expired) return;
|
|
17
17
|
expired = true;
|
|
18
18
|
report(error, { source: 'session' });
|
|
19
|
-
notify();
|
|
19
|
+
subscribers.notify({ expired });
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
const confirmed = () => {
|
|
23
23
|
if (!expired) return;
|
|
24
24
|
expired = false;
|
|
25
|
-
notify();
|
|
25
|
+
subscribers.notify({ expired });
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
// Calls back at once with the current state, and again whenever it changes.
|
|
29
29
|
const subscribe = (callback) => {
|
|
30
|
-
|
|
31
|
-
callback
|
|
32
|
-
return
|
|
33
|
-
callbacks = callbacks.filter((subscribed) => subscribed !== callback);
|
|
34
|
-
};
|
|
30
|
+
const undo = subscribers.add(callback);
|
|
31
|
+
subscribers.deliver(callback, { expired });
|
|
32
|
+
return undo;
|
|
35
33
|
};
|
|
36
34
|
|
|
37
35
|
return { lost, confirmed, subscribe };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { tryCatchAsync } from '@faybl/utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calls `callback(value)` at once and keeps what it throws or rejects with from the caller: it is
|
|
5
|
+
* reported as `subscriber`, so one failing subscriber neither stops the others nor breaks whatever
|
|
6
|
+
* delivered the value.
|
|
7
|
+
*/
|
|
8
|
+
export const deliverSafely = (report) =>
|
|
9
|
+
tryCatchAsync(async (callback, value) => {
|
|
10
|
+
await callback(value);
|
|
11
|
+
})((error) => report(error, { source: 'subscriber' }));
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The callbacks subscribed to one source of values. Each subscription is its own entry, so its undo
|
|
15
|
+
* removes it alone even when the same callback is subscribed twice. `notify` delivers to every
|
|
16
|
+
* callback in subscription order, each isolated as `deliverSafely` describes.
|
|
17
|
+
*/
|
|
18
|
+
export const createSubscribers = (report) => {
|
|
19
|
+
const deliver = deliverSafely(report);
|
|
20
|
+
// eslint-disable-next-line functional/no-let -- The subscriptions outlive the independent subscribe, undo and notify calls; each change replaces the array, so a delivery in progress is never altered.
|
|
21
|
+
let entries = [];
|
|
22
|
+
|
|
23
|
+
const add = (callback) => {
|
|
24
|
+
const entry = { callback };
|
|
25
|
+
entries = [...entries, entry];
|
|
26
|
+
return () => {
|
|
27
|
+
entries = entries.filter((candidate) => candidate !== entry);
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Without a callback, every subscription goes.
|
|
32
|
+
const remove = (callback) => {
|
|
33
|
+
entries = callback === undefined ? [] : entries.filter((entry) => entry.callback !== callback);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const notify = (value) => entries.forEach(({ callback }) => deliver(callback, value));
|
|
37
|
+
|
|
38
|
+
return { add, remove, notify, deliver };
|
|
39
|
+
};
|
package/src/topicSegment.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { describeValue } from './guards.js';
|
|
2
|
+
|
|
1
3
|
const wildcards = ['+', '#'];
|
|
2
4
|
|
|
3
5
|
const invalidReasons = [
|
|
@@ -11,8 +13,6 @@ const invalidReasons = [
|
|
|
11
13
|
[(value) => value.startsWith('$'), "must not start with '$', which the broker reserves"],
|
|
12
14
|
];
|
|
13
15
|
|
|
14
|
-
const describeValue = (value) => JSON.stringify(value) ?? String(value);
|
|
15
|
-
|
|
16
16
|
const invalidReasonFor = (value) => {
|
|
17
17
|
const [, reason] = invalidReasons.find(([isInvalid]) => isInvalid(value)) ?? [];
|
|
18
18
|
return reason;
|
package/src/utils/either.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import * as R from 'ramda';
|
|
2
|
-
|
|
3
|
-
// Expected failures are values: a step returns Right(value) or Left(error) instead of throwing.
|
|
4
|
-
// Fantasy-land map/chain (non-enumerable) → R.map/R.chain work; toEqual compares { isLeft, value }.
|
|
5
|
-
// The methods come as one object to stay within the max-params lint rule.
|
|
6
|
-
const make = (isLeft, value, { map, chain }) =>
|
|
7
|
-
Object.freeze(
|
|
8
|
-
Object.defineProperties(
|
|
9
|
-
{ isLeft, value },
|
|
10
|
-
{ 'fantasy-land/map': { value: map }, 'fantasy-land/chain': { value: chain } },
|
|
11
|
-
),
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
export const Left = (value) =>
|
|
15
|
-
make(true, value, { map: () => Left(value), chain: () => Left(value) });
|
|
16
|
-
export const Right = (value) =>
|
|
17
|
-
make(false, value, { map: (f) => Right(f(value)), chain: (f) => f(value) });
|
|
18
|
-
export const either =
|
|
19
|
-
(onLeft) =>
|
|
20
|
-
(onRight) =>
|
|
21
|
-
({ isLeft, value }) =>
|
|
22
|
-
isLeft ? onLeft(value) : onRight(value);
|
|
23
|
-
|
|
24
|
-
export const fromNil = (error) => R.ifElse(R.isNil, R.always(Left(error)), Right);
|
|
25
|
-
|
|
26
|
-
export const tryCatchAsync =
|
|
27
|
-
(tryer) =>
|
|
28
|
-
(catcher) =>
|
|
29
|
-
async (...args) => {
|
|
30
|
-
try {
|
|
31
|
-
return await tryer(...args);
|
|
32
|
-
} catch (err) {
|
|
33
|
-
return catcher(err, ...args);
|
|
34
|
-
}
|
|
35
|
-
};
|