applesauce-relay 0.0.0-next-20250430195017 → 0.0.0-next-20250430200159
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/dist/__tests__/relay.test.js +8 -0
- package/dist/relay.js +10 -4
- package/package.json +1 -1
|
@@ -115,6 +115,14 @@ describe("req", () => {
|
|
|
115
115
|
// Verify the subscription completed
|
|
116
116
|
expect(spy.receivedError()).toBe(true);
|
|
117
117
|
});
|
|
118
|
+
it("should not send multiple REQ messages for multiple subscriptions", async () => {
|
|
119
|
+
const sub = relay.req([{ kinds: [1] }], "sub1");
|
|
120
|
+
sub.subscribe();
|
|
121
|
+
sub.subscribe();
|
|
122
|
+
// Wait for all messages to be sent
|
|
123
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
124
|
+
expect(server.messages).toEqual([["REQ", "sub1", { kinds: [1] }]]);
|
|
125
|
+
});
|
|
118
126
|
it("should wait for authentication if relay responds with auth-required", async () => {
|
|
119
127
|
// First subscription to trigger auth-required
|
|
120
128
|
const firstSub = subscribeSpyTo(relay.req([{ kinds: [1] }], "sub1"), { expectErrors: true });
|
package/dist/relay.js
CHANGED
|
@@ -191,7 +191,9 @@ export class Relay {
|
|
|
191
191
|
.subscribe(() => this.ready$.next(true));
|
|
192
192
|
}
|
|
193
193
|
/** Wait for ready and authenticated */
|
|
194
|
-
waitForAuth(
|
|
194
|
+
waitForAuth(
|
|
195
|
+
// NOTE: require BehaviorSubject so it always has a value
|
|
196
|
+
requireAuth, observable) {
|
|
195
197
|
return combineLatest([requireAuth, this.authenticated$]).pipe(
|
|
196
198
|
// wait for auth not required or authenticated
|
|
197
199
|
filter(([required, authenticated]) => !required || authenticated),
|
|
@@ -202,7 +204,7 @@ export class Relay {
|
|
|
202
204
|
}
|
|
203
205
|
/** Wait for the relay to be ready to accept connections */
|
|
204
206
|
waitForReady(observable) {
|
|
205
|
-
//
|
|
207
|
+
// Don't wait if the relay is already ready
|
|
206
208
|
if (this.ready$.value)
|
|
207
209
|
return observable;
|
|
208
210
|
else
|
|
@@ -253,7 +255,9 @@ export class Relay {
|
|
|
253
255
|
timeout({
|
|
254
256
|
first: this.eoseTimeout,
|
|
255
257
|
with: () => merge(of("EOSE"), NEVER),
|
|
256
|
-
})
|
|
258
|
+
}),
|
|
259
|
+
// Only create one upstream subscription
|
|
260
|
+
share());
|
|
257
261
|
// Wait for auth if required and make sure to start the watch tower
|
|
258
262
|
return this.waitForReady(this.waitForAuth(this.authRequiredForReq, observable));
|
|
259
263
|
}
|
|
@@ -283,7 +287,9 @@ export class Relay {
|
|
|
283
287
|
timeout({
|
|
284
288
|
first: this.eventTimeout,
|
|
285
289
|
with: () => of({ ok: false, from: this.url, message: "Timeout" }),
|
|
286
|
-
})
|
|
290
|
+
}),
|
|
291
|
+
// Only create one upstream subscription
|
|
292
|
+
share());
|
|
287
293
|
// skip wait for auth if verb is AUTH
|
|
288
294
|
if (verb === "AUTH")
|
|
289
295
|
return this.waitForReady(observable);
|