Skip to main content
Alterations are small functions that take a MessageRequest and return a changed copy. A Processor chains them to prepare a request for its next hop: add Via and Record-Route headers, point the Request-URI at the target, add or remove headers. Each alteration deep-copies the request, so the input is never mutated. Some alterations take the request directly. Others are curried: you call them with a Route (or other arguments) first, and they return a function of the request.

Import

@routr/processor exports the alterations as Alterations and the header helpers as Extensions. The built-in Connect processor composes them with pipe from fp-ts.
The following function is the chain Connect applies to a request routed to its final target (mods/connect/src/tailor.ts).
The Route argument is usually the result of a Location Service lookup. Its relevant fields are user, host, port, transport, advertisedHost, advertisedPort, edgePortRef, listeningPoints, localnets, externalAddrs, registeredOn, and headers.

Summary

Request-URI

fixRequestURI

Sets the Request-URI host, port, and transport parameter to the route’s host, port, and transport. When the route came from a registration (registeredOn is set) and has a user, it also sets the Request-URI user.

Via

addSelfVia

Adds a Via header for this EdgePort at the top of the list, with the rport flag. It picks the EdgePort interface that faces the route’s target. When the route lives on the same EdgePort as the request, it uses the route’s network data; otherwise it uses the request’s.

addSelfViaUsingTheRouteHeaders

Adds a top Via built from the request’s Route headers: the only Route header, or the second one when there are several. The transport defaults to UDP. Returns the request unchanged when it has no Route headers.

addSelfViaUsingExternalAddrs

Experimental. Adds a top Via for the EdgePort interface that faces the Request-URI, taking external addresses into account. Connect uses it for BYE and ACK.

removeTopVia

Removes the first Via header. Connect uses it on responses before it forwards them.

Route

addRouteToNextHop

Adds a loose Route header (lr) for the route’s host, port, and transport at the top of the list. Does nothing when the Request-URI host already equals the route host.

addRouteToPeerEdgePort

Adds a loose Route header that points to the EdgePort holding the target’s connection, using the route’s listeningPoints, localnets, and externalAddrs. Use it with addXEdgePortRef when the target is connected to another EdgePort. See EdgePort cooperation.

removeSelfRoutes

Removes Route headers that point to this EdgePort: those whose host is one of the EdgePort’s local network IPs or external addresses and whose port is the EdgePort’s listening port for that transport.

Record-Route

addSelfRecordRoute

Adds a Record-Route header for this EdgePort so later requests in the dialog pass through it. When the interface the request arrived on differs from the one that faces the target (by host, port, or transport), it adds two Record-Route headers, both with r2=on.

Headers

applyXHeaders

Applies the route’s headers list of header modifiers. Each modifier has a name, an optional value, and an action of HeaderModifierAction.ADD or HeaderModifierAction.REMOVE. It removes every header named in a REMOVE modifier. For each ADD, it removes any existing header with that name and adds the new value. Names are matched case-insensitively.

addXEdgePortRef

Adds an X-Edgeport-Ref header with the request’s edgePortRef at the top of the extension headers. The receiving EdgePort uses it to tell the request came from a peer EdgePort. See SIP headers.

removeXEdgePortRef

Removes every X-Edgeport-Ref header, matched case-insensitively.

removeHeaders

Removes every extension header whose name is in names, matched case-insensitively. Returns the request unchanged when names is empty.

removeAuthorization

Deletes the Authorization header, so the caller’s credentials aren’t forwarded.

decreaseMaxForwards

Subtracts one from Max-Forwards, when the header is present.

Contact

fixInvalidContact

When the Contact host contains .invalid, as WebRTC clients often send, replaces it with the top Via’s received value (or host), the rport value (or port), and the Via transport.

overwriteContactWithSenderInfo

Sets the Contact host, port, and transport to the request’s sender values, when a Contact header is present. Connect does not apply it by default.

Numbers

enforceE164

When enforce is true, tries to rewrite the From, To, and Request-URI users as E.164 numbers (with a leading +). It replaces a value only when it parses as a valid number. validateMobilePrefix applies to the To and Request-URI users; the From user is always checked with mobile prefix validation. Returns the request unchanged when enforce is false.

Header helpers (Extensions)

Extensions holds helpers that read and change extension headers. Unlike alterations, they take the header data as a second argument. getHeaderValue matches names case-insensitively; the others match the exact name. The following snippet reads a header and adds another.

Build a processor

Write a Processor that uses these alterations.

Message flow

Where Processors and alterations fit in a request’s path.