Packages
Bundle functions, a face, and their files into a package another program imports
A package is a folder with a kaya.json that names it. It holds Kaya
modules and any other files they use: a face's HTML, images, data. Another
package or a program imports the folder, and the functions it gets still find
their package's own files.
Packages compose. A general package can sit under a specialized one, and a program can sit on top:
storytelling/ tells a story scene by scene in a player face
kaya.json
main.kaya
player.html
pickleworths/ a show: its cast, their portraits and its own player;
kaya.json imports storytelling
main.kaya
player.html
portraits/
barnaby.png
episode-3/
main.kaya the story itself; imports pickleworthsThe story-episode example runs this project in your browser, portraits and all. The same packages can be served from a web server and imported by URL.
kaya.json
{
"name": "storytelling",
"version": "1.0.0",
"main": "main.kaya",
"description": "Tell a story scene by scene in a player face."
}name: lowercase letters, digits and hyphens, starting with a letter.version: letters, digits,.,-and+.main: the module an import of the folder brings in. It defaults tomain.kaya.descriptionis optional. Any other key is refused, so a misspelled field fails instead of being ignored.
Importing a package
Import the folder. The reference ends in /:
import tellStory from "../storytelling/"The names you import are functions defined in the package's main module. Importing a package runs none of its code and mounts nothing. Its functions run when you call them.
A package's modules may import one another, as ./scenes.kaya, and other
packages, as ../storytelling/. A path that leaves the package is refused
before the program runs.
A package's own files
A package declares the files it uses with resource, and its code reaches
them with asset(path:). The path is resolved beside the module whose code
calls asset(), so it finds the package's file wherever the caller lives:
// storytelling/main.kaya
resource "player.html"
function tellStory(title: string, scenes: json, face?: file): string {
let player = face ?? asset(path: "player.html")
mount(component: "story-player", face: player, reads: ["story.scene"], sends: {"story.next": {zodTypeName: "Object", shape: {}}})
// ...
}Each package keeps its files apart. In the session they live under
.packages/<name>@<version>/, so storytelling and pickleworths can both
ship a player.html. A package's resources keep their own paths: as, in,
from and resources are for programs, not packages.
Specializing a package
A show changes what it needs by passing its own values. Here the show passes its own player face to the storytelling driver:
// pickleworths/main.kaya
import tellStory from "../storytelling/"
resource "player.html"
function playEpisode(title: string, scenes: json): string {
return tellStory(title: "The Pickleworths: {{title}}", scenes: scenes, face: asset(path: "player.html"))
}// episode-3/main.kaya
import playEpisode from "../pickleworths/"
let scenes = []
scenes.push(value: { speaker: "Barnaby", line: "Has anyone seen the garden gnome?" })
let ending = playEpisode(title: "Episode 3", scenes: scenes)
"{{ending}}"A face a package supplies is a face the program writes:
the viewer asks before it runs it the first time. A package's images reach its
face as pinned files: asset() returns each one with its size and digest, and
the face shows it with kaya.media().
Identity and copies
A package is identified by its name and version, not by its folder. Moving a
package's folder changes nothing for the program that imports it. One program
may use one copy of a package version: importing storytelling@1.0.0 from two
places, such as two folders or a folder and a URL, is refused.
When a session is created, it copies the files of every package its program
imports, and those of every package they depend on, into the session. From
then on the run reads only those copies, and so does a paused session that
resumes later: changing or moving a package's folder does not affect a session
that has already started. Each copy is checked against the digest the session
recorded: a changed copy stops the run with a copyMismatch error that gives
the expected and actual digests, and a removed copy stops it with a
copyMissing error that gives the digest it should have had. If the program
used packages when the session began, a package import added afterwards, for
example to a module edited since, is refused; start a new session to use it.
A URL package import added afterwards is refused even if the session began
without packages, because URL packages are fetched only when a session is
created.
Packages from a URL
A package can also be served from a web server. Import it by the URL of its
folder, which ends in /:
// episode-3/main.kaya
import playEpisode from "https://example.com/packages/pickleworths/"A package works the same from a URL as from a folder: the names you import,
its resources, asset(), and the copies a session holds. Its own references
resolve against its URL. Served from https://example.com/packages/, the
show's import tellStory from "../storytelling/" imports
https://example.com/packages/storytelling/, and its resource "player.html"
is https://example.com/packages/pickleworths/player.html.
Kaya fetches a URL package when a session is created, and only then. The
session keeps its copies, so neither its run nor a later resume fetches again,
and a change at the address never reaches a session that has already started.
A launch with a launchKey may import nothing, so one whose program imports
anything is refused before anything is fetched.
kaya check never fetches a package. When a program uses a URL package,
kaya check warns that its check is incomplete, and it checks no imported name
or function call anywhere in the program, local modules included.
Addresses
- Use
https://. Plainhttp://is accepted only for this machine:localhostor a name under it, such asapp.localhost, or a loopback address, such as127.0.0.1or[::1], as inhttp://localhost:8000/storytelling/.kayadstill asks the system's resolver for such a name, and fetches only when every address it answers with is loopback. - End the URL in
/, because it names the package's folder. A single module by URL, such ashttps://example.com/packages/storytelling/main.kaya, is refused before anything is fetched: a module has nokaya.jsonto name it, so import its package's folder. - Leave out any user name or password, query or fragment, even an empty
?or#. - Use a host name, or a public or loopback address. A private or special-use
address, such as
10.0.0.1or169.254.169.254, is refused. - Keep the path plain: no empty,
.or..segments, backslashes, spaces or control characters.
Kaya keeps each package root in one spelling, with the scheme and host in
lowercase and no default port, so HTTPS://Example.com:443/packages/storytelling/
is the same root as https://example.com/packages/storytelling/.
Kaya fetches each file with one GET and sends no cookies, credentials or
Referer. The server must answer with status 200; 404 or 410 means the file is
missing. Kaya never follows a redirect, so import the final address. An HTML
page is refused where a kaya.json, a module or any other file that is not
itself a page (.html, .htm, .xhtml) belongs, so a server that answers a
missing file with a page is never mistaken for that file. Modules must be
UTF-8.
kaya.lock
The first run that uses a package root pins every file it reads under that
root in kaya.lock, beside the program: each file's sha256 and size. Every
later run checks each file against its pin before the program starts. A
program that imports only the storytelling package gets this lock:
{
"lockVersion": 1,
"packages": {
"https://example.com/packages/storytelling/": {
"files": {
"kaya.json": {
"sha256": "4a9e6bb27a352ed06c49b7d0cec020413d858ef8b6c8a6e42be63233b6ec8712",
"size": 140
},
"main.kaya": {
"sha256": "e4574995d63b01ac68fd37fc7999ec68292a40804986e12e3ddb7c7f190b9d70",
"size": 902
},
"player.html": {
"sha256": "dcd2d42c7d47671b671551e91baecaa80a1d10362e931734b9b41a75e236724c",
"size": 2400
}
},
"id": "storytelling@1.0.0"
}
}
}- The lock sits beside the program's real path, after symlinks are resolved, and every program in that folder shares it. A URL package that a local module or a folder package imports is pinned in the program's lock too.
- A root is pinned whole, by the run that first uses it, once the program is
admitted. A refused program pins nothing, unless writing
kaya.lockitself failed: that error says the pins may have been written anyway. Files fetched before a refusal can stay in the source cache, which serves a file only when it matches a pin. - A run only adds roots the lock does not pin yet. It never rewrites a pin that is already there, and pins for roots the program no longer uses stay until you remove them.
- Folder packages are never pinned.
- Commit
kaya.lockwith the program, so everyone who runs it checks the same bytes. A program whose lock already pins every root it uses never writes the lock, so it also runs from a read-only checkout.
When a file Kaya fetches no longer matches its pin, the program is refused
before it starts with a pinMismatch error that names the file and gives the
expected and actual digests. Either the package changed at that address or the
server sent something else. Kaya never repins a root by itself: if you meant to
take the change, unpin the root and run again; if not, find out why the file
changed.
A kaya.lock that does not parse is refused and never rewritten: fix it, or
delete it to pin every URL package again.
Updating a package
Unpin a root to take what its address serves now:
kaya packages unpin episode-3/main.kaya https://example.com/packages/storytelling/
kaya packages unpin episode-3/main.kaya --allThe next run fetches the root again and pins what it reads. Removing the
root's entry from kaya.lock by hand does the same, and deleting the file
unpins every root. A new version at a new URL needs no unpin: change the
import, and the new root is pinned on first use. See
kaya packages unpin.
A session that has already started keeps the copies it was created with, so an unpin changes only new sessions.
Running offline
The native daemon, kayad, keeps the files it fetches in a source cache,
cache/sources under its Kaya home ($KAYA_HOME, by default ~/.kaya). A
pinned file comes from the cache when the cached bytes match its pin, and from
the network only when they do not. So once a program's packages are pinned and
cached, a new session needs no network. A cached file is never used without a
pin that matches it.
Deleting the cache is harmless, and kaya cache clear does so with the
default paths: the next run fetches the files again and checks them against
their pins.
What a URL package may import
- Its relative package references resolve against its own URL, on the same
server. It never names a folder, file or library on this machine: a
/-absolute path, afile://orlib://reference, or any other scheme is refused. - A package from the public internet may import only packages on the public internet, never one on this machine.
- A package on this machine, at
localhost, a name under it or a loopback address, may import packages on this machine and on the public internet.
Your program, its local modules and its folder packages may import any URL
package. When kayad fetches a file, every address the host name resolves to
must match the package's place: public for a package on the internet, loopback
for one on this machine. It connects only to the addresses it checked, and
never through a proxy.
A face a URL package supplies is a program-supplied face like any other: the viewer asks for approval of that exact face, digest and contract before it loads.
In the browser
The browser runtime imports URL packages by the same rules and writes the same
kaya.lock, with three differences:
- A page cannot see DNS answers, so the runtime judges a package's place by its address alone, and the browser decides which networks the page may reach.
- The browser decodes a compressed response before Kaya sees it, so a file
sent with a
Content-Encodingis accepted and pinned as its decoded bytes.kayadasks for each file as stored, and refuses a compressed response or, over HTTP/1, one with neither a length nor chunked framing. A server that sends files as stored gives both runtimes the same digests. - A server on another origin than the page must allow cross-origin reads by
sending
Access-Control-Allow-Originwith every file. Without it, the browser refuses the response and does not say why. Each request is a GET without credentials, soAccess-Control-Allow-Origin: *is enough.
The browser runtime keeps the files it fetches in its own source cache,
.kaya-cache/sources in the runtime's namespace in the browser's
origin-private storage. As with kayad, a pinned file comes from the cache
when the cached bytes match its pin, so once a program's packages are pinned
and cached, a new session in that namespace needs no network for them.
Another namespace starts with an empty cache: a run there fetches the pinned
files again and checks them against their pins.
In the Kaya IDE on this website, the program's kaya.lock appears beside the
entry file, marked generated and read-only: from the start when the example
ships one, and otherwise once a run that pins URL packages starts. The next Run
stages it with your files, so that run checks every file against the same pins.
Delete it to unpin every root: the next Run pins them again. The page keeps a
generated lock only while it is open: after a reload, or in another tab, the
example starts again from the lock it ships, if any, and a run without a lock
pins its packages afresh. Opening your previous run brings back its lock.
Limits
A program's packages, from folders and URLs together, may number at most 64 and hold at most 128 MiB. One package may hold at most 512 files, and one file at most 16 MiB.
Errors
A package problem is a PackageError, located at the statement it concerns,
usually an import or resource statement, with a suggestion that says what to
do. Its additionalDetails.package names the kind and where it happened,
such as the url, root, lock or a copy's path, and a mismatch adds the
expected and actual digests.
| Kind | What happened | What to do |
|---|---|---|
address | The URL breaks an address rule | Fix the address |
urlModule | A single module was imported by URL | Import its package's folder URL, ending in / |
notFound | A file is missing (404 or 410); for kaya.json, nothing at the URL is a Kaya package | Check the address, and that the server has the file |
redirect | The server answered with a redirect | Import the final address |
status | The server answered with a status other than 200 | Fix the server, or use another address |
html | An HTML page arrived where a file belongs, often a "not found" page | Check the address |
encoding, framing | A module is not UTF-8, or kayad got a compressed or unframed response | Save modules as UTF-8; serve files as stored |
reach | A package imports what its place does not allow, or its host resolves outside that place | See what a URL package may import |
escape | A module or resource path leaves the package, or a URL package's relative package reference climbs above its server's root or holds an empty segment, a backslash, %, ?, # or : | Keep paths inside the package; name another package by a relative folder path or by its URL |
pinMismatch | A fetched file differs from its pin | If the change is intended, unpin the root and run again |
lockIncomplete | kaya.lock pins the root without a file the program reads, so the entry was edited by hand | Unpin the root and run again |
lockInvalid | kaya.lock does not parse, or pins the root as another package | Fix kaya.lock, or delete it |
lockConflict | Another run pinned the root to other bytes first | Run again, to check against its pins |
twoPlaces | One name@version comes from two places | Import each package version from one place |
tooLarge, budget | A file, package or program is over a limit | Make it smaller |
noLockSite | The program is not a file in a folder that can hold kaya.lock | Run the program from a file |
unavailable | This host does not fetch URL packages; or, during a run, an import names a URL package and the session began without packages | Import a copy of the package's folder; during a run, start a new session |
notAdmitted | During a run, an import names a package the session was not created with | Start a new session |
copyMismatch, copyMissing | During a run, a package's copy in the session was changed or removed after the session made it | Start a new session |
componentResource | renderHtml was given a file in a package's copy as its component, which the renderer would read unchecked | Render a component the program declares with resource |
Network and storage failures are internal errors rather than PackageErrors,
with the same details: unreachable, timedOut, incomplete and cancelled
for a fetch, and lockRead, lockWrite and lockBusy for kaya.lock. Run
again once the server or the disk recovers. In a read-only folder, a run that
would pin a new root fails with lockWrite: commit a complete kaya.lock
instead.
Not yet
- Skills inside a package.
- Rendering a package's own component with
renderHtml.