npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hemia/workflow-engine

v0.0.5

Published

Motor de flujos de trabajo flexible y extensible desarrollado por Hemia Technologies.

Readme

@hemia/workflow-engine

Motor de flujos de trabajo flexible y extensible desarrollado por Hemia. Orquesta pasos definidos en JSON, integra servicios HTTP, pausa por webhooks, evalúa reglas y maneja errores con rutas dedicadas.

Instalación

npm install @hemia/workflow-engine

Características clave

  • Definición de workflows en JSON.
  • Nodos soportados: http, wait-for-webhook, validator (+ nodos personalizados).
  • Trigger por webhook con autenticación Bearer (startFromWebhook).
  • Configuración global y plantillas con ${config.*} y {{ ... }}.
  • Pausa y reanudación (WAITING) con wait-for-webhook.
  • Manejo de errores: error.next por paso y workflow.errorHandlers.
  • Reintentos automáticos en HTTP con params.retry y política global (retryPolicy).
  • Templates con JEXL y transforms globales (pipes).
  • Persistencia de contexto (save/load) inyectable.

Uso básico

import { WorkflowEngine, NodeRegistry } from "@hemia/workflow-engine";
import { httpNode } from "@hemia/workflow-node-http";
import { waitForWebhookNode } from "@hemia/workflow-node-wait-for-webhook";
import { validatorNode } from "@hemia/workflow-node-validator";
import workflow from "./workflows/mi-flujo.json";

// Registro de nodos
const registry = new NodeRegistry();
registry.register("http", httpNode);
registry.register("wait-for-webhook", waitForWebhookNode);
registry.register("validator", validatorNode);

// Persistencia (implementa según tu app)
const saveContext = async (ctx: any, execId: string, workflowId: string, status?: "RUNNING"|"WAITING"|"FAILED"|"COMPLETED") => { /* ... */ };
const loadContext = async (execId: string) => ({ /* ctx persistido */ });

// Crear y ejecutar
const engine = new WorkflowEngine(workflow, "exec-123", registry, saveContext, loadContext);
await engine.run();

Trigger por Webhook + Bearer

Define el trigger en tu workflow y usa startFromWebhook en tu servidor HTTP.

{
  "trigger": { "type": "webhook", "path": "/webhooks/vacation-request-flow/start", "authentication": { "type": "bearer" } }
}

Ejemplo con Express:

import express from "express";
import { WorkflowEngine, NodeRegistry } from "@hemia/workflow-engine";
// ...registra nodos y prepara save/load...

const app = express();
app.use(express.json());

app.post("/webhooks/vacation-request-flow/start", async (req, res) => {
  const engine = new WorkflowEngine(workflow, "exec-abc", registry, saveContext, loadContext);
  const { accepted, reason } = await engine.startFromWebhook({ headers: req.headers as any, path: req.path, body: req.body });
  if (!accepted) {
    const code = reason === "MISSING_BEARER" || reason === "INVALID_TOKEN" ? 401 : 404;
    return res.status(code).json({ reason });
  }
  return res.sendStatus(202);
});

Configuración global e interpolación

  • Incluye un bloque config en el workflow; el engine lo expone en variables.config.
  • En strings puedes usar:
    • ${config.apiUrl}/path
    • "{{config.apiUrl}}/path" (JEXL)
{
  "config": {
    "apiUrl": "https://api.example.com",
    "retryPolicy": { "maxAttempts": 3, "backoff": { "type": "exponential", "initialDelay": "1000" } }
  }
}

Templates y funciones con JEXL (transforms)

Registra transforms globales en @hemia/workflow-core y úsalos con la sintaxis de pipe:

import { registerJexlTransforms } from "@hemia/workflow-core";

registerJexlTransforms({
  getManagerEmail: (employeeId: string) => `manager+${employeeId}@example.com`,
  generateApprovalUrl: (requestId: string) => `${process.env.APP_URL}/approve/${requestId}`
});

En el workflow:

{ "to": "{{ input.employeeId | getManagerEmail }}" }

Validaciones con el nodo validator

Valida datos de entrada u otras variables con expresiones JEXL.

{
  "id": "validateInput",
  "type": "validator",
  "params": { "rules": [
    "input.startDate < input.endDate",
    "input.employeeId != null",
    "input.email != null"
  ]},
  "next": [{ "id": "checkVacationBalance" }]
}

Reintentos automáticos en HTTP

  • Actívalos por paso con "params.retry": true.
  • Configúralos globalmente en config.retryPolicy (maxAttempts y backoff: fixed|exponential, initialDelay en ms).
  • Ejemplo:
{
  "id": "saveRequest",
  "type": "http",
  "params": {
    "method": "POST",
    "url": "${config.apiUrl}/vacations",
    "body": "{{input}}",
    "retry": true,
    "timeout": 30000
  },
  "error": { "next": "handleError" }
}

Manejo de errores (routing)

  • Por paso: usa "error.next" para redirigir a un handler cuando falle.
  • Global: define "errorHandlers" en el workflow.
{
  "errorHandlers": {
    "handleError": {
      "id": "handleError",
      "type": "http",
      "params": {
        "method": "POST",
        "url": "${config.errorHandlerUrl}",
        "body": { "workflow": "{{context.workflowId}}", "error": "{{error}}", "context": "{{context}}" }
      }
    }
  }
}

El engine intentará:

  1. Redirigir a step.error.next si existe.
  2. Usar un handler global definido en errorHandlers.
  3. Si no hay ruta de error, marca FAILED y detiene.

Pausa y reanudación (wait-for-webhook)

  • Un paso "wait-for-webhook" pausará la ejecución (status WAITING).
  • Reanuda luego con engine.resume(fromStepId) o invocando el webhook que tú manejes.

Ejemplo de pausa:

{
  "id": "waitExtraData",
  "type": "wait-for-webhook",
  "params": { "stepId": "waitExtraData", "reason": "extra-data" }
}

Estados del workflow

  • RUNNING: ejecutando.
  • WAITING: pausado esperando webhook.
  • FAILED: error sin handler o tras agotar reintentos.
  • COMPLETED: flujo finalizado.

Notas y buenas prácticas

  • Establece variables.input en el contexto inicial antes de run() o via startFromWebhook(body).
  • En nodos HTTP usa saveAs para referenciar datos en condiciones: p.ej., "balance.data.availableDays".
  • Usa timeouts numéricos