wao 0.6.0 → 0.6.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/helpers.js CHANGED
@@ -10,6 +10,7 @@ var _assert = _interopRequireDefault(require("assert"));
10
10
  var _aoconnect3 = require("@permaweb/aoconnect");
11
11
  var _path = require("path");
12
12
  var _fs = require("fs");
13
+ var _utils = require("./utils.js");
13
14
  var _yargs = _interopRequireDefault(require("yargs"));
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
15
16
  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; }
@@ -166,12 +167,7 @@ var Testnet = exports.Testnet = /*#__PURE__*/function () {
166
167
  this.arweave = arweave !== null && arweave !== void 0 ? arweave : {
167
168
  port: port
168
169
  };
169
- this.aoconnect = aoconnect !== null && aoconnect !== void 0 ? aoconnect : {
170
- MU_URL: "http://localhost:".concat(port + 2),
171
- SU_URL: "http://localhost:".concat(port + 3),
172
- CU_URL: "http://localhost:".concat(port + 4),
173
- GATEWAY_URL: "http://localhost:".concat(port)
174
- };
170
+ this.aoconnect = aoconnect !== null && aoconnect !== void 0 ? aoconnect : (0, _utils.optAO)(port);
175
171
  this.ar = new _index.AR(this.arweave);
176
172
  }
177
173
  return _createClass(Testnet, [{
@@ -198,7 +194,7 @@ var Testnet = exports.Testnet = /*#__PURE__*/function () {
198
194
  return this.ar.init(jwk);
199
195
  case 8:
200
196
  this.jwk = this.ar.jwk;
201
- this.addr = this.ar.a;
197
+ this.addr = this.ar.addr;
202
198
  this.gql = this.ar.gql;
203
199
  _context4.next = 13;
204
200
  return this.src.upload("aos2_0_1", "wasm");
@@ -355,11 +351,7 @@ var setup = exports.setup = /*#__PURE__*/function () {
355
351
  (_arweave = arweave) !== null && _arweave !== void 0 ? _arweave : arweave = {
356
352
  port: 4000
357
353
  };
358
- (_aoconnect = aoconnect) !== null && _aoconnect !== void 0 ? _aoconnect : aoconnect = {
359
- MU_URL: "http://localhost:4002",
360
- CU_URL: "http://localhost:4004",
361
- GATEWAY_URL: "http://localhost:4000"
362
- };
354
+ (_aoconnect = aoconnect) !== null && _aoconnect !== void 0 ? _aoconnect : aoconnect = (0, _utils.optAO)(4000);
363
355
  ar = new _index.AR(arweave);
364
356
  _context5.next = 37;
365
357
  return ar.gen("10");
package/cjs/kv.js ADDED
@@ -0,0 +1,335 @@
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
+ var _utils = require("./utils.js");
10
+ var _ramda = require("ramda");
11
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
12
+ 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; } } }; }
13
+ 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; } }
14
+ 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; }
15
+ 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; }
16
+ 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); }
17
+ 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); }); }; }
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
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
23
+ var KB = 1024;
24
+ var MB = KB * 1024;
25
+ var CACHE_SZ = 32 * KB;
26
+ var CHUNK_SZ = 128 * MB;
27
+ var NOTIFY_SZ = 512 * MB;
28
+ var log = console.log;
29
+ var KV = exports["default"] = /*#__PURE__*/_createClass(function KV(ar) {
30
+ _classCallCheck(this, KV);
31
+ this.kv = function WeaveDrive(mod, FS) {
32
+ return {
33
+ create: function create(id) {
34
+ return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
35
+ var _yield$ar$data$length, _yield$ar$data;
36
+ var properties, node, bytesLength, stream;
37
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
38
+ while (1) switch (_context.prev = _context.next) {
39
+ case 0:
40
+ properties = {
41
+ isDevice: false,
42
+ contents: null
43
+ };
44
+ if (!FS.analyzePath("/data/").exists) FS.mkdir("/data/");
45
+ node = FS.createFile("/", "data/" + id, properties, true, false); // Set initial parameters
46
+ _context.next = 5;
47
+ return ar.data(id);
48
+ case 5:
49
+ _context.t2 = _yield$ar$data = _context.sent;
50
+ _context.t1 = _context.t2 === null;
51
+ if (_context.t1) {
52
+ _context.next = 9;
53
+ break;
54
+ }
55
+ _context.t1 = _yield$ar$data === void 0;
56
+ case 9:
57
+ if (!_context.t1) {
58
+ _context.next = 13;
59
+ break;
60
+ }
61
+ _context.t3 = void 0;
62
+ _context.next = 14;
63
+ break;
64
+ case 13:
65
+ _context.t3 = _yield$ar$data.length;
66
+ case 14:
67
+ _context.t4 = _yield$ar$data$length = _context.t3;
68
+ _context.t0 = _context.t4 !== null;
69
+ if (!_context.t0) {
70
+ _context.next = 18;
71
+ break;
72
+ }
73
+ _context.t0 = _yield$ar$data$length !== void 0;
74
+ case 18:
75
+ if (!_context.t0) {
76
+ _context.next = 22;
77
+ break;
78
+ }
79
+ _context.t5 = _yield$ar$data$length;
80
+ _context.next = 23;
81
+ break;
82
+ case 22:
83
+ _context.t5 = 0;
84
+ case 23:
85
+ bytesLength = _context.t5;
86
+ node.total_size = Number(bytesLength);
87
+ node.cache = new Uint8Array(0);
88
+ node.position = 0;
89
+
90
+ // Add a function that defers querying the file size until it is asked the first time.
91
+ Object.defineProperties(node, {
92
+ usedBytes: {
93
+ get: function get() {
94
+ return bytesLength;
95
+ }
96
+ }
97
+ });
98
+
99
+ // Now we have created the file in the emscripten FS, we can open it as a stream
100
+ stream = FS.open("/data/" + id, "r"); //console.log("JS: Created file: ", id, " fd: ", stream.fd);
101
+ return _context.abrupt("return", stream);
102
+ case 30:
103
+ case "end":
104
+ return _context.stop();
105
+ }
106
+ }, _callee);
107
+ }))();
108
+ },
109
+ open: function open(filename) {
110
+ var _this = this;
111
+ return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
112
+ var pathCategory, id, stream, _stream;
113
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
114
+ while (1) switch (_context2.prev = _context2.next) {
115
+ case 0:
116
+ pathCategory = filename.split("/")[1];
117
+ id = filename.split("/")[2]; //log("JS: Opening ID: ", id)
118
+ if (!(pathCategory === "data")) {
119
+ _context2.next = 17;
120
+ break;
121
+ }
122
+ if (!FS.analyzePath(filename).exists) {
123
+ _context2.next = 11;
124
+ break;
125
+ }
126
+ stream = FS.open(filename, "r");
127
+ if (!stream.fd) {
128
+ _context2.next = 7;
129
+ break;
130
+ }
131
+ return _context2.abrupt("return", stream.fd);
132
+ case 7:
133
+ console.log("JS: File not found: ", filename);
134
+ return _context2.abrupt("return", 0);
135
+ case 11:
136
+ _context2.next = 13;
137
+ return _this.create(id);
138
+ case 13:
139
+ _stream = _context2.sent;
140
+ return _context2.abrupt("return", _stream.fd);
141
+ case 15:
142
+ _context2.next = 19;
143
+ break;
144
+ case 17:
145
+ console.log("JS: Invalid path category: ", pathCategory);
146
+ return _context2.abrupt("return", 0);
147
+ case 19:
148
+ case "end":
149
+ return _context2.stop();
150
+ }
151
+ }, _callee2);
152
+ }))();
153
+ },
154
+ read: function read(fd, raw_dst_ptr, raw_length) {
155
+ var _this2 = this;
156
+ return _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
157
+ 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;
158
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
159
+ while (1) switch (_context3.prev = _context3.next) {
160
+ case 0:
161
+ to_read = Number(raw_length);
162
+ dst_ptr = Number(raw_dst_ptr);
163
+ stream = 0;
164
+ for (i = 0; i < FS.streams.length; i++) {
165
+ if (FS.streams[i].fd === fd) stream = FS.streams[i];
166
+ }
167
+
168
+ // Satisfy what we can with the cache first
169
+ bytes_read = _this2.readFromCache(stream, dst_ptr, to_read);
170
+ stream.position += bytes_read;
171
+ stream.lastReadPosition = stream.position;
172
+ dst_ptr += bytes_read;
173
+ to_read -= bytes_read;
174
+
175
+ // Return if we have satisfied the request
176
+ if (!(to_read === 0)) {
177
+ _context3.next = 11;
178
+ break;
179
+ }
180
+ return _context3.abrupt("return", bytes_read);
181
+ case 11:
182
+ //console.log("WeaveDrive: Read from cache: ", bytes_read, " Remaining to read: ", to_read)
183
+ chunk_download_sz = Math.max(to_read, CACHE_SZ);
184
+ to = Math.min(stream.node.total_size, stream.position + chunk_download_sz);
185
+ _context3.next = 15;
186
+ return ar.data(stream.node.name);
187
+ case 15:
188
+ data = _context3.sent;
189
+ // Extract the Range header to determine the start and end of the requested chunk
190
+ start = 0;
191
+ end = data.length; // Create a ReadableStream for the requested chunk
192
+ chunk = data.subarray(start, end);
193
+ response = new Response(new ReadableStream({
194
+ start: function start(controller) {
195
+ controller.enqueue(chunk); // Push the chunk to the stream
196
+ controller.close(); // Close the stream when done
197
+ }
198
+ }), {
199
+ headers: {
200
+ "Content-Length": chunk.length.toString()
201
+ }
202
+ });
203
+ reader = response.body.getReader();
204
+ bytes_until_cache = CHUNK_SZ;
205
+ bytes_until_notify = NOTIFY_SZ;
206
+ downloaded_bytes = 0;
207
+ cache_chunks = [];
208
+ _context3.prev = 25;
209
+ case 26:
210
+ if (!true) {
211
+ _context3.next = 44;
212
+ break;
213
+ }
214
+ _context3.next = 29;
215
+ return reader.read();
216
+ case 29:
217
+ _yield$reader$read = _context3.sent;
218
+ done = _yield$reader$read.done;
219
+ chunk_bytes = _yield$reader$read.value;
220
+ if (!done) {
221
+ _context3.next = 34;
222
+ break;
223
+ }
224
+ return _context3.abrupt("break", 44);
225
+ case 34:
226
+ // Update the number of downloaded bytes to be _all_, not just the write length
227
+ downloaded_bytes += chunk_bytes.length;
228
+ bytes_until_cache -= chunk_bytes.length;
229
+ bytes_until_notify -= chunk_bytes.length;
230
+
231
+ // Write bytes from the chunk and update the pointer if necessary
232
+ write_length = Math.min(chunk_bytes.length, to_read);
233
+ if (write_length > 0) {
234
+ //console.log("WeaveDrive: Writing: ", write_length, " bytes to: ", dst_ptr)
235
+ mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr);
236
+ dst_ptr += write_length;
237
+ bytes_read += write_length;
238
+ stream.position += write_length;
239
+ to_read -= write_length;
240
+ }
241
+ if (to_read == 0) {
242
+ // Add excess bytes to our cache
243
+ chunk_to_cache = chunk_bytes.subarray(write_length); //console.log("WeaveDrive: Cacheing excess: ", chunk_to_cache.length)
244
+ cache_chunks.push(chunk_to_cache);
245
+ }
246
+ if (bytes_until_cache <= 0) {
247
+ console.log("WeaveDrive: Chunk size reached. Compressing cache...");
248
+ stream.node.cache = _this2.addChunksToCache(stream.node.cache, cache_chunks);
249
+ cache_chunks = [];
250
+ bytes_until_cache = CHUNK_SZ;
251
+ }
252
+ if (bytes_until_notify <= 0) {
253
+ console.log("WeaveDrive: Downloaded: ", downloaded_bytes / stream.node.total_size * 100, "%");
254
+ bytes_until_notify = NOTIFY_SZ;
255
+ }
256
+ _context3.next = 26;
257
+ break;
258
+ case 44:
259
+ _context3.next = 49;
260
+ break;
261
+ case 46:
262
+ _context3.prev = 46;
263
+ _context3.t0 = _context3["catch"](25);
264
+ console.error("WeaveDrive: Error reading the stream: ", _context3.t0);
265
+ case 49:
266
+ _context3.prev = 49;
267
+ reader.releaseLock();
268
+ return _context3.finish(49);
269
+ case 52:
270
+ // If we have no cache, or we have not satisfied the full request, we need to download the rest
271
+ // Rebuild the cache from the new cache chunks
272
+ stream.node.cache = _this2.addChunksToCache(stream.node.cache, cache_chunks);
273
+
274
+ // Update the last read position
275
+ stream.lastReadPosition = stream.position;
276
+ return _context3.abrupt("return", bytes_read);
277
+ case 55:
278
+ case "end":
279
+ return _context3.stop();
280
+ }
281
+ }, _callee3, null, [[25, 46, 49, 52]]);
282
+ }))();
283
+ },
284
+ close: function close(fd) {
285
+ var stream = 0;
286
+ for (var i = 0; i < FS.streams.length; i++) {
287
+ if (FS.streams[i].fd === fd) stream = FS.streams[i];
288
+ }
289
+ FS.close(stream);
290
+ },
291
+ readFromCache: function readFromCache(stream, dst_ptr, length) {
292
+ // Check if the cache has been invalidated by a seek
293
+ if (stream.lastReadPosition !== stream.position) {
294
+ //console.log("WeaveDrive: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
295
+ stream.node.cache = new Uint8Array(0);
296
+ return 0;
297
+ }
298
+ // Calculate the bytes of the request that can be satisfied with the cache
299
+ var cache_part_length = Math.min(length, stream.node.cache.length);
300
+ var cache_part = stream.node.cache.subarray(0, cache_part_length);
301
+ mod.HEAP8.set(cache_part, dst_ptr);
302
+ // Set the new cache to the remainder of the unused cache and update pointers
303
+ stream.node.cache = stream.node.cache.subarray(cache_part_length);
304
+ return cache_part_length;
305
+ },
306
+ addChunksToCache: function addChunksToCache(old_cache, chunks) {
307
+ // Make a new cache array of the old cache length + the sum of the chunk lengths, capped by the max cache size
308
+ var new_cache_length = Math.min(old_cache.length + chunks.reduce(function (acc, chunk) {
309
+ return acc + chunk.length;
310
+ }, 0), CACHE_SZ);
311
+ var new_cache = new Uint8Array(new_cache_length);
312
+ // Copy the old cache to the new cache
313
+ new_cache.set(old_cache, 0);
314
+ // Load the cache chunks into the new cache
315
+ var current_offset = old_cache.length;
316
+ var _iterator = _createForOfIteratorHelper(chunks),
317
+ _step;
318
+ try {
319
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
320
+ var chunk = _step.value;
321
+ if (current_offset < new_cache_length) {
322
+ new_cache.set(chunk.subarray(0, new_cache_length - current_offset), current_offset);
323
+ current_offset += chunk.length;
324
+ }
325
+ }
326
+ } catch (err) {
327
+ _iterator.e(err);
328
+ } finally {
329
+ _iterator.f();
330
+ }
331
+ return new_cache;
332
+ }
333
+ };
334
+ };
335
+ });
package/cjs/utils.js CHANGED
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.isData = exports.isCheckComplete = exports.getTagVal = exports.getTag = exports.dirname = exports.checkTag = exports.buildTags = exports.action = void 0;
7
7
  exports.isJSON = isJSON;
8
- exports.wait = exports.validAddress = exports.udl = exports.toGraphObj = exports.tags = exports.tagEq = exports.tag = exports.srcs = exports.searchTag = exports.mergeOut = exports.mergeChecks = exports.ltags = exports.jsonToStr = exports.isRegExp = exports.isOutComplete = exports.isLocalhost = void 0;
8
+ exports.wait = exports.validAddress = exports.udl = exports.toGraphObj = exports.tags = exports.tagEq = exports.tag = exports.srcs = exports.searchTag = exports.optServer = exports.optAO = exports.mergeOut = exports.mergeChecks = exports.ltags = exports.jsonToStr = exports.isRegExp = exports.isOutComplete = exports.isLocalhost = void 0;
9
9
  var _graphql = require("graphql");
10
10
  var _ramda = require("ramda");
11
11
  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; }
@@ -595,4 +595,20 @@ var toGraphObj = exports.toGraphObj = function toGraphObj(_ref12) {
595
595
  tar: tar,
596
596
  args: args
597
597
  };
598
+ };
599
+ var optAO = exports.optAO = function optAO(port) {
600
+ return {
601
+ MU_URL: "http://localhost:".concat(port + 2),
602
+ SU_URL: "http://localhost:".concat(port + 3),
603
+ CU_URL: "http://localhost:".concat(port + 4),
604
+ GATEWAY_URL: "http://localhost:".concat(port)
605
+ };
606
+ };
607
+ var optServer = exports.optServer = function optServer(port) {
608
+ return {
609
+ ar: port,
610
+ mu: port + 2,
611
+ su: port + 3,
612
+ cu: port + 4
613
+ };
598
614
  };