trickle-observe 0.2.61 → 0.2.62

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.
@@ -0,0 +1,28 @@
1
+ /**
2
+ * trickle/metro-transformer — Metro bundler transform for React Native observability.
3
+ *
4
+ * Drop-in Metro transformer that instruments React Native components with
5
+ * trickle's render tracking, useState change tracking, and hook observability —
6
+ * the same tracking trickle's Vite plugin provides for web React apps.
7
+ *
8
+ * Setup in metro.config.js:
9
+ *
10
+ * const { getDefaultConfig } = require('expo/metro-config');
11
+ * // or: const { getDefaultConfig } = require('@react-native/metro-config');
12
+ *
13
+ * const config = getDefaultConfig(__dirname);
14
+ * config.transformer.babelTransformerPath = require.resolve('trickle-observe/metro-transformer');
15
+ * module.exports = config;
16
+ *
17
+ * Environment variables:
18
+ * TRICKLE_BACKEND_URL — URL of your trickle backend (default: http://localhost:4888)
19
+ * For real device: use your machine's LAN IP, e.g. http://192.168.1.5:4888
20
+ * TRICKLE_DEBUG — Set to "1" for debug logging
21
+ */
22
+ interface MetroTransformArgs {
23
+ src: string;
24
+ filename: string;
25
+ options: Record<string, unknown>;
26
+ }
27
+ export declare function transform({ src, filename, options }: MetroTransformArgs): Promise<any>;
28
+ export {};
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ /**
3
+ * trickle/metro-transformer — Metro bundler transform for React Native observability.
4
+ *
5
+ * Drop-in Metro transformer that instruments React Native components with
6
+ * trickle's render tracking, useState change tracking, and hook observability —
7
+ * the same tracking trickle's Vite plugin provides for web React apps.
8
+ *
9
+ * Setup in metro.config.js:
10
+ *
11
+ * const { getDefaultConfig } = require('expo/metro-config');
12
+ * // or: const { getDefaultConfig } = require('@react-native/metro-config');
13
+ *
14
+ * const config = getDefaultConfig(__dirname);
15
+ * config.transformer.babelTransformerPath = require.resolve('trickle-observe/metro-transformer');
16
+ * module.exports = config;
17
+ *
18
+ * Environment variables:
19
+ * TRICKLE_BACKEND_URL — URL of your trickle backend (default: http://localhost:4888)
20
+ * For real device: use your machine's LAN IP, e.g. http://192.168.1.5:4888
21
+ * TRICKLE_DEBUG — Set to "1" for debug logging
22
+ */
23
+ var __importDefault = (this && this.__importDefault) || function (mod) {
24
+ return (mod && mod.__esModule) ? mod : { "default": mod };
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.transform = transform;
28
+ const path_1 = __importDefault(require("path"));
29
+ const vite_plugin_1 = require("./vite-plugin");
30
+ // The upstream Babel transformer — try Expo first, fall back to bare RN
31
+ function getUpstreamTransformer() {
32
+ const candidates = [
33
+ '@expo/metro-config/babel-transformer',
34
+ 'metro-react-native-babel-transformer',
35
+ ];
36
+ for (const candidate of candidates) {
37
+ try {
38
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
39
+ return require(candidate);
40
+ }
41
+ catch {
42
+ // not installed, try next
43
+ }
44
+ }
45
+ throw new Error('[trickle/metro-transformer] Could not find a Metro Babel transformer. ' +
46
+ 'Install either @expo/metro-config or metro-react-native-babel-transformer.');
47
+ }
48
+ const backendUrl = process.env.TRICKLE_BACKEND_URL ?? 'http://localhost:4888';
49
+ const debug = process.env.TRICKLE_DEBUG === '1';
50
+ async function transform({ src, filename, options }) {
51
+ const upstreamTransformer = getUpstreamTransformer();
52
+ // Only instrument React Native component files
53
+ const ext = path_1.default.extname(filename).toLowerCase();
54
+ const isReactFile = ext === '.tsx' || ext === '.jsx';
55
+ const isJsFile = ext === '.ts' || ext === '.js';
56
+ if ((isReactFile || isJsFile) && !filename.includes('node_modules') && !filename.includes('trickle-observe')) {
57
+ const moduleName = path_1.default.basename(filename).replace(/\.[jt]sx?$/, '');
58
+ const transformed = (0, vite_plugin_1.transformEsmSource)(src, filename, moduleName, backendUrl, debug, false);
59
+ if (transformed !== src) {
60
+ if (debug) {
61
+ console.log(`[trickle/metro] Instrumented ${filename}`);
62
+ }
63
+ return upstreamTransformer.transform({ src: transformed, filename, options });
64
+ }
65
+ }
66
+ return upstreamTransformer.transform({ src, filename, options });
67
+ }
@@ -37,4 +37,5 @@ export declare function tricklePlugin(options?: TricklePluginOptions): {
37
37
  map: null;
38
38
  } | null;
39
39
  };
40
+ export declare function transformEsmSource(source: string, filename: string, moduleName: string, backendUrl: string, debug: boolean, traceVars: boolean, originalSource?: string | null): string;
40
41
  export default tricklePlugin;
@@ -23,6 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.tricklePlugin = tricklePlugin;
26
+ exports.transformEsmSource = transformEsmSource;
26
27
  const path_1 = __importDefault(require("path"));
27
28
  const fs_1 = __importDefault(require("fs"));
28
29
  function tricklePlugin(options = {}) {
@@ -496,8 +497,8 @@ function escapeRegexStr(str) {
496
497
  function transformEsmSource(source, filename, moduleName, backendUrl, debug, traceVars, originalSource) {
497
498
  // Detect React files for component render tracking
498
499
  const isReactFile = /\.(tsx|jsx)$/.test(filename);
499
- // Match top-level and nested function declarations (including async, export)
500
- const funcRegex = /^[ \t]*(?:export\s+)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
500
+ // Match top-level and nested function declarations (including async, export, export default)
501
+ const funcRegex = /^[ \t]*(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
501
502
  const funcInsertions = [];
502
503
  // Body insertions: insert at start of function body (for React render tracking)
503
504
  // propsExpr: JS expression to evaluate as the props object at render time
@@ -487,11 +487,11 @@ function findOriginalLineDestructured(origLines, varNames, transformedLine) {
487
487
  function escapeRegexStr(str) {
488
488
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
489
489
  }
490
- function transformEsmSource(source, filename, moduleName, backendUrl, debug, traceVars, originalSource) {
490
+ export function transformEsmSource(source, filename, moduleName, backendUrl, debug, traceVars, originalSource) {
491
491
  // Detect React files for component render tracking
492
492
  const isReactFile = /\.(tsx|jsx)$/.test(filename);
493
- // Match top-level and nested function declarations (including async, export)
494
- const funcRegex = /^[ \t]*(?:export\s+)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
493
+ // Match top-level and nested function declarations (including async, export, export default)
494
+ const funcRegex = /^[ \t]*(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
495
495
  const funcInsertions = [];
496
496
  // Body insertions: insert at start of function body (for React render tracking)
497
497
  // propsExpr: JS expression to evaluate as the props object at render time
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trickle-observe",
3
- "version": "0.2.61",
3
+ "version": "0.2.62",
4
4
  "description": "Runtime type observability for JavaScript applications",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,11 +17,12 @@
17
17
  "require": "./dist/vite-plugin.js"
18
18
  },
19
19
  "./trace-var": "./dist/trace-var.js",
20
- "./lambda": "./dist/lambda.js"
20
+ "./lambda": "./dist/lambda.js",
21
+ "./metro-transformer": "./dist/metro-transformer.js"
21
22
  },
22
23
  "scripts": {
23
24
  "build": "tsc && tsc -p tsconfig.esm.json",
24
- "test": "npm run build && node --experimental-strip-types --test src/vite-plugin.test.ts src/lambda.test.ts",
25
+ "test": "npm run build && node --experimental-strip-types --test src/vite-plugin.test.ts src/lambda.test.ts src/metro-transformer.test.ts",
25
26
  "prepublishOnly": "npm run build"
26
27
  },
27
28
  "optionalDependencies": {
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Unit tests for the Metro transformer (React Native observability).
3
+ *
4
+ * Tests the transformation logic independently — without needing a real Metro
5
+ * bundler or Babel pipeline. We test that transformEsmSource is applied correctly
6
+ * to React Native component source files.
7
+ *
8
+ * Run with: node --experimental-strip-types --test src/metro-transformer.test.ts
9
+ */
10
+ import { describe, it } from 'node:test';
11
+ import assert from 'node:assert/strict';
12
+ import { transformEsmSource } from '../dist/vite-plugin.js';
13
+
14
+ const BACKEND_URL = 'http://localhost:4888';
15
+
16
+ // Helper: transform as if coming from a .tsx React Native file
17
+ function transformRNTsx(code: string, filename = '/app/components/MyComponent.tsx'): string {
18
+ return transformEsmSource(code, filename, 'MyComponent', BACKEND_URL, false, false);
19
+ }
20
+
21
+ // Helper: transform as if coming from a .ts utility file
22
+ function transformRNTs(code: string, filename = '/app/utils/helper.ts'): string {
23
+ return transformEsmSource(code, filename, 'helper', BACKEND_URL, false, false);
24
+ }
25
+
26
+ // ── Metro transformer: React component detection ──────────────────────────────
27
+
28
+ describe('Metro transformer: React component detection', () => {
29
+ it('instruments uppercase components in .tsx files', () => {
30
+ const code = `function OrderCard({ order }) { return null; }`;
31
+ const out = transformRNTsx(code);
32
+ assert.notEqual(out, code, 'should transform');
33
+ assert.ok(out.includes('__trickle_rc'), 'should inject render tracker');
34
+ });
35
+
36
+ it('does not inject render tracker for .ts utility files', () => {
37
+ const code = `function formatPrice(amount) { return '$' + amount; }`;
38
+ const out = transformRNTs(code);
39
+ assert.ok(!out.includes('__trickle_rc'), 'should not inject render tracker in .ts files');
40
+ });
41
+
42
+ it('instruments export default function components (common React Native screen pattern)', () => {
43
+ const code = `export default function HomeScreen() { return null; }`;
44
+ const out = transformRNTsx(code);
45
+ assert.ok(out.includes('__trickle_rc'), 'should inject render tracker for export default function');
46
+ });
47
+
48
+ it('does not instrument lowercase utility functions as components', () => {
49
+ const code = `function formatOrder(order) { return order.id; }`;
50
+ const out = transformRNTsx(code);
51
+ if (out !== code) {
52
+ assert.ok(!out.includes('__trickle_rc'), 'lowercase function should not be tracked as component');
53
+ }
54
+ });
55
+ });
56
+
57
+ // ── Metro transformer: useState tracking ─────────────────────────────────────
58
+
59
+ describe('Metro transformer: useState tracking in React Native', () => {
60
+ it('tracks useState in a React Native functional component', () => {
61
+ const code = [
62
+ `import React, { useState } from 'react';`,
63
+ `function Counter() {`,
64
+ ` const [count, setCount] = useState(0);`,
65
+ ` return null;`,
66
+ `}`,
67
+ ].join('\n');
68
+ const out = transformRNTsx(code);
69
+ assert.ok(out.includes('__trickle_ss'), 'should inject state setter wrapper');
70
+ assert.ok(out.includes('__trickle_s_setCount'), 'should rename original setter');
71
+ assert.ok(out.includes('"count"'), 'should include state variable name');
72
+ });
73
+
74
+ it('tracks multiple useState calls in a React Native screen', () => {
75
+ const code = [
76
+ `function CheckoutScreen() {`,
77
+ ` const [items, setItems] = useState([]);`,
78
+ ` const [total, setTotal] = useState(0);`,
79
+ ` const [loading, setLoading] = useState(false);`,
80
+ ` return null;`,
81
+ `}`,
82
+ ].join('\n');
83
+ const out = transformRNTsx(code);
84
+ const count = (out.match(/const \w+=__trickle_ss/g) || []).length;
85
+ assert.equal(count, 3, 'should wrap all 3 useState setters');
86
+ });
87
+
88
+ it('handles TypeScript typed useState in React Native', () => {
89
+ const code = [
90
+ `function ProfileScreen() {`,
91
+ ` const [user, setUser] = useState<User | null>(null);`,
92
+ ` return null;`,
93
+ `}`,
94
+ ].join('\n');
95
+ const out = transformRNTsx(code);
96
+ assert.ok(out.includes('__trickle_ss'), 'should track typed useState');
97
+ assert.ok(out.includes('"user"'), 'should include state name');
98
+ });
99
+ });
100
+
101
+ // ── Metro transformer: hook observability ────────────────────────────────────
102
+
103
+ describe('Metro transformer: hook observability in React Native', () => {
104
+ it('wraps useEffect in a React Native component', () => {
105
+ const code = [
106
+ `function DataScreen() {`,
107
+ ` useEffect(() => {`,
108
+ ` fetchData();`,
109
+ ` }, []);`,
110
+ ` return null;`,
111
+ `}`,
112
+ ].join('\n');
113
+ const out = transformRNTsx(code);
114
+ assert.ok(out.includes('__trickle_hw'), 'should inject hook wrapper for useEffect');
115
+ assert.ok(out.includes('"useEffect"'), 'should record hook name');
116
+ });
117
+
118
+ it('wraps useCallback in a React Native component', () => {
119
+ const code = [
120
+ `function ListScreen() {`,
121
+ ` const handlePress = useCallback(() => {`,
122
+ ` navigate('Detail');`,
123
+ ` }, []);`,
124
+ ` return null;`,
125
+ `}`,
126
+ ].join('\n');
127
+ const out = transformRNTsx(code);
128
+ assert.ok(out.includes('__trickle_hw'), 'should inject hook wrapper for useCallback');
129
+ assert.ok(out.includes('"useCallback"'), 'should record hook name');
130
+ });
131
+ });
132
+
133
+ // ── Metro transformer: source unchanged for non-RN files ─────────────────────
134
+
135
+ describe('Metro transformer: passthrough for non-component files', () => {
136
+ it('does not modify a plain TypeScript utility file', () => {
137
+ const code = `export function add(a: number, b: number): number { return a + b; }`;
138
+ const out = transformRNTs(code);
139
+ // .ts files should not get __trickle_rc (no React components)
140
+ assert.ok(!out.includes('__trickle_rc'), 'should not inject React tracking in .ts files');
141
+ });
142
+
143
+ it('does not inject useState tracking in .ts files', () => {
144
+ const code = `function helper() {\n const [x, setX] = useState(0);\n return x;\n}`;
145
+ const out = transformRNTs(code);
146
+ assert.ok(!out.includes('__trickle_ss'), 'should not track useState in .ts files');
147
+ });
148
+ });
@@ -0,0 +1,75 @@
1
+ /**
2
+ * trickle/metro-transformer — Metro bundler transform for React Native observability.
3
+ *
4
+ * Drop-in Metro transformer that instruments React Native components with
5
+ * trickle's render tracking, useState change tracking, and hook observability —
6
+ * the same tracking trickle's Vite plugin provides for web React apps.
7
+ *
8
+ * Setup in metro.config.js:
9
+ *
10
+ * const { getDefaultConfig } = require('expo/metro-config');
11
+ * // or: const { getDefaultConfig } = require('@react-native/metro-config');
12
+ *
13
+ * const config = getDefaultConfig(__dirname);
14
+ * config.transformer.babelTransformerPath = require.resolve('trickle-observe/metro-transformer');
15
+ * module.exports = config;
16
+ *
17
+ * Environment variables:
18
+ * TRICKLE_BACKEND_URL — URL of your trickle backend (default: http://localhost:4888)
19
+ * For real device: use your machine's LAN IP, e.g. http://192.168.1.5:4888
20
+ * TRICKLE_DEBUG — Set to "1" for debug logging
21
+ */
22
+
23
+ import path from 'path';
24
+ import { transformEsmSource } from './vite-plugin';
25
+
26
+ // The upstream Babel transformer — try Expo first, fall back to bare RN
27
+ function getUpstreamTransformer() {
28
+ const candidates = [
29
+ '@expo/metro-config/babel-transformer',
30
+ 'metro-react-native-babel-transformer',
31
+ ];
32
+ for (const candidate of candidates) {
33
+ try {
34
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
35
+ return require(candidate);
36
+ } catch {
37
+ // not installed, try next
38
+ }
39
+ }
40
+ throw new Error(
41
+ '[trickle/metro-transformer] Could not find a Metro Babel transformer. ' +
42
+ 'Install either @expo/metro-config or metro-react-native-babel-transformer.',
43
+ );
44
+ }
45
+
46
+ interface MetroTransformArgs {
47
+ src: string;
48
+ filename: string;
49
+ options: Record<string, unknown>;
50
+ }
51
+
52
+ const backendUrl = process.env.TRICKLE_BACKEND_URL ?? 'http://localhost:4888';
53
+ const debug = process.env.TRICKLE_DEBUG === '1';
54
+
55
+ export async function transform({ src, filename, options }: MetroTransformArgs) {
56
+ const upstreamTransformer = getUpstreamTransformer();
57
+
58
+ // Only instrument React Native component files
59
+ const ext = path.extname(filename).toLowerCase();
60
+ const isReactFile = ext === '.tsx' || ext === '.jsx';
61
+ const isJsFile = ext === '.ts' || ext === '.js';
62
+
63
+ if ((isReactFile || isJsFile) && !filename.includes('node_modules') && !filename.includes('trickle-observe')) {
64
+ const moduleName = path.basename(filename).replace(/\.[jt]sx?$/, '');
65
+ const transformed = transformEsmSource(src, filename, moduleName, backendUrl, debug, false);
66
+ if (transformed !== src) {
67
+ if (debug) {
68
+ console.log(`[trickle/metro] Instrumented ${filename}`);
69
+ }
70
+ return upstreamTransformer.transform({ src: transformed, filename, options });
71
+ }
72
+ }
73
+
74
+ return upstreamTransformer.transform({ src, filename, options });
75
+ }
@@ -40,6 +40,13 @@ describe('React file detection', () => {
40
40
  }
41
41
  });
42
42
 
43
+ it('tracks export default function components', () => {
44
+ const code = `export default function HomeScreen() { return null; }`;
45
+ const out = transformTsx(code);
46
+ assert.ok(out, 'should transform');
47
+ assert.ok(out!.includes('__trickle_rc'), 'export default function should be tracked as component');
48
+ });
49
+
43
50
  it('does not track lowercase functions as components', () => {
44
51
  const code = `function helper(x) { return x + 1; }`;
45
52
  const out = transformTsx(code);
@@ -481,7 +481,7 @@ function escapeRegexStr(str: string): string {
481
481
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
482
482
  }
483
483
 
484
- function transformEsmSource(
484
+ export function transformEsmSource(
485
485
  source: string,
486
486
  filename: string,
487
487
  moduleName: string,
@@ -493,8 +493,8 @@ function transformEsmSource(
493
493
  // Detect React files for component render tracking
494
494
  const isReactFile = /\.(tsx|jsx)$/.test(filename);
495
495
 
496
- // Match top-level and nested function declarations (including async, export)
497
- const funcRegex = /^[ \t]*(?:export\s+)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
496
+ // Match top-level and nested function declarations (including async, export, export default)
497
+ const funcRegex = /^[ \t]*(?:export\s+(?:default\s+)?)?(?:async\s+)?function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/gm;
498
498
  const funcInsertions: Array<{ position: number; name: string; paramNames: string[] }> = [];
499
499
  // Body insertions: insert at start of function body (for React render tracking)
500
500
  // propsExpr: JS expression to evaluate as the props object at render time