Skip to content

aibridgejs: Cross-Context Bridge

aibridgejs solves this problem: each host runtime communicates in its own way, but you do not want platform details such as postMessage and flutter_inappwebview.callHandler scattered through your business logic. It provides a stable call / on / emit API, and the differences between hosts are encapsulated by adapters.

終端機視窗
npm install aibridgejs
Subpath Contents
aibridgejs Core: createBridge, types, error classes
aibridgejs/mock createMockAdapter: loopback, suited to development and unit tests
aibridgejs/iframe createIframeAdapter: origin-verified postMessage
aibridgejs/flutter createFlutterAdapter: Flutter InAppWebView JS handler
aibridgejs/detect detectBridgeAdapter: picks an adapter automatically from host globals

Quick Start (mock adapter, suited to development)

Section titled “Quick Start (mock adapter, suited to development)”
import { createBridge } from "aibridgejs";
import { createMockAdapter } from "aibridgejs/mock";
const bridge = createBridge({ adapter: createMockAdapter() });
await bridge.ready();
// The mock loops the event back to this listener.
const off = bridge.on("player.ready", (payload) => {
console.log("player.ready", payload);
});
await bridge.emit("player.ready", { playerId: "p1" });
off();
bridge.dispose();
import { createBridge } from "aibridgejs";
import { createIframeAdapter } from "aibridgejs/iframe";
const target = iframe.contentWindow!;
const adapter = createIframeAdapter(window, {
targetOrigin: "https://game.example.com", // Must be an exact origin; "*", a path or a trailing slash throws BridgeError
postTarget: target,
expectedSource: target,
});
const bridge = createBridge({ adapter });
await bridge.ready();
import { createBridge } from "aibridgejs";
import { detectBridgeAdapter } from "aibridgejs/detect";
// Checks flutter → iframe → mock in order; inside an iframe, iframe.targetOrigin is required
const adapter = detectBridgeAdapter(window, {
iframe: { targetOrigin: "https://host.example.com" },
});
const bridge = createBridge({ adapter });

Every envelope carries a string id used to match requests and responses. This supports multiple concurrent call()s, each matched by its id on return. A call() or emit() issued before the bridge is ready is held back by a gating mechanism and sent in call order once it is ready, regardless of which options each carries.

  • bridge.reset() rejects pending call()s and in-flight emit()s with BridgeResetError, then waits for adapter.ready() again. Registered on() listeners and adapter subscriptions are kept, so do not register them again after a reset.
  • bridge.dispose() is a permanent teardown: in-flight call()s and emit()s reject with BridgeDisposedError, and all listeners are removed.
  • An emit() without signal / timeoutMs has no time limit, but is reclaimed by reset() / dispose(). Code that awaits it must handle both errors; for fire-and-forget, write bridge.emit(...).catch(() => {}).
  • Misuse throws BridgeError before any side effect, with the message prefix aibridgejs: : missing options, an adapter missing ready / post / subscribe / dispose, a listener that is not a function, a host missing addEventListener / removeEventListener, or an iframe targetOrigin that is not an exact origin. Check with instanceof BridgeError and do not match on the full message.
  • A custom adapter must implement the four methods ready, post, subscribe and dispose (leave an empty implementation for any you do not need). The signal passed to ready() is scoped to each readiness round; both reset() and dispose() abort it, and the adapter should remove its own listeners on abort.
  • With aiecsjs: serialise the world snapshot with aiecsjs/serialize and send it through the bridge to another window or a Flutter shell
  • With aifsmjs: a machine definition is plain JSON, so it can be sent through the bridge and used to create a runtime on the other side
  • Hybrid apps: in Flutter + WebView scenarios, use it to call native features (camera, saving files, notifications)