socket-function 1.1.15 → 1.1.16

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/index.d.ts CHANGED
@@ -473,6 +473,7 @@ declare module "socket-function/src/CallFactory" {
473
473
  receivedInitializeState?: InitializeState;
474
474
  performCall(call: CallType): Promise<unknown>;
475
475
  onNextDisconnect(callback: () => void): void;
476
+ disconnect(): void;
476
477
  connectionId: {
477
478
  nodeId: string;
478
479
  };
@@ -481,6 +482,7 @@ declare module "socket-function/src/CallFactory" {
481
482
  nodeId?: string;
482
483
  _socket?: tls.TLSSocket;
483
484
  send(data: string | Buffer): void;
485
+ close(): void;
484
486
  addEventListener(event: "open", listener: () => void): void;
485
487
  addEventListener(event: "close", listener: () => void): void;
486
488
  addEventListener(event: "error", listener: (err: {
@@ -1072,7 +1074,7 @@ declare module "socket-function/src/nodeCache" {
1072
1074
  originalNodeId: string;
1073
1075
  newNodeId: string;
1074
1076
  callFactory: CallFactory;
1075
- }): void;
1077
+ }): boolean;
1076
1078
  export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
1077
1079
  export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
1078
1080
  export declare function debugGetAllCallFactories(): CallFactory[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.1.15",
3
+ "version": "1.1.16",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -12,6 +12,7 @@ export interface CallFactory {
12
12
  receivedInitializeState?: InitializeState;
13
13
  performCall(call: CallType): Promise<unknown>;
14
14
  onNextDisconnect(callback: () => void): void;
15
+ disconnect(): void;
15
16
  connectionId: {
16
17
  nodeId: string;
17
18
  };
@@ -20,6 +21,7 @@ export interface SenderInterface {
20
21
  nodeId?: string;
21
22
  _socket?: tls.TLSSocket;
22
23
  send(data: string | Buffer): void;
24
+ close(): void;
23
25
  addEventListener(event: "open", listener: () => void): void;
24
26
  addEventListener(event: "close", listener: () => void): void;
25
27
  addEventListener(event: "error", listener: (err: {
@@ -52,6 +52,7 @@ export interface CallFactory {
52
52
  // Trigger performLocalCall on the other side of the connection
53
53
  performCall(call: CallType): Promise<unknown>;
54
54
  onNextDisconnect(callback: () => void): void;
55
+ disconnect(): void;
55
56
  connectionId: { nodeId: string };
56
57
  }
57
58
 
@@ -61,6 +62,7 @@ export interface SenderInterface {
61
62
  _socket?: tls.TLSSocket;
62
63
 
63
64
  send(data: string | Buffer): void;
65
+ close(): void;
64
66
 
65
67
  addEventListener(event: "open", listener: () => void): void;
66
68
  addEventListener(event: "close", listener: () => void): void;
@@ -108,7 +110,8 @@ export async function createCallFactory(
108
110
  // The node id we are connecting to (or that connected to us)
109
111
  nodeId: string,
110
112
  // The node id that we were contacted on
111
- localNodeId = "",
113
+ // NOTE: This used to default to empty string, which would be the case if we made the connection. But this caused problems when we want it to stop making two connections per server and reuse the connection if both the servers want it to talk to each other. I guess we can default it to our reconnection URL. I mean, how else are people going to connect to us other than that?
114
+ localNodeId = ownReconnectionUrl || "",
112
115
  ): Promise<CallFactory> {
113
116
  let niceConnectionName = nodeId;
114
117
 
@@ -157,6 +160,11 @@ export async function createCallFactory(
157
160
  connectionId: { nodeId },
158
161
  receivedInitializeState: undefined,
159
162
  onNextDisconnect,
163
+ disconnect() {
164
+ if (webSocketPromise) {
165
+ webSocketPromise.then(ws => ws.close()).catch(() => { });
166
+ }
167
+ },
160
168
  async performCall(call: CallType) {
161
169
  let seqNum = nextSeqNum++;
162
170
  let fullCall: InternalCallType = {
@@ -549,18 +557,19 @@ export async function createCallFactory(
549
557
  if (!SocketFunction.LEGACY_INITIALIZE && call.seqNum === INITIALIZE_STATE_SEQ_NUM) {
550
558
  callFactory.receivedInitializeState = call.result as InitializeState;
551
559
  let otherReconnectionUrl = callFactory.receivedInitializeState.ownReconnectionUrl;
552
- if (otherReconnectionUrl && !canReconnect && !!getNodeIdLocation(otherReconnectionUrl)) {
553
- changeNodeId({
560
+ if (otherReconnectionUrl && !canReconnect && !!getNodeIdLocation(otherReconnectionUrl) && otherReconnectionUrl !== ownReconnectionUrl) {
561
+ if (changeNodeId({
554
562
  originalNodeId: nodeId,
555
563
  newNodeId: otherReconnectionUrl,
556
564
  callFactory,
557
- });
558
- nodeId = otherReconnectionUrl;
559
- niceConnectionName = otherReconnectionUrl;
560
- callerContext.nodeId = nodeId;
561
- canReconnect = !!getNodeIdLocation(nodeId);
562
- callFactory.nodeId = nodeId;
563
- callFactory.connectionId.nodeId = nodeId;
565
+ })) {
566
+ nodeId = otherReconnectionUrl;
567
+ niceConnectionName = otherReconnectionUrl;
568
+ callerContext.nodeId = nodeId;
569
+ canReconnect = !!getNodeIdLocation(nodeId);
570
+ callFactory.nodeId = nodeId;
571
+ callFactory.connectionId.nodeId = nodeId;
572
+ }
564
573
  }
565
574
  return;
566
575
  }
@@ -22,7 +22,7 @@ export declare function changeNodeId(config: {
22
22
  originalNodeId: string;
23
23
  newNodeId: string;
24
24
  callFactory: CallFactory;
25
- }): void;
25
+ }): boolean;
26
26
  export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
27
27
  export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
28
28
  export declare function debugGetAllCallFactories(): CallFactory[];
package/src/nodeCache.ts CHANGED
@@ -82,9 +82,13 @@ export function changeNodeId(config: {
82
82
  newNodeId: string;
83
83
  callFactory: CallFactory;
84
84
  }) {
85
- if (nodeCache.has(config.newNodeId)) return;
85
+ if (nodeCache.has(config.newNodeId)) {
86
+ console.warn(`Received connection we already have. Likely we and them tried to connect at the same time. Other nodeId: ${config.newNodeId}`);
87
+ return false;
88
+ }
86
89
  nodeCache.delete(config.originalNodeId);
87
90
  nodeCache.set(config.newNodeId, config.callFactory);
91
+ return true;
88
92
  }
89
93
 
90
94
  export function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory> {