Skip to content
das-developersPublic

About

das client utilities for browsers

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

38 Commits

Folders and files

Repository files navigation

das3js

Das servers typically provide data relevant to space plasma and magnetospheric physics research. To retrieve data, an HTTP GET request is sent to a das server by a client program and a self-describing stream of data values covering the requested time range, at the requested time resolution, is provided in the response body. This project reads and processes those streams in web browsers.

A new wrapper around a fast core. The JavaScript layer is new; the parser underneath is not. Das3js runs on das2C and provides a three-file ES module (das3js.mjs, das2c.mjs, das2c.wasm) that fetches a das stream, parses it as it arrives and hands back decoded values, int64 and all. Modules are also provided to read data source interface and protocol definitions. These gather user input and submit server queries. The core module does no plotting, though test pages exercise a simple renderer which paints spectrogram data as they arrive. Currently, the renderer is mostly for performance tests.

Layout:

  • src/ - the ES modules: das3js.mjs, the core, and two custom elements, <das-catalog-tree> and <das-source-form>
  • glue/ - a small flat C interface over das2C, the wasm export surface
  • test/ - the glue test (wasm under node) and the browser tests (headless Firefox via a node runner)
  • deps/ - das2C, pinned as a git submodule and compiled in; it fetches and builds expat and FFTW for wasm32 itself
  • notes/ - build recipe, measurements

It's fast. A full day of Juno Waves burst waveforms, 505 MB of samples, parses in Firefox in under 300 ms once the bytes are in memory, about 1.7 GB/s, and the first record is usable half a second after the request goes out while the server spends the next 36 s producing the rest. There's no JSON on the wire, no number conversion in JavaScript: the values arrive in their native binary encoding and the browser reads them in place. A screenshot of a live run is below. Method and more streams are in notes/performance.md.

notes/hammer_perf.png

Using das3js

Install it from npm, where it is published by the DasDevelopers organization:

npm install @dasdevelopers/das3js

Then read a stream:

import {load} from '@dasdevelopers/das3js';

const das3 = await load();                  // once per page or worker
const strm = await das3.fetchStream(url);   // parses as the bytes arrive

for(let i = 0; i < strm.length; ++i){
	const ds = strm.dataset(i);
	for(const dim of ds.data){
		const v = dim.var();                  // the dimension's center values
		console.log(ds.name, dim.name, v.units, v.values());
	}
}
strm.free();                                // datasets live in wasm memory

values() returns typed arrays (Float32Array, BigInt64Array, ...) that view the decoded data directly. Das3js runs in browsers and web workers; Node is not supported yet.

Two optional custom elements build on the core, for pages that let people find and request data. Each import defines its tag:

import '@dasdevelopers/das3js/catalog-tree';    // <das-catalog-tree>: browse a das catalog
import '@dasdevelopers/das3js/source-form';     // <das-source-form>: a request form for one source

const tree = document.querySelector('das-catalog-tree');
const form = document.querySelector('das-source-form');
tree.catalog = das3.newCatalog();
tree.addEventListener('das-select', ev => {
	const node = ev.detail.node;
	if(!node?.isSource) return;
	const old = form.source;
	form.source = node.source();       // the page owns sources,
	old?.free();                       // so it frees the one replaced
});
form.addEventListener('das-request', ev => fetchOrDownload(ev.detail.url));

They draw in a shadow root and take their colors from --das-* CSS custom properties; the comment at the top of each file lists those, the attributes and the events. The form never fetches; it hands the page a checked request URL.

The package is three files that must stay together: das3js.mjs, its loader das2c.mjs, and the compiled library das2c.wasm, about 235 KB over the wire. The loader finds the .wasm beside itself, and the common setups below handle that with no configuration. If yours moves the .wasm somewhere else, tell load() where: load({locateFile: name => '/my/assets/' + name}).

Vite

No configuration needed; tested with Vite 8, both vite build and the dev server.

If the dev server fails to load the .wasm (a failed request for das2c.wasm, or a WebAssembly compile error in the browser console), your Vite's dependency pre-bundling moved das3js without fixing its path. You can exclude it from pre-bundling in vite.config.js:

export default {
	optimizeDeps: {exclude: ['@dasdevelopers/das3js']},
};

To ship the license notices with your app, turn on build.license; Vite then writes .vite/license.md, das3js's notices included:

export default {
	build: {license: true},
};

webpack

No configuration needed; tested with webpack 5, which emits das2c.wasm as an asset beside your bundle.

During a build, webpack warns that the .wasm exceeds its recommended asset size of 244 KiB. That limit is a general hint for JavaScript bundles; the library loads once and is compiled while it downloads. To silence the warning for it alone, in webpack.config.js:

export default {
	// ...
	performance: {assetFilter: name => !name.endsWith('.wasm')},
};

No bundler

A plain page can import das3js from a CDN (content delivery network) with an import map:

<script type="importmap">
{"imports": {"@dasdevelopers/das3js": "https://cdn.jsdelivr.net/npm/@dasdevelopers/das3js@0.2/build.wasm32/das3js.mjs"}}
</script>
<script type="module">
import {load} from '@dasdevelopers/das3js';
// ...
</script>

To host the files yourself, copy the three files from the package's build.wasm32/ directory together. The web server must send .wasm as application/wasm and .mjs as text/javascript; browsers refuse either one otherwise.

Checking a setup

make pack_test installs the package into a small project for each of the setups above and runs them in headless Firefox; see notes/build.md.

Building

Like the other das2C bindings (das2py, das2dlm), this project statically compiles in a selected das2C version, rather than depending on shared objects. The current pin is das2C v3.0.6, a git submodule.

You need GNU make, emscripten through emsdk (6.0.13, the version das2C's CI pins, which wants Python 3.10 or newer), network access for the first build, and Firefox for the browser tests. Then:

git clone --recurse-submodules https://github.com/das-developers/das3js.git
cd das3js
source /path/to/emsdk/emsdk_env.sh
make                  # the module in build.wasm32/, and the test programs
make test             # the C layer under Node
make headless_test    # the module in headless Firefox
make pack             # the npm tarball, build.wasm32/dasdevelopers-das3js-VERSION.tgz

The first make downloads expat and FFTW, checks them against pinned SHA-256 sums and builds them for wasm32, and emscripten fetches its zlib port. That takes several minutes; later builds take seconds. make help lists every target. notes/build.md has the details: installing emsdk, the Python workaround for older systems, the live and npm consumer tests, the local test server, and deploying the test pages.

History

Das was invented by Larry Granroth at the University of Iowa. He started das1 in 1996 as a plot generation service built on Perl CGI scripts and IDL. In 2002 he hired E. West and J. Faden to give das publication-quality interactive plots. The resulting das2 system was built in Java, and as browser support for Java web apps dwindled, it came to run mostly on the desktop. The das2 graphics engine lives on in das2java, which is actively maintained and forms the core of Autoplot. C. Piker developed das3 from 2016 on to handle the stream types das2 does not support, consulting Granroth on the design throughout. Das3 tools are the primary components supporting the TRACERS Science Operations Center (SOC). Das3js, part of the das3 family, brings das back to web browsers, where it all began.

License

Das3js's own code, the JavaScript included, is MIT licensed. The compiled das2c.wasm contains das2C and so is distributed under the LGPL 2.1. Bundling and minifying with Vite, webpack and the like is fine and does not put an application under either license; see LICENSE for what each file is under and the notices to ship.

About

das client utilities for browsers

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages