-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (130 loc) · 3.89 KB
/
Copy pathindex.js
File metadata and controls
165 lines (130 loc) · 3.89 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class Hoster {
constructor(server, rootChannel, options) {
this.server = server;
this.rootChannel = rootChannel;
this.files = {};
let numWorkers = 4;
if (options) {
if (options && options.numWorkers) {
numWorkers = options.numWorkers;
}
}
for (let i = 0; i < numWorkers; i++) {
this.hostFileWorker(i);
}
}
async hostFile(file, path) {
this.files[path] = file;
}
async hostFileWorker(workerId) {
while (true) {
const switchUrl = this.server + '/res' + this.rootChannel + '?switch=true';
const randChan = randomChannelId();
const response = await fetch(switchUrl, {
method: 'POST',
body: randChan,
});
//if (response.status < 200 || response.status > 299) {
// continue;
//}
let reqMethod;
let reqPath;
const reqHeaders = {};
for (const entry of response.headers.entries()) {
const headerName = entry[0];
if (headerName === 'pb-method') {
reqMethod = entry[1];
}
else if (headerName === 'pb-path') {
reqPath = entry[1];
}
else if (headerName.startsWith('pb-h-')) {
reqHeaders[headerName.slice('pb-h-'.length)] = entry[1];
}
}
console.log(workerId, reqMethod, reqPath, reqHeaders);
let sendFile = this.files[reqPath];
const fileUrl = this.server + '/res/' + randChan;
const resHeaders = {};
if (!sendFile) {
resHeaders['Pb-Status'] = '404';
await fetch(fileUrl, {
method: 'POST',
body: "Not found",
headers: resHeaders,
});
continue;
}
// TODO: parse byte range specs properly according to
// https://tools.ietf.org/html/rfc7233
if (reqHeaders.range) {
const range = {};
const right = reqHeaders.range.split('=')[1];
const rangeParts = right.split('-');
range.start = Number(rangeParts[0]);
range.end = sendFile.size - 1;
if (rangeParts[1]) {
// Need to add one because HTTP ranges are inclusive
range.end = Number(rangeParts[1]);
}
const originalSize = sendFile.size;
console.log(sendFile.size, range);
sendFile = sendFile.slice(range.start, range.end + 1);
resHeaders['Pb-H-Content-Range'] = `bytes ${range.start}-${range.end}/${originalSize}`;
resHeaders['Pb-H-Content-Length'] = range.end - range.start + 1;
resHeaders['Pb-Status'] = '206';
}
else {
resHeaders['Pb-H-Content-Length'] = `${sendFile.size}`;
}
resHeaders['Pb-H-Accept-Ranges'] = 'bytes';
console.log(resHeaders);
await new Promise((resolve, reject) => {
const XHR_STATE_DONE = 4;
const xhr = new XMLHttpRequest();
xhr.open('POST', fileUrl + '?register-pulse=true');
for (const header in resHeaders) {
xhr.setRequestHeader(header, resHeaders[header]);
}
xhr.send(sendFile);
fetch(fileUrl + '?check-pulse=true', {
mode: 'no-cors',
})
.then(r => {
console.log("request done");
if (xhr.readyState !== XHR_STATE_DONE) {
xhr.abort();
}
resolve();
})
.catch(e => {
console.error(e);
});
});
}
}
}
function createHoster(server, rootChannel, options) {
return new Hoster(server, rootChannel, options);
}
function randomChannelId() {
const possible = "0123456789abcdefghijkmnpqrstuvwxyz";
function genCluster() {
let cluster = "";
for (let i = 0; i < 32; i++) {
const randIndex = Math.floor(Math.random() * possible.length);
cluster += possible[randIndex];
}
return cluster;
}
let id = "";
id += genCluster();
//id += '-';
//id += genCluster();
//id += '-';
//id += genCluster();
return id;
}
export {
createHoster,
};