# Config

One file, `tosijs-native.config.ts`, describes the app to every target. Shells are generated
from it, and the JS API checks ids against it, so a typo in a product or achievement id fails
at the call instead of silently never unlocking.

```typescript
import { defineConfig } from 'tosijs-native'

export default defineConfig({
  appId: 'net.tosijs.manta',
  name: 'Manta',
  products: [{ id: 'full_game', grants: ['full_game'] }],
  achievements: [{ id: 'first_flight', name: 'First flight' }],
  targets: ['web', 'ios', 'mac', 'steam'],
})
```

## Products grant entitlements

A **product** is what a store sells; an **entitlement** is what the game asks about. They are
separate because the store's ids are not the game's business: a launch-sale SKU and the
regular SKU can both grant `full_game`, and the game still asks one question.
`native.entitlements.has('full_game')` is manta's `hasFullGame()`, generalised.

Native shells only ever report **product ids**. The mapping to entitlements happens here, in
JS, once, so no shell re-implements it.

## Capabilities are declared, or they do not exist

`capabilities` lists what the app uses beyond the basics. It is optional, and an app that
leaves it out gets none: a shell is generated with only the entitlements, usage strings and
permissions the config declares, and its bridge refuses the rest.

```typescript
capabilities: {
  keepAwake: true,                               // the screen stays on during play
  share: true,                                   // the system share sheet
  openExternal: ['https://tosijs.net/', 'mailto:'],  // links the app may open outside itself
  network: ['https://cdn.tosijs.net/'],          // where the page may load data from
}
```

| capability | value | what it allows |
| --- | --- | --- |
| `camera`, `microphone`, `motion` | the reason, as the player is shown it | the web build may use that device permission (`getUserMedia`, device motion) |
| `location` | the reason, or `{ reason, background, approximate }` | `native.location`: where the device is, while the app is in use, and with `background` a route the player starts that keeps recording when they leave the app |
| `cloudSaves` | `true` | saves survive a reinstall and follow the player to their other devices, where the platform has a cloud store |
| `files` | `true` | `native.files`: a folder of the player's own documents |
| `keepAwake` | `true` | `native.keepAwake(on)` |
| `share` | `true` | `native.share(content)` |
| `notifications` | `true` | `native.notifications`: a local notification at a time the app chooses |
| `openExternal` | a list of link prefixes | `native.openExternal(url)`, for a URL that starts with one of them |
| `network` | a list of https prefixes | the page may load data from them: `fetch`, a WebSocket, images, media and fonts |
| `reviews` | `true` | `native.reviews`: the store's own rating prompt, and its write-a-review page |
| `tasks` | `true` | `native.tasks`: the app's one background task, run at most once per interval |
| `backgroundAudio` | `true` | `native.media`: audio the player started keeps playing in the background, with the system's media controls |
| `settings` | a schema (tosijs-schema) | `native.settings`: the app's settings, shown where the platform shows them (the iOS Settings app, Android's App info, the Mac's Settings…); see Settings |

A device permission is declared by giving its reason, because the platform shows the player
that sentence when it asks (`camera: 'Scan a code to join a friend'`). A build gets the
usage strings for exactly the permissions declared, and the shell refuses the rest without
asking the player.

## Location

Location is a device permission too, declared by its reason. It goes through the shell, never
the web view's own `navigator.geolocation`, which the system web views cannot gate per
request.

```typescript
capabilities: {
  location: 'to show the shops near you',      // while the app is in use
}
capabilities: {
  location: {
    reason: 'to record your route',            // what the player is shown when asked
    background: true,                          // a route the player starts keeps recording
                                               //   when they leave the app or lock the screen
    approximate: true,                         // never more precise than about a kilometre
  },
}
```

`background` needs only the player's "while using the app" permission, because the player
starts the recording while the app is in front and the system shows it while it runs (the
location indicator on iOS, a notification on Android). Location while the app is closed,
or "always" permission for geofences and background task runs, is not offered: it costs a
second permission prompt and a store review of its own.

An `openExternal` entry is `'mailto:'`, an https prefix that ends its host with a `/`
(`'https://tosijs.net/'`, `'https://tosijs.net/docs/'`), or `'https://'` alone, which allows
any https link and says so. The `/` is required because `'https://tosijs.net'` would also
match `https://tosijs.net.example.com`.

## The network is closed until the config opens it

A page in a shell is served from the app itself, and by default that is all it can reach:
a `fetch` to anywhere else is refused, and so is an image or a stream from another site. An
app that needs the network says where:

```typescript
capabilities: {
  network: ['https://api.example.com/', 'https://cdn.example.com/assets/'],
}
```

An entry is written as an `openExternal` entry is: an https prefix that ends its host with
a `/`, or `'https://'` alone for any https address, which says so. An entry also allows a
WebSocket to the same host (`wss:`). Plain `http:` is never allowed.

What an entry opens is **data**: `fetch`, `XMLHttpRequest`, WebSocket, images, audio and
video, fonts. It never opens code. Scripts, stylesheets and frames come from the app's own
files whatever the config says, because a remote script would run with the bridge: bundle
them. On the web the page is the app and nothing enforces this; in a shell the policy is
served with every page (`contentSecurityPolicy`), and an Android app that declares no
network is not given the permission to use one at all.

## What the build needs

`tosijs-native build` makes each target's app from this file and the web build beside it.
Four optional fields say where things are; each has a default.

```typescript
export default defineConfig({
  appId: 'net.tosijs.manta',
  name: 'Manta Recon',
  shortName: 'Manta',        // under the icon, where the full name is too long
  version: '1.2.0',          // default: the version in the package.json beside this file
  web: 'dist',               // the web build, relative to this file (default 'dist')
  icon: { square: 'icon.svg', layered: 'AppIcon.icon', background: '#101820' },
  // …products, achievements, targets, capabilities
})
```

| field | value | default |
| --- | --- | --- |
| `shortName` | the name under the icon | `name` |
| `version` | three numbers, `1.2.3`: the stores accept nothing else | the `package.json` beside the config |
| `web` | the folder with the web build's `index.html` | `dist` |
| `icon` | a full-bleed square image (SVG, or a PNG of 1024 pixels or more), or `{ square, layered, background }` | a placeholder |
| `appStoreId` | the app's number in the App Store (`'1234567890'`), once App Store Connect has given it one | none: on Apple platforms `reviews.open()` has nowhere to go |
| `orientation` | `'portrait'`, `'landscape'` or `'any'`: how a phone or tablet may be held | `'any'` |
| `launch` | the launch screen: `{ background, image, fit, width }` | the system's plain screen |

In `icon`, `layered` is an Icon Composer bundle (`.icon`) for Apple's layered icons, and
`background` is the colour behind the square in Android's adaptive icon (`#rrggbb`, white
if left out). Never give a pre-rounded icon: every system masks it itself.

## The launch screen

What the player sees from tapping the icon to the page's first frame. The build draws it
twice, so it looks like one screen: as the system's own launch screen (a storyboard on iOS,
the splash screen on Android), and as the shell's view over the web view, which stays until
the page calls `native.ready()`.

```typescript
launch: {
  background: '#101820',   // the colour (default: the icon's background, then white)
  image: 'splash.svg',     // an SVG or PNG, optional
  fit: 'center',           // 'center': `width` points wide, in the middle (default)
                           // 'contain': scaled to fit the screen, the colour around it
                           // 'cover': scaled to fill the screen, cropped
  width: 200,              // for 'center' (default 200)
}
```

An SVG is drawn at every size a screen needs; a PNG for `contain` or `cover` should be big
enough for the largest tablet (2732 pixels on its long side). Call `native.ready()` once
the first frame is drawn: until then the launch screen covers the page. Android 12 and
later always open with the system's own splash, which can only show a colour and an icon,
so there the image appears a moment after the colour.

`orientation` holds a phone or tablet the way the app is designed for. iPadOS and Android
16 on a large screen may still put the app in a window of any shape, so a layout should not
assume the screen's aspect.

The app id is used as it is written on Apple platforms. Android does not allow `-` in one,
so there it becomes `_` (`net.tosijs.native-hello` is `net.tosijs.native_hello`).

## What the package needs from its host

The language, and nothing else: no DOM, no `URL`, no `fetch`. So the same API and config
run in a browser, in a web view, and in a bare JavaScript engine, which is all a platform
with no web view offers. Timers are used where they exist (the heartbeat that notices a
sleep), and the contract suite needs `setTimeout`. `src/headless.test.ts` runs the suite in
a context with only that.

## Cloud saves have one size everywhere

With `cloudSaves` declared, `saves` enforces `SAVE_LIMITS` on **every** target, in JS, so a
save that fits on the desktop fits on the phone. They are the limits of the smallest cloud
store a shell uses (Apple's iCloud key-value store, which holds 1024 keys and 1 MB): 256
keys, a key of at most 64 bytes, and 1,000,000 bytes in total, counted as the UTF-8 bytes of
every key and value. The margin is real: a removed key keeps counting against the 1024 until
the removal has synced, and the shell needs room for its own records. A save that would go
over throws a `QuotaExceededError`, as `localStorage` does. Without `cloudSaves` there is no
limit. Bigger data belongs in files.

## defineConfig

Validates the config and returns it. Beyond the shape, it refuses what the shape cannot say:
duplicate ids, a product that grants nothing, and an app id that a store would reject.

## What the config declares

`declaredCapabilities(config)` is the config's capabilities with the switched-off ones
removed: the one reading of "is this declared" that the API, the web fallback and the
shells' generated config all share.

`locationOptions(value)` is a `location` declaration in its long form,
`{ reason, background, approximate }`, with the two switches false when left out.

## The launch screen

`launchScreen(config)` is the config's `launch` with its defaults filled in, or `null` when
the config has none: `background` falls back to the icon's background, then white; `fit` to
`'center'` and `width` to 200 points when there is an image.

## Links the app may open

`allowedLink(entries, url)` returns the link when the `openExternal` entries allow it, and
`null` when they do not. A link is allowed when it starts with an entry and means what it
reads: it must be written in full, as a browser's address bar shows it
(`https://tosijs.net/docs/`, lower-case host). Anything a browser would quietly turn into a
different address is refused rather than tidied: a backslash, a user name, a `.` or `..`
step in the path (encoded or not), a space or control character. Only `https:` and
`mailto:` links can ever be allowed.

The rule is plain string work on purpose. Every shell applies it again natively, because the
page is on the other side of a trust boundary, and the same few lines in JS, Swift, Rust and
Kotlin cannot disagree the way four URL parsers can.

## What the page may load

`contentSecurityPolicy(config)` is the policy a shell serves with every page: the app's own
files, and the config's `network` entries for data. It is made here, once, and each shell
serves the string it is given, so three shells cannot hold three policies. `shell.connect`
adds what a shell's own bridge needs (Tauri's talks to the page over a local address). With
`downloads` declared, the origin downloads are served from (`tosijs://downloads`, never the
page's own) is listed for media, images and fetch, and nothing else: not script, not frames.

## The shell's copy of the config

`shellConfig(config)` is what a shell is built with: a JSON file in the app bundle
(`tosijs-native.config.json`) that the page cannot change. A shell reads what it may do from
this file, never from the page.

## Entitlements from products

The one place products become entitlements. Unknown product ids are ignored rather than
refused: a store can report a product this build no longer sells, and owning it must not
break the game.
