@block65/webcrypto-web-push

中文文档
2.0.0发布于 2026/09/03MITTypeScriptES Module构建来源已验证近 30 天下载 88.5万

Send notifications using the Web Push Protocol (RFC 8291) and Web Crypto APIs (works with NodeJS, Cloudflare Workers, Bun and Deno)

原始使用文档
文档目录6 节
  1. Installation
  2. Configuration
  3. Typescript
  4. Usage
  5. Upgrading from 1.x
  6. License

@block65/webcrypto-web-push

Send notifications using Web Push Protocol and Web Crypto APIs (works with NodeJS, Cloudflare Workers, Bun and Deno)

Messages are encrypted with aes128gcm as specified in RFC 8291, and authenticated with the VAPID vapid scheme from RFC 8292. Both are accepted by every current push service, including Apple.

Every message is padded to a constant 4096 octets, so the ciphertext length does not disclose the plaintext length, and the body stays within the size a push service is obliged to accept. The maximum payload is therefore 3993 bytes. Subscription endpoints must be https.

Installation

Using pnpm:

pnpm add @block65/webcrypto-web-push

Using npm:

npm install @block65/webcrypto-web-push

Configuration

Typescript

Note This is an ESM package. If you are using Typescript, you will need to set moduleResolution to node16, nodenext or bundler in order to resolve the package exports.

Usage

import {
  buildPushPayload,
  type PushSubscription,
  type PushMessage,
  type VapidKeys,
} from '@block65/webcrypto-web-push';

// `env` could be `process.env` or `import.meta.env` depending
// on your platform or build tooling
const vapid: VapidKeys = {
  subject: env.VAPID_SUBJECT,
  publicKey: env.VAPID_SERVER_PUBLIC_KEY,
  privateKey: env.VAPID_SERVER_PRIVATE_KEY,
};

// Obtained from the client using PushManager subscribe() method:
// https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe
const subscription: PushSubscription = {
  endpoint: 'https://fcm.googleapis.com/fcm/send/...',
  expirationTime: null,
  keys: {
    p256dh: '...',
    auth: '...',
  },
};

const message: PushMessage = {
  data: "You've got mail!",
  options: {
    ttl: 60,
  },
};

const payload = await buildPushPayload(message, subscription, vapid);

// send the payload to the subscription endpoint using your favourite HTTP client
const res = await fetch(subscription.endpoint, payload);

console.log(res.status); // 201

Upgrading from 1.x

The API is unchanged. Version 1.x sent the legacy aesgcm content encoding and the draft WebPush authorization scheme, neither of which Apple accepts. Consumers that need the aesgcm encoding should stay on 1.x.

License

This package is licensed under the MIT license. See the LICENSE file for more information.

版本与发布记录

@block65/webcrypto-web-push 共发布 9 个版本,首次发布于 2023/04/22;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/09/032 个月前
1.0.22024/12/151.9 年前
1.0.12024/08/012 年前
1.0.02024/06/112 年前
0.3.22023/09/183 年前
0.3.12023/09/043 年前

关于 @block65/webcrypto-web-push 的常见问题

@block65/webcrypto-web-push 是什么?

Send notifications using the Web Push Protocol (RFC 8291) and Web Crypto APIs (works with NodeJS, Cloudflare Workers, Bun and Deno)

如何安装 @block65/webcrypto-web-push?

在项目目录运行 npm install @block65/webcrypto-web-push,或使用 pnpm add / yarn add / bun add。

@block65/webcrypto-web-push 支持 TypeScript 吗?

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

@block65/webcrypto-web-push 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@block65/webcrypto-web-push 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@block65/webcrypto-web-push 可以商用吗?

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

@block65/webcrypto-web-push 的下载量有多少?

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

@block65/webcrypto-web-push 还在维护吗?

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