Rugo in JavaScript

Alpha. @opteryx/rugo is published under npm's alpha tag. The API and the value mapping may change between releases, so pin the version you test with.

Rugo's Parquet, ORC and Avro readers, compiled to WebAssembly from the same C++ as the Python package. The examples on this page run in your browser: press Run and watch them read a file from this site by byte range.

npm install @opteryx/rugo@alpha

About 370 KB gzipped, no dependencies, ES modules only. It runs in current browsers and in Node 24 or later, on a single thread, so it needs no special server headers.

A Parquet file keeps its schema and row-group metadata at the end. readFooter fetches that and nothing else: usually one request for the last 64 KB, however big the file is.

import { httpSource, readFooter } from "@opteryx/rugo";

const src = await httpSource("https://rugo.dev/data/space_missions.parquet");
const footer = await readFooter(src);

console.log(`${footer.num_rows} rows in ${footer.num_row_groups} row group`);
for (const c of footer.schema) {
  console.log(`  ${c.name}: ${c.logical_type || c.physical_type}`);
}

const bytes = src.reads.reduce((n, [, length]) => n + length, 0);
console.log(`${src.reads.length} request, ${bytes} of ${src.size} bytes`);
4630 rows in 1 row group
  Company: varchar
  Location: varchar
  Price: float64
  Lauched_at: timestamp[us]
  Rocket: varchar
  Rocket_Status: varchar
  Mission: varchar
  Mission_Status: varchar
1 request, 65536 of 93169 bytes

footer.row_groups has the per-column detail: codec, encodings, compressed and uncompressed sizes, null counts, and whether each chunk has statistics, a bloom filter or a page index.

Read only the columns you need

openParquet keeps the file open so you can decode values. readRowGroup takes a row group and a list of columns and makes one request per column chunk. Each column comes back as a plain array, with null for nulls. This counts successful launches by company from two of the file's eight columns.

import { httpSource, openParquet } from "@opteryx/rugo";

const src = await httpSource("https://rugo.dev/data/space_missions.parquet");
const pq = await openParquet(src);
try {
  const { num_rows, columns } = await pq.readRowGroup(0, ["Company", "Mission_Status"]);

  const launches = new Map();
  for (let i = 0; i < num_rows; i++) {
    if (columns.Mission_Status[i] !== "Success") continue;
    launches.set(columns.Company[i], (launches.get(columns.Company[i]) ?? 0) + 1);
  }

  const top = [...launches].sort((a, b) => b[1] - a[1]).slice(0, 5);
  for (const [company, n] of top) console.log(`${String(n).padStart(5)}  ${company}`);
  console.log(`${src.reads.length} requests`);
} finally {
  pq.close();   // frees the file's state in the WebAssembly heap
}
 1614  RVSN USSR
  318  CASC
  282  Arianespace
  203  General Dynamics
  202  VKS RF
3 requests

For a file with many row groups, loop over pq.footer.num_row_groups and read one at a time, so only one row group's values are in memory at once. There is no predicate argument: filter in JavaScript, and use the footer to skip row groups you don't need.

Open a file from your disk

blobSource takes a File from a file picker or a drop and reads it with Blob.slice(). The file is read in this tab. Nothing is uploaded. Choose a Parquet file to run it.

import { blobSource, openParquet } from "@opteryx/rugo";

const file = document.querySelector("#your-file").files[0];
const pq = await openParquet(blobSource(file));
try {
  const { num_rows, schema, row_groups } = pq.footer;
  console.log(`${file.name}: ${num_rows} rows, ${schema.length} columns, ${row_groups.length} row group(s)`);

  // The first three columns of the first five rows
  const names = schema.slice(0, 3).map((c) => c.name);
  const { columns } = await pq.readRowGroup(0, names);
  for (let i = 0; i < Math.min(5, row_groups[0].num_rows); i++) {
    console.log(names.map((name) => `${name}=${columns[name][i]}`).join("  "));
  }
} finally {
  pq.close();
}

                    

Want the whole picture of a file? The viewer shows info, schema and per-column detail for Parquet, Avro and ORC, the same way.

Read Avro and ORC

Avro and ORC have the same shape: open, read a unit, close. Avro is read a batch at a time (whole blocks, up to 65,536 rows) and ORC a stripe at a time. Avro column names can be dotted paths into a record.

import { httpSource, openAvro } from "@opteryx/rugo";

const avro = await openAvro(await httpSource("https://rugo.dev/data/space_missions.avro"));
try {
  const { num_rows, codec, batches } = avro.meta;
  console.log(`${num_rows} rows, ${codec}, ${batches.length} batch`);

  const { columns } = await avro.readBatch(0, ["Mission", "Rocket.Name", "Rocket.Status"]);
  for (let i = 0; i < 3; i++) {
    console.log(`${columns.Mission[i]}: ${columns["Rocket.Name"][i]} (${columns["Rocket.Status"][i]})`);
  }
} finally {
  avro.close();
}
4630 rows, deflate, 1 batch
Sputnik-1: Sputnik 8K71PS (Retired)
Sputnik-2: Sputnik 8K71PS (Retired)
Vanguard TV3: Vanguard (Retired)

ORC is openOrc(source) and readStripe(i, columns). Both of these formats read the whole file into memory when you open them, because neither can be read by byte range the way Parquet can. Files of 4 GB or more are refused, and about 2 GB is the practical limit.

In Node

The same module runs in Node 24 or later. fs.openAsBlob gives you a Blob that reads from disk lazily, so blobSource still reads only the ranges it needs.

import { openAsBlob } from "node:fs";
import { blobSource, openParquet } from "@opteryx/rugo";

const pq = await openParquet(blobSource(await openAsBlob("space_missions.parquet")));
try {
  const { columns } = await pq.readRowGroup(0, ["Mission", "Lauched_at"]);

  // Timestamps are the stored integers, as bigint. The schema says this one is timestamp[us].
  const micros = columns.Lauched_at[0];
  console.log(columns.Mission[0], new Date(Number(micros / 1000n)).toISOString());
} finally {
  pq.close();
}
Sputnik-1 1957-10-04T19:28:00.000Z

Without a build step

The package is plain ES modules plus one .wasm file, so a page can import it straight from a CDN that serves npm packages.

<input type="file" id="file" accept=".parquet" />
<pre id="out"></pre>

<script type="module">
  import { blobSource, readFooter } from "https://cdn.jsdelivr.net/npm/@opteryx/rugo@0.5.13/rugo_footer.mjs";

  document.getElementById("file").addEventListener("change", async (event) => {
    const footer = await readFooter(blobSource(event.target.files[0]));
    document.getElementById("out").textContent = footer.schema
      .map((c) => `${c.name}: ${c.logical_type || c.physical_type}`)
      .join("\n");
  });
</script>

Bring your own source

A source is anything with a size and a read(offset, length) that resolves to a Uint8Array. Wrap an object-store SDK, a cache or a file handle and every reader works with it.

import { open } from "node:fs/promises";
import { readFooter } from "@opteryx/rugo";

const fh = await open("space_missions.parquet");
const source = {
  size: (await fh.stat()).size,
  async read(offset, length) {
    const buf = new Uint8Array(length);
    await fh.read(buf, 0, length, offset);
    return buf;
  },
};

const footer = await readFooter(source);
await fh.close();

What the values look like

Column typeJavaScript value
8, 16 and 32-bit integers, floatsnumber
dates, 32-bit timesnumber (the stored integer)
64-bit integers, timestamps, 64-bit timesbigint (the stored integer; the type gives the unit)
booleanboolean
varchar / string, enumstring
binaryUint8Array
decimalstring, with the decimal point in place
uuidhyphenated hex string
interval{ months: bigint, us: bigint }
list / arraynested arrays, to any depth
ORC struct and map, Avro record and mapJSON text string

A null is always null, at any level.

Good to know

  • Remote files need Range support. httpSource rejects if the server sends the whole file instead. From a browser, reading a file on another origin also needs that server's CORS settings to allow the Range request header and expose Content-Range. If you can't change the server, fetch the file as a Blob and use blobSource.
  • It's a reader. Writing files, CSV and JSONL, and predicate pushdown are in the Python package, not this one.
  • Everything rejects with an Error: a malformed or wrong-format file, an unknown column, an out-of-range row group, a read after close(). Nothing returns a partial result.
  • Versions match the Python package. @opteryx/rugo@0.5.13 is built from the same source as rugo==0.5.13.

More

Examples checked against @opteryx/rugo 0.5.13.

← Back to rugo.dev