@rnzeus/heimdall-sdk 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -1
- package/dist/core/connection/channel/generate.d.ts +0 -4
- package/dist/core/connection/channel/generate.js +3 -7
- package/dist/core/connection/socket/disconnect-socket.js +3 -23
- package/dist/core/connection/socket/handle-socket-lost.js +1 -0
- package/dist/core/network/headers.d.ts +0 -3
- package/dist/core/network/headers.js +2 -2
- package/dist/core/network/should-skip.js +2 -15
- package/dist/core/network/xhr-callbacks.d.ts +1 -1
- package/dist/core/network/xhr-callbacks.js +3 -2
- package/dist/core/network/xhr-error.d.ts +7 -0
- package/dist/core/network/xhr-error.js +12 -0
- package/dist/core/network/xhr-types.d.ts +3 -0
- package/dist/core/system/dev-server.d.ts +0 -5
- package/dist/core/system/dev-server.js +1 -1
- package/dist/core/websocket/patch-websocket.js +24 -60
- package/dist/core/websocket/should-skip.js +2 -16
- package/dist/noop/index.d.ts +7 -0
- package/dist/noop/index.js +10 -0
- package/dist/noop/zustand.d.ts +2 -0
- package/dist/noop/zustand.js +1 -0
- package/dist/public/prompt/ui/header.d.ts +1 -1
- package/dist/utils/body-snippet.d.ts +0 -6
- package/dist/utils/body-snippet.js +1 -1
- package/dist/utils/debug-log.d.ts +1 -1
- package/dist/utils/generate-random-id.d.ts +1 -1
- package/dist/utils/generate-random-id.js +2 -2
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/safe-serialize.d.ts +0 -5
- package/dist/utils/safe-serialize.js +1 -1
- package/dist/utils/url-filters.d.ts +2 -0
- package/dist/utils/url-filters.js +17 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/metro/index.cjs +91 -0
- package/noop/index.d.ts +1 -0
- package/noop/index.js +1 -0
- package/noop/zustand.d.ts +1 -0
- package/noop/zustand.js +1 -0
- package/package.json +28 -4
package/README.md
CHANGED
|
@@ -83,7 +83,7 @@ Host to use:
|
|
|
83
83
|
| Android emulator | `10.0.2.2` |
|
|
84
84
|
| Real device | LAN IP of the machine running Heimdall |
|
|
85
85
|
|
|
86
|
-
On a real iOS device the first connection triggers the Local Network permission dialog
|
|
86
|
+
On a real iOS device the first connection triggers the Local Network permission dialog. `NSLocalNetworkUsageDescription` in `Info.plist` sets the text of that dialog; it is optional — without it iOS shows a generic message.
|
|
87
87
|
|
|
88
88
|
## Zustand
|
|
89
89
|
|
|
@@ -175,6 +175,49 @@ The cloud only stores accounts, devices and the current address of the desktop.
|
|
|
175
175
|
- When disabled there are no patches, no sockets and no subscriptions.
|
|
176
176
|
- Patches are transparent: original behaviour and return values are preserved, and SDK errors never reach your app.
|
|
177
177
|
- Payloads are capped: JSON bodies at 500 KB, text at 200 KB, everything else at 50 KB, console messages at 8 KB.
|
|
178
|
+
- `enabled: false` turns the SDK off, but its code and native modules stay in the build. To remove them, see below.
|
|
179
|
+
|
|
180
|
+
## Removing the SDK from production builds
|
|
181
|
+
|
|
182
|
+
This is optional: without it the SDK works as described above. With `enabled: false` the SDK is off, but its code and `react-native-tcp-socket` are still in the build. To leave them out of production builds:
|
|
183
|
+
|
|
184
|
+
**1. Replace the SDK with no-ops in `metro.config.js`:**
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
|
|
188
|
+
const { withHeimdall } = require("@rnzeus/heimdall-sdk/metro");
|
|
189
|
+
|
|
190
|
+
const config = mergeConfig(getDefaultConfig(__dirname), {
|
|
191
|
+
// your config
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
module.exports = withHeimdall(config, {
|
|
195
|
+
include: process.env.APP_ENV !== "production",
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
- `include: false` — `@rnzeus/heimdall-sdk` and `@rnzeus/heimdall-sdk/zustand` are replaced with modules that have the same API and do nothing: `connect` and the other calls are no-ops, `<HeimdallConnectPrompt />` renders nothing, `heimdallZustand` returns your store creator as is. Your code does not change.
|
|
200
|
+
- `include: true` — the config is returned unchanged.
|
|
201
|
+
- The `include` option has no default, so the choice is always explicit. Wrap your final config, after any other wrappers.
|
|
202
|
+
- Any other runtime import of the SDK, such as `@rnzeus/heimdall-sdk/dist/...`, fails the build with an error that names the file. Type-only imports (`import type`) are fine.
|
|
203
|
+
|
|
204
|
+
**2. Leave the native module out** in `react-native.config.js`:
|
|
205
|
+
|
|
206
|
+
```js
|
|
207
|
+
const production = process.env.APP_ENV === "production";
|
|
208
|
+
|
|
209
|
+
module.exports = {
|
|
210
|
+
dependencies: production
|
|
211
|
+
? { "react-native-tcp-socket": { platforms: { ios: null, android: null } } }
|
|
212
|
+
: {},
|
|
213
|
+
};
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Do this only together with step 1, otherwise the app crashes on the missing native module. The setting applies to the whole `pod install` run, so run `pod install` again without the variable before a dev or stage build. Leave `react-native-mmkv` alone if your app uses it for anything else.
|
|
217
|
+
|
|
218
|
+
**Which variable to use.** Both files read environment variables of the build process. Use one that is set for the whole build — usually a variable of your CI job, such as the build stage. Values from `.env` files are not visible here. `APP_ENV` above is only an example.
|
|
219
|
+
|
|
220
|
+
A production bundle built this way contains no `heimdall://` strings — only the no-op names, such as `heimdallZustand`. With Expo, step 1 works as is; for step 2 use your own config plugin.
|
|
178
221
|
|
|
179
222
|
## Your data
|
|
180
223
|
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { CHANNEL_SCHEME } from "./types.js";
|
|
2
2
|
import { sanitizeChannelSlug } from "./sanitize.js";
|
|
3
|
+
import { generateRandomId } from "../../../utils/index.js";
|
|
3
4
|
/**
|
|
4
5
|
* Generate a random short ID (default length = 6).
|
|
5
6
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
let out = "";
|
|
9
|
-
for (let i = 0; i < length; i++) {
|
|
10
|
-
out += chars[Math.floor(Math.random() * chars.length)];
|
|
11
|
-
}
|
|
12
|
-
return out;
|
|
7
|
+
function generateShortId(length = 6) {
|
|
8
|
+
return generateRandomId(length, "abcdefghijklmnopqrstuvwxyz0123456789");
|
|
13
9
|
}
|
|
14
10
|
/**
|
|
15
11
|
* Generate a full Channel identifier:
|
|
@@ -1,26 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { clearLastConnection, setCurrentSession } from "../session/index.js";
|
|
1
|
+
import { clearLastConnection } from "../session/index.js";
|
|
2
|
+
import { handleSocketLost } from "./handle-socket-lost.js";
|
|
4
3
|
export function disconnectSocket() {
|
|
5
|
-
|
|
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);
|
|
4
|
+
handleSocketLost();
|
|
23
5
|
clearLastConnection();
|
|
24
|
-
clearEarlyEvents();
|
|
25
|
-
disableCaptureEarlyEvents();
|
|
26
6
|
}
|
|
@@ -6,6 +6,7 @@ export function handleSocketLost() {
|
|
|
6
6
|
const socket = getSocket();
|
|
7
7
|
if (socket) {
|
|
8
8
|
try {
|
|
9
|
+
// first remove listeners, then gracefully close, then hard close
|
|
9
10
|
(_a = socket.removeAllListeners) === null || _a === void 0 ? void 0 : _a.call(socket);
|
|
10
11
|
(_b = socket.end) === null || _b === void 0 ? void 0 : _b.call(socket);
|
|
11
12
|
socket.destroy();
|
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import type { HeadersMap } from "./xhr-types";
|
|
2
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
3
|
export declare function normaliseResponseHeaders(input: any): HeadersMap;
|
|
@@ -11,7 +11,7 @@ export function normaliseHeaderName(name) {
|
|
|
11
11
|
return null;
|
|
12
12
|
return n;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
function normaliseHeaders(input) {
|
|
15
15
|
const out = {};
|
|
16
16
|
if (!input || typeof input !== "object")
|
|
17
17
|
return out;
|
|
@@ -27,7 +27,7 @@ export function normaliseHeaders(input) {
|
|
|
27
27
|
return out;
|
|
28
28
|
}
|
|
29
29
|
/** Parses the raw "name: value\r\n" block returned by getAllResponseHeaders(). */
|
|
30
|
-
|
|
30
|
+
function parseRawHeaders(raw) {
|
|
31
31
|
const out = {};
|
|
32
32
|
for (const line of raw.split(/\r?\n/)) {
|
|
33
33
|
const index = line.indexOf(":");
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { isCloudHost } from "../cloud/state.js";
|
|
2
2
|
import { isDevServerHost } from "../system/index.js";
|
|
3
|
-
|
|
4
|
-
if (!list || list.length === 0)
|
|
5
|
-
return false;
|
|
6
|
-
return list.some((rule) => typeof rule === "string" ? value === rule : rule.test(value));
|
|
7
|
-
}
|
|
3
|
+
import { isExcludedByFilters } from "../../utils/index.js";
|
|
8
4
|
export function shouldSkip(url, host, path, filters) {
|
|
9
5
|
if (isCloudHost(host))
|
|
10
6
|
return true;
|
|
@@ -17,14 +13,5 @@ export function shouldSkip(url, host, path, filters) {
|
|
|
17
13
|
return true;
|
|
18
14
|
}
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
matchList(path, filters.ignoreUrls)) {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
if (filters.allowedHosts && filters.allowedHosts.length > 0) {
|
|
25
|
-
if (!matchList(host, filters.allowedHosts)) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
16
|
+
return isExcludedByFilters(url, host, path, filters);
|
|
30
17
|
}
|
|
@@ -8,5 +8,5 @@ export declare function createXhrCallbacks(deps: {
|
|
|
8
8
|
requestHeadersCallback: (header: string, value: string, xhr: XHR) => void;
|
|
9
9
|
headerReceivedCallback: (responseContentType: string, responseSize: number, responseHeaders: HeadersMap, xhr: XHR) => void;
|
|
10
10
|
sendCallback: (data: any, xhr: XHR) => void;
|
|
11
|
-
responseCallback: (status: number,
|
|
11
|
+
responseCallback: (status: number, _timeout: number, response: any, responseURL: string, responseType: string, xhr: XHR) => void;
|
|
12
12
|
};
|
|
@@ -5,6 +5,7 @@ import { normaliseHeaderName, normaliseResponseHeaders, } from "./headers.js";
|
|
|
5
5
|
import { buildBodySnippet, debugLog } from "../../utils/index.js";
|
|
6
6
|
import { nextXhrIndex } from "./xhr-runtime-state.js";
|
|
7
7
|
import { readRNBlobAsText } from "./rn-blob.js";
|
|
8
|
+
import { xhrError } from "./xhr-error.js";
|
|
8
9
|
export function createXhrCallbacks(deps) {
|
|
9
10
|
const { requests, getFilters } = deps;
|
|
10
11
|
const openCallback = (method, url, xhr) => {
|
|
@@ -64,7 +65,7 @@ export function createXhrCallbacks(deps) {
|
|
|
64
65
|
meta.__requestBlob = undefined;
|
|
65
66
|
}
|
|
66
67
|
};
|
|
67
|
-
const responseCallback = (status,
|
|
68
|
+
const responseCallback = (status, _timeout, response, responseURL, responseType, xhr) => {
|
|
68
69
|
const meta = requests.get(xhr._index);
|
|
69
70
|
if (!meta)
|
|
70
71
|
return;
|
|
@@ -105,7 +106,7 @@ export function createXhrCallbacks(deps) {
|
|
|
105
106
|
status: typeof status === "number" ? status : undefined,
|
|
106
107
|
ok,
|
|
107
108
|
durationMs: tsEnd - (meta.tsStart || tsEnd),
|
|
108
|
-
error:
|
|
109
|
+
error: xhrError(xhr, ok),
|
|
109
110
|
requestHeaders: meta.requestHeaders,
|
|
110
111
|
responseHeaders,
|
|
111
112
|
requestContentType: meta.requestHeaders["content-type"],
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { XHR } from "./xhr-types";
|
|
2
|
+
/**
|
|
3
|
+
* Status 0 alone cannot tell an abort from a network failure, so read the
|
|
4
|
+
* XHR flags. The interceptor's `timeout` argument is the configured timeout,
|
|
5
|
+
* not a sign that it fired.
|
|
6
|
+
*/
|
|
7
|
+
export declare function xhrError(xhr: XHR, ok: boolean): string | undefined;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status 0 alone cannot tell an abort from a network failure, so read the
|
|
3
|
+
* XHR flags. The interceptor's `timeout` argument is the configured timeout,
|
|
4
|
+
* not a sign that it fired.
|
|
5
|
+
*/
|
|
6
|
+
export function xhrError(xhr, ok) {
|
|
7
|
+
if (xhr._aborted)
|
|
8
|
+
return "xhr_aborted";
|
|
9
|
+
if (xhr._timedOut)
|
|
10
|
+
return "xhr_timeout";
|
|
11
|
+
return ok ? undefined : "xhr_error";
|
|
12
|
+
}
|
|
@@ -3,6 +3,9 @@ export type HeadersMap = Record<string, string>;
|
|
|
3
3
|
export type XHR = {
|
|
4
4
|
_index: number;
|
|
5
5
|
responseHeaders?: HeadersMap;
|
|
6
|
+
/** React Native XMLHttpRequest internals; still set when the interceptor reports DONE. */
|
|
7
|
+
_aborted?: boolean;
|
|
8
|
+
_timedOut?: boolean;
|
|
6
9
|
};
|
|
7
10
|
export type Meta = {
|
|
8
11
|
tsStart: number;
|
|
@@ -1,6 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* host:port of the Metro server the bundle was loaded from (e.g. "127.0.0.1:8081"),
|
|
3
|
-
* or null for an embedded release bundle. Traffic to it is RN tooling, never app traffic.
|
|
4
|
-
*/
|
|
5
|
-
export declare function getDevServerHost(): string | null;
|
|
6
1
|
export declare function isDevServerHost(host: string): boolean;
|
|
@@ -26,7 +26,7 @@ const readScriptUrl = () => {
|
|
|
26
26
|
* host:port of the Metro server the bundle was loaded from (e.g. "127.0.0.1:8081"),
|
|
27
27
|
* or null for an embedded release bundle. Traffic to it is RN tooling, never app traffic.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
function getDevServerHost() {
|
|
30
30
|
if (cachedHost === undefined)
|
|
31
31
|
cachedHost = hostOf(readScriptUrl());
|
|
32
32
|
return cachedHost;
|
|
@@ -70,8 +70,24 @@ function buildWsFrame(meta, direction, data) {
|
|
|
70
70
|
dataContentType: undefined,
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
|
-
function sendWsEvent(
|
|
74
|
-
sendEvent(
|
|
73
|
+
function sendWsEvent(meta, fields) {
|
|
74
|
+
sendEvent({
|
|
75
|
+
id: generateRandomId(),
|
|
76
|
+
type: "ws",
|
|
77
|
+
transport: "websocket",
|
|
78
|
+
connectionId: meta.connectionId,
|
|
79
|
+
ts: now(),
|
|
80
|
+
url: meta.url,
|
|
81
|
+
...fields,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function sendWsFrame(meta, direction, data) {
|
|
85
|
+
var _a, _b, _c;
|
|
86
|
+
const frame = buildWsFrame(meta, direction, data);
|
|
87
|
+
const raw = typeof data === "string" ? previewString(data) : typeof data;
|
|
88
|
+
debugLog("ws", `frame(${direction}) connectionId=${meta.connectionId} bytes=${String((_b = (_a = frame.dataSize) !== null && _a !== void 0 ? _a : frame.dataSnippetBytes) !== null && _b !== void 0 ? _b : "?")} truncated=${String(frame.dataTruncated)} ct=${String(frame.dataContentType)} data=${String(raw)}`);
|
|
89
|
+
debugLog("ws", `sendEvent: frame(${direction}) connectionId=${meta.connectionId} bytes=${String((_c = frame.dataSize) !== null && _c !== void 0 ? _c : frame.dataSnippetBytes)}`);
|
|
90
|
+
sendWsEvent(meta, { kind: "frame", ...frame });
|
|
75
91
|
}
|
|
76
92
|
export function patchWebSocket(options) {
|
|
77
93
|
var _a;
|
|
@@ -136,27 +152,13 @@ export function patchWebSocket(options) {
|
|
|
136
152
|
attach("open", () => {
|
|
137
153
|
debugLog("ws", `open connectionId=${meta.connectionId}`);
|
|
138
154
|
debugLog("ws", `sendEvent: open connectionId=${meta.connectionId} url=${meta.url}`);
|
|
139
|
-
sendWsEvent({
|
|
140
|
-
id: generateRandomId(),
|
|
141
|
-
type: "ws",
|
|
142
|
-
transport: "websocket",
|
|
143
|
-
connectionId: meta.connectionId,
|
|
144
|
-
ts: now(),
|
|
145
|
-
url: meta.url,
|
|
146
|
-
kind: "open",
|
|
147
|
-
});
|
|
155
|
+
sendWsEvent(meta, { kind: "open" });
|
|
148
156
|
});
|
|
149
157
|
// close
|
|
150
158
|
attach("close", (ev) => {
|
|
151
159
|
debugLog("ws", `close connectionId=${meta.connectionId} code=${String(ev === null || ev === void 0 ? void 0 : ev.code)} reason=${String(ev === null || ev === void 0 ? void 0 : ev.reason)}`);
|
|
152
160
|
debugLog("ws", `sendEvent: close connectionId=${meta.connectionId} code=${String(ev === null || ev === void 0 ? void 0 : ev.code)}`);
|
|
153
|
-
sendWsEvent({
|
|
154
|
-
id: generateRandomId(),
|
|
155
|
-
type: "ws",
|
|
156
|
-
transport: "websocket",
|
|
157
|
-
connectionId: meta.connectionId,
|
|
158
|
-
ts: now(),
|
|
159
|
-
url: meta.url,
|
|
161
|
+
sendWsEvent(meta, {
|
|
160
162
|
kind: "close",
|
|
161
163
|
code: typeof (ev === null || ev === void 0 ? void 0 : ev.code) === "number" ? ev.code : undefined,
|
|
162
164
|
reason: typeof (ev === null || ev === void 0 ? void 0 : ev.reason) === "string" ? ev.reason : undefined,
|
|
@@ -171,57 +173,19 @@ export function patchWebSocket(options) {
|
|
|
171
173
|
"websocket_error";
|
|
172
174
|
debugLog("ws", `error connectionId=${meta.connectionId} msg=${msg}`);
|
|
173
175
|
debugLog("ws", `sendEvent: error connectionId=${meta.connectionId} msg=${msg}`);
|
|
174
|
-
sendWsEvent({
|
|
175
|
-
id: generateRandomId(),
|
|
176
|
-
type: "ws",
|
|
177
|
-
transport: "websocket",
|
|
178
|
-
connectionId: meta.connectionId,
|
|
179
|
-
ts: now(),
|
|
180
|
-
url: meta.url,
|
|
181
|
-
kind: "error",
|
|
182
|
-
error: msg,
|
|
183
|
-
});
|
|
176
|
+
sendWsEvent(meta, { kind: "error", error: msg });
|
|
184
177
|
});
|
|
185
178
|
// incoming frame
|
|
186
179
|
attach("message", (ev) => {
|
|
187
|
-
|
|
188
|
-
const frame = buildWsFrame(meta, "in", ev === null || ev === void 0 ? void 0 : ev.data);
|
|
189
|
-
const raw = typeof (ev === null || ev === void 0 ? void 0 : ev.data) === "string" ? previewString(ev.data) : typeof (ev === null || ev === void 0 ? void 0 : ev.data);
|
|
190
|
-
debugLog("ws", `frame(in) connectionId=${meta.connectionId} bytes=${String((_b = (_a = frame.dataSize) !== null && _a !== void 0 ? _a : frame.dataSnippetBytes) !== null && _b !== void 0 ? _b : "?")} truncated=${String(frame.dataTruncated)} ct=${String(frame.dataContentType)} data=${String(raw)}`);
|
|
191
|
-
debugLog("ws", `sendEvent: frame(in) connectionId=${meta.connectionId} bytes=${String((_c = frame.dataSize) !== null && _c !== void 0 ? _c : frame.dataSnippetBytes)}`);
|
|
192
|
-
sendWsEvent({
|
|
193
|
-
id: generateRandomId(),
|
|
194
|
-
type: "ws",
|
|
195
|
-
transport: "websocket",
|
|
196
|
-
connectionId: meta.connectionId,
|
|
197
|
-
ts: now(),
|
|
198
|
-
url: meta.url,
|
|
199
|
-
kind: "frame",
|
|
200
|
-
...frame,
|
|
201
|
-
});
|
|
180
|
+
sendWsFrame(meta, "in", ev === null || ev === void 0 ? void 0 : ev.data);
|
|
202
181
|
});
|
|
203
182
|
// outgoing frame
|
|
204
183
|
const originalSend = ws.send;
|
|
205
184
|
if (typeof originalSend === "function") {
|
|
206
185
|
ws.send = function patchedSend(data) {
|
|
207
|
-
var _a, _b, _c;
|
|
208
186
|
const m = wsMeta.get(ws);
|
|
209
|
-
if (m)
|
|
210
|
-
|
|
211
|
-
const raw = typeof data === "string" ? previewString(data) : typeof data;
|
|
212
|
-
debugLog("ws", `frame(out) connectionId=${m.connectionId} bytes=${String((_b = (_a = frame.dataSize) !== null && _a !== void 0 ? _a : frame.dataSnippetBytes) !== null && _b !== void 0 ? _b : "?")} truncated=${String(frame.dataTruncated)} ct=${String(frame.dataContentType)} data=${String(raw)}`);
|
|
213
|
-
debugLog("ws", `sendEvent: frame(out) connectionId=${m.connectionId} bytes=${String((_c = frame.dataSize) !== null && _c !== void 0 ? _c : frame.dataSnippetBytes)}`);
|
|
214
|
-
sendWsEvent({
|
|
215
|
-
id: generateRandomId(),
|
|
216
|
-
type: "ws",
|
|
217
|
-
transport: "websocket",
|
|
218
|
-
connectionId: m.connectionId,
|
|
219
|
-
ts: now(),
|
|
220
|
-
url: m.url,
|
|
221
|
-
kind: "frame",
|
|
222
|
-
...frame,
|
|
223
|
-
});
|
|
224
|
-
}
|
|
187
|
+
if (m)
|
|
188
|
+
sendWsFrame(m, "out", data);
|
|
225
189
|
return originalSend.call(ws, data);
|
|
226
190
|
};
|
|
227
191
|
}
|
|
@@ -1,22 +1,8 @@
|
|
|
1
1
|
import { isDevServerHost } from "../system/index.js";
|
|
2
|
-
|
|
3
|
-
if (!list || list.length === 0)
|
|
4
|
-
return false;
|
|
5
|
-
return list.some((rule) => typeof rule === "string" ? value === rule : rule.test(value));
|
|
6
|
-
}
|
|
2
|
+
import { isExcludedByFilters } from "../../utils/index.js";
|
|
7
3
|
export function shouldSkipWs(url, host, path, filters) {
|
|
8
4
|
// Metro HMR / debugger sockets
|
|
9
5
|
if (isDevServerHost(host))
|
|
10
6
|
return true;
|
|
11
|
-
|
|
12
|
-
if (matchList(url, filters.ignoreUrls) || matchList(path, filters.ignoreUrls)) {
|
|
13
|
-
return true;
|
|
14
|
-
}
|
|
15
|
-
// allowedHosts
|
|
16
|
-
if (filters.allowedHosts && filters.allowedHosts.length > 0) {
|
|
17
|
-
if (!matchList(host, filters.allowedHosts)) {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return false;
|
|
7
|
+
return isExcludedByFilters(url, host, path, filters);
|
|
22
8
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type * as Sdk from "../index";
|
|
2
|
+
export type { HeimdallConnectOptions } from "../types/public";
|
|
3
|
+
export type { Channel } from "../core/public";
|
|
4
|
+
export declare const connect: typeof Sdk.connect, disconnect: typeof Sdk.disconnect, configureConnectPrompt: typeof Sdk.configureConnectPrompt, showSDKConnectPrompt: typeof Sdk.showSDKConnectPrompt, HeimdallConnectPrompt: {
|
|
5
|
+
({ onCopyToClipboard, }: import("../public/prompt/types").Props): import("react").JSX.Element | null;
|
|
6
|
+
displayName: string;
|
|
7
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const noop = {
|
|
2
|
+
connect: () => { },
|
|
3
|
+
disconnect: () => { },
|
|
4
|
+
configureConnectPrompt: () => { },
|
|
5
|
+
showSDKConnectPrompt: () => { },
|
|
6
|
+
HeimdallConnectPrompt: Object.assign(() => null, {
|
|
7
|
+
displayName: "HeimdallConnectPrompt",
|
|
8
|
+
}),
|
|
9
|
+
};
|
|
10
|
+
export const { connect, disconnect, configureConnectPrompt, showSDKConnectPrompt, HeimdallConnectPrompt, } = noop;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const heimdallZustand = (_storeName, createState) => createState;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { JSX } from "react";
|
|
2
2
|
import type { PromptTheme } from "../theme";
|
|
3
|
-
|
|
3
|
+
type HeaderStatus = "idle" | "busy" | "connected" | "error";
|
|
4
4
|
type Props = {
|
|
5
5
|
title: string;
|
|
6
6
|
status: HeaderStatus;
|
|
@@ -1,9 +1,3 @@
|
|
|
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
1
|
export declare function tryMinifyJsonString(s: string): string | null;
|
|
8
2
|
export type BodySnippetResult = {
|
|
9
3
|
snippet?: string;
|
|
@@ -4,7 +4,7 @@ const MAX_BODY_BYTES_JSON = 500 * 1024; // 500 KB
|
|
|
4
4
|
const MAX_BODY_BYTES_TEXT = 200 * 1024; // 200 KB
|
|
5
5
|
// Safety gate: avoid JSON.parse+stringify for very large strings.
|
|
6
6
|
const MAX_JSON_MINIFY_BYTES = 1000000; // 1 MB
|
|
7
|
-
|
|
7
|
+
const NETWORK = {
|
|
8
8
|
MAX_BODY_BYTES_DEFAULT,
|
|
9
9
|
MAX_BODY_BYTES_JSON,
|
|
10
10
|
MAX_BODY_BYTES_TEXT,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function generateRandomId(length?: number): string;
|
|
1
|
+
export declare function generateRandomId(length?: number, chars?: string): string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const ALPHANUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
2
|
+
export function generateRandomId(length = 16, chars = ALPHANUMERIC) {
|
|
3
3
|
let out = "";
|
|
4
4
|
for (let i = 0; i < length; i++) {
|
|
5
5
|
out += chars[Math.floor(Math.random() * chars.length)];
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -1,6 +1 @@
|
|
|
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
1
|
export declare function safeStringify(value: unknown): string;
|
|
@@ -6,7 +6,7 @@ const clip = (value) => value.length > MAX_STRING ? `${value.slice(0, MAX_STRING
|
|
|
6
6
|
* Converts any value into a JSON-safe snapshot with depth, width and string limits.
|
|
7
7
|
* Never throws: getters that throw and host objects become placeholders.
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
function toSafeValue(value, depth = 0, seen = new WeakSet()) {
|
|
10
10
|
if (value === null || value === undefined)
|
|
11
11
|
return value !== null && value !== void 0 ? value : null;
|
|
12
12
|
const kind = typeof value;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function matchList(value, list) {
|
|
2
|
+
if (!list || list.length === 0)
|
|
3
|
+
return false;
|
|
4
|
+
return list.some((rule) => typeof rule === "string" ? value === rule : rule.test(value));
|
|
5
|
+
}
|
|
6
|
+
export function isExcludedByFilters(url, host, path, filters) {
|
|
7
|
+
if (matchList(url, filters.ignoreUrls) ||
|
|
8
|
+
matchList(path, filters.ignoreUrls)) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
if (filters.allowedHosts && filters.allowedHosts.length > 0) {
|
|
12
|
+
if (!matchList(host, filters.allowedHosts)) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "0.
|
|
1
|
+
export declare const SDK_VERSION = "0.3.0";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "0.
|
|
1
|
+
export const SDK_VERSION = "0.3.0";
|
package/metro/index.cjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const path = require("node:path");
|
|
2
|
+
|
|
3
|
+
const PACKAGE = "@rnzeus/heimdall-sdk";
|
|
4
|
+
|
|
5
|
+
const NOOP_FILES = {
|
|
6
|
+
[PACKAGE]: path.resolve(__dirname, "../dist/noop/index.js"),
|
|
7
|
+
[`${PACKAGE}/zustand`]: path.resolve(__dirname, "../dist/noop/zustand.js"),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const NOOP_ENTRIES = new Set([`${PACKAGE}/noop`, `${PACKAGE}/noop/zustand`]);
|
|
11
|
+
|
|
12
|
+
const isSdkImport = (moduleName) =>
|
|
13
|
+
moduleName === PACKAGE || moduleName.startsWith(`${PACKAGE}/`);
|
|
14
|
+
|
|
15
|
+
const unsupportedImportError = ({ moduleName, originModulePath }) => {
|
|
16
|
+
const origin = originModulePath
|
|
17
|
+
? path.relative(process.cwd(), originModulePath)
|
|
18
|
+
: "<unknown file>";
|
|
19
|
+
|
|
20
|
+
return new Error(
|
|
21
|
+
[
|
|
22
|
+
`[heimdall] Unsupported import of the Heimdall SDK in a build that excludes it.`,
|
|
23
|
+
``,
|
|
24
|
+
` File: ${origin}`,
|
|
25
|
+
` Import: "${moduleName}"`,
|
|
26
|
+
``,
|
|
27
|
+
`withHeimdall(config, { include: false }) in metro.config.js replaces the SDK with no-ops,`,
|
|
28
|
+
`so none of its code, its native modules or its dependencies end up in this bundle.`,
|
|
29
|
+
`Only these imports can be replaced:`,
|
|
30
|
+
``,
|
|
31
|
+
` "${PACKAGE}" -> no-op connect / disconnect / prompt`,
|
|
32
|
+
` "${PACKAGE}/zustand" -> heimdallZustand passes the store creator through`,
|
|
33
|
+
``,
|
|
34
|
+
`"${moduleName}" is not one of them. Letting it through would put the real SDK into this bundle,`,
|
|
35
|
+
`and if react-native-tcp-socket is unlinked for this build, the app would crash at runtime.`,
|
|
36
|
+
``,
|
|
37
|
+
`How to fix:`,
|
|
38
|
+
` - import from "${PACKAGE}" or "${PACKAGE}/zustand" instead;`,
|
|
39
|
+
` - type-only imports are fine anywhere: use "import type { ... }";`,
|
|
40
|
+
` - if this build should include Heimdall, pass { include: true } to withHeimdall.`,
|
|
41
|
+
].join("\n")
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Wraps a Metro config. With `include: false` imports of the SDK resolve to no-op modules,
|
|
47
|
+
* so the SDK is not bundled. With `include: true` the config is returned unchanged.
|
|
48
|
+
*
|
|
49
|
+
* @template T
|
|
50
|
+
* @param {T} config
|
|
51
|
+
* @param {{ include: boolean }} options
|
|
52
|
+
* @returns {T}
|
|
53
|
+
*/
|
|
54
|
+
function withHeimdall(config, options) {
|
|
55
|
+
if (typeof options?.include !== "boolean") {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`[heimdall] withHeimdall(config, { include }) needs "include" set to true or false explicitly, ` +
|
|
58
|
+
`e.g. { include: process.env.APP_ENV !== "production" }.`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (options.include) return config;
|
|
63
|
+
|
|
64
|
+
const resolver = config.resolver ?? {};
|
|
65
|
+
const resolveNext = resolver.resolveRequest;
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
...config,
|
|
69
|
+
resolver: {
|
|
70
|
+
...resolver,
|
|
71
|
+
resolveRequest: (context, moduleName, platform) => {
|
|
72
|
+
if (isSdkImport(moduleName) && !NOOP_ENTRIES.has(moduleName)) {
|
|
73
|
+
const filePath = NOOP_FILES[moduleName];
|
|
74
|
+
if (!filePath) {
|
|
75
|
+
throw unsupportedImportError({
|
|
76
|
+
moduleName,
|
|
77
|
+
originModulePath: context.originModulePath,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return { type: "sourceFile", filePath };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return resolveNext
|
|
84
|
+
? resolveNext(context, moduleName, platform)
|
|
85
|
+
: context.resolveRequest(context, moduleName, platform);
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
module.exports = { withHeimdall };
|
package/noop/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../dist/noop";
|
package/noop/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../dist/noop/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../dist/noop/zustand";
|
package/noop/zustand.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../dist/noop/zustand.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rnzeus/heimdall-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "React Native client for Heimdall — a desktop debug inspector for network, WebSocket, Zustand and console",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Volodymyr Dudkevych",
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
"sideEffects": false,
|
|
30
30
|
"files": [
|
|
31
31
|
"dist",
|
|
32
|
-
"zustand"
|
|
32
|
+
"zustand",
|
|
33
|
+
"noop",
|
|
34
|
+
"metro"
|
|
33
35
|
],
|
|
34
36
|
"main": "dist/index.js",
|
|
35
37
|
"types": "dist/index.d.ts",
|
|
@@ -48,13 +50,33 @@
|
|
|
48
50
|
"import": "./zustand/index.js",
|
|
49
51
|
"require": "./zustand/index.js",
|
|
50
52
|
"default": "./zustand/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./noop": {
|
|
55
|
+
"types": "./noop/index.d.ts",
|
|
56
|
+
"react-native": "./noop/index.js",
|
|
57
|
+
"import": "./noop/index.js",
|
|
58
|
+
"require": "./noop/index.js",
|
|
59
|
+
"default": "./noop/index.js"
|
|
60
|
+
},
|
|
61
|
+
"./noop/zustand": {
|
|
62
|
+
"types": "./noop/zustand.d.ts",
|
|
63
|
+
"react-native": "./noop/zustand.js",
|
|
64
|
+
"import": "./noop/zustand.js",
|
|
65
|
+
"require": "./noop/zustand.js",
|
|
66
|
+
"default": "./noop/zustand.js"
|
|
67
|
+
},
|
|
68
|
+
"./metro": {
|
|
69
|
+
"require": "./metro/index.cjs",
|
|
70
|
+
"default": "./metro/index.cjs"
|
|
51
71
|
}
|
|
52
72
|
},
|
|
53
73
|
"scripts": {
|
|
54
74
|
"clean": "rimraf dist",
|
|
55
|
-
"build": "node scripts/write-version.mjs && yarn clean && tsc -p tsconfig.build.json && node scripts/fix-esm-extensions.mjs dist",
|
|
75
|
+
"build": "node scripts/write-version.mjs && yarn clean && tsc -p tsconfig.build.json && node scripts/fix-esm-extensions.mjs dist && node scripts/check-noop.mjs",
|
|
56
76
|
"dev": "tsc -p tsconfig.build.json -w",
|
|
57
|
-
"typecheck": "node scripts/write-version.mjs && tsc -p tsconfig.json --noEmit"
|
|
77
|
+
"typecheck": "node scripts/write-version.mjs && tsc -p tsconfig.json --noEmit",
|
|
78
|
+
"knip": "knip",
|
|
79
|
+
"dupes": "jscpd"
|
|
58
80
|
},
|
|
59
81
|
"peerDependencies": {
|
|
60
82
|
"react": ">=18.0.0",
|
|
@@ -74,6 +96,8 @@
|
|
|
74
96
|
"devDependencies": {
|
|
75
97
|
"@types/node": "^25.0.3",
|
|
76
98
|
"@types/react": "^18.3.12",
|
|
99
|
+
"jscpd": "5.4.1",
|
|
100
|
+
"knip": "6.41.0",
|
|
77
101
|
"react": "^18.3.1",
|
|
78
102
|
"react-native": "^0.79.0",
|
|
79
103
|
"react-native-mmkv": "^3.3.0",
|