socket-function 1.1.12 → 1.1.14

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,6 +26,7 @@ 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;
29
30
  static readonly WIRE_SERIALIZER: {
30
31
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
31
32
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
package/SocketFunction.ts CHANGED
@@ -16,7 +16,7 @@ import "./SetProcessVariables";
16
16
  import cborx from "cbor-x";
17
17
  import { setFlag } from "./require/compileFlags";
18
18
  import { isNode } from "./src/misc";
19
- import { getPendingCallCount, harvestCallTimes, harvestFailedCallCount } from "./src/CallFactory";
19
+ import { getPendingCallCount, harvestCallTimes, harvestFailedCallCount, registerOwnReconnectionUrl } from "./src/CallFactory";
20
20
  import { measureWrap } from "./src/profiling/measure";
21
21
 
22
22
  setFlag(require, "cbor-x", "allowclient", true);
@@ -71,6 +71,10 @@ 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
+
74
78
  // In retrospect... dynamically changing the wire serializer is a BAD idea. If any calls happen
75
79
  // before it is changed, things just break. Also, it needs to be changed on both sides,
76
80
  // or else things break. Also, it is very hard to detect when the issue is different serializers
package/index.d.ts CHANGED
@@ -35,6 +35,7 @@ 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;
38
39
  static readonly WIRE_SERIALIZER: {
39
40
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
40
41
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
@@ -492,6 +493,7 @@ declare module "socket-function/src/CallFactory" {
492
493
  }
493
494
  type InitializeState = {
494
495
  supportsLZ4?: boolean;
496
+ ownReconnectionUrl?: string;
495
497
  };
496
498
  export declare function harvestFailedCallCount(): number;
497
499
  export declare function getPendingCallCount(): number;
@@ -500,6 +502,7 @@ declare module "socket-function/src/CallFactory" {
500
502
  end: number;
501
503
  }[];
502
504
  export declare function createCallFactory(webSocketBase: SenderInterface | undefined, nodeId: string, localNodeId?: string): Promise<CallFactory>;
505
+ export declare function registerOwnReconnectionUrl(url: string): void;
503
506
  export {};
504
507
 
505
508
  }
@@ -1066,6 +1069,11 @@ declare module "socket-function/src/nodeCache" {
1066
1069
  export declare function getNodeIdDomain(nodeId: string): string;
1067
1070
  export declare function getNodeIdDomainMaybeUndefined(nodeId: string): string | undefined;
1068
1071
  export declare function registerNodeClient(callFactory: CallFactory): void;
1072
+ export declare function changeNodeId(config: {
1073
+ originalNodeId: string;
1074
+ newNodeId: string;
1075
+ callFactory: CallFactory;
1076
+ }): void;
1069
1077
  export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
1070
1078
  export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
1071
1079
  export declare function debugGetAllCallFactories(): CallFactory[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.1.12",
3
+ "version": "1.1.14",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -31,6 +31,7 @@ export interface SenderInterface {
31
31
  }
32
32
  type InitializeState = {
33
33
  supportsLZ4?: boolean;
34
+ ownReconnectionUrl?: string;
34
35
  };
35
36
  export declare function harvestFailedCallCount(): number;
36
37
  export declare function getPendingCallCount(): number;
@@ -39,4 +40,5 @@ export declare function harvestCallTimes(): {
39
40
  end: number;
40
41
  }[];
41
42
  export declare function createCallFactory(webSocketBase: SenderInterface | undefined, nodeId: string, localNodeId?: string): Promise<CallFactory>;
43
+ export declare function registerOwnReconnectionUrl(url: string): void;
42
44
  export {};
@@ -5,7 +5,7 @@ import { convertErrorStackToError, formatNumberSuffixed, isBufferType, isNode, l
5
5
  import { createWebsocketFactory, getTLSSocket } from "./websocketFactory";
6
6
  import { SocketFunction } from "../SocketFunction";
7
7
  import * as tls from "tls";
8
- import { getClientNodeId, getNodeIdLocation, registerNodeClient } from "./nodeCache";
8
+ import { changeNodeId, getClientNodeId, getNodeIdLocation, registerNodeClient } from "./nodeCache";
9
9
  import debugbreak from "debugbreak";
10
10
  import { lazy } from "./caching";
11
11
  import { blue, green, red, yellow } from "./formatting/logColors";
@@ -74,6 +74,7 @@ export interface SenderInterface {
74
74
 
75
75
  type InitializeState = {
76
76
  supportsLZ4?: boolean;
77
+ ownReconnectionUrl?: string;
77
78
  };
78
79
 
79
80
  const INITIALIZE_STATE_SEQ_NUM = -1;
@@ -114,7 +115,7 @@ export async function createCallFactory(
114
115
  const createWebsocket = createWebsocketFactory();
115
116
  const registerOnce = lazy(() => registerNodeClient(callFactory));
116
117
 
117
- const canReconnect = !!getNodeIdLocation(nodeId);
118
+ let canReconnect = !!getNodeIdLocation(nodeId);
118
119
 
119
120
  let pendingCalls: Map<number, {
120
121
  data: Buffer[];
@@ -547,6 +548,20 @@ export async function createCallFactory(
547
548
  if (call.isReturn) {
548
549
  if (!SocketFunction.LEGACY_INITIALIZE && call.seqNum === INITIALIZE_STATE_SEQ_NUM) {
549
550
  callFactory.receivedInitializeState = call.result as InitializeState;
551
+ let otherReconnectionUrl = callFactory.receivedInitializeState.ownReconnectionUrl;
552
+ if (otherReconnectionUrl && !canReconnect && !!getNodeIdLocation(otherReconnectionUrl)) {
553
+ changeNodeId({
554
+ originalNodeId: nodeId,
555
+ newNodeId: otherReconnectionUrl,
556
+ 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;
564
+ }
550
565
  return;
551
566
  }
552
567
  let callbackObj = pendingCalls.get(call.seqNum);
@@ -759,6 +774,7 @@ export async function createCallFactory(
759
774
  if (!SocketFunction.LEGACY_INITIALIZE) {
760
775
  let initState: InitializeState = {
761
776
  supportsLZ4: true,
777
+ ownReconnectionUrl,
762
778
  };
763
779
  let initReturn: InternalReturnType = {
764
780
  isReturn: true,
@@ -772,6 +788,10 @@ export async function createCallFactory(
772
788
  return callFactory;
773
789
  }
774
790
 
791
+ let ownReconnectionUrl: string | undefined;
792
+ export function registerOwnReconnectionUrl(url: string) {
793
+ ownReconnectionUrl = url;
794
+ }
775
795
 
776
796
 
777
797
  let uncompressedSent = 0;
@@ -18,6 +18,11 @@ export declare function getNodeIdLocation(nodeId: string): {
18
18
  export declare function getNodeIdDomain(nodeId: string): string;
19
19
  export declare function getNodeIdDomainMaybeUndefined(nodeId: string): string | undefined;
20
20
  export declare function registerNodeClient(callFactory: CallFactory): void;
21
+ export declare function changeNodeId(config: {
22
+ originalNodeId: string;
23
+ newNodeId: string;
24
+ callFactory: CallFactory;
25
+ }): void;
21
26
  export declare function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory>;
22
27
  export declare function getCallFactory(nodeId: string): MaybePromise<CallFactory | undefined>;
23
28
  export declare function debugGetAllCallFactories(): CallFactory[];
package/src/nodeCache.ts CHANGED
@@ -77,6 +77,16 @@ export function registerNodeClient(callFactory: CallFactory) {
77
77
  startCleanupLoop();
78
78
  }
79
79
 
80
+ export function changeNodeId(config: {
81
+ originalNodeId: string;
82
+ newNodeId: string;
83
+ callFactory: CallFactory;
84
+ }) {
85
+ if (nodeCache.has(config.newNodeId)) return;
86
+ nodeCache.delete(config.originalNodeId);
87
+ nodeCache.set(config.newNodeId, config.callFactory);
88
+ }
89
+
80
90
  export function getCreateCallFactory(nodeId: string): MaybePromise<CallFactory> {
81
91
  let callFactory = nodeCache.get(nodeId);
82
92
  if (callFactory === undefined) {