forked from electron/electron-quick-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (77 loc) · 2.43 KB
/
script.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
window.onclick = async ({ target }) => {
const output = document.querySelector("#output")
output.textContent = `Exectuing ${target.getAttribute("data-case")}\n`
switch (target.getAttribute("data-case")) {
case "xhr-string": {
const result = await writeFile("./text/echo", "hello there string")
output.textContent += await formatResult(result)
break
}
case "xhr-binary": {
const encoder = new TextEncoder()
const buffer = encoder.encode("hello there binary")
const result = await writeFile("./binary/echo", buffer)
output.textContent += await formatResult(result)
break
}
case "xhr-blob": {
const blob = new Blob(["hello there blob"], { type: "text/plain" })
const result = await writeFile("./blob/echo", blob)
output.textContent += await formatResult(result)
break
}
case "xhr-form": {
const formData = new FormData()
formData.append("username", "form")
formData.append("accountnum", 123456)
const blob = new Blob(["<b>hello there form</b>"], { type: "text/html" })
formData.append("data.html", blob)
const result = await writeFile("./form/echo", formData)
output.textContent += await formatResult(result)
break
}
case "put-fetch": {
const result = await fetch("./put-fetch/echo", {
method: "PUT",
body: "Hi there fetch API"
})
output.textContent += await formatResult(result)
break
}
case "post-fetch": {
const result = await fetch("./post-fetch/echo", {
method: "POST",
body: "Hi there fetch API"
})
output.textContent += await formatResult(result)
break
}
case "cors-fetch": {
const result = await fetch("bug://corst/fetch/echo", {
method: "PUT",
body: "Hi there CORS fetch API"
})
output.textContent += await formatResult(result)
break
}
default: {
}
}
}
var writeFile = (url, data) =>
new Promise((resolve, reject) => {
const request = new XMLHttpRequest()
request.open("PUT", url, true)
request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8")
request.onerror = () => resolve(request)
request.onloadend = () => resolve(request)
request.send(data)
})
var formatResult = async result => `
------
status: ${result.status}
statusText: ${result.statusText}
body:
${result.text ? await result.text() : result.responseText}
------
`