src/upnp/ directory contains two modules that implement the two protocol layers the server relies on: SSDP for discovery over UDP, and SOAP for control over HTTP.
SSDP module (src/upnp/ssdp.ts)
Class hierarchy
SsdpMessageOptions:
SsdpIncomingMessage
Represents a message received from a UPnP control point (e.g. anM-SEARCH discovery request).
- Passing a
Bufferparses the raw UDP datagram: the first line is split to extractmethod,path, andversion; subsequent lines separated by\r\nare parsed asHeader: Valuepairs. - Passing an options object constructs the message directly from the provided fields.
SsdpOutgoingMessage
Represents a response sent back to a control point.The serialize() method
All three classes implement serialize(), which builds a Buffer from the message’s headers and body:
- The status/request line is prepended (class-specific format).
- Each header is written as
Name: Value\r\n. - A blank
\r\nseparates headers from the body. - The body
Bufferis appended.
Exported socket instance
server.ts attaches a 'message' listener to it.
SOAP module (src/upnp/soap.ts)
SoapRequest class
from static factory method abstracts the async work of reading an HTTP request body:
- Collects all chunks with
request.toArray()and concatenates them into a singleBuffer. - Reads the
SOAPActionHTTP header to identify which UPnP action was invoked. - Parses the XML body using Cheerio (
load(buffer.toString())), exposing it as a jQuery-likeCheerioAPIonbody.
Exported HTTP server
http.Server already listening on port 8080. server.ts attaches a 'request' listener to it.
Router (src/router.ts)
The router is a plain nested object — no framework required:
server.ts dispatches to it with a simple two-level lookup:
Routes
| Method | Path | Description |
|---|---|---|
GET | /description.xml | Renders the UPnP device description XML via description.edge |
POST | /upnp/control/ContentDirectory | Handles ContentDirectory SOAP actions (Browse, GetSortCapabilities, etc.) |
POST | /upnp/event/ContentDirectory | Handles UPnP event subscription requests |