Solid Accord
    Preparing search index...

    Module @viboko/solid-accord-browser

    @viboko/solid-accord-browser: the client a web app uses to save a child's work, in the browser, and to their Pod once they connect one: what Solid Accord does with the Pod, and the types an app needs with it.

    @viboko/solid-accord-browser

    The client a web app uses to save a child's work: in the browser, and to their own Solid Pod once they connect one, loading it back into the app after the Solid login. It uses @viboko/solid-accord-core, which needs no browser, for the app's Client ID Document.

    An app uses it by telling it what a piece of its work is (a title and a file), saving it, and letting the user connect their Pod. The first app built on it is solid-blocks, for block-based coding.

    Built on Inrupt's JavaScript client libraries. MIT licensed.

    This is v0.1: an app has one current work. It's deliberately small, and shows the direction: listing works and opening an earlier one come later, behind the same interface.

    • Saving without a Pod. A save keeps the work in the browser, in IndexedDB, so the user can start before they have a Pod, or before they connect it. The first save starts a new work, and every save after it replaces it.
    • Connecting a Pod. The app asks the user for their WebID and connects it. The client reads the Solid OIDC issuer from their profile and sends the browser there to log in. When the browser comes back, it saves the current work to the Pod, and every save after that goes to the Pod too.
    • Where work lives in a Pod. Each app gets one container at the root of the Pod, such as solid-blocks/, which it registers in the user's private type index: see The works' container. Each work is a container inside it, named by a UUID that the client makes, holding the content as content.<ext> and a meta.ttl that gives its title, type and last change, and names the shape it meets. The title is only metadata, so renaming a work changes nothing about where it lives. Apps never see the UUID.
    • Reading afresh. What Solid Accord reads from a profile or a Pod, it asks the server for every time, rather than taking the browser's cached copy, which may be out of date: a Pod's answers usually say nothing about caching, so a browser can reuse one for a while it guesses. An unchanged document costs only a short answer saying so.
    • Lapsed sessions. The session is restored on every page load. If it has lapsed, a save logs in again with the connected WebID, without asking, keeping the work in the browser while the page is away.
    • Disconnecting. It logs out, and forgets the WebID and the current work in this browser, which matters on a shared computer. The next save starts a new work.
    • Client ID Documents. The app identifies itself to a provider with a Solid-OIDC Client ID Document, rather than registering itself afresh. Core makes it: see The Client ID Document.

    Sharing (requests, decisions, guardian links and the rule that decides whether a share is approved) comes later.

    // src/identity.ts: how the app identifies itself to a Solid provider, which vite.config.ts gives the plugin too
    import type {ClientIdentity} from '@viboko/solid-accord-browser';

    export const IDENTITY: ClientIdentity = {clientName: 'Solid Blocks'};
    import {createClient} from '@viboko/solid-accord-browser';
    import {IDENTITY} from './identity';

    const client = createClient(IDENTITY, {
    containerName: 'solid-blocks',
    fileExtension: 'sb3',
    contentType: 'application/x.scratch.sb3'
    });

    // Everything to show about saving: whether a Pod is connected, and where the work is. It's heard on every change,
    // including each step of a save, of connecting a Pod, and of coming back from logging in.
    client.onStateChange = ({pod, work}) => showSaving(pod, work);

    // On every page load: puts the work back in the editor straight away, then restores the session, and finishes
    // connecting if the page left to log in, which writes the work to the Pod.
    await client.resumeSession(work => putBackInEditor(work));

    // When the user saves: to the browser, then to the Pod if one is connected.
    await client.save({title, data});

    // When the user connects their Pod: the work is kept in the browser, and the page leaves to log in. If it can't, the
    // state's pod is `failed`, and its failure's code says whether to ask for the WebID again.
    await client.connectPod(webId, {title, data});

    // When the user disconnects it.
    await client.disconnectPod();

    createClient(identity, options) returns a Client. There's one for each app. Its identity is how the app identifies itself to a Solid provider, as a ClientIdentity: the same object core's Vite plugin is given, made into URLs on the page's origin. Its options say where the app's work is saved:

    Option
    containerName The name the app would like for the container, at the root of the Pod, that its works are saved in, such as solid-blocks: see The works' container. It also names the browser storage the client uses, accord-<containerName>, so two apps on one origin keep their work apart
    fileExtension The extension of a work's content file, without the dot, such as sb3
    contentType The media type a work's content file is written with
    workKind Optional. What kind of work the app makes, which each work's meta.ttl gives: see The kind of work. Without it, a work is a schema:CreativeWork
    logger Optional. Where failures are logged: those reported as a status, those disconnectPod can't report, and errors thrown by the app's own restoreWork and onStateChange. Defaults to console

    workKind says what the app's works are, so that other apps can find them. It's a class, type, written as the work's rdf:type, and properties that point at shared concepts, such as Wikidata's. Solid Accord doesn't keep a list of kinds of work: the app chooses its terms, from schema.org where it has them.

    // A Scratch project: source code in the programming language Scratch
    workKind: {
    type: 'https://schema.org/SoftwareSourceCode',
    properties: {'https://schema.org/programmingLanguage': 'http://www.wikidata.org/entity/Q275131'}
    }

    // A haiku: a creative work that's a poem, in the genre haiku
    workKind: {
    properties: {
    'https://schema.org/additionalType': 'http://www.wikidata.org/entity/Q5185279', // poem
    'https://schema.org/genre': 'http://www.wikidata.org/entity/Q37707' // haiku
    }
    }
    • Every value is a URL, never text, so that another app can match it: Wikidata's charcoal, not "charcoal". A property can have several values, as an array.
    • Solid Accord writes some properties itself, so a kind can't give them: rdf:type (that's type), dcterms:conformsTo, schema:name, schema:encoding and schema:dateModified.
    • createClient throws a TypeError for a kind that breaks either rule, so the mistake shows as the page loads.
    • It's the same for every work the app saves, for now.

    Saves work ({title, data}) as the current work: in the browser, then, if a Pod is connected, to the Pod. The first save starts a new work. Every save after it replaces that work, whatever its title, until disconnectPod.

    Situation What it does Returns
    No Pod is connected Keeps the work in the browser {status: 'saved-locally'}
    A Pod is connected, with a session Keeps it in the browser, then writes it to the Pod {status: 'saved-to-pod'}
    The session has lapsed Keeps it in the browser, then logs in again with the connected WebID, without asking. resumeSession writes it when the page comes back {status: 'logging-in'}
    Something fails Keeps it in the browser if it got that far {status: 'failed', message, code, params}: see Errors

    Each step is the state's work as it happens, so onStateChange hears it: saving-locally then saved-locally with no Pod, or saving-to-pod then the outcome with one. Keeping the work in the browser on the way to the Pod is part of saving to the Pod, not a step of its own.

    It never rejects: a failure is a status. If the connected WebID can no longer log in, as when its Pod has gone, it's failed with a code such as profile-unreadable or no-oidc-issuer, and the work stays in the browser. The Pod stays connected, so the app can suggest disconnecting and connecting another.

    Connects the user's Pod. If work is given, it's kept in the browser as the current work first, so it's safe while the page is away and is written to the Pod when the page comes back. Then it reads the Solid OIDC issuer from the WebID's profile and leaves the page to log in there. resumeSession finishes connecting when the page comes back, and writes the current work to the Pod, if there is one.

    It never rejects. The state's pod is connecting, with the WebID, until the page leaves, and work is saved-locally once the work is kept: from then on, leaving the page loses nothing, so an app that warns before unsaved work is left can stop warning. If the login can't start, the page stays, and pod is failed, with the WebID and the failure. When its code is invalid-web-id, profile-unreadable or no-oidc-issuer, the problem is the WebID, so the app can say why and ask for it again. invalid-web-id comes before anything else is tried, so an app that checks the WebID is a web address itself, to say so at once, never sees it. The other codes it can fail with, network-failed, login-failed and browser-storage-failed, aren't about the WebID.

    Ends the session in this app, forgets the WebID, and forgets the current work, removing it from this browser. The work already in the Pod stays there. The next save starts a new work, kept in the browser until a Pod is connected again, so connecting the same Pod again makes a second work rather than replacing the first. The user stays logged in at their provider, which may offer to continue as the same account at the next login.

    The state is then not-connected, with no work. It never rejects. If the browser's storage wouldn't let it forget all of that, the failure goes to logger, and it tries every step even so, forgetting the WebID first, so unless that's the step that failed, the next save doesn't go to this Pod.

    Call it on every page load, once onStateChange is set. In order, it:

    1. reads the current work from the browser, sets the state's work to say where it's kept, and calls restoreWork with it, or with null if there isn't one, to put it back in the editor. If restoreWork returns a promise, that's waited for;
    2. restores an earlier session, so later saves need no login;
    3. if the page is returning from logging in, finishes connecting: the state's pod goes from connecting to connected, and then the current work, if there is one, is written to the Pod, with work going to saving-to-pod and its outcome. If the Pod doesn't connect, pod is failed instead, and nothing is written.

    It never rejects: a failure is in the state. If the current work can't be read, work is failed with browser-storage-failed, and restoreWork gets null. If an earlier session can't be restored, that's logged but not reported: it's like a lapsed session, and the next save to the Pod logs in again. If restoreWork throws or rejects, that's logged too, and the rest goes on: the work is still kept in the browser, so a login still finishes and writes it to the Pod.

    The state is everything an app shows about saving, as a ClientState, {pod, work}. onStateChange, a property the app sets to a function, is called with it each time it changes. Nothing is heard until something happens, so the app learns where things stand as the page loads from resumeSession, whose first step reports the state before restoreWork is called. work is the same object until the work's part of the state changes, so an app can tell a change to the work from a change to the Pod. If it throws, that's logged through logger, and whatever changed the state goes on, so a mistake in showing the state never stops a save or a login part way through.

    pod is a PodState: whether saves go to a Pod.

    pod.status
    not-connected No Pod is connected, so saves stay in this browser
    connecting A Pod is being connected: connectPod is getting the login ready, or the page has come back from logging in and resumeSession hasn't finished. webId is the one being connected, or null if the browser wouldn't keep it. It's connecting from the moment the page loads, so an app can show it straight away. Logging in again for a lapsed session comes back connecting too
    connected Saves go to the Pod of webId
    failed Connecting the Pod of webId (null if it isn't known) didn't work, and failure says why: see Errors. No Pod is connected. It lasts until the next connectPod or disconnectPod, or the next page load

    work is a WorkState: where the current work is. It's {status: 'none'} when there isn't one, or until resumeSession has read it, and otherwise a SaveStatus, where the work's last save got to:

    work.status
    saving-locally, then saved-locally With no Pod connected, the work is being kept, then is kept, in this browser. With a Pod connected, saved-locally says it's kept here but its last version isn't in the Pod yet
    saving-to-pod, then saved-to-pod The work is being written, then is written, to the Pod
    logging-in The session had lapsed, so the page is leaving to log in again, with the work kept safe. It's written to the Pod when the page comes back
    failed It went wrong, saying why: see Errors. The work is still kept in this browser, unless keeping it there failed

    work.kept says whether the work is safe, so leaving the page loses nothing: true when it's saved-locally, saved-to-pod or logging-in, and false otherwise. An app that warns before unsaved work is left stops warning when it's true, until the work is changed again. It's false after a save fails, so the warning stays until one lands.

    client.onStateChange = state => {
    if (state.work.kept) unsavedChanges = false;
    };
    • The types Client, ClientOptions, ClientState, PodState, WorkState, Work, SaveStatus, SaveResult, FailedStatus, WorkKind and ClientIdentity.
    • The types AccordErrorCode, AccordErrorParams and AccordErrorParamsByCode: see Errors.

    ClientIdentity is core's, given here too so an app imports its types from this package alone.

    The works go in the container that the user's private type index registers for the app's kind of work: the class of its workKind, or schema:CreativeWork. A registration is how a container is claimed, so Solid Accord never writes in one that something else uses, and other apps the user logs in to can find the works by their kind. On the first save to a Pod, the client:

    1. Uses the registered container, if the index registers one in this Pod for that class, whatever it's called. Apps that make the same kind of work share it, as the type index intends.
    2. Otherwise picks one, and registers it before writing anything in it: containerName, or if that's taken, <containerName>-2, -3 and so on. A container that doesn't exist is free. One that does is taken only if it already holds works of this kind, as one saved before registrations were made would. One that's empty or holds anything else belongs to something else, and is left alone.
    3. Makes the private type index if there isn't one, at settings/privateTypeIndex.ttl, linked from a preferences file, settings/prefs.ttl, which it makes and links from the profile if there isn't one either. Community Solid Server makes neither. It never replaces a document that's there.

    Every read asks the Pod afresh, since each one decides what's written. If the type index can't be used, as when the profile can't be changed, the container is chosen the same way without being registered, and the failure is logged. The client looks for it once per Pod.

    Every failure the client reports has a code. It's a failed status ({status: 'failed', message, code, params}), as the state's work, as the failure of a failed pod, or as what save returns. No method throws or rejects, not even when the app's own restoreWork or onStateChange does. There are nine codes, and each always comes with the same params:

    code What went wrong Where it can come from params
    invalid-web-id The WebID isn't a web address (an http or https URL), so there's no profile to read Only connectPod, before it reads or keeps anything webId: the WebID as given, without whitespace around it
    profile-unreadable The WebID's profile couldn't be read: the server answered with an error, or it isn't RDF. A Pod server may answer HTTP 401 rather than 404 for a profile that doesn't exist, so this can't say which it was connectPod. save, when it reads the profile to find the Pod or to log in again. Writing the work to the Pod after resumeSession connects it webId: the WebID whose profile was read. status: the HTTP status the server answered with, a number, if it answered with one
    no-oidc-issuer The profile names no Solid OIDC issuer (solid:oidcIssuer) to log in with connectPod. save, when it logs in again webId
    no-pod-storage The profile lists no Pod storage (pim:storage), so there's nowhere to write the work save, with a Pod connected, and the write after resumeSession connects it. The work stays in the browser and the Pod stays connected webId
    login-not-completed The page came back from the identity provider without a session, as when the user cancelled the login Only resumeSession, as the pod's failure. The Pod isn't connected, and the pod's webId lets the app offer it again None: params is {}
    login-failed The provider wouldn't start the login, as when it refuses the app's Client ID, or the login couldn't be finished when the page came back connectPod. save, when it logs in again. resumeSession, as the pod's failure: the Pod isn't connected webId
    pod-write-refused The Pod answered with an error when writing a file of the work, as when it's full or the user may not write there. A 401 from save isn't one: it logs in again instead save, with a Pod connected, and the write after resumeSession connects it url: the file it was writing. status: the HTTP status the Pod answered with, a number, if it answered with one
    network-failed A server couldn't be reached at all, as when the network is down connectPod. save, with a Pod connected, and the write after resumeSession connects it url: the profile, provider or file it was trying to reach
    browser-storage-failed The browser's storage (IndexedDB or localStorage) couldn't be used, as when it's blocked or full save, which then doesn't try the Pod. connectPod, which then doesn't leave the page. resumeSession: as the work's state when it can't read the work, or the pod's failure when it can't read the login or remember the WebID, so the Pod isn't connected and the work isn't written None: params is {}

    In TypeScript, FailedStatus is a union with one member for each code, so checking code narrows params to that code's. AccordErrorParamsByCode gives each code's params.

    Solid Accord holds no translations. message is always English, for logs and for apps with only one language. To describe a failure in the user's language, an app picks its wording by code and fills in the params.

    The package is published on npm. A project installs it with core, whose Vite plugin builds the app's Client ID Document:

    npm install @viboko/solid-accord-browser @viboko/solid-accord-core
    

    From the root of the repository, just test, just typecheck and just build run these for every package. In this folder, npm test, npm run typecheck and npm run build run them for this one.

    AccordErrorParamsByCode
    Client
    ClientIdentity
    ClientOptions
    ClientState
    Work
    WorkKind
    AccordErrorCode
    AccordErrorParams
    FailedStatus
    PodState
    SaveResult
    SaveStatus
    WorkState
    createClient