feat: add getPort for detecting pid port#233
Conversation
🦋 Changeset detectedLatest commit: 1be3735 The changes in this PR will be included in the next version bump. This PR includes changesets to release 15 packages
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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
4a90dfd to
89abfce
Compare
81612a9 to
7fb7d19
Compare
There was a problem hiding this comment.
🔧 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 buildResult:
✘ [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.
detects the port of the current node process so no manual port assignment is needed (ex. in SvelteKit)
closes #236