forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLHttpRequestSyncWorker.ts
73 lines (61 loc) · 1.58 KB
/
XMLHttpRequestSyncWorker.ts
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
import Window from '../window/Window';
import * as util from 'util';
const window = new Window();
const xhr = new window.XMLHttpRequest();
const chunks = [];
process.stdin.on('data', (chunk) => {
chunks.push(chunk);
});
process.stdin.on('end', () => {
const buffer = Buffer.concat(chunks);
const serials = JSON.parse(buffer.toString());
if (serials.body && serials.body.type === 'Buffer' && serials.body.data) {
serials.body = Buffer.from(serials.body.data);
}
if (serials.origin) {
window.location.href = serials.origin;
}
if (serials.cookie) {
window.document.cookie = serials.cookie;
}
xhr.overrideMimeType(serials.mimeType);
xhr.open(serials.method, serials.uri, true, serials.user, serials.password);
if (serials.headers) {
Object.keys(serials.headers).forEach((key) => {
xhr.setRequestHeader(key, serials.headers[key]);
});
}
xhr.timeout = serials.timeout;
try {
xhr.addEventListener('loadend', () => {
if (xhr._syncGetError()) {
const err = xhr._syncGetError();
xhr._syncSetErrorString(err.stack || util.inspect(err));
}
process.stdout.write(
JSON.stringify({
responseURL: xhr.responseUrl,
responseText: xhr.responseText,
status: xhr.status,
statusText: xhr.statusText,
}),
() => {
process.exit(0);
}
);
});
xhr.send(serials.body);
} catch (error) {
// Properties.error += error.stack || util.inspect(error);
process.stdout.write(
JSON.stringify({
responseURL: xhr.responseUrl,
status: xhr.status,
statusText: xhr.statusText
}),
() => {
process.exit(0);
}
);
}
});