UNPKG

1.9 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Class describing a received SSDP message.
5 */
6class Message {
7 constructor(remoteAddress, message) {
8 this.remoteAddress = remoteAddress;
9 this.headers = {};
10 this.parseHeaders(message);
11 }
12 /**
13 * Gets the HTTP method.
14 */
15 get method() {
16 return this.headers['method'];
17 }
18 /**
19 * Gets the URL to the UPnP description of the root device.
20 */
21 get location() {
22 return this.getHeaderValue('LOCATION');
23 }
24 /**
25 * Gets the Unique Service Name (USN) header.
26 */
27 get usn() {
28 return this.getHeaderValue('USN');
29 }
30 /**
31 * Gets the Notification Type (NT) header.
32 */
33 get nt() {
34 return this.getHeaderValue('NT');
35 }
36 /**
37 * Gets the Notification Sub Type (NTS).
38 */
39 get nts() {
40 return this.getHeaderValue('NTS');
41 }
42 parseHeaders(message) {
43 const headers = message
44 .toString()
45 .trim()
46 .split('\r\n');
47 const method = headers.shift();
48 if (method === undefined) {
49 throw new Error('SSDP message is not specifying the method.');
50 }
51 this.headers['method'] = method;
52 for (const header of headers) {
53 const indexOfValueSeparator = header.indexOf(':');
54 const name = header.slice(0, indexOfValueSeparator).trim();
55 const value = header.slice(indexOfValueSeparator + 1, header.length).trim();
56 this.headers[name] = value;
57 }
58 }
59 getHeaderValue(headerName) {
60 const headerValue = this.headers[headerName];
61 if (!headerValue) {
62 throw new Error(`Header with name ${headerName} does not exist.`);
63 }
64 return headerValue;
65 }
66}
67exports.Message = Message;
68//# sourceMappingURL=Message.js.map
\No newline at end of file