applesauce-relay 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/relay.d.ts CHANGED
@@ -98,9 +98,9 @@ export declare class Relay implements IRelay {
98
98
  /** Authenticate with the relay using a signer */
99
99
  authenticate(signer: AuthSigner): Promise<PublishResponse>;
100
100
  /** Internal operator for creating the retry() operator */
101
- protected customRetryOperator<T extends unknown = unknown>(times: number | RetryConfig): MonoTypeOperatorFunction<T>;
101
+ protected customRetryOperator<T extends unknown = unknown>(times: undefined | boolean | number | RetryConfig, base?: RetryConfig): MonoTypeOperatorFunction<T>;
102
102
  /** Internal operator for creating the repeat() operator */
103
- protected customRepeatOperator<T extends unknown = unknown>(times: boolean | number | RepeatConfig | undefined): MonoTypeOperatorFunction<T>;
103
+ protected customRepeatOperator<T extends unknown = unknown>(times: undefined | boolean | number | RepeatConfig | undefined): MonoTypeOperatorFunction<T>;
104
104
  /** Creates a REQ that retries when relay errors ( default 3 retries ) */
105
105
  subscription(filters: Filter | Filter[], opts?: SubscriptionOptions): Observable<SubscriptionResponse>;
106
106
  /** Makes a single request that retires on errors and completes on EOSE */
package/dist/relay.js CHANGED
@@ -7,6 +7,7 @@ import { BehaviorSubject, catchError, combineLatest, defer, endWith, filter, fin
7
7
  import { webSocket } from "rxjs/webSocket";
8
8
  import { completeOnEose } from "./operators/complete-on-eose.js";
9
9
  import { markFromRelay } from "./operators/mark-from-relay.js";
10
+ const DEFAULT_RETRY_CONFIG = { count: 10, delay: 1000, resetOnSuccess: true };
10
11
  /** An error that is thrown when a REQ is closed from the relay side */
11
12
  export class ReqCloseError extends Error {
12
13
  }
@@ -356,11 +357,15 @@ export class Relay {
356
357
  return lastValueFrom(start.pipe(switchMap((event) => this.auth(event))));
357
358
  }
358
359
  /** Internal operator for creating the retry() operator */
359
- customRetryOperator(times) {
360
- if (typeof times === "number")
361
- return retry(times);
360
+ customRetryOperator(times, base) {
361
+ if (times === false)
362
+ return identity;
363
+ else if (typeof times === "number")
364
+ return retry({ ...base, count: times });
365
+ else if (times === true)
366
+ return base ? retry(base) : retry();
362
367
  else
363
- return retry(times);
368
+ return retry({ ...base, ...times });
364
369
  }
365
370
  /** Internal operator for creating the repeat() operator */
366
371
  customRepeatOperator(times) {
@@ -377,9 +382,9 @@ export class Relay {
377
382
  subscription(filters, opts) {
378
383
  return this.req(filters, opts?.id).pipe(
379
384
  // Retry on connection errors
380
- this.customRetryOperator(opts?.retries ?? 3),
381
- // Create reconnect logic (repeat operator)
382
- this.customRepeatOperator(opts?.reconnect),
385
+ this.customRetryOperator(opts?.retries ?? opts?.reconnect ?? true, DEFAULT_RETRY_CONFIG),
386
+ // Create resubscribe logic (repeat operator)
387
+ this.customRepeatOperator(opts?.resubscribe),
383
388
  // Single subscription
384
389
  share());
385
390
  }
@@ -387,9 +392,9 @@ export class Relay {
387
392
  request(filters, opts) {
388
393
  return this.req(filters, opts?.id).pipe(
389
394
  // Retry on connection errors
390
- this.customRetryOperator(opts?.retries ?? 3),
391
- // Create reconnect logic (repeat operator)
392
- this.customRepeatOperator(opts?.reconnect),
395
+ this.customRetryOperator(opts?.retries ?? opts?.reconnect ?? true, DEFAULT_RETRY_CONFIG),
396
+ // Create resubscribe logic (repeat operator)
397
+ this.customRepeatOperator(opts?.resubscribe),
393
398
  // Complete when EOSE is received
394
399
  completeOnEose(),
395
400
  // Single subscription
@@ -404,9 +409,7 @@ export class Relay {
404
409
  return of(result);
405
410
  }),
406
411
  // Retry the publish until it succeeds or the number of retries is reached
407
- this.customRetryOperator(opts?.retries ?? 3),
408
- // Create reconnect logic (repeat operator)
409
- this.customRepeatOperator(opts?.reconnect),
412
+ this.customRetryOperator(opts?.retries ?? opts?.reconnect ?? true, DEFAULT_RETRY_CONFIG),
410
413
  // Single subscription
411
414
  share()));
412
415
  }
package/dist/types.d.ts CHANGED
@@ -11,15 +11,16 @@ export type MultiplexWebSocket<T = any> = Pick<WebSocketSubject<T>, "multiplex">
11
11
  /** Options for the publish method on the pool and relay */
12
12
  export type PublishOptions = {
13
13
  /**
14
- * Number of times to retry the publish. default is 3
14
+ * Number of times to retry the publish. default is 10
15
15
  * @see https://rxjs.dev/api/index/function/retry
16
+ * @deprecated use `reconnect` instead
16
17
  */
17
18
  retries?: number | Parameters<typeof retry>[0];
18
19
  /**
19
- * Whether to reconnect when socket is closed. A number of times or true for infinite. default is false
20
- * @see https://rxjs.dev/api/index/function/repeat
20
+ * Whether to reconnect when socket fails to connect. default is true (10 retries with 1 second delay)
21
+ * @see https://rxjs.dev/api/index/function/retry
21
22
  */
22
- reconnect?: boolean | Parameters<typeof repeat>[0];
23
+ reconnect?: boolean | number | Parameters<typeof retry>[0];
23
24
  };
24
25
  /** Options for the request method on the pool and relay */
25
26
  export type RequestOptions = SubscriptionOptions;
@@ -28,15 +29,21 @@ export type SubscriptionOptions = {
28
29
  /** Custom REQ id for the subscription */
29
30
  id?: string;
30
31
  /**
31
- * Number of times to retry a request. default is 3
32
+ * Number of times to retry the subscription if the relay fails to connect. default is 10
32
33
  * @see https://rxjs.dev/api/index/function/retry
34
+ * @deprecated use `reconnect` instead
33
35
  */
34
36
  retries?: number | Parameters<typeof retry>[0];
35
37
  /**
36
- * Whether to reconnect when socket is closed. A number of times or true for infinite. default is false
38
+ * Whether to resubscribe if the subscription is closed by the relay. default is false
37
39
  * @see https://rxjs.dev/api/index/function/repeat
38
40
  */
39
- reconnect?: boolean | Parameters<typeof repeat>[0];
41
+ resubscribe?: boolean | number | Parameters<typeof repeat>[0];
42
+ /**
43
+ * Whether to reconnect when socket is closed. default is true (10 retries with 1 second delay)
44
+ * @see https://rxjs.dev/api/index/function/retry
45
+ */
46
+ reconnect?: boolean | number | Parameters<typeof retry>[0];
40
47
  };
41
48
  export type AuthSigner = {
42
49
  signEvent: (event: EventTemplate) => NostrEvent | Promise<NostrEvent>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-relay",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "nostr relay communication framework built on rxjs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -54,14 +54,14 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@noble/hashes": "^1.7.1",
57
- "applesauce-core": "^3.0.0",
57
+ "applesauce-core": "^3.1.0",
58
58
  "nanoid": "^5.0.9",
59
- "nostr-tools": "^2.13",
59
+ "nostr-tools": "~2.15",
60
60
  "rxjs": "^7.8.1"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@hirez_io/observer-spy": "^2.2.0",
64
- "applesauce-signers": "^3.0.0",
64
+ "applesauce-signers": "^3.1.0",
65
65
  "@vitest/expect": "^3.1.1",
66
66
  "typescript": "^5.7.3",
67
67
  "vitest": "^3.2.3",