-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket.js
73 lines (67 loc) · 1.7 KB
/
packet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* [Packet description]
* @param {[type]} method [description]
* @param {[type]} headers [description]
* @rfc https://tools.ietf.org/html/draft-cai-ssdp-v1-03
*/
function Packet(method, headers, path){
this.path = path || '*';
this.method = method || Packet.METHODS.SEARCH;
this.headers = {
MAN: 'ssdp:discover'
};
for(var header in headers){
this.headers[ header ] = headers[ header ];
}
return this;
}
Packet.METHODS = {
SEARCH: 'M-SEARCH',
NOTIFY: 'NOTIFY'
};
/**
* [escape description]
* @param {[type]} str [description]
* @return {[type]} [description]
*/
Packet.escape = function(str){
if(/[:|\s]/.test(str))
return '"' + str + '"';
return str;
};
/**
* [parse description]
* @param {[type]} data [description]
* @return {[type]} [description]
*/
Packet.parse = function(data){
var response = new Packet();
var lines = data.toString().split('\r\n');
var message = lines.shift();
var status = message.split(' ');
lines.filter(Boolean).forEach(function(line){
const [ _, name, value ] = line.match(/(\w+):\s+?(.*)/);
response.headers[name] = value;
});
response.statusCode = status[0];
response.statusText = status[1];
response.version = status[2];
return response;
};
/**
* [toBuffer description]
* @return {[type]} [description]
*/
Packet.prototype.toBuffer = function(){
const EOL = '\r\n';
const { method, path } = this;
let request = `${method} ${path} HTTP/1.1${EOL}`;
Object.keys(this.headers).forEach(name => {
const value = Packet.escape(this.headers[ name ]);
request += `${name}: ${value}${EOL}`;
});
request += EOL;
request += EOL;
return Buffer.from(request);
};
module.exports = Packet;