@siglo/http Client Step Guide
@siglo/http provides resilient, enterprise-grade, W3C-traced HTTP client steps for Siglo workflows. It replaces fragile, ad-hoc fetch() and axios() calls inside workflow steps with coordinated retry backoff, first-class socket abort signal binding, AsyncLocalStorage (ASL) network egress perimeter enforcement, declarative saga compensations, and Standard Schema validation.
1. Overview & Architecture
In modern backend architectures, workflow steps frequently interact with third-party SaaS APIs, payment gateways, and microservices via HTTP/REST. Ad-hoc HTTP clients introduce systemic failure modes: conflicting retries that multiply thundering herds, unclosed TCP sockets that leak file descriptors on timeout, missing W3C trace context, and uncompensated mutations when downstream steps fail.@siglo/http integrates directly into Siglo’s 5-phase step lifecycle:
2. Key Capabilities & Technical Specifications
3. Installation
Install@siglo/http alongside @siglo/core:
@siglo/core:^1.0.0@opentelemetry/api:^1.0.0(optional, for distributed trace context propagation)
4. Deep Technical Capabilities
4.1 AsyncLocalStorage (ASL) Network Egress Perimeter
@siglo/http integrates natively with Siglo’s least-privilege network egress security perimeter:
- Host Verification: Before sending a single byte over the wire,
httpStepextracts the target hostname and port from the URL (static or dynamically resolved from workflow context). - Policy Assertion: It verifies the host against active AsyncLocalStorage storage (
networkPolicyStorage.getStore()):- If the host matches an authorized pattern (e.g.
api.stripe.com,*.internal.net, orlocalhost), execution proceeds. - If unauthorized, it immediately throws
NetworkPolicyViolationExceptionwith{ phase: 'before', retryable: false }.
- If the host matches an authorized pattern (e.g.
- AST Graph Governance: When static URLs are provided,
httpStepregisters the target host into the step’s metadata (step.allowNetwork([host])), making egress dependencies visible intoGraph(),inspectGraph(), and Mermaid diagrams.
4.2 W3C Distributed Trace Context Propagation
When running within an instrumented workflow (@siglo/opentelemetry), @siglo/http maintains an unbroken trace across service boundaries:
- Outgoing Headers: Automatically injects W3C headers:
traceparent:00-{traceId}-{spanId}-{traceFlags}tracestate: vendor routing state (if present).
- Span Semantic Conventions: Enriches the active span with standard HTTP attributes:
http.request.method:POSTurl.full:https://api.example.com/v1/resourceserver.address:api.example.comhttp.response.status_code:200
- Metadata Output: Populates
$httpMetadata.traceIdon the step’s return payload.
4.3 Status Code Classification & Retry-After Backoff
@siglo/http intelligently classifies HTTP response codes:
allowNotFound: true
When fetching optional resources, setting allowNotFound: true prevents 404 from throwing an error. Instead, the step returns:
4.4 Declarative Saga Compensation (.compensateWithHttp)
When an HTTP step creates an external state mutation (such as an authorized charge or hotel reservation), and a subsequent workflow step fails, Siglo’s Saga engine rolls back state in reverse order (LIFO).
You can declare the rollback HTTP call inline via compensate or fluent .compensateWithHttp():
5. API Reference
httpStep(name, options)
Creates an HttpStep instance conforming to Step<TInput, HttpStepResult<TOutput>, TName>.
HttpStepOptions Configuration
HttpMetadata Telemetry
Every httpStep attaches telemetry accessible via $httpMetadata and $steps.<stepName>.httpMetadata:
6. Concrete Usage Examples
Example 1: E-Commerce Payment with Refund Compensation & Zod Validation
Example 2: Dynamic Routing Slip with ASL Egress Governance
7. Deterministic Testing with @siglo/http/testing
@siglo/http/testing provides createMockHttpServer to deterministically mock outgoing HTTP requests without starting external processes or opening live network sockets:
Testing Rate Limiting with FakeClock
Test 429 Too Many Requests responses with Retry-After: 60 headers instantly using clock and sleep hooks:
