Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/features-encryption-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/core": minor
---

Add `features.encryption` to `WorkflowMetadata` returned by `getWorkflowMetadata()`
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ You may want to use this function when you need to:

* Log workflow run IDs
* Access timing information of a workflow
* Detect whether encryption is enabled for the current run

<Callout>
If you need to access step context, take a look at [`getStepMetadata`](/docs/api-reference/workflow/get-step-metadata).
Expand All @@ -29,6 +30,32 @@ async function testWorkflow() {
}
```

### Detecting Encryption

The `features` object indicates which capabilities are active for the current run. Library authors can use `features.encryption` to control whether sensitive data is included in step return values, which are serialized to the event log:

```typescript lineNumbers
import { getWorkflowMetadata } from "workflow"

declare function getUserProfile(userId: string): Promise<{ name: string; ssn: string }>; // @setup

async function fetchUserProfile(userId: string) {
"use step"

const { features } = getWorkflowMetadata() // [!code highlight]
const profile = await getUserProfile(userId)

if (!features.encryption) { // [!code highlight]
// Omit sensitive fields from the return value,
// since it will be stored unencrypted in the event log
const { ssn, ...safe } = profile
return safe
}

return profile
}
```

## API Signature

### Parameters
Expand Down
10 changes: 10 additions & 0 deletions packages/core/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@ describe('e2e', () => {
);
expect(returnValue.stepMetadata.url).toBeUndefined();

// workflow context should have features and stepMetadata shouldn't
expect(returnValue.workflowMetadata.features).toBeDefined();
expect(typeof returnValue.workflowMetadata.features.encryption).toBe(
'boolean'
);
expect(returnValue.innerWorkflowMetadata.features).toStrictEqual(
returnValue.workflowMetadata.features
);
expect(returnValue.stepMetadata.features).toBeUndefined();

// workflow context shouldn't have stepId, stepStartedAt, or attempt
expect(returnValue.workflowMetadata.stepId).toBeUndefined();
expect(returnValue.workflowMetadata.stepStartedAt).toBeUndefined();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/runtime/step-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ const stepHandler = createQueueHandler(
url: isVercel
? `https://${process.env.VERCEL_URL}`
: `http://localhost:${port ?? 3000}`,
features: { encryption: !!encryptionKey },
},
ops,
closureVars: hydratedInput.closureVars,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/serialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,7 @@ describe('step function serialization', () => {
workflowRunId: 'test-run',
workflowStartedAt: new Date(),
url: 'http://localhost:3000',
features: { encryption: false },
},
ops: [],
},
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/step/writable-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('step-level getWritable', () => {
workflowName: 'test-workflow',
workflowRunId: 'wrun_test123',
workflowStartedAt: new Date(),
url: 'http://localhost:3000',
features: { encryption: false },
},
ops,
encryptionKey: undefined,
Expand Down Expand Up @@ -82,6 +84,8 @@ describe('step-level getWritable', () => {
workflowName: 'test-workflow',
workflowRunId: 'wrun_test123',
workflowStartedAt: new Date(),
url: 'http://localhost:3000',
features: { encryption: false },
},
ops,
encryptionKey: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export async function runWorkflow(
workflowRunId: workflowRun.runId,
workflowStartedAt: new vmGlobalThis.Date(+startedAt),
url,
features: { encryption: !!encryptionKey },
};

// @ts-expect-error - `@types/node` says symbol is not valid, but it does work
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/workflow/get-workflow-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ export interface WorkflowMetadata {
* The URL where the workflow can be triggered.
*/
url: string;

/**
* Feature flags indicating which capabilities are active for this workflow run.
*/
features: {
/**
* Whether encryption is enabled for this workflow run.
* When `true`, step inputs, outputs, and other serialized data
* are encrypted at rest.
*/
encryption: boolean;
};
Comment thread
TooTallNate marked this conversation as resolved.
}

export const WORKFLOW_CONTEXT_SYMBOL =
Expand Down
1 change: 1 addition & 0 deletions workbench/example/workflows/99_e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ export async function workflowAndStepMetadataWorkflow() {
workflowRunId: workflowMetadata.workflowRunId,
workflowStartedAt: workflowMetadata.workflowStartedAt,
url: workflowMetadata.url,
features: workflowMetadata.features,
},
stepMetadata,
innerWorkflowMetadata,
Expand Down
Loading