@welshman/util 0.0.2 → 0.0.5
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/Address.cjs +40 -32
- package/build/Address.cjs.map +1 -1
- package/build/Address.d.ts +20 -18
- package/build/Address.mjs +38 -26
- package/build/Address.mjs.map +1 -1
- package/build/Events.cjs +29 -14
- package/build/Events.cjs.map +1 -1
- package/build/Events.d.ts +42 -15
- package/build/Events.mjs +15 -7
- package/build/Events.mjs.map +1 -1
- package/build/Filters.cjs +40 -8
- package/build/Filters.cjs.map +1 -1
- package/build/Filters.d.ts +7 -6
- package/build/Filters.mjs +37 -6
- package/build/Filters.mjs.map +1 -1
- package/build/Kinds.cjs +111 -46
- package/build/Kinds.cjs.map +1 -1
- package/build/Kinds.d.ts +108 -44
- package/build/Kinds.mjs +108 -44
- package/build/Kinds.mjs.map +1 -1
- package/build/Links.cjs +1 -1
- package/build/Links.cjs.map +1 -1
- package/build/Links.mjs +1 -1
- package/build/Links.mjs.map +1 -1
- package/build/Relay.cjs +59 -131
- package/build/Relay.cjs.map +1 -1
- package/build/Relay.d.ts +16 -18
- package/build/Relay.mjs +58 -132
- package/build/Relay.mjs.map +1 -1
- package/build/Repository.cjs +205 -0
- package/build/Repository.cjs.map +1 -0
- package/build/Repository.d.ts +44 -0
- package/build/Repository.mjs +201 -0
- package/build/Repository.mjs.map +1 -0
- package/build/Router.cjs +24 -24
- package/build/Router.cjs.map +1 -1
- package/build/Router.d.ts +68 -28
- package/build/Router.mjs +24 -24
- package/build/Router.mjs.map +1 -1
- package/build/Tags.cjs +17 -19
- package/build/Tags.cjs.map +1 -1
- package/build/Tags.d.ts +2 -9
- package/build/Tags.mjs +18 -20
- package/build/Tags.mjs.map +1 -1
- package/build/index.cjs +1 -1
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.mjs +1 -1
- package/package.json +2 -3
- package/build/Relays.cjs +0 -36
- package/build/Relays.cjs.map +0 -1
- package/build/Relays.d.ts +0 -6
- package/build/Relays.mjs +0 -31
- package/build/Relays.mjs.map +0 -1
package/build/Relay.mjs
CHANGED
|
@@ -1,149 +1,75 @@
|
|
|
1
|
-
import { Emitter,
|
|
2
|
-
import { matchFilters
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export const
|
|
6
|
-
|
|
1
|
+
import { Emitter, normalizeUrl, sleep, stripProtocol } from '@welshman/lib';
|
|
2
|
+
import { matchFilters } from "./Filters.mjs";
|
|
3
|
+
export const LOCAL_RELAY_URL = "local://welshman.relay";
|
|
4
|
+
export const BOGUS_RELAY_URL = "bogus://welshman.relay";
|
|
5
|
+
export const isShareableRelayUrl = (url) => {
|
|
6
|
+
var _a;
|
|
7
|
+
return Boolean(typeof url === 'string' &&
|
|
8
|
+
// Is it actually a websocket url and has a dot
|
|
9
|
+
url.match(/^wss:\/\/.+\..+/) &&
|
|
10
|
+
// Sometimes bugs cause multiple relays to get concatenated
|
|
11
|
+
((_a = url.match(/:\/\//g)) === null || _a === void 0 ? void 0 : _a.length) === 1 &&
|
|
12
|
+
// It shouldn't have any whitespace, url-encoded or otherwise
|
|
13
|
+
!url.match(/\s|%/) &&
|
|
14
|
+
// Don't match stuff with a port number
|
|
15
|
+
!url.slice(6).match(/:\d+/) &&
|
|
16
|
+
// Don't match raw ip addresses
|
|
17
|
+
!url.slice(6).match(/\d+\.\d+\.\d+\.\d+/) &&
|
|
18
|
+
// Skip nostr.wine's virtual relays
|
|
19
|
+
!url.slice(6).match(/\/npub/));
|
|
20
|
+
};
|
|
21
|
+
export const normalizeRelayUrl = (url, { allowInsecure = false } = {}) => {
|
|
22
|
+
var _a;
|
|
23
|
+
const prefix = allowInsecure ? ((_a = url.match(/^wss?:\/\//)) === null || _a === void 0 ? void 0 : _a[0]) || "wss://" : "wss://";
|
|
24
|
+
// Use our library to normalize
|
|
25
|
+
url = normalizeUrl(url, { stripHash: true, stripAuthentication: false });
|
|
26
|
+
// Strip the protocol since only wss works, lowercase
|
|
27
|
+
url = stripProtocol(url).toLowerCase();
|
|
28
|
+
// Urls without pathnames are supposed to have a trailing slash
|
|
29
|
+
if (!url.includes("/")) {
|
|
30
|
+
url += "/";
|
|
31
|
+
}
|
|
32
|
+
return prefix + url;
|
|
33
|
+
};
|
|
7
34
|
export class Relay extends Emitter {
|
|
8
|
-
constructor() {
|
|
9
|
-
super(
|
|
10
|
-
this.
|
|
11
|
-
this.eventsByAddress = new Map();
|
|
12
|
-
this.eventsByTag = new Map();
|
|
13
|
-
this.eventsByDay = new Map();
|
|
14
|
-
this.eventsByAuthor = new Map();
|
|
35
|
+
constructor(repository) {
|
|
36
|
+
super();
|
|
37
|
+
this.repository = repository;
|
|
15
38
|
this.subs = new Map();
|
|
16
|
-
this.deletes = new Map();
|
|
17
|
-
}
|
|
18
|
-
dump() {
|
|
19
|
-
return Array.from(this.eventsById.values());
|
|
20
|
-
}
|
|
21
|
-
async load(events, chunkSize = 1000) {
|
|
22
|
-
for (const eventsChunk of chunk(chunkSize, events)) {
|
|
23
|
-
for (const event of eventsChunk) {
|
|
24
|
-
this._addEvent(event);
|
|
25
|
-
}
|
|
26
|
-
if (eventsChunk.length === chunkSize) {
|
|
27
|
-
await sleep(1);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
39
|
}
|
|
31
40
|
send(type, ...message) {
|
|
32
41
|
switch (type) {
|
|
33
|
-
case 'EVENT': return this.
|
|
34
|
-
case 'CLOSE': return this.
|
|
35
|
-
case 'REQ': return this.
|
|
42
|
+
case 'EVENT': return this.handleEVENT(message);
|
|
43
|
+
case 'CLOSE': return this.handleCLOSE(message);
|
|
44
|
+
case 'REQ': return this.handleREQ(message);
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const hasAddress = isReplaceable(event);
|
|
51
|
-
const address = encodeAddress(addressFromEvent(event));
|
|
52
|
-
const duplicateByAddress = hasAddress ? this.eventsByAddress.get(address) : undefined;
|
|
53
|
-
if (duplicateByAddress && duplicateByAddress.created_at >= event.created_at) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
this._addEvent(event, duplicateByAddress);
|
|
57
|
-
this.emit('OK', event.id, true, "");
|
|
58
|
-
if (!this._isDeleted(event)) {
|
|
59
|
-
for (const [subId, filters] of this.subs.entries()) {
|
|
60
|
-
if (matchFilters(filters, event)) {
|
|
61
|
-
this.emit('EVENT', subId, json);
|
|
47
|
+
handleEVENT([event]) {
|
|
48
|
+
this.repository.publish(event);
|
|
49
|
+
// Callers generally expect async relays
|
|
50
|
+
sleep(1).then(() => {
|
|
51
|
+
this.emit('OK', event.id, true, "");
|
|
52
|
+
if (!this.repository.isDeleted(event)) {
|
|
53
|
+
for (const [subId, filters] of this.subs.entries()) {
|
|
54
|
+
if (matchFilters(filters, event)) {
|
|
55
|
+
this.emit('EVENT', subId, event);
|
|
56
|
+
}
|
|
62
57
|
}
|
|
63
58
|
}
|
|
64
|
-
}
|
|
59
|
+
});
|
|
65
60
|
}
|
|
66
|
-
|
|
61
|
+
handleCLOSE([subId]) {
|
|
67
62
|
this.subs.delete(subId);
|
|
68
63
|
}
|
|
69
|
-
|
|
64
|
+
handleREQ([subId, ...filters]) {
|
|
70
65
|
this.subs.set(subId, filters);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
filter = omit(['ids'], filter);
|
|
76
|
-
events = filter.ids.map(id => this.eventsById.get(id)).filter(identity);
|
|
77
|
-
}
|
|
78
|
-
else if (filter.authors) {
|
|
79
|
-
filter = omit(['authors'], filter);
|
|
80
|
-
events = uniq(filter.authors.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []));
|
|
81
|
-
}
|
|
82
|
-
else if (filter.since || filter.until) {
|
|
83
|
-
const sinceDay = getDay(filter.since || 0);
|
|
84
|
-
const untilDay = getDay(filter.since || now());
|
|
85
|
-
filter = omit(['since', 'until'], filter);
|
|
86
|
-
events = uniq(Array.from(range(sinceDay, untilDay))
|
|
87
|
-
.flatMap((day) => this.eventsByDay.get(day) || []));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
for (const [k, values] of Object.entries(filter)) {
|
|
91
|
-
if (!k.startsWith('#') || k.length !== 2) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
filter = omit([k], filter);
|
|
95
|
-
events = uniq(values.flatMap(v => this.eventsByTag.get(`${k[1]}:${v}`) || []));
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
for (const event of events) {
|
|
100
|
-
if (!this._isDeleted(event) && matchFilter(filter, event)) {
|
|
101
|
-
result.add(event);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
for (const event of result) {
|
|
106
|
-
this.emit('EVENT', subId, JSON.stringify(event));
|
|
107
|
-
}
|
|
108
|
-
this.emit('EOSE', subId);
|
|
109
|
-
}
|
|
110
|
-
_isDeleted(event) {
|
|
111
|
-
const idDeletedAt = this.deletes.get(event.id) || 0;
|
|
112
|
-
if (idDeletedAt > event.created_at) {
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
if (isReplaceable(event)) {
|
|
116
|
-
const address = encodeAddress(addressFromEvent(event));
|
|
117
|
-
const addressDeletedAt = this.deletes.get(address) || 0;
|
|
118
|
-
if (addressDeletedAt > event.created_at) {
|
|
119
|
-
return true;
|
|
66
|
+
// Callers generally expect async relays
|
|
67
|
+
sleep(1).then(() => {
|
|
68
|
+
for (const event of this.repository.query(filters)) {
|
|
69
|
+
this.emit('EVENT', subId, event);
|
|
120
70
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
_addEvent(event, duplicate) {
|
|
125
|
-
this.eventsById.set(event.id, event);
|
|
126
|
-
if (isReplaceable(event)) {
|
|
127
|
-
this.eventsByAddress.set(encodeAddress(addressFromEvent(event)), event);
|
|
128
|
-
}
|
|
129
|
-
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate);
|
|
130
|
-
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate);
|
|
131
|
-
for (const tag of event.tags) {
|
|
132
|
-
if (tag[0].length === 1) {
|
|
133
|
-
this._updateIndex(this.eventsByTag, tag.slice(0, 2).join(':'), event, duplicate);
|
|
134
|
-
if (event.kind === 5) {
|
|
135
|
-
this.deletes.set(tag[1], Math.max(event.created_at, this.deletes.get(tag[1]) || 0));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
_updateIndex(m, k, e, duplicate) {
|
|
141
|
-
let a = m.get(k) || [];
|
|
142
|
-
if (duplicate) {
|
|
143
|
-
a = a.filter((x) => x !== duplicate);
|
|
144
|
-
}
|
|
145
|
-
a.push(e);
|
|
146
|
-
m.set(k, a);
|
|
71
|
+
this.emit('EOSE', subId);
|
|
72
|
+
});
|
|
147
73
|
}
|
|
148
74
|
}
|
|
149
75
|
//# sourceMappingURL=Relay.mjs.map
|
package/build/Relay.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Relay.mjs","sourceRoot":"","sources":["../Relay.ts"],"names":[],"mappings":"OAAO,EAAC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"Relay.mjs","sourceRoot":"","sources":["../Relay.ts"],"names":[],"mappings":"OAAO,EAAC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAC,MAAM,eAAe;OAClE,EAAC,YAAY,EAAC;AAKrB,MAAM,CAAC,MAAM,eAAe,GAAG,wBAAwB,CAAA;AAEvD,MAAM,CAAC,MAAM,eAAe,GAAG,wBAAwB,CAAA;AAEvD,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,GAAW,EAAE,EAAE;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AK,EAAE,GAAG,OAAO,CAAwB;QAClD,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAE7B,wCAAwC;QACxC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACjB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE;gBAClD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;aACjC;YAED,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;QAC1B,CAAC,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Repository = exports.DAY = void 0;
|
|
4
|
+
const throttle_debounce_1 = require("throttle-debounce");
|
|
5
|
+
const lib_1 = require("@welshman/lib");
|
|
6
|
+
const Kinds_1 = require("./Kinds.cjs");
|
|
7
|
+
const Filters_1 = require("./Filters.cjs");
|
|
8
|
+
const Events_1 = require("./Events.cjs");
|
|
9
|
+
const Address_1 = require("./Address.cjs");
|
|
10
|
+
exports.DAY = 86400;
|
|
11
|
+
const getDay = (ts) => Math.floor(ts / exports.DAY);
|
|
12
|
+
const maybeThrottle = (t, f) => t ? (0, throttle_debounce_1.throttle)(t, f) : f;
|
|
13
|
+
class Repository extends lib_1.Emitter {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super();
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.eventsById = new Map();
|
|
18
|
+
this.eventsByAddress = new Map();
|
|
19
|
+
this.eventsByTag = new Map();
|
|
20
|
+
this.eventsByDay = new Map();
|
|
21
|
+
this.eventsByAuthor = new Map();
|
|
22
|
+
this.deletes = new Map();
|
|
23
|
+
this.subs = [];
|
|
24
|
+
this.notifyUpdate = maybeThrottle(this.options.throttle, () => {
|
|
25
|
+
const events = this.get();
|
|
26
|
+
for (const sub of this.subs) {
|
|
27
|
+
sub(events);
|
|
28
|
+
}
|
|
29
|
+
this.emit('update');
|
|
30
|
+
});
|
|
31
|
+
this.notifyEvent = (event) => {
|
|
32
|
+
this.emit('event', event);
|
|
33
|
+
};
|
|
34
|
+
this.notifyDelete = (event) => {
|
|
35
|
+
this.emit('delete', event);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Methods for implementing store interface
|
|
39
|
+
get() {
|
|
40
|
+
return Array.from(this.eventsById.values());
|
|
41
|
+
}
|
|
42
|
+
async set(events, chunkSize = 1000) {
|
|
43
|
+
for (const eventsChunk of (0, lib_1.chunk)(chunkSize, events)) {
|
|
44
|
+
for (const event of eventsChunk) {
|
|
45
|
+
this.publish(event);
|
|
46
|
+
}
|
|
47
|
+
if (eventsChunk.length === chunkSize) {
|
|
48
|
+
await (0, lib_1.sleep)(1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
subscribe(f, invalidate) {
|
|
53
|
+
this.subs.push(f);
|
|
54
|
+
return () => {
|
|
55
|
+
this.subs = this.subs.filter(sub => sub !== f);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
derived(f) {
|
|
59
|
+
return new lib_1.Derived(this, f);
|
|
60
|
+
}
|
|
61
|
+
throttle(t) {
|
|
62
|
+
return new lib_1.Derived(this, lib_1.identity, t);
|
|
63
|
+
}
|
|
64
|
+
filter(filters, { includeDeleted = false } = {}) {
|
|
65
|
+
const getValue = () => Array.from(this.query(filters, { includeDeleted }));
|
|
66
|
+
return (0, lib_1.customStore)({
|
|
67
|
+
get: getValue,
|
|
68
|
+
start: setValue => {
|
|
69
|
+
const onEvent = (event) => {
|
|
70
|
+
if ((0, Filters_1.matchFilters)(filters, event)) {
|
|
71
|
+
setValue(getValue());
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const onRefresh = () => {
|
|
75
|
+
setValue(getValue());
|
|
76
|
+
};
|
|
77
|
+
this.on('event', onEvent);
|
|
78
|
+
this.on('delete', onRefresh);
|
|
79
|
+
return () => {
|
|
80
|
+
this.off('event', onEvent);
|
|
81
|
+
this.off('delete', onRefresh);
|
|
82
|
+
};
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// API
|
|
87
|
+
getEvent(idOrAddress) {
|
|
88
|
+
return idOrAddress.includes(':')
|
|
89
|
+
? this.eventsByAddress.get(idOrAddress)
|
|
90
|
+
: this.eventsById.get(idOrAddress);
|
|
91
|
+
}
|
|
92
|
+
hasEvent(event) {
|
|
93
|
+
const duplicate = (this.eventsById.get(event.id) ||
|
|
94
|
+
this.eventsByAddress.get((0, Address_1.getAddress)(event)));
|
|
95
|
+
return duplicate && duplicate.created_at >= event.created_at;
|
|
96
|
+
}
|
|
97
|
+
watchEvent(idOrAddress) {
|
|
98
|
+
return this.filter((0, Filters_1.getIdFilters)([idOrAddress]), { includeDeleted: true }).derived(lib_1.first);
|
|
99
|
+
}
|
|
100
|
+
*query(filters, { includeDeleted = false } = {}) {
|
|
101
|
+
for (let filter of filters) {
|
|
102
|
+
let events = Array.from(this.eventsById.values());
|
|
103
|
+
if (filter.ids) {
|
|
104
|
+
events = filter.ids.map(id => this.eventsById.get(id)).filter(lib_1.identity);
|
|
105
|
+
filter = (0, lib_1.omit)(['ids'], filter);
|
|
106
|
+
}
|
|
107
|
+
else if (filter.authors) {
|
|
108
|
+
events = (0, lib_1.uniq)(filter.authors.flatMap(pubkey => this.eventsByAuthor.get(pubkey) || []));
|
|
109
|
+
filter = (0, lib_1.omit)(['authors'], filter);
|
|
110
|
+
}
|
|
111
|
+
else if (filter.since || filter.until) {
|
|
112
|
+
const sinceDay = getDay(filter.since || Filters_1.EPOCH);
|
|
113
|
+
const untilDay = getDay(filter.until || (0, lib_1.now)());
|
|
114
|
+
events = (0, lib_1.uniq)(Array.from((0, lib_1.range)(sinceDay, (0, lib_1.inc)(untilDay)))
|
|
115
|
+
.flatMap((day) => this.eventsByDay.get(day) || []));
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
for (const [k, values] of Object.entries(filter)) {
|
|
119
|
+
if (!k.startsWith('#') || k.length !== 2) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
filter = (0, lib_1.omit)([k], filter);
|
|
123
|
+
events = (0, lib_1.uniq)(values.flatMap(v => this.eventsByTag.get(`${k[1]}:${v}`) || []));
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
let i = 0;
|
|
128
|
+
for (const event of (0, lib_1.sortBy)((e) => -e.created_at, events)) {
|
|
129
|
+
if (filter.limit && i > filter.limit) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
if (!includeDeleted && this.isDeleted(event)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if ((0, Filters_1.matchFilter)(filter, event)) {
|
|
136
|
+
yield event;
|
|
137
|
+
i += 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
publish(event) {
|
|
143
|
+
if (!(0, Events_1.isTrustedEvent)(event)) {
|
|
144
|
+
throw new Error("Invalid event published to Repository", event);
|
|
145
|
+
}
|
|
146
|
+
const address = (0, Address_1.getAddress)(event);
|
|
147
|
+
const duplicate = (this.eventsById.get(event.id) ||
|
|
148
|
+
this.eventsByAddress.get(address));
|
|
149
|
+
// If our duplicate is newer than the event we're adding, we're done
|
|
150
|
+
if (duplicate && duplicate.created_at >= event.created_at) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Delete our duplicate first
|
|
154
|
+
if (duplicate) {
|
|
155
|
+
this.eventsById.delete(duplicate.id);
|
|
156
|
+
if ((0, Events_1.isReplaceable)(duplicate)) {
|
|
157
|
+
this.eventsByAddress.delete(address);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
// Now add our new event
|
|
161
|
+
this.eventsById.set(event.id, event);
|
|
162
|
+
if ((0, Events_1.isReplaceable)(event)) {
|
|
163
|
+
this.eventsByAddress.set(address, event);
|
|
164
|
+
}
|
|
165
|
+
// Update our timestamp and author indexes
|
|
166
|
+
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate);
|
|
167
|
+
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate);
|
|
168
|
+
// Update our tag indexes
|
|
169
|
+
for (const tag of event.tags) {
|
|
170
|
+
if (tag[0].length === 1) {
|
|
171
|
+
this._updateIndex(this.eventsByTag, tag.slice(0, 2).join(':'), event, duplicate);
|
|
172
|
+
// If this is a delete event, the tag value is an id or address. Track when it was
|
|
173
|
+
// deleted so that replaceables can be restored.
|
|
174
|
+
if (event.kind === Kinds_1.DELETE) {
|
|
175
|
+
this.deletes.set(tag[1], Math.max(event.created_at, this.deletes.get(tag[1]) || 0));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!this.isDeleted(event)) {
|
|
180
|
+
this.notifyUpdate();
|
|
181
|
+
this.notifyEvent(event);
|
|
182
|
+
// Deletes are tricky, re-evaluate all subscriptions if that's what we're dealing with
|
|
183
|
+
if (event.kind === Kinds_1.DELETE) {
|
|
184
|
+
this.notifyDelete(event);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
isDeleted(event) {
|
|
189
|
+
const deletedAt = (this.deletes.get(event.id) ||
|
|
190
|
+
this.deletes.get((0, Address_1.getAddress)(event)) ||
|
|
191
|
+
0);
|
|
192
|
+
return deletedAt > event.created_at;
|
|
193
|
+
}
|
|
194
|
+
// Utilities
|
|
195
|
+
_updateIndex(m, k, e, duplicate) {
|
|
196
|
+
let a = m.get(k) || [];
|
|
197
|
+
if (duplicate) {
|
|
198
|
+
a = a.filter((x) => x !== duplicate);
|
|
199
|
+
}
|
|
200
|
+
a.push(e);
|
|
201
|
+
m.set(k, a);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.Repository = Repository;
|
|
205
|
+
//# sourceMappingURL=Repository.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Repository.cjs","sourceRoot":"","sources":["../Repository.ts"],"names":[],"mappings":";;;AAAA,yDAA0C;AAE1C,uCAA+H;AAC/H,uCAA8B;AAC9B,2CAAwE;AACxE,yCAAsD;AACtD,2CAAoC;AAIvB,QAAA,GAAG,GAAG,KAAK,CAAA;AAExB,MAAM,MAAM,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,WAAG,CAAC,CAAA;AAEnD,MAAM,aAAa,GAAG,CAAC,CAAqB,EAAE,CAAa,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAA,4BAAQ,EAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAMtF,MAAa,UAAW,SAAQ,aAAO;IASrC,YAAoB,OAA0B;QAC5C,KAAK,EAAE,CAAA;QADW,YAAO,GAAP,OAAO,CAAmB;QAR9C,eAAU,GAAG,IAAI,GAAG,EAAwB,CAAA;QAC5C,oBAAe,GAAG,IAAI,GAAG,EAAwB,CAAA;QACjD,gBAAW,GAAG,IAAI,GAAG,EAA0B,CAAA;QAC/C,gBAAW,GAAG,IAAI,GAAG,EAA0B,CAAA;QAC/C,mBAAc,GAAG,IAAI,GAAG,EAA0B,CAAA;QAClD,YAAO,GAAG,IAAI,GAAG,EAAkB,CAAA;QACnC,SAAI,GAAiC,EAAE,CAAA;QAoEvC,iBAAY,GAAG,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,EAAE;YACvD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;YAEzB,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,IAAI,EAAE;gBAC3B,GAAG,CAAC,MAAM,CAAC,CAAA;a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AmB;QAC3B,MAAM,SAAS,GAAG,CAChB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAA,oBAAU,EAAC,KAAK,CAAC,CAAC;YACnC,CAAC,CACF,CAAA;QAED,OAAO,SAAS,GAAG,KAAK,CAAC,UAAU,CAAA;IACrC,CAAC;IAED,YAAY;IAEZ,YAAY,CAAI,CAAyB,EAAE,CAAI,EAAE,CAAe,EAAE,SAAwB;QACxF,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;QAEtB,IAAI,SAAS,EAAE;YACb,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAe,EAAE,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAA;SACnD;QAED,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACT,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IACb,CAAC;CACF;AAxPD,gCAwPC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { IReadable, Subscriber, Invalidator } from '@welshman/lib';
|
|
2
|
+
import { Derived, Emitter } from '@welshman/lib';
|
|
3
|
+
import type { Filter } from './Filters';
|
|
4
|
+
import type { TrustedEvent } from './Events';
|
|
5
|
+
export declare const DAY = 86400;
|
|
6
|
+
export type RepositoryOptions = {
|
|
7
|
+
throttle?: number;
|
|
8
|
+
};
|
|
9
|
+
export declare class Repository extends Emitter implements IReadable<TrustedEvent[]> {
|
|
10
|
+
private options;
|
|
11
|
+
eventsById: Map<string, TrustedEvent>;
|
|
12
|
+
eventsByAddress: Map<string, TrustedEvent>;
|
|
13
|
+
eventsByTag: Map<string, TrustedEvent[]>;
|
|
14
|
+
eventsByDay: Map<number, TrustedEvent[]>;
|
|
15
|
+
eventsByAuthor: Map<string, TrustedEvent[]>;
|
|
16
|
+
deletes: Map<string, number>;
|
|
17
|
+
subs: Subscriber<TrustedEvent[]>[];
|
|
18
|
+
constructor(options: RepositoryOptions);
|
|
19
|
+
get(): TrustedEvent[];
|
|
20
|
+
set(events: TrustedEvent[], chunkSize?: number): Promise<void>;
|
|
21
|
+
subscribe(f: Subscriber<TrustedEvent[]>, invalidate?: Invalidator<TrustedEvent[]>): () => void;
|
|
22
|
+
derived<U>(f: (v: TrustedEvent[]) => U): Derived<U>;
|
|
23
|
+
throttle(t: number): Derived<TrustedEvent[]>;
|
|
24
|
+
filter(filters: Filter[], { includeDeleted }?: {
|
|
25
|
+
includeDeleted?: boolean | undefined;
|
|
26
|
+
}): {
|
|
27
|
+
derived: <U>(f: (v: TrustedEvent[]) => U) => Derived<U>;
|
|
28
|
+
throttle: (t: number) => Derived<TrustedEvent[]>;
|
|
29
|
+
get: () => TrustedEvent[];
|
|
30
|
+
subscribe(this: void, run: Subscriber<TrustedEvent[]>, invalidate?: Invalidator<TrustedEvent[]> | undefined): () => void;
|
|
31
|
+
};
|
|
32
|
+
notifyUpdate: () => void;
|
|
33
|
+
notifyEvent: (event: TrustedEvent) => void;
|
|
34
|
+
notifyDelete: (event: TrustedEvent) => void;
|
|
35
|
+
getEvent(idOrAddress: string): TrustedEvent | undefined;
|
|
36
|
+
hasEvent(event: TrustedEvent): boolean | undefined;
|
|
37
|
+
watchEvent(idOrAddress: string): Derived<TrustedEvent>;
|
|
38
|
+
query(filters: Filter[], { includeDeleted }?: {
|
|
39
|
+
includeDeleted?: boolean | undefined;
|
|
40
|
+
}): Generator<TrustedEvent, void, unknown>;
|
|
41
|
+
publish(event: TrustedEvent): void;
|
|
42
|
+
isDeleted(event: TrustedEvent): boolean;
|
|
43
|
+
_updateIndex<K>(m: Map<K, TrustedEvent[]>, k: K, e: TrustedEvent, duplicate?: TrustedEvent): void;
|
|
44
|
+
}
|