# Build an extension

> What an extension is, the two places its code runs, and the ideas the rest of this section builds on.

Page: https://songstress.app/docs/develop/

An extension adds to Songstress: a source to search, lyrics, a scrobbler, a theme, a page of its own. It is a
package: a folder with a manifest, `songstress.json`, at its root, and beside it whatever the manifest names. A theme
is a manifest and its colours. A scrobbler is a manifest and one JavaScript file.

You write extensions against the `songstress` package on npm, the SDK. It holds the API's types, the manifest's
schema and validator, a mock Songstress for your tests, and the `songstress` command that checks, builds, packs and
runs an extension on your own server. Start with [Your first extension](/docs/develop/quickstart/).

## Six ideas [#six-ideas]

**The manifest declares, code attaches.** Everything an extension adds is listed in the manifest: its commands,
menu items, settings, pages, providers and the rest. Code gives each one its behaviour, by id. So an app can show an
extension's menus and settings, and a listing can say what it does, without running a line of it.

**Two entries, both optional.** An extension's code is one JavaScript bundle for each side it runs on.

| Entry | Runs | Good for |
| --- | --- | --- |
| `entry.server` | On the user's own server, in a sandbox, for every member and every app | Anything that must work for a phone, or while no app is open: hearing plays, providers, jobs, routes, pages every app can draw |
| `entry.client` | Inside one app, in a sandbox, while that app is open | Following this device's player live, and interface that reacts at once |

An app may not run client code at all, so what must work everywhere belongs in the server entry.

**Code starts when it is needed.** There is no "on start-up". Songstress starts an entry when something it declared
is called for: a hook fires, a command runs, a provider is asked, a page is drawn, a job is due, a route is called. It
may stop the entry again when it is idle. So an entry registers everything at its top level each time it starts, and
keeps what must last in `songstress.storage`, never in a variable.

**Hooks observe.** A hook tells an extension what happened. Nothing waits for a listener, and no hook can refuse or
delay anything: no extension can stall the music.

**An extension adds, and never replaces.** A provider answers beside Songstress's own sources, in the order the
admin sets. An extension draws inside the places given to it, never over Songstress's own interface.

**Nothing is allowed until a person allows it.** The manifest asks for each permission with a reason in plain words.
The server's admin grants them, and each member grants what concerns their own listening.

## The sandbox [#the-sandbox]

Your code runs with the JavaScript language and the `songstress` API, and nothing else.

- **There:** ECMAScript 2023 (promises, async functions, iterators), timers, and `console.log`, which writes to the
  extension's log.
- **Not there:** `fetch` (use `songstress.net.fetch`), the DOM, every Node.js API, the file system, `eval`,
  `new Function` and WebAssembly. On the server, don't count on `URL`, `TextEncoder`, `crypto` or `Intl` either, and
  dates are in UTC. Bundle what you need.
- **One file an entry.** An entry is one classic script with everything bundled into it and no `import` or `export`
  left: a bundler's IIFE output. `songstress build` makes it for you.
- **Limits on the server:** 64 MiB of memory and 30 seconds for one call (a hook, a command, a provider's answer). A
  timer doesn't keep a call alive past the promise it returned.
- **Limits in an app:** client code that stops answering, or floods the app with messages, is stopped. An extension
  stopped again and again is switched off.
- **JSON at the border:** everything that crosses between your code and Songstress is JSON. No functions, dates or
  binary values.

## Where each part works [#where-each-part-works]

"Phone" means the iPhone, iPad and Android apps. A phone build made for an app store leaves out three things: the
web views that run client code, free-form views, and the in-app catalogue of extensions. Everything an extension does
on the server, its declared interface, its settings, and installing by link work in every build.

| Part | Server | Web and desktop | Phone | Phone build for a store |
| --- | --- | --- | --- | --- |
| Server entry: hooks, providers, jobs, routes | Runs here | Reaches them | Reaches them | Reaches them |
| Client entry and its hooks | | Runs while the app is open; not yet on Windows and Linux | Runs while the app is open and in front | Not available |
| Commands with `run: server` | Runs here | Yes | Yes | Yes |
| Commands with `run: client` | | Yes; not yet on Windows and Linux | Yes | Not available |
| Menus | | Every location | Every location but `sidebar`, in one "Extensions" submenu | As a phone |
| Settings | Kept here | Drawn | Drawn | Drawn |
| Pages, panels and status items rendered by the server | Rendered here | Drawn | Drawn | Drawn |
| Pages, panels and status items rendered by the client | | Drawn; not yet on Windows and Linux | Drawn | Not available |
| Views | Serves the files | A browser tab (web), the system's browser (desktop) | The in-app browser | Not available |
| Themes | | Applied | Applied | Applied |
| Providers | Runs here | Shows the results | Shows the results | Shows the results |
| Jobs | Runs here | Shown in Activity | Shown in Activity | Shown in Activity |
| Sign-in | Runs here, and keeps the account's name | From the extension's page | As the web | As the web |

Nothing of an extension appears on a watch, in a car or in a widget.

Your code doesn't have to guess which app it is drawing for. Every render, and every command a person runs, is told
the app in its context (`client`), and `songstress.host.info()` tells client code about its own app: its
`platform`, the version of the components it draws (`ui`), and its `capabilities` (`clientCode`, `views`).

## In this section [#in-this-section]

- [Your first extension](/docs/develop/quickstart/): start one, run it on your server, change it.
- [The manifest](/docs/develop/manifest/), every field, from the SDK's own schema.
- [Permissions](/docs/develop/permissions/), [Hooks](/docs/develop/hooks/),
  [Commands, menus and settings](/docs/develop/commands/), [Pages, panels and status](/docs/develop/interface/),
  [Providers](/docs/develop/providers/) and [Themes](/docs/develop/themes/): what an extension contributes.
- [The host API](/docs/develop/api/): what your code can call, and its limits.
- [The command line](/docs/develop/cli/), [Test and debug](/docs/develop/testing/),
  [Package, release and publish](/docs/develop/publish/) and [Versions](/docs/develop/versions/).
- [Validation codes](/docs/develop/validation/), [Examples](/docs/develop/examples/) and the
  [API reference](/docs/develop/reference/songstress/).
