---
title: Files
summary: "`host.fs`: read and change the files in the SpaceMIX server's files folder, with paths that can never leave it."
audience: both
stability: stable
since: protocol 1
---

# Files

`host.fs` reaches the server's **files folder**: one folder on the disk of the computer that runs the SpaceMIX
server (the desktop app, or `spacemix-server`), also when the extension runs in a browser elsewhere. Nothing outside
it is reachable: every path is relative to it, and the core refuses any path that leads outside it, symlinks
included.

The folder is always `files` in the server's data directory (`SPACEMIX_DATA_DIR` on a headless server), created
automatically. There is no way for an extension or the user to add other files or folders from the computer.

The API calls the folder a **root**: `roots()` returns exactly one, with the id `"server"`, and every other method
takes that id.

```ts
interface SpacemixHost {
  fs?: FilesApi; // present with `fs`
}

interface FilesApi {
  roots(): Promise<FileRoot[]>;
  list(root: string, path?: string): Promise<FileEntry[]>;
  read(root: string, path?: string): Promise<FileContent>;
  write(root: string, path: string, text: string, options?: { create?: boolean }): Promise<FileEntry>;
  mkdir(root: string, path: string): Promise<FileEntry>;
  rename(root: string, from: string, to: string): Promise<FileEntry>;
  delete(root: string, path: string): Promise<void>;
  import(root: string, dir: string, items: string[] | File[]): Promise<FileEntry[]>;
  subscribe(fn: () => void): () => void;
  onDragDrop(fn: (event: DragDropEvent) => void): () => void;
}

interface FileRoot { id: string; name: string; path: string; kind: "file" | "dir" }
interface FileEntry { name: string; path: string; kind: "file" | "dir"; size: number; modified: number }
interface FileContent extends FileEntry { text?: string | null; binary: boolean; tooLarge: boolean }

type DragDropEvent =
  | { type: "enter" | "over"; paths: string[]; x: number; y: number }
  | { type: "drop"; paths: string[]; x: number; y: number }
  | { type: "leave" };
```

**Permission:** `fs`. Without it `host.fs` is `undefined`.

**Shared folder.** The files folder belongs to the server, not to one extension or person: every extension with `fs`,
in every window and browser, sees and changes the same files. Approving `fs` means "everything in the server's files
folder".

**Turned off.** While a paid extension is turned off on the server, the changes made for it (`write`, `mkdir`,
`rename`, `delete`, `import`) reject with `<Name> is turned off on this server until it's paid for.` Reads still work
(see [Turned off until it's paid for](../permissions.md#turned-off-until-its-paid-for)).

## Paths

- `root` is `"server"`, the id `roots()` returns. Any other id rejects with
  `Unknown root. The server's files folder is "server".` If the folder is missing on disk it is created again; when
  that fails, calls reject with `Couldn't create the files folder <path>: <reason>`.
- `path` is relative to the folder, `/`-separated (`\` also splits). `""` (the default) is the folder itself. Empty
  and `.` segments are ignored; `..` or an absolute segment rejects with
  `Invalid path "<path>". Use a path relative to the root, without "..".`
- A path that resolves outside the root through a symlink rejects with
  `That path leads outside the files folder.` Listings leave such links (and broken ones) out.
- `FileEntry.path` comes back relative to the folder, `/`-separated; `modified` is milliseconds since the epoch;
  `size` is 0 for folders.

## `host.fs.roots`

```ts
roots(): Promise<FileRoot[]>;
```

The server's files folder, as the one root: `{ id: "server", name: "Files", path, kind: "dir" }`. `path` is the
absolute location, for display. `FileRoot.kind` stays in the type, but is always `"dir"`. Bridge method: `fs.roots`.

## `host.fs.list`

```ts
list(root: string, path?: string): Promise<FileEntry[]>;
```

The entries directly inside a folder, folders first, then by name (case-insensitive). Rejects with `Not a folder.`
for a file. Bridge method: `fs.list`.

## `host.fs.read`

```ts
read(root: string, path?: string): Promise<FileContent>;
```

A file's entry and, when it is text, its contents.

- Larger than 5 MB: `tooLarge: true`, no `text`.
- A NUL byte in the first 8 KB, or not valid UTF-8: `binary: true`, `text: null`.
- Otherwise `text` is the whole file.

Rejects with `That's a folder; list it instead.` for a folder. Bridge method: `fs.read`.

## `host.fs.write`

```ts
write(root: string, path: string, text: string, options?: { create?: boolean }): Promise<FileEntry>;
```

Writes `text` (UTF-8) to a file, creating it or replacing its contents. With `create: true`, rejects with
`Something with that name already exists.` if anything is there. The parent folder must exist. Rejects with
`That's a folder.` for a folder. Text only: binary writes aren't supported. Bridge method: `fs.write`.

## `host.fs.mkdir`

```ts
mkdir(root: string, path: string): Promise<FileEntry>;
```

Creates one folder; its parent must exist, and nothing may be there yet. Bridge method: `fs.mkdir`.

## `host.fs.rename`

```ts
rename(root: string, from: string, to: string): Promise<FileEntry>;
```

Renames or moves within the files folder. Rejects with `No such file or folder.` when `from` doesn't exist and
`Something with that name already exists.` when `to` does. Not on the folder itself:
`Can't do that to the files folder itself.` A symlink is renamed, not its target. Bridge
method: `fs.rename`.

## `host.fs.delete`

```ts
delete(root: string, path: string): Promise<void>;
```

Moves a file or folder to the system trash, so it can be restored there. Not on the folder itself.
Rejects with `No such file or folder.` or `Couldn't move to trash: <reason>`. Bridge method:
`fs.delete`.

## `host.fs.import`

```ts
import(root: string, dir: string, items: string[] | File[]): Promise<FileEntry[]>;
```

Copies dropped items into the folder `dir`. Nothing is overwritten (an existing name rejects with
`"<name>" already exists here.`), and the originals stay where they are.

- **Absolute paths** (`string[]`): what `onDragDrop` gives in the desktop window. Only works in the desktop app
  on the server's own computer; elsewhere rejects with
  `Paths can only be dropped onto this computer's own server in the desktop app.`
- **`File` objects**: what an HTML5 drop gives in a browser. Only in a browser; in the desktop window rejects with
  `Drop files from your file manager instead.`

Frame timeout: 10 minutes. Bridge method: `fs.import`.

## `host.fs.subscribe`

```ts
subscribe(fn: () => void): () => void;
```

Calls `fn` (no arguments) after any change made through SpaceMIX's file API, by any extension, window or
browser: a write, rename, delete or import. Changes made to the disk outside SpaceMIX are **not** reported.
Re-read what you show. Channel: `fs`.

## `host.fs.onDragDrop`

```ts
onDragDrop(fn: (event: DragDropEvent) => void): () => void;
```

Files dragged from the operating system over the extension's view, **in the desktop app only**; in a browser it
never fires (use ordinary HTML5 drop events, which give `File` objects). Only fires while the view is showing.
`x` and `y` are relative to the view. `enter` and `drop` carry the absolute paths; `over` has `paths: []`.
`leave` fires when the drag leaves the view. Pass `drop.paths` to `import`. Channel: `dragdrop`.

## Example

```ts
import { host } from "@spacemix/sdk";

export async function appendLine(path: string, line: string): Promise<void> {
  const fs = host.fs;
  if (!fs) throw new Error("This extension needs the fs permission.");
  const [root] = await fs.roots();
  const current = await fs.read(root.id, path).catch(() => null);
  if (current && (current.binary || current.tooLarge)) throw new Error(`${path} isn't a text file SpaceMIX can edit.`);
  await fs.write(root.id, path, `${current?.text ?? ""}${line}\n`);
}
```

## Stability

Stable.
