[![logo][]](https://xyo.network)

# @xyo-network/xl1-rest-block-viewer

[![npm-badge][]][npm-link]
[![license-badge][]][license-link]

> A BlockViewer over a statically generated REST file layout — anonymous HTTP reads of immutable chain files, no RPC server required.

## About

Finalized XL1 blocks and completed steps never change, so the whole chain can be published once as static files (e.g. to a public Cloudflare R2 bucket behind a CDN) and read forever with plain HTTP GETs. `RestBlockViewer` implements the full `BlockViewer` interface against that layout:

```text
Finalized bucket:
  block/number/<n>.json
  block/hash/<hash>.json
  payload/<hash>.json
  manifest.json

Chain-state bucket:
  chain/head.json                 ← mutable head pointer

Index bucket:
  <family>/<level>/<index>.json   ← completed step summaries (blocks, balances, …)
  manifest.json
  head.json                       ← indexer progress watermark
```

The layout protocol (path builders, manifest schemas, versioning) lives in [`@xyo-network/xl1-network-model`](https://www.npmjs.com/package/@xyo-network/xl1-network-model). See [`STATIC_REST_LAYOUT.md`](../../../protocol/STATIC_REST_LAYOUT.md) for the normative spec. The finalized (immutable) files, chain state, and index may live in separate buckets behind separate domains. Every file except the head pointer and index watermark is immutable, so responses are cached aggressively (in-memory LRU here, CDN/edge in front). `blocksByStep` serves completed steps only — the in-flight tail step has no file and returns `[]`.

```ts
import { RestBlockViewer } from '@xyo-network/xl1-rest-block-viewer'

const viewer = await RestBlockViewer.create({
  finalizedBaseUrl: 'https://chain.example.com', // finalized (immutable) files
  chainStateBaseUrl: 'https://head.example.com', // optional: chain state / head pointer domain (defaults to finalizedBaseUrl)
  headPollIntervalMs: 10_000, // optional: emit headUpdated as the head advances
})
await viewer.start()
const head = await viewer.currentBlock()
```

Reads are anonymous — no S3 client or credentials. Parsed files are validated with the protocol Zod schemas before being returned.

## Install

Using npm:

```sh
npm i --save @xyo-network/xl1-rest-block-viewer
```

Using yarn:

```sh
yarn add @xyo-network/xl1-rest-block-viewer
```

Using pnpm:

```sh
pnpm add @xyo-network/xl1-rest-block-viewer
```

Using bun:

```sh
bun add @xyo-network/xl1-rest-block-viewer
```

## What's Inside

- **`RestBlockViewer`** — full `BlockViewer` implementation: blocks by number/hash, walking reads, `currentBlock` head polling with `headUpdated` events, completed-step `blocksByStep`, payloads by hash, and the rate calculations.
- **`RestChainStateViewer`** — focused `ChainStateViewer` implementation: just the head pointer, with optional `headUpdated` polling.
- **`RestIndexViewer`** — focused `IndexViewer` implementation: published step summaries (the chain index).
- **Layout contract** — re-exported from `@xyo-network/xl1-network-model` for backward compatibility (`headPath`, `blockNumberPath`, `indexSummaryPath`, manifest schemas, etc.). New code should import the contract directly from `@xyo-network/xl1-network-model`.

## Building Locally

```sh
xy build @xyo-network/xl1-rest-block-viewer
xy test @xyo-network/xl1-rest-block-viewer
xy lint @xyo-network/xl1-rest-block-viewer
```

## Maintainers

<table>
  <tr>
    <td align="center" valign="top" width="120">
      <a href="https://github.com/arietrouw">
        <img src="https://github.com/arietrouw.png" width="80" height="80" alt="Arie Trouw" /><br />
        <sub><b>Arie Trouw</b></sub>
      </a>
      <br />
      <a href="https://arietrouw.com">arietrouw.com</a>
    </td>
    <td align="center" valign="top" width="120">
      <a href="https://github.com/jonesmac">
        <img src="https://github.com/jonesmac.png" width="80" height="80" alt="Matt Jones" /><br />
        <sub><b>Matt Jones</b></sub>
      </a>
    </td>
    <td align="center" valign="top" width="120">
      <a href="https://github.com/JoelBCarter">
        <img src="https://github.com/JoelBCarter.png" width="80" height="80" alt="Joel Carter" /><br />
        <sub><b>Joel Carter</b></sub>
      </a>
    </td>
  </tr>
</table>

## License

See the [LICENSE](./LICENSE) file (LGPL-3.0-only).

## Credits

[Made with 🔥 and ❄️ by XYO Foundation](https://xyo.network)

[npm-badge]: https://img.shields.io/npm/v/@xyo-network/xl1-rest-block-viewer.svg
[npm-link]: https://www.npmjs.com/package/@xyo-network/xl1-rest-block-viewer
[license-badge]: https://img.shields.io/npm/l/@xyo-network/xl1-rest-block-viewer.svg
[license-link]: ./LICENSE
[logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
