applesauce-core 0.0.0-next-20250725170333 → 0.0.0-next-20250729145125

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.
Files changed (97) hide show
  1. package/dist/__tests__/exports.test.d.ts +1 -0
  2. package/dist/__tests__/exports.test.js +27 -0
  3. package/dist/__tests__/fixtures.d.ts +17 -0
  4. package/dist/__tests__/fixtures.js +28 -0
  5. package/dist/event-store/__tests__/event-store.test.d.ts +1 -0
  6. package/dist/event-store/__tests__/event-store.test.js +386 -0
  7. package/dist/event-store/common.d.ts +1 -0
  8. package/dist/event-store/common.js +2 -0
  9. package/dist/event-store/event-set.js +5 -3
  10. package/dist/event-store/event-store.d.ts +43 -24
  11. package/dist/event-store/event-store.js +87 -47
  12. package/dist/event-store/interface.d.ts +20 -18
  13. package/dist/helpers/__tests__/app-handlers.test.d.ts +1 -0
  14. package/dist/helpers/__tests__/app-handlers.test.js +184 -0
  15. package/dist/helpers/__tests__/blossom.test.d.ts +1 -0
  16. package/dist/helpers/__tests__/blossom.test.js +13 -0
  17. package/dist/helpers/__tests__/bookmarks.test.d.ts +1 -0
  18. package/dist/helpers/__tests__/bookmarks.test.js +88 -0
  19. package/dist/helpers/__tests__/comment.test.d.ts +1 -0
  20. package/dist/helpers/__tests__/comment.test.js +249 -0
  21. package/dist/helpers/__tests__/contacts.test.d.ts +1 -0
  22. package/dist/helpers/__tests__/contacts.test.js +34 -0
  23. package/dist/helpers/__tests__/emoji.test.d.ts +1 -0
  24. package/dist/helpers/__tests__/emoji.test.js +110 -0
  25. package/dist/helpers/__tests__/encrypted-content-cache.test.d.ts +1 -0
  26. package/dist/helpers/__tests__/encrypted-content-cache.test.js +65 -0
  27. package/dist/helpers/__tests__/encryption.test.d.ts +1 -0
  28. package/dist/helpers/__tests__/encryption.test.js +21 -0
  29. package/dist/helpers/__tests__/event.test.d.ts +1 -0
  30. package/dist/helpers/__tests__/event.test.js +36 -0
  31. package/dist/helpers/__tests__/events.test.d.ts +1 -0
  32. package/dist/helpers/__tests__/events.test.js +32 -0
  33. package/dist/helpers/__tests__/exports.test.d.ts +1 -0
  34. package/dist/helpers/__tests__/exports.test.js +295 -0
  35. package/dist/helpers/__tests__/file-metadata.test.d.ts +1 -0
  36. package/dist/helpers/__tests__/file-metadata.test.js +103 -0
  37. package/dist/helpers/__tests__/groups.test.d.ts +1 -0
  38. package/dist/helpers/__tests__/groups.test.js +61 -0
  39. package/dist/helpers/__tests__/hidden-tags.test.d.ts +1 -0
  40. package/dist/helpers/__tests__/hidden-tags.test.js +29 -0
  41. package/dist/helpers/__tests__/mailboxes.test.d.ts +1 -0
  42. package/dist/helpers/__tests__/mailboxes.test.js +81 -0
  43. package/dist/helpers/__tests__/messages.test.d.ts +1 -0
  44. package/dist/helpers/__tests__/messages.test.js +91 -0
  45. package/dist/helpers/__tests__/mutes.test.d.ts +1 -0
  46. package/dist/helpers/__tests__/mutes.test.js +55 -0
  47. package/dist/helpers/__tests__/pointers.test.d.ts +1 -0
  48. package/dist/helpers/__tests__/pointers.test.js +118 -0
  49. package/dist/helpers/__tests__/profile.test.d.ts +1 -0
  50. package/dist/helpers/__tests__/profile.test.js +72 -0
  51. package/dist/helpers/__tests__/reactions.test.d.ts +1 -0
  52. package/dist/helpers/__tests__/reactions.test.js +88 -0
  53. package/dist/helpers/__tests__/relays.test.d.ts +1 -0
  54. package/dist/helpers/__tests__/relays.test.js +21 -0
  55. package/dist/helpers/__tests__/tags.test.d.ts +1 -0
  56. package/dist/helpers/__tests__/tags.test.js +24 -0
  57. package/dist/helpers/__tests__/threading.test.d.ts +1 -0
  58. package/dist/helpers/__tests__/threading.test.js +41 -0
  59. package/dist/helpers/event.d.ts +1 -1
  60. package/dist/helpers/event.js +1 -1
  61. package/dist/helpers/index.d.ts +2 -0
  62. package/dist/helpers/index.js +2 -0
  63. package/dist/helpers/stream-chat.d.ts +4 -0
  64. package/dist/helpers/stream-chat.js +9 -0
  65. package/dist/helpers/stream.d.ts +31 -0
  66. package/dist/helpers/stream.js +81 -0
  67. package/dist/models/__tests__/comments.test.d.ts +1 -0
  68. package/dist/models/__tests__/comments.test.js +36 -0
  69. package/dist/models/__tests__/exports.test.d.ts +1 -0
  70. package/dist/models/__tests__/exports.test.js +53 -0
  71. package/dist/models/blossom.d.ts +2 -1
  72. package/dist/models/blossom.js +4 -2
  73. package/dist/models/common.d.ts +14 -10
  74. package/dist/models/common.js +39 -76
  75. package/dist/models/contacts.d.ts +1 -1
  76. package/dist/models/contacts.js +4 -2
  77. package/dist/models/mailboxes.d.ts +2 -1
  78. package/dist/models/mailboxes.js +4 -2
  79. package/dist/models/mutes.d.ts +3 -2
  80. package/dist/models/mutes.js +4 -2
  81. package/dist/models/profile.d.ts +2 -1
  82. package/dist/models/profile.js +4 -2
  83. package/dist/observable/__tests__/claim-events.test.d.ts +1 -0
  84. package/dist/observable/__tests__/claim-events.test.js +23 -0
  85. package/dist/observable/__tests__/claim-latest.test.d.ts +1 -0
  86. package/dist/observable/__tests__/claim-latest.test.js +37 -0
  87. package/dist/observable/__tests__/exports.test.d.ts +1 -0
  88. package/dist/observable/__tests__/exports.test.js +21 -0
  89. package/dist/observable/__tests__/map-events-to-store.test.d.ts +1 -0
  90. package/dist/observable/__tests__/map-events-to-store.test.js +38 -0
  91. package/dist/observable/__tests__/simple-timeout.test.d.ts +1 -0
  92. package/dist/observable/__tests__/simple-timeout.test.js +34 -0
  93. package/dist/observable/__tests__/watch-event-updates.test.d.ts +1 -0
  94. package/dist/observable/__tests__/watch-event-updates.test.js +55 -0
  95. package/dist/promise/__tests__/exports.test.d.ts +1 -0
  96. package/dist/promise/__tests__/exports.test.js +11 -0
  97. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import * as exports from "../index.js";
3
+ describe("exports", () => {
4
+ it("should export the expected functions", () => {
5
+ expect(Object.keys(exports).sort()).toMatchInlineSnapshot(`
6
+ [
7
+ "EventSet",
8
+ "EventStore",
9
+ "EventStoreSymbol",
10
+ "Helpers",
11
+ "Models",
12
+ "TimeoutError",
13
+ "defined",
14
+ "firstValueFrom",
15
+ "getObservableValue",
16
+ "lastValueFrom",
17
+ "logger",
18
+ "mapEventsToStore",
19
+ "mapEventsToTimeline",
20
+ "simpleTimeout",
21
+ "watchEventUpdates",
22
+ "watchEventsUpdates",
23
+ "withImmediateValueOrDefault",
24
+ ]
25
+ `);
26
+ });
27
+ });
@@ -0,0 +1,17 @@
1
+ import type { NostrEvent } from "nostr-tools";
2
+ import { EncryptedContentSigner } from "../helpers/encrypted-content.js";
3
+ export declare class FakeUser implements EncryptedContentSigner {
4
+ key: Uint8Array<ArrayBufferLike>;
5
+ pubkey: string;
6
+ nip04: {
7
+ encrypt: (pubkey: string, plaintext: string) => string;
8
+ decrypt: (pubkey: string, ciphertext: string) => string;
9
+ };
10
+ nip44: {
11
+ encrypt: (pubkey: string, plaintext: string) => string;
12
+ decrypt: (pubkey: string, ciphertext: string) => string;
13
+ };
14
+ event(data?: Partial<NostrEvent>): NostrEvent;
15
+ note(content?: string, extra?: Partial<NostrEvent>): import("nostr-tools").Event;
16
+ profile(profile: any, extra?: Partial<NostrEvent>): import("nostr-tools").Event;
17
+ }
@@ -0,0 +1,28 @@
1
+ import { finalizeEvent, generateSecretKey, getPublicKey, kinds, nip04, nip44 } from "nostr-tools";
2
+ import { unixNow } from "../helpers/time.js";
3
+ export class FakeUser {
4
+ key = generateSecretKey();
5
+ pubkey = getPublicKey(this.key);
6
+ nip04 = {
7
+ encrypt: (pubkey, plaintext) => nip04.encrypt(this.key, pubkey, plaintext),
8
+ decrypt: (pubkey, ciphertext) => nip04.decrypt(this.key, pubkey, ciphertext),
9
+ };
10
+ nip44 = {
11
+ encrypt: (pubkey, plaintext) => nip44.encrypt(plaintext, nip44.getConversationKey(this.key, pubkey)),
12
+ decrypt: (pubkey, ciphertext) => nip44.decrypt(ciphertext, nip44.getConversationKey(this.key, pubkey)),
13
+ };
14
+ event(data) {
15
+ return finalizeEvent({
16
+ kind: data?.kind ?? kinds.ShortTextNote,
17
+ content: data?.content || "",
18
+ created_at: data?.created_at ?? unixNow(),
19
+ tags: data?.tags || [],
20
+ }, this.key);
21
+ }
22
+ note(content = "Hello World", extra) {
23
+ return this.event({ kind: kinds.ShortTextNote, content, ...extra });
24
+ }
25
+ profile(profile, extra) {
26
+ return this.event({ kind: kinds.Metadata, content: JSON.stringify({ ...profile }), ...extra });
27
+ }
28
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,386 @@
1
+ import { subscribeSpyTo } from "@hirez_io/observer-spy";
2
+ import { kinds } from "nostr-tools";
3
+ import { beforeEach, describe, expect, it, vi } from "vitest";
4
+ import { FakeUser } from "../../__tests__/fixtures.js";
5
+ import { getEventUID } from "../../helpers/event.js";
6
+ import { addSeenRelay, getSeenRelays } from "../../helpers/relays.js";
7
+ import { EventModel } from "../../models/common.js";
8
+ import { ProfileModel } from "../../models/profile.js";
9
+ import { EventStore } from "../event-store.js";
10
+ let eventStore;
11
+ beforeEach(() => {
12
+ eventStore = new EventStore();
13
+ });
14
+ const user = new FakeUser();
15
+ const profile = user.profile({ name: "fake user" });
16
+ const note = user.note();
17
+ describe("add", () => {
18
+ it("should return original event in case of duplicates", () => {
19
+ const a = { ...profile };
20
+ expect(eventStore.add(a)).toBe(a);
21
+ const b = { ...profile };
22
+ expect(eventStore.add(b)).toBe(a);
23
+ const c = { ...profile };
24
+ expect(eventStore.add(c)).toBe(a);
25
+ });
26
+ it("should merge seen relays on duplicate events", () => {
27
+ const a = { ...profile };
28
+ addSeenRelay(a, "wss://relay.a.com");
29
+ eventStore.add(a);
30
+ const b = { ...profile };
31
+ addSeenRelay(b, "wss://relay.b.com");
32
+ eventStore.add(b);
33
+ expect(eventStore.getEvent(profile.id)).toBeDefined();
34
+ expect([...getSeenRelays(eventStore.getEvent(profile.id))]).toEqual(expect.arrayContaining(["wss://relay.a.com", "wss://relay.b.com"]));
35
+ });
36
+ it("should ignore old deleted events but not newer ones", () => {
37
+ const deleteEvent = {
38
+ id: "delete event id",
39
+ kind: kinds.EventDeletion,
40
+ created_at: profile.created_at + 100,
41
+ pubkey: user.pubkey,
42
+ tags: [["e", profile.id]],
43
+ sig: "this should be ignored for the test",
44
+ content: "test",
45
+ };
46
+ // add delete event first
47
+ eventStore.add(deleteEvent);
48
+ // now event should be ignored
49
+ eventStore.add(profile);
50
+ const newProfile = user.profile({ name: "new name" }, { created_at: profile.created_at + 1000 });
51
+ eventStore.add(newProfile);
52
+ expect(eventStore.getEvent(profile.id)).toBeUndefined();
53
+ expect(eventStore.getEvent(newProfile.id)).toBeDefined();
54
+ });
55
+ it("should remove profile events when delete event is added", () => {
56
+ // Add initial replaceable event
57
+ eventStore.add(profile);
58
+ expect(eventStore.getEvent(profile.id)).toBeDefined();
59
+ const newProfile = user.profile({ name: "new name" }, { created_at: profile.created_at + 1000 });
60
+ eventStore.add(newProfile);
61
+ const deleteEvent = {
62
+ id: "delete event id",
63
+ kind: kinds.EventDeletion,
64
+ created_at: profile.created_at + 100,
65
+ pubkey: user.pubkey,
66
+ tags: [["a", `${profile.kind}:${profile.pubkey}`]],
67
+ sig: "this should be ignored for the test",
68
+ content: "test",
69
+ };
70
+ // Add delete event with coordinate
71
+ eventStore.add(deleteEvent);
72
+ // Profile should be removed since delete event is newer
73
+ expect(eventStore.getEvent(profile.id)).toBeUndefined();
74
+ expect(eventStore.getEvent(newProfile.id)).toBeDefined();
75
+ expect(eventStore.getReplaceable(profile.kind, profile.pubkey)).toBe(newProfile);
76
+ });
77
+ it("should remove addressable replaceable events when delete event is added", () => {
78
+ // Add initial replaceable event
79
+ const event = user.event({ content: "test", kind: 30000, tags: [["d", "test"]] });
80
+ eventStore.add(event);
81
+ expect(eventStore.getEvent(event.id)).toBeDefined();
82
+ const newEvent = user.event({
83
+ ...event,
84
+ created_at: event.created_at + 500,
85
+ });
86
+ eventStore.add(newEvent);
87
+ const deleteEvent = {
88
+ id: "delete event id",
89
+ kind: kinds.EventDeletion,
90
+ created_at: event.created_at + 100,
91
+ pubkey: user.pubkey,
92
+ tags: [["a", `${event.kind}:${event.pubkey}:test`]],
93
+ sig: "this should be ignored for the test",
94
+ content: "test",
95
+ };
96
+ // Add delete event with coordinate
97
+ eventStore.add(deleteEvent);
98
+ // Profile should be removed since delete event is newer
99
+ expect(eventStore.getEvent(event.id)).toBeUndefined();
100
+ expect(eventStore.getEvent(newEvent.id)).toBeDefined();
101
+ expect(eventStore.getReplaceable(event.kind, event.pubkey, "test")).toBe(newEvent);
102
+ });
103
+ it("should return null when event is invalid and there isn't an existing event", () => {
104
+ const verifyEvent = vi.fn().mockReturnValue(false);
105
+ eventStore.verifyEvent = verifyEvent;
106
+ expect(eventStore.add(profile)).toBeNull();
107
+ expect(verifyEvent).toHaveBeenCalledWith(profile);
108
+ });
109
+ });
110
+ describe("inserts", () => {
111
+ it("should emit newer replaceable events", () => {
112
+ const spy = subscribeSpyTo(eventStore.insert$);
113
+ eventStore.add(profile);
114
+ const newer = user.profile({ name: "new name" }, { created_at: profile.created_at + 100 });
115
+ eventStore.add(newer);
116
+ expect(spy.getValues()).toEqual([profile, newer]);
117
+ });
118
+ it("should not emit when older replaceable event is added", () => {
119
+ const spy = subscribeSpyTo(eventStore.insert$);
120
+ eventStore.add(profile);
121
+ eventStore.add(user.profile({ name: "new name" }, { created_at: profile.created_at - 1000 }));
122
+ expect(spy.getValues()).toEqual([profile]);
123
+ });
124
+ });
125
+ describe("removes", () => {
126
+ it("should emit older replaceable events when the newest replaceable event is added", () => {
127
+ const spy = subscribeSpyTo(eventStore.remove$);
128
+ eventStore.add(profile);
129
+ const newer = user.profile({ name: "new name" }, { created_at: profile.created_at + 1000 });
130
+ eventStore.add(newer);
131
+ expect(spy.getValues()).toEqual([profile]);
132
+ });
133
+ });
134
+ describe("verifyEvent", () => {
135
+ it("should be called for all events added", () => {
136
+ const verifyEvent = vi.fn().mockReturnValue(true);
137
+ eventStore.verifyEvent = verifyEvent;
138
+ eventStore.add(profile);
139
+ expect(verifyEvent).toHaveBeenCalledWith(profile);
140
+ });
141
+ it("should not be called for duplicate events", () => {
142
+ const verifyEvent = vi.fn().mockReturnValue(true);
143
+ eventStore.verifyEvent = verifyEvent;
144
+ const a = { ...profile };
145
+ eventStore.add(a);
146
+ expect(verifyEvent).toHaveBeenCalledWith(a);
147
+ const b = { ...profile };
148
+ eventStore.add(b);
149
+ expect(verifyEvent).toHaveBeenCalledTimes(1);
150
+ const c = { ...profile };
151
+ eventStore.add(c);
152
+ expect(verifyEvent).toHaveBeenCalledTimes(1);
153
+ });
154
+ });
155
+ describe("removed", () => {
156
+ it("should complete when event is removed", () => {
157
+ eventStore.add(profile);
158
+ const spy = subscribeSpyTo(eventStore.removed(profile.id));
159
+ eventStore.remove(profile);
160
+ expect(spy.getValues()).toEqual([]);
161
+ expect(spy.receivedComplete()).toBe(true);
162
+ });
163
+ });
164
+ describe("model", () => {
165
+ it("should emit synchronous value if it exists", () => {
166
+ let value = undefined;
167
+ eventStore.add(profile);
168
+ eventStore.model(ProfileModel, user.pubkey).subscribe((v) => (value = v));
169
+ expect(value).not.toBe(undefined);
170
+ });
171
+ it("should not emit undefined if value exists", () => {
172
+ eventStore.add(profile);
173
+ const spy = subscribeSpyTo(eventStore.model(EventModel, profile.id));
174
+ expect(spy.getValues()).toEqual([profile]);
175
+ });
176
+ it("should emit synchronous undefined if value does not exists", () => {
177
+ let value = 0;
178
+ eventStore.model(ProfileModel, user.pubkey).subscribe((v) => {
179
+ value = v;
180
+ });
181
+ expect(value).not.toBe(0);
182
+ expect(value).toBe(undefined);
183
+ });
184
+ it("should share latest value", () => {
185
+ eventStore.add(profile);
186
+ const spy = subscribeSpyTo(eventStore.model(EventModel, profile.id));
187
+ const spy2 = subscribeSpyTo(eventStore.model(EventModel, profile.id));
188
+ expect(spy.getValues()).toEqual([profile]);
189
+ expect(spy2.getValues()).toEqual([profile]);
190
+ });
191
+ });
192
+ describe("event", () => {
193
+ it("should emit existing event", () => {
194
+ eventStore.add(profile);
195
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
196
+ expect(spy.getValues()).toEqual([profile]);
197
+ });
198
+ it("should emit then event when its added", () => {
199
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
200
+ expect(spy.getValues()).toEqual([undefined]);
201
+ eventStore.add(profile);
202
+ expect(spy.getValues()).toEqual([undefined, profile]);
203
+ });
204
+ it("should emit undefined when event is removed", () => {
205
+ eventStore.add(profile);
206
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
207
+ expect(spy.getValues()).toEqual([profile]);
208
+ eventStore.remove(profile);
209
+ expect(spy.getValues()).toEqual([profile, undefined]);
210
+ });
211
+ it("should emit new value if event is re-added", () => {
212
+ eventStore.add(profile);
213
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
214
+ eventStore.remove(profile);
215
+ eventStore.add(profile);
216
+ expect(spy.getValues()).toEqual([profile, undefined, profile]);
217
+ });
218
+ it("should not complete when event is removed", () => {
219
+ eventStore.add(profile);
220
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
221
+ eventStore.remove(profile);
222
+ expect(spy.receivedComplete()).toBe(false);
223
+ });
224
+ it("should emit undefined if event is not found", () => {
225
+ const spy = subscribeSpyTo(eventStore.event(profile.id));
226
+ expect(spy.getValues()).toEqual([undefined]);
227
+ });
228
+ });
229
+ describe("events", () => {
230
+ it("should emit existing events", () => {
231
+ eventStore.add(profile);
232
+ const spy = subscribeSpyTo(eventStore.events([profile.id]));
233
+ expect(spy.getValues()).toEqual([{ [profile.id]: profile }]);
234
+ });
235
+ it("should remove events when they are removed", () => {
236
+ eventStore.add(profile);
237
+ const spy = subscribeSpyTo(eventStore.events([profile.id]));
238
+ expect(spy.getValues()).toEqual([{ [profile.id]: profile }]);
239
+ eventStore.remove(profile);
240
+ expect(spy.getValues()).toEqual([{ [profile.id]: profile }, {}]);
241
+ });
242
+ it("should add events back if then are re-added", () => {
243
+ eventStore.add(profile);
244
+ const spy = subscribeSpyTo(eventStore.events([profile.id]));
245
+ eventStore.remove(profile);
246
+ eventStore.add(profile);
247
+ expect(spy.getValues()).toEqual([{ [profile.id]: profile }, {}, { [profile.id]: profile }]);
248
+ });
249
+ it("should not emit any values if there are no events", () => {
250
+ const spy = subscribeSpyTo(eventStore.events([profile.id]));
251
+ expect(spy.receivedNext()).toBe(false);
252
+ });
253
+ });
254
+ describe("replaceable", () => {
255
+ it("should emit existing events", () => {
256
+ eventStore.add(profile);
257
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
258
+ expect(spy.getValues()).toEqual([profile]);
259
+ });
260
+ it("should emit undefined when event is removed", () => {
261
+ eventStore.add(profile);
262
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
263
+ eventStore.remove(profile);
264
+ expect(spy.getValues()).toEqual([profile, undefined]);
265
+ });
266
+ it("should not complete when event is removed", () => {
267
+ eventStore.add(profile);
268
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
269
+ eventStore.remove(profile);
270
+ expect(spy.receivedComplete()).toBe(false);
271
+ });
272
+ it("should emit event when re-added", () => {
273
+ eventStore.add(profile);
274
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
275
+ eventStore.remove(profile);
276
+ eventStore.add(profile);
277
+ expect(spy.getValues()).toEqual([profile, undefined, profile]);
278
+ });
279
+ it("should claim event", () => {
280
+ eventStore.add(profile);
281
+ eventStore.replaceable(0, user.pubkey).subscribe();
282
+ expect(eventStore.database.isClaimed(profile)).toBe(true);
283
+ });
284
+ it("should remove claim when event is removed", () => {
285
+ eventStore.add(profile);
286
+ eventStore.replaceable(0, user.pubkey).subscribe();
287
+ eventStore.remove(profile);
288
+ expect(eventStore.database.isClaimed(profile)).toBe(false);
289
+ });
290
+ it("should ignore older events added later", () => {
291
+ eventStore.add(profile);
292
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
293
+ eventStore.add(user.profile({ name: "old name" }, { created_at: profile.created_at - 500 }));
294
+ eventStore.add(user.profile({ name: "really old name" }, { created_at: profile.created_at - 1000 }));
295
+ expect(spy.getValues()).toEqual([profile]);
296
+ });
297
+ it("should emit newer events", () => {
298
+ eventStore.add(profile);
299
+ const spy = subscribeSpyTo(eventStore.replaceable(0, user.pubkey));
300
+ const newProfile = user.profile({ name: "new name" }, { created_at: profile.created_at + 500 });
301
+ eventStore.add(newProfile);
302
+ expect(spy.getValues()).toEqual([profile, newProfile]);
303
+ });
304
+ });
305
+ describe("timeline", () => {
306
+ it("should emit an empty array if there are not events", () => {
307
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [1] }));
308
+ expect(spy.getValues()).toEqual([[]]);
309
+ });
310
+ it("should emit existing events", () => {
311
+ eventStore.add(profile);
312
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0] }));
313
+ expect(spy.getValues()).toEqual([[profile]]);
314
+ });
315
+ it("should emit new events", () => {
316
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0, 1] }));
317
+ eventStore.add(profile);
318
+ eventStore.add(note);
319
+ expect(spy.getValues()).toEqual([[], [profile], [note, profile]]);
320
+ });
321
+ it("should remove event when its removed", () => {
322
+ eventStore.add(profile);
323
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0] }));
324
+ eventStore.remove(profile);
325
+ expect(spy.getValues()).toEqual([[profile], []]);
326
+ });
327
+ it("should not emit when other events are removed", () => {
328
+ eventStore.add(profile);
329
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0] }));
330
+ eventStore.add(note);
331
+ eventStore.remove(note);
332
+ expect(spy.getValues()).toEqual([[profile]]);
333
+ });
334
+ it("should ignore older events added later", () => {
335
+ eventStore.add(profile);
336
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0] }));
337
+ eventStore.add(user.profile({ name: "old-name" }, { created_at: profile.created_at - 1000 }));
338
+ expect(spy.getValues()).toEqual([[profile]]);
339
+ });
340
+ it("should return new array for every value", () => {
341
+ const first = user.note("first note");
342
+ const second = user.note("second note");
343
+ const third = user.note("third note");
344
+ eventStore.add(first);
345
+ const spy = subscribeSpyTo(eventStore.timeline({ kinds: [0] }));
346
+ eventStore.add(second);
347
+ eventStore.add(third);
348
+ const hasDuplicates = (arr) => {
349
+ return new Set(arr).size !== arr.length;
350
+ };
351
+ expect(hasDuplicates(spy.getValues())).toBe(false);
352
+ });
353
+ });
354
+ describe("replaceableSet", () => {
355
+ it("should not emit if there are not events", () => {
356
+ const spy = subscribeSpyTo(eventStore.replaceableSet([{ kind: 0, pubkey: user.pubkey }]));
357
+ expect(spy.receivedNext()).toBe(false);
358
+ });
359
+ it("should emit existing events", () => {
360
+ eventStore.add(profile);
361
+ const spy = subscribeSpyTo(eventStore.replaceableSet([{ kind: 0, pubkey: user.pubkey }]));
362
+ expect(spy.getValues()).toEqual([{ [getEventUID(profile)]: profile }]);
363
+ });
364
+ it("should remove event when removed", () => {
365
+ eventStore.add(profile);
366
+ const spy = subscribeSpyTo(eventStore.replaceableSet([{ kind: 0, pubkey: user.pubkey }]));
367
+ eventStore.remove(profile);
368
+ expect(spy.getValues()).toEqual([{ [getEventUID(profile)]: profile }, {}]);
369
+ });
370
+ it("should replace older events", () => {
371
+ const event2 = { ...profile, created_at: profile.created_at + 100, id: "newer-event" };
372
+ const uid = getEventUID(profile);
373
+ eventStore.add(profile);
374
+ const spy = subscribeSpyTo(eventStore.replaceableSet([{ kind: 0, pubkey: user.pubkey }]));
375
+ eventStore.add(event2);
376
+ expect(spy.getValues()).toEqual([{ [uid]: profile }, { [uid]: event2 }]);
377
+ });
378
+ it("should ignore old events added later", () => {
379
+ const old = user.profile({ name: "old-name" }, { created_at: profile.created_at - 1000 });
380
+ const uid = getEventUID(profile);
381
+ eventStore.add(profile);
382
+ const spy = subscribeSpyTo(eventStore.replaceableSet([{ kind: 0, pubkey: user.pubkey }]));
383
+ eventStore.add(old);
384
+ expect(spy.getValues()).toEqual([{ [uid]: profile }]);
385
+ });
386
+ });
@@ -0,0 +1 @@
1
+ export declare const INDEXABLE_TAGS: Set<string>;
@@ -0,0 +1,2 @@
1
+ const LETTERS = "abcdefghijklmnopqrstuvwxyz";
2
+ export const INDEXABLE_TAGS = new Set((LETTERS + LETTERS.toUpperCase()).split(""));
@@ -1,7 +1,7 @@
1
1
  import { binarySearch, insertEventIntoDescendingList } from "nostr-tools/utils";
2
2
  import { Subject } from "rxjs";
3
3
  import { getIndexableTags, INDEXABLE_TAGS } from "../helpers/event-tags.js";
4
- import { createReplaceableAddress, getReplaceableAddress, isReplaceable } from "../helpers/event.js";
4
+ import { createReplaceableAddress, isReplaceable } from "../helpers/event.js";
5
5
  import { LRU } from "../helpers/lru.js";
6
6
  import { logger } from "../logger.js";
7
7
  /**
@@ -92,7 +92,8 @@ export class EventSet {
92
92
  insertEventIntoDescendingList(this.created_at, event);
93
93
  // Insert into replaceable index
94
94
  if (isReplaceable(event.kind)) {
95
- const address = getReplaceableAddress(event);
95
+ const identifier = event.tags.find((t) => t[0] === "d")?.[1];
96
+ const address = createReplaceableAddress(event.kind, event.pubkey, identifier);
96
97
  let array = this.replaceable.get(address);
97
98
  if (!this.replaceable.has(address)) {
98
99
  // add an empty array if there is no array
@@ -135,7 +136,8 @@ export class EventSet {
135
136
  this.events.delete(id);
136
137
  // remove from replaceable index
137
138
  if (isReplaceable(event.kind)) {
138
- const address = getReplaceableAddress(event);
139
+ const identifier = event.tags.find((t) => t[0] === "d")?.[1];
140
+ const address = createReplaceableAddress(event.kind, event.pubkey, identifier);
139
141
  const array = this.replaceable.get(address);
140
142
  if (array && array.includes(event)) {
141
143
  const idx = array.indexOf(event);
@@ -1,8 +1,9 @@
1
1
  import { Filter, NostrEvent } from "nostr-tools";
2
2
  import { Observable } from "rxjs";
3
+ import { AddressPointer, EventPointer, ProfilePointer } from "nostr-tools/nip19";
4
+ import { AddressPointerWithoutD } from "../helpers/pointers.js";
3
5
  import { EventSet } from "./event-set.js";
4
6
  import { IEventStore, ModelConstructor } from "./interface.js";
5
- import { AddressPointer, EventPointer } from "nostr-tools/nip19";
6
7
  /** An extended {@link EventSet} that handles replaceable events, delets, and models */
7
8
  export declare class EventStore implements IEventStore {
8
9
  database: EventSet;
@@ -19,6 +20,21 @@ export declare class EventStore implements IEventStore {
19
20
  update$: Observable<NostrEvent>;
20
21
  /** A stream of events that have been removed */
21
22
  remove$: Observable<NostrEvent>;
23
+ /**
24
+ * A method that will be called when an event isn't found in the store
25
+ * @experimental
26
+ */
27
+ eventLoader?: (pointer: EventPointer) => Observable<NostrEvent>;
28
+ /**
29
+ * A method that will be called when a replaceable event isn't found in the store
30
+ * @experimental
31
+ */
32
+ replaceableLoader?: (pointer: AddressPointerWithoutD) => Observable<NostrEvent>;
33
+ /**
34
+ * A method that will be called when an addressable event isn't found in the store
35
+ * @experimental
36
+ */
37
+ addressableLoader?: (pointer: AddressPointer) => Observable<NostrEvent>;
22
38
  constructor();
23
39
  protected deletedIds: Set<string>;
24
40
  protected deletedCoords: Map<string, number>;
@@ -76,36 +92,39 @@ export declare class EventStore implements IEventStore {
76
92
  /** Creates an observable that emits when event is updated */
77
93
  updated(event: string | NostrEvent): Observable<NostrEvent>;
78
94
  /** Creates a {@link EventModel} */
79
- event(id: string): Observable<NostrEvent | undefined>;
95
+ event(pointer: string | EventPointer): Observable<NostrEvent | undefined>;
80
96
  /** Creates a {@link ReplaceableModel} */
97
+ replaceable(pointer: AddressPointer | AddressPointerWithoutD): Observable<NostrEvent | undefined>;
81
98
  replaceable(kind: number, pubkey: string, identifier?: string): Observable<NostrEvent | undefined>;
99
+ /** Subscribe to an addressable event by pointer */
100
+ addressable(pointer: AddressPointer): Observable<NostrEvent | undefined>;
82
101
  /** Creates a {@link TimelineModel} */
83
102
  timeline(filters: Filter | Filter[], includeOldVersion?: boolean): Observable<NostrEvent[]>;
84
- /** Creates a {@link EventsModel} */
85
- events(ids: string[]): Observable<Record<string, NostrEvent>>;
86
- /** Creates a {@link ReplaceableSetModel} */
87
- replaceableSet(pointers: {
88
- kind: number;
89
- pubkey: string;
90
- identifier?: string;
91
- }[]): Observable<Record<string, NostrEvent>>;
92
- /** Creates a {@link ProfileModel} */
93
- profile(pubkey: string): Observable<import("../helpers/profile.js").ProfileContent | undefined>;
94
- /** Creates a {@link ContactsModel} */
95
- contacts(pubkey: string): Observable<import("nostr-tools/nip19").ProfilePointer[]>;
96
- /** Creates a {@link MuteModel} */
97
- mutes(pubkey: string): Observable<import("../helpers/mutes.js").Mutes | undefined>;
98
- /** Creates a {@link ReactionsModel} */
99
- reactions(event: NostrEvent): Observable<import("nostr-tools").Event[]>;
100
- /** Creates a {@link MailboxesModel} */
101
- mailboxes(pubkey: string): Observable<{
103
+ /** Subscribe to a users profile */
104
+ profile(user: string | ProfilePointer): Observable<import("../helpers/profile.js").ProfileContent | undefined>;
105
+ /** Subscribe to a users contacts */
106
+ contacts(user: string | ProfilePointer): Observable<ProfilePointer[]>;
107
+ /** Subscribe to a users mutes */
108
+ mutes(user: string | ProfilePointer): Observable<import("../helpers/mutes.js").Mutes | undefined>;
109
+ /** Subscribe to a users NIP-65 mailboxes */
110
+ mailboxes(user: string | ProfilePointer): Observable<{
102
111
  inboxes: string[];
103
112
  outboxes: string[];
104
113
  } | undefined>;
105
- /** Creates a {@link UserBlossomServersModel} */
106
- blossomServers(pubkey: string): Observable<URL[]>;
107
- /** Creates a {@link ThreadModel} */
114
+ /** Subscribe to a users blossom servers */
115
+ blossomServers(user: string | ProfilePointer): Observable<URL[]>;
116
+ /** Subscribe to an event's reactions */
117
+ reactions(event: NostrEvent): Observable<import("nostr-tools").Event[]>;
118
+ /** Subscribe to a thread */
108
119
  thread(root: string | EventPointer | AddressPointer): Observable<import("../models/thread.js").Thread>;
109
- /** Creates a {@link CommentsModel} */
120
+ /** Subscribe to a event's comments */
110
121
  comments(event: NostrEvent): Observable<import("nostr-tools").Event[]>;
122
+ /** @deprecated use multiple {@link EventModel} instead */
123
+ events(ids: string[]): Observable<Record<string, NostrEvent | undefined>>;
124
+ /** @deprecated use multiple {@link ReplaceableModel} instead */
125
+ replaceableSet(pointers: {
126
+ kind: number;
127
+ pubkey: string;
128
+ identifier?: string;
129
+ }[]): Observable<Record<string, NostrEvent | undefined>>;
111
130
  }