Macaw.Live.Assets.Plug (macaw_live v0.1.0)

Copy Markdown View Source

The plug that serves every file the runtime and its components load from the page's own origin (spec/06 §2.2, §3), mounted once:

plug Macaw.Live.Assets.Plug, live: MyApp.Macaw, registry: registry

GET /assets/<hex>.css answers the stylesheet registered under that name (Macaw.Live.Assets.put_stylesheet/2) as text/css with cache-control: public, max-age=31536000, immutable, since its name is its content; a name not registered, or a path of another shape below /assets, is 404. The URL never carries content: no route reflects CSS from a request, which would defeat style-src 'self' against an injected <link> (visualize D-297).

Static files (spec/06 §3): at start it builds its index from the registry (Macaw.Live.Assets.index/1), each enabled component's declared hooks: and assets:, each read once and digested, and answers GET /macaw/static/<app>/<path> with the file, the type its extension names and an immutable cache, its URL's v= its digest (Macaw.Live.Assets.url/2). A path not in the index is 404, so nothing outside a declaration is reachable: a file no component declares, a disabled package's file and a traversal alike.

It also serves the runtime's modules, each read or generated once at start: the live module at /macaw_live/live.js, the generated hooks module at /macaw/hooks.js (Macaw.Live.Hooks.module/1 of the registry), macaw_component's client module at /macaw_component/client.js and its drawn-state sheet at /macaw_component/drawn.css. Their URLs carry no version, so they are answered cache-control: no-cache: a browser revalidates them, and a deploy that changes them is seen at once.

Every other request passes on untouched, so a host mounts it after its own static files. Copied from visualize's Visualize.Sheets.Plug.

Summary

Functions

Answers what is the runtime's, halting; passes every other request on.

Reads the options with options!/1: a bad option is the host's boot failing.

The plug's options read (spec/06 §2.2): live:, the supervisor's name, and registry:, the host's Macaw.Registry, both required, with the registry's static files indexed and the runtime's modules read. {:ok, options}, or every fault at once: {[key], :required}, {[:live], {:type, :atom}}, {[:registry], {:type, :registry}}, {[key], :unknown_key}, and {[], {:type, :keyword}} for options that aren't a keyword list; then Macaw.Live.Assets.index/1's, Macaw.Live.Hooks.module/1's, and {[:files, url], {:unreadable, path}} for a runtime module its package doesn't hold.

options/1, raising its faults as an ArgumentError for a host at boot.

Functions

call(conn, options)

Answers what is the runtime's, halting; passes every other request on.

init(opts)

Reads the options with options!/1: a bad option is the host's boot failing.

options(opts)

@spec options(term()) :: {:ok, map()} | {:error, [Macaw.Base.Fault.t(), ...]}

The plug's options read (spec/06 §2.2): live:, the supervisor's name, and registry:, the host's Macaw.Registry, both required, with the registry's static files indexed and the runtime's modules read. {:ok, options}, or every fault at once: {[key], :required}, {[:live], {:type, :atom}}, {[:registry], {:type, :registry}}, {[key], :unknown_key}, and {[], {:type, :keyword}} for options that aren't a keyword list; then Macaw.Live.Assets.index/1's, Macaw.Live.Hooks.module/1's, and {[:files, url], {:unreadable, path}} for a runtime module its package doesn't hold.

options!(opts)

@spec options!(term()) :: map()

options/1, raising its faults as an ArgumentError for a host at boot.