Skip to content
superdbPublic

About

A web-based JSON explorer with SuperSQL queries

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

SuperLens

SuperLens is a web app for exploring data files with SuperSQL. You open or drop in JSON, CSV, Parquet, and other formats, type a query, and browse the results in a table, a tree inspector, or as SUP text. It's meant to be hosted at lens.superdb.org.

The whole SuperDB query engine runs inside your browser, compiled to WebAssembly.

Your data stays on your computer

SuperLens has no server-side component. The site is a set of static files: HTML, JavaScript, CSS, and the query engine (super.wasm).

  • Files are never uploaded. When you open or drop a file, the query engine in the same tab reads it from your computer as queries need it. Queries run locally, and the results are rendered locally.
  • The page can't send your data anywhere. Its content security policy (set in index.html, and also as an HTTP header by the dev server and public/_headers) only allows connections to the site itself (connect-src 'self'). The only things it fetches from there are its own code and the engine.
  • Nothing is kept. Closing or reloading the tab discards your files and results. The only things saved, in the browser's local storage, are a few preferences: the last query, the input format, the result limit, and the view.
  • Downloads are made locally. "Download" builds the SUP file in the page.

Using SuperLens

  • Open files with the Open files… button, or drop them anywhere on the page. You can open several files; dropping a file with the same name replaces it. Remove files with the × on their chips. Files are read as each query needs them, so they can be larger than the 4 GB of memory the engine has (a query's results still have to fit). If a file changes after you open it, open it again.

  • Formats are detected automatically: JSON (including NDJSON), CSV, TSV, Parquet, SUP, BSUP, Arrow IPC streams, Zeek logs, and plain lines of text. If detection guesses wrong, choose the format from the Format menu (it applies to every file).

  • With no query, everything is shown. Every value in every file appears, so a single JSON document shows up as one value in the inspector.

  • Type a query and press Enter to run it (Shift+Enter adds a new line). A query runs over all of the open files, e.g. count() by type, where status >= 500 | sort -r time, or select name, count(*) from .... To query one file, name it: from 'events.json' | head 10. A query that starts with from, or that has its own SQL FROM clause, reads only the files it names. See the SuperSQL reference for the language.

  • Superfuse shows the shape of your data: one type that every value fits, with fusion(...) marking where the values differ. The button adds aggregate superfuse(this) to the end of the query (or uses it alone if the query box is empty), runs it, and shows the result in the inspector. The query box shows what ran, so you can edit it, e.g., to fuse just the values from one file or of one kind.

  • Views:

    • Table shows a row per value and a column per field. Nested records are flattened into dotted column names. When values have different shapes, their columns are merged, and a hatched cell marks a field that a row doesn't have. The columns come from the results' fused type, so every field of every value has one, even when there are thousands. Drag a column's right edge to resize it. Click a row (or use the arrow keys) to inspect it in the side panel, where you can copy it as SUP or JSON.
    • Inspector shows values as an expandable tree. Small results open fully, larger ones partially, and big ones collapsed. Use Expand all and Collapse all, or click a row to toggle it. After Expand all, each value expands as it scrolls into view. After Collapse all, later results open collapsed too, until you click Expand all. This browser remembers the choice. Type values, such as the results of typeof(this) or superfuse(this), open the same way, into the fields of their types.
    • SUP shows each value as a line of SUP.

    SuperLens picks the table for a set of records and the inspector for anything else. Once you pick a view, it sticks.

  • Show limits how many values are displayed (100,000 by default). When a result is cut off, a warning says so. Choose all to remove the limit. Values are decoded only as they're shown, so a big result mostly costs the memory of its BSUP.

  • Copy and Download save the results in the format chosen next to them (SUP by default), from any view. They run the query again, so they include all of the results, not just the ones Show allows, and the output comes straight from SuperDB. CSV, TSV, Parquet, and Arrow need one set of columns for every value, so | fuse is added to the query for them, and a note says so when the results have more than one shape. Binary formats (Parquet, BSUP, and Arrow) can be downloaded but not copied. Since they include everything, they can need more memory than showing the results does.

  • Stop cancels a running query. It restarts the engine, which takes a moment.

How it works

 ┌─────────────── browser tab ───────────────────────────────────────────┐
 │                                                                       │
 │  app (src/app.ts)              engine worker (src/engine/worker.ts)   │
 │  ┌──────────────────┐  files   ┌───────────────────────────────────┐  │
 │  │ files, query box │ ───────► │ super.wasm (wasm/main.go)          │  │
 │  │                  │  query   │  SuperDB compiler + runtime        │  │
 │  │ table, inspector │ ◄─────── │  reads the files in pieces         │  │
 │  │ SUP views        │   BSUP   │  writes results as BSUP            │  │
 │  └──────────────────┘          └───────────────────────────────────┘  │
 │     ▲ decodes and defuses just the values on screen, with super-ts    │
 └───────────────────────────────────────────────────────────────────────┘
  • The engine (wasm/main.go) is a small Go program built against your checkout of the super repo. It's adapted from the playground in super's book (book/super-example) with these changes:

    • Inputs are a list of named files that the engine reads in pieces as it needs them, straight from the browser (with FileReaderSync in the worker). So files don't have to fit in the engine's memory, Parquet (which needs random access) works, and queries can refer to files by name.
    • JSON is read in batches of up to 1,024 values, not super's 10,240, so files of records the size of GitHub Archive events (about 20 KB each) don't use up the engine's memory.
    • Files are given to the engine once and reused by later queries.
    • Results are written as BSUP bytes, the same format and control messages the SuperDB service uses, instead of as text.
    • Fused values stay fused. When super reads data whose values have many different shapes, such as JSON, it fuses them into one supertype. Every place where the values differ becomes a fusion value, which holds the value as the supertype plus its original type. Defusing them in the engine (as super does on output) and writing the result means encoding a vector for every distinct type. With tens of thousands of types, that runs out of the engine's 4 GB. Left fused, the result is a few column frames of one supertype. (Copy and Download match super's output, so they defuse every format except BSUP, which keeps the fusion values as super -f bsup does.)
    • There's a result limit that sends a warning when it's reached.
  • The worker loads the engine and runs queries off the main thread, so the page stays responsive. Stopping a query replaces the worker, reusing the already-compiled engine.

  • The results (src/results.ts) keep the BSUP and decode values only when a view asks for them. They use these parts of super-ts:

    • BSUPResults reads the frames' headers and locators up front, which takes milliseconds, and decodes value i on demand. Each column segment is decompressed and decoded the first time a value needs it and then cached. The cache drops its least recently used segments once it passes 256 MB.
    • Defuser defuses each value as it's decoded, the same way super does.
    • The last 2,000 values are cached. Values that drop out of the cache, like rows that have scrolled away, can be garbage collected.

    The views use super-ts's types and values directly: the fused types for the table's columns, records for cells, the Type inside a type value for the inspector's type tree, and toString() for SUP.

  • The views (src/views/) are written in plain TypeScript and DOM and work on a source of values (ValueSource) instead of an array. They're modeled on zui's table and inspector and only render, and so only decode, what's on screen:

    • The table renders only the rows and columns in view.
    • The inspector only builds the rows of the values that are expanded, and rebuilds them from the results if they've been dropped.

There's no bundler or framework. The build compiles TypeScript to ES modules that browsers load directly, and the site has no runtime dependencies.

Building

Prerequisites

  • Go 1.27 or later, the version super's go.mod requires, to build the engine.

  • Node.js 22.18 or later, for the build scripts and tests.

  • Checkouts of super and super-ts next to this repo:

    ~/repo/super       (github.com/superdb/super)
    ~/repo/super-ts    (github.com/superdb/super-ts)
    ~/repo/superlens   (this repo)
    

    Set SUPER_REPO and SUPER_TS to use checkouts elsewhere.

Build and run

npm install          # installs TypeScript (for the build) and Playwright (for the smoke tests)
npm run build        # builds the engine and the app into dist/
npm run serve        # serves dist/ at http://localhost:8080/

npm run build does the following:

  1. Builds dist/super.wasm by compiling wasm/main.go from inside the super repo (scripts/build-wasm.sh), so it uses that checkout and its go.mod. It also copies Go's wasm_exec.js into dist/. This step takes a minute or so and, the first time, needs the network to download Go modules.
  2. Compiles super-ts from SUPER_TS into dist/vendor/super-ts/.
  3. Compiles the app from src/ into dist/app/, pointing its super-ts imports at the vendored copy.
  4. Copies public/ (the HTML, CSS, icon, sample data, and _headers) into dist/.

Other scripts:

command what it does
npm run dev builds the app (and the engine, if dist/super.wasm is missing), recompiles on changes, and serves at :8080. Reload the page to see changes.
npm run build:app rebuilds the app and super-ts without rebuilding the engine
npm run build:wasm rebuilds just the engine, e.g., after pulling super
npm run check type checks the app (run a build first so the super-ts types are in dist/vendor)
npm test runs the unit tests (tree, columns, cell formatting) against the super-ts sources
npm run test:e2e runs the browser smoke tests in e2e/ against dist/ (see Testing)
npm run clean removes dist/

Examples:

SUPER_REPO=~/src/super npm run build:wasm      # use another super checkout
SUPER_TS=~/src/super-ts npm run build:app      # use another super-ts checkout
npm run serve -- --port 9000

Testing

npm test runs the unit tests in Node. npm run test:e2e runs smoke tests of the built site in headless Chromium with Playwright: it serves dist/ the way npm run serve does, then opens the sample data and some small files, runs queries, checks what the views show, and downloads results. They catch a build that's broken, e.g., by a change in super or super-ts. To run them:

npm run build
npx playwright install chromium    # once, to download the browser
npm run test:e2e

To run them against a site that's already up instead, such as a new deploy, set SUPERLENS_URL to its address, ending in a slash, e.g., SUPERLENS_URL=https://example.com/ npm run test:e2e.

GitHub Actions (.github/workflows/test.yaml) runs the build, the unit tests, and the smoke tests with the current main of super and super-ts on each pull request and merge. Run it by hand from the Actions tab to build with another branch or commit of either one.

Hosting

dist/ is a static site that any static host can serve, such as Cloudflare Pages, Netlify, S3 with CloudFront, or GitHub Pages. To deploy, upload the contents of dist/. Some things to configure:

  • Headers. public/_headers (copied to dist/_headers) sets the content security policy, nosniff, and the WebAssembly content type for hosts that read that file, such as Cloudflare Pages and Netlify. On other hosts, set these headers yourself. The policy is also in index.html as a <meta> tag, but the HTTP header covers the worker too.
  • super.wasm must be served as application/wasm.
  • Compression. super.wasm is large (about 90 MB uncompressed, 16 MB with gzip), so make sure the host serves it compressed with gzip or brotli. Browsers cache it after the first visit.
  • No server code and no cookies are needed. SuperLens never calls an API.

Project layout

wasm/main.go              the engine (Go, built against the super repo)
src/main.ts               entry point
src/app.ts                the app: files, query, views, status
src/engine/engine.ts      runs queries in the worker
src/results.ts            query results: values decoded and defused on demand (super-ts)
src/engine/worker.ts      loads super.wasm and runs queries off the main thread
src/engine/protocol.ts    messages between the app and the worker
src/views/table.ts        the table view
src/views/columns.ts      table columns from the results' fused types
src/views/inspector.ts    the inspector view
src/views/tree.ts         the inspector's tree model and ValueSource
src/views/text.ts         the SUP view
src/views/virtual.ts      the virtualized list behind all three views
src/views/cells.ts        value text, colors, and JSON
src/views/choose.ts       picks the default view for a result
public/                   index.html, style.css, favicon, sample data, _headers
scripts/                  build, dev server, and wasm build scripts
test/                     unit tests (node --test)
e2e/                      browser smoke tests (Playwright; settings in playwright.config.ts)
.github/workflows/        CI: build and test with super and super-ts main

Troubleshooting

  • "The query engine failed to load." Check that dist/super.wasm and dist/wasm_exec.js exist and are served as application/wasm and JavaScript. wasm_exec.js must come from the same Go version that built super.wasm; npm run build:wasm copies the matching one.
  • The wasm build fails. The engine compiles against your super checkout. If super's internal APIs have changed, wasm/main.go may need updating. It uses parser.ParseText, storage.NewInternalEngine, exec.NewEnvironment, compiler.NewCompilerWithEnv, runtime.CompileQuery, queryio.NewBSUPWriter, and expr.NewSuperDefuse (for Copy and Download).
  • A file is read in the wrong format. Pick the format from the Format menu.
  • The page gets slow with huge results. Lower Show, or aggregate in the query (count() by ..., head, where).

About

A web-based JSON explorer with SuperSQL queries

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages