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

mp-text-decoder

v1.0.2

Published

WeChat Mini Program TextDecoder.decode polyfill for SSE and UTF-8 decoding, supports Chinese, emoji, and other multi-byte characters.

Readme

mp-text-decoder

mp-text-decoder 是一个面向微信小程序场景的 TextDecoder.decode 兼容实现,主要用于小程序 SSE 响应和其他 UTF-8 二进制文本内容的解码。

A lightweight TextDecoder.decode polyfill for WeChat Mini Program SSE and UTF-8 decoding.

特性

  • 微信小程序 TextDecoder.decode 兼容实现
  • 适合小程序 SSE、text/event-stream、流式文本响应解码
  • 支持 UTF-8 编码
  • 支持多字节字符处理,包括中文、emoji 等内容
  • 支持 Uint8ArrayArrayBuffer 输入
  • 非法字节序列会回退为 Unicode 替换字符 U+FFFD
  • 体积轻量、无运行时依赖

安装

npm install mp-text-decoder

使用示例

import { MpTextDecoder } from 'mp-text-decoder';

const decoder = new MpTextDecoder();

const text = decoder.decode(
  new Uint8Array([0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, 0xf0, 0x9f, 0x98, 0x80]),
);

console.log(text); // 你好😀

小程序 SSE 解码示例

下面这个例子适合把小程序收到的二进制 chunk 解成字符串,再交给 SSE 解析逻辑处理:

import { MpTextDecoder } from 'mp-text-decoder';

const decoder = new MpTextDecoder();
let buffer = '';

function handleChunk(chunk: ArrayBuffer) {
  buffer += decoder.decode(chunk);

  const frames = buffer.split('\n\n');
  buffer = frames.pop() ?? '';

  for (const frame of frames) {
    for (const line of frame.split('\n')) {
      if (line.startsWith('data:')) {
        console.log(line.slice(5).trim());
      }
    }
  }
}

API

new MpTextDecoder()

创建一个 UTF-8 解码器实例。

decode(input: ArrayBuffer | Uint8Array): string

把 UTF-8 二进制数据解码为字符串。

  • 输入类型支持 ArrayBufferUint8Array
  • 主要面向 UTF-8 文本解码
  • 可用于小程序 SSE chunk 的文本转换
  • 遇到非法字节序列时会输出 U+FFFD
  • 遇到被截断的不完整序列时,会在边界处停止解码

注意事项

当前 decode 是单次解码接口。

如果你的网络层会把一个 UTF-8 多字节字符拆到两个 chunk 中,建议先在业务层缓存残缺字节,再调用 decode,这样更适合严格的 SSE 流式解码场景。

适用场景

  • 微信小程序中没有原生 TextDecoder 的环境
  • 需要在小程序里解码 SSE、text/event-stream 或其他流式文本响应
  • 需要在小程序里解码 UTF-8 文本、接口二进制响应或自定义协议数据
  • 需要正确处理中文、emoji 等多字节字符