scoped npm package, running wasm locally

This commit is contained in:
Andy Pack 2025-02-15 00:26:37 +00:00
parent e13c5e3ac4
commit 1cefa6f756
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
18 changed files with 196 additions and 20 deletions

5
finlib-wasm/js-local/bootstrap.js vendored Normal file

@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>finlib</title>
</head>
<body>
<script src="./bootstrap.js"></script>
</body>
</html>

@ -0,0 +1,4 @@
import { ValueAtRisk } from "finlib";
console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.1));
console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.05));

@ -0,0 +1,23 @@
{
"name": "finlib-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.prod.js --env production",
"devbuild": "webpack --config webpack.dev.js",
"start": "webpack serve --config webpack.dev.js --progress"
},
"dependencies": {
"finlib": "file:../pkg"
},
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-merge": "^5.9.0"
}
}

@ -0,0 +1,22 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
experiments: {
asyncWebAssembly: true
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "index.html")
}
]
})
],
};

@ -0,0 +1,8 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
watch: true
});

@ -0,0 +1,7 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production',
devtool: 'source-map'
});

5
finlib-wasm/js-repo/bootstrap.js vendored Normal file

@ -0,0 +1,5 @@
// A dependency graph that contains any wasm must all be imported
// asynchronously. This `bootstrap.js` file does the single async import, so
// that no one else needs to worry about it again.
import("./index.js")
.catch(e => console.error("Error importing `index.js`:", e));

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>finlib</title>
</head>
<body>
<script src="./bootstrap.js"></script>
</body>
</html>

@ -0,0 +1,4 @@
import { ValueAtRisk } from "finlib";
console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.1));
console.log(ValueAtRisk.varcovar([1, 2, 3, 4], 0.05));

@ -0,0 +1,23 @@
{
"name": "finlib-test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.prod.js --env production",
"devbuild": "webpack --config webpack.dev.js",
"start": "webpack serve --config webpack.dev.js --progress"
},
"dependencies": {
"@sarsoo/finlib": "0.0.1"
},
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1",
"webpack-merge": "^5.9.0"
}
}

@ -0,0 +1,22 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require('path');
module.exports = {
entry: "./bootstrap.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "bootstrap.js",
},
experiments: {
asyncWebAssembly: true
},
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, "index.html")
}
]
})
],
};

@ -0,0 +1,8 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
watch: true
});

@ -0,0 +1,7 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production',
devtool: 'source-map'
});

@ -1,14 +0,0 @@
{
"name": "finlib",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"draught": "file:../pkg"
},
"author": "",
"license": "ISC",
"description": ""
}

@ -1 +1 @@
sed -i -e 's/"name": "finlib-wasm"/"name": "finlib"/g' pkg/package.json
sed -i -e 's/"name": "finlib-wasm"/"name": "@sarsoo\/finlib"/g' pkg/package.json

@ -1,11 +1,35 @@
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen]
pub fn compound(principal: f64, rate: f64, time: f64, n: f64) -> f64 {
finlib::interest::compound(principal, rate, time, n)
pub struct Interest { }
#[wasm_bindgen]
impl Interest {
pub fn compound(principal: f64, rate: f64, time: f64, n: f64) -> f64 {
finlib::interest::compound(principal, rate, time, n)
}
}
#[wasm_bindgen]
pub fn covariance(slice: Vec<f64>, slice_two: Vec<f64>) -> Option<f64> {
finlib::stats::covariance(&slice, &slice_two)
}
pub struct ValueAtRisk { }
#[wasm_bindgen]
impl ValueAtRisk {
pub fn historical(values: Vec<f64>, confidence: f64) -> f64 {
finlib::risk::var::historical::value_at_risk(&values, confidence)
}
pub fn varcovar(values: Vec<f64>, confidence: f64) -> f64 {
finlib::risk::var::varcovar::value_at_risk(&values, confidence)
}
}
#[wasm_bindgen]
pub struct Stats { }
#[wasm_bindgen]
impl Stats {
pub fn covariance(slice: Vec<f64>, slice_two: Vec<f64>) -> Option<f64> {
finlib::stats::covariance(&slice, &slice_two)
}
}