@rnzeus/heimdall-sdk 0.0.0-stage → 0.2.3

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,26 @@
1
+ import { SOCKET } from "./constants.js";
2
+ const earlyEvents = [];
3
+ let captureEarlyEvents = false;
4
+ export function getEarlyEvents() {
5
+ return earlyEvents;
6
+ }
7
+ export function getCaptureEarlyEvents() {
8
+ return captureEarlyEvents;
9
+ }
10
+ export function enableCaptureEarlyEvents() {
11
+ captureEarlyEvents = true;
12
+ }
13
+ export function disableCaptureEarlyEvents() {
14
+ captureEarlyEvents = false;
15
+ }
16
+ export function pushEarlyEvent(payload) {
17
+ if (!getCaptureEarlyEvents())
18
+ return;
19
+ earlyEvents.push(payload);
20
+ if (earlyEvents.length > SOCKET.MAX_EARLY_EVENTS) {
21
+ earlyEvents.shift();
22
+ }
23
+ }
24
+ export function clearEarlyEvents() {
25
+ earlyEvents.length = 0;
26
+ }
@@ -0,0 +1 @@
1
+ export declare function flushEarlyEvents(): void;
@@ -0,0 +1,18 @@
1
+ import { getSocket, isConnected } from "./state.js";
2
+ import { clearEarlyEvents, getEarlyEvents } from "./early-buffer.js";
3
+ import { internalSendNetworkEvent } from "./send-event.js";
4
+ import { getCurrentSession } from "../session/index.js";
5
+ export function flushEarlyEvents() {
6
+ const socket = getSocket();
7
+ const connected = isConnected();
8
+ const currentSession = getCurrentSession();
9
+ const earlyEvents = getEarlyEvents();
10
+ if (!socket || !connected || !currentSession)
11
+ return;
12
+ if (earlyEvents.length === 0)
13
+ return;
14
+ for (const ev of earlyEvents) {
15
+ internalSendNetworkEvent(ev);
16
+ }
17
+ clearEarlyEvents();
18
+ }
@@ -0,0 +1 @@
1
+ export declare function handleSocketLost(): void;
@@ -0,0 +1,24 @@
1
+ import { getSocket, setSocket, setConnected } from "./state.js";
2
+ import { clearEarlyEvents, disableCaptureEarlyEvents } from "./early-buffer.js";
3
+ import { setCurrentSession } from "../session/index.js";
4
+ export function handleSocketLost() {
5
+ var _a, _b;
6
+ const socket = getSocket();
7
+ if (socket) {
8
+ try {
9
+ (_a = socket.removeAllListeners) === null || _a === void 0 ? void 0 : _a.call(socket);
10
+ (_b = socket.end) === null || _b === void 0 ? void 0 : _b.call(socket);
11
+ socket.destroy();
12
+ }
13
+ catch {
14
+ // ignore any socket-level errors
15
+ }
16
+ finally {
17
+ setSocket(null);
18
+ }
19
+ }
20
+ setConnected(false);
21
+ setCurrentSession(null);
22
+ clearEarlyEvents();
23
+ disableCaptureEarlyEvents();
24
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./connect-socket";
2
+ export * from "./disconnect-socket";
3
+ export * from "./destroy-socket";
4
+ export * from "./send-event";
5
+ export * from "./early-buffer";
6
+ export * from "./state";
@@ -0,0 +1,6 @@
1
+ export * from "./connect-socket.js";
2
+ export * from "./disconnect-socket.js";
3
+ export * from "./destroy-socket.js";
4
+ export * from "./send-event.js";
5
+ export * from "./early-buffer.js";
6
+ export * from "./state.js";
@@ -0,0 +1 @@
1
+ export declare function normalizeSocketError(error: unknown): string;
@@ -0,0 +1,13 @@
1
+ const UNKNOWN_SOCKET_ERROR = "Unknown socket error";
2
+ export function normalizeSocketError(error) {
3
+ if (typeof error === "string" && error.length > 0)
4
+ return error;
5
+ if (error &&
6
+ typeof error === "object" &&
7
+ "message" in error &&
8
+ typeof error.message === "string" &&
9
+ error.message.length > 0) {
10
+ return error.message;
11
+ }
12
+ return UNKNOWN_SOCKET_ERROR;
13
+ }
@@ -0,0 +1,3 @@
1
+ import type { EventWirePayload } from "../../../types";
2
+ export declare function sendEvent(payload: EventWirePayload): void;
3
+ export declare function internalSendNetworkEvent(payload: EventWirePayload): void;
@@ -0,0 +1,45 @@
1
+ import { isConnected, getSocket } from "./state.js";
2
+ import { getCaptureEarlyEvents, pushEarlyEvent } from "./early-buffer.js";
3
+ import { getCurrentSession } from "../session/index.js";
4
+ export function sendEvent(payload) {
5
+ const currentSession = getCurrentSession();
6
+ const captureEarlyEvents = getCaptureEarlyEvents();
7
+ const socket = getSocket();
8
+ const connected = isConnected();
9
+ if (!currentSession) {
10
+ if (captureEarlyEvents) {
11
+ pushEarlyEvent(payload);
12
+ }
13
+ return;
14
+ }
15
+ if (!socket || !connected) {
16
+ // connect() was already called, but the socket is not ready — in v1 we don't buffer, just drop
17
+ return;
18
+ }
19
+ internalSendNetworkEvent(payload);
20
+ }
21
+ export function internalSendNetworkEvent(payload) {
22
+ const currentSession = getCurrentSession();
23
+ const socket = getSocket();
24
+ const connected = isConnected();
25
+ if (!socket || !connected || !currentSession)
26
+ return;
27
+ const { options, clientId, sessionId } = currentSession;
28
+ const message = {
29
+ kind: "event",
30
+ v: 1,
31
+ channel: options.channel,
32
+ clientId,
33
+ sessionId,
34
+ deviceName: options.deviceName,
35
+ platform: options.platform,
36
+ appId: options.appId,
37
+ event: payload,
38
+ };
39
+ try {
40
+ socket.write(JSON.stringify(message) + "\r\n");
41
+ }
42
+ catch {
43
+ // ignore
44
+ }
45
+ }
@@ -0,0 +1,8 @@
1
+ /** Notified when the socket connects, disconnects or reports an error. */
2
+ export declare function subscribeSocketState(listener: () => void): () => void;
3
+ export declare function getLastError(): string | null;
4
+ export declare function setLastError(value: string | null): void;
5
+ export declare function getSocket(): any | null;
6
+ export declare function setSocket(value: any | null): void;
7
+ export declare function isConnected(): boolean;
8
+ export declare function setConnected(value: boolean): void;
@@ -0,0 +1,35 @@
1
+ let socket = null;
2
+ let connected = false;
3
+ let lastError = null;
4
+ const listeners = new Set();
5
+ function notify() {
6
+ for (const listener of listeners)
7
+ listener();
8
+ }
9
+ /** Notified when the socket connects, disconnects or reports an error. */
10
+ export function subscribeSocketState(listener) {
11
+ listeners.add(listener);
12
+ return () => {
13
+ listeners.delete(listener);
14
+ };
15
+ }
16
+ export function getLastError() {
17
+ return lastError;
18
+ }
19
+ export function setLastError(value) {
20
+ lastError = value;
21
+ notify();
22
+ }
23
+ export function getSocket() {
24
+ return socket;
25
+ }
26
+ export function setSocket(value) {
27
+ socket = value;
28
+ }
29
+ export function isConnected() {
30
+ return connected;
31
+ }
32
+ export function setConnected(value) {
33
+ connected = value;
34
+ notify();
35
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./patch-console";
2
+ export * from "./restore-console";
@@ -0,0 +1,2 @@
1
+ export * from "./patch-console.js";
2
+ export * from "./restore-console.js";
@@ -0,0 +1 @@
1
+ export declare function patchConsole(): void;
@@ -0,0 +1,64 @@
1
+ import { debugLog } from "../../utils/index.js";
2
+ import { sendConsoleEvent } from "./send-console-event.js";
3
+ import { getConsoleState } from "./state.js";
4
+ const METHODS = ["log", "info", "warn", "error", "debug"];
5
+ const UNHANDLED_REJECTION = /possible unhandled promise rejection|^uncaught \(in promise/i;
6
+ const UNCAUGHT_ECHO_MS = 1000;
7
+ const errorMessage = (error) => error instanceof Error ? error.message : typeof error === "string" ? error : "";
8
+ /** RN's default handler logs the uncaught error again via console.error right after ours. */
9
+ const isUncaughtEcho = (args) => {
10
+ const last = getConsoleState().lastUncaught;
11
+ if (!last || !last.message || Date.now() - last.ts > UNCAUGHT_ECHO_MS)
12
+ return false;
13
+ return args.some((arg) => (typeof arg === "string" && arg.includes(last.message)) ||
14
+ (arg instanceof Error && arg.message === last.message));
15
+ };
16
+ const isOwnMessage = (args) => typeof args[0] === "string" && /^\[heimdall\]/i.test(args[0]);
17
+ const getErrorUtils = () => {
18
+ const g = globalThis;
19
+ return g.ErrorUtils;
20
+ };
21
+ export function patchConsole() {
22
+ const state = getConsoleState();
23
+ if (state.patched || typeof console === "undefined")
24
+ return;
25
+ state.patched = true;
26
+ for (const method of METHODS) {
27
+ const original = console[method];
28
+ if (typeof original !== "function")
29
+ continue;
30
+ state.originals[method] = original;
31
+ console[method] = function heimdallConsole(...args) {
32
+ original.apply(console, args);
33
+ if (isOwnMessage(args))
34
+ return;
35
+ if (method === "error" && isUncaughtEcho(args))
36
+ return;
37
+ const rejection = (method === "warn" || method === "error") && UNHANDLED_REJECTION.test(errorMessage(args[0]));
38
+ sendConsoleEvent({
39
+ level: rejection ? "error" : method,
40
+ source: rejection ? "unhandled-rejection" : "console",
41
+ args,
42
+ });
43
+ };
44
+ }
45
+ const errorUtils = getErrorUtils();
46
+ if ((errorUtils === null || errorUtils === void 0 ? void 0 : errorUtils.getGlobalHandler) && errorUtils.setGlobalHandler) {
47
+ try {
48
+ const previous = errorUtils.getGlobalHandler();
49
+ const own = (error, isFatal) => {
50
+ state.lastUncaught = { message: errorMessage(error), ts: Date.now() };
51
+ sendConsoleEvent({ level: "error", source: "uncaught", args: [error], fatal: Boolean(isFatal) });
52
+ if (typeof previous === "function")
53
+ previous(error, isFatal);
54
+ };
55
+ state.previousErrorHandler = previous !== null && previous !== void 0 ? previous : null;
56
+ state.ownErrorHandler = own;
57
+ errorUtils.setGlobalHandler(own);
58
+ }
59
+ catch {
60
+ // ignore
61
+ }
62
+ }
63
+ debugLog("console", "console capture enabled");
64
+ }
@@ -0,0 +1 @@
1
+ export declare function restoreConsole(): void;
@@ -0,0 +1,28 @@
1
+ import { getConsoleState, resetConsoleState } from "./state.js";
2
+ export function restoreConsole() {
3
+ const state = getConsoleState();
4
+ if (!state.patched)
5
+ return;
6
+ for (const [method, original] of Object.entries(state.originals)) {
7
+ try {
8
+ console[method] = original;
9
+ }
10
+ catch {
11
+ // ignore
12
+ }
13
+ }
14
+ const g = globalThis;
15
+ const errorUtils = g.ErrorUtils;
16
+ try {
17
+ if ((errorUtils === null || errorUtils === void 0 ? void 0 : errorUtils.getGlobalHandler) &&
18
+ errorUtils.setGlobalHandler &&
19
+ state.previousErrorHandler &&
20
+ errorUtils.getGlobalHandler() === state.ownErrorHandler) {
21
+ errorUtils.setGlobalHandler(state.previousErrorHandler);
22
+ }
23
+ }
24
+ catch {
25
+ // ignore
26
+ }
27
+ resetConsoleState();
28
+ }
@@ -0,0 +1,8 @@
1
+ import type { ConsoleEventPayload } from "../../types";
2
+ /** Guarded against re-entry: anything logged while sending is not captured again. */
3
+ export declare function sendConsoleEvent(input: {
4
+ level: ConsoleEventPayload["level"];
5
+ source: ConsoleEventPayload["source"];
6
+ args: unknown[];
7
+ fatal?: boolean;
8
+ }): void;
@@ -0,0 +1,95 @@
1
+ import { generateRandomId, safeStringify } from "../../utils/index.js";
2
+ import { getCaptureEarlyEvents, getCurrentSession, sendEvent } from "../connection/index.js";
3
+ import { getConsoleState } from "./state.js";
4
+ const MAX_MESSAGE = 8 * 1024;
5
+ const MAX_ARGS = 16 * 1024;
6
+ const MAX_STACK = 8 * 1024;
7
+ const RATE_WINDOW_MS = 1000;
8
+ const RATE_LIMIT = 200;
9
+ const clip = (value, limit) => value.length > limit ? { text: value.slice(0, limit), truncated: true } : { text: value, truncated: false };
10
+ const formatArg = (arg) => {
11
+ if (typeof arg === "string")
12
+ return arg;
13
+ if (arg instanceof Error)
14
+ return `${arg.name}: ${arg.message}`;
15
+ return safeStringify(arg);
16
+ };
17
+ const firstStack = (args) => {
18
+ for (const arg of args) {
19
+ if (arg instanceof Error && arg.stack)
20
+ return String(arg.stack);
21
+ }
22
+ return undefined;
23
+ };
24
+ const allowByRate = () => {
25
+ const state = getConsoleState();
26
+ const now = Date.now();
27
+ if (now - state.windowStart >= RATE_WINDOW_MS) {
28
+ const dropped = state.dropped;
29
+ state.windowStart = now;
30
+ state.windowCount = 0;
31
+ state.dropped = 0;
32
+ if (dropped > 0) {
33
+ state.windowCount += 1;
34
+ emit({
35
+ level: "warn",
36
+ source: "console",
37
+ message: `Heimdall: ${dropped} console messages dropped (rate limit ${RATE_LIMIT}/s)`,
38
+ });
39
+ }
40
+ }
41
+ if (state.windowCount >= RATE_LIMIT) {
42
+ state.dropped += 1;
43
+ return false;
44
+ }
45
+ state.windowCount += 1;
46
+ return true;
47
+ };
48
+ const emit = (input) => {
49
+ const message = clip(input.message, MAX_MESSAGE);
50
+ const args = input.args !== undefined ? clip(input.args, MAX_ARGS) : undefined;
51
+ const stack = input.stack !== undefined ? clip(input.stack, MAX_STACK) : undefined;
52
+ const truncated = message.truncated || Boolean(args === null || args === void 0 ? void 0 : args.truncated) || Boolean(stack === null || stack === void 0 ? void 0 : stack.truncated);
53
+ const payload = {
54
+ type: "console",
55
+ id: generateRandomId(),
56
+ ts: Date.now(),
57
+ level: input.level,
58
+ source: input.source,
59
+ message: message.text,
60
+ ...(args ? { args: args.text } : {}),
61
+ ...(stack ? { stack: stack.text } : {}),
62
+ ...(input.fatal !== undefined ? { fatal: input.fatal } : {}),
63
+ ...(truncated ? { truncated: true } : {}),
64
+ };
65
+ sendEvent(payload);
66
+ };
67
+ /** Guarded against re-entry: anything logged while sending is not captured again. */
68
+ export function sendConsoleEvent(input) {
69
+ const state = getConsoleState();
70
+ if (state.sending)
71
+ return;
72
+ if (!getCurrentSession() && !getCaptureEarlyEvents())
73
+ return;
74
+ state.sending = true;
75
+ try {
76
+ if (!allowByRate())
77
+ return;
78
+ const message = input.args.map(formatArg).join(" ");
79
+ const hasStructured = input.args.some((arg) => arg !== null && typeof arg === "object");
80
+ emit({
81
+ level: input.level,
82
+ source: input.source,
83
+ message,
84
+ ...(hasStructured ? { args: safeStringify(input.args) } : {}),
85
+ stack: firstStack(input.args),
86
+ fatal: input.fatal,
87
+ });
88
+ }
89
+ catch {
90
+ // never break the host app
91
+ }
92
+ finally {
93
+ state.sending = false;
94
+ }
95
+ }
@@ -0,0 +1,19 @@
1
+ export type ConsoleMethod = "log" | "info" | "warn" | "error" | "debug";
2
+ export type ErrorHandler = (error: unknown, isFatal?: boolean) => void;
3
+ type ConsoleState = {
4
+ patched: boolean;
5
+ originals: Partial<Record<ConsoleMethod, (...args: unknown[]) => void>>;
6
+ previousErrorHandler: ErrorHandler | null;
7
+ ownErrorHandler: ErrorHandler | null;
8
+ sending: boolean;
9
+ windowStart: number;
10
+ windowCount: number;
11
+ dropped: number;
12
+ lastUncaught: {
13
+ message: string;
14
+ ts: number;
15
+ } | null;
16
+ };
17
+ export declare function getConsoleState(): ConsoleState;
18
+ export declare function resetConsoleState(): void;
19
+ export {};
@@ -0,0 +1,25 @@
1
+ const state = {
2
+ patched: false,
3
+ originals: {},
4
+ previousErrorHandler: null,
5
+ ownErrorHandler: null,
6
+ sending: false,
7
+ windowStart: 0,
8
+ windowCount: 0,
9
+ dropped: 0,
10
+ lastUncaught: null,
11
+ };
12
+ export function getConsoleState() {
13
+ return state;
14
+ }
15
+ export function resetConsoleState() {
16
+ state.patched = false;
17
+ state.originals = {};
18
+ state.previousErrorHandler = null;
19
+ state.ownErrorHandler = null;
20
+ state.sending = false;
21
+ state.windowStart = 0;
22
+ state.windowCount = 0;
23
+ state.dropped = 0;
24
+ state.lastUncaught = null;
25
+ }
File without changes
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ // auto-generated barrel
@@ -0,0 +1,11 @@
1
+ export type XHRInterceptorModule = {
2
+ isInterceptorEnabled: () => boolean;
3
+ setOpenCallback: (...props: any[]) => void;
4
+ setRequestHeaderCallback: (...props: any[]) => void;
5
+ setSendCallback: (...props: any[]) => void;
6
+ setHeaderReceivedCallback: (...props: any[]) => void;
7
+ setResponseCallback: (...props: any[]) => void;
8
+ enableInterception: () => void;
9
+ disableInterception: () => void;
10
+ };
11
+ export declare function loadTryGetXHRInterceptorModule(): XHRInterceptorModule | null;
@@ -0,0 +1,19 @@
1
+ export function loadTryGetXHRInterceptorModule() {
2
+ var _a, _b, _c;
3
+ try {
4
+ const module = require("react-native/src/private/devsupport/devmenu/elementinspector/XHRInterceptor");
5
+ return (_a = module.default) !== null && _a !== void 0 ? _a : module;
6
+ }
7
+ catch { }
8
+ try {
9
+ const module = require("react-native/src/private/inspector/XHRInterceptor");
10
+ return (_b = module.default) !== null && _b !== void 0 ? _b : module;
11
+ }
12
+ catch { }
13
+ try {
14
+ const module = require("react-native/Libraries/Network/XHRInterceptor");
15
+ return (_c = module.default) !== null && _c !== void 0 ? _c : module;
16
+ }
17
+ catch { }
18
+ return null;
19
+ }
@@ -0,0 +1,6 @@
1
+ import type { HeadersMap } from "./xhr-types";
2
+ export declare function normaliseHeaderName(name: any): string | null;
3
+ export declare function normaliseHeaders(input: any): HeadersMap;
4
+ /** Parses the raw "name: value\r\n" block returned by getAllResponseHeaders(). */
5
+ export declare function parseRawHeaders(raw: string): HeadersMap;
6
+ export declare function normaliseResponseHeaders(input: any): HeadersMap;
@@ -0,0 +1,46 @@
1
+ export function normaliseHeaderName(name) {
2
+ if (typeof name !== "string")
3
+ return null;
4
+ const n = name.trim().toLowerCase();
5
+ if (!n)
6
+ return null;
7
+ // Basic RFC-ish safety: no control chars, no colon
8
+ if (/[\u0000-\u001F\u007F]/.test(n))
9
+ return null;
10
+ if (n.includes(":"))
11
+ return null;
12
+ return n;
13
+ }
14
+ export function normaliseHeaders(input) {
15
+ const out = {};
16
+ if (!input || typeof input !== "object")
17
+ return out;
18
+ for (const k of Object.keys(input)) {
19
+ const key = normaliseHeaderName(k);
20
+ if (!key)
21
+ continue;
22
+ const v = input[k];
23
+ if (v == null)
24
+ continue;
25
+ out[key] = Array.isArray(v) ? v.join(", ") : String(v);
26
+ }
27
+ return out;
28
+ }
29
+ /** Parses the raw "name: value\r\n" block returned by getAllResponseHeaders(). */
30
+ export function parseRawHeaders(raw) {
31
+ const out = {};
32
+ for (const line of raw.split(/\r?\n/)) {
33
+ const index = line.indexOf(":");
34
+ if (index <= 0)
35
+ continue;
36
+ const key = normaliseHeaderName(line.slice(0, index));
37
+ if (!key)
38
+ continue;
39
+ const value = line.slice(index + 1).trim();
40
+ out[key] = key in out ? `${out[key]}, ${value}` : value;
41
+ }
42
+ return out;
43
+ }
44
+ export function normaliseResponseHeaders(input) {
45
+ return typeof input === "string" ? parseRawHeaders(input) : normaliseHeaders(input);
46
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./patch-xhr";
2
+ export * from "./restore-xhr";
@@ -0,0 +1,2 @@
1
+ export * from "./patch-xhr.js";
2
+ export * from "./restore-xhr.js";
@@ -0,0 +1,2 @@
1
+ import type { NetworkFilters } from "../../types";
2
+ export declare function patchXHR(options?: NetworkFilters): boolean | undefined;
@@ -0,0 +1,40 @@
1
+ import { getFilters, isPatched, setFilters, setPatched } from "./state.js";
2
+ import { loadTryGetXHRInterceptorModule } from "./get-interceptor.js";
3
+ import { createXhrCallbacks } from "./xhr-callbacks.js";
4
+ import { getOrCreateHeimdallXhrState } from "./xhr-runtime-state.js";
5
+ import { debugLog } from "../../utils/index.js";
6
+ export function patchXHR(options) {
7
+ debugLog("network", `patchXHR called, allowedHosts=${JSON.stringify(options === null || options === void 0 ? void 0 : options.allowedHosts)}, ignoreUrls=${JSON.stringify(options === null || options === void 0 ? void 0 : options.ignoreUrls)}`);
8
+ if (typeof XMLHttpRequest === "undefined")
9
+ return false;
10
+ setFilters({
11
+ allowedHosts: options === null || options === void 0 ? void 0 : options.allowedHosts,
12
+ ignoreUrls: options === null || options === void 0 ? void 0 : options.ignoreUrls,
13
+ });
14
+ if (isPatched())
15
+ return;
16
+ setPatched(true);
17
+ const XHRInterceptor = loadTryGetXHRInterceptorModule();
18
+ if (!XHRInterceptor) {
19
+ setPatched(false);
20
+ return false;
21
+ }
22
+ const state = getOrCreateHeimdallXhrState(XHRInterceptor);
23
+ if (state.enabled || state.interceptor.isInterceptorEnabled()) {
24
+ state.enabled = true;
25
+ return;
26
+ }
27
+ const { openCallback, requestHeadersCallback, headerReceivedCallback, sendCallback, responseCallback, } = createXhrCallbacks({
28
+ requests: state.requests,
29
+ getFilters,
30
+ });
31
+ state.interceptor.setOpenCallback(openCallback);
32
+ state.interceptor.setRequestHeaderCallback(requestHeadersCallback);
33
+ state.interceptor.setHeaderReceivedCallback(headerReceivedCallback);
34
+ state.interceptor.setSendCallback(sendCallback);
35
+ state.interceptor.setResponseCallback(responseCallback);
36
+ state.interceptor.enableInterception();
37
+ state.enabled = true;
38
+ debugLog("network", "XHR interceptor enabled successfully");
39
+ return true;
40
+ }
@@ -0,0 +1 @@
1
+ export declare function restoreXHR(): void;
@@ -0,0 +1,40 @@
1
+ import { setFilters, setPatched } from "./state.js";
2
+ import { loadTryGetXHRInterceptorModule } from "./get-interceptor.js";
3
+ export function restoreXHR() {
4
+ var _a;
5
+ const g = globalThis;
6
+ setPatched(false);
7
+ setFilters({});
8
+ const state = g.__heimdallXhrState;
9
+ const interceptor = (_a = state === null || state === void 0 ? void 0 : state.interceptor) !== null && _a !== void 0 ? _a : loadTryGetXHRInterceptorModule();
10
+ if (!interceptor) {
11
+ delete g.__heimdallXhrState;
12
+ return;
13
+ }
14
+ if (state === null || state === void 0 ? void 0 : state.requests) {
15
+ try {
16
+ state.requests.clear();
17
+ }
18
+ catch {
19
+ // ignore
20
+ }
21
+ }
22
+ const noop = () => null;
23
+ try {
24
+ interceptor.setOpenCallback(noop);
25
+ interceptor.setRequestHeaderCallback(noop);
26
+ interceptor.setHeaderReceivedCallback(noop);
27
+ interceptor.setSendCallback(noop);
28
+ interceptor.setResponseCallback(noop);
29
+ }
30
+ catch {
31
+ // ignore
32
+ }
33
+ try {
34
+ interceptor.disableInterception();
35
+ }
36
+ catch {
37
+ // ignore
38
+ }
39
+ delete g.__heimdallXhrState;
40
+ }
@@ -0,0 +1 @@
1
+ export declare function readRNBlobAsText(blob: any): Promise<string>;