godprotocol 1.0.841 → 1.0.842

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.
@@ -1,139 +1,146 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- var _functions = require("generalised-datastore/utils/functions");
8
- var _framer = _interopRequireDefault(require("../framer"));
9
- var _opcodes = _interopRequireDefault(require("../opcodes"));
10
- var _Account = _interopRequireDefault(require("./Account"));
11
- var _Blockweb = _interopRequireDefault(require("./Blockweb"));
12
- var _Oracle = _interopRequireDefault(require("./Oracle"));
13
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
14
- 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); }
15
- 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; }
16
- 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; }
17
- 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); } }
18
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
19
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
20
- 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; }
21
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
22
- 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); }
23
- var Manager = /*#__PURE__*/_createClass(function Manager(_meta) {
24
- var _this = this;
25
- _classCallCheck(this, Manager);
26
- _defineProperty(this, "stringify", function (str) {
27
- var obj = {
28
- _id: _this.manager_id,
29
- servers: _this.servers
1
+ import { _id } from "generalised-datastore/utils/functions";
2
+ import framer from "../framer";
3
+ import opcodes from "../opcodes";
4
+ import Account from "./Account";
5
+ import Blockweb from "./Blockweb";
6
+ import Oracle from "./Oracle";
7
+
8
+ class Manager {
9
+ constructor(meta) {
10
+ meta = meta || {};
11
+
12
+ this.compression = meta.compression;
13
+ this.manager_id = _id("manager");
14
+ this.accounts = new Object();
15
+ this.running = 0;
16
+ this.tracks = new Object();
17
+ this.web = new Blockweb(this);
18
+ this.oracle = new Oracle(this, {
19
+ compression: this.compression,
20
+ datastore: meta.datastore,
21
+ });
22
+ this.servers = new Array();
23
+ }
24
+
25
+ stringify = (str) => {
26
+ let obj = {
27
+ _id: this.manager_id,
28
+ servers: this.servers,
30
29
  };
30
+
31
31
  return str ? JSON.stringify(obj) : obj;
32
- });
33
- _defineProperty(this, "initiate", function (meta) {
34
- _this.initiator = _this.add_account(process.env.INITIATOR || "initiator", meta);
35
- return _this.initiator;
36
- });
37
- _defineProperty(this, "start_clock", function () {
38
- var frequency = Number(process.env.HERTS) || 10;
39
- _this.clock = setInterval(function () {
40
- for (var account in _this.tracks) {
41
- var track = _this.tracks[account];
42
- var program = track.slice(-1)[0];
32
+ };
33
+
34
+ initiate = (meta) => {
35
+ this.initiator = this.add_account(
36
+ process.env.INITIATOR || "initiator",
37
+ meta
38
+ );
39
+
40
+ return this.initiator;
41
+ };
42
+
43
+ start_clock = () => {
44
+ let frequency = Number(process.env.HERTS) || 10;
45
+ this.clock = setInterval(() => {
46
+ for (let account in this.tracks) {
47
+ let track = this.tracks[account];
48
+ let program = track.slice(-1)[0];
49
+
43
50
  if (program.hold) continue;
44
- var instruction = program.sequence[program.pointer];
51
+
52
+ let instruction = program.sequence[program.pointer];
53
+
45
54
  program.account.vm.execute(instruction, program);
46
55
  program.pointer++;
56
+
47
57
  if (program.pointer === program.sequence.length) {
48
58
  program.account.flush_buffer(program.pid);
59
+
49
60
  track.pop();
50
61
  }
51
62
  if (!track.length) {
52
- _this.running -= 1;
53
- delete _this.tracks[account];
63
+ this.running -= 1;
64
+ delete this.tracks[account];
54
65
  program.account.chain.mine(program.account.vm);
55
66
  }
56
67
  }
57
- !_this.running && clearInterval(_this.clock);
68
+
69
+ !this.running && clearInterval(this.clock);
58
70
  }, frequency);
59
- });
60
- _defineProperty(this, "get_account", function (name) {
61
- return _this.accounts[name];
62
- });
63
- _defineProperty(this, "add_account", function (name, meta) {
71
+ };
72
+
73
+ get_account = (name) => this.accounts[name];
74
+
75
+ add_account = (name, meta) => {
64
76
  meta = meta || {};
65
77
  if (!name) name = meta.name;
66
- var account;
78
+
79
+ let account;
67
80
  if (!name) {
68
- account = {
69
- error: true,
70
- error_message: "Name cannot be blank"
71
- };
81
+ account = { error: true, error_message: "Name cannot be blank" };
72
82
  } else {
73
- account = _this.get_account(name);
83
+ account = this.get_account(name);
84
+
74
85
  if (!account) {
75
- account = new _Account["default"](name, _objectSpread(_objectSpread({}, meta), {}, {
76
- manager: _this
77
- }));
78
- (0, _opcodes["default"])(account);
79
- (0, _framer["default"])(account, meta.on_framed);
80
- _this.accounts[account.name] = account;
86
+ account = new Account(name, { ...meta, manager: this });
87
+
88
+ opcodes(account);
89
+ framer(account, meta.on_framed);
90
+
91
+ this.accounts[account.name] = account;
81
92
  if (meta.string) account = account.stringify();
82
- } else if (meta && meta["private"] && !account["private"]) {
83
- account["private"] = true;
93
+ } else if (meta && meta.private && !account.private) {
94
+ account.private = true;
84
95
  }
85
96
  }
97
+
86
98
  return meta.string ? JSON.stringify(account) : account;
87
- });
88
- _defineProperty(this, "push", function (program) {
89
- var track = _this.tracks[program.account.name];
99
+ };
100
+
101
+ push = (program) => {
102
+ let track = this.tracks[program.account.name];
103
+
90
104
  if (track) {
91
- track.push(_objectSpread(_objectSpread({}, program), {}, {
92
- pointer: 0
93
- }));
105
+ track.push({ ...program, pointer: 0 });
94
106
  } else {
95
- track = [_objectSpread(_objectSpread({}, program), {}, {
96
- pointer: 0
97
- })];
98
- _this.tracks[program.account.name] = track;
99
- _this.running++;
100
- _this.running === 1 && _this.start_clock();
107
+ track = [{ ...program, pointer: 0 }];
108
+ this.tracks[program.account.name] = track;
109
+
110
+ this.running++;
111
+ this.running === 1 && this.start_clock();
101
112
  }
102
- });
103
- _defineProperty(this, "get_initiator", function () {
104
- return _this.initiator || _this.initiate();
105
- });
106
- _defineProperty(this, "endpoint", function (endpoint, payload, callback) {
107
- var account = _this.get_account(payload.account);
113
+ };
114
+
115
+ get_initiator = () => this.initiator || this.initiate();
116
+
117
+ endpoint = (endpoint, payload, callback) => {
118
+ let account = this.get_account(payload.account);
108
119
  if (!account) {
109
- return _this.get_initiator().run_callback(callback, {
120
+ return this.get_initiator().run_callback(callback, {
110
121
  error: true,
111
122
  error_message: "Account is not found",
112
- payload: payload,
113
- endpoint: endpoint
123
+ payload,
124
+ endpoint,
114
125
  });
115
126
  }
116
- if (!account.validate(payload.program || payload.payload, payload.signature, callback || payload.callback)) return;
117
- var method = account[endpoint];
118
- typeof method === "function" && method(_objectSpread(_objectSpread({}, payload), {}, {
119
- callback: callback
120
- }));
121
- });
122
- _defineProperty(this, "add_server", function (server) {
123
- _this.servers.push(server);
124
- });
125
- _meta = _meta || {};
126
- this.compression = _meta.compression;
127
- this.manager_id = (0, _functions._id)("manager");
128
- this.accounts = new Object();
129
- this.running = 0;
130
- this.tracks = new Object();
131
- this.web = new _Blockweb["default"](this);
132
- this.oracle = new _Oracle["default"](this, {
133
- compression: this.compression,
134
- datastore: _meta.datastore
135
- });
136
- this.servers = new Array();
137
- });
138
- var _default = Manager;
139
- exports["default"] = _default;
127
+
128
+ if (
129
+ !account.validate(
130
+ payload.program || payload.payload,
131
+ payload.signature,
132
+ callback || payload.callback
133
+ )
134
+ )
135
+ return;
136
+
137
+ let method = account[endpoint];
138
+ typeof method === "function" && method({ ...payload, callback });
139
+ };
140
+
141
+ add_server = (server) => {
142
+ this.servers.push(server);
143
+ };
144
+ }
145
+
146
+ export default Manager;
@@ -1,49 +1,49 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- var _framer = require("../framer");
8
- 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); }
9
- 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); } }
10
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
11
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
12
- 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; }
13
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
- 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); }
15
- var Opcodes = /*#__PURE__*/_createClass(function Opcodes() {
16
- var _this = this;
17
- _classCallCheck(this, Opcodes);
18
- _defineProperty(this, "set", function (opcode, circuit) {
19
- _this.opcodes[opcode] = circuit;
20
- _framer.obj.Opcodes[opcode] = null;
21
- _this.account.assembler.opcodes.push(opcode);
22
- });
23
- _defineProperty(this, "prepare_operation", function (opcode, context) {
24
- var circ = _this.opcodes[opcode];
1
+ import { obj } from "../framer";
2
+
3
+ class Opcodes {
4
+ constructor() {
5
+ this.opcodes = new Object();
6
+ }
7
+
8
+ set = (opcode, circuit) => {
9
+ this.opcodes[opcode] = circuit;
10
+ obj.Opcodes[opcode] = null;
11
+
12
+ this.account.assembler.opcodes.push(opcode);
13
+ };
14
+
15
+ prepare_operation = (opcode, context) => {
16
+ let circ = this.opcodes[opcode];
17
+
25
18
  if (typeof circ !== "function") return;
26
- var buf = context.get_buffer();
27
- var args = context.account.read(buf && buf.inputs) || _this.account.stdin.splice(-1)[0];
28
- var res = circ(args, _this);
29
- _this.stdin(res);
30
- });
31
- _defineProperty(this, "stdin", function (object, cb) {
19
+
20
+ let buf = context.get_buffer();
21
+
22
+ let args =
23
+ context.account.read(buf && buf.inputs) ||
24
+ this.account.stdin.splice(-1)[0];
25
+
26
+ let res = circ(args, this);
27
+
28
+ this.stdin(res);
29
+ };
30
+
31
+ stdin = (object, cb) => {
32
32
  if (object == null) {
33
- _this.flags["void"] = true;
33
+ this.flags.void = true;
34
34
  return;
35
35
  }
36
- _this.flags["void"] = false;
37
- _this.flags.zero = !object;
38
- if (typeof object === "number") _this.flags.neg = object < 0;
39
- var code_string = typeof object !== "string" ? JSON.stringify(object) : object;
40
- _this.account.assembler.run(code_string, {
41
- cb: cb,
42
- pure: true
43
- });
36
+ this.flags.void = false;
37
+
38
+ this.flags.zero = !object;
39
+ if (typeof object === "number") this.flags.neg = object < 0;
40
+
41
+ let code_string =
42
+ typeof object !== "string" ? JSON.stringify(object) : object;
43
+
44
+ this.account.assembler.run(code_string, { cb, pure: true });
44
45
  // console.log(`Writing in: ${code_string}`);
45
- });
46
- this.opcodes = new Object();
47
- });
48
- var _default = Opcodes;
49
- exports["default"] = _default;
46
+ };
47
+ }
48
+
49
+ export default Opcodes;
package/Objects/Oracle.js CHANGED
@@ -1,144 +1,108 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
- var _fs = _interopRequireDefault(require("fs"));
8
- var _generalisedDatastore = _interopRequireDefault(require("generalised-datastore"));
9
- var _hash = require("../utils/hash");
10
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
- 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; }
12
- 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); }
13
- 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); }); }; }
14
- 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); }
15
- 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); } }
16
- function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
17
- function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
18
- 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; }
19
- function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
20
- 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); }
21
- var Oracle = /*#__PURE__*/_createClass(function Oracle(mgr, meta) {
22
- var _this = this;
23
- _classCallCheck(this, Oracle);
24
- _defineProperty(this, "get_folder", function (physical_address, options) {
25
- if (_typeof(options) !== "object") {
26
- options = {
27
- no_new: options ? false : true
28
- };
1
+ import fs from "fs";
2
+ import GDS from "generalised-datastore";
3
+ import { hash } from "../utils/hash";
4
+
5
+ class Oracle {
6
+ constructor(mgr, meta) {
7
+ meta = meta || {};
8
+
9
+ this.compression = meta.compression;
10
+ this.fs = fs;
11
+ this.mgr = mgr;
12
+ this.datapaths = new Object();
13
+ this.gds = new GDS(
14
+ meta.datastore || process.env.DATASTORE || "godprotocol"
15
+ ).sync({
16
+ manager: this.mgr,
17
+ });
18
+ }
19
+
20
+ get_folder = (physical_address, options) => {
21
+ if (typeof options !== "object") {
22
+ options = { no_new: options ? false : true };
29
23
  }
30
- return _this.gds.folder(_this.hash(physical_address));
31
- });
32
- _defineProperty(this, "set", function (path, _ref) {
33
- var type = _ref.type,
34
- obj = _ref.obj;
35
- _this.datapaths[path] = {
36
- obj: obj,
37
- type: type
24
+ return this.gds.folder(this.hash(physical_address));
25
+ };
26
+
27
+ set = (path, { type, obj }) => {
28
+ this.datapaths[path] = {
29
+ obj,
30
+ type,
38
31
  };
39
- });
40
- _defineProperty(this, "get", function (path) {
41
- var obj = _this.datapaths[path];
32
+ };
33
+
34
+ get = (path) => {
35
+ let obj = this.datapaths[path];
36
+
42
37
  if (!obj) {
43
38
  try {
44
- obj = _this.fs.readFileSync("".concat(_this.mgr.initiator.root, "/").concat(path));
39
+ obj = this.fs.readFileSync(`${this.mgr.initiator.root}/${path}`);
45
40
  obj = JSON.parse(obj).payload;
46
- var type = obj && obj.payload && _typeof(obj.payload);
47
- type = type === "object" ? Array.isArray(obj) ? "array" : "twain" : type;
48
- obj = {
49
- type: type,
50
- obj: obj
51
- };
52
- _this.set(path, obj);
41
+ let type = obj && obj.payload && typeof obj.payload;
42
+ type =
43
+ type === "object" ? (Array.isArray(obj) ? "array" : "twain") : type;
44
+
45
+ obj = { type, obj };
46
+ this.set(path, obj);
53
47
  } catch (e) {}
54
48
  }
49
+
55
50
  return obj;
56
- });
57
- _defineProperty(this, "hash", function (data) {
51
+ };
52
+
53
+ hash = (data) => {
58
54
  data = typeof data !== "string" ? JSON.stringify(data) : data;
59
- return (0, _hash.hash)(data);
60
- });
61
- _defineProperty(this, "handle_compression", function (_ref2) {
62
- var addr = _ref2.addr,
63
- data = _ref2.data,
64
- no_string = _ref2.no_string;
65
- if (_this.compression) {
66
- var res = _this.compression({
67
- addr: addr,
68
- data: data,
69
- no_string: no_string
70
- });
55
+ return hash(data);
56
+ };
57
+
58
+ handle_compression = ({ addr, data, no_string }) => {
59
+ if (this.compression) {
60
+ let res = this.compression({ addr, data, no_string });
71
61
  if (res) data = res.data;
72
62
  }
73
- if (!no_string && (typeof addr !== "string" || typeof data !== "string")) return;
63
+ if (!no_string && (typeof addr !== "string" || typeof data !== "string"))
64
+ return;
65
+
74
66
  return data;
75
- });
76
- _defineProperty(this, "write", function (address, payload) {
77
- payload = JSON.stringify({
78
- payload: payload
79
- });
80
- var data = _this.handle_compression({
67
+ };
68
+
69
+ write = (address, payload) => {
70
+ payload = JSON.stringify({ payload });
71
+ let data = this.handle_compression({
81
72
  addr: address,
82
- data: payload
73
+ data: payload,
83
74
  });
84
- _this.fs.writeFileSync("".concat(_this.mgr.initiator.root, "/").concat(address), data, {
85
- encoding: "utf-8"
75
+
76
+ this.fs.writeFileSync(`${this.mgr.initiator.root}/${address}`, data, {
77
+ encoding: "utf-8",
86
78
  });
87
- });
88
- _defineProperty(this, "fetch", /*#__PURE__*/function () {
89
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(payload, callback) {
90
- var physical_address, config, remote, query, result, folder, op;
91
- return _regeneratorRuntime().wrap(function _callee$(_context) {
92
- while (1) switch (_context.prev = _context.next) {
93
- case 0:
94
- physical_address = payload.physical_address, config = payload.config, remote = payload.remote, query = payload.query;
95
- folder = _this.get_folder(physical_address);
96
- if (!config) {
97
- _context.next = 7;
98
- break;
99
- }
100
- result = folder.config.stringify();
101
- folder.add_remote(remote);
102
- _context.next = 16;
103
- break;
104
- case 7:
105
- if (folder.check_remote(query.operation)) {
106
- _context.next = 11;
107
- break;
108
- }
109
- result = {
110
- error: true,
111
- error_message: "Query operation cannot be performed remotely."
112
- };
113
- _context.next = 16;
114
- break;
115
- case 11:
116
- op = folder[query.operation];
117
- _context.next = 14;
118
- return op(query.query, query.options);
119
- case 14:
120
- result = _context.sent;
121
- folder.remote_stuff(remote, result);
122
- case 16:
123
- callback(result);
124
- case 17:
125
- case "end":
126
- return _context.stop();
127
- }
128
- }, _callee);
129
- }));
130
- return function (_x, _x2) {
131
- return _ref3.apply(this, arguments);
132
- };
133
- }());
134
- meta = meta || {};
135
- this.compression = meta.compression;
136
- this.fs = _fs["default"];
137
- this.mgr = mgr;
138
- this.datapaths = new Object();
139
- this.gds = new _generalisedDatastore["default"](meta.datastore || process.env.DATASTORE || "godprotocol").sync({
140
- manager: this.mgr
141
- });
142
- });
143
- var _default = Oracle;
144
- exports["default"] = _default;
79
+ };
80
+
81
+ fetch = async (payload, callback) => {
82
+ let { physical_address, config, remote, query } = payload,
83
+ result;
84
+
85
+ let folder = this.get_folder(physical_address);
86
+
87
+ if (config) {
88
+ result = folder.config.stringify();
89
+ folder.add_remote(remote);
90
+ } else {
91
+ if (!folder.check_remote(query.operation)) {
92
+ result = {
93
+ error: true,
94
+ error_message: "Query operation cannot be performed remotely.",
95
+ };
96
+ } else {
97
+ let op = folder[query.operation];
98
+
99
+ result = await op(query.query, query.options);
100
+ folder.remote_stuff(remote, result);
101
+ }
102
+ }
103
+
104
+ callback(result);
105
+ };
106
+ }
107
+
108
+ export default Oracle;