socket-function 1.1.14 → 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.
@@ -26,7 +26,6 @@ export declare class SocketFunction {
26
26
  static COEP: string;
27
27
  static COOP: string;
28
28
  static TOTAL_CALLS: number;
29
- static registerOwnReconnectionUrl(url: string): void;
30
29
  static readonly WIRE_SERIALIZER: {
31
30
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
32
31
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
package/SocketFunction.ts CHANGED
@@ -71,10 +71,6 @@ export class SocketFunction {
71
71
 
72
72
  public static TOTAL_CALLS = 0;
73
73
 
74
- public static registerOwnReconnectionUrl(url: string) {
75
- registerOwnReconnectionUrl(url);
76
- }
77
-
78
74
  // In retrospect... dynamically changing the wire serializer is a BAD idea. If any calls happen
79
75
  // before it is changed, things just break. Also, it needs to be changed on both sides,
80
76
  // or else things break. Also, it is very hard to detect when the issue is different serializers
package/index.d.ts CHANGED
@@ -35,7 +35,6 @@ declare module "socket-function/SocketFunction" {
35
35
  static COEP: string;
36
36
  static COOP: string;
37
37
  static TOTAL_CALLS: number;
38
- static registerOwnReconnectionUrl(url: string): void;
39
38
  static readonly WIRE_SERIALIZER: {
40
39
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
41
40
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
@@ -474,6 +473,7 @@ declare module "socket-function/src/CallFactory" {
474
473
  receivedInitializeState?: InitializeState;
475
474
  performCall(call: CallType): Promise<unknown>;
476
475
  onNextDisconnect(callback: () => void): void;
476
+ disconnect(): void;
477
477
  connectionId: {
478
478
  nodeId: string;
479
479
  };
@@ -482,6 +482,7 @@ declare module "socket-function/src/CallFactory" {
482
482
  nodeId?: string;
483
483
  _socket?: tls.TLSSocket;
484
484
  send(data: string | Buffer): void;
485
+ close(): void;
485
486
  addEventListener(event: "open", listener: () => void): void;
486
487
  addEventListener(event: "close", listener: () => void): void;
487
488
  addEventListener(event: "error", listener: (err: {
@@ -1073,7 +1074,7 @@ declare module "socket-function/src/nodeCache" {
1073
1074
  originalNodeId: string;
1074
1075
  newNodeId: string;
1075
1076
  callFactory: CallFactory;
1076
- }): void;
1077
+ }): boolean;
1077
1078
  export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
1078
1079
  export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
1079
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.14",
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> {
@@ -5,7 +5,7 @@ import tls from "tls";
5
5
  import { getNodeIdsFromRequest, httpCallHandler } from "./callHTTPHandler";
6
6
  import { SocketFunction } from "../SocketFunction";
7
7
  import { getTrustedCertificates, watchTrustedCertificates } from "./certStore";
8
- import { createCallFactory } from "./CallFactory";
8
+ import { createCallFactory, registerOwnReconnectionUrl } from "./CallFactory";
9
9
  import { parseSNIExtension, parseTLSHello, SNIType } from "./tlsParsing";
10
10
  import debugbreak from "debugbreak";
11
11
  import { getNodeId } from "./nodeCache";
@@ -351,6 +351,7 @@ export async function startSocketServer(
351
351
 
352
352
  let nodeId = getNodeId(getCommonName(config.cert), port);
353
353
  console.log(green(`Started Listening on ${nodeId} (${host}) after ${formatTime(process.uptime() * 1000)}`));
354
+ registerOwnReconnectionUrl(nodeId);
354
355
 
355
356
  return nodeId;
356
357
  }