Skip to content
Comvi

@comvi/core/devtools

View .md

Browser-extension discovery for a @comvi/core instance, the loader the extension calls to bring the in-context editor onto the page, plus the editor bridge the in-context editor and the SSR adapters share.

  • @comvi/core/devtools: devtools, attachDevtools; types DevtoolsOptions, ComviQueue, ComviQueueEntry, ComviHook, EditorLoaderSlot, EditorLoadResult, EditorLoadFailureCode.
  • @comvi/core/editor-bridge: EDITOR_MAPPINGS_GLOBAL, EDITOR_INITIAL_MAPPINGS_GLOBAL, readEditorMappings, toRecordOfNumbers; type InContextEditorMappings.
function devtools(options?: DevtoolsOptions): <T extends I18n<any>>(i18n: T) => T
function attachDevtools<T extends I18n<any>>(i18n: T, options?: DevtoolsOptions): T
Option Type Default Meaning
instanceId string comvi-<n> when exposing Stable id for the instance
exposeGlobal boolean true in a browser, false under SSR Publish the instance on window.__COMVI__; false also leaves instanceId unset
editorVersion string 'latest' Exact @comvi/plugin-in-context-editor version the editor loader requests, '0.6.0'. Leaving it out, or passing 'latest', requests latest. Every other value has to match the exact-version grammar, major.minor.patch with no leading zeros and an optional pre-release suffix such as 0.6.0-rc.1, with build metadata accepted nowhere. One that does not fails closed: the loader requests nothing, answers invalid-editor-version, and warns in a development build, rather than falling back to latest

When exposing, assigns instanceId, publishes the instance on the discovery queue, installs the editor loader once per page and removes the instance from the queue on destroy(); with exposeGlobal: true and no window it assigns the id and publishes nothing. It adds no public member, so the instance type comes back unchanged. An instance that already has an id keeps it. One composed with exposeGlobal: false can be exposed later by a second .use(devtools({ exposeGlobal: true })). A base instance that never composes devtools() carries none of this: the discovery code is absent from its graph, not switched off.

src/devtools-instance.ts
import { createI18n } from '@comvi/core';
import { devtools } from '@comvi/core/devtools';
export const i18n = createI18n({ locale: 'en' }).use(devtools({ instanceId: 'app' }));
i18n.instanceId; // "app" in a browser, undefined under SSR
interface ComviQueueEntry {
v: string; // core version that published the entry
i: I18n;
}
interface ComviHook {
push(entry: ComviQueueEntry): void;
remove(entry: ComviQueueEntry): void;
}
type ComviQueue = ComviQueueEntry[] | ComviHook

The global is an array of entries, or a hook object an extension swapped in. Removal on destroy() is by entry identity. A failure inside discovery is swallowed, so it never breaks construction or teardown.

// globalThis[Symbol.for('comvi.devtools.editorLoader')]
interface EditorLoaderSlot {
protocol: number; // 1
sdkVersion: string;
editorVersion: string; // the pin, or 'latest'
url: string;
load(): Promise<EditorLoadResult>;
}
interface EditorLoadResult {
ok: boolean;
url: string;
sdkVersion: string;
protocol: number;
editorVersion: string | undefined; // the editor's own version when ok, and it reports one
code: EditorLoadFailureCode | undefined; // the failure, when ok is false
host: string | undefined; // when ok is false: the host a CSP has to allow
message?: string; // the one optional key; reserved, never set
}
type EditorLoadFailureCode =
| 'csp-blocked'
| 'load-error'
| 'timeout'
| 'no-global'
| 'invalid-editor-version'

The Comvi browser extension carries no editor. When exposing in a browser, devtools() installs this slot on globalThis, once per page, and the extension calls load() when its user presses Enable editor. load() inserts one <script crossorigin="anonymous"> for https://cdn.jsdelivr.net/npm/@comvi/plugin-in-context-editor@<editorVersion>/dist/standalone.iife.js and resolves, never rejects, within 10 seconds. Nothing touches the DOM before that call, and an instance under SSR or with exposeGlobal: false installs nothing.

code Meaning
csp-blocked A script-src policy, or Trusted Types, refused the script. Allow https://cdn.jsdelivr.net in script-src, and under Trusted Types accept that URL in the default policy’s createScriptURL
load-error The script did not load: network, a filter, or a pinned version that does not exist
timeout No answer within 10 seconds
no-global The script ran but window.ComviInContextEditor is not there
invalid-editor-version editorVersion is not an exact version, so nothing was requested; url is empty

Every key except message is always present, holding undefined where it does not apply rather than being left out, so a consumer reads result.code without a guard. host is cdn.jsdelivr.net on every failure, and editorVersion is the editor’s own reported version on success, undefined both on failure and for an editor older than 0.6.0, which reports none. On latest the request carries an hourly cache salt, ?comvi=<hour>, so the browser’s own cache holds a copy for at most an hour; an exact editorVersion is immutable and takes no query. The CDN is a cache of its own: jsDelivr’s edge can serve the previous latest for hours after a release. A pending or successful load is cached, so later calls return the same promise and one page never inserts two scripts; a failure is not cached, and the next load() starts fresh, for instance after the site’s policy was fixed. When window.ComviInContextEditor is already on the page, load() resolves ok without a request. A second core on the same page reuses the slot unless its protocol is newer; a later instance that passes a different editorVersion warns in development and the first instance’s choice stands, valid or not. The slot and its cached load promise stay after destroy(). Protocol-1 fields are never removed or retyped, and the editor accepts any protocol from 1 up.

The loader is a page global, so any script on the page can call it. That grants nothing a page script could not already do, because it can add the same <script> itself. exposeGlobal: false installs neither discovery nor the loader. The <script> global build of @comvi/core composes discovery itself, so it carries the loader and always requests latest: the global takes no editorVersion. inContextEditor() composes discovery in a development build, which installs the loader there too; its production entry installs nothing.

const EDITOR_MAPPINGS_GLOBAL: '__comviInContextEditorMappings'
const EDITOR_INITIAL_MAPPINGS_GLOBAL: '__comviInContextEditorInitialMappings'
function readEditorMappings(instance: unknown): InContextEditorMappings | undefined
function toRecordOfNumbers(value: unknown): Record<string, number> | undefined
interface InContextEditorMappings {
getKeyMappings(): Record<string, number>;
loadKeyMappings(mappings: Record<string, number>): void;
}
Export Meaning
EDITOR_MAPPINGS_GLOBAL Property on the instance the editor plugin writes the bridge under
EDITOR_INITIAL_MAPPINGS_GLOBAL Property on the instance an SSR adapter writes server mappings under, consumed once
readEditorMappings(i18n) The bridge, or undefined when absent or malformed
toRecordOfNumbers(value) Validates untrusted payload state; undefined if any entry is not a finite number