@effing/serde

中文文档
0.48.0发布于 2026/11/12MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2223

URL-safe serialization with compression and HMAC signing

原始使用文档
文档目录10 节
  1. Installation
  2. Quick Start
  3. Concepts
  4. URL-Safe Base64
  5. Signed Serialization
  6. Compression
  7. API Overview
  8. Examples
  9. Passing Props in URLs
  10. Secure Tokens

@effing/serde

URL-safe serialization with compression and HMAC signing.

Part of the Effing family — programmatic video creation with TypeScript.

Serialize JSON data into URL-safe strings with automatic compression and cryptographic signing. Compatible with Python's itsdangerous.

Installation

npm install @effing/serde

Quick Start

import { serialize, deserialize } from "@effing/serde";

const secret = process.env.SECRET_KEY!;
const data = { userId: 123, action: "view" };

// Signed (and sometimes compressed) URL segment
const segment = await serialize(data, secret);

// Verify signature (and decompress if needed)
const restored = await deserialize<typeof data>(segment, secret);

Concepts

URL-Safe Base64

Standard Base64 uses + and / which have special meaning in URLs. This package uses URL-safe Base64:

  • + → -
  • / → _
  • No padding (=)

Signed Serialization

Serialization is signed with HMAC (default: sha1) so the result can safely be used in a URL without being tampered with:

// Server: create signed URL segment
const segment = await serialize(data, secret);

// Client: passes segment in URL
// Server: verify and deserialize
const data = await deserialize(segment, secret);
// Throws if signature is invalid.

Compression

Payloads are gzip-compressed when it saves space. Compressed payloads are prefixed with a leading "." (matching itsdangerous).

API Overview

serialize(obj, secretKey, options?)

Serialize a value to a URL-safe string.

function serialize(
  obj: object,
  secretKey: string,
  options?: {
    /** Salt for key derivation (default: "itsdangerous") */
    salt?: string;
    /** Hash algorithm for HMAC (default: "sha1") */
    algorithm?: string;
  },
): Promise<string>;

deserialize(segment, secretKey, options?)

Deserialize a URL segment back to a value.

function deserialize<T = Record<string, unknown>>(
  segment: string,
  secretKey: string,
  options?: {
    /** Salt for key derivation (default: "itsdangerous") */
    salt?: string;
    /** Hash algorithm for HMAC (default: "sha1") */
    algorithm?: string;
    /** Convert snake_case keys to camelCase (default: true) */
    convertKeysToCamel?: boolean;
  },
): Promise<T>;

Throws:

  • Error — If signature verification fails

Examples

Passing Props in URLs

import { serialize, deserialize } from "@effing/serde";

// Create URL with serialized props
const secret = process.env.SECRET_KEY!;
const props = { imageUrl: "https://example.com/image.png", duration: 5 };
const segment = await serialize(props, secret);
const url = `/render/${segment}`;

// In route handler
async function loader({ params }) {
  const props = await deserialize(params.segment, secret);
  // props = { imageUrl: "https://example.com/image.png", duration: 5 }
}

[!NOTE] The convertKeysToCamel deserialization option (which is true by default) is useful when URLs are built in Python (with itsdangerous) and then consumed by Effing. Python typically uses snake_case keys, while TypeScript prefers camelCase. The conversion is recursive — keys inside nested objects and arrays of objects are converted too.

Secure Tokens

const SECRET = process.env.TOKEN_SECRET!;

// Create signed token
async function createToken(userId: number, expiresAt: number) {
  return serialize({ userId, expiresAt }, SECRET);
}

// Verify token
async function verifyToken(token: string) {
  try {
    const { userId, expiresAt } = await deserialize(token, SECRET);
    if (Date.now() > expiresAt) throw new Error("Token expired");
    return userId;
  } catch (e) {
    throw new Error("Invalid token");
  }
}

版本与发布记录

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

版本发布日期距今
0.48.0latest2026/11/121 天前
0.47.32026/11/112 天前
0.47.22026/11/104 天前
0.47.12026/10/2915 天前
0.47.02026/10/2717 天前
0.46.22026/10/2222 天前

关于 @effing/serde 的常见问题

@effing/serde 是什么?

URL-safe serialization with compression and HMAC signing

如何安装 @effing/serde?

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

@effing/serde 支持 TypeScript 吗?

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

@effing/serde 支持 ESM 还是 CommonJS?

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

@effing/serde 可以商用吗?

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

@effing/serde 的下载量有多少?

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

@effing/serde 还在维护吗?

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