# SB3 expanded source format v1

[日本語版](ja/source-format-v1.md)

Import and unpack always write v1. Folders in [format v2](source-format-v2.md), which 0.25 wrote for
projects with block palette comments, are still read and built.

## Layout

```text
SOURCE_DIR/
├── sb3-source.json
├── project.source.json
├── embedded-extensions.json
├── .tw-plus/
│   └── workspace.json        (optional)
├── assets/
│   └── <assetId>.<dataFormat>
└── extensions/
    └── <extensionId>.js
```

`.tw-plus/workspace.json` is the screen state TurboWarp Plus Editor saves with a project: its tab layout,
the block groups each sprite shows in the palette, and so on. An SB3 carries it as the ZIP entry
`tw-plus/workspace.json`, which Scratch and TurboWarp ignore. Import copies the entry to this file byte for
byte and lists it in `archiveEntries`; build puts the file back in the same place. The Editor writes the file
itself when it saves a project folder; such a file, not listed in `archiveEntries`, becomes the last entry of
the SB3. The toolchain only checks that it is JSON, and a folder without it builds without the entry.

`sb3-source.json` records the format version, paths to each component, and the ZIP entry order of the
original SB3.

```json
{
  "formatVersion": 1,
  "project": "project.source.json",
  "embeddedExtensions": "embedded-extensions.json",
  "assetsDirectory": "assets",
  "archiveEntries": ["project.json", "<assetId>.svg"]
}
```

## `project.source.json`

This file is the original SB3 `project.json` formatted as two-space-indented JSON. Array and object
order are preserved as meaningful input. A TurboWarp extension that was embedded as a data URL is
replaced by this reference:

```text
embedded-extension:extensions/<extensionId>.js
```

During a build, the media type, parameters, and encoding from `embedded-extensions.json` are used to
reconstruct the data URL.

## `embedded-extensions.json`

This file records embedded extension data URL information and extraction paths. Optional `source`
metadata can be added for an extension managed from GitHub or an installed npm package.

```json
{
  "formatVersion": 1,
  "extensions": [
    {
      "id": "example",
      "path": "extensions/example.js",
      "mediaType": "text/javascript",
      "parameters": [],
      "encoding": "base64",
      "source": {
        "provider": "github",
        "repository": "owner/example-extension",
        "ref": "main",
        "resolvedCommit": "0123456789abcdef0123456789abcdef01234567",
        "artifact": "dist/example.js",
        "integrity": "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
        "apiManifest": {
          "artifact": "dist/extension-manifest.json",
          "path": "extensions/example.manifest.json",
          "formatVersion": 1,
          "integrity": "sha256-BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB="
        }
      }
    }
  ]
}
```

For an exact npm dependency, replace the GitHub-specific fields with the package identity:

```json
{
  "provider": "npm",
  "package": "@owner/example-extension",
  "version": "1.2.3",
  "artifact": "dist/example.js",
  "integrity": "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
}
```

- `provider`: `github` or `npm`
- `repository`: GitHub repository in `<owner>/<repository>` form
- `ref`: branch, tag, or commit to track for update candidates
- `resolvedCommit`: lowercase 40-character commit SHA used to obtain the current file
- `package`: exact npm package name, including its scope when applicable
- `version`: exact semantic version recorded in the installed package's `package.json`; ranges are not accepted
- `artifact`: JavaScript artifact path relative to the repository or npm package root
- `integrity`: SRI-form SHA-256 of the current file
- `apiManifest`: optional, opt-in API compatibility metadata
  - `artifact`: API manifest path relative to the same GitHub repository
  - `path`: required local path `extensions/<extensionId>.manifest.json`
  - `formatVersion`: supported manifest format, `1` or `2`
  - `integrity`: SRI-form SHA-256 of the installed manifest

Validation and builds verify managed extensions without accessing the network:

- The actual file SHA-256 matches `integrity`
- The file's `// ID: <extensionId>` header matches `id`
- The GitHub repository, ref, commit, npm package, exact version, and artifact path use safe forms
- When `apiManifest` exists, its JSON schema, ID, format version, path, and SHA-256 are valid

Re-importing an SB3 into an existing expanded source directory preserves `source` metadata for the
same ID and path. If the imported file does not match the recorded hash or ID, the import is rejected
without changing the existing output. A new import cannot infer provenance, so it does not create
`source` automatically.

For GitHub sources, `extensions sync` downloads the JavaScript and optional API manifest using the
immutable `resolvedCommit`. For npm sources, it reads them from the exact package installed in the
nearest ancestor `node_modules` directory without network access. Both paths restore local files only
when IDs and hashes match existing metadata. After the package manager installs a newer exact npm
version, `extensions update` copies its artifacts and updates `version` and integrity metadata.
GitHub updates instead resolve `ref` and update `resolvedCommit`. Candidate API manifests are compared
before either type is installed. The expanded source remains unchanged until every selected entry has
been validated.

An extension ID migration changes the manifest `id` and `path`, known references in
`project.source.json`, and `extensions/<id>.js` in one transaction. When a managed extension is
migrated as part of `extensions update`, `artifact` when explicitly supplied, `resolvedCommit`, and
`integrity` are also updated for the new artifact. An opt-in API manifest is compared after
normalizing the explicit old and new top-level IDs, saved under the new ID, and updated in the same
transaction. If its remote filename also changes, `--api-manifest-artifact` updates
`source.apiManifest.artifact` explicitly.

API manifest files are expanded-source validation inputs, not SB3 archive entries. They do not
change deterministic SB3 bytes. See
[`extension-api-compatibility.md`](extension-api-compatibility.md) for manifest v1, compatibility
classification, the explicit breaking-change override, and rollback.

### `extensionBundles`

Add the optional `extensionBundles` array to make multiple individual extensions one permission unit
only in the generated SB3.

```json
{
  "formatVersion": 1,
  "extensions": [
    {
      "id": "extensionone",
      "path": "extensions/extensionone.js",
      "mediaType": "text/javascript",
      "parameters": [],
      "encoding": "base64"
    },
    {
      "id": "extensiontwo",
      "path": "extensions/extensiontwo.js",
      "mediaType": "text/javascript",
      "parameters": [],
      "encoding": "base64"
    }
  ],
  "extensionBundles": [
    {
      "id": "projectbundle",
      "name": "Project Extension Bundle",
      "members": ["extensionone", "extensiontwo"]
    }
  ]
}
```

- `id`: composite extension ID in TurboWarp's `[a-z0-9]+` form that does not collide with an individual extension
- `name`: single-line name displayed in the permission prompt and extension category
- `members`: two or more individual extension IDs; their order defines block and menu composition order. The palette receives a decorated, metadata-bearing bundle LABEL heading and a double separator between groups, while LABEL entries and separators from the original extensions remain unchanged
- `recoveryCapsule`: optional boolean, default `false`. Set it to `true` only when the extra size is acceptable and direct unbundling from the generated SB3 is required
- `buildMode`: optional `classic` (default) or `module`. Module mode requires each member's npm `source.module` JavaScript artifact, format 1, SHA-256 integrity, and explicit `unsandboxed` requirement. It builds one browser dependency graph without executing owner code
- `sharedDependencies`: optional package-to-exact-version map, allowed only in module mode. The selected packages must be installed in the project with a dependency lock; compatible member imports share one real package. See the [module build guide](extension-bundles.md#module-builds-off-by-default)
- `sandboxedNotice`: optional object mapping lowercase language tags to the message shown when the bundle is loaded sandboxed. Allowed only when a member's `// Unsandboxed:` header makes the bundle `required` or `optional`; built-in `en` and `ja` messages apply otherwise. See [`extension-bundles.md`](extension-bundles.md#declare-unsandboxed-loading)

A member cannot belong to more than one bundle. Individual entries, individual JavaScript, `source`,
and `project.source.json` remain authoritative. `build` adds `memberId + "__"` to each original opcode
and changes only the output `project.json` opcodes, `extensions`, `extensionURLs`, and extension storage
to the bundle ID. It embeds one bundle JavaScript data URL.

By default, the bundle omits duplicate recovery data while leaving member code and header notices in
the generated JavaScript. `recoveryCapsule: true` adds a format 1 capsule with the original individual
data URLs and extension order. Removing the configuration always restores the ordinary output from
expanded source; direct `extensions unbundle` of the generated SB3 requires the capsule. See
[`extension-bundles.md`](extension-bundles.md) for the compatibility contract and restoration steps.

## Assets

`assets/` contains costumes, sounds, and embedded fonts referenced by `project.source.json`.
Each filename is `<assetId>.<dataFormat>`, and `assetId` must match the MD5 of its contents.
A `customFonts` entry with `system: false` references a font file by `md5ext` (`ttf`, `otf`, `woff`, or `woff2`).
Entries with `system: true` are kept in the JSON, including any web font reference.
An unreferenced, missing, or extra file is a validation error.

## Deterministic output

- ZIP entries follow `archiveEntries`
- ZIP timestamps are fixed at 1980-01-01 00:00:00
- Compression level is fixed at 6
- `project.json` is stored as minimal JSON with a trailing newline
- The same source format version, input files, and tool version produce the same byte sequence

## Safety

- Reject ZIP entries containing absolute paths, `\`, empty components, `.`, or `..`
- Reject symbolic links and special files as source input
- Reject a filesystem root, Git repository root, its ancestor, or `.git/` as an import destination
- Never overwrite an unrecognized existing directory
- Never discard Git-managed changes without the explicit two-stage override
- Never overwrite automatically when an interrupted replacement rollback area is present
- Verify managed extensions offline against their commit, SHA-256, and extension ID
- Preserve individual JavaScript and provenance during bundling and reject contracts whose compatibility cannot be established
- Fetch from GitHub only over HTTPS with redirects rejected and a size limit, and never execute downloaded JavaScript
