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

@scratch-code/scratchblocks-codec

v0.1.0

Published

Convert scratchblocks-plus syntax trees to and from @scratch-code/ast

Readme

@scratch-code/scratchblocks-codec

Convert syntax trees from scratchblocks-plus to and from the semantic tree-shaped AST from @scratch-code/ast.

Installation

npm install @scratch-code/scratchblocks-codec @scratch-code/turbowarp-blocks

This package is ESM-only and requires Node.js 22 or newer. A block-spec registry is supplied by the caller; the example below uses the TurboWarp catalog.

import { createTurboWarpBlockRegistry } from '@scratch-code/turbowarp-blocks';
import {
  deserializeScratchblocks,
  serializeScratchblocks,
} from '@scratch-code/scratchblocks-codec';
import { parse } from 'scratchblocks-plus/syntax';

const registry = createTurboWarpBlockRegistry();
const document = parse('move (10) steps', { languages: ['en'] });
const scripts = deserializeScratchblocks(document, registry);
const rebuilt = serializeScratchblocks(scripts, registry);

rebuilt.stringify(); // "move (10) steps"

Both directions require a block-spec registry. The registry supplies semantic input and field types, menu-shadow defaults, ordered arguments, explicit syntax bindings, and block shapes. The codec uses scratchblocks-plus/syntax; it does not require a DOM, canvas implementation, or renderer.

Type conversion

Scratchblocks input shapes are intentionally permissive. The default loose mode converts scalar values to the type required by the registry. Numeric text is preserved as text, so values such as 0010 remain intact. A scalar in a boolean slot becomes an empty input because the semantic AST has no boolean literal. Nested blocks are retained rather than coerced into literals.

Use {coercion: "strict"} in either direction to reject incompatible scalar or statement shapes instead.

Dropdown children are interpreted using the spec:

  • field slots become semantic fields;
  • value slots with a menu-shadow default become a nested menu block;
  • static option labels are converted back to their canonical Scratch values.

Languages and children

Serialization starts with scratchblocks-plus's built-in English language. A BlockSpec's ordered arguments and explicit bindings.scratchblocks.blockId construct a minimal English pivot block containing only semantic children— fields, inputs, nested blocks, and scripts. It then calls Block.translate(), allowing scratchblocks-plus to rebuild labels, icons, RTL layout, and localized child ordering. The codec does not read raw blockJson.args* and does not use English message matching as its primary identity mechanism.

Pass a loaded LanguageData object as language to produce another language. Blocks not registered in the English scratchblocks language table cannot be reconstructed merely from a translation key and produce an explicit metadata error.

Custom procedures

Definitions are indexed before calls, including forward references. Procedure argument IDs are deterministic and shared by matching definitions and calls. Pass createProcedureArgumentId to choose another ID scheme.

Scratchblocks syntax does not contain Scratch 3 prototype defaults or warp state. New mutations therefore use Scratch 3-compatible defaults:

  • %n and %s: ""
  • %b: false
  • warp: false

Preserved scratchblocks metadata

Comments, diff markers, and glow wrappers are stored under versioned metadata.scratchblocks and restored when serializing. Script metadata only permits glow; Block metadata permits comment, diff, and glow. Use getScratchblocksScriptMetadata() and getScratchblocksBlockMetadata() for typed access. Ordinary labels and icons are rebuilt and are not copied into semantic metadata.

Scratchblocks ASTs do not carry workspace coordinates or variable, list, and broadcast IDs. Those values are not invented. This package provides semantic round-tripping, not byte-for-byte SB3 preservation.

Tests

The test suite includes reproducible block-graph fragments extracted from these local corpus projects:

  • ASCII Key Detector.sb3
  • Tile Based Game Tutorial Part 1.sb3
  • Interactive Bézier Curve.sb3

The fixtures exercise SB3 → scratch-code → scratchblocks → scratch-code for variables/lists, nested boolean reporters and custom arguments, and broadcast menu shadows without requiring the external corpus directory in CI.

License

MIT