wao 0.3.1 → 0.4.0
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/accounts.js +12 -12
- package/cjs/aoconnect.js +410 -1475
- package/cjs/ar.js +1 -1
- package/cjs/armem.js +52 -0
- package/cjs/gql.js +393 -0
- package/cjs/index.js +7 -0
- package/cjs/tao.js +9 -9
- package/cjs/tar.js +279 -0
- package/cjs/test.js +57 -3
- package/cjs/utils.js +41 -1
- package/cjs/weavedrive.js +979 -0
- package/esm/accounts.js +36 -30
- package/esm/aoconnect.js +65 -808
- package/esm/ar.js +1 -1
- package/esm/armem.js +38 -0
- package/esm/gql.js +218 -0
- package/esm/index.js +2 -1
- package/esm/tao.js +7 -9
- package/esm/tar.js +98 -0
- package/esm/test.js +13 -2
- package/esm/utils.js +10 -0
- package/esm/weavedrive.js +709 -0
- package/package.json +1 -1
|
@@ -0,0 +1,979 @@
|
|
|
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 _arweave = _interopRequireDefault(require("arweave"));
|
|
9
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
11
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
12
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
13
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
14
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
15
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
16
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
17
|
+
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; }
|
|
18
|
+
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; } } }; }
|
|
19
|
+
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; } }
|
|
20
|
+
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; }
|
|
21
|
+
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); }
|
|
22
|
+
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); }); }; }
|
|
23
|
+
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); } }
|
|
24
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
25
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
26
|
+
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); }
|
|
27
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
28
|
+
var KB = 1024;
|
|
29
|
+
var MB = KB * 1024;
|
|
30
|
+
var CACHE_SZ = 32 * KB;
|
|
31
|
+
var CHUNK_SZ = 128 * MB;
|
|
32
|
+
var NOTIFY_SZ = 512 * MB;
|
|
33
|
+
var WeaveDrive = exports["default"] = /*#__PURE__*/_createClass(function WeaveDrive(ar) {
|
|
34
|
+
_classCallCheck(this, WeaveDrive);
|
|
35
|
+
this.drive = function WeaveDrive(mod, FS) {
|
|
36
|
+
return {
|
|
37
|
+
reset: function reset(fd) {
|
|
38
|
+
//console.log("WeaveDrive: Resetting fd: ", fd)
|
|
39
|
+
FS.streams[fd].node.position = 0;
|
|
40
|
+
FS.streams[fd].node.cache = new Uint8Array(0);
|
|
41
|
+
},
|
|
42
|
+
joinUrl: function joinUrl(_ref) {
|
|
43
|
+
var url = _ref.url,
|
|
44
|
+
path = _ref.path;
|
|
45
|
+
if (!path) return url;
|
|
46
|
+
if (path.startsWith("/")) return this.joinUrl({
|
|
47
|
+
url: url,
|
|
48
|
+
path: path.slice(1)
|
|
49
|
+
});
|
|
50
|
+
url = new URL(url);
|
|
51
|
+
url.pathname += path;
|
|
52
|
+
return url.toString();
|
|
53
|
+
},
|
|
54
|
+
customFetch: function customFetch(path, options) {
|
|
55
|
+
var _this = this;
|
|
56
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
57
|
+
var urlList, p, _iterator, _step, url, res;
|
|
58
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
59
|
+
while (1) switch (_context.prev = _context.next) {
|
|
60
|
+
case 0:
|
|
61
|
+
/**
|
|
62
|
+
* mod.ARWEAVE may be a comma-delimited list of urls.
|
|
63
|
+
* So we parse it into an array that we sequentially consume
|
|
64
|
+
* using fetch, and return the first successful response.
|
|
65
|
+
*
|
|
66
|
+
* The first url is considered "primary". So if all urls fail
|
|
67
|
+
* to produce a successful response, then we return the primary's
|
|
68
|
+
* error response
|
|
69
|
+
*/
|
|
70
|
+
urlList = mod.ARWEAVE.includes(",") ? mod.ARWEAVE.split(",").map(function (url) {
|
|
71
|
+
return url.trim();
|
|
72
|
+
}) : [mod.ARWEAVE];
|
|
73
|
+
_iterator = _createForOfIteratorHelper(urlList);
|
|
74
|
+
_context.prev = 2;
|
|
75
|
+
_iterator.s();
|
|
76
|
+
case 4:
|
|
77
|
+
if ((_step = _iterator.n()).done) {
|
|
78
|
+
_context.next = 14;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
url = _step.value;
|
|
82
|
+
res = fetch(_this.joinUrl({
|
|
83
|
+
url: url,
|
|
84
|
+
path: path
|
|
85
|
+
}), options);
|
|
86
|
+
_context.next = 9;
|
|
87
|
+
return res.then(function (r) {
|
|
88
|
+
return r.ok;
|
|
89
|
+
})["catch"](function () {
|
|
90
|
+
return false;
|
|
91
|
+
});
|
|
92
|
+
case 9:
|
|
93
|
+
if (!_context.sent) {
|
|
94
|
+
_context.next = 11;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
return _context.abrupt("return", res);
|
|
98
|
+
case 11:
|
|
99
|
+
if (!p) p = res;
|
|
100
|
+
case 12:
|
|
101
|
+
_context.next = 4;
|
|
102
|
+
break;
|
|
103
|
+
case 14:
|
|
104
|
+
_context.next = 19;
|
|
105
|
+
break;
|
|
106
|
+
case 16:
|
|
107
|
+
_context.prev = 16;
|
|
108
|
+
_context.t0 = _context["catch"](2);
|
|
109
|
+
_iterator.e(_context.t0);
|
|
110
|
+
case 19:
|
|
111
|
+
_context.prev = 19;
|
|
112
|
+
_iterator.f();
|
|
113
|
+
return _context.finish(19);
|
|
114
|
+
case 22:
|
|
115
|
+
return _context.abrupt("return", p);
|
|
116
|
+
case 23:
|
|
117
|
+
case "end":
|
|
118
|
+
return _context.stop();
|
|
119
|
+
}
|
|
120
|
+
}, _callee, null, [[2, 16, 19, 22]]);
|
|
121
|
+
}))();
|
|
122
|
+
},
|
|
123
|
+
create: function create(id) {
|
|
124
|
+
var _this2 = this;
|
|
125
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
|
126
|
+
var properties, node, data, bytesLength, stream;
|
|
127
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
128
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
129
|
+
case 0:
|
|
130
|
+
properties = {
|
|
131
|
+
isDevice: false,
|
|
132
|
+
contents: null
|
|
133
|
+
};
|
|
134
|
+
_context2.next = 3;
|
|
135
|
+
return _this2.checkAdmissible(id);
|
|
136
|
+
case 3:
|
|
137
|
+
if (_context2.sent) {
|
|
138
|
+
_context2.next = 5;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
return _context2.abrupt("return", 0);
|
|
142
|
+
case 5:
|
|
143
|
+
// Create the file in the emscripten FS
|
|
144
|
+
|
|
145
|
+
// This check/mkdir was added for AOP 6 Boot loader because create is
|
|
146
|
+
// called first because were only loading Data, we needed to create
|
|
147
|
+
// the directory. See: https://github.com/permaweb/aos/issues/342
|
|
148
|
+
if (!FS.analyzePath("/data/").exists) {
|
|
149
|
+
FS.mkdir("/data/");
|
|
150
|
+
}
|
|
151
|
+
node = FS.createFile("/", "data/" + id, properties, true, false); // Set initial parameters
|
|
152
|
+
/*
|
|
153
|
+
var bytesLength = await this.customFetch(`/${id}`, {
|
|
154
|
+
method: "HEAD",
|
|
155
|
+
}).then(res => res.headers.get("Content-Length"))
|
|
156
|
+
*/
|
|
157
|
+
_context2.next = 9;
|
|
158
|
+
return ar.data(id);
|
|
159
|
+
case 9:
|
|
160
|
+
data = _context2.sent;
|
|
161
|
+
bytesLength = data ? new TextEncoder().encode(data).length : 100;
|
|
162
|
+
node.total_size = Number(bytesLength);
|
|
163
|
+
node.cache = new Uint8Array(0);
|
|
164
|
+
node.position = 0;
|
|
165
|
+
|
|
166
|
+
// Add a function that defers querying the file size until it is asked the first time.
|
|
167
|
+
Object.defineProperties(node, {
|
|
168
|
+
usedBytes: {
|
|
169
|
+
get: function get() {
|
|
170
|
+
return bytesLength;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Now we have created the file in the emscripten FS, we can open it as a stream
|
|
176
|
+
stream = FS.open("/data/" + id, "r"); //console.log("JS: Created file: ", id, " fd: ", stream.fd);
|
|
177
|
+
return _context2.abrupt("return", stream);
|
|
178
|
+
case 17:
|
|
179
|
+
case "end":
|
|
180
|
+
return _context2.stop();
|
|
181
|
+
}
|
|
182
|
+
}, _callee2);
|
|
183
|
+
}))();
|
|
184
|
+
},
|
|
185
|
+
createBlockHeader: function createBlockHeader(id) {
|
|
186
|
+
var _this3 = this;
|
|
187
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
|
188
|
+
var customFetch, retry, _retry, result, bytesLength, node, stream;
|
|
189
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
190
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
191
|
+
case 0:
|
|
192
|
+
_retry = function _retry3() {
|
|
193
|
+
_retry = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(x) {
|
|
194
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
195
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
196
|
+
case 0:
|
|
197
|
+
return _context3.abrupt("return", new Promise(function (r) {
|
|
198
|
+
setTimeout(function () {
|
|
199
|
+
r(customFetch("/block/height/".concat(id)));
|
|
200
|
+
}, x * 10000);
|
|
201
|
+
}));
|
|
202
|
+
case 1:
|
|
203
|
+
case "end":
|
|
204
|
+
return _context3.stop();
|
|
205
|
+
}
|
|
206
|
+
}, _callee3);
|
|
207
|
+
}));
|
|
208
|
+
return _retry.apply(this, arguments);
|
|
209
|
+
};
|
|
210
|
+
retry = function _retry2(_x) {
|
|
211
|
+
return _retry.apply(this, arguments);
|
|
212
|
+
};
|
|
213
|
+
customFetch = _this3.customFetch; // todo: add a bunch of retries
|
|
214
|
+
_context4.next = 5;
|
|
215
|
+
return _this3.customFetch("/block/height/".concat(id)).then(function (res) {
|
|
216
|
+
return !res.ok ? retry(1) : res;
|
|
217
|
+
}).then(function (res) {
|
|
218
|
+
return !res.ok ? retry(2) : res;
|
|
219
|
+
}).then(function (res) {
|
|
220
|
+
return !res.ok ? retry(3) : res;
|
|
221
|
+
}).then(function (res) {
|
|
222
|
+
return !res.ok ? retry(4) : res;
|
|
223
|
+
}).then(function (res) {
|
|
224
|
+
return res.text();
|
|
225
|
+
});
|
|
226
|
+
case 5:
|
|
227
|
+
result = _context4.sent;
|
|
228
|
+
bytesLength = result.length;
|
|
229
|
+
node = FS.createDataFile("/", "block/" + id, Buffer.from(result, "utf-8"), true, false);
|
|
230
|
+
stream = FS.open("/block/" + id, "r");
|
|
231
|
+
return _context4.abrupt("return", stream);
|
|
232
|
+
case 10:
|
|
233
|
+
case "end":
|
|
234
|
+
return _context4.stop();
|
|
235
|
+
}
|
|
236
|
+
}, _callee4);
|
|
237
|
+
}))();
|
|
238
|
+
},
|
|
239
|
+
createTxHeader: function createTxHeader(id) {
|
|
240
|
+
var _this4 = this;
|
|
241
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8() {
|
|
242
|
+
var customFetch, toAddress, _toAddress, retry, _retry4, result, node, stream;
|
|
243
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
|
|
244
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
245
|
+
case 0:
|
|
246
|
+
_retry4 = function _retry6() {
|
|
247
|
+
_retry4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(x) {
|
|
248
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
|
|
249
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
250
|
+
case 0:
|
|
251
|
+
return _context7.abrupt("return", new Promise(function (r) {
|
|
252
|
+
setTimeout(function () {
|
|
253
|
+
r(customFetch("/tx/".concat(id)));
|
|
254
|
+
}, x * 10000);
|
|
255
|
+
}));
|
|
256
|
+
case 1:
|
|
257
|
+
case "end":
|
|
258
|
+
return _context7.stop();
|
|
259
|
+
}
|
|
260
|
+
}, _callee7);
|
|
261
|
+
}));
|
|
262
|
+
return _retry4.apply(this, arguments);
|
|
263
|
+
};
|
|
264
|
+
retry = function _retry5(_x3) {
|
|
265
|
+
return _retry4.apply(this, arguments);
|
|
266
|
+
};
|
|
267
|
+
_toAddress = function _toAddress3() {
|
|
268
|
+
_toAddress = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(owner) {
|
|
269
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
270
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
271
|
+
case 0:
|
|
272
|
+
_context6.t0 = _arweave["default"].utils;
|
|
273
|
+
_context6.next = 3;
|
|
274
|
+
return _arweave["default"].crypto.hash(_arweave["default"].utils.b64UrlToBuffer(owner));
|
|
275
|
+
case 3:
|
|
276
|
+
_context6.t1 = _context6.sent;
|
|
277
|
+
return _context6.abrupt("return", _context6.t0.bufferTob64Url.call(_context6.t0, _context6.t1));
|
|
278
|
+
case 5:
|
|
279
|
+
case "end":
|
|
280
|
+
return _context6.stop();
|
|
281
|
+
}
|
|
282
|
+
}, _callee6);
|
|
283
|
+
}));
|
|
284
|
+
return _toAddress.apply(this, arguments);
|
|
285
|
+
};
|
|
286
|
+
toAddress = function _toAddress2(_x2) {
|
|
287
|
+
return _toAddress.apply(this, arguments);
|
|
288
|
+
};
|
|
289
|
+
customFetch = _this4.customFetch; // todo: add a bunch of retries
|
|
290
|
+
_context8.next = 7;
|
|
291
|
+
return _this4.customFetch("/tx/".concat(id)).then(function (res) {
|
|
292
|
+
return !res.ok ? retry(1) : res;
|
|
293
|
+
}).then(function (res) {
|
|
294
|
+
return !res.ok ? retry(2) : res;
|
|
295
|
+
}).then(function (res) {
|
|
296
|
+
return !res.ok ? retry(3) : res;
|
|
297
|
+
}).then(function (res) {
|
|
298
|
+
return !res.ok ? retry(4) : res;
|
|
299
|
+
}).then(function (res) {
|
|
300
|
+
return res.json();
|
|
301
|
+
}).then(/*#__PURE__*/function () {
|
|
302
|
+
var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(entry) {
|
|
303
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
304
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
305
|
+
case 0:
|
|
306
|
+
_context5.t0 = _objectSpread;
|
|
307
|
+
_context5.t1 = _objectSpread({}, entry);
|
|
308
|
+
_context5.t2 = {};
|
|
309
|
+
_context5.next = 5;
|
|
310
|
+
return toAddress(entry.owner);
|
|
311
|
+
case 5:
|
|
312
|
+
_context5.t3 = _context5.sent;
|
|
313
|
+
_context5.t4 = {
|
|
314
|
+
ownerAddress: _context5.t3
|
|
315
|
+
};
|
|
316
|
+
return _context5.abrupt("return", (0, _context5.t0)(_context5.t1, _context5.t2, _context5.t4));
|
|
317
|
+
case 8:
|
|
318
|
+
case "end":
|
|
319
|
+
return _context5.stop();
|
|
320
|
+
}
|
|
321
|
+
}, _callee5);
|
|
322
|
+
}));
|
|
323
|
+
return function (_x4) {
|
|
324
|
+
return _ref2.apply(this, arguments);
|
|
325
|
+
};
|
|
326
|
+
}())
|
|
327
|
+
//.then(x => (console.error(x), x))
|
|
328
|
+
.then(function (x) {
|
|
329
|
+
return JSON.stringify(x);
|
|
330
|
+
});
|
|
331
|
+
case 7:
|
|
332
|
+
result = _context8.sent;
|
|
333
|
+
node = FS.createDataFile("/", "tx/" + id, Buffer.from(result, "utf-8"), true, false);
|
|
334
|
+
stream = FS.open("/tx/" + id, "r");
|
|
335
|
+
return _context8.abrupt("return", stream);
|
|
336
|
+
case 11:
|
|
337
|
+
case "end":
|
|
338
|
+
return _context8.stop();
|
|
339
|
+
}
|
|
340
|
+
}, _callee8);
|
|
341
|
+
}))();
|
|
342
|
+
},
|
|
343
|
+
createDataItemTxHeader: function createDataItemTxHeader(id) {
|
|
344
|
+
var _this5 = this;
|
|
345
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
|
|
346
|
+
var gqlQuery, GET_TRANSACTION_QUERY, variables, retry, _retry7, gqlExists, result, stream;
|
|
347
|
+
return _regeneratorRuntime().wrap(function _callee11$(_context11) {
|
|
348
|
+
while (1) switch (_context11.prev = _context11.next) {
|
|
349
|
+
case 0:
|
|
350
|
+
_retry7 = function _retry9() {
|
|
351
|
+
_retry7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(x) {
|
|
352
|
+
return _regeneratorRuntime().wrap(function _callee10$(_context10) {
|
|
353
|
+
while (1) switch (_context10.prev = _context10.next) {
|
|
354
|
+
case 0:
|
|
355
|
+
return _context10.abrupt("return", new Promise(function (r) {
|
|
356
|
+
setTimeout(function () {
|
|
357
|
+
r(gqlQuery(GET_TRANSACTION_QUERY, variables));
|
|
358
|
+
}, x * 10000);
|
|
359
|
+
}));
|
|
360
|
+
case 1:
|
|
361
|
+
case "end":
|
|
362
|
+
return _context10.stop();
|
|
363
|
+
}
|
|
364
|
+
}, _callee10);
|
|
365
|
+
}));
|
|
366
|
+
return _retry7.apply(this, arguments);
|
|
367
|
+
};
|
|
368
|
+
retry = function _retry8(_x5) {
|
|
369
|
+
return _retry7.apply(this, arguments);
|
|
370
|
+
};
|
|
371
|
+
gqlQuery = _this5.gqlQuery;
|
|
372
|
+
GET_TRANSACTION_QUERY = "\n query GetTransactions ($transactionIds: [ID!]!) {\n transactions(ids: $transactionIds) {\n edges {\n node {\n id\n anchor\n data {\n size\n }\n signature\n recipient \n owner {\n address \n key\n }\n fee {\n ar \n winston\n }\n quantity {\n winston\n ar\n }\n tags {\n name \n value \n }\n bundledIn {\n id\n }\n block { \n id\n timestamp\n height\n previous\n }\n }\n }\n }\n }";
|
|
373
|
+
variables = {
|
|
374
|
+
transactionIds: [id]
|
|
375
|
+
};
|
|
376
|
+
_context11.next = 7;
|
|
377
|
+
return _this5.gqlExists();
|
|
378
|
+
case 7:
|
|
379
|
+
gqlExists = _context11.sent;
|
|
380
|
+
if (gqlExists) {
|
|
381
|
+
_context11.next = 10;
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
return _context11.abrupt("return", "GQL Not Found!");
|
|
385
|
+
case 10:
|
|
386
|
+
_context11.next = 12;
|
|
387
|
+
return _this5.gqlQuery(GET_TRANSACTION_QUERY, variables).then(function (res) {
|
|
388
|
+
return !res.ok ? retry(1) : res;
|
|
389
|
+
}).then(function (res) {
|
|
390
|
+
return !res.ok ? retry(2) : res;
|
|
391
|
+
}).then(function (res) {
|
|
392
|
+
return !res.ok ? retry(3) : res;
|
|
393
|
+
}).then(function (res) {
|
|
394
|
+
return !res.ok ? retry(4) : res;
|
|
395
|
+
}).then(function (res) {
|
|
396
|
+
return res.json();
|
|
397
|
+
}).then(function (res) {
|
|
398
|
+
var _res$data;
|
|
399
|
+
return res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && (_res$data = _res$data.transactions) !== null && _res$data !== void 0 && (_res$data = _res$data.edges) !== null && _res$data !== void 0 && (_res$data = _res$data[0]) !== null && _res$data !== void 0 && _res$data.node ? res.data.transactions.edges[0].node : "No results";
|
|
400
|
+
}).then(/*#__PURE__*/function () {
|
|
401
|
+
var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(entry) {
|
|
402
|
+
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
|
|
403
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
404
|
+
case 0:
|
|
405
|
+
return _context9.abrupt("return", typeof entry == "string" ? entry : _objectSpread({
|
|
406
|
+
format: 3
|
|
407
|
+
}, entry));
|
|
408
|
+
case 1:
|
|
409
|
+
case "end":
|
|
410
|
+
return _context9.stop();
|
|
411
|
+
}
|
|
412
|
+
}, _callee9);
|
|
413
|
+
}));
|
|
414
|
+
return function (_x6) {
|
|
415
|
+
return _ref3.apply(this, arguments);
|
|
416
|
+
};
|
|
417
|
+
}()).then(function (x) {
|
|
418
|
+
return typeof x == "string" ? x : JSON.stringify(x);
|
|
419
|
+
});
|
|
420
|
+
case 12:
|
|
421
|
+
result = _context11.sent;
|
|
422
|
+
if (!(result === "No results")) {
|
|
423
|
+
_context11.next = 15;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
return _context11.abrupt("return", result);
|
|
427
|
+
case 15:
|
|
428
|
+
FS.createDataFile("/", "tx2/" + id, Buffer.from(result, "utf-8"), true, false);
|
|
429
|
+
stream = FS.open("/tx2/" + id, "r");
|
|
430
|
+
return _context11.abrupt("return", stream);
|
|
431
|
+
case 18:
|
|
432
|
+
case "end":
|
|
433
|
+
return _context11.stop();
|
|
434
|
+
}
|
|
435
|
+
}, _callee11);
|
|
436
|
+
}))();
|
|
437
|
+
},
|
|
438
|
+
open: function open(filename) {
|
|
439
|
+
var _this6 = this;
|
|
440
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
|
|
441
|
+
var pathCategory, id, stream, _stream, _stream2, _stream3, _stream4;
|
|
442
|
+
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
443
|
+
while (1) switch (_context12.prev = _context12.next) {
|
|
444
|
+
case 0:
|
|
445
|
+
pathCategory = filename.split("/")[1];
|
|
446
|
+
id = filename.split("/")[2];
|
|
447
|
+
console.log("JS: Opening ID: ", id);
|
|
448
|
+
if (!(pathCategory === "tx")) {
|
|
449
|
+
_context12.next = 16;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
FS.createPath("/", "tx", true, false);
|
|
453
|
+
if (!FS.analyzePath(filename).exists) {
|
|
454
|
+
_context12.next = 12;
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
stream = FS.open(filename, "r");
|
|
458
|
+
if (!stream.fd) {
|
|
459
|
+
_context12.next = 9;
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
return _context12.abrupt("return", stream.fd);
|
|
463
|
+
case 9:
|
|
464
|
+
return _context12.abrupt("return", 0);
|
|
465
|
+
case 12:
|
|
466
|
+
_context12.next = 14;
|
|
467
|
+
return _this6.createTxHeader(id);
|
|
468
|
+
case 14:
|
|
469
|
+
_stream = _context12.sent;
|
|
470
|
+
return _context12.abrupt("return", _stream.fd);
|
|
471
|
+
case 16:
|
|
472
|
+
if (!(pathCategory === "tx2")) {
|
|
473
|
+
_context12.next = 31;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
FS.createPath("/", "tx2", true, false);
|
|
477
|
+
if (!FS.analyzePath(filename).exists) {
|
|
478
|
+
_context12.next = 25;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
stream = FS.open(filename, "r");
|
|
482
|
+
if (!stream.fd) {
|
|
483
|
+
_context12.next = 22;
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
return _context12.abrupt("return", stream.fd);
|
|
487
|
+
case 22:
|
|
488
|
+
return _context12.abrupt("return", 0);
|
|
489
|
+
case 25:
|
|
490
|
+
_context12.next = 27;
|
|
491
|
+
return _this6.createDataItemTxHeader(id);
|
|
492
|
+
case 27:
|
|
493
|
+
_stream2 = _context12.sent;
|
|
494
|
+
if (!_stream2.fd) {
|
|
495
|
+
_context12.next = 30;
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
return _context12.abrupt("return", _stream2.fd);
|
|
499
|
+
case 30:
|
|
500
|
+
return _context12.abrupt("return", 0);
|
|
501
|
+
case 31:
|
|
502
|
+
if (!(pathCategory === "block")) {
|
|
503
|
+
_context12.next = 44;
|
|
504
|
+
break;
|
|
505
|
+
}
|
|
506
|
+
FS.createPath("/", "block", true, false);
|
|
507
|
+
if (!FS.analyzePath(filename).exists) {
|
|
508
|
+
_context12.next = 40;
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
stream = FS.open(filename, "r");
|
|
512
|
+
if (!stream.fd) {
|
|
513
|
+
_context12.next = 37;
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
return _context12.abrupt("return", stream.fd);
|
|
517
|
+
case 37:
|
|
518
|
+
return _context12.abrupt("return", 0);
|
|
519
|
+
case 40:
|
|
520
|
+
_context12.next = 42;
|
|
521
|
+
return _this6.createBlockHeader(id);
|
|
522
|
+
case 42:
|
|
523
|
+
_stream3 = _context12.sent;
|
|
524
|
+
return _context12.abrupt("return", _stream3.fd);
|
|
525
|
+
case 44:
|
|
526
|
+
if (!(pathCategory === "data")) {
|
|
527
|
+
_context12.next = 59;
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
if (!FS.analyzePath(filename).exists) {
|
|
531
|
+
_context12.next = 53;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
stream = FS.open(filename, "r");
|
|
535
|
+
if (!stream.fd) {
|
|
536
|
+
_context12.next = 49;
|
|
537
|
+
break;
|
|
538
|
+
}
|
|
539
|
+
return _context12.abrupt("return", stream.fd);
|
|
540
|
+
case 49:
|
|
541
|
+
console.log("JS: File not found: ", filename);
|
|
542
|
+
return _context12.abrupt("return", 0);
|
|
543
|
+
case 53:
|
|
544
|
+
_context12.next = 55;
|
|
545
|
+
return _this6.create(id);
|
|
546
|
+
case 55:
|
|
547
|
+
_stream4 = _context12.sent;
|
|
548
|
+
return _context12.abrupt("return", _stream4.fd);
|
|
549
|
+
case 57:
|
|
550
|
+
_context12.next = 66;
|
|
551
|
+
break;
|
|
552
|
+
case 59:
|
|
553
|
+
if (!(pathCategory === "headers")) {
|
|
554
|
+
_context12.next = 64;
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
console.log("Header access not implemented yet.");
|
|
558
|
+
return _context12.abrupt("return", 0);
|
|
559
|
+
case 64:
|
|
560
|
+
console.log("JS: Invalid path category: ", pathCategory);
|
|
561
|
+
return _context12.abrupt("return", 0);
|
|
562
|
+
case 66:
|
|
563
|
+
case "end":
|
|
564
|
+
return _context12.stop();
|
|
565
|
+
}
|
|
566
|
+
}, _callee12);
|
|
567
|
+
}))();
|
|
568
|
+
},
|
|
569
|
+
read: function read(fd, raw_dst_ptr, raw_length) {
|
|
570
|
+
var _this7 = this;
|
|
571
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
|
|
572
|
+
var _yield$ar$data;
|
|
573
|
+
var to_read, dst_ptr, stream, i, bytes_read, chunk_download_sz, to, data, 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;
|
|
574
|
+
return _regeneratorRuntime().wrap(function _callee13$(_context13) {
|
|
575
|
+
while (1) switch (_context13.prev = _context13.next) {
|
|
576
|
+
case 0:
|
|
577
|
+
// Note: The length and dst_ptr are 53 bit integers in JS, so this _should_ be ok into a large memspace.
|
|
578
|
+
to_read = Number(raw_length);
|
|
579
|
+
dst_ptr = Number(raw_dst_ptr);
|
|
580
|
+
stream = 0;
|
|
581
|
+
for (i = 0; i < FS.streams.length; i++) {
|
|
582
|
+
if (FS.streams[i].fd === fd) {
|
|
583
|
+
stream = FS.streams[i];
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
// read block headers
|
|
587
|
+
if (!stream.path.includes("/block")) {
|
|
588
|
+
_context13.next = 7;
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
mod.HEAP8.set(stream.node.contents.subarray(0, to_read), dst_ptr);
|
|
592
|
+
return _context13.abrupt("return", to_read);
|
|
593
|
+
case 7:
|
|
594
|
+
if (!stream.path.includes("/tx")) {
|
|
595
|
+
_context13.next = 10;
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
mod.HEAP8.set(stream.node.contents.subarray(0, to_read), dst_ptr);
|
|
599
|
+
return _context13.abrupt("return", to_read);
|
|
600
|
+
case 10:
|
|
601
|
+
// Satisfy what we can with the cache first
|
|
602
|
+
bytes_read = _this7.readFromCache(stream, dst_ptr, to_read);
|
|
603
|
+
stream.position += bytes_read;
|
|
604
|
+
stream.lastReadPosition = stream.position;
|
|
605
|
+
dst_ptr += bytes_read;
|
|
606
|
+
to_read -= bytes_read;
|
|
607
|
+
|
|
608
|
+
// Return if we have satisfied the request
|
|
609
|
+
if (!(to_read === 0)) {
|
|
610
|
+
_context13.next = 17;
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
return _context13.abrupt("return", bytes_read);
|
|
614
|
+
case 17:
|
|
615
|
+
//console.log("WeaveDrive: Read from cache: ", bytes_read, " Remaining to read: ", to_read)
|
|
616
|
+
chunk_download_sz = Math.max(to_read, CACHE_SZ);
|
|
617
|
+
to = Math.min(stream.node.total_size, stream.position + chunk_download_sz); //console.log("WeaveDrive: fd: ", fd, " Read length: ", to_read, " Reading ahead:", to - to_read - stream.position)
|
|
618
|
+
// Fetch with streaming
|
|
619
|
+
/*
|
|
620
|
+
const response = await this.customFetch(`/${stream.node.name}`, {
|
|
621
|
+
method: "GET",
|
|
622
|
+
redirect: "follow",
|
|
623
|
+
headers: { Range: `bytes=${stream.position}-${to}` },
|
|
624
|
+
})
|
|
625
|
+
const reader = response.body.getReader()
|
|
626
|
+
*/
|
|
627
|
+
_context13.t0 = new TextEncoder();
|
|
628
|
+
_context13.next = 22;
|
|
629
|
+
return ar.data(stream.node.name);
|
|
630
|
+
case 22:
|
|
631
|
+
_context13.t2 = _yield$ar$data = _context13.sent;
|
|
632
|
+
_context13.t1 = _context13.t2 !== null;
|
|
633
|
+
if (!_context13.t1) {
|
|
634
|
+
_context13.next = 26;
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
_context13.t1 = _yield$ar$data !== void 0;
|
|
638
|
+
case 26:
|
|
639
|
+
if (!_context13.t1) {
|
|
640
|
+
_context13.next = 30;
|
|
641
|
+
break;
|
|
642
|
+
}
|
|
643
|
+
_context13.t3 = _yield$ar$data;
|
|
644
|
+
_context13.next = 31;
|
|
645
|
+
break;
|
|
646
|
+
case 30:
|
|
647
|
+
_context13.t3 = "";
|
|
648
|
+
case 31:
|
|
649
|
+
_context13.t4 = _context13.t3;
|
|
650
|
+
data = _context13.t0.encode.call(_context13.t0, _context13.t4);
|
|
651
|
+
// Extract the Range header to determine the start and end of the requested chunk
|
|
652
|
+
start = 0;
|
|
653
|
+
end = data.length; // Create a ReadableStream for the requested chunk
|
|
654
|
+
chunk = data.subarray(start, end);
|
|
655
|
+
response = new Response(new ReadableStream({
|
|
656
|
+
start: function start(controller) {
|
|
657
|
+
controller.enqueue(chunk); // Push the chunk to the stream
|
|
658
|
+
controller.close(); // Close the stream when done
|
|
659
|
+
}
|
|
660
|
+
}), {
|
|
661
|
+
headers: {
|
|
662
|
+
"Content-Length": chunk.length.toString()
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
reader = response.body.getReader();
|
|
666
|
+
bytes_until_cache = CHUNK_SZ;
|
|
667
|
+
bytes_until_notify = NOTIFY_SZ;
|
|
668
|
+
downloaded_bytes = 0;
|
|
669
|
+
cache_chunks = [];
|
|
670
|
+
_context13.prev = 42;
|
|
671
|
+
case 43:
|
|
672
|
+
if (!true) {
|
|
673
|
+
_context13.next = 61;
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
_context13.next = 46;
|
|
677
|
+
return reader.read();
|
|
678
|
+
case 46:
|
|
679
|
+
_yield$reader$read = _context13.sent;
|
|
680
|
+
done = _yield$reader$read.done;
|
|
681
|
+
chunk_bytes = _yield$reader$read.value;
|
|
682
|
+
if (!done) {
|
|
683
|
+
_context13.next = 51;
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
return _context13.abrupt("break", 61);
|
|
687
|
+
case 51:
|
|
688
|
+
// Update the number of downloaded bytes to be _all_, not just the write length
|
|
689
|
+
downloaded_bytes += chunk_bytes.length;
|
|
690
|
+
bytes_until_cache -= chunk_bytes.length;
|
|
691
|
+
bytes_until_notify -= chunk_bytes.length;
|
|
692
|
+
|
|
693
|
+
// Write bytes from the chunk and update the pointer if necessary
|
|
694
|
+
write_length = Math.min(chunk_bytes.length, to_read);
|
|
695
|
+
if (write_length > 0) {
|
|
696
|
+
//console.log("WeaveDrive: Writing: ", write_length, " bytes to: ", dst_ptr)
|
|
697
|
+
mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr);
|
|
698
|
+
dst_ptr += write_length;
|
|
699
|
+
bytes_read += write_length;
|
|
700
|
+
stream.position += write_length;
|
|
701
|
+
to_read -= write_length;
|
|
702
|
+
}
|
|
703
|
+
if (to_read == 0) {
|
|
704
|
+
// Add excess bytes to our cache
|
|
705
|
+
chunk_to_cache = chunk_bytes.subarray(write_length); //console.log("WeaveDrive: Cacheing excess: ", chunk_to_cache.length)
|
|
706
|
+
cache_chunks.push(chunk_to_cache);
|
|
707
|
+
}
|
|
708
|
+
if (bytes_until_cache <= 0) {
|
|
709
|
+
console.log("WeaveDrive: Chunk size reached. Compressing cache...");
|
|
710
|
+
stream.node.cache = _this7.addChunksToCache(stream.node.cache, cache_chunks);
|
|
711
|
+
cache_chunks = [];
|
|
712
|
+
bytes_until_cache = CHUNK_SZ;
|
|
713
|
+
}
|
|
714
|
+
if (bytes_until_notify <= 0) {
|
|
715
|
+
console.log("WeaveDrive: Downloaded: ", downloaded_bytes / stream.node.total_size * 100, "%");
|
|
716
|
+
bytes_until_notify = NOTIFY_SZ;
|
|
717
|
+
}
|
|
718
|
+
_context13.next = 43;
|
|
719
|
+
break;
|
|
720
|
+
case 61:
|
|
721
|
+
_context13.next = 66;
|
|
722
|
+
break;
|
|
723
|
+
case 63:
|
|
724
|
+
_context13.prev = 63;
|
|
725
|
+
_context13.t5 = _context13["catch"](42);
|
|
726
|
+
console.error("WeaveDrive: Error reading the stream: ", _context13.t5);
|
|
727
|
+
case 66:
|
|
728
|
+
_context13.prev = 66;
|
|
729
|
+
reader.releaseLock();
|
|
730
|
+
return _context13.finish(66);
|
|
731
|
+
case 69:
|
|
732
|
+
// If we have no cache, or we have not satisfied the full request, we need to download the rest
|
|
733
|
+
// Rebuild the cache from the new cache chunks
|
|
734
|
+
stream.node.cache = _this7.addChunksToCache(stream.node.cache, cache_chunks);
|
|
735
|
+
|
|
736
|
+
// Update the last read position
|
|
737
|
+
stream.lastReadPosition = stream.position;
|
|
738
|
+
return _context13.abrupt("return", bytes_read);
|
|
739
|
+
case 72:
|
|
740
|
+
case "end":
|
|
741
|
+
return _context13.stop();
|
|
742
|
+
}
|
|
743
|
+
}, _callee13, null, [[42, 63, 66, 69]]);
|
|
744
|
+
}))();
|
|
745
|
+
},
|
|
746
|
+
close: function close(fd) {
|
|
747
|
+
var stream = 0;
|
|
748
|
+
for (var i = 0; i < FS.streams.length; i++) {
|
|
749
|
+
if (FS.streams[i].fd === fd) {
|
|
750
|
+
stream = FS.streams[i];
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
FS.close(stream);
|
|
754
|
+
},
|
|
755
|
+
// Readahead cache functions
|
|
756
|
+
readFromCache: function readFromCache(stream, dst_ptr, length) {
|
|
757
|
+
// Check if the cache has been invalidated by a seek
|
|
758
|
+
if (stream.lastReadPosition !== stream.position) {
|
|
759
|
+
//console.log("WeaveDrive: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
|
|
760
|
+
stream.node.cache = new Uint8Array(0);
|
|
761
|
+
return 0;
|
|
762
|
+
}
|
|
763
|
+
// Calculate the bytes of the request that can be satisfied with the cache
|
|
764
|
+
var cache_part_length = Math.min(length, stream.node.cache.length);
|
|
765
|
+
var cache_part = stream.node.cache.subarray(0, cache_part_length);
|
|
766
|
+
mod.HEAP8.set(cache_part, dst_ptr);
|
|
767
|
+
// Set the new cache to the remainder of the unused cache and update pointers
|
|
768
|
+
stream.node.cache = stream.node.cache.subarray(cache_part_length);
|
|
769
|
+
return cache_part_length;
|
|
770
|
+
},
|
|
771
|
+
addChunksToCache: function addChunksToCache(old_cache, chunks) {
|
|
772
|
+
// Make a new cache array of the old cache length + the sum of the chunk lengths, capped by the max cache size
|
|
773
|
+
var new_cache_length = Math.min(old_cache.length + chunks.reduce(function (acc, chunk) {
|
|
774
|
+
return acc + chunk.length;
|
|
775
|
+
}, 0), CACHE_SZ);
|
|
776
|
+
var new_cache = new Uint8Array(new_cache_length);
|
|
777
|
+
// Copy the old cache to the new cache
|
|
778
|
+
new_cache.set(old_cache, 0);
|
|
779
|
+
// Load the cache chunks into the new cache
|
|
780
|
+
var current_offset = old_cache.length;
|
|
781
|
+
var _iterator2 = _createForOfIteratorHelper(chunks),
|
|
782
|
+
_step2;
|
|
783
|
+
try {
|
|
784
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
785
|
+
var chunk = _step2.value;
|
|
786
|
+
if (current_offset < new_cache_length) {
|
|
787
|
+
new_cache.set(chunk.subarray(0, new_cache_length - current_offset), current_offset);
|
|
788
|
+
current_offset += chunk.length;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
} catch (err) {
|
|
792
|
+
_iterator2.e(err);
|
|
793
|
+
} finally {
|
|
794
|
+
_iterator2.f();
|
|
795
|
+
}
|
|
796
|
+
return new_cache;
|
|
797
|
+
},
|
|
798
|
+
// General helpder functions
|
|
799
|
+
checkAdmissible: function checkAdmissible(ID) {
|
|
800
|
+
var _this8 = this;
|
|
801
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee14() {
|
|
802
|
+
var bootTag, blockHeight, moduleExtensions, moduleHasWeaveDrive, processExtensions, processHasWeaveDrive, modes, moduleAvailabilityType, moduleMode, processAvailabilityType, processMode, attestors, assignmentsHaveID, individualsHaveID;
|
|
803
|
+
return _regeneratorRuntime().wrap(function _callee14$(_context14) {
|
|
804
|
+
while (1) switch (_context14.prev = _context14.next) {
|
|
805
|
+
case 0:
|
|
806
|
+
if (!(mod.mode && mod.mode == "test")) {
|
|
807
|
+
_context14.next = 2;
|
|
808
|
+
break;
|
|
809
|
+
}
|
|
810
|
+
return _context14.abrupt("return", true);
|
|
811
|
+
case 2:
|
|
812
|
+
// Check if we are attempting to load the On-Boot id, if so allow it
|
|
813
|
+
// this was added for AOP 6 Boot loader See: https://github.com/permaweb/aos/issues/342
|
|
814
|
+
bootTag = _this8.getTagValue("On-Boot", mod.spawn.tags);
|
|
815
|
+
if (!(bootTag && bootTag === ID)) {
|
|
816
|
+
_context14.next = 5;
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
return _context14.abrupt("return", true);
|
|
820
|
+
case 5:
|
|
821
|
+
// Check that this module or process set the WeaveDrive tag on spawn
|
|
822
|
+
blockHeight = mod.blockHeight;
|
|
823
|
+
moduleExtensions = _this8.getTagValues("Extension", mod.module.tags);
|
|
824
|
+
moduleHasWeaveDrive = moduleExtensions.includes("WeaveDrive");
|
|
825
|
+
processExtensions = _this8.getTagValues("Extension", mod.spawn.tags);
|
|
826
|
+
processHasWeaveDrive = moduleHasWeaveDrive || processExtensions.includes("WeaveDrive");
|
|
827
|
+
if (processHasWeaveDrive) {
|
|
828
|
+
_context14.next = 13;
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
console.log("WeaveDrive: Process tried to call WeaveDrive, but extension not set!");
|
|
832
|
+
return _context14.abrupt("return", false);
|
|
833
|
+
case 13:
|
|
834
|
+
modes = ["Assignments", "Individual", "Library"]; // Get the Availability-Type from the spawned process's Module or Process item
|
|
835
|
+
// First check the module for its defaults
|
|
836
|
+
moduleAvailabilityType = _this8.getTagValue("Availability-Type", mod.module.tags);
|
|
837
|
+
moduleMode = moduleAvailabilityType ? moduleAvailabilityType : "Assignments"; // Default to assignments
|
|
838
|
+
// Now check the process's spawn item. These settings override Module item settings.
|
|
839
|
+
processAvailabilityType = _this8.getTagValue("Availability-Type", mod.spawn.tags);
|
|
840
|
+
processMode = processAvailabilityType ? processAvailabilityType : moduleMode;
|
|
841
|
+
if (modes.includes(processMode)) {
|
|
842
|
+
_context14.next = 20;
|
|
843
|
+
break;
|
|
844
|
+
}
|
|
845
|
+
throw "Unsupported WeaveDrive mode: ".concat(processMode);
|
|
846
|
+
case 20:
|
|
847
|
+
attestors = _this8.serializeStringArr([_this8.getTagValue("Scheduler", mod.spawn.tags)].concat(_toConsumableArray(_this8.getTagValues("Attestor", mod.spawn.tags))).filter(function (t) {
|
|
848
|
+
return !!t;
|
|
849
|
+
})); // Init a set of GraphQL queries to run in order to find a valid attestation
|
|
850
|
+
// Every WeaveDrive process has at least the "Assignments" availability check form.
|
|
851
|
+
_context14.next = 23;
|
|
852
|
+
return _this8.queryHasResult("query {\n transactions(\n owners: ".concat(attestors, ",\n block: {min: 0, max: ").concat(blockHeight, "},\n tags: [\n { name: \"Type\", values: [\"Attestation\"] },\n { name: \"Message\", values: [\"").concat(ID, "\"]}\n { name: \"Data-Protocol\", values: [\"ao\"] },\n ]\n ) \n {\n edges {\n node {\n tags {\n name\n value\n }\n }\n }\n }\n }"));
|
|
853
|
+
case 23:
|
|
854
|
+
assignmentsHaveID = _context14.sent;
|
|
855
|
+
if (!assignmentsHaveID) {
|
|
856
|
+
_context14.next = 26;
|
|
857
|
+
break;
|
|
858
|
+
}
|
|
859
|
+
return _context14.abrupt("return", true);
|
|
860
|
+
case 26:
|
|
861
|
+
if (!(processMode == "Individual")) {
|
|
862
|
+
_context14.next = 32;
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
_context14.next = 29;
|
|
866
|
+
return _this8.queryHasResult("query {\n transactions(\n owners: ".concat(attestors, ",\n block: {min: 0, max: ").concat(blockHeight, "},\n tags: [\n { name: \"Type\", values: [\"Available\"]},\n { name: \"ID\", values: [\"").concat(ID, "\"]}\n { name: \"Data-Protocol\", values: [\"WeaveDrive\"] },\n ]\n ) \n {\n edges {\n node {\n tags {\n name\n value\n }\n }\n }\n }\n }"));
|
|
867
|
+
case 29:
|
|
868
|
+
individualsHaveID = _context14.sent;
|
|
869
|
+
if (!individualsHaveID) {
|
|
870
|
+
_context14.next = 32;
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
return _context14.abrupt("return", true);
|
|
874
|
+
case 32:
|
|
875
|
+
if (!(processMode == "Library")) {
|
|
876
|
+
_context14.next = 34;
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
throw "This WeaveDrive implementation does not support Library attestations yet!";
|
|
880
|
+
case 34:
|
|
881
|
+
return _context14.abrupt("return", false);
|
|
882
|
+
case 35:
|
|
883
|
+
case "end":
|
|
884
|
+
return _context14.stop();
|
|
885
|
+
}
|
|
886
|
+
}, _callee14);
|
|
887
|
+
}))();
|
|
888
|
+
},
|
|
889
|
+
serializeStringArr: function serializeStringArr() {
|
|
890
|
+
var arr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
891
|
+
return "[".concat(arr.map(function (s) {
|
|
892
|
+
return "\"".concat(s, "\"");
|
|
893
|
+
}).join(", "), "]");
|
|
894
|
+
},
|
|
895
|
+
getTagValues: function getTagValues(key, tags) {
|
|
896
|
+
var values = [];
|
|
897
|
+
for (i = 0; i < tags.length; i++) {
|
|
898
|
+
if (tags[i].name == key) {
|
|
899
|
+
values.push(tags[i].value);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return values;
|
|
903
|
+
},
|
|
904
|
+
getTagValue: function getTagValue(key, tags) {
|
|
905
|
+
var values = this.getTagValues(key, tags);
|
|
906
|
+
return values.pop();
|
|
907
|
+
},
|
|
908
|
+
queryHasResult: function queryHasResult(query, variables) {
|
|
909
|
+
var _this9 = this;
|
|
910
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee15() {
|
|
911
|
+
var _json$data;
|
|
912
|
+
var json;
|
|
913
|
+
return _regeneratorRuntime().wrap(function _callee15$(_context15) {
|
|
914
|
+
while (1) switch (_context15.prev = _context15.next) {
|
|
915
|
+
case 0:
|
|
916
|
+
_context15.next = 2;
|
|
917
|
+
return _this9.gqlQuery(query, variables).then(function (res) {
|
|
918
|
+
return res.json();
|
|
919
|
+
});
|
|
920
|
+
case 2:
|
|
921
|
+
json = _context15.sent;
|
|
922
|
+
return _context15.abrupt("return", !!(json !== null && json !== void 0 && (_json$data = json.data) !== null && _json$data !== void 0 && (_json$data = _json$data.transactions) !== null && _json$data !== void 0 && (_json$data = _json$data.edges) !== null && _json$data !== void 0 && _json$data.length));
|
|
923
|
+
case 4:
|
|
924
|
+
case "end":
|
|
925
|
+
return _context15.stop();
|
|
926
|
+
}
|
|
927
|
+
}, _callee15);
|
|
928
|
+
}))();
|
|
929
|
+
},
|
|
930
|
+
gqlExists: function gqlExists() {
|
|
931
|
+
var _this10 = this;
|
|
932
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee16() {
|
|
933
|
+
var query, gqlExists;
|
|
934
|
+
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
|
|
935
|
+
while (1) switch (_context16.prev = _context16.next) {
|
|
936
|
+
case 0:
|
|
937
|
+
query = "query {\n transactions(\n first: 1\n ) {\n pageInfo {\n hasNextPage\n }\n }\n }\n ";
|
|
938
|
+
_context16.next = 3;
|
|
939
|
+
return _this10.gqlQuery(query, {}).then(function (res) {
|
|
940
|
+
return res.ok;
|
|
941
|
+
});
|
|
942
|
+
case 3:
|
|
943
|
+
gqlExists = _context16.sent;
|
|
944
|
+
return _context16.abrupt("return", gqlExists);
|
|
945
|
+
case 5:
|
|
946
|
+
case "end":
|
|
947
|
+
return _context16.stop();
|
|
948
|
+
}
|
|
949
|
+
}, _callee16);
|
|
950
|
+
}))();
|
|
951
|
+
},
|
|
952
|
+
gqlQuery: function gqlQuery(query, variables) {
|
|
953
|
+
var _this11 = this;
|
|
954
|
+
return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee17() {
|
|
955
|
+
var options;
|
|
956
|
+
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
|
|
957
|
+
while (1) switch (_context17.prev = _context17.next) {
|
|
958
|
+
case 0:
|
|
959
|
+
options = {
|
|
960
|
+
method: "POST",
|
|
961
|
+
body: JSON.stringify({
|
|
962
|
+
query: query,
|
|
963
|
+
variables: variables
|
|
964
|
+
}),
|
|
965
|
+
headers: {
|
|
966
|
+
"Content-Type": "application/json"
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
return _context17.abrupt("return", _this11.customFetch("graphql", options));
|
|
970
|
+
case 2:
|
|
971
|
+
case "end":
|
|
972
|
+
return _context17.stop();
|
|
973
|
+
}
|
|
974
|
+
}, _callee17);
|
|
975
|
+
}))();
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
};
|
|
979
|
+
});
|