feat: testing on how to flatten tree structures
This commit is contained in:
35
packages/node-registry/src/lib/registry.ts
Normal file
35
packages/node-registry/src/lib/registry.ts
Normal file
@ -0,0 +1,35 @@
|
||||
export async function getNodeWrapper(id: `${string}/${string}/${string}`) {
|
||||
|
||||
let wrapperCode = await (await fetch(`/${id}/wrapper`)).text();
|
||||
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
|
||||
wasm = val;`);
|
||||
const wasmWrapper = await import(/*vite-ignore*/`data:text/javascript;base64,${btoa(wrapperCode)}`);
|
||||
|
||||
return wasmWrapper;
|
||||
}
|
||||
|
||||
export async function getNodeWasm(id: `${string}/${string}/${string}`): Promise<WebAssembly.Instance> {
|
||||
|
||||
const wasmResponse = await fetch(`/${id}/wasm`);
|
||||
|
||||
const wasmWrapper = await getNodeWrapper(id);
|
||||
|
||||
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
|
||||
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper });
|
||||
wasmWrapper.__wbg_set_wasm(instance.exports);
|
||||
|
||||
return wasmWrapper;
|
||||
}
|
||||
|
||||
|
||||
export async function getNode(id: `${string}/${string}/${string}`) {
|
||||
|
||||
const wrapper = await getNodeWasm(id);
|
||||
|
||||
const node_id = wrapper.get_id();
|
||||
const outputs = wrapper.get_outputs();
|
||||
const inputTypes = JSON.parse(wrapper.get_input_types());
|
||||
|
||||
return { id: node_id, outputs, inputs: inputTypes }
|
||||
|
||||
}
|
@ -2,13 +2,18 @@ import { json } from "@sveltejs/kit";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const GET: RequestHandler = async function GET({ fetch, params }) {
|
||||
const d = await fetch(`/${params.user}/${params.collection}/${params.node}/wasm`);
|
||||
const wrapWasm = await import("./wrap-wasm");
|
||||
const module = new WebAssembly.Module(await d.arrayBuffer());
|
||||
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wrapWasm });
|
||||
wrapWasm.__wbg_set_wasm(instance.exports);
|
||||
const id = wrapWasm.get_id();
|
||||
const outputs = wrapWasm.get_outputs();
|
||||
const inputTypes = JSON.parse(wrapWasm.get_input_types());
|
||||
const wasmResponse = await fetch(`/${params.user}/${params.collection}/${params.node}/wasm`);
|
||||
|
||||
let wrapperCode = await (await fetch(`/${params.user}/${params.collection}/${params.node}/wrapper`)).text();
|
||||
wrapperCode = wrapperCode.replace("wasm = val;", `if(wasm) return;
|
||||
wasm = val;`);
|
||||
const wasmWrapper = await import(`data:text/javascript;base64,${btoa(wrapperCode)}`);
|
||||
|
||||
const module = new WebAssembly.Module(await wasmResponse.arrayBuffer());
|
||||
const instance = new WebAssembly.Instance(module, { ["./index_bg.js"]: wasmWrapper });
|
||||
wasmWrapper.__wbg_set_wasm(instance.exports);
|
||||
const id = wasmWrapper.get_id();
|
||||
const outputs = wasmWrapper.get_outputs();
|
||||
const inputTypes = JSON.parse(wasmWrapper.get_input_types());
|
||||
return json({ id, outputs, inputs: inputTypes, });
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
<script lang="ts">
|
||||
import type { PageData } from './$types';
|
||||
|
||||
import { getNode, getNodeWasm } from '$lib/registry';
|
||||
import { onMount } from 'svelte';
|
||||
|
||||
export let data: PageData;
|
||||
const nodeId = `${data.params.user}/${data.params.collection}/${data.params.node}` as const;
|
||||
|
||||
let node;
|
||||
let wasm;
|
||||
|
||||
onMount(async () => {
|
||||
wasm = await getNodeWasm(nodeId);
|
||||
window['wasm'] = wasm;
|
||||
node = await getNode(nodeId);
|
||||
});
|
||||
</script>
|
||||
|
||||
<h1>{data.params.user}/{data.params.collection}/{data.params.node}</h1>
|
||||
|
||||
<h3>Node Definition</h3>
|
||||
{#if !node}
|
||||
<p>Loading Node</p>
|
||||
{:else}
|
||||
<pre>{JSON.stringify(node, null, 2)}</pre>
|
||||
{/if}
|
@ -0,0 +1,7 @@
|
||||
import type { PageLoad } from "./$types";
|
||||
|
||||
export const load: PageLoad = ({ params }) => {
|
||||
return {
|
||||
params
|
||||
}
|
||||
};
|
@ -1,254 +0,0 @@
|
||||
let wasm;
|
||||
export function __wbg_set_wasm(val) {
|
||||
wasm = val;
|
||||
}
|
||||
|
||||
|
||||
const heap = new Array(128).fill(undefined);
|
||||
|
||||
heap.push(undefined, null, true, false);
|
||||
|
||||
function getObject(idx) { return heap[idx]; }
|
||||
|
||||
let heap_next = heap.length;
|
||||
|
||||
function dropObject(idx) {
|
||||
if (idx < 132) return;
|
||||
heap[idx] = heap_next;
|
||||
heap_next = idx;
|
||||
}
|
||||
|
||||
function takeObject(idx) {
|
||||
const ret = getObject(idx);
|
||||
dropObject(idx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
let WASM_VECTOR_LEN = 0;
|
||||
|
||||
let cachedUint8Memory0 = null;
|
||||
|
||||
function getUint8Memory0() {
|
||||
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
|
||||
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachedUint8Memory0;
|
||||
}
|
||||
|
||||
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
|
||||
|
||||
let cachedTextEncoder = new lTextEncoder('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
|
||||
};
|
||||
});
|
||||
|
||||
function passStringToWasm0(arg, malloc, realloc) {
|
||||
|
||||
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 isLikeNone(x) {
|
||||
return x === undefined || x === null;
|
||||
}
|
||||
|
||||
let cachedInt32Memory0 = null;
|
||||
|
||||
function getInt32Memory0() {
|
||||
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
|
||||
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachedInt32Memory0;
|
||||
}
|
||||
|
||||
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
||||
|
||||
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
||||
|
||||
cachedTextDecoder.decode();
|
||||
|
||||
function getStringFromWasm0(ptr, len) {
|
||||
ptr = ptr >>> 0;
|
||||
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
||||
}
|
||||
|
||||
function addHeapObject(obj) {
|
||||
if (heap_next === heap.length) heap.push(heap.length + 1);
|
||||
const idx = heap_next;
|
||||
heap_next = heap[idx];
|
||||
|
||||
heap[idx] = obj;
|
||||
return idx;
|
||||
}
|
||||
|
||||
let cachedUint32Memory0 = null;
|
||||
|
||||
function getUint32Memory0() {
|
||||
if (cachedUint32Memory0 === null || cachedUint32Memory0.byteLength === 0) {
|
||||
cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer);
|
||||
}
|
||||
return cachedUint32Memory0;
|
||||
}
|
||||
|
||||
function getArrayJsValueFromWasm0(ptr, len) {
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* @returns {(string)[]}
|
||||
*/
|
||||
export function get_outputs() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
export function get_id() {
|
||||
let deferred1_0;
|
||||
let deferred1_1;
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
wasm.get_id(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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
export function get_input_types() {
|
||||
let deferred1_0;
|
||||
let deferred1_1;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} var_min
|
||||
* @param {any} var_max
|
||||
* @param {any} var_seed
|
||||
* @returns {string}
|
||||
*/
|
||||
export function execute(var_min, var_max, var_seed) {
|
||||
let deferred1_0;
|
||||
let deferred1_1;
|
||||
try {
|
||||
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
||||
wasm.execute(retptr, addHeapObject(var_min), addHeapObject(var_max), addHeapObject(var_seed));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
export function __wbindgen_is_undefined(arg0) {
|
||||
const ret = getObject(arg0) === undefined;
|
||||
return ret;
|
||||
};
|
||||
|
||||
export function __wbindgen_is_null(arg0) {
|
||||
const ret = getObject(arg0) === null;
|
||||
return ret;
|
||||
};
|
||||
|
||||
export function __wbindgen_object_drop_ref(arg0) {
|
||||
takeObject(arg0);
|
||||
};
|
||||
|
||||
export function __wbindgen_string_get(arg0, arg1) {
|
||||
const obj = getObject(arg1);
|
||||
const ret = typeof (obj) === 'string' ? obj : undefined;
|
||||
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
||||
var len1 = WASM_VECTOR_LEN;
|
||||
getInt32Memory0()[arg0 / 4 + 1] = len1;
|
||||
getInt32Memory0()[arg0 / 4 + 0] = ptr1;
|
||||
};
|
||||
|
||||
export function __wbindgen_string_new(arg0, arg1) {
|
||||
const ret = getStringFromWasm0(arg0, arg1);
|
||||
return addHeapObject(ret);
|
||||
};
|
||||
|
||||
|
@ -0,0 +1,12 @@
|
||||
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`);
|
||||
|
||||
const file = await fs.readFile(filePath);
|
||||
|
||||
return new Response(file, { status: 200, headers: { "Content-Type": "text/javascript" } });
|
||||
}
|
@ -45,7 +45,24 @@ export type Socket = {
|
||||
|
||||
|
||||
export interface NodeRegistry {
|
||||
/**
|
||||
* Load the nodes with the given ids
|
||||
* @param nodeIds - The ids of the nodes to load
|
||||
* @returns A promise that resolves when the nodes are loaded
|
||||
* @throws An error if the nodes could not be loaded
|
||||
* @remarks This method should be called before calling getNode or getAllNodes
|
||||
*/
|
||||
load: (nodeIds: string[]) => Promise<void>;
|
||||
/**
|
||||
* Get a node by id
|
||||
* @param id - The id of the node to get
|
||||
* @returns The node with the given id, or undefined if no such node exists
|
||||
*/
|
||||
getNode: (id: string) => NodeType | undefined;
|
||||
/**
|
||||
* Get all nodes
|
||||
* @returns An array of all nodes
|
||||
*/
|
||||
getAllNodes: () => NodeType[];
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ type NodeInputSelect = {
|
||||
|
||||
type DefaultOptions = {
|
||||
internal?: boolean;
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export type NodeInput = (NodeInputBoolean | NodeInputFloat | NodeInputInteger | NodeInputSelect) & DefaultOptions;
|
||||
|
26
packages/types/parameters.ts
Normal file
26
packages/types/parameters.ts
Normal file
@ -0,0 +1,26 @@
|
||||
type RandomParameter = {
|
||||
type: "random";
|
||||
min: Parameter;
|
||||
max: Parameter;
|
||||
seed: number;
|
||||
}
|
||||
|
||||
|
||||
type MathParameter = {
|
||||
type: "math";
|
||||
op_type: number;
|
||||
a: Parameter;
|
||||
b: Parameter;
|
||||
}
|
||||
|
||||
|
||||
type NoiseParameter = {
|
||||
type: "noise";
|
||||
frequency: Parameter;
|
||||
amplitude: Parameter;
|
||||
seed: number;
|
||||
}
|
||||
|
||||
|
||||
type Parameter = number | RandomParameter | MathParameter | NoiseParameter;
|
||||
|
Reference in New Issue
Block a user