lynx-console 0.3.0 → 0.4.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 CHANGED
@@ -1,16 +1,37 @@
1
+ [한국어](https://github.com/daangn/lynx-console/blob/main/package/README_ko.md) | English
2
+
1
3
  # lynx-console
2
4
 
3
5
  An in-app developer console that can be embedded in Lynx apps. View console logs, network requests, and performance metrics in real time.
4
6
 
7
+ ## Demo
8
+
9
+ https://github.com/user-attachments/assets/dcd874bf-ff2e-4a98-ae03-d83de5fae31c
10
+
11
+ <img width="450" height="450" alt="lynx_bundle_qrcode_fullscreen" src="https://github.com/user-attachments/assets/8bbb9bfe-df2b-436d-ad17-6e4eb4b672c9" />
12
+
13
+ Scan the QR code above with the [Lynx Explorer](https://lynxjs.org/guide/start/quick-start.html#via-lynx-explorer-app) app to try the demo.
14
+
5
15
  ## Features
6
16
 
7
- - **Console Logs** — View output from `console.log`, `console.error`, and more in real time
8
- - **Main Thread Console** — Capture logs from the main thread
9
- - **Network Monitor** — Inspect method, headers, body, and response of `fetch` requests
10
- - **Performance Monitor** — Track performance metrics such as FCP (First Contentful Paint)
11
- - **Floating Button** — Open and close the console with a floating button that displays the FCP value
17
+ - **Console Logs** — View output from `console.log`, `console.error`, and more in real time. Supports level filtering, keyword search, log clearing, and a built-in REPL
18
+ - **Main Thread Console** — Capture logs from the main thread alongside background thread logs
19
+
20
+ https://github.com/user-attachments/assets/539fe31a-aca4-468d-b673-3b070b21cd08
21
+
22
+ - **Network Monitor** — Inspect method, status, headers, request body, and response of `fetch` requests
23
+
24
+ https://github.com/user-attachments/assets/edda4778-ab8d-4cb9-a3c5-bd8c42c81bde
25
+
26
+ - **Performance Monitor** — Track FCP (First Contentful Paint) and other performance metrics with raw entry details
27
+
28
+ https://github.com/user-attachments/assets/d231bdf5-71bb-483f-9bdb-5843279c1308
29
+
30
+ - **Floating Button** — Displays the latest FCP value; tap to open the console, long-press and drag to reposition it
31
+ - **Resizable Panel** — Drag the handle to resize the console panel (200–700px); swipe down to dismiss
32
+ - **Tab Visibility** — Only tabs for initialized monitors are shown; uninitialized monitors are automatically hidden
33
+ - **Custom Tabs** — Add your own tabs to the console via the `customTabs` prop
12
34
  - **Light/Dark Theme** support
13
- - **Seed Design** based UI
14
35
 
15
36
  ## Installation
16
37
 
@@ -22,10 +43,29 @@ yarn add lynx-console
22
43
 
23
44
  ```bash
24
45
  yarn add @lynx-js/react @lynx-js/types
46
+ yarn add -D @types/react
25
47
  ```
26
48
 
49
+ > **Note:** Each monitor requires the corresponding Lynx API to be available at runtime. If `lynx.fetch` is not present, `initNetworkMonitor()` will be skipped with a warning. Likewise, `initPerformanceMonitor()` requires `lynx.performance`.
50
+
27
51
  ## Usage
28
52
 
53
+ ### 0. Configure Build (Required for iOS)
54
+
55
+ On iOS, the Lynx runtime (JSC) injects a separate `console` object that is different from `globalThis.console`. This means patches applied by `initLogMonitor()` won't take effect on iOS unless you explicitly replace the `console` identifier with `globalThis.console` at build time.
56
+
57
+ Add the following to your `lynx.config.ts`:
58
+
59
+ ```typescript
60
+ export default defineConfig({
61
+ source: {
62
+ define: {
63
+ console: "globalThis.console",
64
+ },
65
+ },
66
+ });
67
+ ```
68
+
29
69
  ### 1. Initialize Monitors
30
70
 
31
71
  Call the monitoring functions at your app's entry point. This setup must run **before** the `LynxConsole` component is rendered.
@@ -44,6 +84,8 @@ initNetworkMonitor();
44
84
  initPerformanceMonitor();
45
85
  ```
46
86
 
87
+ > **Note:** `initLogMonitor()` must be called before `initMainThreadConsole()`, as the main thread console depends on the log monitor being initialized first.
88
+
47
89
  ### 2. Render the Component
48
90
 
49
91
  ```tsx
@@ -68,7 +110,31 @@ function App() {
68
110
  {/* Your app content */}
69
111
  <Suspense>
70
112
  <LynxConsole theme="light" safeAreaInsetBottom="34px" />
71
- <Suspense>
113
+ </Suspense>
114
+ </view>
115
+ );
116
+ }
117
+ ```
118
+
119
+ ### Adding Custom Tabs
120
+
121
+ You can add your own tabs to the console using the `customTabs` prop.
122
+
123
+ ```tsx
124
+ import LynxConsole, { type CustomTab } from "lynx-console";
125
+
126
+ const customTabs: CustomTab[] = [
127
+ {
128
+ key: "debug",
129
+ label: "Debug",
130
+ renderContent: () => <text>Custom debug content</text>,
131
+ },
132
+ ];
133
+
134
+ function App() {
135
+ return (
136
+ <view>
137
+ <LynxConsole customTabs={customTabs} />
72
138
  </view>
73
139
  );
74
140
  }
@@ -99,34 +165,45 @@ function App() {
99
165
  <view>
100
166
  <Suspense>
101
167
  <LynxConsole ref={consoleRef} />
102
- <Suspense>
168
+ </Suspense>
103
169
  </view>
104
170
  );
105
171
  }
106
172
  ```
107
173
 
174
+ You can also integrate it with a back press handler so that the console closes when the back button is pressed.
175
+
108
176
  ## API
109
177
 
110
178
  ### `LynxConsole` Props
111
179
 
112
- | Prop | Type | Default | Description |
113
- |------|------|---------|-------------|
114
- | `theme` | `"light" \| "dark"` | `"light"` | Console UI theme |
115
- | `safeAreaInsetBottom` | `string` | `"50px"` | Bottom safe area inset |
180
+ | Prop | Type | Default | Description |
181
+ | --------------------- | ------------------- | ----------- | ------------------------------------------------ |
182
+ | `theme` | `"light" \| "dark"` | `"light"` | Console UI theme |
183
+ | `safeAreaInsetBottom` | `string` | `"50px"` | Bottom safe area inset |
184
+ | `customTabs` | `CustomTab[]` | `undefined` | Additional custom tabs to display in the console |
185
+
186
+ ### `CustomTab`
187
+
188
+ | Property | Type | Description |
189
+ | --------------- | ----------------- | ------------------------------------- |
190
+ | `key` | `string` | Unique identifier for the tab |
191
+ | `label` | `string` | Tab label text |
192
+ | `renderContent` | `() => ReactNode` | Function that renders the tab content |
116
193
 
117
194
  ### `LynxConsoleHandle`
118
195
 
119
- | Method | Description |
120
- |--------|-------------|
121
- | `open()` | Opens the console |
122
- | `close()` | Closes the console |
196
+ | Method | Description |
197
+ | ---------- | ----------------------------------- |
198
+ | `open()` | Opens the console |
199
+ | `close()` | Closes the console |
123
200
  | `isOpen()` | Returns whether the console is open |
124
201
 
125
202
  ### Monitor Initialization Functions
126
203
 
127
- | Function | Description |
128
- |----------|-------------|
129
- | `initLogMonitor()` | Captures `console.log`, `console.error`, etc. |
130
- | `initMainThreadConsole()` | Captures console output from the main thread |
131
- | `initNetworkMonitor()` | Intercepts and records `fetch` requests |
132
- | `initPerformanceMonitor()` | Collects performance metrics |
204
+ | Function | Description |
205
+ | -------------------------- | --------------------------------------------- |
206
+ | `initLogMonitor()` | Captures `console.log`, `console.error`, etc. |
207
+ | `initMainThreadConsole()` | Captures console output from the main thread |
208
+ | `initNetworkMonitor()` | Intercepts and records `fetch` requests |
209
+ | `initPerformanceMonitor()` | Collects performance metrics |
package/dist/index.cjs CHANGED
@@ -270,6 +270,102 @@ const usePerformance = () => {
270
270
  };
271
271
  };
272
272
 
273
+ //#endregion
274
+ //#region src/utils/parseCssStyle.ts
275
+ const DANGEROUS_VALUE = /url\s*\(|expression\s*\(|@import/i;
276
+ const toCamelCase = (name) => {
277
+ if (name.startsWith("--")) return name;
278
+ return name.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
279
+ };
280
+ const parseCssString = (css) => {
281
+ const result = {};
282
+ for (const raw of css.split(";")) {
283
+ const decl = raw.trim();
284
+ if (!decl) continue;
285
+ const colon = decl.indexOf(":");
286
+ if (colon <= 0) continue;
287
+ const name = decl.slice(0, colon).trim().toLowerCase();
288
+ const value = decl.slice(colon + 1).trim();
289
+ if (!name || !value) continue;
290
+ if (DANGEROUS_VALUE.test(value)) continue;
291
+ result[toCamelCase(name)] = value;
292
+ }
293
+ return result;
294
+ };
295
+
296
+ //#endregion
297
+ //#region src/utils/parseFormat.ts
298
+ const HAS_FORMAT = /%[csdifoO%]/;
299
+ const formatNumber = (v, int) => {
300
+ const n = typeof v === "number" ? int ? Math.trunc(v) : v : typeof v === "symbol" ? NaN : int ? parseInt(String(v), 10) : parseFloat(String(v));
301
+ return Number.isNaN(n) ? "NaN" : String(n);
302
+ };
303
+ const parseConsoleArgs = (args) => {
304
+ const first = args[0];
305
+ if (typeof first !== "string" || !HAS_FORMAT.test(first)) return {
306
+ segments: [],
307
+ rest: args
308
+ };
309
+ const segments = [];
310
+ let currentText = "";
311
+ let currentStyle;
312
+ let argIndex = 1;
313
+ let lastIndex = 0;
314
+ const flushText = () => {
315
+ if (currentText) {
316
+ segments.push({
317
+ type: "text",
318
+ text: currentText,
319
+ style: currentStyle
320
+ });
321
+ currentText = "";
322
+ }
323
+ };
324
+ const re = /%([csdifoO%])/g;
325
+ let match = re.exec(first);
326
+ while (match !== null) {
327
+ currentText += first.slice(lastIndex, match.index);
328
+ lastIndex = re.lastIndex;
329
+ const spec = match[1];
330
+ if (spec === "%") currentText += "%";
331
+ else if (argIndex >= args.length) currentText += match[0];
332
+ else {
333
+ const arg = args[argIndex++];
334
+ switch (spec) {
335
+ case "c":
336
+ flushText();
337
+ currentStyle = typeof arg === "string" ? parseCssString(arg) : void 0;
338
+ break;
339
+ case "s":
340
+ currentText += String(arg);
341
+ break;
342
+ case "d":
343
+ case "i":
344
+ currentText += formatNumber(arg, true);
345
+ break;
346
+ case "f":
347
+ currentText += formatNumber(arg, false);
348
+ break;
349
+ case "o":
350
+ case "O":
351
+ flushText();
352
+ segments.push({
353
+ type: "arg",
354
+ value: arg
355
+ });
356
+ break;
357
+ }
358
+ }
359
+ match = re.exec(first);
360
+ }
361
+ currentText += first.slice(lastIndex);
362
+ flushText();
363
+ return {
364
+ segments,
365
+ rest: args.slice(argIndex)
366
+ };
367
+ };
368
+
273
369
  //#endregion
274
370
  //#region src/components/LogPanel.tsx
275
371
  const LOG_LEVELS = [
@@ -400,7 +496,6 @@ const LogPanel = ({ logs, clearLogs }) => {
400
496
  params: { value: "" }
401
497
  }).exec();
402
498
  runCode(trimmed);
403
- setTimeout(() => scrollToBottom(false), 100);
404
499
  };
405
500
  const renderArg = (arg, parentKey, level) => {
406
501
  const key = parentKey;
@@ -601,9 +696,31 @@ const LogPanel = ({ logs, clearLogs }) => {
601
696
  </text>
602
697
  </view>
603
698
  <view className={"cp-logArgsContainer"}>
604
- {log.args.map((arg, index) => <view key={`${log.id}-${index.toString()}`} className={"cp-logArgItem"} style={{ fontWeight: fontWeight.regular }}>
605
- {renderArg(arg, `${log.id}-${index.toString()}`, log.level)}
606
- </view>)}
699
+ {(() => {
700
+ const { segments, rest } = parseConsoleArgs(log.args);
701
+ const baseTextStyle = {
702
+ color: getStringColor(colors, log.level),
703
+ fontWeight: fontWeight.regular
704
+ };
705
+ const wrap = (key, content) => <view key={key} className={"cp-logArgItem"} style={{ fontWeight: fontWeight.regular }}>
706
+ {content}
707
+ </view>;
708
+ return <>
709
+ {segments.map((seg, index) => {
710
+ const key = `${log.id}-seg-${index.toString()}`;
711
+ return wrap(key, seg.type === "text" ? <text className={"cp-argString t3"} style={{
712
+ ...baseTextStyle,
713
+ ...seg.style
714
+ }}>
715
+ {seg.text}
716
+ </text> : renderArg(seg.value, key, log.level));
717
+ })}
718
+ {rest.map((arg, index) => {
719
+ const key = `${log.id}-rest-${index.toString()}`;
720
+ return wrap(key, renderArg(arg, key, log.level));
721
+ })}
722
+ </>;
723
+ })()}
607
724
  </view>
608
725
  </view>
609
726
  </list-item>;
package/dist/index.mjs CHANGED
@@ -270,6 +270,102 @@ const usePerformance = () => {
270
270
  };
271
271
  };
272
272
 
273
+ //#endregion
274
+ //#region src/utils/parseCssStyle.ts
275
+ const DANGEROUS_VALUE = /url\s*\(|expression\s*\(|@import/i;
276
+ const toCamelCase = (name) => {
277
+ if (name.startsWith("--")) return name;
278
+ return name.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
279
+ };
280
+ const parseCssString = (css) => {
281
+ const result = {};
282
+ for (const raw of css.split(";")) {
283
+ const decl = raw.trim();
284
+ if (!decl) continue;
285
+ const colon = decl.indexOf(":");
286
+ if (colon <= 0) continue;
287
+ const name = decl.slice(0, colon).trim().toLowerCase();
288
+ const value = decl.slice(colon + 1).trim();
289
+ if (!name || !value) continue;
290
+ if (DANGEROUS_VALUE.test(value)) continue;
291
+ result[toCamelCase(name)] = value;
292
+ }
293
+ return result;
294
+ };
295
+
296
+ //#endregion
297
+ //#region src/utils/parseFormat.ts
298
+ const HAS_FORMAT = /%[csdifoO%]/;
299
+ const formatNumber = (v, int) => {
300
+ const n = typeof v === "number" ? int ? Math.trunc(v) : v : typeof v === "symbol" ? NaN : int ? parseInt(String(v), 10) : parseFloat(String(v));
301
+ return Number.isNaN(n) ? "NaN" : String(n);
302
+ };
303
+ const parseConsoleArgs = (args) => {
304
+ const first = args[0];
305
+ if (typeof first !== "string" || !HAS_FORMAT.test(first)) return {
306
+ segments: [],
307
+ rest: args
308
+ };
309
+ const segments = [];
310
+ let currentText = "";
311
+ let currentStyle;
312
+ let argIndex = 1;
313
+ let lastIndex = 0;
314
+ const flushText = () => {
315
+ if (currentText) {
316
+ segments.push({
317
+ type: "text",
318
+ text: currentText,
319
+ style: currentStyle
320
+ });
321
+ currentText = "";
322
+ }
323
+ };
324
+ const re = /%([csdifoO%])/g;
325
+ let match = re.exec(first);
326
+ while (match !== null) {
327
+ currentText += first.slice(lastIndex, match.index);
328
+ lastIndex = re.lastIndex;
329
+ const spec = match[1];
330
+ if (spec === "%") currentText += "%";
331
+ else if (argIndex >= args.length) currentText += match[0];
332
+ else {
333
+ const arg = args[argIndex++];
334
+ switch (spec) {
335
+ case "c":
336
+ flushText();
337
+ currentStyle = typeof arg === "string" ? parseCssString(arg) : void 0;
338
+ break;
339
+ case "s":
340
+ currentText += String(arg);
341
+ break;
342
+ case "d":
343
+ case "i":
344
+ currentText += formatNumber(arg, true);
345
+ break;
346
+ case "f":
347
+ currentText += formatNumber(arg, false);
348
+ break;
349
+ case "o":
350
+ case "O":
351
+ flushText();
352
+ segments.push({
353
+ type: "arg",
354
+ value: arg
355
+ });
356
+ break;
357
+ }
358
+ }
359
+ match = re.exec(first);
360
+ }
361
+ currentText += first.slice(lastIndex);
362
+ flushText();
363
+ return {
364
+ segments,
365
+ rest: args.slice(argIndex)
366
+ };
367
+ };
368
+
273
369
  //#endregion
274
370
  //#region src/components/LogPanel.tsx
275
371
  const LOG_LEVELS = [
@@ -400,7 +496,6 @@ const LogPanel = ({ logs, clearLogs }) => {
400
496
  params: { value: "" }
401
497
  }).exec();
402
498
  runCode(trimmed);
403
- setTimeout(() => scrollToBottom(false), 100);
404
499
  };
405
500
  const renderArg = (arg, parentKey, level) => {
406
501
  const key = parentKey;
@@ -601,9 +696,31 @@ const LogPanel = ({ logs, clearLogs }) => {
601
696
  </text>
602
697
  </view>
603
698
  <view className={"cp-logArgsContainer"}>
604
- {log.args.map((arg, index) => <view key={`${log.id}-${index.toString()}`} className={"cp-logArgItem"} style={{ fontWeight: fontWeight.regular }}>
605
- {renderArg(arg, `${log.id}-${index.toString()}`, log.level)}
606
- </view>)}
699
+ {(() => {
700
+ const { segments, rest } = parseConsoleArgs(log.args);
701
+ const baseTextStyle = {
702
+ color: getStringColor(colors, log.level),
703
+ fontWeight: fontWeight.regular
704
+ };
705
+ const wrap = (key, content) => <view key={key} className={"cp-logArgItem"} style={{ fontWeight: fontWeight.regular }}>
706
+ {content}
707
+ </view>;
708
+ return <>
709
+ {segments.map((seg, index) => {
710
+ const key = `${log.id}-seg-${index.toString()}`;
711
+ return wrap(key, seg.type === "text" ? <text className={"cp-argString t3"} style={{
712
+ ...baseTextStyle,
713
+ ...seg.style
714
+ }}>
715
+ {seg.text}
716
+ </text> : renderArg(seg.value, key, log.level));
717
+ })}
718
+ {rest.map((arg, index) => {
719
+ const key = `${log.id}-rest-${index.toString()}`;
720
+ return wrap(key, renderArg(arg, key, log.level));
721
+ })}
722
+ </>;
723
+ })()}
607
724
  </view>
608
725
  </view>
609
726
  </list-item>;