1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 | class Message {
|
7 | constructor(remoteAddress, message) {
|
8 | this.remoteAddress = remoteAddress;
|
9 | this.headers = {};
|
10 | this.parseHeaders(message);
|
11 | }
|
12 | |
13 |
|
14 |
|
15 | get method() {
|
16 | return this.headers['method'];
|
17 | }
|
18 | |
19 |
|
20 |
|
21 | get location() {
|
22 | return this.getHeaderValue('LOCATION');
|
23 | }
|
24 | |
25 |
|
26 |
|
27 | get usn() {
|
28 | return this.getHeaderValue('USN');
|
29 | }
|
30 | |
31 |
|
32 |
|
33 | get nt() {
|
34 | return this.getHeaderValue('NT');
|
35 | }
|
36 | |
37 |
|
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 | }
|
67 | exports.Message = Message;
|
68 |
|
\ | No newline at end of file |