wasiConfigFromPath function

WasiConfig wasiConfigFromPath(
  1. String witPath, {
  2. Map<String, WasiDirectory>? webBrowserFileSystem,
})

Creates a WasiConfig from the given witPath. If witPath is a file, it will use its parent directory as the preopened directory for the WASI config. If witPath is a directory, it will use it as the preopened directory. For web platforms, the witPath will be the directory name used as the root of the web browser file system.

Implementation

WasiConfig wasiConfigFromPath(
  String witPath, {
  Map<String, WasiDirectory>? webBrowserFileSystem,
}) {
  Uri allowedPath = Uri.parse(witPath);
  if (!_isWeb) {
    final type = FileSystemEntity.typeSync(witPath);
    if (type == FileSystemEntityType.notFound) {
      throw Exception('wit file not found: $witPath');
    }

    final allowedDir = type == FileSystemEntityType.file
        ? File(witPath).parent
        : Directory(witPath);
    allowedPath = allowedDir.uri;
  }
  return WasiConfig(
    inheritEnv: true,
    preopenedDirs: [
      PreopenedDir(
        hostPath:
            allowedPath.toFilePath(windows: !_isWeb && Platform.isWindows),
        wasmGuestPath: allowedPath.toFilePath(windows: false),
      ),
    ],
    webBrowserFileSystem: webBrowserFileSystem ?? {},
  );
}