webc 11.0.0

WebContainer implementation for wapm.io
Documentation
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Regression tests to make sure `wapm-targz-to-pirita` conversion will produce
//! the same results as it has in the past.

#![cfg(feature = "v2")]

#[macro_use]
extern crate pretty_assertions;

use std::{collections::BTreeMap, panic::Location, path::Path};

use bytes::Bytes;
use indexmap::IndexMap;
use webc::{
    Container, PathSegments, Version,
    compat::{Metadata, Volume},
    metadata::{
        Manifest, UrlOrManifest,
        annotations::{Atom, Emscripten, Wasi},
    },
};

const WEBC_V1: &[u8] = include_bytes!("./fixtures/bindings-package-test-3.webc");
const WEBC_V2: &[u8] = include_bytes!("./cowsay.webc");

#[test]
#[cfg_attr(not(feature = "v1"), ignore)]
fn read_webc_v1() {
    assert_eq!(webc::detect(WEBC_V1).unwrap(), Version::V1);

    let webc = Container::from_bytes_and_version(Bytes::from(WEBC_V1), Version::V1).unwrap();

    // ciborium and serde_json have different orderings so we serialize-deserialize again
    // using serde_json to match the expected ordering of fields.
    let webc_manifest = webc.manifest();
    let webc_manifest: Manifest =
        serde_json::from_value(serde_json::to_value(webc_manifest).unwrap()).unwrap();

    // Make sure we get the following manifest
    let expected_manifest: Manifest = serde_json::from_value(serde_json::json! {
        {
            "atoms": {
              "entry": {
                "kind": "https://webc.org/kind/wasm",
                "signature": "sha256:l6BDeuPzv/vsgXIMfGs+mg3k6raif/fJUen0XVzIjTA="
              }
            },
            "bindings": [
              {
                "annotations": {
                  "wit": {
                    "exports": "metadata://./bindings.wit",
                    "module": "atoms://entry"
                  }
                },
                "kind": "wit@0.1.0",
                "name": "library-bindings"
              }
            ],
            "package": {
              "wapm": {
                "description": "Package description for bindings-package-test-3",
                "license": "ISC",
                "name": "bindings-package-test-3",
                "version": "1.0.0"
              }
            }
          }
    })
    .unwrap();
    assert_eq!(&webc_manifest, &expected_manifest);

    let atoms = webc.atoms();
    let atom_names: Vec<_> = atoms.keys().map(|s| s.as_str()).collect();
    assert_eq!(atom_names, ["entry"]);

    let volumes = webc.volumes();
    let volume_names: Vec<_> = volumes.keys().map(|s| s.as_str()).collect();
    assert_eq!(volume_names, ["atom", "metadata"]);

    let atom_volume = &volumes["atom"];
    assert_eq!(atom_volume.read_dir("/").unwrap().len(), 2);
    assert_eq!(atom_volume.read_file("bindings.wit").unwrap().0.len(), 36);
    assert_eq!(atom_volume.read_file("entry.wat").unwrap().0.len(), 110);

    record_snapshots(&webc);
}

#[test]
#[cfg_attr(not(feature = "v2"), ignore)]
fn read_webc_v2() {
    assert_eq!(webc::detect(WEBC_V2).unwrap(), Version::V2);

    let webc = Container::from_bytes_and_version(Bytes::from(WEBC_V2), Version::V2).unwrap();

    // ciborium and serde_json have different orderings so we serialize-deserialize again
    // using serde_json to match the expected ordering of fields.
    let webc_manifest = webc.manifest();
    let webc_manifest: Manifest =
        serde_json::from_value(serde_json::to_value(webc_manifest).unwrap()).unwrap();

    let expected_manifest: Manifest = serde_json::from_value(serde_json::json!(
      {
        "package": {
          "wapm": {
            "name": "wiqar/cowsay",
            "readme": {
              "path": "README.md",
              "volume": "metadata"
            },
            "version": "0.3.0",
            "repository": "https://github.com/wapm-packages/cowsay",
            "description": "cowsay is a program that generates ASCII pictures of a cow with a message"
          }
        },
        "atoms": {
          "cowsay": {
            "kind": "https://webc.org/kind/wasm",
            "signature": "sha256:DPmhiSNXCg5261eTUi3BIvAc/aJttGj+nD+bGhQkVQo="
          }
        },
        "commands": {
          "cowsay": {
            "runner": "https://webc.org/runner/wasi",
            "annotations": {
              "wasi": {
                "atom": "cowsay",
                "package": null,
                "main_args": null
              }
            }
          },
          "cowthink": {
            "runner": "https://webc.org/runner/wasi",
            "annotations": {
              "wasi": {
                "atom": "cowsay",
                "package": null,
                "main_args": null
              }
            }
          }
        }
      }
    )).unwrap();
    assert_eq!(&webc_manifest, &expected_manifest);

    let atoms = webc.atoms();
    let atom_names = atoms.keys().collect::<Vec<_>>();
    assert_eq!(atom_names, ["cowsay"]);
    assert!(webc.get_atom("cowsay").unwrap().starts_with(b"\0asm"));

    let volumes = webc.volumes();
    let volume_names = volumes.keys().collect::<Vec<_>>();
    assert_eq!(volume_names, ["atom", "metadata"]);

    let atom_volume = webc.get_volume("atom").unwrap();
    assert_eq!(atom_volume.read_dir("/").unwrap().len(), 0);

    let metadata_volume = webc.get_volume("metadata").unwrap();
    assert_eq!(
        metadata_volume.read_file("/README.md").unwrap().0.len(),
        2542
    );
    assert_eq!(metadata_volume.read_file("/LICENSE").unwrap().0.len(), 1070);

    record_snapshots(&webc);
}

#[test]
fn container_unpack() {
    let dir = tempfile::tempdir().unwrap();
    let webc = Container::from_bytes_and_version(Bytes::from(WEBC_V2), Version::V2).unwrap();
    webc.unpack(dir.path(), false).unwrap();

    assert_eq!(
        dir.path()
            .join("metadata/README.md")
            .metadata()
            .unwrap()
            .len(),
        2542
    );

    let manifest_path = dir.path().join("manifest.json");
    let manifest_data = std::fs::read_to_string(manifest_path).unwrap();
    let manifest = serde_json::from_str::<Manifest>(&manifest_data).unwrap();
    assert_eq!(&manifest, webc.manifest(),);
}

#[test]
fn container_unpack_overwrite() {
    let dir = tempfile::tempdir().unwrap();

    std::fs::write(dir.path().join("hello"), "123").unwrap();

    let webc = Container::from_bytes_and_version(Bytes::from(WEBC_V2), Version::V2).unwrap();
    let res = webc.unpack(dir.path(), false);
    assert!(res.is_err(), "should not unpack into non-empty directory");

    // Should ignore the existing file with overwrite=true.
    webc.unpack(dir.path(), true).unwrap();
    assert_eq!(
        dir.path()
            .join("metadata/README.md")
            .metadata()
            .unwrap()
            .len(),
        2542
    );
}

// Right now backend is using webc v2 so the latest version of webc (ie. v3)
// is incompatible with what backend returns. Once backend transitions to v3,
// this should be re-enabled and pass.
#[ignore = "we have a breaking change in FileSystemMap"]
#[test]
fn website_with_static_web_server_dependency() {
    let (_tarball, mut manifest) = fixtures::load("wasmer", "docs", "0.15.1");
    patch_up_dependencies(&mut manifest);

    // let pkg = Package::from_tarball_file(tarball).unwrap();

    // assert_eq!(pkg.manifest(), &manifest);
    // insta::assert_yaml_snapshot!(pkg.manifest());
}

/// When wasmer-toml went to 0.7.0, it switched from using a bare `String` to
/// a `semver::VersionReq` for dependency versions. This function will patch
/// up old manifests so you get the `^` in
/// `coreutils: sharrattj/coreutils@^1.0.16`.
///
/// When we merged pirita#178, we changed things so `"wasmer/python" = "1.2.3"`
/// would be translated to `"wasmer/python": "wasmer/python@1.2.3"` instead of
/// `"python": "wasmer/python@1.2.3"` (i.e. the full package name is used as the
/// dependency alias, rather than just the name section).
fn patch_up_dependencies(manifest: &mut Manifest) {
    for (_, dep) in &mut manifest.use_map {
        if let UrlOrManifest::RegistryDependentUrl(url) = dep {
            let (name, version) = url.split_once('@').unwrap();
            if !version.starts_with('^') {
                *url = format!("{name}@^{version}");
            }
        }
    }

    let aliases_to_rename: Vec<_> = manifest
        .use_map
        .iter()
        .filter_map(|(alias, dep)| match dep {
            UrlOrManifest::RegistryDependentUrl(url) => {
                let (name, _) = url.split_once('@').unwrap();

                if name != alias {
                    Some((alias.clone(), name.to_string()))
                } else {
                    None
                }
            }
            _ => None,
        })
        .collect();
    for (old_alias, new_alias) in aliases_to_rename {
        let value = manifest.use_map.swap_remove(&old_alias).unwrap();
        manifest.use_map.insert(new_alias, value);
    }
}

// Right now backend is using webc v2 so the latest version of webc (ie. v3)
// is incompatible with what backend returns. Once backend transitions to v3,
// this should be re-enabled and pass.
#[ignore = "we have a breaking change in FileSystemMap"]
#[test]
fn static_web_server() {
    let (_tarball, mut manifest) = fixtures::load("sharrattj", "static-web-server", "1.0.96");
    patch_up_commands(&mut manifest.commands);
    patch_up_wapm_metadata(manifest.package.get_mut("wapm").unwrap());

    // let pkg = Package::from_tarball_file(tarball).unwrap();

    // assert_eq!(pkg.manifest(), &manifest);
    // insta::assert_yaml_snapshot!(pkg.manifest());
}

/// Patch up `"wapm"` annotations so that they use absolute paths when referring
/// to a volume-specific item.
fn patch_up_wapm_metadata(wapm: &mut ciborium::Value) {
    let map = match wapm {
        ciborium::Value::Map(map) => map,
        _ => return,
    };

    let keys = ["license-file", "readme"];

    for key in keys {
        let key = ciborium::Value::Text(key.to_string());
        let path = ciborium::Value::Text("path".to_string());

        let mut value = match map.iter_mut().find(|e| e.0 == key).map(|e| e.1.clone()) {
            Some(ciborium::Value::Map(v)) => v,
            _ => continue,
        };
        match value.iter_mut().find(|e| e.0 == path).map(|e| e.1.clone()) {
            Some(ciborium::Value::Text(mut s)) if !s.starts_with('/') => {
                s.insert(0, '/');
            }
            _ => continue,
        }
    }
}

/// Patch up minor differences between current and previous versions of
/// `wapm-targz-to-pirita`.
#[allow(deprecated)]
fn patch_up_commands(commands: &mut IndexMap<String, webc::metadata::Command>) {
    for (name, cmd) in commands {
        // We've used several URIs for the WASI runner in the past
        let wasi_command_names = ["https://webc.org/runner/wasi@unstable_"];
        if wasi_command_names.contains(&cmd.runner.as_str()) {
            cmd.runner = webc::metadata::annotations::WASI_RUNNER_URI.to_string();
        }

        if cmd.runner.as_str() == webc::metadata::annotations::WASI_RUNNER_URI
            && !cmd.annotations.contains_key(Wasi::KEY)
        {
            // We now always set "wasi.atom", even if the command has the
            // same name as the module it uses.
            cmd.annotations.insert(
                Wasi::KEY.to_string(),
                ciborium::Value::serialized(&Wasi::new(name)).unwrap(),
            );
        }

        if let Some(Wasi { atom, .. }) = cmd.annotation(Wasi::KEY).unwrap() {
            cmd.annotations.insert(
                Atom::KEY.to_string(),
                ciborium::Value::serialized(&Atom::new(atom, None)).unwrap(),
            );
        }

        if let Some(Emscripten {
            atom: Some(atom), ..
        }) = cmd.annotation(Emscripten::KEY).unwrap()
        {
            cmd.annotations.insert(
                Atom::KEY.to_string(),
                ciborium::Value::serialized(&Atom::new(atom, None)).unwrap(),
            );
        }
    }
}

/// Record a snapshot of the [`Container`]'s contents.
#[track_caller]
fn record_snapshots(container: &Container) {
    let mut settings = insta::Settings::clone_current();
    let location = Location::caller();
    let caller = calling_function(location);

    settings.set_snapshot_suffix(format!("{caller}.manifest"));
    settings.bind(|| insta::assert_yaml_snapshot!(container.manifest()));

    let atoms: BTreeMap<String, usize> = container
        .atoms()
        .into_iter()
        .map(|(k, v)| (k, v.len()))
        .collect();
    settings.set_snapshot_suffix(format!("{caller}.atoms"));
    settings.bind(|| insta::assert_yaml_snapshot!(atoms));

    let volumes: BTreeMap<String, BTreeMap<String, Metadata>> = container
        .volumes()
        .into_iter()
        .map(|(k, volume)| (k, volume_metadata(&volume)))
        .collect();
    settings.set_snapshot_suffix(format!("{caller}.volumes"));
    settings.bind(|| insta::assert_yaml_snapshot!(volumes));
}

/// Determine the name of the calling function based on a [`Location`].
fn calling_function(location: &Location) -> String {
    let filename = Path::new(env!("CARGO_MANIFEST_DIR"))
        .ancestors()
        .find_map(|base_dir| {
            let path = base_dir.join(location.file());
            path.exists().then_some(path)
        })
        .unwrap();
    let src = std::fs::read_to_string(filename).unwrap();

    let lines: Vec<_> = src.lines().take(location.line() as usize).collect();
    let function_line = lines.iter().rfind(|line| line.contains("fn ")).unwrap();
    let line_starting_with_ident = function_line.trim().trim_start_matches("fn ");
    let opening_paren = line_starting_with_ident.find('(').unwrap();

    line_starting_with_ident[..opening_paren].to_string()
}

fn volume_metadata(volume: &Volume) -> BTreeMap<String, Metadata> {
    fn all_files(
        volume: &Volume,
        path: &mut PathSegments,
        metadata: &mut BTreeMap<String, Metadata>,
    ) {
        for (segment, _hash, mut meta) in volume.read_dir(&*path).unwrap() {
            // do not include timestamps in snapshot testing since it will change every time
            // we run the test.

            // Clear timestamps for snapshot testing
            if let Some(timestamps) = meta.timestamps_mut() {
                *timestamps = Default::default();
            }

            path.push(segment);

            if meta.is_dir() {
                all_files(volume, path, metadata);
            }

            metadata.insert(path.to_string(), meta);
            path.pop();
        }
    }

    let mut path = PathSegments::ROOT;
    let mut metadata = BTreeMap::new();

    all_files(volume, &mut path, &mut metadata);

    metadata
}

mod fixtures {
    use std::path::{Path, PathBuf};

    use tempfile::NamedTempFile;

    const GRAPHQL_ENDPOINT: &str = "https://registry.wasmer.io/graphql";
    const QUERY: &str = r#"
{
    getPackageVersion(name: "$NAMESPACE/$PACKAGE", version: "=$VERSION") {
        piritaManifest
        distribution { downloadUrl }
    }
}"#;

    pub fn load(
        namespace: &str,
        package: &str,
        version: &str,
    ) -> (PathBuf, webc::metadata::Manifest) {
        let cache_dir = Path::new(env!("CARGO_TARGET_TMPDIR"))
            .join(env!("CARGO_PKG_NAME"))
            .join(namespace)
            .join(format!("{package}-{version}"));
        let tarball = cache_dir.join(format!("{package}-{version}.tar.gz"));
        let manifest = cache_dir.join("manifest.json");

        if !tarball.exists() {
            #[derive(serde::Serialize)]
            struct Body {
                query: String,
            }

            let body = serde_json::to_string(&Body {
                query: QUERY
                    .replace("$NAMESPACE", namespace)
                    .replace("$PACKAGE", package)
                    .replace("$VERSION", version),
            })
            .unwrap();

            let body = ureq::post(GRAPHQL_ENDPOINT)
                .header("Content-Type", "application/json")
                .send(&body)
                .unwrap()
                .body_mut()
                .read_to_string()
                .unwrap();
            let Response { data, errors } = serde_json::from_str(&body).unwrap();
            if let Some(errors) = errors {
                panic!("One or more errors occurred: {errors}");
            }
            std::fs::create_dir_all(&cache_dir).unwrap();
            let mut temp = NamedTempFile::new_in(&cache_dir).unwrap();
            let response = ureq::get(&data.get_package_version.distribution.download_url)
                .call()
                .unwrap();
            assert_eq!(response.status(), 200);
            let mut reader = response.into_body().into_reader();
            std::io::copy(&mut reader, &mut temp).unwrap();

            temp.persist(&tarball).unwrap();
            std::fs::write(&manifest, &data.get_package_version.pirita_manifest).unwrap();
        }

        let manifest = std::fs::read_to_string(&manifest).unwrap();
        let manifest = serde_json::from_str(&manifest).unwrap();

        (tarball, manifest)
    }

    #[derive(Debug, serde::Deserialize)]
    struct Response {
        data: Data,
        #[serde(default)]
        errors: Option<serde_json::Value>,
    }

    #[derive(Debug, serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Data {
        get_package_version: PackageVersion,
    }

    #[derive(Debug, serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct PackageVersion {
        pirita_manifest: String,
        distribution: Distribution,
    }

    #[derive(Debug, serde::Deserialize)]
    #[serde(rename_all = "camelCase")]
    struct Distribution {
        download_url: String,
    }
}