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

json-packer-napi

v0.1.0

Published

Node.js N-API bindings for json-packer-core - reversible JSON binary compression

Readme

json-packer-napi

Node.js N-API 绑定,基于 json_packer_core Rust 库。

安装

npm install json-packer-napi
# 或
pnpm add json-packer-napi

API

interface CompressOptions {
  enable_value_pool?: boolean;     // 是否启用字符串值池(默认 false)
  pool_min_repeats?: number;       // 计入值池的最小重复次数(默认 3)
  pool_min_string_len?: number;    // 计入值池的最小字符串长度(默认 8)
}

// 压缩为字节数组
export function compressToBytes(jsonString: string, options?: CompressOptions): Buffer;

// 压缩为 Base64 字符串
export function compressToBase64(jsonString: string, options?: CompressOptions): string;

// 从字节数组解压
export function decompressFromBytes(bytes: Buffer): string;

// 从 Base64 字符串解压
export function decompressFromBase64(base64: string): string;

快速开始

基础用法

const { compressToBase64, decompressFromBase64 } = require('json-packer-napi');

const data = { name: "Alice", age: 30, active: true };
const jsonStr = JSON.stringify(data);

// 压缩
const compressed = compressToBase64(jsonStr);
console.log('Compressed:', compressed);

// 解压
const decompressed = decompressFromBase64(compressed);
const restored = JSON.parse(decompressed);
console.log('Restored:', restored);

ESM 导入

import { compressToBase64, decompressFromBase64 } from 'json-packer-napi';

const data = { items: ["apple", "banana", "cherry"] };
const compressed = compressToBase64(JSON.stringify(data));
const restored = JSON.parse(decompressFromBase64(compressed));

启用字符串值池

const { compressToBytes, decompressFromBytes } = require('json-packer-napi');

const data = {
  users: [
    { status: "connected", message: "connected to server" },
    { status: "connected", message: "connected to server" },
    { status: "connected", message: "connected to server" }
  ]
};

const options = {
  enable_value_pool: true,
  pool_min_repeats: 2,      // 重复 2 次以上进入值池
  pool_min_string_len: 5    // 字符串长度 >= 5 才考虑值池
};

const compressed = compressToBytes(JSON.stringify(data), options);
const restored = JSON.parse(decompressFromBytes(compressed));

console.log('Original size:', Buffer.byteLength(JSON.stringify(data), 'utf8'));
console.log('Compressed size:', compressed.length);

平台支持

预编译二进制文件支持:

  • Windows (x64, ARM64)
  • macOS (x64, ARM64)
  • Linux (x64, ARM64, musl)

构建

从源码构建(需要 Rust 工具链):

git clone <repository>
cd json-packer/napi
pnpm install
pnpm run build

注意事项

  • 输入必须是有效的 JSON 字符串
  • 不支持 NaN、Infinity 等非标准 JSON 值
  • 压缩结果是确定性的(相同输入+配置 → 相同输出)
  • 值池功能对小数据可能增加开销,建议在重复字符串较多时使用

许可证

MIT