socket-function 1.1.5 → 1.1.6

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.
@@ -29,6 +29,10 @@ export declare class SocketFunction {
29
29
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
30
30
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
31
31
  };
32
+ /** We will try the alternate node IDs first, however, if they fail, we will go through all of them and then eventually try the original node ID.
33
+ * VERY useful, allowing us to change global ips to local ones, which short-circuits the router, massively increasing bandwidth and decreasing latency.
34
+ */
35
+ static GET_ALTERNATE_NODE_IDS: (nodeId: string) => MaybePromise<string[] | undefined>;
32
36
  static WIRE_WARN_TIME: number;
33
37
  private static onMountCallbacks;
34
38
  static exposedClasses: Set<string>;
package/SocketFunction.ts CHANGED
@@ -72,11 +72,17 @@ export class SocketFunction {
72
72
  // In retrospect... dynamically changing the wire serializer is a BAD idea. If any calls happen
73
73
  // before it is changed, things just break. Also, it needs to be changed on both sides,
74
74
  // or else things break. Also, it is very hard to detect when the issue is different serializers
75
+ // NOTE: The only reason this is still exposed is in case in the future we want to intercept our traffic, and we want convenient functions to know how to decode it (although there are a still few other layers under this, for compression and Buffer[] sending efficiency).
75
76
  public static readonly WIRE_SERIALIZER = {
76
77
  serialize: measureWrap((obj: unknown): MaybePromise<Buffer[]> => [cborxInstance.encode(obj)], "WIRE_SERIALIZER|serialize"),
77
78
  deserialize: measureWrap((buffers: Buffer[]): MaybePromise<unknown> => cborxInstance.decode(buffers[0]), "WIRE_SERIALIZER|deserialize"),
78
79
  };
79
80
 
81
+ /** We will try the alternate node IDs first, however, if they fail, we will go through all of them and then eventually try the original node ID.
82
+ * VERY useful, allowing us to change global ips to local ones, which short-circuits the router, massively increasing bandwidth and decreasing latency.
83
+ */
84
+ public static GET_ALTERNATE_NODE_IDS = (nodeId: string): MaybePromise<string[] | undefined> => undefined;
85
+
80
86
  public static WIRE_WARN_TIME = 100;
81
87
 
82
88
  private static onMountCallbacks = new Map<string, (() => MaybePromise<void>)[]>();
package/index.d.ts CHANGED
@@ -38,6 +38,10 @@ declare module "socket-function/SocketFunction" {
38
38
  serialize: (obj: unknown) => MaybePromise<Buffer[]>;
39
39
  deserialize: (buffers: Buffer[]) => MaybePromise<unknown>;
40
40
  };
41
+ /** We will try the alternate node IDs first, however, if they fail, we will go through all of them and then eventually try the original node ID.
42
+ * VERY useful, allowing us to change global ips to local ones, which short-circuits the router, massively increasing bandwidth and decreasing latency.
43
+ */
44
+ static GET_ALTERNATE_NODE_IDS: (nodeId: string) => MaybePromise<string[] | undefined>;
41
45
  static WIRE_WARN_TIME: number;
42
46
  private static onMountCallbacks;
43
47
  static exposedClasses: Set<string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -20,7 +20,6 @@
20
20
  "type": "yarn tsc --noEmit",
21
21
  "emit-dts": "yarn tsc --project tsconfig.declarations.json || exit 0",
22
22
  "generate-index-dts": "node ./generateIndexDts.js",
23
- "update-typings": "yarn generate-index-dts && yarn emit-dts",
24
23
  "update-types": "yarn emit-dts && yarn generate-index-dts",
25
24
  "prepublishOnly": "yarn update-types",
26
25
  "testsni": "yarn typenode ./src/sniTest.ts"
@@ -19,8 +19,6 @@ import { measureFnc, measureWrap, registerMeasureInfo } from "./profiling/measur
19
19
  import { MaybePromise } from "./types";
20
20
  import { Zip } from "./Zip";
21
21
  import { LZ4 } from "./lz4/LZ4";
22
- //LZ4.compress;
23
- //LZ4.decompress;
24
22
 
25
23
  setFlag(require, "pako", "allowclient", true);
26
24
 
@@ -282,16 +280,22 @@ export async function createCallFactory(
282
280
  };
283
281
 
284
282
  let webSocketPromise: Promise<SenderInterface> | undefined;
283
+ let hasEverConnected = false;
285
284
  if (webSocketBase) {
286
285
  webSocketPromise = Promise.resolve(webSocketBase);
287
286
  await initializeWebsocket(webSocketBase);
288
287
  }
289
288
 
290
- async function initializeWebsocket(newWebSocket: SenderInterface) {
289
+ async function initializeWebsocket(newWebSocket: SenderInterface, skipCloseHandling = false) {
291
290
  registerOnce();
292
291
  callFactory.receivedInitializeState = undefined;
293
292
 
294
293
  function onClose(error: string) {
294
+ // We try various connections, and if they fail, we will just try other node IDs until we finally do connect, and then we stick with that nodeId, and when it disconnects we need to handle disconnections normally.
295
+ if (skipCloseHandling && !hasEverConnected) {
296
+ return;
297
+ }
298
+
295
299
  callFactory.connectionId = { nodeId };
296
300
  callFactory.lastClosed = Date.now();
297
301
  callFactory.isConnected = false;
@@ -341,6 +345,7 @@ export async function createCallFactory(
341
345
  console.log(`Connection established to ${niceConnectionName}`);
342
346
  }
343
347
  callFactory.isConnected = true;
348
+ hasEverConnected = true;
344
349
  resolve();
345
350
  });
346
351
  newWebSocket.addEventListener("close", () => resolve());
@@ -348,6 +353,7 @@ export async function createCallFactory(
348
353
  });
349
354
  } else if (newWebSocket.readyState === 1 /* OPEN */) {
350
355
  callFactory.isConnected = true;
356
+ hasEverConnected = true;
351
357
  } else {
352
358
  onClose(new Error(`Websocket received in closed state`).stack!);
353
359
  }
@@ -441,6 +447,23 @@ export async function createCallFactory(
441
447
  }
442
448
  lastConnectionAttempt = Date.now();
443
449
 
450
+ // Try alternates, and if any work, use them
451
+ try {
452
+ let alternates = await SocketFunction.GET_ALTERNATE_NODE_IDS(nodeId);
453
+ if (alternates) {
454
+ for (let alternateNodeId of alternates) {
455
+ let newWebSocket = createWebsocket(alternateNodeId);
456
+ await initializeWebsocket(newWebSocket, true);
457
+
458
+ if (callFactory.isConnected) {
459
+ return newWebSocket;
460
+ }
461
+ }
462
+ }
463
+ } catch (e) {
464
+ console.error("Error getting alternate node IDs", e);
465
+ }
466
+
444
467
  let newWebSocket = createWebsocket(nodeId);
445
468
  await initializeWebsocket(newWebSocket);
446
469