WebAssembly Integration
This document explains how to set up and use the Swiss Ephemeris WebAssembly integration in the Kaabalah library.
Overview
The Swiss Ephemeris is a high-precision astronomical calculation library written in C that is widely used for astrological calculations. To use it in web browsers via our JavaScript/TypeScript library, we’ve created a WebAssembly wrapper.
Prerequisites
To compile the Swiss Ephemeris to WebAssembly, you will need:
- Emscripten SDK (emsdk): The compiler toolchain for WebAssembly
- Installation instructions: https://emscripten.org/docs/getting_started/downloads.html
- Swiss Ephemeris Source Code: The C code that will be compiled
- Download from: https://www.astro.com/swisseph/swesrc.htm
- Ephemeris Files: Data files used by Swiss Ephemeris for calculations
- Download from: https://www.astro.com/ftp/swisseph/ephe/
Compilation Process
-
Download the Swiss Ephemeris source code and extract it to the
wasm/swissephdirectory -
Install Emscripten SDK by following their installation instructions
-
Compile the code using the provided script:
Terminal window cd wasm/scriptschmod +x compile.sh./compile.sh -
Verify the output files:
wasm/build/swisseph.js: JavaScript glue codewasm/build/swisseph.wasm: WebAssembly binary
Integration with the Library
The Swiss Ephemeris WebAssembly integration is used by the astrology module to perform accurate astronomical calculations.
Module Structure
src/astrology/swisseph.ts: Wrapper that interfaces with the WebAssembly modulesrc/astrology/index.ts: Main astrology functionality that uses the wrapper
Dynamic Loading
The WebAssembly module is loaded dynamically at runtime, making it compatible with both Node.js and browser environments. This approach ensures that:
- The heavy WebAssembly code is only loaded when needed
- The same code works in both Node.js and browsers
- The library can gracefully fall back if WebAssembly is not available
Usage in Browser
To use the Swiss Ephemeris in a browser environment:
- Copy the WebAssembly files to a location accessible by your web application:
swisseph.jsswisseph.wasm
- Handle cross-origin issues: Ensure your web server is configured to serve WebAssembly files with the correct MIME type (
application/wasm) - Provide the ephemeris files that Swiss Ephemeris needs for calculations:
- Create a directory accessible by your web application
- Place the ephemeris files (*.se1) in that directory
- Specify the path when initializing the Swiss Ephemeris module
Usage in Node.js
For Node.js environments:
- Include the built WebAssembly files in your published package:
- Add them to the
filesarray inpackage.json - Make sure they’re copied to the
distdirectory during build
- Add them to the
- Place ephemeris files in a known location:
- For example, in a subdirectory named
ephewithin your package - Set the ephemeris path when initializing the module
- For example, in a subdirectory named
Fallback Mechanism
The astrology module includes a fallback mechanism to handle situations where the WebAssembly module might not be available:
- If compilation fails or the WebAssembly files are missing, the module gracefully degrades
- Placeholder values are returned instead of throwing errors
- A warning is logged to the console
Example Implementation
import { getBirthChart } from 'kaabalah/astrology';
// Calculate a birth chartconst chart = await getBirthChart({ date: new Date('1990-06-15T12:30:00'), latitude: 40.7128, longitude: -74.0060, timezone: -4});
console.log(`Ascendant: ${chart.ascendant}`);console.log(`Sun position: ${chart.planets.sun.longitude}`);Troubleshooting
Common Issues
- “WebAssembly module not found”:
- Ensure the .wasm and .js files are properly included in your distribution
- Check the paths in your code
- “Cannot find ephemeris files”:
- Make sure the ephemeris files are available at the specified path
- Check file permissions
- “Calculation error”:
- Verify the input parameters (date, location) are valid
- Ensure you have the correct ephemeris files for the date range
Debugging Tools
- Use browser developer tools to check for WASM-related errors in the console
- Enable the
ASSERTIONS=1flag during compilation for more verbose error messages