dcp-client 4.2.23 → 4.2.24

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.
@@ -2362,6 +2362,50 @@ eval("\n\nvar undefined;\n\nvar $SyntaxError = SyntaxError;\nvar $Function = Fun
2362
2362
 
2363
2363
  /***/ }),
2364
2364
 
2365
+ /***/ "./node_modules/gopd/index.js":
2366
+ /*!************************************!*\
2367
+ !*** ./node_modules/gopd/index.js ***!
2368
+ \************************************/
2369
+ /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
2370
+
2371
+ "use strict";
2372
+ eval("\n\nvar GetIntrinsic = __webpack_require__(/*! get-intrinsic */ \"./node_modules/gopd/node_modules/get-intrinsic/index.js\");\n\nvar $gOPD = GetIntrinsic('%Object.getOwnPropertyDescriptor%', true);\n\nif ($gOPD) {\n\ttry {\n\t\t$gOPD([], 'length');\n\t} catch (e) {\n\t\t// IE 8 has a broken gOPD\n\t\t$gOPD = null;\n\t}\n}\n\nmodule.exports = $gOPD;\n\n\n//# sourceURL=webpack://dcp/./node_modules/gopd/index.js?");
2373
+
2374
+ /***/ }),
2375
+
2376
+ /***/ "./node_modules/gopd/node_modules/get-intrinsic/index.js":
2377
+ /*!***************************************************************!*\
2378
+ !*** ./node_modules/gopd/node_modules/get-intrinsic/index.js ***!
2379
+ \***************************************************************/
2380
+ /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
2381
+
2382
+ "use strict";
2383
+ eval("\n\nvar undefined;\n\nvar $SyntaxError = SyntaxError;\nvar $Function = Function;\nvar $TypeError = TypeError;\n\n// eslint-disable-next-line consistent-return\nvar getEvalledConstructor = function (expressionSyntax) {\n\ttry {\n\t\treturn $Function('\"use strict\"; return (' + expressionSyntax + ').constructor;')();\n\t} catch (e) {}\n};\n\nvar $gOPD = Object.getOwnPropertyDescriptor;\nif ($gOPD) {\n\ttry {\n\t\t$gOPD({}, '');\n\t} catch (e) {\n\t\t$gOPD = null; // this is IE 8, which has a broken gOPD\n\t}\n}\n\nvar throwTypeError = function () {\n\tthrow new $TypeError();\n};\nvar ThrowTypeError = $gOPD\n\t? (function () {\n\t\ttry {\n\t\t\t// eslint-disable-next-line no-unused-expressions, no-caller, no-restricted-properties\n\t\t\targuments.callee; // IE 8 does not throw here\n\t\t\treturn throwTypeError;\n\t\t} catch (calleeThrows) {\n\t\t\ttry {\n\t\t\t\t// IE 8 throws on Object.getOwnPropertyDescriptor(arguments, '')\n\t\t\t\treturn $gOPD(arguments, 'callee').get;\n\t\t\t} catch (gOPDthrows) {\n\t\t\t\treturn throwTypeError;\n\t\t\t}\n\t\t}\n\t}())\n\t: throwTypeError;\n\nvar hasSymbols = __webpack_require__(/*! has-symbols */ \"./node_modules/gopd/node_modules/has-symbols/index.js\")();\n\nvar getProto = Object.getPrototypeOf || function (x) { return x.__proto__; }; // eslint-disable-line no-proto\n\nvar needsEval = {};\n\nvar TypedArray = typeof Uint8Array === 'undefined' ? undefined : getProto(Uint8Array);\n\nvar INTRINSICS = {\n\t'%AggregateError%': typeof AggregateError === 'undefined' ? undefined : AggregateError,\n\t'%Array%': Array,\n\t'%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,\n\t'%ArrayIteratorPrototype%': hasSymbols ? getProto([][Symbol.iterator]()) : undefined,\n\t'%AsyncFromSyncIteratorPrototype%': undefined,\n\t'%AsyncFunction%': needsEval,\n\t'%AsyncGenerator%': needsEval,\n\t'%AsyncGeneratorFunction%': needsEval,\n\t'%AsyncIteratorPrototype%': needsEval,\n\t'%Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,\n\t'%BigInt%': typeof BigInt === 'undefined' ? undefined : BigInt,\n\t'%BigInt64Array%': typeof BigInt64Array === 'undefined' ? undefined : BigInt64Array,\n\t'%BigUint64Array%': typeof BigUint64Array === 'undefined' ? undefined : BigUint64Array,\n\t'%Boolean%': Boolean,\n\t'%DataView%': typeof DataView === 'undefined' ? undefined : DataView,\n\t'%Date%': Date,\n\t'%decodeURI%': decodeURI,\n\t'%decodeURIComponent%': decodeURIComponent,\n\t'%encodeURI%': encodeURI,\n\t'%encodeURIComponent%': encodeURIComponent,\n\t'%Error%': Error,\n\t'%eval%': eval, // eslint-disable-line no-eval\n\t'%EvalError%': EvalError,\n\t'%Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,\n\t'%Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,\n\t'%FinalizationRegistry%': typeof FinalizationRegistry === 'undefined' ? undefined : FinalizationRegistry,\n\t'%Function%': $Function,\n\t'%GeneratorFunction%': needsEval,\n\t'%Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,\n\t'%Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,\n\t'%Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,\n\t'%isFinite%': isFinite,\n\t'%isNaN%': isNaN,\n\t'%IteratorPrototype%': hasSymbols ? getProto(getProto([][Symbol.iterator]())) : undefined,\n\t'%JSON%': typeof JSON === 'object' ? JSON : undefined,\n\t'%Map%': typeof Map === 'undefined' ? undefined : Map,\n\t'%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols ? undefined : getProto(new Map()[Symbol.iterator]()),\n\t'%Math%': Math,\n\t'%Number%': Number,\n\t'%Object%': Object,\n\t'%parseFloat%': parseFloat,\n\t'%parseInt%': parseInt,\n\t'%Promise%': typeof Promise === 'undefined' ? undefined : Promise,\n\t'%Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,\n\t'%RangeError%': RangeError,\n\t'%ReferenceError%': ReferenceError,\n\t'%Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,\n\t'%RegExp%': RegExp,\n\t'%Set%': typeof Set === 'undefined' ? undefined : Set,\n\t'%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols ? undefined : getProto(new Set()[Symbol.iterator]()),\n\t'%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,\n\t'%String%': String,\n\t'%StringIteratorPrototype%': hasSymbols ? getProto(''[Symbol.iterator]()) : undefined,\n\t'%Symbol%': hasSymbols ? Symbol : undefined,\n\t'%SyntaxError%': $SyntaxError,\n\t'%ThrowTypeError%': ThrowTypeError,\n\t'%TypedArray%': TypedArray,\n\t'%TypeError%': $TypeError,\n\t'%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,\n\t'%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,\n\t'%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,\n\t'%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,\n\t'%URIError%': URIError,\n\t'%WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,\n\t'%WeakRef%': typeof WeakRef === 'undefined' ? undefined : WeakRef,\n\t'%WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet\n};\n\ntry {\n\tnull.error; // eslint-disable-line no-unused-expressions\n} catch (e) {\n\t// https://github.com/tc39/proposal-shadowrealm/pull/384#issuecomment-1364264229\n\tvar errorProto = getProto(getProto(e));\n\tINTRINSICS['%Error.prototype%'] = errorProto;\n}\n\nvar doEval = function doEval(name) {\n\tvar value;\n\tif (name === '%AsyncFunction%') {\n\t\tvalue = getEvalledConstructor('async function () {}');\n\t} else if (name === '%GeneratorFunction%') {\n\t\tvalue = getEvalledConstructor('function* () {}');\n\t} else if (name === '%AsyncGeneratorFunction%') {\n\t\tvalue = getEvalledConstructor('async function* () {}');\n\t} else if (name === '%AsyncGenerator%') {\n\t\tvar fn = doEval('%AsyncGeneratorFunction%');\n\t\tif (fn) {\n\t\t\tvalue = fn.prototype;\n\t\t}\n\t} else if (name === '%AsyncIteratorPrototype%') {\n\t\tvar gen = doEval('%AsyncGenerator%');\n\t\tif (gen) {\n\t\t\tvalue = getProto(gen.prototype);\n\t\t}\n\t}\n\n\tINTRINSICS[name] = value;\n\n\treturn value;\n};\n\nvar LEGACY_ALIASES = {\n\t'%ArrayBufferPrototype%': ['ArrayBuffer', 'prototype'],\n\t'%ArrayPrototype%': ['Array', 'prototype'],\n\t'%ArrayProto_entries%': ['Array', 'prototype', 'entries'],\n\t'%ArrayProto_forEach%': ['Array', 'prototype', 'forEach'],\n\t'%ArrayProto_keys%': ['Array', 'prototype', 'keys'],\n\t'%ArrayProto_values%': ['Array', 'prototype', 'values'],\n\t'%AsyncFunctionPrototype%': ['AsyncFunction', 'prototype'],\n\t'%AsyncGenerator%': ['AsyncGeneratorFunction', 'prototype'],\n\t'%AsyncGeneratorPrototype%': ['AsyncGeneratorFunction', 'prototype', 'prototype'],\n\t'%BooleanPrototype%': ['Boolean', 'prototype'],\n\t'%DataViewPrototype%': ['DataView', 'prototype'],\n\t'%DatePrototype%': ['Date', 'prototype'],\n\t'%ErrorPrototype%': ['Error', 'prototype'],\n\t'%EvalErrorPrototype%': ['EvalError', 'prototype'],\n\t'%Float32ArrayPrototype%': ['Float32Array', 'prototype'],\n\t'%Float64ArrayPrototype%': ['Float64Array', 'prototype'],\n\t'%FunctionPrototype%': ['Function', 'prototype'],\n\t'%Generator%': ['GeneratorFunction', 'prototype'],\n\t'%GeneratorPrototype%': ['GeneratorFunction', 'prototype', 'prototype'],\n\t'%Int8ArrayPrototype%': ['Int8Array', 'prototype'],\n\t'%Int16ArrayPrototype%': ['Int16Array', 'prototype'],\n\t'%Int32ArrayPrototype%': ['Int32Array', 'prototype'],\n\t'%JSONParse%': ['JSON', 'parse'],\n\t'%JSONStringify%': ['JSON', 'stringify'],\n\t'%MapPrototype%': ['Map', 'prototype'],\n\t'%NumberPrototype%': ['Number', 'prototype'],\n\t'%ObjectPrototype%': ['Object', 'prototype'],\n\t'%ObjProto_toString%': ['Object', 'prototype', 'toString'],\n\t'%ObjProto_valueOf%': ['Object', 'prototype', 'valueOf'],\n\t'%PromisePrototype%': ['Promise', 'prototype'],\n\t'%PromiseProto_then%': ['Promise', 'prototype', 'then'],\n\t'%Promise_all%': ['Promise', 'all'],\n\t'%Promise_reject%': ['Promise', 'reject'],\n\t'%Promise_resolve%': ['Promise', 'resolve'],\n\t'%RangeErrorPrototype%': ['RangeError', 'prototype'],\n\t'%ReferenceErrorPrototype%': ['ReferenceError', 'prototype'],\n\t'%RegExpPrototype%': ['RegExp', 'prototype'],\n\t'%SetPrototype%': ['Set', 'prototype'],\n\t'%SharedArrayBufferPrototype%': ['SharedArrayBuffer', 'prototype'],\n\t'%StringPrototype%': ['String', 'prototype'],\n\t'%SymbolPrototype%': ['Symbol', 'prototype'],\n\t'%SyntaxErrorPrototype%': ['SyntaxError', 'prototype'],\n\t'%TypedArrayPrototype%': ['TypedArray', 'prototype'],\n\t'%TypeErrorPrototype%': ['TypeError', 'prototype'],\n\t'%Uint8ArrayPrototype%': ['Uint8Array', 'prototype'],\n\t'%Uint8ClampedArrayPrototype%': ['Uint8ClampedArray', 'prototype'],\n\t'%Uint16ArrayPrototype%': ['Uint16Array', 'prototype'],\n\t'%Uint32ArrayPrototype%': ['Uint32Array', 'prototype'],\n\t'%URIErrorPrototype%': ['URIError', 'prototype'],\n\t'%WeakMapPrototype%': ['WeakMap', 'prototype'],\n\t'%WeakSetPrototype%': ['WeakSet', 'prototype']\n};\n\nvar bind = __webpack_require__(/*! function-bind */ \"./node_modules/function-bind/index.js\");\nvar hasOwn = __webpack_require__(/*! has */ \"./node_modules/has/src/index.js\");\nvar $concat = bind.call(Function.call, Array.prototype.concat);\nvar $spliceApply = bind.call(Function.apply, Array.prototype.splice);\nvar $replace = bind.call(Function.call, String.prototype.replace);\nvar $strSlice = bind.call(Function.call, String.prototype.slice);\nvar $exec = bind.call(Function.call, RegExp.prototype.exec);\n\n/* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */\nvar rePropName = /[^%.[\\]]+|\\[(?:(-?\\d+(?:\\.\\d+)?)|([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2)\\]|(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|%$))/g;\nvar reEscapeChar = /\\\\(\\\\)?/g; /** Used to match backslashes in property paths. */\nvar stringToPath = function stringToPath(string) {\n\tvar first = $strSlice(string, 0, 1);\n\tvar last = $strSlice(string, -1);\n\tif (first === '%' && last !== '%') {\n\t\tthrow new $SyntaxError('invalid intrinsic syntax, expected closing `%`');\n\t} else if (last === '%' && first !== '%') {\n\t\tthrow new $SyntaxError('invalid intrinsic syntax, expected opening `%`');\n\t}\n\tvar result = [];\n\t$replace(string, rePropName, function (match, number, quote, subString) {\n\t\tresult[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match;\n\t});\n\treturn result;\n};\n/* end adaptation */\n\nvar getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {\n\tvar intrinsicName = name;\n\tvar alias;\n\tif (hasOwn(LEGACY_ALIASES, intrinsicName)) {\n\t\talias = LEGACY_ALIASES[intrinsicName];\n\t\tintrinsicName = '%' + alias[0] + '%';\n\t}\n\n\tif (hasOwn(INTRINSICS, intrinsicName)) {\n\t\tvar value = INTRINSICS[intrinsicName];\n\t\tif (value === needsEval) {\n\t\t\tvalue = doEval(intrinsicName);\n\t\t}\n\t\tif (typeof value === 'undefined' && !allowMissing) {\n\t\t\tthrow new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');\n\t\t}\n\n\t\treturn {\n\t\t\talias: alias,\n\t\t\tname: intrinsicName,\n\t\t\tvalue: value\n\t\t};\n\t}\n\n\tthrow new $SyntaxError('intrinsic ' + name + ' does not exist!');\n};\n\nmodule.exports = function GetIntrinsic(name, allowMissing) {\n\tif (typeof name !== 'string' || name.length === 0) {\n\t\tthrow new $TypeError('intrinsic name must be a non-empty string');\n\t}\n\tif (arguments.length > 1 && typeof allowMissing !== 'boolean') {\n\t\tthrow new $TypeError('\"allowMissing\" argument must be a boolean');\n\t}\n\n\tif ($exec(/^%?[^%]*%?$/, name) === null) {\n\t\tthrow new $SyntaxError('`%` may not be present anywhere but at the beginning and end of the intrinsic name');\n\t}\n\tvar parts = stringToPath(name);\n\tvar intrinsicBaseName = parts.length > 0 ? parts[0] : '';\n\n\tvar intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing);\n\tvar intrinsicRealName = intrinsic.name;\n\tvar value = intrinsic.value;\n\tvar skipFurtherCaching = false;\n\n\tvar alias = intrinsic.alias;\n\tif (alias) {\n\t\tintrinsicBaseName = alias[0];\n\t\t$spliceApply(parts, $concat([0, 1], alias));\n\t}\n\n\tfor (var i = 1, isOwn = true; i < parts.length; i += 1) {\n\t\tvar part = parts[i];\n\t\tvar first = $strSlice(part, 0, 1);\n\t\tvar last = $strSlice(part, -1);\n\t\tif (\n\t\t\t(\n\t\t\t\t(first === '\"' || first === \"'\" || first === '`')\n\t\t\t\t|| (last === '\"' || last === \"'\" || last === '`')\n\t\t\t)\n\t\t\t&& first !== last\n\t\t) {\n\t\t\tthrow new $SyntaxError('property names with quotes must have matching quotes');\n\t\t}\n\t\tif (part === 'constructor' || !isOwn) {\n\t\t\tskipFurtherCaching = true;\n\t\t}\n\n\t\tintrinsicBaseName += '.' + part;\n\t\tintrinsicRealName = '%' + intrinsicBaseName + '%';\n\n\t\tif (hasOwn(INTRINSICS, intrinsicRealName)) {\n\t\t\tvalue = INTRINSICS[intrinsicRealName];\n\t\t} else if (value != null) {\n\t\t\tif (!(part in value)) {\n\t\t\t\tif (!allowMissing) {\n\t\t\t\t\tthrow new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.');\n\t\t\t\t}\n\t\t\t\treturn void undefined;\n\t\t\t}\n\t\t\tif ($gOPD && (i + 1) >= parts.length) {\n\t\t\t\tvar desc = $gOPD(value, part);\n\t\t\t\tisOwn = !!desc;\n\n\t\t\t\t// By convention, when a data property is converted to an accessor\n\t\t\t\t// property to emulate a data property that does not suffer from\n\t\t\t\t// the override mistake, that accessor's getter is marked with\n\t\t\t\t// an `originalValue` property. Here, when we detect this, we\n\t\t\t\t// uphold the illusion by pretending to see that original data\n\t\t\t\t// property, i.e., returning the value rather than the getter\n\t\t\t\t// itself.\n\t\t\t\tif (isOwn && 'get' in desc && !('originalValue' in desc.get)) {\n\t\t\t\t\tvalue = desc.get;\n\t\t\t\t} else {\n\t\t\t\t\tvalue = value[part];\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tisOwn = hasOwn(value, part);\n\t\t\t\tvalue = value[part];\n\t\t\t}\n\n\t\t\tif (isOwn && !skipFurtherCaching) {\n\t\t\t\tINTRINSICS[intrinsicRealName] = value;\n\t\t\t}\n\t\t}\n\t}\n\treturn value;\n};\n\n\n//# sourceURL=webpack://dcp/./node_modules/gopd/node_modules/get-intrinsic/index.js?");
2384
+
2385
+ /***/ }),
2386
+
2387
+ /***/ "./node_modules/gopd/node_modules/has-symbols/index.js":
2388
+ /*!*************************************************************!*\
2389
+ !*** ./node_modules/gopd/node_modules/has-symbols/index.js ***!
2390
+ \*************************************************************/
2391
+ /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
2392
+
2393
+ "use strict";
2394
+ eval("\n\nvar origSymbol = typeof Symbol !== 'undefined' && Symbol;\nvar hasSymbolSham = __webpack_require__(/*! ./shams */ \"./node_modules/gopd/node_modules/has-symbols/shams.js\");\n\nmodule.exports = function hasNativeSymbols() {\n\tif (typeof origSymbol !== 'function') { return false; }\n\tif (typeof Symbol !== 'function') { return false; }\n\tif (typeof origSymbol('foo') !== 'symbol') { return false; }\n\tif (typeof Symbol('bar') !== 'symbol') { return false; }\n\n\treturn hasSymbolSham();\n};\n\n\n//# sourceURL=webpack://dcp/./node_modules/gopd/node_modules/has-symbols/index.js?");
2395
+
2396
+ /***/ }),
2397
+
2398
+ /***/ "./node_modules/gopd/node_modules/has-symbols/shams.js":
2399
+ /*!*************************************************************!*\
2400
+ !*** ./node_modules/gopd/node_modules/has-symbols/shams.js ***!
2401
+ \*************************************************************/
2402
+ /***/ ((module) => {
2403
+
2404
+ "use strict";
2405
+ eval("\n\n/* eslint complexity: [2, 18], max-statements: [2, 33] */\nmodule.exports = function hasSymbols() {\n\tif (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }\n\tif (typeof Symbol.iterator === 'symbol') { return true; }\n\n\tvar obj = {};\n\tvar sym = Symbol('test');\n\tvar symObj = Object(sym);\n\tif (typeof sym === 'string') { return false; }\n\n\tif (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; }\n\tif (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; }\n\n\t// temp disabled per https://github.com/ljharb/object.assign/issues/17\n\t// if (sym instanceof Symbol) { return false; }\n\t// temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4\n\t// if (!(symObj instanceof Symbol)) { return false; }\n\n\t// if (typeof Symbol.prototype.toString !== 'function') { return false; }\n\t// if (String(sym) !== Symbol.prototype.toString.call(sym)) { return false; }\n\n\tvar symVal = 42;\n\tobj[sym] = symVal;\n\tfor (sym in obj) { return false; } // eslint-disable-line no-restricted-syntax, no-unreachable-loop\n\tif (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; }\n\n\tif (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; }\n\n\tvar syms = Object.getOwnPropertySymbols(obj);\n\tif (syms.length !== 1 || syms[0] !== sym) { return false; }\n\n\tif (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; }\n\n\tif (typeof Object.getOwnPropertyDescriptor === 'function') {\n\t\tvar descriptor = Object.getOwnPropertyDescriptor(obj, sym);\n\t\tif (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; }\n\t}\n\n\treturn true;\n};\n\n\n//# sourceURL=webpack://dcp/./node_modules/gopd/node_modules/has-symbols/shams.js?");
2406
+
2407
+ /***/ }),
2408
+
2365
2409
  /***/ "./node_modules/has-property-descriptors/index.js":
2366
2410
  /*!********************************************************!*\
2367
2411
  !*** ./node_modules/has-property-descriptors/index.js ***!
@@ -3869,7 +3913,7 @@ eval("// Copyright Joyent, Inc. and other Node contributors.\n//\n// Permission
3869
3913
  /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
3870
3914
 
3871
3915
  "use strict";
3872
- eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Modal)\n/* harmony export */ });\n/**\n * A Small Modal Class\n * @module Modal\n */\n/* globals Event dcpConfig */\nclass Modal {\n constructor (title, message, callback = false, exitHandler = false, {\n continueLabel = 'Continue',\n cancelLabel = 'Cancel',\n cancelVisible = true\n } = {}) {\n const modal = document.createElement('div')\n modal.className = 'dcp-modal-container-old day'\n modal.innerHTML = `\n <dialog class=\"dcp-modal-content\">\n <div class=\"dcp-modal-header\">\n <h2>${title}<button type=\"button\" class=\"close\">&times;</button></h2>\n ${message ? '<p>' + message + '</p>' : ''}\n </div>\n <div class=\"dcp-modal-loading hidden\">\n <div class='loading'></div>\n </div>\n <form onsubmit='return false' method=\"dialog\">\n <div class=\"dcp-modal-body\"></div>\n <div class=\"dcp-modal-footer ${cancelVisible ? '' : 'centered'}\">\n <button type=\"submit\" class=\"continue green-modal-button\">${continueLabel}</button>\n <button type=\"button\" class=\"cancel green-modal-button\">${cancelLabel}</button>\n </div>\n </form>\n </dialog>`\n\n // To give a reference to do developer who wants to override the form submit.\n // May occur if they want to validate the information in the backend\n // without closing the modal prematurely.\n this.form = modal.querySelector('.dcp-modal-content form')\n this.continueButton = modal.querySelector('.dcp-modal-footer button.continue')\n this.cancelButton = modal.querySelector('.dcp-modal-footer button.cancel')\n this.closeButton = modal.querySelector('.dcp-modal-header .close')\n if (!cancelVisible) {\n this.cancelButton.style.display = 'none'\n }\n\n // To remove the event listener, the reference to the original function\n // added is required.\n this.formSubmitHandler = this.continue.bind(this)\n\n modal.addEventListener('keydown', function (event) {\n event.stopPropagation()\n // 27 is the keycode for the escape key.\n if (event.keyCode === 27) this.close()\n }.bind(this))\n\n this.container = modal\n this.callback = callback\n this.exitHandler = exitHandler\n document.body.appendChild(modal)\n }\n\n changeFormSubmitHandler (newFormSubmitHandler) {\n this.formSubmitHandler = newFormSubmitHandler\n }\n\n /**\n * Validates the form values in the modal and calls the modal's callback\n */\n async continue (event) {\n // To further prevent form submission from trying to redirect from the\n // current page.\n if (event instanceof Event) {\n event.preventDefault()\n }\n let fieldsAreValid = true\n let formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input, .dcp-modal-body textarea')\n\n const formValues = []\n if (typeof formElements.length === 'undefined') formElements = [formElements]\n // Separate into two loops to enable input validation requiring formValues\n // that come after it. e.g. Two password fields matching.\n for (let i = 0; i < formElements.length; i++) {\n switch (formElements[i].type) {\n case 'file':\n formValues.push(formElements[i])\n break\n case 'checkbox':\n formValues.push(formElements[i].checked)\n break\n default:\n formValues.push(formElements[i].value)\n break\n }\n }\n for (let i = 0; i < formElements.length; i++) {\n if (formElements[i].validation) {\n // Optional fields are allowed to be empty but still can't be wrong if not empty.\n if (!(formElements[i].value === '' && !formElements[i].required)) {\n if (typeof formElements[i].validation === 'function') {\n if (!formElements[i].validation(formValues)) {\n fieldsAreValid = false\n formElements[i].classList.add('is-invalid')\n }\n } else if (!formElements[i].validation.test(formElements[i].value)) {\n fieldsAreValid = false\n formElements[i].classList.add('is-invalid')\n }\n }\n }\n }\n\n if (!fieldsAreValid) return\n\n this.loading()\n if (typeof this.callback === 'function') {\n try {\n return this.callback(formValues)\n } catch (error) {\n console.error('Unexpected error in modal.continue:', error);\n return this.close(false)\n }\n }\n this.close(true)\n }\n\n loading () {\n this.container.querySelector('.dcp-modal-loading').classList.remove('hidden')\n this.container.querySelector('.dcp-modal-body').classList.add('hidden')\n this.container.querySelector('.dcp-modal-footer').classList.add('hidden')\n }\n\n open () {\n this.form.addEventListener('submit', async (event) => {\n const success = await this.formSubmitHandler(event)\n if (success === false) {\n return\n }\n this.close(true)\n })\n // When the user clicks on <span> (x), close the modal\n this.closeButton.addEventListener('click', this.close.bind(this))\n this.cancelButton.addEventListener('click', this.close.bind(this))\n\n // Prevent lingering outlines after clicking some form elements.\n this.container.querySelectorAll('.dcp-modal-body button, .dcp-modal-body input[type=\"checkbox\"]').forEach(element => {\n element.addEventListener('click', () => {\n element.blur()\n })\n })\n\n // Show the modal.\n this.container.style.display = 'block'\n\n const formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input')\n if (formElements.length) {\n formElements[0].focus()\n if (formElements[0].type === 'text') {\n formElements[0].select()\n }\n for (const el of formElements) {\n if (el.realType) {\n el.type = el.realType\n }\n }\n } else {\n // With no form elements to allow for form submission on enter, focus the\n // continue button.\n this.container.querySelector('.dcp-modal-footer button.continue').focus()\n }\n } // TODO: This should return a promise with the action resolving it\n\n /**\n * Shows the modal and returns a promise of the result of the modal (e.g. was\n * it closed, did its action succeed?)\n */\n showModal () {\n return new Promise((resolve, reject) => {\n this.form.addEventListener('submit', handleContinue.bind(this))\n this.cancelButton.addEventListener('click', handleCancel.bind(this))\n this.closeButton.addEventListener('click', handleCancel.bind(this))\n\n // Prevent lingering outlines after clicking some form elements.\n this.container.querySelectorAll('.dcp-modal-body button, .dcp-modal-body input[type=\"checkbox\"]').forEach(element => {\n element.addEventListener('click', () => {\n element.blur()\n })\n })\n\n // Show the modal.\n this.container.style.display = 'block'\n\n const formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input')\n if (formElements.length) {\n formElements[0].focus()\n if (formElements[0].type === 'text') {\n formElements[0].select()\n }\n for (const el of formElements) {\n if (el.realType) {\n el.type = el.realType\n }\n }\n } else {\n // With no form elements to allow for form submission on enter, focus the\n // continue button.\n this.continueButton.focus()\n }\n\n async function handleContinue (event) {\n let result\n try {\n result = await this.formSubmitHandler(event)\n } catch (error) {\n reject(error)\n }\n this.close(true)\n resolve(result)\n }\n\n async function handleCancel () {\n let result\n try {\n result = await this.close()\n } catch (error) {\n reject(error)\n }\n resolve(result)\n }\n })\n }\n\n close (success = false) {\n this.container.style.display = 'none'\n if (this.container.parentNode) {\n this.container.parentNode.removeChild(this.container)\n }\n\n // @todo this needs to remove eventlisteners to prevent memory leaks\n\n if ((success !== true) && typeof this.exitHandler === 'function') {\n return this.exitHandler(this)\n }\n }\n\n /**\n * Adds different form elements to the modal depending on the case.\n *\n * @param {*} elements - The properties of the form elements to add.\n * @returns {HTMLElement} The input form elements.\n */\n addFormElement (...elements) {\n const body = this.container.querySelector('.dcp-modal-body')\n const inputElements = []\n let label\n for (let i = 0; i < elements.length; i++) {\n let row = document.createElement('div')\n row.className = 'row'\n\n let col, input\n switch (elements[i].type) {\n case 'button':\n col = document.createElement('div')\n col.className = 'col-md-12'\n\n input = document.createElement('button')\n input.innerHTML = elements[i].label\n input.type = 'button'\n input.classList.add('green-modal-button')\n if (!elements[i].onclick) {\n throw new Error('A button in the modal body should have an on click event handler.')\n }\n input.addEventListener('click', elements[i].onclick)\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'textarea':\n col = document.createElement('div')\n col.className = 'col-md-12'\n\n input = document.createElement('textarea')\n input.className = 'text-input-field form-control'\n if (elements[i].placeholder) input.placeholder = elements[i].placeholder\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'text':\n case 'email':\n case 'number':\n case 'password': {\n const inputCol = document.createElement('div')\n\n input = document.createElement('input')\n input.type = elements[i].type\n input.validation = elements[i].validation\n input.autocomplete = elements[i].autocomplete || (elements[i].type === 'password' ? 'off' : 'on')\n input.className = 'text-input-field form-control'\n\n // Adding bootstraps custom feedback styles.\n let invalidFeedback = null\n if (elements[i].invalidFeedback) {\n invalidFeedback = document.createElement('div')\n invalidFeedback.className = 'invalid-feedback'\n invalidFeedback.innerText = elements[i].invalidFeedback\n }\n\n if (elements[i].type === 'password') {\n elements[i].realType = 'password'\n }\n\n if (elements[i].label) {\n const labelCol = document.createElement('div')\n label = document.createElement('label')\n label.innerText = elements[i].label\n const inputId = 'dcp-modal-input-' + this.container.querySelectorAll('input[type=\"text\"], input[type=\"email\"], input[type=\"number\"], input[type=\"password\"]').length\n label.setAttribute('for', inputId)\n input.id = inputId\n labelCol.classList.add('col-md-6', 'label-column')\n labelCol.appendChild(label)\n row.appendChild(labelCol)\n inputCol.className = 'col-md-6'\n } else {\n inputCol.className = 'col-md-12'\n }\n\n inputCol.appendChild(input)\n if (invalidFeedback !== null) {\n inputCol.appendChild(invalidFeedback)\n }\n row.appendChild(inputCol)\n break\n }\n case 'select':\n col = document.createElement('div')\n col.className = 'col-md-4'\n\n label = document.createElement('span')\n label.innerText = elements[i].label\n\n col.appendChild(label)\n row.appendChild(col)\n\n col = document.createElement('div')\n col.className = 'col-md-8'\n\n input = document.createElement('select')\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'checkbox': {\n row.classList.add('checkbox-row')\n const checkboxLabelCol = document.createElement('div')\n checkboxLabelCol.classList.add('label-column', 'checkbox-label-column')\n\n label = document.createElement('label')\n label.innerText = elements[i].label\n label.for = 'dcp-checkbox-input-' + this.container.querySelectorAll('input[type=\"checkbox\"]').length\n label.setAttribute('for', label.for)\n label.className = 'checkbox-label'\n\n checkboxLabelCol.appendChild(label)\n\n const checkboxCol = document.createElement('div')\n checkboxCol.classList.add('checkbox-column')\n\n input = document.createElement('input')\n input.type = 'checkbox'\n input.id = label.for\n if (elements[i].checked) {\n input.checked = true\n }\n\n checkboxCol.appendChild(input)\n\n if (elements[i].labelToTheRightOfCheckbox) {\n checkboxCol.classList.add('col-md-5')\n row.appendChild(checkboxCol)\n checkboxLabelCol.classList.add('col-md-7')\n row.appendChild(checkboxLabelCol)\n } else {\n checkboxLabelCol.classList.add('col-md-6')\n checkboxCol.classList.add('col-md-6')\n row.appendChild(checkboxLabelCol)\n row.appendChild(checkboxCol)\n }\n break\n }\n case 'file':\n [input, row] = this.addFileInput(elements[i], input, row)\n break\n case 'label':\n row.classList.add('label-row')\n label = document.createElement('label')\n label.innerText = elements[i].label\n row.appendChild(label)\n break\n }\n\n // Copy other possibly specified element properties:\n const inputPropertyNames = ['title', 'inputmode', 'value', 'minLength', 'maxLength', 'size', 'required', 'pattern', 'min', 'max', 'step', 'placeholder', 'accept', 'multiple', 'id', 'onkeypress', 'oninput', 'for', 'readonly', 'autocomplete']\n for (const propertyName of inputPropertyNames) {\n if (Object.prototype.hasOwnProperty.call(elements[i], propertyName)) {\n if (propertyName === 'for' && !label.hasAttribute(propertyName)) {\n label.setAttribute(propertyName, elements[i][propertyName])\n }\n if (propertyName.startsWith('on')) {\n input.addEventListener(propertyName.slice(2), elements[i][propertyName])\n } else {\n input.setAttribute(propertyName, elements[i][propertyName])\n }\n }\n }\n\n inputElements.push(input)\n body.appendChild(row)\n }\n\n if (inputElements.length === 1) return inputElements[0]\n else return inputElements\n }\n\n /**\n * Adds a drag and drop file form element to the modal.\n *\n * @param {*} fileInputProperties - An object specifying some of the\n * properties of the file input element.\n * @param {*} fileInput - Placeholders to help create the file\n * input.\n * @param {HTMLDivElement} row - Placeholders to help create the file\n * input.\n */\n addFileInput (fileInputProperties, fileInput, row) {\n // Adding the upload label.\n const uploadLabel = document.createElement('label')\n uploadLabel.innerText = fileInputProperties.label\n row.appendChild(uploadLabel)\n const body = this.container.querySelector('.dcp-modal-body')\n body.appendChild(row)\n const fileSelectionRow = document.createElement('div')\n fileSelectionRow.id = 'file-selection-row'\n\n // Adding the drag and drop file upload input.\n const dropContainer = document.createElement('div')\n dropContainer.id = 'drop-container'\n\n // Adding an image of a wallet\n const imageContainer = document.createElement('div')\n imageContainer.id = 'image-container'\n const walletImage = document.createElement('span')\n walletImage.classList.add('fas', 'fa-wallet')\n imageContainer.appendChild(walletImage)\n\n // Adding some text prompts\n const dropMessage = document.createElement('span')\n dropMessage.innerText = 'Drop a keystore file here'\n const orMessage = document.createElement('span')\n orMessage.innerText = 'or'\n\n // Adding the manual file input element (hiding the default one)\n const fileInputContainer = document.createElement('div')\n const fileInputLabel = document.createElement('label')\n // Linking the label to the file input so that clicking on the label\n // activates the file input.\n fileInputLabel.setAttribute('for', 'file-input')\n fileInputLabel.innerText = 'Browse'\n fileInput = document.createElement('input')\n fileInput.type = fileInputProperties.type\n fileInput.id = 'file-input'\n // To remove the lingering outline after selecting the file.\n fileInput.addEventListener('click', () => {\n fileInput.blur()\n })\n fileInputContainer.append(fileInput, fileInputLabel)\n\n // Creating the final row element to append to the modal body.\n dropContainer.append(imageContainer, dropMessage, orMessage, fileInputContainer)\n fileSelectionRow.appendChild(dropContainer)\n\n // Adding functionality to the drag and drop file input.\n dropContainer.addEventListener('drop', selectDroppedFile.bind(this))\n dropContainer.addEventListener('drop', unhighlightDropArea)\n // Prevent file from being opened by the browser.\n dropContainer.ondragover = highlightDropArea\n dropContainer.ondragenter = highlightDropArea\n dropContainer.ondragleave = unhighlightDropArea\n\n fileInput.addEventListener('change', handleFileChange)\n\n const fileNamePlaceholder = document.createElement('center')\n fileNamePlaceholder.id = 'file-name-placeholder'\n fileNamePlaceholder.className = 'row'\n fileNamePlaceholder.innerText = ''\n fileSelectionRow.appendChild(fileNamePlaceholder)\n fileNamePlaceholder.classList.add('hidden')\n\n // Check if the continue button is invalid on the keystore upload modal and\n // click it if it should no longer be invalid.\n this.continueButton.addEventListener('invalid', () => {\n const fileFormElements = this.container.querySelectorAll('.dcp-modal-body input[type=\"file\"], .dcp-modal-body input[type=\"text\"]')\n const filledInFileFormElements = Array.from(fileFormElements).filter(fileFormElement => fileFormElement.value !== '')\n if (fileFormElements.length !== 0 && filledInFileFormElements.length !== 0) {\n this.continueButton.setCustomValidity('')\n // Clicking instead of dispatching a submit event to ensure other form validation is used before submitting the form.\n this.continueButton.click()\n }\n })\n\n return [fileInput, fileSelectionRow]\n\n /**\n * Checks that the dropped items contain only a single keystore file.\n * If valid, sets the file input's value to the dropped file.\n * @param {DragEvent} event - Contains the files dropped.\n */\n function selectDroppedFile (event) {\n // Prevent file from being opened.\n event.preventDefault()\n\n // Check if only one file was dropped.\n const wasOneFileDropped = event.dataTransfer.items.length === 1 ||\n event.dataTransfer.files.length === 1\n updateFileSelectionStatus(wasOneFileDropped)\n if (!wasOneFileDropped) {\n fileInput.setCustomValidity('Only one file can be uploaded.')\n fileInput.reportValidity()\n return\n } else {\n fileInput.setCustomValidity('')\n }\n\n // Now to use the DataTransfer interface to access the file(s), setting\n // the value of the file input.\n const file = event.dataTransfer.files[0]\n\n if (checkFileExtension(file)) {\n fileInput.files = event.dataTransfer.files\n fileInput.dispatchEvent(new Event('change'))\n }\n }\n\n function handleFileChange () {\n if (checkFileExtension(this.files[0]) && this.files.length === 1) {\n fileNamePlaceholder.innerText = `Selected File: ${this.files[0].name}`\n updateFileSelectionStatus(true)\n // Invoke a callback if additional functionality is required.\n if (typeof fileInputProperties.callback === 'function') {\n fileInputProperties.callback(this.files[0])\n }\n }\n }\n\n /**\n * Checks if the file extension on the inputted file is correct.\n * @param {File} file - The file to check\n * @returns {boolean} True if the file extension is valid, false otherwise.\n */\n function checkFileExtension (file) {\n // If there's no restriction, return true.\n if (!fileInputProperties.extension) {\n return true\n }\n const fileExtension = file.name.split('.').pop()\n const isValidExtension = fileExtension === fileInputProperties.extension\n updateFileSelectionStatus(isValidExtension)\n if (!isValidExtension) {\n fileInput.setCustomValidity(`Only a .${fileInputProperties.extension} file can be uploaded.`)\n fileInput.reportValidity()\n fileNamePlaceholder.classList.add('hidden')\n } else {\n fileInput.setCustomValidity('')\n }\n return isValidExtension\n }\n\n /**\n * Updates the file input to reflect the validity of the current file\n * selection.\n * @param {boolean} isValidFileSelection - True if a single .keystore file\n * was selected. False otherwise.\n */\n function updateFileSelectionStatus (isValidFileSelection) {\n imageContainer.innerHTML = ''\n const statusImage = document.createElement('span')\n statusImage.classList.add('fas', isValidFileSelection ? 'fa-check' : 'fa-times')\n statusImage.style.color = isValidFileSelection ? 'green' : 'red'\n imageContainer.appendChild(statusImage)\n\n if (!isValidFileSelection) {\n fileInput.value = null\n fileNamePlaceholder.classList.add('hidden')\n } else {\n fileNamePlaceholder.classList.remove('hidden')\n }\n\n // If the modal contains a password field for a keystore file, change its\n // visibility.\n const walletPasswordInputContainer = document.querySelector('.dcp-modal-body input[type=\"password\"]').parentElement.parentElement\n if (walletPasswordInputContainer) {\n if (isValidFileSelection) {\n walletPasswordInputContainer.classList.remove('hidden')\n const walletPasswordInput = document.querySelector('.dcp-modal-body input[type=\"password\"]')\n walletPasswordInput.focus()\n } else {\n walletPasswordInputContainer.classList.add('hidden')\n }\n }\n }\n\n function highlightDropArea (event) {\n event.preventDefault()\n this.classList.add('highlight')\n }\n\n function unhighlightDropArea (event) {\n event.preventDefault()\n this.classList.remove('highlight')\n }\n }\n\n /**\n * Sets up a custom tooltip to pop up when the passwords do not match, but are\n * valid otherwise.\n */\n addFormValidationForPasswordConfirmation () {\n const [newPassword, confirmPassword] = document.querySelectorAll('.dcp-modal-body input[type=\"password\"]')\n if (!newPassword || !confirmPassword) {\n throw Error('New Password field and Confirm Password fields not present.')\n }\n\n newPassword.addEventListener('input', checkMatchingPasswords)\n confirmPassword.addEventListener('input', checkMatchingPasswords)\n\n function checkMatchingPasswords () {\n if (newPassword.value !== confirmPassword.value &&\n newPassword.validity.valid &&\n confirmPassword.validity.valid) {\n newPassword.setCustomValidity('Both passwords must match.')\n } else if (newPassword.value === confirmPassword.value ||\n newPassword.validity.tooShort ||\n newPassword.validity.patternMismatch ||\n newPassword.validity.valueMissing ||\n confirmPassword.validity.tooShort ||\n confirmPassword.validity.patternMismatch ||\n confirmPassword.validity.valueMissing) {\n // If the passwords fields match or have become invalidated some other\n // way again, reset the custom message.\n newPassword.setCustomValidity('')\n }\n }\n }\n\n updateInvalidEmailMessage() {\n const email = document.querySelector('.dcp-modal-body input[id=\"email\"')\n if (!email){\n throw Error(\"Email field not present\")\n }\n email.addEventListener('input', checkValidEmail);\n function checkValidEmail() {\n if (!email.validity.patternMismatch &&\n !email.validity.valueMissing) {\n email.setCustomValidity('')\n } else {\n email.setCustomValidity(\"Enter a valid email address.\")\n }\n\n }\n }\n\n /**\n * Adds message(s) to the modal's body.\n * @param {string} messages - The message(s) to add to the modal's body.\n * @returns Paragraph element(s) containing the message(s) added to the\n * modal's body.\n */\n addMessage (...messages) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n for (let i = 0; i < messages.length; i++) {\n const row = document.createElement('div')\n row.className = 'row'\n\n const paragraph = document.createElement('p')\n paragraph.innerHTML = messages[i]\n paragraph.classList.add('message')\n row.appendChild(paragraph)\n body.appendChild(row)\n\n elements.push(paragraph)\n }\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n\n addHorizontalRule () {\n const body = this.container.querySelector('.dcp-modal-body')\n body.appendChild(document.createElement('hr'))\n }\n\n // Does what it says. Still ill advised to use unless you have to.\n addCustomHTML (htmlStr, browseCallback) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n body.innerHTML += htmlStr\n body.querySelector('#browse-button').addEventListener('click', browseCallback.bind(this, this))\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n\n addButton (...buttons) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n for (let i = 0; i < buttons.length; i++) {\n const row = document.createElement('div')\n row.className = 'row'\n\n let col = document.createElement('div')\n col.className = 'col-md-4'\n\n const description = document.createElement('span')\n description.innerText = buttons[i].description\n\n col.appendChild(description)\n row.appendChild(col)\n\n col = document.createElement('div')\n col.className = 'col-md-8'\n\n const button = document.createElement('button')\n button.innerText = buttons[i].label\n button.addEventListener('click', buttons[i].callback.bind(this, this))\n\n elements.push(button)\n\n col.appendChild(button)\n row.appendChild(col)\n\n body.appendChild(row)\n }\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n}\n\n\n// Inject our special stylesheet from dcp-client only if we're on the portal webpage.\nif (typeof window !== 'undefined' && typeof document !== 'undefined' && dcpConfig.portal.location.hostname === window.location.hostname) {\n // <link rel='stylesheet' href='/css/dashboard.css'>\n const stylesheet = document.createElement('link')\n stylesheet.rel = 'stylesheet'\n // Needed for the duplicate check done later.\n stylesheet.id = 'dcp-modal-styles'\n\n const dcpClientBundle = document.getElementById('_dcp_client_bundle')\n let src\n if (dcpClientBundle) {\n src = dcpClientBundle.src.replace('dcp-client-bundle.js', 'dcp-modal-style.css')\n } else {\n src = dcpConfig.portal.location.href + 'dcp-client/dist/dcp-modal-style.css'\n }\n\n stylesheet.href = src\n // If the style was injected before, don't inject it again.\n // Could occur when loading a file that imports Modal.js and loading\n // comput.min.js in the same HTML file.\n if (document.getElementById(stylesheet.id) === null) {\n document.getElementsByTagName('head')[0].appendChild(stylesheet)\n }\n\n if (typeof {\"version\":\"0bf54dbe88ee11e84b28e62b73cbd154d06967ea\",\"branch\":\"release\",\"dcpClient\":{\"version\":\"4.2.23\",\"from\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#prod-20221207\",\"resolved\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#0e40d23d5bd715d4f2ae2c79cc52981fa0a0408e\"},\"built\":\"Thu Dec 08 2022 15:53:38 GMT-0500 (Eastern Standard Time)\",\"config\":{\"generated\":\"Thu 08 Dec 2022 03:53:36 PM EST by erose on lorge\",\"build\":\"debug\"},\"webpack\":\"5.74.0\",\"node\":\"v14.21.1\"} !== 'undefined' && typeof window.Modal === 'undefined') {\n window.Modal = Modal\n }\n}\n\n\n//# sourceURL=webpack://dcp/./portal/www/js/modal.js?");
3916
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (/* binding */ Modal)\n/* harmony export */ });\n/**\n * A Small Modal Class\n * @module Modal\n */\n/* globals Event dcpConfig */\nclass Modal {\n constructor (title, message, callback = false, exitHandler = false, {\n continueLabel = 'Continue',\n cancelLabel = 'Cancel',\n cancelVisible = true\n } = {}) {\n const modal = document.createElement('div')\n modal.className = 'dcp-modal-container-old day'\n modal.innerHTML = `\n <dialog class=\"dcp-modal-content\">\n <div class=\"dcp-modal-header\">\n <h2>${title}<button type=\"button\" class=\"close\">&times;</button></h2>\n ${message ? '<p>' + message + '</p>' : ''}\n </div>\n <div class=\"dcp-modal-loading hidden\">\n <div class='loading'></div>\n </div>\n <form onsubmit='return false' method=\"dialog\">\n <div class=\"dcp-modal-body\"></div>\n <div class=\"dcp-modal-footer ${cancelVisible ? '' : 'centered'}\">\n <button type=\"submit\" class=\"continue green-modal-button\">${continueLabel}</button>\n <button type=\"button\" class=\"cancel green-modal-button\">${cancelLabel}</button>\n </div>\n </form>\n </dialog>`\n\n // To give a reference to do developer who wants to override the form submit.\n // May occur if they want to validate the information in the backend\n // without closing the modal prematurely.\n this.form = modal.querySelector('.dcp-modal-content form')\n this.continueButton = modal.querySelector('.dcp-modal-footer button.continue')\n this.cancelButton = modal.querySelector('.dcp-modal-footer button.cancel')\n this.closeButton = modal.querySelector('.dcp-modal-header .close')\n if (!cancelVisible) {\n this.cancelButton.style.display = 'none'\n }\n\n // To remove the event listener, the reference to the original function\n // added is required.\n this.formSubmitHandler = this.continue.bind(this)\n\n modal.addEventListener('keydown', function (event) {\n event.stopPropagation()\n // 27 is the keycode for the escape key.\n if (event.keyCode === 27) this.close()\n }.bind(this))\n\n this.container = modal\n this.callback = callback\n this.exitHandler = exitHandler\n document.body.appendChild(modal)\n }\n\n changeFormSubmitHandler (newFormSubmitHandler) {\n this.formSubmitHandler = newFormSubmitHandler\n }\n\n /**\n * Validates the form values in the modal and calls the modal's callback\n */\n async continue (event) {\n // To further prevent form submission from trying to redirect from the\n // current page.\n if (event instanceof Event) {\n event.preventDefault()\n }\n let fieldsAreValid = true\n let formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input, .dcp-modal-body textarea')\n\n const formValues = []\n if (typeof formElements.length === 'undefined') formElements = [formElements]\n // Separate into two loops to enable input validation requiring formValues\n // that come after it. e.g. Two password fields matching.\n for (let i = 0; i < formElements.length; i++) {\n switch (formElements[i].type) {\n case 'file':\n formValues.push(formElements[i])\n break\n case 'checkbox':\n formValues.push(formElements[i].checked)\n break\n default:\n formValues.push(formElements[i].value)\n break\n }\n }\n for (let i = 0; i < formElements.length; i++) {\n if (formElements[i].validation) {\n // Optional fields are allowed to be empty but still can't be wrong if not empty.\n if (!(formElements[i].value === '' && !formElements[i].required)) {\n if (typeof formElements[i].validation === 'function') {\n if (!formElements[i].validation(formValues)) {\n fieldsAreValid = false\n formElements[i].classList.add('is-invalid')\n }\n } else if (!formElements[i].validation.test(formElements[i].value)) {\n fieldsAreValid = false\n formElements[i].classList.add('is-invalid')\n }\n }\n }\n }\n\n if (!fieldsAreValid) return\n\n this.loading()\n if (typeof this.callback === 'function') {\n try {\n return this.callback(formValues)\n } catch (error) {\n console.error('Unexpected error in modal.continue:', error);\n return this.close(false)\n }\n }\n this.close(true)\n }\n\n loading () {\n this.container.querySelector('.dcp-modal-loading').classList.remove('hidden')\n this.container.querySelector('.dcp-modal-body').classList.add('hidden')\n this.container.querySelector('.dcp-modal-footer').classList.add('hidden')\n }\n\n open () {\n this.form.addEventListener('submit', async (event) => {\n const success = await this.formSubmitHandler(event)\n if (success === false) {\n return\n }\n this.close(true)\n })\n // When the user clicks on <span> (x), close the modal\n this.closeButton.addEventListener('click', this.close.bind(this))\n this.cancelButton.addEventListener('click', this.close.bind(this))\n\n // Prevent lingering outlines after clicking some form elements.\n this.container.querySelectorAll('.dcp-modal-body button, .dcp-modal-body input[type=\"checkbox\"]').forEach(element => {\n element.addEventListener('click', () => {\n element.blur()\n })\n })\n\n // Show the modal.\n this.container.style.display = 'block'\n\n const formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input')\n if (formElements.length) {\n formElements[0].focus()\n if (formElements[0].type === 'text') {\n formElements[0].select()\n }\n for (const el of formElements) {\n if (el.realType) {\n el.type = el.realType\n }\n }\n } else {\n // With no form elements to allow for form submission on enter, focus the\n // continue button.\n this.container.querySelector('.dcp-modal-footer button.continue').focus()\n }\n } // TODO: This should return a promise with the action resolving it\n\n /**\n * Shows the modal and returns a promise of the result of the modal (e.g. was\n * it closed, did its action succeed?)\n */\n showModal () {\n return new Promise((resolve, reject) => {\n this.form.addEventListener('submit', handleContinue.bind(this))\n this.cancelButton.addEventListener('click', handleCancel.bind(this))\n this.closeButton.addEventListener('click', handleCancel.bind(this))\n\n // Prevent lingering outlines after clicking some form elements.\n this.container.querySelectorAll('.dcp-modal-body button, .dcp-modal-body input[type=\"checkbox\"]').forEach(element => {\n element.addEventListener('click', () => {\n element.blur()\n })\n })\n\n // Show the modal.\n this.container.style.display = 'block'\n\n const formElements = this.container.querySelectorAll('.dcp-modal-body select, .dcp-modal-body input')\n if (formElements.length) {\n formElements[0].focus()\n if (formElements[0].type === 'text') {\n formElements[0].select()\n }\n for (const el of formElements) {\n if (el.realType) {\n el.type = el.realType\n }\n }\n } else {\n // With no form elements to allow for form submission on enter, focus the\n // continue button.\n this.continueButton.focus()\n }\n\n async function handleContinue (event) {\n let result\n try {\n result = await this.formSubmitHandler(event)\n } catch (error) {\n reject(error)\n }\n this.close(true)\n resolve(result)\n }\n\n async function handleCancel () {\n let result\n try {\n result = await this.close()\n } catch (error) {\n reject(error)\n }\n resolve(result)\n }\n })\n }\n\n close (success = false) {\n this.container.style.display = 'none'\n if (this.container.parentNode) {\n this.container.parentNode.removeChild(this.container)\n }\n\n // @todo this needs to remove eventlisteners to prevent memory leaks\n\n if ((success !== true) && typeof this.exitHandler === 'function') {\n return this.exitHandler(this)\n }\n }\n\n /**\n * Adds different form elements to the modal depending on the case.\n *\n * @param {*} elements - The properties of the form elements to add.\n * @returns {HTMLElement} The input form elements.\n */\n addFormElement (...elements) {\n const body = this.container.querySelector('.dcp-modal-body')\n const inputElements = []\n let label\n for (let i = 0; i < elements.length; i++) {\n let row = document.createElement('div')\n row.className = 'row'\n\n let col, input\n switch (elements[i].type) {\n case 'button':\n col = document.createElement('div')\n col.className = 'col-md-12'\n\n input = document.createElement('button')\n input.innerHTML = elements[i].label\n input.type = 'button'\n input.classList.add('green-modal-button')\n if (!elements[i].onclick) {\n throw new Error('A button in the modal body should have an on click event handler.')\n }\n input.addEventListener('click', elements[i].onclick)\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'textarea':\n col = document.createElement('div')\n col.className = 'col-md-12'\n\n input = document.createElement('textarea')\n input.className = 'text-input-field form-control'\n if (elements[i].placeholder) input.placeholder = elements[i].placeholder\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'text':\n case 'email':\n case 'number':\n case 'password': {\n const inputCol = document.createElement('div')\n\n input = document.createElement('input')\n input.type = elements[i].type\n input.validation = elements[i].validation\n input.autocomplete = elements[i].autocomplete || (elements[i].type === 'password' ? 'off' : 'on')\n input.className = 'text-input-field form-control'\n\n // Adding bootstraps custom feedback styles.\n let invalidFeedback = null\n if (elements[i].invalidFeedback) {\n invalidFeedback = document.createElement('div')\n invalidFeedback.className = 'invalid-feedback'\n invalidFeedback.innerText = elements[i].invalidFeedback\n }\n\n if (elements[i].type === 'password') {\n elements[i].realType = 'password'\n }\n\n if (elements[i].label) {\n const labelCol = document.createElement('div')\n label = document.createElement('label')\n label.innerText = elements[i].label\n const inputId = 'dcp-modal-input-' + this.container.querySelectorAll('input[type=\"text\"], input[type=\"email\"], input[type=\"number\"], input[type=\"password\"]').length\n label.setAttribute('for', inputId)\n input.id = inputId\n labelCol.classList.add('col-md-6', 'label-column')\n labelCol.appendChild(label)\n row.appendChild(labelCol)\n inputCol.className = 'col-md-6'\n } else {\n inputCol.className = 'col-md-12'\n }\n\n inputCol.appendChild(input)\n if (invalidFeedback !== null) {\n inputCol.appendChild(invalidFeedback)\n }\n row.appendChild(inputCol)\n break\n }\n case 'select':\n col = document.createElement('div')\n col.className = 'col-md-4'\n\n label = document.createElement('span')\n label.innerText = elements[i].label\n\n col.appendChild(label)\n row.appendChild(col)\n\n col = document.createElement('div')\n col.className = 'col-md-8'\n\n input = document.createElement('select')\n\n col.appendChild(input)\n row.appendChild(col)\n break\n case 'checkbox': {\n row.classList.add('checkbox-row')\n const checkboxLabelCol = document.createElement('div')\n checkboxLabelCol.classList.add('label-column', 'checkbox-label-column')\n\n label = document.createElement('label')\n label.innerText = elements[i].label\n label.for = 'dcp-checkbox-input-' + this.container.querySelectorAll('input[type=\"checkbox\"]').length\n label.setAttribute('for', label.for)\n label.className = 'checkbox-label'\n\n checkboxLabelCol.appendChild(label)\n\n const checkboxCol = document.createElement('div')\n checkboxCol.classList.add('checkbox-column')\n\n input = document.createElement('input')\n input.type = 'checkbox'\n input.id = label.for\n if (elements[i].checked) {\n input.checked = true\n }\n\n checkboxCol.appendChild(input)\n\n if (elements[i].labelToTheRightOfCheckbox) {\n checkboxCol.classList.add('col-md-5')\n row.appendChild(checkboxCol)\n checkboxLabelCol.classList.add('col-md-7')\n row.appendChild(checkboxLabelCol)\n } else {\n checkboxLabelCol.classList.add('col-md-6')\n checkboxCol.classList.add('col-md-6')\n row.appendChild(checkboxLabelCol)\n row.appendChild(checkboxCol)\n }\n break\n }\n case 'file':\n [input, row] = this.addFileInput(elements[i], input, row)\n break\n case 'label':\n row.classList.add('label-row')\n label = document.createElement('label')\n label.innerText = elements[i].label\n row.appendChild(label)\n break\n }\n\n // Copy other possibly specified element properties:\n const inputPropertyNames = ['title', 'inputmode', 'value', 'minLength', 'maxLength', 'size', 'required', 'pattern', 'min', 'max', 'step', 'placeholder', 'accept', 'multiple', 'id', 'onkeypress', 'oninput', 'for', 'readonly', 'autocomplete']\n for (const propertyName of inputPropertyNames) {\n if (Object.prototype.hasOwnProperty.call(elements[i], propertyName)) {\n if (propertyName === 'for' && !label.hasAttribute(propertyName)) {\n label.setAttribute(propertyName, elements[i][propertyName])\n }\n if (propertyName.startsWith('on')) {\n input.addEventListener(propertyName.slice(2), elements[i][propertyName])\n } else {\n input.setAttribute(propertyName, elements[i][propertyName])\n }\n }\n }\n\n inputElements.push(input)\n body.appendChild(row)\n }\n\n if (inputElements.length === 1) return inputElements[0]\n else return inputElements\n }\n\n /**\n * Adds a drag and drop file form element to the modal.\n *\n * @param {*} fileInputProperties - An object specifying some of the\n * properties of the file input element.\n * @param {*} fileInput - Placeholders to help create the file\n * input.\n * @param {HTMLDivElement} row - Placeholders to help create the file\n * input.\n */\n addFileInput (fileInputProperties, fileInput, row) {\n // Adding the upload label.\n const uploadLabel = document.createElement('label')\n uploadLabel.innerText = fileInputProperties.label\n row.appendChild(uploadLabel)\n const body = this.container.querySelector('.dcp-modal-body')\n body.appendChild(row)\n const fileSelectionRow = document.createElement('div')\n fileSelectionRow.id = 'file-selection-row'\n\n // Adding the drag and drop file upload input.\n const dropContainer = document.createElement('div')\n dropContainer.id = 'drop-container'\n\n // Adding an image of a wallet\n const imageContainer = document.createElement('div')\n imageContainer.id = 'image-container'\n const walletImage = document.createElement('span')\n walletImage.classList.add('fas', 'fa-wallet')\n imageContainer.appendChild(walletImage)\n\n // Adding some text prompts\n const dropMessage = document.createElement('span')\n dropMessage.innerText = 'Drop a keystore file here'\n const orMessage = document.createElement('span')\n orMessage.innerText = 'or'\n\n // Adding the manual file input element (hiding the default one)\n const fileInputContainer = document.createElement('div')\n const fileInputLabel = document.createElement('label')\n // Linking the label to the file input so that clicking on the label\n // activates the file input.\n fileInputLabel.setAttribute('for', 'file-input')\n fileInputLabel.innerText = 'Browse'\n fileInput = document.createElement('input')\n fileInput.type = fileInputProperties.type\n fileInput.id = 'file-input'\n // To remove the lingering outline after selecting the file.\n fileInput.addEventListener('click', () => {\n fileInput.blur()\n })\n fileInputContainer.append(fileInput, fileInputLabel)\n\n // Creating the final row element to append to the modal body.\n dropContainer.append(imageContainer, dropMessage, orMessage, fileInputContainer)\n fileSelectionRow.appendChild(dropContainer)\n\n // Adding functionality to the drag and drop file input.\n dropContainer.addEventListener('drop', selectDroppedFile.bind(this))\n dropContainer.addEventListener('drop', unhighlightDropArea)\n // Prevent file from being opened by the browser.\n dropContainer.ondragover = highlightDropArea\n dropContainer.ondragenter = highlightDropArea\n dropContainer.ondragleave = unhighlightDropArea\n\n fileInput.addEventListener('change', handleFileChange)\n\n const fileNamePlaceholder = document.createElement('center')\n fileNamePlaceholder.id = 'file-name-placeholder'\n fileNamePlaceholder.className = 'row'\n fileNamePlaceholder.innerText = ''\n fileSelectionRow.appendChild(fileNamePlaceholder)\n fileNamePlaceholder.classList.add('hidden')\n\n // Check if the continue button is invalid on the keystore upload modal and\n // click it if it should no longer be invalid.\n this.continueButton.addEventListener('invalid', () => {\n const fileFormElements = this.container.querySelectorAll('.dcp-modal-body input[type=\"file\"], .dcp-modal-body input[type=\"text\"]')\n const filledInFileFormElements = Array.from(fileFormElements).filter(fileFormElement => fileFormElement.value !== '')\n if (fileFormElements.length !== 0 && filledInFileFormElements.length !== 0) {\n this.continueButton.setCustomValidity('')\n // Clicking instead of dispatching a submit event to ensure other form validation is used before submitting the form.\n this.continueButton.click()\n }\n })\n\n return [fileInput, fileSelectionRow]\n\n /**\n * Checks that the dropped items contain only a single keystore file.\n * If valid, sets the file input's value to the dropped file.\n * @param {DragEvent} event - Contains the files dropped.\n */\n function selectDroppedFile (event) {\n // Prevent file from being opened.\n event.preventDefault()\n\n // Check if only one file was dropped.\n const wasOneFileDropped = event.dataTransfer.items.length === 1 ||\n event.dataTransfer.files.length === 1\n updateFileSelectionStatus(wasOneFileDropped)\n if (!wasOneFileDropped) {\n fileInput.setCustomValidity('Only one file can be uploaded.')\n fileInput.reportValidity()\n return\n } else {\n fileInput.setCustomValidity('')\n }\n\n // Now to use the DataTransfer interface to access the file(s), setting\n // the value of the file input.\n const file = event.dataTransfer.files[0]\n\n if (checkFileExtension(file)) {\n fileInput.files = event.dataTransfer.files\n fileInput.dispatchEvent(new Event('change'))\n }\n }\n\n function handleFileChange () {\n if (checkFileExtension(this.files[0]) && this.files.length === 1) {\n fileNamePlaceholder.innerText = `Selected File: ${this.files[0].name}`\n updateFileSelectionStatus(true)\n // Invoke a callback if additional functionality is required.\n if (typeof fileInputProperties.callback === 'function') {\n fileInputProperties.callback(this.files[0])\n }\n }\n }\n\n /**\n * Checks if the file extension on the inputted file is correct.\n * @param {File} file - The file to check\n * @returns {boolean} True if the file extension is valid, false otherwise.\n */\n function checkFileExtension (file) {\n // If there's no restriction, return true.\n if (!fileInputProperties.extension) {\n return true\n }\n const fileExtension = file.name.split('.').pop()\n const isValidExtension = fileExtension === fileInputProperties.extension\n updateFileSelectionStatus(isValidExtension)\n if (!isValidExtension) {\n fileInput.setCustomValidity(`Only a .${fileInputProperties.extension} file can be uploaded.`)\n fileInput.reportValidity()\n fileNamePlaceholder.classList.add('hidden')\n } else {\n fileInput.setCustomValidity('')\n }\n return isValidExtension\n }\n\n /**\n * Updates the file input to reflect the validity of the current file\n * selection.\n * @param {boolean} isValidFileSelection - True if a single .keystore file\n * was selected. False otherwise.\n */\n function updateFileSelectionStatus (isValidFileSelection) {\n imageContainer.innerHTML = ''\n const statusImage = document.createElement('span')\n statusImage.classList.add('fas', isValidFileSelection ? 'fa-check' : 'fa-times')\n statusImage.style.color = isValidFileSelection ? 'green' : 'red'\n imageContainer.appendChild(statusImage)\n\n if (!isValidFileSelection) {\n fileInput.value = null\n fileNamePlaceholder.classList.add('hidden')\n } else {\n fileNamePlaceholder.classList.remove('hidden')\n }\n\n // If the modal contains a password field for a keystore file, change its\n // visibility.\n const walletPasswordInputContainer = document.querySelector('.dcp-modal-body input[type=\"password\"]').parentElement.parentElement\n if (walletPasswordInputContainer) {\n if (isValidFileSelection) {\n walletPasswordInputContainer.classList.remove('hidden')\n const walletPasswordInput = document.querySelector('.dcp-modal-body input[type=\"password\"]')\n walletPasswordInput.focus()\n } else {\n walletPasswordInputContainer.classList.add('hidden')\n }\n }\n }\n\n function highlightDropArea (event) {\n event.preventDefault()\n this.classList.add('highlight')\n }\n\n function unhighlightDropArea (event) {\n event.preventDefault()\n this.classList.remove('highlight')\n }\n }\n\n /**\n * Sets up a custom tooltip to pop up when the passwords do not match, but are\n * valid otherwise.\n */\n addFormValidationForPasswordConfirmation () {\n const [newPassword, confirmPassword] = document.querySelectorAll('.dcp-modal-body input[type=\"password\"]')\n if (!newPassword || !confirmPassword) {\n throw Error('New Password field and Confirm Password fields not present.')\n }\n\n newPassword.addEventListener('input', checkMatchingPasswords)\n confirmPassword.addEventListener('input', checkMatchingPasswords)\n\n function checkMatchingPasswords () {\n if (newPassword.value !== confirmPassword.value &&\n newPassword.validity.valid &&\n confirmPassword.validity.valid) {\n newPassword.setCustomValidity('Both passwords must match.')\n } else if (newPassword.value === confirmPassword.value ||\n newPassword.validity.tooShort ||\n newPassword.validity.patternMismatch ||\n newPassword.validity.valueMissing ||\n confirmPassword.validity.tooShort ||\n confirmPassword.validity.patternMismatch ||\n confirmPassword.validity.valueMissing) {\n // If the passwords fields match or have become invalidated some other\n // way again, reset the custom message.\n newPassword.setCustomValidity('')\n }\n }\n }\n\n updateInvalidEmailMessage() {\n const email = document.querySelector('.dcp-modal-body input[id=\"email\"')\n if (!email){\n throw Error(\"Email field not present\")\n }\n email.addEventListener('input', checkValidEmail);\n function checkValidEmail() {\n if (!email.validity.patternMismatch &&\n !email.validity.valueMissing) {\n email.setCustomValidity('')\n } else {\n email.setCustomValidity(\"Enter a valid email address.\")\n }\n\n }\n }\n\n /**\n * Adds message(s) to the modal's body.\n * @param {string} messages - The message(s) to add to the modal's body.\n * @returns Paragraph element(s) containing the message(s) added to the\n * modal's body.\n */\n addMessage (...messages) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n for (let i = 0; i < messages.length; i++) {\n const row = document.createElement('div')\n row.className = 'row'\n\n const paragraph = document.createElement('p')\n paragraph.innerHTML = messages[i]\n paragraph.classList.add('message')\n row.appendChild(paragraph)\n body.appendChild(row)\n\n elements.push(paragraph)\n }\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n\n addHorizontalRule () {\n const body = this.container.querySelector('.dcp-modal-body')\n body.appendChild(document.createElement('hr'))\n }\n\n // Does what it says. Still ill advised to use unless you have to.\n addCustomHTML (htmlStr, browseCallback) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n body.innerHTML += htmlStr\n body.querySelector('#browse-button').addEventListener('click', browseCallback.bind(this, this))\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n\n addButton (...buttons) {\n const elements = []\n const body = this.container.querySelector('.dcp-modal-body')\n for (let i = 0; i < buttons.length; i++) {\n const row = document.createElement('div')\n row.className = 'row'\n\n let col = document.createElement('div')\n col.className = 'col-md-4'\n\n const description = document.createElement('span')\n description.innerText = buttons[i].description\n\n col.appendChild(description)\n row.appendChild(col)\n\n col = document.createElement('div')\n col.className = 'col-md-8'\n\n const button = document.createElement('button')\n button.innerText = buttons[i].label\n button.addEventListener('click', buttons[i].callback.bind(this, this))\n\n elements.push(button)\n\n col.appendChild(button)\n row.appendChild(col)\n\n body.appendChild(row)\n }\n\n if (elements.length === 1) return elements[0]\n else return elements\n }\n}\n\n\n// Inject our special stylesheet from dcp-client only if we're on the portal webpage.\nif (typeof window !== 'undefined' && typeof document !== 'undefined' && dcpConfig.portal.location.hostname === window.location.hostname) {\n // <link rel='stylesheet' href='/css/dashboard.css'>\n const stylesheet = document.createElement('link')\n stylesheet.rel = 'stylesheet'\n // Needed for the duplicate check done later.\n stylesheet.id = 'dcp-modal-styles'\n\n const dcpClientBundle = document.getElementById('_dcp_client_bundle')\n let src\n if (dcpClientBundle) {\n src = dcpClientBundle.src.replace('dcp-client-bundle.js', 'dcp-modal-style.css')\n } else {\n src = dcpConfig.portal.location.href + 'dcp-client/dist/dcp-modal-style.css'\n }\n\n stylesheet.href = src\n // If the style was injected before, don't inject it again.\n // Could occur when loading a file that imports Modal.js and loading\n // comput.min.js in the same HTML file.\n if (document.getElementById(stylesheet.id) === null) {\n document.getElementsByTagName('head')[0].appendChild(stylesheet)\n }\n\n if (typeof {\"version\":\"5895f3465816246196dbdb9c0cd5aa200a943c59\",\"branch\":\"release\",\"dcpClient\":{\"version\":\"4.2.24\",\"from\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#prod-20230111\",\"resolved\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#0b4bbf4f5e91b57e19c5bbb66c8160479dca32cd\"},\"built\":\"Mon Jan 23 2023 14:29:03 GMT-0500 (Eastern Standard Time)\",\"config\":{\"generated\":\"Mon 23 Jan 2023 02:29:01 PM EST by erose on lorge\",\"build\":\"debug\"},\"webpack\":\"5.74.0\",\"node\":\"v14.21.2\"} !== 'undefined' && typeof window.Modal === 'undefined') {\n window.Modal = Modal\n }\n}\n\n\n//# sourceURL=webpack://dcp/./portal/www/js/modal.js?");
3873
3917
 
3874
3918
  /***/ }),
3875
3919
 
@@ -4135,7 +4179,7 @@ eval("/**\n * @file client-modal/index.js\n * @author KC Erb\n * @date Mar 2020\
4135
4179
  \*****************************************************/
4136
4180
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4137
4181
 
4138
- eval("/**\n * @file keystoreFile.js\n * Modal providing a way to upload a keystore file and cache it (on request) in local/session storage.\n * \n * @author KC Erb - kcerb@kingsds.network\n * @author Nazila Kharazian - nazila@kingsds.network\n * @date Apr 2020\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\nconst { confirm } = __webpack_require__(/*! ./confirm */ \"./src/dcp-client/client-modal/confirm.js\");\nconst passwordModule = __webpack_require__(/*! ./passwordEntry */ \"./src/dcp-client/client-modal/passwordEntry.js\");\n\nexports.config = {\n id: 'dcp-modal-keystore-file',\n required: ['dcp-modal-keystore-file-form', 'dcp-modal-keystore-file-prompt', 'dcp-modal-keystore-file-input', 'dcp-modal-keystore-file-checkbox'],\n path: \"./templates/keystore-file-modal.html\",\n eagerLoad: [passwordModule.config]\n};\n\n/**\n * @param {Object} options From `wallet.get_internal`\n * @param {string} options.name - The name of the keystore type (wallet.get => 'default' and wallet.getId => 'id').\n * @param {string} options.contextId - An optional, user-defined identifier used for caching keystores.\n * See `job.contextId` in the compute-api spec.\n * @param {string} options.jobName - A user-defined job property provided to identify this job.\n * @param {string} options.msg - Optional string denoting the purpose for fetching the keystore\n * @param {function} [onClose] Override default onClose behavior. An instance of `modal` is handed to the function.\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n * @returns {string} contents of Keystore file\n * @memberof module:dcp/client-modal\n * @access public\n * @async\n * @alias getKeystoreFile\n */\nexports.keystoreFile = async function (options, onClose=null) {\n const storageKey = `dcp-keystore-${options.name}-${options.contextId}`;\n \n let jobName = options.jobName;\n if (jobName) {\n jobName = `\"${jobName}\"`;\n } else {\n jobName = 'this job';\n }\n\n let keystoreType;\n switch (options.name) {\n case 'id':\n keystoreType = \"DCP identity\";\n break;\n case 'default':\n keystoreType = \"DCP Bank Account\";\n default:\n keystoreType = options.name;\n break;\n };\n\n let keystoreMsg = options.msg;\n\n let keystoreFile = await getKeystoreFromStorage(storageKey, jobName, onClose);\n if (!keystoreFile) {\n keystoreFile = await getKeystoreFromUser(keystoreType, jobName, storageKey, onClose, keystoreMsg);\n }\n\n return keystoreFile;\n};\n\n/**\n * Alert user that a key from storage will be used, give them a chance to ditch it.\n * @param {string} storageKey key in local or session storage where the file is stored\n * @param {string} jobName helps user know which key is retrieved from storage\n * @returns {string} contents of Keystore file\n */\nasync function getKeystoreFromStorage (storageKey, jobName) {\n let keystoreFile = sessionStorage.getItem(storageKey);\n if (keystoreFile) return keystoreFile;\n \n keystoreFile = localStorage.getItem(storageKey);\n if (keystoreFile) {\n let ks = JSON.parse(keystoreFile);\n let label = ks.label || ks.address;\n let prompt;\n // alert is not necessarily for a job, specify jobname only if there is an actual job\n if(jobName)\n prompt = `You have keystore \"${label}\" saved for ${jobName}. Do you want to use it?`;\n else\n prompt = `You have keystore \"${label}\". Do you want to use it?`;\n let opts = { positive: \"Yes\", negative: \"No\", title: 'Keystore File' };\n let confirmation = await confirm(prompt, opts);\n if (confirmation) {\n sessionStorage.setItem(storageKey, keystoreFile)\n return keystoreFile;\n }\n localStorage.removeItem(storageKey);\n return false;\n };\n};\n\n/**\n * Bring up file prompt modal in browser, give user option to save the file locally for easier access.\n * @param {string} keystoreType Help user know whether to upload an identity or bank or whatever keystore.\n * @param {string} jobName Help user identify the job this key will be used on.\n * @param {string} storageKey Store the key in local/session storage under this key.\n * @param {string} msg Custom message for the modal that specifies the purpose for providing a keystore\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n */\nasync function getKeystoreFromUser (keystoreType, jobName, storageKey, onClose, msg) {\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n const [modal, form, prompt, fileInput, rememberInput] = elements;\n \n // default modal text that explains what a keystore is for.\n prompt.innerText = `This application is requesting a keystore file. Keystore files are used to identify ownership or stewardship of bank accounts, users, and other DCP resources. If you upload a keystore file which has been encrypted with a passphrase, the application will not be able to use it until it prompts for a passphrase and you enter it.`;\n if (msg)\n {\n prompt.innerText = msg;\n }\n\n fileInput.focus();\n fileInput.onchange = async function () {\n let files = fileInput.files;\n if (!files || files.length === 0) return;\n const keystoreFile = await fileToString(files[0]);\n try {\n JSON.parse(keystoreFile);\n } catch (error) {\n fileInput.setCustomValidity(\"File could not be parsed as JSON. Please upload a JSON keystore file.\");\n return;\n };\n fileInput.setCustomValidity(\"\");\n };\n\n await formPromise;\n let files = fileInput.files;\n const keystoreFile = await fileToString(files[0]);\n if (rememberInput.checked) {\n window.sessionStorage.setItem(storageKey, keystoreFile);\n window.localStorage.setItem(storageKey, keystoreFile);\n };\n \n utils.MicroModal.close(modal.id);\n return keystoreFile;\n};\n\n/**\n * Helper for reading in an uploaded file and returning the stringified version. \n * @param {File} inputFile Element of FileList\n * @returns {string} Contents of file.\n */\nconst fileToString = function (inputFile) {\n return new Promise((resolve, reject) => {\n const fileReader = new FileReader();\n\n fileReader.onerror = function () {\n reject(new DOMException('Problem parsing input file.'));\n };\n\n fileReader.onload = function () {\n resolve(this.result);\n };\n\n fileReader.readAsText(inputFile);\n });\n};\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/keystoreFile.js?");
4182
+ eval("/**\n * @file keystoreFile.js\n * Modal providing a way to upload a keystore file and cache it (on request) in local/session storage.\n * \n * @author KC Erb - kcerb@kingsds.network\n * @author Nazila Kharazian - nazila@kingsds.network\n * @date Apr 2020\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\nconst { confirm } = __webpack_require__(/*! ./confirm */ \"./src/dcp-client/client-modal/confirm.js\");\nconst passwordModule = __webpack_require__(/*! ./passwordEntry */ \"./src/dcp-client/client-modal/passwordEntry.js\");\n\nexports.config = {\n id: 'dcp-modal-keystore-file',\n required: ['dcp-modal-keystore-file-form', 'dcp-modal-keystore-file-prompt', 'dcp-modal-keystore-file-input'],\n path: \"./templates/keystore-file-modal.html\",\n eagerLoad: [passwordModule.config]\n};\n\n/**\n * @param {Object} options From `wallet.get_internal`\n * @param {string} options.name - The name of the keystore type (wallet.get => 'default' and wallet.getId => 'id').\n * @param {string} options.contextId - An optional, user-defined identifier used for caching keystores.\n * See `job.contextId` in the compute-api spec.\n * @param {string} options.jobName - A user-defined job property provided to identify this job.\n * @param {string} options.msg - Optional string denoting the purpose for fetching the keystore\n * @param {function} [onClose] Override default onClose behavior. An instance of `modal` is handed to the function.\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n * @returns {string} contents of Keystore file\n * @memberof module:dcp/client-modal\n * @access public\n * @async\n * @alias getKeystoreFile\n */\nexports.keystoreFile = async function (options, onClose=null) {\n const storageKey = `dcp-keystore-${options.name}-${options.contextId}`;\n \n let jobName = options.jobName;\n if (jobName) {\n jobName = `\"${jobName}\"`;\n } else {\n jobName = 'this job';\n }\n\n let keystoreType;\n switch (options.name) {\n case 'id':\n keystoreType = \"DCP identity\";\n break;\n case 'default':\n keystoreType = \"DCP Bank Account\";\n default:\n keystoreType = options.name;\n break;\n };\n\n let keystoreMsg = options.msg;\n\n let keystoreFile = await getKeystoreFromStorage(storageKey, jobName, onClose);\n if (!keystoreFile) {\n keystoreFile = await getKeystoreFromUser(keystoreType, jobName, storageKey, onClose, keystoreMsg);\n }\n\n return keystoreFile;\n};\n\n/**\n * Alert user that a key from storage will be used, give them a chance to ditch it.\n * @param {string} storageKey key in local or session storage where the file is stored\n * @param {string} jobName helps user know which key is retrieved from storage\n * @returns {string} contents of Keystore file\n */\nasync function getKeystoreFromStorage (storageKey, jobName) {\n let keystoreFile = sessionStorage.getItem(storageKey);\n if (keystoreFile) return keystoreFile;\n \n keystoreFile = localStorage.getItem(storageKey);\n if (keystoreFile) {\n let ks = JSON.parse(keystoreFile);\n let label = ks.label || ks.address;\n let prompt;\n // alert is not necessarily for a job, specify jobname only if there is an actual job\n if(jobName)\n prompt = `You have keystore \"${label}\" saved for ${jobName}. Do you want to use it?`;\n else\n prompt = `You have keystore \"${label}\". Do you want to use it?`;\n let opts = { positive: \"Yes\", negative: \"No\", title: 'Keystore File' };\n let confirmation = await confirm(prompt, opts);\n if (confirmation) {\n sessionStorage.setItem(storageKey, keystoreFile)\n return keystoreFile;\n }\n localStorage.removeItem(storageKey);\n return false;\n };\n};\n\n/**\n * Bring up file prompt modal in browser, give user option to save the file locally for easier access.\n * @param {string} keystoreType Help user know whether to upload an identity or bank or whatever keystore.\n * @param {string} jobName Help user identify the job this key will be used on.\n * @param {string} storageKey Store the key in local/session storage under this key.\n * @param {string} msg Custom message for the modal that specifies the purpose for providing a keystore\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n */\nasync function getKeystoreFromUser (keystoreType, jobName, storageKey, onClose, msg) {\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n const [modal, form, prompt, fileInput] = elements;\n \n // default modal text that explains what a keystore is for.\n prompt.innerText = `This application is requesting a keystore file. Keystore files are used to identify ownership or stewardship of bank accounts, users, and other DCP resources. If you upload a keystore file which has been encrypted with a passphrase, the application will not be able to use it until it prompts for a passphrase and you enter it.`;\n if (msg)\n {\n prompt.innerHTML = msg;\n }\n \n document.getElementById('dcp-modal-keystore-file-upload-button').onclick = function (e) {\n fileInput.click();\n }\n\n fileInput.focus();\n fileInput.onchange = async function () {\n let files = fileInput.files;\n if (!files || files.length === 0) return;\n const keystoreFile = await fileToString(files[0]);\n try {\n JSON.parse(keystoreFile);\n } catch (error) {\n fileInput.setCustomValidity(\"File could not be parsed as JSON. Please upload a JSON keystore file.\");\n return;\n };\n fileInput.setCustomValidity(\"\");\n document.getElementById('dcp-modal-keystore-file-submit').click();\n };\n\n await formPromise;\n let files = fileInput.files;\n const keystoreFile = await fileToString(files[0]);\n \n utils.MicroModal.close(modal.id);\n return keystoreFile;\n};\n\n/**\n * Helper for reading in an uploaded file and returning the stringified version. \n * @param {File} inputFile Element of FileList\n * @returns {string} Contents of file.\n */\nconst fileToString = function (inputFile) {\n return new Promise((resolve, reject) => {\n const fileReader = new FileReader();\n\n fileReader.onerror = function () {\n reject(new DOMException('Problem parsing input file.'));\n };\n\n fileReader.onload = function () {\n resolve(this.result);\n };\n\n fileReader.readAsText(inputFile);\n });\n};\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/keystoreFile.js?");
4139
4183
 
4140
4184
  /***/ }),
4141
4185
 
@@ -4145,7 +4189,7 @@ eval("/**\n * @file keystoreFile.js\n * Modal providing a wa
4145
4189
  \***************************************************/
4146
4190
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4147
4191
 
4148
- eval("/**\n * @file oauthLogin.js\n * Modal providing a way to retrieve a keystore from the oauth login service\n * Originally based on keystoreFile.js\n * \n * @author Alex Huctwith - alex@kingsds.network\n * @date Feb 2022\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\nconst { confirm } = __webpack_require__(/*! ./confirm */ \"./src/dcp-client/client-modal/confirm.js\");\nconst passwordModule = __webpack_require__(/*! ./passwordEntry */ \"./src/dcp-client/client-modal/passwordEntry.js\");\n\nexports.config = {\n id: 'dcp-modal-oauth-login',\n required: ['dcp-modal-oauth-login-form', 'dcp-modal-oauth-login-checkbox'],\n path: \"./templates/oauth-login-modal.html\",\n eagerLoad: [passwordModule.config]\n};\n\n/**\n* @param {Object} options From `wallet.get_internal`\n* @param {string} options.name - The name of the keystore type (wallet.get => 'default' and wallet.getId => 'id').\n* @param {string} options.contextId - An optional, user-defined identifier used for caching keystores.\n* See `job.contextId` in the compute-api spec.\n* @param {string} options.jobName - A user-defined job property provided to identify this job.\n* @param {string} options.msg - Optional string denoting the purpose for fetching the keystore\n* @param {function} [onClose] Override default onClose behavior. An instance of `modal` is handed to the function.\n* @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n* @returns {string} contents of Keystore file\n* @memberof module:dcp/client-modal\n* @access public\n* @async\n* @alias getKeystoreFile\n*/\nexports.oauthLogin = async function (options, onClose=null) {\n const storageKey = `dcp-keystore-${options.name}-${options.contextId}`;\n \n let jobName = options.jobName;\n if (jobName) {\n jobName = `\"${jobName}\"`;\n } else {\n jobName = 'this job';\n }\n\n const keystoreType = \"DCP Bank Account\";\n\n let keystoreMsg = options.msg;\n\n let keystoreFile = await getKeystoreFromStorage(storageKey, jobName, onClose);\n if (!keystoreFile) {\n // currently only anonymous oauth access is implemented in this version\n keystoreFile = await anonGetKeystoreFromPopup(keystoreType, jobName, storageKey, onClose, keystoreMsg);\n }\n\n return keystoreFile;\n};\n\n/**\n* Alert user that a key from storage will be used, give them a chance to ditch it.\n* @param {string} storageKey key in local or session storage where the file is stored\n* @param {string} jobName helps user know which key is retrieved from storage\n* @returns {string} contents of Keystore file\n*/\nasync function getKeystoreFromStorage (storageKey, jobName) {\n let keystoreFile = sessionStorage.getItem(storageKey);\n if (keystoreFile) return keystoreFile;\n \n keystoreFile = localStorage.getItem(storageKey);\n if (keystoreFile) {\n let ks = JSON.parse(keystoreFile);\n let label = ks.label || ks.address;\n let prompt;\n // alert is not necessarily for a job, specify jobname only if there is an actual job\n if(jobName)\n prompt = `You have keystore \"${label}\" saved for ${jobName}. Do you want to use it?`;\n else\n prompt = `You have keystore \"${label}\". Do you want to use it?`;\n let opts = { positive: \"Yes\", negative: \"No\", title: 'Keystore File' };\n let confirmation = await confirm(prompt, opts);\n if (confirmation) {\n sessionStorage.setItem(storageKey, keystoreFile)\n return keystoreFile;\n }\n localStorage.removeItem(storageKey);\n return false;\n };\n};\n\n/**\n* Bring up anonymous (unregistered, default permissions) login prompt modal in browser\n* @param {string} keystoreType Help user know whether to upload an identity or bank or whatever keystore.\n* @param {string} jobName Help user identify the job this key will be used on.\n* @param {string} storageKey Store the key in local/session storage under this key.\n* @param {string} msg Custom message for the modal that specifies the purpose for providing a keystore\n* @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n*/\nasync function anonGetKeystoreFromPopup (keystoreType, jobName, storageKey, onClose, msg) {\n if (!onClose) {\n onClose = ()=>{};\n }\n\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n const [modal, form, prompt, fileInput, rememberInput] = elements;\n \n // default modal text that explains what a keystore is for.\n prompt.innerText = `This application requires that you log in to DCP`;\n if (msg)\n {\n prompt.innerText = msg;\n }\n \n const loginBtn = document.getElementById('dcp-modal-oauth-login-button');\n // make button open a popup with the configured oauth server's reflector page\n loginBtn.onclick = () => window.open(dcpConfig.oAuth.location.resolve('/reflector'), 'DCP Login', 'popup=yes,left=100,top=100,width=800,height=600');\n \n let loginks = 'none';\n \n // wait for a message back from the popup with the bank account keystore\n await new Promise((resolve) => {\n window.addEventListener('message', (event) => {\n // make sure it is coming from the reflector window\n if (dcpConfig.oAuth.location.sameOrigin(event.origin)) {\n loginks = event.data;\n resolve();\n } else {\n return;\n }\n });\n });\n \n const keystoreFile = loginks;\n \n utils.MicroModal.close(modal.id);\n return keystoreFile;\n};\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/oauthLogin.js?");
4192
+ eval("/**\n * @file oauthLogin.js\n * Modal providing a way to retrieve a keystore from the oauth login service\n * Originally based on keystoreFile.js\n * \n * @author Alex Huctwith - alex@kingsds.network\n * @date Feb 2022\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\nconst { confirm } = __webpack_require__(/*! ./confirm */ \"./src/dcp-client/client-modal/confirm.js\");\nconst passwordModule = __webpack_require__(/*! ./passwordEntry */ \"./src/dcp-client/client-modal/passwordEntry.js\");\n\nexports.config = {\n id: 'dcp-modal-oauth-login',\n required: ['dcp-modal-oauth-login-form'],\n path: \"./templates/oauth-login-modal.html\",\n eagerLoad: [passwordModule.config]\n};\n\n/**\n* @param {Object} options From `wallet.get_internal`\n* @param {string} options.name - The name of the keystore type (wallet.get => 'default' and wallet.getId => 'id').\n* @param {string} options.contextId - An optional, user-defined identifier used for caching keystores.\n* See `job.contextId` in the compute-api spec.\n* @param {string} options.jobName - A user-defined job property provided to identify this job.\n* @param {string} options.msg - Optional string denoting the purpose for fetching the keystore\n* @param {function} [onClose] Override default onClose behavior. An instance of `modal` is handed to the function.\n* @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n* @returns {string} contents of Keystore file\n* @memberof module:dcp/client-modal\n* @access public\n* @async\n* @alias getKeystoreFile\n*/\nexports.oauthLogin = async function (options, onClose=null) {\n const storageKey = `dcp-keystore-${options.name}-${options.contextId}`;\n \n let jobName = options.jobName;\n if (jobName) {\n jobName = `\"${jobName}\"`;\n } else {\n jobName = 'this job';\n }\n\n const keystoreType = \"DCP Bank Account\";\n\n let keystoreMsg = options.msg;\n\n let keystoreFile = await getKeystoreFromStorage(storageKey, jobName, onClose);\n if (!keystoreFile) {\n // currently only anonymous oauth access is implemented in this version\n keystoreFile = await anonGetKeystoreFromPopup(keystoreType, jobName, storageKey, onClose, keystoreMsg);\n }\n\n return keystoreFile;\n};\n\n/**\n* Alert user that a key from storage will be used, give them a chance to ditch it.\n* @param {string} storageKey key in local or session storage where the file is stored\n* @param {string} jobName helps user know which key is retrieved from storage\n* @returns {string} contents of Keystore file\n*/\nasync function getKeystoreFromStorage (storageKey, jobName) {\n let keystoreFile = sessionStorage.getItem(storageKey);\n if (keystoreFile) return keystoreFile;\n \n keystoreFile = localStorage.getItem(storageKey);\n if (keystoreFile) {\n let ks = JSON.parse(keystoreFile);\n let label = ks.label || ks.address;\n let prompt;\n // alert is not necessarily for a job, specify jobname only if there is an actual job\n if(jobName)\n prompt = `You have keystore \"${label}\" saved for ${jobName}. Do you want to use it?`;\n else\n prompt = `You have keystore \"${label}\". Do you want to use it?`;\n let opts = { positive: \"Yes\", negative: \"No\", title: 'Keystore File' };\n let confirmation = await confirm(prompt, opts);\n if (confirmation) {\n sessionStorage.setItem(storageKey, keystoreFile)\n return keystoreFile;\n }\n localStorage.removeItem(storageKey);\n return false;\n };\n};\n\n/**\n* Bring up anonymous (unregistered, default permissions) login prompt modal in browser\n* @param {string} keystoreType Help user know whether to upload an identity or bank or whatever keystore.\n* @param {string} jobName Help user identify the job this key will be used on.\n* @param {string} storageKey Store the key in local/session storage under this key.\n* @param {string} msg Custom message for the modal that specifies the purpose for providing a keystore\n* @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n*/\nasync function anonGetKeystoreFromPopup (keystoreType, jobName, storageKey, onClose, msg) {\n if (!onClose) {\n onClose = ()=>{};\n }\n\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n const [modal, form, prompt, fileInput] = elements;\n \n const loginBtn = document.getElementById('dcp-modal-oauth-login-button');\n // make button open a popup with the configured oauth server's reflector page\n loginBtn.onclick = () => window.open(dcpConfig.oAuth.location.resolve('/reflector'), 'DCP Login', 'popup=yes,left=200,top=200,width=400,height=360');\n \n let loginks = 'none';\n \n // wait for a message back from the popup with the bank account keystore\n await new Promise((resolve) => {\n window.addEventListener('message', (event) => {\n // make sure it is coming from the reflector window\n if (dcpConfig.oAuth.location.sameOrigin(event.origin)) {\n loginks = event.data;\n resolve();\n } else {\n return;\n }\n });\n });\n \n const keystoreFile = loginks;\n \n utils.MicroModal.close(modal.id);\n return keystoreFile;\n};\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/oauthLogin.js?");
4149
4193
 
4150
4194
  /***/ }),
4151
4195
 
@@ -4165,7 +4209,7 @@ eval("/**\n * @file passwordCreation.js\n * Modal providing
4165
4209
  \******************************************************/
4166
4210
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4167
4211
 
4168
- eval("/**\n * @file password.js\n * Modal providing a way to enter a password.\n * \n * @author KC Erb - kcerb@kingsds.network\n * @author Nazila Kharazian - nazila@kingsds.network\n * @author Andrew Fryer - andrewf@kingds.network\n * @date Apr 2020\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\n\nexports.config = {\n id: 'dcp-modal-password',\n required: ['dcp-modal-password-form', 'dcp-modal-password-hidden-username', 'dcp-modal-password-prompt', 'dcp-modal-password-input'],\n path: \"./templates/password-entry-modal.html\",\n};\n\n/**\n * @param {string} username The hidden username provided to give a hint to password managers.\n * @param {string} label We expect and use the label property to identify which thing this password is associated to.\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n */\nexports.passwordEntry = async function (username, label, maxTries, tryPassphrase, onClose=null)\n{\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n let [modal, form, hiddenUsername, prompt, passwordInput] = elements;\n\n hiddenUsername.value = username;\n prompt.innerText = `Please enter the passphrase for \"${label}\".`;\n\n const submitHandler = function(e)\n {\n setTimeout(function(){\n history.replaceState(null, document.title); // This should signal to the browser that the login was successful.\n }, 1);\n };\n\n // Don't overwrite the existing event handler!\n let existingSubmitHandler = form.onsubmit;\n form.onsubmit = async function(e)\n {\n // waiting for password validation promise to resolve\n const passwordIsValid = await tryPassphrase(passwordInput.value);\n if (passwordIsValid)\n {\n submitHandler(e);\n existingSubmitHandler(e);\n }\n else\n {\n prompt.innerText = `Incorrect passphrase for \"${label}\". Please try again.`;\n passwordInput.focus();\n }\n form.reset();\n };\n \n passwordInput.focus();\n await formPromise;\n\n // At this point, form validation has ensured that the last pasword entered was correct,\n // and this password was passed out of the modal via tryPassphrase.\n\n utils.MicroModal.close(modal.id);\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/passwordEntry.js?");
4212
+ eval("/**\n * @file password.js\n * Modal providing a way to enter a password.\n * \n * @author KC Erb - kcerb@kingsds.network\n * @author Nazila Kharazian - nazila@kingsds.network\n * @author Andrew Fryer - andrewf@kingds.network\n * @date Apr 2020\n */\nconst utils = __webpack_require__(/*! ./utils */ \"./src/dcp-client/client-modal/utils.js\");\n\nexports.config = {\n id: 'dcp-modal-password',\n required: ['dcp-modal-password-form', 'dcp-modal-password-hidden-username', 'dcp-modal-password-prompt', 'dcp-modal-password-input'],\n path: \"./templates/password-entry-modal.html\",\n};\n\n/**\n * @param {string} username The hidden username provided to give a hint to password managers.\n * @param {string} label We expect and use the label property to identify which thing this password is associated to.\n * @throws If modal is closed without the form being submitted (submitting the form closes the modal).\n */\nexports.passwordEntry = async function (username, label, maxTries, tryPassphrase, onClose=null)\n{\n const [elements, formPromise] = await utils.initModal(exports.config, onClose);\n let [modal, form, hiddenUsername, prompt, passwordInput] = elements;\n\n hiddenUsername.value = username;\n prompt.innerHTML = `Enter passphrase for <span style=\"color:#1aa473\"><i>${label}</i></span><br>`;\n\n const submitHandler = function(e)\n {\n setTimeout(function(){\n history.replaceState(null, document.title); // This should signal to the browser that the login was successful.\n }, 1);\n };\n\n // Don't overwrite the existing event handler!\n let existingSubmitHandler = form.onsubmit;\n form.onsubmit = async function(e)\n {\n // waiting for password validation promise to resolve\n const passwordIsValid = await tryPassphrase(passwordInput.value);\n if (passwordIsValid)\n {\n submitHandler(e);\n existingSubmitHandler(e);\n }\n else\n {\n prompt.innerHTML = `Incorrect passphrase for <span style=\"color:#1aa473\"><i>${label}</i></span><br><span style=\"color:#b83b33;text-align:center;\">Please try again.</span>`;\n passwordInput.focus();\n }\n form.reset();\n };\n \n passwordInput.focus();\n await formPromise;\n\n // At this point, form validation has ensured that the last pasword entered was correct,\n // and this password was passed out of the modal via tryPassphrase.\n\n utils.MicroModal.close(modal.id);\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/passwordEntry.js?");
4169
4213
 
4170
4214
  /***/ }),
4171
4215
 
@@ -4175,7 +4219,7 @@ eval("/**\n * @file password.js\n * Modal providing a way to
4175
4219
  \**********************************************/
4176
4220
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4177
4221
 
4178
- eval("/**\n * @file client-modal/utils.js\n * @author KC Erb\n * @date Mar 2020\n * \n * All shared functions among the modals.\n */\nconst { fetchRelative } = __webpack_require__(/*! ./fetch-relative */ \"./src/dcp-client/client-modal/fetch-relative.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nexports.OnCloseErrorCode = 'DCP_CM:CANCELX';\n\nif (DCP_ENV.isBrowserPlatform) {\n // Provide as export for the convenience of `utils.MicroModal` instead of a separate require.\n exports.MicroModal = __webpack_require__(/*! micromodal */ \"./node_modules/micromodal/dist/micromodal.es.js\")[\"default\"];\n}\n\n/**\n * Return a unique string, formatted as a GET parameter, that changes often enough to\n * always force the browser to fetch the latest version of our resource.\n *\n * @note Currently always returns the Date-based poison due to webpack. \n */\nfunction cachePoison() {\n if (true)\n return '?ucp=0bf54dbe88ee11e84b28e62b73cbd154d06967ea'; /* installer token */\n return '?ucp=' + Date.now();\n}\n \n/* Detect load type - on webpack, load dynamic content relative to webpack bundle;\n * otherwise load relative to the current scheduler's configured portal.\n */\nexports.myScript = (typeof document !== 'undefined') && document.currentScript;\nexports.corsProxyHref = undefined;\nif (exports.myScript && exports.myScript === (__webpack_require__(/*! ./fetch-relative */ \"./src/dcp-client/client-modal/fetch-relative.js\").myScript)) {\n let url = new ((__webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\").DcpURL))(exports.myScript.src);\n exports.corsProxyHref = url.resolve('../cors-proxy.html');\n}\n\n/**\n * Look for modal id and required ids on page based on config, if not found, provide from dcp-client.\n * The first id in the required array must be the id of the modal's form element.\n * @param {Object} modalConfig Modal configuration object\n * @param {string} modalConfig.id Id of parent modal element\n * @param {string[]} modalConfig.required Array of required ids in parent modal element\n * @param {string[]} [modalConfig.optional] Array of optional ids in parent modal element\n * @param {string} modalConfig.path Relative path to modal html in dcp-client\n * @returns {DOMElement[]} Array of modal elements on page [config.id, ...config.required]\n */\nexports.initModal = async function (modalConfig, onClose) {\n exports.corsProxyHref = exports.corsProxyHref || dcpConfig.portal.location.resolve('dcp-client/cors-proxy.html');\n\n // Call ensure modal on any eager-loaded modals.\n if (modalConfig.eagerLoad) {\n Promise.all(\n modalConfig.eagerLoad.map(config => ensureModal(config))\n )\n };\n\n const [elements, optionalElements] = await ensureModal(modalConfig);\n\n // Wire up form to prevent default, resolve on submission, reject+reset when closed (or call onClose when closed)\n const [modal, form] = elements;\n form.reset(); // ensure that form is fresh\n let formResolve, formReject;\n let formPromise = new Promise( function(res, rej) {\n formResolve = res;\n formReject = rej;\n });\n form.onsubmit = function (submitEvent) {\n submitEvent.preventDefault();\n modal.setAttribute(\"data-state\", \"submitted\");\n formResolve(submitEvent);\n }\n\n exports.MicroModal.show(modalConfig.id, { \n disableFocus: true, \n onClose: onClose || getDefaultOnClose(formReject)\n });\n return [elements, formPromise, optionalElements];\n};\n\n// Ensure all required modal elements are on page according to modalConfig\nasync function ensureModal(modalConfig) {\n let allRequiredIds = [modalConfig.id, ...modalConfig.required];\n let missing = allRequiredIds.filter( id => !document.getElementById(id) );\n if (missing.length > 0) {\n if (missing.length !== allRequiredIds.length)\n console.warn(`Some of the ids needed to replace the default DCP-modal were found, but not all. So the default DCP-Modal will be used. Missing ids are: [${missing}].`);\n let contents = await fetchRelative(exports.corsProxyHref, modalConfig.path + cachePoison());\n const container = document.createElement('div');\n container.innerHTML = contents;\n document.body.appendChild(container);\n }\n\n const elements = allRequiredIds.map(id => document.getElementById(id));\n const optionalElements = (modalConfig.optional || []).map(id => document.getElementById(id));\n return [elements, optionalElements];\n};\n\n// This onClose is called by MicroModal and thus has the modal passed to it.\nfunction getDefaultOnClose (formReject) {\n return (modal) => {\n modal.offsetLeft; // forces style recalc\n const origState = modal.dataset.state;\n // reset form including data-state\n modal.setAttribute(\"data-state\", \"new\");\n // reject if closed without submitting form.\n if (origState !== \"submitted\") {\n const err = new DCPError(\"Modal was closed but modal's form was not submitted.\", exports.OnCloseErrorCode);\n formReject(err);\n }\n }\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/utils.js?");
4222
+ eval("/**\n * @file client-modal/utils.js\n * @author KC Erb\n * @date Mar 2020\n * \n * All shared functions among the modals.\n */\nconst { fetchRelative } = __webpack_require__(/*! ./fetch-relative */ \"./src/dcp-client/client-modal/fetch-relative.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nexports.OnCloseErrorCode = 'DCP_CM:CANCELX';\n\nif (DCP_ENV.isBrowserPlatform) {\n // Provide as export for the convenience of `utils.MicroModal` instead of a separate require.\n exports.MicroModal = __webpack_require__(/*! micromodal */ \"./node_modules/micromodal/dist/micromodal.es.js\")[\"default\"];\n}\n\n/**\n * Return a unique string, formatted as a GET parameter, that changes often enough to\n * always force the browser to fetch the latest version of our resource.\n *\n * @note Currently always returns the Date-based poison due to webpack. \n */\nfunction cachePoison() {\n if (true)\n return '?ucp=5895f3465816246196dbdb9c0cd5aa200a943c59'; /* installer token */\n return '?ucp=' + Date.now();\n}\n \n/* Detect load type - on webpack, load dynamic content relative to webpack bundle;\n * otherwise load relative to the current scheduler's configured portal.\n */\nexports.myScript = (typeof document !== 'undefined') && document.currentScript;\nexports.corsProxyHref = undefined;\nif (exports.myScript && exports.myScript === (__webpack_require__(/*! ./fetch-relative */ \"./src/dcp-client/client-modal/fetch-relative.js\").myScript)) {\n let url = new ((__webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\").DcpURL))(exports.myScript.src);\n exports.corsProxyHref = url.resolve('../cors-proxy.html');\n}\n\n/**\n * Look for modal id and required ids on page based on config, if not found, provide from dcp-client.\n * The first id in the required array must be the id of the modal's form element.\n * @param {Object} modalConfig Modal configuration object\n * @param {string} modalConfig.id Id of parent modal element\n * @param {string[]} modalConfig.required Array of required ids in parent modal element\n * @param {string[]} [modalConfig.optional] Array of optional ids in parent modal element\n * @param {string} modalConfig.path Relative path to modal html in dcp-client\n * @returns {DOMElement[]} Array of modal elements on page [config.id, ...config.required]\n */\nexports.initModal = async function (modalConfig, onClose) {\n exports.corsProxyHref = exports.corsProxyHref || dcpConfig.portal.location.resolve('dcp-client/cors-proxy.html');\n\n // Call ensure modal on any eager-loaded modals.\n if (modalConfig.eagerLoad) {\n Promise.all(\n modalConfig.eagerLoad.map(config => ensureModal(config))\n )\n };\n\n const [elements, optionalElements] = await ensureModal(modalConfig);\n\n // Wire up form to prevent default, resolve on submission, reject+reset when closed (or call onClose when closed)\n const [modal, form] = elements;\n form.reset(); // ensure that form is fresh\n let formResolve, formReject;\n let formPromise = new Promise( function(res, rej) {\n formResolve = res;\n formReject = rej;\n });\n form.onsubmit = function (submitEvent) {\n submitEvent.preventDefault();\n modal.setAttribute(\"data-state\", \"submitted\");\n formResolve(submitEvent);\n }\n\n exports.MicroModal.show(modalConfig.id, { \n disableFocus: true, \n onClose: onClose || getDefaultOnClose(formReject)\n });\n return [elements, formPromise, optionalElements];\n};\n\n// Ensure all required modal elements are on page according to modalConfig\nasync function ensureModal(modalConfig) {\n let allRequiredIds = [modalConfig.id, ...modalConfig.required];\n let missing = allRequiredIds.filter( id => !document.getElementById(id) );\n if (missing.length > 0) {\n if (missing.length !== allRequiredIds.length)\n console.warn(`Some of the ids needed to replace the default DCP-modal were found, but not all. So the default DCP-Modal will be used. Missing ids are: [${missing}].`);\n let contents = await fetchRelative(exports.corsProxyHref, modalConfig.path + cachePoison());\n const container = document.createElement('div');\n container.innerHTML = contents;\n document.body.appendChild(container);\n }\n\n const elements = allRequiredIds.map(id => document.getElementById(id));\n const optionalElements = (modalConfig.optional || []).map(id => document.getElementById(id));\n return [elements, optionalElements];\n};\n\n// This onClose is called by MicroModal and thus has the modal passed to it.\nfunction getDefaultOnClose (formReject) {\n return (modal) => {\n modal.offsetLeft; // forces style recalc\n const origState = modal.dataset.state;\n // reset form including data-state\n modal.setAttribute(\"data-state\", \"new\");\n // reject if closed without submitting form.\n if (origState !== \"submitted\") {\n const err = new DCPError(\"Modal was closed but modal's form was not submitted.\", exports.OnCloseErrorCode);\n formReject(err);\n }\n }\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/client-modal/utils.js?");
4179
4223
 
4180
4224
  /***/ }),
4181
4225
 
@@ -4186,7 +4230,7 @@ eval("/**\n * @file client-modal/utils.js\n * @author KC Erb\n * @date Mar 2020\
4186
4230
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4187
4231
 
4188
4232
  "use strict";
4189
- eval("/**\n * @file Client facing module that implements Compute Groups API\n * @module dcp/compute-groups\n * @access public\n * @author Kayra E-A <kayra@kingsds.network>\n * Wes Garland <wes@kingsds.network>\n * Paul <paul@kingsds.network>\n * @date Sept 2020\n * February 2022\n * May 2022\n */\n\n\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst hash = __webpack_require__(/*! ../../common/hash */ \"./src/common/hash.js\");\nconst { DCPError } = __webpack_require__(/*! ../../common/dcp-error */ \"./src/common/dcp-error.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('scheduler');\nconst { Address } = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst constants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { clientError, reconstructServiceError } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\n/** @typedef {import('dcp/utils').apiServiceType} apiServiceType */\n/** @typedef {import('dcp/utils').apiClientType} apiClientType */\n/** @typedef {string} opaqueId */\n\n/**\n * @typedef {object} cgAccessType\n * @property {opaqueId} [id]\n * @property {string} [joinKey]\n */\n\n/**\n * @typedef {object} cgClientJoinType\n * @property {opaqueId} [id]\n * @property {Address} [joinAddress]\n * @property {string} [joinKey]\n * @property {string} [joinSecret]\n * @property {string} [joinHash]\n */\n\n/**\n * @typedef {object} cgServiceJoinType\n * @property {opaqueId} [id]\n * @property {Address} [joinAddress]\n * @property {string} [joinKey]\n * @property {string} [joinHashHash]\n */\n\n/**\n * Establishes the client connection to the computeGroups microservice if it does not exist already from the default config.\n * \n * @returns {protocolV4.Connection}\n * @access public\n * @example\n * const result = await exports.serviceConnection.send('createGroup', {\n name: name,\n description: description,\n });\n */\n\nexports.serviceConnection = null;\n\n//\n// Reference counting pattern:\n// For every time addRef is called,\n// closeServiceConnection must eventually be called.\n// Reference counting allows multiple execs in a Promise.all .\n//\nvar refCount = 0;\nexports.addRef = function addRef() {\n refCount++;\n}\n\nconst openAndConnectServiceConn = async function openAndConnectServiceConn()\n{\n exports.serviceConnection = new protocolV4.Connection(dcpConfig.scheduler.services.computeGroups);\n exports.serviceConnection.on('close', openAndConnectServiceConn);\n await exports.serviceConnection.connect();\n refCount = 0; // Help with sanity.\n}\n\n/**\n * Resets the client connection to the computeGroups microservice.\n */\nexports.closeServiceConnection = async function closeServiceConnection() {\n if (refCount > 0) refCount--;\n if (exports.serviceConnection && refCount < 1)\n {\n exports.serviceConnection.off('close', openAndConnectServiceConn);\n exports.serviceConnection.close(null, true);\n refCount = 0; // Help with sanity.\n exports.serviceConnection = null;\n }\n};\n\n/**\n * (Used in jobs/index.js)\n * KeepAlive for the service connection to compute groups.\n */\nexports.keepAlive = async function keepAlive() {\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n exports.serviceConnection.keepalive().catch(err => console.error('Warning: keepalive failed for compute groups service', err));\n}\n\n/**\n * Checks whether descriptor corresponds to the public compute group from the scheduler constants.\n */\nexports.isPublicComputeGroup = function isPublicComputeGroup(descriptor) {\n return descriptor.id === constants.computeGroups.public.id\n && descriptor.opaqueId === constants.computeGroups.public.opaqueId;\n};\n\n/**\n * Returns a compute group identification snippet for diagnostic messages,\n * @param {object} descriptor - Must have one of the properties joinKey, id (id:=opaqueId). Specifically\n * descriptor = { joinKey: 'dcpDemo' } or descriptor = { id: 'bYcYGQ3NOpFnP4FKs6IBQd' },\n * where the corresponding row in table computeGroups have attributes\n * joinKey:='dcpDemo' or opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd' .\n * @returns {string}\n */\nfunction cgId(descriptor) {\n return (descriptor.joinKey) ? `joinKey ${descriptor.joinKey}` : `id ${descriptor.id}`;\n}\n\n/**\n * Verify sufficient information in descriptor to access a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgAccessType} descriptor \n * @param {string} methodName \n */\nfunction validateCGDescriptor(descriptor, methodName) {\n for (const prop in descriptor) {\n if ([ 'id', 'joinKey' ].includes(prop)) continue;\n if ([ 'joinAddress', 'joinHash', 'joinSecret' ].includes(prop))\n console.warn(`It is not necessary to specify '${prop}' in the descriptor ${JSON.stringify(descriptor)} when calling ${methodName}`);\n else\n console.error(`Do not specify '${prop}' in the descriptor ${JSON.stringify(descriptor)} when calling ${methodName}`);\n }\n}\n\n/**\n * Verify sufficient information in descriptor to authorize a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgClientJoinType} joinDescriptor \n * @param {string} methodName \n */\nfunction validateCGJoinDescriptor(joinDescriptor, methodName) {\n for (const prop in joinDescriptor) {\n if ([ 'id', 'joinKey', 'joinSecret', 'joinHash', 'joinAddress' ].includes(prop)) continue;\n console.error(`Do not specify '${prop}' in the descriptor ${JSON.stringify(joinDescriptor)} when calling ${methodName}`);\n }\n}\n\n/**\n * Build message to go across the wire.\n * Verify sufficient information in descriptor to access a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgAccessType} descriptor\n * @param {string} methodName\n * @returns {cgAccessType}\n */\nfunction buildCGMessage(descriptor, methodName)\n{\n if (exports.isPublicComputeGroup(descriptor)) return descriptor;\n\n const message = {};\n // Construct message.joinKey xor message.id .\n if (descriptor.joinKey) message.joinKey = descriptor.joinKey;\n else if (descriptor.id) message.id = descriptor.id; // id:=opaqueId\n\n debugging('computeGroups') && console.debug(`${methodName}:buildCGMessage: descriptor`, descriptor, 'message', message);\n\n validateCGDescriptor(descriptor, methodName);\n\n return message;\n}\n\n/**\n * Build message so that joinHash, joinSecret, opaqueId do not go across the wire.\n * Verify sufficient information in descriptor to authorize a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgClientJoinType} descriptor\n * @param {string} methodName\n * @returns {cgServiceJoinType}\n */\nfunction buildCGJoinMessage(descriptor, methodName)\n{\n if (exports.isPublicComputeGroup(descriptor)) return descriptor;\n\n const message = {};\n // Construct message.joinKey xor message.id .\n if (descriptor.joinKey) message.joinKey = descriptor.joinKey;\n else if (descriptor.id) message.id = descriptor.id; // id:=opaqueId\n // Construct message.joinAddress .\n if (descriptor.joinAddress) message.joinAddress = descriptor.joinAddress;\n\n debugging('computeGroups') && console.debug(`${methodName}:buildCGJoinMessage: descriptor`, descriptor, 'message', message);\n\n validateCGJoinDescriptor(descriptor, methodName);\n\n // Construct message.joinHashHash .\n if (descriptor.joinSecret) message.joinHashHash = hash.calculate(hash.eh1, exports.calculateJoinHash(descriptor), exports.serviceConnection.dcpsid);\n if (descriptor.joinHash) message.joinHashHash = hash.calculate(hash.eh1, descriptor.joinHash, exports.serviceConnection.dcpsid);\n\n return message;\n}\n\nfunction hasSufficientJoinInfo(joinDescriptor) {\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n return (joinDescriptor.joinKey && (joinDescriptor.joinSecret || joinDescriptor.joinHash))\n || (joinDescriptor.id && joinDescriptor.joinAddress)\n || exports.isPublicComputeGroup(joinDescriptor);\n}\n\nconst newCGPrototype = { type: 'object',\n parameters: {\n // name: { type: 'string', default: undefined }, /* name of group (length <= 255) */\n // description: { type: 'string', default: undefined }, /* description of group (length <= 255) */\n // id: { type: 'string', default: undefined }, /* opaqueId, the unique identifier of the compute group; nanoid (length === 22) */\n // joinKey: { type: 'string', default: undefined }, /* basically the login (length <= 255) */\n // joinSecret: { type: 'string', default: undefined }, /* basically the password (length <= 255) */\n // joinHash: { type: 'string', default: undefined }, /* basically the password, the joinSecret seeded & hashed */\n // joinAddress: { type: Address, default: undefined }, /* signature gives alternative to login/password */\n\n commissionRate: { type: 'BigNumber', default: undefined }, /* commission, see DCP-1889 */\n deployFee: { type: 'BigNumber', default: undefined }, /* number of DCC to take for every deployment */\n deployAccess: { type: 'string', default: undefined }, /* can be \"owner\"|\"join\" (dcp-1910) */\n addJobFee: { type: 'BigNumber', default: undefined }, /* fee required each time a job joins a compute group */\n maxTotalPayment: { type: 'BigNumber', default: undefined }, /* limit on maximum job payment, NULL => Infinity */\n\n /* Administrative limits on group. NULL => Infinity: Should all be integers or undefined. */\n maxConcurrentJobs: { type: 'number', default: undefined },\n maxConcurrentWorkers: { type: 'number', default: undefined },\n maxConcurrentSandboxes: { type: 'number', default: undefined },\n maxConcurrentCPUs: { type: 'number', default: undefined },\n maxConcurrentGPUs: { type: 'number', default: undefined },\n maxConcurrentEscrow: { type: 'BigNumber', default: undefined },\n },\n};\n\n/**\n * Async function that creates a new Compute Group.\n *\n * The joinDescriptor is of the form { joinKey, joinSecret }, { joinKey, joinHash } or { id, joinAddress }.\n * where id will correspond to the attribute opaqueId in the new row in the computeGroups table.\n *\n * This function can only be called with ADMIN permission.\n * Properties not appearing in newCGPrototype.parameters are not allowed in otherProperties.\n *\n * @param {cgClientJoinType} joinDescriptor - Must have properly defined { joinKey, joinSecret }, { joinKey, joinHash }\n * or { id, joinAddress }, where id will correspond to the attribute opaqueId\n * in the new row in the computeGroups table.\n * @param {string} [name] - The name of the compute group.\n * @param {string} [description] - The description of the compute group.\n * @param {object} [otherProperties] - The 5 attributes of table computeGroup related to commissions and fees.\n * commissionRate: notNull(zFinNum),// commission, see DCP-1889\n * deployFee: notNull(zFinNum),// number of DCC to take for every deployment\n * deployAccess: string, // can be \"owner\"|\"join\" (dcp-1910)\n * addJobFee: notNull(zFinNum),// fee required each time a job joins a compute group\n * maxTotalPayment: finNum, // limit on maximum job payment, NULL => Infinity\n * And the 6 attributes of table computeGroup related to limits.\n * maxConcurrentJobs: integer,\n * maxConcurrentWorkers: integer,\n * maxConcurrentSandboxes: integer,\n * maxConcurrentCPUs: integer,\n * maxConcurrentGPUs: integer,\n * maxConcurrentEscrow: finNum,\n * @returns {Promise<apiClientType>} - { success, payload: computeGroup.id }\n * @access public\n * @example\n * await computeGroup.createGroup({ joinKey: 'dcpDemo', joinSecret: 'theSecret' }, 'myCGName', 'myCGDescription', { deployFee: 0.00015 });\n * await computeGroup.createGroup({ joinKey: 'dcpDemo2', joinHash: 'eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2' });\n * await computeGroup.createGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd', joinAddress: 'c15053fc30d4bdf91e2e0bba79578f8b649e55ea' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: joinKey:='dcpDemo2', joinHash:='eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2'\n * row3: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd', joinAddress:='c15053fc30d4bdf91e2e0bba79578f8b649e55ea' .\n */\nexports.createGroup = async function createGroup(joinDescriptor, name, description, otherProperties)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n validateCGJoinDescriptor(joinDescriptor, 'createGroup');\n\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n if (!hasSufficientJoinInfo(joinDescriptor))\n return clientError(`createGroup: Insufficient information to authorize compute group: ${JSON.stringify(joinDescriptor)}.`);\n\n // Validate the properties in otherProperties.\n for (const methodName in otherProperties) {\n if (!Object.keys(newCGPrototype.parameters).includes(methodName))\n return clientError(`createGroup: Property ${methodName} cannot be speicfied in otherProperties. Can only specify ${JSON.stringify(Object.keys(newCGPrototype.parameters))}`);\n }\n\n // Translate joinSecret to joinHash.\n if (joinDescriptor.joinSecret) {\n joinDescriptor.joinHash = exports.calculateJoinHash(joinDescriptor);\n delete joinDescriptor.joinSecret;\n }\n\n if (otherProperties && (otherProperties.commissionRate < 0 || otherProperties.commissionRate >= 1))\n return clientError(`client-createGroup: commissionRate ${otherProperties.commissionRate} must be between 0 and 1 (0 <= commissionRate < 1).`);\n\n debugging('computeGroups') && console.debug('client-createGroup: input:', joinDescriptor, name, description, otherProperties);\n\n const { success, payload } = await exports.serviceConnection.send('createGroup', { joinDescriptor, name, description, otherProperties });\n\n if (!success) return clientError(`Cannot create new compute group, with ${cgId(joinDescriptor)}.`);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n debugging('computeGroups') && console.debug('client-createGroup: payload', payload);\n\n return payload;\n};\n\nconst changeCGPrototype = { type: 'object',\n parameters: {\n name: { type: 'string', default: undefined }, /* name of group (length <= 255) */\n description: { type: 'string', default: undefined }, /* description of group (length <= 255) */\n joinHash: { type: 'string', default: undefined }, /* basically the password, seeded & hashed (length <= 255) */\n joinAddress: { type: Address, default: undefined }, /* signature gives alternative to login/password */\n\n commissionRate: { type: 'BigNumber', default: undefined }, /* commission, see DCP-1889 */\n deployFee: { type: 'BigNumber', default: undefined }, /* number of DCC to take for every deployment */\n deployAccess: { type: 'string', default: undefined }, /* can be \"owner\"|\"join\" (dcp-1910) */\n addJobFee: { type: 'BigNumber', default: undefined }, /* fee required each time a job joins a compute group */\n maxTotalPayment: { type: 'BigNumber', default: undefined }, /* limit on maximum job payment, NULL => Infinity */\n\n /* Administrative limits on group. NULL => Infinity: Should all be integers or undefined. */\n maxConcurrentJobs: { type: 'number', default: undefined },\n maxConcurrentWorkers: { type: 'number', default: undefined },\n maxConcurrentSandboxes: { type: 'number', default: undefined },\n maxConcurrentCPUs: { type: 'number', default: undefined },\n maxConcurrentGPUs: { type: 'number', default: undefined },\n maxConcurrentEscrow: { type: 'BigNumber', default: undefined },\n },\n};\n\n/**\n * Async function that changes a new Compute Group.\n * \n * The parameter newDescriptor contains the new property values,\n * and the properties that are allowed to be changed appear in changeCGPrototype.parameters.\n * \n * The descriptor must have joinKey or id, where id:=opaqueId.\n * Must own the compute group or be ADMIN to use changeGroup.\n * \n * @param {cgAccessType} descriptor - Must have joinkey or id, where id:=opaqueId.\n * @param {object} newDescriptor - Properties not appearing in changeCGPrototype.parameters are not allowed.\n * @returns {Promise<apiClientType>}\n * await computeGroup.changeGroup({ joinKey: 'dcpDemo' }, { joinSecret: 'myNewPasswrd' });\n * await computeGroup.changeGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' }, { name: 'myNewName', deployFee: 0.0001 });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.changeGroup = async function changeGroup(descriptor, newDescriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`changeGroup: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n // Validate the properties in newDescriptor.\n for (const methodName in newDescriptor) {\n if (!Object.keys(changeCGPrototype.parameters).includes(methodName))\n return clientError(`changeGroup: Not allowed to change property ${methodName}. Can only change ${JSON.stringify(Object.keys(changeCGPrototype.parameters))}`);\n }\n\n // Translate joinSecret to joinHash.\n if (newDescriptor.joinSecret) {\n newDescriptor.joinHash = exports.calculateJoinHash(newDescriptor);\n delete newDescriptor.joinSecret;\n }\n\n descriptor = buildCGMessage(descriptor, 'changeGroup');\n debugging('computeGroups') && console.debug('change compute group client:', descriptor, newDescriptor);\n const { success, payload } = await exports.serviceConnection.send('changeGroup', { descriptor, newDescriptor });\n\n if (!success) throw new DCPError(`Cannot change compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that deletes a compute group.\n * \n * The descriptor must have joinkey or id, where id:=opaqueId.\n * \n * Must either own the group or be ADMIN.\n * If not ADMIN, then the following config must be true:\n * dcpConfig.scheduler.services.computeGroups.usersCanDeleteGroups\n * \n * @param {cgAccessType} descriptor - Must contain joinKey or id (id:=opaqueId) \n * @returns {Promise<apiClientType>}\n * await computeGroup.deleteGroup({ joinKey: 'dcpDemo' });\n * await computeGroup.deleteGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.deleteGroup = async function deleteGroup(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`deleteGroup: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'deleteGroup');\n debugging('computeGroups') && console.debug('delete compute group client:', descriptor);\n const { success, payload } = await exports.serviceConnection.send('deleteGroup', { descriptor });\n\n if (!success) throw new DCPError(`Cannot delete compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that adds a job to a specified compute group. \n * \n * Must be the owner of the job.\n *\n * Useful feedback is provided from this function, as it\n * will make its way back to the application developer, *after* they have made the\n * deployment fee micropayment.\n *\n * On the client side the access model in place is that if you know the (user/password)\n * joinKey+joinSecret/joinKey+joinHash/joinKey+joinHashHash/id+joinAddress,\n * you can add the job to the compute groups, where id:=opaqueId from table computeGroups.\n * On the service side the corresponding access model is\n * joinKey+joinHashHash/id+joinAddress .\n * Access is also allowed if the compute group owner is the connection peerAddress.\n * \n * Unless the compute group owner is the connection peerAddress, element of the descriptor array must contain\n * { joinKey, joinSecret }, { joinKey, joinHash } or { id, joinAddress }\n * where the value of id in { id, joinAddress } is the opaqueId attribute of the row in table computeGroups.\n *\n * @param {Address} job The address of the Job that will be added to the Compute Group.\n * @param {Array} computeGroups Array of descriptor objects for the compute groups. This descriptor\n * needs to contain enough information to authorize access to the\n * compute group. Properties may include:\n * - id (id:=opaqueId)\n * - joinKey\n * - joinSecret\n * - joinHash\n * - joinAddress\n * \n * Additional, either the joinKey or id MUST be specified so\n * that we can identify the compute group in question.\n *\n * All compute groups can have jobs submitted to them, provided either the joinKey\n * or the id are specified, and the message contains valid join permission and the \n * job is owned by the caller of addJobToGroups.\n *\n * FUTURE - after DCP-1910\n * keystore A keystore used to grant access to job deployment within this compute group.\n * This can be either the ownerKeystore or the joinAddress keystore when the\n * compute group is in deployAccessType='join' mode.\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.addJobToGroups('P+Y4IApeFQLrYS2W7MkVg7', \n * [ { joinKey: 'dcpDemo', joinSecret: 'theSecret' },\n * { joinKey: 'dcpDemo2', joinHash: 'eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2' }, \n * { id: 'bYcYGQ3NOpFnP4FKs6IBQd', joinAddress: 'c15053fc30d4bdf91e2e0bba79578f8b649e55ea' } ]);\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: joinKey:='dcpDemo2', joinHash:='eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2'\n * row3: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd', joinAddress:='c15053fc30d4bdf91e2e0bba79578f8b649e55ea' .\n */\nexports.addJobToGroups = async function addJobToGroups(job, computeGroups)\n{\n // ensure the service connection is hot before calling buildCGJoinMessage:\n await exports.keepAlive();\n\n const cgArray = [];\n for (const joinDescriptor of computeGroups)\n {\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n if (!hasSufficientJoinInfo(joinDescriptor))\n {\n console.error(`addJobToGroups: Insufficient information to authorize compute group: ${JSON.stringify(joinDescriptor)}.`);\n continue;\n }\n\n // Translate so that neither joinHash nor joinSecret goes across the wire.\n const message = buildCGJoinMessage(joinDescriptor, 'addJobToGroups');\n debugging('computeGroups') && console.debug(`addJobToGroups client: job ${job}, message`, message);\n\n cgArray.push(message);\n }\n\n const { success, payload } = await exports.serviceConnection.send('addJobToGroups', { job, cgArray });\n\n debugging('computeGroups') && console.debug('addJobToGroups payload', payload);\n\n if (!success) throw new DCPError(`Cannot add job ${job} to compute groups.`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n // If the server reported success but did not return a list of CGs (eg. v.4.2.5 server),\n // assume (and inform the client) we added all groups successfully\n return payload || computeGroups;\n};\n\n/**\n * Async function that lists all the Jobs in a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to list jobs from it.\n * The job does not need to be owned.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {cgAccessType} descriptor - Must have one of the properties joinKey, id (id:=opaqueId). Specifically\n * descriptor = { joinKey: 'dcpDemo' } or descriptor = { id: opaqueId }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * let listOfJobs1 = await computeGroup.listJobs({ joinKey: 'dcpDemo' });\n * let listOfJobs2 = await computeGroup.listJobs({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.listJobs = async function listJobs(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`listJobs: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'listJobs');\n debugging('computeGroups') && console.debug('listJob client: descriptor', descriptor);\n const { success, payload } = await exports.serviceConnection.send('listJobs', { descriptor });\n\n if (!success) throw new DCPError(`Cannot list jobs for compute group with ${cgId(descriptor)}`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that removes a job from a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to remove a job from it.\n * The job does not need to be owned.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {Address} job - The address of the Job that will be added to the Compute Group.\n * @param {cgAccessType} descriptor - { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.removeJob( 'P+Y4IApeFQLrYS2W7MkVg7', { joinKey: 'dcpDemo' });\n * await computeGroup.removeJob( 'P+Y4IApeFQLrYS2W7MkVg7', { id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.removeJob = async function removeJob(job, descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`removeJob: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'removeJob');\n debugging('computeGroups') && console.debug(`removeJob client: job ${job}, descriptor`, descriptor);\n const { success, payload } = await exports.serviceConnection.send('removeJob', { job, descriptor });\n\n if (!success) throw new DCPError(`Cannot remove job ${job} from compute group with ${cgId(descriptor)}`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that removes all jobs from a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to remove jobs from it.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {cgAccessType} descriptor - { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.removeAllJobs({ joinKey: 'dcpDemo' });\n * await computeGroup.removeAllJobs({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.removeAllJobs = async function removeAllJobs(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`removeAllJobs: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'removeAllJobs');\n debugging('computeGroups') && console.debug('removeAllJobs client: descriptor', descriptor);\n const { success, payload } = await exports.serviceConnection.send('removeAllJobs', { descriptor });\n\n if (!success) throw new DCPError(`Cannot remove all jobs from compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Calculate a joinHash for a compute group. This is an eh1- hash of the cg salt and \n * joinSecret components of a compute group description.\n *\n * @param {object} details an object containing the cg salt, which is\n * the joinKey if the compute group uses one;\n * otherwise it is the joinAddress. This object\n * may also contain the joinSecret.\n * @param {string} [joinSecret] the join secret -- plain text -- that is\n * the \"password\" for the compute group. If not\n * specified, we use details.joinSecret.\n */\nexports.calculateJoinHash = function computeGroups$calculateJoinHash(details, joinSecret)\n{\n if (typeof joinSecret === 'undefined')\n joinSecret = details.joinSecret;\n\n return hash.calculate(hash.eh1, `${details.joinKey || details.joinAddress} ${joinSecret}`);\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/compute-groups/index.js?");
4233
+ eval("/**\n * @file Client facing module that implements Compute Groups API\n * @module dcp/compute-groups\n * @access public\n * @author Kayra E-A <kayra@kingsds.network>\n * Wes Garland <wes@kingsds.network>\n * Paul <paul@kingsds.network>\n * @date Sept 2020\n * February 2022\n * May 2022\n */\n\n\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst hash = __webpack_require__(/*! ../../common/hash */ \"./src/common/hash.js\");\nconst { DCPError } = __webpack_require__(/*! ../../common/dcp-error */ \"./src/common/dcp-error.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('scheduler');\nconst { Address } = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst constants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { clientError, reconstructServiceError } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\n/** @typedef {import('dcp/utils').apiServiceType} apiServiceType */\n/** @typedef {import('dcp/utils').apiClientType} apiClientType */\n/** @typedef {string} opaqueId */\n\n/**\n * @typedef {object} cgAccessType\n * @property {opaqueId} [id]\n * @property {string} [joinKey]\n */\n\n/**\n * @typedef {object} cgClientJoinType\n * @property {opaqueId} [id]\n * @property {Address} [joinAddress]\n * @property {string} [joinKey]\n * @property {string} [joinSecret]\n * @property {string} [joinHash]\n */\n\n/**\n * @typedef {object} cgServiceJoinType\n * @property {opaqueId} [id]\n * @property {Address} [joinAddress]\n * @property {string} [joinKey]\n * @property {string} [joinHashHash]\n */\n\n/**\n * Establishes the client connection to the computeGroups microservice if it does not exist already from the default config.\n * \n * @returns {protocolV4.Connection}\n * @access public\n * @example\n * const result = await exports.serviceConnection.send('createGroup', {\n name: name,\n description: description,\n });\n */\n\nexports.serviceConnection = null;\n\n//\n// Reference counting pattern:\n// For every time addRef is called,\n// closeServiceConnection must eventually be called.\n// Reference counting allows multiple execs in a Promise.all .\n//\nvar refCount = 0;\nexports.addRef = function addRef() {\n refCount++;\n}\n\nconst openAndConnectServiceConn = async function openAndConnectServiceConn()\n{\n exports.serviceConnection = new protocolV4.Connection(dcpConfig.scheduler.services.computeGroups);\n exports.serviceConnection.on('close', openAndConnectServiceConn);\n await exports.serviceConnection.connect();\n refCount = 0; // Help with sanity.\n}\n\n/**\n * Resets the client connection to the computeGroups microservice.\n */\nexports.closeServiceConnection = async function closeServiceConnection() {\n if (refCount > 0) refCount--;\n if (exports.serviceConnection && refCount < 1)\n {\n exports.serviceConnection.off('close', openAndConnectServiceConn);\n exports.serviceConnection.close(null, true);\n refCount = 0; // Help with sanity.\n exports.serviceConnection = null;\n }\n};\n\n/**\n * (Used in jobs/index.js)\n * KeepAlive for the service connection to compute groups.\n */\nexports.keepAlive = async function keepAlive() {\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n exports.serviceConnection.keepalive().catch(err => console.error('Warning: keepalive failed for compute groups service', err));\n}\n\n/**\n * Checks whether descriptor corresponds to the public compute group\n * defined in scheduler constants.\n */\nexports.isPublicComputeGroup = function isPublicComputeGroup(descriptor) {\n const publicGroup = constants.computeGroups.public;\n return descriptor.id === publicGroup.id\n || descriptor.opaqueId === publicGroup.opaqueId\n || descriptor.joinKey === publicGroup.joinKey;\n};\n\n/**\n * Returns a compute group identification snippet for diagnostic messages,\n * @param {object} descriptor - Must have one of the properties joinKey, id (id:=opaqueId). Specifically\n * descriptor = { joinKey: 'dcpDemo' } or descriptor = { id: 'bYcYGQ3NOpFnP4FKs6IBQd' },\n * where the corresponding row in table computeGroups have attributes\n * joinKey:='dcpDemo' or opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd' .\n * @returns {string}\n */\nfunction cgId(descriptor) {\n return (descriptor.joinKey) ? `joinKey ${descriptor.joinKey}` : `id ${descriptor.id}`;\n}\n\n/**\n * Verify sufficient information in descriptor to access a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgAccessType} descriptor \n * @param {string} methodName \n */\nfunction validateCGDescriptor(descriptor, methodName) {\n for (const prop in descriptor) {\n if ([ 'id', 'joinKey' ].includes(prop)) continue;\n if ([ 'joinAddress', 'joinHash', 'joinSecret' ].includes(prop))\n console.warn(`It is not necessary to specify '${prop}' in the descriptor ${JSON.stringify(descriptor)} when calling ${methodName}`);\n else\n console.error(`Do not specify '${prop}' in the descriptor ${JSON.stringify(descriptor)} when calling ${methodName}`);\n }\n}\n\n/**\n * Verify sufficient information in descriptor to authorize a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgClientJoinType} joinDescriptor \n * @param {string} methodName \n */\nfunction validateCGJoinDescriptor(joinDescriptor, methodName) {\n for (const prop in joinDescriptor) {\n if ([ 'id', 'joinKey', 'joinSecret', 'joinHash', 'joinAddress' ].includes(prop)) continue;\n console.error(`Do not specify '${prop}' in the descriptor ${JSON.stringify(joinDescriptor)} when calling ${methodName}`);\n }\n}\n\n/**\n * Build message to go across the wire.\n * Verify sufficient information in descriptor to access a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgAccessType} descriptor\n * @param {string} methodName\n * @returns {cgAccessType}\n */\nfunction buildCGMessage(descriptor, methodName)\n{\n if (exports.isPublicComputeGroup(descriptor)) return descriptor;\n\n const message = {};\n // Construct message.joinKey xor message.id .\n if (descriptor.joinKey) message.joinKey = descriptor.joinKey;\n else if (descriptor.id) message.id = descriptor.id; // id:=opaqueId\n\n debugging('computeGroups') && console.debug(`${methodName}:buildCGMessage: descriptor`, descriptor, 'message', message);\n\n validateCGDescriptor(descriptor, methodName);\n\n return message;\n}\n\n/**\n * Build message so that joinHash, joinSecret, opaqueId do not go across the wire.\n * Verify sufficient information in descriptor to authorize a compute group.\n * Emit diagnostics about unnecessary information.\n * @param {cgClientJoinType} descriptor\n * @param {string} methodName\n * @returns {cgServiceJoinType}\n */\nfunction buildCGJoinMessage(descriptor, methodName)\n{\n if (exports.isPublicComputeGroup(descriptor)) return descriptor;\n\n const message = {};\n // Construct message.joinKey xor message.id .\n if (descriptor.joinKey) message.joinKey = descriptor.joinKey;\n else if (descriptor.id) message.id = descriptor.id; // id:=opaqueId\n // Construct message.joinAddress .\n if (descriptor.joinAddress) message.joinAddress = descriptor.joinAddress;\n\n debugging('computeGroups') && console.debug(`${methodName}:buildCGJoinMessage: descriptor`, descriptor, 'message', message);\n\n validateCGJoinDescriptor(descriptor, methodName);\n\n // Construct message.joinHashHash .\n if (descriptor.joinSecret) message.joinHashHash = hash.calculate(hash.eh1, exports.calculateJoinHash(descriptor), exports.serviceConnection.dcpsid);\n if (descriptor.joinHash) message.joinHashHash = hash.calculate(hash.eh1, descriptor.joinHash, exports.serviceConnection.dcpsid);\n\n return message;\n}\n\nfunction hasSufficientJoinInfo(joinDescriptor) {\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n return (joinDescriptor.joinKey && (joinDescriptor.joinSecret || joinDescriptor.joinHash))\n || (joinDescriptor.id && joinDescriptor.joinAddress)\n || exports.isPublicComputeGroup(joinDescriptor);\n}\n\nconst newCGPrototype = { type: 'object',\n parameters: {\n // name: { type: 'string', default: undefined }, /* name of group (length <= 255) */\n // description: { type: 'string', default: undefined }, /* description of group (length <= 255) */\n // id: { type: 'string', default: undefined }, /* opaqueId, the unique identifier of the compute group; nanoid (length === 22) */\n // joinKey: { type: 'string', default: undefined }, /* basically the login (length <= 255) */\n // joinSecret: { type: 'string', default: undefined }, /* basically the password (length <= 255) */\n // joinHash: { type: 'string', default: undefined }, /* basically the password, the joinSecret seeded & hashed */\n // joinAddress: { type: Address, default: undefined }, /* signature gives alternative to login/password */\n\n commissionRate: { type: 'BigNumber', default: undefined }, /* commission, see DCP-1889 */\n deployFee: { type: 'BigNumber', default: undefined }, /* number of DCC to take for every deployment */\n deployAccess: { type: 'string', default: undefined }, /* can be \"owner\"|\"join\" (dcp-1910) */\n addJobFee: { type: 'BigNumber', default: undefined }, /* fee required each time a job joins a compute group */\n maxTotalPayment: { type: 'BigNumber', default: undefined }, /* limit on maximum job payment, NULL => Infinity */\n\n /* Administrative limits on group. NULL => Infinity: Should all be integers or undefined. */\n maxConcurrentJobs: { type: 'number', default: undefined },\n maxConcurrentWorkers: { type: 'number', default: undefined },\n maxConcurrentSandboxes: { type: 'number', default: undefined },\n maxConcurrentCPUs: { type: 'number', default: undefined },\n maxConcurrentGPUs: { type: 'number', default: undefined },\n maxConcurrentEscrow: { type: 'BigNumber', default: undefined },\n },\n};\n\n/**\n * Async function that creates a new Compute Group.\n *\n * The joinDescriptor is of the form { joinKey, joinSecret }, { joinKey, joinHash } or { id, joinAddress }.\n * where id will correspond to the attribute opaqueId in the new row in the computeGroups table.\n *\n * This function can only be called with ADMIN permission.\n * Properties not appearing in newCGPrototype.parameters are not allowed in otherProperties.\n *\n * @param {cgClientJoinType} joinDescriptor - Must have properly defined { joinKey, joinSecret }, { joinKey, joinHash }\n * or { id, joinAddress }, where id will correspond to the attribute opaqueId\n * in the new row in the computeGroups table.\n * @param {string} [name] - The name of the compute group.\n * @param {string} [description] - The description of the compute group.\n * @param {object} [otherProperties] - The 5 attributes of table computeGroup related to commissions and fees.\n * commissionRate: notNull(zFinNum),// commission, see DCP-1889\n * deployFee: notNull(zFinNum),// number of DCC to take for every deployment\n * deployAccess: string, // can be \"owner\"|\"join\" (dcp-1910)\n * addJobFee: notNull(zFinNum),// fee required each time a job joins a compute group\n * maxTotalPayment: finNum, // limit on maximum job payment, NULL => Infinity\n * And the 6 attributes of table computeGroup related to limits.\n * maxConcurrentJobs: integer,\n * maxConcurrentWorkers: integer,\n * maxConcurrentSandboxes: integer,\n * maxConcurrentCPUs: integer,\n * maxConcurrentGPUs: integer,\n * maxConcurrentEscrow: finNum,\n * @returns {Promise<apiClientType>} - { success, payload: computeGroup.id }\n * @access public\n * @example\n * await computeGroup.createGroup({ joinKey: 'dcpDemo', joinSecret: 'theSecret' }, 'myCGName', 'myCGDescription', { deployFee: 0.00015 });\n * await computeGroup.createGroup({ joinKey: 'dcpDemo2', joinHash: 'eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2' });\n * await computeGroup.createGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd', joinAddress: 'c15053fc30d4bdf91e2e0bba79578f8b649e55ea' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: joinKey:='dcpDemo2', joinHash:='eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2'\n * row3: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd', joinAddress:='c15053fc30d4bdf91e2e0bba79578f8b649e55ea' .\n */\nexports.createGroup = async function createGroup(joinDescriptor, name, description, otherProperties)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n validateCGJoinDescriptor(joinDescriptor, 'createGroup');\n\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n if (!hasSufficientJoinInfo(joinDescriptor))\n return clientError(`createGroup: Insufficient information to authorize compute group: ${JSON.stringify(joinDescriptor)}.`);\n\n // Validate the properties in otherProperties.\n for (const methodName in otherProperties) {\n if (!Object.keys(newCGPrototype.parameters).includes(methodName))\n return clientError(`createGroup: Property ${methodName} cannot be speicfied in otherProperties. Can only specify ${JSON.stringify(Object.keys(newCGPrototype.parameters))}`);\n }\n\n // Translate joinSecret to joinHash.\n if (joinDescriptor.joinSecret) {\n joinDescriptor.joinHash = exports.calculateJoinHash(joinDescriptor);\n delete joinDescriptor.joinSecret;\n }\n\n if (otherProperties && (otherProperties.commissionRate < 0 || otherProperties.commissionRate >= 1))\n return clientError(`client-createGroup: commissionRate ${otherProperties.commissionRate} must be between 0 and 1 (0 <= commissionRate < 1).`);\n\n debugging('computeGroups') && console.debug('client-createGroup: input:', joinDescriptor, name, description, otherProperties);\n\n const { success, payload } = await exports.serviceConnection.send('createGroup', { joinDescriptor, name, description, otherProperties });\n\n if (!success) return clientError(`Cannot create new compute group, with ${cgId(joinDescriptor)}.`);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n debugging('computeGroups') && console.debug('client-createGroup: payload', payload);\n\n return payload;\n};\n\nconst changeCGPrototype = { type: 'object',\n parameters: {\n name: { type: 'string', default: undefined }, /* name of group (length <= 255) */\n description: { type: 'string', default: undefined }, /* description of group (length <= 255) */\n joinHash: { type: 'string', default: undefined }, /* basically the password, seeded & hashed (length <= 255) */\n joinAddress: { type: Address, default: undefined }, /* signature gives alternative to login/password */\n\n commissionRate: { type: 'BigNumber', default: undefined }, /* commission, see DCP-1889 */\n deployFee: { type: 'BigNumber', default: undefined }, /* number of DCC to take for every deployment */\n deployAccess: { type: 'string', default: undefined }, /* can be \"owner\"|\"join\" (dcp-1910) */\n addJobFee: { type: 'BigNumber', default: undefined }, /* fee required each time a job joins a compute group */\n maxTotalPayment: { type: 'BigNumber', default: undefined }, /* limit on maximum job payment, NULL => Infinity */\n\n /* Administrative limits on group. NULL => Infinity: Should all be integers or undefined. */\n maxConcurrentJobs: { type: 'number', default: undefined },\n maxConcurrentWorkers: { type: 'number', default: undefined },\n maxConcurrentSandboxes: { type: 'number', default: undefined },\n maxConcurrentCPUs: { type: 'number', default: undefined },\n maxConcurrentGPUs: { type: 'number', default: undefined },\n maxConcurrentEscrow: { type: 'BigNumber', default: undefined },\n },\n};\n\n/**\n * Async function that changes a new Compute Group.\n * \n * The parameter newDescriptor contains the new property values,\n * and the properties that are allowed to be changed appear in changeCGPrototype.parameters.\n * \n * The descriptor must have joinKey or id, where id:=opaqueId.\n * Must own the compute group or be ADMIN to use changeGroup.\n * \n * @param {cgAccessType} descriptor - Must have joinkey or id, where id:=opaqueId.\n * @param {object} newDescriptor - Properties not appearing in changeCGPrototype.parameters are not allowed.\n * @returns {Promise<apiClientType>}\n * await computeGroup.changeGroup({ joinKey: 'dcpDemo' }, { joinSecret: 'myNewPasswrd' });\n * await computeGroup.changeGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' }, { name: 'myNewName', deployFee: 0.0001 });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.changeGroup = async function changeGroup(descriptor, newDescriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`changeGroup: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n // Validate the properties in newDescriptor.\n for (const methodName in newDescriptor) {\n if (!Object.keys(changeCGPrototype.parameters).includes(methodName))\n return clientError(`changeGroup: Not allowed to change property ${methodName}. Can only change ${JSON.stringify(Object.keys(changeCGPrototype.parameters))}`);\n }\n\n // Translate joinSecret to joinHash.\n if (newDescriptor.joinSecret) {\n newDescriptor.joinHash = exports.calculateJoinHash(newDescriptor);\n delete newDescriptor.joinSecret;\n }\n\n descriptor = buildCGMessage(descriptor, 'changeGroup');\n debugging('computeGroups') && console.debug('change compute group client:', descriptor, newDescriptor);\n const { success, payload } = await exports.serviceConnection.send('changeGroup', { descriptor, newDescriptor });\n\n if (!success) throw new DCPError(`Cannot change compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that deletes a compute group.\n * \n * The descriptor must have joinkey or id, where id:=opaqueId.\n * \n * Must either own the group or be ADMIN.\n * If not ADMIN, then the following config must be true:\n * dcpConfig.scheduler.services.computeGroups.usersCanDeleteGroups\n * \n * @param {cgAccessType} descriptor - Must contain joinKey or id (id:=opaqueId) \n * @returns {Promise<apiClientType>}\n * await computeGroup.deleteGroup({ joinKey: 'dcpDemo' });\n * await computeGroup.deleteGroup({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.deleteGroup = async function deleteGroup(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`deleteGroup: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'deleteGroup');\n debugging('computeGroups') && console.debug('delete compute group client:', descriptor);\n const { success, payload } = await exports.serviceConnection.send('deleteGroup', { descriptor });\n\n if (!success) throw new DCPError(`Cannot delete compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that adds a job to a specified compute group. \n * \n * Must be the owner of the job.\n *\n * Useful feedback is provided from this function, as it\n * will make its way back to the application developer, *after* they have made the\n * deployment fee micropayment.\n *\n * On the client side the access model in place is that if you know the (user/password)\n * joinKey+joinSecret/joinKey+joinHash/joinKey+joinHashHash/id+joinAddress,\n * you can add the job to the compute groups, where id:=opaqueId from table computeGroups.\n * On the service side the corresponding access model is\n * joinKey+joinHashHash/id+joinAddress .\n * Access is also allowed if the compute group owner is the connection peerAddress.\n * \n * Unless the compute group owner is the connection peerAddress, element of the descriptor array must contain\n * { joinKey, joinSecret }, { joinKey, joinHash } or { id, joinAddress }\n * where the value of id in { id, joinAddress } is the opaqueId attribute of the row in table computeGroups.\n *\n * @param {Address} job The address of the Job that will be added to the Compute Group.\n * @param {Array} computeGroups Array of descriptor objects for the compute groups. This descriptor\n * needs to contain enough information to authorize access to the\n * compute group. Properties may include:\n * - id (id:=opaqueId)\n * - joinKey\n * - joinSecret\n * - joinHash\n * - joinAddress\n * \n * Additional, either the joinKey or id MUST be specified so\n * that we can identify the compute group in question.\n *\n * All compute groups can have jobs submitted to them, provided either the joinKey\n * or the id are specified, and the message contains valid join permission and the \n * job is owned by the caller of addJobToGroups.\n *\n * FUTURE - after DCP-1910\n * keystore A keystore used to grant access to job deployment within this compute group.\n * This can be either the ownerKeystore or the joinAddress keystore when the\n * compute group is in deployAccessType='join' mode.\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.addJobToGroups('P+Y4IApeFQLrYS2W7MkVg7', \n * [ { joinKey: 'dcpDemo', joinSecret: 'theSecret' },\n * { joinKey: 'dcpDemo2', joinHash: 'eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2' }, \n * { id: 'bYcYGQ3NOpFnP4FKs6IBQd', joinAddress: 'c15053fc30d4bdf91e2e0bba79578f8b649e55ea' } ]);\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo',\n * row2: joinKey:='dcpDemo2', joinHash:='eh1-e063976b20a92da97a27b9873465c6f2c9d6e4370befa86c8c1dd312c78befc2'\n * row3: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd', joinAddress:='c15053fc30d4bdf91e2e0bba79578f8b649e55ea' .\n */\nexports.addJobToGroups = async function addJobToGroups(job, computeGroups)\n{\n // ensure the service connection is hot before calling buildCGJoinMessage:\n await exports.keepAlive();\n\n const cgArray = [];\n for (const joinDescriptor of computeGroups)\n {\n // Verify joinDescriptor has sufficient information to authorize a compute group (not guarenteed).\n if (!hasSufficientJoinInfo(joinDescriptor))\n {\n console.error(`addJobToGroups: Insufficient information to authorize compute group: ${JSON.stringify(joinDescriptor)}.`);\n continue;\n }\n\n // Translate so that neither joinHash nor joinSecret goes across the wire.\n const message = buildCGJoinMessage(joinDescriptor, 'addJobToGroups');\n debugging('computeGroups') && console.debug(`addJobToGroups client: job ${job}, message`, message);\n\n cgArray.push(message);\n }\n\n const { success, payload } = await exports.serviceConnection.send('addJobToGroups', { job, cgArray });\n\n debugging('computeGroups') && console.debug('addJobToGroups payload', payload);\n\n if (!success) throw new DCPError(`Cannot add job ${job} to compute groups.`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n // If the server reported success but did not return a list of CGs (eg. v.4.2.5 server),\n // assume (and inform the client) we added all groups successfully\n return payload || computeGroups;\n};\n\n/**\n * Async function that lists all the Jobs in a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to list jobs from it.\n * The job does not need to be owned.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {cgAccessType} descriptor - Must have one of the properties joinKey, id (id:=opaqueId). Specifically\n * descriptor = { joinKey: 'dcpDemo' } or descriptor = { id: opaqueId }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * let listOfJobs1 = await computeGroup.listJobs({ joinKey: 'dcpDemo' });\n * let listOfJobs2 = await computeGroup.listJobs({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.listJobs = async function listJobs(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`listJobs: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'listJobs');\n debugging('computeGroups') && console.debug('listJob client: descriptor', descriptor);\n const { success, payload } = await exports.serviceConnection.send('listJobs', { descriptor });\n\n if (!success) throw new DCPError(`Cannot list jobs for compute group with ${cgId(descriptor)}`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that removes a job from a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to remove a job from it.\n * The job does not need to be owned.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {Address} job - The address of the Job that will be added to the Compute Group.\n * @param {cgAccessType} descriptor - { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.removeJob( 'P+Y4IApeFQLrYS2W7MkVg7', { joinKey: 'dcpDemo' });\n * await computeGroup.removeJob( 'P+Y4IApeFQLrYS2W7MkVg7', { id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.removeJob = async function removeJob(job, descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`removeJob: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'removeJob');\n debugging('computeGroups') && console.debug(`removeJob client: job ${job}, descriptor`, descriptor);\n const { success, payload } = await exports.serviceConnection.send('removeJob', { job, descriptor });\n\n if (!success) throw new DCPError(`Cannot remove job ${job} from compute group with ${cgId(descriptor)}`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Async function that removes all jobs from a Compute Group.\n * \n * The descriptor must have one of the properties joinkey, id (id:=opaqueId).\n * Must be the owner of the Compute Group to remove jobs from it.\n * \n * The descriptor is of the form { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }.\n * where 'bYcYGQ3NOpFnP4FKs6IBQd' is the opaqueId of the Compute Group.\n *\n * @param {cgAccessType} descriptor - { joinKey: 'dcpDemo' } or { id: 'bYcYGQ3NOpFnP4FKs6IBQd' }\n * @returns {Promise<apiClientType>}\n * @access public\n * @example\n * await computeGroup.removeAllJobs({ joinKey: 'dcpDemo' });\n * await computeGroup.removeAllJobs({ id: 'bYcYGQ3NOpFnP4FKs6IBQd' });\n * where the corresponding rows in table computeGroups have attributes\n * row1: joinKey:='dcpDemo'\n * row2: opaqueId:='bYcYGQ3NOpFnP4FKs6IBQd'\n */\nexports.removeAllJobs = async function removeAllJobs(descriptor)\n{\n if (!exports.serviceConnection)\n await openAndConnectServiceConn();\n\n // Verify descriptor has sufficient information to access a compute group (not guarenteed).\n if (!descriptor.joinKey && !descriptor.id)\n return clientError(`removeAllJobs: Insufficient information to identify compute group: ${JSON.stringify(descriptor)}.`);\n\n descriptor = buildCGMessage(descriptor, 'removeAllJobs');\n debugging('computeGroups') && console.debug('removeAllJobs client: descriptor', descriptor);\n const { success, payload } = await exports.serviceConnection.send('removeAllJobs', { descriptor });\n\n if (!success) throw new DCPError(`Cannot remove all jobs from compute group with ${cgId(descriptor)}:`, payload);\n if (payload && !payload.success) return reconstructServiceError(payload);\n\n return payload;\n};\n\n/**\n * Calculate a joinHash for a compute group. This is an eh1- hash of the cg salt and \n * joinSecret components of a compute group description.\n *\n * @param {object} details an object containing the cg salt, which is\n * the joinKey if the compute group uses one;\n * otherwise it is the joinAddress. This object\n * may also contain the joinSecret.\n * @param {string} [joinSecret] the join secret -- plain text -- that is\n * the \"password\" for the compute group. If not\n * specified, we use details.joinSecret.\n */\nexports.calculateJoinHash = function computeGroups$calculateJoinHash(details, joinSecret)\n{\n if (typeof joinSecret === 'undefined')\n joinSecret = details.joinSecret;\n\n return hash.calculate(hash.eh1, `${details.joinKey || details.joinAddress} ${joinSecret}`);\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/compute-groups/index.js?");
4190
4234
 
4191
4235
  /***/ }),
4192
4236
 
@@ -4206,7 +4250,7 @@ eval("/**\n * @file Module that implements Compute API\n * @module dcp/comput
4206
4250
  \*********************************/
4207
4251
  /***/ ((module, exports, __webpack_require__) => {
4208
4252
 
4209
- eval("/* module decorator */ module = __webpack_require__.nmd(module);\n/**\n * @file dcp-client-bundle-src.js\n * Top-level file which gets webpacked into the bundle consumed by dcp-client 2.5\n * @author Wes Garland, wes@kingsds.network\n * @date July 2019\n */\n\n{\n let thisScript = typeof document !== 'undefined' ? (typeof document.currentScript !== 'undefined' && document.currentScript) || document.getElementById('_dcp_client_bundle') : {}\n let realModuleDeclare\n\n if ( false || typeof module.declare === 'undefined') {\n realModuleDeclare = ( true) ? module.declare : 0\n if (false) {}\n module.declare = function moduleUnWrapper (deps, factory) {\n factory(null, module.exports, module)\n return module.exports\n }\n }\n\n let _debugging = () => false\n dcpConfig.future = (__webpack_require__(/*! ../common/config-future.js */ \"./src/common/config-future.js\").futureFactory)(_debugging, dcpConfig);\n\n /* These modules are official API and must be part of DCP Client */\n let officialApi = {\n 'protocol': __webpack_require__(/*! ../protocol-v4 */ \"./src/protocol-v4/index.js\"),\n 'compute': (__webpack_require__(/*! ./compute */ \"./src/dcp-client/compute.js\").compute),\n 'worker': __webpack_require__(/*! ./worker */ \"./src/dcp-client/worker/index.js\"),\n 'wallet': __webpack_require__(/*! ./wallet */ \"./src/dcp-client/wallet/index.js\"),\n };\n\n /* Allow client programs to use modules which happen to be in the bundle anyhow */\n let conveniencePeers = {\n 'ethereumjs-wallet': (__webpack_require__(/*! ./wallet/keystore */ \"./src/dcp-client/wallet/keystore.js\")._internalEth.wallet),\n 'ethereumjs-util': (__webpack_require__(/*! ./wallet/keystore */ \"./src/dcp-client/wallet/keystore.js\")._internalEth.util),\n 'socket.io-client': __webpack_require__(/*! @kingsds/socket.io-client */ \"./node_modules/@kingsds/socket.io-client/build/cjs/index.js\"),\n '@kingsds/socket.io-client': __webpack_require__(/*! @kingsds/socket.io-client */ \"./node_modules/@kingsds/socket.io-client/build/cjs/index.js\"),\n 'bignumber.js': __webpack_require__(/*! bignumber.js */ \"./node_modules/bignumber.js/bignumber.js\"),\n 'semver': __webpack_require__(/*! semver */ \"./node_modules/semver/semver.js\"),\n };\n\n /* Some of these modules are API-track. Some of them need to be published to be\n * available for top-level resolution by DCP internals. Those (mostly) should have\n * been written using relative module paths.....\n */\n let modules = Object.assign({\n 'dcp-build': {\"version\":\"0bf54dbe88ee11e84b28e62b73cbd154d06967ea\",\"branch\":\"release\",\"dcpClient\":{\"version\":\"4.2.23\",\"from\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#prod-20221207\",\"resolved\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#0e40d23d5bd715d4f2ae2c79cc52981fa0a0408e\"},\"built\":\"Thu Dec 08 2022 15:53:38 GMT-0500 (Eastern Standard Time)\",\"config\":{\"generated\":\"Thu 08 Dec 2022 03:53:36 PM EST by erose on lorge\",\"build\":\"debug\"},\"webpack\":\"5.74.0\",\"node\":\"v14.21.1\"},\n 'dcp-xhr': __webpack_require__(/*! ../common/dcp-xhr */ \"./src/common/dcp-xhr.js\"),\n 'dcp-env': __webpack_require__(/*! ../common/dcp-env */ \"./src/common/dcp-env.js\"),\n 'dcp-url': __webpack_require__(/*! ../common/dcp-url */ \"./src/common/dcp-url.js\"),\n 'cli': __webpack_require__(/*! ../common/cli */ \"./src/common/cli.js\"),\n 'dcp-timers': __webpack_require__(/*! ../common/dcp-timers */ \"./src/common/dcp-timers.js\"),\n 'dcp-dot-dir': __webpack_require__(/*! ../common/dcp-dot-dir */ \"./src/common/dcp-dot-dir.js\"),\n 'dcp-assert': __webpack_require__(/*! ../common/dcp-assert */ \"./src/common/dcp-assert.js\"),\n 'dcp-events': __webpack_require__(/*! ../common/dcp-events */ \"./src/common/dcp-events/index.js\"),\n 'utils': __webpack_require__(/*! ../utils */ \"./src/utils/index.js\"),\n 'debugging': __webpack_require__(/*! ../debugging */ \"./src/debugging.js\"),\n 'publish': __webpack_require__(/*! ../common/dcp-publish */ \"./src/common/dcp-publish.js\"),\n 'compute-groups': {\n ...__webpack_require__(/*! ./compute-groups */ \"./src/dcp-client/compute-groups/index.js\"),\n publicGroupOpaqueId: (__webpack_require__(/*! ../common/scheduler-constants */ \"./src/common/scheduler-constants.js\").computeGroups[\"public\"].opaqueId),\n },\n 'bank-util': __webpack_require__(/*! ./bank-util */ \"./src/dcp-client/bank-util.js\"),\n 'protocol-v4': __webpack_require__(/*! ../protocol-v4 */ \"./src/protocol-v4/index.js\"), /* deprecated */\n 'client-modal': __webpack_require__(/*! ./client-modal */ \"./src/dcp-client/client-modal/index.js\"),\n 'legacy-modal': (__webpack_require__(/*! ../../portal/www/js/modal */ \"./portal/www/js/modal.js\").Modal),\n 'eth': __webpack_require__(/*! ./wallet/eth */ \"./src/dcp-client/wallet/eth.js\"),\n 'serialize': __webpack_require__(/*! ../utils/serialize */ \"./src/utils/serialize.js\"),\n 'job': __webpack_require__(/*! ./job */ \"./src/dcp-client/job/index.js\"),\n 'range-object': __webpack_require__(/*! ./range-object */ \"./src/dcp-client/range-object.js\"),\n 'stats-ranges': __webpack_require__(/*! ./stats-ranges */ \"./src/dcp-client/stats-ranges.js\"),\n 'job-values': __webpack_require__(/*! ./job-values */ \"./src/dcp-client/job-values.js\"),\n 'signal-handler': __webpack_require__(/*! ../node-libs/signal-handler */ \"./src/node-libs/signal-handler.js\"),\n 'standard-objects': {}\n }, conveniencePeers, officialApi);\n\n /* Export the JS Standard Classes (etc) from the global object of the bundle evaluation context,\n * in case we have code somewhere that needs to use these for instanceof checks.\n */\n ;[ Object, Function, Boolean, Symbol,\n Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError,\n Number, Math, Date,\n String, RegExp,\n Array, Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array,\n Map, Set, WeakMap, WeakSet,\n ArrayBuffer, DataView, JSON,\n Promise, \n Reflect, Proxy, Intl, WebAssembly, __webpack_require__\n ].forEach(function (obj) {\n if (obj.name && (typeof obj === 'function' || typeof obj === 'object'))\n modules['standard-objects'][obj.name] = obj\n })\n\n if (typeof BigInt !== 'undefined')\n modules['standard-objects']['BigInt'] === BigInt;\n if (typeof BigInt64Array !== 'undefined')\n modules['standard-objects']['BigInt64Array'] === BigInt64Array;\n if (typeof BigInt64Array !== 'undefined')\n modules['standard-objects']['BigUint64Array'] === BigUint64Array;\n\n module.declare([], function(require, exports, module) {\n Object.assign(exports, modules)\n exports['dcp-config'] = dcpConfig\n })\n if (realModuleDeclare)\n module.declare = realModuleDeclare\n\n bundleExports = thisScript.exports = exports; /* must be last expression evaluated! */\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/index.js?");
4253
+ eval("/* module decorator */ module = __webpack_require__.nmd(module);\n/**\n * @file dcp-client-bundle-src.js\n * Top-level file which gets webpacked into the bundle consumed by dcp-client 2.5\n * @author Wes Garland, wes@kingsds.network\n * @date July 2019\n */\n\n{\n let thisScript = typeof document !== 'undefined' ? (typeof document.currentScript !== 'undefined' && document.currentScript) || document.getElementById('_dcp_client_bundle') : {}\n let realModuleDeclare\n\n if ( false || typeof module.declare === 'undefined') {\n realModuleDeclare = ( true) ? module.declare : 0\n if (false) {}\n module.declare = function moduleUnWrapper (deps, factory) {\n factory(null, module.exports, module)\n return module.exports\n }\n }\n\n let _debugging = () => false\n dcpConfig.future = (__webpack_require__(/*! ../common/config-future.js */ \"./src/common/config-future.js\").futureFactory)(_debugging, dcpConfig);\n\n /* These modules are official API and must be part of DCP Client */\n let officialApi = {\n 'protocol': __webpack_require__(/*! ../protocol-v4 */ \"./src/protocol-v4/index.js\"),\n 'compute': (__webpack_require__(/*! ./compute */ \"./src/dcp-client/compute.js\").compute),\n 'worker': __webpack_require__(/*! ./worker */ \"./src/dcp-client/worker/index.js\"),\n 'wallet': __webpack_require__(/*! ./wallet */ \"./src/dcp-client/wallet/index.js\"),\n };\n\n /* Allow client programs to use modules which happen to be in the bundle anyhow */\n let conveniencePeers = {\n 'ethereumjs-wallet': (__webpack_require__(/*! ./wallet/keystore */ \"./src/dcp-client/wallet/keystore.js\")._internalEth.wallet),\n 'ethereumjs-util': (__webpack_require__(/*! ./wallet/keystore */ \"./src/dcp-client/wallet/keystore.js\")._internalEth.util),\n 'socket.io-client': __webpack_require__(/*! @kingsds/socket.io-client */ \"./node_modules/@kingsds/socket.io-client/build/cjs/index.js\"),\n '@kingsds/socket.io-client': __webpack_require__(/*! @kingsds/socket.io-client */ \"./node_modules/@kingsds/socket.io-client/build/cjs/index.js\"),\n 'bignumber.js': __webpack_require__(/*! bignumber.js */ \"./node_modules/bignumber.js/bignumber.js\"),\n 'semver': __webpack_require__(/*! semver */ \"./node_modules/semver/semver.js\"),\n };\n\n /* Some of these modules are API-track. Some of them need to be published to be\n * available for top-level resolution by DCP internals. Those (mostly) should have\n * been written using relative module paths.....\n */\n let modules = Object.assign({\n 'dcp-build': {\"version\":\"5895f3465816246196dbdb9c0cd5aa200a943c59\",\"branch\":\"release\",\"dcpClient\":{\"version\":\"4.2.24\",\"from\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#prod-20230111\",\"resolved\":\"git+ssh://git@gitlab.com/Distributed-Compute-Protocol/dcp-client.git#0b4bbf4f5e91b57e19c5bbb66c8160479dca32cd\"},\"built\":\"Mon Jan 23 2023 14:29:03 GMT-0500 (Eastern Standard Time)\",\"config\":{\"generated\":\"Mon 23 Jan 2023 02:29:01 PM EST by erose on lorge\",\"build\":\"debug\"},\"webpack\":\"5.74.0\",\"node\":\"v14.21.2\"},\n 'dcp-xhr': __webpack_require__(/*! ../common/dcp-xhr */ \"./src/common/dcp-xhr.js\"),\n 'dcp-env': __webpack_require__(/*! ../common/dcp-env */ \"./src/common/dcp-env.js\"),\n 'dcp-url': __webpack_require__(/*! ../common/dcp-url */ \"./src/common/dcp-url.js\"),\n 'cli': __webpack_require__(/*! ../common/cli */ \"./src/common/cli.js\"),\n 'dcp-timers': __webpack_require__(/*! ../common/dcp-timers */ \"./src/common/dcp-timers.js\"),\n 'dcp-dot-dir': __webpack_require__(/*! ../common/dcp-dot-dir */ \"./src/common/dcp-dot-dir.js\"),\n 'dcp-assert': __webpack_require__(/*! ../common/dcp-assert */ \"./src/common/dcp-assert.js\"),\n 'dcp-events': __webpack_require__(/*! ../common/dcp-events */ \"./src/common/dcp-events/index.js\"),\n 'utils': __webpack_require__(/*! ../utils */ \"./src/utils/index.js\"),\n 'debugging': __webpack_require__(/*! ../debugging */ \"./src/debugging.js\"),\n 'publish': __webpack_require__(/*! ../common/dcp-publish */ \"./src/common/dcp-publish.js\"),\n 'compute-groups': {\n ...__webpack_require__(/*! ./compute-groups */ \"./src/dcp-client/compute-groups/index.js\"),\n publicGroupOpaqueId: (__webpack_require__(/*! ../common/scheduler-constants */ \"./src/common/scheduler-constants.js\").computeGroups[\"public\"].opaqueId),\n },\n 'bank-util': __webpack_require__(/*! ./bank-util */ \"./src/dcp-client/bank-util.js\"),\n 'protocol-v4': __webpack_require__(/*! ../protocol-v4 */ \"./src/protocol-v4/index.js\"), /* deprecated */\n 'client-modal': __webpack_require__(/*! ./client-modal */ \"./src/dcp-client/client-modal/index.js\"),\n 'legacy-modal': (__webpack_require__(/*! ../../portal/www/js/modal */ \"./portal/www/js/modal.js\").Modal),\n 'eth': __webpack_require__(/*! ./wallet/eth */ \"./src/dcp-client/wallet/eth.js\"),\n 'serialize': __webpack_require__(/*! ../utils/serialize */ \"./src/utils/serialize.js\"),\n 'job': __webpack_require__(/*! ./job */ \"./src/dcp-client/job/index.js\"),\n 'range-object': __webpack_require__(/*! ./range-object */ \"./src/dcp-client/range-object.js\"),\n 'stats-ranges': __webpack_require__(/*! ./stats-ranges */ \"./src/dcp-client/stats-ranges.js\"),\n 'job-values': __webpack_require__(/*! ./job-values */ \"./src/dcp-client/job-values.js\"),\n 'signal-handler': __webpack_require__(/*! ../node-libs/signal-handler */ \"./src/node-libs/signal-handler.js\"),\n 'standard-objects': {}\n }, conveniencePeers, officialApi);\n\n /* Export the JS Standard Classes (etc) from the global object of the bundle evaluation context,\n * in case we have code somewhere that needs to use these for instanceof checks.\n */\n ;[ Object, Function, Boolean, Symbol,\n Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError,\n Number, Math, Date,\n String, RegExp,\n Array, Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array,\n Map, Set, WeakMap, WeakSet,\n ArrayBuffer, DataView, JSON,\n Promise, \n Reflect, Proxy, Intl, WebAssembly, __webpack_require__\n ].forEach(function (obj) {\n if (obj.name && (typeof obj === 'function' || typeof obj === 'object'))\n modules['standard-objects'][obj.name] = obj\n })\n\n if (typeof BigInt !== 'undefined')\n modules['standard-objects']['BigInt'] === BigInt;\n if (typeof BigInt64Array !== 'undefined')\n modules['standard-objects']['BigInt64Array'] === BigInt64Array;\n if (typeof BigInt64Array !== 'undefined')\n modules['standard-objects']['BigUint64Array'] === BigUint64Array;\n\n module.declare([], function(require, exports, module) {\n Object.assign(exports, modules)\n exports['dcp-config'] = dcpConfig\n })\n if (realModuleDeclare)\n module.declare = realModuleDeclare\n\n bundleExports = thisScript.exports = exports; /* must be last expression evaluated! */\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/index.js?");
4210
4254
 
4211
4255
  /***/ }),
4212
4256
 
@@ -4227,7 +4271,7 @@ eval("/**\n * @file job-values.js\n * Utility code related t
4227
4271
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4228
4272
 
4229
4273
  "use strict";
4230
- eval("/**\n * @file job/index.js\n * @author Eddie Roosenmaallen, eddie@kingsds.network\n * Matthew Palma, mpalma@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * Ryan Saweczko, ryansaweczko@kingsds.network\n * @date November 2018\n * November 2018\n * February 2022\n * May 2022\n * Jun 2022\n *\n * This module implements the Compute API's Job Handle\n *\n */\n\n\nconst { BigNumber } = __webpack_require__(/*! bignumber.js */ \"./node_modules/bignumber.js/bignumber.js\");\nconst { v4: uuidv4 } = __webpack_require__(/*! uuid */ \"./node_modules/uuid/dist/esm-browser/index.js\");\nconst { EventEmitter, PropagatingEventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { RangeObject, MultiRangeObject, DistributionRange, SuperRangeObject, SparseRangeObject } = __webpack_require__(/*! dcp/dcp-client/range-object */ \"./src/dcp-client/range-object.js\");\nconst { fetchURI, encodeDataURI, createTempFile } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { serializeJobValue, RemoteValue } = __webpack_require__(/*! dcp/dcp-client/job-values */ \"./src/dcp-client/job-values.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst { DcpURL } = __webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\");\nconst ClientModal = __webpack_require__(/*! dcp/dcp-client/client-modal */ \"./src/dcp-client/client-modal/index.js\");\nconst { Worker } = __webpack_require__(/*! dcp/dcp-client/worker */ \"./src/dcp-client/worker/index.js\");\nconst { RemoteDataSet } = __webpack_require__(/*! dcp/dcp-client/remote-data-set */ \"./src/dcp-client/remote-data-set.js\");\nconst { RemoteDataPattern } = __webpack_require__(/*! dcp/dcp-client/remote-data-pattern */ \"./src/dcp-client/remote-data-pattern.js\");\nconst { ResultHandle } = __webpack_require__(/*! ./result-handle */ \"./src/dcp-client/job/result-handle.js\");\nconst { SlicePaymentOffer } = __webpack_require__(/*! ./slice-payment-offer */ \"./src/dcp-client/job/slice-payment-offer.js\");\nconst { addSlices } = __webpack_require__(/*! ./upload-slices */ \"./src/dcp-client/job/upload-slices.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst computeGroups = __webpack_require__(/*! dcp/dcp-client/compute-groups */ \"./src/dcp-client/compute-groups/index.js\");\nconst schedulerConstants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { DEFAULT_REQUIREMENTS, removeBadRequirements } = __webpack_require__(/*! dcp/common/job-requirements-defaults */ \"./src/common/job-requirements-defaults.js\");\nconst { sliceStatus, jobValueKind } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { jobStatus } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst bankUtil = __webpack_require__(/*! dcp/dcp-client/bank-util */ \"./src/dcp-client/bank-util.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('dcp-client');\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nlet tunedKvin;\n\nconst log = (...args) => {\n if (debugging('job')) {\n console.debug('dcp-client:job', ...args);\n }\n};\n\nconst ON_BROWSER = DCP_ENV.isBrowserPlatform;\nconst sideloaderModuleIdentifier = 'sideloader-v1';\n\n\n/** @typedef {import('dcp/dcp-client/wallet/keystore').Keystore} Keystore */\n/** @typedef {import('dcp/dcp-client/range-object').RangeLike} RangeLike */\n/** @typedef {import('scheduler-v4/libexec/job-submit/operations/submit').MarshaledInputData} MarshaledInputData */\n\n/**\n * Ensure input data is an appropriate format\n * @param {RangeObject | DistributionRange | RemoteDataSet | Array | Iterable}\n * inputData - A URI-shaped string, a [Multi]RangeObject-constructing value, or\n * an array of slice data\n * @return {RangeObject | RangeLike | DistributionRange | RemoteDataSet | Array}\n * The coerced input in an appropriate format ([Multi]RangeObject,\n * DistributionRange, RemoteDataSet, or array)\n */\n const wrangleData = (inputData) => {\n\n if (RangeObject.isRangelike(inputData)) { return inputData }\n if (RangeObject.isRangeObject(inputData)) { return inputData }\n if (DistributionRange.isDistribution(inputData)) { return inputData }\n if (inputData instanceof SparseRangeObject) { return inputData }\n if (inputData instanceof MultiRangeObject) { return inputData }\n if (MultiRangeObject.isProtoMultiRangelike(inputData)) { return new MultiRangeObject(inputData) }\n if (RangeObject.isProtoRangelike(inputData)) { return new RangeObject(inputData) }\n if (DistributionRange.isProtoDistribution(inputData)) { return new DistributionRange(inputData) }\n if (RemoteDataSet.isRemoteDataSet(inputData)) { return inputData }\n if (RemoteDataPattern.isRemoteDataPattern(inputData)) { return inputData }\n\n return Array.isArray(inputData) ? inputData : [inputData];\n};\n\n/**\n * @classdesc The Compute API's Job Handle (see {@link https://docs.dcp.dev/specs/compute-api.html#job-handles|Compute API spec})\n * Job handles are objects which correspond to jobs. \n * They are created by some exports of the compute module, such as {@link module:dcp/compute.do|compute.do} and {@link module:dcp/compute.for|compute.for}.\n * @extends module:dcp/dcp-events.PropagatingEventEmitter\n * @hideconstructor\n * @access public\n */\nclass Job extends PropagatingEventEmitter\n{\n /**\n * Fired when the job is accepted by the scheduler on deploy.\n * \n * @event Job#accepted\n * @access public\n * @type {object}\n *//**\n * Fired when the job is cancelled.\n * \n * @event Job#cancel\n * @access public\n *//**\n * Fired when a result is returned.\n * \n * @event Job#result\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {string} task ID of the task (slice) the result came from\n * @property {number} sort The index of the slice\n * @property {object} result\n * @property {string} result.request\n * @property {*} result.result The value returned from the work function\n *//**\n * Fired when the result handle is modified, either when a new `result` event is fired or when the results are populated with `results.fetch()`\n * \n * @event Job#resultsUpdated\n * @access public\n *//**\n * Fired when the job has been completed.\n * \n * @event Job#complete\n * @access public\n * @type {ResultHandle}\n *//**\n * Fired when the job's status changes.\n * \n * @event Job#status\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} total Total number of slices in the job\n * @property {number} distributed Number of slices that have been distributed\n * @property {number} computed Number of slices that have completed execution (returned a result)\n * @property {string} runStatus Current runStatus of the job\n *//**\n * Fired when a slice throws an error.\n * \n * @event Job#error\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex Index of the slice that threw the error\n * @property {string} message The error message\n * @property {string} stack The error stacktrace\n * @property {string} name The error type name\n *//**\n * Fired when a slice uses one of the console log functions.\n * \n * @event Job#console\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex The index of the slice that produced this event\n * @property {string} level The log level, one of `debug`, `info`, `log`, `warn`, or `error`\n * @property {string} message The console log message\n *//**\n * Fired when a slice is stopped for not calling progress. Contains information about how long the slice ran for, and about the last reported progress calls.\n * \n * @event Job#noProgress\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex The index of the slice that failed due to no progress\n * @property {number} timestamp How long the slice ran before failing\n * @property {object} progressReports\n * @property {object} progressReports.last The last progress report received from the worker\n * @property {number} progressReports.last.timestamp Time since the start of the slice\n * @property {number} progressReports.last.progress Progress value reported\n * @property {*} progressReports.last.value The last value that was passed to the progress function\n * @property {number} progressReports.last.throttledReports Number of calls to progress that were throttled since the last report\n * @property {object} progressReports.lastUpdate The last determinate (update to the progress param) progress report received from the worker\n * @property {number} progressReports.lastUpdate.timestamp\n * @property {number} progressReports.lastUpdate.progress\n * @property {*} progressReports.lastUpdate.value\n * @property {number} progressReports.lastUpdate.throttledReports\n *//**\n @todo: is this in the spec? is there a no progress data? should there be?\n * Identical to `noProgress`, except that it also contains the data that the slice was executed with.\n * \n * @event Job#noProgressData\n * @access public\n * @type {object}\n * @property {*} data The data that the slice was executed with\n *//**\n * Fired when the job is paused due to running out of funds. The job can be resumed by escrowing more funds then resuming the job.\n * @todo: is this a thing, should it be a thing (the payload)\n * Event payload is the estimated funds required to complete the job\n * \n * @event Job#nofunds\n * @access public\n * @type {BigNumber}\n *//**\n * Fired when the job cannot be deployed due to no bank account / not enough balance to deploy the job\n * \n * @event Job#ENOFUNDS\n * @access public\n *//**\n * Fired when the job is cancelled due to the work function not calling the `progress` method frequently enough.\n * \n * @event Job#ENOPROGRESS\n * @access public\n *//**\n * The job was cancelled because scheduler has determined that individual tasks in this job exceed the maximum allowable execution time.\n * \n * @event Job#ESLICETOOSLOW\n * @access public\n *//**\n * Fired when the job is cancelled because too many work functions are terminating with uncaught exceptions.\n * \n * @event Job#ETOOMANYERRORS\n * @access public\n */\n\n /**\n * @form1 new Job('application_worker_address'[, data[, arguments]])\n * @form2a new Job('worker source'[, data[, arguments]])\n * @form2b new Job(worker_function[, data[, arguments]])\n */\n constructor ()\n {\n super('Job');\n if (typeof arguments[0] === 'function')\n arguments[0] = arguments[0].toString();\n\n if (typeof arguments[0] === 'string')\n {\n const { encodeDataURI } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n this.workFunctionURI = encodeDataURI(arguments[0], 'application/javascript');\n } \n else if (DcpURL.isURL(arguments[0]))\n this.workFunctionURI = arguments[0].href;\n\n this.jobInputData = wrangleData(arguments[1] || []);\n this.jobArguments = wrangleData(arguments[2] || []);\n \n log('num wrangledInputData:', this.jobInputData.length);\n log('num wrangledArguments:', this.jobArguments.length);\n\n this.initEventSystems();\n\n /**\n * An object describing the cost the user believes each the average slice will incur, in terms of CPU/GPU and I/O.\n * If defined, this object is used to provide initial scheduling hints and to calculate escrow amounts.\n *\n * @type {object}\n * @access public\n */\n this.initialSliceProfile = undefined;\n \n // The max value that the client is willing to spend to deploy\n // (list on the scheduler, doesn't include compute payment)\n // maxDeployPayment is the max the user is willing to pay to DCP (as a\n // Hold), in addition to the per-slice offer and associated scrape.\n // Currently calculated as `deployCost = costPerKB *\n // (JSON.stringify(job).length / 1024) // 1e-9 per kb`\n // @todo: figure this out / er nov 2018\n this.maxDeployPayment = 1;\n\n /**\n * An object describing the requirements that workers must have to be eligible for this job. The default values are set in the job-submitter, and only the client specified\n * requirements are sent over the wire. See {@link https://docs.dcp.dev/specs/compute-api.html#requirements-objects|Requirements Objects}.\n *\n * @type {object}\n * @access public\n */\n this.requirements = {};\n\n /**\n * A place to store public-facing attributes of the job. Anything stored on this object will be available inside the work \n * function (see {@link module:dcp/compute~sandboxEnv.work}). The properties documented here may be used by workers to display what jobs are currently being \n * worked on.\n * @access public\n * @property {string} name Public-facing name of this job.\n * @property {string} description Public-facing description for this job.\n * @property {string} link Public-facing link to external resource about this job.\n */\n this.public = {\n name: null,\n description: null,\n link: null,\n };\n \n /**\n * A cryptographic receipt indicating deployment of the job on the scheduler\n * @type {object}\n * @access public\n */\n this.receipt = null;\n \n /**\n * a SliceProfile object which contains the average costs for the slices which have been computed to date.\n * Until the first result is returned, this property is undefined.\n * @type {object}\n * @access public\n */\n this.meanSliceProfile = null;\n \n /**\n * A number (can be null, undefined, or infinity) describing the estimationSlicesRemaining in the jpd (dcp-2593)\n * @type {number}\n * @access public\n */\n this.estimationSlices = undefined;\n /**\n * When true, allows a job in estimation to have requestTask return multiple estimation slices.\n * This flag applies independent of infinite estimation, viz., this.estimationSlices === null .\n * @type {boolean}\n * @access public\n */\n this.greedyEstimation = false;\n /**\n * tunable parameters per job\n * @access public\n * @param {object} tuning \n * @param {string} tuning.kvin Encode the TypedArray into a string, trying multiple methods to determine optimum \n * size/performance. The this.tune variable affects the behavior of this code this:\n * @param {boolean} speed If true, only do naive encoding: floats get represented as byte-per-digit strings\n * @param {boolean} size If true, try the naive, ab8, and ab16 encodings; pick the smallest\n * If both are false try the naive encoding if under typedArrayPackThreshold and use if smaller\n * than ab8; otherwise, use ab8\n */\n this.tuning = {\n kvin: {\n size: false,\n speed: false,\n },\n }\n /* For API interface to end-users only */\n Object.defineProperty(this, 'id', {\n get: () => this.address,\n set: (id) => { this.address = id }\n });\n \n this.uuid = uuidv4(); /** @see {@link https://kingsds.atlassian.net/browse/DCP-1475?atlOrigin=eyJpIjoiNzg3NmEzOWE0OWI4NGZkNmI5NjU0MWNmZGY2OTYzZDUiLCJwIjoiaiJ9|Jira Issue} */\n this.dependencies = []; /* dependencies of the work function */\n this.requirePath = []; /* require path for dependencies */\n this.modulePath = []; /* path to module that invoked .exec() for job */\n this.connected = false; /* true when exec or resume called */\n this.results = new ResultHandle(this); /* result handle */\n this.collateResults = true; /* option to receive results as they are computed & ensure all are received on finish */\n this.contextId = null; /* optional string which is used to indicate to caching mechanisms different keystores with same name */ \n this.force100pctCPUDensity = false; /* tell scheduler to assume this job uses 100% cpu density */\n this.workerConsole = false; /* tell workers to log more information about failures in the evaluator this job causes */\n this.address = null; /* job address, created by scheduler during exec call. */\n this.paymentAccountKeystore = null; /* keystore for payment for job to come from */\n this.status = { /* job status details */\n runStatus: null,\n total: null,\n distributed: null,\n computed: null\n };\n \n // service locations\n this.scheduler = dcpConfig.scheduler.services.jobSubmit.location;\n this.bank = dcpConfig.bank.services.bankTeller.location;\n \n // Compute groups. Add to public compute group by default\n this.computeGroups = [ Object.assign({}, schedulerConstants.computeGroups.public) ];\n \n // Update the ready state as we go through job deployment\n this.readyState = sliceStatus.new;\n const that = this;\n this.readyStateChange = function job$$readyStateChange (readyState)\n {\n that.readyState = readyState;\n that.emit('readyStateChange', that.readyState);\n }\n }\n \n /**\n * Initialize the various event systems the job handle requires. These include:\n * - an internal event emitter (this.ee)\n * - an event emitter for any events emitted on `work.emit` within work functions (this.work)\n * - an event subscriber to subscribe (to receive) events from the scheduler (this.eventSubscriber)\n */\n initEventSystems ()\n {\n // Handle the various event-related things required in the constructor\n\n // Internal event emitter for events within job handle\n this.ee = new EventEmitter('Job Internal');\n\n /**\n * An EventEmitter for custom events dispatched by the work function.\n * @type {module:dcp/dcp-events.EventEmitter}\n * @access public\n * @example\n * // in work function\n * work.emit('myEventName', 1, [2], \"three\");\n * // client-side\n * job.work.on('myEventName', (num, arr, string) => { });\n */\n this.work = new EventEmitter('job.work');\n this.listenForCustomEvents = false;\n\n // Initialize the eventSubscriber so each job has unique eventSubscriber\n this.eventSubscriber = new ((__webpack_require__(/*! dcp/events/event-subscriber */ \"./src/events/event-subscriber.js\").EventSubscriber))(this);\n\n // Some events from the event subscriber can't be emitted immediately upon receipt without having \n // weird/wrong output due to things like serialization. We allow interceptors in the event subscriber\n // to handle this.\n const that = this\n var lastConsoleEv;\n var sameCounter = 1;\n const parseConsole = function deserializeConsoleMessage(ev) {\n if (tunedKvin)\n ev.message = tunedKvin.unmarshal(ev.message);\n else \n ev.message = kvin.unmarshal(ev.message);\n \n if (lastConsoleEv && ev.message[0] === lastConsoleEv.message[0] && ev.sliceNumber === lastConsoleEv.sliceNumber && ev.level === lastConsoleEv.level)\n ev.same = ++sameCounter;\n else\n sameCounter = 1;\n lastConsoleEv = ev;\n \n /* if we have the same message being logged (same sliceNumber, message, log level), the console event object will have the sole property same, nothing else */\n if (ev.same > 1)\n that.emit('console', { same: ev.same });\n else\n {\n delete ev.same;\n that.emit('console', ev);\n }\n }\n\n this.eventIntercepts = {\n result: (ev) => this.handleResult(ev),\n status: (ev) => this.handleStatus(ev),\n cancel: (ev) => this.ee.emit('stopped', ev),\n custom: (ev) => this.work.emit(ev.customEvent, ev),\n console: parseConsole,\n };\n \n this.eventTypes = (__webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\").eventTypes);\n\n this.work.on('newListener', (evt) => {\n this.listenForCustomEvents = true;\n });\n this.desiredEvents = []\n this.on('newListener', (evt) => {\n if (!this.connected && evt !== 'newListener')\n this.desiredEvents.push(evt);\n if (evt === 'cancel')\n this.listeningForCancel = true;\n });\n }\n \n /** \n * Cancel the job\n * @access public\n * @param {string} reason If provided, will be sent to client\n */\n async cancel (reason = undefined)\n {\n const response = await this.useDeployConnection('cancelJob', {\n job: this.address,\n owner: this.paymentAccountKeystore.address,\n reason,\n }, this.paymentAccountKeystore);\n\n return response.payload;\n }\n\n /** \n * Resume this job\n * @access public\n */\n async resume ()\n {\n // Careful -- this.schedulerConnection does not exist.\n const response = await this.schedulerConnection.send('resumeJob', {\n job: this.address,\n owner: this.paymentAccountKeystore.address,\n }, this.paymentAccountKeystore);\n\n return response.payload;\n }\n\n /**\n * Helper function for retrieving info about the job. The job must have already been deployed.\n * An alias for {@link module:dcp/compute.getJobInfo}.\n * @access public\n */\n getJobInfo ()\n {\n return (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.getJobInfo)(this.address);\n }\n \n /**\n * Helper function for retrieving info about the job's slices. The job must have already been deployed.\n * An alias for {@link module:dcp/compute.getSliceInfo}.\n * @access public\n */\n getSliceInfo ()\n {\n return (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.getSliceInfo)(this.address);\n }\n \n /** Escrow additional funds for this job\n * @access public\n * @param {number|BigNumber} fundsRequired - A number or BigNumber instance representing the funds to escrow for this job\n */\n async escrow (fundsRequired)\n {\n if ((typeof fundsRequired !== 'number' && !BigNumber.isBigNumber(fundsRequired))\n || fundsRequired <= 0 || !Number.isFinite(fundsRequired) || Number.isNaN(fundsRequired))\n throw new Error(`Job.escrow: fundsRequired must be a number greater than zero. (not ${fundsRequired})`);\n\n const bankConnection = new protocolV4.Connection(dcpConfig.bank.services.bankTeller);\n\n /*\n * escrow has been broken for an unknown amount of time. `feeStructureId` is not defined anywhere in the job class, and hasn't\n * for a period of time. When fixed, `this[INTERNAL_SYMBOL].payloadDetails.feeStructureId` will likely become just `this.feeStructureId`,\n * but it's being left alone until someone spends the time to fix escrow. / rs Jul 2022\n */\n const response = await bankConnection.send('embiggenFeeStructure', {\n feeStructureAddress: this[INTERNAL_SYMBOL].payloadDetails.feeStructureId,\n additionalEscrow: new BigNumber(fundsRequired),\n fromAddress: this.paymentAccountKeystore.address,\n }, this.paymentAccountKeystore);\n\n bankConnection.close();\n const escrowReceipt = response.payload;\n return escrowReceipt;\n }\n\n /**\n * create bundles for local dependencies\n */\n _pack ()\n {\n var retval = (__webpack_require__(/*! ./node-modules */ \"./src/dcp-client/job/node-modules.js\").createModuleBundle)(this.dependencies);\n return retval;\n }\n\n /** \n * Collect all of the dependencies together, throw them into a BravoJS\n * module which sideloads them as a side effect of declaration, and transmit\n * them to the package manager. Then we return the package descriptor object,\n * which is guaranteed to have only one file in it.\n *\n * @returns {object} with properties name and files[0]\n */\n async _publishLocalModules()\n {\n const dcpPublish = __webpack_require__(/*! dcp/common/dcp-publish */ \"./src/common/dcp-publish.js\");\n \n const { tempFile, hash, unresolved } = await this._pack();\n\n if (!tempFile) {\n return { unresolved };\n }\n\n const sideloaderFilename = tempFile.filename;\n const pkg = {\n name: `dcp-pkg-v1-localhost-${hash.toString('hex')}`,\n version: '1.0.0',\n files: {\n [sideloaderFilename]: `${sideloaderModuleIdentifier}.js`,\n },\n }\n\n await dcpPublish.publish(pkg);\n tempFile.remove();\n\n return { pkg, unresolved };\n }\n \n /**\n * This function specifies a module dependency (when the argument is a string)\n * or a list of dependencies (when the argument is an array) of the work\n * function. This function can be invoked multiple times before deployment.\n * @param {string | string[]} modulePaths - A string or array describing one\n * or more dependencies of the job.\n * @access public\n */\n requires (modulePaths)\n {\n if (typeof modulePaths !== 'string' && (!Array.isArray(modulePaths) || modulePaths.some((modulePath) => typeof modulePath !== 'string')))\n throw new TypeError('The argument to dependencies is not a string or an array of strings');\n else if (modulePaths.length === 0)\n throw new RangeError('The argument to dependencies cannot be an empty string or array');\n else if (Array.isArray(modulePaths) && modulePaths.some((modulePath) => modulePath.length === 0))\n throw new RangeError('The argument to dependencies cannot be an array containing an empty string');\n\n if (!Array.isArray(modulePaths))\n modulePaths = [modulePaths];\n\n for (const modulePath of modulePaths)\n {\n if (modulePath[0] !== '.' && modulePath.indexOf('/') !== -1)\n {\n const modulePrefixRegEx = /^(.*)\\/.*?$/;\n const [, modulePrefix] = modulePath.match(modulePrefixRegEx);\n if (modulePrefix && this.requirePath.indexOf(modulePrefix) === -1)\n this.requirePath.push(modulePrefix);\n }\n this.dependencies.push(modulePath);\n }\n }\n \n /** Set the account upon which funds will be drawn to pay for the job.\n * @param {module:dcp/wallet.AuthKeystore} keystore A keystore that representa a bank account.\n * @access public\n */\n setPaymentAccountKeystore (keystore)\n {\n if (this.address)\n {\n if (!keystore.address.eq(this.paymentAccountKeystore))\n {\n let message = 'Cannot change payment account after job has been deployed';\n this.emit('EPERM', message);\n throw new Error(`EPERM: ${message}`);\n }\n }\n \n if (!(keystore instanceof wallet.Keystore))\n throw new Error('Not an instance of Keystore: ' + keystore.toString());\n this.paymentAccountKeystore = keystore;\n }\n \n /** Set the slice payment offer. This is equivalent to the first argument to exec.\n * @param {number} slicePaymentOffer - The number of DCC the user is willing to pay to compute one slice of this job\n */\n setSlicePaymentOffer (slicePaymentOffer)\n {\n this.slicePaymentOffer = new SlicePaymentOffer(slicePaymentOffer);\n }\n \n \n /**\n * @param {URL|DcpURL} locationUrl - A URL object\n * @param {object} postParams - An object with any parameters that a user would like to be passed to a \n * remote result location. This object is capable of carry API keys for S3, \n * DropBox, etc. These parameters are passed as parameters in an \n * application/x-www-form-urlencoded request.\n */\n setResultStorage (locationUrl, postParams)\n {\n if (locationUrl instanceof URL || locationUrl instanceof DcpURL)\n this.resultStorageDetails = locationUrl;\n else\n throw new Error('Not an instance of a DCP URL: ' + locationUrl);\n \n\n // resultStorageParams contains any post params required for off-prem storage\n if (typeof postParams !== 'undefined' && typeof postParams === 'object' )\n this.resultStorageParams = postParams;\n else\n throw new Error('Not an instance of a object: ' + postParams);\n\n // Some type of object here\n this.resultStorageType = 'pattern';\n }\n \n /**\n * This function is identical to exec, except that the job is executed locally\n * in the client.\n * @async\n * @param {number} cores - the number of local cores in which to execute the job.\n * @param {...any} args - The remaining arguments are identical to the arguments of exec\n * @return {Promise<ResultHandle>} - resolves with the results of the job, rejects on an error\n * @access public\n */\n async localExec (cores = 1, ...args)\n {\n this.inLocalExec = true;\n this.estimationSlices = 0;\n this.greedyEstimation = false;\n this.isCI = false;\n\n let worker;\n this.on('accepted', () => {\n // Start a worker for this job\n worker = new Worker({\n localExec: true,\n jobAddresses: [this.address],\n allowedOrigins: this.localExecAllowedFiles,\n paymentAddress: this.paymentAccountKeystore.address,\n identity: this.identityKeystore,\n maxWorkingSandboxes: cores,\n sandboxOptions: {\n ignoreNoProgress: true,\n SandboxConstructor: (DCP_ENV.platform === 'nodejs'\n && (__webpack_require__(/*! ../worker/evaluators */ \"./src/dcp-client/worker/evaluators/index.js\").nodeEvaluatorFactory)())\n },\n });\n \n worker.on('error', (error) => {\n console.error('Worker Error:', error);\n });\n \n worker.on('warning', (warning) => {\n console.warn('Worker Warning:', warning);\n });\n\n worker.start().catch((e) => {\n console.error('Failed to start worker for localExec:');\n console.error(e.message);\n });\n });\n \n if (DCP_ENV.platform === 'nodejs')\n {\n this.localExecAllowedFiles =\n {\n any: [],\n fetchData: [],\n fetchWorkFunctions: [],\n fetchArguments: [],\n sendResults: [],\n };\n \n // Determine type of input data\n const { dataRange, dataValues, dataPattern, sliceCount } = marshalInputData(this.jobInputData);\n \n const inputSetFiles = [];\n \n let inputSetURIs = [];\n let dataSet;\n \n if (dataValues)\n {\n for (let i = 0; i < dataValues.length; i++)\n {\n if (!(dataValues[i] instanceof URL))\n {\n let marshaledInputValue = kvinMarshal(dataValues[i]);\n let inputDataFile = createTempFile('dcp-localExec-sliceData-XXXXXXXXX', 'kvin');\n inputDataFile.writeSync(JSON.stringify(marshaledInputValue));\n inputSetFiles.push(inputDataFile);\n inputSetURIs.push(new URL('file://' + inputDataFile.filename));\n }\n else\n {\n inputSetURIs.push(dataValues[i]);\n if (this.localExecAllowedFiles['fetchData'].indexOf(dataValues[i].origin) === -1)\n this.localExecAllowedFiles['fetchData'].push(dataValues[i].origin);\n }\n }\n dataSet = new RemoteDataSet(inputSetURIs);\n if (dataSet.length > 0)\n this.marshaledDataValues = dataSet;\n }\n if (dataRange)\n {\n inputSetFiles.push(createTempFile('dcp-localExec-sliceData-XXXXXXXXX', 'json'));\n let marshaledInputSet = JSON.stringify(dataRange);\n inputSetFiles[0].writeSync(marshaledInputSet)\n inputSetURIs.push(new URL('file://' + inputSetFiles[0].filename));\n dataSet = new RemoteDataSet(inputSetURIs);\n this.marshaledDataRange = dataSet;\n this.rangeLength = dataRange.length;\n }\n \n if (dataPattern)\n {\n let uri = dataPattern;\n for (let i = 0; i < sliceCount; i++)\n {\n let sliceNum = i+1;\n let newURI = new URL(uri.replace('{slice}', sliceNum.toString()));\n this.localExecAllowedFiles['fetchData'].push(newURI.origin);\n }\n }\n \n // For allowed origins of the localexec worker. Only allow the origins (files in this case) in this list.\n for (let i = 0; i < inputSetFiles.length; i++)\n {\n // Coerce into a URI to handle OS specific path separators.\n const inputFileURI = new URL(`file://${inputSetFiles[i].filename}`);\n this.localExecAllowedFiles['fetchData'].push(inputFileURI.pathname);\n }\n \n // Save work function to disk if work function starts with data (ie not remote)\n if (this.workFunctionURI.startsWith('data:'))\n {\n const workFunctionFile = createTempFile('dcp-localExec-workFunction-XXXXXXXXX', 'js');\n const workFunction = await fetchURI(this.workFunctionURI);\n workFunctionFile.writeSync(workFunction);\n \n const workFunctionFileURL = new URL('file://' + workFunctionFile);\n this.workFunctionURI = workFunctionFileURL.href;\n this.localExecAllowedFiles['fetchWorkFunctions'].push(workFunctionFileURL.pathname);\n }\n else\n this.localExecAllowedFiles['fetchWorkFunctions'].push(new URL(this.workFunctionURI).origin);\n \n let encodedJobArgumentUris = [];\n if (this.jobArguments)\n {\n if (this.jobArguments instanceof RemoteDataPattern) /* Not supported */\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG')\n if (this.jobArguments instanceof RemoteDataSet) /* Entire set is RemoteDataSet */\n {\n this.jobArguments.forEach((e) =>\n {\n this.localExecAllowedFiles['fetchArguments'].push(new URL(e).origin);\n encodedJobArgumentUris.push(new URL(e));\n });\n }\n else\n {\n for (let i = 0; i < this.jobArguments.length; i++)\n {\n if (this.jobArguments[i] instanceof URL)\n {\n if (this.localExecAllowedFiles['fetchArguments'].indexOf(this.jobArguments[i].origin) === -1)\n this.localExecAllowedFiles['fetchArguments'].push(this.jobArguments[i].origin);\n encodedJobArgumentUris.push(this.jobArguments[i]);\n }\n else\n {\n if (this.jobArguments[i] instanceof RemoteDataSet) /* Member of set is RemoteDataSet */\n {\n this.jobArguments[i].forEach((e) =>\n {\n this.localExecAllowedFiles['fetchArguments'].push(new URL(e).origin);\n encodedJobArgumentUris.push(new URL(e));\n });\n }\n else /* Actual Value */\n {\n const localArgFile = createTempFile(`dcp-localExec-argument-${i}-XXXXXXXXX`, 'kvin');\n const localArgFileURI = new URL(`file://${localArgFile.filename}`);\n\n localArgFile.writeSync(JSON.stringify(kvinMarshal(this.jobArguments[i])));\n this.localExecAllowedFiles['fetchArguments'].push(localArgFileURI.pathname);\n encodedJobArgumentUris.push(localArgFileURI);\n }\n }\n } \n }\n }\n this.marshaledArguments = kvinMarshal(encodedJobArgumentUris);\n\n // Support for remote results with localExec.\n if (this.resultStorageDetails)\n {\n this.localExecAllowedFiles['sendResults'].push(this.resultStorageDetails.origin);\n }\n }\n \n return this.exec(...args).finally(() => {\n if (worker) {\n setTimeout(() => {\n // stop the worker\n worker.stop(true);\n }, 3000);\n }\n });\n }\n\n /**\n * Deploys the job to the scheduler.\n * @param {number | object} [slicePaymentOffer=compute.marketValue] - Amount\n * in DCC that the user is willing to pay per slice.\n * @param {Keystore} [paymentAccountKeystore=wallet.get] - An instance of the\n * Wallet API Keystore that's used as the payment account when executing the\n * job.\n * @param {object} [initialSliceProfile] - An object describing the cost the\n * user believes the average slice will incur.\n * @access public\n * @emits Job#accepted\n */\n async exec (slicePaymentOffer = (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.marketValue), paymentAccountKeystore, initialSliceProfile)\n {\n if (this.connected)\n throw new Error('Exec called twice on the same job handle.');\n \n if (this.estimationSlices === Infinity)\n this.estimationSlices = null;\n else if (this.estimationSlices < 0)\n throw new Error('Incorrect value for estimationSlices; it can be an integer or Infinity!');\n \n if (this.tuning.kvin.speed || this.tuning.kvin.size)\n {\n tunedKvin = new kvin.KVIN();\n tunedKvin.tune = 'size';\n if(this.tuning.kvin.speed)\n tunedKvin.tune = 'speed';\n // If both size and speed are true, kvin will optimize based on speed\n if(this.tuning.kvin.speed && this.tuning.kvin.size)\n console.log('Slices and arguments are being uploaded with speed optimization.');\n }\n \n /* slight optimization to ensure we don't send requirements that will be ignored in the job submitter. Make a copy of the client specified requirements for this so that we dont magically override something they manually set */\n const _DEFAULT_REQUIREMENTS = JSON.parse(JSON.stringify(DEFAULT_REQUIREMENTS));\n removeBadRequirements(this.requirements, _DEFAULT_REQUIREMENTS);\n \n this.readyStateChange('exec');\n if ((typeof slicePaymentOffer === 'number') || (typeof slicePaymentOffer === 'object')\n || ((this.slicePaymentOffer === null || this.slicePaymentOffer === undefined) && typeof slicePaymentOffer === 'function'))\n this.setSlicePaymentOffer(slicePaymentOffer);\n if (typeof initialSliceProfile !== 'undefined')\n this.initialSliceProfile = initialSliceProfile;\n \n if (typeof paymentAccountKeystore !== 'undefined')\n {\n /** XXX @todo deprecate use of ethereum wallet objects */\n if (typeof paymentAccountKeystore === 'object' && paymentAccountKeystore.hasOwnProperty('_privKey'))\n {\n console.warn('* deprecated API * - job.exec invoked with ethereum wallet object as paymentAccountKeystore') /* /wg oct 2019 */\n paymentAccountKeystore = paymentAccountKeystore._privKey\n }\n /** XXX @todo deprecate use of private keys */\n if (wallet.isPrivateKey(paymentAccountKeystore))\n {\n console.warn('* deprecated API * - job.exec invoked with private key as paymentAccountKeystore') /* /wg dec 2019 */\n paymentAccountKeystore = await new wallet.Keystore(paymentAccountKeystore, '');\n }\n\n this.setPaymentAccountKeystore(paymentAccountKeystore)\n }\n \n if (this.paymentAccountKeystore)\n // Throws if they fail to unlock, we allow this since the keystore was set programmatically. \n await this.paymentAccountKeystore.unlock(undefined, parseFloat(dcpConfig.job.maxDeployTime));\n else\n {\n // If not set programmatically, we keep trying to get an unlocked keystore ... forever.\n let locked = true;\n let safety = 0; // no while loop shall go unguarded\n let ks;\n do\n {\n ks = null;\n // custom message for the browser modal to denote the purpose of keystore submission\n let msg = `This application is requesting a keystore file to execute ${this.public.description || this.public.name || 'this job'}. Please upload the corresponding keystore file. If you upload a keystore file which has been encrypted with a passphrase, the application will not be able to use it until it prompts for a passphrase and you enter it.`;\n try\n {\n ks = await wallet.get({ contextId: this.contextId, jobName: this.public.name, msg});\n }\n catch (e)\n {\n if (e.code !== ClientModal.CancelErrorCode) throw e;\n };\n if (ks)\n {\n try\n {\n await ks.unlock(undefined, parseFloat(dcpConfig.job.maxDeployTime));\n locked = false;\n }\n catch (e)\n {\n // prompt user again if user enters password incorrectly, exit modal otherwise\n if (e.code !== wallet.unlockFailErrorCode) throw e;\n }\n }\n if (safety++ > 1000) throw new Error('EINFINITY: job.exec tried wallet.get more than 1000 times.')\n } while (locked);\n this.setPaymentAccountKeystore(ks)\n }\n \n // We either have a valid keystore + password or we have rejected by this point.\n if (!this.slicePaymentOffer)\n throw new Error('A payment profile must be assigned before executing the job');\n else\n this.feeStructure = this.slicePaymentOffer.toFeeStructure(this.jobInputData.length);\n\n if (!this.address)\n {\n try\n {\n this.readyStateChange('init');\n await this.deployJob();\n const listenersPromise = this.addInitialEvents();\n const computeGroupsPromise = this.joinComputeGroups();\n let uploadSlicePromise;\n // if job data is by value then upload data to the scheduler in a staggered fashion\n if (Array.isArray(this.dataValues) && !this.marshaledDataValues)\n {\n this.readyStateChange('uploading');\n uploadSlicePromise = addSlices(this.dataValues, this.address, tunedKvin)\n .then(() => {\n debugging('slice-upload') && console.info(`970: slice data uploaded, closing job...`);\n return this.close();\n });\n }\n \n // await all promises for operations that can be done after the job is deployed\n await Promise.all([listenersPromise, computeGroupsPromise, uploadSlicePromise]);\n \n this.readyStateChange('deployed');\n this.emit('accepted', { job: this });\n }\n catch (error)\n {\n if (ON_BROWSER)\n await ClientModal.alert(error, { title: 'Failed to deploy job!' });\n throw error;\n }\n }\n else\n {\n // reconnecting to an old job\n await this.addInitialEvents();\n this.readyStateChange('reconnected');\n }\n\n this.connected = true;\n \n // start a timer to keep the node process alive until the results come back. Keep refreshing this timer until the results promise is resovled or rejected.\n let pendingResultsTimer;\n if (DCP_ENV.platform === 'nodejs')\n {\n pendingResultsTimer = setTimeout(() => {\n pendingResultsTimer.refresh();\n }, 1000 * 60 * 60) /* hour long timer */\n }\n\n return new Promise((resolve, reject) => {\n this.startEventBandaidWatchdog();\n \n const onComplete = () => {\n if (DCP_ENV.platform === 'nodejs')\n clearTimeout(pendingResultsTimer);\n this.stopEventBandaidWatchdog();\n resolve(this.results); \n };\n\n const onCancel = (event) => {\n /**\n * FIXME(DCP-1150): Remove this since normal cancel event is noisy\n * enough to not need stopped event too.\n */\n if (ON_BROWSER && !this.listeningForCancel)\n ClientModal.alert('More details in console...', { title: 'Job Canceled' });\n this.emit('cancel', event);\n\n let errorMsg = event.reason;\n if (event.error && event.error !== 'undefined')\n errorMsg = errorMsg +`\\n Recent error message: ${event.error.message}`\n \n if (DCP_ENV.platform === 'nodejs')\n clearTimeout(pendingResultsTimer); \n\n this.stopEventBandaidWatchdog();\n\n reject(new DCPError(errorMsg, event.code));\n };\n\n this.ee.once('stopped', async (stopEvent) => {\n // There is a chance the result submitter will emit finished > 1 time. Only handle it once.\n if (this.receivedStop)\n return;\n this.receivedStop = true;\n this.emit('stopped', stopEvent.runStatus);\n switch (stopEvent.runStatus) {\n case jobStatus.finished:\n if (this.collateResults)\n {\n let report = await this.getJobInfo();\n let allSliceNumbers = Array.from(Array(report.totalSlices)).map((e,i)=>i+1);\n let remainSliceNumbers = allSliceNumbers.filter((e) => !this.results.isAvailable(e));\n\n if (remainSliceNumbers.length)\n {\n const promises = remainSliceNumbers.map(sliceNumber => this.results.fetch([sliceNumber], true));\n await Promise.all(promises);\n }\n }\n\n this.emit('complete', this.results);\n onComplete();\n break;\n case jobStatus.cancelled:\n onCancel(stopEvent);\n break;\n default:\n /**\n * Asserting that we should never be able to reach here. The only\n * scheduler events that should trigger the Job's 'stopped' event\n * are jobStatus.cancelled, jobStatus.finished, and sliceStatus.paused.\n */\n reject(new Error(`Unknown event \"${stopEvent.runStatus}\" caused the job to be stopped.`));\n break;\n }\n });\n\n }).finally(() => {\n const handleErr = (e) => {\n console.error('Error while closing job connection:');\n console.error(e);\n }\n\n // Create an async IIFE to not block the promise chain\n (async () => {\n // delay to let last few events to be received\n await new Promise((resolve) => setTimeout(resolve, 1000));\n \n // close all of the connections so that we don't cause node processes to hang.\n this.closeDeployConnection();\n await this.eventSubscriber.close().catch(handleErr);\n await computeGroups.closeServiceConnection().catch((err) => {\n console.error('Warning: could not close compute groups service connection', err);\n });\n })();\n });\n }\n\n /**\n * Start the event-bandaid watchdog. Every [configurable] seconds, fetch\n * the job report and do appropriate things if the job is stopped.\n */\n startEventBandaidWatchdog()\n {\n if (this.eventBandaidHandle)\n this.stopEventBandaidWatchdog();\n\n this.eventBandaidHandle = setInterval(() => {\n this.eventBandaid();\n }, 60 * 1000);\n\n // if we are in a nodelike environment with unreffable timers, unref the\n // timer so it doesn't hold the process open. \n // \n // (n.b. while I understand that we _want_ the process to stay open while\n // the job lives, but this is Not The Way; the whole eventBandaid is a\n // temporary hack which will go away in time ~ER 2022-11-02)\n if (this.eventBandaidHandle.unref)\n this.eventBandaidHandle.unref();\n }\n\n /**\n * Stop and clean up the even-bandaid watchdog.\n */\n stopEventBandaidWatchdog()\n {\n if (this.eventBandaidHandle)\n clearInterval(this.eventBandaidHandle);\n\n this.eventBandaidHandle = false;\n }\n\n /**\n * Called on an interval, started by .exec and stopped by\n * onComplete/onCancel. Fetches the jobReport and verifies the job is\n * not in a terminal state; if the job is finished or cancelled, we\n * emit a synthetic internal stop event to trigger regular cleanup\n * (result retrieval, etc) and finalize the job handle.\n */\n eventBandaid()\n {\n this.getJobInfo()\n .then(jobInfo => {\n if ([jobStatus.finished, jobStatus.cancelled].includes(jobInfo.status))\n this.ee.emit('stopped', { runStatus: jobInfo.status });\n })\n .catch(error => {\n\n });\n }\n \n /**\n * job.addListeners(): Private function used to set up event listeners to the scheduler\n * before deploying the job.\n */\n async addInitialEvents ()\n {\n this.readyStateChange('listeners');\n\n // This is important: We need to flush the task queue before adding listeners\n // because we queue pending listeners by listening to the newListener event (in the constructor).\n // If we don't flush here, then the newListener events may fire after this function has run,\n // and the events won't be properly set up.\n await new Promise(resolve => setTimeout(resolve, 0));\n\n // @todo: Listen for an estimated cost, probably emit an \"estimated\" event when it comes in?\n // also @todo: Do the estimation task(s) on the scheduler and send an \"estimated\" event\n\n // Always listen to the stop event. It will resolve the work function promise, so is always needed.\n this.on('stop', (ev) => {this.ee.emit('stopped', ev)});\n\n // Connect listeners that were set up before exec\n if (this.desiredEvents.includes('result'))\n this.listeningForResults = true;\n await this.subscribeNewEvents(this.desiredEvents);\n\n // Connect listeners that are set up after exec\n this.on('newListener', (evt) => {\n if (evt === 'newListener' || this.desiredEvents.includes(evt))\n return;\n this.subscribeNewEvents([evt]);\n });\n \n // automatically add a listener for results if collateResults is on\n if (this.collateResults && !this.listeningForResults)\n this.on('result', () => {});\n\n debugging('dcp-client') && console.debug('subscribedEvents', this.desiredEvents);\n\n // If we have listeners for job.work, subscribe to custom events\n if (this.listenForCustomEvents)\n await this.subscribeCustomEvents();\n // Connect work event listeners that are set up after exec\n else\n this.work.on('newListener', () => this.subscribeCustomEvents());\n }\n \n /**\n * Subscribes to either reliable events or optional events. It is assumed that\n * any call to this function will include only new events.\n * @param {string[]} events \n */\n async subscribeNewEvents (events)\n {\n const reliableEvents = [];\n const optionalEvents = [];\n for (let eventName of events)\n {\n eventName = eventName.toLowerCase();\n if (this.eventTypes[eventName] && this.eventTypes[eventName].reliable)\n reliableEvents.push(eventName);\n else if (this.eventTypes[eventName] && !this.eventTypes[eventName].reliable)\n optionalEvents.push(eventName);\n else\n debugging('dcp-client') && console.debug(`Job handler has listener ${eventName} which isn't an event-router event.`);\n }\n if (debugging('dcp-client'))\n {\n console.debug('reliableEvents', reliableEvents);\n console.debug('optionalEvents', optionalEvents);\n }\n await this.eventSubscriber.subscribeManyEvents(reliableEvents, optionalEvents, { filter: { job: this.address } });\n }\n \n /**\n * Establishes listeners for worker events when requested by the client\n */\n async subscribeCustomEvents ()\n {\n if (!this.listeningForCustomEvents)\n await this.eventSubscriber.subscribeManyEvents([], ['custom'], { filter: { job: this.address } });\n this.listeningForCustomEvents = true\n }\n \n async joinComputeGroups ()\n {\n // localExec jobs are not entered in any compute group.\n if (!this.inLocalExec && this.computeGroups && this.computeGroups.length > 0)\n {\n this.readyStateChange('compute-groups');\n computeGroups.addRef(); // Just in case we're doing a Promise.all on multiple execs.\n\n // Add this job to its currently-defined compute groups (as well as public group, if included)\n let success;\n \n if (!Array.isArray(this.computeGroups)) \n throw new DCPError('Compute groups must be wrapped in an Array', 'DCPL-1101');\n\n for (let i = 0; i < this.computeGroups.length; i++)\n {\n let value = this.computeGroups[i];\n \n if (typeof value !== 'object')\n throw new DCPError(`This compute group: ${value[i]} must be an object`, 'DCPL-1102');\n \n if (value.joinKey && typeof value.joinKey !== 'string' && !(value.joinKey instanceof String))\n throw new DCPError(`This join key: ${value.joinKey} must be a string or a string literal`, 'DCPL-1103');\n else if (value.joinKeystore && !(value.joinKeystore instanceof wallet.Keystore))\n throw new DCPError(`This join Keystore: ${value.joinKeystore} must be an instance of wallet.Keystore`, 'DCPL-1104');\n else if (!value.joinKey && !value.joinKeystore)\n throw new DCPError('Compute group must contain a joinKey or a joinKeystore', 'DCPL-1105');\n }\n \n try\n {\n const cgPayload = await computeGroups.addJobToGroups(this.address, this.computeGroups);\n success = true; // To support older version of CG service where addJobToGroups had void/undefined return.\n if (cgPayload) success = cgPayload.success;\n debugging('dcp-client') && console.debug('job/index: addJobToGroups cgPayload:', cgPayload ? cgPayload : 'cgPayload is not defined; probably from legacy CG service.');\n }\n catch (e)\n {\n debugging('dcp-client') && console.debug('job/index: addJobToGroups threw exception:', e);\n success = false;\n }\n\n computeGroups.closeServiceConnection().catch((err) => {\n console.error('Warning: could not close compute groups service connection', err)\n });\n\n /* Could not put the job in any compute group, even though the user wanted it to run. Cancel the job. */\n if (!success)\n {\n await this.cancel('compute-groups::Unable to join any compute groups');\n throw new DCPError(`Access Denied::Failed to add job ${this.address} to any of the desired compute groups`, 'DCPL-1100');\n }\n }\n }\n \n /**\n * Takes result events as input, stores the result and fires off\n * events on the job handle as required. (result, duplicate-result)\n *\n * @param {object} ev - the event recieved from protocol.listen('/results/0xThisGenAdr')\n */\n async handleResult (ev)\n {\n if (this.results === null)\n // This should never happen - the onResult event should only be established/called\n // in addListeners which should also initialize the internal results array\n throw new Error('Job.onResult was invoked before initializing internal results');\n \n const { result: _result, time } = ev.result;\n debugging('dcp-client') && console.debug('handleResult', _result);\n let result = await fetchURI(_result);\n\n if (this.results.isAvailable(ev.sliceNumber))\n {\n const changed = JSON.stringify(this.results[ev.sliceNumber]) !== JSON.stringify(result);\n this.emit('duplicate-result', { sliceNumber: ev.sliceNumber, changed });\n }\n \n this.results.newResult(result, ev.sliceNumber - 1);\n }\n \n /**\n * Receives status events from the scheduler, updates the local status object\n * and emits a 'status' event\n *\n * @param {object} ev - the status event received from\n * protocol.listen('/status/0xThisGenAdr')\n * @param {boolean} emitStatus - value indicating whether or not the status\n * event should be emitted\n */\n handleStatus ({ runStatus, total, distributed, computed }, emitStatus = true)\n {\n Object.assign(this.status, {\n runStatus,\n total,\n distributed,\n computed,\n });\n\n if (emitStatus)\n this.emit('status', { ...this.status, job: this.address });\n }\n \n /**\n * Sends a request to the scheduler to deploy the job.\n */\n async deployJob ()\n {\n var moduleDependencies; \n \n /* Send sideloader bundle to the package server */\n if (DCP_ENV.platform === 'nodejs' && this.dependencies.length)\n {\n try\n {\n let { pkg, unresolved } = await this._publishLocalModules();\n\n moduleDependencies = unresolved;\n if (pkg)\n moduleDependencies.push(pkg.name + '/' + sideloaderModuleIdentifier); \n }\n catch(error)\n {\n throw new DCPError(`Error trying to communicate with package manager server: ${error}`);\n }\n }\n else\n moduleDependencies = this.dependencies;\n \n this.readyStateChange('preauth');\n\n const adhocId = this.uuid.slice(this.uuid.length - 6, this.uuid.length);\n const schedId = await dcpConfig.scheduler.identity;\n // The following check is needed for when using dcp-rtlink and loading the config through source, instead of using the dcp-client bundle\n let schedIdAddress = schedId;\n if(schedId.address)\n schedIdAddress = schedId.address;\n this.identityKeystore = await wallet.getId();\n const preauthToken = await bankUtil.preAuthorizePayment(schedIdAddress, this.maxDeployPayment, this.paymentAccountKeystore);\n const { dataRange, dataValues, dataPattern, sliceCount } = marshalInputData(this.jobInputData);\n if(dataValues)\n this.dataValues = dataValues;\n\n this.readyStateChange('deploying');\n\n /* Payload format is documented in scheduler-v4/libexec/job-submit/operations/submit.js */\n const submitPayload = {\n owner: this.identityKeystore.address,\n paymentAccount: this.paymentAccountKeystore.address,\n priority: 0, // @nyi\n\n workFunctionURI: this.workFunctionURI,\n uuid: this.uuid,\n mvMultSlicePayment: Number(this.feeStructure.marketValue) || 0, // @todo: improve feeStructure internals to better reflect v4\n absoluteSlicePayment: Number(this.feeStructure.maxPerRequest) || 0,\n requirePath: this.requirePath,\n dependencies: moduleDependencies,\n requirements: this.requirements, /* capex */\n localExec: this.inLocalExec,\n force100pctCPUDensity: this.force100pctCPUDensity,\n estimationSlices: this.estimationSlices,\n greedyEstimation: this.greedyEstimation,\n workerConsole: this.workerConsole,\n isCI: this.isCI,\n\n description: this.public.description || 'Discreetly making the world smarter',\n name: this.public.name || 'Ad-Hoc Job' + adhocId,\n link: this.public.link || '',\n\n preauthToken, // XXXwg/er @todo: validate this after fleshing out the stub(s)\n\n resultStorageType: this.resultStorageType, // @todo: implement other result types\n resultStorageDetails: this.resultStorageDetails, // Content depends on resultStorageType\n resultStorageParams: this.resultStorageParams, // Post params for off-prem storage\n dataRange,\n dataPattern,\n sliceCount,\n marshaledDataValues: this.marshaledDataValues,\n rangeLength: this.rangeLength\n };\n\n // Check if dataRange or dataPattern input is already marshaled\n if (this.marshaledDataRange)\n submitPayload.dataRange = this.marshaledDataRange;\n\n /* Determine composition of argument set and build payload */\n if (this.marshaledArguments)\n submitPayload.marshaledArguments = this.marshaledArguments;\n else if (this.jobArguments)\n {\n if (this.jobArguments instanceof RemoteDataPattern) /* Not supported */\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG')\n if (this.jobArguments instanceof RemoteDataSet) /* Entire set is RemoteDataSet */\n this.jobArguments = this.jobArguments.map((e) => new URL(e));\n\n submitPayload.marshaledArguments = kvin.marshal(encodeJobValueList(this.jobArguments, 'jobArguments'));\n }\n\n // XXXpfr Excellent tracing.\n if (debugging('dcp-client'))\n {\n const { dumpObject } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n dumpObject(submitPayload, 'Submit: Job Index: submitPayload', 256);\n }\n debugging('dcp-client') && console.debug('submitPayload.marshaledArguments', JSON.stringify(submitPayload.marshaledArguments).length, JSON.stringify(submitPayload).length);\n\n // Deploy the job! If we get an error, try again a few times until threshold of errors is reached, then actually throw it\n let deployed\n let deployAttempts = 0;\n while (deployAttempts++ < (dcpConfig.job.deployAttempts || 10))\n {\n try\n {\n deployed = await this.useDeployConnection('submit', submitPayload, this.identityKeystore);\n break;\n }\n catch (e)\n {\n if (deployAttempts < 10)\n debugging('dcp-client') && console.debug('Error when trying to deploy job, trying again', e);\n else\n throw e;\n }\n }\n\n if (!deployed.success)\n {\n // close all of the connections so that we don't cause node processes to hang.\n const handleErr = (e) => {\n console.error('Error while closing job connection:');\n console.error(e);\n };\n \n this.closeDeployConnection();\n this.eventSubscriber.close().catch(handleErr);\n computeGroups.closeServiceConnection().catch(handleErr);\n \n // Yes, it is possible for deployed or deployed.payload to be undefined.\n if (deployed.payload)\n {\n if (deployed.payload.code === 'ENOTFOUND')\n throw new DCPError(`Failed to submit job to scheduler. Account: ${submitPayload.paymentAccount} was not found or does not have sufficient balance (${deployed.payload.info.deployCost} DCCs needed to deploy this job)`, deployed.payload); \n throw new DCPError('Failed to submit job to scheduler', deployed.payload);\n }\n throw new DCPError('Failed to submit job to scheduler (no payload)', deployed ? deployed : '');\n }\n\n debugging('dcp-client') && console.debug('After Deploy', JSON.stringify(deployed));\n\n this.address = deployed.payload.job;\n this.deployCost = deployed.payload.deployCost;\n\n if (!this.status)\n this.status = {\n runStatus: null,\n total: 0,\n computed: 0,\n distributed: 0,\n };\n \n this.status.runStatus = deployed.payload.status;\n this.status.total = deployed.payload.lastSliceNumber;\n this.running = true;\n }\n \n /** close an open job to indicate we are done adding data so it is okay to finish\n * the job at the appropriate time\n */\n close ()\n {\n return this.useDeployConnection('closeJob', {\n job: this.id,\n });\n }\n \n /** Use the connection to job submit service. Will open a new connection if one does not exist,\n * and close the connection if it is idle for more than 10 seconds (tuneable).\n */\n useDeployConnection(...args)\n {\n if (!this.useDeployConnection.uses)\n this.useDeployConnection.uses = 0;\n this.useDeployConnection.uses++;\n if (!this.deployConnection)\n {\n debugging('deploy-connection') && console.info(`1453: making a new deployConnection...`)\n this.deployConnection = new protocolV4.Connection(dcpConfig.scheduler.services.jobSubmit); \n this.deployConnection.on('close', () => { this.deployConnection = null; });\n }\n if (this.deployConnectionTimeout)\n clearTimeout(this.deployConnectionTimeout);\n\n debugging('deploy-connection') && console.info(`1460: sending ${args[0]} request...`);\n const deployPromise = this.deployConnection.send(...args);\n \n deployPromise.finally(() => {\n this.useDeployConnection.uses--;\n\n debugging('deploy-connection') && console.info(`1462: deployConnection done ${args[0]} request, connection uses is ${this.useDeployConnection.uses}`)\n\n this.deployConnectionTimeout = setTimeout(() => {\n if (this.useDeployConnection.uses === 0 && this.deployConnection)\n {\n debugging('deploy-connection') && console.info(`1469: done with deployConn, closing...`);\n // if we're done w/ the connection, then remove its cleanup\n // function, close it, and clean up manually to make room for a\n // new conn when needed (ie, don't leave the closing conn where\n // someone could accidentally pick it up)\n this.deployConnection.removeAllListeners('close');\n this.deployConnection.close();\n this.deployConnection = null;\n }\n }, (dcpConfig.job.deployCloseTimeout || 10 * 1000));\n if (!ON_BROWSER)\n this.deployConnectionTimeout.unref();\n }); \n \n return deployPromise;\n }\n \n /**\n * Close the connection to the job submit (if it exists), and clear the close timeout (if needed).\n */\n closeDeployConnection()\n {\n if (this.deployConnection)\n this.deployConnection.close();\n if (this.deployConnectionTimeout)\n clearTimeout(this.deployConnectionTimeout);\n }\n}\n\n/** \n * Encode a value list for transmission to the job-submit daemon. This could be either job arguments\n * or the slice input set, if the input set was an Array-like object.\n *\n * @param {ArrayLike} valueList - The list of values to encode\n * @returns {Array<URL|{string: string, method:string, MIMEType: string }>} - URL or object with 3 properties:\n * string - serialization of value\n * method - how value was serialized (e.g. 'kvin', 'json')\n * MIMEType - MIME information corresponding to method (e.g. 'application/kvin', 'application/json')\n */\nfunction encodeJobValueList(valueList, valueKind)\n{\n var list = [];\n\n /*\n * We need to handle several different styles of datasets, and create the output array accordingly.\n *\n * 1. instance of RemoteDataSet or RemoteDataPattern => apply new URI(?)\n * 2. an Array-like objects => apply serializeJobValue\n * Values sent to the scheduler in payload are either URL or the result of calling serializeJobValue.\n */\n\n if (typeof valueList === 'undefined' || (typeof valueList === 'object' && valueList.length === 0))\n return list; /* empty set */\n\n if (typeof valueList !== 'object' || !valueList.hasOwnProperty('length'))\n throw new Error('value list must be an Array-like object');\n\n for (let i = 0; i < valueList.length; i++) /* Set is composed of values from potentially varying sources */\n {\n let value = valueList[i];\n if (value instanceof RemoteDataSet)\n value.forEach((el) => list.push(new URL(el)));\n else if (value instanceof RemoteDataPattern)\n {\n if (valueKind === jobValueKind.jobArguments)\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG');\n else\n {\n let uri = valueList['pattern'];\n for (let sliceNum = 1; sliceNum <= valueList['sliceCount']; sliceNum++)\n list.push(new URL(uri.replace('{slice}', sliceNum)))\n }\n }\n else if (value instanceof RemoteValue)\n list.push(serializeJobValue(value.href));\n else\n list.push(serializeJobValue(value));\n }\n\n return list;\n} \n\n/**\n * Depending on the shape of the job's data, resolve it into a RangeObject, a\n * Pattern, or a values array, and return it in the appropriate property.\n *\n * @param {any} data Job's input data\n * @return {MarshaledInputData} An object with one of the following properties set:\n * - dataValues: job input is an array of arbitrary values \n * - dataPattern: job input is a URI pattern \n * - dataRange: job input is a RangeObject (and/or friends)\n */\nfunction marshalInputData (data)\n{\n if (!(data instanceof Object || data instanceof SuperRangeObject))\n throw new TypeError(`Invalid job data type: ${typeof data}`);\n\n /**\n * @type {MarshaledInputData}\n */\n const marshalledInputData = {};\n\n // TODO(wesgarland): Make this more robust.\n if (data instanceof SuperRangeObject ||\n (data.hasOwnProperty('ranges') && data.ranges instanceof MultiRangeObject) ||\n (data.hasOwnProperty('start') && data.hasOwnProperty('end')))\n marshalledInputData.dataRange = data;\n else if (Array.isArray(data))\n marshalledInputData.dataValues = data;\n else if (data instanceof URL || data instanceof DcpURL)\n marshalledInputData.dataPattern = String(data);\n else if(data instanceof RemoteDataSet)\n marshalledInputData.dataValues = data.map(e => new URL(e));\n else if(data instanceof RemoteDataPattern)\n {\n marshalledInputData.dataPattern = data['pattern'];\n marshalledInputData.sliceCount = data['sliceCount'];\n }\n\n debugging('job') && console.debug('marshalledInputData:', marshalledInputData);\n return marshalledInputData;\n}\n\n/**\n * marshal the value using kvin or instance of the kvin (tunedKvin)\n * tunedKvin is defined if job.tuning.kvin is specified.\n *\n * @param {any} value \n * @return {object} A marshaled object\n * \n */\nfunction kvinMarshal (value) {\n if (tunedKvin)\n return tunedKvin.marshal(value);\n\n return kvin.marshal(value);\n}\n\n\n\nexports.Job = Job;\nexports.SlicePaymentOffer = SlicePaymentOffer;\nexports.ResultHandle = ResultHandle;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/job/index.js?");
4274
+ eval("/**\n * @file job/index.js\n * @author Eddie Roosenmaallen, eddie@kingsds.network\n * Matthew Palma, mpalma@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * Ryan Saweczko, ryansaweczko@kingsds.network\n * @date November 2018\n * November 2018\n * February 2022\n * May 2022\n * Jun 2022\n *\n * This module implements the Compute API's Job Handle\n *\n */\n\n\nconst { BigNumber } = __webpack_require__(/*! bignumber.js */ \"./node_modules/bignumber.js/bignumber.js\");\nconst { v4: uuidv4 } = __webpack_require__(/*! uuid */ \"./node_modules/uuid/dist/esm-browser/index.js\");\nconst { EventEmitter, PropagatingEventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { RangeObject, MultiRangeObject, DistributionRange, SuperRangeObject, SparseRangeObject } = __webpack_require__(/*! dcp/dcp-client/range-object */ \"./src/dcp-client/range-object.js\");\nconst { fetchURI, encodeDataURI, createTempFile } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { serializeJobValue, RemoteValue } = __webpack_require__(/*! dcp/dcp-client/job-values */ \"./src/dcp-client/job-values.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst { DcpURL } = __webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\");\nconst ClientModal = __webpack_require__(/*! dcp/dcp-client/client-modal */ \"./src/dcp-client/client-modal/index.js\");\nconst { Worker } = __webpack_require__(/*! dcp/dcp-client/worker */ \"./src/dcp-client/worker/index.js\");\nconst { RemoteDataSet } = __webpack_require__(/*! dcp/dcp-client/remote-data-set */ \"./src/dcp-client/remote-data-set.js\");\nconst { RemoteDataPattern } = __webpack_require__(/*! dcp/dcp-client/remote-data-pattern */ \"./src/dcp-client/remote-data-pattern.js\");\nconst { ResultHandle } = __webpack_require__(/*! ./result-handle */ \"./src/dcp-client/job/result-handle.js\");\nconst { SlicePaymentOffer } = __webpack_require__(/*! ./slice-payment-offer */ \"./src/dcp-client/job/slice-payment-offer.js\");\nconst { addSlices } = __webpack_require__(/*! ./upload-slices */ \"./src/dcp-client/job/upload-slices.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst computeGroups = __webpack_require__(/*! dcp/dcp-client/compute-groups */ \"./src/dcp-client/compute-groups/index.js\");\nconst schedulerConstants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { DEFAULT_REQUIREMENTS, removeBadRequirements } = __webpack_require__(/*! dcp/common/job-requirements-defaults */ \"./src/common/job-requirements-defaults.js\");\nconst { sliceStatus, jobValueKind } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { jobStatus } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst bankUtil = __webpack_require__(/*! dcp/dcp-client/bank-util */ \"./src/dcp-client/bank-util.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('dcp-client');\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nlet tunedKvin;\n\nconst log = (...args) => {\n if (debugging('job')) {\n console.debug('dcp-client:job', ...args);\n }\n};\n\nconst ON_BROWSER = DCP_ENV.isBrowserPlatform;\nconst sideloaderModuleIdentifier = 'sideloader-v1';\n\n\n/** @typedef {import('dcp/dcp-client/wallet/keystore').Keystore} Keystore */\n/** @typedef {import('dcp/dcp-client/range-object').RangeLike} RangeLike */\n/** @typedef {import('scheduler-v4/libexec/job-submit/operations/submit').MarshaledInputData} MarshaledInputData */\n\n/**\n * Ensure input data is an appropriate format\n * @param {RangeObject | DistributionRange | RemoteDataSet | Array | Iterable}\n * inputData - A URI-shaped string, a [Multi]RangeObject-constructing value, or\n * an array of slice data\n * @return {RangeObject | RangeLike | DistributionRange | RemoteDataSet | Array}\n * The coerced input in an appropriate format ([Multi]RangeObject,\n * DistributionRange, RemoteDataSet, or array)\n */\n const wrangleData = (inputData) => {\n\n if (RangeObject.isRangelike(inputData)) { return inputData }\n if (RangeObject.isRangeObject(inputData)) { return inputData }\n if (DistributionRange.isDistribution(inputData)) { return inputData }\n if (inputData instanceof SparseRangeObject) { return inputData }\n if (inputData instanceof MultiRangeObject) { return inputData }\n if (MultiRangeObject.isProtoMultiRangelike(inputData)) { return new MultiRangeObject(inputData) }\n if (RangeObject.isProtoRangelike(inputData)) { return new RangeObject(inputData) }\n if (DistributionRange.isProtoDistribution(inputData)) { return new DistributionRange(inputData) }\n if (RemoteDataSet.isRemoteDataSet(inputData)) { return inputData }\n if (RemoteDataPattern.isRemoteDataPattern(inputData)) { return inputData }\n\n return Array.isArray(inputData) ? inputData : [inputData];\n};\n\n/**\n * @classdesc The Compute API's Job Handle (see {@link https://docs.dcp.dev/specs/compute-api.html#job-handles|Compute API spec})\n * Job handles are objects which correspond to jobs. \n * They are created by some exports of the compute module, such as {@link module:dcp/compute.do|compute.do} and {@link module:dcp/compute.for|compute.for}.\n * @extends module:dcp/dcp-events.PropagatingEventEmitter\n * @hideconstructor\n * @access public\n */\nclass Job extends PropagatingEventEmitter\n{\n /**\n * Fired when the job is accepted by the scheduler on deploy.\n * \n * @event Job#accepted\n * @access public\n * @type {object}\n *//**\n * Fired when the job is cancelled.\n * \n * @event Job#cancel\n * @access public\n *//**\n * Fired when a result is returned.\n * \n * @event Job#result\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {string} task ID of the task (slice) the result came from\n * @property {number} sort The index of the slice\n * @property {object} result\n * @property {string} result.request\n * @property {*} result.result The value returned from the work function\n *//**\n * Fired when the result handle is modified, either when a new `result` event is fired or when the results are populated with `results.fetch()`\n * \n * @event Job#resultsUpdated\n * @access public\n *//**\n * Fired when the job has been completed.\n * \n * @event Job#complete\n * @access public\n * @type {ResultHandle}\n *//**\n * Fired when the job's status changes.\n * \n * @event Job#status\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} total Total number of slices in the job\n * @property {number} distributed Number of slices that have been distributed\n * @property {number} computed Number of slices that have completed execution (returned a result)\n * @property {string} runStatus Current runStatus of the job\n *//**\n * Fired when a slice throws an error.\n * \n * @event Job#error\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex Index of the slice that threw the error\n * @property {string} message The error message\n * @property {string} stack The error stacktrace\n * @property {string} name The error type name\n *//**\n * Fired when a slice uses one of the console log functions.\n * \n * @event Job#console\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex The index of the slice that produced this event\n * @property {string} level The log level, one of `debug`, `info`, `log`, `warn`, or `error`\n * @property {string} message The console log message\n *//**\n * Fired when a slice is stopped for not calling progress. Contains information about how long the slice ran for, and about the last reported progress calls.\n * \n * @event Job#noProgress\n * @access public\n * @type {object}\n * @property {string} jobAddress Address of the job\n * @property {number} sliceIndex The index of the slice that failed due to no progress\n * @property {number} timestamp How long the slice ran before failing\n * @property {object} progressReports\n * @property {object} progressReports.last The last progress report received from the worker\n * @property {number} progressReports.last.timestamp Time since the start of the slice\n * @property {number} progressReports.last.progress Progress value reported\n * @property {*} progressReports.last.value The last value that was passed to the progress function\n * @property {number} progressReports.last.throttledReports Number of calls to progress that were throttled since the last report\n * @property {object} progressReports.lastUpdate The last determinate (update to the progress param) progress report received from the worker\n * @property {number} progressReports.lastUpdate.timestamp\n * @property {number} progressReports.lastUpdate.progress\n * @property {*} progressReports.lastUpdate.value\n * @property {number} progressReports.lastUpdate.throttledReports\n *//**\n @todo: is this in the spec? is there a no progress data? should there be?\n * Identical to `noProgress`, except that it also contains the data that the slice was executed with.\n * \n * @event Job#noProgressData\n * @access public\n * @type {object}\n * @property {*} data The data that the slice was executed with\n *//**\n * Fired when the job is paused due to running out of funds. The job can be resumed by escrowing more funds then resuming the job.\n * @todo: is this a thing, should it be a thing (the payload)\n * Event payload is the estimated funds required to complete the job\n * \n * @event Job#nofunds\n * @access public\n * @type {BigNumber}\n *//**\n * Fired when the job cannot be deployed due to no bank account / not enough balance to deploy the job\n * \n * @event Job#ENOFUNDS\n * @access public\n *//**\n * Fired when the job is cancelled due to the work function not calling the `progress` method frequently enough.\n * \n * @event Job#ENOPROGRESS\n * @access public\n *//**\n * The job was cancelled because scheduler has determined that individual tasks in this job exceed the maximum allowable execution time.\n * \n * @event Job#ESLICETOOSLOW\n * @access public\n *//**\n * Fired when the job is cancelled because too many work functions are terminating with uncaught exceptions.\n * \n * @event Job#ETOOMANYERRORS\n * @access public\n */\n\n /**\n * @form1 new Job('application_worker_address'[, data[, arguments]])\n * @form2a new Job('worker source'[, data[, arguments]])\n * @form2b new Job(worker_function[, data[, arguments]])\n */\n constructor ()\n {\n super('Job');\n if (typeof arguments[0] === 'function')\n arguments[0] = arguments[0].toString();\n\n if (typeof arguments[0] === 'string')\n {\n const { encodeDataURI } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n this.workFunctionURI = encodeDataURI(arguments[0], 'application/javascript');\n } \n else if (DcpURL.isURL(arguments[0]))\n this.workFunctionURI = arguments[0].href;\n\n this.jobInputData = wrangleData(arguments[1] || []);\n this.jobArguments = wrangleData(arguments[2] || []);\n \n log('num wrangledInputData:', this.jobInputData.length);\n log('num wrangledArguments:', this.jobArguments.length);\n\n this.initEventSystems();\n\n /**\n * An object describing the cost the user believes each the average slice will incur, in terms of CPU/GPU and I/O.\n * If defined, this object is used to provide initial scheduling hints and to calculate escrow amounts.\n *\n * @type {object}\n * @access public\n */\n this.initialSliceProfile = undefined;\n \n // The max value that the client is willing to spend to deploy\n // (list on the scheduler, doesn't include compute payment)\n // maxDeployPayment is the max the user is willing to pay to DCP (as a\n // Hold), in addition to the per-slice offer and associated scrape.\n // Currently calculated as `deployCost = costPerKB *\n // (JSON.stringify(job).length / 1024) // 1e-9 per kb`\n // @todo: figure this out / er nov 2018\n this.maxDeployPayment = 1;\n\n /**\n * An object describing the requirements that workers must have to be eligible for this job. The default values are set in the job-submitter, and only the client specified\n * requirements are sent over the wire. See {@link https://docs.dcp.dev/specs/compute-api.html#requirements-objects|Requirements Objects}.\n *\n * @type {object}\n * @access public\n */\n this.requirements = {};\n\n /**\n * A place to store public-facing attributes of the job. Anything stored on this object will be available inside the work \n * function (see {@link module:dcp/compute~sandboxEnv.work}). The properties documented here may be used by workers to display what jobs are currently being \n * worked on.\n * @access public\n * @property {string} name Public-facing name of this job.\n * @property {string} description Public-facing description for this job.\n * @property {string} link Public-facing link to external resource about this job.\n */\n this.public = {\n name: null,\n description: null,\n link: null,\n };\n \n /**\n * A cryptographic receipt indicating deployment of the job on the scheduler\n * @type {object}\n * @access public\n */\n this.receipt = null;\n \n /**\n * a SliceProfile object which contains the average costs for the slices which have been computed to date.\n * Until the first result is returned, this property is undefined.\n * @type {object}\n * @access public\n */\n this.meanSliceProfile = null;\n \n /**\n * A number (can be null, undefined, or infinity) describing the estimationSlicesRemaining in the jpd (dcp-2593)\n * @type {number}\n * @access public\n */\n this.estimationSlices = undefined;\n /**\n * When true, allows a job in estimation to have requestTask return multiple estimation slices.\n * This flag applies independent of infinite estimation, viz., this.estimationSlices === null .\n * @type {boolean}\n * @access public\n */\n this.greedyEstimation = false;\n /**\n * tunable parameters per job\n * @access public\n * @param {object} tuning \n * @param {string} tuning.kvin Encode the TypedArray into a string, trying multiple methods to determine optimum \n * size/performance. The this.tune variable affects the behavior of this code this:\n * @param {boolean} speed If true, only do naive encoding: floats get represented as byte-per-digit strings\n * @param {boolean} size If true, try the naive, ab8, and ab16 encodings; pick the smallest\n * If both are false try the naive encoding if under typedArrayPackThreshold and use if smaller\n * than ab8; otherwise, use ab8\n */\n this.tuning = {\n kvin: {\n size: false,\n speed: false,\n },\n }\n /* For API interface to end-users only */\n Object.defineProperty(this, 'id', {\n get: () => this.address,\n set: (id) => { this.address = id }\n });\n \n this.uuid = uuidv4(); /** @see {@link https://kingsds.atlassian.net/browse/DCP-1475?atlOrigin=eyJpIjoiNzg3NmEzOWE0OWI4NGZkNmI5NjU0MWNmZGY2OTYzZDUiLCJwIjoiaiJ9|Jira Issue} */\n this.dependencies = []; /* dependencies of the work function */\n this.requirePath = []; /* require path for dependencies */\n this.modulePath = []; /* path to module that invoked .exec() for job */\n this.connected = false; /* true when exec or resume called */\n this.results = new ResultHandle(this); /* result handle */\n this.collateResults = true; /* option to receive results as they are computed & ensure all are received on finish */\n this.contextId = null; /* optional string which is used to indicate to caching mechanisms different keystores with same name */ \n this.force100pctCPUDensity = false; /* tell scheduler to assume this job uses 100% cpu density */\n this.workerConsole = false; /* tell workers to log more information about failures in the evaluator this job causes */\n this.address = null; /* job address, created by scheduler during exec call. */\n this.paymentAccountKeystore = null; /* keystore for payment for job to come from */\n this.status = { /* job status details */\n runStatus: null,\n total: null,\n distributed: null,\n computed: null\n };\n \n // service locations\n this.scheduler = dcpConfig.scheduler.services.jobSubmit.location;\n this.bank = dcpConfig.bank.services.bankTeller.location;\n \n // Compute groups. Add to public compute group by default\n this.computeGroups = [ Object.assign({}, schedulerConstants.computeGroups.public) ];\n \n // Update the ready state as we go through job deployment\n this.readyState = sliceStatus.new;\n const that = this;\n this.readyStateChange = function job$$readyStateChange (readyState)\n {\n that.readyState = readyState;\n that.emit('readyStateChange', that.readyState);\n }\n }\n \n /**\n * Initialize the various event systems the job handle requires. These include:\n * - an internal event emitter (this.ee)\n * - an event emitter for any events emitted on `work.emit` within work functions (this.work)\n * - an event subscriber to subscribe (to receive) events from the scheduler (this.eventSubscriber)\n */\n initEventSystems ()\n {\n // Handle the various event-related things required in the constructor\n\n // Internal event emitter for events within job handle\n this.ee = new EventEmitter('Job Internal');\n\n /**\n * An EventEmitter for custom events dispatched by the work function.\n * @type {module:dcp/dcp-events.EventEmitter}\n * @access public\n * @example\n * // in work function\n * work.emit('myEventName', 1, [2], \"three\");\n * // client-side\n * job.work.on('myEventName', (num, arr, string) => { });\n */\n this.work = new EventEmitter('job.work');\n this.listenForCustomEvents = false;\n\n // Initialize the eventSubscriber so each job has unique eventSubscriber\n this.eventSubscriber = new ((__webpack_require__(/*! dcp/events/event-subscriber */ \"./src/events/event-subscriber.js\").EventSubscriber))(this);\n\n // Some events from the event subscriber can't be emitted immediately upon receipt without having \n // weird/wrong output due to things like serialization. We allow interceptors in the event subscriber\n // to handle this.\n const that = this\n var lastConsoleEv;\n var sameCounter = 1;\n const parseConsole = function deserializeConsoleMessage(ev) {\n if (tunedKvin)\n ev.message = tunedKvin.unmarshal(ev.message);\n else \n ev.message = kvin.unmarshal(ev.message);\n \n if (lastConsoleEv && ev.message[0] === lastConsoleEv.message[0] && ev.sliceNumber === lastConsoleEv.sliceNumber && ev.level === lastConsoleEv.level)\n ev.same = ++sameCounter;\n else\n sameCounter = 1;\n lastConsoleEv = ev;\n \n /* if we have the same message being logged (same sliceNumber, message, log level), the console event object will have the sole property same, nothing else */\n if (ev.same > 1)\n that.emit('console', { same: ev.same });\n else\n {\n delete ev.same;\n that.emit('console', ev);\n }\n }\n\n this.eventIntercepts = {\n result: (ev) => this.handleResult(ev),\n status: (ev) => this.handleStatus(ev),\n cancel: (ev) => this.ee.emit('stopped', ev),\n custom: (ev) => this.work.emit(ev.customEvent, ev),\n console: parseConsole,\n };\n \n this.eventTypes = (__webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\").eventTypes);\n\n this.work.on('newListener', (evt) => {\n this.listenForCustomEvents = true;\n });\n this.desiredEvents = []\n this.on('newListener', (evt) => {\n if (!this.connected && evt !== 'newListener')\n this.desiredEvents.push(evt);\n if (evt === 'cancel')\n this.listeningForCancel = true;\n });\n }\n \n /** \n * Cancel the job\n * @access public\n * @param {string} reason If provided, will be sent to client\n */\n async cancel (reason = undefined)\n {\n const response = await this.useDeployConnection('cancelJob', {\n job: this.address,\n owner: this.paymentAccountKeystore.address,\n reason,\n }, this.paymentAccountKeystore);\n\n return response.payload;\n }\n\n /** \n * Resume this job\n * @access public\n */\n async resume ()\n {\n // Careful -- this.schedulerConnection does not exist.\n const response = await this.schedulerConnection.send('resumeJob', {\n job: this.address,\n owner: this.paymentAccountKeystore.address,\n }, this.paymentAccountKeystore);\n\n return response.payload;\n }\n\n /**\n * Helper function for retrieving info about the job. The job must have already been deployed.\n * An alias for {@link module:dcp/compute.getJobInfo}.\n * @access public\n */\n getJobInfo ()\n {\n return (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.getJobInfo)(this.address);\n }\n \n /**\n * Helper function for retrieving info about the job's slices. The job must have already been deployed.\n * An alias for {@link module:dcp/compute.getSliceInfo}.\n * @access public\n */\n getSliceInfo ()\n {\n return (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.getSliceInfo)(this.address);\n }\n \n /** Escrow additional funds for this job\n * @access public\n * @param {number|BigNumber} fundsRequired - A number or BigNumber instance representing the funds to escrow for this job\n */\n async escrow (fundsRequired)\n {\n if ((typeof fundsRequired !== 'number' && !BigNumber.isBigNumber(fundsRequired))\n || fundsRequired <= 0 || !Number.isFinite(fundsRequired) || Number.isNaN(fundsRequired))\n throw new Error(`Job.escrow: fundsRequired must be a number greater than zero. (not ${fundsRequired})`);\n\n const bankConnection = new protocolV4.Connection(dcpConfig.bank.services.bankTeller);\n\n /*\n * escrow has been broken for an unknown amount of time. `feeStructureId` is not defined anywhere in the job class, and hasn't\n * for a period of time. When fixed, `this[INTERNAL_SYMBOL].payloadDetails.feeStructureId` will likely become just `this.feeStructureId`,\n * but it's being left alone until someone spends the time to fix escrow. / rs Jul 2022\n */\n const response = await bankConnection.send('embiggenFeeStructure', {\n feeStructureAddress: this[INTERNAL_SYMBOL].payloadDetails.feeStructureId,\n additionalEscrow: new BigNumber(fundsRequired),\n fromAddress: this.paymentAccountKeystore.address,\n }, this.paymentAccountKeystore);\n\n bankConnection.close();\n const escrowReceipt = response.payload;\n return escrowReceipt;\n }\n\n /**\n * create bundles for local dependencies\n */\n _pack ()\n {\n var retval = (__webpack_require__(/*! ./node-modules */ \"./src/dcp-client/job/node-modules.js\").createModuleBundle)(this.dependencies);\n return retval;\n }\n\n /** \n * Collect all of the dependencies together, throw them into a BravoJS\n * module which sideloads them as a side effect of declaration, and transmit\n * them to the package manager. Then we return the package descriptor object,\n * which is guaranteed to have only one file in it.\n *\n * @returns {object} with properties name and files[0]\n */\n async _publishLocalModules()\n {\n const dcpPublish = __webpack_require__(/*! dcp/common/dcp-publish */ \"./src/common/dcp-publish.js\");\n \n const { tempFile, hash, unresolved } = await this._pack();\n\n if (!tempFile) {\n return { unresolved };\n }\n\n const sideloaderFilename = tempFile.filename;\n const pkg = {\n name: `dcp-pkg-v1-localhost-${hash.toString('hex')}`,\n version: '1.0.0',\n files: {\n [sideloaderFilename]: `${sideloaderModuleIdentifier}.js`,\n },\n }\n\n await dcpPublish.publish(pkg);\n tempFile.remove();\n\n return { pkg, unresolved };\n }\n \n /**\n * This function specifies a module dependency (when the argument is a string)\n * or a list of dependencies (when the argument is an array) of the work\n * function. This function can be invoked multiple times before deployment.\n * @param {string | string[]} modulePaths - A string or array describing one\n * or more dependencies of the job.\n * @access public\n */\n requires (modulePaths)\n {\n if (typeof modulePaths !== 'string' && (!Array.isArray(modulePaths) || modulePaths.some((modulePath) => typeof modulePath !== 'string')))\n throw new TypeError('The argument to dependencies is not a string or an array of strings');\n else if (modulePaths.length === 0)\n throw new RangeError('The argument to dependencies cannot be an empty string or array');\n else if (Array.isArray(modulePaths) && modulePaths.some((modulePath) => modulePath.length === 0))\n throw new RangeError('The argument to dependencies cannot be an array containing an empty string');\n\n if (!Array.isArray(modulePaths))\n modulePaths = [modulePaths];\n\n for (const modulePath of modulePaths)\n {\n if (modulePath[0] !== '.' && modulePath.indexOf('/') !== -1)\n {\n const modulePrefixRegEx = /^(.*)\\/.*?$/;\n const [, modulePrefix] = modulePath.match(modulePrefixRegEx);\n if (modulePrefix && this.requirePath.indexOf(modulePrefix) === -1)\n this.requirePath.push(modulePrefix);\n }\n this.dependencies.push(modulePath);\n }\n }\n \n /** Set the account upon which funds will be drawn to pay for the job.\n * @param {module:dcp/wallet.AuthKeystore} keystore A keystore that representa a bank account.\n * @access public\n */\n setPaymentAccountKeystore (keystore)\n {\n if (this.address)\n {\n if (!keystore.address.eq(this.paymentAccountKeystore))\n {\n let message = 'Cannot change payment account after job has been deployed';\n this.emit('EPERM', message);\n throw new Error(`EPERM: ${message}`);\n }\n }\n \n if (!(keystore instanceof wallet.Keystore))\n throw new Error('Not an instance of Keystore: ' + keystore.toString());\n this.paymentAccountKeystore = keystore;\n }\n \n /** Set the slice payment offer. This is equivalent to the first argument to exec.\n * @param {number} slicePaymentOffer - The number of DCC the user is willing to pay to compute one slice of this job\n */\n setSlicePaymentOffer (slicePaymentOffer)\n {\n this.slicePaymentOffer = new SlicePaymentOffer(slicePaymentOffer);\n }\n \n \n /**\n * @param {URL|DcpURL} locationUrl - A URL object\n * @param {object} postParams - An object with any parameters that a user would like to be passed to a \n * remote result location. This object is capable of carry API keys for S3, \n * DropBox, etc. These parameters are passed as parameters in an \n * application/x-www-form-urlencoded request.\n */\n setResultStorage (locationUrl, postParams)\n {\n if (locationUrl instanceof URL || locationUrl instanceof DcpURL)\n this.resultStorageDetails = locationUrl;\n else\n throw new Error('Not an instance of a DCP URL: ' + locationUrl);\n \n\n // resultStorageParams contains any post params required for off-prem storage\n if (typeof postParams !== 'undefined' && typeof postParams === 'object' )\n this.resultStorageParams = postParams;\n else\n throw new Error('Not an instance of a object: ' + postParams);\n\n // Some type of object here\n this.resultStorageType = 'pattern';\n }\n \n /**\n * This function is identical to exec, except that the job is executed locally\n * in the client.\n * @async\n * @param {number} cores - the number of local cores in which to execute the job.\n * @param {...any} args - The remaining arguments are identical to the arguments of exec\n * @return {Promise<ResultHandle>} - resolves with the results of the job, rejects on an error\n * @access public\n */\n async localExec (cores = 1, ...args)\n {\n this.inLocalExec = true;\n this.estimationSlices = 0;\n this.greedyEstimation = false;\n this.isCI = false;\n\n let worker;\n this.on('accepted', () => {\n // Start a worker for this job\n worker = new Worker({\n localExec: true,\n jobAddresses: [this.address],\n allowedOrigins: this.localExecAllowedFiles,\n paymentAddress: this.paymentAccountKeystore.address,\n identity: this.identityKeystore,\n maxWorkingSandboxes: cores,\n sandboxOptions: {\n ignoreNoProgress: true,\n SandboxConstructor: (DCP_ENV.platform === 'nodejs'\n && (__webpack_require__(/*! ../worker/evaluators */ \"./src/dcp-client/worker/evaluators/index.js\").nodeEvaluatorFactory)())\n },\n });\n \n worker.on('error', (error) => {\n console.error('LocalExec Error:', error);\n });\n \n worker.on('warning', (warning) => {\n console.warn('LocalExec Warning:', warning);\n });\n\n worker.start().catch((e) => {\n console.error('Failed to start worker for localExec:');\n console.error(e.message);\n });\n });\n \n if (DCP_ENV.platform === 'nodejs')\n {\n this.localExecAllowedFiles =\n {\n any: [],\n fetchData: [],\n fetchWorkFunctions: [],\n fetchArguments: [],\n sendResults: [],\n };\n \n // Determine type of input data\n const { dataRange, dataValues, dataPattern, sliceCount } = marshalInputData(this.jobInputData);\n \n const inputSetFiles = [];\n \n let inputSetURIs = [];\n let dataSet;\n \n if (dataValues)\n {\n for (let i = 0; i < dataValues.length; i++)\n {\n if (!(dataValues[i] instanceof URL))\n {\n let marshaledInputValue = kvinMarshal(dataValues[i]);\n let inputDataFile = createTempFile('dcp-localExec-sliceData-XXXXXXXXX', 'kvin');\n inputDataFile.writeSync(JSON.stringify(marshaledInputValue));\n inputSetFiles.push(inputDataFile);\n inputSetURIs.push(new URL('file://' + inputDataFile.filename));\n }\n else\n {\n inputSetURIs.push(dataValues[i]);\n if (this.localExecAllowedFiles['fetchData'].indexOf(dataValues[i].origin) === -1)\n this.localExecAllowedFiles['fetchData'].push(dataValues[i].origin);\n }\n }\n dataSet = new RemoteDataSet(inputSetURIs);\n if (dataSet.length > 0)\n this.marshaledDataValues = dataSet;\n }\n if (dataRange)\n {\n inputSetFiles.push(createTempFile('dcp-localExec-sliceData-XXXXXXXXX', 'json'));\n let marshaledInputSet = JSON.stringify(dataRange);\n inputSetFiles[0].writeSync(marshaledInputSet)\n inputSetURIs.push(new URL('file://' + inputSetFiles[0].filename));\n dataSet = new RemoteDataSet(inputSetURIs);\n this.marshaledDataRange = dataSet;\n this.rangeLength = dataRange.length;\n }\n \n if (dataPattern)\n {\n let uri = dataPattern;\n for (let i = 0; i < sliceCount; i++)\n {\n let sliceNum = i+1;\n let newURI = new URL(uri.replace('{slice}', sliceNum.toString()));\n this.localExecAllowedFiles['fetchData'].push(newURI.origin);\n }\n }\n \n // For allowed origins of the localexec worker. Only allow the origins (files in this case) in this list.\n for (let i = 0; i < inputSetFiles.length; i++)\n {\n // Coerce into a URI to handle OS specific path separators.\n const inputFileURI = new URL(`file://${inputSetFiles[i].filename}`);\n this.localExecAllowedFiles['fetchData'].push(inputFileURI.pathname);\n }\n \n // Save work function to disk if work function starts with data (ie not remote)\n if (this.workFunctionURI.startsWith('data:'))\n {\n const workFunctionFile = createTempFile('dcp-localExec-workFunction-XXXXXXXXX', 'js');\n const workFunction = await fetchURI(this.workFunctionURI);\n workFunctionFile.writeSync(workFunction);\n \n const workFunctionFileURL = new URL('file://' + workFunctionFile);\n this.workFunctionURI = workFunctionFileURL.href;\n this.localExecAllowedFiles['fetchWorkFunctions'].push(workFunctionFileURL.pathname);\n }\n else\n this.localExecAllowedFiles['fetchWorkFunctions'].push(new URL(this.workFunctionURI).origin);\n \n let encodedJobArgumentUris = [];\n if (this.jobArguments)\n {\n if (this.jobArguments instanceof RemoteDataPattern) /* Not supported */\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG')\n if (this.jobArguments instanceof RemoteDataSet) /* Entire set is RemoteDataSet */\n {\n this.jobArguments.forEach((e) =>\n {\n this.localExecAllowedFiles['fetchArguments'].push(new URL(e).origin);\n encodedJobArgumentUris.push(new URL(e));\n });\n }\n else\n {\n for (let i = 0; i < this.jobArguments.length; i++)\n {\n if (this.jobArguments[i] instanceof URL)\n {\n if (this.localExecAllowedFiles['fetchArguments'].indexOf(this.jobArguments[i].origin) === -1)\n this.localExecAllowedFiles['fetchArguments'].push(this.jobArguments[i].origin);\n encodedJobArgumentUris.push(this.jobArguments[i]);\n }\n else\n {\n if (this.jobArguments[i] instanceof RemoteDataSet) /* Member of set is RemoteDataSet */\n {\n this.jobArguments[i].forEach((e) =>\n {\n this.localExecAllowedFiles['fetchArguments'].push(new URL(e).origin);\n encodedJobArgumentUris.push(new URL(e));\n });\n }\n else /* Actual Value */\n {\n const localArgFile = createTempFile(`dcp-localExec-argument-${i}-XXXXXXXXX`, 'kvin');\n const localArgFileURI = new URL(`file://${localArgFile.filename}`);\n\n localArgFile.writeSync(JSON.stringify(kvinMarshal(this.jobArguments[i])));\n this.localExecAllowedFiles['fetchArguments'].push(localArgFileURI.pathname);\n encodedJobArgumentUris.push(localArgFileURI);\n }\n }\n } \n }\n }\n this.marshaledArguments = kvinMarshal(encodedJobArgumentUris);\n\n // Support for remote results with localExec.\n if (this.resultStorageDetails)\n {\n this.localExecAllowedFiles['sendResults'].push(this.resultStorageDetails.origin);\n }\n }\n \n return this.exec(...args).finally(() => {\n if (worker) {\n setTimeout(() => {\n // stop the worker\n worker.stop(true);\n }, 3000);\n }\n });\n }\n\n /**\n * Deploys the job to the scheduler.\n * @param {number | object} [slicePaymentOffer=compute.marketValue] - Amount\n * in DCC that the user is willing to pay per slice.\n * @param {Keystore} [paymentAccountKeystore=wallet.get] - An instance of the\n * Wallet API Keystore that's used as the payment account when executing the\n * job.\n * @param {object} [initialSliceProfile] - An object describing the cost the\n * user believes the average slice will incur.\n * @access public\n * @emits Job#accepted\n */\n async exec (slicePaymentOffer = (__webpack_require__(/*! ../compute */ \"./src/dcp-client/compute.js\").compute.marketValue), paymentAccountKeystore, initialSliceProfile)\n {\n if (this.connected)\n throw new Error('Exec called twice on the same job handle.');\n \n if (this.estimationSlices === Infinity)\n this.estimationSlices = null;\n else if (this.estimationSlices < 0)\n throw new Error('Incorrect value for estimationSlices; it can be an integer or Infinity!');\n \n if (this.tuning.kvin.speed || this.tuning.kvin.size)\n {\n tunedKvin = new kvin.KVIN();\n tunedKvin.tune = 'size';\n if(this.tuning.kvin.speed)\n tunedKvin.tune = 'speed';\n // If both size and speed are true, kvin will optimize based on speed\n if(this.tuning.kvin.speed && this.tuning.kvin.size)\n console.log('Slices and arguments are being uploaded with speed optimization.');\n }\n \n /* slight optimization to ensure we don't send requirements that will be ignored in the job submitter. Make a copy of the client specified requirements for this so that we dont magically override something they manually set */\n const _DEFAULT_REQUIREMENTS = JSON.parse(JSON.stringify(DEFAULT_REQUIREMENTS));\n removeBadRequirements(this.requirements, _DEFAULT_REQUIREMENTS);\n \n this.readyStateChange('exec');\n if ((typeof slicePaymentOffer === 'number') || (typeof slicePaymentOffer === 'object')\n || ((this.slicePaymentOffer === null || this.slicePaymentOffer === undefined) && typeof slicePaymentOffer === 'function'))\n this.setSlicePaymentOffer(slicePaymentOffer);\n if (typeof initialSliceProfile !== 'undefined')\n this.initialSliceProfile = initialSliceProfile;\n \n if (typeof paymentAccountKeystore !== 'undefined')\n {\n /** XXX @todo deprecate use of ethereum wallet objects */\n if (typeof paymentAccountKeystore === 'object' && paymentAccountKeystore.hasOwnProperty('_privKey'))\n {\n console.warn('* deprecated API * - job.exec invoked with ethereum wallet object as paymentAccountKeystore') /* /wg oct 2019 */\n paymentAccountKeystore = paymentAccountKeystore._privKey\n }\n /** XXX @todo deprecate use of private keys */\n if (wallet.isPrivateKey(paymentAccountKeystore))\n {\n console.warn('* deprecated API * - job.exec invoked with private key as paymentAccountKeystore') /* /wg dec 2019 */\n paymentAccountKeystore = await new wallet.Keystore(paymentAccountKeystore, '');\n }\n\n this.setPaymentAccountKeystore(paymentAccountKeystore)\n }\n \n if (this.paymentAccountKeystore)\n // Throws if they fail to unlock, we allow this since the keystore was set programmatically. \n await this.paymentAccountKeystore.unlock(undefined, parseFloat(dcpConfig.job.maxDeployTime));\n else\n {\n // If not set programmatically, we keep trying to get an unlocked keystore ... forever.\n let locked = true;\n let safety = 0; // no while loop shall go unguarded\n let ks;\n do\n {\n ks = null;\n // custom message for the browser modal to denote the purpose of keystore submission\n // let msg = `This application is requesting a keystore file to execute ${this.public.description || this.public.name || 'this job'}.<br><br> Please upload the corresponding keystore file. If you upload a keystore file which has been encrypted with a passphrase, the application will not be able to use it until it prompts for a passphrase and you enter it.`;\n let msg = `Upload a DCP bank account keystore<br> containing compute credits.`;\n try\n {\n ks = await wallet.get({ contextId: this.contextId, jobName: this.public.name, msg});\n }\n catch (e)\n {\n if (e.code !== ClientModal.CancelErrorCode) throw e;\n };\n if (ks)\n {\n try\n {\n await ks.unlock(undefined, parseFloat(dcpConfig.job.maxDeployTime));\n locked = false;\n }\n catch (e)\n {\n // prompt user again if user enters password incorrectly, exit modal otherwise\n if (e.code !== wallet.unlockFailErrorCode) throw e;\n }\n }\n if (safety++ > 1000) throw new Error('EINFINITY: job.exec tried wallet.get more than 1000 times.')\n } while (locked);\n this.setPaymentAccountKeystore(ks)\n }\n \n // We either have a valid keystore + password or we have rejected by this point.\n if (!this.slicePaymentOffer)\n throw new Error('A payment profile must be assigned before executing the job');\n else\n this.feeStructure = this.slicePaymentOffer.toFeeStructure(this.jobInputData.length);\n\n if (!this.address)\n {\n try\n {\n this.readyStateChange('init');\n await this.deployJob();\n const listenersPromise = this.addInitialEvents();\n const computeGroupsPromise = this.joinComputeGroups();\n let uploadSlicePromise;\n // if job data is by value then upload data to the scheduler in a staggered fashion\n if (Array.isArray(this.dataValues) && !this.marshaledDataValues)\n {\n this.readyStateChange('uploading');\n uploadSlicePromise = addSlices(this.dataValues, this.address, tunedKvin)\n .then(() => {\n debugging('slice-upload') && console.info(`970: slice data uploaded, closing job...`);\n return this.close();\n });\n }\n \n // await all promises for operations that can be done after the job is deployed\n await Promise.all([listenersPromise, computeGroupsPromise, uploadSlicePromise]);\n \n this.readyStateChange('deployed');\n this.emit('accepted', { job: this });\n }\n catch (error)\n {\n if (ON_BROWSER)\n await ClientModal.alert(error, { title: 'Failed to deploy job!' });\n throw error;\n }\n }\n else\n {\n // reconnecting to an old job\n await this.addInitialEvents();\n this.readyStateChange('reconnected');\n }\n\n this.connected = true;\n \n // start a timer to keep the node process alive until the results come back. Keep refreshing this timer until the results promise is resovled or rejected.\n let pendingResultsTimer;\n if (DCP_ENV.platform === 'nodejs')\n {\n pendingResultsTimer = setTimeout(() => {\n pendingResultsTimer.refresh();\n }, 1000 * 60 * 60) /* hour long timer */\n }\n\n return new Promise((resolve, reject) => {\n this.startEventBandaidWatchdog();\n \n const onComplete = () => {\n if (DCP_ENV.platform === 'nodejs')\n clearTimeout(pendingResultsTimer);\n this.stopEventBandaidWatchdog();\n resolve(this.results); \n };\n\n const onCancel = (event) => {\n /**\n * FIXME(DCP-1150): Remove this since normal cancel event is noisy\n * enough to not need stopped event too.\n */\n if (ON_BROWSER && !this.listeningForCancel)\n ClientModal.alert('More details in console...', { title: 'Job Canceled' });\n this.emit('cancel', event);\n\n let errorMsg = event.reason;\n if (event.error && event.error !== 'undefined')\n errorMsg = errorMsg +`\\n Recent error message: ${event.error.message}`\n \n if (DCP_ENV.platform === 'nodejs')\n clearTimeout(pendingResultsTimer); \n\n this.stopEventBandaidWatchdog();\n\n reject(new DCPError(errorMsg, event.code));\n };\n\n this.ee.once('stopped', async (stopEvent) => {\n // There is a chance the result submitter will emit finished > 1 time. Only handle it once.\n if (this.receivedStop)\n return;\n this.receivedStop = true;\n this.emit('stopped', stopEvent.runStatus);\n switch (stopEvent.runStatus) {\n case jobStatus.finished:\n if (this.collateResults)\n {\n let report = await this.getJobInfo();\n let allSliceNumbers = Array.from(Array(report.totalSlices)).map((e,i)=>i+1);\n let remainSliceNumbers = allSliceNumbers.filter((e) => !this.results.isAvailable(e));\n\n if (remainSliceNumbers.length)\n {\n const promises = remainSliceNumbers.map(sliceNumber => this.results.fetch([sliceNumber], true));\n await Promise.all(promises);\n }\n }\n\n this.emit('complete', this.results);\n onComplete();\n break;\n case jobStatus.cancelled:\n onCancel(stopEvent);\n break;\n default:\n /**\n * Asserting that we should never be able to reach here. The only\n * scheduler events that should trigger the Job's 'stopped' event\n * are jobStatus.cancelled, jobStatus.finished, and sliceStatus.paused.\n */\n reject(new Error(`Unknown event \"${stopEvent.runStatus}\" caused the job to be stopped.`));\n break;\n }\n });\n\n }).finally(() => {\n const handleErr = (e) => {\n console.error('Error while closing job connection:');\n console.error(e);\n }\n\n // Create an async IIFE to not block the promise chain\n (async () => {\n // delay to let last few events to be received\n await new Promise((resolve) => setTimeout(resolve, 1000));\n \n // close all of the connections so that we don't cause node processes to hang.\n this.closeDeployConnection();\n await this.eventSubscriber.close().catch(handleErr);\n await computeGroups.closeServiceConnection().catch((err) => {\n console.error('Warning: could not close compute groups service connection', err);\n });\n })();\n });\n }\n\n /**\n * Start the event-bandaid watchdog. Every [configurable] seconds, fetch\n * the job report and do appropriate things if the job is stopped.\n */\n startEventBandaidWatchdog()\n {\n if (this.eventBandaidHandle)\n this.stopEventBandaidWatchdog();\n\n this.eventBandaidHandle = setInterval(() => {\n this.eventBandaid();\n }, 60 * 1000);\n\n // if we are in a nodelike environment with unreffable timers, unref the\n // timer so it doesn't hold the process open. \n // \n // (n.b. while I understand that we _want_ the process to stay open while\n // the job lives, but this is Not The Way; the whole eventBandaid is a\n // temporary hack which will go away in time ~ER 2022-11-02)\n if (this.eventBandaidHandle.unref)\n this.eventBandaidHandle.unref();\n }\n\n /**\n * Stop and clean up the even-bandaid watchdog.\n */\n stopEventBandaidWatchdog()\n {\n if (this.eventBandaidHandle)\n clearInterval(this.eventBandaidHandle);\n\n this.eventBandaidHandle = false;\n }\n\n /**\n * Called on an interval, started by .exec and stopped by\n * onComplete/onCancel. Fetches the jobReport and verifies the job is\n * not in a terminal state; if the job is finished or cancelled, we\n * emit a synthetic internal stop event to trigger regular cleanup\n * (result retrieval, etc) and finalize the job handle.\n */\n eventBandaid()\n {\n this.getJobInfo()\n .then(jobInfo => {\n if ([jobStatus.finished, jobStatus.cancelled].includes(jobInfo.status))\n this.ee.emit('stopped', { runStatus: jobInfo.status });\n })\n .catch(error => {\n\n });\n }\n \n /**\n * job.addListeners(): Private function used to set up event listeners to the scheduler\n * before deploying the job.\n */\n async addInitialEvents ()\n {\n this.readyStateChange('listeners');\n\n // This is important: We need to flush the task queue before adding listeners\n // because we queue pending listeners by listening to the newListener event (in the constructor).\n // If we don't flush here, then the newListener events may fire after this function has run,\n // and the events won't be properly set up.\n await new Promise(resolve => setTimeout(resolve, 0));\n\n // @todo: Listen for an estimated cost, probably emit an \"estimated\" event when it comes in?\n // also @todo: Do the estimation task(s) on the scheduler and send an \"estimated\" event\n\n // Always listen to the stop event. It will resolve the work function promise, so is always needed.\n this.on('stop', (ev) => {this.ee.emit('stopped', ev)});\n\n // Connect listeners that were set up before exec\n if (this.desiredEvents.includes('result'))\n this.listeningForResults = true;\n await this.subscribeNewEvents(this.desiredEvents);\n\n // Connect listeners that are set up after exec\n this.on('newListener', (evt) => {\n if (evt === 'newListener' || this.desiredEvents.includes(evt))\n return;\n this.subscribeNewEvents([evt]);\n });\n \n // automatically add a listener for results if collateResults is on\n if (this.collateResults && !this.listeningForResults)\n this.on('result', () => {});\n\n debugging('dcp-client') && console.debug('subscribedEvents', this.desiredEvents);\n\n // If we have listeners for job.work, subscribe to custom events\n if (this.listenForCustomEvents)\n await this.subscribeCustomEvents();\n // Connect work event listeners that are set up after exec\n else\n this.work.on('newListener', () => this.subscribeCustomEvents());\n }\n \n /**\n * Subscribes to either reliable events or optional events. It is assumed that\n * any call to this function will include only new events.\n * @param {string[]} events \n */\n async subscribeNewEvents (events)\n {\n const reliableEvents = [];\n const optionalEvents = [];\n for (let eventName of events)\n {\n eventName = eventName.toLowerCase();\n if (this.eventTypes[eventName] && this.eventTypes[eventName].reliable)\n reliableEvents.push(eventName);\n else if (this.eventTypes[eventName] && !this.eventTypes[eventName].reliable)\n optionalEvents.push(eventName);\n else\n debugging('dcp-client') && console.debug(`Job handler has listener ${eventName} which isn't an event-router event.`);\n }\n if (debugging('dcp-client'))\n {\n console.debug('reliableEvents', reliableEvents);\n console.debug('optionalEvents', optionalEvents);\n }\n await this.eventSubscriber.subscribeManyEvents(reliableEvents, optionalEvents, { filter: { job: this.address } });\n }\n \n /**\n * Establishes listeners for worker events when requested by the client\n */\n async subscribeCustomEvents ()\n {\n if (!this.listeningForCustomEvents)\n await this.eventSubscriber.subscribeManyEvents([], ['custom'], { filter: { job: this.address } });\n this.listeningForCustomEvents = true\n }\n \n async joinComputeGroups ()\n {\n // localExec jobs are not entered in any compute group.\n if (!this.inLocalExec && this.computeGroups && this.computeGroups.length > 0)\n {\n this.readyStateChange('compute-groups');\n computeGroups.addRef(); // Just in case we're doing a Promise.all on multiple execs.\n\n // Add this job to its currently-defined compute groups (as well as public group, if included)\n let success;\n \n if (!Array.isArray(this.computeGroups)) \n throw new DCPError('Compute groups must be wrapped in an Array', 'DCPL-1101');\n\n for (let i = 0; i < this.computeGroups.length; i++)\n {\n let value = this.computeGroups[i];\n \n if (typeof value !== 'object')\n throw new DCPError(`This compute group: ${value[i]} must be an object`, 'DCPL-1102');\n \n if (value.joinKey && typeof value.joinKey !== 'string' && !(value.joinKey instanceof String))\n throw new DCPError(`This join key: ${value.joinKey} must be a string or a string literal`, 'DCPL-1103');\n else if (value.joinKeystore && !(value.joinKeystore instanceof wallet.Keystore))\n throw new DCPError(`This join Keystore: ${value.joinKeystore} must be an instance of wallet.Keystore`, 'DCPL-1104');\n else if (!value.joinKey && !value.joinKeystore)\n throw new DCPError('Compute group must contain a joinKey or a joinKeystore', 'DCPL-1105');\n }\n \n try\n {\n const cgPayload = await computeGroups.addJobToGroups(this.address, this.computeGroups);\n success = true; // To support older version of CG service where addJobToGroups had void/undefined return.\n if (cgPayload) success = cgPayload.success;\n debugging('dcp-client') && console.debug('job/index: addJobToGroups cgPayload:', cgPayload ? cgPayload : 'cgPayload is not defined; probably from legacy CG service.');\n }\n catch (e)\n {\n debugging('dcp-client') && console.debug('job/index: addJobToGroups threw exception:', e);\n success = false;\n }\n\n computeGroups.closeServiceConnection().catch((err) => {\n console.error('Warning: could not close compute groups service connection', err)\n });\n\n /* Could not put the job in any compute group, even though the user wanted it to run. Cancel the job. */\n if (!success)\n {\n await this.cancel('compute-groups::Unable to join any compute groups');\n throw new DCPError(`Access Denied::Failed to add job ${this.address} to any of the desired compute groups`, 'DCPL-1100');\n }\n }\n }\n \n /**\n * Takes result events as input, stores the result and fires off\n * events on the job handle as required. (result, duplicate-result)\n *\n * @param {object} ev - the event recieved from protocol.listen('/results/0xThisGenAdr')\n */\n async handleResult (ev)\n {\n if (this.results === null)\n // This should never happen - the onResult event should only be established/called\n // in addListeners which should also initialize the internal results array\n throw new Error('Job.onResult was invoked before initializing internal results');\n \n const { result: _result, time } = ev.result;\n debugging('dcp-client') && console.debug('handleResult', _result);\n let result = await fetchURI(_result);\n\n if (this.results.isAvailable(ev.sliceNumber))\n {\n const changed = JSON.stringify(this.results[ev.sliceNumber]) !== JSON.stringify(result);\n this.emit('duplicate-result', { sliceNumber: ev.sliceNumber, changed });\n }\n \n this.results.newResult(result, ev.sliceNumber - 1);\n }\n \n /**\n * Receives status events from the scheduler, updates the local status object\n * and emits a 'status' event\n *\n * @param {object} ev - the status event received from\n * protocol.listen('/status/0xThisGenAdr')\n * @param {boolean} emitStatus - value indicating whether or not the status\n * event should be emitted\n */\n handleStatus ({ runStatus, total, distributed, computed }, emitStatus = true)\n {\n Object.assign(this.status, {\n runStatus,\n total,\n distributed,\n computed,\n });\n\n if (emitStatus)\n this.emit('status', { ...this.status, job: this.address });\n }\n \n /**\n * Sends a request to the scheduler to deploy the job.\n */\n async deployJob ()\n {\n var moduleDependencies; \n \n /* Send sideloader bundle to the package server */\n if (DCP_ENV.platform === 'nodejs' && this.dependencies.length)\n {\n try\n {\n let { pkg, unresolved } = await this._publishLocalModules();\n\n moduleDependencies = unresolved;\n if (pkg)\n moduleDependencies.push(pkg.name + '/' + sideloaderModuleIdentifier); \n }\n catch(error)\n {\n throw new DCPError(`Error trying to communicate with package manager server: ${error}`);\n }\n }\n else\n moduleDependencies = this.dependencies;\n \n this.readyStateChange('preauth');\n\n const adhocId = this.uuid.slice(this.uuid.length - 6, this.uuid.length);\n const schedId = await dcpConfig.scheduler.identity;\n // The following check is needed for when using dcp-rtlink and loading the config through source, instead of using the dcp-client bundle\n let schedIdAddress = schedId;\n if(schedId.address)\n schedIdAddress = schedId.address;\n this.identityKeystore = await wallet.getId();\n const preauthToken = await bankUtil.preAuthorizePayment(schedIdAddress, this.maxDeployPayment, this.paymentAccountKeystore);\n const { dataRange, dataValues, dataPattern, sliceCount } = marshalInputData(this.jobInputData);\n if(dataValues)\n this.dataValues = dataValues;\n\n this.readyStateChange('deploying');\n\n /* Payload format is documented in scheduler-v4/libexec/job-submit/operations/submit.js */\n const submitPayload = {\n owner: this.identityKeystore.address,\n paymentAccount: this.paymentAccountKeystore.address,\n priority: 0, // @nyi\n\n workFunctionURI: this.workFunctionURI,\n uuid: this.uuid,\n mvMultSlicePayment: Number(this.feeStructure.marketValue) || 0, // @todo: improve feeStructure internals to better reflect v4\n absoluteSlicePayment: Number(this.feeStructure.maxPerRequest) || 0,\n requirePath: this.requirePath,\n dependencies: moduleDependencies,\n requirements: this.requirements, /* capex */\n localExec: this.inLocalExec,\n force100pctCPUDensity: this.force100pctCPUDensity,\n estimationSlices: this.estimationSlices,\n greedyEstimation: this.greedyEstimation,\n workerConsole: this.workerConsole,\n isCI: this.isCI,\n\n description: this.public.description || 'Discreetly making the world smarter',\n name: this.public.name || 'Ad-Hoc Job' + adhocId,\n link: this.public.link || '',\n\n preauthToken, // XXXwg/er @todo: validate this after fleshing out the stub(s)\n\n resultStorageType: this.resultStorageType, // @todo: implement other result types\n resultStorageDetails: this.resultStorageDetails, // Content depends on resultStorageType\n resultStorageParams: this.resultStorageParams, // Post params for off-prem storage\n dataRange,\n dataPattern,\n sliceCount,\n marshaledDataValues: this.marshaledDataValues,\n rangeLength: this.rangeLength\n };\n\n // Check if dataRange or dataPattern input is already marshaled\n if (this.marshaledDataRange)\n submitPayload.dataRange = this.marshaledDataRange;\n\n /* Determine composition of argument set and build payload */\n if (this.marshaledArguments)\n submitPayload.marshaledArguments = this.marshaledArguments;\n else if (this.jobArguments)\n {\n if (this.jobArguments instanceof RemoteDataPattern) /* Not supported */\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG')\n if (this.jobArguments instanceof RemoteDataSet) /* Entire set is RemoteDataSet */\n this.jobArguments = this.jobArguments.map((e) => new URL(e));\n\n submitPayload.marshaledArguments = kvin.marshal(encodeJobValueList(this.jobArguments, 'jobArguments'));\n }\n\n // XXXpfr Excellent tracing.\n if (debugging('dcp-client'))\n {\n const { dumpObject } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n dumpObject(submitPayload, 'Submit: Job Index: submitPayload', 256);\n }\n debugging('dcp-client') && console.debug('submitPayload.marshaledArguments', JSON.stringify(submitPayload.marshaledArguments).length, JSON.stringify(submitPayload).length);\n\n // Deploy the job! If we get an error, try again a few times until threshold of errors is reached, then actually throw it\n let deployed\n let deployAttempts = 0;\n while (deployAttempts++ < (dcpConfig.job.deployAttempts || 10))\n {\n try\n {\n deployed = await this.useDeployConnection('submit', submitPayload, this.identityKeystore);\n break;\n }\n catch (e)\n {\n if (deployAttempts < 10)\n debugging('dcp-client') && console.debug('Error when trying to deploy job, trying again', e);\n else\n throw e;\n }\n }\n\n if (!deployed.success)\n {\n // close all of the connections so that we don't cause node processes to hang.\n const handleErr = (e) => {\n console.error('Error while closing job connection:');\n console.error(e);\n };\n \n this.closeDeployConnection();\n this.eventSubscriber.close().catch(handleErr);\n computeGroups.closeServiceConnection().catch(handleErr);\n \n // Yes, it is possible for deployed or deployed.payload to be undefined.\n if (deployed.payload)\n {\n if (deployed.payload.code === 'ENOTFOUND')\n throw new DCPError(`Failed to submit job to scheduler. Account: ${submitPayload.paymentAccount} was not found or does not have sufficient balance (${deployed.payload.info.deployCost} DCCs needed to deploy this job)`, deployed.payload); \n throw new DCPError('Failed to submit job to scheduler', deployed.payload);\n }\n throw new DCPError('Failed to submit job to scheduler (no payload)', deployed ? deployed : '');\n }\n\n debugging('dcp-client') && console.debug('After Deploy', JSON.stringify(deployed));\n\n this.address = deployed.payload.job;\n this.deployCost = deployed.payload.deployCost;\n\n if (!this.status)\n this.status = {\n runStatus: null,\n total: 0,\n computed: 0,\n distributed: 0,\n };\n \n this.status.runStatus = deployed.payload.status;\n this.status.total = deployed.payload.lastSliceNumber;\n this.running = true;\n }\n \n /** close an open job to indicate we are done adding data so it is okay to finish\n * the job at the appropriate time\n */\n close ()\n {\n return this.useDeployConnection('closeJob', {\n job: this.id,\n });\n }\n \n /** Use the connection to job submit service. Will open a new connection if one does not exist,\n * and close the connection if it is idle for more than 10 seconds (tuneable).\n */\n useDeployConnection(...args)\n {\n if (!this.useDeployConnection.uses)\n this.useDeployConnection.uses = 0;\n this.useDeployConnection.uses++;\n if (!this.deployConnection)\n {\n debugging('deploy-connection') && console.info(`1453: making a new deployConnection...`)\n this.deployConnection = new protocolV4.Connection(dcpConfig.scheduler.services.jobSubmit); \n this.deployConnection.on('close', () => { this.deployConnection = null; });\n }\n if (this.deployConnectionTimeout)\n clearTimeout(this.deployConnectionTimeout);\n\n debugging('deploy-connection') && console.info(`1460: sending ${args[0]} request...`);\n const deployPromise = this.deployConnection.send(...args);\n \n deployPromise.finally(() => {\n this.useDeployConnection.uses--;\n\n debugging('deploy-connection') && console.info(`1462: deployConnection done ${args[0]} request, connection uses is ${this.useDeployConnection.uses}`)\n\n this.deployConnectionTimeout = setTimeout(() => {\n if (this.useDeployConnection.uses === 0 && this.deployConnection)\n {\n debugging('deploy-connection') && console.info(`1469: done with deployConn, closing...`);\n // if we're done w/ the connection, then remove its cleanup\n // function, close it, and clean up manually to make room for a\n // new conn when needed (ie, don't leave the closing conn where\n // someone could accidentally pick it up)\n this.deployConnection.removeAllListeners('close');\n this.deployConnection.close();\n this.deployConnection = null;\n }\n }, (dcpConfig.job.deployCloseTimeout || 10 * 1000));\n if (!ON_BROWSER)\n this.deployConnectionTimeout.unref();\n }); \n \n return deployPromise;\n }\n \n /**\n * Close the connection to the job submit (if it exists), and clear the close timeout (if needed).\n */\n closeDeployConnection()\n {\n if (this.deployConnection)\n this.deployConnection.close();\n if (this.deployConnectionTimeout)\n clearTimeout(this.deployConnectionTimeout);\n }\n}\n\n/** \n * Encode a value list for transmission to the job-submit daemon. This could be either job arguments\n * or the slice input set, if the input set was an Array-like object.\n *\n * @param {ArrayLike} valueList - The list of values to encode\n * @returns {Array<URL|{string: string, method:string, MIMEType: string }>} - URL or object with 3 properties:\n * string - serialization of value\n * method - how value was serialized (e.g. 'kvin', 'json')\n * MIMEType - MIME information corresponding to method (e.g. 'application/kvin', 'application/json')\n */\nfunction encodeJobValueList(valueList, valueKind)\n{\n var list = [];\n\n /*\n * We need to handle several different styles of datasets, and create the output array accordingly.\n *\n * 1. instance of RemoteDataSet or RemoteDataPattern => apply new URI(?)\n * 2. an Array-like objects => apply serializeJobValue\n * Values sent to the scheduler in payload are either URL or the result of calling serializeJobValue.\n */\n\n if (typeof valueList === 'undefined' || (typeof valueList === 'object' && valueList.length === 0))\n return list; /* empty set */\n\n if (typeof valueList !== 'object' || !valueList.hasOwnProperty('length'))\n throw new Error('value list must be an Array-like object');\n\n for (let i = 0; i < valueList.length; i++) /* Set is composed of values from potentially varying sources */\n {\n let value = valueList[i];\n if (value instanceof RemoteDataSet)\n value.forEach((el) => list.push(new URL(el)));\n else if (value instanceof RemoteDataPattern)\n {\n if (valueKind === jobValueKind.jobArguments)\n throw new DCPError('Cannot use RemoteDataPattern as work function arguments', 'EBADARG');\n else\n {\n let uri = valueList['pattern'];\n for (let sliceNum = 1; sliceNum <= valueList['sliceCount']; sliceNum++)\n list.push(new URL(uri.replace('{slice}', sliceNum)))\n }\n }\n else if (value instanceof RemoteValue)\n list.push(serializeJobValue(value.href));\n else\n list.push(serializeJobValue(value));\n }\n\n return list;\n} \n\n/**\n * Depending on the shape of the job's data, resolve it into a RangeObject, a\n * Pattern, or a values array, and return it in the appropriate property.\n *\n * @param {any} data Job's input data\n * @return {MarshaledInputData} An object with one of the following properties set:\n * - dataValues: job input is an array of arbitrary values \n * - dataPattern: job input is a URI pattern \n * - dataRange: job input is a RangeObject (and/or friends)\n */\nfunction marshalInputData (data)\n{\n if (!(data instanceof Object || data instanceof SuperRangeObject))\n throw new TypeError(`Invalid job data type: ${typeof data}`);\n\n /**\n * @type {MarshaledInputData}\n */\n const marshalledInputData = {};\n\n // TODO(wesgarland): Make this more robust.\n if (data instanceof SuperRangeObject ||\n (data.hasOwnProperty('ranges') && data.ranges instanceof MultiRangeObject) ||\n (data.hasOwnProperty('start') && data.hasOwnProperty('end')))\n marshalledInputData.dataRange = data;\n else if (Array.isArray(data))\n marshalledInputData.dataValues = data;\n else if (data instanceof URL || data instanceof DcpURL)\n marshalledInputData.dataPattern = String(data);\n else if(data instanceof RemoteDataSet)\n marshalledInputData.dataValues = data.map(e => new URL(e));\n else if(data instanceof RemoteDataPattern)\n {\n marshalledInputData.dataPattern = data['pattern'];\n marshalledInputData.sliceCount = data['sliceCount'];\n }\n\n debugging('job') && console.debug('marshalledInputData:', marshalledInputData);\n return marshalledInputData;\n}\n\n/**\n * marshal the value using kvin or instance of the kvin (tunedKvin)\n * tunedKvin is defined if job.tuning.kvin is specified.\n *\n * @param {any} value \n * @return {object} A marshaled object\n * \n */\nfunction kvinMarshal (value) {\n if (tunedKvin)\n return tunedKvin.marshal(value);\n\n return kvin.marshal(value);\n}\n\n\n\nexports.Job = Job;\nexports.SlicePaymentOffer = SlicePaymentOffer;\nexports.ResultHandle = ResultHandle;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/job/index.js?");
4231
4275
 
4232
4276
  /***/ }),
4233
4277
 
@@ -4248,7 +4292,7 @@ eval("/**\n * @file node-modules.js Node-specific support for sen
4248
4292
  \*********************************************/
4249
4293
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4250
4294
 
4251
- eval("/**\n * @file job/result-handle.js\n * @author Ryan Rossiter, ryan@kingsds.network\n * Robert Mirandola, <robert@kingsds.network>\n * @date June 2020\n * July 2022\n *\n * The ResultHandle acts as a proxy for a job's results, querying\n * internal results when available or the scheduler when the results\n * are not available locally.\n */\n\nconst { rehydrateRange, RangeObject, SparseRangeObject } = __webpack_require__(/*! ../range-object */ \"./src/dcp-client/range-object.js\");\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst { fetchURI } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');\n\n// Array methods that don't modify the array, will be applied to the ResultHandle's values object\nconst RH_ARRAY_METHODS = [\n 'slice', 'filter', 'concat', 'find', 'findIndex', 'indexOf', 'map', 'reduce', 'includes', 'toString', 'forEach'\n];\n\n/**\n * This class represents a handle on a job's results. It can be used for accessing the job's results, or for querying the scheduler to fetch results.\n * In addition to the properties and methods of this class, the following standard array methods are also available for accessing the available results: `slice`, `filter`, `concat`, `find`, `findIndex`, `indexOf`, `map`, `reduce`, `includes`, `toString`, and `forEach`.\n * The results can also be accessed by index (`results[i]`), but an error will be thrown if the result for that index is not yet available.\n * @access public\n */\nclass ResultHandle extends Array\n{\n constructor(job)\n {\n super();\n\n const { Job } = __webpack_require__(/*! . */ \"./src/dcp-client/job/index.js\");\n if (!(job instanceof Job))\n throw new TypeError('ResultHandle must be constructed from a Job object');\n\n /**\n * The length of the available results array.\n * @type {number}\n * @access public\n */\n this.length = 0; // overridden by the proxy, here for JSDoc purposes\n\n this.job = job;\n this.realKeys = job.jobInputData;\n this.realValues = [];\n this.valuesAvailable = [];\n\n return new Proxy(this, {\n get: (target, name) => {\n if ((typeof name === 'string' || typeof name === 'number') && Number.isInteger(parseFloat(name)))\n {\n let i = parseFloat(name);\n if (target.isAvailable(i))\n return target.realValues[i];\n else\n throw new Error(`Result ${i} is not available. It has either not been computed or you need to fetch it.`);\n } \n else if (name === 'length')\n return target.getLength();\n else if (name === 'constructor')\n return Array.constructor;\n else if (RH_ARRAY_METHODS.includes(name))\n {\n let values = target.values();\n return values[name].bind(values);\n }\n else\n {\n // only return methods on this class, don't allow access\n // to array methods other than the allowlisted ones\n let p = target.__proto__[name];\n if (typeof p === 'function') {\n return p.bind(target);\n } else {\n return p;\n }\n }\n }\n });\n }\n\n toJSON ()\n {\n return JSON.stringify(this.values());\n }\n\n isAvailable(index)\n {\n return this.valuesAvailable[index];\n }\n\n reset() {\n // quickly empty the realValues array\n this.realValues.length = this.valuesAvailable.length = 0;\n this.realValues.length = this.valuesAvailable.length =this.realKeys.length;\n }\n\n /**\n * Returns an array of input values. Will only return input values that have a completed result available.\n * @access public\n * @returns {Array<*>}\n */\n keys() {\n // Keys can be a RangeObject, faster to iterate over valuesAvailable\n return this.valuesAvailable.reduce((keysList, valueAvailable, index) => {\n if (valueAvailable) keysList.push(this.realKeys[index]);\n return keysList;\n }, []);\n }\n\n /**\n * Returns an array of results. Will only return results that have been received from the scheduler, if only one result is complete the array will contain one value.\n * @access public\n * @returns {Array<*>}\n */\n values() {\n return this.realValues.filter((v, i) => this.isAvailable(i));\n }\n\n // Aliased as length in proxy, can't use getter because Array.length is not configurable\n getLength() {\n return this.valuesAvailable.reduce((n, v) => (n + (v ? 1 : 0)), 0);\n }\n \n [customInspectSymbol]() /* overrides the output when console.log(result) is called */\n { \n return this.values(); \n }\n\n [Symbol.iterator]() {\n let index = 0;\n let values = this.values(); // use available values\n\n return {\n next: () => ({\n value: values[index++],\n done: index > values.length\n })\n };\n }\n\n /** \n * Returns an array of [input, output] pairs, in sliceNumber order.\n * Return value is undefined if the input is not an ES5 primitive.\n * @access public\n * @returns {Array<*>}\n */\n entries() {\n return this.valuesAvailable.reduce((keyValuePairs, valueAvailable, index) => {\n if (valueAvailable) keyValuePairs.push([\n String(this.realKeys[index]),\n this.realValues[index],\n ]);\n return keyValuePairs;\n }, []);\n }\n\n /** \n * Returns an Object associating input and output values where the inputs are ES5 primitive types.\n * Return value is undefined if the input is not an ES5 primitive.\n * @access public\n * @returns {object}\n */\n fromEntries() {\n return this.entries().reduce((o, [k, v]) => {\n o[k] = v; return o;\n }, {});\n }\n\n /** \n * Return the nth input value/input vector \n * @access public\n * @param {number} n Index in the input set to return the value for.\n * @returns {*} Input set value\n */\n key(n) {\n return this.realKeys[n];\n }\n\n /** \n * Return the value corresponding to the provided key \n * @access public\n * @param {*} key Corresponds to a value in the job's input set.\n * @returns {*} Result corresponding to the input value\n */\n lookupValue(key) {\n // use keys instead of _keys so we only lookup on available results\n let ind = this.keys().indexOf(key);\n if (ind === -1)\n throw new DCPError('Argument passed into the function was not found in input set', 'ERANGE');\n\n return this.values()[ind];\n }\n\n /**\n * Sends request to scheduler to fetch results, the retrieved results will be populated on this object.\n * @param {RangeObject} [rangeObject] - range object to query results\n * @param {string} [emitEvents] - if truthy, emits a `result` event for new results as they are added to the handle, and if set to 'all' emits 'resultUpdated' for duplicate results \n * @access public\n * @emits Job#resultsUpdated\n */\n async fetch(rangeObject, emitEvents) {\n const range = rangeObject && rehydrateRange(rangeObject);\n\n const job = this.job;\n \n // Fetch any existing results\n let ks = await wallet.getId();\n const conn = new protocolV4.Connection(dcpConfig.scheduler.services.resultSubmitter.location, ks);\n\n const { success, payload } = await conn.send('fetchResult', {\n job: job.id,\n owner: job.paymentAccountKeystore.address,\n range,\n }, job.paymentAccountKeystore);\n\n // Unpack results, using fetchURI to decode/fetch result URIs\n await Promise.all(payload.map(async r => {\n if (!r) {\n console.warn(`ResultHandle.fetch: Received result was not defined (${r}), ignoring...`);\n return;\n }\n\n const decodedValue = decodeURIComponent(r.value)\n try\n {\n this.realValues[r.slice - 1] = await fetchURI(decodedValue, dcpConfig.scheduler.location.origin); \n } catch (error)\n {\n // ensure the error is an actual URI parsing error and not due actual problems\n if (error.code === 'ERR_INVALID_URL')\n this.realValues[r.slice - 1] = decodedValue;\n else\n return console.warn('Error in ResultHandle.fetch():', error.message);\n }\n\n if (emitEvents)\n {\n if (!this.valuesAvailable[r.slice - 1])\n this.job.emit('result', { sliceNumber: r.slice, result: this.realValues[r.slice] });\n else if (emitEvents === 'all')\n {\n this.job.emit('resultsUpdated', { sliceNumber: r.slice }); \n this.job.emit('result', { sliceNumber: r.slice, result: this.realValues[r.slice] });\n }\n }\n this.valuesAvailable[r.slice - 1] = true;\n }));\n await conn.close(null, true);\n }\n \n /**\n * Handle adding a new result\n */\n newResult(result, index)\n {\n if (this.valuesAvailable[index])\n {\n this.realValues[index] = result;\n this.job.emit('resultsUpdated', { sliceNumber: index + 1 }); \n }\n else\n {\n this.valuesAvailable[index] = true;\n this.realValues[index] = result;\n this.job.emit('result', { job: this.job.address, sliceNumber: index + 1, result });\n }\n }\n \n list(rangeObject) {\n let range = rangeObject && rehydrateRange(rangeObject);\n if (!range) range = new RangeObject(1, this.valuesAvailable.length);\n let sparseObjects = [];\n let min = range.start + range.step;\n let goodRange = false;\n let step = range.step;\n for (let i = range.start; i <= range.end; i+= step)\n {\n if (!goodRange && this.isAvailable(i - 1))\n {\n min = i\n goodRange = true;\n }\n if (goodRange && !this.isAvailable(i - 1))\n {\n if (min !== i-step)\n sparseObjects.push(new RangeObject(min, i-step, step))\n else\n sparseObjects.push(new RangeObject(min, i-step))\n goodRange = false;\n }\n }\n if (goodRange)\n {\n if (min !== range.end)\n sparseObjects.push(new RangeObject(min, range.end, step))\n else\n sparseObjects.push(new RangeObject(min, range.end))\n }\n\n try\n {\n return new SparseRangeObject(...sparseObjects);\n }\n catch\n {\n return [];\n }\n\n }\n\n async delete(rangeObject) {\n var range;\n try\n {\n range = rehydrateRange(rangeObject);\n }\n catch\n {\n range = 'all';\n }\n \n const job = this.job;\n let ks = await wallet.getId();\n const conn = new protocolV4.Connection(dcpConfig.scheduler.services.resultSubmitter.location, ks);\n \n const { success, payload } = await conn.send('deleteResult', {\n job: job.id,\n owner: job.paymentAccountKeystore.address,\n range,\n }, job.paymentAccountKeystore);\n \n if (!success)\n throw new Error('ResultHandle.delete: Could not delete slices from scheduler');\n \n await Promise.all(payload.map(r => {\n if (!r) {\n console.warn(`ResultHandle.delete: Received result was not defined (${r}), ignoring...`);\n return;\n }\n this.valuesAvailable[r - 1] = false;\n delete this.realValues[r - 1];\n }));\n \n await conn.close(null, true);\n return payload;\n }\n\n async stat(rangeObject) {\n const range = rehydrateRange(rangeObject);\n throw new Error(\"ResultHandle.stat not implemented\");\n }\n}\n\nObject.assign(exports, {\n ResultHandle,\n});\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/job/result-handle.js?");
4295
+ eval("/**\n * @file job/result-handle.js\n * @author Ryan Rossiter, ryan@kingsds.network\n * Robert Mirandola, <robert@kingsds.network>\n * @date June 2020\n * July 2022\n *\n * The ResultHandle acts as a proxy for a job's results, querying\n * internal results when available or the scheduler when the results\n * are not available locally.\n */\n\nconst { rehydrateRange, RangeObject, SparseRangeObject } = __webpack_require__(/*! ../range-object */ \"./src/dcp-client/range-object.js\");\nconst protocolV4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst wallet = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst { fetchURI } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');\n\n// Array methods that don't modify the array, will be applied to the ResultHandle's values object\nconst RH_ARRAY_METHODS = [\n 'slice', 'filter', 'concat', 'find', 'findIndex', 'indexOf', 'map', 'reduce', 'includes', 'toString', 'forEach'\n];\n\n/**\n * This class represents a handle on a job's results. It can be used for accessing the job's results, or for querying the scheduler to fetch results.\n * In addition to the properties and methods of this class, the following standard array methods are also available for accessing the available results: `slice`, `filter`, `concat`, `find`, `findIndex`, `indexOf`, `map`, `reduce`, `includes`, `toString`, and `forEach`.\n * The results can also be accessed by index (`results[i]`), but an error will be thrown if the result for that index is not yet available.\n * @access public\n */\nclass ResultHandle extends Array\n{\n constructor(job)\n {\n super();\n\n const { Job } = __webpack_require__(/*! . */ \"./src/dcp-client/job/index.js\");\n if (!(job instanceof Job))\n throw new TypeError('ResultHandle must be constructed from a Job object');\n\n /**\n * The length of the available results array.\n * @type {number}\n * @access public\n */\n this.length = 0; // overridden by the proxy, here for JSDoc purposes\n\n this.job = job;\n this.realKeys = job.jobInputData;\n this.realValues = [];\n this.valuesAvailable = [];\n\n return new Proxy(this, {\n get: (target, name) => {\n if ((typeof name === 'string' || typeof name === 'number') && Number.isInteger(parseFloat(name)))\n {\n let i = parseFloat(name);\n if (target.isAvailable(i))\n return target.realValues[i];\n else\n throw new Error(`Result ${i} is not available. It has either not been computed or you need to fetch it.`);\n } \n else if (name === 'length')\n return target.getLength();\n else if (name === 'constructor')\n return Array.constructor;\n else if (RH_ARRAY_METHODS.includes(name))\n {\n let values = target.values();\n return values[name].bind(values);\n }\n else\n {\n // only return methods on this class, don't allow access\n // to array methods other than the allowlisted ones\n let p = target.__proto__[name];\n if (typeof p === 'function') {\n return p.bind(target);\n } else {\n return p;\n }\n }\n }\n });\n }\n\n toJSON ()\n {\n return JSON.stringify(this.values());\n }\n\n isAvailable(index)\n {\n return this.valuesAvailable[index];\n }\n\n reset() {\n // quickly empty the realValues array\n this.realValues.length = this.valuesAvailable.length = 0;\n this.realValues.length = this.valuesAvailable.length =this.realKeys.length;\n }\n\n /**\n * Returns an array of input values. Will only return input values that have a completed result available.\n * @access public\n * @returns {Array<*>}\n */\n keys() {\n // Keys can be a RangeObject, faster to iterate over valuesAvailable\n return this.valuesAvailable.reduce((keysList, valueAvailable, index) => {\n if (valueAvailable) keysList.push(this.realKeys[index]);\n return keysList;\n }, []);\n }\n\n /**\n * Returns an array of results. Will only return results that have been received from the scheduler, if only one result is complete the array will contain one value.\n * @access public\n * @returns {Array<*>}\n */\n values() {\n return this.realValues.filter((v, i) => this.isAvailable(i));\n }\n\n // Aliased as length in proxy, can't use getter because Array.length is not configurable\n getLength() {\n return this.valuesAvailable.reduce((n, v) => (n + (v ? 1 : 0)), 0);\n }\n \n [customInspectSymbol]() /* overrides the output when console.log(result) is called */\n { \n return this.values(); \n }\n\n [Symbol.iterator]() {\n let index = 0;\n let values = this.values(); // use available values\n\n return {\n next: () => ({\n value: values[index++],\n done: index > values.length\n })\n };\n }\n\n /** \n * Returns an array of [input, output] pairs, in sliceNumber order.\n * Return value is undefined if the input is not an ES5 primitive.\n * @access public\n * @returns {Array<*>}\n */\n entries() {\n return this.valuesAvailable.reduce((keyValuePairs, valueAvailable, index) => {\n if (valueAvailable) keyValuePairs.push([\n String(this.realKeys[index]),\n this.realValues[index],\n ]);\n return keyValuePairs;\n }, []);\n }\n\n /** \n * Returns an Object associating input and output values where the inputs are ES5 primitive types.\n * Return value is undefined if the input is not an ES5 primitive.\n * @access public\n * @returns {object}\n */\n fromEntries() {\n return this.entries().reduce((o, [k, v]) => {\n o[k] = v; return o;\n }, {});\n }\n\n /** \n * Return the nth input value/input vector \n * @access public\n * @param {number} n Index in the input set to return the value for.\n * @returns {*} Input set value\n */\n key(n) {\n return this.realKeys[n];\n }\n\n /** \n * Return the value corresponding to the provided key \n * @access public\n * @param {*} key Corresponds to a value in the job's input set.\n * @returns {*} Result corresponding to the input value\n */\n lookupValue(key) {\n // use keys instead of _keys so we only lookup on available results\n let ind = this.keys().indexOf(key);\n if (ind === -1)\n throw new DCPError('Argument passed into the function was not found in input set', 'ERANGE');\n\n return this.values()[ind];\n }\n\n /**\n * Sends request to scheduler to fetch results, the retrieved results will be populated on this object.\n * @param {RangeObject} [rangeObject] - range object to query results\n * @param {string} [emitEvents] - if truthy, emits a `result` event for new results as they are added to the handle, and if set to 'all' emits 'resultUpdated' for duplicate results \n * @access public\n * @emits Job#resultsUpdated\n */\n async fetch(rangeObject, emitEvents) {\n const range = rangeObject && rehydrateRange(rangeObject);\n\n const job = this.job;\n \n // Fetch any existing results\n let ks = await wallet.getId();\n const conn = new protocolV4.Connection(dcpConfig.scheduler.services.resultSubmitter.location, ks);\n\n const { success, payload } = await conn.send('fetchResult', {\n job: job.id,\n owner: job.paymentAccountKeystore.address,\n range,\n }, job.paymentAccountKeystore);\n\n // Unpack results, using fetchURI to decode/fetch result URIs\n await Promise.all(payload.map(async r => {\n if (!r) {\n console.warn(`ResultHandle.fetch: Received result was not defined (${r}), ignoring...`);\n return;\n }\n\n const decodedValue = decodeURIComponent(r.value)\n try\n {\n this.realValues[r.slice - 1] = await fetchURI(decodedValue, dcpConfig.scheduler.location.origin); \n } catch (error)\n {\n // ensure the error is an actual URI parsing error and not due actual problems\n if (error instanceof TypeError)\n this.realValues[r.slice - 1] = decodedValue;\n else\n return console.warn('Error in ResultHandle.fetch():', error.message);\n }\n\n if (emitEvents)\n {\n if (!this.valuesAvailable[r.slice - 1])\n this.job.emit('result', { sliceNumber: r.slice, result: this.realValues[r.slice] });\n else if (emitEvents === 'all')\n {\n this.job.emit('resultsUpdated', { sliceNumber: r.slice }); \n this.job.emit('result', { sliceNumber: r.slice, result: this.realValues[r.slice] });\n }\n }\n this.valuesAvailable[r.slice - 1] = true;\n }));\n await conn.close(null, true);\n }\n \n /**\n * Handle adding a new result\n */\n newResult(result, index)\n {\n if (this.valuesAvailable[index])\n {\n this.realValues[index] = result;\n this.job.emit('resultsUpdated', { sliceNumber: index + 1 }); \n }\n else\n {\n this.valuesAvailable[index] = true;\n this.realValues[index] = result;\n this.job.emit('result', { job: this.job.address, sliceNumber: index + 1, result });\n }\n }\n \n list(rangeObject) {\n let range = rangeObject && rehydrateRange(rangeObject);\n if (!range) range = new RangeObject(1, this.valuesAvailable.length);\n let sparseObjects = [];\n let min = range.start + range.step;\n let goodRange = false;\n let step = range.step;\n for (let i = range.start; i <= range.end; i+= step)\n {\n if (!goodRange && this.isAvailable(i - 1))\n {\n min = i\n goodRange = true;\n }\n if (goodRange && !this.isAvailable(i - 1))\n {\n if (min !== i-step)\n sparseObjects.push(new RangeObject(min, i-step, step))\n else\n sparseObjects.push(new RangeObject(min, i-step))\n goodRange = false;\n }\n }\n if (goodRange)\n {\n if (min !== range.end)\n sparseObjects.push(new RangeObject(min, range.end, step))\n else\n sparseObjects.push(new RangeObject(min, range.end))\n }\n\n try\n {\n return new SparseRangeObject(...sparseObjects);\n }\n catch\n {\n return [];\n }\n\n }\n\n async delete(rangeObject) {\n var range;\n try\n {\n range = rehydrateRange(rangeObject);\n }\n catch\n {\n range = 'all';\n }\n \n const job = this.job;\n let ks = await wallet.getId();\n const conn = new protocolV4.Connection(dcpConfig.scheduler.services.resultSubmitter.location, ks);\n \n const { success, payload } = await conn.send('deleteResult', {\n job: job.id,\n owner: job.paymentAccountKeystore.address,\n range,\n }, job.paymentAccountKeystore);\n \n if (!success)\n throw new Error('ResultHandle.delete: Could not delete slices from scheduler');\n \n await Promise.all(payload.map(r => {\n if (!r) {\n console.warn(`ResultHandle.delete: Received result was not defined (${r}), ignoring...`);\n return;\n }\n this.valuesAvailable[r - 1] = false;\n delete this.realValues[r - 1];\n }));\n \n await conn.close(null, true);\n return payload;\n }\n\n async stat(rangeObject) {\n const range = rehydrateRange(rangeObject);\n throw new Error(\"ResultHandle.stat not implemented\");\n }\n}\n\nObject.assign(exports, {\n ResultHandle,\n});\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/job/result-handle.js?");
4252
4296
 
4253
4297
  /***/ }),
4254
4298
 
@@ -4328,7 +4372,7 @@ eval("/* provided dependency */ var process = __webpack_require__(/*! ./node_mod
4328
4372
  \*************************************************/
4329
4373
  /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
4330
4374
 
4331
- eval("/**\n * @file /src/schedmsg/schedmsg-web.js\n * @author Ryan Rossiter, ryan@kingsds.network\n * @date March 2020\n *\n * This is the SchedMsg implementation for commands that are browser-specific\n * or have browser-specific behaviour.\n */\n\nconst { SchedMsg } = __webpack_require__(/*! ./schedmsg */ \"./src/dcp-client/schedmsg/schedmsg.js\");\n\nclass SchedMsgWeb extends SchedMsg {\n constructor(worker) {\n super(worker);\n this.modal = null;\n\n this.registerHandler('announce', this.onAnnouncement.bind(this));\n this.registerHandler('openPopup', this.onOpenPopup.bind(this));\n this.registerHandler('reload', this.onReload.bind(this));\n }\n\n onAnnouncement({ message }) {\n if (this.modal) {\n this.modal.close();\n }\n\n this.modal = window.userInterface.alert('Announcement', '' /* subtitle */, message,\n /* onClose */ () => this.modal = null);\n }\n\n onOpenPopup({ href }) {\n window.open(href);\n }\n\n onReload() {\n const hash = window.location.hash;\n\n let newUrl = window.location.href.replace(/#.*/, '');\n newUrl += (newUrl.indexOf('?') === -1 ? '?' : '&');\n newUrl += 'dcp=0bf54dbe88ee11e84b28e62b73cbd154d06967ea,' + Date.now() + hash;\n\n window.location.replace(newUrl);\n }\n}\n\nObject.assign(module.exports, {\n SchedMsgWeb\n});\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/schedmsg/schedmsg-web.js?");
4375
+ eval("/**\n * @file /src/schedmsg/schedmsg-web.js\n * @author Ryan Rossiter, ryan@kingsds.network\n * @date March 2020\n *\n * This is the SchedMsg implementation for commands that are browser-specific\n * or have browser-specific behaviour.\n */\n\nconst { SchedMsg } = __webpack_require__(/*! ./schedmsg */ \"./src/dcp-client/schedmsg/schedmsg.js\");\n\nclass SchedMsgWeb extends SchedMsg {\n constructor(worker) {\n super(worker);\n this.modal = null;\n\n this.registerHandler('announce', this.onAnnouncement.bind(this));\n this.registerHandler('openPopup', this.onOpenPopup.bind(this));\n this.registerHandler('reload', this.onReload.bind(this));\n }\n\n onAnnouncement({ message }) {\n if (this.modal) {\n this.modal.close();\n }\n\n this.modal = window.userInterface.alert('Announcement', '' /* subtitle */, message,\n /* onClose */ () => this.modal = null);\n }\n\n onOpenPopup({ href }) {\n window.open(href);\n }\n\n onReload() {\n const hash = window.location.hash;\n\n let newUrl = window.location.href.replace(/#.*/, '');\n newUrl += (newUrl.indexOf('?') === -1 ? '?' : '&');\n newUrl += 'dcp=5895f3465816246196dbdb9c0cd5aa200a943c59,' + Date.now() + hash;\n\n window.location.replace(newUrl);\n }\n}\n\nObject.assign(module.exports, {\n SchedMsgWeb\n});\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/schedmsg/schedmsg-web.js?");
4332
4376
 
4333
4377
  /***/ }),
4334
4378
 
@@ -4527,7 +4571,7 @@ eval("/* provided dependency */ var process = __webpack_require__(/*! ./node_mod
4527
4571
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4528
4572
 
4529
4573
  "use strict";
4530
- eval("/* provided dependency */ var process = __webpack_require__(/*! ./node_modules/process/browser.js */ \"./node_modules/process/browser.js\");\n/**\n * @file dcp-client/worker/supervisor2/index.js\n * Code managing sandboxes, tasks, jobs, and slices within in a DCP Worker.\n * @author Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date Dec 2020\n * June 2022\n * @module supervisor\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { requireNative } = __webpack_require__(/*! dcp/dcp-client/webpack-native-bridge */ \"./src/dcp-client/webpack-native-bridge.js\");\nconst dcp4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst constants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { setImmediate } = __webpack_require__(/*! dcp/common/dcp-timers */ \"./src/common/dcp-timers.js\");\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst { Keystore, Address } = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst { localStorage } = __webpack_require__(/*! dcp/common/dcp-localstorage */ \"./src/common/dcp-localstorage.js\");\nconst { DcpURL } = __webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\");\nconst RingBuffer = __webpack_require__(/*! dcp/utils/ringBuffer */ \"./src/utils/ringBuffer.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { JobManager } = __webpack_require__(/*! ./job-manager */ \"./src/dcp-client/worker/supervisor2/job-manager.js\");\nconst { Load } = __webpack_require__(/*! ./load */ \"./src/dcp-client/worker/supervisor2/load.js\");\nconst { Sandbox, SandboxError } = __webpack_require__(/*! ./sandbox2 */ \"./src/dcp-client/worker/supervisor2/sandbox2.js\");\nconst { sliceStatus } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst hash = __webpack_require__(/*! dcp/common/hash */ \"./src/common/hash.js\");\nconst { calculateJoinHash } = __webpack_require__(/*! dcp/dcp-client/compute-groups */ \"./src/dcp-client/compute-groups/index.js\");\nconst { ModuleCache } = __webpack_require__(/*! ./module-cache */ \"./src/dcp-client/worker/supervisor2/module-cache.js\");\nconst { Inventory, leafMerge, a$sleepMs, ms, pct, generateOpaqueId, booley, compressJobMap, \n toJobMap, truncateAddress, encodeDataURI, makeValueURI, justFetch, stringify } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n//const { CodeFlow } = require('client-oauth2');\n//const { text } = require('./lang').getLocaleStrings('en_CA'); /** XXXpfr @todo Don't know what to do with localization? */\n\n/** @typedef {import('dcp/dcp-client/wallet/eth').Address} Address */\n/** @typedef {import('dcp/protocol-v4/connection/connection').Connection} Connection */\n/** @typedef {string} opaqueId */ // 22 character base64 string\n/** @typedef {import('..').Worker} Worker */\n/** @typedef {import('..').SupervisorOptions} SupervisorOptions */\n/** @typedef {import('./slice2').Slice} Slice */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n\nconst addressTruncationLength = 20;\n\n//\n// Configs are moving around in dcpConfig and local worker configs, so set up some defaults.\nlet workerTuning = dcpConfig.worker;\nif (!workerTuning) workerTuning = dcpConfig.Supervisor;\nif (!workerTuning || !workerTuning.dcp || !workerTuning.dcp.tuning || !workerTuning.dcp.tuning.watchdogInterval\n || !workerTuning.sandbox || !workerTuning.allowOrigins || !workerTuning.minimumWage || !workerTuning.computeGroups)\n workerTuning = {\n dcp: {\n tuning: { watchdogInterval: 7, minSandboxStartDelay: 0.1, maxSandboxStartDelay: 0.7 },\n connectionOptions: { default: { identityUnlockTimeout: 15 * 60 /* seconds */ } },\n },\n sandbox: { progressReportInterval: 2 * 60 * 100 },\n allowOrigins: { fetchWorkFunctions: [], fetchArguments: [], fetchData: [], sendResults: [], any: [] },\n minimumWage: { CPU: 0, GPU: 0, 'in': 0, out: 0 },\n leavePublicGroup: false,\n pCores: 0,\n computeGroups: {},\n // The following configs are not in dcpConfig or worker configs (yet), but may be specified in local worker configs to override the defaults.\n pruneFrequency: 15 * 1000, // Maxiumum time interval where we check to prune used sandboxes.\n workerSandboxThreshold: 7, // When maxWorkingSandboxes >= workerSandboxThreshold, we allow an extra 25% of assigned sandboxes that won't be pruned.\n cachedJobsThreshold: 12, // Prune the unused job managers >= cachedJobsThreshold.\n};\n\n//\n// Flags for tracing.\n//\nconst selectiveEnable = false;\nconst displayWarnError = false || selectiveEnable;\nconst selectiveDebugging = selectiveEnable || debugging();\nconst debuggingError = false || selectiveDebugging || displayWarnError;\nconst debuggingWarn = false || selectiveDebugging || displayWarnError;\nconst selectiveDebugging2 = selectiveEnable && false || debugging('supervisor');\nconst displaySliceState = true;\nconst displayCompletedResults = false;\n\n/** \n * Adjust delay times when debugging.\n * The adjustment for niim is automatic, other debuggers must manually change this value.\n */\nlet timeDilation = 1;\nif (DCP_ENV.platform === 'nodejs')\n{\n /** Make timers 10x slower when running in niim */\n timeDilation = (requireNative('module')._cache.niim instanceof requireNative('module').Module) ? 10 : 1;\n}\n\n//\n// Index to functionality -- search for '_Idx' to toggle through the index.\n//\n// 1) Ctor: Supervisor constructor.\n// 2) Important property-like functions.\n// 3) Dtors: screenSaverDestroy, stopWork, purgeJob.\n// 4) Connection code.\n// 5) Work: Distribute slice to sandboxes.\n// 6) Return slices and sent progress reports to result-submitter-results.\n// 7) Task Distributor (TD): requestTask (Rq) support -- communication with TD.\n// 8) Aggregators from the job managers.\n// 9) Sandbox creation and management.\n// 10) Result-submitter-result support functions.\n// 11) Work reject.\n// 12) Unused functions that we need to review.\n//\n\n// _Idx\n//\n// Ctor: Supervisor constructor.\n//\n\n/** \n * Supervisor constructor\n * \n * A supervisor manages the communication with the scheduler, manages sandboxes, and\n * decides which workload should be sent to which sandboxes when.\n *\n * Start state:\n * - initial\n *\n * Intermediate states:\n * - ready\n * - stopping\n *\n * Terminal states:\n * - stopped\n *\n * Valid transitions:\n * - initial -> ready where that happens \n * - ready -> stopping\n * - stopping -> stopped\n *\n * @param {Worker} worker - The worker that created this instance.\n * @param {SupervisorOptions} options - Options for specifying custom behaviour and tuning,\n */\nfunction Supervisor(worker, options)\n{\n assert(options.identity instanceof Keystore);\n assert(options.paymentAddress instanceof Address);\n\n /**\n * Flag to indicate a debug build.\n * Used when we want to display extra information and do extra checks for developers only.\n * @type {boolean}\n */\n this.debugBuild = ((__webpack_require__(/*! dcp/common/dcp-build */ \"./src/common/dcp-build.js\").build) === 'debug');\n /**\n * When Supervisor.sliceTiming is set to be true, it displays the timings of a every slice\n * slice['queueingDelta'] = timespan of when slice is passed to jobManager.runQueuedSlice until sandbox.work\n * slice['executionDelta'] = timespan of execution in sandbox\n * slice['resultDelta'] = timespan of when sandbox finishes executing until recordResult completes.\n * @type {boolean}\n */\n this.sliceTiming = false;\n /** Used for analyzing the completed results in Supervisor.recordResult. */\n this.resultMap = {};\n\n /** @type {ModuleCache} */\n this.moduleCache = new ModuleCache(this);\n\n this.worker = worker;\n this.identity = options.identity;\n this.paymentAddress = options.paymentAddress;\n this.options = options;\n this.maxWorkingSandboxes = options.maxWorkingSandboxes || 1;\n this.maxTotalSandboxes = this.maxWorkingSandboxes;\n\n // We're making the assumption that if a worker has at least 7 sandboxes, \n // then the worker has sufficient resources to handle 25% more sandboxes in memory.\n // This assumption may be overridden by changing workerSandboxThreshold.\n if (this.maxWorkingSandboxes >= this.workerSandboxThreshold)\n this.maxTotalSandboxes = Math.ceil(1.25 * this.maxWorkingSandboxes);\n // When # of sandboxes reaches this level, we more aggressively prune.\n this.mustPruneSandboxLevel = Math.ceil(1.5 * this.maxTotalSandboxes);\n // Last prune time stamp.\n this.lastPrune = 0;\n // General time stamp.\n this.lastTime = 0;\n\n // Supervisor may get created by Worker where options.cores or options.targetLoad is not defined.\n this.numCPU = this.maxWorkingSandboxes;\n this.numGPU = 1;\n this.portionToUseCPU = pct(100);\n this.portionToUseGPU = pct(100);\n\n if (options.cores)\n {\n this.numCPU = options.cores.cpu || this.numCPU;\n this.numGPU = options.cores.gpu || this.numGPU;\n }\n if (options.targetLoad)\n {\n this.portionToUseCPU = options.targetLoad.cpu || pct(100);\n this.portionToUseGPU = options.targetLoad.gpu || pct(100);\n }\n\n this.tuning = {\n maxCPUAlloc: this.portionToUseCPU, /**< Maximum proportion of CPU time to attempt to use. */\n maxGPUAlloc: this.portionToUseGPU, /**< Maximum proportion of GPU time to attempt to use. */\n watchdogInterval: 7, /**< (seconds) How frequently to kick off an unsolicited requestTask. */\n prefetchInterval: 20, /**< (seconds) How many seconds into the future are looking to project capacity during work fetch. */\n minSandboxStartDelay: 0.1, /**< (seconds) Base minimum of this.delayMs, scaled by this.delayScaler. */\n maxSandboxStartDelay: 0.7, /**< (seconds) Base maximum random component of this.delayMs, scaled by this.delayScaler. */\n };\n this.tuning = leafMerge(this.tuning, workerTuning.dcp.tuning);\n if (options.watchdogInterval > 0)\n this.tuning.watchdogInterval = options.watchdogInterval; // Override.\n //this.tuning.watchdogInterval = 0.25;\n\n /**\n * Fine tune this.delayMs.\n * Note: Please discuss any change with Paul, paul@kingsds.network.\n * XXXpfr @todo Finalize the delay tuning.\n */\n this.delayScaler = 0.5;\n\n debugging('supervisor') && console.debug('Supervisor.tuning', this.tuning);\n\n /**\n * Note: targetLoad is not properly implemented yet.\n * XXXpfr @todo Collaborate with Wes to get it right.\n * @type {Load}\n */\n this.targetLoad = new Load({\n cpu: Math.min(this.maxWorkingSandboxes, this.numCPU),\n gpu: Math.min(this.maxWorkingSandboxes, this.numGPU)\n }).scale(this.tuning.maxCPUAlloc, this.tuning.maxGPUAlloc);\n\n /** @type {string[]} */\n this.allowedOrigins = workerTuning.allowOrigins.any;\n /** @type {string[]} */\n this.fetchWorkFunctions = workerTuning.allowOrigins.fetchWorkFunctions;\n /** @type {string[]} */\n this.fetchArguments = workerTuning.allowOrigins.fetchArguments;\n /** @type {string[]} */\n this.fetchData = workerTuning.allowOrigins.fetchData;\n /** @type {string[]} */\n this.sendResults = workerTuning.allowOrigins.sendResults;\n\n // In localExec, do not allow work function or arguments to come from the 'any' origins\n if (this.options.localExec)\n {\n this.allowedOrigins = this.allowedOrigins.concat(options.allowedOrigins.any);\n this.fetchWorkFunctions = this.fetchWorkFunctions.concat(options.allowedOrigins.fetchWorkFunctions);\n this.fetchArguments = this.fetchArguments.concat(options.allowedOrigins.fetchArguments);\n this.fetchData = this.fetchData.concat(options.allowedOrigins.fetchData);\n this.sendResults = this.sendResults.concat(options.allowedOrigins.sendResults)\n }\n\n if (options.allowedOrigins && options.allowedOrigins.length > 0)\n this.allowedOrigins = options.allowedOrigins.concat(this.allowedOrigins);\n \n //\n // The following 3 configs are not in dcpConfig or worker configs (yet), but may be specified in local worker configs to override the defaults.\n //\n /** @type {number} - Maxiumum time interval where we check to prune used sandboxes. */\n this.pruneFrequency = workerTuning.pruneFrequency || 15 * 1000;\n /** @type {number} - When maxWorkingSandboxes >= workerSandboxThreshold, we allow an extra 25% of assigned sandboxes that won't be pruned. */\n this.workerSandboxThreshold = workerTuning.workerSandboxThreshold || 7;\n /** @type {number} - Prune the unused job managers >= cachedJobsThreshold. */\n this.cachedJobsThreshold = workerTuning.cachedJobsThreshold || 12;\n\n /** @type {Object.<Address, JobManager>} */\n this.jobMap = {}; \n /** @type {Sandbox[]} - All sandboxes that are being used by the job managers. Makes sure we don't lose sandboxes. */\n this.sandboxInventory = [];\n /** @type {Sandbox[]} - Started sandboxes that are not in sandboxInventory yet. */\n this.readiedSandboxes = [];\n /** @type {JobManager[]} */\n this.jobManagerInventory = new Inventory('jobManagers');\n /** @type {Synchronizer} */\n this.state = new Synchronizer('initial', [ 'initial', 'ready', 'reconnecting', 'stopping', 'stopped', 'broken']);\n\n /** @type {string} */\n this.lastDcpsid = undefined;\n /** @type {Connection} */\n this.taskDistributor = null;\n /** @type {Connection} */\n this.resultSubmitter = null;\n /** @type {Connection} */\n this.eventRouter = null;\n /** @type {Connection} */\n this.packageManager = null;\n /** @type {Array<object>} */\n this.resultSubmitterMessageQueue = [];\n /** @type {Array<object>} */\n this.eventRouterMessageQueue = [];\n\n /** @type {object} */\n this.schedulerConfig = leafMerge(dcpConfig.scheduler, options.schedulerConfig);\n\n /** @type {opaqueId} */\n this.workerId = localStorage.getItem('workerId');\n if (!this.workerId || this.workerId.length !== constants.workerIdLength)\n {\n this.workerId = generateOpaqueId();\n localStorage.setItem('workerId', this.workerId);\n }\n /** @type {object[]} */\n this.rejectedJobs = [];\n /** \n * An N-slot ring buffer of job addresses. Stores all jobs that have had no more than 1 slice run in the ring buffer.\n * Required for the implementation of discrete jobs \n * @type {RingBuffer} \n */\n this.ringBufferofJobs = new RingBuffer(100); // N = 100 should be more than enough. \n /** @type {boolean} - pseudo-mutex guarding requestTask. */\n this.isFetchingNewWork = false;\n\n // Start up the connections.\n this.instantiateAllConnections();\n\n /**\n * Note: DCP-3241 asks to test Android to see if we need this restriction any longer.\n * XXXpfr @todo Hopefully we can delete this @hack.\n */\n // @hack - dcp-env.isBrowserPlatform is not set unless the platform is _explicitly_ set,\n // using the default detected platform doesn't set it.\n // Fixing that causes an error in the wallet module's startup on web platform, which I\n // probably can't fix in a reasonable time this morning.\n // ~ER2020-02-20\n if (!options.maxWorkingSandboxes\n && DCP_ENV.browserPlatformList.includes(DCP_ENV.platform)\n && navigator.hardwareConcurrency > 1) {\n this.maxWorkingSandboxes = navigator.hardwareConcurrency - 1;\n if (typeof navigator.userAgent === 'string') {\n if (/(Android).*(Chrome|Chromium)/.exec(navigator.userAgent)) {\n this.maxWorkingSandboxes = 1;\n this.emit('warning', 'Doing work with Chromimum browsers on Android is currently limited to one sandbox');\n }\n }\n }\n}\nexports.Supervisor = Supervisor;\nSupervisor.prototype = Object.getPrototypeOf(new EventEmitter('Supervisor')); // Fake out VSCode -- get's rid of a billion red-squigglies.\nSupervisor.prototype = new EventEmitter('Supervisor');\n/**\n * Preserve the constructor property.\n * @constructor\n */\nSupervisor.prototype.constructor = Supervisor;\n\n/**\n * Set up sandboxes and interval timers, then start to search for work.\n **/\nSupervisor.prototype.startWork = function Supervisor$startWork ()\n{\n /* Provide opportunity for calling code to hook ready/error events. */\n setImmediate(async () => {\n try\n {\n if (this.state.isNot('initial'))\n {\n if (this.state.setIf('stopped', 'initial')) {}\n else if (this.state.setIf('reconnecting', 'initial')) {}\n else if (this.state.setIf('broken', 'initial')) {}\n else if (this.state.is('ready')) return\n else throw new Error(`Supervisor startWork is in unexpected state ${this.state}, aborting...`);\n }\n this.instantiateAllConnections();\n\n await this.createSandboxes(this.maxWorkingSandboxes)\n .then(() => this.checkCapabilities());\n\n // Beacon interval timer.\n this.progressReportTimer = setInterval(() => this.emitProgressReport(), (workerTuning.sandbox.progressReportInterval || 2 * 60 * 100));\n // Watchdog: requestTask-driven interval timer.\n this.watchdogTimer = setInterval(() => this.requestTask() , ms(this.tuning.watchdogInterval));\n if (DCP_ENV.platform === 'nodejs' && this.options.localExec)\n {\n /* Interval timer helps keep worker alive forever, which we don't want in localExec. */\n this.progressReportTimer.unref();\n this.watchdogTimer.unref();\n }\n\n this.state.set('initial', 'ready');\n\n setImmediate(() => this.requestTask()); // Don't wait for watchdog.\n }\n catch(error)\n {\n this.state.set('initial', 'broken');\n this.emit('error', error);\n }\n });\n}\n\n/** Construct capabilities when necessary. */\nSupervisor.prototype.checkCapabilities = function Supervisor$checkCapabilities ()\n{\n if (!this.capabilities)\n {\n /**\n * Assign the capabilities of one the sandboxes before fetching slices from the scheduler.\n * @todo Remove this once fetchTask uses the capabilities of every sandbox to fetch slices.\n */\n const sandbox = this.readiedSandboxes.length > 0 ? this.readiedSandboxes[0] : this.sandboxInventory[0];\n if (sandbox)\n {\n this.capabilities = sandbox.capabilities;\n this.emit('capabilitiesCalculated', this.capabilities);\n }\n }\n\n if (DCP_ENV.isBrowserPlatform && this.capabilities.browser)\n this.capabilities.browser.chrome = DCP_ENV.isBrowserChrome;\n}\n\n// _Idx\n//\n// Important property-like functions.\n//\n\n/**\n * Universal delay milliseconds..\n * @returns {number}\n */\nSupervisor.prototype.delayMs = function Supervisor$delayMs (max = this.tuning.maxSandboxStartDelay, min = this.tuning.minSandboxStartDelay)\n{\n // Note: Please discuss any change with Paul, paul@kingsds.network.\n return 1000 * timeDilation * this.delayScaler * (min + Math.random() * (max - min));\n}\n\n/**\n * Indicates whether supervisor is ready for business.\n * @returns {boolean} - When true, the party is on...\n */\nSupervisor.prototype.isReady = function Supervisor$isReady()\n{\n return this.worker.working && this.state.is('ready');\n}\n\n/**\n * Safe access to Connection.close(...).\n * @param {Connection} connection\n * @param {string} [reason='requested']\n * @param {boolean} [immediate=false]\n * @returns {Promise<string>}\n */\nfunction safeClose(connection, reason = 'requested', immediate = false)\n{\n if (connection)\n {\n let msg;\n if (connection.state.is('closed')) msg = 'closed';\n else if (connection.state.is('closing')) msg = 'closing';\n else if (connection.state.is('close-wait')) msg = 'close-wait';\n if (msg)\n {\n debuggingWarn && console.warn(`${msg}, do not try to close again.`);\n return Promise.resolve(msg);\n }\n return connection.close(reason, immediate)\n .then(() => {\n return Promise.resolve(null);\n });\n }\n return Promise.resolve('already closed');\n}\n\n/**\n *\n * @param {string} operation\n * @param {*} data\n * @returns {Promise<string>}\n */\nSupervisor.prototype.saveForResubmitToRS = function Supervisor$saveForResubmitToRS(operation, data)\n{\n this.resultSubmitterMessageQueue.push({ operation, data });\n return safeClose(this.resultSubmitter);\n}\n\n/**\n * Error feedback to user.\n * @param {string} message\n * @param {*} extra\n */\nSupervisor.prototype.error = function Supervisor$error(message, extra)\n{\n const dcpError = new DCPError(message, extra);\n this.emit('error', dcpError);\n}\n\n/**\n * When true, the sandbox complete handler will look for another slice in the same job,\n * and if not found, then proceed to Supervisor.requestTask.\n * @returns {boolean}\n */\nSupervisor.prototype.runSliceFromSameJob = function Supervisor$runSliceFromSameJob()\n{\n //\n // Experimental, should be off by default.\n // Cf. sandbox complete handler in JobManager.hookUpSandboxListeners.\n //\n const disable = true;\n const tooManyJobs = this.activeJobCount() > this.maxWorkingSandboxes;\n return !disable && !tooManyJobs && this.unusedSandboxSlots() < 2;\n}\n\n/**\n * This function is used as the target number of sandboxes to be associated with slices and start working.\n * Warning: Do not rely on this information being 100% accurate -- it may change in the next instant.\n * @returns {number}\n */\nSupervisor.prototype.unusedSandboxSlots = function Supervisor$unusedSandboxSlots ()\n{\n return this.maxWorkingSandboxes - this.workingSliceCount();\n}\n\n/**\n * Create errorObj with error.code if it exists.\n * @param {Error} error\n * @returns {object}\n */\nSupervisor.prototype.checkCode = function Supervisor$checkCode (error)\n{\n if (!error) return '';\n const errorObj = { message: error.message };\n if (error['errorCode']) errorObj['errorCode'] = error['errorCode'];\n if (error['code']) errorObj['code'] = error['code'];\n return errorObj;\n}\n\n/**\n * Remove stack trace from error.\n * May not work when error is a string with no new-lines.\n * @param {string|Error} error\n * @returns {string|Error}\n */\nSupervisor.prototype.removeStackTrace = function Supervisor$removeStackTrace (error)\n{\n if (typeof error === 'string')\n {\n const errorLines = error.split('\\n');\n return (errorLines && errorLines.length > 0) ? errorLines[0] : error;\n }\n if (error instanceof Error)\n return error.message;\n return error;\n}\n\n// _Idx\n//\n// Dtors: screenSaverDestroy, stopWork, purgeJob.\n//\n\n/**\n * If we cannot create a new sandbox, that probably means we're on a screensaver worker\n * and the screensaver is down. So return the slices to the scheduler.\n */\nSupervisor.prototype.screenSaverDestroy = function Supervisor$screenSaverDestroy()\n{\n debugging('supervisor') && console.debug(`Supervisor.screenSaverDestroy: destroying all job managers and terminating all sandboxes.`);\n this.jobManagerInventory.forEach(jm => jm.destroy());\n this.jobManagerInventory = new Inventory('jobManagers');\n\n this.readiedSandboxes.forEach(sandbox => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.readiedSandboxes = [];\n\n this.sandboxInventory.forEach(sandbox => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.sandboxInventory = [];\n}\n\n/**\n * Terminates sandboxes and returns slices.\n * Sets the working flag to false, call @this.work to start working again.\n * \n * If forceTerminate is true: Terminates all sandboxes and returns all slices.\n * If forceTerminate is false: Terminates non-working sandboxes and returns initial and ready slices.\n *\n * @param {boolean} [forceTerminate = true] - true if you want to stop the sandboxes from completing their current slice.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.stopWork = async function Supervisor$stopWork (forceTerminate = true)\n{\n selectiveDebugging && console.debug(`Supervisor.stopWork(${forceTerminate},${this.state}): terminating sandboxes and returning slices to scheduler.`);\n\n // Do a hard flush of the microtask queue and finish the current event loop.\n await new Promise((resolve) => setImmediate(() => setTimeout(resolve, 0)));\n\n if (this.state.setIf('ready', 'stopping')) {}\n else if (this.state.setIf('reconnecting', 'stopping')) {}\n else if (this.state.setIf('broken', 'stopping')) {}\n else if (this.state.is('stopped')) return\n else throw new Error(`Supervisor stopWork is in unexpected state ${this.state}, aborting...`);\n\n this.instantiateAllConnections();\n\n this.readiedSandboxes.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.readiedSandboxes = [];\n\n if (forceTerminate)\n {\n for (const jm of this.jobManagerInventory)\n jm.destroy();\n\n this.sandboxInventory.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n }\n else\n {\n let activeSliceCount = 0;\n const slicesToReturn = [];\n for (const jm of this.jobManagerInventory)\n {\n //jm.dumpSlices(`stopWork1:${jm.address}`);\n const queuedSlices = jm.queuedSlices;\n slicesToReturn.push(...queuedSlices);\n jm.removeSlices(queuedSlices);\n activeSliceCount += jm.activeSlices.length;\n jm.nonWorkingSandboxes.forEach((sandbox) => jm.returnSandbox(sandbox));\n }\n\n const reason = `Supervisor stopWork(${forceTerminate}): returning all non-finished slices that are not working.`;\n this.returnSlices(slicesToReturn, reason, false /*removeSlices*/);\n\n await new Promise((resolve) => {\n if (activeSliceCount === 0)\n resolve();\n // Resolve and finish work once all sandboxes have finished submitting their results.\n this.on('submitFinished', () => {\n if (--activeSliceCount === 0)\n {\n debuggingWarn && console.warn('All sandboxes empty, stopping worker and closing all connections');\n resolve();\n }\n });\n });\n\n for (const jm of this.jobManagerInventory)\n {\n //jm.dumpSlices(`stopWork2:${jm.address}`);\n jm.liveSandboxes.forEach((sandbox) => sandbox.terminate(false));\n jm._sandboxInventory = [];\n }\n }\n this.sandboxInventory = [];\n this.jobManagerInventory = new Inventory('jobManagers');\n\n this.closeConnections();\n\n this.emit('stop');\n this.state.set('stopping', 'stopped');\n}\n\n/**\n* Purge all traces of the job.\n* @param {JobManager} jobManager \n*/\nSupervisor.prototype.purgeJob = function Supervisor$purgeJob (jobManager)\n{\n selectiveDebugging && console.debug(`Supervisor.purgeJob ${jobManager.identifier}.`);\n this.jobManagerInventory.delete(jobManager);\n this.moduleCache.removeJob(jobManager.address);\n jobManager.destroy();\n}\n\n// _Idx\n//\n// Connection code.\n//\n\n/** \n * Connect the supervisor to a given scheduler sub-service. Reconnection \"DDoS\" from workers\n * mitigated via an exponential backoff algorithm.\n *\n * DCPv4 connections are lazily-initiated. Successful connection establishment detected by\n * observing the payload event, which normally triggers during versioning.\n */\nSupervisor.prototype.connectTo = function Supervisor$connectTo(which)\n{\n const config = (which === 'packageManager') ? dcpConfig.packageManager : this.schedulerConfig.services[which];\n const retryMinSleepMs = 100;\n const payloadResetCount = 3; /* How many payloadCount before we reset retryNextSleepMs. */\n \n var retryNextSleepMs = retryMinSleepMs;\n var payloadCount = 0;\n var options = leafMerge(/* ordered from most to least specific */\n workerTuning.dcp.connectionOptions.default,\n workerTuning.dcp.connectionOptions[which],\n workerTuning.dcp.connectionOptions[config.location.href],\n );\n\n /**\n * The payload event handler is an interesting way to handle exponential backoff\n * for the delay in this.reconnect.\n * XXXpfr @todo Revisit exponential backoff for this.reconnect in Sup2 Part II.\n */\n\n const handlePayloadEventFn = (ev) =>\n {\n if (++payloadCount === payloadResetCount)\n {\n this[which].removeEventListener('payload', handlePayloadEventFn);\n retryNextSleepMs = retryMinSleepMs; \n }\n }\n\n this[which] = new dcp4.Connection(config, this.identity, options);\n\n debugging() && console.debug(`Opening new connection ${this[which].id} to ${which}.`)\n\n const messageQueue = this[which + 'MessageQueue'];\n if (messageQueue && messageQueue.length > 0)\n resendRejectedMessages(this[which], messageQueue);\n\n this[which].on('payload', handlePayloadEventFn);\n\n this['reconnect' + which] = this.reconnect.bind(this, which);\n this[which].on('close', () => { this['reconnect' + which](); });\n}\n\n/**\n * Reconnect logic.\n * @param {string} which -- Name of the connection \n */\nSupervisor.prototype.reconnect = async function Supervisor$reconnect(which)\n{\n debugging('supervisor') && console.debug(`Supervisor.reconnect: ${which}:`, this.state.valueOf(), this[which].state.valueOf());\n const stateChange = this.state.testAndSet('ready', 'reconnecting');\n if (stateChange || this.state.is('reconnecting'))\n {\n await a$sleepMs(100); // Sleep for 100ms, maybe exp-backoff later if we need it.\n this.connectTo(which);\n if (stateChange) this.state.set('reconnecting', 'ready');\n debugging() && console.debug(`Supervisor.reconnect: Trying to reconnect ${which}`, this.state.valueOf(), this[which].state.valueOf());\n }\n}\n\n/**\n * Close a connection properly.\n * @param {string} which -- Name of the connection to close.\n */\nSupervisor.prototype.closeConnection = function Supervisor$closeConnection(which)\n{\n if (this[which])\n {\n this[which].off('close', this['reconnect' + which]);\n this[which].close();\n this[which] = null;\n }\n}\n\n/**\n * Close all connections.\n */\nSupervisor.prototype.closeConnections = function Supervisor$closeConnections()\n{\n /** XXXpfr @todo Remove when bug DCP-3094 is fixed. */\n a$sleepMs(500);\n\n this.closeConnection('taskDistributor');\n this.closeConnection('resultSubmitter');\n this.closeConnection('eventRouter');\n this.closeConnection('packageManager');\n}\n\n/**\n * Broadcast keepalive to all connections and optionally start up sandboxes.\n * @param {boolean} [createSandbox=false] - When true, creates sandboxes for unused sandbox slots.\n */\nSupervisor.prototype.keepalive = function Supervisor$keepalive(createSandbox = false)\n{\n return Promise.all([\n this.taskDistributor.keepalive(),\n this.resultSubmitter.keepalive(),\n this.eventRouter .keepalive(),\n this.packageManager .keepalive(),\n (createSandbox ? this.createSandboxes(this.maxWorkingSandboxes) : Promise.resolve()),\n ]);\n}\n\n/**\n * Open all connections. Used when supervisor is instantiated or stopped/started to initially open connections.\n */\nSupervisor.prototype.instantiateAllConnections = function Supervisor$instantiateAllConnections ()\n{\n if (!this.taskDistributor)\n this.connectTo('taskDistributor');\n if (!this.eventRouter)\n this.connectTo('eventRouter');\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n if (!this.packageManager)\n this.connectTo('packageManager');\n}\n\n/**\n * Try sending messages that were rejected on an old instance of the given connection.\n * @param {Connection} connection\n * @param {Array<object>} messageQueue\n */\nasync function resendRejectedMessages (connection, messageQueue)\n{\n var message = messageQueue.shift();\n do \n {\n try\n {\n await connection.send(message.operation, message.data);\n }\n catch (error)\n {\n debuggingError && console.error(`Failed to resend message ${message.operation} to scheduler: ${error}. Will try again on a new connection.`);\n messageQueue.unshift(message);\n safeClose(connection);\n break;\n }\n message = messageQueue.shift();\n } while (message);\n}\n \n// _Idx\n//\n// Work: Distribute slice to sandboxes.\n//\n\n/** \n * UNUSED\n * @deprecated\n * Round-robin through the job managers, picking 1 slice to run each time.\n * Try to have the same number of working sandboxes for each job.\n * Try to run a slice on every available sandbox.\n */\nSupervisor.prototype.workOnCurrentTask = function Supervisor$workOnCurrentTask ()\n{\n return this.roundRobinSlices();\n}\n\n/**\n * This is for compatibility with Supervisor1 in sa worker.\n * When we get rid of Supervisor1 we can delete the ref in sa worker and then get rid of this function.\n */\nSupervisor.prototype.setDefaultIdentityKeystore = function Supervisor$setDefaultIdentityKeystore ()\n{\n}\n\n/**\n * Called in Worker.start().\n * Initial entry point after Worker constructor.\n * We need to start searching for work here to allow starting and stopping a worker.\n */\nSupervisor.prototype.work = function Supervisor$work ()\n{\n // Set up sandboxes and interval timers, then start to search for work.\n this.startWork();\n}\n\n/** \n * Round-robin through the job managers, picking 1 slice to run each time.\n * Try to have the same number of working sandboxes for each job.\n * Try to run a slice on every available sandbox.\n */\nSupervisor.prototype.roundRobinSlices2 = function Supervisor$roundRobinSlices2 ()\n{\n try\n {\n let first = true;\n const cursor = this.makeJobSelectionCursor(this.jobManagerInventory);\n while (true)\n {\n const slice = cursor.next();\n if (!slice) break; /* No more work can fit. */\n debugging('supervisor') && console.debug('roundRobinSlices: Executing slice', slice.identifier);\n slice.markAsReserved();\n slice.jobManager.runSlice(slice, first ? 0 : this.delayMs());\n first = false;\n }\n }\n finally\n {\n this.isFetchingNewWork = false;\n }\n}\n\n/**\n * We try to balance so that each job has the same number of working slices.\n *\n * NOTES:\n * 1) If count is such that it cannot be distributed evenly, we do not yet randomly assign the extras.\n * E.g. 3 jobs, j1, j2, j3: count = 5 -- so 2 jobs get extras -- the extras need to be assigned randomly.\n * @todo Assign the extras randomly.\n * 2) this.roundRobinSlices is not going to be what we use when sup2 is in final form.\n * We want to this.makeJobSelectionCursor and something like this.roundRobinSlices2\n *\n * In the outer loop,\n * when numworkingSandboxes=1, try to get a slice running for each job with 0 working sandboxes.\n * when numworkingSandboxes=2, try to get a slice running for each job with 1 working sandboxes.\n * when numworkingSandboxes=3, try to get a slice running for each job with 2 working sandboxes. Etc.\n * The idea is to balance the number of slices working on each job.\n * @param {number} [count=0] - The number of new slices to try to get running in sandboxes.\n */\nSupervisor.prototype.roundRobinSlices = function Supervisor$roundRobinSlices (count = 0)\n{\n try\n {\n if (!count) count = this.unusedSandboxSlots();\n if (!count || this.readySliceCount() < 1)\n return Promise.resolve();\n\n const slices = [];\n let numScheduled = 0\n let readyJobManagers = this.jobManagerInventory.filter(jm => jm.ready);\n let first = true;\n selectiveDebugging && console.debug('roundRobinSlices: START count', count, 'numJobMgrs', readyJobManagers.length, 'totalWorking(w/r/wo/wsbx/sbx)', this.workingSliceCount(), this.reservedSliceCount(), this.workingSliceOnlyCount(), this.workingSandboxCount(), this.sandboxCount());\n\n for (let numWorkingSandboxes = 1; numWorkingSandboxes <= this.maxWorkingSandboxes; numWorkingSandboxes++)\n {\n let sliceCount = 0;\n const beginNumScheduled = numScheduled;\n for (const jobMan of readyJobManagers)\n {\n const _readySlices = jobMan.readySlices;\n sliceCount += _readySlices.length\n const skip = numWorkingSandboxes <= jobMan.workingSlices.length;\n\n if (skip || _readySlices.length < 1)\n {\n // Noisy log message turned off by default.\n //debugging('supervisor') && console.debug('RRS0(numS, beginNumS, count, sliceCount, skip, _ready, numWorkingS(loop), workingSlices):', numScheduled, beginNumScheduled, count, sliceCount, skip, _readySlices.length, numWorkingSandboxes, jobMan.workingSlices.length);\n continue;\n }\n\n const slice = _readySlices[0];\n slices.push(slice);\n\n slice.markAsReserved();\n jobMan.runSlice(slice, first ? 0 : this.delayMs());\n\n first = false;\n if (++numScheduled >= count)\n break;\n }\n if (numScheduled >= count)\n {\n debugging('supervisor') && console.debug('RRS1(numS, beginNumS, count, sliceCount):', numScheduled, beginNumScheduled, count, sliceCount);\n break;\n }\n if (beginNumScheduled === numScheduled && sliceCount < 1)\n {\n debugging('supervisor') && console.debug('RRS2(numS, beginNumS, count, sliceCount):', numScheduled, beginNumScheduled, count, sliceCount);\n break;\n }\n }\n if (selectiveDebugging)\n {\n console.debug(`roundRobinSlices(working:(w/r/wo/wsbx/sbx)${this.workingSliceCount()},${this.reservedSliceCount()},${this.workingSliceOnlyCount()},${this.workingSandboxCount()},${this.sandboxCount()}): Started ${slices.length}/${numScheduled}/${count} scheduled slices`, compressSlices(slices));\n const sliceGrouper = {};\n slices.forEach((slice) => {\n const jm = slice.jobManager;\n if (!sliceGrouper[slice.jobAddress]) sliceGrouper[slice.jobAddress] = { cnt: 1, working: jm.workingSlices.length, queued: jm.queuedSlices.length, assigned: jm.assignedSandboxes.length, estimation: jm.isEstimation };\n else sliceGrouper[slice.jobAddress].cnt++;\n });\n console.debug(sliceGrouper);\n }\n if (selectiveDebugging2)\n {\n const jobGrouper = {};\n this.jobManagerInventory.forEach((jm) => {\n jobGrouper[jm.address] = { working: jm.workingSlices.length, queued: jm.queuedSlices.length, assigned: jm.assignedSandboxes.length, estimation: jm.isEstimation };\n });\n console.debug(jobGrouper);\n }\n }\n finally\n {\n this.isFetchingNewWork = false;\n }\n}\n\n/**\n * @callback cbNext\n * @returns {Slice}\n */\n\n/** \n * Factory function which instantiates a JobSelectionCursor. A JobSelectionCursor\n * steps the order that job slices should be selected for execution in the supervisor,\n * given the current state of the supervisor and the availability of jobs when the\n * inventory was snapshot. The entire slice scheduling algorithm is represented by\n * this cursor.\n *\n * The basic idea behind the scheduling of slices in this implementation is to keep as\n * many slices from different jobs running as possible, so as to reduce the likelihood\n * of resource contention between sandboxes.\n *\n * Slices are scheduled in here based on the following ruleset:\n * - pick a slice from the longest job that isn't running\n * - choose a slice from the remaining jobs, in order for shortest to longest slice time\n * - if there are any jobs which are nearly finished, every other slice comes from one\n * of these jobs, selected randomly????? <-- NYI. XXXpfr @todo Think about implementing...\n * - jobs which have slicePriority set by the task-distributor may be chosen in place\n * of slices in the above algorith. Jobs with a slicePriority closer to 1 are more likely\n * to exhibit this behaviour.\n * @param {JobManager[]} jobManagerInventory\n * @returns { { next: cbNext } }\n */\nSupervisor.prototype.makeJobSelectionCursor = function Supervisor$JobSelectionCursor (jobManagerInventory)\n{\n /* Variables in this scope function as state information for next() */\n var jobManIdx = 0;\n /** @type {JobManager[]} - All job managers that are ready that have at least one ready slice. */\n var jobManList;\n /** @type {JobManager[]} - All jobManList elements that correspond to preferred jobs. */\n var prefJobManList;\n /* Number of working sandboxes allowed for a given job. */\n var concurrency = 1;\n const that = this;\n \n function seed (_concurrency)\n {\n function countSandboxes(jobAddress)\n {\n const jobManager = that.jobMap[jobAddress];\n if (!jobManager) throw new Error(`Lost track of job manager for address '${jobAddress}'`);\n return jobManager.workingSlices.length;\n }\n \n // Reset.\n jobManIdx = 0;\n\n /* Populate jobManList with jobs which are ready and have at least one slice which is ready. */\n jobManList = jobManagerInventory.filter((jobMan) => jobMan.ready && jobMan.readySlices.length > 0);\n\n /* Populate jobManList with jobManagers whose # of working slices is less than _concurrency. */\n jobManList = jobManList.filter((jobMan) => countSandboxes(jobMan.address) < _concurrency);\n\n /* Increasing sort. */\n jobManList.sort((a,b) => a.estimateWallMs - b.estimateWallMs);\n\n /* Populate prefJobManList with jobs from jobManList which also have a slicePreference set. */\n prefJobManList = jobManList.filter(jobMan => jobMan.hasOwnProperty('slicePreference') );\n }\n\n /**\n * Takes slices off the ready list, marks it reserved and increments workingSandboxCoun,\n * because the slice will soon be working.\n * @param {JobManager} jobMan \n * @returns {Slice}\n */\n function reserveSlice (jobMan)\n {\n const _readySlices = jobMan.readySlices;\n if (_readySlices.length > 0)\n {\n const slice = _readySlices[0];\n slice.markAsReserved();\n return slice;\n }\n return null;\n }\n\n /**\n * Each invocation of next() identifies one slice to run, or returns false if none can run.\n * @returns {Slice}\n */\n function next ()\n {\n /* Adjust order to schedule the heaviest job's first slice asap. */\n jobManList.unshift(jobManList.pop());\n\n let workingSliceCount;\n do\n {\n seed(concurrency);\n\n /* Maybe schedule a prefered job slice based on random chance .*/\n if (prefJobManList.length > 0)\n {\n let prioRan = Math.random();\n let list = prefJobManList.filter(jm => jm['slicePreference'] >= prioRan);\n\n if (list.length > 0)\n {\n const jobMan = list[list.length * Math.random()];\n const slice = reserveSlice(jobMan);\n if (slice)\n return slice;\n }\n }\n\n /* Schedule a slice from next job; jobs are in increasing order of estimated run time. */\n while (jobManIdx < jobManList.length)\n {\n const jobMan = jobManList[jobManIdx];\n jobManIdx++;\n const slice = reserveSlice(jobMan);\n if (slice)\n return slice;\n }\n\n /* If this is reached, we did not schedule a slice with current seed. We need\n * to re-seed to look for newly-available work and sandboxes, ratcheting up the\n * concurrency (max # of each job running) until we find something we can do.\n */\n workingSliceCount = that.workingSliceCount();\n debugging() && console.debug(`job selection - no slice found from ${jobManList.length} jobs for concurrency=${concurrency} and ${workingSliceCount} working sandboxes`);\n } while (jobManList.length > 0 && workingSliceCount < that.maxWorkingSandboxes && concurrency++ < that.maxWorkingSandboxes);\n\n return false; /* Did not find any more work that fits. */\n }\n \n return { next };\n}\n\n/**\n * Handle sandbox.work(...) errors.\n * @param {Sandbox} sandbox \n * @param {Slice} slice \n * @param {Error} error \n * @return {Promise<string>}\n */\nSupervisor.prototype.handleSandboxWorkError = async function Supervisor$handleSandboxWorkError (sandbox, slice, error)\n{\n if (!slice.isWorking) // Sanity. Exception should never fire.\n throw new Error(`handleSandboxWorkError: slice ${slice.identifier} must be WORKING.`);\n\n let logLevel, reason;\n \n if (error instanceof SandboxError)\n {\n logLevel = 'warn';\n reason = error.errorCode;\n // The message and stack properties of error objects are not enumerable,\n // so they have to be copied into a plain object this way\n // @ts-ignore\n error = Object.getOwnPropertyNames(error).reduce((o, p) => {\n o[p] = error[p]; return o;\n }, { message: 'Unexpected worker error' });\n }\n else\n {\n logLevel = 'error';\n if (error)\n reason = `Slice ${slice.sliceNumber} in state ${slice.state} of job ${slice.jobAddress} failed to complete execution with error ${this.checkCode(error)}.`;\n else\n {\n reason = `Slice ${slice.sliceNumber} in state ${slice.state} of job ${slice.jobAddress} failed to complete execution.`;\n error = new Error(reason);\n }\n // This error was unrelated to the work being done, so just return the slice\n // in the promise.catch in JobManager.runSliceOnSandbox .\n assert(slice.result === null);\n }\n \n this.handleFailedSlice(slice, error);\n\n let errorString;\n switch (reason)\n {\n case 'ENOPROGRESS':\n errorString = 'No progress error in sandbox.\\n';\n break;\n case 'ESLICETOOSLOW':\n errorString = 'Slice too slow error in sandbox.\\n';\n break;\n case 'EUNCAUGHT':\n errorString = `Uncaught error in sandbox ${error.message}.\\n`;\n break;\n case 'EFETCH':\n // The status.js processing does not have a case for 'EFETCH' yet.\n errorString = `Could not fetch data: ${error.message}.\\n`;\n break;\n }\n\n // Always display max info under debug builds, otherwise maximal error.\n // messages are displayed to the worker, only if both worker and client agree.\n const displayMaxInfo = slice.jobManager.displayMaxDiagInfo;\n\n const errorObject = {\n jobAddress: truncateAddress(slice.jobAddress, addressTruncationLength),\n sliceNumber: slice.sliceNumber,\n sandbox: sandbox.id,\n jobName: sandbox.public ? sandbox.public.name : 'unnamed',\n };\n if (displayMaxInfo)\n errorObject.stack += '\\n --------------------\\n' + (error.stack.split('\\n').slice(1).join('\\n'));\n\n if (error.name === 'EWORKREJECT')\n {\n reason = 'EWORKREJECT'; // The status.js processing does not have a case for 'EWORKREJECT' yet.\n error.stack = 'Sandbox was terminated by work.reject()';\n await this.handleWorkReject(sandbox, slice, error.message);\n }\n else\n {\n this.returnSlice(slice, reason);\n slice.jobManager.returnSandbox(sandbox);\n }\n\n if (errorString)\n console[logLevel](errorString, errorObject);\n else if (error.name === 'EWORKREJECT')\n console[logLevel](`Slice rejected work: ${error.message}`)\n else\n console[logLevel](`Slice failed: ${error.message}\\n`, errorObject);\n\n return reason;\n}\n\n/**\n * Slice has thrown error during execution:\n * Mark slice as failed, compensate when job is dicrete, emit events.\n * @param {Slice} slice\n * @param {Error} error\n */\nSupervisor.prototype.handleFailedSlice = function Supervisor$handleFailedSlice (slice, error)\n{\n assert(error, 'error must be valid');\n slice.collectResult(error, false);\n\n // If the slice from a job never completes and the job address exists in the ringBufferofJobs,\n // then we remove it to allow for another slice (from the same job) to be obtained by fetchTask\n this.ringBufferofJobs.buf = this.ringBufferofJobs.filter(element => element !== slice.jobAddress);\n\n this.emit('submitSliceFailed', error);\n this.emit('submitFinished');\n}\n\n// _Idx\n//\n// Return slices and sent progress reports to result-submitter-results.\n// Return slices to result-submitter-status which marks the\n// corresponding row in activeSlices to be rescheduled on usually another worker.\n//\n\n/**\n * Bulk-return multiple slices, possibly for assorted jobs.\n * Returns slices to the scheduler to be redistributed.\n * Called in the sandbox terminate handler and purgeAllWork(jobAddress)\n * and stopWork(forceTerminate).\n *\n * @param {Slice[]} slices - The slices to return to the scheduler.\n * @param {string} [reason='unknown'] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @param {boolean} [removeSlices=true] - When true, removes slices from this.sliceInventory .\n * @returns {Promise<*>} - Response from the scheduler.\n */\nSupervisor.prototype.returnSlices = function Supervisor$$returnSlices (slices, reason = 'unknown', removeSlices = true)\n{\n if (!slices || !slices.length) return Promise.resolve();\n debugging('supervisor') && console.debug('Supervisor.returnSlices: Returning slices', slices.map(slice => slice.identifier));\n\n const slicePayload = constructReturnSliceBuckets(slices, reason);\n if (removeSlices) slices.forEach((slice) => slice.jobManager.removeSlice(slice));\n\n try\n {\n return this.resultSubmitter.send('status', {\n worker: this.workerId,\n slices: slicePayload,\n }).catch(error => {\n const errorInfo = slices.map(slice => slice.identifier).sort();\n debuggingError && console.error('Failed to return slice(s)', { errorInfo, error }, 'Will try again on new connection.');\n return this.saveForResubmitToRS('status', { worker: this.workerId, slices: slicePayload });\n });\n }\n catch (error)\n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to return slices ${compressSlices(slices)}, no connection to result submitter:`, error);\n }\n}\n\n/** XXXpfr @todo TEMP -- Remove when sup2 replaces sup1 */\nfunction compressSlices(sliceArray)\n{\n const jobSliceMap = toJobMap(sliceArray, slice => slice.sliceNumber);\n return compressJobMap(jobSliceMap, false /* skipFirst*/, addressTruncationLength);\n}\n\n/**\n * Takes a slice and returns it to the scheduler to be redistributed.\n * Usually called when an exception is thrown by sandbox.work(...) .\n * Or when the supervisor tells it to forcibly stop working.\n *\n * @param {Slice} slice - The slice to return to the scheduler.\n * @param {string} [reason] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @returns {Promise<*>} - Response from the scheduler.\n */\nSupervisor.prototype.returnSlice = function Supervisor$$returnSlice (slice, reason)\n{\n assert(slice.sliceNumber > 0 && slice.jobManager);\n debugging() && console.debug(`Supervisor.returnSlice: Returning slice ${slice.identifier} with reason ${reason}.`);\n\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n\n try\n {\n slice.jobManager.removeSlice(slice);\n const payload = slice.getReturnMessagePayload(this.workerId, reason);\n return this.resultSubmitter.send('status', payload)\n .catch(error => {\n debuggingError && console.error('Failed to return slice', {\n sliceNumber: slice.sliceNumber,\n jobAddress: slice.jobAddress,\n status: slice.state.valueOf(),\n error,\n }, 'Will try again on a new connection.');\n return this.saveForResubmitToRS('status', payload);\n });\n }\n catch (error)\n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to return slice ${slice.identifier}, no connection to result submitter:`, error);\n }\n}\n\n/**\n * Send beacon to status.js for 'progress' and sliceStatus.scheduled.\n *\n * Run in an interval created in @constructor .\n * @returns {Promise<*>}\n */\nSupervisor.prototype.emitProgressReport = function emitProgressReport () \n{\n const slices = constructSliceBuckets( this.readySlices(), sliceStatus.scheduled );\n constructSliceBuckets( this.workingSlices(), 'progress', slices );\n\n debugging('supervisor') && console.debug('emitProgressReport:', stringify(slices));\n\n if (slices.length)\n {\n const progressReportPayload = {\n worker: this.workerId,\n slices,\n };\n\n try\n {\n return this.resultSubmitter.send('status', progressReportPayload)\n .catch(error => {\n debuggingError && console.error('479: Failed to send status beacon update:', error/*.message*/);\n return this.saveForResubmitToRS('status', progressReportPayload);\n });\n }\n catch (error) \n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to emit progress report, no connection to result submitter:`, error);\n }\n }\n}\n\n/**\n * Add a slice to the slice buckets being built. If a sliceBucket already exists for the\n * job-status-authMessage tuple, then the slice will be added to that, otherwise a new\n * sliceBucket will be added to the buckets.\n *\n * @param {Slice[]} slices - The slices.\n * @param {String} status - Status update, eg. progress or scheduled.\n * @param {Object[]} [sliceBuckets] - Slice buckets being built. Will be mutated in place.\n * @returns {Object[]} - mutated sliceBuckets array\n */\nfunction constructSliceBuckets (slices, status, sliceBuckets)\n{\n const jobMap = {};\n for (const slice of slices)\n {\n assert(slice.sliceNumber > 0 );\n if (!jobMap[slice.jobAddress]) jobMap[slice.jobAddress] = [];\n\n // Try to find a sliceBucket in the buckets which matches the job, status, and auth message.\n let sliceBucket = jobMap[slice.jobAddress].find(desc => {\n return desc.status === status\n && desc.authorizationMessage === slice.authorizationMessage;\n });\n\n if (!sliceBucket) jobMap[slice.jobAddress].push(slice.getMessage(status));\n else sliceBucket.sliceNumbers.push(slice.sliceNumber);\n }\n\n if (!sliceBuckets) return Object.values(jobMap);\n sliceBuckets.push(...Object.values(jobMap));\n return sliceBuckets;\n}\n \n/**\n * Add a slice to the returnSlice bucket being built. If a sliceBucket already exists for the\n * job-isEstimation-authMessage-reason tuple, then the slice will be added to that, otherwise a new\n * sliceBucket will be added to the buckets.\n *\n * @param {Slice[]} slices - The slices.\n * @param {String} [reason] - Optional reason to further characterize status; e.g. 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @param {Object[]} [sliceBuckets] - Optional slice buckets being built. Will be mutated in place.\n * @returns {Object[]} - mutated sliceBuckets array\n */\nfunction constructReturnSliceBuckets (slices, reason, sliceBuckets)\n{\n const jobMap = {};\n for (const slice of slices)\n {\n assert(slice.sliceNumber > 0 );\n if (!jobMap[slice.jobAddress]) jobMap[slice.jobAddress] = [];\n \n // Try to find a sliceBucket in the buckets which matches the job, estimation status, reason, and auth message.\n let sliceBucket = jobMap[slice.jobAddress].find(desc => {\n return desc.isEstimationSlice === slice.isEstimation\n && desc.authorizationMessage === slice.authorizationMessage\n && desc.reason === reason;\n });\n\n if (!sliceBucket) \n jobMap[slice.jobAddress].push(slice.getMessage('return', { isEstimationSlice: slice.isEstimation, reason }));\n else sliceBucket.sliceNumbers.push(slice.sliceNumber);\n }\n\n if (!sliceBuckets) return Object.values(jobMap);\n sliceBuckets.push(...Object.values(jobMap));\n return sliceBuckets;\n}\n \n// _Idx\n//\n// Task Distributor (TD): requestTask (Rq) support -- communication with TD.\n//\n\n/**\n * XXXpfr @todo Needs Work\n * For a given job, the scheduler stores an EMA approximation of average slice completion time in\n * jobPerfData.sliceCPUTime (and jobPerfData.sliceGPUTime, but we don't do the GPU analysis yet.)\n * However, each worker also tracks the same information and the ratio of local-info to scheduler-info\n * is returned by this.conversionQuantum so we can tell the task distributor how much work to return\n * from requestTask so that the work actually takes 5 minutes to complete when using all the worker sandboxes.\n * Note: \n * We average the completion times over the current jobs.\n * Define completion time in terms of sliceC(G)PUTime and sliceC(G)PUDensity\n * completion-time = (sliceCGPUTime + sliceCGPUTime) / ( sliceCPUDensity + sliceGPUDensity);\n * The local completion time is an EMA approximation of local completion-time as computed by Supervisor.recordResult.\n * The scheduler completion-time is computed directly from the corresponding row in jobPerfData.\n */\nSupervisor.prototype.conversionQuantum = function Supervisor$conversionQuantum()\n{\n let globalSpeed = 0, localSpeed = 0;\n for (const jobMan of this.jobManagerInventory)\n {\n const _globalTime = jobMan.globalTime;\n const _localTime = jobMan.statistics.ema;\n if (_globalTime > 0 && _localTime > 0)\n {\n //console.debug('conversionQuantum: local', _localTime, 'global', _globalTime);\n globalSpeed += _globalTime;\n localSpeed += _localTime;\n }\n }\n const conversion = globalSpeed > 0 ? localSpeed / globalSpeed : 1;\n return Math.min(Math.max(conversion, 0.2), 5.0); // Truncate if conversion is too bizarre.\n}\n\n/**\n * Remove all unreferenced jobs in this.jobManagerInventory and this.moduleCache.\n * Since job-managers are inserted into this.jobManagerInventory with a push, the job managers at the beginning are oldest.\n * Only delete #deleteCount of the oldest job-managers:\n * let deleteCount = this.jobManagerInventory.length - cachedJobsThreshold;\n * Edit cachedJobsThreshold to adjust the cache cleanup threshold.\n * @param {object[]} [newJobKeys=[]] - Jobs that should not be removed from this.jobManagerInventory and this.moduleCache.\n */\nSupervisor.prototype.clearUnusedJobManagersAndModuleCache = function Supervisor$clearUnusedJobManagersAndModuleCache(newJobKeys=[])\n{\n let deleteCount = this.jobManagerInventory.length - this.cachedJobsThreshold;\n if (deleteCount > 0)\n {\n selectiveDebugging && console.debug(`Supervisor.clearUnusedJobManagersAndModuleCache START: deleteCount ${deleteCount}/${this.jobManagerInventory.length}/${this.cachedJobsThreshold}.`);\n const jobMap = {};\n newJobKeys.forEach(jobAddress => { jobMap[jobAddress] = 1; });\n for (const jobManager of this.jobManagerInventory)\n {\n if (!jobMap[jobManager.address])\n {\n const sliceInventory = jobManager.sliceInventory.filter(slice => slice.isActive || slice.isQueued);\n if (sliceInventory.length < 1)\n {\n this.purgeJob(jobManager);\n if (--deleteCount < 1)\n break;\n }\n }\n }\n selectiveDebugging && console.debug(`Supervisor.clearUnusedJobManagersAndModuleCache FINISH: deleteCount ${deleteCount}/${this.jobManagerInventory.length}/${this.cachedJobsThreshold}.`);\n }\n}\n\n/**\n * Ask the scheduler (task distributor) for work.\n * @param {number} [unusedSandboxSlots]\n * @param {object[]} [jobs=[]] \n */\nSupervisor.prototype.requestTask = function Supervisor$requestTask (unusedSandboxSlots, jobs = [])\n{\n if (!this.isReady() || this.isFetchingNewWork)\n return Promise.resolve();\n\n if(!unusedSandboxSlots) unusedSandboxSlots = this.unusedSandboxSlots();\n if (unusedSandboxSlots < 1)\n {\n debugging('supervisor') && console.debug('requestTask: There are no unused sandbox slots.');\n return Promise.resolve();\n }\n\n // Refresh connections.\n this.instantiateAllConnections();\n\n // We prune for over this.maxTotalSandboxes about every 15 seconds, or when must prune level is reached.\n if (this.sandboxCount() > this.mustPruneSandboxLevel || Date.now() > this.lastPrune + this.pruneFrequency)\n {\n this.lastPrune = Date.now();\n this.pruneSandboxes();\n }\n\n try\n {\n this.isFetchingNewWork = true;\n const numCPUSlotToFill = this.numberOfAvailableSandboxSlots(unusedSandboxSlots);\n if (numCPUSlotToFill < 1)\n {\n //debugging() && console.debug('Predicted workload too high; not fetching additional work yet'); <-- Save Wes' msg...\n debugging() && console.debug('Supervisor.requestTask: We have enough, so start executing some slices.');\n return this.roundRobinSlices(); // roundRobinSlices guarantees this.isFetchingNewWork === false\n }\n\n /** XXXpfr @todo Get together with Wes to figure this out. */\n //let predictedLoad = this.predictLoad(Date.now() + ms(this.tuning.prefetchInterval)).load;\n\n const request = {\n numCores: numCPUSlotToFill,\n coreStats: this.getStatisticsCPU(),\n numGPUs: this.numGPU,\n //targetLoad: this.targetLoad.subtract(predictedLoad), /** XXXpfr @todo Get together with Wes to figure this out. */\n conversionQuantum: this.conversionQuantum(),\n capabilities: this.capabilities,\n paymentAddress: this.paymentAddress,\n jobAddresses: jobs.concat(this.options.jobAddresses || []), // When set, only fetches slices for these jobs.\n localExec: this.options.localExec,\n workerComputeGroups: this.generateWorkerComputeGroups(),\n minimumWage: workerTuning.minimumWage || this.options.minimumWage,\n loadedJobs: this.jobManagerInventory.map(jobMan => jobMan.address),\n readyJobs: this.jobManagerInventory.filter(jobMan => jobMan.ready).map(jobMan => jobMan.address),\n previouslyWorkedJobs: this.ringBufferofJobs.buf, // Only discrete jobs.\n rejectedJobs: this.rejectedJobs,\n };\n // Workers should be part of the public compute group by default.\n if (!booley(workerTuning.leavePublicGroup) && !booley(this.options.leavePublicGroup))\n request.workerComputeGroups.push(constants.computeGroups.public);\n\n // Call Task Distributor and handle response with this.addTaskToWorkload.\n this.fetchTask(request, (response) => this.addTaskToWorkload(request, response));\n }\n catch (error)\n {\n // Paranoid double-checking we don't accidently leave a live this.isFetchingNewWork.\n this.isFetchingNewWork = false;\n throw error;\n }\n}\n\n/** Gets the logical and physical number of cores and also the total number of sandboxes the worker is allowed to run. */\nSupervisor.prototype.getStatisticsCPU = function Supervisor$getStatisticsCPU ()\n{\n if (DCP_ENV.isBrowserPlatform)\n {\n return {\n worker: this.workerId,\n lCores: window.navigator.hardwareConcurrency,\n pCores: workerTuning.pCores || window.navigator.hardwareConcurrency,\n sandbox: this.maxWorkingSandboxes,\n }\n }\n\n return {\n worker: this.workerId,\n lCores: requireNative('os').cpus().length,\n pCores: requireNative('physical-cpu-count'),\n sandbox: this.maxWorkingSandboxes,\n }\n}\n\n/**\n * Callback for fetchTask.\n * @param {object} request \n * @param {object} response\n */\nSupervisor.prototype.addTaskToWorkload = function Supervisor$addTaskToWorkload (request, response)\n{\n try\n {\n const payload = response.payload;\n if (!response.success)\n {\n debugging() && console.debug('Task fetch failure; request=', request);\n debugging() && console.debug('Task fetch failure; response=', payload);\n // Only report errors when in 'ready' state.\n if (this.isReady()) throw new DCPError('Unable to fetch task for worker', payload);\n else return;\n }\n\n const sliceCount = payload.body.task.length || 0;\n if (sliceCount < 1)\n {\n if (selectiveDebugging2 && (this.lastTime + 7000 < Date.now()))\n {\n this.lastTime = Date.now();\n // Display the state of every slice.\n if (displaySliceState)\n {\n /** @type {JobManager} */\n const jm = this.jobManagerInventory.top();\n jm.dumpSlices(false /*details*/);\n }\n // Display completed results so far.\n if (displayCompletedResults && this.queuedSliceCount() < 1)\n {\n const values = Object.values(this.resultMap);\n if (values.length > 0)\n {\n values.forEach((descriptor) => descriptor.slices.sort((x,y) => x-y))\n console.debug(`Recorded results: job managers ${values.length}:`, this.resultMap);\n }\n }\n }\n this.emit('fetchedTask', { jobs: 0, slices: sliceCount });\n // There may be an extra slice to process.\n // roundRobinSlices guarantees this.isFetchingNewWork === false;\n return this.roundRobinSlices();\n }\n\n /**\n * payload structure: { owner: this.address, signature: signature, auth: messageLightWeight, body: messageBody };\n * messageLightWeight: { workerId: worker, jobSlices, schedulerId, jobCommissions }\n * messageBody: { newJobs: await-getNewJobsForTask(dbScheduler, task, request), task }\n */\n const { body, ...authorizationMessage } = payload;\n /** @type {{ newJobs: object, task: SliceMessage[] }} */\n const { newJobs, task } = body;\n assert(newJobs); // It should not be possible to have !newJobs -- we throw on !success.\n const newJobKeys = Object.keys(newJobs);\n const jobCount = newJobKeys.length;\n\n /*\n * Ensure all jobs received from the scheduler (task distributor) are:\n * 1. If we have specified specific jobs the worker may work on, the received jobs are in the specified job list\n * 2. If we are in localExec, at most 1 unique job type was received (since localExec workers are designated for only one job)\n * If the received jobs are not within these parameters, stop the worker since the scheduler cannot be trusted at that point.\n */\n if (request.jobAddresses.length > 0 && !newJobKeys.every((ele) => request.jobAddresses.includes(ele))\n || request.localExec && jobCount > 1)\n {\n this.error(\"Worker received slices it shouldn't have. Rejecting the work and stopping.\");\n process.exit(1);\n }\n\n selectiveDebugging && console.debug(`Supervisor.addTaskToWorkload: task: ${task.length}/${request.numCores}/${this.maxWorkingSandboxes}, conversion: ${request.conversionQuantum}, jobs: ${jobCount}, authSlices: ${compressJobMap(authorizationMessage.auth.authSlices, true /* skipFirst*/, addressTruncationLength /* digits*/)}`);\n\n // Clear out job managers w/o any queued slices,\n // and remove corresponding job references from module cache.\n // When a cached module no longer has any job references it is removed from the cache.\n this.clearUnusedJobManagersAndModuleCache(newJobKeys);\n\n // this.jobMap: job.address --> jobManager\n /** @type {Object.<Address, JobManager>} */\n this.jobMap = {};\n this.jobManagerInventory.forEach(jobManager => {\n this.jobMap[jobManager.address] = jobManager;\n });\n\n /** @type {Object.<Address, SliceMessage[]>} */\n const jobSliceMap = {};\n task.forEach((element) => {\n const address = String(element.jobAddress);\n if (!jobSliceMap[address]) jobSliceMap[address] = [element];\n else jobSliceMap[address].push(element);\n });\n\n debugging('supervisor') && console.debug('requestTask: slices, newJobs and jobMap', task.length, Object.keys(newJobs), Object.keys(this.jobMap));\n\n // Populate the job managers with slices, creating new job managers when necessary.\n // Set up discrete job ring buffer.\n for (const [jobAddress, jobEl] of Object.entries(newJobs))\n {\n if (this.jobMap.hasOwnProperty(jobAddress))\n {\n /** @type {JobManager} */\n const jm = this.jobMap[jobAddress];\n jm.update(jobEl, jobSliceMap[jobAddress], authorizationMessage);\n }\n else\n {\n // Add the slice messages to the job manager ctor, so that slice construction is after job manager is ready.\n const jobManager = new JobManager(this, jobEl, jobSliceMap[jobAddress], authorizationMessage);\n this.jobMap[jobAddress] = jobManager;\n this.jobManagerInventory.push(jobManager);\n\n // Populate the ring buffer based on job's discrete property.\n assert(jobEl.requirements);\n if (jobEl.requirements.discrete && this.ringBufferofJobs.find(address => address === jobEl.address) === undefined)\n this.ringBufferofJobs.push(jobEl.address);\n }\n }\n\n /**\n * The requestTask event fires when the supervisor has finished trying to\n * fetch work from the scheduler (task distributor). The data emitted is the\n * number of jobs and new slices to work on in the fetched task.\n *\n * @event Supervisor#requestTask\n * @type {object}\n */\n this.emit('fetchedTask', { jobs: jobCount, slices: sliceCount });\n\n // Start working on the slices.\n setImmediate(() => this.roundRobinSlices());\n }\n catch (error)\n {\n this.isFetchingNewWork = false; // Paranoid double checking that we don't leave this.isFetchingNewWork live.\n this.emit('fetchTaskFailed', error);\n debuggingError && console.error('Supervisor.requestTask failed!', error);\n }\n}\n\n/**\n * Returns the number of unused sandbox slots to fill -- sent to requestTask.\n * @returns {number}\n */\nSupervisor.prototype.numberOfAvailableSandboxSlots = function Supervisor$numberOfAvailableSandboxSlots(unusedSandboxSlots)\n{\n const _readySlices = this.readySlices();\n let numCores;\n if (this.options.priorityOnly && this.options.jobAddresses.length === 0)\n numCores = 0;\n else if (_readySlices.length > 1) // We have slices ready, no need to fetch.\n numCores = 0;\n else\n {\n // There are almost no ready slices (there may be 0 or 1), fetch a full task.\n // The task is full, in the sense that it will contain slices whose\n // aggregate execution time is this.maxWorkingSandboxes * 5-minutes.\n // However, there can only be unusedSandboxSlots # of long slices.\n // Thus we need to know whether the last slice in this.readySlices() is long or not.\n // (A long slice has estimated execution time >= 5-minutes on an average worker.)\n const longSliceCount = (_readySlices.length > 0 && _readySlices[0].isLong) ? 1 : 0;\n numCores = unusedSandboxSlots - longSliceCount;\n debugging('supervisor') && console.debug('numberOfAvailableSandboxSlots', numCores, unusedSandboxSlots, longSliceCount);\n }\n return numCores;\n}\n\n/**\n * @callback cbAddTaskToWorkload\n * @param {Response} response\n * @returns {Promise<void>}\n */\n\n/**\n * Call to fetch new slices from task distributor.\n * @param {*} request\n * @param {cbAddTaskToWorkload} addTaskToWorkload\n * @returns {Promise<void>}\n */\nSupervisor.prototype.fetchTask = async function Supervisor$fetchTask (request, addTaskToWorkload)\n{\n // Fetch a new task if we have insufficient slices queued, then start workers\n assert(this.isFetchingNewWork);\n\n this.instantiateAllConnections();\n\n // Top up sandboxes when necessary.\n const moreSandboxes = this.maxWorkingSandboxes - this.sandboxCount();\n if (moreSandboxes > 0)\n {\n await this.carefullyCreateSandboxes(moreSandboxes)\n .then(() => this.checkCapabilities()); /** XXXpfr @todo Do we need to check capabilities again? */\n }\n\n const fetchTimeout = setTimeout(() => {\n this.isFetchingNewWork = false;\n this.emit('warning', 'Fetch exceeded timeout, will reconnect at next watchdog interval');\n safeClose(this.taskDistributor, 'Fetch timed out', Math.random() > 0.5).catch(error => {\n this.error('Failed to close task-distributor connection', error);\n });\n safeClose(this.resultSubmitter, 'Fetch timed out', Math.random() > 0.5).catch(error => {\n this.error('Failed to close result-submitter connection', error);\n });\n this.instantiateAllConnections();\n }, 3 * 60 * 1000); // Max out at 3 minutes to fetch.\n\n // Ensure result submitter and task distributor connections before fetching tasks.\n try\n {\n await this.taskDistributor.keepalive();\n await this.resultSubmitter.keepalive();\n await this.taskDistributor.keepalive();\n }\n catch (e)\n {\n this.isFetchingNewWork = false;\n this.error('Failed to connect to result submitter, refusing to fetch slices. Will try again at next fetch cycle.', e);\n clearTimeout(fetchTimeout);\n safeClose(this.taskDistributor, 'Failed to connect to task-distributor', true).catch(error => {\n this.error('Failed to close task-distributor connection', error);\n });\n safeClose(this.resultSubmitter, 'Failed to connect to result-submitter', true).catch(error => {\n this.error('Failed to close result-submitter connection', error);\n });\n return Promise.resolve();\n }\n\n this.emit('fetchingTask');\n\n if (!this.taskDistributor) return\n return this.taskDistributor.send('requestTask', request)\n .then((response) => {\n addTaskToWorkload(response);\n })\n .catch((error) => {\n this.isFetchingNewWork = false; // Redundant.\n this.emit('fetchTaskFailed', error);\n this.error('Unable to request task from scheduler. Will try again on a new connection.', error);\n safeClose(this.taskDistributor, 'Failed to request task.', true);\n })\n .finally(() => {\n this.isFetchingNewWork = false;\n clearTimeout(fetchTimeout);\n });\n}\n\n/**\n * Generate the workerComputeGroups property of the requestTask message. \n * \n * Concatenate the compute groups object from dcpConfig with the list of compute groups\n * from the supervisor, and remove the public group if accidentally present. Finally,\n * we transform joinSecrets/joinHash into joinHashHash for secure transmission.\n *\n * @note computeGroup objects with joinSecrets are mutated to record their hashes. This\n * affects the supervisor options and dcpConfig. Re-adding a joinSecret property\n * to one of these will cause the hash to be recomputed.\n */\nSupervisor.prototype.generateWorkerComputeGroups = function Supervisor$generateWorkerComputeGroups ()\n{\n let computeGroups = Object.values(workerTuning.computeGroups || {});\n if (this.options.computeGroups)\n computeGroups = computeGroups.concat(this.options.computeGroups);\n computeGroups = computeGroups.filter(group => group.id !== constants.computeGroups.public.id);\n const hashedComputeGroups = [];\n for (const group of computeGroups)\n {\n const groupCopy = Object.assign({}, group);\n if ((group.joinSecret || group.joinHash) && (!group.joinHashHash || this.lastDcpsid !== this.taskDistributor.dcpsid))\n {\n let joinHash;\n if (group.joinHash)\n joinHash = group.joinHash.replace(/\\s+/g, ''); // strip whitespace\n else\n joinHash = calculateJoinHash(groupCopy);\n\n groupCopy.joinHashHash = hash.calculate(hash.eh1, joinHash, this.taskDistributor.dcpsid);\n delete groupCopy.joinSecret;\n delete groupCopy.joinHash;\n debugging('computeGroups') && console.debug(`Calculated joinHash=${joinHash} for`, groupCopy);\n }\n hashedComputeGroups.push(groupCopy);\n }\n this.lastDcpsid = this.taskDistributor.dcpsid;\n debugging('computeGroups') && console.debug('Requesting ', computeGroups.length, 'non-public groups for session', this.lastDcpsid);\n return hashedComputeGroups;\n}\n\n// _Idx\n//\n// Aggregators from the job managers.\n// Note: Not all functions are used yet.\n//\n/** XXXpfr @todo Figure out which aggregators to keep. */\n\n/**\n * Gather the count of job managers with queuedSlices.\n * @returns {number}\n */\nSupervisor.prototype.activeJobCount = function Supervisor$activeJobCount ()\n{\n let count = 0;\n this.jobManagerInventory.forEach((jobManager) => {\n if (jobManager.queuedSlices.length > 0) count++;\n });\n return count;\n}\n\n/**\n * Gather the ready slices from the job managers.\n * @returns {Slice[]}\n */\nSupervisor.prototype.readySlices = function Supervisor$readySlices ()\n{\n const readySlices = [];\n this.jobManagerInventory.forEach((jobManager) => {\n readySlices.push(...jobManager.readySlices);\n });\n return readySlices;\n}\n\n/**\n * Gather the working slices in the job managers.\n * @returns {Slice[]}\n */\nSupervisor.prototype.workingSlices = function Supervisor$workingSlices ()\n{\n const workingSlices = [];\n this.jobManagerInventory.forEach((jobManager) => {\n workingSlices.push(...jobManager.workingSlices);\n });\n return workingSlices;\n}\n\n/**\n * Gather the count of various kinds of slices over all the job managers.\n * @param {string} predicate - 'all;, 'ready', 'queued', 'reserved', 'working', 'workingOnly'.\n * @returns {number}\n */\nSupervisor.prototype.predicateSliceCount = function Supervisor$predicateSliceCount (predicate)\n{\n let count = 0;\n switch (predicate)\n {\n case 'all':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.sliceInventory.length;\n });\n break\n case 'ready':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.readySlices.length;\n });\n break;\n case 'queued':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.queuedSlices.length;\n });\n break;\n case 'reserved':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.reservedSlices.length;\n });\n break;\n case 'working': // both working and reserved (viz., soon-to-be-working)\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.workingSlices.length;\n });\n break;\n case 'workingOnly':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.workingSlicesOnly.length;\n });\n break;\n }\n return count;\n}\n/** @returns {number} */\nSupervisor.prototype.sliceCount = function Supervisor$sliceCount () { return this.predicateSliceCount('all'); }\n/** @returns {number} */\nSupervisor.prototype.readySliceCount = function Supervisor$readySliceCount () { return this.predicateSliceCount('ready'); }\n/** @returns {number} */\nSupervisor.prototype.queuedSliceCount = function Supervisor$queuedSliceCount () { return this.predicateSliceCount('queued'); }\n/** @returns {number} */\nSupervisor.prototype.reservedSliceCount = function Supervisor$reservedSliceCount () { return this.predicateSliceCount('reserved'); }\n/** @returns {number} */\nSupervisor.prototype.workingSliceCount = function Supervisor$workingSliceCount () { return this.predicateSliceCount('working'); }\n/** @returns {number} */\nSupervisor.prototype.workingSliceOnlyCount = function Supervisor$workingSliceOnlyCount () { return this.predicateSliceCount('workingOnly'); }\n\n/**\n * Gather the count of working sandboxes over all the job managers.\n * @returns {number}\n */\nSupervisor.prototype.sandboxCount = function Supervisor$sandboxCount ()\n{\n return this.readiedSandboxes.length + this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated).length;\n}\n\n/**\n * Gather the count of working sandboxes over all the job managers.\n * @returns {number}\n */\nSupervisor.prototype.workingSandboxCount = function Supervisor$workingSandboxCount ()\n{\n return this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated && sandbox.isWorking).length;\n}\n\n// _Idx\n//\n// Sandbox creation and management.\n// \n\n/**\n * Create and start a Sandbox.\n * When this.readiedSandboxes.length > 0, use one of those sandboxes, instead of creating a new one.\n * @param {number} [delayMs=0] - The delay ms when calling sandbox.start(delayMs) .\n * @returns {Promise<Sandbox>}\n */\nSupervisor.prototype.createSandbox = function Supervisor$createSandbox (delayMs = 0)\n{\n const that = this;\n function getReadiedSandbox()\n {\n const sandbox = that.readiedSandboxes.pop();\n that.sandboxInventory.push(sandbox);\n return Promise.resolve(sandbox);\n }\n\n if (this.readiedSandboxes.length > 0)\n return getReadiedSandbox();\n\n // Do not place in this.readiedSandboxes, we'll directly use the return value of createSandbox.\n return this.createNewSandbox(delayMs, true/*putInInventory*/)\n .catch(() => {\n return this.carefullyCreateSandboxes(1)\n .then(() => {\n return getReadiedSandbox();\n });\n });\n}\n \n/**\n * Create and start a Sandbox.\n * Store it in this.readiedSandboxes or this.sandboxInventory according to putInInventory.\n * @param {number} [delayMs=0] - Millisecond delay when calling sandbox.start(delayMs), otherwise return it and use it.\n * @param {boolean} [putInInventory=false]\n * @returns {Promise<Sandbox>}\n */\nSupervisor.prototype.createNewSandbox = function Supervisor$createNewSandbox (delayMs = 0, putInInventory = false)\n{\n const rawSandbox = new Sandbox(this, { ...this.options.sandboxOptions });\n this.hookUpSandboxListeners(rawSandbox);\n return rawSandbox.start(delayMs)\n .then((sandbox) => {\n if (putInInventory) this.sandboxInventory.push(sandbox);\n else this.readiedSandboxes.push(sandbox);\n return sandbox;\n })\n .catch((error) => {\n if (!error) error = new Error('Unknown error creating sandbox.');\n debuggingWarn && console.warn(`Supervisor.createNewSandbox: Failed to start sandbox ${rawSandbox.identifier}.`, error);\n rawSandbox.terminate(false);\n if (error.code === 'ENOWORKER')\n throw new DCPError(\"Cannot use localExec without dcp-worker installed. Use the command 'npm install dcp-worker' to install the neccessary modules.\", 'ENOWORKER');\n throw error;\n });\n}\n\n/**\n * Bulk: create and start sandboxes and save in this.readiedSandboxes.\n * Call this function when there's a chance the evaluator is down.\n * @param {number} count - The number of sandboxes to create.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.carefullyCreateSandboxes = async function Supervisor$carefullyCreateSandboxes (count)\n{\n if (count < 1) return;\n // If the evaluator cannot start (e.g. if the evalServer is not running),\n // then the while loop will keep retrying until the evalServer comes online.\n let retry = 0;\n while (true)\n {\n try\n {\n await this.createNewSandbox();\n if (count > 1)\n this.createSandboxes(count - 1);\n }\n catch (error)\n {\n if (error.code === 'ENOWORKER') throw error;\n // Now assume the evaluator is down and keep retrying.\n /** XXXpfr @todo Need better indicator that evaluator is down. */\n if ((retry++ % 6) === 0)\n this.error(`Failed to ready sandboxes; will keep retrying: ${this.checkCode(error)}`);\n await a$sleepMs(1000 * Math.max(5, retry));\n }\n }\n}\n\n/**\n * Bulk: create and start sandboxes and save in this.readiedSandboxes.\n * @param {number} count - The number of sandboxes to create.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.createSandboxes = async function Supervisor$createSandboxes (count)\n{\n assert(count > 0);\n const promises = [], errors = [];\n for (let k = 0; k < count; k++)\n {\n promises.push(\n this.createNewSandbox(k === 0 ? 0: this.delayMs())\n .catch((error) => errors.push(this.checkCode(error))));\n }\n\n await Promise.all(promises);\n\n if (errors.length)\n this.emit('warning', `Failed to ready ${errors.length} of ${count} sandboxes: ${errors.map(err => err.message)}`);\n\n // Sort so that pop() will return sandboxes in increasing order.\n this.readiedSandboxes.sort((x,y) => y.id - x.id);\n\n debugging('supervisor') && console.debug(`createSandboxes: Created ${count-errors.length} sandboxes.`, this.readiedSandboxes.map(s => s.id));\n}\n\n/**\n * For a given sandbox, hook up all the Sandbox listeners.\n * @param {Sandbox} sandbox \n */\nSupervisor.prototype.hookUpSandboxListeners = function hookUpSandboxListeners (sandbox) \n{\n sandbox.addListener('ready', () => this.emit('sandboxReady', sandbox));\n\n sandbox.addListener('start', () => {\n this.emit('sandboxStart', sandbox);\n\n if (sandbox.slice)\n {\n try\n {\n const statusPayload = sandbox.slice.getMessagePayload(this.workerId, 'begin');\n return this.resultSubmitter.send('status', statusPayload).catch((error) => {\n debuggingError && console.error(`Error sending 'status' for slice ${sandbox.slice.identifier}:\\n\\t${error}\\n\\tWill try again on a new connection`);\n return this.saveForResubmitToRS('status', statusPayload);\n });\n } \n catch (error)\n {\n /* resultSubmitterConnection can be null if worker is stopped */\n debuggingError && console.error(`Failed to send 'begin' status for slice ${sandbox.slice.identifier}, no connection to result submitter`, error);\n }\n }\n });\n\n sandbox.addListener('workEmit', ({ eventName, payload }) => {\n // Need to check if the sandbox hasn't been assigned a slice yet.\n if (!sandbox.slice)\n this.error(`Sandbox not assigned a slice before sending workEmit message to scheduler.\\n\\t'workEmit' event originates from '${eventName}' event`);\n else\n {\n const slice = sandbox.slice;\n // Sometimes a sliceNumber===0 workEmit comes in before the client bundle is properly loaded.\n // Also happens with minor dcp-client version mismatches.\n // sliceNumber===0 <==> authorizationMessage undefined...\n if (!slice.authorizationMessage)\n this.emit('warning', `workEmit: missing authorization message for slice ${slice.identifier}`);\n else if (this.eventRouter) // No reason to emit if event router is closed.\n {\n const workEmitPayload = {\n eventName,\n payload,\n job: slice.jobAddress,\n slice: slice.sliceNumber,\n worker: this.workerId,\n authorizationMessage : slice.authorizationMessage,\n };\n\n const workEmitPromise = this.eventRouter.send('workEmit', workEmitPayload).catch(error => {\n debuggingWarn && console.warn(`workEmit: Unable to send ${eventName} for slice ${slice.identifier}: ${error.message}.\\n\\tTrying again on a new connection.`);\n this.eventRouterMessageQueue.push({ operation: 'workEmit', data: workEmitPayload })\n safeClose(this.eventRouter); // stopWork could slip-in during eventRouter.send\n if (this.debugBuild) this.error('workEmit error:', error);\n });\n\n if (this.debugBuild)\n {\n workEmitPromise.then(result => {\n if (!result) this.emit('warning', `workEmit: Event router did not accept event ${eventName}`);\n });\n }\n }\n }\n });\n\n sandbox.on('rejectedWorkMetrics', (data) => {\n // If the slice already has rejectedTimeReport, add this data to it. If not, assign this data to slices rejectedTimeReport property\n if (sandbox.slice) \n {\n if (!sandbox.slice.rejectedTimeReport) sandbox.slice.rejectedTimeReport = data.timeReport;\n else \n {\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (data.timeReport[key]) sandbox.slice.rejectedTimeReport[key] += data.timeReport[key];\n })\n }\n }\n });\n\n // If the sandbox terminated and we are not shutting down, then we should return all work which is\n // currently not being computed if all sandboxes are dead and the attempt to create a new one fails.\n sandbox.on('terminated', async () => {\n let nonTerminatedSandboxes = this.sandboxInventory.filter(sbx => !sbx.isTerminated);\n if (nonTerminatedSandboxes.length === 0 && this.worker.working)\n {\n debugging('supervisor') && console.debug(`hookUpSandboxListeners: Try to create 1 sandbox in the sandbox-terminated-handler.`);\n const _sandbox = await this.createNewSandbox()\n .catch((error) => {\n debugging('supervisor') && console.warn('Failed to replace terminated sandbox; evalserver may be gone.', error.message);\n error.message = 'Failed to replace terminated sandbox: ' + error.message;\n this.emit('warning', error);\n });\n\n // If we cannot create a new sandbox, that probably means we're on a screensaver worker\n // and the screensaver is down. So return the slices to the scheduler.\n if (!_sandbox) this.screenSaverDestroy();\n }\n });\n\n sandbox.on('error', (error) => this.emit('error', error));\n sandbox.on('warning', (warning) => this.emit('warning', warning));\n}\n\n/**\n * Terminate extra sandboxes over the limit: this.maxTotalSandboxes.\n * First terminate assigned sandboxes which are unlikely to be used with the current ready slices.\n * Then terminate the unassigned sandboxes: this.readiedSandboxes.\n * (There should be no readied sandboxes at this point.)\n * Then round-robin prune 1 assigned sandbox from each jobmanager.\n * XXXpfr @todo Prioritize sandboxes that we wish to keep.\n * E.g. When a sandbox is especially expensive to assign.\n */\nSupervisor.prototype.pruneSandboxes = function Supervisor$pruneSandboxes () \n{\n let pruneCount = this.sandboxCount() - this.maxTotalSandboxes;\n if (pruneCount <= 0) return;\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes START: pruneCount ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n // Only prune the extras: jm.assignedSandboxes.length > jm.queuedSlices.length .\n // Round-robin prune 1 extra assigned sandbox from each jobmanager.\n const readyJobManagers = this.jobManagerInventory.filter(jm => jm.ready);\n while (true)\n {\n const _pruneCount = pruneCount;\n for (const jm of readyJobManagers)\n {\n if (jm.pruneExtraAssignedSandbox())\n {\n if (--pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n }\n if (pruneCount === _pruneCount)\n break;\n }\n assert(pruneCount > 0);\n // Prune the excess non-assigned sandboxes -- we should never hit this.\n if (this.readiedSandboxes.length > 0)\n {\n const toPrune = this.readiedSandboxes.slice(0, pruneCount);\n this.readiedSandboxes = this.readiedSandboxes.slice(pruneCount);\n toPrune.forEach(sandbox => sandbox.terminate(false));\n pruneCount -= toPrune.length;\n if (pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n // Round-robin prune 1 assigned sandbox from each jobmanager.\n while (true)\n {\n const _pruneCount = pruneCount;\n for (const jm of readyJobManagers)\n {\n if (jm.pruneAssignedSandbox())\n {\n if (--pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n }\n if (pruneCount === _pruneCount)\n break;\n }\n this.sandboxInventory = this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated);\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n}\n\n// _Idx\n//\n// Result-submitter-result support functions.\n// Send in the results!!!\n//\n\n/**\n * Submits the slice results to the result-submitter service.\n * Then remove the slice from the its job manager.\n *\n * @param {Slice} slice - The slice to submit.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.recordResult = function Supervisor$recordResult (slice)\n{\n // It is possible for slice.result to be undefined when there are upstream errors.\n if ( !(slice && slice.result))\n throw new Error(`recordResult: slice.result is undefined for slice ${slice.identifier}. This is ok when there are upstream errors.`); \n if (!slice.isComplete)\n throw new Error('Cannot record result for slice that has not completed execution successfully.');\n\n debugging('supervisor') && console.debug(`supervisor: recording result for slice ${slice.identifier}.`);\n\n /* @see result-submitter::result for full message details */\n const metrics = { GPUTime: 0, CPUTime: 0, CPUDensity: 0, GPUDensity: 0, total: 0 };\n const payloadData = {\n slice: slice.sliceNumber,\n job: slice.jobAddress,\n worker: this.workerId,\n paymentAddress: this.paymentAddress,\n metrics,\n authorizationMessage: slice.authorizationMessage,\n }\n\n const timeReport = slice.timeReport;\n if (timeReport)\n {\n debugging('supervisor') && console.debug('recordResult timeReport', timeReport);\n // If slice takes less than 1ms to execute, CPUTime will be 0, so compensate.\n if (timeReport.CPU < 1)\n {\n timeReport.CPU++;\n timeReport.total++;\n }\n if (timeReport.total < timeReport.CPU + timeReport.webGL)\n {\n // Compensate or throw? For now we compensate.\n debuggingWarn && console.warn(`Supervisor.recordResult:: Inconsistent time report -- total < CPU + webGL -- ${stringify(timeReport)}`)\n //throw new Error(`recordResult: Inconsistent time report -- total < CPU + webGL -- ${stringify(timeReport)}`)\n timeReport.total = timeReport.CPU + timeReport.webGL;\n }\n if (timeReport.total > 0)\n {\n slice.jobManager.updateStatistics(timeReport);\n metrics.total = timeReport.total;\n metrics.CPUTime = timeReport.CPU;\n metrics.GPUTime = timeReport.webGL;\n metrics.CPUDensity = metrics.CPUTime / timeReport.total;\n metrics.GPUDensity = metrics.GPUTime / timeReport.total;\n }\n }\n\n this.emit('submittingResult');\n\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n\n if (slice.resultStorageType === 'pattern')\n return this.sendResultToRemote(slice)\n .then((response) => {\n payloadData.result = response;\n this.sendToResultSubmitter(slice, payloadData);\n });\n\n payloadData.result = encodeDataURI(slice.result.result);\n return this.sendToResultSubmitter(slice, payloadData);\n}\n\n/**\n * @param {Slice} slice\n * @param {*} payloadData\n * @returns {Promise<void>}\n */\nSupervisor.prototype.sendToResultSubmitter = function Supervisor$sendToResultSubmitter (slice, payloadData)\n{\n const that = this;\n function handleRSError (error, payloadData)\n {\n that.error(`Failed to submit results to scheduler for slice ${payloadData.slice} of job ${payloadData.job}`, error);\n //slice.jobManager.dumpSlices('recordResult');\n that.saveForResubmitToRS('result', payloadData)\n .then((msg) => {\n if (!error && msg) error = new Error(`resultSubmitter is ${msg}`);\n that.emit('submitSliceFailed', error);\n throw error;\n });\n }\n\n try\n {\n debugging('supervisor') && console.debug('Supervisor.recordResult: payloadData', payloadData.result.slice(0, 256), slice.identifier);\n\n return this.resultSubmitter.send('result', payloadData)\n .then((resp) => {\n if (!resp.success)\n throw resp.payload;\n\n debugging('supervisor') && console.debug('recordResult: SUCCESS', slice.identifier);\n\n const receipt = {\n accepted: true,\n payment: resp.payload.slicePaymentAmount,\n };\n this.emit('submittedResult', resp.payload);\n this.emit('dccCredit', receipt);\n })\n .catch ((error) => {\n handleRSError (error, payloadData);\n });\n }\n catch (error)\n {\n handleRSError (error, payloadData);\n }\n finally\n {\n slice.markAsFinished();\n this.emit('submitFinished');\n // Remove the slice from the job manager.\n slice.jobManager.removeSlice(slice);\n if (this.sliceTiming)\n {\n slice['resultDelta'] = Date.now() - slice['resultDelta'];\n console.debug(`recordResult(${slice['queueingDelta']}, ${slice['executionDelta']}, ${slice['resultDelta']}): Completed slice ${slice.identifier}.`);\n }\n if (selectiveDebugging)\n {\n if (!this.resultMap[slice.jobAddress]) this.resultMap[slice.jobAddress] = { slices: [], totalTimes: [] };\n this.resultMap[slice.jobAddress].slices.push(slice.sliceNumber);\n this.resultMap[slice.jobAddress].totalTimes.push(payloadData.metrics.total);\n }\n }\n}\n\n/**\n * Send a work function's result to a server that speaks our DCP Remote Data Server protocol.\n * E.g. https://gitlab.com/Distributed-Compute-Protocol/dcp-rds\n *\n * @param {Slice} slice - Slice object whose result we are sending.\n * @returns {Promise<string>}\n * @throws When HTTP status not in the 2xx range.\n */\nSupervisor.prototype.sendResultToRemote = function Supervisor$sendResultToRemote (slice)\n{ \n /** XXXpfr @todo: Support file upload and other contentTypes. */\n function serializeContent(result, postParams) {\n const ctArray = postParams.contentType.split(';');\n switch (ctArray[0]) {\n case 'application/json':\n return JSON.stringify(result);\n case 'application/kvin':\n return (__webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\").serialize)(result);\n case 'application/octet-stream':\n case 'application/gzip':\n if (ctArray.length === 2 && ctArray[1] === 'base64')\n return btoa(result);\n // fall-through\n case 'text/plain':\n case 'text/html':\n case 'text/css':\n case 'text/csv':\n case 'text/javascript':\n return result;\n\n default:\n {\n const mtArray = ctArray[0].split('/');\n if (mtArray[0] === 'image' || mtArray[0] === 'video' || mtArray[0] === 'audio')\n return (ctArray.length === 2 && ctArray[1] === 'base64') ? btoa(result) : result;\n throw new Error(`sendResultToRemote: Unsupported contentType ${postParams.contentType}`);\n }\n }\n }\n\n // Construct postParams.\n const postParams = { ...slice.resultStorageParams };\n if (!postParams.contentType)\n postParams.contentType = 'application/json';\n if (!postParams.element)\n postParams.element = slice.sliceNumber;\n debugging('supervisor') && console.debug('sendResultToRemote postParams: ', postParams);\n\n // Construct result.\n const result = slice.result.result;\n if (result)\n postParams.content = serializeContent(result, postParams);\n else\n postParams.error = serializeContent(slice.error, postParams);\n debugging('supervisor') && console.debug('sendResultToRemote content: ', (result ? postParams.content : postParams.error).slice(0, 512));\n\n // Construct url.\n const sliceResultUri = makeValueURI('pattern', slice.resultStorageDetails, {\n slice: slice.sliceNumber,\n job: slice.jobAddress,\n });\n debugging() && console.debug('sendResultToRemote sliceResultUri: ', sliceResultUri);\n const url = new DcpURL(sliceResultUri);\n\n // Check allowed origins.\n if (this.makeSafeOriginList('sendResults').indexOf(url.origin) === -1)\n throw new Error(`Invalid origin for remote result storage: '${url.origin}'`);\n\n // Fetch.\n return justFetch(url, 'JSON', 'POST', false, postParams)\n .then((response) => encodeDataURI(JSON.stringify(response)));\n}\n\n// _Idx\n//\n// Reject.\n//\n\n/**\n * Handles reassigning or returning a slice that was rejected by a sandbox.\n *\n * If the slice does not have a rejected property already, reassign the\n * slice to a new sandbox and add a rejected property to the slice to\n * indicate it has already rejected once.\n *\n * If the slice rejects with a reason, or has a rejected time stamp\n * (ie. has been rejected once already) then return all slices from the\n * job to the scheduler and terminate all sandboxes with that jobAddress.\n *\n * The sandbox will be terminated.\n *\n * @param {Sandbox} sandbox\n * @param {Slice} slice\n */\nSupervisor.prototype.handleWorkReject = async function Supervisor$handleWorkReject (sandbox, slice, rejectReason)\n{\n debugging() && console.debug('handleWorkReject', rejectReason, slice.rejectedTimeStamp, slice.identifier);\n\n // Do a hard flush of the microtask queue and finish the current event loop.\n await new Promise((resolve) => setImmediate(() => setTimeout(resolve, 0)));\n\n const jobManager = slice.jobManager;\n jobManager.rejectedJobReasons.push(rejectReason); // memoize reasons\n\n // First time rejecting without a reason. Try assigning slice to a new sandbox.\n if (rejectReason === 'false' && !slice.rejectedTimeStamp)\n {\n // Set rejected time stamp.\n slice.rejectedTimeStamp = Date.now();\n // Schedule the slice for execution.\n jobManager.scheduleSlice(slice, true /* placeInTheFrontOfTheQueue*/);\n // Slice has been rescheduled, but we still need to terminate the sandbox.\n jobManager.returnSandbox(sandbox);\n }\n else\n { \n // Slice has a reason OR rejected without a reason already and got stamped.\n // Add to array of rejected jobs.\n let rejectedJob = {\n address: slice.jobAddress,\n reasons: jobManager.rejectedJobReasons,\n }\n this.rejectedJobs.push(rejectedJob);\n\n // Purge the job.\n this.purgeJob(jobManager);\n\n // Tell everyone all about it, when allowed.\n if (jobManager.displayMaxDiagInfo)\n {\n const suffixMsg = '\\n\\tAll slices with the same jobAddress returned to the scheduler.\\n\\tAll sandboxes with the same jobAddress are terminated.';\n if (slice.rejectedTimeStamp)\n this.emit('warning', `work.reject: The slice ${slice.identifier} was rejected twice.${suffixMsg}`);\n else\n this.emit('warning', `work.reject: The slice ${slice.identifier} was rejected with reason ${rejectReason}.${suffixMsg}`);\n }\n }\n}\n\n// _Idx\n//\n// Unused functions that we need to review.\n// 1) destroy, shutdown, halt -- possibly need to incorporate these ideas in stopWork\n// 2) predictLoad -- XXXpfr: I really feel bad about not being able to figure out how to incorporate\n// this into the design of sup2. This was a central part of Wes' design of sup2.\n// I need to collaborate with Wes to resolve my ignorance.\n//\n\n/**\n * UNUSED\n * @deprecated -- may use later\n **/\nSupervisor.prototype.destroy = function Supervisor$destory()\n{\n selectiveDebugging && console.debug(`Supervisor.screenSaverDestroy: destroying Supervisor and everything else.`);\n this.stopWork(true /*forceTerminate*/);\n if (this.state) this.state.destroy();\n if (this.progressReportTimer) clearInterval(this.progressReportTimer);\n if (this.watchdogTimer) clearInterval(this.watchdogTimer);\n this.state = null;\n this.progressReportTimer = null;\n this.watchdogTimer = null;\n this.jobManagerInventory = null;\n this.sandboxInventory = [];\n this.readiedSandboxes = [];\n this.closeConnections();\n}\n\n/**\n * UNUSED\n * @deprecated -- may use later \n * Halt the Supervisor as quickly as possible.\n **/\nSupervisor.prototype.halt = function Supervisor$halt()\n{\n this.state.setIf('ready', 'stopping');\n if (!this.state.is('stopping'))\n throw new Error(`Supervisor has an invalid state ${this.state} for halt`);\n clearInterval(this.watchdogTimer);\n\n for (let jobMan of this.jobManagerInventory)\n {\n jobMan.state.setIf('ready', 'stop');\n for (const sandbox of jobMan.workingSandboxes)\n {\n sandbox.stop(); // NYI -- will terminate.\n }\n }\n}\n \n/**\n * UNUSED\n * @deprecated -- may use later \n * Shutdown the supervisor; attempts to return work which will not be finished before timeout expires.\n * The shutdown is complete once this supervisor emits the stopped state change.\n */\nSupervisor.prototype.shutdown = function Supervisor$shutdown(timeoutMs)\n{\n var ps = [], returnSliceInventory = [];\n var timer;\n\n this.state.setIf('ready', 'stopping');\n if (!this.state.is('stopping'))\n throw new Error(`Supervisor has an invalid state ${this.state} for shutdown`);\n clearInterval(this.watchdogTimer);\n\n for (let jobMan of this.jobManagerInventory)\n {\n jobMan.state.setIf('ready', 'stop');\n\n for (let slice of jobMan.sliceInventory)\n {\n if (slice.state.is('initial') || slice.state.is('ready'))\n {\n returnSliceInventory.push(slice);\n }\n else if (slice.state.is(sliceStatus.working))\n {\n ps.push(new Promise((resolve, reject) => {\n slice.state.on('change', (status) => {\n if (status === 'done')\n resolve();\n });\n }));\n }\n }\n }\n\n const reason = 'Supervisor.shutdown';\n this.returnSlices(returnSliceInventory, reason);\n timer = setTimeout(this.halt.bind(this), timeoutMs);\n Promise.all(ps)\n .then(() => {\n clearTimeout(timer);\n this.state.set('stopping', 'stopped');\n })\n .catch((e) => {\n if (e.code !== 'DCP_SUPERVISOR_ESYNC')\n throw e; /* becomes unhandled rejection */\n });\n}\n\n/** \n * Factory function which generates a list of origins which are safe to communicate \n * with for this purpose. Currently-valid purposes (more will be added):\n * - any\n * - fetchData\n * - fetchWorkFunctions\n * - fetchArguments\n * - sendResults\n */\nSupervisor.prototype.makeSafeOriginList = function Supervisor$$makeSafeOriginList(purpose)\n{\n var list = [];\n \n if (this[purpose])\n list = list.concat(this[purpose]);\n \n /* Add 'any' origin(s) to list iff not in localExec, or in localExec and purpose is sendResults */\n if (!this.options.localExec || (this.options.localExec && purpose === 'sendResults'))\n list = list.concat(this.allowedOrigins)\n \n return list;\n}\n \n/**\n * UNUSED -- DOES NOT WORK YET.\n * NEED TO WORK WITH WES TO FIGURE OUT BEST WAY TO GET PREDICTLOAD TO WORK.\n * Predict the load on this supervisor based on the local job measurement data.\n * Works by looking at current conditions and available slices, and tries to guess\n * in what order they will be finished, working, etc. \n *\n * The simulation is very naive, but is expected to be accurate several seconds\n * into the future, particularly as we approach the end of a task.\n *\n * @param {number} whenMs \n * \n * @returns {Object<load, jobManagerInventory>} where load is and instance of Load and the predicted \n * load at the prediction time, and jobManagerInventory \n * is a counterfeit which holds the predicted state of \n * the jobManagerInventory at that time.\n */\nSupervisor.prototype.predictLoad = function Supervisor$predictLoad (whenMs)\n{\n /** @type {JobManager[]} */\n var jmi = new Inventory(); /* Inventory of counterfeit JobManagers. */\n var load = new Load(0,0); /* This \"current\" load throughout the prediction. */\n /** @type {Slice} */\n var next; /* The next slice to \"finish\". */\n\n /* Initialize data structures for prediction from current activity. */\n for (let jobMan of this.jobManagerInventory.filter(jm => jm.state.is('ready') && jm.sliceInventory.length))\n {\n jobMan = jobMan.counterfeit();\n jmi.push(jobMan);\n jobMan.sliceInventory.forEach((s) => s.state.setIf('initial', 'ready'));\n }\n next = findNextSlice();\n \n /**\n * Routine that finds the slice that will end next (soonest.)\n * @returns {Slice}\n */\n function findNextSlice()\n {\n /** @type {Slice} */\n var _next;\n for (let jobMan of jmi)\n {\n const _workingSlices = jobMan.workingSlices;\n for (let slice of _workingSlices)\n {\n //\n // slice.etaMs is the estimated time interval until slice execution completes.\n //\n // If the slice hasn't started,\n // slice.etaMs = slice.jobManager.estimateWallMs,\n // else if the slice has completed execution:\n // slice.etaMs = 0.\n // else if the slice has started:\n // slice.jobManager.estimateWallMs - (Date.now() - slice.startTime).\n //\n if (_next && (_next.etaMs <= slice.etaMs))\n continue;\n\n _next = slice;\n }\n }\n load.add(_next.jobManager.metrics);\n \n return _next;\n }\n\n /* At this point, jmi is an Inventory of counterfeit job managers that are \"ready\" for\n * work, next.etaMs is the time interval until the next slice will finish, and we have\n * a reasonably accurate picture of our current load.\n *\n * Next, we \"end\" this slice, try to fill all cores, and push the timeline forward to\n * the next predicted end of slice.\n */\n for (next = findNextSlice();\n next && (next.etaMs < whenMs);\n next = findNextSlice())\n {\n let ended = next;\n let cursor = this.makeJobSelectionCursor(jmi);\n\n /* \"end\" this slice */\n load.subtract(ended.jobManager.metrics);\n /* Fake out collecting result to transition state to FINISHED. */\n ended.collectResult(null);\n\n /* \"start\" as many slices as we can - given our CPU/GPU constraints, slice data in memory, etc */\n while (this.targetLoad.fits(load))\n {\n let slice = cursor.next();\n if (!slice)\n break; /* Running out of work that fits. */\n\n if (!load.fits(this.targetLoad, slice.jobManager.metrics))\n continue;\n\n /* Pick a ready slice from this job and add its anticipated load to our current load if it will fit */\n slice = slice.jobManager.readySlices.shift();\n slice.markAsWorking(); // ?? Not sure this is correct.\n //slice.etaMs = ended.etaMs + slice.jobManager.estimateWallMs; wtf?!?! <--- LOOK HERE\n\n load.add(slice.jobManager.metrics);\n }\n }\n\n return { load, jobManagerInventory: jmi };\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/index.js?");
4574
+ eval("/* provided dependency */ var process = __webpack_require__(/*! ./node_modules/process/browser.js */ \"./node_modules/process/browser.js\");\n/**\n * @file dcp-client/worker/supervisor2/index.js\n * Code managing sandboxes, tasks, jobs, and slices within in a DCP Worker.\n * @author Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date Dec 2020\n * June 2022\n * @module supervisor\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { requireNative } = __webpack_require__(/*! dcp/dcp-client/webpack-native-bridge */ \"./src/dcp-client/webpack-native-bridge.js\");\nconst dcp4 = __webpack_require__(/*! dcp/protocol-v4 */ \"./src/protocol-v4/index.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst constants = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst { setImmediate } = __webpack_require__(/*! dcp/common/dcp-timers */ \"./src/common/dcp-timers.js\");\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst { Keystore, Address } = __webpack_require__(/*! dcp/dcp-client/wallet */ \"./src/dcp-client/wallet/index.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst { localStorage } = __webpack_require__(/*! dcp/common/dcp-localstorage */ \"./src/common/dcp-localstorage.js\");\nconst { DcpURL } = __webpack_require__(/*! dcp/common/dcp-url */ \"./src/common/dcp-url.js\");\nconst RingBuffer = __webpack_require__(/*! dcp/utils/ringBuffer */ \"./src/utils/ringBuffer.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { JobManager } = __webpack_require__(/*! ./job-manager */ \"./src/dcp-client/worker/supervisor2/job-manager.js\");\nconst { Load } = __webpack_require__(/*! ./load */ \"./src/dcp-client/worker/supervisor2/load.js\");\nconst { Sandbox, SandboxError } = __webpack_require__(/*! ./sandbox2 */ \"./src/dcp-client/worker/supervisor2/sandbox2.js\");\nconst { sliceStatus } = __webpack_require__(/*! dcp/common/scheduler-constants */ \"./src/common/scheduler-constants.js\");\nconst hash = __webpack_require__(/*! dcp/common/hash */ \"./src/common/hash.js\");\nconst { calculateJoinHash } = __webpack_require__(/*! dcp/dcp-client/compute-groups */ \"./src/dcp-client/compute-groups/index.js\");\nconst { ModuleCache } = __webpack_require__(/*! ./module-cache */ \"./src/dcp-client/worker/supervisor2/module-cache.js\");\nconst { Inventory, leafMerge, a$sleepMs, ms, pct, generateOpaqueId, booley, compressJobMap, \n toJobMap, truncateAddress, encodeDataURI, makeValueURI, justFetch, stringify } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n//const { CodeFlow } = require('client-oauth2');\n//const { text } = require('./lang').getLocaleStrings('en_CA'); /** XXXpfr @todo Don't know what to do with localization? */\n\n/** @typedef {import('dcp/dcp-client/wallet/eth').Address} Address */\n/** @typedef {import('dcp/protocol-v4/connection/connection').Connection} Connection */\n/** @typedef {string} opaqueId */ // 22 character base64 string\n/** @typedef {import('..').Worker} Worker */\n/** @typedef {import('..').SupervisorOptions} SupervisorOptions */\n/** @typedef {import('./slice2').Slice} Slice */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n\nconst addressTruncationLength = 20;\n\n//\n// Configs are moving around in dcpConfig and local worker configs, so set up some defaults.\nlet workerTuning = dcpConfig.worker;\nif (!workerTuning) workerTuning = dcpConfig.Supervisor;\nif (!workerTuning || !workerTuning.dcp || !workerTuning.dcp.tuning || !workerTuning.dcp.tuning.watchdogInterval\n || !workerTuning.sandbox || !workerTuning.allowOrigins || !workerTuning.minimumWage || !workerTuning.computeGroups)\n workerTuning = {\n dcp: {\n tuning: { watchdogInterval: 7, minSandboxStartDelay: 0.1, maxSandboxStartDelay: 0.7 },\n connectionOptions: { default: { identityUnlockTimeout: 15 * 60 /* seconds */ } },\n },\n sandbox: { progressReportInterval: 2 * 60 * 100 },\n allowOrigins: { fetchWorkFunctions: [], fetchArguments: [], fetchData: [], sendResults: [], any: [] },\n minimumWage: { CPU: 0, GPU: 0, 'in': 0, out: 0 },\n leavePublicGroup: false,\n pCores: 0,\n computeGroups: {},\n // The following configs are not in dcpConfig or worker configs (yet), but may be specified in local worker configs to override the defaults.\n pruneFrequency: 15 * 1000, // Maxiumum time interval where we check to prune used sandboxes.\n workerSandboxThreshold: 7, // When maxWorkingSandboxes >= workerSandboxThreshold, we allow an extra 25% of assigned sandboxes that won't be pruned.\n cachedJobsThreshold: 12, // Prune the unused job managers >= cachedJobsThreshold.\n};\n\n//\n// Flags for tracing.\n//\nconst selectiveEnable = false;\nconst displayWarnError = false || selectiveEnable;\nconst selectiveDebugging = selectiveEnable || debugging();\nconst debuggingError = false || selectiveDebugging || displayWarnError;\nconst debuggingWarn = false || selectiveDebugging || displayWarnError;\nconst selectiveDebugging2 = selectiveEnable && false || debugging('supervisor');\nconst displaySliceState = true;\nconst displayCompletedResults = false;\n\n/** \n * Adjust delay times when debugging.\n * The adjustment for niim is automatic, other debuggers must manually change this value.\n */\nlet timeDilation = 1;\nif (DCP_ENV.platform === 'nodejs')\n{\n /** Make timers 10x slower when running in niim */\n timeDilation = (requireNative('module')._cache.niim instanceof requireNative('module').Module) ? 10 : 1;\n}\n\n//\n// Index to functionality -- search for '_Idx' to toggle through the index.\n//\n// 1) Ctor: Supervisor constructor.\n// 2) Important property-like functions.\n// 3) Dtors: screenSaverDestroy, stopWork, purgeJob.\n// 4) Connection code.\n// 5) Work: Distribute slice to sandboxes.\n// 6) Return slices and sent progress reports to result-submitter-results.\n// 7) Task Distributor (TD): requestTask (Rq) support -- communication with TD.\n// 8) Aggregators from the job managers.\n// 9) Sandbox creation and management.\n// 10) Result-submitter-result support functions.\n// 11) Work reject.\n// 12) Unused functions that we need to review.\n//\n\n// _Idx\n//\n// Ctor: Supervisor constructor.\n//\n\n/** \n * Supervisor constructor\n * \n * A supervisor manages the communication with the scheduler, manages sandboxes, and\n * decides which workload should be sent to which sandboxes when.\n *\n * Start state:\n * - initial\n *\n * Intermediate states:\n * - ready\n * - stopping\n *\n * Terminal states:\n * - stopped\n *\n * Valid transitions:\n * - initial -> ready where that happens \n * - ready -> stopping\n * - stopping -> stopped\n *\n * @param {Worker} worker - The worker that created this instance.\n * @param {SupervisorOptions} options - Options for specifying custom behaviour and tuning,\n */\nfunction Supervisor(worker, options)\n{\n assert(options.identity instanceof Keystore);\n assert(options.paymentAddress instanceof Address);\n\n /**\n * Flag to indicate a debug build.\n * Used when we want to display extra information and do extra checks for developers only.\n * @type {boolean}\n */\n this.debugBuild = ((__webpack_require__(/*! dcp/common/dcp-build */ \"./src/common/dcp-build.js\").build) === 'debug');\n /**\n * When Supervisor.sliceTiming is set to be true, it displays the timings of a every slice\n * slice['queueingDelta'] = timespan of when slice is passed to jobManager.runQueuedSlice until sandbox.work\n * slice['executionDelta'] = timespan of execution in sandbox\n * slice['resultDelta'] = timespan of when sandbox finishes executing until recordResult completes.\n * @type {boolean}\n */\n this.sliceTiming = false;\n /** Used for analyzing the completed results in Supervisor.recordResult. */\n this.resultMap = {};\n\n /** @type {ModuleCache} */\n this.moduleCache = new ModuleCache(this);\n\n this.worker = worker;\n this.identity = options.identity;\n this.paymentAddress = options.paymentAddress;\n this.options = options;\n this.maxWorkingSandboxes = options.maxWorkingSandboxes || 1;\n this.maxTotalSandboxes = this.maxWorkingSandboxes;\n\n // We're making the assumption that if a worker has at least 7 sandboxes, \n // then the worker has sufficient resources to handle 25% more sandboxes in memory.\n // This assumption may be overridden by changing workerSandboxThreshold.\n if (this.maxWorkingSandboxes >= this.workerSandboxThreshold)\n this.maxTotalSandboxes = Math.ceil(1.25 * this.maxWorkingSandboxes);\n // When # of sandboxes reaches this level, we more aggressively prune.\n this.mustPruneSandboxLevel = Math.ceil(1.5 * this.maxTotalSandboxes);\n // Last prune time stamp.\n this.lastPrune = 0;\n // General time stamp.\n this.lastTime = 0;\n\n // Supervisor may get created by Worker where options.cores or options.targetLoad is not defined.\n this.numCPU = this.maxWorkingSandboxes;\n this.numGPU = 1;\n this.portionToUseCPU = pct(100);\n this.portionToUseGPU = pct(100);\n\n if (options.cores)\n {\n this.numCPU = options.cores.cpu || this.numCPU;\n this.numGPU = options.cores.gpu || this.numGPU;\n }\n if (options.targetLoad)\n {\n this.portionToUseCPU = options.targetLoad.cpu || pct(100);\n this.portionToUseGPU = options.targetLoad.gpu || pct(100);\n }\n\n this.tuning = {\n maxCPUAlloc: this.portionToUseCPU, /**< Maximum proportion of CPU time to attempt to use. */\n maxGPUAlloc: this.portionToUseGPU, /**< Maximum proportion of GPU time to attempt to use. */\n watchdogInterval: 7, /**< (seconds) How frequently to kick off an unsolicited requestTask. */\n prefetchInterval: 20, /**< (seconds) How many seconds into the future are looking to project capacity during work fetch. */\n minSandboxStartDelay: 0.1, /**< (seconds) Base minimum of this.delayMs, scaled by this.delayScaler. */\n maxSandboxStartDelay: 0.7, /**< (seconds) Base maximum random component of this.delayMs, scaled by this.delayScaler. */\n };\n this.tuning = leafMerge(this.tuning, workerTuning.dcp.tuning);\n if (options.watchdogInterval > 0)\n this.tuning.watchdogInterval = options.watchdogInterval; // Override.\n //this.tuning.watchdogInterval = 0.25;\n\n /**\n * Fine tune this.delayMs.\n * Note: Please discuss any change with Paul, paul@kingsds.network.\n * XXXpfr @todo Finalize the delay tuning.\n */\n this.delayScaler = 0.5;\n\n debugging('supervisor') && console.debug('Supervisor.tuning', this.tuning);\n\n /**\n * Note: targetLoad is not properly implemented yet.\n * XXXpfr @todo Collaborate with Wes to get it right.\n * @type {Load}\n */\n this.targetLoad = new Load({\n cpu: Math.min(this.maxWorkingSandboxes, this.numCPU),\n gpu: Math.min(this.maxWorkingSandboxes, this.numGPU)\n }).scale(this.tuning.maxCPUAlloc, this.tuning.maxGPUAlloc);\n\n /** @type {string[]} */\n this.allowedOrigins = workerTuning.allowOrigins.any;\n /** @type {string[]} */\n this.fetchWorkFunctions = workerTuning.allowOrigins.fetchWorkFunctions;\n /** @type {string[]} */\n this.fetchArguments = workerTuning.allowOrigins.fetchArguments;\n /** @type {string[]} */\n this.fetchData = workerTuning.allowOrigins.fetchData;\n /** @type {string[]} */\n this.sendResults = workerTuning.allowOrigins.sendResults;\n\n // In localExec, do not allow work function or arguments to come from the 'any' origins\n if (this.options.localExec)\n {\n this.allowedOrigins = this.allowedOrigins.concat(options.allowedOrigins.any);\n this.fetchWorkFunctions = this.fetchWorkFunctions.concat(options.allowedOrigins.fetchWorkFunctions);\n this.fetchArguments = this.fetchArguments.concat(options.allowedOrigins.fetchArguments);\n this.fetchData = this.fetchData.concat(options.allowedOrigins.fetchData);\n this.sendResults = this.sendResults.concat(options.allowedOrigins.sendResults)\n }\n\n if (options.allowedOrigins && options.allowedOrigins.length > 0)\n this.allowedOrigins = options.allowedOrigins.concat(this.allowedOrigins);\n \n //\n // The following 3 configs are not in dcpConfig or worker configs (yet), but may be specified in local worker configs to override the defaults.\n //\n /** @type {number} - Maxiumum time interval where we check to prune used sandboxes. */\n this.pruneFrequency = workerTuning.pruneFrequency || 15 * 1000;\n /** @type {number} - When maxWorkingSandboxes >= workerSandboxThreshold, we allow an extra 25% of assigned sandboxes that won't be pruned. */\n this.workerSandboxThreshold = workerTuning.workerSandboxThreshold || 7;\n /** @type {number} - Prune the unused job managers >= cachedJobsThreshold. */\n this.cachedJobsThreshold = workerTuning.cachedJobsThreshold || 12;\n\n /** @type {Object.<Address, JobManager>} */\n this.jobMap = {}; \n /** @type {Sandbox[]} - All sandboxes that are being used by the job managers. Makes sure we don't lose sandboxes. */\n this.sandboxInventory = [];\n /** @type {Sandbox[]} - Started sandboxes that are not in sandboxInventory yet. */\n this.readiedSandboxes = [];\n /** @type {JobManager[]} */\n this.jobManagerInventory = new Inventory('jobManagers');\n /** @type {Synchronizer} */\n this.state = new Synchronizer('initial', [ 'initial', 'ready', 'reconnecting', 'stopping', 'stopped', 'broken']);\n\n /** @type {string} */\n this.lastDcpsid = undefined;\n /** @type {Connection} */\n this.taskDistributor = null;\n /** @type {Connection} */\n this.resultSubmitter = null;\n /** @type {Connection} */\n this.eventRouter = null;\n /** @type {Connection} */\n this.packageManager = null;\n /** @type {Array<object>} */\n this.resultSubmitterMessageQueue = [];\n /** @type {Array<object>} */\n this.eventRouterMessageQueue = [];\n\n /** @type {object} */\n this.schedulerConfig = leafMerge(dcpConfig.scheduler, options.schedulerConfig);\n\n /** @type {opaqueId} */\n this.workerId = localStorage.getItem('workerId');\n if (!this.workerId || this.workerId.length !== constants.workerIdLength)\n {\n this.workerId = generateOpaqueId();\n localStorage.setItem('workerId', this.workerId);\n }\n /** @type {object[]} */\n this.rejectedJobs = [];\n /** \n * An N-slot ring buffer of job addresses. Stores all jobs that have had no more than 1 slice run in the ring buffer.\n * Required for the implementation of discrete jobs \n * @type {RingBuffer} \n */\n this.ringBufferofJobs = new RingBuffer(100); // N = 100 should be more than enough. \n /** @type {boolean} - pseudo-mutex guarding requestTask. */\n this.isFetchingNewWork = false;\n\n // Start up the connections.\n this.instantiateAllConnections();\n\n /**\n * Note: DCP-3241 asks to test Android to see if we need this restriction any longer.\n * XXXpfr @todo Hopefully we can delete this @hack.\n */\n // @hack - dcp-env.isBrowserPlatform is not set unless the platform is _explicitly_ set,\n // using the default detected platform doesn't set it.\n // Fixing that causes an error in the wallet module's startup on web platform, which I\n // probably can't fix in a reasonable time this morning.\n // ~ER2020-02-20\n if (!options.maxWorkingSandboxes\n && DCP_ENV.browserPlatformList.includes(DCP_ENV.platform)\n && navigator.hardwareConcurrency > 1) {\n this.maxWorkingSandboxes = navigator.hardwareConcurrency - 1;\n if (typeof navigator.userAgent === 'string') {\n if (/(Android).*(Chrome|Chromium)/.exec(navigator.userAgent)) {\n this.maxWorkingSandboxes = 1;\n this.emit('warning', 'Doing work with Chromimum browsers on Android is currently limited to one sandbox');\n }\n }\n }\n}\nexports.Supervisor = Supervisor;\nSupervisor.prototype = Object.getPrototypeOf(new EventEmitter('Supervisor')); // Fake out VSCode -- get's rid of a billion red-squigglies.\nSupervisor.prototype = new EventEmitter('Supervisor');\n/**\n * Preserve the constructor property.\n * @constructor\n */\nSupervisor.prototype.constructor = Supervisor;\n\n/**\n * Set up sandboxes and interval timers, then start to search for work.\n **/\nSupervisor.prototype.startWork = function Supervisor$startWork ()\n{\n /* Provide opportunity for calling code to hook ready/error events. */\n setImmediate(async () => {\n try\n {\n if (this.state.isNot('initial'))\n {\n if (this.state.setIf('stopped', 'initial')) {}\n else if (this.state.setIf('reconnecting', 'initial')) {}\n else if (this.state.setIf('broken', 'initial')) {}\n else if (this.state.is('ready')) return\n else throw new Error(`Supervisor startWork is in unexpected state ${this.state}, aborting...`);\n }\n this.instantiateAllConnections();\n\n await this.createSandboxes(this.maxWorkingSandboxes)\n .then(() => this.checkCapabilities());\n\n // Beacon interval timer.\n this.progressReportTimer = setInterval(() => this.emitProgressReport(), (workerTuning.sandbox.progressReportInterval || 2 * 60 * 100));\n // Watchdog: requestTask-driven interval timer.\n this.watchdogTimer = setInterval(() => this.requestTask() , ms(this.tuning.watchdogInterval));\n if (DCP_ENV.platform === 'nodejs' && this.options.localExec)\n {\n /* Interval timer helps keep worker alive forever, which we don't want in localExec. */\n this.progressReportTimer.unref();\n this.watchdogTimer.unref();\n }\n\n this.state.set('initial', 'ready');\n\n setImmediate(() => this.requestTask()); // Don't wait for watchdog.\n }\n catch(error)\n {\n this.state.set('initial', 'broken');\n this.emit('error', error);\n }\n });\n}\n\n/** Construct capabilities when necessary. */\nSupervisor.prototype.checkCapabilities = function Supervisor$checkCapabilities ()\n{\n if (!this.capabilities)\n {\n /**\n * Assign the capabilities of one the sandboxes before fetching slices from the scheduler.\n * @todo Remove this once fetchTask uses the capabilities of every sandbox to fetch slices.\n */\n const sandbox = this.readiedSandboxes.length > 0 ? this.readiedSandboxes[0] : this.sandboxInventory[0];\n if (sandbox)\n {\n this.capabilities = sandbox.capabilities;\n this.emit('capabilitiesCalculated', this.capabilities);\n }\n }\n\n if (DCP_ENV.isBrowserPlatform && this.capabilities.browser)\n this.capabilities.browser.chrome = DCP_ENV.isBrowserChrome;\n}\n\n// _Idx\n//\n// Important property-like functions.\n//\n\n/**\n * Universal delay milliseconds..\n * @returns {number}\n */\nSupervisor.prototype.delayMs = function Supervisor$delayMs (max = this.tuning.maxSandboxStartDelay, min = this.tuning.minSandboxStartDelay)\n{\n // Note: Please discuss any change with Paul, paul@kingsds.network.\n return 1000 * timeDilation * this.delayScaler * (min + Math.random() * (max - min));\n}\n\n/**\n * Indicates whether supervisor is ready for business.\n * @returns {boolean} - When true, the party is on...\n */\nSupervisor.prototype.isReady = function Supervisor$isReady()\n{\n return this.worker.working && this.state.is('ready');\n}\n\n/**\n * Safe access to Connection.close(...).\n * @param {Connection} connection\n * @param {string} [reason='requested']\n * @param {boolean} [immediate=false]\n * @returns {Promise<string>}\n */\nfunction safeClose(connection, reason = 'requested', immediate = false)\n{\n if (connection)\n {\n let msg;\n if (connection.state.is('closed')) msg = 'closed';\n else if (connection.state.is('closing')) msg = 'closing';\n else if (connection.state.is('close-wait')) msg = 'close-wait';\n if (msg)\n {\n debuggingWarn && console.warn(`${msg}, do not try to close again.`);\n return Promise.resolve(msg);\n }\n return connection.close(reason, immediate)\n .then(() => {\n return Promise.resolve(null);\n });\n }\n return Promise.resolve('already closed');\n}\n\n/**\n *\n * @param {string} operation\n * @param {*} data\n * @returns {Promise<string>}\n */\nSupervisor.prototype.saveForResubmitToRS = function Supervisor$saveForResubmitToRS(operation, data)\n{\n this.resultSubmitterMessageQueue.push({ operation, data });\n return safeClose(this.resultSubmitter);\n}\n\n/**\n * Error feedback to user.\n * @param {string} message\n * @param {{message}} [coreMessage]\n */\nSupervisor.prototype.error = function Supervisor$error(message, coreMessage)\n{\n if (coreMessage)\n {\n const errorObject = Object.assign({ Error: message }, this.checkCode(coreMessage, true /*isInner*/));\n this.emit('error', errorObject);\n }\n else\n this.emit('error', message);\n}\n\n/**\n * When true, the sandbox complete handler will look for another slice in the same job,\n * and if not found, then proceed to Supervisor.requestTask.\n * @returns {boolean}\n */\nSupervisor.prototype.runSliceFromSameJob = function Supervisor$runSliceFromSameJob()\n{\n //\n // Experimental, should be off by default.\n // Cf. sandbox complete handler in JobManager.hookUpSandboxListeners.\n //\n const disable = true;\n const tooManyJobs = this.activeJobCount() > this.maxWorkingSandboxes;\n return !disable && !tooManyJobs && this.unusedSandboxSlots() < 2;\n}\n\n/**\n * This function is used as the target number of sandboxes to be associated with slices and start working.\n * Warning: Do not rely on this information being 100% accurate -- it may change in the next instant.\n * @returns {number}\n */\nSupervisor.prototype.unusedSandboxSlots = function Supervisor$unusedSandboxSlots ()\n{\n return this.maxWorkingSandboxes - this.workingSliceCount();\n}\n\n/**\n * Create errorObj with error.code if it exists.\n * Return everything when this.debugBuild.\n * @todo Need better mechanism for displaying everything.\n * @param {{message}} error\n * @param {boolean} [isInner=false]\n * @returns {object}\n */\nSupervisor.prototype.checkCode = function Supervisor$checkCode (error, isInner = false)\n{\n if (this.debugBuild || !(error && error.message)) return error;\n const errorObj = isInner ? { InnerError: error.message } : { message: error.message };\n if (error['errorCode']) errorObj['errorCode'] = error['errorCode'];\n if (error['code']) errorObj['code'] = error['code'];\n return errorObj;\n}\n\n/**\n * Remove stack trace from error.\n * May not work when error is a string with no new-lines.\n * @param {string|{message}} error\n * @returns {string|{message}}\n */\nSupervisor.prototype.removeStackTrace = function Supervisor$removeStackTrace (error)\n{\n if (typeof error === 'string')\n {\n const errorLines = error.split('\\n');\n return (errorLines && errorLines.length > 0) ? errorLines[0] : error;\n }\n if (error['message'])\n return this.checkCode(error)\n return error;\n}\n\n// _Idx\n//\n// Dtors: screenSaverDestroy, stopWork, purgeJob.\n//\n\n/**\n * If we cannot create a new sandbox, that probably means we're on a screensaver worker\n * and the screensaver is down. So return the slices to the scheduler.\n */\nSupervisor.prototype.screenSaverDestroy = function Supervisor$screenSaverDestroy()\n{\n debugging('supervisor') && console.debug(`Supervisor.screenSaverDestroy: destroying all job managers and terminating all sandboxes.`);\n this.jobManagerInventory.forEach(jm => jm.destroy());\n this.jobManagerInventory = new Inventory('jobManagers');\n\n this.readiedSandboxes.forEach(sandbox => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.readiedSandboxes = [];\n\n this.sandboxInventory.forEach(sandbox => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.sandboxInventory = [];\n}\n\n/**\n * Terminates sandboxes and returns slices.\n * Sets the working flag to false, call @this.work to start working again.\n * \n * If forceTerminate is true: Terminates all sandboxes and returns all slices.\n * If forceTerminate is false: Terminates non-working sandboxes and returns initial and ready slices.\n *\n * @param {boolean} [forceTerminate = true] - true if you want to stop the sandboxes from completing their current slice.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.stopWork = async function Supervisor$stopWork (forceTerminate = true)\n{\n selectiveDebugging && console.debug(`Supervisor.stopWork(${forceTerminate},${this.state}): terminating sandboxes and returning slices to scheduler.`);\n\n // Do a hard flush of the microtask queue and finish the current event loop.\n await new Promise((resolve) => setImmediate(() => setTimeout(resolve, 0)));\n\n if (this.state.setIf('ready', 'stopping')) {}\n else if (this.state.setIf('reconnecting', 'stopping')) {}\n else if (this.state.setIf('broken', 'stopping')) {}\n else if (this.state.is('stopped')) return\n else throw new Error(`Supervisor stopWork is in unexpected state ${this.state}, aborting...`);\n\n this.instantiateAllConnections();\n\n this.readiedSandboxes.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this.readiedSandboxes = [];\n\n if (forceTerminate)\n {\n for (const jm of this.jobManagerInventory)\n jm.destroy();\n\n this.sandboxInventory.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n }\n else\n {\n let activeSliceCount = 0;\n const slicesToReturn = [];\n for (const jm of this.jobManagerInventory)\n {\n //jm.dumpSlices(`stopWork1:${jm.address}`);\n const queuedSlices = jm.queuedSlices;\n slicesToReturn.push(...queuedSlices);\n jm.removeSlices(queuedSlices);\n activeSliceCount += jm.activeSlices.length;\n jm.nonWorkingSandboxes.forEach((sandbox) => jm.returnSandbox(sandbox));\n }\n\n const reason = `Supervisor stopWork(${forceTerminate}): returning all non-finished slices that are not working.`;\n this.returnSlices(slicesToReturn, reason, false /*removeSlices*/);\n\n await new Promise((resolve) => {\n if (activeSliceCount === 0)\n resolve();\n // Resolve and finish work once all sandboxes have finished submitting their results.\n this.on('submitFinished', () => {\n if (--activeSliceCount === 0)\n {\n debuggingWarn && console.warn('All sandboxes empty, stopping worker and closing all connections');\n resolve();\n }\n });\n });\n\n for (const jm of this.jobManagerInventory)\n {\n //jm.dumpSlices(`stopWork2:${jm.address}`);\n jm.liveSandboxes.forEach((sandbox) => sandbox.terminate(false));\n jm._sandboxInventory = [];\n }\n }\n this.sandboxInventory = [];\n this.jobManagerInventory = new Inventory('jobManagers');\n\n this.closeConnections();\n\n this.emit('stop');\n this.state.set('stopping', 'stopped');\n}\n\n/**\n* Purge all traces of the job.\n* @param {JobManager} jobManager \n*/\nSupervisor.prototype.purgeJob = function Supervisor$purgeJob (jobManager)\n{\n selectiveDebugging && console.debug(`Supervisor.purgeJob ${jobManager.identifier}.`);\n this.jobManagerInventory.delete(jobManager);\n this.moduleCache.removeJob(jobManager.address);\n jobManager.destroy();\n}\n\n// _Idx\n//\n// Connection code.\n//\n\n/** \n * Connect the supervisor to a given scheduler sub-service. Reconnection \"DDoS\" from workers\n * mitigated via an exponential backoff algorithm.\n *\n * DCPv4 connections are lazily-initiated. Successful connection establishment detected by\n * observing the payload event, which normally triggers during versioning.\n */\nSupervisor.prototype.connectTo = function Supervisor$connectTo(which)\n{\n const config = (which === 'packageManager') ? dcpConfig.packageManager : this.schedulerConfig.services[which];\n const retryMinSleepMs = 100;\n const payloadResetCount = 3; /* How many payloadCount before we reset retryNextSleepMs. */\n \n var retryNextSleepMs = retryMinSleepMs;\n var payloadCount = 0;\n var options = leafMerge(/* ordered from most to least specific */\n workerTuning.dcp.connectionOptions.default,\n workerTuning.dcp.connectionOptions[which],\n workerTuning.dcp.connectionOptions[config.location.href],\n );\n\n /**\n * The payload event handler is an interesting way to handle exponential backoff\n * for the delay in this.reconnect.\n * XXXpfr @todo Revisit exponential backoff for this.reconnect in Sup2 Part II.\n */\n\n const handlePayloadEventFn = (ev) =>\n {\n if (++payloadCount === payloadResetCount)\n {\n this[which].removeEventListener('payload', handlePayloadEventFn);\n retryNextSleepMs = retryMinSleepMs; \n }\n }\n\n this[which] = new dcp4.Connection(config, this.identity, options);\n\n debugging() && console.debug(`Opening new connection ${this[which].id} to ${which}.`)\n\n const messageQueue = this[which + 'MessageQueue'];\n if (messageQueue && messageQueue.length > 0)\n resendRejectedMessages(this[which], messageQueue);\n\n this[which].on('payload', handlePayloadEventFn);\n\n this['reconnect' + which] = this.reconnect.bind(this, which);\n this[which].on('close', () => { this['reconnect' + which](); });\n}\n\n/**\n * Reconnect logic.\n * @param {string} which -- Name of the connection \n */\nSupervisor.prototype.reconnect = async function Supervisor$reconnect(which)\n{\n debugging('supervisor') && console.debug(`Supervisor.reconnect: ${which}:`, this.state.valueOf(), this[which].state.valueOf());\n const stateChange = this.state.testAndSet('ready', 'reconnecting');\n if (stateChange || this.state.is('reconnecting'))\n {\n await a$sleepMs(100); // Sleep for 100ms, maybe exp-backoff later if we need it.\n this.connectTo(which);\n if (stateChange) this.state.set('reconnecting', 'ready');\n debugging() && console.debug(`Supervisor.reconnect: Trying to reconnect ${which}`, this.state.valueOf(), this[which].state.valueOf());\n }\n}\n\n/**\n * Close a connection properly.\n * @param {string} which -- Name of the connection to close.\n */\nSupervisor.prototype.closeConnection = function Supervisor$closeConnection(which)\n{\n if (this[which])\n {\n this[which].off('close', this['reconnect' + which]);\n this[which].close();\n this[which] = null;\n }\n}\n\n/**\n * Close all connections.\n */\nSupervisor.prototype.closeConnections = function Supervisor$closeConnections()\n{\n /** XXXpfr @todo Remove when bug DCP-3094 is fixed. */\n a$sleepMs(500);\n\n this.closeConnection('taskDistributor');\n this.closeConnection('resultSubmitter');\n this.closeConnection('eventRouter');\n this.closeConnection('packageManager');\n}\n\n/**\n * Broadcast keepalive to all connections and optionally start up sandboxes.\n * @param {boolean} [createSandbox=false] - When true, creates sandboxes for unused sandbox slots.\n */\nSupervisor.prototype.keepalive = function Supervisor$keepalive(createSandbox = false)\n{\n return Promise.all([\n this.taskDistributor.keepalive(),\n this.resultSubmitter.keepalive(),\n this.eventRouter .keepalive(),\n this.packageManager .keepalive(),\n (createSandbox ? this.createSandboxes(this.maxWorkingSandboxes) : Promise.resolve()),\n ]);\n}\n\n/**\n * Open all connections. Used when supervisor is instantiated or stopped/started to initially open connections.\n */\nSupervisor.prototype.instantiateAllConnections = function Supervisor$instantiateAllConnections ()\n{\n if (!this.taskDistributor)\n this.connectTo('taskDistributor');\n if (!this.eventRouter)\n this.connectTo('eventRouter');\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n if (!this.packageManager)\n this.connectTo('packageManager');\n}\n\n/**\n * Try sending messages that were rejected on an old instance of the given connection.\n * @param {Connection} connection\n * @param {Array<object>} messageQueue\n */\nasync function resendRejectedMessages (connection, messageQueue)\n{\n var message = messageQueue.shift();\n do \n {\n try\n {\n await connection.send(message.operation, message.data);\n }\n catch (error)\n {\n debuggingError && console.error(`Failed to resend message ${message.operation} to scheduler: ${error}. Will try again on a new connection.`);\n messageQueue.unshift(message);\n safeClose(connection);\n break;\n }\n message = messageQueue.shift();\n } while (message);\n}\n \n// _Idx\n//\n// Work: Distribute slice to sandboxes.\n//\n\n/** \n * UNUSED\n * @deprecated\n * Round-robin through the job managers, picking 1 slice to run each time.\n * Try to have the same number of working sandboxes for each job.\n * Try to run a slice on every available sandbox.\n */\nSupervisor.prototype.workOnCurrentTask = function Supervisor$workOnCurrentTask ()\n{\n return this.roundRobinSlices();\n}\n\n/**\n * This is for compatibility with Supervisor1 in sa worker.\n * When we get rid of Supervisor1 we can delete the ref in sa worker and then get rid of this function.\n */\nSupervisor.prototype.setDefaultIdentityKeystore = function Supervisor$setDefaultIdentityKeystore ()\n{\n}\n\n/**\n * Called in Worker.start().\n * Initial entry point after Worker constructor.\n * We need to start searching for work here to allow starting and stopping a worker.\n */\nSupervisor.prototype.work = function Supervisor$work ()\n{\n // Set up sandboxes and interval timers, then start to search for work.\n this.startWork();\n}\n\n/** \n * Round-robin through the job managers, picking 1 slice to run each time.\n * Try to have the same number of working sandboxes for each job.\n * Try to run a slice on every available sandbox.\n */\nSupervisor.prototype.roundRobinSlices2 = function Supervisor$roundRobinSlices2 ()\n{\n try\n {\n let first = true;\n const cursor = this.makeJobSelectionCursor(this.jobManagerInventory);\n while (true)\n {\n const slice = cursor.next();\n if (!slice) break; /* No more work can fit. */\n debugging('supervisor') && console.debug('roundRobinSlices: Executing slice', slice.identifier);\n slice.markAsReserved();\n slice.jobManager.runSlice(slice, first ? 0 : this.delayMs());\n first = false;\n }\n }\n finally\n {\n this.isFetchingNewWork = false;\n }\n}\n\n/**\n * We try to balance so that each job has the same number of working slices.\n *\n * NOTES:\n * 1) If count is such that it cannot be distributed evenly, we do not yet randomly assign the extras.\n * E.g. 3 jobs, j1, j2, j3: count = 5 -- so 2 jobs get extras -- the extras need to be assigned randomly.\n * @todo Assign the extras randomly.\n * 2) this.roundRobinSlices is not going to be what we use when sup2 is in final form.\n * We want to this.makeJobSelectionCursor and something like this.roundRobinSlices2\n *\n * In the outer loop,\n * when numworkingSandboxes=1, try to get a slice running for each job with 0 working sandboxes.\n * when numworkingSandboxes=2, try to get a slice running for each job with 1 working sandboxes.\n * when numworkingSandboxes=3, try to get a slice running for each job with 2 working sandboxes. Etc.\n * The idea is to balance the number of slices working on each job.\n * @param {number} [count=0] - The number of new slices to try to get running in sandboxes.\n */\nSupervisor.prototype.roundRobinSlices = function Supervisor$roundRobinSlices (count = 0)\n{\n try\n {\n if (!count) count = this.unusedSandboxSlots();\n if (!count || this.readySliceCount() < 1)\n return Promise.resolve();\n\n const slices = [];\n let numScheduled = 0\n let readyJobManagers = this.jobManagerInventory.filter(jm => jm.ready);\n let first = true;\n selectiveDebugging && console.debug('roundRobinSlices: START count', count, 'numJobMgrs', readyJobManagers.length, 'totalWorking(w/r/wo/wsbx/sbx)', this.workingSliceCount(), this.reservedSliceCount(), this.workingSliceOnlyCount(), this.workingSandboxCount(), this.sandboxCount());\n\n for (let numWorkingSandboxes = 1; numWorkingSandboxes <= this.maxWorkingSandboxes; numWorkingSandboxes++)\n {\n let sliceCount = 0;\n const beginNumScheduled = numScheduled;\n for (const jobMan of readyJobManagers)\n {\n const _readySlices = jobMan.readySlices;\n sliceCount += _readySlices.length\n const skip = numWorkingSandboxes <= jobMan.workingSlices.length;\n\n if (skip || _readySlices.length < 1)\n {\n // Noisy log message turned off by default.\n //debugging('supervisor') && console.debug('RRS0(numS, beginNumS, count, sliceCount, skip, _ready, numWorkingS(loop), workingSlices):', numScheduled, beginNumScheduled, count, sliceCount, skip, _readySlices.length, numWorkingSandboxes, jobMan.workingSlices.length);\n continue;\n }\n\n const slice = _readySlices[0];\n slices.push(slice);\n\n slice.markAsReserved();\n jobMan.runSlice(slice, first ? 0 : this.delayMs());\n\n first = false;\n if (++numScheduled >= count)\n break;\n }\n if (numScheduled >= count)\n {\n debugging('supervisor') && console.debug('RRS1(numS, beginNumS, count, sliceCount):', numScheduled, beginNumScheduled, count, sliceCount);\n break;\n }\n if (beginNumScheduled === numScheduled && sliceCount < 1)\n {\n debugging('supervisor') && console.debug('RRS2(numS, beginNumS, count, sliceCount):', numScheduled, beginNumScheduled, count, sliceCount);\n break;\n }\n }\n if (selectiveDebugging)\n {\n console.debug(`roundRobinSlices(working:(w/r/wo/wsbx/sbx)${this.workingSliceCount()},${this.reservedSliceCount()},${this.workingSliceOnlyCount()},${this.workingSandboxCount()},${this.sandboxCount()}): Started ${slices.length}/${numScheduled}/${count} scheduled slices`, compressSlices(slices));\n const sliceGrouper = {};\n slices.forEach((slice) => {\n const jm = slice.jobManager;\n if (!sliceGrouper[slice.jobAddress]) sliceGrouper[slice.jobAddress] = { cnt: 1, working: jm.workingSlices.length, queued: jm.queuedSlices.length, assigned: jm.assignedSandboxes.length, estimation: jm.isEstimation };\n else sliceGrouper[slice.jobAddress].cnt++;\n });\n console.debug(sliceGrouper);\n }\n if (selectiveDebugging2)\n {\n const jobGrouper = {};\n this.jobManagerInventory.forEach((jm) => {\n jobGrouper[jm.address] = { working: jm.workingSlices.length, queued: jm.queuedSlices.length, assigned: jm.assignedSandboxes.length, estimation: jm.isEstimation };\n });\n console.debug(jobGrouper);\n }\n }\n finally\n {\n this.isFetchingNewWork = false;\n }\n}\n\n/**\n * @callback cbNext\n * @returns {Slice}\n */\n\n/** \n * Factory function which instantiates a JobSelectionCursor. A JobSelectionCursor\n * steps the order that job slices should be selected for execution in the supervisor,\n * given the current state of the supervisor and the availability of jobs when the\n * inventory was snapshot. The entire slice scheduling algorithm is represented by\n * this cursor.\n *\n * The basic idea behind the scheduling of slices in this implementation is to keep as\n * many slices from different jobs running as possible, so as to reduce the likelihood\n * of resource contention between sandboxes.\n *\n * Slices are scheduled in here based on the following ruleset:\n * - pick a slice from the longest job that isn't running\n * - choose a slice from the remaining jobs, in order for shortest to longest slice time\n * - if there are any jobs which are nearly finished, every other slice comes from one\n * of these jobs, selected randomly????? <-- NYI. XXXpfr @todo Think about implementing...\n * - jobs which have slicePriority set by the task-distributor may be chosen in place\n * of slices in the above algorith. Jobs with a slicePriority closer to 1 are more likely\n * to exhibit this behaviour.\n * @param {JobManager[]} jobManagerInventory\n * @returns { { next: cbNext } }\n */\nSupervisor.prototype.makeJobSelectionCursor = function Supervisor$JobSelectionCursor (jobManagerInventory)\n{\n /* Variables in this scope function as state information for next() */\n var jobManIdx = 0;\n /** @type {JobManager[]} - All job managers that are ready that have at least one ready slice. */\n var jobManList;\n /** @type {JobManager[]} - All jobManList elements that correspond to preferred jobs. */\n var prefJobManList;\n /* Number of working sandboxes allowed for a given job. */\n var concurrency = 1;\n const that = this;\n \n function seed (_concurrency)\n {\n function countSandboxes(jobAddress)\n {\n const jobManager = that.jobMap[jobAddress];\n if (!jobManager) throw new Error(`Lost track of job manager for address '${jobAddress}'`);\n return jobManager.workingSlices.length;\n }\n \n // Reset.\n jobManIdx = 0;\n\n /* Populate jobManList with jobs which are ready and have at least one slice which is ready. */\n jobManList = jobManagerInventory.filter((jobMan) => jobMan.ready && jobMan.readySlices.length > 0);\n\n /* Populate jobManList with jobManagers whose # of working slices is less than _concurrency. */\n jobManList = jobManList.filter((jobMan) => countSandboxes(jobMan.address) < _concurrency);\n\n /* Increasing sort. */\n jobManList.sort((a,b) => a.estimateWallMs - b.estimateWallMs);\n\n /* Populate prefJobManList with jobs from jobManList which also have a slicePreference set. */\n prefJobManList = jobManList.filter(jobMan => jobMan.hasOwnProperty('slicePreference') );\n }\n\n /**\n * Takes slices off the ready list, marks it reserved and increments workingSandboxCoun,\n * because the slice will soon be working.\n * @param {JobManager} jobMan \n * @returns {Slice}\n */\n function reserveSlice (jobMan)\n {\n const _readySlices = jobMan.readySlices;\n if (_readySlices.length > 0)\n {\n const slice = _readySlices[0];\n slice.markAsReserved();\n return slice;\n }\n return null;\n }\n\n /**\n * Each invocation of next() identifies one slice to run, or returns false if none can run.\n * @returns {Slice}\n */\n function next ()\n {\n /* Adjust order to schedule the heaviest job's first slice asap. */\n jobManList.unshift(jobManList.pop());\n\n let workingSliceCount;\n do\n {\n seed(concurrency);\n\n /* Maybe schedule a prefered job slice based on random chance .*/\n if (prefJobManList.length > 0)\n {\n let prioRan = Math.random();\n let list = prefJobManList.filter(jm => jm['slicePreference'] >= prioRan);\n\n if (list.length > 0)\n {\n const jobMan = list[list.length * Math.random()];\n const slice = reserveSlice(jobMan);\n if (slice)\n return slice;\n }\n }\n\n /* Schedule a slice from next job; jobs are in increasing order of estimated run time. */\n while (jobManIdx < jobManList.length)\n {\n const jobMan = jobManList[jobManIdx];\n jobManIdx++;\n const slice = reserveSlice(jobMan);\n if (slice)\n return slice;\n }\n\n /* If this is reached, we did not schedule a slice with current seed. We need\n * to re-seed to look for newly-available work and sandboxes, ratcheting up the\n * concurrency (max # of each job running) until we find something we can do.\n */\n workingSliceCount = that.workingSliceCount();\n debugging() && console.debug(`job selection - no slice found from ${jobManList.length} jobs for concurrency=${concurrency} and ${workingSliceCount} working sandboxes`);\n } while (jobManList.length > 0 && workingSliceCount < that.maxWorkingSandboxes && concurrency++ < that.maxWorkingSandboxes);\n\n return false; /* Did not find any more work that fits. */\n }\n \n return { next };\n}\n\n/**\n * Handle sandbox.work(...) errors.\n * @param {Sandbox} sandbox \n * @param {Slice} slice \n * @param {Error} error \n * @return {Promise<string>}\n */\nSupervisor.prototype.handleSandboxWorkError = async function Supervisor$handleSandboxWorkError (sandbox, slice, error)\n{\n if (!slice.isWorking) // Sanity. Exception should never fire.\n throw new Error(`handleSandboxWorkError: slice ${slice.identifier} must be WORKING.`);\n\n let logLevel, reason;\n \n if (error instanceof SandboxError)\n {\n logLevel = 'warn';\n reason = error.errorCode;\n // The message and stack properties of error objects are not enumerable,\n // so they have to be copied into a plain object this way\n // @ts-ignore\n error = Object.getOwnPropertyNames(error).reduce((o, p) => {\n o[p] = error[p]; return o;\n }, { message: 'Unexpected worker error' });\n }\n else\n {\n logLevel = 'error';\n if (error)\n reason = `Slice ${slice.sliceNumber} in state ${slice.state} of job ${slice.jobAddress} failed to complete execution with error ${this.checkCode(error)}.`;\n else\n {\n reason = `Slice ${slice.sliceNumber} in state ${slice.state} of job ${slice.jobAddress} failed to complete execution.`;\n error = new Error(reason);\n }\n // This error was unrelated to the work being done, so just return the slice\n // in the promise.catch in JobManager.runSliceOnSandbox .\n assert(slice.result === null);\n }\n \n this.handleFailedSlice(slice, error);\n\n let errorString;\n switch (reason)\n {\n case 'ENOPROGRESS':\n errorString = 'No progress error in sandbox.\\n';\n break;\n case 'ESLICETOOSLOW':\n errorString = 'Slice too slow error in sandbox.\\n';\n break;\n case 'EUNCAUGHT':\n errorString = `Uncaught error in sandbox ${error.message}.\\n`;\n break;\n case 'EFETCH':\n // The status.js processing does not have a case for 'EFETCH' yet.\n errorString = `Could not fetch data: ${error.message}.\\n`;\n break;\n }\n\n // Always display max info under debug builds, otherwise maximal error.\n // messages are displayed to the worker, only if both worker and client agree.\n const displayMaxInfo = slice.jobManager.displayMaxDiagInfo;\n\n const errorObject = {\n jobAddress: truncateAddress(slice.jobAddress, addressTruncationLength),\n sliceNumber: slice.sliceNumber,\n sandbox: sandbox.id,\n jobName: sandbox.public ? sandbox.public.name : 'unnamed',\n };\n if (displayMaxInfo)\n errorObject.stack += '\\n --------------------\\n' + (error.stack.split('\\n').slice(1).join('\\n'));\n\n if (error.name === 'EWORKREJECT')\n {\n reason = 'EWORKREJECT'; // The status.js processing does not have a case for 'EWORKREJECT' yet.\n error.stack = 'Sandbox was terminated by work.reject()';\n await this.handleWorkReject(sandbox, slice, error.message);\n }\n else\n {\n this.returnSlice(slice, reason);\n slice.jobManager.returnSandbox(sandbox);\n }\n\n if (errorString)\n console[logLevel](errorString, errorObject);\n else if (error.name === 'EWORKREJECT')\n console[logLevel](`Slice rejected work: ${error.message}`)\n else\n console[logLevel](`Slice failed: ${error.message}\\n`, errorObject);\n\n return reason;\n}\n\n/**\n * Slice has thrown error during execution:\n * Mark slice as failed, compensate when job is dicrete, emit events.\n * @param {Slice} slice\n * @param {Error} error\n */\nSupervisor.prototype.handleFailedSlice = function Supervisor$handleFailedSlice (slice, error)\n{\n assert(error, 'error must be valid');\n slice.collectResult(error, false);\n\n // If the slice from a job never completes and the job address exists in the ringBufferofJobs,\n // then we remove it to allow for another slice (from the same job) to be obtained by fetchTask\n this.ringBufferofJobs.buf = this.ringBufferofJobs.filter(element => element !== slice.jobAddress);\n\n this.emit('submitSliceFailed', error);\n this.emit('submitFinished');\n}\n\n// _Idx\n//\n// Return slices and sent progress reports to result-submitter-results.\n// Return slices to result-submitter-status which marks the\n// corresponding row in activeSlices to be rescheduled on usually another worker.\n//\n\n/**\n * Bulk-return multiple slices, possibly for assorted jobs.\n * Returns slices to the scheduler to be redistributed.\n * Called in the sandbox terminate handler and purgeAllWork(jobAddress)\n * and stopWork(forceTerminate).\n *\n * @param {Slice[]} slices - The slices to return to the scheduler.\n * @param {string} [reason='unknown'] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @param {boolean} [removeSlices=true] - When true, removes slices from this.sliceInventory .\n * @returns {Promise<*>} - Response from the scheduler.\n */\nSupervisor.prototype.returnSlices = function Supervisor$$returnSlices (slices, reason = 'unknown', removeSlices = true)\n{\n if (!slices || !slices.length) return Promise.resolve();\n debugging('supervisor') && console.debug('Supervisor.returnSlices: Returning slices', slices.map(slice => slice.identifier));\n\n const slicePayload = constructReturnSliceBuckets(slices, reason);\n if (removeSlices) slices.forEach((slice) => slice.jobManager.removeSlice(slice));\n\n try\n {\n return this.resultSubmitter.send('status', {\n worker: this.workerId,\n slices: slicePayload,\n }).catch(error => {\n const errorInfo = slices.map(slice => slice.identifier).sort();\n debuggingError && console.error('Failed to return slice(s)', { errorInfo, error }, 'Will try again on new connection.');\n return this.saveForResubmitToRS('status', { worker: this.workerId, slices: slicePayload });\n });\n }\n catch (error)\n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to return slices ${compressSlices(slices)}, no connection to result submitter:`, error);\n }\n}\n\n/** XXXpfr @todo TEMP -- Remove when sup2 replaces sup1 */\nfunction compressSlices(sliceArray)\n{\n const jobSliceMap = toJobMap(sliceArray, slice => slice.sliceNumber);\n return compressJobMap(jobSliceMap, false /* skipFirst*/, addressTruncationLength);\n}\n\n/**\n * Takes a slice and returns it to the scheduler to be redistributed.\n * Usually called when an exception is thrown by sandbox.work(...) .\n * Or when the supervisor tells it to forcibly stop working.\n *\n * @param {Slice} slice - The slice to return to the scheduler.\n * @param {string} [reason] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @returns {Promise<*>} - Response from the scheduler.\n */\nSupervisor.prototype.returnSlice = function Supervisor$$returnSlice (slice, reason)\n{\n assert(slice.sliceNumber > 0 && slice.jobManager);\n debugging() && console.debug(`Supervisor.returnSlice: Returning slice ${slice.identifier} with reason ${reason}.`);\n\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n\n try\n {\n slice.jobManager.removeSlice(slice);\n const payload = slice.getReturnMessagePayload(this.workerId, reason);\n return this.resultSubmitter.send('status', payload)\n .catch(error => {\n debuggingError && console.error('Failed to return slice', {\n sliceNumber: slice.sliceNumber,\n jobAddress: slice.jobAddress,\n status: slice.state.valueOf(),\n error,\n }, 'Will try again on a new connection.');\n return this.saveForResubmitToRS('status', payload);\n });\n }\n catch (error)\n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to return slice ${slice.identifier}, no connection to result submitter:`, error);\n }\n}\n\n/**\n * Send beacon to status.js for 'progress' and sliceStatus.scheduled.\n *\n * Run in an interval created in @constructor .\n * @returns {Promise<*>}\n */\nSupervisor.prototype.emitProgressReport = function emitProgressReport () \n{\n const slices = constructSliceBuckets( this.readySlices(), sliceStatus.scheduled );\n constructSliceBuckets( this.workingSlices(), 'progress', slices );\n\n debugging('supervisor') && console.debug('emitProgressReport:', stringify(slices));\n\n if (slices.length)\n {\n const progressReportPayload = {\n worker: this.workerId,\n slices,\n };\n\n try\n {\n return this.resultSubmitter.send('status', progressReportPayload)\n .catch(error => {\n debuggingError && console.error('479: Failed to send status beacon update:', error/*.message*/);\n return this.saveForResubmitToRS('status', progressReportPayload);\n });\n }\n catch (error) \n {\n /* resultSubmitter can be null if worker is stopped */\n debuggingError && console.error(`Failed to emit progress report, no connection to result submitter:`, error);\n }\n }\n}\n\n/**\n * Add a slice to the slice buckets being built. If a sliceBucket already exists for the\n * job-status-authMessage tuple, then the slice will be added to that, otherwise a new\n * sliceBucket will be added to the buckets.\n *\n * @param {Slice[]} slices - The slices.\n * @param {String} status - Status update, eg. progress or scheduled.\n * @param {Object[]} [sliceBuckets] - Slice buckets being built. Will be mutated in place.\n * @returns {Object[]} - mutated sliceBuckets array\n */\nfunction constructSliceBuckets (slices, status, sliceBuckets)\n{\n const jobMap = {};\n for (const slice of slices)\n {\n assert(slice.sliceNumber > 0 );\n if (!jobMap[slice.jobAddress]) jobMap[slice.jobAddress] = [];\n\n // Try to find a sliceBucket in the buckets which matches the job, status, and auth message.\n let sliceBucket = jobMap[slice.jobAddress].find(desc => {\n return desc.status === status\n && desc.authorizationMessage === slice.authorizationMessage;\n });\n\n if (!sliceBucket) jobMap[slice.jobAddress].push(slice.getMessage(status));\n else sliceBucket.sliceNumbers.push(slice.sliceNumber);\n }\n\n if (!sliceBuckets) return Object.values(jobMap);\n sliceBuckets.push(...Object.values(jobMap));\n return sliceBuckets;\n}\n \n/**\n * Add a slice to the returnSlice bucket being built. If a sliceBucket already exists for the\n * job-isEstimation-authMessage-reason tuple, then the slice will be added to that, otherwise a new\n * sliceBucket will be added to the buckets.\n *\n * @param {Slice[]} slices - The slices.\n * @param {String} [reason] - Optional reason to further characterize status; e.g. 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @param {Object[]} [sliceBuckets] - Optional slice buckets being built. Will be mutated in place.\n * @returns {Object[]} - mutated sliceBuckets array\n */\nfunction constructReturnSliceBuckets (slices, reason, sliceBuckets)\n{\n const jobMap = {};\n for (const slice of slices)\n {\n assert(slice.sliceNumber > 0 );\n if (!jobMap[slice.jobAddress]) jobMap[slice.jobAddress] = [];\n \n // Try to find a sliceBucket in the buckets which matches the job, estimation status, reason, and auth message.\n let sliceBucket = jobMap[slice.jobAddress].find(desc => {\n return desc.isEstimationSlice === slice.isEstimation\n && desc.authorizationMessage === slice.authorizationMessage\n && desc.reason === reason;\n });\n\n if (!sliceBucket) \n jobMap[slice.jobAddress].push(slice.getMessage('return', { isEstimationSlice: slice.isEstimation, reason }));\n else sliceBucket.sliceNumbers.push(slice.sliceNumber);\n }\n\n if (!sliceBuckets) return Object.values(jobMap);\n sliceBuckets.push(...Object.values(jobMap));\n return sliceBuckets;\n}\n \n// _Idx\n//\n// Task Distributor (TD): requestTask (Rq) support -- communication with TD.\n//\n\n/**\n * XXXpfr @todo Needs Work\n * For a given job, the scheduler stores an EMA approximation of average slice completion time in\n * jobPerfData.sliceCPUTime (and jobPerfData.sliceGPUTime, but we don't do the GPU analysis yet.)\n * However, each worker also tracks the same information and the ratio of local-info to scheduler-info\n * is returned by this.conversionQuantum so we can tell the task distributor how much work to return\n * from requestTask so that the work actually takes 5 minutes to complete when using all the worker sandboxes.\n * Note: \n * We average the completion times over the current jobs.\n * Define completion time in terms of sliceC(G)PUTime and sliceC(G)PUDensity\n * completion-time = (sliceCGPUTime + sliceCGPUTime) / ( sliceCPUDensity + sliceGPUDensity);\n * The local completion time is an EMA approximation of local completion-time as computed by Supervisor.recordResult.\n * The scheduler completion-time is computed directly from the corresponding row in jobPerfData.\n */\nSupervisor.prototype.conversionQuantum = function Supervisor$conversionQuantum()\n{\n let globalSpeed = 0, localSpeed = 0;\n for (const jobMan of this.jobManagerInventory)\n {\n const _globalTime = jobMan.globalTime;\n const _localTime = jobMan.statistics.ema;\n if (_globalTime > 0 && _localTime > 0)\n {\n //console.debug('conversionQuantum: local', _localTime, 'global', _globalTime);\n globalSpeed += _globalTime;\n localSpeed += _localTime;\n }\n }\n const conversion = globalSpeed > 0 ? localSpeed / globalSpeed : 1;\n return Math.min(Math.max(conversion, 0.2), 5.0); // Truncate if conversion is too bizarre.\n}\n\n/**\n * Remove all unreferenced jobs in this.jobManagerInventory and this.moduleCache.\n * Since job-managers are inserted into this.jobManagerInventory with a push, the job managers at the beginning are oldest.\n * Only delete #deleteCount of the oldest job-managers:\n * let deleteCount = this.jobManagerInventory.length - cachedJobsThreshold;\n * Edit cachedJobsThreshold to adjust the cache cleanup threshold.\n * @param {object[]} [newJobKeys=[]] - Jobs that should not be removed from this.jobManagerInventory and this.moduleCache.\n */\nSupervisor.prototype.clearUnusedJobManagersAndModuleCache = function Supervisor$clearUnusedJobManagersAndModuleCache(newJobKeys=[])\n{\n let deleteCount = this.jobManagerInventory.length - this.cachedJobsThreshold;\n if (deleteCount > 0)\n {\n selectiveDebugging && console.debug(`Supervisor.clearUnusedJobManagersAndModuleCache START: deleteCount ${deleteCount}/${this.jobManagerInventory.length}/${this.cachedJobsThreshold}.`);\n const jobMap = {};\n newJobKeys.forEach(jobAddress => { jobMap[jobAddress] = 1; });\n for (const jobManager of this.jobManagerInventory)\n {\n if (!jobMap[jobManager.address])\n {\n const sliceInventory = jobManager.sliceInventory.filter(slice => slice.isActive || slice.isQueued);\n if (sliceInventory.length < 1)\n {\n this.purgeJob(jobManager);\n if (--deleteCount < 1)\n break;\n }\n }\n }\n selectiveDebugging && console.debug(`Supervisor.clearUnusedJobManagersAndModuleCache FINISH: deleteCount ${deleteCount}/${this.jobManagerInventory.length}/${this.cachedJobsThreshold}.`);\n }\n}\n\n/**\n * Ask the scheduler (task distributor) for work.\n * @param {number} [unusedSandboxSlots]\n * @param {object[]} [jobs=[]] \n */\nSupervisor.prototype.requestTask = function Supervisor$requestTask (unusedSandboxSlots, jobs = [])\n{\n if (!this.isReady() || this.isFetchingNewWork)\n return Promise.resolve();\n\n if(!unusedSandboxSlots) unusedSandboxSlots = this.unusedSandboxSlots();\n if (unusedSandboxSlots < 1)\n {\n debugging('supervisor') && console.debug('requestTask: There are no unused sandbox slots.');\n return Promise.resolve();\n }\n\n // Refresh connections.\n this.instantiateAllConnections();\n\n // We prune for over this.maxTotalSandboxes about every 15 seconds, or when must prune level is reached.\n if (this.sandboxCount() > this.mustPruneSandboxLevel || Date.now() > this.lastPrune + this.pruneFrequency)\n {\n this.lastPrune = Date.now();\n this.pruneSandboxes();\n }\n\n try\n {\n this.isFetchingNewWork = true;\n const numCPUSlotToFill = this.numberOfAvailableSandboxSlots(unusedSandboxSlots);\n if (numCPUSlotToFill < 1)\n {\n //debugging() && console.debug('Predicted workload too high; not fetching additional work yet'); <-- Save Wes' msg...\n debugging() && console.debug('Supervisor.requestTask: We have enough, so start executing some slices.');\n return this.roundRobinSlices(); // roundRobinSlices guarantees this.isFetchingNewWork === false\n }\n\n /** XXXpfr @todo Get together with Wes to figure this out. */\n //let predictedLoad = this.predictLoad(Date.now() + ms(this.tuning.prefetchInterval)).load;\n\n const request = {\n numCores: numCPUSlotToFill,\n coreStats: this.getStatisticsCPU(),\n numGPUs: this.numGPU,\n //targetLoad: this.targetLoad.subtract(predictedLoad), /** XXXpfr @todo Get together with Wes to figure this out. */\n conversionQuantum: this.conversionQuantum(),\n capabilities: this.capabilities,\n paymentAddress: this.paymentAddress,\n jobAddresses: jobs.concat(this.options.jobAddresses || []), // When set, only fetches slices for these jobs.\n localExec: this.options.localExec,\n workerComputeGroups: this.generateWorkerComputeGroups(),\n minimumWage: workerTuning.minimumWage || this.options.minimumWage,\n loadedJobs: this.jobManagerInventory.map(jobMan => jobMan.address),\n readyJobs: this.jobManagerInventory.filter(jobMan => jobMan.ready).map(jobMan => jobMan.address),\n previouslyWorkedJobs: this.ringBufferofJobs.buf, // Only discrete jobs.\n rejectedJobs: this.rejectedJobs,\n };\n // Workers should be part of the public compute group by default.\n if (!booley(workerTuning.leavePublicGroup) && !booley(this.options.leavePublicGroup))\n request.workerComputeGroups.push(constants.computeGroups.public);\n\n // Call Task Distributor and handle response with this.addTaskToWorkload.\n this.fetchTask(request, (response) => this.addTaskToWorkload(request, response));\n }\n catch (error)\n {\n // Paranoid double-checking we don't accidently leave a live this.isFetchingNewWork.\n this.isFetchingNewWork = false;\n throw error;\n }\n}\n\n/** Gets the logical and physical number of cores and also the total number of sandboxes the worker is allowed to run. */\nSupervisor.prototype.getStatisticsCPU = function Supervisor$getStatisticsCPU ()\n{\n if (DCP_ENV.isBrowserPlatform)\n {\n return {\n worker: this.workerId,\n lCores: window.navigator.hardwareConcurrency,\n pCores: workerTuning.pCores || window.navigator.hardwareConcurrency,\n sandbox: this.maxWorkingSandboxes,\n }\n }\n\n return {\n worker: this.workerId,\n lCores: requireNative('os').cpus().length,\n pCores: requireNative('physical-cpu-count'),\n sandbox: this.maxWorkingSandboxes,\n }\n}\n\n/**\n * Callback for fetchTask.\n * @param {object} request \n * @param {object} response\n */\nSupervisor.prototype.addTaskToWorkload = function Supervisor$addTaskToWorkload (request, response)\n{\n try\n {\n const payload = response.payload;\n if (!response.success)\n {\n debugging() && console.debug('Task fetch failure; request=', request);\n debugging() && console.debug('Task fetch failure; response=', payload);\n // Only report errors when in 'ready' state.\n if (this.isReady()) throw new DCPError('Unable to fetch task for worker', payload);\n else return;\n }\n\n const sliceCount = payload.body.task.length || 0;\n if (sliceCount < 1)\n {\n if (selectiveDebugging2 && (this.lastTime + 7000 < Date.now()))\n {\n this.lastTime = Date.now();\n // Display the state of every slice.\n if (displaySliceState)\n {\n /** @type {JobManager} */\n const jm = this.jobManagerInventory.top();\n jm.dumpSlices(false /*details*/);\n }\n // Display completed results so far.\n if (displayCompletedResults && this.queuedSliceCount() < 1)\n {\n const values = Object.values(this.resultMap);\n if (values.length > 0)\n {\n values.forEach((descriptor) => descriptor.slices.sort((x,y) => x-y))\n console.debug(`Recorded results: job managers ${values.length}:`, this.resultMap);\n }\n }\n }\n this.emit('fetchedTask', { jobs: 0, slices: sliceCount });\n // There may be an extra slice to process.\n // roundRobinSlices guarantees this.isFetchingNewWork === false;\n return this.roundRobinSlices();\n }\n\n /**\n * payload structure: { owner: this.address, signature: signature, auth: messageLightWeight, body: messageBody };\n * messageLightWeight: { workerId: worker, jobSlices, schedulerId, jobCommissions }\n * messageBody: { newJobs: await-getNewJobsForTask(dbScheduler, task, request), task }\n */\n const { body, ...authorizationMessage } = payload;\n /** @type {{ newJobs: object, task: SliceMessage[] }} */\n const { newJobs, task } = body;\n assert(newJobs); // It should not be possible to have !newJobs -- we throw on !success.\n const newJobKeys = Object.keys(newJobs);\n const jobCount = newJobKeys.length;\n\n /*\n * Ensure all jobs received from the scheduler (task distributor) are:\n * 1. If we have specified specific jobs the worker may work on, the received jobs are in the specified job list\n * 2. If we are in localExec, at most 1 unique job type was received (since localExec workers are designated for only one job)\n * If the received jobs are not within these parameters, stop the worker since the scheduler cannot be trusted at that point.\n */\n if (request.jobAddresses.length > 0 && !newJobKeys.every((ele) => request.jobAddresses.includes(ele))\n || request.localExec && jobCount > 1)\n {\n this.error(\"Worker received slices it shouldn't have. Rejecting the work and stopping.\");\n process.exit(1);\n }\n\n selectiveDebugging && console.debug(`Supervisor.addTaskToWorkload: task: ${task.length}/${request.numCores}/${this.maxWorkingSandboxes}, conversion: ${request.conversionQuantum}, jobs: ${jobCount}, authSlices: ${compressJobMap(authorizationMessage.auth.authSlices, true /* skipFirst*/, addressTruncationLength /* digits*/)}`);\n\n // Clear out job managers w/o any queued slices,\n // and remove corresponding job references from module cache.\n // When a cached module no longer has any job references it is removed from the cache.\n this.clearUnusedJobManagersAndModuleCache(newJobKeys);\n\n // this.jobMap: job.address --> jobManager\n /** @type {Object.<Address, JobManager>} */\n this.jobMap = {};\n this.jobManagerInventory.forEach(jobManager => {\n this.jobMap[jobManager.address] = jobManager;\n });\n\n /** @type {Object.<Address, SliceMessage[]>} */\n const jobSliceMap = {};\n task.forEach((element) => {\n const address = String(element.jobAddress);\n if (!jobSliceMap[address]) jobSliceMap[address] = [element];\n else jobSliceMap[address].push(element);\n });\n\n debugging('supervisor') && console.debug('requestTask: slices, newJobs and jobMap', task.length, Object.keys(newJobs), Object.keys(this.jobMap));\n\n // Populate the job managers with slices, creating new job managers when necessary.\n // Set up discrete job ring buffer.\n for (const [jobAddress, jobEl] of Object.entries(newJobs))\n {\n if (this.jobMap.hasOwnProperty(jobAddress))\n {\n /** @type {JobManager} */\n const jm = this.jobMap[jobAddress];\n jm.update(jobEl, jobSliceMap[jobAddress], authorizationMessage);\n }\n else\n {\n // Add the slice messages to the job manager ctor, so that slice construction is after job manager is ready.\n const jobManager = new JobManager(this, jobEl, jobSliceMap[jobAddress], authorizationMessage);\n this.jobMap[jobAddress] = jobManager;\n this.jobManagerInventory.push(jobManager);\n\n // Populate the ring buffer based on job's discrete property.\n assert(jobEl.requirements);\n if (jobEl.requirements.discrete && this.ringBufferofJobs.find(address => address === jobEl.address) === undefined)\n this.ringBufferofJobs.push(jobEl.address);\n }\n }\n\n /**\n * The requestTask event fires when the supervisor has finished trying to\n * fetch work from the scheduler (task distributor). The data emitted is the\n * number of jobs and new slices to work on in the fetched task.\n *\n * @event Supervisor#requestTask\n * @type {object}\n */\n this.emit('fetchedTask', { jobs: jobCount, slices: sliceCount });\n\n // Start working on the slices.\n setImmediate(() => this.roundRobinSlices());\n }\n catch (error)\n {\n this.isFetchingNewWork = false; // Paranoid double checking that we don't leave this.isFetchingNewWork live.\n this.emit('fetchTaskFailed', error);\n debuggingError && console.error('Supervisor.requestTask failed!', error);\n }\n}\n\n/**\n * Returns the number of unused sandbox slots to fill -- sent to requestTask.\n * @returns {number}\n */\nSupervisor.prototype.numberOfAvailableSandboxSlots = function Supervisor$numberOfAvailableSandboxSlots(unusedSandboxSlots)\n{\n const _readySlices = this.readySlices();\n let numCores;\n if (this.options.priorityOnly && this.options.jobAddresses.length === 0)\n numCores = 0;\n else if (_readySlices.length > 1) // We have slices ready, no need to fetch.\n numCores = 0;\n else\n {\n // There are almost no ready slices (there may be 0 or 1), fetch a full task.\n // The task is full, in the sense that it will contain slices whose\n // aggregate execution time is this.maxWorkingSandboxes * 5-minutes.\n // However, there can only be unusedSandboxSlots # of long slices.\n // Thus we need to know whether the last slice in this.readySlices() is long or not.\n // (A long slice has estimated execution time >= 5-minutes on an average worker.)\n const longSliceCount = (_readySlices.length > 0 && _readySlices[0].isLong) ? 1 : 0;\n numCores = unusedSandboxSlots - longSliceCount;\n debugging('supervisor') && console.debug('numberOfAvailableSandboxSlots', numCores, unusedSandboxSlots, longSliceCount);\n }\n return numCores;\n}\n\n/**\n * @callback cbAddTaskToWorkload\n * @param {Response} response\n * @returns {Promise<void>}\n */\n\n/**\n * Call to fetch new slices from task distributor.\n * @param {*} request\n * @param {cbAddTaskToWorkload} addTaskToWorkload\n * @returns {Promise<void>}\n */\nSupervisor.prototype.fetchTask = async function Supervisor$fetchTask (request, addTaskToWorkload)\n{\n // Fetch a new task if we have insufficient slices queued, then start workers\n assert(this.isFetchingNewWork);\n\n this.instantiateAllConnections();\n\n // Top up sandboxes when necessary.\n const moreSandboxes = this.maxWorkingSandboxes - this.sandboxCount();\n if (moreSandboxes > 0)\n {\n await this.carefullyCreateSandboxes(moreSandboxes)\n .then(() => this.checkCapabilities()); /** XXXpfr @todo Do we need to check capabilities again? */\n }\n\n const fetchTimeout = setTimeout(() => {\n this.isFetchingNewWork = false;\n this.emit('warning', 'Fetch exceeded timeout, will reconnect at next watchdog interval');\n safeClose(this.taskDistributor, 'Fetch timed out', Math.random() > 0.5).catch(error => {\n this.error('Failed to close task-distributor connection', error);\n });\n safeClose(this.resultSubmitter, 'Fetch timed out', Math.random() > 0.5).catch(error => {\n this.error('Failed to close result-submitter connection', error);\n });\n this.instantiateAllConnections();\n }, 3 * 60 * 1000); // Max out at 3 minutes to fetch.\n\n // Ensure result submitter and task distributor connections before fetching tasks.\n try\n {\n await this.taskDistributor.keepalive();\n await this.resultSubmitter.keepalive();\n await this.taskDistributor.keepalive();\n }\n catch (e)\n {\n this.isFetchingNewWork = false;\n this.error('Failed to connect to result submitter, refusing to fetch slices. Will try again at next fetch cycle.', e);\n clearTimeout(fetchTimeout);\n safeClose(this.taskDistributor, 'Failed to connect to task-distributor', true).catch(error => {\n this.error('Failed to close task-distributor connection', error);\n });\n safeClose(this.resultSubmitter, 'Failed to connect to result-submitter', true).catch(error => {\n this.error('Failed to close result-submitter connection', error);\n });\n return Promise.resolve();\n }\n\n this.emit('fetchingTask');\n\n if (!this.taskDistributor) return\n return this.taskDistributor.send('requestTask', request)\n .then((response) => {\n addTaskToWorkload(response);\n })\n .catch((error) => {\n this.isFetchingNewWork = false; // Redundant.\n this.emit('fetchTaskFailed', error);\n this.error('Unable to request task from scheduler. Will try again on a new connection.', error);\n safeClose(this.taskDistributor, 'Failed to request task.', true);\n })\n .finally(() => {\n this.isFetchingNewWork = false;\n clearTimeout(fetchTimeout);\n });\n}\n\n/**\n * Generate the workerComputeGroups property of the requestTask message. \n * \n * Concatenate the compute groups object from dcpConfig with the list of compute groups\n * from the supervisor, and remove the public group if accidentally present. Finally,\n * we transform joinSecrets/joinHash into joinHashHash for secure transmission.\n *\n * @note computeGroup objects with joinSecrets are mutated to record their hashes. This\n * affects the supervisor options and dcpConfig. Re-adding a joinSecret property\n * to one of these will cause the hash to be recomputed.\n */\nSupervisor.prototype.generateWorkerComputeGroups = function Supervisor$generateWorkerComputeGroups ()\n{\n let computeGroups = Object.values(workerTuning.computeGroups || {});\n if (this.options.computeGroups)\n computeGroups = computeGroups.concat(this.options.computeGroups);\n computeGroups = computeGroups.filter(group => group.id !== constants.computeGroups.public.id);\n const hashedComputeGroups = [];\n for (const group of computeGroups)\n {\n const groupCopy = Object.assign({}, group);\n if ((group.joinSecret || group.joinHash) && (!group.joinHashHash || this.lastDcpsid !== this.taskDistributor.dcpsid))\n {\n let joinHash;\n if (group.joinHash)\n joinHash = group.joinHash.replace(/\\s+/g, ''); // strip whitespace\n else\n joinHash = calculateJoinHash(groupCopy);\n\n groupCopy.joinHashHash = hash.calculate(hash.eh1, joinHash, this.taskDistributor.dcpsid);\n delete groupCopy.joinSecret;\n delete groupCopy.joinHash;\n debugging('computeGroups') && console.debug(`Calculated joinHash=${joinHash} for`, groupCopy);\n }\n hashedComputeGroups.push(groupCopy);\n }\n this.lastDcpsid = this.taskDistributor.dcpsid;\n debugging('computeGroups') && console.debug('Requesting ', computeGroups.length, 'non-public groups for session', this.lastDcpsid);\n return hashedComputeGroups;\n}\n\n// _Idx\n//\n// Aggregators from the job managers.\n// Note: Not all functions are used yet.\n//\n/** XXXpfr @todo Figure out which aggregators to keep. */\n\n/**\n * Gather the count of job managers with queuedSlices.\n * @returns {number}\n */\nSupervisor.prototype.activeJobCount = function Supervisor$activeJobCount ()\n{\n let count = 0;\n this.jobManagerInventory.forEach((jobManager) => {\n if (jobManager.queuedSlices.length > 0) count++;\n });\n return count;\n}\n\n/**\n * Gather the ready slices from the job managers.\n * @returns {Slice[]}\n */\nSupervisor.prototype.readySlices = function Supervisor$readySlices ()\n{\n const readySlices = [];\n this.jobManagerInventory.forEach((jobManager) => {\n readySlices.push(...jobManager.readySlices);\n });\n return readySlices;\n}\n\n/**\n * Gather the working slices in the job managers.\n * @returns {Slice[]}\n */\nSupervisor.prototype.workingSlices = function Supervisor$workingSlices ()\n{\n const workingSlices = [];\n this.jobManagerInventory.forEach((jobManager) => {\n workingSlices.push(...jobManager.workingSlices);\n });\n return workingSlices;\n}\n\n/**\n * Gather the count of various kinds of slices over all the job managers.\n * @param {string} predicate - 'all;, 'ready', 'queued', 'reserved', 'working', 'workingOnly'.\n * @returns {number}\n */\nSupervisor.prototype.predicateSliceCount = function Supervisor$predicateSliceCount (predicate)\n{\n let count = 0;\n switch (predicate)\n {\n case 'all':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.sliceInventory.length;\n });\n break\n case 'ready':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.readySlices.length;\n });\n break;\n case 'queued':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.queuedSlices.length;\n });\n break;\n case 'reserved':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.reservedSlices.length;\n });\n break;\n case 'working': // both working and reserved (viz., soon-to-be-working)\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.workingSlices.length;\n });\n break;\n case 'workingOnly':\n this.jobManagerInventory.forEach((jobManager) => {\n count += jobManager.workingSlicesOnly.length;\n });\n break;\n }\n return count;\n}\n/** @returns {number} */\nSupervisor.prototype.sliceCount = function Supervisor$sliceCount () { return this.predicateSliceCount('all'); }\n/** @returns {number} */\nSupervisor.prototype.readySliceCount = function Supervisor$readySliceCount () { return this.predicateSliceCount('ready'); }\n/** @returns {number} */\nSupervisor.prototype.queuedSliceCount = function Supervisor$queuedSliceCount () { return this.predicateSliceCount('queued'); }\n/** @returns {number} */\nSupervisor.prototype.reservedSliceCount = function Supervisor$reservedSliceCount () { return this.predicateSliceCount('reserved'); }\n/** @returns {number} */\nSupervisor.prototype.workingSliceCount = function Supervisor$workingSliceCount () { return this.predicateSliceCount('working'); }\n/** @returns {number} */\nSupervisor.prototype.workingSliceOnlyCount = function Supervisor$workingSliceOnlyCount () { return this.predicateSliceCount('workingOnly'); }\n\n/**\n * Gather the count of working sandboxes over all the job managers.\n * @returns {number}\n */\nSupervisor.prototype.sandboxCount = function Supervisor$sandboxCount ()\n{\n return this.readiedSandboxes.length + this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated).length;\n}\n\n/**\n * Gather the count of working sandboxes over all the job managers.\n * @returns {number}\n */\nSupervisor.prototype.workingSandboxCount = function Supervisor$workingSandboxCount ()\n{\n return this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated && sandbox.isWorking).length;\n}\n\n// _Idx\n//\n// Sandbox creation and management.\n// \n\n/**\n * Create and start a Sandbox.\n * When this.readiedSandboxes.length > 0, use one of those sandboxes, instead of creating a new one.\n * @param {number} [delayMs=0] - The delay ms when calling sandbox.start(delayMs) .\n * @returns {Promise<Sandbox>}\n */\nSupervisor.prototype.createSandbox = function Supervisor$createSandbox (delayMs = 0)\n{\n const that = this;\n function getReadiedSandbox()\n {\n const sandbox = that.readiedSandboxes.pop();\n that.sandboxInventory.push(sandbox);\n return Promise.resolve(sandbox);\n }\n\n if (this.readiedSandboxes.length > 0)\n return getReadiedSandbox();\n\n // Do not place in this.readiedSandboxes, we'll directly use the return value of createSandbox.\n return this.createNewSandbox(delayMs, true/*putInInventory*/)\n .catch(() => {\n return this.carefullyCreateSandboxes(1)\n .then(() => {\n return getReadiedSandbox();\n });\n });\n}\n \n/**\n * Create and start a Sandbox.\n * Store it in this.readiedSandboxes or this.sandboxInventory according to putInInventory.\n * @param {number} [delayMs=0] - Millisecond delay when calling sandbox.start(delayMs), otherwise return it and use it.\n * @param {boolean} [putInInventory=false]\n * @returns {Promise<Sandbox>}\n */\nSupervisor.prototype.createNewSandbox = function Supervisor$createNewSandbox (delayMs = 0, putInInventory = false)\n{\n const rawSandbox = new Sandbox(this, { ...this.options.sandboxOptions });\n this.hookUpSandboxListeners(rawSandbox);\n return rawSandbox.start(delayMs)\n .then((sandbox) => {\n if (putInInventory) this.sandboxInventory.push(sandbox);\n else this.readiedSandboxes.push(sandbox);\n return sandbox;\n })\n .catch((error) => {\n if (!error) error = new Error('Unknown error creating sandbox.');\n debuggingWarn && console.warn(`Supervisor.createNewSandbox: Failed to start sandbox ${rawSandbox.identifier}.`, error);\n rawSandbox.terminate(false);\n if (error.code === 'ENOWORKER')\n throw new DCPError(\"Cannot use localExec without dcp-worker installed. Use the command 'npm install dcp-worker' to install the neccessary modules.\", 'ENOWORKER');\n throw error;\n });\n}\n\n/**\n * Bulk: create and start sandboxes and save in this.readiedSandboxes.\n * Call this function when there's a chance the evaluator is down.\n * @param {number} count - The number of sandboxes to create.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.carefullyCreateSandboxes = async function Supervisor$carefullyCreateSandboxes (count)\n{\n if (count < 1) return;\n // If the evaluator cannot start (e.g. if the evalServer is not running),\n // then the while loop will keep retrying until the evalServer comes online.\n let retry = 0;\n while (true)\n {\n try\n {\n await this.createNewSandbox();\n if (count > 1)\n this.createSandboxes(count - 1);\n }\n catch (error)\n {\n if (error.code === 'ENOWORKER') throw error;\n // Now assume the evaluator is down and keep retrying.\n /** XXXpfr @todo Need better indicator that evaluator is down. */\n if ((retry++ % 6) === 0)\n this.error('Failed to ready sandboxes; will keep retrying.', error);\n await a$sleepMs(1000 * Math.max(5, retry));\n }\n }\n}\n\n/**\n * Bulk: create and start sandboxes and save in this.readiedSandboxes.\n * @param {number} count - The number of sandboxes to create.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.createSandboxes = async function Supervisor$createSandboxes (count)\n{\n assert(count > 0);\n const promises = [], errors = [];\n for (let k = 0; k < count; k++)\n {\n promises.push(\n this.createNewSandbox(k === 0 ? 0: this.delayMs())\n .catch((error) => errors.push(this.checkCode(error))));\n }\n\n await Promise.all(promises);\n\n if (errors.length)\n this.emit('warning', `Failed to ready ${errors.length} of ${count} sandboxes: ${errors.map(err => \"\\n\\t\" + err.message)}`);\n\n // Sort so that pop() will return sandboxes in increasing order.\n this.readiedSandboxes.sort((x,y) => y.id - x.id);\n\n debugging('supervisor') && console.debug(`createSandboxes: Created ${count-errors.length} sandboxes.`, this.readiedSandboxes.map(s => s.id));\n}\n\n/**\n * For a given sandbox, hook up all the Sandbox listeners.\n * @param {Sandbox} sandbox \n */\nSupervisor.prototype.hookUpSandboxListeners = function hookUpSandboxListeners (sandbox) \n{\n sandbox.addListener('ready', () => this.emit('sandboxReady', sandbox));\n\n sandbox.addListener('start', () => {\n this.emit('sandboxStart', sandbox);\n\n if (sandbox.slice)\n {\n try\n {\n const statusPayload = sandbox.slice.getMessagePayload(this.workerId, 'begin');\n return this.resultSubmitter.send('status', statusPayload).catch((error) => {\n debuggingError && console.error(`Error sending 'status' for slice ${sandbox.slice.identifier}:\\n\\t${error}\\n\\tWill try again on a new connection`);\n return this.saveForResubmitToRS('status', statusPayload);\n });\n } \n catch (error)\n {\n /* resultSubmitterConnection can be null if worker is stopped */\n debuggingError && console.error(`Failed to send 'begin' status for slice ${sandbox.slice.identifier}, no connection to result submitter`, error);\n }\n }\n });\n\n sandbox.addListener('workEmit', ({ eventName, payload }) => {\n // Need to check if the sandbox hasn't been assigned a slice yet.\n if (!sandbox.slice)\n this.error(`Sandbox not assigned a slice before sending workEmit message to scheduler.\\n\\t'workEmit' event originates from '${eventName}' event`);\n else\n {\n const slice = sandbox.slice;\n // Sometimes a sliceNumber===0 workEmit comes in before the client bundle is properly loaded.\n // Also happens with minor dcp-client version mismatches.\n // sliceNumber===0 <==> authorizationMessage undefined...\n if (!slice.authorizationMessage)\n {\n if (slice.sliceNumber > 0)\n this.emit('warning', `workEmit: missing authorization message for slice ${slice.identifier}`);\n }\n else if (this.eventRouter) // No reason to emit if event router is closed.\n {\n const workEmitPayload = {\n eventName,\n payload,\n job: slice.jobAddress,\n slice: slice.sliceNumber,\n worker: this.workerId,\n authorizationMessage : slice.authorizationMessage,\n };\n\n const workEmitPromise = this.eventRouter.send('workEmit', workEmitPayload).catch(error => {\n debuggingWarn && console.warn(`workEmit: Unable to send ${eventName} for slice ${slice.identifier}: ${error.message}.\\n\\tTrying again on a new connection.`);\n this.eventRouterMessageQueue.push({ operation: 'workEmit', data: workEmitPayload })\n safeClose(this.eventRouter); // stopWork could slip-in during eventRouter.send\n if (this.debugBuild) this.error('workEmit error:', error);\n });\n\n if (this.debugBuild)\n {\n workEmitPromise.then(result => {\n if (!result) this.emit('warning', `workEmit: Event router did not accept event ${eventName}`);\n });\n }\n }\n }\n });\n\n sandbox.on('rejectedWorkMetrics', (data) => {\n // If the slice already has rejectedTimeReport, add this data to it. If not, assign this data to slices rejectedTimeReport property\n if (sandbox.slice) \n {\n if (!sandbox.slice.rejectedTimeReport) sandbox.slice.rejectedTimeReport = data.timeReport;\n else \n {\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (data.timeReport[key]) sandbox.slice.rejectedTimeReport[key] += data.timeReport[key];\n })\n }\n }\n });\n\n // If the sandbox terminated and we are not shutting down, then we should return all work which is\n // currently not being computed if all sandboxes are dead and the attempt to create a new one fails.\n sandbox.on('terminated', async () => {\n let nonTerminatedSandboxes = this.sandboxInventory.filter(sbx => !sbx.isTerminated);\n if (nonTerminatedSandboxes.length === 0 && this.worker.working)\n {\n debugging('supervisor') && console.debug(`hookUpSandboxListeners: Try to create 1 sandbox in the sandbox-terminated-handler.`);\n const _sandbox = await this.createNewSandbox()\n .catch((error) => {\n debugging('supervisor') && console.warn('Failed to replace terminated sandbox; evalserver may be gone.', error.message);\n error.message = 'Failed to replace terminated sandbox: ' + error.message;\n this.emit('warning', error);\n });\n\n // If we cannot create a new sandbox, that probably means we're on a screensaver worker\n // and the screensaver is down. So return the slices to the scheduler.\n if (!_sandbox) this.screenSaverDestroy();\n }\n });\n\n sandbox.on('error', (error) => this.emit('error', error));\n sandbox.on('warning', (warning) => this.emit('warning', warning));\n}\n\n/**\n * Terminate extra sandboxes over the limit: this.maxTotalSandboxes.\n * First terminate assigned sandboxes which are unlikely to be used with the current ready slices.\n * Then terminate the unassigned sandboxes: this.readiedSandboxes.\n * (There should be no readied sandboxes at this point.)\n * Then round-robin prune 1 assigned sandbox from each jobmanager.\n * XXXpfr @todo Prioritize sandboxes that we wish to keep.\n * E.g. When a sandbox is especially expensive to assign.\n */\nSupervisor.prototype.pruneSandboxes = function Supervisor$pruneSandboxes () \n{\n let pruneCount = this.sandboxCount() - this.maxTotalSandboxes;\n if (pruneCount <= 0) return;\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes START: pruneCount ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n // Only prune the extras: jm.assignedSandboxes.length > jm.queuedSlices.length .\n // Round-robin prune 1 extra assigned sandbox from each jobmanager.\n const readyJobManagers = this.jobManagerInventory.filter(jm => jm.ready);\n while (true)\n {\n const _pruneCount = pruneCount;\n for (const jm of readyJobManagers)\n {\n if (jm.pruneExtraAssignedSandbox())\n {\n if (--pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n }\n if (pruneCount === _pruneCount)\n break;\n }\n assert(pruneCount > 0);\n // Prune the excess non-assigned sandboxes -- we should never hit this.\n if (this.readiedSandboxes.length > 0)\n {\n const toPrune = this.readiedSandboxes.slice(0, pruneCount);\n this.readiedSandboxes = this.readiedSandboxes.slice(pruneCount);\n toPrune.forEach(sandbox => sandbox.terminate(false));\n pruneCount -= toPrune.length;\n if (pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n // Round-robin prune 1 assigned sandbox from each jobmanager.\n while (true)\n {\n const _pruneCount = pruneCount;\n for (const jm of readyJobManagers)\n {\n if (jm.pruneAssignedSandbox())\n {\n if (--pruneCount < 1)\n {\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n return;\n }\n }\n }\n if (pruneCount === _pruneCount)\n break;\n }\n this.sandboxInventory = this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated);\n selectiveDebugging && console.debug(`Supervisor.pruneSandboxes FINISH: unpruned ${pruneCount}/${this.sandboxCount()}/${this.maxTotalSandboxes}.`);\n}\n\n// _Idx\n//\n// Result-submitter-result support functions.\n// Send in the results!!!\n//\n\n/**\n * Submits the slice results to the result-submitter service.\n * Then remove the slice from the its job manager.\n *\n * @param {Slice} slice - The slice to submit.\n * @returns {Promise<void>}\n */\nSupervisor.prototype.recordResult = function Supervisor$recordResult (slice)\n{\n // It is possible for slice.result to be undefined when there are upstream errors.\n if ( !(slice && slice.result))\n throw new Error(`recordResult: slice.result is undefined for slice ${slice.identifier}. This is ok when there are upstream errors.`); \n if (!slice.isComplete)\n throw new Error('Cannot record result for slice that has not completed execution successfully.');\n\n debugging('supervisor') && console.debug(`supervisor: recording result for slice ${slice.identifier}.`);\n\n /* @see result-submitter::result for full message details */\n const metrics = { GPUTime: 0, CPUTime: 0, CPUDensity: 0, GPUDensity: 0, total: 0 };\n const payloadData = {\n slice: slice.sliceNumber,\n job: slice.jobAddress,\n worker: this.workerId,\n paymentAddress: this.paymentAddress,\n metrics,\n authorizationMessage: slice.authorizationMessage,\n }\n\n const timeReport = slice.timeReport;\n if (timeReport)\n {\n debugging('supervisor') && console.debug('recordResult timeReport', timeReport);\n // If slice takes less than 1ms to execute, CPUTime will be 0, so compensate.\n if (timeReport.CPU < 1)\n {\n timeReport.CPU++;\n timeReport.total++;\n }\n if (timeReport.total < timeReport.CPU + timeReport.webGL)\n {\n // Compensate or throw? For now we compensate.\n debuggingWarn && console.warn(`Supervisor.recordResult:: Inconsistent time report -- total < CPU + webGL -- ${stringify(timeReport)}`)\n //throw new Error(`recordResult: Inconsistent time report -- total < CPU + webGL -- ${stringify(timeReport)}`)\n timeReport.total = timeReport.CPU + timeReport.webGL;\n }\n if (timeReport.total > 0)\n {\n slice.jobManager.updateStatistics(timeReport);\n metrics.total = timeReport.total;\n metrics.CPUTime = timeReport.CPU;\n metrics.GPUTime = timeReport.webGL;\n metrics.CPUDensity = metrics.CPUTime / timeReport.total;\n metrics.GPUDensity = metrics.GPUTime / timeReport.total;\n }\n }\n\n this.emit('submittingResult');\n\n if (!this.resultSubmitter)\n this.connectTo('resultSubmitter');\n\n if (slice.resultStorageType === 'pattern')\n return this.sendResultToRemote(slice)\n .then((response) => {\n payloadData.result = response;\n this.sendToResultSubmitter(slice, payloadData);\n });\n\n payloadData.result = encodeDataURI(slice.result.result);\n return this.sendToResultSubmitter(slice, payloadData);\n}\n\n/**\n * @param {Slice} slice\n * @param {*} payloadData\n * @returns {Promise<void>}\n */\nSupervisor.prototype.sendToResultSubmitter = function Supervisor$sendToResultSubmitter (slice, payloadData)\n{\n const that = this;\n function handleRSError (error, payloadData)\n {\n that.error(`Failed to submit results to scheduler for slice ${payloadData.slice} of job ${payloadData.job}`, error);\n //slice.jobManager.dumpSlices('recordResult');\n that.saveForResubmitToRS('result', payloadData)\n .then((msg) => {\n if (!error && msg) error = new Error(`resultSubmitter is ${msg}`);\n that.emit('submitSliceFailed', error);\n throw error;\n });\n }\n\n try\n {\n debugging('supervisor') && console.debug('Supervisor.recordResult: payloadData', payloadData.result.slice(0, 256), slice.identifier);\n\n return this.resultSubmitter.send('result', payloadData)\n .then((resp) => {\n if (!resp.success)\n throw resp.payload;\n\n debugging('supervisor') && console.debug('recordResult: SUCCESS', slice.identifier);\n\n const receipt = {\n accepted: true,\n payment: resp.payload.slicePaymentAmount,\n };\n this.emit('submittedResult', resp.payload);\n this.emit('dccCredit', receipt);\n })\n .catch ((error) => {\n handleRSError (error, payloadData);\n });\n }\n catch (error)\n {\n handleRSError (error, payloadData);\n }\n finally\n {\n slice.markAsFinished();\n this.emit('submitFinished');\n // Remove the slice from the job manager.\n slice.jobManager.removeSlice(slice);\n if (this.sliceTiming)\n {\n slice['resultDelta'] = Date.now() - slice['resultDelta'];\n console.debug(`recordResult(${slice['queueingDelta']}, ${slice['executionDelta']}, ${slice['resultDelta']}): Completed slice ${slice.identifier}.`);\n }\n if (selectiveDebugging)\n {\n if (!this.resultMap[slice.jobAddress]) this.resultMap[slice.jobAddress] = { slices: [], totalTimes: [] };\n this.resultMap[slice.jobAddress].slices.push(slice.sliceNumber);\n this.resultMap[slice.jobAddress].totalTimes.push(payloadData.metrics.total);\n }\n }\n}\n\n/**\n * Send a work function's result to a server that speaks our DCP Remote Data Server protocol.\n * E.g. https://gitlab.com/Distributed-Compute-Protocol/dcp-rds\n *\n * @param {Slice} slice - Slice object whose result we are sending.\n * @returns {Promise<string>}\n * @throws When HTTP status not in the 2xx range.\n */\nSupervisor.prototype.sendResultToRemote = function Supervisor$sendResultToRemote (slice)\n{ \n /** XXXpfr @todo: Support file upload and other contentTypes. */\n function serializeContent(result, postParams) {\n const ctArray = postParams.contentType.split(';');\n switch (ctArray[0]) {\n case 'application/json':\n return JSON.stringify(result);\n case 'application/kvin':\n return (__webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\").serialize)(result);\n case 'application/octet-stream':\n case 'application/gzip':\n if (ctArray.length === 2 && ctArray[1] === 'base64')\n return btoa(result);\n // fall-through\n case 'text/plain':\n case 'text/html':\n case 'text/css':\n case 'text/csv':\n case 'text/javascript':\n return result;\n\n default:\n {\n const mtArray = ctArray[0].split('/');\n if (mtArray[0] === 'image' || mtArray[0] === 'video' || mtArray[0] === 'audio')\n return (ctArray.length === 2 && ctArray[1] === 'base64') ? btoa(result) : result;\n throw new Error(`sendResultToRemote: Unsupported contentType ${postParams.contentType}`);\n }\n }\n }\n\n // Construct postParams.\n const postParams = { ...slice.resultStorageParams };\n if (!postParams.contentType)\n postParams.contentType = 'application/json';\n if (!postParams.element)\n postParams.element = slice.sliceNumber;\n debugging('supervisor') && console.debug('sendResultToRemote postParams: ', postParams);\n\n // Construct result.\n const result = slice.result.result;\n if (result)\n postParams.content = serializeContent(result, postParams);\n else\n postParams.error = serializeContent(slice.error, postParams);\n debugging('supervisor') && console.debug('sendResultToRemote content: ', (result ? postParams.content : postParams.error).slice(0, 512));\n\n // Construct url.\n const sliceResultUri = makeValueURI('pattern', slice.resultStorageDetails, {\n slice: slice.sliceNumber,\n job: slice.jobAddress,\n });\n debugging() && console.debug('sendResultToRemote sliceResultUri: ', sliceResultUri);\n const url = new DcpURL(sliceResultUri);\n\n // Check allowed origins.\n if (this.makeSafeOriginList('sendResults').indexOf(url.origin) === -1)\n throw new Error(`Invalid origin for remote result storage: '${url.origin}'`);\n\n // Fetch.\n return justFetch(url, 'JSON', 'POST', false, postParams)\n .then((response) => encodeDataURI(JSON.stringify(response)));\n}\n\n// _Idx\n//\n// Reject.\n//\n\n/**\n * Handles reassigning or returning a slice that was rejected by a sandbox.\n *\n * If the slice does not have a rejected property already, reassign the\n * slice to a new sandbox and add a rejected property to the slice to\n * indicate it has already rejected once.\n *\n * If the slice rejects with a reason, or has a rejected time stamp\n * (ie. has been rejected once already) then return all slices from the\n * job to the scheduler and terminate all sandboxes with that jobAddress.\n *\n * The sandbox will be terminated.\n *\n * @param {Sandbox} sandbox\n * @param {Slice} slice\n */\nSupervisor.prototype.handleWorkReject = async function Supervisor$handleWorkReject (sandbox, slice, rejectReason)\n{\n debugging() && console.debug('handleWorkReject', rejectReason, slice.rejectedTimeStamp, slice.identifier);\n\n // Do a hard flush of the microtask queue and finish the current event loop.\n await new Promise((resolve) => setImmediate(() => setTimeout(resolve, 0)));\n\n const jobManager = slice.jobManager;\n jobManager.rejectedJobReasons.push(rejectReason); // memoize reasons\n\n // First time rejecting without a reason. Try assigning slice to a new sandbox.\n if (rejectReason === 'false' && !slice.rejectedTimeStamp)\n {\n // Set rejected time stamp.\n slice.rejectedTimeStamp = Date.now();\n // Schedule the slice for execution.\n jobManager.scheduleSlice(slice, true /* placeInTheFrontOfTheQueue*/);\n // Slice has been rescheduled, but we still need to terminate the sandbox.\n jobManager.returnSandbox(sandbox);\n }\n else\n { \n // Slice has a reason OR rejected without a reason already and got stamped.\n // Add to array of rejected jobs.\n let rejectedJob = {\n address: slice.jobAddress,\n reasons: jobManager.rejectedJobReasons,\n }\n this.rejectedJobs.push(rejectedJob);\n\n // Purge the job.\n this.purgeJob(jobManager);\n\n // Tell everyone all about it, when allowed.\n if (jobManager.displayMaxDiagInfo)\n {\n const suffixMsg = '\\n\\tAll slices with the same jobAddress returned to the scheduler.\\n\\tAll sandboxes with the same jobAddress are terminated.';\n if (slice.rejectedTimeStamp)\n this.emit('warning', `work.reject: The slice ${slice.identifier} was rejected twice.${suffixMsg}`);\n else\n this.emit('warning', `work.reject: The slice ${slice.identifier} was rejected with reason ${rejectReason}.${suffixMsg}`);\n }\n }\n}\n\n// _Idx\n//\n// Unused functions that we need to review.\n// 1) destroy, shutdown, halt -- possibly need to incorporate these ideas in stopWork\n// 2) predictLoad -- XXXpfr: I really feel bad about not being able to figure out how to incorporate\n// this into the design of sup2. This was a central part of Wes' design of sup2.\n// I need to collaborate with Wes to resolve my ignorance.\n//\n\n/**\n * UNUSED\n * @deprecated -- may use later\n **/\nSupervisor.prototype.destroy = function Supervisor$destory()\n{\n selectiveDebugging && console.debug(`Supervisor.screenSaverDestroy: destroying Supervisor and everything else.`);\n this.stopWork(true /*forceTerminate*/);\n if (this.state) this.state.destroy();\n if (this.progressReportTimer) clearInterval(this.progressReportTimer);\n if (this.watchdogTimer) clearInterval(this.watchdogTimer);\n this.state = null;\n this.progressReportTimer = null;\n this.watchdogTimer = null;\n this.jobManagerInventory = null;\n this.sandboxInventory = [];\n this.readiedSandboxes = [];\n this.closeConnections();\n}\n\n/**\n * UNUSED\n * @deprecated -- may use later \n * Halt the Supervisor as quickly as possible.\n **/\nSupervisor.prototype.halt = function Supervisor$halt()\n{\n this.state.setIf('ready', 'stopping');\n if (!this.state.is('stopping'))\n throw new Error(`Supervisor has an invalid state ${this.state} for halt`);\n clearInterval(this.watchdogTimer);\n\n for (let jobMan of this.jobManagerInventory)\n {\n jobMan.state.setIf('ready', 'stop');\n for (const sandbox of jobMan.workingSandboxes)\n {\n sandbox.stop(); // NYI -- will terminate.\n }\n }\n}\n \n/**\n * UNUSED\n * @deprecated -- may use later \n * Shutdown the supervisor; attempts to return work which will not be finished before timeout expires.\n * The shutdown is complete once this supervisor emits the stopped state change.\n */\nSupervisor.prototype.shutdown = function Supervisor$shutdown(timeoutMs)\n{\n var ps = [], returnSliceInventory = [];\n var timer;\n\n this.state.setIf('ready', 'stopping');\n if (!this.state.is('stopping'))\n throw new Error(`Supervisor has an invalid state ${this.state} for shutdown`);\n clearInterval(this.watchdogTimer);\n\n for (let jobMan of this.jobManagerInventory)\n {\n jobMan.state.setIf('ready', 'stop');\n\n for (let slice of jobMan.sliceInventory)\n {\n if (slice.state.is('initial') || slice.state.is('ready'))\n {\n returnSliceInventory.push(slice);\n }\n else if (slice.state.is(sliceStatus.working))\n {\n ps.push(new Promise((resolve, reject) => {\n slice.state.on('change', (status) => {\n if (status === 'done')\n resolve();\n });\n }));\n }\n }\n }\n\n const reason = 'Supervisor.shutdown';\n this.returnSlices(returnSliceInventory, reason);\n timer = setTimeout(this.halt.bind(this), timeoutMs);\n Promise.all(ps)\n .then(() => {\n clearTimeout(timer);\n this.state.set('stopping', 'stopped');\n })\n .catch((e) => {\n if (e.code !== 'DCP_SUPERVISOR_ESYNC')\n throw e; /* becomes unhandled rejection */\n });\n}\n\n/** \n * Factory function which generates a list of origins which are safe to communicate \n * with for this purpose. Currently-valid purposes (more will be added):\n * - any\n * - fetchData\n * - fetchWorkFunctions\n * - fetchArguments\n * - sendResults\n */\nSupervisor.prototype.makeSafeOriginList = function Supervisor$$makeSafeOriginList(purpose)\n{\n var list = [];\n \n if (this[purpose])\n list = list.concat(this[purpose]);\n \n /* Add 'any' origin(s) to list iff not in localExec, or in localExec and purpose is sendResults */\n if (!this.options.localExec || (this.options.localExec && purpose === 'sendResults'))\n list = list.concat(this.allowedOrigins)\n \n return list;\n}\n \n/**\n * UNUSED -- DOES NOT WORK YET.\n * NEED TO WORK WITH WES TO FIGURE OUT BEST WAY TO GET PREDICTLOAD TO WORK.\n * Predict the load on this supervisor based on the local job measurement data.\n * Works by looking at current conditions and available slices, and tries to guess\n * in what order they will be finished, working, etc. \n *\n * The simulation is very naive, but is expected to be accurate several seconds\n * into the future, particularly as we approach the end of a task.\n *\n * @param {number} whenMs \n * \n * @returns {Object<load, jobManagerInventory>} where load is and instance of Load and the predicted \n * load at the prediction time, and jobManagerInventory \n * is a counterfeit which holds the predicted state of \n * the jobManagerInventory at that time.\n */\nSupervisor.prototype.predictLoad = function Supervisor$predictLoad (whenMs)\n{\n /** @type {JobManager[]} */\n var jmi = new Inventory(); /* Inventory of counterfeit JobManagers. */\n var load = new Load(0,0); /* This \"current\" load throughout the prediction. */\n /** @type {Slice} */\n var next; /* The next slice to \"finish\". */\n\n /* Initialize data structures for prediction from current activity. */\n for (let jobMan of this.jobManagerInventory.filter(jm => jm.state.is('ready') && jm.sliceInventory.length))\n {\n jobMan = jobMan.counterfeit();\n jmi.push(jobMan);\n jobMan.sliceInventory.forEach((s) => s.state.setIf('initial', 'ready'));\n }\n next = findNextSlice();\n \n /**\n * Routine that finds the slice that will end next (soonest.)\n * @returns {Slice}\n */\n function findNextSlice()\n {\n /** @type {Slice} */\n var _next;\n for (let jobMan of jmi)\n {\n const _workingSlices = jobMan.workingSlices;\n for (let slice of _workingSlices)\n {\n //\n // slice.etaMs is the estimated time interval until slice execution completes.\n //\n // If the slice hasn't started,\n // slice.etaMs = slice.jobManager.estimateWallMs,\n // else if the slice has completed execution:\n // slice.etaMs = 0.\n // else if the slice has started:\n // slice.jobManager.estimateWallMs - (Date.now() - slice.startTime).\n //\n if (_next && (_next.etaMs <= slice.etaMs))\n continue;\n\n _next = slice;\n }\n }\n load.add(_next.jobManager.metrics);\n \n return _next;\n }\n\n /* At this point, jmi is an Inventory of counterfeit job managers that are \"ready\" for\n * work, next.etaMs is the time interval until the next slice will finish, and we have\n * a reasonably accurate picture of our current load.\n *\n * Next, we \"end\" this slice, try to fill all cores, and push the timeline forward to\n * the next predicted end of slice.\n */\n for (next = findNextSlice();\n next && (next.etaMs < whenMs);\n next = findNextSlice())\n {\n let ended = next;\n let cursor = this.makeJobSelectionCursor(jmi);\n\n /* \"end\" this slice */\n load.subtract(ended.jobManager.metrics);\n /* Fake out collecting result to transition state to FINISHED. */\n ended.collectResult(null);\n\n /* \"start\" as many slices as we can - given our CPU/GPU constraints, slice data in memory, etc */\n while (this.targetLoad.fits(load))\n {\n let slice = cursor.next();\n if (!slice)\n break; /* Running out of work that fits. */\n\n if (!load.fits(this.targetLoad, slice.jobManager.metrics))\n continue;\n\n /* Pick a ready slice from this job and add its anticipated load to our current load if it will fit */\n slice = slice.jobManager.readySlices.shift();\n slice.markAsWorking(); // ?? Not sure this is correct.\n //slice.etaMs = ended.etaMs + slice.jobManager.estimateWallMs; wtf?!?! <--- LOOK HERE\n\n load.add(slice.jobManager.metrics);\n }\n }\n\n return { load, jobManagerInventory: jmi };\n}\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/index.js?");
4531
4575
 
4532
4576
  /***/ }),
4533
4577
 
@@ -4538,7 +4582,7 @@ eval("/* provided dependency */ var process = __webpack_require__(/*! ./node_mod
4538
4582
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4539
4583
 
4540
4584
  "use strict";
4541
- eval("/**\n * @file dcp-client/worker/supervisor2/job-manager.js\n *\n * A support class for Supervisor2.\n * It is a wrapper for the job object returned from the requestTask,\n * along with tracking slices and sandboxes associated with the job. \n *\n * @author Wes Garland, wes@kingsds.network,\n * Paul, paul@kingsds.network,\n * @date Dec 2020,\n * June 2022,\n * @module JobManager\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\n/** @typedef {import('./').Supervisor} Supervisor */\n/** @typedef {import('./sandbox2').Sandbox} Sandbox */\n/** @typedef {import('dcp/common/dcp-url').DcpURL} DcpURL */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n/** @typedef {string} opaqueId */ // 22 character base64 string \n/** @typedef {string} address */ // String(Address)\n\nconst inspect = Symbol.for('nodejs.util.inspect.custom');\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst { Inventory } = __webpack_require__(/*! dcp/utils/inventory */ \"./src/utils/inventory.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { Slice } = __webpack_require__(/*! ./slice2 */ \"./src/dcp-client/worker/supervisor2/slice2.js\");\nconst { Load } = __webpack_require__(/*! ./load */ \"./src/dcp-client/worker/supervisor2/load.js\");\nconst { Statistics } = __webpack_require__(/*! ./rolling-statistics */ \"./src/dcp-client/worker/supervisor2/rolling-statistics.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { rehydrateRange } = __webpack_require__(/*! dcp/dcp-client/range-object */ \"./src/dcp-client/range-object.js\");\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nconst scopedKvin = new kvin.KVIN({Object: ({}).constructor,\n Array: ([]).constructor, \n Function: (()=>{}).constructor});\nconst { fetchURI, dumpObject, truncateAddress, stringify } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\nconst addressTruncationLength = 20;\nlet workerTuning = dcpConfig.worker;\nif (!workerTuning) workerTuning = dcpConfig.Supervisor;\nif (!workerTuning || !workerTuning.maxSandboxErrorsPerSlice || !workerTuning.allowConsoleAccess)\n workerTuning = { maxSandboxErrorsPerSlice: 2, allowConsoleAccess: false };\n\n// This is for debugging and performance testing.\n// Several functions chain and return non-awaited promises.\n// When AWAIT_ALL is true, we await the promises, which makes easier debugging.\nconst AWAIT_ALL = false;\n\n// Debug tracing helpers.\nconst selectiveEnable = false;\nconst displayWarnError = false || selectiveEnable;\nconst selectiveDebugging = selectiveEnable || debugging();\nconst debuggingError = false || selectiveDebugging || displayWarnError;\nconst debuggingWarn = false || selectiveDebugging || displayWarnError;\nconst selectiveDebugging2 = selectiveEnable && false || debugging('jobmanager');\n\nconst INITIAL = 'initial';\nconst READY = 'ready';\nconst STOP = 'stop';\nconst REFUSE = 'refuse';\nconst BROKEN = 'broken';\n\n//\n// Index to functionality -- search for '_Idx' to toggle through the index.\n//\n// 1) Ctor: JobManager class definition and various properties.\n// 2) Statistics: updateStatistics, update.\n// 3) Dtors: destroy, stopWork.\n// 4) Assign sandboxes and execute slices.\n// 5) Remove from array and return sandbox.\n// 6) Fetch: WorkFn, slices, arguments.\n// 7) Miscellaneous.\n// 8) Unused functions that we need to review.\n//\n\n// _Idx\n//\n// Ctor: JobManager class definition and various properties.\n// A JobManager handles all scheduling of slices for a given job.\n// It's also responsible for fetching slice data, work functions and arguments.\n// And it collects statistics about slice completion times and resource usage.\n// All functionality across jobs is handled by Supervisor.\n//\n\n/**\n * JobManager Constructor. An instance of JobManager knows everything about a given\n * job within the supervisor, including:\n * - work function code\n * - work function arguments\n * - all the SliceManager instances that go with this job\n * - how long a slice of this job is expected to run\n *\n * Instances of JobManager emit the following events:\n * - addSlice(sliceHandle)\n * - deleteSlice(sliceHandle)\n * - statusChange(new state, old state);\n *\n * JobManager States\n *\n * Start state:\n * initial\n *\n * Intermediate states:\n * ready\n *\n * Terminal states:\n * broken - job could not be initialized\n * refuse - for some reason, we have decided that we don't want work for this job\n * stop - job manager has been stopped\n *\n * Valid transitions:\n * initial -> broken\n * initial -> ready\n * ready -> stop\n * \n * NOTE: If you ever use a property with a leading underscore you are probably making a mistake.\n * But if you must, please ask paul, yarn, bryan or eddie for a CR.\n */\nclass JobManager extends EventEmitter\n{\n /**\n * @constructor\n * @param {Supervisor} parent - Owning Supervisor.\n * @param {object} jobMessage - Job Descriptor from getNewJobsForTask.\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n constructor(parent, jobMessage, sliceMessages, authorizationMessage)\n {\n super('JobManager');\n /** @type {Supervisor} */\n this._supervisor = parent;\n /** @type {Slice[]} */\n this._sliceInventory = []; // All slices for this.address.\n /** @type {Sandbox[]} */\n this._sandboxInventory = []; // All sandboxes for this.address.\n /** @type {Synchronizer} */\n this._state = new Synchronizer(INITIAL, [ INITIAL, READY, STOP, REFUSE, BROKEN ]);\n this.state.on('change', (neu, old) => this.emit('statusChange', neu, old));\n\n /** XXXpfr @todo Is there any reason to do a deeper clone here. */\n this.jobMessage = { ...jobMessage };\n\n /** @type {string} */\n this._address = String(this.jobMessage.address);\n /** @type {Load} */\n this._load = null;\n /** @type {Statistics} */\n this._statistics = new Statistics(0.25);\n /** @type {number} */\n this._emaSliceTime = 0;\n /** @type {string[]} */\n this.rejectedJobReasons = [];\n /** @type {boolean} */\n this.isEstimation = true;\n\n const that = this;\n /** \n * Start loading dependencies in the background. Once these are loaded, this.state will \n * transition to 'ready' and the job will be ready for work.\n */\n (async function supervisor$$JobManager$$loadDependencies() {\n await that.fetchJob(that.jobMessage);\n })(/* iife */)\n .then (() => {\n debugging('jobmanager') && console.debug('JobManager is transitioning to READY', this.identifier);\n this.state.set(INITIAL, READY);\n this.addSlices(sliceMessages, authorizationMessage);\n })\n .catch((error) => {\n debuggingError && console.error('fetchJob has failed', error);\n this.state.set(INITIAL, BROKEN);\n })\n .finally(() => {\n selectiveDebugging && console.debug('JobManager.loadDependencies completed.', this.identifier);\n });\n }\n\n /** @type {string} */\n get address () { return this._address; }\n /** @type {string} */\n get uuid () { return this.jobMessage.uuid; }\n /** @type {boolean} */\n get workerConsole () { return this.jobMessage.workerConsole; }\n /** @type {object} */\n get requirements () { return this.jobMessage.requirements; }\n\n // These 3 properties have type object.\n get mro () { return this.jobMessage.mro; }\n get arguments () { return this.jobMessage.arguments; }\n get workFunction () { return this.jobMessage.workFunction; }\n\n /** @type {{computeGroups: Array<{opaqueId: string, name: string, description: string}>, name: string, description: string, link: string}} */\n get public () { return this.jobMessage.public; }\n /** @type {{sliceCPUTime: number, sliceGPUTime: number, sliceCPUDensity: number, sliceGPUDensity: number}} */\n get metrics () { return this.jobMessage.metrics; }\n /** @type {Supervisor} */\n get supervisor () { return this._supervisor; }\n /** @type {Synchronizer} */\n get state () { return this._state; }\n\n /** @type {boolean} */\n get initial () { return this.state.is(INITIAL); }\n /** @type {boolean} */\n get ready () { return this.state.is(READY); }\n /** @type {boolean} */\n get stopped () { return this.state.is(STOP); }\n /** @type {boolean} */\n get refuse () { return this.state.is(REFUSE); }\n /** @type {boolean} */\n get broken () { return this.state.is(BROKEN); }\n\n /** @type {Sandbox[]} */\n get sandboxInventory () { return this._sandboxInventory; }\n /** @type {Sandbox[]} */\n get liveSandboxes () { return this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated); }\n /** @type {Sandbox[]} */\n get assignedSandboxes () { return this.sandboxInventory.filter((sandbox) => sandbox.isAssigned); }\n /** @type {Sandbox[]} */\n get workingSandboxes () { return this.sandboxInventory.filter((sandbox) => sandbox.isWorking); }\n /** @type {Sandbox[]} */\n get nonWorkingSandboxes () { return this.sandboxInventory.filter((sandbox) => !sandbox.isWorking && !sandbox.isTerminated); }\n /** @type {Slice[]} */\n get sliceInventory () { return this._sliceInventory; }\n /** @type {Slice[]} */\n get readySlices () { return this.sliceInventory.filter((slice) => slice.isReady); }\n /** @type {Slice[]} - ready and soon-to-be-ready */\n get queuedSlices () { return this.sliceInventory.filter((slice) => slice.isQueued); }\n /** @type {Slice[]} */\n get reservedSlices () { return this.sliceInventory.filter((slice) => slice.isReserved); }\n /** @type {Slice[]} - unassigned, ready and reserved */\n get activeSlices () { return this.sliceInventory.filter((slice) => slice.isActive); }\n /** @type {Slice[]} - working and soon-to-be-working */\n get workingSlices () { return this.sliceInventory.filter((slice) => slice.isWorking || slice.isReserved); }\n /** @type {Slice[]} - working only */\n get workingSlicesOnly () { return this.sliceInventory.filter((slice) => slice.isWorking); }\n\n /** @type {Load} */\n get load () { return this._load; }\n /** @type {number} */\n get delayMs () { return this.supervisor.delayMs(); }\n /** @type {Statistics} */\n get statistics () { return this._statistics; }\n /** @type {number} */\n get emaSliceTime () { return this._emaSliceTime; }\n /** @type {number} */\n get globalTime () { \n const denominator = this.metrics.sliceCPUDensity + this.metrics.sliceGPUDensity;\n return denominator > 0 ? (this.metrics.sliceCPUTime + this.metrics.sliceGPUTime) / denominator : 0;\n }\n /** @type {string} */\n get identifier () { return `${truncateAddress(this.address, addressTruncationLength)}.${this.state}`; }\n /** @type {string} */\n get [inspect] () { return `[Object JobManager <${this.public.name}::${this.address}::${this.state}>]`; }\n /** \n * Estimate the wall time (i.e. actual elapsed time on a wall clock) for a slice of this job.\n * We pick the ema time (alpha=0.25) + 0.7 * stddev to incorporate uncertainty.\n * About 76% of slice completion times are <= ema + 0.7 * stddev.\n * This estimate is very primitive and assumes that CPU and GPU code do not run at the same\n * time.\n *\n * @type {number} - estimated time period in milliseconds\n */ \n get estimateWallMs () { return this.emaSliceTime + 0.7 * this.statistics.stddev; } /** XXXpfr @todo make this.statistics.stddev reflect this.globalTime too. */\n /** @type {boolean} */\n get debugBuild () { return this.supervisor.debugBuild; }\n /**\n * Always display max info under debug builds, otherwise maximal error\n * messages are displayed to the worker, only if both worker and client agree.\n * @type {boolean} - When true, display stack trace and other enhanced diag info.\n **/\n get displayMaxDiagInfo () { return this.workerConsole && workerTuning.allowConsoleAccess || this.debugBuild; }\n\n // _Idx\n //\n // Statistics: updateStatistics, update.\n //\n\n /** @param {{ total, CPU, webGL }} timeReport */\n updateStatistics (timeReport)\n {\n this.statistics.add(timeReport.total);\n this._emaSliceTime = 0.5 * (this._emaSliceTime + this.statistics.ema); /** XXXpfr @todo double smoothing, need to test and compare against this.statistics.ema . */\n debugging('jobmanager') && console.debug('JobManager.updateStatistics: mean', this.statistics.mean, 'stddev', this.statistics.stddev, 'ema', this.statistics.ema, 'ma', this.statistics.ma, 'x', this.statistics.x);\n }\n\n /**\n * Update jobMessage, add some slices to inventory and possibly update the initial seed of the statistics.\n * @param {object} jobMessage - Job Descriptor from getNewJobsForTask.\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n update (jobMessage, sliceMessages, authorizationMessage)\n {\n this.jobMessage = Object.assign(this.jobMessage, { ...jobMessage });\n this.addSlices(sliceMessages, authorizationMessage);\n if (!this._emaSliceTime || this.statistics.count < 1)\n this._emaSliceTime = this.globalTime;\n // this._load = new Load(this.metrics.sliceCPUTime, this.metrics.sliceGPUTime); // SAVE for Sup2 Part II\n }\n\n // _Idx\n //\n // Dtors: destroy, stopWork.\n //\n\n /**\n * Destructor.\n */\n destroy()\n {\n selectiveDebugging && console.debug(`JobManager.destroy: terminating sandboxes and returning slices to scheduler for job manager ${this.identifier}.`);\n \n this.sandboxInventory.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this._sandboxInventory = [];\n\n const slicesToReturn = this.sliceInventory.filter((slice) => !slice.isFinished);\n const reason = `JobManager destroy: ${this.identifier}`;\n this.supervisor.returnSlices(slicesToReturn, reason, false /*removeSlices*/);\n this._sliceInventory = [];\n }\n\n /**\n * Terminates sandboxes and returns slices.\n * Sets the working flag to false, call @this.work to start working again.\n * \n * If forceTerminate is true: Terminates all sandboxes and returns all slices.\n * If forceTerminate is false: Terminates non-working sandboxes and returns queued slices.\n *\n * @param {boolean} [forceTerminate = true] - true if you want to stop the sandboxes from completing their current slice.\n */\n stopWork (forceTerminate = true)\n {\n selectiveDebugging && console.debug(`JobManager.stopWork(${forceTerminate}): terminating sandboxes and returning slices to scheduler.`);\n if (forceTerminate)\n {\n this.destroy();\n }\n else\n {\n // Return all non-finished slices which are not working.\n const reason = `JobManager stopWork(false): ${this.identifier}`;\n this.supervisor.returnSlices(this.queuedSlices, reason);\n this._sliceInventory = [];\n\n // Terminate all sandboxes that are not working.\n let idleSandboxes = this.sandboxInventory.filter(w => !w.isWorking);\n for (const sandbox of idleSandboxes)\n this.returnSandbox(sandbox);\n this._sandboxInventory = [];\n }\n }\n \n /**\n * Terminate 1 assigned sandbox when there are more than threshold assigned sandboxes.\n * @param {number} [threshold=0] - integer >= 0\n * @returns {boolean}\n */\n pruneAssignedSandbox(threshold = 0)\n {\n const _assignedSandboxes = this.assignedSandboxes;\n if (_assignedSandboxes.length > threshold)\n {\n debugging('supervisor') && console.debug(`JobManager.pruneAssignedSandbox terminate ${_assignedSandboxes[0].identifier}, threshold ${threshold}`);\n _assignedSandboxes[0].markAsUnready(); // Remove from this.assignedSandboxes.\n this.returnSandbox(_assignedSandboxes[0]);\n return true;\n }\n return false;\n }\n\n /**\n * Terminate 1 assigned sandbox when there are more than this.queuedSlices.length assigned sandboxes.\n * @returns {boolean}\n */\n pruneExtraAssignedSandbox()\n {\n return this.pruneAssignedSandbox(this.queuedSlices.length);\n }\n\n // _Idx\n //\n // Work: Assign sandboxes and execute slices.\n //\n // hookUpSandboxListeners handles the sandbox complete event and tries to schedule another slice\n // from the same job on the sandbox. If there aren't any slices from the same job, it tries slices\n // from other job managers, and if there are no other ready slices, it calls Supervisor.requestTask\n // to get more work from TD .\n //\n // Several functions chain and return non-awaited promises.\n // When AWAIT_ALL is true, we await the promises, which makes easier debugging.\n //\n\n /**\n * Hook up JobManager specific Sandbox event listenters.\n * @param {Sandbox} sandbox\n */\n hookUpSandboxListeners(sandbox)\n {\n // Sandbox error handler.\n sandbox.on('sandboxError', (error) => JobManager.handleSandboxError(this, sandbox, error));\n // Sandbox complete handler.\n // When any sandbox completes, try to run another slice in the same jobManager.\n // Otherwise, go through the Supervisor.requestTask protocol.\n sandbox.addListener('complete', () => {\n if (this.supervisor.isReady())\n {\n const _readySlices = this.readySlices;\n if (this.supervisor.runSliceFromSameJob() && _readySlices.length > 0)\n {\n const slice = _readySlices[0];\n slice.markAsReserved();\n this.runSlice(slice, this.delayMs);\n }\n else if (!this.supervisor.isFetchingNewWork)\n {\n const unusedSandboxSlots = this.supervisor.unusedSandboxSlots();\n if (unusedSandboxSlots > 0)\n this.supervisor.requestTask(unusedSandboxSlots);\n }\n }\n });\n }\n\n /**\n * Create a Sandbox.\n * Start it. Assign it. Add it to jobManager inventory.\n * @param {number} delayStartMs - The delay ms to pass to sandbox.start .\n * @param {number} delayAssignMs - The delay ms to pass to sandbox.assign .\n * @returns {Promise<Sandbox>}\n */\n async readySandbox (delayStartMs, delayAssignMs)\n {\n const sandbox = await this.supervisor.createSandbox(delayStartMs);\n this.hookUpSandboxListeners(sandbox);\n return sandbox.assign(this, delayAssignMs)\n .catch((error) => { // error could be a string coming from evaluator\n const errorObj = { jobAddress: truncateAddress(this.address, addressTruncationLength), error: this.checkStackTrace(error) };\n this.supervisor.error(`Failed to assign job to sandbox: ${errorObj}`);\n this.returnSandbox(sandbox);\n throw error;\n });\n }\n\n /**\n * Mark both sandbox and slice as working.\n * @param {Sandbox} sandbox\n * @param {Slice} slice\n */\n markAsWorking(sandbox, slice)\n {\n assert(sandbox);\n assert(slice);\n slice.markAsWorking();\n sandbox.markAsWorking(slice);\n }\n\n /**\n * Create or reuse a sandbox and run a slice on it.\n * @param {Slice} slice - The slice to execute on a sandbox.\n * @param {number} [delayMs=0] - The delay ms to pass to sandbox.start .\n * @returns {Promise<void>}\n */\n async runSlice(slice, delayMs)\n {\n assert(slice);\n if (this.supervisor.sliceTiming) slice['queueingDelta'] = Date.now();\n\n const _assignedSandboxes = this.assignedSandboxes;\n if (AWAIT_ALL)\n {\n if (_assignedSandboxes.length > 0 && this.supervisor.readiedSandboxes.length < 1)\n {\n const sandbox = _assignedSandboxes[0];\n this.markAsWorking(sandbox, slice);\n return await this.runSliceOnSandbox(slice, sandbox, this.delayMs); \n }\n try\n {\n const sandbox = await this.readySandbox(delayMs/*delayStartMs*/, 0.5 * this.delayMs/*delayAssignMs*/);\n this.sandboxInventory.push(sandbox);\n this.markAsWorking(sandbox, slice);\n return await this.runSliceOnSandbox(slice, sandbox, this.delayMs);\n }\n catch (error)\n {\n // Any exception from readySandbox will have already been emitted.\n debugging('jobmanager') && console.error(`JobManager.runSlice: Failure trying to run slice ${slice.identifier} on a sandbox.`, error);\n this.supervisor.returnSlice(slice, 'EUNCAUGHT');\n }\n }\n else\n {\n if (_assignedSandboxes.length > 0 && this.supervisor.readiedSandboxes.length < 1)\n {\n const sandbox = _assignedSandboxes[0];\n this.markAsWorking(sandbox, slice);\n return this.runSliceOnSandbox(slice, sandbox, this.delayMs); \n }\n return this.readySandbox(delayMs/*delayStartMs*/, this.delayMs/*delayAssignMs*/)\n .then((sandbox) => {\n this.sandboxInventory.push(sandbox);\n this.markAsWorking(sandbox, slice);\n return this.runSliceOnSandbox(slice, sandbox, this.delayMs);\n })\n .catch((error) => {\n // Any exception from readySandbox will have already been emitted.\n debugging('jobmanager') && console.error(`JobManager.runSlice: Failure trying to run slice ${slice.identifier} on a sandbox.`, error);\n this.supervisor.returnSlice(slice, 'EUNCAUGHT');\n })\n }\n }\n\n /**\n * Execute slice on sandbox, collect results or handle errors, and clean up.\n * @param {Slice} slice - The slice to execute on the sandbox.\n * @param {Sandbox} sandbox - The sandbox on which to execute the slice.\n * @param {number} [delayMs=0] - The delay ms to pass to sandbox.work .\n * @returns {Promise<void>}\n */\n async runSliceOnSandbox(slice, sandbox, delayMs = 0)\n {\n selectiveDebugging2 && console.debug(`runSliceOnSandbox ${sandbox.identifier}~${slice.identifier} #readySlices ${this.readySlices.length}/${this.supervisor.readySliceCount()}, #SBs, ${this.sandboxInventory.length}/${this.supervisor.sandboxCount()}, #working(w/r/wo/wsbx), ${this.workingSlices.length}/${this.reservedSlices.length}/${this.workingSlicesOnly.length}/${this.workingSandboxes.length}, #sup-working(w/r/wo/wsbx), ${this.supervisor.workingSliceCount()}/${this.supervisor.reservedSliceCount()}/${this.supervisor.workingSliceOnlyCount()}/${this.supervisor.workingSandboxCount()}, #assigned, ${this.assignedSandboxes.length}, #readiedSBs, ${this.supervisor.readiedSandboxes.length}, #localSBs, ${this.sandboxInventory.map(s => Number(s.id)).sort((x,y)=>x-y)}`);\n //console.log('runSliceOnSandbox', Date.now() - this.supervisor.lastTime, slice.identifier); // SAVE temporarily\n assert(slice && sandbox);\n slice.verifyWorking();\n sandbox.verifyWorking();\n\n if (this.supervisor.sliceTiming)\n {\n slice['queueingDelta'] = Date.now() - slice['queueingDelta'];\n slice['executionDelta'] = Date.now();\n }\n slice.startTime = Date.now();\n\n if (AWAIT_ALL)\n {\n try\n {\n const result = await sandbox.work(delayMs);\n debugging('jobmanager') && console.debug(`runSliceOnSandbox - success: ${sandbox.id}~${slice.identifier}`, result);\n slice.collectResult(result, true);\n sandbox.changeWorkingToAssigned();\n this.supervisor.recordResult(slice);\n }\n catch (error)\n {\n const reason = await this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n debuggingError && console.error(`runSliceOnSandbox - failure: ${sandbox.id}~${slice.identifier}`, reason, error);\n }\n finally\n {\n slice.startTime = null;\n if (this.supervisor.sliceTiming)\n {\n slice['executionDelta'] = Date.now() - slice['executionDelta'];\n slice['resultDelta'] = Date.now();\n }\n }\n }\n else\n {\n return sandbox.work(delayMs)\n .then((result) => {\n debugging('jobmanager') && console.debug(`runSliceOnSandbox - success: ${sandbox.id}~${slice.identifier}`, result);\n slice.collectResult(result, true);\n sandbox.changeWorkingToAssigned();\n this.supervisor.recordResult(slice);\n })\n .catch(async (error) => {\n const reason = await this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n debuggingError && console.error(`runSliceOnSandbox - failure: ${sandbox.id}~${slice.identifier}`, reason, error);\n })\n .finally(() => {\n slice.startTime = null;\n if (this.supervisor.sliceTiming)\n {\n slice['executionDelta'] = Date.now() - slice['executionDelta'];\n slice['resultDelta'] = Date.now();\n }\n });\n }\n }\n\n /**\n * Sandbox has had an error which is not from the work function: terminate it\n * and try to redo the slice.\n * @param {JobManager} jobManager \n * @param {Sandbox} sandbox \n * @param {Error} error \n */\n static handleSandboxError (jobManager, sandbox, error)\n {\n const slice = sandbox.slice;\n if (slice)\n {\n if (!slice.isWorking) // Sanity. Exception should never fire.\n throw new Error(`handleSandboxError: slice ${slice.identifier} must be WORKING.`);\n\n slice['sandboxErrorCount'] = (slice['sandboxErrorCount'] || 0) + 1;\n sandbox.slice = null;\n if (slice['sandboxErrorCount'] < (workerTuning.maxSandboxErrorsPerSlice || 2))\n slice.resetState();\n else\n {\n slice.collectResult(error, false /*success*/);\n const reason = `sandboxError event handler, too many errors ${slice['sandboxErrorCount']}: ${sandbox.id}~${jobManager.identifier}`;\n jobManager.supervisor.returnSlice(slice, reason);\n }\n jobManager.supervisor.error(`JobManager.handleSandboxError: Sandbox ${sandbox.identifier}...(${sandbox.public.name}/${slice['sandboxErrorCount']}) with slice ${slice.identifier} had error.`, error);\n }\n else\n jobManager.supervisor.error(`JobManager.handleSandboxError: Sandbox ${sandbox.identifier} has no slice...(${sandbox.public.name} had error.`, error);\n\n jobManager.returnSandbox(sandbox); /* terminate the sandbox */\n }\n\n // _Idx\n //\n // Remove from array and return sandbox.\n //\n\n /**\n * Remove sandbox from this.sandboxInventory and terminate is not already terminated.\n * @param {Sandbox} sandbox - The sandbox to remove from this.sandboxInventory.\n * @param {boolean} [assertExists=true] - When true (and type=array) then assert that sandbox exists in this.sandboxInventory.\n */\n returnSandbox(sandbox, assertExists = true)\n {\n debugging('supervisor') && console.debug(`JobManager.returnSandbox: ${sandbox.identifier}, assertExists ${assertExists}`);\n if (this.sandboxInventory && this.sandboxInventory.length > 0)\n JobManager.removeElement(this.sandboxInventory, sandbox, assertExists);\n // The cleaning up of terminated sandboxes in this.supervisor.sandboxInventory happens in this.supervisor.purgeSandboxes.\n if (!sandbox.isTerminated)\n sandbox.terminate(false);\n }\n /**\n * Remove slice from this.sliceInventory.\n * @param {Slice} slice - The slice to remove from this.sliceInventory.\n * @param {boolean} [assertExists=true] - When true (and type=array) then assert that slice exists in this.sliceInventory.\n */\n removeSlice(slice, assertExists = true)\n {\n assert(this.sliceInventory instanceof Array);\n if (this.sliceInventory && this.sliceInventory.length > 0)\n JobManager.removeElement(this.sliceInventory, slice, assertExists);\n }\n\n /**\n * Remove slices from this.sliceInventory.\n * @param {Slice[]} slices - The slices to remove from this.sliceInventory.\n */\n removeSlices(slices)\n {\n assert(this.sliceInventory instanceof Array);\n if (this.sliceInventory && this.sliceInventory.length > 0)\n this._sliceInventory = this.sliceInventory.filter((slice) => slices.indexOf(slice) === -1);\n }\n\n /**\n * Remove element from arrayLike.\n * @param {Array<*>} array\n * @param {object|number} element\n * @param {boolean} [assertExists=true]\n */\n static removeElement(array, element, assertExists = true)\n {\n let index = array.indexOf(element);\n if (assertExists)\n {\n assert(index !== -1);\n array.splice(index, 1);\n } else if (index !== -1) array.splice(index, 1);\n }\n\n // _Idx\n //\n // Fetch: WorkFn, slices, arguments.\n //\n\n /**\n * Fetch work function, work function arguments and possibly the range object describing the jobs slices..\n * @param {object} mpe - messagePayloadElement: job object returned by task-jobs.js . \n */\n async fetchJob(mpe)\n {\n if (debugging('worker'))\n dumpObject(mpe, 'JobManager.fetchJob: mpe', 512);\n\n if (AWAIT_ALL)\n {\n if (!mpe.workFunction)\n {\n mpe.workFunction = await fetchURI(mpe.codeLocation, this.supervisor.makeSafeOriginList('fetchWorkFunctions'));\n if(mpe.requirements.useStrict)\n mpe.useStrict = true;\n delete mpe.codeLocation;\n }\n if (!mpe.arguments)\n {\n let promises = [];\n let uris = mpe.argumentsLocation;\n if (uris)\n for (let i = 0; i < uris.length; i++)\n promises.push(fetchURI(uris[i].value, this.supervisor.makeSafeOriginList('fetchArguments')));\n\n mpe.arguments = await Promise.all(promises);\n\n // node localExec jobs read arguments from a file, so need to ensure they are properly parsed after being read.\n if (this.supervisor.options.localExec && !DCP_ENV.isBrowserPlatform)\n mpe.arguments = scopedKvin.parse(mpe.arguments[0]);\n\n delete mpe.argumentsLocation;\n }\n // if job input data is range object, we send the range object URI to the worker\n if (!mpe.mro && mpe.MROLocation)\n {\n mpe.mro = await fetchURI(mpe.MROLocation, this.supervisor.makeSafeOriginList('fetchData'));\n delete mpe.MROLocation;\n }\n }\n else\n {\n let promises = [];\n\n // Get workFn.\n if (!mpe.workFunction)\n {\n const workFunctionPromise = fetchURI(mpe.codeLocation, this.supervisor.makeSafeOriginList('fetchWorkFunctions'))\n .then((workFunction) => {\n mpe.workFunction = workFunction;\n if (mpe.requirements.useStrict)\n mpe.useStrict = true;\n delete mpe.codeLocation;\n });\n promises.push(workFunctionPromise);\n }\n\n // if job input data is range object, we send the range object URI to the worker\n if (!mpe.mro && mpe.MROLocation)\n {\n // Do we need this? SAVE.\n // if (this.supervisor.options.localExec && !DCP_ENV.isBrowserPlatform)\n // mpe.MROLocation = await fetchURI(mpe.MROLocation, this.supervisor.allowedOrigins, this.supervisor.fetchData);\n\n const mroPromise = fetchURI(mpe.MROLocation, this.supervisor.makeSafeOriginList('fetchData'))\n .then((mro) => {\n mpe.mro = mro;\n delete mpe.MROLocation;\n });\n promises.push(mroPromise)\n }\n\n // Get workFn args.\n if (!mpe.arguments && mpe.argumentsLocation)\n {\n mpe.arguments = new Array(mpe.argumentsLocation.length);\n for (let k = 0; k < mpe.argumentsLocation.length; k++)\n promises.push(fetchURI(mpe.argumentsLocation[k].value, this.supervisor.makeSafeOriginList('fetchArguments'))\n .then((arg) => (mpe.arguments[k] = arg) ));\n }\n\n await Promise.all(promises);\n\n if (mpe.argumentsLocation) delete mpe.argumentsLocation;\n }\n\n return mpe;\n }\n\n /**\n * Look up slice.datumUri or use the range object this.mro, constructed by fetchJob.\n * @param {Slice} slice \n * @returns {Promise<{ inputDatum, dataError }>}\n */\n async fetchSliceData (datumUri, slice) // eslint-disable-line require-await\n {\n function catchHandler(error)\n {\n const dataError = error;\n dataError['errorCode'] = (error.code === 'EFETCH') ? 'EFETCH' : 'EUNCAUGHTERROR';\n return dataError;\n }\n\n if (!datumUri && !this.state.is(READY))\n throw new Error(`When !datumUri, JobManager '${this.identifier}' must be in READY state.`);\n\n try\n {\n if (!datumUri)\n {\n if (!this.mro) throw new Error('Must complete call to JobManager.fetchJob before calling JobManager.fetchSliceData.');\n const ro = rehydrateRange(this.mro);\n // Slice numbers start at 1.\n const inputDatum = ro[slice.sliceNumber - 1];\n debugging('jobmanager') && console.debug(`Fetched mro datum: ${stringify(inputDatum, 512)}`);\n return { inputDatum, dataError: null };\n }\n\n return fetchURI(datumUri, this.supervisor.makeSafeOriginList('fetchData'))\n .then((inputDatum) => {\n debugging('jobmanager') && console.debug(`Fetched datum: ${stringify(inputDatum, 512)}`);\n return { inputDatum, dataError: null };\n })\n .catch((error) => {\n const dataError = catchHandler(error);\n return { inputDatum: null, dataError };\n });\n }\n catch (error)\n {\n const dataError = catchHandler(error);\n return { inputDatum: null, dataError };\n }\n }\n\n // _Idx\n //\n // Miscellaneous.\n //\n\n /**\n * Debugging helper.\n * @param {boolean} [details=true]\n */\n dumpSlices(details = true)\n {\n if (this.sliceInventory.length < 1) return;\n const queued = [], working = [], complete = [], failed = [], finished = [], extras = [];\n for (const slice of this.sliceInventory)\n {\n if (slice.isUnassigned || slice.isReady)\n queued.push(slice);\n else if (slice.isReserved || slice.isWorking)\n working.push(slice);\n else if (slice.isComplete)\n complete.push(slice);\n else if (slice.hasFailed)\n failed.push(slice);\n else if (slice.isFinished)\n finished.push(slice);\n else\n extras.push(slice);\n }\n console.log(`:--JobManager.dumpSlices--${this.identifier} slices ${this.sliceInventory.length}-------`);\n console.log(`-----queued(${queued.length})-----------------------------------------------------------------`);\n if (details)\n {\n for (const slice of queued)\n console.log(slice.identifier);\n console.log(`-----working(${working.length})----------------------------------------------------------------`);\n for (const slice of working)\n console.log(slice.identifier);\n console.log(`-----complete(${complete.length})----------------------------------------------------------------`);\n for (const slice of complete)\n console.log(slice.identifier);\n console.log(`-----failed(${failed.length})----------------------------------------------------------------`);\n for (const slice of failed)\n console.log(slice.identifier);\n console.log(`-----finished(${finished.length})---------------------------------------------------------------`);\n for (const slice of finished)\n console.log(slice.identifier);\n console.log(`-----extras(${extras.length})---------------------------------------------------------------`);\n for (const slice of extras)\n console.log(slice.identifier);\n }\n else\n {\n console.log(`-----working(${working.length})----------------------------------------------------------------`);\n console.log(`-----complete(${complete.length})----------------------------------------------------------------`);\n console.log(`-----failed(${failed.length})----------------------------------------------------------------`);\n console.log(`-----finished(${finished.length})---------------------------------------------------------------`);\n console.log(`-----extras(${extras.length})---------------------------------------------------------------`);\n }\n console.log('-----------------------------------------------------------------------------------');\n }\n\n /**\n * Calls removeStackTrace when !this.displayMaxDiagInfo .\n * @param {string|Error} error\n * @returns {string|Error}\n */\n checkStackTrace(error)\n {\n return this.displayMaxDiagInfo ? error : this.supervisor.removeStackTrace(error);\n }\n\n /**\n * Add slices to the job manager's inventory.\n *\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n addSlices (sliceMessages, authorizationMessage)\n {\n sliceMessages.forEach((sliceMessage) => {\n const slice = new Slice(this, sliceMessage, authorizationMessage);\n if (!slice.isEstimation) this.isEstimation = false;\n this.sliceInventory.push(slice);\n });\n }\n\n /**\n * Schedule the slice to be executed.\n * @param {Slice} slice\n * @param {boolean} [placeInTheFrontOfTheQueue=false]\n */\n scheduleSlice(slice, placeInTheFrontOfTheQueue = false)\n {\n // Reset slice state to allow execution.\n slice.resetState();\n // Enqueue in the to-be-executed queue.\n if (placeInTheFrontOfTheQueue) this.sliceInventory.unshift(slice);\n else this.sliceInventory.push(slice);\n }\n\n /**\n * XXXpfr @todo UNUSED but may be used soon\n * Returns a counterfeit JobManager - a generic object with most of the same\n * ownProperties as a real exports.JobManager. Any inventories and synchronizer are\n * duplicated (equivalent state, no events copied, ~ shallow clone)\n * @returns {JobManager}\n */\n counterfeit()\n {\n /** @type {JobManager} */\n const fake = {};\n \n for (let prop in Object.keys(this))\n {\n if (this[prop] instanceof Inventory)\n fake[prop] = this[prop].duplicate();\n else if (this[prop] instanceof Synchronizer)\n fake[prop] = this[prop].valueOf();\n else\n fake[prop] = this[prop];\n }\n\n return fake;\n }\n\n}\nexports.JobManager = JobManager;\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/job-manager.js?");
4585
+ eval("/**\n * @file dcp-client/worker/supervisor2/job-manager.js\n *\n * A support class for Supervisor2.\n * It is a wrapper for the job object returned from the requestTask,\n * along with tracking slices and sandboxes associated with the job. \n *\n * @author Wes Garland, wes@kingsds.network,\n * Paul, paul@kingsds.network,\n * @date Dec 2020,\n * June 2022,\n * @module JobManager\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\n/** @typedef {import('./').Supervisor} Supervisor */\n/** @typedef {import('./sandbox2').Sandbox} Sandbox */\n/** @typedef {import('dcp/common/dcp-url').DcpURL} DcpURL */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n/** @typedef {string} opaqueId */ // 22 character base64 string \n/** @typedef {string} address */ // String(Address)\n\nconst inspect = Symbol.for('nodejs.util.inspect.custom');\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst { Inventory } = __webpack_require__(/*! dcp/utils/inventory */ \"./src/utils/inventory.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { Slice } = __webpack_require__(/*! ./slice2 */ \"./src/dcp-client/worker/supervisor2/slice2.js\");\nconst { Load } = __webpack_require__(/*! ./load */ \"./src/dcp-client/worker/supervisor2/load.js\");\nconst { Statistics } = __webpack_require__(/*! ./rolling-statistics */ \"./src/dcp-client/worker/supervisor2/rolling-statistics.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { rehydrateRange } = __webpack_require__(/*! dcp/dcp-client/range-object */ \"./src/dcp-client/range-object.js\");\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nconst scopedKvin = new kvin.KVIN({Object: ({}).constructor,\n Array: ([]).constructor, \n Function: (()=>{}).constructor});\nconst { fetchURI, dumpObject, truncateAddress, stringify } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\nconst addressTruncationLength = 20;\nlet workerTuning = dcpConfig.worker;\nif (!workerTuning) workerTuning = dcpConfig.Supervisor;\nif (!workerTuning || !workerTuning.maxSandboxErrorsPerSlice || !workerTuning.allowConsoleAccess)\n workerTuning = { maxSandboxErrorsPerSlice: 2, allowConsoleAccess: false };\n\n// This is for debugging and performance testing.\n// Several functions chain and return non-awaited promises.\n// When AWAIT_ALL is true, we await the promises, which makes easier debugging.\nconst AWAIT_ALL = false;\n\n// Debug tracing helpers.\nconst selectiveEnable = false;\nconst displayWarnError = false || selectiveEnable;\nconst selectiveDebugging = selectiveEnable || debugging();\nconst debuggingError = false || selectiveDebugging || displayWarnError;\nconst debuggingWarn = false || selectiveDebugging || displayWarnError;\nconst selectiveDebugging2 = selectiveEnable && false || debugging('jobmanager');\n\nconst INITIAL = 'initial';\nconst READY = 'ready';\nconst STOP = 'stop';\nconst REFUSE = 'refuse';\nconst BROKEN = 'broken';\n\n//\n// Index to functionality -- search for '_Idx' to toggle through the index.\n//\n// 1) Ctor: JobManager class definition and various properties.\n// 2) Statistics: updateStatistics, update.\n// 3) Dtors: destroy, stopWork.\n// 4) Assign sandboxes and execute slices.\n// 5) Remove from array and return sandbox.\n// 6) Fetch: WorkFn, slices, arguments.\n// 7) Miscellaneous.\n// 8) Unused functions that we need to review.\n//\n\n// _Idx\n//\n// Ctor: JobManager class definition and various properties.\n// A JobManager handles all scheduling of slices for a given job.\n// It's also responsible for fetching slice data, work functions and arguments.\n// And it collects statistics about slice completion times and resource usage.\n// All functionality across jobs is handled by Supervisor.\n//\n\n/**\n * JobManager Constructor. An instance of JobManager knows everything about a given\n * job within the supervisor, including:\n * - work function code\n * - work function arguments\n * - all the SliceManager instances that go with this job\n * - how long a slice of this job is expected to run\n *\n * Instances of JobManager emit the following events:\n * - addSlice(sliceHandle)\n * - deleteSlice(sliceHandle)\n * - statusChange(new state, old state);\n *\n * JobManager States\n *\n * Start state:\n * initial\n *\n * Intermediate states:\n * ready\n *\n * Terminal states:\n * broken - job could not be initialized\n * refuse - for some reason, we have decided that we don't want work for this job\n * stop - job manager has been stopped\n *\n * Valid transitions:\n * initial -> broken\n * initial -> ready\n * ready -> stop\n * \n * NOTE: If you ever use a property with a leading underscore you are probably making a mistake.\n * But if you must, please ask paul, yarn, bryan or eddie for a CR.\n */\nclass JobManager extends EventEmitter\n{\n /**\n * @constructor\n * @param {Supervisor} parent - Owning Supervisor.\n * @param {object} jobMessage - Job Descriptor from getNewJobsForTask.\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n constructor(parent, jobMessage, sliceMessages, authorizationMessage)\n {\n super('JobManager');\n /** @type {Supervisor} */\n this._supervisor = parent;\n /** @type {Slice[]} */\n this._sliceInventory = []; // All slices for this.address.\n /** @type {Sandbox[]} */\n this._sandboxInventory = []; // All sandboxes for this.address.\n /** @type {Synchronizer} */\n this._state = new Synchronizer(INITIAL, [ INITIAL, READY, STOP, REFUSE, BROKEN ]);\n this.state.on('change', (neu, old) => this.emit('statusChange', neu, old));\n\n /** XXXpfr @todo Is there any reason to do a deeper clone here. */\n this.jobMessage = { ...jobMessage };\n\n /** @type {string} */\n this._address = String(this.jobMessage.address);\n /** @type {Load} */\n this._load = null;\n /** @type {Statistics} */\n this._statistics = new Statistics(0.25);\n /** @type {number} */\n this._emaSliceTime = 0;\n /** @type {string[]} */\n this.rejectedJobReasons = [];\n /** @type {boolean} */\n this.isEstimation = true;\n\n const that = this;\n /** \n * Start loading dependencies in the background. Once these are loaded, this.state will \n * transition to 'ready' and the job will be ready for work.\n */\n (async function supervisor$$JobManager$$loadDependencies() {\n await that.fetchJob(that.jobMessage);\n })(/* iife */)\n .then (() => {\n debugging('jobmanager') && console.debug('JobManager is transitioning to READY', this.identifier);\n this.state.set(INITIAL, READY);\n this.addSlices(sliceMessages, authorizationMessage);\n })\n .catch((error) => {\n debuggingError && console.error('fetchJob has failed', error);\n this.state.set(INITIAL, BROKEN);\n })\n .finally(() => {\n selectiveDebugging && console.debug('JobManager.loadDependencies completed.', this.identifier);\n });\n }\n\n /** @type {string} */\n get address () { return this._address; }\n /** @type {string} */\n get uuid () { return this.jobMessage.uuid; }\n /** @type {boolean} */\n get workerConsole () { return this.jobMessage.workerConsole; }\n /** @type {object} */\n get requirements () { return this.jobMessage.requirements; }\n\n // These 3 properties have type object.\n get mro () { return this.jobMessage.mro; }\n get arguments () { return this.jobMessage.arguments; }\n get workFunction () { return this.jobMessage.workFunction; }\n\n /** @type {{computeGroups: Array<{opaqueId: string, name: string, description: string}>, name: string, description: string, link: string}} */\n get public () { return this.jobMessage.public; }\n /** @type {{sliceCPUTime: number, sliceGPUTime: number, sliceCPUDensity: number, sliceGPUDensity: number}} */\n get metrics () { return this.jobMessage.metrics; }\n /** @type {Supervisor} */\n get supervisor () { return this._supervisor; }\n /** @type {Synchronizer} */\n get state () { return this._state; }\n\n /** @type {boolean} */\n get initial () { return this.state.is(INITIAL); }\n /** @type {boolean} */\n get ready () { return this.state.is(READY); }\n /** @type {boolean} */\n get stopped () { return this.state.is(STOP); }\n /** @type {boolean} */\n get refuse () { return this.state.is(REFUSE); }\n /** @type {boolean} */\n get broken () { return this.state.is(BROKEN); }\n\n /** @type {Sandbox[]} */\n get sandboxInventory () { return this._sandboxInventory; }\n /** @type {Sandbox[]} */\n get liveSandboxes () { return this.sandboxInventory.filter((sandbox) => !sandbox.isTerminated); }\n /** @type {Sandbox[]} */\n get assignedSandboxes () { return this.sandboxInventory.filter((sandbox) => sandbox.isAssigned); }\n /** @type {Sandbox[]} */\n get workingSandboxes () { return this.sandboxInventory.filter((sandbox) => sandbox.isWorking); }\n /** @type {Sandbox[]} */\n get nonWorkingSandboxes () { return this.sandboxInventory.filter((sandbox) => !sandbox.isWorking && !sandbox.isTerminated); }\n /** @type {Slice[]} */\n get sliceInventory () { return this._sliceInventory; }\n /** @type {Slice[]} */\n get readySlices () { return this.sliceInventory.filter((slice) => slice.isReady); }\n /** @type {Slice[]} - ready and soon-to-be-ready */\n get queuedSlices () { return this.sliceInventory.filter((slice) => slice.isQueued); }\n /** @type {Slice[]} */\n get reservedSlices () { return this.sliceInventory.filter((slice) => slice.isReserved); }\n /** @type {Slice[]} - unassigned, ready and reserved */\n get activeSlices () { return this.sliceInventory.filter((slice) => slice.isActive); }\n /** @type {Slice[]} - working and soon-to-be-working */\n get workingSlices () { return this.sliceInventory.filter((slice) => slice.isWorking || slice.isReserved); }\n /** @type {Slice[]} - working only */\n get workingSlicesOnly () { return this.sliceInventory.filter((slice) => slice.isWorking); }\n\n /** @type {Load} */\n get load () { return this._load; }\n /** @type {number} */\n get delayMs () { return this.supervisor.delayMs(); }\n /** @type {Statistics} */\n get statistics () { return this._statistics; }\n /** @type {number} */\n get emaSliceTime () { return this._emaSliceTime; }\n /** @type {number} */\n get globalTime () { \n const denominator = this.metrics.sliceCPUDensity + this.metrics.sliceGPUDensity;\n return denominator > 0 ? (this.metrics.sliceCPUTime + this.metrics.sliceGPUTime) / denominator : 0;\n }\n /** @type {string} */\n get identifier () { return `${truncateAddress(this.address, addressTruncationLength)}.${this.state}`; }\n /** @type {string} */\n get [inspect] () { return `[Object JobManager <${this.public.name}::${this.address}::${this.state}>]`; }\n /** \n * Estimate the wall time (i.e. actual elapsed time on a wall clock) for a slice of this job.\n * We pick the ema time (alpha=0.25) + 0.7 * stddev to incorporate uncertainty.\n * About 76% of slice completion times are <= ema + 0.7 * stddev.\n * This estimate is very primitive and assumes that CPU and GPU code do not run at the same\n * time.\n *\n * @type {number} - estimated time period in milliseconds\n */ \n get estimateWallMs () { return this.emaSliceTime + 0.7 * this.statistics.stddev; } /** XXXpfr @todo make this.statistics.stddev reflect this.globalTime too. */\n /** @type {boolean} */\n get debugBuild () { return this.supervisor.debugBuild; }\n /**\n * Always display max info under debug builds, otherwise maximal error\n * messages are displayed to the worker, only if both worker and client agree.\n * @type {boolean} - When true, display stack trace and other enhanced diag info.\n **/\n get displayMaxDiagInfo () { return this.workerConsole && workerTuning.allowConsoleAccess || this.debugBuild; }\n\n // _Idx\n //\n // Statistics: updateStatistics, update.\n //\n\n /** @param {{ total, CPU, webGL }} timeReport */\n updateStatistics (timeReport)\n {\n this.statistics.add(timeReport.total);\n this._emaSliceTime = 0.5 * (this._emaSliceTime + this.statistics.ema); /** XXXpfr @todo double smoothing, need to test and compare against this.statistics.ema . */\n debugging('jobmanager') && console.debug('JobManager.updateStatistics: mean', this.statistics.mean, 'stddev', this.statistics.stddev, 'ema', this.statistics.ema, 'ma', this.statistics.ma, 'x', this.statistics.x);\n }\n\n /**\n * Update jobMessage, add some slices to inventory and possibly update the initial seed of the statistics.\n * @param {object} jobMessage - Job Descriptor from getNewJobsForTask.\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n update (jobMessage, sliceMessages, authorizationMessage)\n {\n this.jobMessage = Object.assign(this.jobMessage, { ...jobMessage });\n this.addSlices(sliceMessages, authorizationMessage);\n if (!this._emaSliceTime || this.statistics.count < 1)\n this._emaSliceTime = this.globalTime;\n // this._load = new Load(this.metrics.sliceCPUTime, this.metrics.sliceGPUTime); // SAVE for Sup2 Part II\n }\n\n // _Idx\n //\n // Dtors: destroy, stopWork.\n //\n\n /**\n * Destructor.\n */\n destroy()\n {\n selectiveDebugging && console.debug(`JobManager.destroy: terminating sandboxes and returning slices to scheduler for job manager ${this.identifier}.`);\n \n this.sandboxInventory.forEach((sandbox) => {\n if (!sandbox.isTerminated) sandbox.terminate(false);\n });\n this._sandboxInventory = [];\n\n const slicesToReturn = this.sliceInventory.filter((slice) => !slice.isFinished);\n const reason = `JobManager destroy: ${this.identifier}`;\n this.supervisor.returnSlices(slicesToReturn, reason, false /*removeSlices*/);\n this._sliceInventory = [];\n }\n\n /**\n * Terminates sandboxes and returns slices.\n * Sets the working flag to false, call @this.work to start working again.\n * \n * If forceTerminate is true: Terminates all sandboxes and returns all slices.\n * If forceTerminate is false: Terminates non-working sandboxes and returns queued slices.\n *\n * @param {boolean} [forceTerminate = true] - true if you want to stop the sandboxes from completing their current slice.\n */\n stopWork (forceTerminate = true)\n {\n selectiveDebugging && console.debug(`JobManager.stopWork(${forceTerminate}): terminating sandboxes and returning slices to scheduler.`);\n if (forceTerminate)\n {\n this.destroy();\n }\n else\n {\n // Return all non-finished slices which are not working.\n const reason = `JobManager stopWork(false): ${this.identifier}`;\n this.supervisor.returnSlices(this.queuedSlices, reason);\n this._sliceInventory = [];\n\n // Terminate all sandboxes that are not working.\n let idleSandboxes = this.sandboxInventory.filter(w => !w.isWorking);\n for (const sandbox of idleSandboxes)\n this.returnSandbox(sandbox);\n this._sandboxInventory = [];\n }\n }\n \n /**\n * Terminate 1 assigned sandbox when there are more than threshold assigned sandboxes.\n * @param {number} [threshold=0] - integer >= 0\n * @returns {boolean}\n */\n pruneAssignedSandbox(threshold = 0)\n {\n const _assignedSandboxes = this.assignedSandboxes;\n if (_assignedSandboxes.length > threshold)\n {\n debugging('supervisor') && console.debug(`JobManager.pruneAssignedSandbox terminate ${_assignedSandboxes[0].identifier}, threshold ${threshold}`);\n _assignedSandboxes[0].markAsUnready(); // Remove from this.assignedSandboxes.\n this.returnSandbox(_assignedSandboxes[0]);\n return true;\n }\n return false;\n }\n\n /**\n * Terminate 1 assigned sandbox when there are more than this.queuedSlices.length assigned sandboxes.\n * @returns {boolean}\n */\n pruneExtraAssignedSandbox()\n {\n return this.pruneAssignedSandbox(this.queuedSlices.length);\n }\n\n // _Idx\n //\n // Work: Assign sandboxes and execute slices.\n //\n // hookUpSandboxListeners handles the sandbox complete event and tries to schedule another slice\n // from the same job on the sandbox. If there aren't any slices from the same job, it tries slices\n // from other job managers, and if there are no other ready slices, it calls Supervisor.requestTask\n // to get more work from TD .\n //\n // Several functions chain and return non-awaited promises.\n // When AWAIT_ALL is true, we await the promises, which makes easier debugging.\n //\n\n /**\n * Hook up JobManager specific Sandbox event listenters.\n * @param {Sandbox} sandbox\n */\n hookUpSandboxListeners(sandbox)\n {\n // Sandbox error handler.\n sandbox.on('sandboxError', (error) => JobManager.handleSandboxError(this, sandbox, error));\n // Sandbox complete handler.\n // When any sandbox completes, try to run another slice in the same jobManager.\n // Otherwise, go through the Supervisor.requestTask protocol.\n sandbox.addListener('complete', () => {\n if (this.supervisor.isReady())\n {\n const _readySlices = this.readySlices;\n if (this.supervisor.runSliceFromSameJob() && _readySlices.length > 0)\n {\n const slice = _readySlices[0];\n slice.markAsReserved();\n this.runSlice(slice, this.delayMs);\n }\n else if (!this.supervisor.isFetchingNewWork)\n {\n const unusedSandboxSlots = this.supervisor.unusedSandboxSlots();\n if (unusedSandboxSlots > 0)\n this.supervisor.requestTask(unusedSandboxSlots);\n }\n }\n });\n }\n\n /**\n * Create a Sandbox.\n * Start it. Assign it. Add it to jobManager inventory.\n * @param {number} delayStartMs - The delay ms to pass to sandbox.start .\n * @param {number} delayAssignMs - The delay ms to pass to sandbox.assign .\n * @returns {Promise<Sandbox>}\n */\n async readySandbox (delayStartMs, delayAssignMs)\n {\n const sandbox = await this.supervisor.createSandbox(delayStartMs);\n this.hookUpSandboxListeners(sandbox);\n return sandbox.assign(this, delayAssignMs)\n .catch((error) => { // error could be a string coming from evaluator\n this.supervisor.error(`Failed to assign job ${truncateAddress(this.address, addressTruncationLength)} to sandbox ${sandbox.id}`, error);\n this.returnSandbox(sandbox);\n throw error;\n });\n }\n\n /**\n * Mark both sandbox and slice as working.\n * @param {Sandbox} sandbox\n * @param {Slice} slice\n */\n markAsWorking(sandbox, slice)\n {\n assert(sandbox);\n assert(slice);\n slice.markAsWorking();\n sandbox.markAsWorking(slice);\n }\n\n /**\n * Create or reuse a sandbox and run a slice on it.\n * @param {Slice} slice - The slice to execute on a sandbox.\n * @param {number} [delayMs=0] - The delay ms to pass to sandbox.start .\n * @returns {Promise<void>}\n */\n async runSlice(slice, delayMs)\n {\n assert(slice);\n if (this.supervisor.sliceTiming) slice['queueingDelta'] = Date.now();\n\n const _assignedSandboxes = this.assignedSandboxes;\n if (AWAIT_ALL)\n {\n if (_assignedSandboxes.length > 0 && this.supervisor.readiedSandboxes.length < 1)\n {\n const sandbox = _assignedSandboxes[0];\n this.markAsWorking(sandbox, slice);\n return await this.runSliceOnSandbox(slice, sandbox, this.delayMs); \n }\n try\n {\n const sandbox = await this.readySandbox(delayMs/*delayStartMs*/, 0.5 * this.delayMs/*delayAssignMs*/);\n this.sandboxInventory.push(sandbox);\n this.markAsWorking(sandbox, slice);\n return await this.runSliceOnSandbox(slice, sandbox, this.delayMs);\n }\n catch (error)\n {\n // Any exception from readySandbox will have already been emitted.\n debugging('jobmanager') && console.error(`JobManager.runSlice: Failure trying to run slice ${slice.identifier} on a sandbox.`, error);\n this.supervisor.returnSlice(slice, 'EUNCAUGHT');\n }\n }\n else\n {\n if (_assignedSandboxes.length > 0 && this.supervisor.readiedSandboxes.length < 1)\n {\n const sandbox = _assignedSandboxes[0];\n this.markAsWorking(sandbox, slice);\n return this.runSliceOnSandbox(slice, sandbox, this.delayMs); \n }\n return this.readySandbox(delayMs/*delayStartMs*/, this.delayMs/*delayAssignMs*/)\n .then((sandbox) => {\n this.sandboxInventory.push(sandbox);\n this.markAsWorking(sandbox, slice);\n return this.runSliceOnSandbox(slice, sandbox, this.delayMs);\n })\n .catch((error) => {\n // Any exception from readySandbox will have already been emitted.\n debugging('jobmanager') && console.error(`JobManager.runSlice: Failure trying to run slice ${slice.identifier} on a sandbox.`, error);\n this.supervisor.returnSlice(slice, 'EUNCAUGHT');\n })\n }\n }\n\n /**\n * Execute slice on sandbox, collect results or handle errors, and clean up.\n * @param {Slice} slice - The slice to execute on the sandbox.\n * @param {Sandbox} sandbox - The sandbox on which to execute the slice.\n * @param {number} [delayMs=0] - The delay ms to pass to sandbox.work .\n * @returns {Promise<void>}\n */\n async runSliceOnSandbox(slice, sandbox, delayMs = 0)\n {\n selectiveDebugging2 && console.debug(`runSliceOnSandbox ${sandbox.identifier}~${slice.identifier} #readySlices ${this.readySlices.length}/${this.supervisor.readySliceCount()}, #SBs, ${this.sandboxInventory.length}/${this.supervisor.sandboxCount()}, #working(w/r/wo/wsbx), ${this.workingSlices.length}/${this.reservedSlices.length}/${this.workingSlicesOnly.length}/${this.workingSandboxes.length}, #sup-working(w/r/wo/wsbx), ${this.supervisor.workingSliceCount()}/${this.supervisor.reservedSliceCount()}/${this.supervisor.workingSliceOnlyCount()}/${this.supervisor.workingSandboxCount()}, #assigned, ${this.assignedSandboxes.length}, #readiedSBs, ${this.supervisor.readiedSandboxes.length}, #localSBs, ${this.sandboxInventory.map(s => Number(s.id)).sort((x,y)=>x-y)}`);\n //console.log('runSliceOnSandbox', Date.now() - this.supervisor.lastTime, slice.identifier); // SAVE temporarily\n assert(slice && sandbox);\n slice.verifyWorking();\n sandbox.verifyWorking();\n\n if (this.supervisor.sliceTiming)\n {\n slice['queueingDelta'] = Date.now() - slice['queueingDelta'];\n slice['executionDelta'] = Date.now();\n }\n slice.startTime = Date.now();\n\n if (AWAIT_ALL)\n {\n try\n {\n const result = await sandbox.work(delayMs);\n debugging('jobmanager') && console.debug(`runSliceOnSandbox - success: ${sandbox.id}~${slice.identifier}`, result);\n slice.collectResult(result, true);\n sandbox.changeWorkingToAssigned();\n this.supervisor.recordResult(slice);\n }\n catch (error)\n {\n const reason = await this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n debuggingError && console.error(`runSliceOnSandbox - failure: ${sandbox.id}~${slice.identifier}`, reason, error);\n }\n finally\n {\n slice.startTime = null;\n if (this.supervisor.sliceTiming)\n {\n slice['executionDelta'] = Date.now() - slice['executionDelta'];\n slice['resultDelta'] = Date.now();\n }\n }\n }\n else\n {\n return sandbox.work(delayMs)\n .then((result) => {\n debugging('jobmanager') && console.debug(`runSliceOnSandbox - success: ${sandbox.id}~${slice.identifier}`, result);\n slice.collectResult(result, true);\n sandbox.changeWorkingToAssigned();\n this.supervisor.recordResult(slice);\n })\n .catch(async (error) => {\n const reason = await this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n debuggingError && console.error(`runSliceOnSandbox - failure: ${sandbox.id}~${slice.identifier}`, reason, error);\n })\n .finally(() => {\n slice.startTime = null;\n if (this.supervisor.sliceTiming)\n {\n slice['executionDelta'] = Date.now() - slice['executionDelta'];\n slice['resultDelta'] = Date.now();\n }\n });\n }\n }\n\n /**\n * Sandbox has had an error which is not from the work function: terminate it\n * and try to redo the slice.\n * @param {JobManager} jobManager \n * @param {Sandbox} sandbox \n * @param {Error} error \n */\n static handleSandboxError (jobManager, sandbox, error)\n {\n const slice = sandbox.slice;\n if (slice)\n {\n if (!slice.isWorking) // Sanity. Exception should never fire.\n throw new Error(`handleSandboxError: slice ${slice.identifier} must be WORKING.`);\n\n slice['sandboxErrorCount'] = (slice['sandboxErrorCount'] || 0) + 1;\n sandbox.slice = null;\n if (slice['sandboxErrorCount'] < (workerTuning.maxSandboxErrorsPerSlice || 2))\n slice.resetState();\n else\n {\n slice.collectResult(error, false /*success*/);\n const reason = `sandboxError event handler, too many errors ${slice['sandboxErrorCount']}: ${sandbox.id}~${jobManager.identifier}`;\n jobManager.supervisor.returnSlice(slice, reason);\n }\n jobManager.supervisor.error(`JobManager.handleSandboxError: Sandbox ${sandbox.identifier}...(${sandbox.public.name}/${slice['sandboxErrorCount']}) with slice ${slice.identifier} had error.`, error);\n }\n else\n jobManager.supervisor.error(`JobManager.handleSandboxError: Sandbox ${sandbox.identifier} has no slice...(${sandbox.public.name} had error.`, error);\n\n jobManager.returnSandbox(sandbox); /* terminate the sandbox */\n }\n\n // _Idx\n //\n // Remove from array and return sandbox.\n //\n\n /**\n * Remove sandbox from this.sandboxInventory and terminate is not already terminated.\n * @param {Sandbox} sandbox - The sandbox to remove from this.sandboxInventory.\n * @param {boolean} [assertExists=true] - When true (and type=array) then assert that sandbox exists in this.sandboxInventory.\n */\n returnSandbox(sandbox, assertExists = true)\n {\n debugging('supervisor') && console.debug(`JobManager.returnSandbox: ${sandbox.identifier}, assertExists ${assertExists}`);\n if (this.sandboxInventory && this.sandboxInventory.length > 0)\n JobManager.removeElement(this.sandboxInventory, sandbox, assertExists);\n // The cleaning up of terminated sandboxes in this.supervisor.sandboxInventory happens in this.supervisor.purgeSandboxes.\n if (!sandbox.isTerminated)\n sandbox.terminate(false);\n }\n /**\n * Remove slice from this.sliceInventory.\n * @param {Slice} slice - The slice to remove from this.sliceInventory.\n * @param {boolean} [assertExists=true] - When true (and type=array) then assert that slice exists in this.sliceInventory.\n */\n removeSlice(slice, assertExists = true)\n {\n assert(this.sliceInventory instanceof Array);\n if (this.sliceInventory && this.sliceInventory.length > 0)\n JobManager.removeElement(this.sliceInventory, slice, assertExists);\n }\n\n /**\n * Remove slices from this.sliceInventory.\n * @param {Slice[]} slices - The slices to remove from this.sliceInventory.\n */\n removeSlices(slices)\n {\n assert(this.sliceInventory instanceof Array);\n if (this.sliceInventory && this.sliceInventory.length > 0)\n this._sliceInventory = this.sliceInventory.filter((slice) => slices.indexOf(slice) === -1);\n }\n\n /**\n * Remove element from arrayLike.\n * @param {Array<*>} array\n * @param {object|number} element\n * @param {boolean} [assertExists=true]\n */\n static removeElement(array, element, assertExists = true)\n {\n let index = array.indexOf(element);\n if (assertExists)\n {\n assert(index !== -1);\n array.splice(index, 1);\n } else if (index !== -1) array.splice(index, 1);\n }\n\n // _Idx\n //\n // Fetch: WorkFn, slices, arguments.\n //\n\n /**\n * Fetch work function, work function arguments and possibly the range object describing the jobs slices..\n * @param {object} mpe - messagePayloadElement: job object returned by task-jobs.js . \n */\n async fetchJob(mpe)\n {\n if (debugging('worker'))\n dumpObject(mpe, 'JobManager.fetchJob: mpe', 512);\n\n if (AWAIT_ALL)\n {\n if (!mpe.workFunction)\n {\n mpe.workFunction = await fetchURI(mpe.codeLocation, this.supervisor.makeSafeOriginList('fetchWorkFunctions'));\n if(mpe.requirements.useStrict)\n mpe.useStrict = true;\n delete mpe.codeLocation;\n }\n if (!mpe.arguments)\n {\n let promises = [];\n let uris = mpe.argumentsLocation;\n if (uris)\n for (let i = 0; i < uris.length; i++)\n promises.push(fetchURI(uris[i].value, this.supervisor.makeSafeOriginList('fetchArguments')));\n\n mpe.arguments = await Promise.all(promises);\n\n // node localExec jobs read arguments from a file, so need to ensure they are properly parsed after being read.\n if (this.supervisor.options.localExec && !DCP_ENV.isBrowserPlatform)\n mpe.arguments = scopedKvin.parse(mpe.arguments[0]);\n\n delete mpe.argumentsLocation;\n }\n // if job input data is range object, we send the range object URI to the worker\n if (!mpe.mro && mpe.MROLocation)\n {\n mpe.mro = await fetchURI(mpe.MROLocation, this.supervisor.makeSafeOriginList('fetchData'));\n delete mpe.MROLocation;\n }\n }\n else\n {\n let promises = [];\n\n // Get workFn.\n if (!mpe.workFunction)\n {\n const workFunctionPromise = fetchURI(mpe.codeLocation, this.supervisor.makeSafeOriginList('fetchWorkFunctions'))\n .then((workFunction) => {\n mpe.workFunction = workFunction;\n if (mpe.requirements.useStrict)\n mpe.useStrict = true;\n delete mpe.codeLocation;\n });\n promises.push(workFunctionPromise);\n }\n\n // if job input data is range object, we send the range object URI to the worker\n if (!mpe.mro && mpe.MROLocation)\n {\n // Do we need this? SAVE.\n // if (this.supervisor.options.localExec && !DCP_ENV.isBrowserPlatform)\n // mpe.MROLocation = await fetchURI(mpe.MROLocation, this.supervisor.allowedOrigins, this.supervisor.fetchData);\n\n const mroPromise = fetchURI(mpe.MROLocation, this.supervisor.makeSafeOriginList('fetchData'))\n .then((mro) => {\n mpe.mro = mro;\n delete mpe.MROLocation;\n });\n promises.push(mroPromise)\n }\n\n // Get workFn args.\n if (!mpe.arguments && mpe.argumentsLocation)\n {\n mpe.arguments = new Array(mpe.argumentsLocation.length);\n for (let k = 0; k < mpe.argumentsLocation.length; k++)\n promises.push(fetchURI(mpe.argumentsLocation[k].value, this.supervisor.makeSafeOriginList('fetchArguments'))\n .then((arg) => (mpe.arguments[k] = arg) ));\n }\n\n await Promise.all(promises);\n\n if (mpe.argumentsLocation) delete mpe.argumentsLocation;\n }\n\n return mpe;\n }\n\n /**\n * Look up slice.datumUri or use the range object this.mro, constructed by fetchJob.\n * @param {Slice} slice \n * @returns {Promise<{ inputDatum, dataError }>}\n */\n async fetchSliceData (datumUri, slice) // eslint-disable-line require-await\n {\n function catchHandler(error)\n {\n const dataError = error;\n dataError['errorCode'] = (error.code === 'EFETCH') ? 'EFETCH' : 'EUNCAUGHTERROR';\n return dataError;\n }\n\n if (!datumUri && !this.state.is(READY))\n throw new Error(`When !datumUri, JobManager '${this.identifier}' must be in READY state.`);\n\n try\n {\n if (!datumUri)\n {\n if (!this.mro) throw new Error('Must complete call to JobManager.fetchJob before calling JobManager.fetchSliceData.');\n const ro = rehydrateRange(this.mro);\n // Slice numbers start at 1.\n const inputDatum = ro[slice.sliceNumber - 1];\n debugging('jobmanager') && console.debug(`Fetched mro datum: ${stringify(inputDatum, 512)}`);\n return { inputDatum, dataError: null };\n }\n\n return fetchURI(datumUri, this.supervisor.makeSafeOriginList('fetchData'))\n .then((inputDatum) => {\n debugging('jobmanager') && console.debug(`Fetched datum: ${stringify(inputDatum, 512)}`);\n return { inputDatum, dataError: null };\n })\n .catch((error) => {\n const dataError = catchHandler(error);\n return { inputDatum: null, dataError };\n });\n }\n catch (error)\n {\n const dataError = catchHandler(error);\n return { inputDatum: null, dataError };\n }\n }\n\n // _Idx\n //\n // Miscellaneous.\n //\n\n /**\n * Debugging helper.\n * @param {boolean} [details=true]\n */\n dumpSlices(details = true)\n {\n if (this.sliceInventory.length < 1) return;\n const queued = [], working = [], complete = [], failed = [], finished = [], extras = [];\n for (const slice of this.sliceInventory)\n {\n if (slice.isUnassigned || slice.isReady)\n queued.push(slice);\n else if (slice.isReserved || slice.isWorking)\n working.push(slice);\n else if (slice.isComplete)\n complete.push(slice);\n else if (slice.hasFailed)\n failed.push(slice);\n else if (slice.isFinished)\n finished.push(slice);\n else\n extras.push(slice);\n }\n console.log(`:--JobManager.dumpSlices--${this.identifier} slices ${this.sliceInventory.length}-------`);\n console.log(`-----queued(${queued.length})-----------------------------------------------------------------`);\n if (details)\n {\n for (const slice of queued)\n console.log(slice.identifier);\n console.log(`-----working(${working.length})----------------------------------------------------------------`);\n for (const slice of working)\n console.log(slice.identifier);\n console.log(`-----complete(${complete.length})----------------------------------------------------------------`);\n for (const slice of complete)\n console.log(slice.identifier);\n console.log(`-----failed(${failed.length})----------------------------------------------------------------`);\n for (const slice of failed)\n console.log(slice.identifier);\n console.log(`-----finished(${finished.length})---------------------------------------------------------------`);\n for (const slice of finished)\n console.log(slice.identifier);\n console.log(`-----extras(${extras.length})---------------------------------------------------------------`);\n for (const slice of extras)\n console.log(slice.identifier);\n }\n else\n {\n console.log(`-----working(${working.length})----------------------------------------------------------------`);\n console.log(`-----complete(${complete.length})----------------------------------------------------------------`);\n console.log(`-----failed(${failed.length})----------------------------------------------------------------`);\n console.log(`-----finished(${finished.length})---------------------------------------------------------------`);\n console.log(`-----extras(${extras.length})---------------------------------------------------------------`);\n }\n console.log('-----------------------------------------------------------------------------------');\n }\n\n /**\n * Calls removeStackTrace when !this.displayMaxDiagInfo .\n * @param {string|Error} error\n * @returns {string|Error}\n */\n checkStackTrace(error)\n {\n return this.displayMaxDiagInfo ? error : this.supervisor.removeStackTrace(error);\n }\n\n /**\n * Add slices to the job manager's inventory.\n *\n * @param {SliceMessage[]} sliceMessages - Messages from task distributor describing slices.\n * @param {object} authorizationMessage - The signature that shipped with the task authorizing this worker.\n */\n addSlices (sliceMessages, authorizationMessage)\n {\n sliceMessages.forEach((sliceMessage) => {\n const slice = new Slice(this, sliceMessage, authorizationMessage);\n if (!slice.isEstimation) this.isEstimation = false;\n this.sliceInventory.push(slice);\n });\n }\n\n /**\n * Schedule the slice to be executed.\n * @param {Slice} slice\n * @param {boolean} [placeInTheFrontOfTheQueue=false]\n */\n scheduleSlice(slice, placeInTheFrontOfTheQueue = false)\n {\n // Reset slice state to allow execution.\n slice.resetState();\n // Enqueue in the to-be-executed queue.\n if (placeInTheFrontOfTheQueue) this.sliceInventory.unshift(slice);\n else this.sliceInventory.push(slice);\n }\n\n /**\n * XXXpfr @todo UNUSED but may be used soon\n * Returns a counterfeit JobManager - a generic object with most of the same\n * ownProperties as a real exports.JobManager. Any inventories and synchronizer are\n * duplicated (equivalent state, no events copied, ~ shallow clone)\n * @returns {JobManager}\n */\n counterfeit()\n {\n /** @type {JobManager} */\n const fake = {};\n \n for (let prop in Object.keys(this))\n {\n if (this[prop] instanceof Inventory)\n fake[prop] = this[prop].duplicate();\n else if (this[prop] instanceof Synchronizer)\n fake[prop] = this[prop].valueOf();\n else\n fake[prop] = this[prop];\n }\n\n return fake;\n }\n\n}\nexports.JobManager = JobManager;\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/job-manager.js?");
4542
4586
 
4543
4587
  /***/ }),
4544
4588
 
@@ -4558,7 +4602,7 @@ eval("/**\n * @file worker/supervisor2/load.js\n *\n * A support class for Super
4558
4602
  \***********************************************************/
4559
4603
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4560
4604
 
4561
- eval("/**\n * @file worker/module-cache.js\n *\n * A cache for modules.\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * June 2022\n */\n\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\n \nclass ModuleCache extends EventEmitter\n{\n constructor (supervisor)\n {\n super('ModuleCache')\n this.supervisor = supervisor;\n this.cache = {}; // Modules keyed by module group.\n }\n\n /**\n * Returns an object listing what jobs and modules are currently cached.\n * @returns {string} - in the form: moduleGroup1: [ job1 ], moduleGroup2: [ job1, job2 ]...\n */\n get cacheDescription ()\n {\n const desc = {};\n for (const [ id, moduleJobs ] of Object.entries(this.cache))\n desc[id] = moduleJobs.jobs;\n return JSON.stringify(desc);\n }\n\n /**\n * Attempts to look up an item from the cache.\n * @param {string} id - the items module group name\n * @param {string} job - the job calling fetch\n * @returns {any} The value of the stored item or undefined if nothing is found\n */\n fetch(id, job)\n {\n const moduleJobs = this.cache[id];\n if (!moduleJobs) return moduleJobs;\n if (moduleJobs.jobs.indexOf(job) === -1 )\n moduleJobs.jobs.push(job);\n return moduleJobs.module;\n }\n\n /**\n * Stores a fetched value for one of the caches.\n * @param {string} id - the items identifier module group name\n * @param {string} job - the job calling fetch\n * @param {any} value - the item to store\n * @returns {any} - @value passed in\n */\n store (id, job, value)\n {\n if (!this.cache[id]) this.cache[id] = { module: value, jobs: [ job ] };\n else if (this.cache[id].jobs.indexOf(job) === -1) this.cache[id].jobs.push(job);\n return value\n }\n \n /**\n * Removes a job reference from a module cache.\n * When there are no jobs left, the module cache entry is destroyed.\n * @param {string} job - the job to be removed.\n */\n removeJob (job)\n {\n for (const [ id, moduleJobs ] of Object.entries(this.cache))\n {\n moduleJobs.jobs = moduleJobs.jobs.filter(_job => _job !== job);\n if (moduleJobs.jobs.length < 1) delete this.cache[id];\n }\n }\n\n /**\n * Attempts to fetch a module group from the cache and\n * if it's not found it attempts to fetch then store\n * the module group from the package manager.\n *\n * @param {array} modulesArray - the array of modules requested \n * - (when stringified it's the identifier of the module group)\n *\n * @returns {Promise<object>} - the module group\n * @throws when the module group can not be fetched\n */\n async fetchModule(modulesArray, job)\n {\n const cacheKey = JSON.stringify(modulesArray);\n let modules = this.fetch(cacheKey, job);\n if (modules !== null)\n {\n debugging() && console.debug('FetchModule: Cache hit', { cacheKey, job });\n return modules;\n }\n\n const {\n success,\n payload: responsePayload,\n } = await this.supervisor.packageManagerConnection.send('fetchModule', {\n modules: modulesArray,\n });\n\n if (!success)\n {\n /**\n * Preserving the error message by not rewrapping it with DCPError in case\n * we want to let clients know which module couldn't be fetched.\n */\n throw responsePayload;\n }\n\n modules = await responsePayload;\n return this.store(cacheKey, modules, job);\n }\n}\nexports.ModuleCache = ModuleCache;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/module-cache.js?");
4605
+ eval("/**\n * @file worker/module-cache.js\n *\n * A cache for modules.\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * June 2022\n */\n\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\n \nclass ModuleCache extends EventEmitter\n{\n constructor (supervisor)\n {\n super('ModuleCache')\n this.supervisor = supervisor;\n this.cache = {}; // Modules keyed by module group.\n }\n\n /**\n * Returns an object listing what jobs and modules are currently cached.\n * @returns {string} - in the form: moduleGroup1: [ job1 ], moduleGroup2: [ job1, job2 ]...\n */\n get cacheDescription ()\n {\n const desc = {};\n for (const [ id, moduleJobs ] of Object.entries(this.cache))\n desc[id] = moduleJobs.jobs;\n return JSON.stringify(desc);\n }\n\n /**\n * Attempts to look up an item from the cache.\n * @param {string} id - the items module group name\n * @param {string} job - the job calling fetch\n * @returns {any} The value of the stored item or undefined if nothing is found\n */\n fetch(id, job)\n {\n const moduleJobs = this.cache[id];\n if (!moduleJobs) return moduleJobs;\n if (moduleJobs.jobs.indexOf(job) === -1 )\n moduleJobs.jobs.push(job);\n return moduleJobs.module;\n }\n\n /**\n * Stores a fetched value for one of the caches.\n * @param {string} id - the items identifier module group name\n * @param {string} job - the job calling fetch\n * @param {any} value - the item to store\n * @returns {any} - @value passed in\n */\n store (id, job, value)\n {\n if (!this.cache[id]) this.cache[id] = { module: value, jobs: [ job ] };\n else if (this.cache[id].jobs.indexOf(job) === -1) this.cache[id].jobs.push(job);\n return value\n }\n \n /**\n * Removes a job reference from a module cache.\n * When there are no jobs left, the module cache entry is destroyed.\n * @param {string} job - the job to be removed.\n */\n removeJob (job)\n {\n for (const [ id, moduleJobs ] of Object.entries(this.cache))\n {\n moduleJobs.jobs = moduleJobs.jobs.filter(_job => _job !== job);\n if (moduleJobs.jobs.length < 1) delete this.cache[id];\n }\n }\n\n /**\n * Attempts to fetch a module group from the cache and\n * if it's not found it attempts to fetch then store\n * the module group from the package manager.\n *\n * @param {array} modulesArray - the array of modules requested \n * - (when stringified it's the identifier of the module group)\n *\n * @returns {Promise<object>} - the module group\n * @throws when the module group can not be fetched\n */\n async fetchModule(modulesArray, job)\n {\n const cacheKey = JSON.stringify(modulesArray);\n let modules = this.fetch(cacheKey, job);\n if (modules)\n {\n debugging() && console.debug('FetchModule: Cache hit', { cacheKey, job });\n return modules;\n }\n\n const {\n success,\n payload: responsePayload,\n } = await this.supervisor.packageManager.send('fetchModule', {\n modules: modulesArray,\n });\n\n if (!success)\n {\n /**\n * Preserving the error message by not rewrapping it with DCPError in case\n * we want to let clients know which module couldn't be fetched.\n */\n debugging() && console.debug('FetchModule failure:', responsePayload);\n throw responsePayload;\n }\n\n modules = await responsePayload;\n return this.store(cacheKey, job, modules);\n }\n}\nexports.ModuleCache = ModuleCache;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/module-cache.js?");
4562
4606
 
4563
4607
  /***/ }),
4564
4608
 
@@ -4579,7 +4623,7 @@ eval("/**\n * @file worker/supervisor2/rolling-statistics.js\n *\n * A support c
4579
4623
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4580
4624
 
4581
4625
  "use strict";
4582
- eval("/* eslint-disable require-await */\n// NOTE - need timeout/postmessage function\n/**\n * @file dcp-client/worker/supervisor2/sandbox.js\n *\n * A sandbox that when constructed and assigned can do work for\n * a distributed slice. A sandbox runs for a single slice at a time.\n *\n * Usage (simplified...):\n * const sandbox = new Sandbox(this, { ...this.options.sandboxOptions });\n * await sandbox.start(delayStartMs);\n * await sandbox.assign(this, delayAssignMs);\n * return sandbox.work(delayWorkMs)\n * .then((result) => { \n * slice.collectResult(result, true);\n * sandbox.changeWorkingToAssigned();\n * this.supervisor.recordResult(slice)\n * })\n * .catch((error) => {\n * slice.collectResult(error, false);\n * const reason = this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n * this.supervisor.returnSlice(slice, reason);\n * this.returnSandbox(sandbox);\n * });\n *\n * Debug flags:\n * Sandbox.debugWork = true // - turns off 30 second timeout to let user debug sandbox innards more easily\n * Sandbox.debugState = true // - logs all state transitions for this sandbox\n * Sandbox.debugEvents = true // - logs all events received from the sandbox\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Ryan Rossiter, ryan@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * May 2019\n * Decemeber 2020\n * June 2022\n * @module sandbox\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { assert, assertEq3 } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst nanoid = (__webpack_require__(/*! nanoid */ \"./node_modules/nanoid/index.browser.js\").nanoid);\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { truncateAddress } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nconst scopedKvin = new kvin.KVIN({Object: ({}).constructor,\n Array: ([]).constructor, \n Function: (()=>{}).constructor});\n\nlet timeDilation = 1;\nif (DCP_ENV.platform === 'nodejs') {\n const { requireNative } = __webpack_require__(/*! dcp/dcp-client/webpack-native-bridge */ \"./src/dcp-client/webpack-native-bridge.js\");\n /** Make timers 10x slower when running in niim */\n timeDilation = (requireNative('module')._cache.niim instanceof requireNative('module').Module) ? 10 : 1;\n}\nconst addressTruncationLength = 20;\nconst workerTuning = dcpConfig.worker;\ntimeDilation = 1;\nlet sbCnter = 0; // Global counter of terminated sandboxes guarded by debugging().\n\n/**\n * Wraps console.debug to emulate debug module prefixing messages on npm.\n * @param {...any} args\n */\nconst debug = (...args) => {\n if (debugging()) {\n console.debug('Sandbox:', ...args);\n }\n};\n\n// Sandbox states\nconst UNREADY = 'UNREADY' // No Sandbox (web worker, saworker, etc) has been constructed yet\nconst READYING = 'READYING' // Sandbox is being constructed and environment (bravojs, env) is being set up\nconst READY_FOR_ASSIGN = 'READY_FOR_ASSIGN' // Sandbox is ready to be assigned\nconst ASSIGNED = 'ASSIGNED' // Sandbox is assigned but not working\nconst ASSIGNING = 'ASSIGNING' // Sandbox is in the process of being ASSIGNED\nconst WORKING = 'WORKING' // Sandbox is working\nconst BROKEN = 'BROKEN' // Sandbox is broken and should be terminated.\nconst EVAL_RESULT_PREFIX = 'evalResult::';\n\nclass SandboxError extends Error { constructor(errorCode, ...args) { super(...args); this.errorCode = errorCode; }}\nclass NoProgressError extends SandboxError { constructor(...args) { super('ENOPROGRESS', ...args); } }\nclass SliceTooSlowError extends SandboxError { constructor(...args) { super('ESLICETOOSLOW', ...args); } }\nclass UncaughtExceptionError extends SandboxError { constructor(...args) { super('EUNCAUGHT', ...args); } }\nclass RemoteFetchError extends SandboxError { constructor(...args) { super('EFETCH', ...args); } }\n\nfunction fillInError(errorCtor, errorIn) {\n const errorOut = new errorCtor(errorIn.message);\n errorOut.name = errorIn.name;\n errorOut.fileName = errorIn.fileName;\n errorOut.lineNumber = errorIn.lineNumber;\n errorOut.stack = errorIn.stack;\n return errorOut;\n}\n\n/** @typedef {import('dcp/common/dcp-events').EventEmitter} EventEmitter */\n/** @typedef {import('./index').Supervisor} Supervisor */\n/** @typedef {import('./slice2').Slice} Slice */\n/** @typedef {import('./job-manager').JobManager} JobManager */\n/** @typedef {import('./module-cache').ModuleCache} ModuleCache */\n\n/**\n * @access public\n * @typedef {object} SandboxOptions\n * @constructor {function} [SandboxConstructor]\n * @property {boolean} [ignoreNoProgress] - When true, the sandbox will not be stopped for not calling progress\n */\n\nclass Sandbox extends EventEmitter {\n /**\n * A Sandbox (i.e. a worker sandbox) which executes distributed slices.\n *\n * @constructor\n * @param {Supervisor} supervisor\n * @param {SandboxOptions} options\n */\n constructor (supervisor, options) {\n super('Sandbox');\n /** @type {Supervisor} */\n this.supervisor = supervisor; /** XXXpfr @todo Temporary for sandbox1 compat. Remove later. Directly pass in moduleCache. */\n /** @type {ModuleCache} */\n this.moduleCache = supervisor.moduleCache;\n /** @type {SandboxOptions} */\n this.options = {\n ignoreNoProgress: false,\n ...options,\n SandboxConstructor: options.SandboxConstructor || (__webpack_require__(/*! ../evaluators */ \"./src/dcp-client/worker/evaluators/index.js\").BrowserEvaluator),\n }\n /** @type {Synchronizer} */\n this.state = new Synchronizer(UNREADY, [ UNREADY, READYING, READY_FOR_ASSIGN, ASSIGNING, ASSIGNED, WORKING, BROKEN ]);\n\n /** @type {string} */\n this.jobAddress = null;\n /** @type {object} */\n this.evaluatorHandle = null;\n /** @type {object} */\n this.capabilities = null;\n /** @type {EventEmitter} */\n this.ee = new EventEmitter('SandboxInternal');\n\n /** @type {boolean} */\n this.terminated = false;\n /** @type {number?} */\n this.progress = 100;\n /** @type {object} */\n this.progressReports = null;\n /** @type {object} */\n this.progressTimeout = null;\n /** @type {object} */\n this.sliceTimeout = null;\n /** @type {object} */\n this.rejectionData = null;\n /** @type {Slice} */\n this.slice = null;\n /** @type {number?} */\n this.sliceStartTime = null;\n /** @type {number} */\n this.id = Sandbox.getNewId();\n\n this.ringMessageHandlers = [\n this.handleRing0Message,\n this.handleRing1Message,\n this.handleRing2Message,\n this.handleRing3Message,\n ];\n\n this.resetSliceTimeReport();\n }\n\n /** @type {string} - debug string that characterizes sandbox. */\n get identifier() {\n if (!this.jobAddress) return `${this.id}.${this.state}`;\n return `${this.id}.${truncateAddress(this.jobAddress, addressTruncationLength)}.${this.state}`;\n }\n\n static getNewId() {\n return Sandbox.idCounter++;\n }\n\n /** @type {boolean} */\n get isReadyForAssign () {\n return this.state.is(READY_FOR_ASSIGN);\n }\n /** @type {boolean} */\n get isAssigned () {\n return this.state.is(ASSIGNED);\n }\n /** @type {boolean} */\n get isWorking () {\n return this.state.is(WORKING);\n }\n /** @type {boolean} */\n get isBroken () {\n return this.state.is(BROKEN);\n }\n /** @type {boolean} */\n get isTerminated () {\n return this.terminated;\n }\n\n /**\n * Mark WORKING sandbox as ASSIGNED in preparation for possible reuse,\n */\n changeWorkingToAssigned () {\n this.state.testAndSet(WORKING, ASSIGNED);\n }\n \n /**\n * Remove from collection of ASSIGNED sandboxes in preparation for termination,\n */\n markAsUnready() {\n this.state.testAndSet(ASSIGNED, UNREADY);\n }\n \n /**\n * Transitions: ASSIGNED --> WORKING and assigns the slice.\n * @param {Slice} slice \n */\n markAsWorking (slice) {\n if (!this.isAssigned)\n throw new Error(`Sandbox ${this.identifier} is not ready to work`);\n this.state.set(ASSIGNED, WORKING);\n this.slice = slice;\n }\n \n /**\n * Fancy assert.\n */\n verifyWorking () {\n if (!this.isWorking) {\n throw new Error(`Sandbox ${this.identifier} is not working`);\n }\n }\n\n /**\n * Error feedback to user.\n * @param {string} message\n * @param {*} extra\n */\n error(message, extra)\n {\n const dcpError = new DCPError(message, extra);\n this.emit('error', dcpError);\n }\n\n /**\n * Readies the sandbox. This will result in the sandbox being ready and not assigned,\n * it will need to be assigned with a job before it is able to do work.\n *\n * @todo maybe preload specific modules or let the cache pass in what modules to load?\n * @param {number} [delay=0]\n * @returns {Promise<Sandbox>}\n * @throws on failure to ready\n */\n async start(delay = 0) {\n this.state.set(UNREADY, READYING);\n\n if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay * timeDilation));\n\n try {\n // RING 0\n this.evaluatorHandle = new this.options.SandboxConstructor({\n name: `DCP Sandbox #${this.id}`,\n });\n this.evaluatorHandle.onerror = this.onerror.bind(this);\n\n const messageHandler = this.onmessage.bind(this);\n this.evaluatorHandle.onmessage = function onmessage(event)\n {\n let data;\n if (event.data.serialized)\n data = kvin.parse(event.data.message);\n else\n data = kvin.unmarshal(event.data);\n messageHandler({ data });\n }\n\n const evaluatorPostMessage = this.evaluatorHandle.postMessage.bind(this.evaluatorHandle);\n this.evaluatorHandle.postMessage = function postMessage(message)\n {\n evaluatorPostMessage(scopedKvin.marshal(message));\n }\n\n const that = this;\n this.evaluatorHandle.addEventListener('end', () => that.terminate(true));\n\n // Now in RING 1\n\n // Now in RING 2\n await this.describe();\n this.state.set(READYING, READY_FOR_ASSIGN);\n this.emit('ready', this);\n } catch (error) {\n this.error('Failed to start the sandbox.', error);\n this.state.set(READYING, BROKEN);\n this.terminate(false);\n throw error;\n }\n \n return this;\n }\n\n /**\n * This will assign the sandbox with a job, loading its sandbox code\n * into the sandbox.\n *\n * @param {JobManager} jobManager - The job manager that will be the owner of this sandbox.\n * @param {number} [delay=0]\n * @returns {Promise<Sandbox>}\n * @throws on initialization failure\n */\n async assign (jobManager, delay = 0) {\n \n if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay * timeDilation));\n \n this.state.set(READY_FOR_ASSIGN, ASSIGNING);\n this.jobAddress = jobManager.address;\n this.job = jobManager.jobMessage;\n \n assertEq3(this.job.address, this.jobAddress);\n assert(typeof this.job === 'object');\n assert(typeof this.job.requirements === 'object');\n assert(Array.isArray(this.job.dependencies));\n assert(Array.isArray(this.job.requirePath));\n\n // Extract public data from job, with defaults\n this.public = Object.assign({\n name: `Anonymous Job ${truncateAddress(this.jobAddress, addressTruncationLength)}`,\n description: 'Discreetly helping make the world smarter.',\n link: 'https://distributed.computer/about',\n }, this.job.public);\n\n // Future: We may want other filename tags for appliances // RR Nov 2019\n\n // Important: The order of applying requirements before loading the sandbox code\n // is important for modules and sandbox code to set globals over the whitelist.\n await this.applySandboxRequirements(this.job.requirements);\n await this.assignEvaluator();\n \n return this;\n }\n \n async assignEvaluator() {\n debug('Begin assigning job to evaluator');\n const that = this;\n\n return new Promise(function sandbox$$assignEvaluatorPromise(resolve, reject) {\n const message = {\n request: 'assign',\n job: that.job,\n sandboxConfig: workerTuning.sandbox,\n };\n\n /* note - onFailListener used for removal. This is necessary due to a bug in ee.once. /wg Feb 2022 */\n \n const onSuccess = (event) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('reject', onFailListener);\n that.emit('assigned', event.jobAddress); /** XXXpfr @todo Who is listening to event 'assigned' ? */\n debug('Job assigned to evaluator');\n resolve();\n };\n\n const onFail = (error) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('assigned', onSuccessListener);\n reject(error);\n };\n\n const onSuccessListener = that.ee.once('assigned', onSuccess); // Emitted by handleRing2Message.\n const onFailListener = that.ee.once('reject', onFail);\n that.evaluatorHandle.postMessage(message);\n }).then((event) => {\n that.state.set(ASSIGNING, ASSIGNED);\n return event;\n }).catch(error => {\n that.state.set(ASSIGNING, BROKEN);\n debug('Failure in assigning job to evaluator', error);\n throw error;\n });\n }\n\n /**\n * Evaluates a string inside the sandbox.\n *\n * @param {string} code - the code to evaluate in the sandbox\n * @param {string} filename - the name of the 'file' to help with debugging,\n * no longer working though?\n * @returns {Promise} - resolves with eval result on success, rejects\n * otherwise\n */\n eval(code, filename) {\n const that = this;\n \n return new Promise(function sandbox$$eval$Promise(resolve, reject) {\n let msgId = nanoid();\n let msg = {\n request: 'eval',\n data: code,\n filename,\n msgId, \n };\n\n const eventId = EVAL_RESULT_PREFIX + msgId;\n\n const onSuccess = (event) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('reject', onFailListener);\n resolve(event);\n }\n\n const onFail = (error) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener(eventId, onSuccessListener);\n reject(error);\n }\n\n const onSuccessListener = that.ee.once(eventId, onSuccess);\n const onFailListener = that.ee.once('reject', onFail);\n\n that.evaluatorHandle.postMessage(msg);\n })\n }\n\n /**\n * Resets the state of the bootstrap, without resetting the sandbox function if assigned.\n * Mostly used to reset the progress status before reusing a sandbox on another slice.\n * Must be called after @start.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n resetSandboxState () {\n const that = this;\n\n return new Promise(function sandbox$resetSandboxStatePromise(resolve, reject) {\n let successCb, failTimeout;\n let msg = {\n request: 'resetState',\n };\n\n successCb = that.ee.once('resetStateDone', function sandbox$resetSandboxState$success () {\n if (failTimeout === false)\n return; /* already rejected */\n clearTimeout(failTimeout);\n failTimeout = false;\n resolve();\n });\n\n failTimeout = setTimeout(function sandbox$resetSandboxState$fail() {\n if (failTimeout === false)\n return; /* already resolved */\n \n that.ee.removeListener('resetStateDone', successCb);\n that.terminate(false);\n failTimeout = false;\n\n reject(new Error('resetState never received resetStateDone event from sandbox'));\n }, 3000 * timeDilation); /* XXXwg need tuneable */\n\n assert(that.evaluatorHandle); // It is possible that that.terminate nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(msg);\n });\n }\n\n /**\n * Clear all timers that are set inside the sandbox (evaluator) environment.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n clearSandboxTimers() {\n const that = this;\n \n return new Promise(function sandbox$clearSandboxTimersPromise(resolve, reject) {\n let successCb, failTimeout;\n let msg = {\n request: 'clearTimers',\n };\n\n successCb = that.ee.once('clearTimersDone', function sandbox$clearSandboxTimers$success() {\n if (failTimeout === false)\n return; /* already rejected */\n clearTimeout(failTimeout);\n failTimeout = false;\n resolve();\n });\n\n failTimeout = setTimeout(function sanbox$clearSandboxTimers$fail() {\n if (failTimeout === false)\n return; /* already resolved */\n \n that.ee.removeListener('clearTimersDone', successCb);\n that.terminate(false);\n failTimeout = false;\n \n reject(new Error('clearTimers never received clearTimersDone event from sandbox'));\n }, 3000 * timeDilation); /* XXXwg need tuneable */\n\n if (that.evaluatorHandle) // Sometimes that.terminate nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(msg);\n });\n }\n\n /**\n * Sends a post message to describe its capabilities.\n *\n * Side effect: Sets the capabilities property of the current sandbox.\n *\n * @returns {Promise} Resolves with the sandbox's capabilities. Rejects with\n * an error saying a response was not received.\n * @memberof Sandbox\n */\n describe() {\n debug('Beginning to describe evaluator');\n const that = this;\n \n return new Promise(function sandbox$describePromise(resolve, reject) {\n if (that.evaluatorHandle === null) {\n return reject(new Error('Evaluator has not been initialized.'));\n }\n\n /**\n * Opted to create a flag for the describe response being received so that\n * we don't have to *hoist* the timeout's id to clear it in the response\n * handler.\n */\n let didReceiveDescribeResponse = false;\n const describeResponseHandler = that.ee.once('describe', (data) => { // Emitted by handleRing2Message.\n didReceiveDescribeResponse = true;\n const { capabilities } = data;\n if (typeof capabilities === 'undefined') {\n reject(new Error('Did not receive capabilities from describe response.'));\n }\n that.capabilities = capabilities;\n\n // Currently only used in tests. May use the event in the future.\n that.emit('described', capabilities); /** XXXpfr @todo Who is listening to event 'described' ? */\n debug('Evaluator has been described');\n resolve(capabilities);\n });\n const describeResponseFailedHandler = () => {\n if (!didReceiveDescribeResponse) {\n that.ee.removeListener('describe', describeResponseHandler);\n that.terminate(false);\n reject(new Error( 'Describe message timed-out. No describe response was received from the describe command.'));\n }\n };\n\n const message = {\n request: 'describe',\n };\n\n // Arbitrarily set the waiting time.\n setTimeout(describeResponseFailedHandler, 6000 * timeDilation); /* XXXwg need tuneable */\n assert(that.evaluatorHandle); // It is possible that that.terminate nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(message);\n });\n }\n\n /**\n * Passes the job's requirements object into the sandbox so that the global\n * access lists can be updated accordingly.\n *\n * e.g. disallow access to OffscreenCanvas without\n * environment.offscreenCanvas=true present.\n *\n * Must be called after @start.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n applySandboxRequirements(requirements) {\n const that = this;\n \n return new Promise(function sandbox$applySandboxRequirementsPromise(resolve, reject) {\n const message = {\n requirements,\n request: 'applyRequirements',\n };\n let wereRequirementsApplied = false;\n\n const successCb = that.ee.once(\n 'applyRequirementsDone',\n function sandbox$applyRequirements$success() {\n wereRequirementsApplied = true;\n resolve();\n },\n );\n\n assert(typeof message.requirements === 'object');\n that.evaluatorHandle.postMessage(message);\n\n setTimeout(function sandbox$finishApplySandboxRequirements() {\n if (!wereRequirementsApplied) {\n that.ee.removeListener('applyRequirementsDone', successCb);\n that.terminate(false);\n reject(new Error('applyRequirements never received applyRequirementsDone response from sandbox'));\n }\n }, 3000 * timeDilation); /* XXXwg needs tunable */\n });\n }\n\n /**\n * Executes a slice received from the supervisor.\n * Must be called after this.start, this.assign and this.markAsWorking .\n *\n * @param {number} [delay = 0] the delay that this method should wait before beginning work, used to avoid starting all sandboxes at once\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n\n async work (delay = 0) {\n const that = this;\n\n if (!this.slice)\n throw new Error('The slice in Sandbox.work has not been set; be sure to call markAsWorking before.');\n\n // cf. DCP-1720\n this.resetSliceTimeReport();\n \n // Now wait for the delay if provided, prevents many sandboxes starting at once from crashing the supervisor\n if (delay > 0) await new Promise(resolve => setTimeout(resolve, (delay + 1) * timeDilation));\n if (!this.isWorking) return; // sandbox.terminate could have been called during the delay timeout\n\n // Prepare the sandbox to begin work\n // will be replaced by `assign` message that should be called before emitting a `work` message\n if (this.jobAddress !== this.slice.jobAddress) {\n throw new Error(`Sandbox.run: Sandbox is already assigned and jobAddress doesn't match previous (${this.jobAddress} !== ${this.slice.jobAddress})`);\n }\n\n let sliceHnd = { job: this.public, sandbox: this };\n await this.resetSandboxState();\n if (!this.slice) {\n this.error(`Slice for job ${this.jobAddress} vanished during work initialization - aborting`);\n return;\n }\n\n const { datum: inputDatum, error: dataError } = this.slice;\n\n this.resetProgressTimeout();\n this.resetSliceTimeout();\n\n return new Promise(function sandbox$$workPromise(resolve, reject) {\n let onSuccess, onFail\n\n onSuccess = that.ee.once('resolve', function sandbox$$work$success (event) {\n that.ee.removeListener('reject', onFail)\n resolve(event)\n }.bind(that));\n\n onFail = that.ee.once('reject', function sandbox$$work$fail (err) {\n that.ee.removeListener('resolve', onSuccess)\n reject(err)\n }.bind(that))\n\n that.sliceStartTime = Date.now();\n that.progress = null;\n that.progressReports = {\n last: undefined,\n lastDeterministic: undefined,\n };\n\n that.resetProgressTimeout();\n that.resetSliceTimeout();\n that.emit('start', sliceHnd);\n \n if (dataError) {\n that.ee.removeListener('resolve', onSuccess);\n that.ee.removeListener('reject', onFail);\n that.emit('workEmit', {\n eventName: 'error',\n payload: {\n message: dataError.message,\n stack: dataError.stack,\n name: that.public.name\n }\n });\n setTimeout(() => reject(dataError), 0)\n\n } else {\n that.evaluatorHandle.postMessage({\n request: 'main',\n data: inputDatum,\n })\n }\n })\n .then(async function sandbox$$work$then(event) {\n // prevent any hanging timers from being fired\n await that.clearSandboxTimers();\n\n /** @todo Should sliceHnd just be replaced with that.public? */\n /** @todo Decide which event is right. */\n //that.emit('slice', sliceHnd); // Unused -- XXXpfr\n that.emit('sliceFinish', event);\n that.emit('complete', that.jobAddress);\n\n that.state.set(WORKING, ASSIGNED);\n that.slice = false;\n\n return event;\n })\n .catch((err) => { \n if (err.name === 'EWORKREJECT') {\n that.rejectionData = err;\n that.evaluatorHandle.postMessage({ request: 'resetAndGetCPUTime' })\n } else { // sandbox termination for rejected work happens in Supervisor.handleRejectedWork\n // Ceci is the reject callback for when the slice throws an error\n that.terminate(false);\n }\n\n that.emit('error', err, 'slice');\n\n if (err instanceof NoProgressError) {\n that.emit('workEmit', {\n eventName: 'noProgress',\n payload: {\n timestamp: Date.now() - that.sliceStartTime,\n data: that.slice.datumUri,\n progressReports: that.progressReports,\n }\n });\n }\n throw err;\n })\n .finally(function sandbox$$work$finally() {\n that.emit('end', sliceHnd); /** XXXpfr @todo Who is listening to event 'end' ? */\n });\n }\n\n resetProgressTimeout() {\n if (this.progressTimeout) {\n clearTimeout(this.progressTimeout);\n this.progressTimeout = null;\n }\n\n this.progressTimeout = setTimeout(() => {\n if (this.options.ignoreNoProgress)\n return this.emit('warning', \"ENOPROGRESS silenced by localExec: In a remote worker, this slice would be stopped for not calling progress frequently enough.\");\n\n this.ee.emit('reject', new NoProgressError(`No progress event was received in the last ${workerTuning.sandbox.progressTimeout / 1000} seconds.`));\n }, +workerTuning.sandbox.progressTimeout * timeDilation);\n }\n\n resetSliceTimeout() {\n if (this.sliceTimeout) clearTimeout(this.sliceTimeout);\n\n this.sliceTimeout = setTimeout(() => {\n if (Sandbox.debugWork) return this.emit('warning', 'Sandbox.debugWork: Ignoring slice timeout');\n\n this.ee.emit('reject', new SliceTooSlowError(`Slice took longer than ${workerTuning.sandbox.sliceTimeout / 1000} seconds.`));\n }, +workerTuning.sandbox.sliceTimeout * timeDilation);\n }\n \n async handleRing0Message(data) {\n debugging('event:ring-0') && debug('event:ring-0', data);\n //handling a true ring 0 message\n switch (data.request) {\n case 'sandboxLoaded':\n // emit externally\n this.emit('sandboxLoaded', this)\n break;\n\n case 'scriptLoaded':\n // emit externally\n this.emit('scriptLoaded', data);\n if(data.result !== \"success\") {\n this.onerror(data);\n }\n break;\n \n case 'clearTimersDone':\n this.ee.emit(data.request, data);\n break;\n case 'totalCPUTime':\n this.updateTime(data);\n if (this.ee.listenerCount('resolve') > 0) {\n this.completeData.timeReport = this.sliceTimeReport;\n this.ee.emit('resolve', this.completeData);\n delete this.completeData;\n } else {\n this.rejectionData.timeReport = this.sliceTimeReport\n this.emit('rejectedWorkMetrics', this.rejectionData) // If there is no internal listener for 'resolve', the slice was rejected and\n delete this.rejectionData; // we need to send the rejected metrics to the supervisor\n } \n break;\n case 'error': {\n // Warning: rejecting here with just event.data.error causes issues\n // where the reject handlers modify the object so it interferes with the\n // workEmit event payload, wrapping in an Error instance copies the values\n const wrappedError = fillInError(UncaughtExceptionError, data.error);\n\n if (this.ee.listenerCount('reject') > 0) {\n this.ee.emit('reject', wrappedError);\n } else {\n // This will happen if the error is thrown during initialization\n throw wrappedError;\n }\n break;\n }\n default: {\n this.error('Received unhandled request from sandbox: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break;\n } \n }\n }\n\n async handleRing1Message(data) {\n switch (data.request) {\n case 'applyRequirementsDone':\n // emit internally\n this.ee.emit(data.request, data)\n break;\n default: {\n this.error('Received unhandled request from sandbox ring 1: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break; \n }\n }\n }\n\n async handleRing2Message(data) {\n debugging('event:ring-2') && debug('event:ring-2', data);\n switch (data.request) {\n case 'dependency': {\n let moduleData;\n try {\n moduleData = await this.moduleCache.fetchModule(data.data, this.jobAddress);\n } catch (error) {\n /*\n * In the event of an error here, we want to let the client know there was a problem in\n * loading their module. However, there hasn't yet been an actual slice assigned to the sandbox.\n * Therefore, we assign 'slice 0' to the sandbox, a slice that will never exist, and is used\n * purely for this purpose. \n */\n this.slice = {\n jobAddress: this.jobAddress,\n sliceNumber: 0,\n };\n\n const payload = {\n name: error.name,\n timestamp: error.timestamp,\n message: error.message,\n };\n\n const wrappedError = fillInError(RemoteFetchError, error);\n\n this.emit('workEmit', {\n eventName: 'error',\n payload,\n });\n this.ee.emit('reject', wrappedError);\n break;\n }\n this.evaluatorHandle.postMessage({\n request: 'moduleGroup',\n data: moduleData,\n id: data.id,\n });\n break;\n }\n case 'error':\n /*\n * Ring 2 error messages will only fire for problems inside of the worker that are separate from\n * the work function. In most cases there are other handlers for situations where 'error' may be emitted\n * such as timeouts if the expected message isn't recieved. Thus, we will output the error, but nothing else.\n */\n this.error('event:ring-2: some error happened.', data.error);\n break;\n case 'describe':\n case 'evalResult':\n case 'resetStateDone':\n case 'assigned':\n // emit internally\n this.ee.emit(data.request, data);\n break;\n case 'reject':\n // emit internally\n this.ee.emit(data.request, data.error);\n break;\n default: {\n this.error(`Received unhandled request from sandbox ring 2. Data: ${JSON.stringify(data, null, 2)}`);\n break;\n }\n }\n }\n\n async handleRing3Message(data) {\n switch (data.request) {\n case 'complete':\n clearTimeout(this.progressTimeout);\n clearTimeout(this.sliceTimeout);\n this.progressTimeout = this.sliceTimeout = null;\n\n if (this.progress === null) {\n if (this.options.ignoreNoProgress) {\n this.emit('warning', \"ENOPROGRESS silenced by localExec: Progress was not called during this slice's execution, in a remote sandbox this would cause the slice to be failed\");\n } else {\n // If a progress update was never received (progress === null) then reject\n this.ee.emit('reject', new NoProgressError('Sandbox never emitted a progress event.'));\n break;\n }\n }\n this.evaluatorHandle.postMessage({ request: 'resetAndGetCPUTime' })\n this.progress = 100;\n this.completeData = data;\n // The timing report and resolve will be emitted when the CPU time is received. \n break;\n case 'progress': {\n let { progress, indeterminate, throttledReports, value } = data;\n this.progress = progress;\n const progressReport = {\n timestamp: Date.now() - this.sliceStartTime,\n progress,\n value,\n throttledReports,\n }\n this.progressReports.last = progressReport;\n if (!indeterminate) {\n this.progressReports.lastDeterministic = progressReport;\n }\n\n this.resetProgressTimeout();\n this.emit('sliceProgress', data);\n break;\n }\n case 'noProgress':\n this.ee.emit('reject', new NoProgressError(data.message));\n break;\n case 'console':\n data.payload.message = scopedKvin.marshal(data.payload.message);\n this.emit('workEmit', {\n eventName: 'console',\n payload: data.payload,\n });\n break;\n case 'emitEvent':/* ad-hoc event from the sandbox (work.emit) */\n this.emit('workEmit', {\n eventName: 'custom',\n payload: data.payload\n });\n break;\n case 'measurement':\n this.updateTime(data);\n break;\n case 'sandboxError': /* the sandbox itself has an error condition */\n this.emit('sandboxError', data.error);\n break;\n case 'workError': { /* the work function threw/rejected */\n this.emit('workEmit', {\n eventName: 'error',\n payload: data.error,\n });\n\n // Warning: rejecting here with just .data.error causes issues\n // where the reject handlers modify the object so it interferes with the\n // workEmit payload, wrapping in an Error instance copies the values\n const wrappedError = fillInError(UncaughtExceptionError, data.error);\n\n if (this.ee.listenerCount('reject') > 0) {\n this.ee.emit('reject', wrappedError);\n } else {\n // This will happen if the error is thrown during initialization\n throw wrappedError;\n }\n break;\n }\n default: {\n this.error('Received unhandled request from sandbox ring 3: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break; \n }\n }\n }\n\n /**\n * Handles progress and completion events from sandbox.\n * Unless explicitly returned out of this function will re-emit the event\n * on @this.ee where the name of the event is event.data.request.\n *\n * @param {object} event - event received from the sandbox\n */\n async onmessage(event) {\n debugging('event') && debug('event', event);\n if (Sandbox.debugEvents) {\n console.debug('sandbox - eventDebug:', {\n id: this.id,\n state: this.state.valueOf(),\n event: JSON.stringify(event)\n })\n }\n\n const { data } = event;\n const ringLevel = data.ringSource\n\n // Give the data to a handler depending on ring level\n if (ringLevel === -1) {\n this.error('Message sent directly from raw postMessage. Terminating worker...');\n console.debug(event);\n return this.terminate(true);\n } else {\n const handler = this.ringMessageHandlers[ringLevel];\n if (handler) {\n handler.call(this, data.value);\n } else {\n this.emit('warning', `No handler defined for message from ring ${ringLevel}`);\n console.debug(event);\n }\n }\n }\n\n /**\n * Error handler for the internal sandbox.\n * Emits error event that gets handled up in the Worker class.\n */\n onerror(event) {\n if (event instanceof Error) this.error('Sandbox emitted an error:', event);\n else this.error(`Sandbox emitted an error: ${event}`);\n this.terminate(true, true);\n }\n\n /**\n * Clears the timeout and terminates the sandbox and sometimes emits a reject event.\n *\n * @param {boolean} [reject = true] - if true emit reject event\n * @param {boolean} [immediate = false] - passed to terminate, used by standaloneWorker to immediately close the connection\n */\n terminate (reject = true, immediate = false) {\n debugging() && console.debug(`Sandbox.terminate ${this.identifier}, count: ${++sbCnter}`);\n\n if (this.isTerminated) return;\n this.terminated = true;\n\n clearTimeout(this.progressTimeout);\n clearTimeout(this.sliceTimeout);\n this.progressTimeout = this.sliceTimeout = null;\n \n if (this.evaluatorHandle && typeof this.evaluatorHandle.terminate === 'function') {\n try {\n this.evaluatorHandle.terminate(immediate);\n this.evaluatorHandle = null;\n } catch (e) {\n this.error(`Error terminating sandbox ${this.id}:`, e);\n } finally {\n this.emit('terminate', this);\n }\n }\n\n if (reject) {\n this.ee.emit('reject', new Error(`Sandbox ${this.id} was terminated.`));\n }\n\n this.emit('terminated');\n }\n\n /**\n * Attempts to stop the sandbox from doing completing its current\n * set of work without terminating the working. \n * \n *** Until stop is implemented properly, use terminate(false).\n *\n * @todo Properly implement stop\n */\n stop () {\n //throw new Error('Sandbox.stop is not yet implemented.')\n this.terminate(false);\n }\n\n /**\n * ringNPostMessage can send a `measurement` request and update these\n * totals.\n */\n updateTime (measurementEvent) {\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (measurementEvent[key]) this.sliceTimeReport[key] += measurementEvent[key];\n })\n }\n\n resetSliceTimeReport () {\n this.sliceTimeReport = {\n total: 0,\n CPU: 0,\n webGL: 0,\n }\n }\n}\n\nSandbox.idCounter = 1;\nSandbox.debugWork = false;\nSandbox.debugState = false;\nSandbox.debugEvents = false;\n\nexports.Sandbox = Sandbox;\nexports.SandboxError = SandboxError;\nexports.NoProgressError = NoProgressError;\nexports.SliceTooSlowError = SliceTooSlowError;\nexports.UncaughtExceptionError = UncaughtExceptionError;\nexports.RemoteFetchError = RemoteFetchError;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/sandbox2.js?");
4626
+ eval("/* eslint-disable require-await */\n// NOTE - need timeout/postmessage function\n/**\n * @file dcp-client/worker/supervisor2/sandbox.js\n *\n * A sandbox that when constructed and assigned can do work for\n * a distributed slice. A sandbox runs for a single slice at a time.\n *\n * Usage (simplified...):\n * const sandbox = new Sandbox(this, { ...this.options.sandboxOptions });\n * await sandbox.start(delayStartMs);\n * await sandbox.assign(this, delayAssignMs);\n * return sandbox.work(delayWorkMs)\n * .then((result) => { \n * slice.collectResult(result, true);\n * sandbox.changeWorkingToAssigned();\n * this.supervisor.recordResult(slice)\n * })\n * .catch((error) => {\n * slice.collectResult(error, false);\n * const reason = this.supervisor.handleSandboxWorkError(sandbox, slice, error);\n * this.supervisor.returnSlice(slice, reason);\n * this.returnSandbox(sandbox);\n * });\n *\n * Debug flags:\n * Sandbox.debugWork = true // - turns off 30 second timeout to let user debug sandbox innards more easily\n * Sandbox.debugState = true // - logs all state transitions for this sandbox\n * Sandbox.debugEvents = true // - logs all events received from the sandbox\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Ryan Rossiter, ryan@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * May 2019\n * Decemeber 2020\n * June 2022\n * @module sandbox\n */\n\n/* global dcpConfig */ // eslint-disable-line no-redeclare\n// @ts-check\n\n\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { assert, assertEq3 } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst DCP_ENV = __webpack_require__(/*! dcp/common/dcp-env */ \"./src/common/dcp-env.js\");\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst nanoid = (__webpack_require__(/*! nanoid */ \"./node_modules/nanoid/index.browser.js\").nanoid);\nconst { EventEmitter } = __webpack_require__(/*! dcp/common/dcp-events */ \"./src/common/dcp-events/index.js\");\nconst { truncateAddress } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\nconst { DCPError } = __webpack_require__(/*! dcp/common/dcp-error */ \"./src/common/dcp-error.js\");\nconst kvin = __webpack_require__(/*! kvin */ \"./node_modules/kvin/kvin.js\");\nconst scopedKvin = new kvin.KVIN({Object: ({}).constructor,\n Array: ([]).constructor, \n Function: (()=>{}).constructor});\n\nlet timeDilation = 1;\nif (DCP_ENV.platform === 'nodejs') {\n const { requireNative } = __webpack_require__(/*! dcp/dcp-client/webpack-native-bridge */ \"./src/dcp-client/webpack-native-bridge.js\");\n /** Make timers 10x slower when running in niim */\n timeDilation = (requireNative('module')._cache.niim instanceof requireNative('module').Module) ? 10 : 1;\n}\nconst addressTruncationLength = 20;\nconst workerTuning = dcpConfig.worker;\ntimeDilation = 1;\nlet sbCnter = 0; // Global counter of terminated sandboxes guarded by debugging().\n\n/**\n * Wraps console.debug to emulate debug module prefixing messages on npm.\n * @param {...any} args\n */\nconst debug = (...args) => {\n if (debugging()) {\n console.debug('Sandbox:', ...args);\n }\n};\n\n// Sandbox states\nconst UNREADY = 'UNREADY' // No Sandbox (web worker, saworker, etc) has been constructed yet\nconst READYING = 'READYING' // Sandbox is being constructed and environment (bravojs, env) is being set up\nconst READY_FOR_ASSIGN = 'READY_FOR_ASSIGN' // Sandbox is ready to be assigned\nconst ASSIGNED = 'ASSIGNED' // Sandbox is assigned but not working\nconst ASSIGNING = 'ASSIGNING' // Sandbox is in the process of being ASSIGNED\nconst WORKING = 'WORKING' // Sandbox is working\nconst BROKEN = 'BROKEN' // Sandbox is broken and should be terminated.\nconst EVAL_RESULT_PREFIX = 'evalResult::';\n\nclass SandboxError extends Error { constructor(errorCode, msg) { super(msg); this.errorCode = errorCode; }}\nclass NoProgressError extends SandboxError { constructor(msg) { super('ENOPROGRESS', msg); } }\nclass SliceTooSlowError extends SandboxError { constructor(msg) { super('ESLICETOOSLOW', msg); } }\nclass UncaughtExceptionError extends SandboxError { constructor(msg) { super('EUNCAUGHT', msg); } }\nclass RemoteFetchError extends SandboxError { constructor(msg) { super('EFETCH', msg); } }\n\n/** @typedef {import('dcp/common/dcp-events').EventEmitter} EventEmitter */\n/** @typedef {import('./index').Supervisor} Supervisor */\n/** @typedef {import('./slice2').Slice} Slice */\n/** @typedef {import('./job-manager').JobManager} JobManager */\n/** @typedef {import('./module-cache').ModuleCache} ModuleCache */\n\n/**\n * @access public\n * @typedef {object} SandboxOptions\n * @constructor {function} [SandboxConstructor]\n * @property {boolean} [ignoreNoProgress] - When true, the sandbox will not be stopped for not calling progress\n */\n\nclass Sandbox extends EventEmitter {\n /**\n * A Sandbox (i.e. a worker sandbox) which executes distributed slices.\n *\n * @constructor\n * @param {Supervisor} supervisor\n * @param {SandboxOptions} options\n */\n constructor (supervisor, options) {\n super('Sandbox');\n /** @type {Supervisor} */\n this.supervisor = supervisor; /** XXXpfr @todo Temporary for sandbox1 compat. Remove later. Directly pass in moduleCache. */\n /** @type {ModuleCache} */\n this.moduleCache = supervisor.moduleCache;\n /** @type {SandboxOptions} */\n this.options = {\n ignoreNoProgress: false,\n ...options,\n SandboxConstructor: options.SandboxConstructor || (__webpack_require__(/*! ../evaluators */ \"./src/dcp-client/worker/evaluators/index.js\").BrowserEvaluator),\n }\n /** @type {Synchronizer} */\n this.state = new Synchronizer(UNREADY, [ UNREADY, READYING, READY_FOR_ASSIGN, ASSIGNING, ASSIGNED, WORKING, BROKEN ]);\n\n /** @type {string} */\n this.jobAddress = null;\n /** @type {object} */\n this.evaluatorHandle = null;\n /** @type {object} */\n this.capabilities = null;\n /** @type {EventEmitter} */\n this.ee = new EventEmitter('SandboxInternal');\n\n /** @type {boolean} */\n this.terminated = false;\n /** @type {number?} */\n this.progress = 100;\n /** @type {object} */\n this.progressReports = null;\n /** @type {object} */\n this.progressTimeout = null;\n /** @type {object} */\n this.sliceTimeout = null;\n /** @type {object} */\n this.rejectionData = null;\n /** @type {Slice} */\n this.slice = null;\n /** @type {number?} */\n this.sliceStartTime = null;\n /** @type {number} */\n this.id = Sandbox.getNewId();\n\n this.ringMessageHandlers = [\n this.handleRing0Message,\n this.handleRing1Message,\n this.handleRing2Message,\n this.handleRing3Message,\n ];\n\n this.resetSliceTimeReport();\n }\n\n /** @type {string} - debug string that characterizes sandbox. */\n get identifier() {\n if (!this.jobAddress) return `${this.id}.${this.state}`;\n return `${this.id}.${truncateAddress(this.jobAddress, addressTruncationLength)}.${this.state}`;\n }\n\n static getNewId() {\n return Sandbox.idCounter++;\n }\n\n /** @type {boolean} */\n get isReadyForAssign () {\n return this.state.is(READY_FOR_ASSIGN);\n }\n /** @type {boolean} */\n get isAssigned () {\n return this.state.is(ASSIGNED);\n }\n /** @type {boolean} */\n get isWorking () {\n return this.state.is(WORKING);\n }\n /** @type {boolean} */\n get isBroken () {\n return this.state.is(BROKEN);\n }\n /** @type {boolean} */\n get isTerminated () {\n return this.terminated;\n }\n\n /**\n * Mark WORKING sandbox as ASSIGNED in preparation for possible reuse,\n */\n changeWorkingToAssigned () {\n this.state.testAndSet(WORKING, ASSIGNED);\n }\n \n /**\n * Remove from collection of ASSIGNED sandboxes in preparation for termination,\n */\n markAsUnready() {\n this.state.testAndSet(ASSIGNED, UNREADY);\n }\n \n /**\n * Transitions: ASSIGNED --> WORKING and assigns the slice.\n * @param {Slice} slice \n */\n markAsWorking (slice) {\n if (!this.isAssigned)\n throw new Error(`Sandbox ${this.identifier} is not ready to work`);\n this.state.set(ASSIGNED, WORKING);\n this.slice = slice;\n }\n \n /**\n * Fancy assert.\n */\n verifyWorking () {\n if (!this.isWorking) {\n throw new Error(`Sandbox ${this.identifier} is not working`);\n }\n }\n\n /**\n * Error feedback to user.\n * @param {string} message\n * @param {*} extra\n */\n error(message, extra)\n {\n const dcpError = new DCPError(message, extra);\n this.emit('error', dcpError);\n }\n\n /**\n * Readies the sandbox. This will result in the sandbox being ready and not assigned,\n * it will need to be assigned with a job before it is able to do work.\n *\n * @todo maybe preload specific modules or let the cache pass in what modules to load?\n * @param {number} [delay=0]\n * @returns {Promise<Sandbox>}\n * @throws on failure to ready\n */\n async start(delay = 0) {\n this.state.set(UNREADY, READYING);\n\n if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay * timeDilation));\n\n try {\n // RING 0\n this.evaluatorHandle = new this.options.SandboxConstructor({\n name: `DCP Sandbox #${this.id}`,\n });\n this.evaluatorHandle.onerror = this.onerror.bind(this);\n\n const messageHandler = this.onmessage.bind(this);\n this.evaluatorHandle.onmessage = function onmessage(event)\n {\n let data;\n if (event.data.serialized)\n data = kvin.parse(event.data.message);\n else\n data = kvin.unmarshal(event.data);\n messageHandler({ data });\n }\n\n const evaluatorPostMessage = this.evaluatorHandle.postMessage.bind(this.evaluatorHandle);\n this.evaluatorHandle.postMessage = function postMessage(message)\n {\n evaluatorPostMessage(scopedKvin.marshal(message));\n }\n\n const that = this;\n this.evaluatorHandle.addEventListener('end', () => that.terminate(true));\n\n // Now in RING 1\n\n // Now in RING 2\n await this.describe();\n this.state.set(READYING, READY_FOR_ASSIGN);\n this.emit('ready', this);\n } catch (error) {\n this.error('Failed to start the sandbox.', error);\n this.state.set(READYING, BROKEN);\n this.terminate(false);\n throw error;\n }\n \n return this;\n }\n\n /**\n * This will assign the sandbox with a job, loading its sandbox code\n * into the sandbox.\n *\n * @param {JobManager} jobManager - The job manager that will be the owner of this sandbox.\n * @param {number} [delay=0]\n * @returns {Promise<Sandbox>}\n * @throws on initialization failure\n */\n async assign (jobManager, delay = 0) {\n \n if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay * timeDilation));\n \n this.state.set(READY_FOR_ASSIGN, ASSIGNING);\n this.jobAddress = jobManager.address;\n this.job = jobManager.jobMessage;\n \n assertEq3(this.job.address, this.jobAddress);\n assert(typeof this.job === 'object');\n assert(typeof this.job.requirements === 'object');\n assert(Array.isArray(this.job.dependencies));\n assert(Array.isArray(this.job.requirePath));\n\n // Extract public data from job, with defaults\n this.public = Object.assign({\n name: `Anonymous Job ${truncateAddress(this.jobAddress, addressTruncationLength)}`,\n description: 'Discreetly helping make the world smarter.',\n link: 'https://distributed.computer/about',\n }, this.job.public);\n\n // Future: We may want other filename tags for appliances // RR Nov 2019\n\n // Important: The order of applying requirements before loading the sandbox code\n // is important for modules and sandbox code to set globals over the whitelist.\n await this.applySandboxRequirements(this.job.requirements);\n await this.assignEvaluator();\n \n return this;\n }\n \n async assignEvaluator() {\n debug('Begin assigning job to evaluator');\n const that = this;\n\n return new Promise(function sandbox$$assignEvaluatorPromise(resolve, reject) {\n const message = {\n request: 'assign',\n job: that.job,\n sandboxConfig: workerTuning.sandbox,\n };\n\n /* note - onFailListener used for removal. This is necessary due to a bug in ee.once. /wg Feb 2022 */\n \n const onSuccess = (event) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('reject', onFailListener);\n that.emit('assigned', event.jobAddress); /** XXXpfr @todo Who is listening to event 'assigned' ? */\n debug('Job assigned to evaluator');\n resolve();\n };\n\n const onFail = (error) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('assigned', onSuccessListener);\n reject(error);\n };\n\n const onSuccessListener = that.ee.once('assigned', onSuccess); // Emitted by handleRing2Message.\n const onFailListener = that.ee.once('reject', onFail);\n that.evaluatorHandle.postMessage(message);\n }).then((event) => {\n that.state.set(ASSIGNING, ASSIGNED);\n return event;\n }).catch(error => {\n that.state.set(ASSIGNING, BROKEN);\n debug('Failure in assigning job to evaluator', error);\n throw error;\n });\n }\n\n /**\n * Evaluates a string inside the sandbox.\n *\n * @param {string} code - the code to evaluate in the sandbox\n * @param {string} filename - the name of the 'file' to help with debugging,\n * no longer working though?\n * @returns {Promise} - resolves with eval result on success, rejects\n * otherwise\n */\n eval(code, filename) {\n const that = this;\n \n return new Promise(function sandbox$$eval$Promise(resolve, reject) {\n let msgId = nanoid();\n let msg = {\n request: 'eval',\n data: code,\n filename,\n msgId, \n };\n\n const eventId = EVAL_RESULT_PREFIX + msgId;\n\n const onSuccess = (event) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener('reject', onFailListener);\n resolve(event);\n }\n\n const onFail = (error) => {\n // eslint-disable-next-line no-use-before-define\n that.ee.removeListener(eventId, onSuccessListener);\n reject(error);\n }\n\n const onSuccessListener = that.ee.once(eventId, onSuccess);\n const onFailListener = that.ee.once('reject', onFail);\n\n that.evaluatorHandle.postMessage(msg);\n })\n }\n\n /**\n * Resets the state of the bootstrap, without resetting the sandbox function if assigned.\n * Mostly used to reset the progress status before reusing a sandbox on another slice.\n * Must be called after @start.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n resetSandboxState () {\n const that = this;\n\n return new Promise(function sandbox$resetSandboxStatePromise(resolve, reject) {\n let successCb, failTimeout;\n let msg = {\n request: 'resetState',\n };\n\n successCb = that.ee.once('resetStateDone', function sandbox$resetSandboxState$success () {\n if (failTimeout === false)\n return; /* already rejected */\n clearTimeout(failTimeout);\n failTimeout = false;\n resolve();\n });\n\n failTimeout = setTimeout(function sandbox$resetSandboxState$fail() {\n if (failTimeout === false)\n return; /* already resolved */\n \n that.ee.removeListener('resetStateDone', successCb);\n that.terminate(false);\n failTimeout = false;\n\n reject(new Error(`resetState never received resetStateDone event from sandbox: ${that.identifier}`));\n }, 3000 * timeDilation); /* XXXwg need tuneable */\n\n if (that.evaluatorHandle) // Sometimes \"terminate\" nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(msg);\n });\n }\n\n /**\n * Clear all timers that are set inside the sandbox (evaluator) environment.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n clearSandboxTimers() {\n const that = this;\n \n return new Promise(function sandbox$clearSandboxTimersPromise(resolve, reject) {\n let successCb, failTimeout;\n let msg = {\n request: 'clearTimers',\n };\n\n successCb = that.ee.once('clearTimersDone', function sandbox$clearSandboxTimers$success() {\n if (failTimeout === false)\n return; /* already rejected */\n clearTimeout(failTimeout);\n failTimeout = false;\n resolve();\n });\n\n failTimeout = setTimeout(function sanbox$clearSandboxTimers$fail() {\n if (failTimeout === false)\n return; /* already resolved */\n \n that.ee.removeListener('clearTimersDone', successCb);\n that.terminate(false);\n failTimeout = false;\n \n reject(new Error(`clearTimers never received clearTimersDone event from sandbox: ${that.identifier}`));\n }, 3000 * timeDilation); /* XXXwg need tuneable */\n\n if (that.evaluatorHandle) // Sometimes \"terminate\" nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(msg);\n });\n }\n\n /**\n * Sends a post message to describe its capabilities.\n *\n * Side effect: Sets the capabilities property of the current sandbox.\n *\n * @returns {Promise} Resolves with the sandbox's capabilities. Rejects with\n * an error saying a response was not received.\n * @memberof Sandbox\n */\n describe() {\n debug('Beginning to describe evaluator');\n const that = this;\n \n return new Promise(function sandbox$describePromise(resolve, reject) {\n if (that.evaluatorHandle === null) {\n return reject(new Error(`Evaluator has not been initialized: ${that.identifier}`));\n }\n\n /**\n * Opted to create a flag for the describe response being received so that\n * we don't have to *hoist* the timeout's id to clear it in the response\n * handler.\n */\n let didReceiveDescribeResponse = false;\n const describeResponseHandler = that.ee.once('describe', (data) => { // Emitted by handleRing2Message.\n didReceiveDescribeResponse = true;\n const { capabilities } = data;\n if (typeof capabilities === 'undefined') {\n reject(new Error(`Did not receive capabilities from describe response: ${that.identifier}`));\n }\n that.capabilities = capabilities;\n\n // Currently only used in tests. May use the event in the future.\n that.emit('described', capabilities); /** XXXpfr @todo Who is listening to event 'described' ? */\n debug('Evaluator has been described');\n resolve(capabilities);\n });\n const describeResponseFailedHandler = () => {\n if (!didReceiveDescribeResponse) {\n that.ee.removeListener('describe', describeResponseHandler);\n that.terminate(false);\n reject(new Error( `Describe message timed-out. No describe response was received from the describe command: ${that.identifier}`));\n }\n };\n\n const message = {\n request: 'describe',\n };\n\n // Arbitrarily set the waiting time.\n setTimeout(describeResponseFailedHandler, 6000 * timeDilation); /* XXXwg need tuneable */\n if (that.evaluatorHandle) // Sometimes \"terminate\" nulls out evaluatorHandle before getting here.\n that.evaluatorHandle.postMessage(message);\n });\n }\n\n /**\n * Passes the job's requirements object into the sandbox so that the global\n * access lists can be updated accordingly.\n *\n * e.g. disallow access to OffscreenCanvas without\n * environment.offscreenCanvas=true present.\n *\n * Must be called after @start.\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n applySandboxRequirements(requirements) {\n const that = this;\n \n return new Promise(function sandbox$applySandboxRequirementsPromise(resolve, reject) {\n const message = {\n requirements,\n request: 'applyRequirements',\n };\n let wereRequirementsApplied = false;\n\n const successCb = that.ee.once(\n 'applyRequirementsDone',\n function sandbox$applyRequirements$success() {\n wereRequirementsApplied = true;\n resolve();\n },\n );\n\n assert(typeof message.requirements === 'object');\n that.evaluatorHandle.postMessage(message);\n\n setTimeout(function sandbox$finishApplySandboxRequirements() {\n if (!wereRequirementsApplied) {\n that.ee.removeListener('applyRequirementsDone', successCb);\n that.terminate(false);\n reject(new Error(`applyRequirements never received applyRequirementsDone response from sandbox: ${that.identifier}`));\n }\n }, 3000 * timeDilation); /* XXXwg needs tunable */\n });\n }\n\n /**\n * Executes a slice received from the supervisor.\n * Must be called after this.start, this.assign and this.markAsWorking .\n *\n * @param {number} [delay = 0] the delay that this method should wait before beginning work, used to avoid starting all sandboxes at once\n *\n * @returns {Promise} - resolves with result on success, rejects otherwise\n */\n\n async work (delay = 0) {\n const that = this;\n\n if (!this.slice)\n throw new Error(`The slice in Sandbox.work has not been set; be sure to call markAsWorking before: ${that.identifier}`);\n\n // cf. DCP-1720\n this.resetSliceTimeReport();\n \n // Now wait for the delay if provided, prevents many sandboxes starting at once from crashing the supervisor\n if (delay > 0) await new Promise(resolve => setTimeout(resolve, (delay + 1) * timeDilation));\n if (!this.isWorking) return; // sandbox.terminate could have been called during the delay timeout\n\n // Prepare the sandbox to begin work\n // will be replaced by `assign` message that should be called before emitting a `work` message\n if (this.jobAddress !== this.slice.jobAddress) {\n throw new Error(`Sandbox.run: Sandbox ${this.identifier} is already assigned and jobAddress doesn't match previous (${this.jobAddress} !== ${this.slice.jobAddress})`);\n }\n\n let sliceHnd = { job: this.public, sandbox: this };\n await this.resetSandboxState();\n if (!this.slice) {\n this.error(`Slice for job ${this.jobAddress} vanished during work initialization - aborting`);\n return;\n }\n\n const { datum: inputDatum, error: dataError } = this.slice;\n\n this.resetProgressTimeout();\n this.resetSliceTimeout();\n\n return new Promise(function sandbox$$workPromise(resolve, reject) {\n let onSuccess, onFail\n\n onSuccess = that.ee.once('resolve', function sandbox$$work$success (event) {\n that.ee.removeListener('reject', onFail)\n resolve(event)\n }.bind(that));\n\n onFail = that.ee.once('reject', function sandbox$$work$fail (err) {\n that.ee.removeListener('resolve', onSuccess)\n reject(err)\n }.bind(that))\n\n that.sliceStartTime = Date.now();\n that.progress = null;\n that.progressReports = {\n last: undefined,\n lastDeterministic: undefined,\n };\n\n that.resetProgressTimeout();\n that.resetSliceTimeout();\n that.emit('start', sliceHnd);\n \n if (dataError) {\n that.ee.removeListener('resolve', onSuccess);\n that.ee.removeListener('reject', onFail);\n that.emit('workEmit', {\n eventName: 'error',\n payload: {\n message: dataError.message,\n stack: dataError.stack,\n name: that.public.name\n }\n });\n setTimeout(() => reject(dataError), 0)\n\n } else {\n that.evaluatorHandle.postMessage({\n request: 'main',\n data: inputDatum,\n })\n }\n })\n .then(async function sandbox$$work$then(event) {\n // prevent any hanging timers from being fired\n await that.clearSandboxTimers();\n\n /** @todo Should sliceHnd just be replaced with that.public? */\n /** @todo Decide which event is right. */\n //that.emit('slice', sliceHnd); // Unused -- XXXpfr\n that.emit('sliceFinish', event);\n that.emit('complete', that.jobAddress);\n\n that.state.set(WORKING, ASSIGNED);\n that.slice = false;\n\n return event;\n })\n .catch((err) => { \n if (err.name === 'EWORKREJECT') {\n that.rejectionData = err;\n that.evaluatorHandle.postMessage({ request: 'resetAndGetCPUTime' })\n } else { // sandbox termination for rejected work happens in Supervisor.handleRejectedWork\n // Ceci is the reject callback for when the slice throws an error\n that.terminate(false);\n }\n\n that.emit('error', err, 'slice');\n\n if (err instanceof NoProgressError) {\n that.emit('workEmit', {\n eventName: 'noProgress',\n payload: {\n timestamp: Date.now() - that.sliceStartTime,\n data: that.slice.datumUri,\n progressReports: that.progressReports,\n }\n });\n }\n throw err;\n })\n .finally(function sandbox$$work$finally() {\n that.emit('end', sliceHnd); /** XXXpfr @todo Who is listening to event 'end' ? */\n });\n }\n\n resetProgressTimeout() {\n if (this.progressTimeout) {\n clearTimeout(this.progressTimeout);\n this.progressTimeout = null;\n }\n\n this.progressTimeout = setTimeout(() => {\n if (this.options.ignoreNoProgress)\n return this.emit('warning', \"ENOPROGRESS silenced by localExec: In a remote worker, this slice would be stopped for not calling progress frequently enough.\");\n\n this.ee.emit('reject', new NoProgressError(`No progress event was received in the last ${workerTuning.sandbox.progressTimeout / 1000} seconds.`));\n }, +workerTuning.sandbox.progressTimeout * timeDilation);\n }\n\n resetSliceTimeout() {\n if (this.sliceTimeout) clearTimeout(this.sliceTimeout);\n\n this.sliceTimeout = setTimeout(() => {\n if (Sandbox.debugWork) return this.emit('warning', 'Sandbox.debugWork: Ignoring slice timeout');\n\n this.ee.emit('reject', new SliceTooSlowError(`Slice took longer than ${workerTuning.sandbox.sliceTimeout / 1000} seconds.`));\n }, +workerTuning.sandbox.sliceTimeout * timeDilation);\n }\n \n async handleRing0Message(data) {\n debugging('event:ring-0') && debug('event:ring-0', data);\n //handling a true ring 0 message\n switch (data.request) {\n case 'sandboxLoaded':\n // emit externally\n this.emit('sandboxLoaded', this)\n break;\n\n case 'scriptLoaded':\n // emit externally\n this.emit('scriptLoaded', data);\n if(data.result !== \"success\") {\n this.onerror(data);\n }\n break;\n \n case 'clearTimersDone':\n this.ee.emit(data.request, data);\n break;\n case 'totalCPUTime':\n this.updateTime(data);\n if (this.ee.listenerCount('resolve') > 0) {\n this.completeData.timeReport = this.sliceTimeReport;\n this.ee.emit('resolve', this.completeData);\n delete this.completeData;\n } else {\n this.rejectionData.timeReport = this.sliceTimeReport\n this.emit('rejectedWorkMetrics', this.rejectionData) // If there is no internal listener for 'resolve', the slice was rejected and\n delete this.rejectionData; // we need to send the rejected metrics to the supervisor\n } \n break;\n case 'error': {\n // Warning: rejecting here with just event.data.error causes issues\n // where the reject handlers modify the object so it interferes with the\n // workEmit event payload, wrapping in an Error instance copies the values\n const wrappedError = this.fillInError(UncaughtExceptionError, data.error);\n\n if (this.ee.listenerCount('reject') > 0) {\n this.ee.emit('reject', wrappedError);\n } else {\n // This will happen if the error is thrown during initialization\n throw wrappedError;\n }\n break;\n }\n default: {\n this.error('Received unhandled request from sandbox: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break;\n } \n }\n }\n\n async handleRing1Message(data) {\n switch (data.request) {\n case 'applyRequirementsDone':\n // emit internally\n this.ee.emit(data.request, data)\n break;\n default: {\n this.error('Received unhandled request from sandbox ring 1: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break; \n }\n }\n }\n\n async handleRing2Message(data) {\n debugging('event:ring-2') && debug('event:ring-2', data);\n switch (data.request) {\n case 'dependency': {\n let moduleData;\n try {\n moduleData = await this.moduleCache.fetchModule(data.data, this.jobAddress);\n } catch (error) {\n /*\n * In the event of an error here, we want to let the client know there was a problem in\n * loading their module. However, there hasn't yet been an actual slice assigned to the sandbox.\n * Therefore, we assign 'slice 0' to the sandbox, a slice that will never exist, and is used\n * purely for this purpose. \n */\n this.slice = {\n jobAddress: this.jobAddress,\n sliceNumber: 0,\n };\n\n this.emit('workEmit', {\n eventName: 'error',\n payload: error,\n });\n this.ee.emit('reject', error);\n break;\n }\n this.evaluatorHandle.postMessage({\n request: 'moduleGroup',\n data: moduleData,\n id: data.id,\n });\n break;\n }\n case 'error':\n /*\n * Ring 2 error messages will only fire for problems inside of the worker that are separate from\n * the work function. In most cases there are other handlers for situations where 'error' may be emitted\n * such as timeouts if the expected message isn't recieved. Thus, we will output the error, but nothing else.\n */\n this.error('event:ring-2: some error happened.', data.error);\n break;\n case 'describe':\n case 'evalResult':\n case 'resetStateDone':\n case 'assigned':\n // emit internally\n this.ee.emit(data.request, data);\n break;\n case 'reject':\n // emit internally\n this.ee.emit(data.request, data.error);\n break;\n default: {\n this.error(`Received unhandled request from sandbox ring 2. Data: ${JSON.stringify(data, null, 2)}`);\n break;\n }\n }\n }\n\n async handleRing3Message(data) {\n switch (data.request) {\n case 'complete':\n clearTimeout(this.progressTimeout);\n clearTimeout(this.sliceTimeout);\n this.progressTimeout = this.sliceTimeout = null;\n\n if (this.progress === null) {\n if (this.options.ignoreNoProgress) {\n this.emit('warning', \"ENOPROGRESS silenced by localExec: Progress was not called during this slice's execution, in a remote sandbox this would cause the slice to be failed\");\n } else {\n // If a progress update was never received (progress === null) then reject\n this.ee.emit('reject', new NoProgressError('Sandbox never emitted a progress event.'));\n break;\n }\n }\n this.evaluatorHandle.postMessage({ request: 'resetAndGetCPUTime' })\n this.progress = 100;\n this.completeData = data;\n // The timing report and resolve will be emitted when the CPU time is received. \n break;\n case 'progress': {\n let { progress, indeterminate, throttledReports, value } = data;\n this.progress = progress;\n const progressReport = {\n timestamp: Date.now() - this.sliceStartTime,\n progress,\n value,\n throttledReports,\n }\n this.progressReports.last = progressReport;\n if (!indeterminate) {\n this.progressReports.lastDeterministic = progressReport;\n }\n\n this.resetProgressTimeout();\n this.emit('sliceProgress', data);\n break;\n }\n case 'noProgress':\n this.ee.emit('reject', new NoProgressError(data.message));\n break;\n case 'console':\n data.payload.message = scopedKvin.marshal(data.payload.message);\n this.emit('workEmit', {\n eventName: 'console',\n payload: data.payload,\n });\n break;\n case 'emitEvent':/* ad-hoc event from the sandbox (work.emit) */\n this.emit('workEmit', {\n eventName: 'custom',\n payload: data.payload\n });\n break;\n case 'measurement':\n this.updateTime(data);\n break;\n case 'sandboxError': /* the sandbox itself has an error condition */\n this.emit('sandboxError', data.error);\n break;\n case 'workError': { /* the work function threw/rejected */\n this.emit('workEmit', {\n eventName: 'error',\n payload: data.error,\n });\n\n // Warning: rejecting here with just .data.error causes issues\n // where the reject handlers modify the object so it interferes with the\n // workEmit payload, wrapping in an Error instance copies the values\n const wrappedError = this.fillInError(UncaughtExceptionError, data.error);\n\n if (this.ee.listenerCount('reject') > 0) {\n this.ee.emit('reject', wrappedError);\n } else {\n // This will happen if the error is thrown during initialization\n throw wrappedError;\n }\n break;\n }\n default: {\n this.error('Received unhandled request from sandbox ring 3: ' + data.request + '\\n\\t' + JSON.stringify(data));\n break; \n }\n }\n }\n\n /**\n * Handles progress and completion events from sandbox.\n * Unless explicitly returned out of this function will re-emit the event\n * on @this.ee where the name of the event is event.data.request.\n *\n * @param {object} event - event received from the sandbox\n */\n async onmessage(event) {\n debugging('event') && debug('event', event);\n if (Sandbox.debugEvents) {\n console.debug('sandbox - eventDebug:', {\n id: this.id,\n state: this.state.valueOf(),\n event: JSON.stringify(event)\n })\n }\n\n const { data } = event;\n const ringLevel = data.ringSource\n\n // Give the data to a handler depending on ring level\n if (ringLevel === -1) {\n this.error('Message sent directly from raw postMessage. Terminating worker...');\n console.debug(event);\n return this.terminate(true);\n } else {\n const handler = this.ringMessageHandlers[ringLevel];\n if (handler) {\n handler.call(this, data.value);\n } else {\n this.emit('warning', `No handler defined for message from ring ${ringLevel}`);\n console.debug(event);\n }\n }\n }\n\n /**\n * Error handler for the internal sandbox.\n * Emits error event that gets handled up in the Worker class.\n */\n onerror(event) {\n this.error('Sandbox emitted an error:', event);\n this.terminate(true, true);\n }\n\n /**\n * Clears the timeout and terminates the sandbox and sometimes emits a reject event.\n *\n * @param {boolean} [reject = true] - if true emit reject event\n * @param {boolean} [immediate = false] - passed to terminate, used by standaloneWorker to immediately close the connection\n */\n terminate (reject = true, immediate = false) {\n debugging() && console.debug(`Sandbox.terminate ${this.identifier}, count: ${++sbCnter}`);\n\n if (this.isTerminated) return;\n this.terminated = true;\n\n clearTimeout(this.progressTimeout);\n clearTimeout(this.sliceTimeout);\n this.progressTimeout = this.sliceTimeout = null;\n \n if (this.evaluatorHandle && typeof this.evaluatorHandle.terminate === 'function') {\n try {\n this.evaluatorHandle.terminate(immediate);\n this.evaluatorHandle = null;\n } catch (e) {\n this.error(`Error terminating sandbox ${this.id}:`, e);\n } finally {\n this.emit('terminate', this);\n }\n }\n\n if (reject) {\n this.ee.emit('reject', new Error(`Sandbox ${this.identifier} was terminated.`));\n }\n\n this.emit('terminated');\n }\n\n /**\n * Attempts to stop the sandbox from doing completing its current\n * set of work without terminating the working. \n * \n *** Until stop is implemented properly, use terminate(false).\n *\n * @todo Properly implement stop\n */\n stop () {\n //throw new Error('Sandbox.stop is not yet implemented.')\n this.terminate(false);\n }\n\n /**\n * ringNPostMessage can send a `measurement` request and update these\n * totals.\n */\n updateTime (measurementEvent) {\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (measurementEvent[key]) this.sliceTimeReport[key] += measurementEvent[key];\n })\n }\n\n /**\n * Start over sandbox work timers.\n */\n resetSliceTimeReport () {\n this.sliceTimeReport = {\n total: 0,\n CPU: 0,\n webGL: 0,\n }\n }\n\n /**\n * Clone the interesting part of an error.\n * @param {*} errorCtor\n * @param {Error} errorIn\n * @returns {Error}\n */\n fillInError(errorCtor, errorIn) {\n const errorOut = new errorCtor(errorIn.message);\n if (errorIn.name) errorOut.name = errorIn.name;\n if (errorIn.code) errorOut.code = errorIn.code;\n if (errorIn.fileName) errorOut.fileName = errorIn.fileName;\n if (errorIn.lineNumber) errorOut.lineNumber = errorIn.lineNumber;\n // Filter out the stack when we have more context\n if (errorIn.stack /*&& this.supervisor.debugBuild*/) errorOut.stack = errorIn.stack;\n\n return errorOut;\n }\n}\n\nSandbox.idCounter = 1;\nSandbox.debugWork = false;\nSandbox.debugState = false;\nSandbox.debugEvents = false;\n\nexports.Sandbox = Sandbox;\nexports.SandboxError = SandboxError;\nexports.NoProgressError = NoProgressError;\nexports.SliceTooSlowError = SliceTooSlowError;\nexports.UncaughtExceptionError = UncaughtExceptionError;\nexports.RemoteFetchError = RemoteFetchError;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/sandbox2.js?");
4583
4627
 
4584
4628
  /***/ }),
4585
4629
 
@@ -4590,7 +4634,7 @@ eval("/* eslint-disable require-await */\n// NOTE - need timeout/postmessage fun
4590
4634
  /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4591
4635
 
4592
4636
  "use strict";
4593
- eval("/**\n * @file dcp-client/worker/supervisor2/slice2.js\n *\n * A wrapper for the slice object returned from the scheduler's task distributor.\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Ryan Rossiter, ryan@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * May 2019\n * Decemeber 2020\n * June 2022\n * @module slice\n */\n\n// @ts-check\n\n\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { truncateAddress } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\nconst addressTruncationLength = 20;\n\nconst INITIAL = 'INITIAL';\nconst READY = 'READY';\nconst RESERVED = 'RESERVED';\nconst WORKING = 'WORKING';\nconst COMPLETE = 'COMPLETE';\nconst FAILED = 'FAILED';\nconst BROKEN = 'BROKEN';\nconst FINISHED = 'FINISHED';\n\n/** @typedef {import('./job-manager').JobManager} JobManager */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n/** @typedef {string} opaqueId */ // 22 character base64 string \n\n/** \n * Object use to represent a given slice inside the Supervisor. This object's shape \n * current inherits heavily from the message payload originating from the scheduler,\n * but should not /wg dec 2020.\n *\n * Caveat lector: this documentation was created without a 100% understanding of the code.\n * Please improve when possible.\n * \n * The read-only properties of this object are as follows:\n * - state INITIAL | READY | RESERVED | WORKING | COMPLETE | FAILED | BROKEN | FINISHED\n * - sliceNumber the number of the slice within this job\n * - jobAddress the address of the job that this slice belongs to\n * - isEstimation true when slice is used in estimation\n * - isLong true, when slice is estimated to take more than 300 seconds to execute\n * - datum input set element for this slice this slice of the job; could be a data: URI or it could\n * be a URL we need to fetch; note that fetches are limited to worker's allow list\n * - result\n * - result.request 'complete',...\n * - result.result return value from work function\n * - result.timeReport { total, idle } ms \n * - error error info when slice FAILED\n * - jobManager wrapper for the job that owns this slice\n * - resultStorageType 'values' => we are storing individual values,\n * which could be data: URIs or URLs\n * at the scheduler\n * 'pattern' => user-specified pattern for result URLs.\n * Data will be uploaded via POST to the\n * URL matching the pattern, provided the\n * worker is allowed to access it.\n * - resultStorageParams user-supplied POST parameters sent to the result \n * storage server when using resultStorageType = pattern.\n * - resultStorageDetails the pattern when using resultStorageType = pattern.\n * - authorizationMessage authorization from task distributor, sent to result submitter, etc...\n * \n * - isFinished true, when in state FINISHED <-- Indicates result submission has succeeded.\n * - hasFailed true, when in state FAILED, BROKEN\n * - isComplete true, when in state COMPLETED\n * - isReserved true, when in state RESERVED\n * - isActive true, when in state RESERVED, WORKING, COMPLETE\n * - isUnassigned true, when in state INITIAL\n * - isReady true, when in state READY\n * - isWorking true, when in state WORKING\n * - identifier string 'sliceNumber.jobAddress.state'\n * - timeReport accessor for this.result.timeReport that updates from this.rejectedTimeReport when appropriate\n *\n * The r/w properties of this object are as follows:\n * - startTime time when slice execution started\n * - rejectedTimeStamp slice has been rejected\n * - rejectedTimeReport rejected timeReport\n * \n * NOTE: If you ever use a property with a leading underscore you are probably making a mistake.\n * But if you must, please ask paul, yarn, bryan or eddie for a CR.\n */\nclass Slice\n{\n /**\n * @param {JobManager} jobManager\n * @param {SliceMessage} sliceMessage\n * @param {object} authorizationMessage\n */\n constructor (jobManager, sliceMessage, authorizationMessage)\n {\n /** @type {Synchronizer} */\n this._state = new Synchronizer(INITIAL, [ INITIAL, READY, RESERVED, WORKING, COMPLETE, FAILED, BROKEN, FINISHED ]);\n /** @type {JobManager} */\n this._jobManager = jobManager;\n /** @type {SliceMessage} */\n this._sliceMessage = { ...sliceMessage };\n this._authorizationMessage = authorizationMessage;\n this._datum = null;\n this._result = null;\n this._error = null;\n /** @type {number} */\n this.startTime = 0;\n /** @type {number} */\n this.rejectedTimeStamp = null;\n /** @type {{ total: number, CPU: number, webGL: number }} */\n this.rejectedTimeReport = null;\n \n assert(this.jobAddress === String(this._sliceMessage.jobAddress));\n if (!this.authorizationMessage && this.sliceNumber > 0)\n throw new Error(`Undefined authorization for slice ${this.identifier}.`);\n \n const that = this;\n \n /** \n * Start loading dependencies in the background. Once these are loaded, this.state will \n * transition to READY and the job will be ready to transition to WORKING.\n */\n (async function supervisor$$slice$$loadDatum() {\n ({ inputDatum: that._datum, dataError: that._error } = await that.jobManager.fetchSliceData(that.datumUri, that));\n }) (/* iife */)\n .then (() => {\n debugging('slice') && console.debug('Slice is transitioning to READY');\n this.state.set(INITIAL, READY);\n })\n .catch((error) => {\n debugging('slice') && console.debug('jobManager.fetchSliceData failed', error);\n this.state.set(INITIAL, BROKEN);\n })\n .finally(() => {\n debugging('slice') && console.debug('Slice.loadDatum completed.', this.identifier);\n });\n }\n\n /** @type {Synchronizer} */\n get state () { return this._state; }\n /** @type {number} */\n get sliceNumber () { return this._sliceMessage.sliceNumber; }\n /** @type {string} */\n get jobAddress () { return this._jobManager.address; }\n /** @type {boolean} */\n get isEstimation () { return this._sliceMessage.isEstimationSlice; }\n /** @type {boolean} */\n get isLong () { return this._sliceMessage.isLongSlice; }\n /** @type {string} */\n get datumUri () { return this._sliceMessage.datumUri; }\n /** @type {JobManager} */\n get jobManager () { return this._jobManager; }\n /** @type {string} */\n get resultStorageType () { return this._sliceMessage.resultStorageType; }\n /** @type {string} */\n get resultStorageDetails () { return this._sliceMessage.resultStorageDetails; }\n\n /** Read-only properties of type object. */\n get datum () { return this._datum; }\n get result () { return this._result; }\n get error () { return this._error; }\n get resultStorageParams () { return this._sliceMessage.resultStorageParams; }\n get authorizationMessage () { return this._authorizationMessage; }\n\n /** @type {boolean} */\n get isQueued () { return this.isUnassigned || this.isReady; }\n /** @type {boolean} */\n get isActive () { return this.isReserved || this.isWorking || this.isComplete; }\n /** @type {boolean} */\n get isFinished () { return this.state.is(FINISHED); }\n /** @type {boolean} */\n get hasFailed () { return this.state.is(FAILED) || this.state.is(BROKEN); }\n /** @type {boolean} */\n get isComplete () { return this.state.is(COMPLETE); }\n /** @type {boolean} */\n get isUnassigned () { return this.state.is(INITIAL); }\n /** @type {boolean} */\n get isReady () { return this.state.is(READY); }\n /** @type {boolean} */\n get isWorking () { return this.state.is(WORKING); }\n /**\n * Mark a slice as RESERVED to remove it from the ready list, yet still able to transition to WORKING.\n * @type {boolean}\n **/\n get isReserved () { return this.state.is(RESERVED); }\n\n // TEMPORARY: THIS IS ONLY USED FOR COMPATIBILITY WITH SUPERVISOR1.\n // WHEN SUPERVISOR1 IS PUT TO BED, REMOVE THESE TWO FUNCTIONS.\n /** DO NOT USE! @deprecated @type {boolean} */\n get failed () { return this.hasFailed; }\n /** DO NOT USE! @deprecated @type {boolean} */\n get completed () { return this.isComplete; }\n \n /** @type {string} */\n get identifier () { return `${this.sliceNumber}.${truncateAddress(this.jobAddress, addressTruncationLength)}.${this.state}`; }\n /** \n * timeReport accessor that optionally updates from this.rejectedTimeReport. \n * @type { { total, CPU, webGL } }\n **/\n get timeReport ()\n {\n if (this.result.timeReport && this.rejectedTimeReport && this.rejectedTimeReport.total > 0)\n {\n // Data collected from sandboxes that rejected this slice.\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (this.rejectedTimeReport[key]) this.result.timeReport[key] += this.rejectedTimeReport[key];\n });\n this.rejectedTimeReport = null;\n }\n return this.result.timeReport;\n }\n /**\n * Return the time interval to estimated slice completion time.\n * @type {number}\n **/\n get etaMs ()\n {\n if (this.startTime === null) return 0;\n let etaMs = this.jobManager.estimateWallMs;\n if (this.startTime) etaMs -= (Date.now() - this.startTime);\n return etaMs;\n }\n\n /** Start slice over, regardless of what state it is in. */\n resetState()\n {\n if (this.isReady || this.isUnassigned) return;\n this._state = new Synchronizer(READY, [ INITIAL, READY, RESERVED, WORKING, COMPLETE, FAILED, BROKEN, FINISHED ]);\n }\n\n /** \n * Sets the slice status to RESERVED, called to remove slice from the ready list,\n * yet still able to transition to WORKING.\n **/\n markAsReserved() { this.state.set(READY, RESERVED); }\n\n /** Sets the slice status to WORKING, called when the slice is getting ready to be handed to a worker. */\n markAsWorking() { this.state.set(RESERVED, WORKING); }\n\n /** Sets the slice status to FINISHED, called when the slice has completed and submitted results. */\n markAsFinished() { this.state.set(COMPLETE, FINISHED); }\n\n /**\n * Verifies slice status is working and assign sandbox, called when the slice is handed to a worker.\n */\n verifyWorking()\n {\n if (!this.isWorking) {\n throw new Error(`Slice ${this.identifier} is not working.`);\n }\n }\n\n /**\n * Receives a result from the scheduler.\n * It will then put the result in the appropriate place.\n * It could also eventually determine if the slice should be\n * retried before determining that it has failed.\n *\n * @param {object|Error} result - The result that came back from the worker sandbox.\n * @param {boolean} [success=true] - True if result is considered successful, false if error occurred.\n */\n collectResult(result, success = true)\n {\n if (this.result)\n throw new Error(`Slice ${this.identifier} received more than one result.`);\n \n if (success)\n {\n this.state.set(WORKING, COMPLETE);\n this._result = result;\n }\n else\n {\n this.state.set(WORKING, FAILED);\n this._error = result;\n }\n debugging('slice') && console.debug('collectResult', this.identifier);\n }\n\n /**\n * Create basic message object as part of the payload to send back to the result-submitter's status operation.\n * @param {string} status - The kind of status operation\n * @param {object} [extraProperties={}] - Extra properties for the paylaod object.\n * @returns {object}\n */\n getMessage(status, extraProperties = {})\n {\n return {\n sliceNumbers: [this.sliceNumber],\n job: this.jobAddress,\n authorizationMessage: this.authorizationMessage,\n status,\n ...extraProperties,\n }; \n }\n\n /**\n * Create basic payload object to send back to the result-submitter's status operation.\n * @param {opaqueId} worker - The current worker's opaqueId\n * @param {string} status - The kind of status operation\n * @param {object} [extraProperties={}] - Extra properties for the paylaod object.\n * @returns {object}\n */\n getMessagePayload(worker, status, extraProperties = {})\n {\n return {\n worker,\n slices: [ this.getMessage(status, extraProperties) ],\n }; \n }\n\n /**\n * Create slice-return payload object to send to the result-submitter's status operation.\n * @param {opaqueId} worker - The current worker's opaqueId\n * @param {string} [reason] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @return {object}\n */\n getReturnMessagePayload(worker, reason)\n {\n delete this._result;\n\n if (!reason) reason = this.error ? 'EUNCAUGHT' : 'unknown';\n const extraProperties = {\n isEstimationSlice: this.isEstimation,\n error: this.error,\n reason,\n };\n\n return this.getMessagePayload(worker, 'return', extraProperties);\n }\n\n /**\n * @deprecated -- DO NOT USE!\n * TEMPORARY: THIS IS ONLY USED FOR COMPATIBILITY WITH SUPERVISOR1.\n * WHEN SUPERVISOR1 IS PUT TO BED, REMOVE THIS FUNCTION.\n * This function helps enforce the equivalence:\n * !this.authorizationMessage <==> sliceNumber === 0 .\n * @returns {object} this.authorizationMessage\n */\n getAuthorizationMessage () {\n if (!this.authorizationMessage && this.sliceNumber > 0)\n throw new Error(`Undefined authorization for slice ${this.identifier}.`);\n return this.authorizationMessage;\n }\n}\nexports.Slice = Slice;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/slice2.js?");
4637
+ eval("/**\n * @file dcp-client/worker/supervisor2/slice2.js\n *\n * A wrapper for the slice object returned from the scheduler's task distributor.\n *\n * @author Matthew Palma, mpalma@kingsds.network\n * Ryan Rossiter, ryan@kingsds.network\n * Wes Garland, wes@kingsds.network\n * Paul, paul@kingsds.network\n * @date May 2019\n * May 2019\n * Decemeber 2020\n * June 2022\n * @module slice\n */\n\n// @ts-check\n\n\nconst { Synchronizer } = __webpack_require__(/*! dcp/common/concurrency */ \"./src/common/concurrency.js\");\nconst { assert } = __webpack_require__(/*! dcp/common/dcp-assert */ \"./src/common/dcp-assert.js\");\nconst debugging = (__webpack_require__(/*! dcp/debugging */ \"./src/debugging.js\").scope)('worker');\nconst { truncateAddress } = __webpack_require__(/*! dcp/utils */ \"./src/utils/index.js\");\n\nconst addressTruncationLength = 20;\n\nconst INITIAL = 'INITIAL';\nconst READY = 'READY';\nconst RESERVED = 'RESERVED';\nconst WORKING = 'WORKING';\nconst COMPLETE = 'COMPLETE';\nconst FAILED = 'FAILED';\nconst BROKEN = 'BROKEN';\nconst FINISHED = 'FINISHED';\n\n/** @typedef {import('./job-manager').JobManager} JobManager */\n/** @typedef {import('dcp/utils').SliceMessage} SliceMessage */\n/** @typedef {string} opaqueId */ // 22 character base64 string \n\n/** \n * Object use to represent a given slice inside the Supervisor. This object's shape \n * current inherits heavily from the message payload originating from the scheduler,\n * but should not /wg dec 2020.\n *\n * Caveat lector: this documentation was created without a 100% understanding of the code.\n * Please improve when possible.\n * \n * The read-only properties of this object are as follows:\n * - state INITIAL | READY | RESERVED | WORKING | COMPLETE | FAILED | BROKEN | FINISHED\n * - sliceNumber the number of the slice within this job\n * - jobAddress the address of the job that this slice belongs to\n * - isEstimation true when slice is used in estimation\n * - isLong true, when slice is estimated to take more than 300 seconds to execute\n * - datum input set element for this slice this slice of the job; could be a data: URI or it could\n * be a URL we need to fetch; note that fetches are limited to worker's allow list\n * - result\n * - result.request 'complete',...\n * - result.result return value from work function\n * - result.timeReport { total, idle } ms \n * - error error info when slice FAILED\n * - jobManager wrapper for the job that owns this slice\n * - resultStorageType 'values' => we are storing individual values,\n * which could be data: URIs or URLs\n * at the scheduler\n * 'pattern' => user-specified pattern for result URLs.\n * Data will be uploaded via POST to the\n * URL matching the pattern, provided the\n * worker is allowed to access it.\n * - resultStorageParams user-supplied POST parameters sent to the result \n * storage server when using resultStorageType = pattern.\n * - resultStorageDetails the pattern when using resultStorageType = pattern.\n * - authorizationMessage authorization from task distributor, sent to result submitter, etc...\n * \n * - isFinished true, when in state FINISHED <-- Indicates result submission has succeeded.\n * - hasFailed true, when in state FAILED, BROKEN\n * - isComplete true, when in state COMPLETED\n * - isReserved true, when in state RESERVED\n * - isActive true, when in state RESERVED, WORKING, COMPLETE\n * - isUnassigned true, when in state INITIAL\n * - isReady true, when in state READY\n * - isWorking true, when in state WORKING\n * - identifier string 'sliceNumber.jobAddress.state'\n * - timeReport accessor for this.result.timeReport that updates from this.rejectedTimeReport when appropriate\n *\n * The r/w properties of this object are as follows:\n * - startTime time when slice execution started\n * - rejectedTimeStamp slice has been rejected\n * - rejectedTimeReport rejected timeReport\n * \n * NOTE: If you ever use a property with a leading underscore you are probably making a mistake.\n * But if you must, please ask paul, yarn, bryan or eddie for a CR.\n */\nclass Slice\n{\n /**\n * @param {JobManager} jobManager\n * @param {SliceMessage} sliceMessage\n * @param {object} authorizationMessage\n */\n constructor (jobManager, sliceMessage, authorizationMessage)\n {\n /** @type {Synchronizer} */\n this._state = new Synchronizer(INITIAL, [ INITIAL, READY, RESERVED, WORKING, COMPLETE, FAILED, BROKEN, FINISHED ]);\n /** @type {JobManager} */\n this._jobManager = jobManager;\n /** @type {SliceMessage} */\n this._sliceMessage = { ...sliceMessage };\n this._authorizationMessage = authorizationMessage;\n this._datum = null;\n this._result = null;\n this._error = null;\n /** @type {number} */\n this.startTime = 0;\n /** @type {number} */\n this.rejectedTimeStamp = null;\n /** @type {{ total: number, CPU: number, webGL: number }} */\n this.rejectedTimeReport = null;\n \n assert(this.jobAddress === String(this._sliceMessage.jobAddress));\n if (!this.authorizationMessage && this.sliceNumber > 0)\n throw new Error(`Undefined authorization for slice ${this.identifier}.`);\n \n const that = this;\n \n /** \n * Start loading dependencies in the background. Once these are loaded, this.state will \n * transition to READY and the job will be ready to transition to WORKING.\n */\n (async function supervisor$$slice$$loadDatum() {\n ({ inputDatum: that._datum, dataError: that._error } = await that.jobManager.fetchSliceData(that.datumUri, that));\n }) (/* iife */)\n .then (() => {\n debugging('slice') && console.debug('Slice is transitioning to READY');\n this.state.set(INITIAL, READY);\n })\n .catch((error) => {\n debugging('slice') && console.debug('jobManager.fetchSliceData failed', error);\n this.state.set(INITIAL, BROKEN);\n })\n .finally(() => {\n debugging('slice') && console.debug('Slice.loadDatum completed.', this.identifier);\n });\n }\n\n /** @type {Synchronizer} */\n get state () { return this._state; }\n /** @type {number} */\n get sliceNumber () { return this._sliceMessage.sliceNumber; }\n /** @type {string} */\n get jobAddress () { return this._jobManager.address; }\n /** @type {boolean} */\n get isEstimation () { return this._sliceMessage.isEstimationSlice; }\n /** @type {boolean} */\n get isLong () { return this._sliceMessage.isLongSlice; }\n /** @type {string} */\n get datumUri () { return this._sliceMessage.datumUri; }\n /** @type {JobManager} */\n get jobManager () { return this._jobManager; }\n /** @type {string} */\n get resultStorageType () { return this._sliceMessage.resultStorageType; }\n /** @type {string} */\n get resultStorageDetails () { return this._sliceMessage.resultStorageDetails; }\n\n /** Read-only properties of type object. */\n get datum () { return this._datum; }\n get result () { return this._result; }\n get error () { return this._error; }\n get resultStorageParams () { return this._sliceMessage.resultStorageParams; }\n get authorizationMessage () { return this._authorizationMessage; }\n\n /** @type {boolean} */\n get isQueued () { return this.isUnassigned || this.isReady; }\n /** @type {boolean} */\n get isActive () { return this.isReserved || this.isWorking || this.isComplete; }\n /** @type {boolean} */\n get isFinished () { return this.state.is(FINISHED); }\n /** @type {boolean} */\n get hasFailed () { return this.state.is(FAILED) || this.state.is(BROKEN); }\n /** @type {boolean} */\n get isComplete () { return this.state.is(COMPLETE); }\n /** @type {boolean} */\n get isUnassigned () { return this.state.is(INITIAL); }\n /** @type {boolean} */\n get isReady () { return this.state.is(READY); }\n /** @type {boolean} */\n get isWorking () { return this.state.is(WORKING); }\n /**\n * Mark a slice as RESERVED to remove it from the ready list, yet still able to transition to WORKING.\n * @type {boolean}\n **/\n get isReserved () { return this.state.is(RESERVED); }\n\n /** @type {string} */\n get identifier () { return `${this.sliceNumber}.${truncateAddress(this.jobAddress, addressTruncationLength)}.${this.state}`; }\n /** \n * timeReport accessor that optionally updates from this.rejectedTimeReport. \n * @type { { total, CPU, webGL } }\n **/\n get timeReport ()\n {\n if (this.result.timeReport && this.rejectedTimeReport && this.rejectedTimeReport.total > 0)\n {\n // Data collected from sandboxes that rejected this slice.\n ['total', 'CPU', 'webGL'].forEach((key) => {\n if (this.rejectedTimeReport[key]) this.result.timeReport[key] += this.rejectedTimeReport[key];\n });\n this.rejectedTimeReport = null;\n }\n return this.result.timeReport;\n }\n /**\n * Return the time interval to estimated slice completion time.\n * @type {number}\n **/\n get etaMs ()\n {\n if (this.startTime === null) return 0;\n let etaMs = this.jobManager.estimateWallMs;\n if (this.startTime) etaMs -= (Date.now() - this.startTime);\n return etaMs;\n }\n\n /** Start slice over, regardless of what state it is in. */\n resetState()\n {\n if (this.isReady || this.isUnassigned) return;\n this._state = new Synchronizer(READY, [ INITIAL, READY, RESERVED, WORKING, COMPLETE, FAILED, BROKEN, FINISHED ]);\n }\n\n /** \n * Sets the slice status to RESERVED, called to remove slice from the ready list,\n * yet still able to transition to WORKING.\n **/\n markAsReserved() { this.state.set(READY, RESERVED); }\n\n /** Sets the slice status to WORKING, called when the slice is getting ready to be handed to a worker. */\n markAsWorking() { this.state.set(RESERVED, WORKING); }\n\n /** Sets the slice status to FINISHED, called when the slice has completed and submitted results. */\n markAsFinished() { this.state.set(COMPLETE, FINISHED); }\n\n /**\n * Verifies slice status is working and assign sandbox, called when the slice is handed to a worker.\n */\n verifyWorking()\n {\n if (!this.isWorking) {\n throw new Error(`Slice ${this.identifier} is not working.`);\n }\n }\n\n /**\n * Receives a result from the scheduler.\n * It will then put the result in the appropriate place.\n * It could also eventually determine if the slice should be\n * retried before determining that it has failed.\n *\n * @param {object|Error} result - The result that came back from the worker sandbox.\n * @param {boolean} [success=true] - True if result is considered successful, false if error occurred.\n */\n collectResult(result, success = true)\n {\n if (this.result)\n throw new Error(`Slice ${this.identifier} received more than one result.`);\n \n if (success)\n {\n this.state.set(WORKING, COMPLETE);\n this._result = result;\n }\n else\n {\n this.state.set(WORKING, FAILED);\n this._error = result;\n }\n debugging('slice') && console.debug('collectResult', this.identifier);\n }\n\n /**\n * Create basic message object as part of the payload to send back to the result-submitter's status operation.\n * @param {string} status - The kind of status operation\n * @param {object} [extraProperties={}] - Extra properties for the paylaod object.\n * @returns {object}\n */\n getMessage(status, extraProperties = {})\n {\n return {\n sliceNumbers: [this.sliceNumber],\n job: this.jobAddress,\n authorizationMessage: this.authorizationMessage,\n status,\n ...extraProperties,\n }; \n }\n\n /**\n * Create basic payload object to send back to the result-submitter's status operation.\n * @param {opaqueId} worker - The current worker's opaqueId\n * @param {string} status - The kind of status operation\n * @param {object} [extraProperties={}] - Extra properties for the paylaod object.\n * @returns {object}\n */\n getMessagePayload(worker, status, extraProperties = {})\n {\n return {\n worker,\n slices: [ this.getMessage(status, extraProperties) ],\n }; \n }\n\n /**\n * Create slice-return payload object to send to the result-submitter's status operation.\n * @param {opaqueId} worker - The current worker's opaqueId\n * @param {string} [reason] - Optional reason for the return: 'ENOPROGRESS', 'EUNCAUGHT', 'ESLICETOOSLOW', 'unknown'.\n * @return {object}\n */\n getReturnMessagePayload(worker, reason)\n {\n delete this._result;\n\n if (!reason) reason = this.error ? 'EUNCAUGHT' : 'unknown';\n const extraProperties = {\n isEstimationSlice: this.isEstimation,\n error: this.error,\n reason,\n };\n\n return this.getMessagePayload(worker, 'return', extraProperties);\n }\n}\nexports.Slice = Slice;\n\n\n//# sourceURL=webpack://dcp/./src/dcp-client/worker/supervisor2/slice2.js?");
4594
4638
 
4595
4639
  /***/ }),
4596
4640
 
@@ -5779,7 +5823,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexpo
5779
5823
  /***/ ((module, __unused_webpack_exports, __webpack_require__) => {
5780
5824
 
5781
5825
  "use strict";
5782
- eval("\n\nvar GetIntrinsic = __webpack_require__(/*! get-intrinsic */ \"./node_modules/get-intrinsic/index.js\");\n\nvar $gOPD = GetIntrinsic('%Object.getOwnPropertyDescriptor%', true);\nif ($gOPD) {\n\ttry {\n\t\t$gOPD([], 'length');\n\t} catch (e) {\n\t\t// IE 8 has a broken gOPD\n\t\t$gOPD = null;\n\t}\n}\n\nmodule.exports = $gOPD;\n\n\n//# sourceURL=webpack://dcp/./node_modules/es-abstract/helpers/getOwnPropertyDescriptor.js?");
5826
+ eval("\n\n// TODO: remove, semver-major\n\nmodule.exports = __webpack_require__(/*! gopd */ \"./node_modules/gopd/index.js\");\n\n\n//# sourceURL=webpack://dcp/./node_modules/es-abstract/helpers/getOwnPropertyDescriptor.js?");
5783
5827
 
5784
5828
  /***/ }),
5785
5829