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

print-shim

v1.0.1

Published

TypeScript SDK for adapting local WebSocket print components across ecommerce platforms.

Downloads

27

Readme

print-shim 🖨️

print-shim 是一个旨在抹平国内电商平台(拼多多、抖店、小红书等)官方打印组件协议差异的轻量级 TypeScript SDK。它通过统一的 API 封装,让你只需编写一套代码,即可调用不同平台的本地打印服务。

✨ 特性

  • 协议对齐:统一处理不同平台的 WebSocket 指令、RequestID 匹配及返回格式。
  • Promise 化 API:告别繁琐的异步回调,支持 async/await。
  • 连接管理:内置心跳检测、自动重连机制,保障打印稳定性。
  • 轻量可靠:基于 Vite 构建,天然支持 Tree-shaking,体积极小。
  • 开箱即用:预设各平台默认端口与协议映射。

📦 安装

npm install print-shim
# 或者
pnpm add print-shim

🚀 快速上手

import { Platform, PrintShim } from "print-shim"

// 1. 初始化
const printShim = new PrintShim(Platform.XHS, {
  useWss: false,
})

// 2. 建立连接
await printShim.connect()

// 3. 获取打印机列表
const { printers } = await printShim.getPrinters()
console.log("可用打印机:", printers)

// 4. 发送打印任务
try {
  const result = await printShim.print({
    taskID: "demo-task-001",
    printer: "HPRT N41",
    documents: [
      {
        templateURL: "https://example.com/template",
        data: {},
      },
    ],
  })
  console.log("打印成功:", result)
} catch (error) {
  console.error("打印失败:", error)
}

🏗️ 架构设计

print-shim 采用适配器模式,将平台协议差异下沉到 adapter,Core 只保留通用的 WebSocket 请求生命周期管理:

  • Core:负责 WebSocket 连接、发送 payload、pending request 管理、timeout,以及收到消息后的分发流程。
  • Adapter:负责平台协议差异,包括请求构造、响应匹配、最终响应判断、失败判断、结果归一化,以及平台响应类型维护。
  • Shim Layer:提供面向业务开发者的标准化方法。

🛠️ API 详情

new PrintShim(platform, options?)

| 参数 | 类型 | 说明 | | ----------------- | --------------------------------------------- | ------------------------------------- | | platform | Platform.PDD \| Platform.DY \| Platform.XHS | 目标打印平台 | | options.useWss | 可选 boolean | 是否走平台预设 wss 地址 | | options.url | 可选 string | 自定义组件地址(优先级高于 useWss) | | options.timeoutMs | 可选 number | 请求默认超时时间(毫秒) | | options.logger | 可选 Logger | 自定义日志实现 | | options.websocket | 可选 Partial<WebSocketClientOptions> | 自定义 WS 重连/心跳等连接参数 |

实例方法:

  • connect(): Promise<void>
  • close(code?: number, reason?: string): void
  • send<TResponse>(action: string, payload?: AnyJson, options?: SendOptions): Promise<TResponse>
  • getPrinters(): Promise<GetPrintersResponse>
  • print(payload: PrintPayload): Promise<PlatformPrintResponseMap[P]>
  • preview(payload: PrintPayload): Promise<PlatformPreviewResponseMap[P]>
  • onMessage(listener): () => void