-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbrowser_repl.js
More file actions
206 lines (168 loc) · 5.61 KB
/
Copy pathbrowser_repl.js
File metadata and controls
206 lines (168 loc) · 5.61 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
// This REPL evaluates JavaScript source code in a browser environment.
/*jslint node */
import make_repl from "./repl.js";
import make_webl_server from "./webl/webl_server.js";
function make_browser_repl(
capabilities,
port,
hostname = "localhost",
padawan_type = "top",
humanoid = false
) {
// The 'make_browser_repl' function takes several parameters:
// capabilities
// An object containing the standard Replete capability functions.
// port
// The port number of the WEBL server. If undefined, an unallocated
// port will be chosen automatically.
// hostname
// The hostname of the WEBL server.
// padawan_type
// The type of the padawan, see ./webl/webl.js.
// humanoid
// A boolean indicating whether to use C3PO as a favicon, rather than
// R2D2.
// Configure the WEBL server.
let clients = [];
let padawans = new WeakMap();
function create_padawan(client) {
const padawan = client.padawan({
on_log(...strings) {
return capabilities.out(strings.join(" ") + "\n");
},
on_exception(string) {
return capabilities.err(string + "\n");
},
type: padawan_type,
// If the padawan is rendered as an iframe, it fills the WEBL client's
// viewport. We set block display to avoid vertical scrolling.
iframe_style_object: {
border: "none",
width: "100vw",
height: "100vh",
display: "block"
},
iframe_sandbox: false
});
padawans.set(client, padawan);
return padawan.create().catch(function (exception) {
return capabilities.err(exception.stack + "\n");
});
}
function on_client_found(client) {
capabilities.out("WEBL found.\n");
clients.push(client);
// Create a single padawan on each connecting client.
return create_padawan(client);
}
function on_client_lost(client) {
capabilities.out("WEBL lost.\n");
// Forget the client.
clients = clients.filter(function (a_client) {
return a_client !== client;
});
}
function webl_href() {
return "http://" + (
// IPv6 addresses must be wrapped in square brackets to appear in a URL.
hostname.includes(":")
? "[" + hostname + "]"
: hostname
) + ":" + port;
}
let webl_server;
let repl;
function on_start() {
webl_server = make_webl_server(
function on_exception(error) {
return capabilities.err(error.stack + "\n");
},
on_client_found,
on_client_lost,
function on_request(req, res) {
return repl.serve(
new URL(req.url, "http://" + req.host).href,
req.headers
).then(function ({body, headers}) {
Object.entries(headers).forEach(function ([key, value]) {
res.setHeader(key, value);
});
res.end(body);
}).catch(function fail(reason) {
capabilities.err(reason.stack + "\n");
res.statusCode = 500;
return res.end();
});
},
humanoid
);
return webl_server.start(port, hostname).then(function (actual_port) {
port = actual_port;
capabilities.out(
"Waiting for WEBL: " + webl_href() + "\n"
);
});
}
function on_stop() {
return webl_server.stop();
}
function on_eval(
on_result,
produce_script,
dynamic_specifiers,
import_specifiers,
wait
) {
// Evaluates the module in many padawans at once. Results are reported back as
// they arrive.
if (clients.length === 0) {
return Promise.reject(new Error(
"No WEBLs connected. Open " + webl_href() + " in a browser."
));
}
return Promise.all(
clients.map(function (client) {
function qualify(specifier) {
// Generally, padawans have a different origin to that of the WEBL client. This
// means that absolute paths might be resolved against an unexpected origin. To
// avoid this hazard, each absolute path is converted to a fully-qualified URL
// by prepending the client's origin.
return (
specifier.startsWith("/")
? client.origin + specifier
: specifier
);
}
return padawans.get(client).eval(
produce_script(dynamic_specifiers.map(qualify)),
import_specifiers.map(qualify),
wait
).then(function (report) {
return on_result(report.evaluation, report.exception);
});
})
);
}
function specify(locator) {
// If the locator is a file URL, we convert it to an absolute path. This is then
// fully qualified in 'on_eval' above.
return (
locator.startsWith("file:///")
? locator.replace("file://", "")
: locator
);
}
repl = make_repl(
capabilities,
on_start,
on_eval,
on_stop,
specify
);
return Object.freeze({
start: repl.start,
send: repl.send,
stop: repl.stop
});
}
export default Object.freeze(make_browser_repl);