~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs

0
0
main
49 lines · 1.5 KB Raw History
# Use a module in a page

No install. The import map loads atlibs from jsdelivr, and `loadModule` fetches the module from its
publisher's PDS and checks it against the record.

Save as `index.html`:

```html run=network browser index.html
<!DOCTYPE html>
<meta charset="utf-8">
<script type="importmap">
{ "imports": { "atlibs/": "https://cdn.jsdelivr.net/npm/@publicdomainrelay/atlibs@0.5.0/src/" } }
</script>
<output></output>
<script type="module">
import { createPluginContext } from 'atlibs/plugin-context/index.js'
import { hostCall } from 'atlibs/run-module/host-call.js'
import { loadModule, runModule } from 'atlibs/run-module/index.js'

const did = 'did:plc:ocnuqjlzgohypj3zoznermms'
const ctx = createPluginContext({ id: 'demo' })
const hello = await loadModule(ctx, 'bobvmbuilder.bsky.social/hello')
const { result, error } = await runModule(ctx, hello, 'count', { did, collection: 'com.fedfork.exec.wasm.module' }, hostCall({ ctx }))
document.querySelector('output').textContent = JSON.stringify(error ?? result)
</script>
```

```output
{"collection":"com.fedfork.exec.wasm.module","records":…}
```

Serve it and open http://127.0.0.1:8000/:

```bash run=network
python3 -m http.server 8000 --bind 127.0.0.1 &
```

```bash run=network
curl -s localhost:8000/ | grep -c importmap
```

```output
1
```

Signed out, a module makes public reads only. Sign-in: [Sign in from a page](../browser/oauth.md). A
fuller page (components, a cubit, tests): `examples/web/hello`, [Run modules in a page](../browser/run-modules.md).

Next: [Node](node.md)