@rnzeus/heimdall-sdk 0.5.0 → 0.6.1
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/dist/core/connection/resolver/cloud.js +6 -1
- package/dist/core/connection/resolver/pick-reachable-address.d.ts +16 -0
- package/dist/core/connection/resolver/pick-reachable-address.js +55 -0
- package/dist/core/connection/socket/constants.js +1 -1
- package/dist/core/connection/socket/early-buffer.js +0 -2
- package/dist/core/connection/socket/send-event.js +2 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CloudUnauthorizedError, clearCloudDevice, fetchConnection, loadCloudDevice, } from "../../cloud/index.js";
|
|
2
|
+
import { pickReachableAddress } from "./pick-reachable-address.js";
|
|
2
3
|
export function createCloudResolver({ baseUrl, }) {
|
|
3
4
|
return {
|
|
4
5
|
resolve: async () => {
|
|
@@ -12,8 +13,12 @@ export function createCloudResolver({ baseUrl, }) {
|
|
|
12
13
|
});
|
|
13
14
|
if (!connection)
|
|
14
15
|
return null;
|
|
16
|
+
const reachable = await pickReachableAddress({
|
|
17
|
+
addresses: connection.addresses,
|
|
18
|
+
port: connection.port,
|
|
19
|
+
});
|
|
15
20
|
return {
|
|
16
|
-
host: connection.host,
|
|
21
|
+
host: reachable !== null && reachable !== void 0 ? reachable : connection.host,
|
|
17
22
|
port: connection.port,
|
|
18
23
|
channel: connection.channel,
|
|
19
24
|
hostName: connection.hostName,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type ProbeAddress = (options: {
|
|
2
|
+
host: string;
|
|
3
|
+
port: number;
|
|
4
|
+
timeoutMs: number;
|
|
5
|
+
signal: AbortSignal;
|
|
6
|
+
}) => Promise<boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* Opens a bare TCP connection to every address at once and returns the first one that
|
|
9
|
+
* answers, or null. A host drops a connection that sends nothing without reporting it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function pickReachableAddress({ addresses, port, timeoutMs, probe, }: {
|
|
12
|
+
addresses: string[];
|
|
13
|
+
port: number;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
probe?: ProbeAddress;
|
|
16
|
+
}): Promise<string | null>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import TcpSocket from "react-native-tcp-socket";
|
|
2
|
+
const PROBE_TIMEOUT_MS = 3000;
|
|
3
|
+
const probeTcp = ({ host, port, timeoutMs, signal }) => new Promise((resolve) => {
|
|
4
|
+
let socket = null;
|
|
5
|
+
const finish = (reachable) => {
|
|
6
|
+
var _a;
|
|
7
|
+
clearTimeout(timer);
|
|
8
|
+
signal.removeEventListener("abort", onAbort);
|
|
9
|
+
try {
|
|
10
|
+
(_a = socket === null || socket === void 0 ? void 0 : socket.removeAllListeners) === null || _a === void 0 ? void 0 : _a.call(socket);
|
|
11
|
+
socket === null || socket === void 0 ? void 0 : socket.destroy();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// ignore
|
|
15
|
+
}
|
|
16
|
+
resolve(reachable);
|
|
17
|
+
};
|
|
18
|
+
const onAbort = () => finish(false);
|
|
19
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
20
|
+
signal.addEventListener("abort", onAbort);
|
|
21
|
+
try {
|
|
22
|
+
socket = TcpSocket.createConnection({ host, port }, () => finish(true));
|
|
23
|
+
socket.on("error", () => finish(false));
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
finish(false);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Opens a bare TCP connection to every address at once and returns the first one that
|
|
31
|
+
* answers, or null. A host drops a connection that sends nothing without reporting it.
|
|
32
|
+
*/
|
|
33
|
+
export async function pickReachableAddress({ addresses, port, timeoutMs = PROBE_TIMEOUT_MS, probe = probeTcp, }) {
|
|
34
|
+
var _a;
|
|
35
|
+
if (addresses.length < 2)
|
|
36
|
+
return (_a = addresses[0]) !== null && _a !== void 0 ? _a : null;
|
|
37
|
+
const controller = new AbortController();
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
let pending = addresses.length;
|
|
40
|
+
for (const host of addresses) {
|
|
41
|
+
probe({ host, port, timeoutMs, signal: controller.signal })
|
|
42
|
+
.catch(() => false)
|
|
43
|
+
.then((reachable) => {
|
|
44
|
+
pending -= 1;
|
|
45
|
+
if (reachable && !controller.signal.aborted) {
|
|
46
|
+
controller.abort();
|
|
47
|
+
resolve(host);
|
|
48
|
+
}
|
|
49
|
+
else if (pending === 0) {
|
|
50
|
+
resolve(null);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -14,8 +14,6 @@ export function disableCaptureEarlyEvents() {
|
|
|
14
14
|
captureEarlyEvents = false;
|
|
15
15
|
}
|
|
16
16
|
export function pushEarlyEvent(payload) {
|
|
17
|
-
if (!getCaptureEarlyEvents())
|
|
18
|
-
return;
|
|
19
17
|
earlyEvents.push(payload);
|
|
20
18
|
if (earlyEvents.length > SOCKET.MAX_EARLY_EVENTS) {
|
|
21
19
|
earlyEvents.shift();
|
|
@@ -13,7 +13,8 @@ export function sendEvent(payload) {
|
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
15
|
if (!socket || !connected) {
|
|
16
|
-
//
|
|
16
|
+
// The handshake is still running: keep the event until the host accepts the session.
|
|
17
|
+
pushEarlyEvent(payload);
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
20
|
internalSendNetworkEvent(payload);
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.
|
|
1
|
+
export declare const SDK_VERSION = "0.6.1";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.6.1";
|
package/package.json
CHANGED