Using HyperMap in a web page

@hypermap/polyfill makes a web page a HyperMap user agent. It's shaped as the interface browsers could one day provide natively, navigator.hypermap, and has no dependencies. Nothing runs anywhere but in the page and on the service itself: the page fetches the service's JSON directly and runs its scripts in sandboxes.

Installing

From npm:

npm install @hypermap/polyfill
import '@hypermap/polyfill/global';

Or straight from a CDN:

<script type="module" src="https://cdn.jsdelivr.net/npm/@hypermap/[email protected]/src/global.js"></script>

The /global entry point installs navigator.hypermap and the Hypermap… classes as globals. To leave globals alone, import what you need from @hypermap/polyfill instead, and make your own new HypermapUserAgent().

It works in Chrome and Edge 110, Firefox 115 and Safari 16.4 or later. The README lists the few ways browsers differ.

Opening a resource

const frame = await navigator.hypermap.open('https://todos.example/');

A HypermapFrame is like an <iframe>: it holds one resource at a time, navigates, and keeps its own history. Its hypermap is the current resource, the HyperMap equivalent of a document:

const { hypermap } = frame;
frame.url;     // the resource's URL, after any redirects
frame.status;  // its HTTP status

Reading

Address nodes with JSON Pointer paths, or walk the tree from hypermap.root:

hypermap.node('/todos/0/title').value;   // 'Learn HyperMap'

for (const todo of hypermap.root.at('todos').values()) {
  console.log(todo.at('title').value, todo.path);  // …, '/todos/0'
}

Maps (HypermapMap) and lists (HypermapList) work like Map and arrays, with at(), has(), size, keys(), values() and entries(). Values (HypermapValue) have a value. Every node has a parentNode, key, path, and attributes, read with getAttribute().

To render the data, use hypermap.toJSON(), which is also what JSON.stringify(hypermap) gives: the data, with controls marked as {"#": {"type": "control"}} but not described. Pages shouldn't depend on URLs, methods or relation types, any more than a person reading a web page depends on its HTML. For debugging, toSource() on any node gives everything.

Acting

Fill in a value as a person would with input(), and use a control with use(), optionally filling in its fields first:

await hypermap.input('/newTodo/title', 'Buy milk');
await hypermap.use('/newTodo');

// or in one step
await hypermap.use('/newTodo', { title: 'Buy milk' });

Both are asynchronous, because the resource's scripts can respond to them, and cancel them. A control with an href then makes its request, and the frame navigates to the result: frame.hypermap is now a different resource. Navigate directly with frame.navigate(url), and through history with frame.back() and frame.forward().

Watching for changes

The resource's scripts, and navigation, change what the page shows. Watch with a HypermapMutationObserver, which works like the DOM's MutationObserver, and with the frame's navigate event:

const render = () => show(frame.hypermap.toJSON());
const observer = new HypermapMutationObserver(render);

const watch = () => {
  observer.disconnect();
  observer.observe(frame.hypermap.root, {
    children: true, values: true, attributes: true, subtree: true,
  });
  render();
};

frame.addEventListener('navigate', watch);
watch();

The frame also fires load when the resource's scripts have started, error when one fails, and close. Close a frame you're finished with, to stop its scripts:

frame.close();

Capabilities

HyperMap never uses cookies or other ambient authority. A service grants the authority to use a control by attaching a capability to it. The page's user agent keeps capabilities out of the tree and out of scripts' reach, and presents them when the control is used. The page itself can read them:

frame.capabilityFor('/newTodo');

Serving to web pages

Services need to send CORS headers, so that pages on any origin can read them. Converting an API lists them, under Browser Access.