@fluid-app/widget-runtime

中文文档
0.1.271发布于 2026/11/15TypeScriptESM + CJS近 30 天下载 2.9万

Remote DOM host and worker runtime for Fluid widget packages

原始使用文档
文档目录4 节
  1. Host
  2. Worker
  3. Threat model
  4. Non-goals

@fluid-app/widget-runtime

Remote DOM runtime for third-party Fluid widgets.

The package has two deliberately small public entry points:

  • @fluid-app/widget-runtime mounts a widget worker into a host element.
  • @fluid-app/widget-runtime/worker starts a worker containing authored React widgets and exposes declared host capabilities.

Widget discovery, descriptor validation, catalog access, publishing, and builder registration belong to portal packages and the CLI. This package owns only the host/worker execution boundary.

Host

Portal applications should use the Portal SDK host. It owns the complete reviewed Fluid UI renderer map and keeps component overlays inside the widget shadow root. The application only supplies the compiled shadow-root CSS:

import { createPortal } from "@fluid-app/portal-sdk";
import fluidComponentCssUrl from "@fluid-app/portal-sdk/remote-widget-shadow.css?url";

createPortal({
  remoteWidgets: { fluidComponentCssUrls: [fluidComponentCssUrl] },
});

The current portal application and generated portal starter include this configuration. Mist's direct live-preview host supplies the same renderer map, containment wrapper, and stylesheet as inline CSS.

mountRemoteDomWidget() is the lower-level host API. A direct consumer of a current Fluid widget worker must provide all three Fluid UI host pieces:

  • the complete reviewed componentRenderers map;
  • a wrapRemoteRoot implementation that contains portals in the widget area;
  • the matching fluidComponentCssText or fluidComponentCssUrls.

The runtime advertises Fluid UI contract v2 only when every v2 renderer is present. A Fluid UI worker therefore fails contract negotiation instead of mounting against a host with a partial renderer set.

import { mountRemoteDomWidget } from "@fluid-app/widget-runtime/remote-dom";
import {
  REMOTE_DOM_COMPONENT_RENDERERS,
  wrapRemoteDomRoot,
} from "@fluid-app/portal-sdk";
import fluidComponentCssUrl from "@fluid-app/portal-sdk/remote-widget-shadow.css?url";

const handle = mountRemoteDomWidget({
  host: document.querySelector("#widget")!,
  artifact: {
    packageId: "example.widgets",
    version: "1.0.0",
    widgetType: "Greeting",
    workerEntryUrl: "https://cdn.example.com/widget.js",
    cssUrls: [],
    assetUrls: [],
    capabilities: [{ name: "account", version: "1" }],
  },
  props: { name: "Ada" },
  componentRenderers: REMOTE_DOM_COMPONENT_RENDERERS,
  wrapRemoteRoot: wrapRemoteDomRoot,
  fluidComponentCssUrls: [fluidComponentCssUrl],
  callCapability: (capability, method, payload) =>
    portalCapabilities.call(capability, method, payload),
});

await handle.ready;
handle.updateProps({ name: "Grace" });
handle.dispose();

Each mount creates one module worker and one Remote DOM thread. Disposal releases the thread, worker, styles, event listeners, and rendered nodes. The host sanitizes all mutations, URLs, CSS, events, and serializable values before applying them to a shadow root.

Worker

import {
  startRemoteDomWidgetWorker,
  useRemoteDomCapability,
} from "@fluid-app/widget-runtime/worker";

function Greeting({ name }: { readonly name: string }) {
  const account = useRemoteDomCapability("account");
  return (
    <button onClick={() => account.call("openProfile")}>Hello {name}</button>
  );
}

startRemoteDomWidgetWorker({ widgets: { Greeting } });

Workers can render arbitrary supported HTML and CSS through upstream Remote DOM. The runtime disables storage, nested-worker, fresh-realm, and non-fetch network globals where the worker environment permits it. Host calls are limited to capabilities declared by the widget package descriptor.

fetch is denied unless the selected widget declares networkAccess version 1 and the host supplies an exact persisted grant. Granted calls go directly through the preserved native worker fetch; the runtime does not inject Fluid credentials or headers. The initial URL cannot address fluid.app, the host portal origin, loopback, or a private-network IP. DNS resolution and redirect destinations are not inspected. A redirect can therefore reach a target that would have been rejected as the initial URL. WebSocket, EventSource, WebTransport, and streaming-specific APIs remain unavailable in v1.

Threat model

Widget catalog approval is a trust decision: approved packages are trusted application code supplied by a company or Droplet owner. Worker-global lockdown, capability validation, mutation sanitization, URL policy, resource limits, and shadow-root isolation are defense in depth around that trusted code. They protect the host DOM and narrow accidental ambient access, but they do not constitute a complete network sandbox or a substitute for package review, catalog authorization, browser isolation, or server-side access control.

Network-enabled code can send any portal data available to that widget to an external service. Worker isolation does not protect the reputation of Fluid domains from package behavior; catalog, builder, and CLI approval must treat network access as a publisher trust decision.

Non-goals

  • No classic script loading or browser globals.
  • No legacy widget registration or descriptor conversion.
  • No catalog or package validation API.
  • No worker pooling, heartbeat, diagnostics framework, or version disabling.
  • No compatibility layer for legacy portal SDK hooks.

版本与发布记录

@fluid-app/widget-runtime 共发布 272 个版本,首次发布于 2026/07/23;过去 12 个月发布了 272 个版本。

版本发布日期距今
0.1.271latest2026/11/15今天
0.1.2702026/11/15今天
0.1.2692026/11/15今天
0.1.2682026/11/15今天
0.1.2672026/11/15今天
0.1.2662026/11/15今天

关于 @fluid-app/widget-runtime 的常见问题

@fluid-app/widget-runtime 是什么?

Remote DOM host and worker runtime for Fluid widget packages

如何安装 @fluid-app/widget-runtime?

在项目目录运行 npm install @fluid-app/widget-runtime,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、react-dom(>=18.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fluid-app/widget-runtime 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fluid-app/widget-runtime 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fluid-app/widget-runtime 可以商用吗?

@fluid-app/widget-runtime 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@fluid-app/widget-runtime 的下载量有多少?

根据 npm 官方下载统计,@fluid-app/widget-runtime 在 2026/10/15 – 2026/11/13 这 30 天内被下载 29,063 次,日均约 969 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fluid-app/widget-runtime 还在维护吗?

最新版本 0.1.271 发布于 2026/11/15(今天);至今共发布 272 个版本,其中过去 12 个月发布了 272 个;首次发布于 2026/07/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。