@welshman/app 0.0.2 → 0.0.4
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/build/src/collection.cjs +24 -11
- package/build/src/collection.cjs.map +1 -1
- package/build/src/collection.mjs +24 -11
- package/build/src/collection.mjs.map +1 -1
- package/build/src/context.cjs +38 -0
- package/build/src/context.cjs.map +1 -0
- package/build/src/context.d.ts +30 -0
- package/build/src/context.mjs +33 -0
- package/build/src/context.mjs.map +1 -0
- package/build/src/core.cjs +30 -8
- package/build/src/core.cjs.map +1 -1
- package/build/src/core.d.ts +7 -9
- package/build/src/core.mjs +31 -8
- package/build/src/core.mjs.map +1 -1
- package/build/src/follows.cjs +3 -7
- package/build/src/follows.cjs.map +1 -1
- package/build/src/follows.d.ts +1 -1
- package/build/src/follows.mjs +4 -8
- package/build/src/follows.mjs.map +1 -1
- package/build/src/handles.cjs +56 -14
- package/build/src/handles.cjs.map +1 -1
- package/build/src/handles.d.ts +10 -2
- package/build/src/handles.mjs +55 -15
- package/build/src/handles.mjs.map +1 -1
- package/build/src/index.cjs +2 -8
- package/build/src/index.cjs.map +1 -1
- package/build/src/index.d.ts +2 -0
- package/build/src/index.mjs +2 -8
- package/build/src/index.mjs.map +1 -1
- package/build/src/mutes.cjs +3 -7
- package/build/src/mutes.cjs.map +1 -1
- package/build/src/mutes.d.ts +1 -1
- package/build/src/mutes.mjs +4 -8
- package/build/src/mutes.mjs.map +1 -1
- package/build/src/profiles.cjs +11 -11
- package/build/src/profiles.cjs.map +1 -1
- package/build/src/profiles.d.ts +5 -6
- package/build/src/profiles.mjs +14 -14
- package/build/src/profiles.mjs.map +1 -1
- package/build/src/relaySelections.cjs +14 -8
- package/build/src/relaySelections.cjs.map +1 -1
- package/build/src/relaySelections.d.ts +10 -4
- package/build/src/relaySelections.mjs +13 -8
- package/build/src/relaySelections.mjs.map +1 -1
- package/build/src/relays.cjs +22 -28
- package/build/src/relays.cjs.map +1 -1
- package/build/src/relays.d.ts +1 -1
- package/build/src/relays.mjs +23 -29
- package/build/src/relays.mjs.map +1 -1
- package/build/src/router.cjs +375 -0
- package/build/src/router.cjs.map +1 -0
- package/build/src/router.d.ts +197 -0
- package/build/src/router.mjs +355 -0
- package/build/src/router.mjs.map +1 -0
- package/build/src/session.cjs +3 -4
- package/build/src/session.cjs.map +1 -1
- package/build/src/session.mjs +4 -5
- package/build/src/session.mjs.map +1 -1
- package/build/src/storage.cjs +23 -13
- package/build/src/storage.cjs.map +1 -1
- package/build/src/storage.d.ts +26 -2
- package/build/src/storage.mjs +24 -14
- package/build/src/storage.mjs.map +1 -1
- package/build/src/zappers.cjs +37 -16
- package/build/src/zappers.cjs.map +1 -1
- package/build/src/zappers.d.ts +4 -3
- package/build/src/zappers.mjs +37 -17
- package/build/src/zappers.mjs.map +1 -1
- package/package.json +6 -6
package/build/src/relays.cjs
CHANGED
|
@@ -6,7 +6,6 @@ const store_1 = require("svelte/store");
|
|
|
6
6
|
const store_2 = require("@welshman/store");
|
|
7
7
|
const lib_1 = require("@welshman/lib");
|
|
8
8
|
const net_1 = require("@welshman/net");
|
|
9
|
-
const core_1 = require("./core.cjs");
|
|
10
9
|
const util_1 = require("./util.cjs");
|
|
11
10
|
const collection_1 = require("./collection.cjs");
|
|
12
11
|
// Relays
|
|
@@ -23,9 +22,9 @@ exports.makeRelayStats = makeRelayStats;
|
|
|
23
22
|
exports.relays = (0, store_2.withGetter)((0, store_1.writable)([]));
|
|
24
23
|
exports.relaysByPubkey = (0, store_1.derived)(exports.relays, $relays => (0, lib_1.groupBy)($relay => { var _a; return (_a = $relay.profile) === null || _a === void 0 ? void 0 : _a.pubkey; }, $relays.filter($relay => { var _a; return (_a = $relay.profile) === null || _a === void 0 ? void 0 : _a.pubkey; })));
|
|
25
24
|
const fetchRelayProfiles = (urls) => {
|
|
26
|
-
const base =
|
|
25
|
+
const base = lib_1.ctx.app.dufflepudUrl;
|
|
27
26
|
if (!base) {
|
|
28
|
-
throw new Error("
|
|
27
|
+
throw new Error("ctx.app.dufflepudUrl is required to fetch relay metadata");
|
|
29
28
|
}
|
|
30
29
|
const res = (0, lib_1.postJson)(`${base}/relay/info`, { urls });
|
|
31
30
|
const profilesByUrl = new Map();
|
|
@@ -58,41 +57,35 @@ exports.relaySearch = (0, store_1.derived)(exports.relays, $relays => (0, util_1
|
|
|
58
57
|
},
|
|
59
58
|
}));
|
|
60
59
|
const updateRelayStats = (0, lib_1.batch)(500, (updates) => {
|
|
61
|
-
const updatesByUrl = (0, lib_1.groupBy)((0, lib_1.nth)(0), updates);
|
|
62
60
|
exports.relays.update($relays => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
const $relaysByUrl = (0, lib_1.indexBy)(r => r.url, $relays);
|
|
62
|
+
const $itemsByUrl = (0, lib_1.groupBy)(([url]) => url, updates);
|
|
63
|
+
for (const [url, items] of $itemsByUrl.entries()) {
|
|
64
|
+
const $relay = $relaysByUrl.get(url) || { url };
|
|
65
|
+
if (!$relay.stats) {
|
|
66
|
+
$relay.stats = (0, exports.makeRelayStats)();
|
|
67
|
+
}
|
|
68
|
+
for (const [_, update] of items) {
|
|
68
69
|
update($relay.stats);
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
}
|
|
71
|
+
$relaysByUrl.set(url, $relay);
|
|
72
|
+
}
|
|
73
|
+
return Array.from($relaysByUrl.values());
|
|
72
74
|
});
|
|
73
75
|
});
|
|
74
|
-
const onConnectionError = ({ url }) => updateRelayStats([url, stats => {
|
|
75
|
-
stats.recent_errors = stats.recent_errors.concat((0, lib_1.now)()).slice(-10);
|
|
76
|
-
}]);
|
|
77
76
|
const onConnectionSend = ({ url }, socketMessage) => {
|
|
78
77
|
const [verb] = (0, net_1.asMessage)(socketMessage);
|
|
79
78
|
if (verb === 'REQ') {
|
|
80
|
-
updateRelayStats([url, stats =>
|
|
81
|
-
stats.request_count = stats.request_count + 1;
|
|
82
|
-
}]);
|
|
79
|
+
updateRelayStats([url, stats => ++stats.request_count]);
|
|
83
80
|
}
|
|
84
81
|
else if (verb === 'EVENT') {
|
|
85
|
-
updateRelayStats([url, stats =>
|
|
86
|
-
stats.publish_count = stats.publish_count + 1;
|
|
87
|
-
}]);
|
|
82
|
+
updateRelayStats([url, stats => ++stats.publish_count]);
|
|
88
83
|
}
|
|
89
84
|
};
|
|
90
85
|
const onConnectionReceive = ({ url }, socketMessage) => {
|
|
91
86
|
const [verb] = (0, net_1.asMessage)(socketMessage);
|
|
92
87
|
if (verb === 'EVENT') {
|
|
93
|
-
updateRelayStats([url, stats =>
|
|
94
|
-
stats.event_count = stats.event_count + 1;
|
|
95
|
-
}]);
|
|
88
|
+
updateRelayStats([url, stats => ++stats.event_count]);
|
|
96
89
|
}
|
|
97
90
|
else if (verb === 'OK') {
|
|
98
91
|
updateRelayStats([url, stats => {
|
|
@@ -105,17 +98,18 @@ const onConnectionReceive = ({ url }, socketMessage) => {
|
|
|
105
98
|
}]);
|
|
106
99
|
}
|
|
107
100
|
};
|
|
101
|
+
const onConnectionFault = ({ url }) => updateRelayStats([url, stats => {
|
|
102
|
+
stats.recent_errors = stats.recent_errors.concat((0, lib_1.now)()).slice(-10);
|
|
103
|
+
}]);
|
|
108
104
|
const trackRelayStats = (connection) => {
|
|
109
|
-
updateRelayStats([connection.url, stats =>
|
|
110
|
-
stats.connect_count = stats.connect_count + 1;
|
|
111
|
-
}]);
|
|
112
|
-
connection.on('error', onConnectionError);
|
|
105
|
+
updateRelayStats([connection.url, stats => ++stats.connect_count]);
|
|
113
106
|
connection.on('send', onConnectionSend);
|
|
114
107
|
connection.on('receive', onConnectionReceive);
|
|
108
|
+
connection.on('fault', onConnectionFault);
|
|
115
109
|
return () => {
|
|
116
|
-
connection.off('error', onConnectionError);
|
|
117
110
|
connection.off('send', onConnectionSend);
|
|
118
111
|
connection.off('receive', onConnectionReceive);
|
|
112
|
+
connection.off('fault', onConnectionFault);
|
|
119
113
|
};
|
|
120
114
|
};
|
|
121
115
|
exports.trackRelayStats = trackRelayStats;
|
package/build/src/relays.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relays.cjs","sourceRoot":"","sources":["../../src/relays.ts"],"names":[],"mappings":";;;;AAAA,wCAA8C;AAC9C,2CAA0C;AAC1C,
|
|
1
|
+
{"version":3,"file":"relays.cjs","sourceRoot":"","sources":["../../src/relays.ts"],"names":[],"mappings":";;;;AAAA,wCAA8C;AAC9C,2CAA0C;AAC1C,uCAAgG;AAEhG,uCAAwF;AACxF,qCAAmC;AACnC,iDAAuC;AAYvC,SAAS;AAEF,MAAM,cAAc,GAAG,GAAe,EAAE,CAAC,CAAC;IAC/C,UAAU,EAAE,IAAA,SAAG,GAAE;IACjB,WAAW,EAAE,CAAC;IACd,aAAa,EAAE,CAAC;IAChB,aAAa,EAAE,CAAC;IAChB,aAAa,EAAE,CAAC;IAChB,aAAa,EAAE,EAAE;IACjB,gBAAgB,EAAE,gBAAU,CAAC,OAAO;CACrC,CAAC,CAAA;AARW,QAAA,cAAc,kBAQzB;AAQW,QAAA,MAAM,GAAG,IAAA,kBAAU,EAAC,IAAA,gBAAQ,EAAU,EAAE,CAAC,CAAC,CAAA;AAE1C,QAAA,cAAc,GAAG,IAAA,eAAO,EAAC,cAAM,EAAE,OAAO,CAAC,EAAE,CACtD,IAAA,aAAO,EACL,MAAM,CAAC,EAAE,WAAC,OAAA,MAAA,MAAM,CAAC,OAAO,0CAAE,MAAM,CAAA,EAAA,EAChC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,WAAC,OAAA,MAAA,MAAM,CAAC,OAAO,0CAAE,MAAM,CAAA,EAAA,CAAC,CACjD,CACF,CAAA;AAEM,MAAM,kBAAkB,GAAG,CAAC,IAAc,EAAE,EAAE;IACnD,MAAM,IAAI,GAAG,SAAG,CAAC,GAAG,CAAC,YAAa,CAAA;IAElC,IAAI,CAAC,IAAI,EAAE;QACT,MAAM,IAAI,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AAE;QAC1B,gBAAgB,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE;gBAC7B,KAAK,CAAC,gBAAgB,GAAG,gBAAU,CAAC,YAAY,CAAA;YAClD,CAAC,CAAC,CAAC,CAAA;KACJ;AACH,CAAC,CAAA;AAED,MAAM,iBAAiB,GAAG,CAAC,EAAC,GAAG,EAAa,EAAE,EAAE,CAC9C,gBAAgB,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE;QAC7B,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,IAAA,SAAG,GAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAA;IACpE,CAAC,CAAC,CAAC,CAAA;AAEE,MAAM,eAAe,GAAG,CAAC,UAAsB,EAAE,EAAE;IACxD,gBAAgB,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAA;IAElE,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;IACvC,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;IAC7C,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;IAEzC,OAAO,GAAG,EAAE;QACV,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;QACxC,UAAU,CAAC,GAAG,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;QAC9C,UAAU,CAAC,GAAG,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;IAC5C,CAAC,CAAA;AACH,CAAC,CAAA;AAZY,QAAA,eAAe,mBAY3B"}
|
package/build/src/relays.d.ts
CHANGED
package/build/src/relays.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { writable, derived } from 'svelte/store';
|
|
2
2
|
import { withGetter } from '@welshman/store';
|
|
3
|
-
import { groupBy,
|
|
3
|
+
import { ctx, groupBy, indexBy, batch, now, uniq, uniqBy, batcher, postJson } from '@welshman/lib';
|
|
4
4
|
import { AuthStatus, asMessage } from '@welshman/net';
|
|
5
|
-
import { env } from "./core.mjs";
|
|
6
5
|
import { createSearch } from "./util.mjs";
|
|
7
6
|
import { collection } from "./collection.mjs";
|
|
8
7
|
// Relays
|
|
@@ -18,9 +17,9 @@ export const makeRelayStats = () => ({
|
|
|
18
17
|
export const relays = withGetter(writable([]));
|
|
19
18
|
export const relaysByPubkey = derived(relays, $relays => groupBy($relay => { var _a; return (_a = $relay.profile) === null || _a === void 0 ? void 0 : _a.pubkey; }, $relays.filter($relay => { var _a; return (_a = $relay.profile) === null || _a === void 0 ? void 0 : _a.pubkey; })));
|
|
20
19
|
export const fetchRelayProfiles = (urls) => {
|
|
21
|
-
const base =
|
|
20
|
+
const base = ctx.app.dufflepudUrl;
|
|
22
21
|
if (!base) {
|
|
23
|
-
throw new Error("
|
|
22
|
+
throw new Error("ctx.app.dufflepudUrl is required to fetch relay metadata");
|
|
24
23
|
}
|
|
25
24
|
const res = postJson(`${base}/relay/info`, { urls });
|
|
26
25
|
const profilesByUrl = new Map();
|
|
@@ -52,41 +51,35 @@ export const relaySearch = derived(relays, $relays => createSearch($relays, {
|
|
|
52
51
|
},
|
|
53
52
|
}));
|
|
54
53
|
const updateRelayStats = batch(500, (updates) => {
|
|
55
|
-
const updatesByUrl = groupBy(nth(0), updates);
|
|
56
54
|
relays.update($relays => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
const $relaysByUrl = indexBy(r => r.url, $relays);
|
|
56
|
+
const $itemsByUrl = groupBy(([url]) => url, updates);
|
|
57
|
+
for (const [url, items] of $itemsByUrl.entries()) {
|
|
58
|
+
const $relay = $relaysByUrl.get(url) || { url };
|
|
59
|
+
if (!$relay.stats) {
|
|
60
|
+
$relay.stats = makeRelayStats();
|
|
61
|
+
}
|
|
62
|
+
for (const [_, update] of items) {
|
|
62
63
|
update($relay.stats);
|
|
63
64
|
}
|
|
64
|
-
|
|
65
|
-
}
|
|
65
|
+
$relaysByUrl.set(url, $relay);
|
|
66
|
+
}
|
|
67
|
+
return Array.from($relaysByUrl.values());
|
|
66
68
|
});
|
|
67
69
|
});
|
|
68
|
-
const onConnectionError = ({ url }) => updateRelayStats([url, stats => {
|
|
69
|
-
stats.recent_errors = stats.recent_errors.concat(now()).slice(-10);
|
|
70
|
-
}]);
|
|
71
70
|
const onConnectionSend = ({ url }, socketMessage) => {
|
|
72
71
|
const [verb] = asMessage(socketMessage);
|
|
73
72
|
if (verb === 'REQ') {
|
|
74
|
-
updateRelayStats([url, stats =>
|
|
75
|
-
stats.request_count = stats.request_count + 1;
|
|
76
|
-
}]);
|
|
73
|
+
updateRelayStats([url, stats => ++stats.request_count]);
|
|
77
74
|
}
|
|
78
75
|
else if (verb === 'EVENT') {
|
|
79
|
-
updateRelayStats([url, stats =>
|
|
80
|
-
stats.publish_count = stats.publish_count + 1;
|
|
81
|
-
}]);
|
|
76
|
+
updateRelayStats([url, stats => ++stats.publish_count]);
|
|
82
77
|
}
|
|
83
78
|
};
|
|
84
79
|
const onConnectionReceive = ({ url }, socketMessage) => {
|
|
85
80
|
const [verb] = asMessage(socketMessage);
|
|
86
81
|
if (verb === 'EVENT') {
|
|
87
|
-
updateRelayStats([url, stats =>
|
|
88
|
-
stats.event_count = stats.event_count + 1;
|
|
89
|
-
}]);
|
|
82
|
+
updateRelayStats([url, stats => ++stats.event_count]);
|
|
90
83
|
}
|
|
91
84
|
else if (verb === 'OK') {
|
|
92
85
|
updateRelayStats([url, stats => {
|
|
@@ -99,17 +92,18 @@ const onConnectionReceive = ({ url }, socketMessage) => {
|
|
|
99
92
|
}]);
|
|
100
93
|
}
|
|
101
94
|
};
|
|
95
|
+
const onConnectionFault = ({ url }) => updateRelayStats([url, stats => {
|
|
96
|
+
stats.recent_errors = stats.recent_errors.concat(now()).slice(-10);
|
|
97
|
+
}]);
|
|
102
98
|
export const trackRelayStats = (connection) => {
|
|
103
|
-
updateRelayStats([connection.url, stats =>
|
|
104
|
-
stats.connect_count = stats.connect_count + 1;
|
|
105
|
-
}]);
|
|
106
|
-
connection.on('error', onConnectionError);
|
|
99
|
+
updateRelayStats([connection.url, stats => ++stats.connect_count]);
|
|
107
100
|
connection.on('send', onConnectionSend);
|
|
108
101
|
connection.on('receive', onConnectionReceive);
|
|
102
|
+
connection.on('fault', onConnectionFault);
|
|
109
103
|
return () => {
|
|
110
|
-
connection.off('error', onConnectionError);
|
|
111
104
|
connection.off('send', onConnectionSend);
|
|
112
105
|
connection.off('receive', onConnectionReceive);
|
|
106
|
+
connection.off('fault', onConnectionFault);
|
|
113
107
|
};
|
|
114
108
|
};
|
|
115
109
|
//# sourceMappingURL=relays.mjs.map
|
package/build/src/relays.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"relays.mjs","sourceRoot":"","sources":["../../src/relays.ts"],"names":[],"mappings":"OAAO,EAAC,QAAQ,EAAE,OAAO,EAAC,MAAM,cAAc;OACvC,EAAC,UAAU,EAAC,MAAM,iBAAiB;OACnC,EAAC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"relays.mjs","sourceRoot":"","sources":["../../src/relays.ts"],"names":[],"mappings":"OAAO,EAAC,QAAQ,EAAE,OAAO,EAAC,MAAM,cAAc;OACvC,EAAC,UAAU,EAAC,MAAM,iBAAiB;OACnC,EAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAC,MAAM,eAAe;OAEzF,EAAC,UAAU,EAAE,SAAS,EAAsC,MAAM,eAAe;OACjF,EAAC,YAAY,EAAC;OACd,EAAC,UAAU,EAAC;AAYnB,SAAS;AAET,MAAM,CAAC,MAAM,cAAc,GAAG,GAAe,EAAE,CAAC,CAAC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D,gBAAgB,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC,CAAA;IAElE,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;IACvC,UAAU,CAAC,EAAE,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;IAC7C,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;IAEzC,OAAO,GAAG,EAAE;QACV,UAAU,CAAC,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAA;QACxC,UAAU,CAAC,GAAG,CAAC,SAAS,EAAE,mBAAmB,CAAC,CAAA;QAC9C,UAAU,CAAC,GAAG,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAA;IAC5C,CAAC,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFilterSelections = exports.defaultFilterSelectionRules = exports.getFilterSelectionsForUser = exports.getFilterSelectionsForAuthors = exports.getFilterSelectionsForIndexedKinds = exports.getFilterSelectionsForContext = exports.getFilterSelectionsForSearch = exports.getFilterSelectionsForLocalRelay = exports.makeFilterSelection = exports.makeRouter = exports.getSearchRelays = exports.getFallbackRelays = exports.getIndexerRelays = exports.getPubkeyRelays = exports.getRelayQuality = exports.RouterScenario = exports.Router = exports.addMaximalFallbacks = exports.addMinimalFallbacks = exports.addNoFallbacks = exports.RelayMode = exports.INDEXED_KINDS = void 0;
|
|
4
|
+
const lib_1 = require("@welshman/lib");
|
|
5
|
+
const util_1 = require("@welshman/util");
|
|
6
|
+
const net_1 = require("@welshman/net");
|
|
7
|
+
const session_1 = require("./session.cjs");
|
|
8
|
+
const relaySelections_1 = require("./relaySelections.cjs");
|
|
9
|
+
const relays_1 = require("./relays.cjs");
|
|
10
|
+
exports.INDEXED_KINDS = [util_1.PROFILE, util_1.RELAYS, util_1.INBOX_RELAYS, util_1.FOLLOWS];
|
|
11
|
+
var RelayMode;
|
|
12
|
+
(function (RelayMode) {
|
|
13
|
+
RelayMode["Read"] = "read";
|
|
14
|
+
RelayMode["Write"] = "write";
|
|
15
|
+
RelayMode["Inbox"] = "inbox";
|
|
16
|
+
})(RelayMode || (exports.RelayMode = RelayMode = {}));
|
|
17
|
+
const addNoFallbacks = (count, redundancy) => 0;
|
|
18
|
+
exports.addNoFallbacks = addNoFallbacks;
|
|
19
|
+
const addMinimalFallbacks = (count, redundancy) => count > 0 ? 0 : 1;
|
|
20
|
+
exports.addMinimalFallbacks = addMinimalFallbacks;
|
|
21
|
+
const addMaximalFallbacks = (count, redundancy) => redundancy - count;
|
|
22
|
+
exports.addMaximalFallbacks = addMaximalFallbacks;
|
|
23
|
+
class Router {
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.options = options;
|
|
26
|
+
// Utilities derived from options
|
|
27
|
+
this.getPubkeySelection = (pubkey, mode) => { var _a, _b; return this.selection(pubkey, ((_b = (_a = this.options).getPubkeyRelays) === null || _b === void 0 ? void 0 : _b.call(_a, pubkey, mode)) || []); };
|
|
28
|
+
this.getPubkeySelections = (pubkeys, mode) => pubkeys.map(pubkey => this.getPubkeySelection(pubkey, mode));
|
|
29
|
+
this.getUserSelections = (mode) => { var _a, _b; return this.getPubkeySelections([(_b = (_a = this.options).getUserPubkey) === null || _b === void 0 ? void 0 : _b.call(_a)].filter(lib_1.identity), mode); };
|
|
30
|
+
this.getContextSelections = (tags) => {
|
|
31
|
+
return [
|
|
32
|
+
...tags.communities().mapTo(t => { var _a, _b; return this.selection(t.value(), ((_b = (_a = this.options).getCommunityRelays) === null || _b === void 0 ? void 0 : _b.call(_a, t.value())) || []); }).valueOf(),
|
|
33
|
+
...tags.groups().mapTo(t => { var _a, _b; return this.selection(t.value(), ((_b = (_a = this.options).getGroupRelays) === null || _b === void 0 ? void 0 : _b.call(_a, t.value())) || []); }).valueOf(),
|
|
34
|
+
];
|
|
35
|
+
};
|
|
36
|
+
// Utilities for creating ValueRelays
|
|
37
|
+
this.selection = (value, relays) => ({ value, relays: Array.from(relays) });
|
|
38
|
+
this.selections = (values, relays) => values.map(value => this.selection(value, relays));
|
|
39
|
+
this.forceValue = (value, selections) => selections.map(({ relays }) => this.selection(value, relays));
|
|
40
|
+
// Utilities for processing hints
|
|
41
|
+
this.relaySelectionsFromMap = (valuesByRelay) => (0, lib_1.sortBy)(({ values }) => -values.length, Array.from(valuesByRelay)
|
|
42
|
+
.map(([relay, values]) => ({ relay, values: (0, lib_1.uniq)(values) })));
|
|
43
|
+
this.scoreRelaySelection = ({ values, relay }) => { var _a, _b; return values.length * (((_b = (_a = this.options).getRelayQuality) === null || _b === void 0 ? void 0 : _b.call(_a, relay)) || 1); };
|
|
44
|
+
this.sortRelaySelections = (relaySelections) => {
|
|
45
|
+
const scores = new Map();
|
|
46
|
+
const getScore = (relayValues) => -(scores.get(relayValues.relay) || 0);
|
|
47
|
+
for (const relayValues of relaySelections) {
|
|
48
|
+
scores.set(relayValues.relay, this.scoreRelaySelection(relayValues));
|
|
49
|
+
}
|
|
50
|
+
return (0, lib_1.sortBy)(getScore, relaySelections.filter(getScore));
|
|
51
|
+
};
|
|
52
|
+
// Utilities for creating scenarios
|
|
53
|
+
this.scenario = (selections) => new RouterScenario(this, selections);
|
|
54
|
+
this.merge = (scenarios) => this.scenario(scenarios.flatMap((scenario) => scenario.selections));
|
|
55
|
+
this.product = (values, relays) => this.scenario(this.selections(values, relays));
|
|
56
|
+
this.fromRelays = (relays) => this.scenario([this.selection("", relays)]);
|
|
57
|
+
// Routing scenarios
|
|
58
|
+
this.User = () => this.scenario(this.getUserSelections());
|
|
59
|
+
this.ReadRelays = () => this.scenario(this.getUserSelections(RelayMode.Read));
|
|
60
|
+
this.WriteRelays = () => this.scenario(this.getUserSelections(RelayMode.Write));
|
|
61
|
+
this.Messages = (pubkeys) => this.scenario([
|
|
62
|
+
...this.getUserSelections(),
|
|
63
|
+
...this.getPubkeySelections(pubkeys),
|
|
64
|
+
]);
|
|
65
|
+
this.PublishMessage = (pubkey) => this.scenario([
|
|
66
|
+
...this.getUserSelections(RelayMode.Inbox),
|
|
67
|
+
this.getPubkeySelection(pubkey, RelayMode.Inbox),
|
|
68
|
+
]).policy(exports.addMinimalFallbacks);
|
|
69
|
+
this.Event = (event) => this.scenario(this.forceValue(event.id, [
|
|
70
|
+
this.getPubkeySelection(event.pubkey, RelayMode.Write),
|
|
71
|
+
...this.getContextSelections(util_1.Tags.fromEvent(event).context()),
|
|
72
|
+
]));
|
|
73
|
+
this.EventChildren = (event) => this.scenario(this.forceValue(event.id, [
|
|
74
|
+
this.getPubkeySelection(event.pubkey, RelayMode.Read),
|
|
75
|
+
...this.getContextSelections(util_1.Tags.fromEvent(event).context()),
|
|
76
|
+
]));
|
|
77
|
+
this.EventAncestors = (event, type) => {
|
|
78
|
+
var _a, _b;
|
|
79
|
+
const tags = util_1.Tags.fromEvent(event);
|
|
80
|
+
const ancestors = tags.ancestors()[type];
|
|
81
|
+
const pubkeys = tags.values("p").valueOf();
|
|
82
|
+
const communities = tags.communities().values().valueOf();
|
|
83
|
+
const groups = tags.groups().values().valueOf();
|
|
84
|
+
const relays = (0, lib_1.uniq)([
|
|
85
|
+
...((_b = (_a = this.options).getPubkeyRelays) === null || _b === void 0 ? void 0 : _b.call(_a, event.pubkey, RelayMode.Read)) || [],
|
|
86
|
+
...pubkeys.flatMap((k) => { var _a, _b; return ((_b = (_a = this.options).getPubkeyRelays) === null || _b === void 0 ? void 0 : _b.call(_a, k, RelayMode.Write)) || []; }),
|
|
87
|
+
...communities.flatMap((a) => { var _a, _b; return ((_b = (_a = this.options).getCommunityRelays) === null || _b === void 0 ? void 0 : _b.call(_a, a)) || []; }),
|
|
88
|
+
...groups.flatMap((a) => { var _a, _b; return ((_b = (_a = this.options).getGroupRelays) === null || _b === void 0 ? void 0 : _b.call(_a, a)) || []; }),
|
|
89
|
+
...ancestors.relays().valueOf(),
|
|
90
|
+
]);
|
|
91
|
+
return this.product(ancestors.values().valueOf(), relays);
|
|
92
|
+
};
|
|
93
|
+
this.EventMentions = (event) => this.EventAncestors(event, "mentions");
|
|
94
|
+
this.EventParents = (event) => this.EventAncestors(event, "replies");
|
|
95
|
+
this.EventRoots = (event) => this.EventAncestors(event, "roots");
|
|
96
|
+
this.PublishEvent = (event) => {
|
|
97
|
+
const tags = util_1.Tags.fromEvent(event);
|
|
98
|
+
const mentions = tags.values("p").valueOf();
|
|
99
|
+
// If we're publishing to private groups, only publish to those groups' relays
|
|
100
|
+
if (tags.groups().exists()) {
|
|
101
|
+
return this
|
|
102
|
+
.scenario(this.getContextSelections(tags.groups()))
|
|
103
|
+
.policy(exports.addNoFallbacks);
|
|
104
|
+
}
|
|
105
|
+
return this.scenario(this.forceValue(event.id, [
|
|
106
|
+
this.getPubkeySelection(event.pubkey, RelayMode.Write),
|
|
107
|
+
...this.getContextSelections(tags.context()),
|
|
108
|
+
...this.getPubkeySelections(mentions, RelayMode.Read),
|
|
109
|
+
]));
|
|
110
|
+
};
|
|
111
|
+
this.FromPubkeys = (pubkeys) => this.scenario(this.getPubkeySelections(pubkeys, RelayMode.Write));
|
|
112
|
+
this.ForPubkeys = (pubkeys) => this.scenario(this.getPubkeySelections(pubkeys, RelayMode.Read));
|
|
113
|
+
this.WithinGroup = (address, relays) => this
|
|
114
|
+
.scenario(this.getContextSelections(util_1.Tags.wrap([["a", address]])))
|
|
115
|
+
.policy(exports.addNoFallbacks);
|
|
116
|
+
this.WithinCommunity = (address) => this.scenario(this.getContextSelections(util_1.Tags.wrap([["a", address]])));
|
|
117
|
+
this.WithinContext = (address) => {
|
|
118
|
+
if ((0, util_1.isGroupAddress)(address)) {
|
|
119
|
+
return this.WithinGroup(address);
|
|
120
|
+
}
|
|
121
|
+
if ((0, util_1.isCommunityAddress)(address)) {
|
|
122
|
+
return this.WithinCommunity(address);
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Unknown context ${address}`);
|
|
125
|
+
};
|
|
126
|
+
this.WithinMultipleContexts = (addresses) => this.merge(addresses.map(this.WithinContext));
|
|
127
|
+
this.Search = (term, relays = []) => { var _a, _b; return this.product([term], (0, lib_1.uniq)(relays.concat(((_b = (_a = this.options).getSearchRelays) === null || _b === void 0 ? void 0 : _b.call(_a)) || []))); };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.Router = Router;
|
|
131
|
+
class RouterScenario {
|
|
132
|
+
constructor(router, selections, options = {}) {
|
|
133
|
+
this.router = router;
|
|
134
|
+
this.selections = selections;
|
|
135
|
+
this.options = options;
|
|
136
|
+
this.clone = (options) => new RouterScenario(this.router, this.selections, { ...this.options, ...options });
|
|
137
|
+
this.filter = (f) => new RouterScenario(this.router, this.selections.filter(selection => f(selection)), this.options);
|
|
138
|
+
this.update = (f) => new RouterScenario(this.router, this.selections.map(selection => f(selection)), this.options);
|
|
139
|
+
this.redundancy = (redundancy) => this.clone({ redundancy });
|
|
140
|
+
this.policy = (policy) => this.clone({ policy });
|
|
141
|
+
this.limit = (limit) => this.clone({ limit });
|
|
142
|
+
this.getRedundancy = () => { var _a, _b; return this.options.redundancy || ((_b = (_a = this.router.options).getRedundancy) === null || _b === void 0 ? void 0 : _b.call(_a)) || 3; };
|
|
143
|
+
this.getPolicy = () => this.options.policy || exports.addMaximalFallbacks;
|
|
144
|
+
this.getLimit = () => { var _a, _b; return this.options.limit || ((_b = (_a = this.router.options).getLimit) === null || _b === void 0 ? void 0 : _b.call(_a)) || 10; };
|
|
145
|
+
this.getSelections = () => {
|
|
146
|
+
const allValues = new Set();
|
|
147
|
+
const valuesByRelay = new Map();
|
|
148
|
+
for (const { value, relays } of this.selections) {
|
|
149
|
+
allValues.add(value);
|
|
150
|
+
for (const relay of relays) {
|
|
151
|
+
if ((0, util_1.isShareableRelayUrl)(relay)) {
|
|
152
|
+
(0, lib_1.pushToMapKey)(valuesByRelay, relay, value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Adjust redundancy by limit, since if we're looking for very specific values odds
|
|
157
|
+
// are we're less tolerant of failure. Add more redundancy to fill our relay limit.
|
|
158
|
+
const limit = this.getLimit();
|
|
159
|
+
const redundancy = this.getRedundancy();
|
|
160
|
+
const adjustedRedundancy = Math.max(redundancy, redundancy * (limit / (allValues.size * redundancy)));
|
|
161
|
+
const seen = new Map();
|
|
162
|
+
const result = new Map();
|
|
163
|
+
const relaySelections = this.router.relaySelectionsFromMap(valuesByRelay);
|
|
164
|
+
for (const { relay } of this.router.sortRelaySelections(relaySelections)) {
|
|
165
|
+
const values = new Set();
|
|
166
|
+
for (const value of (0, lib_1.uniq)(valuesByRelay.get(relay) || [])) {
|
|
167
|
+
const timesSeen = seen.get(value) || 0;
|
|
168
|
+
if (timesSeen < adjustedRedundancy) {
|
|
169
|
+
seen.set(value, timesSeen + 1);
|
|
170
|
+
values.add(value);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (values.size > 0) {
|
|
174
|
+
result.set(relay, Array.from(values));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const fallbacks = (0, lib_1.shuffle)(this.router.options.getFallbackRelays());
|
|
178
|
+
const fallbackPolicy = this.getPolicy();
|
|
179
|
+
for (const { value } of this.selections) {
|
|
180
|
+
const timesSeen = seen.get(value) || 0;
|
|
181
|
+
const fallbacksNeeded = fallbackPolicy(timesSeen, adjustedRedundancy);
|
|
182
|
+
if (fallbacksNeeded > 0) {
|
|
183
|
+
for (const relay of fallbacks.slice(0, fallbacksNeeded)) {
|
|
184
|
+
(0, lib_1.pushToMapKey)(result, relay, value);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const [keep, discard] = (0, lib_1.splitAt)(limit, this.router.relaySelectionsFromMap(result));
|
|
189
|
+
for (const target of keep.slice(0, redundancy)) {
|
|
190
|
+
target.values = (0, lib_1.uniq)(discard.concat(target).flatMap((selection) => selection.values));
|
|
191
|
+
}
|
|
192
|
+
return keep;
|
|
193
|
+
};
|
|
194
|
+
this.getUrls = () => this.getSelections().map((selection) => selection.relay);
|
|
195
|
+
this.getUrl = () => (0, lib_1.first)(this.getUrls());
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.RouterScenario = RouterScenario;
|
|
199
|
+
// Default router options
|
|
200
|
+
const getRelayQuality = (url) => {
|
|
201
|
+
var _a, _b;
|
|
202
|
+
const oneMinute = 60 * 1000;
|
|
203
|
+
const oneHour = 60 * oneMinute;
|
|
204
|
+
const oneDay = 24 * oneHour;
|
|
205
|
+
const oneWeek = 7 * oneDay;
|
|
206
|
+
const relay = relays_1.relaysByUrl.get().get(url);
|
|
207
|
+
const connect_count = ((_a = relay === null || relay === void 0 ? void 0 : relay.stats) === null || _a === void 0 ? void 0 : _a.connect_count) || 0;
|
|
208
|
+
const recent_errors = ((_b = relay === null || relay === void 0 ? void 0 : relay.stats) === null || _b === void 0 ? void 0 : _b.recent_errors) || [];
|
|
209
|
+
const connection = lib_1.ctx.net.pool.get(url, { autoConnect: false });
|
|
210
|
+
// If we haven't connected, consult our relay record and see if there has
|
|
211
|
+
// been a recent fault. If there has been, penalize the relay. If there have been several,
|
|
212
|
+
// don't use the relay.
|
|
213
|
+
if (!connection) {
|
|
214
|
+
const lastFault = (0, lib_1.last)(recent_errors) || 0;
|
|
215
|
+
if (recent_errors.filter(n => n > (0, lib_1.now)() - oneHour).length > 10) {
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
if (recent_errors.filter(n => n > (0, lib_1.now)() - oneDay).length > 50) {
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
if (recent_errors.filter(n => n > (0, lib_1.now)() - oneWeek).length > 100) {
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
return Math.max(0, Math.min(0.5, ((0, lib_1.now)() - oneMinute - lastFault) / oneHour));
|
|
225
|
+
}
|
|
226
|
+
return (0, lib_1.switcher)(connection.meta.getStatus(), {
|
|
227
|
+
[net_1.ConnectionStatus.Unauthorized]: 0.5,
|
|
228
|
+
[net_1.ConnectionStatus.Forbidden]: 0,
|
|
229
|
+
[net_1.ConnectionStatus.Error]: 0,
|
|
230
|
+
[net_1.ConnectionStatus.Closed]: 0.6,
|
|
231
|
+
[net_1.ConnectionStatus.Slow]: 0.5,
|
|
232
|
+
[net_1.ConnectionStatus.Ok]: 1,
|
|
233
|
+
default: (0, lib_1.clamp)([0.5, 1], connect_count / 1000),
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
exports.getRelayQuality = getRelayQuality;
|
|
237
|
+
const getPubkeyRelays = (pubkey, mode) => {
|
|
238
|
+
const $relaySelections = relaySelections_1.relaySelectionsByPubkey.get();
|
|
239
|
+
const $inboxSelections = relaySelections_1.relaySelectionsByPubkey.get();
|
|
240
|
+
switch (mode) {
|
|
241
|
+
case RelayMode.Read: return (0, relaySelections_1.getReadRelayUrls)($relaySelections.get(pubkey));
|
|
242
|
+
case RelayMode.Write: return (0, relaySelections_1.getWriteRelayUrls)($relaySelections.get(pubkey));
|
|
243
|
+
case RelayMode.Inbox: return (0, relaySelections_1.getRelayUrls)($inboxSelections.get(pubkey));
|
|
244
|
+
default: return (0, relaySelections_1.getRelayUrls)($relaySelections.get(pubkey));
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
exports.getPubkeyRelays = getPubkeyRelays;
|
|
248
|
+
const getIndexerRelays = () => lib_1.ctx.app.indexerRelays || (0, exports.getFallbackRelays)();
|
|
249
|
+
exports.getIndexerRelays = getIndexerRelays;
|
|
250
|
+
exports.getFallbackRelays = (0, lib_1.throttleWithValue)(300, () => (0, lib_1.sortBy)(r => -(0, exports.getRelayQuality)(r.url), relays_1.relays.get()).slice(0, 30).map(r => r.url));
|
|
251
|
+
exports.getSearchRelays = (0, lib_1.throttleWithValue)(300, () => (0, lib_1.sortBy)(r => -(0, exports.getRelayQuality)(r.url), relays_1.relays.get())
|
|
252
|
+
.filter(r => { var _a, _b; return (_b = (_a = r.profile) === null || _a === void 0 ? void 0 : _a.supported_nips) === null || _b === void 0 ? void 0 : _b.includes(50); })
|
|
253
|
+
.slice(0, 30).map(r => r.url));
|
|
254
|
+
const makeRouter = (options = {}) => new Router({
|
|
255
|
+
getPubkeyRelays: exports.getPubkeyRelays,
|
|
256
|
+
getIndexerRelays: exports.getIndexerRelays,
|
|
257
|
+
getFallbackRelays: exports.getFallbackRelays,
|
|
258
|
+
getSearchRelays: exports.getSearchRelays,
|
|
259
|
+
getRelayQuality: exports.getRelayQuality,
|
|
260
|
+
getUserPubkey: () => session_1.pubkey.get(),
|
|
261
|
+
getRedundancy: () => 2,
|
|
262
|
+
getLimit: () => 5,
|
|
263
|
+
...options,
|
|
264
|
+
});
|
|
265
|
+
exports.makeRouter = makeRouter;
|
|
266
|
+
const makeFilterSelection = (id, filter, scenario) => ({ id, filter, scenario });
|
|
267
|
+
exports.makeFilterSelection = makeFilterSelection;
|
|
268
|
+
const getFilterSelectionsForLocalRelay = (state) => {
|
|
269
|
+
const id = (0, util_1.getFilterId)(state.filter);
|
|
270
|
+
const scenario = lib_1.ctx.app.router.product([id], [util_1.LOCAL_RELAY_URL]);
|
|
271
|
+
state.selections.push((0, exports.makeFilterSelection)(id, state.filter, scenario));
|
|
272
|
+
return false;
|
|
273
|
+
};
|
|
274
|
+
exports.getFilterSelectionsForLocalRelay = getFilterSelectionsForLocalRelay;
|
|
275
|
+
const getFilterSelectionsForSearch = (state) => {
|
|
276
|
+
var _a, _b;
|
|
277
|
+
if (!state.filter.search)
|
|
278
|
+
return false;
|
|
279
|
+
const id = (0, util_1.getFilterId)(state.filter);
|
|
280
|
+
const relays = ((_b = (_a = lib_1.ctx.app.router.options).getSearchRelays) === null || _b === void 0 ? void 0 : _b.call(_a)) || [];
|
|
281
|
+
const scenario = lib_1.ctx.app.router.product([id], relays);
|
|
282
|
+
state.selections.push((0, exports.makeFilterSelection)(id, state.filter, scenario));
|
|
283
|
+
return true;
|
|
284
|
+
};
|
|
285
|
+
exports.getFilterSelectionsForSearch = getFilterSelectionsForSearch;
|
|
286
|
+
const getFilterSelectionsForContext = (state) => {
|
|
287
|
+
var _a;
|
|
288
|
+
const contexts = ((_a = state.filter["#a"]) === null || _a === void 0 ? void 0 : _a.filter(util_1.isContextAddress)) || [];
|
|
289
|
+
if (contexts.length === 0)
|
|
290
|
+
return false;
|
|
291
|
+
const scenario = lib_1.ctx.app.router.WithinMultipleContexts(contexts);
|
|
292
|
+
for (const { relay, values } of scenario.getSelections()) {
|
|
293
|
+
const contextFilter = { ...state.filter, "#a": Array.from(values) };
|
|
294
|
+
const id = (0, util_1.getFilterId)(contextFilter);
|
|
295
|
+
const scenario = lib_1.ctx.app.router.product([id], [relay]);
|
|
296
|
+
state.selections.push((0, exports.makeFilterSelection)(id, contextFilter, scenario));
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
};
|
|
300
|
+
exports.getFilterSelectionsForContext = getFilterSelectionsForContext;
|
|
301
|
+
const getFilterSelectionsForIndexedKinds = (state) => {
|
|
302
|
+
var _a, _b;
|
|
303
|
+
const kinds = (0, lib_1.intersection)(exports.INDEXED_KINDS, state.filter.kinds || []);
|
|
304
|
+
if (kinds.length === 0)
|
|
305
|
+
return false;
|
|
306
|
+
const id = (0, util_1.getFilterId)({ ...state.filter, kinds });
|
|
307
|
+
const relays = ((_b = (_a = lib_1.ctx.app.router.options).getIndexerRelays) === null || _b === void 0 ? void 0 : _b.call(_a)) || [];
|
|
308
|
+
const scenario = lib_1.ctx.app.router.product([id], relays);
|
|
309
|
+
state.selections.push((0, exports.makeFilterSelection)(id, state.filter, scenario));
|
|
310
|
+
return false;
|
|
311
|
+
};
|
|
312
|
+
exports.getFilterSelectionsForIndexedKinds = getFilterSelectionsForIndexedKinds;
|
|
313
|
+
const getFilterSelectionsForAuthors = (state) => {
|
|
314
|
+
// If we have a ton of authors, just use our indexers
|
|
315
|
+
if (!state.filter.authors || state.filter.authors.length > 30)
|
|
316
|
+
return false;
|
|
317
|
+
const id = (0, util_1.getFilterId)(state.filter);
|
|
318
|
+
const scenario = lib_1.ctx.app.router.FromPubkeys(state.filter.authors).update((0, lib_1.assoc)('value', id));
|
|
319
|
+
state.selections.push((0, exports.makeFilterSelection)(id, state.filter, scenario));
|
|
320
|
+
return false;
|
|
321
|
+
};
|
|
322
|
+
exports.getFilterSelectionsForAuthors = getFilterSelectionsForAuthors;
|
|
323
|
+
const getFilterSelectionsForUser = (state) => {
|
|
324
|
+
const id = (0, util_1.getFilterId)(state.filter);
|
|
325
|
+
const relays = lib_1.ctx.app.router.ReadRelays().getUrls();
|
|
326
|
+
const scenario = lib_1.ctx.app.router.product([id], relays);
|
|
327
|
+
state.selections.push((0, exports.makeFilterSelection)(id, state.filter, scenario));
|
|
328
|
+
return false;
|
|
329
|
+
};
|
|
330
|
+
exports.getFilterSelectionsForUser = getFilterSelectionsForUser;
|
|
331
|
+
exports.defaultFilterSelectionRules = [
|
|
332
|
+
exports.getFilterSelectionsForLocalRelay,
|
|
333
|
+
exports.getFilterSelectionsForSearch,
|
|
334
|
+
exports.getFilterSelectionsForContext,
|
|
335
|
+
exports.getFilterSelectionsForIndexedKinds,
|
|
336
|
+
exports.getFilterSelectionsForAuthors,
|
|
337
|
+
exports.getFilterSelectionsForUser,
|
|
338
|
+
];
|
|
339
|
+
const getFilterSelections = (filters, rules = exports.defaultFilterSelectionRules) => {
|
|
340
|
+
var _a, _b, _c, _d;
|
|
341
|
+
const scenarios = [];
|
|
342
|
+
const filtersById = new Map();
|
|
343
|
+
for (const filter of filters) {
|
|
344
|
+
const state = { filter, selections: [] };
|
|
345
|
+
for (const rule of rules) {
|
|
346
|
+
const done = rule(state);
|
|
347
|
+
if (done) {
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
for (const { id, filter, scenario } of state.selections) {
|
|
352
|
+
filtersById.set(id, filter);
|
|
353
|
+
scenarios.push(scenario.policy(exports.addNoFallbacks));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// Use low redundancy because filters will be very low cardinality
|
|
357
|
+
const selections = lib_1.ctx.app.router
|
|
358
|
+
.merge(scenarios)
|
|
359
|
+
.redundancy(1)
|
|
360
|
+
.getSelections()
|
|
361
|
+
.map(({ values, relay }) => ({
|
|
362
|
+
filters: values.map(id => filtersById.get(id)),
|
|
363
|
+
relays: [relay],
|
|
364
|
+
}));
|
|
365
|
+
// Pubkey-based selections can get really big. Use the most popular relays for the long tail
|
|
366
|
+
const limit = ((_b = (_a = lib_1.ctx.app.router.options).getLimit) === null || _b === void 0 ? void 0 : _b.call(_a)) || 8;
|
|
367
|
+
const redundancy = ((_d = (_c = lib_1.ctx.app.router.options).getRedundancy) === null || _d === void 0 ? void 0 : _d.call(_c)) || 3;
|
|
368
|
+
const [keep, discard] = (0, lib_1.splitAt)(limit, selections);
|
|
369
|
+
for (const target of keep.slice(0, redundancy)) {
|
|
370
|
+
target.filters = (0, util_1.unionFilters)([...discard, target].flatMap(s => s.filters));
|
|
371
|
+
}
|
|
372
|
+
return keep;
|
|
373
|
+
};
|
|
374
|
+
exports.getFilterSelections = getFilterSelections;
|
|
375
|
+
//# sourceMappingURL=router.cjs.map
|