feat: improve hash functions

This commit is contained in:
max_richter 2024-04-16 13:30:14 +02:00
parent dec205b234
commit 3d3ea5b5f8
33 changed files with 1416 additions and 963 deletions

2
Cargo.lock generated
View File

@ -106,6 +106,7 @@ name = "output"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"macros",
"serde", "serde",
"serde-wasm-bindgen", "serde-wasm-bindgen",
"serde_json", "serde_json",
@ -138,6 +139,7 @@ name = "random"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"macros",
"serde", "serde",
"serde-wasm-bindgen", "serde-wasm-bindgen",
"utils", "utils",

View File

@ -14,34 +14,37 @@
"story:preview": "histoire preview" "story:preview": "histoire preview"
}, },
"dependencies": { "dependencies": {
"@nodes/graph-interface": "link:../packages/graph-interface",
"@nodes/ui": "link:../packages/ui", "@nodes/ui": "link:../packages/ui",
"@sveltejs/kit": "^2.5.0", "@nodes/utils": "link:../packages/utils",
"@sveltejs/kit": "^2.5.6",
"@tauri-apps/api": "2.0.0-beta.2", "@tauri-apps/api": "2.0.0-beta.2",
"@tauri-apps/plugin-shell": "^2.0.0-beta.0", "@tauri-apps/plugin-shell": "2.0.0-beta.2",
"@threlte/core": "^7.1.0", "@threlte/core": "^7.3.0",
"@threlte/extras": "^8.7.5", "@threlte/extras": "^8.11.2",
"@threlte/flex": "^1.0.1", "@threlte/flex": "^1.0.2",
"@types/three": "^0.159.0", "@types/three": "^0.163.0",
"three": "^0.159.0" "comlink": "^4.4.1",
"jsondiffpatch": "^0.6.0",
"three": "^0.163.0"
}, },
"devDependencies": { "devDependencies": {
"@histoire/plugin-svelte": "^0.17.9", "@histoire/plugin-svelte": "^0.17.17",
"@nodes/types": "link:../packages/types", "@nodes/types": "link:../packages/types",
"@sveltejs/adapter-static": "^3.0.1", "@sveltejs/adapter-static": "^3.0.1",
"@sveltejs/vite-plugin-svelte": "^3.0.1", "@sveltejs/vite-plugin-svelte": "^3.1.0",
"@tauri-apps/cli": "2.0.0-beta.3", "@tauri-apps/cli": "2.0.0-beta.3",
"@tsconfig/svelte": "^5.0.2", "@tsconfig/svelte": "^5.0.4",
"@zerodevx/svelte-json-view": "^1.0.9", "@zerodevx/svelte-json-view": "^1.0.9",
"histoire": "^0.17.9", "histoire": "^0.17.17",
"internal-ip": "^7.0.0", "internal-ip": "^8.0.0",
"svelte": "^4.2.8", "svelte": "^4.2.14",
"svelte-check": "^3.4.6", "svelte-check": "^3.6.9",
"tslib": "^2.6.0", "tslib": "^2.6.2",
"typescript": "^5.0.2", "typescript": "^5.4.5",
"vite": "^5.1.4", "vite": "^5.2.9",
"vite-plugin-glsl": "^1.2.1", "vite-plugin-comlink": "^4.0.3",
"vite-plugin-glsl": "^1.3.0",
"vite-plugin-wasm": "^3.3.0", "vite-plugin-wasm": "^3.3.0",
"vitest": "^1.2.0" "vitest": "^1.5.0"
} }
} }

View File

@ -1,11 +1,54 @@
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
import fastHash from './fastHash'; import { fastHashArray, fastHashString } from './fastHash';
test('Hashes dont clash', () => { test('fastHashString doesnt produce clashes', () => {
const hashA = fastHash('abcdef'); const hashA = fastHashString('abcdef');
const hashB = fastHash('abcde'); const hashB = fastHashString('abcdeg');
const hashC = fastHash('abcde'); const hashC = fastHashString('abcdeg');
expect(hashA).not.toEqual(hashB); expect(hashA).not.toEqual(hashB);
expect(hashB).toEqual(hashC); expect(hashB).toEqual(hashC);
}); });
test("fastHashArray doesnt product collisions", () => {
const a = new Int32Array(1000);
const hash_a = fastHashArray(a);
a[0] = 1;
const hash_b = fastHashArray(a);
expect(hash_a).not.toEqual(hash_b);
});
test('fastHashArray is fast(ish) < 20ms', () => {
const a = new Int32Array(10_000);
const t0 = performance.now();
fastHashArray(a);
const t1 = performance.now();
a[0] = 1;
fastHashArray(a);
const t2 = performance.now();
expect(t1 - t0).toBeLessThan(20);
expect(t2 - t1).toBeLessThan(20);
});
// test if the fastHashArray function is deterministic
test('fastHashArray is deterministic', () => {
const a = new Int32Array(1000);
a[42] = 69;
const b = new Int32Array(1000);
b[42] = 69;
const hashA = fastHashArray(a);
const hashB = fastHashArray(b);
expect(hashA).toEqual(hashB);
});

View File

@ -1,7 +1,95 @@
// https://github.com/6502/sha256/blob/main/sha256.js
function sha256(data?: string | Uint8Array) {
let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a,
h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19,
tsz = 0, bp = 0;
const k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
rrot = (x, n) => (x >>> n) | (x << (32 - n)),
w = new Uint32Array(64),
buf = new Uint8Array(64),
process = () => {
for (let j = 0, r = 0; j < 16; j++, r += 4) {
w[j] = (buf[r] << 24) | (buf[r + 1] << 16) | (buf[r + 2] << 8) | buf[r + 3];
}
for (let j = 16; j < 64; j++) {
let s0 = rrot(w[j - 15], 7) ^ rrot(w[j - 15], 18) ^ (w[j - 15] >>> 3);
let s1 = rrot(w[j - 2], 17) ^ rrot(w[j - 2], 19) ^ (w[j - 2] >>> 10);
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) | 0;
}
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
for (let j = 0; j < 64; j++) {
let S1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25),
ch = (e & f) ^ ((~e) & g),
t1 = (h + S1 + ch + k[j] + w[j]) | 0,
S0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22),
maj = (a & b) ^ (a & c) ^ (b & c),
t2 = (S0 + maj) | 0;
h = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
}
h0 = (h0 + a) | 0; h1 = (h1 + b) | 0; h2 = (h2 + c) | 0; h3 = (h3 + d) | 0;
h4 = (h4 + e) | 0; h5 = (h5 + f) | 0; h6 = (h6 + g) | 0; h7 = (h7 + h) | 0;
bp = 0;
},
add = data => {
if (typeof data === "string") {
data = typeof TextEncoder === "undefined" ? Buffer.from(data) : (new TextEncoder).encode(data);
}
for (let i = 0; i < data.length; i++) {
buf[bp++] = data[i];
if (bp === 64) process();
}
tsz += data.length;
},
digest = () => {
buf[bp++] = 0x80; if (bp == 64) process();
if (bp + 8 > 64) {
while (bp < 64) buf[bp++] = 0x00;
process();
}
while (bp < 58) buf[bp++] = 0x00;
// Max number of bytes is 35,184,372,088,831
let L = tsz * 8;
buf[bp++] = (L / 1099511627776.) & 255;
buf[bp++] = (L / 4294967296.) & 255;
buf[bp++] = L >>> 24;
buf[bp++] = (L >>> 16) & 255;
buf[bp++] = (L >>> 8) & 255;
buf[bp++] = L & 255;
process();
let reply = new Uint8Array(32);
reply[0] = h0 >>> 24; reply[1] = (h0 >>> 16) & 255; reply[2] = (h0 >>> 8) & 255; reply[3] = h0 & 255;
reply[4] = h1 >>> 24; reply[5] = (h1 >>> 16) & 255; reply[6] = (h1 >>> 8) & 255; reply[7] = h1 & 255;
reply[8] = h2 >>> 24; reply[9] = (h2 >>> 16) & 255; reply[10] = (h2 >>> 8) & 255; reply[11] = h2 & 255;
reply[12] = h3 >>> 24; reply[13] = (h3 >>> 16) & 255; reply[14] = (h3 >>> 8) & 255; reply[15] = h3 & 255;
reply[16] = h4 >>> 24; reply[17] = (h4 >>> 16) & 255; reply[18] = (h4 >>> 8) & 255; reply[19] = h4 & 255;
reply[20] = h5 >>> 24; reply[21] = (h5 >>> 16) & 255; reply[22] = (h5 >>> 8) & 255; reply[23] = h5 & 255;
reply[24] = h6 >>> 24; reply[25] = (h6 >>> 16) & 255; reply[26] = (h6 >>> 8) & 255; reply[27] = h6 & 255;
reply[28] = h7 >>> 24; reply[29] = (h7 >>> 16) & 255; reply[30] = (h7 >>> 8) & 255; reply[31] = h7 & 255;
let res = "";
reply.forEach(x => res += ("0" + x.toString(16)).slice(-2));
return res;
};
if (data) add(data);
return { add, digest };
}
export function fastHashArray(arr: Int32Array): string {
return sha256(new Uint8Array(arr.buffer)).digest();
}
// Shamelessly copied from // Shamelessly copied from
// https://stackoverflow.com/a/8831937 // https://stackoverflow.com/a/8831937
export function fastHashString(input: string) {
export default function (input: string) {
if (input.length === 0) return 0; if (input.length === 0) return 0;
let hash = 0; let hash = 0;
@ -12,3 +100,23 @@ export default function (input: string) {
return hash; return hash;
} }
export function fastHash(input: (string | Int32Array | number)[]) {
const s = sha256();
for (let i = 0; i < input.length; i++) {
const v = input[i]
if (typeof v === "string") {
s.add(v);
} else if (v instanceof Int32Array) {
s.add(new Uint8Array(v.buffer));
} else {
s.add(v.toString());
}
}
return s.digest()
}

View File

@ -1,4 +1,5 @@
import type { NodeRegistry, NodeType } from "@nodes/types"; import type { NodeRegistry, NodeType } from "@nodes/types";
import { createWasmWrapper } from "@nodes/utils";
import { createLogger } from "./helpers"; import { createLogger } from "./helpers";
@ -49,31 +50,20 @@ export class RemoteNodeRegistry implements NodeRegistry {
private async loadNode(id: string) { private async loadNode(id: string) {
const nodeUrl = `${this.url}/n/${id}`; const nodeUrl = `${this.url}/n/${id}`;
const response = await fetch(nodeUrl); const [response, wasmResponse] = await Promise.all([fetch(nodeUrl), fetch(`${nodeUrl}/wasm`)]);
const wasmResponse = await fetch(`${nodeUrl}/wasm`); if (!wasmResponse.ok || !response.ok) {
const wrapperReponse = await fetch(`${nodeUrl}/wrapper`);
if (!wrapperReponse.ok) {
this.status = "error"; this.status = "error";
throw new Error(`Failed to load node ${id}`); throw new Error(`Failed to load node ${id}`);
} }
let wrapperCode = await wrapperReponse.text(); // Setup Wasm wrapper
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return; const wrapper = createWasmWrapper();
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}#${id}`);
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer()); const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper }); const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wrapper });
wasmWrapper.__wbg_set_wasm(instance.exports); wrapper.setInstance(instance);
if (!response.ok) {
this.status = "error";
throw new Error(`Failed to load node ${id}`);
} else {
log.log("loaded node", id);
}
const node = await response.json(); const node = await response.json();
node.execute = wasmWrapper.execute; node.execute = wrapper.execute;
return node; return node;
} }
@ -85,6 +75,7 @@ wasm = val;`);
nodeIds.push("max/plantarium/output"); nodeIds.push("max/plantarium/output");
nodeIds.push("max/plantarium/array"); nodeIds.push("max/plantarium/array");
nodeIds.push("max/plantarium/sum"); nodeIds.push("max/plantarium/sum");
nodeIds.push("max/plantarium/stem");
const nodes = await Promise.all(nodeIds.map(id => this.loadNode(id))); const nodes = await Promise.all(nodeIds.map(id => this.loadNode(id)));

View File

@ -1,16 +1,9 @@
import type { Graph, NodeRegistry, NodeType, RuntimeExecutor } from "@nodes/types"; import type { Graph, NodeRegistry, NodeType, RuntimeExecutor } from "@nodes/types";
import { encodeFloat } from "./helpers/encode"; import { encodeFloat } from "./helpers/encode";
import { concat_encoded, encode } from "./helpers/flat_tree"; import { concat_encoded, encode } from "./helpers/flat_tree";
import fastHash from "./helpers/fastHash"; import { fastHash, fastHashString } from "./helpers/fastHash";
async function hashIntArray(arr: Int32Array): Promise<string> {
const hashBuffer = await crypto.subtle.digest('SHA-256', arr.buffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
export class MemoryRuntimeExecutor implements RuntimeExecutor { export class MemoryRuntimeExecutor implements RuntimeExecutor {
@ -162,18 +155,14 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
const a0 = performance.now(); const a0 = performance.now();
const node_inputs = Object.entries(inputs); const node_inputs = Object.entries(inputs);
const cacheKey = `${node.id}/${fastHash(node_inputs.map(([_, value]: [string, any]) => { const cacheKey = "123" || `${node.id}/${fastHash(node_inputs.map(([_, value]: [string, any]) => {
if (value instanceof Int32Array) { return value
return hashIntArray(value); }))}`;
}
console.log(value);
return `${value}`
}).join("/"))}`;
const a1 = performance.now(); const a1 = performance.now();
console.log(`${a1 - a0}ms hashed inputs: ${node.id} -> ${cacheKey}`); console.log(`${a1 - a0}ms hashed inputs: ${node.id} -> ${cacheKey}`);
if (this.cache[cacheKey] && this.cache[cacheKey].eol > Date.now()) { if (false && this.cache[cacheKey] && this.cache[cacheKey].eol > Date.now()) {
results[node.id] = this.cache[cacheKey].value; results[node.id] = this.cache[cacheKey].value;
console.log(`Using cached value`); console.log(`Using cached value`);
continue; continue;

View File

@ -11,7 +11,8 @@
"strict": true, "strict": true,
"moduleResolution": "bundler", "moduleResolution": "bundler",
"types": [ "types": [
"vite-plugin-glsl/ext" "vite-plugin-glsl/ext",
"vite-plugin-comlink/client"
] ]
} }
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias

View File

@ -2,9 +2,20 @@ import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import glsl from "vite-plugin-glsl"; import glsl from "vite-plugin-glsl";
import wasm from "vite-plugin-wasm"; import wasm from "vite-plugin-wasm";
import comlink from 'vite-plugin-comlink';
export default defineConfig({ export default defineConfig({
plugins: [sveltekit(), glsl(), wasm()], plugins: [
comlink(),
sveltekit(),
glsl(),
wasm()
],
worker: {
plugins: () => ([
comlink()
])
},
ssr: { ssr: {
noExternal: ['three'], noExternal: ['three'],
} }

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -17,9 +17,6 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
// let value = decode_float(value_encoded[0], value_encoded[1]); // let value = decode_float(value_encoded[0], value_encoded[1]);
let length = args[1]; let length = args[1];
// console::log_1(&format!("WASM(array_node): args {:?} ", args).into());
// console::log_1(&format!("WASM(array_node): value: {:?} length: {:?}", value, length).into());
// construct array of length // construct array of length
let mut res: Vec<i32> = Vec::with_capacity(length[0] as usize * 2 + 2); let mut res: Vec<i32> = Vec::with_capacity(length[0] as usize * 2 + 2);
res.push(0); res.push(0);

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -21,6 +21,7 @@ wasm-bindgen = "0.2.84"
utils = { version = "0.1.0", path = "../../../../packages/utils" } utils = { version = "0.1.0", path = "../../../../packages/utils" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
macros = { version = "0.1.0", path = "../../../../packages/macros" }
serde-wasm-bindgen = "0.4" serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.69", features = ["console"] } web-sys = { version = "0.3.69", features = ["console"] }

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -0,0 +1,7 @@
{
"input": {
"type": "float",
"value": 0.0,
"external": true
}
}

View File

@ -1,20 +1,9 @@
// use utils::decode_float; use macros::generate_input_types_file;
use utils::evaluate_args; use utils::evaluate_args;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
// use web_sys::console;
#[wasm_bindgen] generate_input_types_file!("src/inputs.json");
pub fn get_outputs() -> Vec<String> {
vec![]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
r#"{
"input": { "type": "float", "value": 0.0, "external": true }
}"#
.to_string()
}
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(args: &[i32]) -> Vec<i32> {
utils::set_panic_hook(); utils::set_panic_hook();

View File

@ -18,7 +18,7 @@ wasm-bindgen = "0.2.84"
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for # all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying. # code size when deploying.
utils = { version = "0.1.0", path = "../../../../packages/utils" } utils = { version = "0.1.0", path = "../../../../packages/utils" }
macros = { version = "0.1.0", path = "../../../../packages/macros" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4" serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -21,3 +21,9 @@ web-sys = { version = "0.3.69", features = ["console"] }
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "0.3.34" wasm-bindgen-test = "0.3.34"
[package.metadata.wasm-pack.profile.release.wasm-bindgen]
debug-js-glue = true
demangle-name-section = true
dwarf-debug-info = false
omit-default-module-path = true

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -1,27 +1,15 @@
{ {
"op_type": { "length": {
"label": "type",
"type": "select",
"labels": [
"add",
"subtract",
"multiply",
"divide"
],
"internal": true,
"value": 0
},
"a": {
"type": "float", "type": "float",
"value": 2 "value": 2
}, },
"b": { "thickness": {
"type": "float", "type": "float",
"value": 2 "value": 2
}, },
"clip": { "resolution": {
"type": "boolean", "type": "integer",
"value": 0, "value": 32,
"setting": "math.clipping" "setting": "resolution.stem"
} }
} }

View File

@ -1,18 +1,33 @@
use macros::generate_input_types_file; use macros::generate_input_types_file;
use utils::generate_outputs; use utils::{evaluate_args, generate_outputs, get_args};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::console;
generate_outputs!(["stem"]); generate_outputs!(["stem"]);
generate_input_types_file!("src/input.json"); generate_input_types_file!("src/input.json");
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> { pub fn execute(input: &[i32]) -> Vec<i32> {
let mut result = Vec::with_capacity(args.len() + 3); let args = get_args(input);
let length = evaluate_args(args[0]);
let thickness = evaluate_args(args[1]);
let resolution = evaluate_args(args[2]);
console::log_1(
&format!(
"length: {:?}, thickness: {:?}, resolution: {:?}",
length, thickness, resolution
)
.into(),
);
let mut result: Vec<i32> = Vec::with_capacity(args.len() + 3);
result.push(0); // encoding the [ bracket result.push(0); // encoding the [ bracket
result.push(args[1] + 1); result.push(2);
result.push(0); // adding the node-type, math: 0 result.push(0); // adding the node-type, math: 0
result.extend_from_slice(&args[2..]); result.extend_from_slice(&thickness);
result.push(1); result.push(1);
result.push(1); // closing bracket result.push(1); // closing bracket

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -1,5 +1,6 @@
{ {
"scripts": { "scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features" "build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
} }
} }

View File

@ -1,6 +1,7 @@
{ {
"scripts": { "scripts": {
"build:nodes": "pnpm -r --filter './nodes/**' build", "build:nodes": "pnpm -r --filter './nodes/**' build",
"dev:nodes": "pnpm -r --filter './nodes/**' dev",
"dev": "pnpm -r --filter 'app' --filter './packages/node-registry' dev" "dev": "pnpm -r --filter 'app' --filter './packages/node-registry' dev"
} }
} }

View File

@ -1,39 +1,40 @@
{ {
"name": "node-registry", "name": "node-registry",
"version": "0.0.1", "version": "0.0.1",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "vitest", "test": "vitest",
"lint": "prettier --check . && eslint .", "lint": "prettier --check . && eslint .",
"format": "prettier --write ." "format": "prettier --write ."
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/adapter-auto": "^3.2.0",
"@sveltejs/kit": "^2.0.0", "@sveltejs/kit": "^2.5.6",
"@sveltejs/vite-plugin-svelte": "^3.0.0", "@sveltejs/vite-plugin-svelte": "^3.1.0",
"@types/eslint": "^8.56.0", "@types/eslint": "^8.56.9",
"@typescript-eslint/eslint-plugin": "^7.0.0", "@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.0.0", "@typescript-eslint/parser": "^7.7.0",
"eslint": "^8.56.0", "eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0", "eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.35.1", "eslint-plugin-svelte": "^2.37.0",
"prettier": "^3.1.1", "prettier": "^3.2.5",
"prettier-plugin-svelte": "^3.1.2", "prettier-plugin-svelte": "^3.2.3",
"svelte": "^4.2.7", "svelte": "^4.2.14",
"svelte-check": "^3.6.0", "svelte-check": "^3.6.9",
"tslib": "^2.4.1", "tslib": "^2.6.2",
"typescript": "^5.0.0", "typescript": "^5.4.5",
"vite": "^5.0.3", "vite": "^5.2.9",
"vite-plugin-wasm": "^3.3.0", "vite-plugin-wasm": "^3.3.0",
"vitest": "^1.2.0" "vitest": "^1.5.0"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"math": "link:../../nodes/math/pkg" "@nodes/utils": "link:../utils",
} "utils": "link:../utils"
}
} }

View File

@ -1,19 +1,8 @@
export async function getNodeWrapper(id: `${string}/${string}/${string}`) { import { createWasmWrapper } from "@nodes/utils"
const wrapperReponse = await fetch(`/n/${id}/wrapper`);
if (!wrapperReponse.ok) {
throw new Error(`Failed to load node ${id}`);
}
let wrapperCode = await wrapperReponse.text();
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
wasm = val;`);
const wasmWrapper = await import(/*@vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}#${id}${Math.random().toString().slice(2)}`);
return wasmWrapper; export async function getNodeWasm(id: `${string}/${string}/${string}`) {
}
export async function getNodeWasm(id: `${string}/${string}/${string}`): Promise<WebAssembly.Instance> {
const wasmResponse = await fetch(`/n/${id}/wasm`); const wasmResponse = await fetch(`/n/${id}/wasm`);
@ -21,13 +10,12 @@ export async function getNodeWasm(id: `${string}/${string}/${string}`): Promise<
throw new Error(`Failed to load node ${id}`); throw new Error(`Failed to load node ${id}`);
} }
const wasmWrapper = await getNodeWrapper(id); const wrapper = createWasmWrapper();
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer()); const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper }); const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wrapper });
wasmWrapper.__wbg_set_wasm(instance.exports); wrapper.setInstance(instance)
return wasmWrapper; return wrapper;
} }
@ -35,8 +23,8 @@ export async function getNode(id: `${string}/${string}/${string}`) {
const wrapper = await getNodeWasm(id); const wrapper = await getNodeWasm(id);
const outputs = wrapper.get_outputs(); const outputs = wrapper?.get_outputs?.() || [];
const rawInputs = wrapper.get_input_types(); const rawInputs = wrapper.get_inputs();
try { try {
const inputTypes = JSON.parse(rawInputs); const inputTypes = JSON.parse(rawInputs);
return { id, outputs, inputs: inputTypes } return { id, outputs, inputs: inputTypes }

View File

@ -1,19 +0,0 @@
import type { RequestHandler } from "./$types";
import fs from "fs/promises";
import path from "path";
export const GET: RequestHandler = async function GET({ params }) {
const filePath = path.resolve(`../../nodes/${params.user}/${params.collection}/${params.node}/pkg/index_bg.js`);
try {
await fs.access(filePath);
} catch (e) {
console.log("Not Found", filePath);
return new Response("Not found", { status: 404 });
}
const file = await fs.readFile(filePath);
return new Response(file, { status: 200, headers: { "Content-Type": "text/javascript" } });
}

View File

@ -27,22 +27,22 @@
"svelte": "^4.0.0" "svelte": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0", "@sveltejs/adapter-auto": "^3.2.0",
"@sveltejs/kit": "^2.0.0", "@sveltejs/kit": "^2.5.6",
"@sveltejs/package": "^2.0.0", "@sveltejs/package": "^2.3.1",
"@sveltejs/vite-plugin-svelte": "^3.0.0", "@sveltejs/vite-plugin-svelte": "^3.1.0",
"@types/eslint": "^8.56.0", "@types/eslint": "^8.56.9",
"@typescript-eslint/eslint-plugin": "^7.0.0", "@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.0.0", "@typescript-eslint/parser": "^7.7.0",
"eslint": "^8.56.0", "eslint": "^9.0.0",
"eslint-plugin-svelte": "^2.35.1", "eslint-plugin-svelte": "^2.37.0",
"publint": "^0.1.9", "publint": "^0.2.7",
"svelte": "^4.2.7", "svelte": "^4.2.14",
"svelte-check": "^3.6.0", "svelte-check": "^3.6.9",
"tslib": "^2.4.1", "tslib": "^2.6.2",
"typescript": "^5.0.0", "typescript": "^5.4.5",
"vite": "^5.0.11", "vite": "^5.2.9",
"vitest": "^1.2.0" "vitest": "^1.5.0"
}, },
"svelte": "./dist/index.js", "svelte": "./dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",

View File

@ -2,7 +2,7 @@
"name": "@nodes/utils", "name": "@nodes/utils",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "src/index.ts",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View File

@ -0,0 +1 @@
export * from "./wasm-wrapper";

View File

@ -0,0 +1,260 @@
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
const cachedTextEncoder = new TextEncoder('utf-8');
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});
export function createWasmWrapper() {
let wasm: any;
let cachedUint8Memory0: Uint8Array | null = null;
let cachedInt32Memory0: Int32Array | null = null;
let cachedUint32Memory0: Uint32Array | null = null;
const heap = new Array(128).fill(undefined);
heap.push(undefined, null, true, false);
let heap_next = heap.length;
function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
function getUint32Memory0() {
if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) {
cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer);
}
return cachedUint32Memory0;
}
function getStringFromWasm0(ptr: number, len: number) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
function getObject(idx: number) { return heap[idx]; }
function addHeapObject(obj: any) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];
heap[idx] = obj;
return idx;
}
let WASM_VECTOR_LEN = 0;
function passArray32ToWasm0(arg: ArrayLike<number>, malloc: (arg0: number, arg1: number) => number) {
const ptr = malloc(arg.length * 4, 4) >>> 0;
getUint32Memory0().set(arg, ptr / 4);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
function getArrayI32FromWasm0(ptr: number, len: number) {
ptr = ptr >>> 0;
return getInt32Memory0().subarray(ptr / 4, ptr / 4 + len);
}
function dropObject(idx: number) {
if (idx < 132) return;
heap[idx] = heap_next;
heap_next = idx;
}
function takeObject(idx: number) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
function getArrayJsValueFromWasm0(ptr: number, len: number) {
ptr = ptr >>> 0;
const mem = getUint32Memory0();
const slice = mem.subarray(ptr / 4, ptr / 4 + len);
const result = [];
for (let i = 0; i < slice.length; i++) {
result.push(takeObject(slice[i]));
}
return result;
}
function __wbindgen_string_new(arg0: number, arg1: number) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
};
function get_outputs() {
if (wasm === undefined || !wasm.get_outputs) {
return [];
}
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.get_outputs(retptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayJsValueFromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4, 4);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
// Additional methods and their internal helpers can also be refactored in a similar manner.
function get_inputs() {
let deferred1_0: number;
let deferred1_1: number;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.get_input_types(retptr);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
deferred1_0 = r0;
deferred1_1 = r1;
return getStringFromWasm0(r0, r1);
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
}
}
function execute(args) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray32ToWasm0(args, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.execute(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v2 = getArrayI32FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4, 4);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}
function passStringToWasm0(arg: string, malloc: (arg0: any, arg1: number) => number, realloc: ((arg0: number, arg1: any, arg2: number, arg3: number) => number) | undefined) {
if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}
let len = arg.length;
let ptr = malloc(len, 1) >>> 0;
const mem = getUint8Memory0();
let offset = 0;
for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}
if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}
WASM_VECTOR_LEN = offset;
return ptr;
}
function __wbg_new_abda76e883ba8a5f() {
const ret = new Error();
return addHeapObject(ret);
};
function __wbg_stack_658279fe44541cf6(arg0, arg1) {
const ret = getObject(arg1).stack;
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len1 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len1;
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
};
function __wbg_error_f851667af71bcfc6(arg0, arg1) {
let deferred0_0;
let deferred0_1;
try {
deferred0_0 = arg0;
deferred0_1 = arg1;
console.error(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
}
};
function __wbindgen_object_drop_ref(arg0) {
takeObject(arg0);
};
function __wbg_log_5bb5f88f245d7762(arg0) {
console.log(getObject(arg0));
};
function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
return {
setInstance(instance: WebAssembly.Instance) {
wasm = instance.exports;
},
// Expose other methods that interact with the wasm instance
execute,
get_outputs,
get_inputs,
__wbindgen_string_new,
__wbindgen_object_drop_ref,
__wbg_new_abda76e883ba8a5f,
__wbg_error_f851667af71bcfc6,
__wbg_stack_658279fe44541cf6,
__wbg_log_5bb5f88f245d7762,
__wbindgen_throw,
};
}

File diff suppressed because it is too large Load Diff