summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/xhr_worker.js
blob: 46edd700db762d4da265ecc53ae65ca95c028925 (plain)
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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
var xhr = new XMLHttpRequest();

function onload(event) {
  if (event.target != xhr) {
    throw new Error("onload event.target != xhr");
  }

  if (event.target.status != 200) {
    const message = { type: "error", error: event.target.status };
    postMessage(message);
  }

  const message = { type: "load", data: xhr.responseText };
  postMessage(message);
}

xhr.onload = onload;
xhr.addEventListener("load", onload);
xhr.removeEventListener("load", onload);
if (!xhr.onload) {
  const message = { type: "error", error: "Lost message listener!" };
  postMessage(message);
}

xhr.onerror = function (event) {
  if (event.target != xhr) {
    throw new Error("onerror event.target != xhr");
  }
  var message = { type: "error", error: event.target.status };
  postMessage(message);
};
// eslint-disable-next-line no-self-assign
xhr.onerror = xhr.onerror;
// eslint-disable-next-line no-self-compare
if (!xhr.onerror || xhr.onerror != xhr.onerror) {
  throw new Error("onerror wasn't set properly");
}

function onprogress(event) {
  if (event.target != xhr) {
    throw new Error("onprogress event.target != xhr");
  }
  const message = {
    type: "progress",
    current: event.loaded,
    total: event.total,
  };
  postMessage(message);
}
xhr.addEventListener("progress", onprogress);

xhr.addEventListener("foopety", function (event) {});
xhr.removeEventListener("doopety", function (event) {});

xhr.onloadend = function (event) {
  const message = { type: "loadend" };
  postMessage(message);
};

var upload = xhr.upload;
upload.onprogress = function (event) {};
upload.addEventListener("foo", function (event) {});
upload.removeEventListener("foo", function (event) {});
upload.addEventListener("load", function (event) {});
upload.removeEventListener("foo", function (event) {});
upload.onload = function (event) {
  const message = { type: "upload.load" };
  postMessage(message);
};

onmessage = function (event) {
  if (xhr.DONE != 4 || XMLHttpRequest.DONE != 4) {
    throw new Error("xhr constants not correct!");
  }
  if (xhr.readystate > xhr.UNSENT) {
    throw new Error("XHR already running!");
  }
  xhr.open("POST", event.data);
  xhr.send("Data to send");
};