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

@zerobitlab/playground-sdk

v0.2.1

Published

零板 Playground 嵌入 SDK:连接已嵌入的实验页面,读取 MCU 当前绑定的固件并把已保存的实验工作区打包成 zip

Readme

@zerobitlab/playground-sdk

零板 Playground 的嵌入 SDK。把实验挂到一个容器里,用它在不打断仿真的前提下读取原理图上指定 MCU 当前绑定的固件、把整份实验工作区打包成 zip,并可选择把分享副本的改动缓存在浏览器里。

安装

pnpm add @zerobitlab/playground-sdk

使用

<div id="playground" style="height: 720px"></div>
import { createPlayground } from '@zerobitlab/playground-sdk'

const playground = await createPlayground(document.querySelector('#playground')!, {
  code: 'abc123',                // 实验 code,即 /p/{code} 里的那一段
  storageKey: 'exam-2025-042',   // 可选:本地缓存键
})

// 交卷时读取位号 U1 当前绑定的固件。
const firmware = await playground.getMcuFirmware({ designator: 'U1' })

// 交卷时把整份实验工作区打包成 zip。
const archive = await playground.exportProject()
archive.filename // 建议的下载名,形如 `考试实验.zip`
archive.bytes    // zip 字节

// 交卷后清空本地缓存。
await playground.clearStorage()

// 卸载实验。
playground.dispose()

SDK 按 <baseUrl>/p/<code>/embed 拼出地址;baseUrl 缺省 https://0b.ink,自建站点用 baseUrl 覆盖:

const playground = await createPlayground(div, {
  code: 'abc123',
  baseUrl: 'https://lab.example.com',
})

SDK 创建 iframe 并填满容器(100% 宽高、无边框),因此容器需要自己的尺寸。iframe 通过 playground.iframe 暴露,便于调整样式或监听事件。

designator 是原理图上可见的位号(如 U1),不是内部 UUID。读取不触发编译、保存、暂停或重启,仿真可以继续运行。

固件返回值

type McuFirmware = {
  format: 'bin' | 'hex' | 'elf'
  bytes: ArrayBuffer
  addressSpace?: string
  loadAddress?: number
}
  • 返回对象:位号绑定的代码工程最近一次成功产物,或绑定的固件资产原始字节。
  • 返回 null:位号存在,但没有可读取的固件(器件没有程序绑定、工程还没有成功产物、或引用的资产已不存在)。
  • 抛出 PlaygroundEmbedError:位号不存在、绑定损坏或读取失败;code 是稳定的错误码。

返回的是调用时绑定指向的固件,不是仿真启动时装载的镜像。

打包工作区

const { filename, bytes } = await playground.exportProject()

exportProject() 把当前实验工作区打成 zip 并返回建议文件名与字节;条目是工作区里的相对路径,例如 schematic.sch、projects/firmware/src/main.a51、assets/…、build/firmware/firmware.bin。空目录也会写进归档,因此归档可以直接作为项目重新导入。

归档只含已保存的内容:编辑器里还没保存的修改不在归档里,打包既不会替用户保存,也不改变工作区。打包只读取当前工作区,不编译、不保存到服务端,也不改变仿真状态。

type ProjectArchive = {
  filename: string   // 建议的文件名,形如 `考试实验.zip`
  bytes: ArrayBuffer // zip 字节
}

打包的默认超时是 60 秒(archiveTimeoutMs),比单次读取长:宿主需要把整棵树读出来再压缩。失败时抛出 PlaygroundEmbedError,code 为 ARCHIVE_UNAVAILABLE。

本地缓存

设置 storageKey 后,SDK 会在嵌入地址上追加 ?storage=<key>,页面把这份分享副本的工作副本(改过的文件、编译产物)存进浏览器 IndexedDB:

  • 同一个 key 再次打开会恢复同一份内容,不依赖用户按保存,刷新页面也不丢。
  • 记录按实验 code 分命名空间,同一个 key 在不同实验之间不会串内容。
  • 只在分享副本上生效——自己的工程本来就保存到服务端。
  • IndexedDB 不可用或写入失败时退回纯内存,编辑不受影响。

clearStorage() 清空这份缓存;页面会重新加载回到分享快照,连接重新握手后继续可用。

嵌入边界

嵌入 API 对任何父页面开放:把实验以 /p/{code}/embed 嵌入后即可连接、读取固件、打包工作区与使用本地缓存,不需要登记接入方站点。能不能嵌进去由该实验的可读性决定——匿名可读的分享副本可以,private 与绑定付费专栏的 unlisted 连页面都拿不到。普通 /p/{code} 页面不允许被跨站 framed,也没有嵌入 API。

file:// 本地页面直接打开时也能嵌入:页面的 origin 是 null,握手与调用照常按 window.parent 投递,不依赖 origin 匹配。这类页面需要给 baseUrl 传绝对地址,file: 基址解析不出 http(s) 的嵌入地址。

code 与可选的 baseUrl 决定嵌入地址,协议版本固定在 SDK 内部。

断开

playground.dispose()

断开后已发出的调用以 CONNECTION_CLOSED 拒绝,iframe 从容器移除。页面重新加载后端口随旧文档失效,需要重新 createPlayground。

发布

自动发布只在推送 playground-sdk/v<version> tag 时触发,tag 必须与 package.json 的版本一致:

cd packages/js/playground-sdk
npm version patch --no-git-tag-version   # 或 minor / major
cd ../../..
git add packages/js/playground-sdk/package.json
git commit -m "chore(playground-sdk): 发布 0.1.1"
git tag playground-sdk/v0.1.1
git push origin playground-sdk/v0.1.1

.github/workflows/publish-playground-sdk.yml 会先跑 test、typecheck、build,再校验 tag 与版本,最后 pnpm publish。CI 使用 npm Trusted Publishing(GitHub OIDC),包设置里登记 x-botlab / xbotlab / publish-playground-sdk.yml 即可,不需要 NPM_TOKEN。

pnpm publish 默认做 git 检查,CI 的 tag checkout 是 detached HEAD,所以 workflow 用 --no-git-checks。手动发布同理:

cd packages/js/playground-sdk
pnpm publish --access public --no-git-checks

prepack 会在发布前重新构建 dist/,不必手动 build。仓库目前是 private,npm 的 provenance 要求公开仓库,所以发布命令没有 --provenance。