Build with eeshgarg

A ten-minute start for developers: the browser SDK, window.eeshgarg, workflows and the command line, with a first call, errors, limits and recipes.

eeshgarg.com does its work in the browser, on the user's device, so there is no server to call. You build on it with a small script API, a dependency-free module, or a command line. This page gets you from nothing to a working call in about ten minutes and points to the reference pages for every table.

Samples marked Runs in Node are executed by the test suite against the real operations on every change, so they do what they say. Samples marked Browser only need a page; the tests check their tool ids, option names and SDK names against the real ones but do not run them.

Pick a surface

The integration surfaces
SurfaceWhere your code runsStart here
window.eeshgargThe eeshgarg.com tab: the console, a quick script, an experiment.Your first call below.
Browser SDKYour own web page. A hidden frame on eeshgarg.com does the work on the visitor's device.Load the SDK below.
Workflow runnerEither of the above, given a workflow JSON file saved from the board.Running a saved workflow below.
CLI / headless runnerYour computer, with Node. Runs the same site in a local headless browser.The command-line equivalent below.

There is no HTTP endpoint that converts a file. API reference has the full method list.

Load the SDK

The SDK is one ES module served from /sdk/v1/eeshgarg-sdk.mjs (CORS open, no build step, no dependencies). Import it by URL, create one client for your page, and call dispose() when you are done. Your page's own CSP needs script-src https://eeshgarg.com and frame-src https://eeshgarg.com. A working page is /sdk/v1/example.html. Pin the v1 path: a breaking change gets a new path.

Your first call, end to end

Open eeshgarg.com, open the browser console, and paste this. It builds a file in memory, runs one operation and reads the result. Runs in Node (tested).

const input = new File(["hello from my script\n"], "hello.txt", { type: "text/plain" });
const { files, summary, warnings } = await eeshgarg.runTool("feature:text-transform", [input], { op: "upper" });
console.log(summary, warnings.length, files[0].name, await files[0].text());

You should see:

Text transform: 1 file 0 hello-upper.txt HELLO FROM MY SCRIPT

The same call from your own page, with the SDK. Browser only.

// Browser-only: the SDK adds a hidden frame to your page. Your page's CSP must allow https://eeshgarg.com.
import { createClient, SdkError } from "https://eeshgarg.com/sdk/v1/eeshgarg-sdk.mjs";

const client = await createClient({ origin: "https://eeshgarg.com", timeoutMs: 60000 });
try {
  const { files, summary } = await client.run("feature:text-transform", [input], { op: "upper" });
  const blob = new Blob([files[0].bytes], { type: files[0].type });
  console.log(summary, files[0].name, blob.size);
} finally {
  client.dispose();
}

Not sure which id to use? Find it and read its options. Runs in Node (tested).

const tools = await eeshgarg.listTools();
const mine = tools.filter((t) => t.id.startsWith("feature:text"));
const info = await eeshgarg.describe("feature:text-transform");
console.log(mine.length > 0, info.options.map((o) => o.name), info.options.find((o) => o.name === "op").choices.includes("upper"));

You should see:

true [ 'op', 'find', 'replace', 'regex', 'caseSensitive' ] true

The ids are also listed in the capability catalog.

Inputs and outputs

  • In: a File, a Blob with a name, or { name, bytes } (bytes is a Uint8Array or ArrayBuffer).
  • Out: { files, summary, warnings }. On window.eeshgarg each output is a File; from the SDK each is { name, type, bytes }. The count and names are chosen by the operation, so read files.length instead of assuming one.
const bytes = new TextEncoder().encode("a,b\n1,2\n");
const { files } = await eeshgarg.runTool("feature:convert", [{ name: "table.csv", bytes }], { to: "json" });
// Outputs are File objects here; the SDK returns { name, type, bytes } instead.
console.log(files[0].name, JSON.parse(await files[0].text()));

You should see:

table.json [ { a: 1, b: 2 } ]

To offer a result for download, wrap it in a Blob, make an object URL, and revoke it when done (see the SDK sample above).

Options and validation

Every option is optional and takes its default. An option must be one the tool lists, of the same kind as its default and, if it has choices, one of them. A bad call fails before any file is read, so mistakes are cheap while you develop. Read the legal options from describe(id). Runs in Node (tested).

const input = new File(["x"], "x.txt", { type: "text/plain" });
const attempts = [
  ["unknown tool", () => eeshgarg.runTool("no-such-tool", [input])],
  ["unknown option", () => eeshgarg.runTool("feature:text-transform", [input], { nope: 1 })],
  ["wrong value", () => eeshgarg.runTool("feature:text-transform", [input], { op: "sideways" })],
  ["no files", () => eeshgarg.runTool("merge", [])],
];
for (const [label, attempt] of attempts) {
  try {
    await attempt();
  } catch (e) {
    console.log(label, e.name); // Error, TypeError, TypeError, RangeError
  }
}

You should see:

unknown tool Error
unknown option TypeError
wrong value TypeError
no files RangeError

Errors and error codes

On window.eeshgarg the error class tells you the kind: Error for an unknown id, TypeError for a bad option or a file of the wrong kind, RangeError for the wrong number of files, AbortError when cancelled, and WorkflowValidationError (with .errors) for an invalid workflow. The SDK reports the same problems as an SdkError with a stable code: bad_request, unknown_op, bad_options, bad_files, aborted, timeout, frame_unavailable, incompatible and internal. Branch on the code, never on the message text. The meaning of each is in the API reference. Browser only.

// Browser-only: an SdkError carries a stable code; branch on the code, not the message.
import { createClient, SdkError } from "https://eeshgarg.com/sdk/v1/eeshgarg-sdk.mjs";

const client = await createClient();
const controller = new AbortController(); // controller.abort() rejects the run with code "aborted"
try {
  await client.run("feature:text-transform", [input], { op: "upper" }, {
    signal: controller.signal,
    onProgress: ({ done, total, label }) => console.log(label, done, total),
  });
} catch (e) {
  if (!(e instanceof SdkError)) throw e;
  if (e.code === "aborted") console.log("cancelled");
  else if (e.code === "bad_options" || e.code === "bad_files") console.log("fix the input:", e.message);
  else if (e.code === "timeout" || e.code === "frame_unavailable") console.log("try again later");
  else console.log(e.code, e.message);
} finally {
  client.dispose();
}

Cancellation and progress

Pass an AbortSignal as { signal } to stop a run. An operation stops when it next checks the signal, so a long one may take a moment. A signal that is already aborted rejects immediately and runs nothing. Pass { onProgress } to receive { done, total, label } from tools that work in steps; feature operations do not report progress. Runs in Node (tested).

const input = new File(["x"], "x.txt", { type: "text/plain" });
const controller = new AbortController();
controller.abort(); // in a real page, call this from a Cancel button while a run is in flight
try {
  await eeshgarg.runTool("feature:text-transform", [input], { op: "upper" }, { signal: controller.signal });
  console.log("finished");
} catch (e) {
  console.log("cancelled:", e.name === "AbortError");
}

You should see:

cancelled: true

Batch and chaining

A tool that takes several files (merge, for example) treats them as one batch. A tool that takes one file runs once per call, so loop. Results are file lists, so one result is the next call's input. Runs in Node (tested).

const seen = [];
const names = [];
for (const file of [pdfA, pdfB]) {
  // One call per file for a tool that takes one file; onProgress is only called by tools that work in steps.
  const { files } = await eeshgarg.runTool("rotate", [file], { angle: 90 }, { onProgress: (p) => seen.push(p) });
  names.push(files[0].name);
}
console.log(names, Array.isArray(seen));
const json = await eeshgarg.runTool("feature:convert", [csv], { to: "json" });
const sum = await eeshgarg.runTool("feature:hash", json.files, { algorithm: "sha256" });
console.log(json.files[0].name, sum.files[0].name);

Running a saved workflow

Build and save a workflow on the board, or write the JSON by hand, then run it with runWorkflow(definition, files, { signal, onNode }). The definition can be the object or its JSON text. The result has a status (done, failed or cancelled), the output files per output block, and the state of each block. From the SDK, validate first with validateWorkflow. The format is in the Workflow API; exactly how it runs is in Workflow semantics. The first sample is Runs in Node (tested, using the text-cleanup recipe from Examples and recipes); the second is Browser only.

const lines = new File(["pear\napple\npear\n"], "fruit.txt", { type: "text/plain" });
const states = [];
const result = await eeshgarg.runWorkflow(recipe, [lines], { onNode: (n) => states.push(n.status) });
console.log(result.status, states.includes("done"), await result.outputs[0].files[0].text());

You should see:

done true APPLE
PEAR

Related tools and workflow files

The scripting API exposes the links the site draws between tools. relatedTools(idOrPath) returns the page's route, docs links, workflow op and up to six related pages, the same data as the Related strip. exportWorkflowHtml(definition, { name, usage }) returns the workflow as one self-contained .html file plus an import link, and parseWorkflowLink(linkOrHash) reads such a link back. The workflow travels in the URL fragment, which is never sent to a server. These three are on window.eeshgarg and the module (API 2.1.0); the browser SDK does not carry them yet. Runs in Node (tested).

const info = await eeshgarg.relatedTools("/more/qr");
console.log(info.title, info.related.length > 0, info.related.every((r) => r.route !== "/more/qr"));
const byId = await eeshgarg.relatedTools("feature:convert");
console.log(byId.route, byId.workflowOp);

You should see:

QR & Barcodes true true
/convert feature:convert
const { fileName, html, importUrl } = await eeshgarg.exportWorkflowHtml(recipe, { name: "Tidy lines" });
const back = await eeshgarg.parseWorkflowLink(importUrl);
console.log(fileName, html.startsWith("<!doctype html>"), back.ok, back.name, back.graph.nodes.length === recipe.nodes.length);

You should see:

Tidy-lines.workflow.html true true Tidy lines true
// Browser-only: validateWorkflow never rejects for a bad workflow; it returns { ok, errors }.
import { createClient } from "https://eeshgarg.com/sdk/v1/eeshgarg-sdk.mjs";

const client = await createClient();
try {
  const check = await client.validateWorkflow(recipe);
  if (!check.ok) throw new Error(check.errors.join("; "));
  const result = await client.runWorkflow(recipe, [input], { onNode: (n) => console.log(n.id, n.status) });
  console.log(result.status, result.outputs.map((o) => o.files.length));
} finally {
  client.dispose();
}

The command-line equivalent

The CLI runs the same operations headlessly from a source checkout, for scripts and batch jobs. Each line below is the equivalent of a call above (the tests parse each one with the CLI's own parser). Full flags and exit codes are in CLI and scripting.

Each call and its command-line form
In codeOn the command line
listTools()npm run eeshgarg -- list --json
describe(id)npm run eeshgarg -- describe feature:text-transform
runTool(id, files, options)npm run eeshgarg -- run feature:text-transform hello.txt --opt op=upper --out out
A loop over filesnpm run eeshgarg -- run rotate a.pdf b.pdf --opt angle=90 --out out --json
runWorkflow(definition, files)npm run eeshgarg -- workflow docs/examples/text-cleanup.workflow.json fruit.txt --out out

Limits

The SDK takes at most 25 files, 64 MiB per file and 128 MiB per request. A workflow is at most 256 KB, 100 blocks and 200 connections. Some operations (media, OCR, image and PDF rendering) need a canvas, workers or WebAssembly, so they run in a browser and not in a bare Node process. See API reference and Browser compatibility.

Privacy: everything is local

Files you pass in are read in the browser and processed on the device that runs the page. They are not uploaded. The SDK frame stores no settings and answers only the page that created it. Details are in Privacy and security.

Recipes and where to go next