A publication names every file by its content: objects live at store/<aa>/<sha256>.<extension>, and their meaning is carried by indexes. Readers that address files by path, such as static sites, viewers and download tools, need a resolver that maps a path of a view to an object. sekai-exporter serve is such a resolver. Its core, the Resolver of the sekai-serve crate, does no I/O, so the same code can run in other hosts, such as an edge worker in front of an S3-compatible bucket.
The resolver replaces a materialized path tree. A tree copies or links every file to a fixed path, multiplies the number of stored objects by the number of views, and cannot switch to a new version atomically. A resolved tree stores nothing but its index, every view shares the same objects, and a new version takes effect for every path at once.
A view is a path tree over the objects of a publication:
| View | Paths |
|---|---|
canonical |
The paths of the publication: <name>.asset.json, <name>.<extension>, or <name>/<role>.<extension> |
storage |
The paths of the storage view |
Each publication of a view writes a path index to the store and names it in the pointer of the view, <region>/<view>/current.json, under paths. The same pointer is also kept at <region>/<view>/versions/<published version>.json, so a version stays reachable after newer ones are published, for as long as its objects are kept.
A path index is a small root document and a sequence of chunks, each holding a contiguous range of paths in byte order with the hash, extension, size and media type of each file (format). Chunk boundaries are chosen by the hash of a path, not by position, so a version that adds or changes a few paths changes only the chunks that hold them. Unchanged chunks keep their hashes, and publishing the next version uploads only the chunks that changed.
A lookup reads the root, finds the chunk whose range holds the path by binary search, and finds the path in that chunk by binary search. The resolver uses a root or a chunk only when its bytes have the hash it is named by, and keeps the decoded ones in memory within a limit, so a lookup in a chunk it has already read reads nothing.
A routes file says which URL paths name which views. Routes are tried in order, and the first that matches decides:
{
"routes": [
{ "pattern": "/jp-storage/{path}", "region": "jp", "view": "storage" },
{ "pattern": "/storage/{region}/{path}", "view": "storage" },
{ "pattern": "/{region}@{version}/{path}", "view": "canonical" },
{ "pattern": "/{region}/{path}", "view": "canonical" }
],
"delivery": "bytes",
"max_age": 60,
"cors": "*"
}A pattern starts with / and is a sequence of /-separated segments. A segment is literal text, placeholders, or both, such as {region}@{version}; two placeholders are separated by text. A placeholder takes the characters of its segment up to the first occurrence of the text that follows it, and they must form a name: at most 200 bytes of ASCII letters, digits, ., _ and -, not only dots, and not a name that Windows keeps for a device, such as con or nul.json. A route whose placeholders do not take names does not match, and the next route is tried. The first route that matches decides even when its view has no such path, so routes with literal text come before the more general ones.
| Placeholder | Meaning |
|---|---|
{region} |
The region. Either a placeholder or the route's region key gives it, never both |
{view} |
The view. Either a placeholder or the route's view key gives it, never both |
{version} |
Optional. A published version: the path is resolved in that version instead of the current one |
{form} |
Only on a "listings": true route. Optional: an asset form; either a placeholder or the route's form key may give it, never both |
{path} |
Required on a path route, and the last segment. The rest of the URL path, percent-decoded, is the path in the view. A path that is empty or ends with / names a directory |
A region, view or version without a publication is not found.
| Key | Meaning |
|---|---|
delivery |
"bytes": the response carries the file. "redirect": the response redirects to the object in the store at store_url |
store_url |
With "redirect", the URL of the store, such that an object is at <store_url>/<aa>/<sha256>.<extension> |
max_age |
How many seconds a response may be cached, for the current version and for a named one. Default 60 |
cors |
When present, the value of Access-Control-Allow-Origin |
A route with "tree": true names the directory tree of a view instead of its paths. Its pattern has no {path}, and a URL path matches it only when nothing follows its last segment, such as { "pattern": "/trees/{region}/{view}", "tree": true } for /trees/jp/storage. In the same way, a route with "history": true names the version history of a view, a route with "changes": true the changes of a version, such as { "pattern": "/changes/{region}/{view}@{version}", "changes": true } for /changes/jp/storage@6.1.0, and a route with "listings": true the form listings of a view, such as { "pattern": "/listings/{region}/{view}/{form}", "listings": true } for /listings/jp/canonical/live2d-model or { "pattern": "/listings/{region}/{view}", "listings": true } for the list of forms. A route names at most one of these.
A routes file with another key, an invalid pattern, or a route that gives the region or the view twice or not at all, or gives {form} or form outside "listings": true or twice, is refused when the resolver starts.
The resolver answers GET, HEAD with the headers of GET and no body, and OPTIONS with 204 and the methods it allows. A path that no route matches, or that the view does not have, is 404 with Cache-Control: public, max-age=<max_age>. Other methods are 405, and a URL path longer than 8192 bytes is 414. A file that cannot be read is 500 with Cache-Control: no-store; What the resolver checks lists the reasons. Every response has X-Content-Type-Options: nosniff, so a client takes a file for nothing but its media type.
With "bytes", a file is 200 with:
| Header | Value |
|---|---|
Content-Type |
The media type of the file |
Content-Length |
Its size, or the size of the requested range |
ETag |
"<sha256>", the hash of the file |
Accept-Ranges |
bytes |
Content-Disposition |
inline with the last segment of the path as the file name, also as filename* in UTF-8 when it is not ASCII, so a saved file keeps its name |
Cache-Control |
public, max-age=<max_age>, for the current version and for a named one |
A single byte range, bytes=<first>-<last>, bytes=<first>- or bytes=-<length>, is 206 with Content-Range. A range that starts past the end is 416. A request with several ranges, or with an If-Range that is not the file's ETag, gets the whole file. An If-None-Match that names the file's ETag, or *, is 304.
With "redirect", a file is 302 to its object, with the same Cache-Control. The object's URL never changes, so a client or cache keeps it for as long as it likes. A client that saves the redirected file names it by its hash; "bytes" keeps the path's name.
With cors, every response has Access-Control-Allow-Origin and exposes Accept-Ranges, Content-Disposition, Content-Range and ETag, and OPTIONS allows the Range, If-None-Match and If-Range request headers.
The pointer of a view is read again at most every max_age seconds, so a new publication reaches every path of the view within that time and never mixes two versions in one lookup. A pointer that does not exist is remembered for as long, so a region, a view or a version answers within max_age of its first publication.
The pointer of a named version is read again as often, because a version can be published again under its name: export publishes local on every run, and update --force publishes the version it finds once more, with other files when the converters have changed since. The resolver answers with the new files within max_age, and a response for a named version may be cached no longer than one for the current version, so a client or a cache gets the new files within twice max_age. A file that has not changed keeps its ETag, the hash of the file, so asking for it again with If-None-Match is 304 without a body.
A path that is empty, for the root, or ends with /, such as /storage/jp/character/member/, names a directory of the view under the same routes, in the current version or a named one. The directory is 200 with a JSON document, application/json, that lists what is right under it:
| Key | Value |
|---|---|
format |
"directory-listing" |
version |
1 |
view, published_version, revision |
Those of the publication, from its path index |
path |
The directory: empty for the root, and otherwise ending with / |
directories |
The names of the subdirectories, in byte order |
files |
{ "name", "sha256", "extension", "size", "media_type" } for each file right under the directory, in byte order of the names |
A directory exists when a file of the view is under it; the root of a published view always exists. Any other directory is 404, and so is a directory asked for without its /, which names no file. The ETag of a listing is "<sha256>" of the document, with Cache-Control and If-None-Match as for a file; the answer to HEAD gives the length of the document.
A listing is made from the path index alone. The paths under a directory are one range of the index, in byte order. The resolver reads that range from its start and, at the first path of a subdirectory, goes on past every path under it, so a listing reads only the chunks in which a file or a subdirectory of the directory starts, and the chunks it reads are remembered as for files. Every published version of every view can therefore be listed, and nothing is published for it.
Every publication of a view also has a directory tree: one gzip-compressed JSON document with every directory and file of the view, by name, size and media type. A page that browses a whole view reads the tree once and then reads each file by its path in the same published version, so the tree and the files never come from two versions.
A route to the tree answers it as a file: the object that the pointer of the view names under tree, with Content-Type: application/gzip, ETag the hash of the object, the file name directory-tree.json.gz, ranges and If-None-Match as for a file, or a redirect to the object with "redirect". The answer needs only the pointer. A client that keeps the tree asks for it again with If-None-Match and gets 304 until a new version is published, and then the new tree. A publication whose pointer names no tree has none, and its tree is 404.
Every publication of a view also has a version history: the published versions of the view, newest first, each with its number of files and how many files it added, removed and changed since the version before, and how many of those only the conversion changed. When those are known, the publication also has the changes of its version, which list those files by path, with their sizes and media types. A page that follows a view across versions reads the history, then the changes of a version it chooses, and reads a file of either version by its path.
A route to the history or to the changes answers them as the tree is answered: the object that the pointer names under history or changes, with Content-Type: application/gzip and the file name version-history.json.gz or version-changes.json.gz. A route without {version} answers for the current version, and one with it for the named version, whose history ends with that version. A publication whose pointer names no history or no changes has none, and the request is 404.
Every publication in the index layout (such as canonical) names an index root under root, which holds a chunked listing for each asset form. A route with "listings": true exposes those listings directly as 200 application/json documents, in the current version or a named one, so a reader such as a Live2D viewer can discover every exact asset name of a form in one request instead of walking directories.
When the route names a form (through {form} in its pattern or its form key), the document lists every asset of that form in byte order of name:
| Key | Value |
|---|---|
format |
"form-listing" |
version |
1 |
view |
The view named by the route |
published_version, revision |
Those of the publication, from its index root |
form |
The requested asset form, such as "live2d-model" |
assets |
{ "name", "fingerprint", "size" } for each asset of the form, in byte order of name |
When the route names no form, the document lists the forms the publication carries, in byte order of form:
| Key | Value |
|---|---|
format |
"form-listings" |
version |
1 |
view |
The view named by the route |
published_version, revision |
Those of the publication, from its index root |
forms |
{ "form", "assets" } for each form listed in the index root, where assets is the number of assets of that form |
A publication whose pointer names no root (such as the storage view) has no form listings, and the request is 404; a form not listed in the index root is also 404. As with directory listings, the ETag is "<sha256>" of the document, with Cache-Control, If-None-Match and HEAD as for a file, and the response is sent as JSON even under "delivery": "redirect". Listing all forms reads only the index root; listing one form reads its index root and the chunks of that form, and remembers the decoded objects and the finished document within the index cache limit.
The resolver takes neither a request nor a publication at its word.
A request names files only through the routes. Its region, view, version and form are names, so the pointer they name, <region>/<view>/current.json or <region>/<view>/versions/<version>.json, is a file below the published root. Its path is looked up in a path index and never becomes part of a file name: the file that is opened is the object the index names, <aa>/<sha256>.<extension>.
A publication is used only as far as it is what it says. A request is 500, and the host logs the reason, when:
- the pointer cannot be read, is not a pointer document, or names its index root, its path index, its directory tree, its version history or its changes by something other than a SHA-256;
- the root or a chunk of the path index, or the index root or a chunk of a form listing, is missing or cannot be read, does not have the hash it is named by, does not match the pointer's version and revision (for an index root), or does not follow its format;
- a pointer is longer than 64 KiB, or an index root, path index root, chunk or assembled form listing is longer than 64 MiB;
- the index gives the file an extension that is not a name, or a media type that cannot be the value of a header;
- the object of the file is missing, or is not a file of the size the index gives. The server finds this out before it sends the first byte, so a truncated object is an error and not a response that breaks off. The bytes of an object are not hashed when they are sent.
What failed is not remembered: once the pointer or the object is whole again, the next request reads it.
What the resolver remembers does not grow with what requests name. When a limit is reached, what was used longest ago is dropped and read again when it is next needed.
| Remembered | Limit |
|---|---|
| Pointers, whether they exist or not | 1024 |
| Decoded roots and chunks of path indexes and index roots, and the directory and form listings made from them | 512 MiB, set by --cache |
A decoded path index takes 72 bytes and the length of its path for each file, so a view of a million files whose paths are 60 bytes long takes about 126 MiB. A cache that holds the indexes of the views that are asked for answers every lookup without a read; a smaller one reads the chunks it dropped again.
The server of sekai-exporter serve also limits what clients hold:
| Held by clients | Limit |
|---|---|
| Connections at once | 256, set by --connections. Further connections wait until one closes |
| The head of a request | 16 KiB, sent within 30 seconds; a longer head is 431. A connection is kept for its next request for as long |
| A connection on which no byte moves | Closed after 60 seconds |
The server has no limit for each client and no lowest transfer rate. Where anyone can reach it, it belongs behind a reverse proxy or a CDN.
sekai-exporter serve <output> --routes <file> [--listen <address>] [--threads <n>]
[--connections <n>] [--cache <bytes>]The command is built with the serve feature, which full includes. <output> is the output directory of export or update: pointers are read from <output>/published, and objects from <output>/store. The server only reads the directory, and does not start when it does not exist. --listen is 127.0.0.1:8080 by default, and --threads, the number of threads that answer requests, is the number of logical CPUs by default. --connections and --cache set the limits; --cache takes a number of bytes such as 1G. The resolver serves HTTP/1.1; TLS and HTTP/2 belong to a reverse proxy or CDN in front of it. A client may close its sending side of a connection once its request is sent, and is answered all the same.
The server writes one line to its standard error stream for each request it answers with 500, with the pointer or the object and what is wrong with it. When the stream takes no line, as a pipe does whose reader is gone, the lines are lost and the requests are answered as before.
The Dockerfile at the root of the repository builds an image that holds the command built with the serve feature only. Each commit on main is published as ghcr.io/starmoe-org/sekai-exporter for linux/amd64 and linux/arm64, tagged main and with the first 12 characters of the commit hash. Each release v<version> is also tagged <version>, and a release that is not a pre-release is tagged with its minor version too, such as 0.1. The image runs as an unprivileged user and only needs to read the output. The full target of the Dockerfile holds the command built with every feature, with ffmpeg, and is published as the same image with the same tags after full- (full for main), such as full-0.1; it runs update and gc as well:
docker run -p 8080:8080 -v /path/to/output:/data:ro -v /path/to/routes.json:/routes.json:ro \
ghcr.io/starmoe-org/sekai-exporter:main serve /data --routes /routes.json --listen 0.0.0.0:8080The core is a Resolver: the routes, the checks, what is remembered, and the responses. A host starts a lookup for each request (Resolver::lookup) with the time of its own clock, and makes the reads the lookup asks for: the pointer of a view, the root of its path index and a chunk for a file, the chunks of a listing for a directory, or the index root and form listing chunks for a form listing, each with the most bytes it may give. The lookup ends with a response: a status, headers, and either no body, a short text, a document such as a listing, or a byte range of an object with the size the object must have. A host supplies the reads and the clock and writes the response, so every host checks, remembers and answers alike. The server of sekai-exporter serve is one host, sekai_serve::server behind the server feature of the crate, which another program can run as well. An edge worker in front of a bucket reads from the bucket and caches objects by their hash, so every path of every view that names one object shares one cached copy.