@e4a/pg-js

中文文档
2.6.0发布于 2026/09/22MITTypeScriptES Module构建来源已验证近 30 天下载 2537

Browser SDK for PostGuard — end-to-end encrypted file sharing with identity-based encryption

原始使用文档
文档目录5 节
  1. Quick Start
  2. Server-side usage (Node, Bun, Deno)
  3. Development
  4. Releasing
  5. License

PostGuard

For full documentation, visit docs.postguard.eu.

TypeScript/JavaScript SDK for PostGuard, published as @e4a/pg-js on npm. Works in both browsers and Node.js.

PostGuard encrypts data for recipients based on their identity attributes (email address, phone number, etc.) rather than traditional public keys. Recipients prove their identity via Yivi to decrypt. This SDK is the main way web applications and email add-ons integrate with PostGuard.

Quick Start

npm install @e4a/pg-js
import { PostGuard } from '@e4a/pg-js';

const pg = new PostGuard({
  pkgUrl: 'https://pkg.postguard.eu',
});

// Encrypt
const sealed = pg.encrypt({
  sign: pg.sign.apiKey('your-api-key'),
  recipients: [pg.recipient.email('[email protected]')],
  files: fileList,
});
const { uuid } = await sealed.upload();

// Decrypt
const opened = pg.open({ uuid });
const result = await opened.decrypt({ element: '#yivi-popup' });
result.download();

See the full API reference for encryption options, signing methods, recipient types, and email helpers.

Server-side usage (Node, Bun, Deno)

The SDK works in non-browser runtimes for the encrypt + upload path when signing via sign.apiKey or a custom sign.session callback. No polyfills required.

Minimum runtime versions:

RuntimeMinimumNotes
Node22+Enforced via engines.node. The build tool requires Node 22.18+; runtime is tested on 22 and 24.
Bun1.0.16+First release with AbortSignal.any (the SDK's tightest web-API requirement)
Deno1.39+First release with AbortSignal.any

Other notes:

  • sign.yivi(...) requires a DOM and is browser-only. The SDK throws a clear YiviSessionError upfront on non-browser runtimes — use sign.apiKey or sign.session instead.
  • For decryption, result.blob and result.plaintext are universal; result.download(...) triggers a browser download and is browser-only.
  • sealed.upload() refuses data: ReadableStream — use sealed.toBytes() for streaming, or pass data as Uint8Array.

A manual smoke test for any runtime lives at scripts/smoke.mjs. Set PG_API_KEY to a staging key to exercise the full upload pipeline:

PG_API_KEY=PG-... node scripts/smoke.mjs
PG_API_KEY=PG-... bun  scripts/smoke.mjs
PG_API_KEY=PG-... deno run -A scripts/smoke.mjs

Development

This package lives in a pnpm workspace at packages/pg-js. From the repo root:

pnpm install   # runs the prebuild generators + a full build via `prepare`
pnpm build

Run the tests:

pnpm test

Watch mode (pnpm dev, pnpm test:watch) runs from this directory.

Releasing

Releases are managed by changesets. A PR that should ship runs pnpm changeset and commits the generated file; merging to main opens (or updates) a "Version Packages" PR, and merging that publishes to npm with provenance.

License

MIT

版本与发布记录

@e4a/pg-js 共发布 59 个版本,首次发布于 2026/03/25;过去 12 个月发布了 59 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.6.0latest2026/09/221 个月前
2.5.02026/09/012 个月前
2.4.02026/08/113 个月前
2.3.42026/07/283 个月前
2.3.32026/07/243 个月前
2.3.22026/07/223 个月前

关于 @e4a/pg-js 的常见问题

@e4a/pg-js 是什么?

Browser SDK for PostGuard — end-to-end encrypted file sharing with identity-based encryption

如何安装 @e4a/pg-js?

在项目目录运行 npm install @e4a/pg-js,或使用 pnpm add / yarn add / bun add。

@e4a/pg-js 支持 TypeScript 吗?

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

@e4a/pg-js 支持 ESM 还是 CommonJS?

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

@e4a/pg-js 需要什么版本的 Node.js?

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

@e4a/pg-js 可以商用吗?

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

@e4a/pg-js 的下载量有多少?

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

@e4a/pg-js 还在维护吗?

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