Skip to content

feat: add getPort for detecting pid port#233

Merged
adriandlam merged 22 commits into
mainfrom
feat/detect-app-port
Nov 7, 2025
Merged

feat: add getPort for detecting pid port#233
adriandlam merged 22 commits into
mainfrom
feat/detect-app-port

Conversation

@adriandlam
Copy link
Copy Markdown
Contributor

@adriandlam adriandlam commented Nov 5, 2025

detects the port of the current node process so no manual port assignment is needed (ex. in SvelteKit)

closes #236

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Nov 5, 2025

🦋 Changeset detected

Latest commit: 1be3735

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 15 packages
Name Type
@workflow/core Patch
@workflow/utils Patch
@workflow/world-local Patch
@workflow/builders Patch
@workflow/cli Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/web-shared Patch
workflow Patch
@workflow/errors Patch
@workflow/world-postgres Patch
@workflow/sveltekit Patch
@workflow/world-testing Patch
@workflow/ai Patch
@workflow/world-vercel Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Nov 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
example-nextjs-workflow-turbopack Ready Ready Preview Comment Nov 7, 2025 10:24pm
example-nextjs-workflow-webpack Ready Ready Preview Comment Nov 7, 2025 10:24pm
example-workflow Ready Ready Preview Comment Nov 7, 2025 10:24pm
workbench-nitro-workflow Ready Ready Preview Comment Nov 7, 2025 10:24pm
workbench-nuxt-workflow Ready Ready Preview Comment Nov 7, 2025 10:24pm
workbench-sveltekit-workflow Ready Ready Preview Comment Nov 7, 2025 10:24pm
workbench-vite-workflow Ready Ready Preview Comment Nov 7, 2025 10:24pm
workflow-docs Ready Ready Preview Comment Nov 7, 2025 10:24pm

@adriandlam adriandlam force-pushed the feat/detect-app-port branch from 4a90dfd to 89abfce Compare November 5, 2025 22:16
@adriandlam adriandlam force-pushed the feat/detect-app-port branch from 81612a9 to 7fb7d19 Compare November 5, 2025 22:30
Comment thread packages/core/src/runtime.ts Outdated
Copy link
Copy Markdown
Contributor

@vercel vercel Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Build Fix:

The getPort() function imports pid-port which depends on execa and other Node.js modules that are restricted in workflow functions. This causes build failures when workflow functions try to bundle these Node.js-specific utilities.

View Details
📝 Patch Details
diff --git a/packages/core/src/runtime.ts b/packages/core/src/runtime.ts
index 4f94e79..0bb4cc7 100644
--- a/packages/core/src/runtime.ts
+++ b/packages/core/src/runtime.ts
@@ -8,7 +8,7 @@ import {
   WorkflowRunNotCompletedError,
   WorkflowRuntimeError,
 } from '@workflow/errors';
-import { getPort } from '@workflow/utils';
+import { getPort } from '@workflow/utils/node';
 import type {
   Event,
   WorkflowRun,
diff --git a/packages/core/src/workflow.ts b/packages/core/src/workflow.ts
index a2a8559..3f13384 100644
--- a/packages/core/src/workflow.ts
+++ b/packages/core/src/workflow.ts
@@ -1,6 +1,7 @@
 import { runInContext } from 'node:vm';
 import { ERROR_SLUGS } from '@workflow/errors';
-import { getPort, withResolvers } from '@workflow/utils';
+import { withResolvers } from '@workflow/utils';
+import { getPort } from '@workflow/utils/node';
 import type { Event, WorkflowRun } from '@workflow/world';
 import * as nanoid from 'nanoid';
 import { monotonicFactory } from 'ulid';
diff --git a/packages/utils/package.json b/packages/utils/package.json
index ed493d0..67de50b 100644
--- a/packages/utils/package.json
+++ b/packages/utils/package.json
@@ -20,6 +20,10 @@
     ".": {
       "types": "./dist/index.d.ts",
       "default": "./dist/index.js"
+    },
+    "./node": {
+      "types": "./dist/node.d.ts",
+      "default": "./dist/node.js"
     }
   },
   "scripts": {
diff --git a/packages/utils/src/index.test.ts b/packages/utils/src/index.test.ts
index 6608efe..4b2f851 100644
--- a/packages/utils/src/index.test.ts
+++ b/packages/utils/src/index.test.ts
@@ -1,6 +1,7 @@
 import http from 'node:http';
 import { describe, expect, it } from 'vitest';
-import { getPort, once, parseDurationToDate, withResolvers } from './index';
+import { once, parseDurationToDate, withResolvers } from './index';
+import { getPort } from './node';
 
 describe('parseDurationToDate', () => {
   it('should parse duration strings correctly', () => {
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 375f7ed..6e657c9 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -1,6 +1,5 @@
 import type { StringValue } from 'ms';
 import ms from 'ms';
-import { pidToPorts } from 'pid-port';
 
 export interface PromiseWithResolvers<T> {
   promise: Promise<T>;
@@ -86,18 +85,3 @@ export function parseDurationToDate(param: StringValue | Date | number): Date {
     );
   }
 }
-
-/**
- * Gets the port number that the process is listening on.
- * @returns The port number that the process is listening on, or undefined if the process is not listening on any port.
- */
-export async function getPort(): Promise<number | undefined> {
-  const pid = process.pid;
-  const ports = await pidToPorts(pid);
-  if (!ports || ports.size === 0) {
-    return undefined;
-  }
-
-  const smallest = Math.min(...ports);
-  return smallest;
-}
diff --git a/packages/utils/src/node.ts b/packages/utils/src/node.ts
new file mode 100644
index 0000000..b2c4b1f
--- /dev/null
+++ b/packages/utils/src/node.ts
@@ -0,0 +1,20 @@
+import { pidToPorts } from 'pid-port';
+
+/**
+ * Gets the port number that the process is listening on.
+ * 
+ * **Note: This function uses Node.js APIs and should only be used in step functions or server-side code.**
+ * Do not import this in workflow functions.
+ * 
+ * @returns The port number that the process is listening on, or undefined if the process is not listening on any port.
+ */
+export async function getPort(): Promise<number | undefined> {
+  const pid = process.pid;
+  const ports = await pidToPorts(pid);
+  if (!ports || ports.size === 0) {
+    return undefined;
+  }
+
+  const smallest = Math.min(...ports);
+  return smallest;
+}
\ No newline at end of file
diff --git a/packages/world-local/src/queue.ts b/packages/world-local/src/queue.ts
index 07e0c40..242d9ce 100644
--- a/packages/world-local/src/queue.ts
+++ b/packages/world-local/src/queue.ts
@@ -1,6 +1,6 @@
 import { setTimeout } from 'node:timers/promises';
 import { JsonTransport } from '@vercel/queue';
-import { getPort } from '@workflow/utils';
+import { getPort } from '@workflow/utils/node';
 import { MessageId, type Queue, ValidQueueName } from '@workflow/world';
 import { monotonicFactory } from 'ulid';
 import { Agent } from 'undici';

Analysis

Node.js modules bundled into workflow functions causing build failure

What fails: Build command pnpm build for @workflow/example-vite fails due to Node.js module restrictions in workflow functions

How to reproduce:

cd workbench/vite && pnpm build

Result:

✘ [ERROR] Cannot use Node.js module "node:events" in workflow functions. Move this module to a step function.
✘ [ERROR] Cannot use Node.js module "node:stream" in workflow functions. Move this module to a step function.
✘ [ERROR] Cannot use Node.js module "node:process" in workflow functions. Move this module to a step function.
[... 86+ similar errors from execa, cross-spawn, and other Node.js dependencies]

The getPort() function from @workflow/utils was importing pid-port which depends on execa, bringing Node.js APIs into the workflow function bundle where they are restricted.

Fix on Vercel

Comment thread packages/core/src/util.test.ts Outdated
Comment thread packages/core/src/util.test.ts Outdated
Comment thread packages/world-local/src/queue.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Workflow execution fails in dev when running nitro on port other than 3000

3 participants