-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebcomponent.js
More file actions
219 lines (192 loc) · 7.27 KB
/
webcomponent.js
File metadata and controls
219 lines (192 loc) · 7.27 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Client } from "./eaas-client.js";
import {
InputBuilder,
InputContentBuilder,
MachineComponentBuilder,
} from "./lib/componentBuilder.js";
import { ClientOptions, NetworkConfig } from "./lib/clientOptions.js";
import { NetworkBuilder } from "./lib/networkBuilder.js";
import { _fetch } from "./lib/util.js";
const kbLayoutPrefs = {
language: { name: "us", description: "English (US)" },
layout: {
name: "pc101",
description: "Generic 101-key PC",
vendor: "Generic",
},
};
const containerName = "emulator-container";
class EaasClientElement extends HTMLElement {
constructor() {
super();
if (this.hasAttribute("autoplay")) this.play();
}
async pressKey(key, keyCode = key.toUpperCase().charCodeAt(0), {altKey, ctrlKey, metaKey, timeout = 100} = {})
{
const el = document.getElementById(containerName).firstElementChild;
if (ctrlKey) {
el.dispatchEvent(new KeyboardEvent("keydown", {key: "Control", keyCode: 17, bubbles: true}));
await new Promise(r => setTimeout(r, timeout));
}
if (altKey) {
el.dispatchEvent(new KeyboardEvent("keydown", {key: "Alt", keyCode: 18, bubbles: true}));
await new Promise(r => setTimeout(r, timeout));
}
el.dispatchEvent(new KeyboardEvent("keydown", {key, keyCode, ctrlKey, altKey, metaKey, bubbles: true}));
await new Promise(r => setTimeout(r, timeout));
el.dispatchEvent(new KeyboardEvent("keyup", {key, keyCode, ctrlKey, altKey, metaKey, bubbles: true}));
if (altKey) {
await new Promise(r => setTimeout(r, timeout));
el.dispatchEvent(new KeyboardEvent("keyup", {key: "Alt", keyCode: 18, bubbles: true}));
}
if (ctrlKey) {
await new Promise(r => setTimeout(r, timeout));
el.dispatchEvent(new KeyboardEvent("keyup", {key: "Control", keyCode: 17, bubbles: true}));
}
}
async play() {
const container = document.createElement("div");
container.id = containerName;
container.style.position = "relative";
container.style.zIndex = "0";
this.append(container);
this.container = container;
this.backendUrl = this.getAttribute("eaas-service");
const { backendUrl } = this;
this.client = new Client(backendUrl, undefined, { kbLayoutPrefs });
const { client } = this;
this.style.display = "block";
this.style.width = "min-content";
let envId = this.getAttribute("environment-id");
const imageArchive = this.getAttribute("image-archive") || "public";
const enableInternet = this.hasAttribute("enable-internet");
const internetDate = this.getAttribute("internet-date");
const networkId = this.getAttribute("network-id");
const networkLabel = this.getAttribute("network-label");
const containerId = this.getAttribute("container-id")?.match(/\/?([^/]+)$/)[1];
const xpraEncoding = this.getAttribute("xpra-encoding");
if(containerId && !envId)
envId = this.getAttribute("container-runtime-id");
const networkName =
this.getAttribute("network-name") ?? `network-${Math.random()}`;
const machine = new MachineComponentBuilder(envId, imageArchive);
machine.setInteractive(true);
if (containerId)
machine.setLinuxRuntime({
userContainerEnvironment: containerId,
userContainerArchive: imageArchive,
});
this.client.onEmulatorStopped = async () => {
let result = await this.client.release();
console.log(result);
for (const download of downloads) {
const a = document.createElement("a");
a.href = result.url;
a.append(...download.childNodes);
download.append(a);
download.hidden = false;
this.append(download);
}
};
const userMedia = [...this.querySelectorAll("eaas-medium")];
if(userMedia.length)
{
for (const medium of userMedia) {
const url = medium.getAttribute("source-url");
const type = medium.getAttribute("type");
const objectId = medium.getAttribute("object-id");
const objectArchive = medium.getAttribute("object-archive");
if(url && type)
machine.addUserMedia(url, type);
else if(objectId)
machine.setObject(objectId, objectArchive || "zero conf");
}
}
const downloads = [...this.querySelectorAll("eaas-result")];
for (const download of downloads) {
download.hidden = true;
}
let inputMedia;
const inputs = [...this.querySelectorAll("eaas-input")];
if (inputs.length) {
inputMedia = new InputBuilder("/input");
for (const input of inputs) {
const contentBuilder = new InputContentBuilder(
input.getAttribute("href")
);
contentBuilder.setName(input.getAttribute("name"));
const format = input.getAttribute("format");
if (format) {
contentBuilder.setAction("extract");
contentBuilder.setCompressionFmt(format);
} else {
contentBuilder.setAction("copy");
}
inputMedia.addContent(contentBuilder);
}
}
if (inputMedia) machine.addInputMedia(inputMedia);
const monitorStateChanges = async () => {
let oldState;
while (true) {
const state = (
await this.session.getEmulatorState()
).state.toLowerCase();
if (state !== oldState) {
this.dispatchEvent(new CustomEvent(state));
}
oldState = state;
console.log({ state });
await new Promise((r) => setTimeout(r, 1000));
}
};
let clientOptions = new ClientOptions();
let components = [machine];
if (enableInternet) {
const networkBuilder = new NetworkBuilder(this.backendUrl);
networkBuilder.addComponent(machine);
clientOptions = await networkBuilder.getDefaultClientOptions();
clientOptions.getNetworkConfig().enableInternet(true);
if(!internetDate)
{
clientOptions.getNetworkConfig().enableSlirpDhcp(true);
}
else {
console.log(internetDate);
networkBuilder.getNetworkConfig().archived_internet_date = internetDate;
await networkBuilder.enableDhcpService(networkBuilder.getNetworkConfig());
}
components = await networkBuilder.getComponents();
}
if (networkId) {
const netUtils = new NetworkBuilder(backendUrl);
const session = await netUtils.connectNetworkSession(
client,
networkId,
networkName
);
const realSession = await _fetch(
`${client.API_URL}/sessions/${session.id}`,
"GET"
);
client.load(realSession);
const { componentId } = client.network.networkConfig.components.find(
(e) => e.networkLabel === networkLabel
);
const componentSession = client.getSession(componentId);
this.session = componentSession;
} else {
if (xpraEncoding) clientOptions.setXpraEncoding(xpraEncoding);
await client.start(components, clientOptions);
}
await client.connect(container);
if(this.session == null)
this.session = client.getActiveSession();
if (this.session.hasPointerLock()) this.session.setPointerLock();
for (const el of [...this.childNodes])
if (el !== this.container) el.remove();
monitorStateChanges();
}
attributeChangedCallback() {}
}
customElements.define("eaas-environment", EaasClientElement);