Skip to content

Conversation

@djgrant
Copy link
Contributor

@djgrant djgrant commented Mar 12, 2025

Adds support for context in workflows and HTTP middleware.

Currently middleware can be attached to the HTTP server.

const middleware = createMiddleware(async (req, event, next, logger) => {
  // Before workflow invocation
  event.context.set('key', 'value');
  
  // Continue to next middleware
  const response = await next();
  
  // After workflow invocation
  response.headers.set('X-Custom-Header', 'value');
  return response;
});

const server = createWorkflowHttpServer({
  port: 8080,
  logger,
  invoker,
  middleware: [middleware]
});

When a workflow is invoked, the event object now includes context.

import { workflow } from "yieldstar";

export const simpleWorkflow = workflow(async function* (step, event, logger) {
  const key = event.context.get('key');
  yield* step.delay(1000);
  return key;
});

Mutations to context during workflow phases are not persisted.

Todo:

  • disable context.set to make prevent context being mutated in workflows
  • throw an error from middleware if context is mutated after calling next

@djgrant djgrant force-pushed the middleware branch 3 times, most recently from 6a4ca91 to e6dac11 Compare March 13, 2025 11:58
@djgrant djgrant merged commit 9850fee into main Mar 13, 2025
@djgrant djgrant deleted the middleware branch March 13, 2025 22:27
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.

2 participants