# `Alternera.PluginWasmFramework.Upload`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.0/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/upload.ex#L1)

Takes a compiled module a user uploads into a step: checks it, compiles
it and stores it (`Plugin.Provisional.Files`), so a broken module is
refused in the editor, never on the first request.

The checks, in order, each refused with a text of its own (the
runtime's own reasons go to the log only):

  1. uploads are on for this node (the plugin's `upload_settings/0`),
     and a user is signed in to store the file as;
  2. the file is at most `max_bytes`;
  3. it is a well-formed core module (`ModuleFile`), not a component;
  4. it imports only `alternera.host_call`, `alternera.body_read` and
     functions of WASI preview 1, which the runtime serves with nothing
     granted; anything else is refused, never stubbed;
  5. it has no start function: a start function runs while the module
     is instantiated, before any time limit applies, and one that loops
     keeps a processor busy for good (`_initialize` is the way to
     initialise, and runs within the call's time);
  6. it defines one memory, not shared and not 64-bit, whose minimum is
     within the node's ceiling (`MemoryLimit.ceiling_mib/0`);
  7. it exports `memory`, `alternera_abi`, `alternera_alloc`,
     `alternera_run` and `alternera_check`;
  8. its `alternera.uses` section is well formed (`Uses`);
  9. it fits the tenant's share of storage (`max_tenant_bytes`), read
     before it compiles so a tenant at its quota costs no compile, and
     checked again as it is stored; a file that does not fit first has
     the tenant's unreferenced files removed (`Files.reclaim/1`) and is
     measured again;
 10. the tenant has not compiled more than `per_minute` new modules on
     this node in the last minute (a file the tenant stored before is
     taken as it is: it neither compiles here nor counts, and a step
     compiles it when it first runs on a node that lacks it);
 11. it compiles and answers interface version 0, within 5 s, one
     compile at a time on the node (`UploadedModules`);
 12. it is stored (`Files.put/5`).

The rate and the cache are per node.

An import that carries a module (`Plugin.Provisional.Files`) runs the
same: `check_file/2` (1 to 8 without a user) for its preview,
`accept/5` as it lands.

# `accepted`

```elixir
@type accepted() :: %{
  digest: String.t(),
  name: String.t(),
  size: non_neg_integer(),
  uses: Alternera.PluginWasmFramework.Uses.t(),
  new?: boolean()
}
```

An accepted module, as the step editor shows it.

# `settings`

```elixir
@type settings() :: %{
  enabled?: boolean(),
  max_bytes: pos_integer(),
  per_minute: pos_integer(),
  max_tenant_bytes: pos_integer()
}
```

The guards an uploading plugin sets: whether uploads are on, the
largest file, new modules a tenant may compile a minute, and the most
a tenant's files may hold together.

# `accept`

```elixir
@spec accept(
  Alternera.PluginWasmFramework.Language.t(),
  String.t(),
  String.t() | nil,
  String.t(),
  binary()
) :: {:ok, accepted()} | {:error, String.t()}
```

Checks, compiles and stores `bytes`, uploaded as `name` by `user_id`
for `tenant_id` into a step of `lang`.

# `check`

```elixir
@spec check(binary()) ::
  {:ok, Alternera.PluginWasmFramework.ModuleFile.t()} | {:error, String.t()}
```

The checks on the file's outline (3 to 7 in the moduledoc), without
compiling it: its outline, or the first refusal.

# `check_file`

```elixir
@spec check_file(Alternera.PluginWasmFramework.Language.t(), binary()) ::
  :ok | {:error, String.t()}
```

The checks on `bytes` that cost nothing, for an import's preview: the
switch, the size, the outline and the `alternera.uses` section (1 to 8
above, without the user). `:ok` or the refusal `accept/5` would give.

# `format_bytes`

```elixir
@spec format_bytes(non_neg_integer()) :: String.t()
```

A byte count as people read it (`176 KB`, `8 MB`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
