wao 0.9.2 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/ao.js +31 -32
- package/cjs/aoconnect-base.js +1 -1
- package/cjs/armem-base.js +104 -53
- package/cjs/armem-web.js +11 -1
- package/cjs/armem.js +8 -1
- package/cjs/build.js +8 -0
- package/cjs/compress.js +165 -0
- package/cjs/lfdb.js +5 -4
- package/cjs/waosm/waosm.js +332 -0
- package/cjs/waosm-node/waosm.js +21 -0
- package/cjs/waosm-node/waosm_bg.js +193 -0
- package/cjs/waosm-node.js +28 -0
- package/esm/ao.js +7 -7
- package/esm/aoconnect-base.js +1 -1
- package/esm/armem-base.js +51 -8
- package/esm/armem-web.js +2 -1
- package/esm/armem.js +2 -1
- package/esm/build.js +7 -0
- package/esm/compress.js +113 -0
- package/esm/lfdb.js +2 -3
- package/esm/waosm/README.md +79 -0
- package/esm/waosm/package.json +14 -0
- package/esm/waosm/waosm.d.ts +46 -0
- package/esm/waosm/waosm.js +264 -0
- package/esm/waosm/waosm_bg.wasm +0 -0
- package/esm/waosm/waosm_bg.wasm.d.ts +10 -0
- package/esm/waosm-node/README.md +79 -0
- package/esm/waosm-node/waosm.d.ts +12 -0
- package/esm/waosm-node/waosm.js +4 -0
- package/esm/waosm-node/waosm_bg.js +171 -0
- package/esm/waosm-node/waosm_bg.wasm +0 -0
- package/esm/waosm-node/waosm_bg.wasm.d.ts +10 -0
- package/esm/waosm-node.js +13 -0
- package/package.json +4 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Decompressor = exports.Compressor = void 0;
|
|
7
|
+
exports.__wbg_buffer_609cc3eee51ed158 = __wbg_buffer_609cc3eee51ed158;
|
|
8
|
+
exports.__wbg_length_a446193dc22c12f8 = __wbg_length_a446193dc22c12f8;
|
|
9
|
+
exports.__wbg_newwithbyteoffsetandlength_d97e637ebe145a9a = __wbg_newwithbyteoffsetandlength_d97e637ebe145a9a;
|
|
10
|
+
exports.__wbg_newwithlength_a381634e90c276d4 = __wbg_newwithlength_a381634e90c276d4;
|
|
11
|
+
exports.__wbg_set_65595bdd868b3009 = __wbg_set_65595bdd868b3009;
|
|
12
|
+
exports.__wbg_set_wasm = __wbg_set_wasm;
|
|
13
|
+
exports.__wbindgen_memory = __wbindgen_memory;
|
|
14
|
+
exports.__wbindgen_object_drop_ref = __wbindgen_object_drop_ref;
|
|
15
|
+
exports.__wbindgen_throw = __wbindgen_throw;
|
|
16
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
17
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
18
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
19
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
var wasm;
|
|
23
|
+
function __wbg_set_wasm(val) {
|
|
24
|
+
wasm = val;
|
|
25
|
+
}
|
|
26
|
+
var heap = new Array(128).fill(undefined);
|
|
27
|
+
heap.push(undefined, null, true, false);
|
|
28
|
+
function getObject(idx) {
|
|
29
|
+
return heap[idx];
|
|
30
|
+
}
|
|
31
|
+
var heap_next = heap.length;
|
|
32
|
+
function addHeapObject(obj) {
|
|
33
|
+
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
34
|
+
var idx = heap_next;
|
|
35
|
+
heap_next = heap[idx];
|
|
36
|
+
heap[idx] = obj;
|
|
37
|
+
return idx;
|
|
38
|
+
}
|
|
39
|
+
function dropObject(idx) {
|
|
40
|
+
if (idx < 132) return;
|
|
41
|
+
heap[idx] = heap_next;
|
|
42
|
+
heap_next = idx;
|
|
43
|
+
}
|
|
44
|
+
function takeObject(idx) {
|
|
45
|
+
var ret = getObject(idx);
|
|
46
|
+
dropObject(idx);
|
|
47
|
+
return ret;
|
|
48
|
+
}
|
|
49
|
+
var lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
|
50
|
+
var cachedTextDecoder = new lTextDecoder('utf-8', {
|
|
51
|
+
ignoreBOM: true,
|
|
52
|
+
fatal: true
|
|
53
|
+
});
|
|
54
|
+
cachedTextDecoder.decode();
|
|
55
|
+
var cachedUint8ArrayMemory0 = null;
|
|
56
|
+
function getUint8ArrayMemory0() {
|
|
57
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
58
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
59
|
+
}
|
|
60
|
+
return cachedUint8ArrayMemory0;
|
|
61
|
+
}
|
|
62
|
+
function getStringFromWasm0(ptr, len) {
|
|
63
|
+
ptr = ptr >>> 0;
|
|
64
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
65
|
+
}
|
|
66
|
+
var WASM_VECTOR_LEN = 0;
|
|
67
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
68
|
+
var ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
69
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
70
|
+
WASM_VECTOR_LEN = arg.length;
|
|
71
|
+
return ptr;
|
|
72
|
+
}
|
|
73
|
+
var CompressorFinalization = typeof FinalizationRegistry === 'undefined' ? {
|
|
74
|
+
register: function register() {},
|
|
75
|
+
unregister: function unregister() {}
|
|
76
|
+
} : new FinalizationRegistry(function (ptr) {
|
|
77
|
+
return wasm.__wbg_compressor_free(ptr >>> 0, 1);
|
|
78
|
+
});
|
|
79
|
+
var Compressor = exports.Compressor = /*#__PURE__*/function () {
|
|
80
|
+
function Compressor() {
|
|
81
|
+
_classCallCheck(this, Compressor);
|
|
82
|
+
var ret = wasm.compressor_new();
|
|
83
|
+
this.__wbg_ptr = ret >>> 0;
|
|
84
|
+
CompressorFinalization.register(this, this.__wbg_ptr, this);
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @param {Uint8Array} data
|
|
89
|
+
* @returns {Uint8Array}
|
|
90
|
+
*/
|
|
91
|
+
return _createClass(Compressor, [{
|
|
92
|
+
key: "__destroy_into_raw",
|
|
93
|
+
value: function __destroy_into_raw() {
|
|
94
|
+
var ptr = this.__wbg_ptr;
|
|
95
|
+
this.__wbg_ptr = 0;
|
|
96
|
+
CompressorFinalization.unregister(this);
|
|
97
|
+
return ptr;
|
|
98
|
+
}
|
|
99
|
+
}, {
|
|
100
|
+
key: "free",
|
|
101
|
+
value: function free() {
|
|
102
|
+
var ptr = this.__destroy_into_raw();
|
|
103
|
+
wasm.__wbg_compressor_free(ptr, 0);
|
|
104
|
+
}
|
|
105
|
+
}, {
|
|
106
|
+
key: "compress",
|
|
107
|
+
value: function compress(data) {
|
|
108
|
+
var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
109
|
+
var len0 = WASM_VECTOR_LEN;
|
|
110
|
+
var ret = wasm.compressor_compress(this.__wbg_ptr, ptr0, len0);
|
|
111
|
+
return takeObject(ret);
|
|
112
|
+
}
|
|
113
|
+
}]);
|
|
114
|
+
}();
|
|
115
|
+
var DecompressorFinalization = typeof FinalizationRegistry === 'undefined' ? {
|
|
116
|
+
register: function register() {},
|
|
117
|
+
unregister: function unregister() {}
|
|
118
|
+
} : new FinalizationRegistry(function (ptr) {
|
|
119
|
+
return wasm.__wbg_decompressor_free(ptr >>> 0, 1);
|
|
120
|
+
});
|
|
121
|
+
var Decompressor = exports.Decompressor = /*#__PURE__*/function () {
|
|
122
|
+
function Decompressor() {
|
|
123
|
+
_classCallCheck(this, Decompressor);
|
|
124
|
+
var ret = wasm.compressor_new();
|
|
125
|
+
this.__wbg_ptr = ret >>> 0;
|
|
126
|
+
DecompressorFinalization.register(this, this.__wbg_ptr, this);
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @param {Uint8Array} data
|
|
131
|
+
* @returns {Uint8Array}
|
|
132
|
+
*/
|
|
133
|
+
return _createClass(Decompressor, [{
|
|
134
|
+
key: "__destroy_into_raw",
|
|
135
|
+
value: function __destroy_into_raw() {
|
|
136
|
+
var ptr = this.__wbg_ptr;
|
|
137
|
+
this.__wbg_ptr = 0;
|
|
138
|
+
DecompressorFinalization.unregister(this);
|
|
139
|
+
return ptr;
|
|
140
|
+
}
|
|
141
|
+
}, {
|
|
142
|
+
key: "free",
|
|
143
|
+
value: function free() {
|
|
144
|
+
var ptr = this.__destroy_into_raw();
|
|
145
|
+
wasm.__wbg_decompressor_free(ptr, 0);
|
|
146
|
+
}
|
|
147
|
+
}, {
|
|
148
|
+
key: "decompress",
|
|
149
|
+
value: function decompress(data) {
|
|
150
|
+
var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
151
|
+
var len0 = WASM_VECTOR_LEN;
|
|
152
|
+
var ret = wasm.decompressor_decompress(this.__wbg_ptr, ptr0, len0);
|
|
153
|
+
return takeObject(ret);
|
|
154
|
+
}
|
|
155
|
+
}]);
|
|
156
|
+
}();
|
|
157
|
+
function __wbg_buffer_609cc3eee51ed158(arg0) {
|
|
158
|
+
var ret = getObject(arg0).buffer;
|
|
159
|
+
return addHeapObject(ret);
|
|
160
|
+
}
|
|
161
|
+
;
|
|
162
|
+
function __wbg_length_a446193dc22c12f8(arg0) {
|
|
163
|
+
var ret = getObject(arg0).length;
|
|
164
|
+
return ret;
|
|
165
|
+
}
|
|
166
|
+
;
|
|
167
|
+
function __wbg_newwithbyteoffsetandlength_d97e637ebe145a9a(arg0, arg1, arg2) {
|
|
168
|
+
var ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
|
|
169
|
+
return addHeapObject(ret);
|
|
170
|
+
}
|
|
171
|
+
;
|
|
172
|
+
function __wbg_newwithlength_a381634e90c276d4(arg0) {
|
|
173
|
+
var ret = new Uint8Array(arg0 >>> 0);
|
|
174
|
+
return addHeapObject(ret);
|
|
175
|
+
}
|
|
176
|
+
;
|
|
177
|
+
function __wbg_set_65595bdd868b3009(arg0, arg1, arg2) {
|
|
178
|
+
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
|
|
179
|
+
}
|
|
180
|
+
;
|
|
181
|
+
function __wbindgen_memory() {
|
|
182
|
+
var ret = wasm.memory;
|
|
183
|
+
return addHeapObject(ret);
|
|
184
|
+
}
|
|
185
|
+
;
|
|
186
|
+
function __wbindgen_object_drop_ref(arg0) {
|
|
187
|
+
takeObject(arg0);
|
|
188
|
+
}
|
|
189
|
+
;
|
|
190
|
+
function __wbindgen_throw(arg0, arg1) {
|
|
191
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
192
|
+
}
|
|
193
|
+
;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
var _waosm_bg = _interopRequireWildcard(require("./waosm-node/waosm_bg.js"));
|
|
8
|
+
var wasmBindings = _waosm_bg;
|
|
9
|
+
Object.keys(_waosm_bg).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _waosm_bg[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function get() {
|
|
15
|
+
return _waosm_bg[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
var _fs = require("fs");
|
|
20
|
+
var _path = require("path");
|
|
21
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
22
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
23
|
+
var wasm = (0, _fs.readFileSync)((0, _path.resolve)(import.meta.dirname, "./waosm-node/waosm_bg.wasm"));
|
|
24
|
+
var wasmModule = new WebAssembly.Module(wasm);
|
|
25
|
+
var wasmInstance = new WebAssembly.Instance(wasmModule, {
|
|
26
|
+
"./waosm_bg.js": wasmBindings
|
|
27
|
+
});
|
|
28
|
+
(0, _waosm_bg.__wbg_set_wasm)(wasmInstance.exports);
|
package/esm/ao.js
CHANGED
|
@@ -369,7 +369,6 @@ class AO {
|
|
|
369
369
|
data: jsonToStr(data),
|
|
370
370
|
})
|
|
371
371
|
} catch (e) {
|
|
372
|
-
console.log(e)
|
|
373
372
|
err = e
|
|
374
373
|
}
|
|
375
374
|
const p = pid ? this.p(pid) : null
|
|
@@ -405,8 +404,7 @@ class AO {
|
|
|
405
404
|
}
|
|
406
405
|
}
|
|
407
406
|
}
|
|
408
|
-
const checkOut = async (get, _txs, _txmap) => {
|
|
409
|
-
let out = null
|
|
407
|
+
const checkOut = async (get, _txs, _txmap, out) => {
|
|
410
408
|
for (let v of _txs) {
|
|
411
409
|
if (isNil(_txmap[v.id].res)) {
|
|
412
410
|
const res = await this.result({ process: pid, message: v.id })
|
|
@@ -414,8 +412,9 @@ class AO {
|
|
|
414
412
|
}
|
|
415
413
|
if (!isNil(_txmap[v.id].res) && _txmap[v.id].out !== true) {
|
|
416
414
|
_txmap[v.id].out = true
|
|
417
|
-
|
|
418
|
-
|
|
415
|
+
const _out = getTagVal(get, _txmap[v.id].res, v.from)
|
|
416
|
+
out = mergeOut(out, _out, get)
|
|
417
|
+
if (isOutComplete(out, get)) break
|
|
419
418
|
}
|
|
420
419
|
}
|
|
421
420
|
return out
|
|
@@ -455,14 +454,15 @@ class AO {
|
|
|
455
454
|
await wait(1000)
|
|
456
455
|
await getNewTxs(pid, _txs, _txmap)
|
|
457
456
|
} while (Date.now() - start < timeout)
|
|
457
|
+
|
|
458
458
|
if (!checked) {
|
|
459
459
|
err = "something went wrong!"
|
|
460
460
|
} else {
|
|
461
|
-
out = mergeOut(out, await checkOut(get, _txs, _txmap), get)
|
|
461
|
+
out = mergeOut(out, await checkOut(get, _txs, _txmap, out), get)
|
|
462
462
|
if (!isOutComplete(out, get) && !isNil(get)) {
|
|
463
463
|
while (Date.now() - start < timeout) {
|
|
464
464
|
await getNewTxs(pid, _txs, _txmap)
|
|
465
|
-
out = mergeOut(out, await checkOut(get, _txs, _txmap), get)
|
|
465
|
+
out = mergeOut(out, await checkOut(get, _txs, _txmap, out), get)
|
|
466
466
|
if (isOutComplete(out, get)) break
|
|
467
467
|
await wait(1000)
|
|
468
468
|
}
|
package/esm/aoconnect-base.js
CHANGED
|
@@ -221,7 +221,7 @@ export default ({ AR, scheduler, mu, su, cu, acc, AoLoader, ArMem } = {}) => {
|
|
|
221
221
|
|
|
222
222
|
const assign = async opt => {
|
|
223
223
|
const p = await mem.get("env", opt.process)
|
|
224
|
-
if (!p) return null
|
|
224
|
+
if (!p || !opt.process) return null
|
|
225
225
|
let _opt = await mem.get("msgs", opt.message)
|
|
226
226
|
let hash = genHashChain(p.hash, opt.message)
|
|
227
227
|
p.hash = hash
|
package/esm/armem-base.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import _Arweave from "arweave"
|
|
2
2
|
const Arweave = _Arweave.default ?? _Arweave
|
|
3
|
-
|
|
4
|
-
import { last } from "ramda"
|
|
3
|
+
import { compress, decompress } from "./compress.js"
|
|
4
|
+
import { last, assoc, is } from "ramda"
|
|
5
5
|
import { buildTags, tags } from "./utils.js"
|
|
6
6
|
|
|
7
7
|
export default class ArMemBase {
|
|
8
|
-
constructor({
|
|
8
|
+
constructor({
|
|
9
|
+
MU_URL,
|
|
10
|
+
CU_URL,
|
|
11
|
+
SU_URL,
|
|
12
|
+
GATEWAY_URL,
|
|
13
|
+
scheduler,
|
|
14
|
+
cache,
|
|
15
|
+
init,
|
|
16
|
+
Compressor,
|
|
17
|
+
Decompressor,
|
|
18
|
+
} = {}) {
|
|
9
19
|
this.__type__ = "mem"
|
|
20
|
+
this._init = init
|
|
21
|
+
this.Compressor = Compressor
|
|
22
|
+
this.Decompressor = Decompressor
|
|
10
23
|
this.isInit = false
|
|
11
24
|
this.keyInit = false
|
|
12
25
|
this.keys = {}
|
|
@@ -34,7 +47,22 @@ export default class ArMemBase {
|
|
|
34
47
|
} else {
|
|
35
48
|
this[key] ??= {}
|
|
36
49
|
this[key][field] = val
|
|
37
|
-
if (this.db)
|
|
50
|
+
if (this.db) {
|
|
51
|
+
if (key === "env") {
|
|
52
|
+
let memory = val.memory
|
|
53
|
+
try {
|
|
54
|
+
memory = Array.from(this.compressor.compress(val.memory))
|
|
55
|
+
} catch (e) {
|
|
56
|
+
memory = compress(val.memory)
|
|
57
|
+
}
|
|
58
|
+
await this.db.put(
|
|
59
|
+
`${key}.${field}`,
|
|
60
|
+
assoc("memory", memory, this[key][field]),
|
|
61
|
+
)
|
|
62
|
+
} else {
|
|
63
|
+
await this.db.put(`${key}.${field}`, this[key][field])
|
|
64
|
+
}
|
|
65
|
+
}
|
|
38
66
|
}
|
|
39
67
|
}
|
|
40
68
|
initSync() {
|
|
@@ -118,6 +146,9 @@ export default class ArMemBase {
|
|
|
118
146
|
async init() {
|
|
119
147
|
if (this.isInit) return
|
|
120
148
|
this.isInit = true
|
|
149
|
+
if (typeof this._init === "function") await this._init()
|
|
150
|
+
this.compressor = new this.Compressor()
|
|
151
|
+
this.decompressor = new this.Decompressor()
|
|
121
152
|
if (this.db) {
|
|
122
153
|
for (const v of ["height", "blocks"]) this[v] = await this.get(v)
|
|
123
154
|
for (const v of [
|
|
@@ -136,7 +167,21 @@ export default class ArMemBase {
|
|
|
136
167
|
for (const v2 of items || []) {
|
|
137
168
|
const key = v2.split(".")[0]
|
|
138
169
|
const field = v2.split(".")[1]
|
|
139
|
-
if (key === v)
|
|
170
|
+
if (key === v) {
|
|
171
|
+
if (key.match(/^env/)) {
|
|
172
|
+
let v3 = await this.db.get(v2)
|
|
173
|
+
if (is(Array, v3.memory)) {
|
|
174
|
+
try {
|
|
175
|
+
v3.memory = this.decompressor.decompress(v3.memory)
|
|
176
|
+
} catch (e) {
|
|
177
|
+
v3.memory = decompress(v3.memory)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
this[v][field] = v3
|
|
181
|
+
} else {
|
|
182
|
+
this[v][field] = await this.db.get(v2)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
140
185
|
}
|
|
141
186
|
}
|
|
142
187
|
} else {
|
|
@@ -163,9 +208,7 @@ export default class ArMemBase {
|
|
|
163
208
|
format = tags(tx.tags)["Module-Format"]
|
|
164
209
|
}
|
|
165
210
|
}
|
|
166
|
-
} else
|
|
167
|
-
format = _wasm.format
|
|
168
|
-
}
|
|
211
|
+
} else format = _wasm.format
|
|
169
212
|
format ??= "wasm64-unknown-emscripten-draft_2024_02_15"
|
|
170
213
|
return { format, mod, wasm }
|
|
171
214
|
}
|
package/esm/armem-web.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import wdb from "./lua/weavedb-lite.js"
|
|
2
2
|
import Base from "./armem-base.js"
|
|
3
3
|
import db from "./lfdb.js"
|
|
4
|
+
import init, { Compressor, Decompressor } from "./waosm/waosm.js"
|
|
4
5
|
|
|
5
6
|
export default class ArMem extends Base {
|
|
6
7
|
constructor(args = {}) {
|
|
7
|
-
super(args)
|
|
8
|
+
super({ ...args, init, Compressor, Decompressor })
|
|
8
9
|
this.db = db(this)
|
|
9
10
|
this.initSync()
|
|
10
11
|
}
|
package/esm/armem.js
CHANGED
|
@@ -5,11 +5,12 @@ import { open } from "lmdb"
|
|
|
5
5
|
import { readFileSync } from "fs"
|
|
6
6
|
import { resolve } from "path"
|
|
7
7
|
import Base from "./armem-base.js"
|
|
8
|
+
import { Compressor, Decompressor } from "./waosm-node.js"
|
|
8
9
|
|
|
9
10
|
export default class ArMem extends Base {
|
|
10
11
|
constructor(args = {}) {
|
|
11
12
|
const { cache } = args
|
|
12
|
-
super(args)
|
|
13
|
+
super({ ...args, Compressor, Decompressor })
|
|
13
14
|
if (cache) this.db = open({ path: cache, compression: true })
|
|
14
15
|
this.initSync()
|
|
15
16
|
}
|
package/esm/build.js
ADDED
package/esm/compress.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
function toBase256(n) {
|
|
2
|
+
let result = []
|
|
3
|
+
while (n > 0) {
|
|
4
|
+
result.unshift(n % 256)
|
|
5
|
+
n = Math.floor(n / 256)
|
|
6
|
+
}
|
|
7
|
+
return [result.length, ...result]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const _comp = (arr, _arr, mode) => {
|
|
11
|
+
let _nums = null
|
|
12
|
+
let _elms = null
|
|
13
|
+
let len = _arr.length
|
|
14
|
+
let flag = 0
|
|
15
|
+
if (mode === 1 && _arr[0] !== 0) {
|
|
16
|
+
flag += 100
|
|
17
|
+
_elms = [_arr[0]]
|
|
18
|
+
} else if (mode === 2) {
|
|
19
|
+
flag += 200
|
|
20
|
+
_elms = _arr
|
|
21
|
+
}
|
|
22
|
+
if (len < 10) flag += len
|
|
23
|
+
else if (len !== 10) {
|
|
24
|
+
const [elm, ...nums] = toBase256(len)
|
|
25
|
+
_nums = nums
|
|
26
|
+
flag += elm * 10
|
|
27
|
+
if (_nums[0] < 10) flag += _nums.shift()
|
|
28
|
+
}
|
|
29
|
+
arr.push(flag)
|
|
30
|
+
if (_nums) for (const n of _nums) arr.push(n)
|
|
31
|
+
if (_elms) for (const n of _elms) arr.push(n)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const compress = d => {
|
|
35
|
+
let _arr = []
|
|
36
|
+
let mode = 1
|
|
37
|
+
let prev = null
|
|
38
|
+
let max = 0
|
|
39
|
+
let arr = []
|
|
40
|
+
for (const v of d) {
|
|
41
|
+
if (prev === null) _arr.push(v)
|
|
42
|
+
else if (_arr.length === 1) {
|
|
43
|
+
mode = prev !== v ? 2 : 1
|
|
44
|
+
_arr.push(v)
|
|
45
|
+
} else {
|
|
46
|
+
if (prev !== v && mode === 1) {
|
|
47
|
+
_comp(arr, _arr, mode)
|
|
48
|
+
_arr = [v]
|
|
49
|
+
mode = 2
|
|
50
|
+
} else if (prev === v && mode === 2) {
|
|
51
|
+
const last = _arr.pop()
|
|
52
|
+
_comp(arr, _arr, mode)
|
|
53
|
+
_arr = [last, v]
|
|
54
|
+
mode = 1
|
|
55
|
+
} else {
|
|
56
|
+
_arr.push(v)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
prev = v
|
|
60
|
+
}
|
|
61
|
+
if (_arr.length > 0) _comp(arr, _arr, mode)
|
|
62
|
+
return arr
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const decompress = d => {
|
|
66
|
+
let arr = []
|
|
67
|
+
let flag = null
|
|
68
|
+
let elm = 0
|
|
69
|
+
let sum = 0
|
|
70
|
+
let nums = []
|
|
71
|
+
let first = null
|
|
72
|
+
let second = null
|
|
73
|
+
let third = null
|
|
74
|
+
for (let v of d) {
|
|
75
|
+
if (flag === null) {
|
|
76
|
+
flag = v
|
|
77
|
+
first = Math.floor(flag / 100)
|
|
78
|
+
second = Math.floor((flag - first * 100) / 10)
|
|
79
|
+
third = flag - first * 100 - second * 10
|
|
80
|
+
nums = []
|
|
81
|
+
if (second === 0) {
|
|
82
|
+
nums.push(third === 0 ? 10 : third)
|
|
83
|
+
elm = 0
|
|
84
|
+
} else if (third !== 0) {
|
|
85
|
+
nums.push(third)
|
|
86
|
+
elm = second - 1
|
|
87
|
+
} else elm = second
|
|
88
|
+
console.log(flag, first, second, third, elm, nums)
|
|
89
|
+
} else if (sum === 0) {
|
|
90
|
+
elm -= 1
|
|
91
|
+
nums.push(v)
|
|
92
|
+
}
|
|
93
|
+
if (sum > 0) {
|
|
94
|
+
if (first === 2) arr.push(v)
|
|
95
|
+
else if (first === 1) for (let i = 0; i < to10(nums); i++) arr.push(v)
|
|
96
|
+
sum -= 1
|
|
97
|
+
if (sum === 0) flag = null
|
|
98
|
+
} else if (elm === 0) {
|
|
99
|
+
if (first === 0) {
|
|
100
|
+
for (let i = 0; i < to10(nums); i++) arr.push(0)
|
|
101
|
+
flag = null
|
|
102
|
+
} else if (first === 2) sum = to10(nums)
|
|
103
|
+
else sum = 1
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return new Uint8Array(arr)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function to10(arr) {
|
|
110
|
+
let num = 0
|
|
111
|
+
for (let i = 0; i < arr.length; i++) num = num * 256 + arr[i]
|
|
112
|
+
return num
|
|
113
|
+
}
|
package/esm/lfdb.js
CHANGED
|
@@ -4,6 +4,7 @@ import { is, keys } from "ramda"
|
|
|
4
4
|
export default _this => {
|
|
5
5
|
return {
|
|
6
6
|
put: async (key, val) => {
|
|
7
|
+
console.log(key, val)
|
|
7
8
|
let _val = null
|
|
8
9
|
if (typeof val === "object" && !is(Array, val)) {
|
|
9
10
|
_val = {}
|
|
@@ -48,9 +49,7 @@ export default _this => {
|
|
|
48
49
|
_this.keys[key] = true
|
|
49
50
|
await lf.setItem("keys", keys(_this.keys))
|
|
50
51
|
},
|
|
51
|
-
get: async key =>
|
|
52
|
-
return await lf.getItem(key)
|
|
53
|
-
},
|
|
52
|
+
get: async key => await lf.getItem(key),
|
|
54
53
|
getKeys: async ({ start, end }) => {
|
|
55
54
|
if (!_this.keyInit) {
|
|
56
55
|
const _keys = (await lf.getItem("keys")) ?? []
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Waosm Compressor
|
|
2
|
+
|
|
3
|
+
Waosm is a lightweight new compression algorithm to efficiently compress Wasm memory.
|
|
4
|
+
|
|
5
|
+
It is implemented in Rust and compiled to Wasm for better performance in browsers.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
yarn build
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Using Waosm in nodejs.
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import init, { Compressor, Decompressor } from "../src/waosm-node.js"
|
|
15
|
+
|
|
16
|
+
const main = async ()=>{
|
|
17
|
+
await init()
|
|
18
|
+
const compressor = new Compressor()
|
|
19
|
+
const decompressor = new Decompressor()
|
|
20
|
+
const memory = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
|
|
21
|
+
console.log(memory)
|
|
22
|
+
const compressed = compressor.compress(memory)
|
|
23
|
+
console.log(compressed)
|
|
24
|
+
const decompressed = decompressor.decompress(compressed)
|
|
25
|
+
console.log(decompressed)
|
|
26
|
+
}
|
|
27
|
+
main()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## How Compression Works
|
|
31
|
+
|
|
32
|
+
Waosm Compression is a simple LRF variant that sequentially processes 8-bit integers. It is specifically optimized for zero-padded Wasm memory, achieving a 99% compression rate for largely idle WebAssembly memory. This enables efficient handling of large WebAssembly modules in browser environments.
|
|
33
|
+
|
|
34
|
+
The algorithm packs integers into chunks with 3 parts.
|
|
35
|
+
|
|
36
|
+
- flag
|
|
37
|
+
- 256-base number to tell the number of element in the current chunk
|
|
38
|
+
- actuall 8-bit intergers
|
|
39
|
+
|
|
40
|
+
### Flag
|
|
41
|
+
|
|
42
|
+
A flag is a 1 byte (8 bits) integer, which ranges from 0 to 255. So there are 3 digits.
|
|
43
|
+
|
|
44
|
+
- 1st digit: `mode`
|
|
45
|
+
- `0`: a sequence of 0s
|
|
46
|
+
- `1`: a sequence of a single repeating 8 bit integer
|
|
47
|
+
- `2`: a sequence of multiple unique 8 bit integer
|
|
48
|
+
- 2nd digit:
|
|
49
|
+
- `0`: the 3rd digit is the number of elements, no 256-base number required in this case
|
|
50
|
+
- `1-9`: the number of integers to represent the 256-base number
|
|
51
|
+
- 3rd digit:
|
|
52
|
+
- if the 2nd digit is `0`, the 3rd digit represents the number of the elements, `0` represents `10`
|
|
53
|
+
- otherwise, the 3rd digit represents the first integer in the 256-base number representation
|
|
54
|
+
|
|
55
|
+
### 256-Base Number
|
|
56
|
+
|
|
57
|
+
This part tells how many 8-bit integers are in the current chunk. For instance,
|
|
58
|
+
- `2, 200` = (256 ** 1 * 2) + 200 = 712
|
|
59
|
+
- `5, 50, 200` = (256 ** 2 * 5) + (256 ** 1 * 50) + 200 = 340680
|
|
60
|
+
|
|
61
|
+
Note that for space efficiency, the first integer is in the flag in some cases.
|
|
62
|
+
|
|
63
|
+
### 8 Bit Elements
|
|
64
|
+
|
|
65
|
+
- `Mode 0`: no need for this part since we know it's a sequence of 0
|
|
66
|
+
- `Mode 1`: 1 integer to tell which integer of a sequence this chunk is
|
|
67
|
+
- `Mode 2`: the entire integers are required
|
|
68
|
+
|
|
69
|
+
## Examples
|
|
70
|
+
|
|
71
|
+
`[ 1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]` (28 bytes)
|
|
72
|
+
|
|
73
|
+
- `204` | `-` | `1, 2, 3, 4`
|
|
74
|
+
- `103` | `-` | `5`
|
|
75
|
+
- `204` | `-` | `6, 7, 8, 9`
|
|
76
|
+
- `010` | `17` | `-`
|
|
77
|
+
|
|
78
|
+
It becomes `[204, 1, 2, 3, 4, 103, 5, 204, 6, 7, 8, 9, 10, 17]` (14 bytes - 50% compression rate).
|
|
79
|
+
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export class Compressor {
|
|
4
|
+
free(): void;
|
|
5
|
+
constructor();
|
|
6
|
+
compress(data: Uint8Array): Uint8Array;
|
|
7
|
+
}
|
|
8
|
+
export class Decompressor {
|
|
9
|
+
free(): void;
|
|
10
|
+
constructor();
|
|
11
|
+
decompress(data: Uint8Array): Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
15
|
+
|
|
16
|
+
export interface InitOutput {
|
|
17
|
+
readonly memory: WebAssembly.Memory;
|
|
18
|
+
readonly __wbg_compressor_free: (a: number, b: number) => void;
|
|
19
|
+
readonly compressor_new: () => number;
|
|
20
|
+
readonly compressor_compress: (a: number, b: number, c: number) => number;
|
|
21
|
+
readonly decompressor_decompress: (a: number, b: number, c: number) => number;
|
|
22
|
+
readonly __wbg_decompressor_free: (a: number, b: number) => void;
|
|
23
|
+
readonly decompressor_new: () => number;
|
|
24
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
28
|
+
/**
|
|
29
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
30
|
+
* a precompiled `WebAssembly.Module`.
|
|
31
|
+
*
|
|
32
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
33
|
+
*
|
|
34
|
+
* @returns {InitOutput}
|
|
35
|
+
*/
|
|
36
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
40
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
41
|
+
*
|
|
42
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<InitOutput>}
|
|
45
|
+
*/
|
|
46
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|