wao 0.6.0 → 0.6.2
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 +102 -81
- package/cjs/aoconnect.js +64 -60
- package/cjs/helpers.js +4 -12
- package/cjs/lua/weavedb_mock.lua +43 -0
- package/cjs/server.js +3 -1
- package/cjs/tao.js +3 -1
- package/cjs/utils.js +17 -1
- package/cjs/weavedb.js +447 -0
- package/cjs/weavedrive.js +240 -591
- package/esm/ao.js +25 -13
- package/esm/aoconnect.js +8 -7
- package/esm/helpers.js +4 -12
- package/esm/lua/weavedb_mock.lua +43 -0
- package/esm/server.js +1 -1
- package/esm/tao.js +1 -1
- package/esm/utils.js +14 -0
- package/esm/weavedb.js +309 -0
- package/esm/weavedrive.js +75 -309
- package/package.json +1 -1
package/cjs/weavedb.js
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
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
|
+
exports["default"] = void 0;
|
|
8
|
+
var _path = require("path");
|
|
9
|
+
var _fs = require("fs");
|
|
10
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
11
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
12
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
13
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
14
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
15
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
16
|
+
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); } }
|
|
17
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
18
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
19
|
+
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); }
|
|
20
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
21
|
+
var KB = 1024;
|
|
22
|
+
var MB = KB * 1024;
|
|
23
|
+
var CACHE_SZ = 32 * KB;
|
|
24
|
+
var CHUNK_SZ = 128 * MB;
|
|
25
|
+
var NOTIFY_SZ = 512 * MB;
|
|
26
|
+
var log = console.log;
|
|
27
|
+
var rand = Math.floor(Math.random() * 1000000).toString();
|
|
28
|
+
var WeaveDB = exports["default"] = /*#__PURE__*/_createClass(function WeaveDB(ar, dir) {
|
|
29
|
+
var _dir;
|
|
30
|
+
_classCallCheck(this, WeaveDB);
|
|
31
|
+
(_dir = dir) !== null && _dir !== void 0 ? _dir : dir = (0, _path.resolve)(import.meta.dirname, ".db");
|
|
32
|
+
var kv_dir = (0, _path.resolve)(dir, rand);
|
|
33
|
+
var data_dir = (0, _path.resolve)(kv_dir, "data");
|
|
34
|
+
var rollup_dir = (0, _path.resolve)(kv_dir, "rollup");
|
|
35
|
+
for (var _i = 0, _arr = [dir, kv_dir, rollup_dir, data_dir]; _i < _arr.length; _i++) {
|
|
36
|
+
var v = _arr[_i];
|
|
37
|
+
if (!(0, _fs.existsSync)(v)) (0, _fs.mkdirSync)(v);
|
|
38
|
+
}
|
|
39
|
+
this.ext = function (mod, FS) {
|
|
40
|
+
var cache = {
|
|
41
|
+
data: {},
|
|
42
|
+
rollup: {}
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
create: function create(id) {
|
|
46
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
47
|
+
var _data$length;
|
|
48
|
+
var properties, node, data, bytesLength, stream;
|
|
49
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
50
|
+
while (1) switch (_context.prev = _context.next) {
|
|
51
|
+
case 0:
|
|
52
|
+
properties = {
|
|
53
|
+
isDevice: false,
|
|
54
|
+
contents: null
|
|
55
|
+
};
|
|
56
|
+
if (!FS.analyzePath("/rollup/").exists) FS.mkdir("/rollup/");
|
|
57
|
+
node = FS.createFile("/", "rollup/" + id, properties, true, false); // Set initial parame
|
|
58
|
+
_context.next = 5;
|
|
59
|
+
return ar.data(id);
|
|
60
|
+
case 5:
|
|
61
|
+
data = _context.sent;
|
|
62
|
+
bytesLength = (_data$length = data === null || data === void 0 ? void 0 : data.length) !== null && _data$length !== void 0 ? _data$length : 0;
|
|
63
|
+
node.total_size = Number(bytesLength);
|
|
64
|
+
node.cache = new Uint8Array(0);
|
|
65
|
+
node.position = 0;
|
|
66
|
+
|
|
67
|
+
// Add a function that defers querying the file size until it is asked the first time.
|
|
68
|
+
Object.defineProperties(node, {
|
|
69
|
+
usedBytes: {
|
|
70
|
+
get: function get() {
|
|
71
|
+
return bytesLength;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Now we have created the file in the emscripten FS, we can open it as a stream
|
|
77
|
+
stream = FS.open("/rollup/" + id, "r"); //console.log("JS: Created file: ", id, " fd: ", stream.fd);
|
|
78
|
+
return _context.abrupt("return", stream);
|
|
79
|
+
case 13:
|
|
80
|
+
case "end":
|
|
81
|
+
return _context.stop();
|
|
82
|
+
}
|
|
83
|
+
}, _callee);
|
|
84
|
+
}))();
|
|
85
|
+
},
|
|
86
|
+
createData: function createData(col, doc, val) {
|
|
87
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
88
|
+
var _data$length2, _data2;
|
|
89
|
+
var properties, node, data, _readFileSync, col_dir, _data, bytesLength, stream;
|
|
90
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
91
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
92
|
+
case 0:
|
|
93
|
+
properties = {
|
|
94
|
+
isDevice: false,
|
|
95
|
+
contents: null
|
|
96
|
+
};
|
|
97
|
+
if (!FS.analyzePath("/data/").exists) FS.mkdir("/data/");
|
|
98
|
+
if (!FS.analyzePath("/data/".concat(col)).exists) FS.mkdir("/data/".concat(col));
|
|
99
|
+
node = FS.createFile("/", "data/".concat(col, "/").concat(doc), properties, true, false); // Set initial parame
|
|
100
|
+
data = val;
|
|
101
|
+
if (!val) {
|
|
102
|
+
col_dir = (0, _path.resolve)(data_dir, col);
|
|
103
|
+
_data = (_readFileSync = (0, _fs.readFileSync)((0, _path.resolve)(col_dir, "".concat(doc, ".json")), "utf8")) !== null && _readFileSync !== void 0 ? _readFileSync : "";
|
|
104
|
+
data = Buffer.from(_data, "utf8");
|
|
105
|
+
}
|
|
106
|
+
bytesLength = (_data$length2 = (_data2 = data) === null || _data2 === void 0 ? void 0 : _data2.length) !== null && _data$length2 !== void 0 ? _data$length2 : 0;
|
|
107
|
+
node.total_size = Number(bytesLength);
|
|
108
|
+
node.cache = new Uint8Array(0);
|
|
109
|
+
node.position = 0;
|
|
110
|
+
|
|
111
|
+
// Add a function that defers querying the file size until it is asked the first time.
|
|
112
|
+
Object.defineProperties(node, {
|
|
113
|
+
usedBytes: {
|
|
114
|
+
get: function get() {
|
|
115
|
+
return bytesLength;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Now we have created the file in the emscripten FS, we can open it as a stream
|
|
121
|
+
stream = FS.open("/data/" + "".concat(col, "/").concat(doc), "r"); //console.log("JS: Created file: ", id, " fd: ", stream.fd);
|
|
122
|
+
return _context2.abrupt("return", stream);
|
|
123
|
+
case 13:
|
|
124
|
+
case "end":
|
|
125
|
+
return _context2.stop();
|
|
126
|
+
}
|
|
127
|
+
}, _callee2);
|
|
128
|
+
}))();
|
|
129
|
+
},
|
|
130
|
+
open: function open(filename, val) {
|
|
131
|
+
var _this = this;
|
|
132
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
|
|
133
|
+
var pathCategory, id, stream, _stream, col, doc, _stream2, _stream3;
|
|
134
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
135
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
136
|
+
case 0:
|
|
137
|
+
pathCategory = filename.split("/")[1];
|
|
138
|
+
if (!(pathCategory === "rollup")) {
|
|
139
|
+
_context3.next = 17;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
//log("JS: Opening ID: ", id)
|
|
143
|
+
id = filename.split("/")[2];
|
|
144
|
+
if (!FS.analyzePath(filename).exists) {
|
|
145
|
+
_context3.next = 11;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
stream = FS.open(filename, "r");
|
|
149
|
+
if (!stream.fd) {
|
|
150
|
+
_context3.next = 7;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
return _context3.abrupt("return", stream.fd);
|
|
154
|
+
case 7:
|
|
155
|
+
console.log("JS: File not found: ", filename);
|
|
156
|
+
return _context3.abrupt("return", 0);
|
|
157
|
+
case 11:
|
|
158
|
+
_context3.next = 13;
|
|
159
|
+
return _this.create(id);
|
|
160
|
+
case 13:
|
|
161
|
+
_stream = _context3.sent;
|
|
162
|
+
return _context3.abrupt("return", _stream.fd);
|
|
163
|
+
case 15:
|
|
164
|
+
_context3.next = 36;
|
|
165
|
+
break;
|
|
166
|
+
case 17:
|
|
167
|
+
if (!(pathCategory === "data")) {
|
|
168
|
+
_context3.next = 34;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
//log("JS: Opening ID: ", id)
|
|
172
|
+
col = filename.split("/")[2];
|
|
173
|
+
doc = filename.split("/")[3];
|
|
174
|
+
if (!FS.analyzePath(filename).exists) {
|
|
175
|
+
_context3.next = 28;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
_stream2 = FS.open(filename, "r");
|
|
179
|
+
if (!_stream2.fd) {
|
|
180
|
+
_context3.next = 24;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
return _context3.abrupt("return", _stream2.fd);
|
|
184
|
+
case 24:
|
|
185
|
+
console.log("JS: File not found: ", filename);
|
|
186
|
+
return _context3.abrupt("return", 0);
|
|
187
|
+
case 28:
|
|
188
|
+
_context3.next = 30;
|
|
189
|
+
return _this.createData(col, doc, val);
|
|
190
|
+
case 30:
|
|
191
|
+
_stream3 = _context3.sent;
|
|
192
|
+
return _context3.abrupt("return", _stream3.fd);
|
|
193
|
+
case 32:
|
|
194
|
+
_context3.next = 36;
|
|
195
|
+
break;
|
|
196
|
+
case 34:
|
|
197
|
+
console.log("JS: Invalid path category: ", pathCategory);
|
|
198
|
+
return _context3.abrupt("return", 0);
|
|
199
|
+
case 36:
|
|
200
|
+
case "end":
|
|
201
|
+
return _context3.stop();
|
|
202
|
+
}
|
|
203
|
+
}, _callee3);
|
|
204
|
+
}))();
|
|
205
|
+
},
|
|
206
|
+
read: function read(fd, raw_dst_ptr, raw_length, val) {
|
|
207
|
+
var _this2 = this;
|
|
208
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
209
|
+
var to_read, dst_ptr, stream, i, bytes_read, chunk_download_sz, to, data, sp, _readFileSync2, col_dir, _data, json, _iterator, _step, _v, _val, _fd, _col_dir, start, end, chunk, response, reader, bytes_until_cache, bytes_until_notify, downloaded_bytes, cache_chunks, _yield$reader$read, done, chunk_bytes, write_length, chunk_to_cache;
|
|
210
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
211
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
212
|
+
case 0:
|
|
213
|
+
to_read = Number(raw_length);
|
|
214
|
+
dst_ptr = Number(raw_dst_ptr);
|
|
215
|
+
stream = 0;
|
|
216
|
+
for (i = 0; i < FS.streams.length; i++) {
|
|
217
|
+
if (FS.streams[i].fd === fd) stream = FS.streams[i];
|
|
218
|
+
}
|
|
219
|
+
// Satisfy what we can with the cache first
|
|
220
|
+
bytes_read = _this2.readFromCache(stream, dst_ptr, to_read);
|
|
221
|
+
stream.position += bytes_read;
|
|
222
|
+
stream.lastReadPosition = stream.position;
|
|
223
|
+
dst_ptr += bytes_read;
|
|
224
|
+
to_read -= bytes_read;
|
|
225
|
+
|
|
226
|
+
// Return if we have satisfied the request
|
|
227
|
+
|
|
228
|
+
//console.log("KV: Satisfied request with cache. Returning...")
|
|
229
|
+
if (!(to_read === 0)) {
|
|
230
|
+
_context4.next = 11;
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
return _context4.abrupt("return", bytes_read);
|
|
234
|
+
case 11:
|
|
235
|
+
//console.log("KV: Read from cache: ", bytes_read, " Remaining to read: ", to_read)
|
|
236
|
+
chunk_download_sz = Math.max(to_read, CACHE_SZ);
|
|
237
|
+
to = Math.min(stream.node.total_size, stream.position + chunk_download_sz);
|
|
238
|
+
data = val;
|
|
239
|
+
if (data) {
|
|
240
|
+
_context4.next = 56;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
sp = stream.path.split("/");
|
|
244
|
+
if (!(sp[1] === "data")) {
|
|
245
|
+
_context4.next = 22;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
col_dir = (0, _path.resolve)(data_dir, sp[2]);
|
|
249
|
+
_data = (_readFileSync2 = (0, _fs.readFileSync)((0, _path.resolve)(col_dir, "".concat(sp[3], ".json")), "utf8")) !== null && _readFileSync2 !== void 0 ? _readFileSync2 : "";
|
|
250
|
+
data = Buffer.from(_data, "utf8");
|
|
251
|
+
_context4.next = 56;
|
|
252
|
+
break;
|
|
253
|
+
case 22:
|
|
254
|
+
_context4.next = 24;
|
|
255
|
+
return ar.data(stream.node.name);
|
|
256
|
+
case 24:
|
|
257
|
+
data = _context4.sent;
|
|
258
|
+
_context4.prev = 25;
|
|
259
|
+
json = JSON.parse(Buffer.from(data).toString());
|
|
260
|
+
_iterator = _createForOfIteratorHelper(json.diffs);
|
|
261
|
+
_context4.prev = 28;
|
|
262
|
+
_iterator.s();
|
|
263
|
+
case 30:
|
|
264
|
+
if ((_step = _iterator.n()).done) {
|
|
265
|
+
_context4.next = 43;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
_v = _step.value;
|
|
269
|
+
_val = Buffer.from(JSON.stringify(_v.data));
|
|
270
|
+
_context4.next = 35;
|
|
271
|
+
return _this2.open("/data/".concat(_v.collection, "/").concat(_v.doc), _val);
|
|
272
|
+
case 35:
|
|
273
|
+
_fd = _context4.sent;
|
|
274
|
+
_col_dir = (0, _path.resolve)(data_dir, _v.collection);
|
|
275
|
+
if (!(0, _fs.existsSync)(_col_dir)) (0, _fs.mkdirSync)(_col_dir);
|
|
276
|
+
(0, _fs.writeFileSync)((0, _path.resolve)(_col_dir, "".concat(_v.doc, ".json")), JSON.stringify(_v.data));
|
|
277
|
+
_context4.next = 41;
|
|
278
|
+
return _this2.read(_fd, _fd, _val.length, _val);
|
|
279
|
+
case 41:
|
|
280
|
+
_context4.next = 30;
|
|
281
|
+
break;
|
|
282
|
+
case 43:
|
|
283
|
+
_context4.next = 48;
|
|
284
|
+
break;
|
|
285
|
+
case 45:
|
|
286
|
+
_context4.prev = 45;
|
|
287
|
+
_context4.t0 = _context4["catch"](28);
|
|
288
|
+
_iterator.e(_context4.t0);
|
|
289
|
+
case 48:
|
|
290
|
+
_context4.prev = 48;
|
|
291
|
+
_iterator.f();
|
|
292
|
+
return _context4.finish(48);
|
|
293
|
+
case 51:
|
|
294
|
+
_context4.next = 56;
|
|
295
|
+
break;
|
|
296
|
+
case 53:
|
|
297
|
+
_context4.prev = 53;
|
|
298
|
+
_context4.t1 = _context4["catch"](25);
|
|
299
|
+
log(_context4.t1);
|
|
300
|
+
case 56:
|
|
301
|
+
// Extract the Range header to determine the start and end of the requested chunk
|
|
302
|
+
start = 0;
|
|
303
|
+
end = data.length; // Create a ReadableStream for the requested chunk
|
|
304
|
+
chunk = data.subarray(start, end);
|
|
305
|
+
response = new Response(new ReadableStream({
|
|
306
|
+
start: function start(controller) {
|
|
307
|
+
controller.enqueue(chunk); // Push the chunk to the stream
|
|
308
|
+
controller.close(); // Close the stream when done
|
|
309
|
+
}
|
|
310
|
+
}), {
|
|
311
|
+
headers: {
|
|
312
|
+
"Content-Length": chunk.length.toString()
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
reader = response.body.getReader();
|
|
316
|
+
bytes_until_cache = CHUNK_SZ;
|
|
317
|
+
bytes_until_notify = NOTIFY_SZ;
|
|
318
|
+
downloaded_bytes = 0;
|
|
319
|
+
cache_chunks = [];
|
|
320
|
+
_context4.prev = 65;
|
|
321
|
+
case 66:
|
|
322
|
+
if (!true) {
|
|
323
|
+
_context4.next = 84;
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
_context4.next = 69;
|
|
327
|
+
return reader.read();
|
|
328
|
+
case 69:
|
|
329
|
+
_yield$reader$read = _context4.sent;
|
|
330
|
+
done = _yield$reader$read.done;
|
|
331
|
+
chunk_bytes = _yield$reader$read.value;
|
|
332
|
+
if (!done) {
|
|
333
|
+
_context4.next = 74;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
return _context4.abrupt("break", 84);
|
|
337
|
+
case 74:
|
|
338
|
+
// Update the number of downloaded bytes to be _all_, not just the write length
|
|
339
|
+
downloaded_bytes += chunk_bytes.length;
|
|
340
|
+
bytes_until_cache -= chunk_bytes.length;
|
|
341
|
+
bytes_until_notify -= chunk_bytes.length;
|
|
342
|
+
|
|
343
|
+
// Write bytes from the chunk and update the pointer if necessary
|
|
344
|
+
write_length = Math.min(chunk_bytes.length, to_read);
|
|
345
|
+
if (write_length > 0) {
|
|
346
|
+
//console.log("KV: Writing: ", write_length, " bytes to: ", dst_ptr)
|
|
347
|
+
mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr);
|
|
348
|
+
dst_ptr += write_length;
|
|
349
|
+
bytes_read += write_length;
|
|
350
|
+
stream.position += write_length;
|
|
351
|
+
to_read -= write_length;
|
|
352
|
+
}
|
|
353
|
+
if (to_read == 0) {
|
|
354
|
+
// Add excess bytes to our cache
|
|
355
|
+
chunk_to_cache = chunk_bytes.subarray(write_length); //console.log("KV: Cacheing excess: ", chunk_to_cache.length)
|
|
356
|
+
cache_chunks.push(chunk_to_cache);
|
|
357
|
+
}
|
|
358
|
+
if (bytes_until_cache <= 0) {
|
|
359
|
+
console.log("KV: Chunk size reached. Compressing cache...");
|
|
360
|
+
stream.node.cache = _this2.addChunksToCache(stream.node.cache, cache_chunks);
|
|
361
|
+
cache_chunks = [];
|
|
362
|
+
bytes_until_cache = CHUNK_SZ;
|
|
363
|
+
}
|
|
364
|
+
if (bytes_until_notify <= 0) {
|
|
365
|
+
console.log("KV: Downloaded: ", downloaded_bytes / stream.node.total_size * 100, "%");
|
|
366
|
+
bytes_until_notify = NOTIFY_SZ;
|
|
367
|
+
}
|
|
368
|
+
_context4.next = 66;
|
|
369
|
+
break;
|
|
370
|
+
case 84:
|
|
371
|
+
_context4.next = 89;
|
|
372
|
+
break;
|
|
373
|
+
case 86:
|
|
374
|
+
_context4.prev = 86;
|
|
375
|
+
_context4.t2 = _context4["catch"](65);
|
|
376
|
+
console.error("KV: Error reading the stream: ", _context4.t2);
|
|
377
|
+
case 89:
|
|
378
|
+
_context4.prev = 89;
|
|
379
|
+
reader.releaseLock();
|
|
380
|
+
return _context4.finish(89);
|
|
381
|
+
case 92:
|
|
382
|
+
// If we have no cache, or we have not satisfied the full request, we need to download the rest
|
|
383
|
+
// Rebuild the cache from the new cache chunks
|
|
384
|
+
stream.node.cache = _this2.addChunksToCache(stream.node.cache, cache_chunks);
|
|
385
|
+
|
|
386
|
+
// Update the last read position
|
|
387
|
+
stream.lastReadPosition = stream.position;
|
|
388
|
+
return _context4.abrupt("return", bytes_read);
|
|
389
|
+
case 95:
|
|
390
|
+
case "end":
|
|
391
|
+
return _context4.stop();
|
|
392
|
+
}
|
|
393
|
+
}, _callee4, null, [[25, 53], [28, 45, 48, 51], [65, 86, 89, 92]]);
|
|
394
|
+
}))();
|
|
395
|
+
},
|
|
396
|
+
close: function close(fd) {
|
|
397
|
+
var stream = 0;
|
|
398
|
+
for (var i = 0; i < FS.streams.length; i++) {
|
|
399
|
+
if (FS.streams[i].fd === fd) stream = FS.streams[i];
|
|
400
|
+
}
|
|
401
|
+
FS.close(stream);
|
|
402
|
+
},
|
|
403
|
+
readFromCache: function readFromCache(stream, dst_ptr, length) {
|
|
404
|
+
// Check if the cache has been invalidated by a seek
|
|
405
|
+
if (stream.lastReadPosition !== stream.position) {
|
|
406
|
+
//console.log("KV: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
|
|
407
|
+
stream.node.cache = new Uint8Array(0);
|
|
408
|
+
return 0;
|
|
409
|
+
}
|
|
410
|
+
// Calculate the bytes of the request that can be satisfied with the cache
|
|
411
|
+
var cache_part_length = Math.min(length, stream.node.cache.length);
|
|
412
|
+
var cache_part = stream.node.cache.subarray(0, cache_part_length);
|
|
413
|
+
mod.HEAP8.set(cache_part, dst_ptr);
|
|
414
|
+
// Set the new cache to the remainder of the unused cache and update pointers
|
|
415
|
+
stream.node.cache = stream.node.cache.subarray(cache_part_length);
|
|
416
|
+
return cache_part_length;
|
|
417
|
+
},
|
|
418
|
+
addChunksToCache: function addChunksToCache(old_cache, chunks) {
|
|
419
|
+
// Make a new cache array of the old cache length + the sum of the chunk lengths, capped by the max cache size
|
|
420
|
+
var new_cache_length = Math.min(old_cache.length + chunks.reduce(function (acc, chunk) {
|
|
421
|
+
return acc + chunk.length;
|
|
422
|
+
}, 0), CACHE_SZ);
|
|
423
|
+
var new_cache = new Uint8Array(new_cache_length);
|
|
424
|
+
// Copy the old cache to the new cache
|
|
425
|
+
new_cache.set(old_cache, 0);
|
|
426
|
+
// Load the cache chunks into the new cache
|
|
427
|
+
var current_offset = old_cache.length;
|
|
428
|
+
var _iterator2 = _createForOfIteratorHelper(chunks),
|
|
429
|
+
_step2;
|
|
430
|
+
try {
|
|
431
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
432
|
+
var chunk = _step2.value;
|
|
433
|
+
if (current_offset < new_cache_length) {
|
|
434
|
+
new_cache.set(chunk.subarray(0, new_cache_length - current_offset), current_offset);
|
|
435
|
+
current_offset += chunk.length;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
} catch (err) {
|
|
439
|
+
_iterator2.e(err);
|
|
440
|
+
} finally {
|
|
441
|
+
_iterator2.f();
|
|
442
|
+
}
|
|
443
|
+
return new_cache;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
});
|