@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,103 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+ import { clearCloudDevice, linkDevice, loadCloudDevice, setLastCloudTarget, unlinkDevice, } from "../../../core/cloud/index.js";
3
+ import { connect, createCloudResolver, getPromptState, } from "../../../core/connection/index.js";
4
+ const useCloudView = ({ baseUrl, visible, startConnecting, }) => {
5
+ const [deviceName, setDeviceName] = useState(() => { var _a, _b; return (_b = (_a = loadCloudDevice()) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : null; });
6
+ const [phase, setPhase] = useState("idle");
7
+ const [message, setMessage] = useState(null);
8
+ const cancelledRef = useRef(false);
9
+ useEffect(() => {
10
+ var _a, _b;
11
+ if (visible) {
12
+ setDeviceName((_b = (_a = loadCloudDevice()) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : null);
13
+ return;
14
+ }
15
+ cancelledRef.current = true;
16
+ setMessage(null);
17
+ }, [visible]);
18
+ const onFindMe = useCallback(async () => {
19
+ const defaults = getPromptState().defaults;
20
+ cancelledRef.current = false;
21
+ setMessage(null);
22
+ setPhase("linking");
23
+ try {
24
+ const result = await linkDevice({
25
+ baseUrl,
26
+ appId: defaults === null || defaults === void 0 ? void 0 : defaults.appId,
27
+ platform: defaults === null || defaults === void 0 ? void 0 : defaults.platform,
28
+ isCancelled: () => cancelledRef.current,
29
+ });
30
+ if (result.status === "linked")
31
+ setDeviceName(result.deviceName);
32
+ if (result.status === "expired")
33
+ setMessage("Link expired, try again.");
34
+ }
35
+ catch {
36
+ setMessage("Heimdall cloud is unreachable.");
37
+ }
38
+ finally {
39
+ setPhase("idle");
40
+ }
41
+ }, [baseUrl]);
42
+ const onCancelLinking = useCallback(() => {
43
+ cancelledRef.current = true;
44
+ setPhase("idle");
45
+ }, []);
46
+ const onConnect = useCallback(async () => {
47
+ var _a, _b, _c;
48
+ const defaults = getPromptState().defaults;
49
+ if (!defaults)
50
+ return;
51
+ setMessage(null);
52
+ setPhase("connecting");
53
+ try {
54
+ const target = await createCloudResolver({ baseUrl }).resolve();
55
+ if (!target) {
56
+ setDeviceName((_b = (_a = loadCloudDevice()) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : null);
57
+ setMessage(`Start Heimdall for ${deviceName !== null && deviceName !== void 0 ? deviceName : "this device"} first.`);
58
+ return;
59
+ }
60
+ setLastCloudTarget({ channel: target.channel, hostName: target.hostName });
61
+ startConnecting(() => connect({
62
+ ...defaults,
63
+ host: target.host,
64
+ port: target.port,
65
+ channel: target.channel,
66
+ }));
67
+ }
68
+ catch {
69
+ const stillLinked = loadCloudDevice();
70
+ setDeviceName((_c = stillLinked === null || stillLinked === void 0 ? void 0 : stillLinked.name) !== null && _c !== void 0 ? _c : null);
71
+ setMessage(stillLinked
72
+ ? "Heimdall cloud is unreachable."
73
+ : "This device was unlinked. Find it again.");
74
+ }
75
+ finally {
76
+ setPhase("idle");
77
+ }
78
+ }, [baseUrl, deviceName, startConnecting]);
79
+ const onUnlink = useCallback(async () => {
80
+ const device = loadCloudDevice();
81
+ clearCloudDevice();
82
+ setDeviceName(null);
83
+ setMessage(null);
84
+ if (device) {
85
+ try {
86
+ await unlinkDevice({ baseUrl, token: device.token });
87
+ }
88
+ catch {
89
+ // ignore: local unlink is enough
90
+ }
91
+ }
92
+ }, [baseUrl]);
93
+ return {
94
+ deviceName,
95
+ phase,
96
+ message,
97
+ onFindMe,
98
+ onCancelLinking,
99
+ onConnect,
100
+ onUnlink,
101
+ };
102
+ };
103
+ export default useCloudView;
@@ -0,0 +1 @@
1
+ export { HeimdallConnectPrompt } from "./prompt";
@@ -0,0 +1 @@
1
+ export { HeimdallConnectPrompt } from "./prompt/index.js";
@@ -0,0 +1,2 @@
1
+ export * from "./options";
2
+ export * from "./protocol";
@@ -0,0 +1,2 @@
1
+ export * from "./options.js";
2
+ export * from "./protocol.js";
@@ -0,0 +1,30 @@
1
+ export type HeimdallPlatform = "ios" | "android" | "macos" | "windows" | "other";
2
+ export type HeimdallConnectOptions = {
3
+ host: string;
4
+ port: number;
5
+ channel: string;
6
+ enabled?: boolean;
7
+ platform?: HeimdallPlatform;
8
+ deviceName?: string;
9
+ appId?: string;
10
+ /** white-list by host (without protocol and path) */
11
+ allowedHosts?: (string | RegExp)[];
12
+ /** additional ignore by full url or path */
13
+ ignoreUrls?: (string | RegExp)[];
14
+ /** capture early events (before connect) */
15
+ captureEarlyEvents?: boolean;
16
+ /** keep the connection alive (default: false) */
17
+ keepAlive?: boolean;
18
+ /** capture console.* calls, uncaught JS errors and unhandled promise rejections (default: true) */
19
+ captureConsole?: boolean;
20
+ /** app details shown in heimdall, e.g. from react-native-device-info */
21
+ app?: {
22
+ version?: string;
23
+ build?: string;
24
+ environment?: string;
25
+ };
26
+ /** heimdall-cloud service: enables "Find me" device linking in the connect prompt */
27
+ cloud?: {
28
+ url: string;
29
+ };
30
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type { HelloWireMessageV1, EventWireMessageV1, EventWirePayload, NetworkFilters, NetworkEventPayload, ZustandEventPayload, WsEventPayload, ConsoleEventPayload, HelloAppInfo, HelloDeviceInfo, } from "@rnzeus/heimdall-protocol";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export type { HeimdallConnectOptions } from "./options";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,19 @@
1
+ export declare const NETWORK: {
2
+ MAX_BODY_BYTES_DEFAULT: number;
3
+ MAX_BODY_BYTES_JSON: number;
4
+ MAX_BODY_BYTES_TEXT: number;
5
+ MAX_JSON_MINIFY_BYTES: number;
6
+ };
7
+ export declare function tryMinifyJsonString(s: string): string | null;
8
+ export type BodySnippetResult = {
9
+ snippet?: string;
10
+ /** UTF-8 bytes of the returned snippet */
11
+ snippetBytes?: number;
12
+ /** UTF-8 bytes of the source (best-effort) */
13
+ sizeBytes?: number;
14
+ truncated?: boolean;
15
+ isBlob?: boolean;
16
+ };
17
+ export declare function buildBodySnippet(body: any, options?: {
18
+ contentType?: string;
19
+ }): BodySnippetResult;
@@ -0,0 +1,186 @@
1
+ // Byte-based limits (UTF-8)
2
+ const MAX_BODY_BYTES_DEFAULT = 50000; // ~50 KB
3
+ const MAX_BODY_BYTES_JSON = 500 * 1024; // 500 KB
4
+ const MAX_BODY_BYTES_TEXT = 200 * 1024; // 200 KB
5
+ // Safety gate: avoid JSON.parse+stringify for very large strings.
6
+ const MAX_JSON_MINIFY_BYTES = 1000000; // 1 MB
7
+ export const NETWORK = {
8
+ MAX_BODY_BYTES_DEFAULT,
9
+ MAX_BODY_BYTES_JSON,
10
+ MAX_BODY_BYTES_TEXT,
11
+ MAX_JSON_MINIFY_BYTES,
12
+ };
13
+ function fromEntries(parts) {
14
+ const out = {};
15
+ for (const p of parts) {
16
+ if (!Array.isArray(p) || p.length < 2)
17
+ continue;
18
+ const k = String(p[0]);
19
+ const v = p[1];
20
+ if (out[k] === undefined)
21
+ out[k] = v;
22
+ else if (Array.isArray(out[k]))
23
+ out[k].push(v);
24
+ else
25
+ out[k] = [out[k], v];
26
+ }
27
+ return out;
28
+ }
29
+ function isRNBlobLike(x) {
30
+ return !!(x &&
31
+ typeof x === "object" &&
32
+ x._data &&
33
+ typeof x._data.blobId === "string");
34
+ }
35
+ function blobPlaceholder(x) {
36
+ var _a, _b;
37
+ const d = (_a = x === null || x === void 0 ? void 0 : x._data) !== null && _a !== void 0 ? _a : {};
38
+ const size = typeof d.size === "number" ? d.size : undefined;
39
+ const type = (_b = d.type) !== null && _b !== void 0 ? _b : "unknown";
40
+ const name = d.name ? ` name=${d.name}` : "";
41
+ const snippet = `[blob ${type} ${size !== null && size !== void 0 ? size : "?"} bytes${name}]`;
42
+ return { snippet, size };
43
+ }
44
+ export function tryMinifyJsonString(s) {
45
+ const t = s.trim();
46
+ if (!(t.startsWith("{") || t.startsWith("[")))
47
+ return null;
48
+ try {
49
+ const obj = JSON.parse(s);
50
+ // Intentionally compact (one-line). Presentation/pretty-print should be done by the consumer UI.
51
+ return JSON.stringify(obj);
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ }
57
+ function normaliseContentType(contentType) {
58
+ var _a;
59
+ if (!contentType)
60
+ return undefined;
61
+ const t = String(contentType).toLowerCase();
62
+ const mime = (_a = t.split(";")[0]) === null || _a === void 0 ? void 0 : _a.trim();
63
+ return mime || undefined;
64
+ }
65
+ function isJsonMime(mime) {
66
+ if (!mime)
67
+ return false;
68
+ return mime === "application/json" || mime.endsWith("+json");
69
+ }
70
+ function maxBytesForContentType(contentType) {
71
+ const mime = normaliseContentType(contentType);
72
+ if (isJsonMime(mime))
73
+ return NETWORK.MAX_BODY_BYTES_JSON;
74
+ if (mime === "text/plain" || mime === "text/html")
75
+ return NETWORK.MAX_BODY_BYTES_TEXT;
76
+ return NETWORK.MAX_BODY_BYTES_DEFAULT;
77
+ }
78
+ function utf8ByteLength(s) {
79
+ let bytes = 0;
80
+ for (let i = 0; i < s.length; i++) {
81
+ const cp = s.codePointAt(i);
82
+ if (cp <= 0x7f)
83
+ bytes += 1;
84
+ else if (cp <= 0x7ff)
85
+ bytes += 2;
86
+ else if (cp <= 0xffff)
87
+ bytes += 3;
88
+ else {
89
+ bytes += 4;
90
+ i++; // surrogate pair
91
+ }
92
+ }
93
+ return bytes;
94
+ }
95
+ function utf8TruncateToBytes(s, maxBytes) {
96
+ if (maxBytes <= 0)
97
+ return { out: "", outBytes: 0, truncated: s.length > 0 };
98
+ let bytes = 0;
99
+ let end = 0;
100
+ for (let i = 0; i < s.length; i++) {
101
+ const cp = s.codePointAt(i);
102
+ const add = cp <= 0x7f ? 1 : cp <= 0x7ff ? 2 : cp <= 0xffff ? 3 : 4;
103
+ if (bytes + add > maxBytes)
104
+ break;
105
+ bytes += add;
106
+ end = i + 1;
107
+ if (cp > 0xffff) {
108
+ i++;
109
+ end = i + 1;
110
+ }
111
+ }
112
+ const out = end === s.length ? s : s.slice(0, end);
113
+ return { out, outBytes: bytes, truncated: end !== s.length };
114
+ }
115
+ function maybeMinifyJsonString(input, contentType) {
116
+ // Best-effort: only try to JSON.parse+stringify if it looks like JSON and is not huge.
117
+ const mime = normaliseContentType(contentType);
118
+ const looksJson = isJsonMime(mime) ||
119
+ input.trim().startsWith("{") ||
120
+ input.trim().startsWith("[");
121
+ const originalBytes = utf8ByteLength(input);
122
+ if (!looksJson)
123
+ return { text: input, sizeBytes: originalBytes };
124
+ if (originalBytes > NETWORK.MAX_JSON_MINIFY_BYTES)
125
+ return { text: input, sizeBytes: originalBytes };
126
+ const min = tryMinifyJsonString(input);
127
+ if (min == null)
128
+ return { text: input, sizeBytes: originalBytes };
129
+ return { text: min, sizeBytes: utf8ByteLength(min) };
130
+ }
131
+ export function buildBodySnippet(body, options) {
132
+ try {
133
+ const contentType = options === null || options === void 0 ? void 0 : options.contentType;
134
+ const maxBytes = maxBytesForContentType(contentType);
135
+ if (body == null)
136
+ return {};
137
+ if (isRNBlobLike(body)) {
138
+ const p = blobPlaceholder(body);
139
+ const snippetBytes = p.snippet ? utf8ByteLength(p.snippet) : undefined;
140
+ const sizeBytes = typeof p.size === "number" ? p.size : undefined;
141
+ return {
142
+ snippet: p.snippet,
143
+ snippetBytes,
144
+ sizeBytes,
145
+ truncated: false,
146
+ isBlob: true,
147
+ };
148
+ }
149
+ let text;
150
+ let sizeBytes;
151
+ if (body &&
152
+ typeof body === "object" &&
153
+ Array.isArray(body._parts)) {
154
+ text = JSON.stringify(fromEntries(body._parts));
155
+ sizeBytes = utf8ByteLength(text);
156
+ }
157
+ else if (typeof body === "string") {
158
+ const r = maybeMinifyJsonString(body, contentType);
159
+ text = r.text;
160
+ sizeBytes = r.sizeBytes;
161
+ }
162
+ else if (body instanceof URLSearchParams) {
163
+ text = body.toString();
164
+ sizeBytes = utf8ByteLength(text);
165
+ }
166
+ else if (typeof body === "object") {
167
+ text = JSON.stringify(body);
168
+ sizeBytes = utf8ByteLength(text);
169
+ }
170
+ else {
171
+ text = String(body);
172
+ sizeBytes = utf8ByteLength(text);
173
+ }
174
+ const t = utf8TruncateToBytes(text, maxBytes);
175
+ return {
176
+ snippet: t.out,
177
+ snippetBytes: t.outBytes,
178
+ sizeBytes,
179
+ truncated: t.truncated ||
180
+ (typeof sizeBytes === "number" ? sizeBytes > maxBytes : false),
181
+ };
182
+ }
183
+ catch {
184
+ return {};
185
+ }
186
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Heimdall SDK Debug Logging
3
+ *
4
+ * Usage:
5
+ * ```ts
6
+ * // In browser console or React Native debugger:
7
+ * globalThis.__HEIMDALL_SDK_DEBUG__ = { ws: true, network: true };
8
+ * // or
9
+ * globalThis.__HEIMDALL_SDK_DEBUG__ = { ws: false, network: true };
10
+ * ```
11
+ */
12
+ export type HeimdallDebugConfig = {
13
+ ws?: boolean;
14
+ console?: boolean;
15
+ network?: boolean;
16
+ };
17
+ type DebugModule = keyof HeimdallDebugConfig;
18
+ /**
19
+ * Debug log helper for Heimdall SDK.
20
+ * Only logs strings to avoid HostObject inspection crashes in RN/Hermes.
21
+ *
22
+ * @param module - Module name (ws, network, etc.)
23
+ * @param message - Message to log
24
+ */
25
+ export declare function debugLog(module: DebugModule, message: string): void;
26
+ export {};
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Heimdall SDK Debug Logging
3
+ *
4
+ * Usage:
5
+ * ```ts
6
+ * // In browser console or React Native debugger:
7
+ * globalThis.__HEIMDALL_SDK_DEBUG__ = { ws: true, network: true };
8
+ * // or
9
+ * globalThis.__HEIMDALL_SDK_DEBUG__ = { ws: false, network: true };
10
+ * ```
11
+ */
12
+ /**
13
+ * Check if debug logging is enabled for a specific module.
14
+ */
15
+ function isDebugEnabled(module) {
16
+ try {
17
+ const g = globalThis;
18
+ const cfg = g.__HEIMDALL_SDK_DEBUG__;
19
+ if (!cfg || typeof cfg !== "object")
20
+ return false;
21
+ return cfg[module] === true;
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ }
27
+ /**
28
+ * Debug log helper for Heimdall SDK.
29
+ * Only logs strings to avoid HostObject inspection crashes in RN/Hermes.
30
+ *
31
+ * @param module - Module name (ws, network, etc.)
32
+ * @param message - Message to log
33
+ */
34
+ export function debugLog(module, message) {
35
+ if (!isDebugEnabled(module))
36
+ return;
37
+ try {
38
+ // eslint-disable-next-line no-console
39
+ console.log(`[Heimdall][${module}] ${message}`);
40
+ }
41
+ catch {
42
+ // ignore
43
+ }
44
+ }
@@ -0,0 +1 @@
1
+ export declare function generateRandomId(length?: number): string;
@@ -0,0 +1,8 @@
1
+ export function generateRandomId(length = 16) {
2
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
3
+ let out = "";
4
+ for (let i = 0; i < length; i++) {
5
+ out += chars[Math.floor(Math.random() * chars.length)];
6
+ }
7
+ return out;
8
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./parse-url";
2
+ export * from "./generate-random-id";
3
+ export * from "./body-snippet";
4
+ export * from "./debug-log";
5
+ export * from "./safe-serialize";
@@ -0,0 +1,5 @@
1
+ export * from "./parse-url.js";
2
+ export * from "./generate-random-id.js";
3
+ export * from "./body-snippet.js";
4
+ export * from "./debug-log.js";
5
+ export * from "./safe-serialize.js";
@@ -0,0 +1,5 @@
1
+ export declare function parseUrl(url: string): {
2
+ url: string;
3
+ host: string;
4
+ path: string;
5
+ };
@@ -0,0 +1,32 @@
1
+ export function parseUrl(url) {
2
+ try {
3
+ const u = new URL(url);
4
+ let host = u.host || u.hostname || "";
5
+ let path = (u.pathname || "") + (u.search || "");
6
+ // RN URL polyfills sometimes mis-handle ws/wss and produce empty host.
7
+ // Fallback: parse as http/https for host/path extraction, but keep original url in output.
8
+ if (!host && (url.startsWith("ws://") || url.startsWith("wss://"))) {
9
+ try {
10
+ const httpUrl = url.replace(/^ws(s?):\/\//, "http$1://");
11
+ const u2 = new URL(httpUrl);
12
+ host = u2.host || u2.hostname || host;
13
+ path = (u2.pathname || path) + (u2.search || "");
14
+ }
15
+ catch {
16
+ // ignore
17
+ }
18
+ }
19
+ return {
20
+ url,
21
+ host,
22
+ path,
23
+ };
24
+ }
25
+ catch {
26
+ return {
27
+ url,
28
+ host: "",
29
+ path: url,
30
+ };
31
+ }
32
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Converts any value into a JSON-safe snapshot with depth, width and string limits.
3
+ * Never throws: getters that throw and host objects become placeholders.
4
+ */
5
+ export declare function toSafeValue(value: unknown, depth?: number, seen?: WeakSet<object>): unknown;
6
+ export declare function safeStringify(value: unknown): string;
@@ -0,0 +1,77 @@
1
+ const MAX_DEPTH = 4;
2
+ const MAX_ENTRIES = 20;
3
+ const MAX_STRING = 2048;
4
+ const clip = (value) => value.length > MAX_STRING ? `${value.slice(0, MAX_STRING)}…` : value;
5
+ /**
6
+ * Converts any value into a JSON-safe snapshot with depth, width and string limits.
7
+ * Never throws: getters that throw and host objects become placeholders.
8
+ */
9
+ export function toSafeValue(value, depth = 0, seen = new WeakSet()) {
10
+ if (value === null || value === undefined)
11
+ return value !== null && value !== void 0 ? value : null;
12
+ const kind = typeof value;
13
+ if (kind === "string")
14
+ return clip(value);
15
+ if (kind === "number")
16
+ return Number.isFinite(value) ? value : String(value);
17
+ if (kind === "boolean")
18
+ return value;
19
+ if (kind === "bigint")
20
+ return `${String(value)}n`;
21
+ if (kind === "symbol")
22
+ return String(value);
23
+ if (kind === "function")
24
+ return `[Function ${value.name || "anonymous"}]`;
25
+ const object = value;
26
+ if (seen.has(object))
27
+ return "[Circular]";
28
+ if (value instanceof Error) {
29
+ return {
30
+ name: value.name,
31
+ message: clip(String(value.message)),
32
+ ...(value.stack ? { stack: clip(String(value.stack)) } : {}),
33
+ };
34
+ }
35
+ if (value instanceof Date) {
36
+ return Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
37
+ }
38
+ if (depth >= MAX_DEPTH)
39
+ return Array.isArray(value) ? "[Array]" : "[Object]";
40
+ seen.add(object);
41
+ try {
42
+ if (Array.isArray(value)) {
43
+ const items = value.slice(0, MAX_ENTRIES).map((item) => toSafeValue(item, depth + 1, seen));
44
+ if (value.length > MAX_ENTRIES)
45
+ items.push(`… ${value.length - MAX_ENTRIES} more`);
46
+ return items;
47
+ }
48
+ const out = {};
49
+ const keys = Object.keys(object);
50
+ for (const key of keys.slice(0, MAX_ENTRIES)) {
51
+ try {
52
+ out[key] = toSafeValue(object[key], depth + 1, seen);
53
+ }
54
+ catch {
55
+ out[key] = "[Unreadable]";
56
+ }
57
+ }
58
+ if (keys.length > MAX_ENTRIES)
59
+ out["…"] = `${keys.length - MAX_ENTRIES} more keys`;
60
+ return out;
61
+ }
62
+ catch {
63
+ return "[Unreadable]";
64
+ }
65
+ finally {
66
+ seen.delete(object);
67
+ }
68
+ }
69
+ export function safeStringify(value) {
70
+ var _a;
71
+ try {
72
+ return (_a = JSON.stringify(toSafeValue(value))) !== null && _a !== void 0 ? _a : String(value);
73
+ }
74
+ catch {
75
+ return "[Unserializable]";
76
+ }
77
+ }
@@ -0,0 +1 @@
1
+ export declare const SDK_VERSION = "0.2.2";
@@ -0,0 +1 @@
1
+ export const SDK_VERSION = "0.2.2";
@@ -0,0 +1 @@
1
+ export * from "./middleware";
@@ -0,0 +1 @@
1
+ export * from "./middleware.js";
@@ -0,0 +1,18 @@
1
+ import type { StateCreator, StoreMutatorIdentifier } from "zustand/vanilla";
2
+ /**
3
+ * Heimdall middleware for Zustand.
4
+ *
5
+ * Usage:
6
+ *
7
+ * const useStore = create(
8
+ * heimdallZustand("connection", (set, get, api) => ({
9
+ * ...
10
+ * }))
11
+ * );
12
+ *
13
+ * Features:
14
+ * - Does not store history / diff in SDK.
15
+ * - Sends snapshots of the state to Heimdall.
16
+ * - Tries to determine the "action" by the name of the top-level function of the store.
17
+ */
18
+ export declare function heimdallZustand<TState, Mis extends [StoreMutatorIdentifier, unknown][] = [], Mos extends [StoreMutatorIdentifier, unknown][] = []>(storeName: string, createState: StateCreator<TState, Mis, Mos>): StateCreator<TState, Mis, Mos>;