@rnzeus/heimdall-sdk 0.0.0-stage → 0.2.2
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/LICENSE +21 -0
- package/README.md +210 -2
- package/dist/core/cloud/client.d.ts +44 -0
- package/dist/core/cloud/client.js +65 -0
- package/dist/core/cloud/index.d.ts +4 -0
- package/dist/core/cloud/index.js +4 -0
- package/dist/core/cloud/link-device.d.ts +14 -0
- package/dist/core/cloud/link-device.js +28 -0
- package/dist/core/cloud/state.d.ts +10 -0
- package/dist/core/cloud/state.js +21 -0
- package/dist/core/cloud/storage.d.ts +7 -0
- package/dist/core/cloud/storage.js +31 -0
- package/dist/core/connection/channel/generate.d.ts +10 -0
- package/dist/core/connection/channel/generate.js +22 -0
- package/dist/core/connection/channel/index.d.ts +4 -0
- package/dist/core/connection/channel/index.js +4 -0
- package/dist/core/connection/channel/parse.d.ts +9 -0
- package/dist/core/connection/channel/parse.js +27 -0
- package/dist/core/connection/channel/sanitize.d.ts +9 -0
- package/dist/core/connection/channel/sanitize.js +15 -0
- package/dist/core/connection/channel/types.d.ts +9 -0
- package/dist/core/connection/channel/types.js +1 -0
- package/dist/core/connection/index.d.ts +7 -0
- package/dist/core/connection/index.js +7 -0
- package/dist/core/connection/lifecycle/connect.d.ts +2 -0
- package/dist/core/connection/lifecycle/connect.js +46 -0
- package/dist/core/connection/lifecycle/destroy.d.ts +1 -0
- package/dist/core/connection/lifecycle/destroy.js +12 -0
- package/dist/core/connection/lifecycle/disconnect.d.ts +1 -0
- package/dist/core/connection/lifecycle/disconnect.js +10 -0
- package/dist/core/connection/lifecycle/early-events.d.ts +2 -0
- package/dist/core/connection/lifecycle/early-events.js +18 -0
- package/dist/core/connection/lifecycle/index.d.ts +5 -0
- package/dist/core/connection/lifecycle/index.js +5 -0
- package/dist/core/connection/lifecycle/keep-alive.d.ts +5 -0
- package/dist/core/connection/lifecycle/keep-alive.js +40 -0
- package/dist/core/connection/prompt/index.d.ts +1 -0
- package/dist/core/connection/prompt/index.js +1 -0
- package/dist/core/connection/prompt/prompt-state.d.ts +12 -0
- package/dist/core/connection/prompt/prompt-state.js +30 -0
- package/dist/core/connection/resolver/cloud.d.ts +4 -0
- package/dist/core/connection/resolver/cloud.js +29 -0
- package/dist/core/connection/resolver/index.d.ts +3 -0
- package/dist/core/connection/resolver/index.js +3 -0
- package/dist/core/connection/resolver/manual.d.ts +2 -0
- package/dist/core/connection/resolver/manual.js +3 -0
- package/dist/core/connection/resolver/types.d.ts +9 -0
- package/dist/core/connection/resolver/types.js +1 -0
- package/dist/core/connection/sdk-enabled.d.ts +3 -0
- package/dist/core/connection/sdk-enabled.js +10 -0
- package/dist/core/connection/session/clear-last-connection.d.ts +1 -0
- package/dist/core/connection/session/clear-last-connection.js +11 -0
- package/dist/core/connection/session/client-id.d.ts +3 -0
- package/dist/core/connection/session/client-id.js +25 -0
- package/dist/core/connection/session/constants.d.ts +14 -0
- package/dist/core/connection/session/constants.js +21 -0
- package/dist/core/connection/session/create-session.d.ts +3 -0
- package/dist/core/connection/session/create-session.js +21 -0
- package/dist/core/connection/session/index.d.ts +6 -0
- package/dist/core/connection/session/index.js +6 -0
- package/dist/core/connection/session/load-last-connection.d.ts +2 -0
- package/dist/core/connection/session/load-last-connection.js +16 -0
- package/dist/core/connection/session/state.d.ts +7 -0
- package/dist/core/connection/session/state.js +23 -0
- package/dist/core/connection/session/types.d.ts +12 -0
- package/dist/core/connection/session/types.js +1 -0
- package/dist/core/connection/socket/connect-socket.d.ts +3 -0
- package/dist/core/connection/socket/connect-socket.js +110 -0
- package/dist/core/connection/socket/constants.d.ts +4 -0
- package/dist/core/connection/socket/constants.js +4 -0
- package/dist/core/connection/socket/destroy-socket.d.ts +1 -0
- package/dist/core/connection/socket/destroy-socket.js +4 -0
- package/dist/core/connection/socket/disconnect-socket.d.ts +1 -0
- package/dist/core/connection/socket/disconnect-socket.js +26 -0
- package/dist/core/connection/socket/early-buffer.d.ts +7 -0
- package/dist/core/connection/socket/early-buffer.js +26 -0
- package/dist/core/connection/socket/flush-early-events.d.ts +1 -0
- package/dist/core/connection/socket/flush-early-events.js +18 -0
- package/dist/core/connection/socket/handle-socket-lost.d.ts +1 -0
- package/dist/core/connection/socket/handle-socket-lost.js +24 -0
- package/dist/core/connection/socket/index.d.ts +6 -0
- package/dist/core/connection/socket/index.js +6 -0
- package/dist/core/connection/socket/normalize-socket-error.d.ts +1 -0
- package/dist/core/connection/socket/normalize-socket-error.js +13 -0
- package/dist/core/connection/socket/send-event.d.ts +3 -0
- package/dist/core/connection/socket/send-event.js +45 -0
- package/dist/core/connection/socket/state.d.ts +8 -0
- package/dist/core/connection/socket/state.js +35 -0
- package/dist/core/console/index.d.ts +2 -0
- package/dist/core/console/index.js +2 -0
- package/dist/core/console/patch-console.d.ts +1 -0
- package/dist/core/console/patch-console.js +64 -0
- package/dist/core/console/restore-console.d.ts +1 -0
- package/dist/core/console/restore-console.js +28 -0
- package/dist/core/console/send-console-event.d.ts +8 -0
- package/dist/core/console/send-console-event.js +95 -0
- package/dist/core/console/state.d.ts +19 -0
- package/dist/core/console/state.js +25 -0
- package/dist/core/mmkv/index.d.ts +0 -0
- package/dist/core/mmkv/index.js +2 -0
- package/dist/core/network/get-interceptor.d.ts +11 -0
- package/dist/core/network/get-interceptor.js +19 -0
- package/dist/core/network/headers.d.ts +6 -0
- package/dist/core/network/headers.js +46 -0
- package/dist/core/network/index.d.ts +2 -0
- package/dist/core/network/index.js +2 -0
- package/dist/core/network/patch-xhr.d.ts +2 -0
- package/dist/core/network/patch-xhr.js +40 -0
- package/dist/core/network/restore-xhr.d.ts +1 -0
- package/dist/core/network/restore-xhr.js +40 -0
- package/dist/core/network/rn-blob.d.ts +1 -0
- package/dist/core/network/rn-blob.js +15 -0
- package/dist/core/network/should-skip.d.ts +2 -0
- package/dist/core/network/should-skip.js +30 -0
- package/dist/core/network/state.d.ts +5 -0
- package/dist/core/network/state.js +14 -0
- package/dist/core/network/xhr-callbacks.d.ts +12 -0
- package/dist/core/network/xhr-callbacks.js +169 -0
- package/dist/core/network/xhr-runtime-state.d.ts +4 -0
- package/dist/core/network/xhr-runtime-state.js +15 -0
- package/dist/core/network/xhr-types.d.ts +33 -0
- package/dist/core/network/xhr-types.js +1 -0
- package/dist/core/public.d.ts +2 -0
- package/dist/core/public.js +1 -0
- package/dist/core/react-query/index.d.ts +0 -0
- package/dist/core/react-query/index.js +2 -0
- package/dist/core/system/dev-server.d.ts +6 -0
- package/dist/core/system/dev-server.js +37 -0
- package/dist/core/system/device-info.d.ts +3 -0
- package/dist/core/system/device-info.js +46 -0
- package/dist/core/system/index.d.ts +2 -0
- package/dist/core/system/index.js +2 -0
- package/dist/core/websocket/index.d.ts +2 -0
- package/dist/core/websocket/index.js +2 -0
- package/dist/core/websocket/patch-websocket.d.ts +2 -0
- package/dist/core/websocket/patch-websocket.js +247 -0
- package/dist/core/websocket/restore-websocket.d.ts +1 -0
- package/dist/core/websocket/restore-websocket.js +16 -0
- package/dist/core/websocket/should-skip.d.ts +2 -0
- package/dist/core/websocket/should-skip.js +22 -0
- package/dist/core/websocket/state.d.ts +9 -0
- package/dist/core/websocket/state.js +21 -0
- package/dist/core/zustand/index.d.ts +1 -0
- package/dist/core/zustand/index.js +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/public/copy-button/component.d.ts +9 -0
- package/dist/public/copy-button/component.js +13 -0
- package/dist/public/copy-button/index.d.ts +1 -0
- package/dist/public/copy-button/index.js +1 -0
- package/dist/public/copy-button/styles.d.ts +64 -0
- package/dist/public/copy-button/styles.js +52 -0
- package/dist/public/index.d.ts +1 -0
- package/dist/public/index.js +1 -0
- package/dist/public/prompt/component.d.ts +7 -0
- package/dist/public/prompt/component.js +56 -0
- package/dist/public/prompt/index.d.ts +1 -0
- package/dist/public/prompt/index.js +1 -0
- package/dist/public/prompt/prompt-mode.d.ts +3 -0
- package/dist/public/prompt/prompt-mode.js +19 -0
- package/dist/public/prompt/styles.d.ts +23 -0
- package/dist/public/prompt/styles.js +23 -0
- package/dist/public/prompt/theme.d.ts +16 -0
- package/dist/public/prompt/theme.js +32 -0
- package/dist/public/prompt/types.d.ts +12 -0
- package/dist/public/prompt/types.js +1 -0
- package/dist/public/prompt/ui/action-button.d.ts +11 -0
- package/dist/public/prompt/ui/action-button.js +23 -0
- package/dist/public/prompt/ui/footer.d.ts +7 -0
- package/dist/public/prompt/ui/footer.js +13 -0
- package/dist/public/prompt/ui/header.d.ts +10 -0
- package/dist/public/prompt/ui/header.js +17 -0
- package/dist/public/prompt/ui/report-link.d.ts +8 -0
- package/dist/public/prompt/ui/report-link.js +19 -0
- package/dist/public/prompt/use-connect-awaiter.d.ts +11 -0
- package/dist/public/prompt/use-connect-awaiter.js +45 -0
- package/dist/public/prompt/use-prompt-animation.d.ts +17 -0
- package/dist/public/prompt/use-prompt-animation.js +53 -0
- package/dist/public/prompt/use-prompt-ui.d.ts +3 -0
- package/dist/public/prompt/use-prompt-ui.js +99 -0
- package/dist/public/prompt/use-prompt.d.ts +11 -0
- package/dist/public/prompt/use-prompt.js +43 -0
- package/dist/public/prompt/views/cloud-view.d.ts +13 -0
- package/dist/public/prompt/views/cloud-view.js +19 -0
- package/dist/public/prompt/views/connected-view.d.ts +14 -0
- package/dist/public/prompt/views/connected-view.js +9 -0
- package/dist/public/prompt/views/manual-view.d.ts +16 -0
- package/dist/public/prompt/views/manual-view.js +21 -0
- package/dist/public/prompt/views/styles.d.ts +44 -0
- package/dist/public/prompt/views/styles.js +23 -0
- package/dist/public/prompt/views/use-cloud-view.d.ts +15 -0
- package/dist/public/prompt/views/use-cloud-view.js +103 -0
- package/dist/public/public.d.ts +1 -0
- package/dist/public/public.js +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/options.d.ts +30 -0
- package/dist/types/options.js +1 -0
- package/dist/types/protocol.d.ts +1 -0
- package/dist/types/protocol.js +1 -0
- package/dist/types/public.d.ts +1 -0
- package/dist/types/public.js +1 -0
- package/dist/utils/body-snippet.d.ts +19 -0
- package/dist/utils/body-snippet.js +186 -0
- package/dist/utils/debug-log.d.ts +26 -0
- package/dist/utils/debug-log.js +44 -0
- package/dist/utils/generate-random-id.d.ts +1 -0
- package/dist/utils/generate-random-id.js +8 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/parse-url.d.ts +5 -0
- package/dist/utils/parse-url.js +32 -0
- package/dist/utils/safe-serialize.d.ts +6 -0
- package/dist/utils/safe-serialize.js +77 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/dist/zustand/index.d.ts +1 -0
- package/dist/zustand/index.js +1 -0
- package/dist/zustand/middleware.d.ts +18 -0
- package/dist/zustand/middleware.js +77 -0
- package/package.json +88 -4
- package/zustand/index.d.ts +1 -0
- package/zustand/index.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function destroy(): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { restoreXHR } from "../../network/index.js";
|
|
2
|
+
import { restoreWebSocket } from "../../websocket/index.js";
|
|
3
|
+
import { restoreConsole } from "../../console/index.js";
|
|
4
|
+
import { destroySocket } from "../socket/index.js";
|
|
5
|
+
import { setSdkEnabled } from "../sdk-enabled.js";
|
|
6
|
+
export function destroy() {
|
|
7
|
+
restoreXHR();
|
|
8
|
+
restoreWebSocket();
|
|
9
|
+
restoreConsole();
|
|
10
|
+
destroySocket();
|
|
11
|
+
setSdkEnabled(false);
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function disconnect(): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { restoreXHR } from "../../network/index.js";
|
|
2
|
+
import { restoreWebSocket } from "../../websocket/index.js";
|
|
3
|
+
import { restoreConsole } from "../../console/index.js";
|
|
4
|
+
import { disconnectSocket } from "../socket/index.js";
|
|
5
|
+
export function disconnect() {
|
|
6
|
+
restoreXHR();
|
|
7
|
+
restoreWebSocket();
|
|
8
|
+
restoreConsole();
|
|
9
|
+
disconnectSocket();
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { patchXHR } from "../../network/index.js";
|
|
2
|
+
import { patchWebSocket } from "../../websocket/index.js";
|
|
3
|
+
import { patchConsole } from "../../console/index.js";
|
|
4
|
+
import { enableCaptureEarlyEvents } from "../socket/index.js";
|
|
5
|
+
export function setupEarlyEvents(defaults) {
|
|
6
|
+
enableCaptureEarlyEvents();
|
|
7
|
+
patchXHR({
|
|
8
|
+
allowedHosts: defaults.allowedHosts,
|
|
9
|
+
ignoreUrls: defaults.ignoreUrls,
|
|
10
|
+
});
|
|
11
|
+
patchWebSocket({
|
|
12
|
+
allowedHosts: defaults.allowedHosts,
|
|
13
|
+
ignoreUrls: defaults.ignoreUrls,
|
|
14
|
+
});
|
|
15
|
+
if (defaults.captureConsole !== false) {
|
|
16
|
+
patchConsole();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { setPromptDefaults } from "../prompt/index.js";
|
|
2
|
+
import { loadLastConnection } from "../session/index.js";
|
|
3
|
+
import { setSdkEnabled, resolveEnabled } from "../sdk-enabled.js";
|
|
4
|
+
import { setupEarlyEvents } from "./early-events.js";
|
|
5
|
+
import { connect } from "./connect.js";
|
|
6
|
+
import { registerCloudUrl } from "../../cloud/state.js";
|
|
7
|
+
/**
|
|
8
|
+
* Configure connect prompt + captureEarlyEvents + keepAlive.
|
|
9
|
+
*/
|
|
10
|
+
export function configureConnectPrompt(defaults) {
|
|
11
|
+
var _a;
|
|
12
|
+
const { captureEarlyEvents, keepAlive, enabled, ...rest } = defaults;
|
|
13
|
+
const resolvedEnabled = resolveEnabled(enabled);
|
|
14
|
+
if (!resolvedEnabled) {
|
|
15
|
+
setSdkEnabled(false);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
setSdkEnabled(true);
|
|
19
|
+
registerCloudUrl((_a = rest.cloud) === null || _a === void 0 ? void 0 : _a.url);
|
|
20
|
+
setPromptDefaults({
|
|
21
|
+
...rest,
|
|
22
|
+
enabled: resolvedEnabled,
|
|
23
|
+
});
|
|
24
|
+
// early collection (before connect), if needed
|
|
25
|
+
if (captureEarlyEvents) {
|
|
26
|
+
setupEarlyEvents(rest);
|
|
27
|
+
}
|
|
28
|
+
// keepAlive: auto-connect, if there is a last connection
|
|
29
|
+
if (keepAlive) {
|
|
30
|
+
const last = loadLastConnection();
|
|
31
|
+
if (last && last.host && last.port && last.channel) {
|
|
32
|
+
connect({
|
|
33
|
+
...rest,
|
|
34
|
+
...last,
|
|
35
|
+
channel: last.channel,
|
|
36
|
+
enabled: resolvedEnabled,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./prompt-state";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./prompt-state.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HeimdallConnectOptions } from "../../../types";
|
|
2
|
+
type PromptDefaults = Omit<HeimdallConnectOptions, "channel">;
|
|
3
|
+
type PromptState = {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
defaults: PromptDefaults | null;
|
|
6
|
+
};
|
|
7
|
+
export declare function getPromptState(): PromptState;
|
|
8
|
+
export declare function setPromptDefaults(defaults: PromptDefaults): void;
|
|
9
|
+
export declare function showPrompt(): void;
|
|
10
|
+
export declare function hidePrompt(): void;
|
|
11
|
+
export declare function subscribePrompt(listener: () => void): () => void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
let state = {
|
|
2
|
+
visible: false,
|
|
3
|
+
defaults: null,
|
|
4
|
+
};
|
|
5
|
+
const listeners = new Set();
|
|
6
|
+
export function getPromptState() {
|
|
7
|
+
return state;
|
|
8
|
+
}
|
|
9
|
+
export function setPromptDefaults(defaults) {
|
|
10
|
+
state = { ...state, defaults };
|
|
11
|
+
notify();
|
|
12
|
+
}
|
|
13
|
+
export function showPrompt() {
|
|
14
|
+
state = { ...state, visible: true };
|
|
15
|
+
notify();
|
|
16
|
+
}
|
|
17
|
+
export function hidePrompt() {
|
|
18
|
+
state = { ...state, visible: false };
|
|
19
|
+
notify();
|
|
20
|
+
}
|
|
21
|
+
export function subscribePrompt(listener) {
|
|
22
|
+
listeners.add(listener);
|
|
23
|
+
return () => {
|
|
24
|
+
listeners.delete(listener);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function notify() {
|
|
28
|
+
for (const l of listeners)
|
|
29
|
+
l();
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CloudUnauthorizedError, clearCloudDevice, fetchConnection, loadCloudDevice, } from "../../cloud/index.js";
|
|
2
|
+
export function createCloudResolver({ baseUrl, }) {
|
|
3
|
+
return {
|
|
4
|
+
resolve: async () => {
|
|
5
|
+
const device = loadCloudDevice();
|
|
6
|
+
if (!device)
|
|
7
|
+
return null;
|
|
8
|
+
try {
|
|
9
|
+
const connection = await fetchConnection({
|
|
10
|
+
baseUrl,
|
|
11
|
+
token: device.token,
|
|
12
|
+
});
|
|
13
|
+
if (!connection)
|
|
14
|
+
return null;
|
|
15
|
+
return {
|
|
16
|
+
host: connection.host,
|
|
17
|
+
port: connection.port,
|
|
18
|
+
channel: connection.channel,
|
|
19
|
+
hostName: connection.hostName,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (error instanceof CloudUnauthorizedError)
|
|
24
|
+
clearCloudDevice();
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function clearLastConnection(): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SESSION_STORAGE, SESSION } from "./constants.js";
|
|
2
|
+
export function clearLastConnection() {
|
|
3
|
+
try {
|
|
4
|
+
SESSION_STORAGE.delete(SESSION.KEY_LAST_HOST);
|
|
5
|
+
SESSION_STORAGE.delete(SESSION.KEY_LAST_PORT);
|
|
6
|
+
SESSION_STORAGE.delete(SESSION.KEY_LAST_CHANNEL);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
// ignore
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SESSION, SESSION_STORAGE } from "./constants.js";
|
|
2
|
+
export function generateClientId() {
|
|
3
|
+
return Math.random().toString(16).slice(2) + Date.now().toString(16);
|
|
4
|
+
}
|
|
5
|
+
export function getOrCreateClientId() {
|
|
6
|
+
try {
|
|
7
|
+
const existing = SESSION_STORAGE.getString(SESSION.KEY_CLIENT_ID);
|
|
8
|
+
if (existing)
|
|
9
|
+
return existing;
|
|
10
|
+
const next = generateClientId();
|
|
11
|
+
SESSION_STORAGE.set(SESSION.KEY_CLIENT_ID, next);
|
|
12
|
+
return next;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return generateClientId();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function clearClientId() {
|
|
19
|
+
try {
|
|
20
|
+
SESSION_STORAGE.delete(SESSION.KEY_CLIENT_ID);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// ignore
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const SESSION: {
|
|
2
|
+
STORAGE_ID: string;
|
|
3
|
+
KEY_CLIENT_ID: string;
|
|
4
|
+
KEY_LAST_HOST: string;
|
|
5
|
+
KEY_LAST_PORT: string;
|
|
6
|
+
KEY_LAST_CHANNEL: string;
|
|
7
|
+
};
|
|
8
|
+
type SessionStorage = {
|
|
9
|
+
getString(key: string): string | undefined;
|
|
10
|
+
set(key: string, value: string | number | boolean | Uint8Array): void;
|
|
11
|
+
delete(key: string): void;
|
|
12
|
+
};
|
|
13
|
+
export declare const SESSION_STORAGE: SessionStorage;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as MMKVModule from "react-native-mmkv";
|
|
2
|
+
export const SESSION = {
|
|
3
|
+
STORAGE_ID: "heimdall-sdk",
|
|
4
|
+
KEY_CLIENT_ID: "clientId",
|
|
5
|
+
KEY_LAST_HOST: "lastHost",
|
|
6
|
+
KEY_LAST_PORT: "lastPort",
|
|
7
|
+
KEY_LAST_CHANNEL: "lastChannel",
|
|
8
|
+
};
|
|
9
|
+
function createSessionStorage() {
|
|
10
|
+
const mmkv = MMKVModule;
|
|
11
|
+
// react-native-mmkv v4+
|
|
12
|
+
if (typeof mmkv.createMMKV === "function") {
|
|
13
|
+
return mmkv.createMMKV({ id: SESSION.STORAGE_ID });
|
|
14
|
+
}
|
|
15
|
+
// react-native-mmkv v3
|
|
16
|
+
if (typeof mmkv.MMKV === "function") {
|
|
17
|
+
return new mmkv.MMKV({ id: SESSION.STORAGE_ID });
|
|
18
|
+
}
|
|
19
|
+
throw new Error("react-native-mmkv is installed, but neither createMMKV nor MMKV were found.");
|
|
20
|
+
}
|
|
21
|
+
export const SESSION_STORAGE = createSessionStorage();
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getOrCreateClientId, generateClientId } from "./client-id.js";
|
|
2
|
+
import { SESSION_STORAGE, SESSION } from "./constants.js";
|
|
3
|
+
export function createSession(options) {
|
|
4
|
+
const clientId = getOrCreateClientId();
|
|
5
|
+
const sessionId = generateClientId();
|
|
6
|
+
const createdAt = new Date().toISOString();
|
|
7
|
+
try {
|
|
8
|
+
SESSION_STORAGE.set(SESSION.KEY_LAST_HOST, options.host);
|
|
9
|
+
SESSION_STORAGE.set(SESSION.KEY_LAST_PORT, String(options.port));
|
|
10
|
+
SESSION_STORAGE.set(SESSION.KEY_LAST_CHANNEL, options.channel);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
// ignore
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
clientId,
|
|
17
|
+
sessionId,
|
|
18
|
+
createdAt,
|
|
19
|
+
options,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SESSION, SESSION_STORAGE } from "./constants.js";
|
|
2
|
+
export function loadLastConnection() {
|
|
3
|
+
var _a, _b;
|
|
4
|
+
try {
|
|
5
|
+
const host = (_a = SESSION_STORAGE.getString(SESSION.KEY_LAST_HOST)) !== null && _a !== void 0 ? _a : undefined;
|
|
6
|
+
const portString = SESSION_STORAGE.getString(SESSION.KEY_LAST_PORT);
|
|
7
|
+
const channel = (_b = SESSION_STORAGE.getString(SESSION.KEY_LAST_CHANNEL)) !== null && _b !== void 0 ? _b : undefined;
|
|
8
|
+
const port = portString ? Number(portString) : undefined;
|
|
9
|
+
if (!host && !port && !channel)
|
|
10
|
+
return null;
|
|
11
|
+
return { host, port, channel };
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HeimdallSession } from "./types";
|
|
2
|
+
export declare function getCurrentSession(): HeimdallSession | null;
|
|
3
|
+
export declare function setCurrentSession(session: HeimdallSession | null): void;
|
|
4
|
+
export declare function getCurrentChannel(): string | null;
|
|
5
|
+
export declare function getCurrentHost(): string | null;
|
|
6
|
+
export declare function getCurrentPort(): number | null;
|
|
7
|
+
export declare function getCurrentOptions(): import("../../..").HeimdallConnectOptions | null;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let currentSession = null;
|
|
2
|
+
export function getCurrentSession() {
|
|
3
|
+
return currentSession;
|
|
4
|
+
}
|
|
5
|
+
export function setCurrentSession(session) {
|
|
6
|
+
currentSession = session;
|
|
7
|
+
}
|
|
8
|
+
export function getCurrentChannel() {
|
|
9
|
+
var _a;
|
|
10
|
+
return (_a = currentSession === null || currentSession === void 0 ? void 0 : currentSession.options.channel) !== null && _a !== void 0 ? _a : null;
|
|
11
|
+
}
|
|
12
|
+
export function getCurrentHost() {
|
|
13
|
+
var _a;
|
|
14
|
+
return (_a = currentSession === null || currentSession === void 0 ? void 0 : currentSession.options.host) !== null && _a !== void 0 ? _a : null;
|
|
15
|
+
}
|
|
16
|
+
export function getCurrentPort() {
|
|
17
|
+
var _a;
|
|
18
|
+
return (_a = currentSession === null || currentSession === void 0 ? void 0 : currentSession.options.port) !== null && _a !== void 0 ? _a : null;
|
|
19
|
+
}
|
|
20
|
+
export function getCurrentOptions() {
|
|
21
|
+
var _a;
|
|
22
|
+
return (_a = currentSession === null || currentSession === void 0 ? void 0 : currentSession.options) !== null && _a !== void 0 ? _a : null;
|
|
23
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HeimdallConnectOptions } from "../../../types";
|
|
2
|
+
export type HeimdallSession = {
|
|
3
|
+
clientId: string;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
createdAt: string;
|
|
6
|
+
options: HeimdallConnectOptions;
|
|
7
|
+
};
|
|
8
|
+
export type LastConnection = {
|
|
9
|
+
host?: string;
|
|
10
|
+
port?: number;
|
|
11
|
+
channel?: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import TcpSocket from "react-native-tcp-socket";
|
|
2
|
+
import { isConnected, getSocket, setConnected, setSocket, setLastError, } from "./state.js";
|
|
3
|
+
import { handleSocketLost } from "./handle-socket-lost.js";
|
|
4
|
+
import { flushEarlyEvents } from "./flush-early-events.js";
|
|
5
|
+
import { normalizeSocketError } from "./normalize-socket-error.js";
|
|
6
|
+
import { SOCKET } from "./constants.js";
|
|
7
|
+
import { getCurrentSession, setCurrentSession, } from "../session/index.js";
|
|
8
|
+
import { SDK_VERSION } from "../../../version.js";
|
|
9
|
+
import { normaliseAppInfo, readDeviceInfo } from "../../system/index.js";
|
|
10
|
+
export function connectSocket(optionsOrSession) {
|
|
11
|
+
var _a, _b, _c;
|
|
12
|
+
setLastError(null);
|
|
13
|
+
const session = optionsOrSession
|
|
14
|
+
.clientId
|
|
15
|
+
? optionsOrSession
|
|
16
|
+
: {
|
|
17
|
+
clientId: "legacy-" + Math.random().toString(16).slice(2),
|
|
18
|
+
sessionId: Math.random().toString(16).slice(2),
|
|
19
|
+
createdAt: new Date().toISOString(),
|
|
20
|
+
options: optionsOrSession,
|
|
21
|
+
};
|
|
22
|
+
const options = session.options;
|
|
23
|
+
const enabled = (_a = options.enabled) !== null && _a !== void 0 ? _a : __DEV__;
|
|
24
|
+
if (!enabled)
|
|
25
|
+
return;
|
|
26
|
+
const socket = getSocket();
|
|
27
|
+
const connected = isConnected();
|
|
28
|
+
const currentSession = getCurrentSession();
|
|
29
|
+
// If the socket is already connected and the session is the same, return
|
|
30
|
+
if (socket &&
|
|
31
|
+
connected &&
|
|
32
|
+
currentSession &&
|
|
33
|
+
currentSession.options.channel === options.channel &&
|
|
34
|
+
currentSession.options.host === options.host &&
|
|
35
|
+
currentSession.options.port === options.port) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
// If there is an old socket, close it only, without affecting earlyEvents/lastConnection
|
|
39
|
+
if (socket) {
|
|
40
|
+
try {
|
|
41
|
+
(_b = socket.removeAllListeners) === null || _b === void 0 ? void 0 : _b.call(socket);
|
|
42
|
+
(_c = socket.end) === null || _c === void 0 ? void 0 : _c.call(socket);
|
|
43
|
+
socket.destroy();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// ignore
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
setSocket(null);
|
|
50
|
+
setConnected(false);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Update the current session to the new one
|
|
54
|
+
setCurrentSession(session);
|
|
55
|
+
const newSocket = TcpSocket.createConnection({
|
|
56
|
+
host: options.host,
|
|
57
|
+
port: options.port,
|
|
58
|
+
}, () => {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
clearTimeout(connectTimeout);
|
|
61
|
+
setConnected(true);
|
|
62
|
+
// Take the actual session (if it changed)
|
|
63
|
+
const activeSession = getCurrentSession();
|
|
64
|
+
const hello = {
|
|
65
|
+
kind: "hello",
|
|
66
|
+
v: 1,
|
|
67
|
+
channel: options.channel,
|
|
68
|
+
clientId: (_a = activeSession === null || activeSession === void 0 ? void 0 : activeSession.clientId) !== null && _a !== void 0 ? _a : session.clientId,
|
|
69
|
+
sessionId: (_b = activeSession === null || activeSession === void 0 ? void 0 : activeSession.sessionId) !== null && _b !== void 0 ? _b : session.sessionId,
|
|
70
|
+
deviceName: options.deviceName,
|
|
71
|
+
platform: options.platform,
|
|
72
|
+
appId: options.appId,
|
|
73
|
+
sdkVersion: SDK_VERSION,
|
|
74
|
+
};
|
|
75
|
+
const device = readDeviceInfo();
|
|
76
|
+
if (device)
|
|
77
|
+
hello.device = device;
|
|
78
|
+
const app = normaliseAppInfo(options.app);
|
|
79
|
+
if (app)
|
|
80
|
+
hello.app = app;
|
|
81
|
+
try {
|
|
82
|
+
newSocket.write(JSON.stringify(hello) + "\r\n");
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// ignore
|
|
86
|
+
}
|
|
87
|
+
// After successful HELLO, send early events
|
|
88
|
+
flushEarlyEvents();
|
|
89
|
+
});
|
|
90
|
+
const connectTimeout = setTimeout(() => {
|
|
91
|
+
if (getSocket() !== newSocket || isConnected())
|
|
92
|
+
return;
|
|
93
|
+
setLastError(`Can't reach Heimdall at ${options.host}:${options.port}. Same Wi-Fi?`);
|
|
94
|
+
handleSocketLost();
|
|
95
|
+
}, SOCKET.CONNECT_TIMEOUT_MS);
|
|
96
|
+
newSocket.on("error", (error) => {
|
|
97
|
+
clearTimeout(connectTimeout);
|
|
98
|
+
const message = normalizeSocketError(error);
|
|
99
|
+
setLastError(message);
|
|
100
|
+
if (__DEV__) {
|
|
101
|
+
console.warn("[Heimdall] socket error:", message);
|
|
102
|
+
}
|
|
103
|
+
handleSocketLost();
|
|
104
|
+
});
|
|
105
|
+
newSocket.on("close", () => {
|
|
106
|
+
clearTimeout(connectTimeout);
|
|
107
|
+
handleSocketLost();
|
|
108
|
+
});
|
|
109
|
+
setSocket(newSocket);
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function destroySocket(): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function disconnectSocket(): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getSocket, setSocket, setConnected } from "./state.js";
|
|
2
|
+
import { clearEarlyEvents, disableCaptureEarlyEvents } from "./early-buffer.js";
|
|
3
|
+
import { clearLastConnection, setCurrentSession } from "../session/index.js";
|
|
4
|
+
export function disconnectSocket() {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const socket = getSocket();
|
|
7
|
+
if (socket) {
|
|
8
|
+
try {
|
|
9
|
+
// first remove listeners, then gracefully close, then hard close
|
|
10
|
+
(_a = socket.removeAllListeners) === null || _a === void 0 ? void 0 : _a.call(socket);
|
|
11
|
+
(_b = socket.end) === null || _b === void 0 ? void 0 : _b.call(socket);
|
|
12
|
+
socket.destroy();
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// ignore any socket-level errors
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
setSocket(null);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
setConnected(false);
|
|
22
|
+
setCurrentSession(null);
|
|
23
|
+
clearLastConnection();
|
|
24
|
+
clearEarlyEvents();
|
|
25
|
+
disableCaptureEarlyEvents();
|
|
26
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EventWirePayload } from "../../../types";
|
|
2
|
+
export declare function getEarlyEvents(): EventWirePayload[];
|
|
3
|
+
export declare function getCaptureEarlyEvents(): boolean;
|
|
4
|
+
export declare function enableCaptureEarlyEvents(): void;
|
|
5
|
+
export declare function disableCaptureEarlyEvents(): void;
|
|
6
|
+
export declare function pushEarlyEvent(payload: EventWirePayload): void;
|
|
7
|
+
export declare function clearEarlyEvents(): void;
|