@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.
Files changed (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +210 -2
  3. package/dist/core/cloud/client.d.ts +44 -0
  4. package/dist/core/cloud/client.js +65 -0
  5. package/dist/core/cloud/index.d.ts +4 -0
  6. package/dist/core/cloud/index.js +4 -0
  7. package/dist/core/cloud/link-device.d.ts +14 -0
  8. package/dist/core/cloud/link-device.js +28 -0
  9. package/dist/core/cloud/state.d.ts +10 -0
  10. package/dist/core/cloud/state.js +21 -0
  11. package/dist/core/cloud/storage.d.ts +7 -0
  12. package/dist/core/cloud/storage.js +31 -0
  13. package/dist/core/connection/channel/generate.d.ts +10 -0
  14. package/dist/core/connection/channel/generate.js +22 -0
  15. package/dist/core/connection/channel/index.d.ts +4 -0
  16. package/dist/core/connection/channel/index.js +4 -0
  17. package/dist/core/connection/channel/parse.d.ts +9 -0
  18. package/dist/core/connection/channel/parse.js +27 -0
  19. package/dist/core/connection/channel/sanitize.d.ts +9 -0
  20. package/dist/core/connection/channel/sanitize.js +15 -0
  21. package/dist/core/connection/channel/types.d.ts +9 -0
  22. package/dist/core/connection/channel/types.js +1 -0
  23. package/dist/core/connection/index.d.ts +7 -0
  24. package/dist/core/connection/index.js +7 -0
  25. package/dist/core/connection/lifecycle/connect.d.ts +2 -0
  26. package/dist/core/connection/lifecycle/connect.js +46 -0
  27. package/dist/core/connection/lifecycle/destroy.d.ts +1 -0
  28. package/dist/core/connection/lifecycle/destroy.js +12 -0
  29. package/dist/core/connection/lifecycle/disconnect.d.ts +1 -0
  30. package/dist/core/connection/lifecycle/disconnect.js +10 -0
  31. package/dist/core/connection/lifecycle/early-events.d.ts +2 -0
  32. package/dist/core/connection/lifecycle/early-events.js +18 -0
  33. package/dist/core/connection/lifecycle/index.d.ts +5 -0
  34. package/dist/core/connection/lifecycle/index.js +5 -0
  35. package/dist/core/connection/lifecycle/keep-alive.d.ts +5 -0
  36. package/dist/core/connection/lifecycle/keep-alive.js +40 -0
  37. package/dist/core/connection/prompt/index.d.ts +1 -0
  38. package/dist/core/connection/prompt/index.js +1 -0
  39. package/dist/core/connection/prompt/prompt-state.d.ts +12 -0
  40. package/dist/core/connection/prompt/prompt-state.js +30 -0
  41. package/dist/core/connection/resolver/cloud.d.ts +4 -0
  42. package/dist/core/connection/resolver/cloud.js +29 -0
  43. package/dist/core/connection/resolver/index.d.ts +3 -0
  44. package/dist/core/connection/resolver/index.js +3 -0
  45. package/dist/core/connection/resolver/manual.d.ts +2 -0
  46. package/dist/core/connection/resolver/manual.js +3 -0
  47. package/dist/core/connection/resolver/types.d.ts +9 -0
  48. package/dist/core/connection/resolver/types.js +1 -0
  49. package/dist/core/connection/sdk-enabled.d.ts +3 -0
  50. package/dist/core/connection/sdk-enabled.js +10 -0
  51. package/dist/core/connection/session/clear-last-connection.d.ts +1 -0
  52. package/dist/core/connection/session/clear-last-connection.js +11 -0
  53. package/dist/core/connection/session/client-id.d.ts +3 -0
  54. package/dist/core/connection/session/client-id.js +25 -0
  55. package/dist/core/connection/session/constants.d.ts +14 -0
  56. package/dist/core/connection/session/constants.js +21 -0
  57. package/dist/core/connection/session/create-session.d.ts +3 -0
  58. package/dist/core/connection/session/create-session.js +21 -0
  59. package/dist/core/connection/session/index.d.ts +6 -0
  60. package/dist/core/connection/session/index.js +6 -0
  61. package/dist/core/connection/session/load-last-connection.d.ts +2 -0
  62. package/dist/core/connection/session/load-last-connection.js +16 -0
  63. package/dist/core/connection/session/state.d.ts +7 -0
  64. package/dist/core/connection/session/state.js +23 -0
  65. package/dist/core/connection/session/types.d.ts +12 -0
  66. package/dist/core/connection/session/types.js +1 -0
  67. package/dist/core/connection/socket/connect-socket.d.ts +3 -0
  68. package/dist/core/connection/socket/connect-socket.js +110 -0
  69. package/dist/core/connection/socket/constants.d.ts +4 -0
  70. package/dist/core/connection/socket/constants.js +4 -0
  71. package/dist/core/connection/socket/destroy-socket.d.ts +1 -0
  72. package/dist/core/connection/socket/destroy-socket.js +4 -0
  73. package/dist/core/connection/socket/disconnect-socket.d.ts +1 -0
  74. package/dist/core/connection/socket/disconnect-socket.js +26 -0
  75. package/dist/core/connection/socket/early-buffer.d.ts +7 -0
  76. package/dist/core/connection/socket/early-buffer.js +26 -0
  77. package/dist/core/connection/socket/flush-early-events.d.ts +1 -0
  78. package/dist/core/connection/socket/flush-early-events.js +18 -0
  79. package/dist/core/connection/socket/handle-socket-lost.d.ts +1 -0
  80. package/dist/core/connection/socket/handle-socket-lost.js +24 -0
  81. package/dist/core/connection/socket/index.d.ts +6 -0
  82. package/dist/core/connection/socket/index.js +6 -0
  83. package/dist/core/connection/socket/normalize-socket-error.d.ts +1 -0
  84. package/dist/core/connection/socket/normalize-socket-error.js +13 -0
  85. package/dist/core/connection/socket/send-event.d.ts +3 -0
  86. package/dist/core/connection/socket/send-event.js +45 -0
  87. package/dist/core/connection/socket/state.d.ts +8 -0
  88. package/dist/core/connection/socket/state.js +35 -0
  89. package/dist/core/console/index.d.ts +2 -0
  90. package/dist/core/console/index.js +2 -0
  91. package/dist/core/console/patch-console.d.ts +1 -0
  92. package/dist/core/console/patch-console.js +64 -0
  93. package/dist/core/console/restore-console.d.ts +1 -0
  94. package/dist/core/console/restore-console.js +28 -0
  95. package/dist/core/console/send-console-event.d.ts +8 -0
  96. package/dist/core/console/send-console-event.js +95 -0
  97. package/dist/core/console/state.d.ts +19 -0
  98. package/dist/core/console/state.js +25 -0
  99. package/dist/core/mmkv/index.d.ts +0 -0
  100. package/dist/core/mmkv/index.js +2 -0
  101. package/dist/core/network/get-interceptor.d.ts +11 -0
  102. package/dist/core/network/get-interceptor.js +19 -0
  103. package/dist/core/network/headers.d.ts +6 -0
  104. package/dist/core/network/headers.js +46 -0
  105. package/dist/core/network/index.d.ts +2 -0
  106. package/dist/core/network/index.js +2 -0
  107. package/dist/core/network/patch-xhr.d.ts +2 -0
  108. package/dist/core/network/patch-xhr.js +40 -0
  109. package/dist/core/network/restore-xhr.d.ts +1 -0
  110. package/dist/core/network/restore-xhr.js +40 -0
  111. package/dist/core/network/rn-blob.d.ts +1 -0
  112. package/dist/core/network/rn-blob.js +15 -0
  113. package/dist/core/network/should-skip.d.ts +2 -0
  114. package/dist/core/network/should-skip.js +30 -0
  115. package/dist/core/network/state.d.ts +5 -0
  116. package/dist/core/network/state.js +14 -0
  117. package/dist/core/network/xhr-callbacks.d.ts +12 -0
  118. package/dist/core/network/xhr-callbacks.js +169 -0
  119. package/dist/core/network/xhr-runtime-state.d.ts +4 -0
  120. package/dist/core/network/xhr-runtime-state.js +15 -0
  121. package/dist/core/network/xhr-types.d.ts +33 -0
  122. package/dist/core/network/xhr-types.js +1 -0
  123. package/dist/core/public.d.ts +2 -0
  124. package/dist/core/public.js +1 -0
  125. package/dist/core/react-query/index.d.ts +0 -0
  126. package/dist/core/react-query/index.js +2 -0
  127. package/dist/core/system/dev-server.d.ts +6 -0
  128. package/dist/core/system/dev-server.js +37 -0
  129. package/dist/core/system/device-info.d.ts +3 -0
  130. package/dist/core/system/device-info.js +46 -0
  131. package/dist/core/system/index.d.ts +2 -0
  132. package/dist/core/system/index.js +2 -0
  133. package/dist/core/websocket/index.d.ts +2 -0
  134. package/dist/core/websocket/index.js +2 -0
  135. package/dist/core/websocket/patch-websocket.d.ts +2 -0
  136. package/dist/core/websocket/patch-websocket.js +247 -0
  137. package/dist/core/websocket/restore-websocket.d.ts +1 -0
  138. package/dist/core/websocket/restore-websocket.js +16 -0
  139. package/dist/core/websocket/should-skip.d.ts +2 -0
  140. package/dist/core/websocket/should-skip.js +22 -0
  141. package/dist/core/websocket/state.d.ts +9 -0
  142. package/dist/core/websocket/state.js +21 -0
  143. package/dist/core/zustand/index.d.ts +1 -0
  144. package/dist/core/zustand/index.js +1 -0
  145. package/dist/index.d.ts +3 -0
  146. package/dist/index.js +3 -0
  147. package/dist/public/copy-button/component.d.ts +9 -0
  148. package/dist/public/copy-button/component.js +13 -0
  149. package/dist/public/copy-button/index.d.ts +1 -0
  150. package/dist/public/copy-button/index.js +1 -0
  151. package/dist/public/copy-button/styles.d.ts +64 -0
  152. package/dist/public/copy-button/styles.js +52 -0
  153. package/dist/public/index.d.ts +1 -0
  154. package/dist/public/index.js +1 -0
  155. package/dist/public/prompt/component.d.ts +7 -0
  156. package/dist/public/prompt/component.js +56 -0
  157. package/dist/public/prompt/index.d.ts +1 -0
  158. package/dist/public/prompt/index.js +1 -0
  159. package/dist/public/prompt/prompt-mode.d.ts +3 -0
  160. package/dist/public/prompt/prompt-mode.js +19 -0
  161. package/dist/public/prompt/styles.d.ts +23 -0
  162. package/dist/public/prompt/styles.js +23 -0
  163. package/dist/public/prompt/theme.d.ts +16 -0
  164. package/dist/public/prompt/theme.js +32 -0
  165. package/dist/public/prompt/types.d.ts +12 -0
  166. package/dist/public/prompt/types.js +1 -0
  167. package/dist/public/prompt/ui/action-button.d.ts +11 -0
  168. package/dist/public/prompt/ui/action-button.js +23 -0
  169. package/dist/public/prompt/ui/footer.d.ts +7 -0
  170. package/dist/public/prompt/ui/footer.js +13 -0
  171. package/dist/public/prompt/ui/header.d.ts +10 -0
  172. package/dist/public/prompt/ui/header.js +17 -0
  173. package/dist/public/prompt/ui/report-link.d.ts +8 -0
  174. package/dist/public/prompt/ui/report-link.js +19 -0
  175. package/dist/public/prompt/use-connect-awaiter.d.ts +11 -0
  176. package/dist/public/prompt/use-connect-awaiter.js +45 -0
  177. package/dist/public/prompt/use-prompt-animation.d.ts +17 -0
  178. package/dist/public/prompt/use-prompt-animation.js +53 -0
  179. package/dist/public/prompt/use-prompt-ui.d.ts +3 -0
  180. package/dist/public/prompt/use-prompt-ui.js +99 -0
  181. package/dist/public/prompt/use-prompt.d.ts +11 -0
  182. package/dist/public/prompt/use-prompt.js +43 -0
  183. package/dist/public/prompt/views/cloud-view.d.ts +13 -0
  184. package/dist/public/prompt/views/cloud-view.js +19 -0
  185. package/dist/public/prompt/views/connected-view.d.ts +14 -0
  186. package/dist/public/prompt/views/connected-view.js +9 -0
  187. package/dist/public/prompt/views/manual-view.d.ts +16 -0
  188. package/dist/public/prompt/views/manual-view.js +21 -0
  189. package/dist/public/prompt/views/styles.d.ts +44 -0
  190. package/dist/public/prompt/views/styles.js +23 -0
  191. package/dist/public/prompt/views/use-cloud-view.d.ts +15 -0
  192. package/dist/public/prompt/views/use-cloud-view.js +103 -0
  193. package/dist/public/public.d.ts +1 -0
  194. package/dist/public/public.js +1 -0
  195. package/dist/types/index.d.ts +2 -0
  196. package/dist/types/index.js +2 -0
  197. package/dist/types/options.d.ts +30 -0
  198. package/dist/types/options.js +1 -0
  199. package/dist/types/protocol.d.ts +1 -0
  200. package/dist/types/protocol.js +1 -0
  201. package/dist/types/public.d.ts +1 -0
  202. package/dist/types/public.js +1 -0
  203. package/dist/utils/body-snippet.d.ts +19 -0
  204. package/dist/utils/body-snippet.js +186 -0
  205. package/dist/utils/debug-log.d.ts +26 -0
  206. package/dist/utils/debug-log.js +44 -0
  207. package/dist/utils/generate-random-id.d.ts +1 -0
  208. package/dist/utils/generate-random-id.js +8 -0
  209. package/dist/utils/index.d.ts +5 -0
  210. package/dist/utils/index.js +5 -0
  211. package/dist/utils/parse-url.d.ts +5 -0
  212. package/dist/utils/parse-url.js +32 -0
  213. package/dist/utils/safe-serialize.d.ts +6 -0
  214. package/dist/utils/safe-serialize.js +77 -0
  215. package/dist/version.d.ts +1 -0
  216. package/dist/version.js +1 -0
  217. package/dist/zustand/index.d.ts +1 -0
  218. package/dist/zustand/index.js +1 -0
  219. package/dist/zustand/middleware.d.ts +18 -0
  220. package/dist/zustand/middleware.js +77 -0
  221. package/package.json +88 -4
  222. package/zustand/index.d.ts +1 -0
  223. 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,2 @@
1
+ import type { HeimdallConnectOptions } from "../../../types";
2
+ export declare function setupEarlyEvents(defaults: Omit<HeimdallConnectOptions, "channel">): void;
@@ -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,5 @@
1
+ export * from "./connect";
2
+ export * from "./disconnect";
3
+ export * from "./destroy";
4
+ export * from "./keep-alive";
5
+ export * from "./early-events";
@@ -0,0 +1,5 @@
1
+ export * from "./connect.js";
2
+ export * from "./disconnect.js";
3
+ export * from "./destroy.js";
4
+ export * from "./keep-alive.js";
5
+ export * from "./early-events.js";
@@ -0,0 +1,5 @@
1
+ import type { HeimdallConnectOptions } from "../../../types";
2
+ /**
3
+ * Configure connect prompt + captureEarlyEvents + keepAlive.
4
+ */
5
+ export declare function configureConnectPrompt(defaults: Omit<HeimdallConnectOptions, "channel">): void;
@@ -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,4 @@
1
+ import type { ConnectionResolver } from "./types";
2
+ export declare function createCloudResolver({ baseUrl, }: {
3
+ baseUrl: string;
4
+ }): ConnectionResolver;
@@ -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,3 @@
1
+ export * from "./types";
2
+ export * from "./manual";
3
+ export * from "./cloud";
@@ -0,0 +1,3 @@
1
+ export * from "./types.js";
2
+ export * from "./manual.js";
3
+ export * from "./cloud.js";
@@ -0,0 +1,2 @@
1
+ import type { ConnectionResolver, ConnectionTarget } from "./types";
2
+ export declare function createManualResolver(target: ConnectionTarget): ConnectionResolver;
@@ -0,0 +1,3 @@
1
+ export function createManualResolver(target) {
2
+ return { resolve: async () => target };
3
+ }
@@ -0,0 +1,9 @@
1
+ export type ConnectionTarget = {
2
+ host: string;
3
+ port: number;
4
+ channel: string;
5
+ hostName?: string;
6
+ };
7
+ export type ConnectionResolver = {
8
+ resolve: () => Promise<ConnectionTarget | null>;
9
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export declare function resolveEnabled(flag: boolean | undefined): boolean;
2
+ export declare function setSdkEnabled(value: boolean): void;
3
+ export declare function isSdkEnabled(): boolean;
@@ -0,0 +1,10 @@
1
+ let sdkEnabled = false;
2
+ export function resolveEnabled(flag) {
3
+ return flag !== null && flag !== void 0 ? flag : __DEV__;
4
+ }
5
+ export function setSdkEnabled(value) {
6
+ sdkEnabled = value;
7
+ }
8
+ export function isSdkEnabled() {
9
+ return sdkEnabled;
10
+ }
@@ -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,3 @@
1
+ export declare function generateClientId(): string;
2
+ export declare function getOrCreateClientId(): string;
3
+ export declare function clearClientId(): void;
@@ -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,3 @@
1
+ import type { HeimdallConnectOptions } from "../../../types";
2
+ import type { HeimdallSession } from "./types";
3
+ export declare function createSession(options: HeimdallConnectOptions): HeimdallSession;
@@ -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,6 @@
1
+ export * from "./create-session";
2
+ export * from "./types";
3
+ export * from "./load-last-connection";
4
+ export * from "./clear-last-connection";
5
+ export * from "./client-id";
6
+ export * from "./state";
@@ -0,0 +1,6 @@
1
+ export * from "./create-session.js";
2
+ export * from "./types.js";
3
+ export * from "./load-last-connection.js";
4
+ export * from "./clear-last-connection.js";
5
+ export * from "./client-id.js";
6
+ export * from "./state.js";
@@ -0,0 +1,2 @@
1
+ import type { LastConnection } from "./types";
2
+ export declare function loadLastConnection(): LastConnection | null;
@@ -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,3 @@
1
+ import type { HeimdallConnectOptions } from "../../../types";
2
+ import { type HeimdallSession } from "../session";
3
+ export declare function connectSocket(optionsOrSession: HeimdallSession | HeimdallConnectOptions): void;
@@ -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,4 @@
1
+ export declare const SOCKET: {
2
+ MAX_EARLY_EVENTS: number;
3
+ CONNECT_TIMEOUT_MS: number;
4
+ };
@@ -0,0 +1,4 @@
1
+ export const SOCKET = {
2
+ MAX_EARLY_EVENTS: 50,
3
+ CONNECT_TIMEOUT_MS: 10000,
4
+ };
@@ -0,0 +1 @@
1
+ export declare function destroySocket(): void;
@@ -0,0 +1,4 @@
1
+ import { disconnectSocket } from "./disconnect-socket.js";
2
+ export function destroySocket() {
3
+ disconnectSocket();
4
+ }
@@ -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;