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

@openlee/virtual-parcel

v0.1.0

Published

Virtual Parcel SDK - Core logic for redemption code management

Readme

@openlee/virtual-parcel

数字包裹系统核心 SDK,提供取件码生成、验证、管理等功能。

安装

npm install @openlee/virtual-parcel

快速开始

基本用法

import { VirtualParcelSDK } from '@openlee/virtual-parcel';

// 初始化 SDK(使用远程后端)
const sdk = new VirtualParcelSDK({
  backendUrl: 'https://api.virtual-parcel.com'
});

// 验证取件码
const result = await sdk.verifyCode('ABCD-X7K9');

if (result.valid) {
  console.log('验证成功!');
  console.log('包裹内容:', result.parcel?.payload);
} else {
  console.error('验证失败:', result.message);
}

核销模式

1. 即焚模式 (perish)

取件码只能使用一次,使用后立即失效。

const code = await sdk.createParcel({
  mode: 'perish',
  payload: {
    links: ['https://example.com/vip']
  }
});

// 第一次验证:成功
await sdk.verifyCode(code);

// 第二次验证:失败(已使用)
await sdk.verifyCode(code);

2. 复用模式 (reuse)

取件码可以多次使用,可设置最大使用次数。

const code = await sdk.createParcel({
  mode: 'reuse',
  maxReuse: 10, // 最多使用 10 次
  payload: {
    text: '会员权益'
  }
});

const result = await sdk.verifyCode(code);
console.log(result.remainingUses); // 9

3. 时效模式 (timed)

取件码在指定时间内有效,可多次使用。

const code = await sdk.createParcel({
  mode: 'timed',
  expiresAt: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 天后过期
  payload: {
    videos: ['https://example.com/video.mp4']
  }
});

事件监听

sdk.on('verified', ({ code, parcel }) => {
  console.log(`取件码 ${code} 验证成功`);
});

sdk.on('expired', ({ code }) => {
  console.log(`取件码 ${code} 已过期`);
});

sdk.on('revoked', ({ code }) => {
  console.log(`取件码 ${code} 已被撤销`);
});

存储适配器

使用远程后端(推荐)

const sdk = new VirtualParcelSDK({
  backendUrl: 'https://api.virtual-parcel.com'
});

使用 localStorage(浏览器)

import { LocalStorageAdapter } from '@openlee/virtual-parcel';

const sdk = new VirtualParcelSDK({
  adapter: new LocalStorageAdapter()
});

使用内存存储(测试/演示)

import { MemoryAdapter } from '@openlee/virtual-parcel';

const sdk = new VirtualParcelSDK({
  adapter: new MemoryAdapter()
});

API 参考

VirtualParcelSDK

构造函数

new VirtualParcelSDK(config: SDKConfig)

方法

  • verifyCode(code: string): Promise<VerifyResult> - 验证取件码
  • getParcel(code: string): Promise<Parcel | null> - 获取包裹信息
  • createParcel(input: CreateParcelInput): Promise<string> - 创建包裹
  • revokeCode(code: string): Promise<boolean> - 撤销取件码
  • on(event: string, callback: Function): void - 监听事件
  • off(event: string, callback: Function): void - 取消监听

类型定义

ParcelMode

type ParcelMode = 'perish' | 'reuse' | 'timed';

Parcel

interface Parcel {
  id: string;
  code: string;
  mode: ParcelMode;
  createdAt: number;
  expiresAt?: number;
  maxReuse?: number;
  usedCount: number;
  payload: ParcelPayload;
  metadata?: Record<string, unknown>;
  consumedAt?: number;
  revoked: boolean;
}

VerifyResult

interface VerifyResult {
  valid: boolean;
  code: string;
  mode: ParcelMode;
  parcel?: Parcel;
  remainingUses?: number;
  expiresAt?: number;
  message?: string;
}

取件码格式

  • 格式:XXXX-XXXX(8 个字符,分为两组)
  • 字符集:23456789ABCDEFGHJKLMNPQRSTUVWXYZ(去除易混淆字符 0, 1, I, L, O)
  • 容量:约 11 亿种组合

工具函数

import { generateCode, isValidCodeFormat, formatCode } from '@openlee/virtual-parcel';

// 生成取件码
const code = generateCode(); // "ABCD-X7K9"

// 验证格式
isValidCodeFormat('ABCD-X7K9'); // true
isValidCodeFormat('invalid'); // false

// 格式化
formatCode('ABCDX7K9'); // "ABCD-X7K9"

License

MIT