This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_new.js
101 lines (86 loc) · 2.7 KB
/
index_new.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const WebSocket = WebSocket||require('ws');
const EventEmitter = require('events');
const myhttp = require('./http');
const mypkt = require('./packet');
function onHttpResponse(head, body) {
var {id, type, details} = head;
var stream = this.streams.get(id);
var {request, response} = stream;
if(type==='http+') {
response = stream.response = new myhttp.IncomingMessage(this, details, id);
request.emit('response', response);
}
if(body) response.emit('data', body);
if(type==='http-') {
this.requests.delete(id);
this.streams.delete(id);
response.emit('end');
}
};
function onHttpRequest(head, body) {
var {id, type, details} = head;
if(!this.streams.has(id)) {
var request = new myhttp.IncomingMessage(this, details, id);
var response = new myhttp.ServerResponse(this, details, id);
this.streams.set(id, {request, response});
this.emit('http', request, response);
}
else {
var {request, response} = this.streams.get(id);
}
if(body) request.emit('data', body);
if(type==='http-') request.emit('end');
};
function onHttp(head, body) {
var {id} = head;
if(this.requests.has(id)) onHttpResponse.call(this, head, body);
else onHttpRequest.call(this, head, body);
};
function end(id) {
if(this.requests.has(id)) return;
this.streams.delete(id);
};
function http(url, options, callback) {
var request = myhttp.request(url, options, callback);
this.requests.add(request.id);
return request;
};
function MyceliumLink(url, protocols) {
var ws = new WebSocket(url, protocols);
var streams = new Map();
ws.onerror = (event) => this.onerror? this.onerror(event):null;
ws.onopen = (event) => this.onopen? this.onopen(event):null;
ws.onclose = (event) => this.onclose? this.onclose(event):null;
ws.onmessage = (event) => {
var {head, body} = mypkt.parse(event.data);
var {type} = head;
if(/^http/.test(type)) onHttp.call(this, head, body);
};
this.socket = ws;
this.streams = streams;
this.onerror = null;
this.onhttp = null;
this.onopen = null;
this.onclose = null;
this.onmessage = null;
};
MyceliumLink.prototype.end = end;
MyceliumLink.prototype.http = http;
/* TEST */
function write(head, body, callback) {
var message = mypkt.stringify(head, body);
console.log('write:', message);
if(callback) callback();
};
function BadConnection() {
this.socket = null;
};
BadConnection.prototype.write = write;
var connection = new BadConnection();
var response = new myhttp.ServerResponse(connection, {}, Math.random());
response.setHeader('Accept', 'nothing');
var accept = response.getHeader('accept');
console.log('accept:', accept);
// response.flushHeaders();
response.write('Hello from ServerResponse!');
response.end('Bye!');