diff --git a/typescript/package-lock.json b/typescript/package-lock.json index 95b79af58..9b10742c6 100644 --- a/typescript/package-lock.json +++ b/typescript/package-lock.json @@ -2144,6 +2144,10 @@ "resolved": "packages/common-builder", "link": true }, + "node_modules/@commontools/common-engine": { + "resolved": "packages/common-engine", + "link": true + }, "node_modules/@commontools/common-frp": { "resolved": "packages/common-frp", "link": true @@ -7517,6 +7521,10 @@ "version": "3.1.3", "license": "MIT" }, + "node_modules/ct-engine": { + "resolved": "packages/common-engine", + "link": true + }, "node_modules/cytoscape": { "version": "3.30.4", "resolved": "https://registry.npmjs.org/cytoscape/-/cytoscape-3.30.4.tgz", @@ -19202,6 +19210,10 @@ "wireit": "^0.14.4" } }, + "packages/common-engine": { + "name": "ct-engine", + "version": "0.1.0" + }, "packages/common-frp": { "name": "@commontools/common-frp", "version": "0.0.1", @@ -19361,6 +19373,7 @@ "license": "UNLICENSED", "dependencies": { "@commontools/common-builder": "^0.0.1", + "@commontools/common-engine": "file:../common-engine", "@commontools/common-html": "^0.0.1", "@commontools/common-runner": "^0.0.1", "@commontools/llm-client": "^0.0.1", diff --git a/typescript/packages/common-engine/README.md b/typescript/packages/common-engine/README.md new file mode 100644 index 000000000..c6c407c3d --- /dev/null +++ b/typescript/packages/common-engine/README.md @@ -0,0 +1,20 @@ +# ct-engine + +JavaScript module implementation of [ct-engine]. + +## Using + +See `example.html` on usage. + +## Building + +Only this documentation, example, and tests can be found in the repository. All of the code is generated from compiling [ct-engine] via [wasm-pack]. + +Perform the build via nix: + +```sh +nix build .#engine-web +``` + +[wasm-pack]: https://rustwasm.github.io +[ct-engine]: ./rust/ct-engine diff --git a/typescript/packages/common-engine/ct_engine.d.ts b/typescript/packages/common-engine/ct_engine.d.ts new file mode 100644 index 000000000..afe855dae --- /dev/null +++ b/typescript/packages/common-engine/ct_engine.d.ts @@ -0,0 +1,72 @@ +/* tslint:disable */ +/* eslint-disable */ +/** + * The [`CtStore`] provides direct access to the underlying web store. + */ +export class CTStore { + free(): void; + /** + * Create a new [`CtStore`]. + */ + constructor(db_name: string, store_name: string, hash?: Uint8Array); + /** + * Returns the root hash of the storage, if it contains data. + */ + hash(): Uint8Array | undefined; + /** + * Sets `key` to `value`. + */ + set(key: Uint8Array, value: Uint8Array): Promise; + /** + * Retrieves value with `key`. + */ + get(key: Uint8Array): Promise; + /** + * Calls `callback` with `key` and `value` arguments + * for each entry within `start` and `end` range. + */ + getRange(start: Uint8Array, end: Uint8Array, start_inclusive: boolean, end_inclusive: boolean, callback: Function): Promise; +} + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly __wbg_ctstore_free: (a: number, b: number) => void; + readonly ctstore_new: (a: number, b: number, c: number, d: number, e: number, f: number) => number; + readonly ctstore_hash: (a: number, b: number) => void; + readonly ctstore_set: (a: number, b: number, c: number, d: number, e: number) => number; + readonly ctstore_get: (a: number, b: number, c: number) => number; + readonly ctstore_getRange: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => number; + readonly __wbindgen_export_0: (a: number) => void; + readonly __wbindgen_export_1: (a: number, b: number, c: number) => void; + readonly __wbindgen_export_2: (a: number, b: number) => number; + readonly __wbindgen_export_3: (a: number, b: number, c: number, d: number) => number; + readonly __wbindgen_export_4: WebAssembly.Table; + readonly __wbindgen_add_to_stack_pointer: (a: number) => number; + readonly __wbindgen_export_5: (a: number, b: number, c: number) => void; + readonly __wbindgen_export_6: (a: number, b: number, c: number) => void; + readonly __wbindgen_export_7: (a: number, b: number, c: number) => void; + readonly __wbindgen_export_8: (a: number, b: number, c: number, d: number) => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ +export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/typescript/packages/common-engine/ct_engine.js b/typescript/packages/common-engine/ct_engine.js new file mode 100644 index 000000000..c6f3bfe66 --- /dev/null +++ b/typescript/packages/common-engine/ct_engine.js @@ -0,0 +1,927 @@ +let wasm; + +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 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; +} + +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + wasm.__wbindgen_export_0(addHeapObject(e)); + } +} + +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); + +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +function isLikeNone(x) { + return x === undefined || x === null; +} + +let WASM_VECTOR_LEN = 0; + +const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); + +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; + getUint8ArrayMemory0().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 = getUint8ArrayMemory0(); + + 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 = getUint8ArrayMemory0().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; +} + +let cachedDataViewMemory0 = null; + +function getDataViewMemory0() { + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); + } + return cachedDataViewMemory0; +} + +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; +} + +const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(state => { + wasm.__wbindgen_export_4.get(state.dtor)(state.a, state.b) +}); + +function makeMutClosure(arg0, arg1, dtor, f) { + const state = { a: arg0, b: arg1, cnt: 1, dtor }; + const real = (...args) => { + // First up with a closure we increment the internal reference + // count. This ensures that the Rust closure environment won't + // be deallocated while we're invoking it. + state.cnt++; + const a = state.a; + state.a = 0; + try { + return f(a, state.b, ...args); + } finally { + if (--state.cnt === 0) { + wasm.__wbindgen_export_4.get(state.dtor)(a, state.b); + CLOSURE_DTORS.unregister(state); + } else { + state.a = a; + } + } + }; + real.original = state; + CLOSURE_DTORS.register(real, state, state); + return real; +} + +function debugString(val) { + // primitive types + const type = typeof val; + if (type == 'number' || type == 'boolean' || val == null) { + return `${val}`; + } + if (type == 'string') { + return `"${val}"`; + } + if (type == 'symbol') { + const description = val.description; + if (description == null) { + return 'Symbol'; + } else { + return `Symbol(${description})`; + } + } + if (type == 'function') { + const name = val.name; + if (typeof name == 'string' && name.length > 0) { + return `Function(${name})`; + } else { + return 'Function'; + } + } + // objects + if (Array.isArray(val)) { + const length = val.length; + let debug = '['; + if (length > 0) { + debug += debugString(val[0]); + } + for(let i = 1; i < length; i++) { + debug += ', ' + debugString(val[i]); + } + debug += ']'; + return debug; + } + // Test for built-in + const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val)); + let className; + if (builtInMatches && builtInMatches.length > 1) { + className = builtInMatches[1]; + } else { + // Failed to match the standard '[object ClassName]' + return toString.call(val); + } + if (className == 'Object') { + // we're a user defined class or Object + // JSON.stringify avoids problems with cycles, and is generally much + // easier than looping through ownProperties of `val`. + try { + return 'Object(' + JSON.stringify(val) + ')'; + } catch (_) { + return 'Object'; + } + } + // errors + if (val instanceof Error) { + return `${val.name}: ${val.message}\n${val.stack}`; + } + // TODO we could test for more things here, like `Set`s and `Map`s. + return className; +} + +function getArrayU8FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +} + +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8ArrayMemory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} +function __wbg_adapter_28(arg0, arg1, arg2) { + wasm.__wbindgen_export_5(arg0, arg1, addHeapObject(arg2)); +} + +function __wbg_adapter_31(arg0, arg1, arg2) { + wasm.__wbindgen_export_6(arg0, arg1, addHeapObject(arg2)); +} + +function __wbg_adapter_34(arg0, arg1, arg2) { + wasm.__wbindgen_export_7(arg0, arg1, addHeapObject(arg2)); +} + +function __wbg_adapter_164(arg0, arg1, arg2, arg3) { + wasm.__wbindgen_export_8(arg0, arg1, addHeapObject(arg2), addHeapObject(arg3)); +} + +const __wbindgen_enum_IdbTransactionMode = ["readonly", "readwrite", "versionchange", "readwriteflush", "cleanup"]; + +const CTStoreFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_ctstore_free(ptr >>> 0, 1)); +/** + * The [`CtStore`] provides direct access to the underlying web store. + */ +export class CTStore { + + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(CTStore.prototype); + obj.__wbg_ptr = ptr; + CTStoreFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + CTStoreFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_ctstore_free(ptr, 0); + } + /** + * Create a new [`CtStore`]. + * @param {string} db_name + * @param {string} store_name + * @param {Uint8Array | undefined} [hash] + */ + constructor(db_name, store_name, hash) { + const ptr0 = passStringToWasm0(db_name, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(store_name, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + const len1 = WASM_VECTOR_LEN; + var ptr2 = isLikeNone(hash) ? 0 : passArray8ToWasm0(hash, wasm.__wbindgen_export_2); + var len2 = WASM_VECTOR_LEN; + const ret = wasm.ctstore_new(ptr0, len0, ptr1, len1, ptr2, len2); + return takeObject(ret); + } + /** + * Returns the root hash of the storage, if it contains data. + * @returns {Uint8Array | undefined} + */ + hash() { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + wasm.ctstore_hash(retptr, this.__wbg_ptr); + var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true); + var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true); + let v1; + if (r0 !== 0) { + v1 = getArrayU8FromWasm0(r0, r1).slice(); + wasm.__wbindgen_export_1(r0, r1 * 1, 1); + } + return v1; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } + } + /** + * Sets `key` to `value`. + * @param {Uint8Array} key + * @param {Uint8Array} value + * @returns {Promise} + */ + set(key, value) { + const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_export_2); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(value, wasm.__wbindgen_export_2); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.ctstore_set(this.__wbg_ptr, ptr0, len0, ptr1, len1); + return takeObject(ret); + } + /** + * Retrieves value with `key`. + * @param {Uint8Array} key + * @returns {Promise} + */ + get(key) { + const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_export_2); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.ctstore_get(this.__wbg_ptr, ptr0, len0); + return takeObject(ret); + } + /** + * Calls `callback` with `key` and `value` arguments + * for each entry within `start` and `end` range. + * @param {Uint8Array} start + * @param {Uint8Array} end + * @param {boolean} start_inclusive + * @param {boolean} end_inclusive + * @param {Function} callback + * @returns {Promise} + */ + getRange(start, end, start_inclusive, end_inclusive, callback) { + const ptr0 = passArray8ToWasm0(start, wasm.__wbindgen_export_2); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(end, wasm.__wbindgen_export_2); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.ctstore_getRange(this.__wbg_ptr, ptr0, len0, ptr1, len1, start_inclusive, end_inclusive, addHeapObject(callback)); + return takeObject(ret); + } +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_buffer_61b7ce01341d7f88 = function(arg0) { + const ret = getObject(arg0).buffer; + return addHeapObject(ret); + }; + imports.wbg.__wbg_call_3b770f0d6eb4720e = function() { return handleError(function (arg0, arg1, arg2, arg3) { + const ret = getObject(arg0).call(getObject(arg1), getObject(arg2), getObject(arg3)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_call_500db948e69c7330 = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).call(getObject(arg1), getObject(arg2)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_call_b0d8e36992d9900d = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).call(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_close_f6e618ed8b0326a3 = function(arg0) { + getObject(arg0).close(); + }; + imports.wbg.__wbg_createIndex_14571b3c4eec1628 = function() { return handleError(function (arg0, arg1, arg2, arg3) { + const ret = getObject(arg0).createIndex(getStringFromWasm0(arg1, arg2), getObject(arg3)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_createIndex_1d4b2bbb6b21b8f8 = function() { return handleError(function (arg0, arg1, arg2, arg3, arg4) { + const ret = getObject(arg0).createIndex(getStringFromWasm0(arg1, arg2), getObject(arg3), getObject(arg4)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_createObjectStore_8d7577746ff46a7d = function() { return handleError(function (arg0, arg1, arg2, arg3) { + const ret = getObject(arg0).createObjectStore(getStringFromWasm0(arg1, arg2), getObject(arg3)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_ctstore_new = function(arg0) { + const ret = CTStore.__wrap(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbg_deleteIndex_86ec4c1be2675058 = function() { return handleError(function (arg0, arg1, arg2) { + getObject(arg0).deleteIndex(getStringFromWasm0(arg1, arg2)); + }, arguments) }; + imports.wbg.__wbg_deleteObjectStore_1f50ba0263875deb = function() { return handleError(function (arg0, arg1, arg2) { + getObject(arg0).deleteObjectStore(getStringFromWasm0(arg1, arg2)); + }, arguments) }; + imports.wbg.__wbg_error_7534b8e9a36f1ab4 = function(arg0, arg1) { + let deferred0_0; + let deferred0_1; + try { + deferred0_0 = arg0; + deferred0_1 = arg1; + console.error(getStringFromWasm0(arg0, arg1)); + } finally { + wasm.__wbindgen_export_1(deferred0_0, deferred0_1, 1); + } + }; + imports.wbg.__wbg_error_77233c625ed0c533 = function() { return handleError(function (arg0) { + const ret = getObject(arg0).error; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_error_aabb2ea1160edfa2 = function(arg0) { + const ret = getObject(arg0).error; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_get_6a4f854f5cca7403 = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).get(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_get_9aa3dff3f0266054 = function(arg0, arg1) { + const ret = getObject(arg0)[arg1 >>> 0]; + return addHeapObject(ret); + }; + imports.wbg.__wbg_get_bbccf8970793c087 = function() { return handleError(function (arg0, arg1) { + const ret = Reflect.get(getObject(arg0), getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_get_bfcfa7d168a923b6 = function(arg0, arg1, arg2) { + const ret = getObject(arg1)[arg2 >>> 0]; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + var len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbg_indexNames_ea5a29611c881483 = function(arg0) { + const ret = getObject(arg0).indexNames; + return addHeapObject(ret); + }; + imports.wbg.__wbg_index_871d874253bae760 = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).index(getStringFromWasm0(arg1, arg2)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_instanceof_IdbDatabase_4728aafa594bcd0f = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof IDBDatabase; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_IdbFactory_58497a6cd3e3a103 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof IDBFactory; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_IdbOpenDbRequest_a5b41ce7ffc27340 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof IDBOpenDBRequest; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_IdbRequest_6cd11201baf6632f = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof IDBRequest; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_IdbTransaction_ab63aadc06fe81be = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof IDBTransaction; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_instanceof_Uint8Array_28af5bc19d6acad8 = function(arg0) { + let result; + try { + result = getObject(arg0) instanceof Uint8Array; + } catch (_) { + result = false; + } + const ret = result; + return ret; + }; + imports.wbg.__wbg_isArray_1ba11a930108ec51 = function(arg0) { + const ret = Array.isArray(getObject(arg0)); + return ret; + }; + imports.wbg.__wbg_keyPath_0905ca0995f8c81d = function() { return handleError(function (arg0) { + const ret = getObject(arg0).keyPath; + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_length_5e3f0bd7902bc4ce = function(arg0) { + const ret = getObject(arg0).length; + return ret; + }; + imports.wbg.__wbg_length_65d1cd11729ced11 = function(arg0) { + const ret = getObject(arg0).length; + return ret; + }; + imports.wbg.__wbg_length_d65cf0786bfc5739 = function(arg0) { + const ret = getObject(arg0).length; + return ret; + }; + imports.wbg.__wbg_log_0cc1b7768397bcfe = function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { + let deferred0_0; + let deferred0_1; + try { + deferred0_0 = arg0; + deferred0_1 = arg1; + console.log(getStringFromWasm0(arg0, arg1), getStringFromWasm0(arg2, arg3), getStringFromWasm0(arg4, arg5), getStringFromWasm0(arg6, arg7)); + } finally { + wasm.__wbindgen_export_1(deferred0_0, deferred0_1, 1); + } + }; + imports.wbg.__wbg_log_cb9e190acc5753fb = function(arg0, arg1) { + let deferred0_0; + let deferred0_1; + try { + deferred0_0 = arg0; + deferred0_1 = arg1; + console.log(getStringFromWasm0(arg0, arg1)); + } finally { + wasm.__wbindgen_export_1(deferred0_0, deferred0_1, 1); + } + }; + imports.wbg.__wbg_mark_7438147ce31e9d4b = function(arg0, arg1) { + performance.mark(getStringFromWasm0(arg0, arg1)); + }; + imports.wbg.__wbg_measure_fb7825c11612c823 = function() { return handleError(function (arg0, arg1, arg2, arg3) { + let deferred0_0; + let deferred0_1; + let deferred1_0; + let deferred1_1; + try { + deferred0_0 = arg0; + deferred0_1 = arg1; + deferred1_0 = arg2; + deferred1_1 = arg3; + performance.measure(getStringFromWasm0(arg0, arg1), getStringFromWasm0(arg2, arg3)); + } finally { + wasm.__wbindgen_export_1(deferred0_0, deferred0_1, 1); + wasm.__wbindgen_export_1(deferred1_0, deferred1_1, 1); + } + }, arguments) }; + imports.wbg.__wbg_multiEntry_08ef01df0c584217 = function(arg0) { + const ret = getObject(arg0).multiEntry; + return ret; + }; + imports.wbg.__wbg_new_254fa9eac11932ae = function() { + const ret = new Array(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_3d446df9155128ef = function(arg0, arg1) { + try { + var state0 = {a: arg0, b: arg1}; + var cb0 = (arg0, arg1) => { + const a = state0.a; + state0.a = 0; + try { + return __wbg_adapter_164(a, state0.b, arg0, arg1); + } finally { + state0.a = a; + } + }; + const ret = new Promise(cb0); + return addHeapObject(ret); + } finally { + state0.a = state0.b = 0; + } + }; + imports.wbg.__wbg_new_3ff5b33b1ce712df = function(arg0) { + const ret = new Uint8Array(getObject(arg0)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_688846f374351c92 = function() { + const ret = new Object(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_new_8a6f238a6ece86ea = function() { + const ret = new Error(); + return addHeapObject(ret); + }; + imports.wbg.__wbg_newnoargs_fd9e4bf8be2bc16d = function(arg0, arg1) { + const ret = new Function(getStringFromWasm0(arg0, arg1)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_newwithbyteoffsetandlength_ba35896968751d91 = function(arg0, arg1, arg2) { + const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0); + return addHeapObject(ret); + }; + imports.wbg.__wbg_newwithlength_34ce8f1051e74449 = function(arg0) { + const ret = new Uint8Array(arg0 >>> 0); + return addHeapObject(ret); + }; + imports.wbg.__wbg_objectStoreNames_cb00c8113b3cf5df = function(arg0) { + const ret = getObject(arg0).objectStoreNames; + return addHeapObject(ret); + }; + imports.wbg.__wbg_objectStore_cdbc73ee600a2cfa = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).objectStore(getStringFromWasm0(arg1, arg2)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_open_4588c85c0c43671d = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).open(getStringFromWasm0(arg1, arg2)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_open_5c51d4d6d7ab6da6 = function() { return handleError(function (arg0, arg1, arg2, arg3) { + const ret = getObject(arg0).open(getStringFromWasm0(arg1, arg2), arg3 >>> 0); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_push_6edad0df4b546b2c = function(arg0, arg1) { + const ret = getObject(arg0).push(getObject(arg1)); + return ret; + }; + imports.wbg.__wbg_put_5393e88dbe742e8f = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).put(getObject(arg1), getObject(arg2)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_put_78726bde9e67ce9c = function() { return handleError(function (arg0, arg1) { + const ret = getObject(arg0).put(getObject(arg1)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_queueMicrotask_2181040e064c0dc8 = function(arg0) { + queueMicrotask(getObject(arg0)); + }; + imports.wbg.__wbg_queueMicrotask_ef9ac43769cbcc4f = function(arg0) { + const ret = getObject(arg0).queueMicrotask; + return addHeapObject(ret); + }; + imports.wbg.__wbg_resolve_0bf7c44d641804f9 = function(arg0) { + const ret = Promise.resolve(getObject(arg0)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_result_e6ba6a347dcb7470 = function() { return handleError(function (arg0) { + const ret = getObject(arg0).result; + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_set_23d69db4e5c66a6e = function(arg0, arg1, arg2) { + getObject(arg0).set(getObject(arg1), arg2 >>> 0); + }; + imports.wbg.__wbg_setautoincrement_01d0c0050f6c3ac7 = function(arg0, arg1) { + getObject(arg0).autoIncrement = arg1 !== 0; + }; + imports.wbg.__wbg_setkeypath_ba11b9fa7ea79484 = function(arg0, arg1) { + getObject(arg0).keyPath = getObject(arg1); + }; + imports.wbg.__wbg_setmultientry_44d7522905f48f7b = function(arg0, arg1) { + getObject(arg0).multiEntry = arg1 !== 0; + }; + imports.wbg.__wbg_setonabort_ade6cac5c35a572f = function(arg0, arg1) { + getObject(arg0).onabort = getObject(arg1); + }; + imports.wbg.__wbg_setoncomplete_2747fb71ae0af4a8 = function(arg0, arg1) { + getObject(arg0).oncomplete = getObject(arg1); + }; + imports.wbg.__wbg_setonerror_72b33e31f9edb045 = function(arg0, arg1) { + getObject(arg0).onerror = getObject(arg1); + }; + imports.wbg.__wbg_setonerror_e4beb2652cf66635 = function(arg0, arg1) { + getObject(arg0).onerror = getObject(arg1); + }; + imports.wbg.__wbg_setonsuccess_57167b1c2650357c = function(arg0, arg1) { + getObject(arg0).onsuccess = getObject(arg1); + }; + imports.wbg.__wbg_setonupgradeneeded_887c7a5fca66011e = function(arg0, arg1) { + getObject(arg0).onupgradeneeded = getObject(arg1); + }; + imports.wbg.__wbg_setonversionchange_a6f60bd1d110795d = function(arg0, arg1) { + getObject(arg0).onversionchange = getObject(arg1); + }; + imports.wbg.__wbg_setunique_cfc477dc5825e1c4 = function(arg0, arg1) { + getObject(arg0).unique = arg1 !== 0; + }; + imports.wbg.__wbg_stack_0ed75d68575b0f3c = function(arg0, arg1) { + const ret = getObject(arg1).stack; + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbg_static_accessor_GLOBAL_0be7472e492ad3e3 = function() { + const ret = typeof global === 'undefined' ? null : global; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_GLOBAL_THIS_1a6eb482d12c9bfb = function() { + const ret = typeof globalThis === 'undefined' ? null : globalThis; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_SELF_1dc398a895c82351 = function() { + const ret = typeof self === 'undefined' ? null : self; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_static_accessor_WINDOW_ae1c80c7eea8d64a = function() { + const ret = typeof window === 'undefined' ? null : window; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_stringify_f4f701bc34ceda61 = function() { return handleError(function (arg0) { + const ret = JSON.stringify(getObject(arg0)); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_target_a8fe593e7ee79c21 = function(arg0) { + const ret = getObject(arg0).target; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_then_0438fad860fe38e1 = function(arg0, arg1) { + const ret = getObject(arg0).then(getObject(arg1)); + return addHeapObject(ret); + }; + imports.wbg.__wbg_transaction_d2b6d520837fbe7f = function(arg0) { + const ret = getObject(arg0).transaction; + return isLikeNone(ret) ? 0 : addHeapObject(ret); + }; + imports.wbg.__wbg_transaction_dbab9cd9ea849958 = function() { return handleError(function (arg0, arg1, arg2) { + const ret = getObject(arg0).transaction(getObject(arg1), __wbindgen_enum_IdbTransactionMode[arg2]); + return addHeapObject(ret); + }, arguments) }; + imports.wbg.__wbg_unique_0192502abf384518 = function(arg0) { + const ret = getObject(arg0).unique; + return ret; + }; + imports.wbg.__wbindgen_cb_drop = function(arg0) { + const obj = takeObject(arg0).original; + if (obj.cnt-- == 1) { + obj.a = 0; + return true; + } + const ret = false; + return ret; + }; + imports.wbg.__wbindgen_closure_wrapper237 = function(arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 49, __wbg_adapter_28); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_closure_wrapper369 = function(arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 79, __wbg_adapter_31); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_closure_wrapper872 = function(arg0, arg1, arg2) { + const ret = makeMutClosure(arg0, arg1, 223, __wbg_adapter_34); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_debug_string = function(arg0, arg1) { + const ret = debugString(getObject(arg1)); + const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + const len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbindgen_is_function = function(arg0) { + const ret = typeof(getObject(arg0)) === 'function'; + return ret; + }; + imports.wbg.__wbindgen_is_null = function(arg0) { + const ret = getObject(arg0) === null; + return ret; + }; + imports.wbg.__wbindgen_is_string = function(arg0) { + const ret = typeof(getObject(arg0)) === 'string'; + return ret; + }; + imports.wbg.__wbindgen_is_undefined = function(arg0) { + const ret = getObject(arg0) === undefined; + return ret; + }; + imports.wbg.__wbindgen_memory = function() { + const ret = wasm.memory; + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_clone_ref = function(arg0) { + const ret = getObject(arg0); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_object_drop_ref = function(arg0) { + takeObject(arg0); + }; + imports.wbg.__wbindgen_string_get = function(arg0, arg1) { + const obj = getObject(arg1); + const ret = typeof(obj) === 'string' ? obj : undefined; + var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export_2, wasm.__wbindgen_export_3); + var len1 = WASM_VECTOR_LEN; + getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); + getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); + }; + imports.wbg.__wbindgen_string_new = function(arg0, arg1) { + const ret = getStringFromWasm0(arg0, arg1); + return addHeapObject(ret); + }; + imports.wbg.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + imports.wbg.__wbindgen_uint8_array_new = function(arg0, arg1) { + var v0 = getArrayU8FromWasm0(arg0, arg1).slice(); + wasm.__wbindgen_export_1(arg0, arg1 * 1, 1); + const ret = v0; + return addHeapObject(ret); + }; + + return imports; +} + +function __wbg_init_memory(imports, memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedDataViewMemory0 = null; + cachedUint8ArrayMemory0 = null; + + + + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('ct_engine_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync }; +export default __wbg_init; diff --git a/typescript/packages/common-engine/ct_engine_bg.wasm b/typescript/packages/common-engine/ct_engine_bg.wasm new file mode 100644 index 000000000..cbde6eff5 Binary files /dev/null and b/typescript/packages/common-engine/ct_engine_bg.wasm differ diff --git a/typescript/packages/common-engine/ct_engine_bg.wasm.d.ts b/typescript/packages/common-engine/ct_engine_bg.wasm.d.ts new file mode 100644 index 000000000..c51514e06 --- /dev/null +++ b/typescript/packages/common-engine/ct_engine_bg.wasm.d.ts @@ -0,0 +1,19 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export const __wbg_ctstore_free: (a: number, b: number) => void; +export const ctstore_new: (a: number, b: number, c: number, d: number, e: number, f: number) => number; +export const ctstore_hash: (a: number, b: number) => void; +export const ctstore_set: (a: number, b: number, c: number, d: number, e: number) => number; +export const ctstore_get: (a: number, b: number, c: number) => number; +export const ctstore_getRange: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => number; +export const __wbindgen_export_0: (a: number) => void; +export const __wbindgen_export_1: (a: number, b: number, c: number) => void; +export const __wbindgen_export_2: (a: number, b: number) => number; +export const __wbindgen_export_3: (a: number, b: number, c: number, d: number) => number; +export const __wbindgen_export_4: WebAssembly.Table; +export const __wbindgen_add_to_stack_pointer: (a: number) => number; +export const __wbindgen_export_5: (a: number, b: number, c: number) => void; +export const __wbindgen_export_6: (a: number, b: number, c: number) => void; +export const __wbindgen_export_7: (a: number, b: number, c: number) => void; +export const __wbindgen_export_8: (a: number, b: number, c: number, d: number) => void; diff --git a/typescript/packages/common-engine/example.html b/typescript/packages/common-engine/example.html new file mode 100644 index 000000000..7dc271bb3 --- /dev/null +++ b/typescript/packages/common-engine/example.html @@ -0,0 +1,100 @@ + \ No newline at end of file diff --git a/typescript/packages/common-engine/package.json b/typescript/packages/common-engine/package.json new file mode 100644 index 000000000..357115040 --- /dev/null +++ b/typescript/packages/common-engine/package.json @@ -0,0 +1,16 @@ +{ + "name": "ct-engine", + "type": "module", + "description": "Common engine", + "version": "0.1.0", + "files": [ + "ct_engine_bg.wasm", + "ct_engine.js", + "ct_engine.d.ts" + ], + "main": "ct_engine.js", + "types": "ct_engine.d.ts", + "sideEffects": [ + "./snippets/*" + ] +} \ No newline at end of file diff --git a/typescript/packages/common-system/package.json b/typescript/packages/common-system/package.json index 452f1737f..377b3b54d 100644 --- a/typescript/packages/common-system/package.json +++ b/typescript/packages/common-system/package.json @@ -29,6 +29,7 @@ "@commontools/common-builder": "^0.0.1", "@commontools/common-html": "^0.0.1", "@commontools/common-runner": "^0.0.1", + "@commontools/common-engine": "file:../common-engine", "@commontools/llm-client": "^0.0.1", "@gozala/co-dom": "^0.4.0", "datalogia": "^0.9.2", @@ -67,4 +68,4 @@ "command": "rm -rf ./lib ./dist ./.wireit ./node_modules tsconfig.tsbuildinfo" } } -} +} \ No newline at end of file diff --git a/typescript/packages/common-system/src/ctstore.ts b/typescript/packages/common-system/src/ctstore.ts new file mode 100644 index 000000000..cf7b7da7a --- /dev/null +++ b/typescript/packages/common-system/src/ctstore.ts @@ -0,0 +1,173 @@ +import { + Type, + Task, +} from "synopsys"; +import init, { CTStore } from "@commontools/common-engine"; + +// This isn't exported from "synopsis/store/sequence" +//import { IterationFinished } from "synopsys/store/sequence"; +class CTStoreIterationFinished implements Type.IterationFinished { + name: 'IterationFinished'; + message = 'IterationFinished'; +} + +let READ = 0; +let ENTRIES = 0; + +class CTStoreSequence implements Type.Sequence { + private index: number; + private entries: Type.Entry[]; + private store: CTStore; + private lowerBound: Type.Bound; + private upperBound: Type.Bound; + + constructor(store: CTStore, lowerBound: Type.Bound, upperBound: Type.Bound) { + if (!lowerBound.key || !upperBound.key) { + throw new Error("Null bounds not yet supported."); + } + upperBound.key = padKey(upperBound.key); + lowerBound.key = padKey(lowerBound.key); + this.index = 0; + this.entries = []; + this.store = store; + this.upperBound = upperBound; + this.lowerBound = lowerBound; + } + + *next(): Type.API.Task, Error, typeof Task.SUSPEND | Task.Join | Task.Throw> { + if (this.index === 0) { + if (!this.lowerBound.key || !this.upperBound.key) { + throw new Error("Null bounds not yet supported."); + } + let entries = this.entries; + yield* Task.wait(this.store.getRange(this.lowerBound.key, this.upperBound.key, this.lowerBound.inclusive, this.upperBound.inclusive, (key: Uint8Array, value: Uint8Array) => { + entries.push([key, value]); + })); + } + if (this.index < this.entries.length) { + return { ok: this.entries[this.index++] }; + } else { + return { error: new CTStoreIterationFinished() }; + } + } +} + +export function* open(dbName: string, tableName: string) { + yield* Task.wait(init()); + let store = yield* Task.wait(new CTStore(dbName, tableName, undefined)); + return new CTSynopsisStore(store); +} + +export class CTSynopsisStore implements Type.Store, Type.StoreReader, Type.StoreWriter { + private store: CTStore; + constructor(store: CTStore) { + this.store = store; + } + + // /!\ Should be unnecessary + *getRoot(): Type.API.Task { + let key = new Uint8Array(96); + let hash = this.store.hash() ?? new Uint8Array(32); + return { + level: 0, + key, + hash, + value: undefined, + }; + } + + // /!\ Should be unnecessary + *getChildren(_level: number, _key: Uint8Array): Type.API.Task { + throw new Error("getChildren() not implemented"); + } + // /!\ Should be unnecessary + *getNode(_level: number, _key: Type.Key): Type.API.Task { + throw new Error("getNode() not implemented"); + } + // /!\ Should be unnecessary + nodes(_level: number, _lowerBound?: Type.Bound | null, _upperBound?: Type.Bound | null, _options?: { reverse?: boolean; }): + Type.Sequence { + throw new Error("nodes() not implemented"); + } + + entries(lowerBound?: Type.Bound | null, upperBound?: Type.Bound | null, _options?: { reverse?: boolean; }): + Type.Sequence { + if (!lowerBound || !upperBound) { + throw new Error("Bounds must be defined."); + } + console.log('!entries', ++ENTRIES); + return new CTStoreSequence(this.store, lowerBound, upperBound); + } + + *get(key: Uint8Array): Type.API.Task { + console.log('!get'); + let result = yield* Task.wait(this.store.get(padKey(key))); + return result ?? null; + } + + *delete(_key: Uint8Array): Type.API.Task<{}, Error> { + console.log('!delete'); + throw new Error("delete() not implemented"); + } + + *set(key: Uint8Array, value: Uint8Array): Type.API.Task<{}, Error> { + console.log('!set'); + yield* Task.wait(this.store.set(padKey(key), value)); + return {}; + } + + *integrate(changes: Type.Change[]): Type.API.Task { + console.log('!integrate'); + for (const [key, value] of changes) { + if (value) { + yield* this.set(key, value) + } else { + yield* this.delete(key) + } + } + + return yield* this.getRoot() + } + + *read(read: (reader: Type.StoreReader) => Type.API.Task): Type.API.Task { + const result = yield* read(this); + this.commit(); + console.log('!entries', ++READ); + return result; + } + + *write(write: (editor: Type.StoreEditor) => Type.API.Task): Type.API.Task { + const result = yield* write(this); + console.log('!write'); + this.commit(); + return result; + } + + commit() { + } + + *clear(): Type.API.Task<{}, Error> { + // todo! + throw new Error("clear() not implemented"); + return {}; + } + + *close(): Type.API.Task<{}, Error> { + // todo! + this.store.free(); + return {}; + } +} + +function padKey(key: Uint8Array): Uint8Array { + let len = key.length; + if (len < 96) { + let buffer = new Uint8Array(96); + buffer.set(key); + return buffer; + } else if (len > 96) { + throw new Error("key larger than 96"); + } else { + return key; + } +} \ No newline at end of file diff --git a/typescript/packages/common-system/src/db.ts b/typescript/packages/common-system/src/db.ts index c9a1271dc..9d64eac02 100644 --- a/typescript/packages/common-system/src/db.ts +++ b/typescript/packages/common-system/src/db.ts @@ -21,6 +21,8 @@ import { Constant } from "datalogia"; import { logQuery } from "./debug.js"; import * as Store from "./idb.js"; import { Reference } from "merkle-reference"; +import { open as openCTSynopsisStore } from "./ctstore.js"; + export * from "synopsys"; export type DB = @@ -42,6 +44,7 @@ export interface Options { local?: IDB.Open; } + /** * Represents a connection to a local and a remote databases. It takes care * of synchronizing two by publishing local changes to remote and integrating @@ -49,7 +52,9 @@ export interface Options { */ class Connection { static *open(options: Options = {}) { - const durable = yield* IDB.open(options.local); + const USE_CT_STORE = true; + const durable = yield* (USE_CT_STORE ? openCTSynopsisStore("dbname", "tablename") : IDB.open(options.local)); + //const durable = yield* IDB.open(options.local); // const durable = yield* Memory.open(); const ephemeral = yield* Memory.open(); @@ -71,7 +76,7 @@ class Connection { constructor( public local: DB, public remote: Remote, - ) {} + ) { } *dispatch([entity, attribute, event]: [Reference, string, Event]) { yield* this.local.transact([ @@ -304,7 +309,7 @@ class PullSource implements UnderlyingDefaultSource { public url: URL, public store: Store.Store, public db: DB, - ) {} + ) { } async start(controller: ReadableStreamDefaultController) { while (!this.cancelled) { @@ -419,7 +424,7 @@ class PushTarget implements UnderlyingSink, API.Transactor { constructor( public url: URL, public store: Store.Store, - ) {} + ) { } start() { return Task.perform(this.flush()); } @@ -643,7 +648,7 @@ class Subscription, public effect: Effect