@teleporthq/teleport-plugin-next-workflows 0.43.36 → 0.43.38

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.
@@ -29,6 +29,7 @@ import {
29
29
  collectUsedRealtimeActionTypes,
30
30
  } from './graph-utils'
31
31
  import { nodeRegistry } from './nodes'
32
+ import { resolveHandlerEntryName } from './nodes/types'
32
33
  import {
33
34
  generateClientRuntimeCode,
34
35
  generateServerRuntimeCode,
@@ -915,10 +916,26 @@ export class NextWorkflowProjectPlugin implements ProjectPlugin {
915
916
  // contract (`handlerToString(fn)` preserves `fn.name`, and every handler
916
917
  // names its entry `nodeType.replace(/-/g, '_')`) is enforced by
917
918
  // node-handler-file-inline-map.test.ts across the whole node registry.
919
+ //
920
+ // That contract holds for the handler's OWN source (checked below via
921
+ // resolveHandlerEntryName), but not necessarily for the RUNTIME name a
922
+ // caller's bundler assigns it. Handlers built from `handlerToString(fn)`
923
+ // embed `fn.toString()` — a snapshot of whatever `fn` was actually named
924
+ // at the moment it's read. When this package is bundled and minified by a
925
+ // consumer (e.g. teleport-gui's browser packer worker, built with
926
+ // Next.js/Terser), the minifier freely renames `fn`'s declaration: nothing
927
+ // in the bundle calls it by name, only `.toString()` reads it at runtime,
928
+ // which is invisible to the minifier. The naive
929
+ // `nodeType.replace(/-/g, '_')` name would then reference an identifier
930
+ // that no longer exists in the embedded source — a Vercel-only
931
+ // "Collecting page data" ReferenceError, since local dev never runs a
932
+ // minifier. resolveHandlerEntryName reads the name the source text
933
+ // ACTUALLY declares instead of assuming it matches the convention.
918
934
  const entries = exportNames
919
935
  .map((t, index) => {
920
- const entryFn = t.replace(/-/g, '_')
921
- return ` '${t}': (function () {\n${handlers[index].trim()}\nreturn ${entryFn};\n})()`
936
+ const source = handlers[index].trim()
937
+ const entryFn = resolveHandlerEntryName(source, t)
938
+ return ` '${t}': (function () {\n${source}\nreturn ${entryFn};\n})()`
922
939
  })
923
940
  .join(',\n')
924
941
 
@@ -1530,8 +1547,31 @@ module.exports = __customNodeRegistry;
1530
1547
  }
1531
1548
  })
1532
1549
 
1550
+ // See resolveHandlerEntryName: handlers built from `handlerToString(fn)`
1551
+ // on a real function (e.g. general-if-statement) can have `fn` renamed by
1552
+ // a consumer's own minifier (nothing in the bundle calls it by name, only
1553
+ // this runtime `.toString()` read does) — so the reference below must
1554
+ // match whatever `handlers[index]` actually declares, not the
1555
+ // `t.replace(/-/g, '_')` convention name.
1556
+ //
1557
+ // Each entry is ALSO isolated in its own IIFE, rather than declaring every
1558
+ // handler as a bare sibling statement inside `useGlobalWorkflows()`. Two
1559
+ // DIFFERENT node types are minified independently (each in its own
1560
+ // source file), so their real declared names can coincidentally collide —
1561
+ // e.g. state-update-local-state and payment-cancel-plan can both
1562
+ // legitimately mangle down to the same short name. Declared as siblings
1563
+ // in one shared function body, the second declaration would silently
1564
+ // shadow the first, so BOTH map entries end up pointing at the SAME
1565
+ // (wrong-for-one-of-them) function — a silent wrong-handler-executes bug,
1566
+ // not even a crash. An IIFE per entry gives every handler its own scope,
1567
+ // exactly like generateNodeHandlerFile already does, so a same-named
1568
+ // collision between two unrelated handlers can never shadow each other.
1533
1569
  const handlerEntries = emittedHandlerTypes
1534
- .map((t) => ` '${t}': ${t.replace(/-/g, '_')}`)
1570
+ .map((t, index) => {
1571
+ const source = handlers[index].trim()
1572
+ const entryFn = resolveHandlerEntryName(source, t)
1573
+ return ` '${t}': (function () {\n${source}\nreturn ${entryFn};\n})()`
1574
+ })
1535
1575
  .join(',\n')
1536
1576
 
1537
1577
  // NOTE: this module is pulled into the GLOBAL client bundle (every page,
@@ -1549,8 +1589,6 @@ import workflowRuntime from './runtime';
1549
1589
  const executeWorkflowWithSegments = workflowRuntime.executeWorkflowWithSegments;
1550
1590
 
1551
1591
  export function useGlobalWorkflows() {
1552
- ${handlers.join('\n')}
1553
-
1554
1592
  const clientNodeHandlers = {
1555
1593
  ${handlerEntries}
1556
1594
  };