deno.land / x / wasm@wasmer-sdk-v0.6.0 / src / wasmer.rs

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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::sync::Arc;
use bytes::BytesMut;use futures::{channel::oneshot, TryStreamExt};use js_sys::{JsString, Reflect, Uint8Array};use tracing::Instrument;use virtual_fs::{AsyncReadExt, Pipe};use wasm_bindgen::{prelude::wasm_bindgen, JsValue, UnwrapThrowExt};use wasmer_wasix::{ bin_factory::BinaryPackage, os::{Tty, TtyOptions}, runners::{wasi::WasiRunner, Runner}, runtime::resolver::PackageSpecifier, Runtime as _,};use web_sys::{ReadableStream, WritableStream};
use crate::{ instance::ExitCondition, runtime::Runtime, utils::{Error, GlobalScope}, Instance, JsRuntime, SpawnOptions,};
/// A package from the Wasmer registry.////// @example/// ```ts/// import { Wasmer } from "@wasmer/sdk";////// const pkg = await Wasmer.fromRegistry("wasmer/python");/// const instance = await pkg.entrypoint!.run({ args: ["--version"]});/// const { ok, code, stdout, stderr } = await instance.wait();////// if (ok) {/// console.log(`Version:`, stdout);/// } else {/// throw new Error(`Python exited with ${code}: ${stderr}`);/// }/// ```#[derive(Debug, Clone)]#[wasm_bindgen]pub struct Wasmer { /// The package's entrypoint. #[wasm_bindgen(getter_with_clone)] pub entrypoint: Option<Command>, /// A map containing all commands available to the package (including /// dependencies). #[wasm_bindgen(getter_with_clone)] pub commands: Commands,}
#[wasm_bindgen]impl Wasmer { /// Load a package from the Wasmer registry. #[wasm_bindgen(js_name = "fromRegistry")] pub async fn js_from_registry( specifier: &str, runtime: Option<OptionalRuntime>, ) -> Result<Wasmer, Error> { Wasmer::from_registry(specifier, runtime).await }
/// Load a package from a package file. #[wasm_bindgen(js_name = "fromFile")] pub async fn js_from_file( binary: Uint8Array, runtime: Option<OptionalRuntime>, ) -> Result<Wasmer, Error> { Wasmer::from_file(binary.to_vec(), runtime).await }}
/// The actual impl - with `#[tracing::instrument]` macros.impl Wasmer { #[tracing::instrument(skip(runtime))] async fn from_registry( specifier: &str, runtime: Option<OptionalRuntime>, ) -> Result<Self, Error> { let specifier = PackageSpecifier::parse(specifier)?; let runtime = runtime.unwrap_or_default().resolve()?.into_inner(); let pkg = BinaryPackage::from_registry(&specifier, &*runtime).await?;
Wasmer::from_package(pkg, runtime) }
#[tracing::instrument(skip(runtime))] async fn from_file(binary: Vec<u8>, runtime: Option<OptionalRuntime>) -> Result<Self, Error> { let runtime = runtime.unwrap_or_default().resolve()?.into_inner(); let container = webc::Container::from_bytes(binary)?; let pkg = BinaryPackage::from_webc(&container, &*runtime).await?;
Wasmer::from_package(pkg, runtime) }
fn from_package(pkg: BinaryPackage, runtime: Arc<Runtime>) -> Result<Self, Error> { let pkg = Arc::new(pkg); let commands = Commands::default();
for cmd in &pkg.commands { let name = JsString::from(cmd.name()); let value = JsValue::from(Command { name: name.clone(), runtime: Arc::clone(&runtime), pkg: Arc::clone(&pkg), }); Reflect::set(&commands, &name, &value).map_err(Error::js)?; }
let entrypoint = pkg.entrypoint_cmd.as_deref().map(|name| Command { name: name.into(), pkg: Arc::clone(&pkg), runtime, });
Ok(Wasmer { entrypoint, commands, }) }}
/// A runnable WASIX command.#[derive(Debug, Clone)]#[wasm_bindgen]pub struct Command { #[wasm_bindgen(getter_with_clone)] pub name: JsString, pkg: Arc<BinaryPackage>, runtime: Arc<Runtime>,}
#[wasm_bindgen]impl Command { pub async fn run(&self, options: Option<SpawnOptions>) -> Result<Instance, Error> { let runtime = Arc::clone(&self.runtime); let pkg = Arc::clone(&self.pkg); let tasks = Arc::clone(runtime.task_manager());
let options = options.unwrap_or_default();
let mut runner = WasiRunner::new(); let (stdin, stdout, stderr) = configure_runner(&options, &mut runner, &runtime).await?; let command_name = String::from(&self.name);
tracing::debug!(%command_name, "Starting the WASI runner");
let (sender, receiver) = oneshot::channel();
// Note: The WasiRunner::run_command() method blocks, so we need to run // it on the thread pool. tasks.task_dedicated(Box::new(move || { let result = runner.run_command(&command_name, &pkg, runtime); let _ = sender.send(ExitCondition::from_result(result)); }))?;
Ok(Instance { stdin, stdout, stderr, exit: receiver, }) }
/// Read the binary that will be pub fn binary(&self) -> Uint8Array { let name = String::from(&self.name); let cmd = self.pkg.get_command(&name).unwrap_throw(); Uint8Array::from(cmd.atom()) }}
#[wasm_bindgen]extern "C" { #[wasm_bindgen(typescript_type = "Record<string, Command>", extends = js_sys::Object)] #[derive(Clone, Default, Debug)] pub type Commands;
/// A helper to allow functions to take a `runtime?: Runtime` parameter. #[wasm_bindgen(typescript_type = "Runtime")] pub type OptionalRuntime;}
impl OptionalRuntime { fn resolve(&self) -> Result<JsRuntime, Error> { let js_value: &JsValue = self.as_ref();
if js_value.is_undefined() { Runtime::lazily_initialized().map(JsRuntime::from) } else { let rt = JsRuntime::try_from(js_value).expect_throw("Expected a runtime"); Ok(rt) } }}
impl Default for OptionalRuntime { fn default() -> Self { Self { obj: JsValue::UNDEFINED, } }}
pub(crate) async fn configure_runner( options: &SpawnOptions, runner: &mut WasiRunner, runtime: &Runtime,) -> Result< ( Option<web_sys::WritableStream>, web_sys::ReadableStream, web_sys::ReadableStream, ), Error,> { let args = options.parse_args()?; runner.set_args(args);
let env = options.parse_env()?; runner.set_envs(env);
for (dest, dir) in options.mounted_directories()? { runner.mount(dest, Arc::new(dir)); }
if let Some(uses) = options.uses() { let uses = crate::utils::js_string_array(uses)?; let packages = load_injected_packages(uses, runtime).await?; runner.add_injected_packages(packages); }
let (stderr_pipe, stderr_stream) = crate::streams::output_pipe(); runner.set_stderr(Box::new(stderr_pipe));
let tty_options = runtime.tty_options().clone(); match setup_tty(options, tty_options) { TerminalMode::Interactive { stdin_pipe, stdout_pipe, stdout_stream, stdin_stream, } => { tracing::debug!("Setting up interactive TTY"); runner.set_stdin(Box::new(stdin_pipe)); runner.set_stdout(Box::new(stdout_pipe)); runtime.set_connected_to_tty(true); Ok((Some(stdin_stream), stdout_stream, stderr_stream)) } TerminalMode::NonInteractive { stdin } => { tracing::debug!("Setting up non-interactive TTY"); let (stdout_pipe, stdout_stream) = crate::streams::output_pipe(); runner.set_stdin(Box::new(stdin)); runner.set_stdout(Box::new(stdout_pipe));
// HACK: Make sure we don't report stdin as interactive. This // doesn't belong here because now it'll affect every other // instance sharing the same runtime... In theory, every // instance should get its own TTY state, but that's an issue // for wasmer-wasix to work out. runtime.set_connected_to_tty(false);
Ok((None, stdout_stream, stderr_stream)) } }}
fn setup_tty(options: &SpawnOptions, tty_options: TtyOptions) -> TerminalMode { // Handle the simple (non-interactive) case first. if let Some(stdin) = options.read_stdin() { return TerminalMode::NonInteractive { stdin: virtual_fs::StaticFile::new(stdin), }; }
let (stdout_pipe, stdout_stream) = crate::streams::output_pipe();
// Note: Because this is an interactive session, we want to intercept // stdin and let the TTY modify it. // // To do that, we manually copy data from the user's pipe into the TTY, // then the TTY modifies those bytes and writes them to the pipe we gave // to the runtime. // // To avoid confusing the pipes and how stdin data gets moved around, // here's a diagram: // // --------------------------------- -------------------- ---------------------------- // | stdin_stream (user) u_stdin_rx | --copy--> | (tty) u_stdin_tx | --pipe-> | stdin_pipe (runtime) ... | // --------------------------------- -------------------- ---------------------------- let (u_stdin_rx, stdin_stream) = crate::streams::input_pipe(); let (u_stdin_tx, stdin_pipe) = Pipe::channel();
let tty = Tty::new( Box::new(u_stdin_tx), Box::new(stdout_pipe.clone()), GlobalScope::current().is_mobile(), tty_options, );
// Because the TTY is manually copying between pipes, we need to make // sure the stdin pipe passed to the runtime is closed when the user // closes their end. let cleanup = { let stdin_pipe = stdin_pipe.clone(); move || { tracing::debug!("Closing stdin"); stdin_pipe.close(); } };
// Use the JS event loop to drive our manual user->tty copy wasm_bindgen_futures::spawn_local( copy_stdin_to_tty(u_stdin_rx, tty, cleanup) .in_current_span() .instrument(tracing::debug_span!("tty")), );
TerminalMode::Interactive { stdin_pipe, stdout_pipe, stdout_stream, stdin_stream, }}
fn copy_stdin_to_tty( mut u_stdin_rx: Pipe, mut tty: Tty, cleanup: impl FnOnce(),) -> impl std::future::Future<Output = ()> { /// A RAII guard used to make sure the cleanup function always gets called. struct CleanupGuard<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Drop for CleanupGuard<F> { fn drop(&mut self) { let cb = self.0.take().unwrap(); cb(); } }
async move { let _guard = CleanupGuard(Some(cleanup)); let mut buffer = BytesMut::new();
loop { match u_stdin_rx.read_buf(&mut buffer).await { Ok(0) => { break; } Ok(_) => { // PERF: It'd be nice if we didn't need to do a copy here. let data = buffer.to_vec(); tty = tty.on_event(wasmer_wasix::os::InputEvent::Raw(data)).await; buffer.clear(); } Err(e) => { tracing::warn!( error = &e as &dyn std::error::Error, "Error reading stdin and copying it to the tty" ); break; } } } }}
#[derive(Debug)]enum TerminalMode { Interactive { /// The [`Pipe`] used as the WASIX instance's stdin. stdin_pipe: Pipe, /// The [`Pipe`] used as the WASIX instance's stdout. stdout_pipe: Pipe, /// The [`ReadableStream`] our JavaScript caller will read stdout from. stdout_stream: ReadableStream, /// The [`WritableStream`] our JavaScript caller will write stdin to. stdin_stream: WritableStream, }, NonInteractive { /// The file to use as the WASIX instance's stdin. stdin: virtual_fs::StaticFile, },}
#[tracing::instrument(level = "debug", skip_all)]async fn load_injected_packages( packages: Vec<String>, runtime: &Runtime,) -> Result<Vec<BinaryPackage>, Error> { let futures: futures::stream::FuturesOrdered<_> = packages .into_iter() .map(|pkg| async move { load_package(&pkg, runtime).await }) .collect();
let packages = futures.try_collect().await?;
Ok(packages)}
#[tracing::instrument(level = "debug", skip(runtime))]async fn load_package(pkg: &str, runtime: &Runtime) -> Result<BinaryPackage, Error> { let specifier: PackageSpecifier = pkg.parse()?; let pkg = BinaryPackage::from_registry(&specifier, runtime).await?;
Ok(pkg)}
wasm

Version Info

Tagged at
4 months ago