socket-function 1.1.15 → 1.1.17
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 +4 -1
- package/package.json +1 -1
- package/src/CallFactory.d.ts +3 -0
- package/src/CallFactory.ts +33 -10
- package/src/nodeCache.d.ts +1 -1
- package/src/nodeCache.ts +5 -1
package/index.d.ts
CHANGED
|
@@ -467,12 +467,14 @@ declare module "socket-function/src/CallFactory" {
|
|
|
467
467
|
import * as tls from "tls";
|
|
468
468
|
export interface CallFactory {
|
|
469
469
|
nodeId: string;
|
|
470
|
+
realNodeId?: string;
|
|
470
471
|
lastClosed: number;
|
|
471
472
|
closedForever?: boolean;
|
|
472
473
|
isConnected?: boolean;
|
|
473
474
|
receivedInitializeState?: InitializeState;
|
|
474
475
|
performCall(call: CallType): Promise<unknown>;
|
|
475
476
|
onNextDisconnect(callback: () => void): void;
|
|
477
|
+
disconnect(): void;
|
|
476
478
|
connectionId: {
|
|
477
479
|
nodeId: string;
|
|
478
480
|
};
|
|
@@ -481,6 +483,7 @@ declare module "socket-function/src/CallFactory" {
|
|
|
481
483
|
nodeId?: string;
|
|
482
484
|
_socket?: tls.TLSSocket;
|
|
483
485
|
send(data: string | Buffer): void;
|
|
486
|
+
close(): void;
|
|
484
487
|
addEventListener(event: "open", listener: () => void): void;
|
|
485
488
|
addEventListener(event: "close", listener: () => void): void;
|
|
486
489
|
addEventListener(event: "error", listener: (err: {
|
|
@@ -1072,7 +1075,7 @@ declare module "socket-function/src/nodeCache" {
|
|
|
1072
1075
|
originalNodeId: string;
|
|
1073
1076
|
newNodeId: string;
|
|
1074
1077
|
callFactory: CallFactory;
|
|
1075
|
-
}):
|
|
1078
|
+
}): boolean;
|
|
1076
1079
|
export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
|
|
1077
1080
|
export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
|
|
1078
1081
|
export declare function debugGetAllCallFactories(): CallFactory[];
|
package/package.json
CHANGED
package/src/CallFactory.d.ts
CHANGED
|
@@ -6,12 +6,14 @@ import * as ws from "ws";
|
|
|
6
6
|
import * as tls from "tls";
|
|
7
7
|
export interface CallFactory {
|
|
8
8
|
nodeId: string;
|
|
9
|
+
realNodeId?: string;
|
|
9
10
|
lastClosed: number;
|
|
10
11
|
closedForever?: boolean;
|
|
11
12
|
isConnected?: boolean;
|
|
12
13
|
receivedInitializeState?: InitializeState;
|
|
13
14
|
performCall(call: CallType): Promise<unknown>;
|
|
14
15
|
onNextDisconnect(callback: () => void): void;
|
|
16
|
+
disconnect(): void;
|
|
15
17
|
connectionId: {
|
|
16
18
|
nodeId: string;
|
|
17
19
|
};
|
|
@@ -20,6 +22,7 @@ export interface SenderInterface {
|
|
|
20
22
|
nodeId?: string;
|
|
21
23
|
_socket?: tls.TLSSocket;
|
|
22
24
|
send(data: string | Buffer): void;
|
|
25
|
+
close(): void;
|
|
23
26
|
addEventListener(event: "open", listener: () => void): void;
|
|
24
27
|
addEventListener(event: "close", listener: () => void): void;
|
|
25
28
|
addEventListener(event: "error", listener: (err: {
|
package/src/CallFactory.ts
CHANGED
|
@@ -44,6 +44,7 @@ type InternalReturnType = {
|
|
|
44
44
|
|
|
45
45
|
export interface CallFactory {
|
|
46
46
|
nodeId: string;
|
|
47
|
+
realNodeId?: string;
|
|
47
48
|
lastClosed: number;
|
|
48
49
|
closedForever?: boolean;
|
|
49
50
|
isConnected?: boolean;
|
|
@@ -52,6 +53,8 @@ export interface CallFactory {
|
|
|
52
53
|
// Trigger performLocalCall on the other side of the connection
|
|
53
54
|
performCall(call: CallType): Promise<unknown>;
|
|
54
55
|
onNextDisconnect(callback: () => void): void;
|
|
56
|
+
disconnect(): void;
|
|
57
|
+
// If we change the node ID we need to recreate this object, essentially this object should be immutable. I forget why we wanted this. I think it's because we didn't know for sure if node ID would be unique, but it will be.
|
|
55
58
|
connectionId: { nodeId: string };
|
|
56
59
|
}
|
|
57
60
|
|
|
@@ -61,6 +64,7 @@ export interface SenderInterface {
|
|
|
61
64
|
_socket?: tls.TLSSocket;
|
|
62
65
|
|
|
63
66
|
send(data: string | Buffer): void;
|
|
67
|
+
close(): void;
|
|
64
68
|
|
|
65
69
|
addEventListener(event: "open", listener: () => void): void;
|
|
66
70
|
addEventListener(event: "close", listener: () => void): void;
|
|
@@ -108,7 +112,8 @@ export async function createCallFactory(
|
|
|
108
112
|
// The node id we are connecting to (or that connected to us)
|
|
109
113
|
nodeId: string,
|
|
110
114
|
// The node id that we were contacted on
|
|
111
|
-
|
|
115
|
+
// 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?
|
|
116
|
+
localNodeId = ownReconnectionUrl || "",
|
|
112
117
|
): Promise<CallFactory> {
|
|
113
118
|
let niceConnectionName = nodeId;
|
|
114
119
|
|
|
@@ -157,6 +162,13 @@ export async function createCallFactory(
|
|
|
157
162
|
connectionId: { nodeId },
|
|
158
163
|
receivedInitializeState: undefined,
|
|
159
164
|
onNextDisconnect,
|
|
165
|
+
disconnect() {
|
|
166
|
+
canReconnect = false;
|
|
167
|
+
callFactory.closedForever = true;
|
|
168
|
+
if (webSocketPromise) {
|
|
169
|
+
webSocketPromise.then(ws => ws.close()).catch(() => { });
|
|
170
|
+
}
|
|
171
|
+
},
|
|
160
172
|
async performCall(call: CallType) {
|
|
161
173
|
let seqNum = nextSeqNum++;
|
|
162
174
|
let fullCall: InternalCallType = {
|
|
@@ -549,18 +561,23 @@ export async function createCallFactory(
|
|
|
549
561
|
if (!SocketFunction.LEGACY_INITIALIZE && call.seqNum === INITIALIZE_STATE_SEQ_NUM) {
|
|
550
562
|
callFactory.receivedInitializeState = call.result as InitializeState;
|
|
551
563
|
let otherReconnectionUrl = callFactory.receivedInitializeState.ownReconnectionUrl;
|
|
552
|
-
|
|
553
|
-
|
|
564
|
+
callFactory.realNodeId = otherReconnectionUrl;
|
|
565
|
+
if (SocketFunction.logMessages) {
|
|
566
|
+
console.log(green(`Received initialize state from ${callFactory.realNodeId} at ${Date.now()}`));
|
|
567
|
+
}
|
|
568
|
+
if (otherReconnectionUrl && !canReconnect && !!getNodeIdLocation(otherReconnectionUrl) && otherReconnectionUrl !== ownReconnectionUrl) {
|
|
569
|
+
if (changeNodeId({
|
|
554
570
|
originalNodeId: nodeId,
|
|
555
571
|
newNodeId: otherReconnectionUrl,
|
|
556
572
|
callFactory,
|
|
557
|
-
})
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
573
|
+
})) {
|
|
574
|
+
nodeId = otherReconnectionUrl;
|
|
575
|
+
niceConnectionName = otherReconnectionUrl;
|
|
576
|
+
callerContext.nodeId = nodeId;
|
|
577
|
+
canReconnect = !!getNodeIdLocation(nodeId);
|
|
578
|
+
callFactory.nodeId = nodeId;
|
|
579
|
+
callFactory.connectionId = { nodeId };
|
|
580
|
+
}
|
|
564
581
|
}
|
|
565
582
|
return;
|
|
566
583
|
}
|
|
@@ -781,8 +798,14 @@ export async function createCallFactory(
|
|
|
781
798
|
result: initState,
|
|
782
799
|
seqNum: INITIALIZE_STATE_SEQ_NUM,
|
|
783
800
|
};
|
|
801
|
+
if (SocketFunction.logMessages) {
|
|
802
|
+
console.log(`Sending initialize state to ${nodeId}`);
|
|
803
|
+
}
|
|
784
804
|
let data = await SocketFunction.WIRE_SERIALIZER.serialize(initReturn);
|
|
785
805
|
await send(data);
|
|
806
|
+
if (SocketFunction.logMessages) {
|
|
807
|
+
console.log(`Sent initialize state to ${nodeId}`);
|
|
808
|
+
}
|
|
786
809
|
}
|
|
787
810
|
|
|
788
811
|
return callFactory;
|
package/src/nodeCache.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function changeNodeId(config: {
|
|
|
22
22
|
originalNodeId: string;
|
|
23
23
|
newNodeId: string;
|
|
24
24
|
callFactory: CallFactory;
|
|
25
|
-
}):
|
|
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))
|
|
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> {
|