## [PPWA](https://via.hypothes.is/https://gozala.hashbase.io/posts/Native%20talk.html) ### Setup Application needs to to host a [static html file](https://github.com/Gozala/peerdium/blob/master/docs/index.html) to bootstrap itself off of IPFS network that embeds [lunet client][] implementation (or equivalent) and contain `meta` tag providing [IPFS][] path to an actual application resources. ```html <meta name="mount" content="/ipfs/QmYjtd61SyXU4aVSKWBrtDiXjHtpJVFCbvR7RgJ57BPZro/" /> <script type="module" async src="https://lunet.link/lunet/client.js"> </script> ``` Application also needs to host another [`lunet.js`] file that will be used by a [lunet client][] to setup a [service worker][] that will serve app resources from under the mounted IPFS path. In practice [`lunet.js`][] file just needs to import [lunet proxy][]. ```js importScripts("https://lunet.link/lunet/proxy.js") ``` > That is necessary because [service worker][] can not be registered only from the URL that it will be serving. ### Data Flow When application is loaded [lunet client][] will bootstrap things and then fetch resources from IPFS corresponding to the location (resolves path relative to mounting path). Then update document accordingly. Below diagram illustrates flow through which browser fetches each resource. ```sequence App -> Proxy ServiceWorker:fetch Note left of Proxy ServiceWorker:Realy thorugh client Proxy ServiceWorker -> App: App -> iframe: iframe -> lunet: relay request lunet -> SharedWorker: serve request SharedWorker -> Daemon: /api/v0/cat Daemon -> SharedWorker: SharedWorker -> lunet: lunet -> iframe: iframe -> App: App -> Proxy ServiceWorker: Proxy ServiceWorker -> App: ``` Additonally proxy service worker also will relay request towards IPFS [Daemon REST API](https://docs.ipfs.io/reference/api/http/) that are targeted at `https://lunet.link/` host. That is also exactly how [peerdium example][] [publishes](https://github.com/Gozala/peerdium/blob/960422670399a76d5bbb9aff4f2c1cf704ebf0a9/static/js/editor.js#L97-L108) and [loads](https://github.com/Gozala/peerdium/blob/960422670399a76d5bbb9aff4f2c1cf704ebf0a9/static/js/editor.js#L110-L119) documents out of IPFS network. > **Warning**: At the moment lunet will allow any embedder to read / write data into IPFS. In a future lunet (embedded in iframe) will obtain user consent before doing so. [lunet client]:https://github.com/Gozala/lunet/blob/master/docs/lunet/client.js [IPFS]:http://ipfs.io/ [service worker]:https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Updating_your_service_worker [`lunet.js`]:https://github.com/Gozala/peerdium/blob/master/docs/lunet.js [lunet proxy]:https://github.com/Gozala/lunet/blob/master/docs/lunet/proxy.js [peerdium example]:https://gozala.io/peerdium/