npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

crx-rpc

v2.3.0

Published

A lightweight RPC framework for Chrome Extension (background <-> content <-> web)

Readme

crx-rpc

npm version npm downloads license

一个用于 Chrome 扩展的类型安全 RPC 实现,支持 Content Scripts、Background、Popup/Sidepanel 和 Web Pages 之间的通信。

安装

npm install crx-rpc

或使用其他包管理器:

# pnpm
pnpm add crx-rpc

# yarn
yarn add crx-rpc

特性

  • 类型安全: 基于 TypeScript 构建。
  • 灵活: 支持 Chrome 扩展内的多种通信路径。
  • Observable: 支持类似 RxJS 的 observable 以进行实时更新。
  • 统一 API: 简化的 host 和 client API,自动环境检测。
  • 智能转发: Content script 自动转发 web 消息到 background。

快速开始(统一 API)

1. 定义服务

import { createIdentifier } from 'crx-rpc'

export interface IMathService {
  add(a: number, b: number): Promise<number>
}

export const IMathService = createIdentifier<IMathService>('math-service', 'background')

2. 托管服务(Background 或 Content)

import { createHost } from 'crx-rpc'
import { IMathService } from './api'

class MathService implements IMathService {
  async add(a: number, b: number) {
    return a + b
  }
}

// 自动检测环境(background/content)
// 在 content script 中,自动转发 web 消息到 background
const host = createHost()
host.register(IMathService, new MathService())

3. 调用服务(任意位置)

import { createClient } from 'crx-rpc'
import { IMathService } from './api'

// 自动检测环境(runtime/web)
const client = createClient()

// 调用 background service
const mathService = await client.createRPCService(IMathService)
const result = await mathService.add(1, 2) // 3

// 调用 content service(需提供 tabId)
const contentService = await client.createRPCService(IContentService, { tabId: 123 })
await contentService.doSomething()

主要改进

  • 无需手动环境检测: createHost()createClient() 自动检测环境
  • 无需手动设置代理: Content script 自动转发 web 消息
  • 智能路由: 发往 content service 的 web 消息在本地处理,只有发往 background 的消息才转发
  • 统一上下文注入: Background 和 content service 都会接收 RpcContext 作为最后一个参数
  • 单一 client API: 无需在 RuntimeRPCClientWebRPCClientTabRPCClient 之间选择

特性

  • 类型安全: 基于 TypeScript 构建。
  • 灵活: 支持 Chrome 扩展内的多种通信路径。
  • Observable: 支持类似 RxJS 的 observable 以进行实时更新。

通信架构

本库促进了 Chrome 扩展不同部分之间的通信。

服务提供者 (Service Providers)

服务可以托管在两个位置:

  1. Background: 托管在 background service worker。处理来自 Content Scripts、Popup/Sidepanel 和 Web Pages 的请求。
  2. Content Script: 托管在 content script。处理来自 Background 和 Popup/Sidepanel 的请求。

支持的通信流程

使用统一 API,所有通信流程都自动处理:

| 调用方 | 目标 | 用法 | | :------------------ | :----------------- | :---------------------------------------------- | | Content Script | Background | client.createRPCService(IBackgroundService) | | Web Page | Background | client.createRPCService(IBackgroundService) | | Popup/Sidepanel | Background | client.createRPCService(IBackgroundService) | | Background | Content Script | client.createRPCService(IContentService, { tabId }) | | Popup/Sidepanel | Content Script | client.createRPCService(IContentService, { tabId }) | | Web Page | Content Script | client.createRPCService(IContentService) (本地) |

注意: Web 到 background 的通信会自动通过 content script 中继。发往 content service 的消息如果在同一个 content script 中注册了服务则本地处理。

RpcContext

BackgroundRPCHostContentRPCHost(使用统一 API 时)都会自动将 RpcContext 对象作为最后一个参数注入到服务方法中:

import { RpcContext } from 'crx-rpc'

class MathService implements IMathService {
  async add(a: number, b: number, context: RpcContext) {
    console.log('调用来自 tab:', context.tabId)
    console.log('发送者:', context.sender)
    console.log('是否来自 runtime 上下文:', context.isFromRuntime)
    return a + b
  }
}

RpcContext 包含:

  • tabId: 调用者的 tab ID(popup/sidepanel 为 undefined)
  • sender: 完整的 Chrome MessageSender 对象
  • isFromRuntime: 布尔值,表示调用是否来自 runtime 上下文(popup/sidepanel)而非 content script

API 参考

  • createHost(log?: boolean): 创建自动检测环境的统一 RPC host
  • UnifiedRPCHost: 统一 host 类,自动环境检测和智能 web 转发
  • createClient(): 创建自动检测环境的统一 RPC client
  • UnifiedRPCClient: 统一 client 类,自动环境检测和动态 tabId 支持