@aspectly/core

中文文档
2.2.0发布于 2026/09/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 5798

Core bridge framework for cross-platform communication between WebViews, iframes, and web applications

原始使用文档
文档目录12 节
  1. Installation
  2. Overview
  3. Quick Start
  4. Inside a WebView or iframe
  5. Browser Script Tag Usage
  6. API Reference
  7. AspectlyBridge
  8. Error Handling
  9. Architecture
  10. Native hosts
  11. Related Packages
  12. License

@aspectly/core

npm version npm downloads CI

The core bridge framework for cross-platform communication between WebViews, iframes, and web applications.

Installation

# npm
npm install @aspectly/core

# pnpm
pnpm add @aspectly/core

# yarn
yarn add @aspectly/core

Overview

@aspectly/core provides the foundational bridge communication layer that enables bidirectional message passing between different execution contexts:

  • WebView ↔ React Native: Communication between web content and React Native apps
  • iframe ↔ Parent Window: Communication between iframes and their parent pages
  • Browser Context: Standalone browser usage with global bridge instance

@aspectly/core is the web side of the bridge. The exact same code runs unchanged across every native host — Web (iframe/popup), React Native, .NET (CefSharp/WebView2), iOS/macOS, Android, Flutter, and Linux/WebKitGTK. The correct host transport is auto-detected at runtime via @aspectly/transports (priorities: cefsharp 100 > webkit 95 > react-native 90 > android 85 > flutter 84 > iframe 80 > window 70 > postmessage 10), so your embedded web app never needs host-specific branches.

Quick Start

Inside a WebView or iframe

import { AspectlyBridge } from '@aspectly/core';

// Create a bridge instance
const bridge = new AspectlyBridge();

// Initialize with handlers for incoming requests
await bridge.init({
  greet: async (params: { name: string }) => {
    return { message: `Hello, ${params.name}!` };
  },
  getData: async () => {
    return { timestamp: Date.now() };
  },
});

// Send requests to the parent container
const result = await bridge.send('parentMethod', { data: 'value' });
console.log(result);

Browser Script Tag Usage

<script src="https://unpkg.com/@aspectly/core/dist/browser.js"></script>
<script>
  window.aspectlyBridge.init({
    greet: async (params) => ({ message: `Hello, ${params.name}!` })
  }).then(() => {
    console.log('Bridge initialized');
  });
</script>

API Reference

AspectlyBridge

The main class for bridge communication.

Constructor

const bridge = new AspectlyBridge(options?: BridgeOptions);

Options:

  • timeout?: number - Handler execution timeout in ms (default: 100000)

Methods

init(handlers?: BridgeHandlers): Promise<boolean>

Initialize the bridge with handlers for incoming requests. The returned promise resolves only when both Init and InitResult messages have been exchanged (via tryResolveInit), ensuring the full handshake is complete.

await bridge.init({
  methodName: async (params) => {
    // Handle request and return result
    return { success: true };
  },
});
send<T>(method: string, params?: object): Promise<T>

Send a request to the other side.

const result = await bridge.send<{ data: string }>('getData', { id: 123 });
supports(method: string): boolean

Check if a method is supported by the other side.

if (bridge.supports('getData')) {
  const data = await bridge.send('getData');
}
isAvailable(): boolean

Check if the bridge is initialized and ready.

if (bridge.isAvailable()) {
  // Bridge is ready
}
subscribe(listener: BridgeListener): number

Subscribe to all result events for monitoring/debugging.

bridge.subscribe((result) => {
  console.log('Event:', result.method, result.data);
});
registerHandler(method: string, handler: (params: object) => Promise<unknown>): void

Register a handler for incoming requests after initialization.

bridge.registerHandler('newMethod', async (params) => {
  return { result: 'value' };
});
unregisterHandler(method: string): void

Remove a previously registered handler.

bridge.unregisterHandler('newMethod');
unsubscribe(listener: BridgeListener): void

Remove a result event listener.

destroy(): void

Cleanup bridge subscriptions.

bridge.destroy();

Error Handling

The bridge provides typed errors for different failure scenarios:

import { BridgeErrorType } from '@aspectly/core';

try {
  await bridge.send('unknownMethod');
} catch (error) {
  switch (error.error_type) {
    case BridgeErrorType.UNSUPPORTED_METHOD:
      console.log('Method not registered');
      break;
    case BridgeErrorType.METHOD_EXECUTION_TIMEOUT:
      console.log('Handler timed out');
      break;
    case BridgeErrorType.REJECTED:
      console.log('Handler threw error:', error.error_message);
      break;
    case BridgeErrorType.BRIDGE_NOT_AVAILABLE:
      console.log('Bridge not initialized');
      break;
  }
}

Architecture

The package consists of four layers:

  1. BridgeCore - Low-level platform detection and message serialization
  2. BridgeInternal - Protocol management and request/response lifecycle
  3. BridgeBase - Clean public API interface
  4. AspectlyBridge - Main entry point composing all layers

Native hosts

The same @aspectly/core web code talks to native hosts on every supported platform family, with the transport selected automatically by @aspectly/transports:

  • Web (iframe/popup), React Native, and React Native Web/Expo — npm packages
  • .NET (CefSharp / WebView2, Windows) — NuGet
  • iOS/macOS/visionOS — SwiftPM + CocoaPods (AspectlyBridge, AspectlyBridgeWebKit)
  • Android — Maven Central (io.github.jeanisahakyan:aspectly-bridge-webview)
  • Flutter (Dart) — pub.dev (aspectly_bridge)
  • Linux/WebKitGTK (Python) — PyPI (aspectly-bridge, reuses the WebKit transport)

See the main README for the full host matrix, install commands, and native host APIs.

License

MIT

版本与发布记录

@aspectly/core 共发布 12 个版本,首次发布于 2025/12/05;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.2.0latest2026/09/062 个月前
2.1.02026/06/294 个月前
2.0.152026/06/224 个月前
2.0.142026/03/177 个月前
2.0.132026/03/177 个月前
2.0.122026/03/177 个月前

关于 @aspectly/core 的常见问题

@aspectly/core 是什么?

Core bridge framework for cross-platform communication between WebViews, iframes, and web applications

如何安装 @aspectly/core?

在项目目录运行 npm install @aspectly/core,或使用 pnpm add / yarn add / bun add。

@aspectly/core 支持 TypeScript 吗?

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

@aspectly/core 支持 ESM 还是 CommonJS?

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

@aspectly/core 可以商用吗?

@aspectly/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aspectly/core 的下载量有多少?

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

@aspectly/core 还在维护吗?

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