@coherentglobal/spark-execute-sdk 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -808,9 +808,8 @@ var unzipBuffer = function unzipBuffer(model) {
808
808
  path: dataPath
809
809
  };
810
810
  case 15:
811
- console.log("RES: ", response);
812
811
  return _context4.abrupt("return", response);
813
- case 17:
812
+ case 16:
814
813
  case "end":
815
814
  return _context4.stop();
816
815
  }
@@ -933,6 +932,30 @@ var getRuntimeContent = function getRuntimeContent(js, wasm) {
933
932
  }, _callee9);
934
933
  }));
935
934
  };
935
+ var defaultCallback = function defaultCallback() {
936
+ return __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
937
+ return _regenerator["default"].wrap(function _callee10$(_context10) {
938
+ while (1) {
939
+ switch (_context10.prev = _context10.next) {
940
+ case 0:
941
+ console.log("CALLBACK NOT IMPLEMENTED");
942
+ case 1:
943
+ case "end":
944
+ return _context10.stop();
945
+ }
946
+ }
947
+ }, _callee10);
948
+ }));
949
+ };
950
+ var safeJsonParser = function safeJsonParser(data) {
951
+ try {
952
+ var result = JSON.parse(data);
953
+ return result;
954
+ } catch (err) {
955
+ console.log('SAFE JSON PARSER FAILED: ', err);
956
+ return undefined;
957
+ }
958
+ };
936
959
  /**
937
960
  * WASM runner config
938
961
  * @typedef {Object} RunnerConfig
@@ -959,9 +982,12 @@ var WasmRunner = /*#__PURE__*/function () {
959
982
  */
960
983
  function WasmRunner() {
961
984
  var license = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
985
+ var callback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultCallback;
962
986
  (0, _classCallCheck2["default"])(this, WasmRunner);
963
987
  utils.isCompatible();
988
+ console.log('CALLBACK: ', callback);
964
989
  this.license = license;
990
+ this.callback = callback;
965
991
  this.models = [];
966
992
  }
967
993
  /**
@@ -974,97 +1000,146 @@ var WasmRunner = /*#__PURE__*/function () {
974
1000
  key: "initialize",
975
1001
  value: function initialize() {
976
1002
  var license = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
977
- return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
1003
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
978
1004
  var _this = this;
979
- return _regenerator["default"].wrap(function _callee11$(_context11) {
1005
+ return _regenerator["default"].wrap(function _callee12$(_context12) {
980
1006
  while (1) {
981
- switch (_context11.prev = _context11.next) {
1007
+ switch (_context12.prev = _context12.next) {
982
1008
  case 0:
983
1009
  if (!utils.isEmpty(license)) {
984
1010
  this.license = license;
985
1011
  }
986
- _context11.next = 3;
1012
+ _context12.next = 3;
987
1013
  return Promise.all(this.models.map(function (m) {
988
- return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
989
- return _regenerator["default"].wrap(function _callee10$(_context10) {
1014
+ return __awaiter(_this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
1015
+ return _regenerator["default"].wrap(function _callee11$(_context11) {
990
1016
  while (1) {
991
- switch (_context10.prev = _context10.next) {
1017
+ switch (_context11.prev = _context11.next) {
992
1018
  case 0:
993
- return _context10.abrupt("return", this._initializeModelInstance(m, this.license));
1019
+ return _context11.abrupt("return", this._initializeModelInstance(m, this.license));
994
1020
  case 1:
995
1021
  case "end":
996
- return _context10.stop();
1022
+ return _context11.stop();
997
1023
  }
998
1024
  }
999
- }, _callee10, this);
1025
+ }, _callee11, this);
1000
1026
  }));
1001
1027
  }));
1002
1028
  case 3:
1003
1029
  case "end":
1004
- return _context11.stop();
1030
+ return _context12.stop();
1005
1031
  }
1006
1032
  }
1007
- }, _callee11, this);
1033
+ }, _callee12, this);
1008
1034
  }));
1009
1035
  }
1010
1036
  }, {
1011
1037
  key: "_initializeModelInstance",
1012
1038
  value: function _initializeModelInstance(m, license) {
1013
- return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
1039
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee14() {
1014
1040
  var modelUrl, isDataBuffer, isObject, js, wasm, data, bufferModel, unzipModelFile, unzipped, runtime;
1015
- return _regenerator["default"].wrap(function _callee12$(_context12) {
1041
+ return _regenerator["default"].wrap(function _callee14$(_context14) {
1016
1042
  while (1) {
1017
- switch (_context12.prev = _context12.next) {
1043
+ switch (_context14.prev = _context14.next) {
1018
1044
  case 0:
1019
1045
  modelUrl = m.url;
1020
1046
  isDataBuffer = (0, is_buffer_1["default"])(modelUrl);
1021
1047
  isObject = (0, _typeof2["default"])(modelUrl) === "object";
1022
1048
  console.log("IS BUFFER: ", isDataBuffer);
1023
1049
  if (!isDataBuffer) {
1024
- _context12.next = 13;
1050
+ _context14.next = 13;
1025
1051
  break;
1026
1052
  }
1027
- _context12.next = 7;
1053
+ _context14.next = 7;
1028
1054
  return unzipBuffer(modelUrl);
1029
1055
  case 7:
1030
- bufferModel = _context12.sent;
1056
+ bufferModel = _context14.sent;
1031
1057
  js = bufferModel.js;
1032
1058
  wasm = bufferModel.wasm;
1033
1059
  data = bufferModel.data;
1034
- _context12.next = 29;
1060
+ _context14.next = 29;
1035
1061
  break;
1036
1062
  case 13:
1037
1063
  if (!isObject) {
1038
- _context12.next = 23;
1064
+ _context14.next = 23;
1039
1065
  break;
1040
1066
  }
1041
- _context12.next = 16;
1067
+ _context14.next = 16;
1042
1068
  return unzipppedModel(modelUrl);
1043
1069
  case 16:
1044
- unzipModelFile = _context12.sent;
1070
+ unzipModelFile = _context14.sent;
1045
1071
  js = unzipModelFile.js;
1046
1072
  wasm = unzipModelFile.wasm;
1047
1073
  data = unzipModelFile.data;
1048
1074
  console.log("----MODEL PATH is an OBJECT----");
1049
- _context12.next = 29;
1075
+ _context14.next = 29;
1050
1076
  break;
1051
1077
  case 23:
1052
- _context12.next = 25;
1078
+ _context14.next = 25;
1053
1079
  return unzipFile(modelUrl);
1054
1080
  case 25:
1055
- unzipped = _context12.sent;
1081
+ unzipped = _context14.sent;
1056
1082
  js = unzipped.js;
1057
1083
  wasm = unzipped.wasm;
1058
1084
  data = unzipped.data;
1059
1085
  case 29:
1060
- _context12.next = 31;
1086
+ _context14.next = 31;
1061
1087
  return getRuntimeContent(js, wasm, data);
1062
1088
  case 31:
1063
- runtime = _context12.sent;
1089
+ runtime = _context14.sent;
1090
+ m.parents = this;
1064
1091
  m.worker = new Worker(utils.jsString2workerURL((0, template_1["default"])(runtime, license)));
1065
1092
  m.worker.onmessage = function (oEvent) {
1093
+ var _this2 = this;
1094
+ var _a;
1066
1095
  if (!Array.isArray(oEvent.data) && oEvent.data === "Initialized") {
1067
1096
  m.ready = true;
1097
+ } else if (!Array.isArray(oEvent.data) && ((_a = oEvent.data) === null || _a === void 0 ? void 0 : _a.type) === "stall") {
1098
+ var requestData = JSON.parse(oEvent.data.requestData.request_body);
1099
+ var folderName = oEvent.data.requestData.folder_name;
1100
+ var serviceName = oEvent.data.requestData.service_name;
1101
+ var response = new Promise(function (resolve, reject) {
1102
+ return __awaiter(_this2, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee13() {
1103
+ var option, modelVersionId, results;
1104
+ return _regenerator["default"].wrap(function _callee13$(_context13) {
1105
+ while (1) {
1106
+ switch (_context13.prev = _context13.next) {
1107
+ case 0:
1108
+ _context13.prev = 0;
1109
+ // const modelVersionId = "d18c6f24-92ac-454e-a4db-02233389fe7e"
1110
+ option = {
1111
+ folder_name: folderName,
1112
+ service_name: serviceName
1113
+ // version_id: versionId
1114
+ };
1115
+ _context13.next = 4;
1116
+ return m.parents.callback(option);
1117
+ case 4:
1118
+ modelVersionId = _context13.sent;
1119
+ _context13.next = 7;
1120
+ return m.parents.execute(requestData, modelVersionId);
1121
+ case 7:
1122
+ results = _context13.sent;
1123
+ m.worker.postMessage([{
1124
+ request: oEvent.data.requestData,
1125
+ response: results
1126
+ }, "", "dispatch"]);
1127
+ resolve(results);
1128
+ _context13.next = 16;
1129
+ break;
1130
+ case 12:
1131
+ _context13.prev = 12;
1132
+ _context13.t0 = _context13["catch"](0);
1133
+ console.log('CHILD MODEL ERROR:', _context13.t0);
1134
+ reject(_context13.t0);
1135
+ case 16:
1136
+ case "end":
1137
+ return _context13.stop();
1138
+ }
1139
+ }
1140
+ }, _callee13, null, [[0, 12]]);
1141
+ }));
1142
+ });
1068
1143
  } else if (Array.isArray(oEvent.data)) {
1069
1144
  if (utils.isEmpty(m.results)) {
1070
1145
  m.results = {};
@@ -1072,7 +1147,7 @@ var WasmRunner = /*#__PURE__*/function () {
1072
1147
  m.results[oEvent.data[1]] = oEvent.data[0];
1073
1148
  }
1074
1149
  };
1075
- return _context12.abrupt("return", new Promise(function (resolve, reject) {
1150
+ return _context14.abrupt("return", new Promise(function (resolve, reject) {
1076
1151
  var checkModelStatus = function checkModelStatus() {
1077
1152
  setTimeout(function () {
1078
1153
  if (!m.ready) {
@@ -1084,12 +1159,12 @@ var WasmRunner = /*#__PURE__*/function () {
1084
1159
  };
1085
1160
  checkModelStatus();
1086
1161
  }));
1087
- case 35:
1162
+ case 36:
1088
1163
  case "end":
1089
- return _context12.stop();
1164
+ return _context14.stop();
1090
1165
  }
1091
1166
  }
1092
- }, _callee12);
1167
+ }, _callee14, this);
1093
1168
  }));
1094
1169
  }
1095
1170
  }, {
@@ -1144,7 +1219,6 @@ var WasmRunner = /*#__PURE__*/function () {
1144
1219
  var model = this.models.find(function (m) {
1145
1220
  return m.id === id;
1146
1221
  });
1147
- console.log("BROWSER MODEL FOUND: ", Object.keys(model));
1148
1222
  if (!model) throw new error_1["default"].MissingModelError(id);
1149
1223
  if (!model.ready) throw new error_1["default"].ModelInitializationError(id);
1150
1224
  return model;
@@ -1173,10 +1247,10 @@ var WasmRunner = /*#__PURE__*/function () {
1173
1247
  }, {
1174
1248
  key: "remove",
1175
1249
  value: function remove(id) {
1176
- return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee13() {
1177
- return _regenerator["default"].wrap(function _callee13$(_context13) {
1250
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee15() {
1251
+ return _regenerator["default"].wrap(function _callee15$(_context15) {
1178
1252
  while (1) {
1179
- switch (_context13.prev = _context13.next) {
1253
+ switch (_context15.prev = _context15.next) {
1180
1254
  case 0:
1181
1255
  if (this.isExist(id)) {
1182
1256
  this.models = this.models.filter(function (m) {
@@ -1185,10 +1259,10 @@ var WasmRunner = /*#__PURE__*/function () {
1185
1259
  }
1186
1260
  case 1:
1187
1261
  case "end":
1188
- return _context13.stop();
1262
+ return _context15.stop();
1189
1263
  }
1190
1264
  }
1191
- }, _callee13, this);
1265
+ }, _callee15, this);
1192
1266
  }));
1193
1267
  }
1194
1268
  /**
@@ -1199,32 +1273,33 @@ var WasmRunner = /*#__PURE__*/function () {
1199
1273
  }, {
1200
1274
  key: "append",
1201
1275
  value: function append(modelConfig) {
1202
- return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee14() {
1276
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee16() {
1203
1277
  var model;
1204
- return _regenerator["default"].wrap(function _callee14$(_context14) {
1278
+ return _regenerator["default"].wrap(function _callee16$(_context16) {
1205
1279
  while (1) {
1206
- switch (_context14.prev = _context14.next) {
1280
+ switch (_context16.prev = _context16.next) {
1207
1281
  case 0:
1208
1282
  if (!this.isExist(modelConfig.id)) {
1209
- _context14.next = 2;
1283
+ _context16.next = 2;
1210
1284
  break;
1211
1285
  }
1212
- return _context14.abrupt("return");
1286
+ return _context16.abrupt("return");
1213
1287
  case 2:
1214
1288
  model = {
1215
1289
  id: modelConfig.id,
1216
- url: modelConfig.url
1290
+ url: modelConfig.url,
1291
+ meta: {}
1217
1292
  };
1218
- _context14.next = 5;
1293
+ _context16.next = 5;
1219
1294
  return this._initializeModelInstance(model, this.license);
1220
1295
  case 5:
1221
1296
  this.models.push(model);
1222
1297
  case 6:
1223
1298
  case "end":
1224
- return _context14.stop();
1299
+ return _context16.stop();
1225
1300
  }
1226
1301
  }
1227
- }, _callee14, this);
1302
+ }, _callee16, this);
1228
1303
  }));
1229
1304
  }
1230
1305
  /**
@@ -1238,30 +1313,45 @@ var WasmRunner = /*#__PURE__*/function () {
1238
1313
  value: function execute(input) {
1239
1314
  var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined;
1240
1315
  var _a, _b;
1241
- return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee15() {
1316
+ return __awaiter(this, void 0, void 0, /*#__PURE__*/_regenerator["default"].mark(function _callee17() {
1242
1317
  var version_uuid, model, callid;
1243
- return _regenerator["default"].wrap(function _callee15$(_context15) {
1318
+ return _regenerator["default"].wrap(function _callee17$(_context17) {
1244
1319
  while (1) {
1245
- switch (_context15.prev = _context15.next) {
1320
+ switch (_context17.prev = _context17.next) {
1246
1321
  case 0:
1247
1322
  console.log("EXECUTING: ".concat(id, ", input: ").concat(JSON.stringify(input)));
1248
1323
  version_uuid = id || ((_a = input.request_meta) === null || _a === void 0 ? void 0 : _a.version_id) || ((_b = input.request_meta) === null || _b === void 0 ? void 0 : _b.version_uuid);
1249
1324
  if (!version_uuid) {
1250
- _context15.next = 7;
1325
+ _context17.next = 7;
1251
1326
  break;
1252
1327
  }
1253
1328
  model = this._getModel(version_uuid);
1254
1329
  callid = (0, cuid_1["default"])();
1255
1330
  model.worker.postMessage([JSON.stringify(input), callid]);
1256
- return _context15.abrupt("return", new Promise(function (resolve, reject) {
1331
+ return _context17.abrupt("return", new Promise(function (resolve, reject) {
1257
1332
  var counter = 0;
1258
1333
  var getResult = function getResult() {
1259
1334
  setTimeout(function () {
1260
1335
  var _a, _b;
1336
+ // try {
1337
+ // const result = JSON.parse(model.results?.[callid]);
1338
+ // } catch (err) {
1339
+ // console.log('JSON PARSE ERR: ', err)
1340
+ // }
1261
1341
  if ((_a = model.results) === null || _a === void 0 ? void 0 : _a[callid]) {
1262
- var result = JSON.parse((_b = model.results) === null || _b === void 0 ? void 0 : _b[callid]);
1263
- model.results[callid] = undefined;
1264
- resolve(result);
1342
+ var tryParsingResult = safeJsonParser((_b = model.results) === null || _b === void 0 ? void 0 : _b[callid]);
1343
+ // const result = JSON.parse(model.results?.[callid]);
1344
+ if (tryParsingResult) {
1345
+ model.results[callid] = undefined;
1346
+ resolve(tryParsingResult);
1347
+ } else {
1348
+ if (counter < 100) {
1349
+ counter++;
1350
+ getResult();
1351
+ } else {
1352
+ reject(new Error("Execution timeout"));
1353
+ }
1354
+ }
1265
1355
  } else {
1266
1356
  if (counter < 100) {
1267
1357
  counter++;
@@ -1278,10 +1368,10 @@ var WasmRunner = /*#__PURE__*/function () {
1278
1368
  throw new error_1["default"].ParameterRequiredError("request_meta.version_id", input);
1279
1369
  case 8:
1280
1370
  case "end":
1281
- return _context15.stop();
1371
+ return _context17.stop();
1282
1372
  }
1283
1373
  }
1284
- }, _callee15, this);
1374
+ }, _callee17, this);
1285
1375
  }));
1286
1376
  }
1287
1377
  }]);
@@ -1297,7 +1387,7 @@ Object.defineProperty(exports, "__esModule", {
1297
1387
  });
1298
1388
  function buildWorkerFN(runtime) {
1299
1389
  var license = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
1300
- return "\n ".concat(runtime, "\n\n const license = '").concat(license, "';\n\n let wasm,wb;function moduleInitialize(){Module().then((e=>{wasm=e,wb=e._construct(),postMessage(\"Initialized\")}))}moduleInitialize(),onmessage=function(e){const a=e.data[0],s=e.data[1];if(void 0===wasm||void 0===wb)return new Error(\"WASM is not ready\");const t=(new TextEncoder).encode(a),n=wasm._malloc(t.length),o=t.BYTES_PER_ELEMENT;wasm.HEAP8.set(t,n/o);const w=wasm._malloc(8),i=wasm._node_calc_v3(wb,n,t.length,w),m=wasm.getValue(w,\"i64\"),r=new TextDecoder(\"utf-8\"),c=new Uint8Array(wasm.HEAPU8.subarray(i,i+m)),d=r.decode(c);wasm._free(n),wasm._free(w),wasm._free(i),postMessage([d,s])};\n\n");
1390
+ return "\n ".concat(runtime, "\n\n const license = '").concat(license, "';\n class MyEventEmitter{constructor(){this._events={}}on(e,t){this._events[e]||(this._events[e]=[]),this._events[e].push(t)}removeListener(e,t){if(!this._events[e]){throw new Error(\"Can't emit an event. Event doesn't exits.\"+e)}this._events[e]=this._events[e].filter((e=>e!==t))}emit(e,t){if(!this._events[e]){throw new Error(\"Can't emit an event. Event doesn't exits.\"+e)}this._events[e].forEach((e=>{e(t)}))}}const workerEventEmitter=new MyEventEmitter;let wasm,wb;const CALL_TYPE={0:\"SparkService\",1:\"ExternalApi\"},XcallResBuilder={Json:function(e,t,s,a,n,r){return JSON.stringify({metadata:{calltype:e,name:t,status:s,error_code:a,response_time:n},data:r||{}})},XML:()=>{}};function moduleInitialize(){Module().then((e=>{wasm=e,wb=e._construct(),console.log(\"ASYNC FLAG: \",e._is_async),console.log(\"ASYNC FLAG CALL: \",e._is_async(wb));var t=new TextDecoder(\"utf-8\");new TextEncoder;const s=0;e.dispatch=async(a,n,r)=>{const c=new Uint8Array(e.HEAPU8.subarray(n,n+r)),o=t.decode(c).slice(),i=JSON.parse(o),m=(i.folder_name,i.service_name);if(a===s){return await new Promise((async(e,t)=>{postMessage({requestData:i,type:\"stall\"});const s=\"xcall-loopback-result-\"+m;workerEventEmitter.on(s,(async t=>{const s=XcallResBuilder.Json(CALL_TYPE[0],m,202,\"\",0,JSON.stringify(t));e(s)}))}))}},postMessage(\"Initialized\")}))}moduleInitialize(),onmessage=async function(e){const t=e.data[0];e.data[1];if(\"dispatch\"===e.data[2]){t.request.folder_name;const e=t.request.service_name,s=t.response,a=\"xcall-loopback-result-\"+e;workerEventEmitter.emit(a,s)}else{const t=await runComputation(e);postMessage(t)}};const runComputation=async e=>{const t=e.data[0],s=e.data[1];if(void 0===wasm||void 0===wb)return new Error(\"WASM is not ready\");t?.request_meta?._ctx?.authorization&&(ctx.headers.authorization=t?.request_meta?._ctx?.authorization,delete t?.request_meta?._ctx?.authorization),t?.request_meta?._ctx?.secretkey&&(ctx.headers.secretkey=t?.request_meta?._ctx.secretkey,delete t?.request_meta?._ctx?.secretkey),t?.request_meta?._ctx?.[\"x-synthetic-key\"]&&(ctx.headers[\"x-synthetic-key\"]=t?.request_meta?._ctx?.[\"x-synthetic-key\"],delete t?.request_meta?._ctx?.[\"x-synthetic-key\"]),t?.request_meta?._ctx?.tenant&&(ctx.headers.tenant=t?.request_meta?._ctx?.tenant,delete t?.request_meta?._ctx?.tenant),t?.request_meta?._ctx?.servicemap&&(ctx.servicemap=t?.request_meta?._ctx?.servicemap,delete t?.request_meta?._ctx.servicemap);const a=(new TextEncoder).encode(t),n=wasm._malloc(a.length),r=a.BYTES_PER_ELEMENT;wasm.HEAP8.set(a,n/r);const c=wasm._malloc(8),o=await wasm.ccall(\"node_calc_v3\",\"number\",[\"number\",\"number\",\"number\",\"number\"],[wb,n,a.length,c],{async:!0});var i=wasm.getValue(c,\"i64\");const m=new Uint8Array(wasm.HEAPU8.subarray(o,o+i));const _=new TextDecoder(\"utf-8\").decode(m).slice();return wasm._free(n),wasm._free(c),wasm._free(o),[_,s]};\n\n");
1301
1391
  }
1302
1392
  exports["default"] = buildWorkerFN;
1303
1393
 
@@ -3803,19 +3893,9 @@ exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
3803
3893
  * @license MIT
3804
3894
  */
3805
3895
 
3806
- // The _isBuffer check is for Safari 5-7 support, because it's missing
3807
- // Object.prototype.constructor. Remove this eventually
3808
- module.exports = function (obj) {
3809
- return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer)
3810
- }
3811
-
3812
- function isBuffer (obj) {
3813
- return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
3814
- }
3815
-
3816
- // For Node v0.10 support. Remove this eventually.
3817
- function isSlowBuffer (obj) {
3818
- return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0))
3896
+ module.exports = function isBuffer (obj) {
3897
+ return obj != null && obj.constructor != null &&
3898
+ typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
3819
3899
  }
3820
3900
 
3821
3901
  },{}],31:[function(require,module,exports){
@@ -6210,6 +6290,16 @@ var Spark = /*#__PURE__*/function () {
6210
6290
  if (!model) throw new WasmRunnerErrors.MissingModelError(id);
6211
6291
  return model;
6212
6292
  }
6293
+ }, {
6294
+ key: "_getModelByMeta",
6295
+ value: function _getModelByMeta(folderName, serviceName) {
6296
+ var model = this.model.find(function (m) {
6297
+ return (m === null || m === void 0 ? void 0 : m.folder_name) === folderName && (m === null || m === void 0 ? void 0 : m.service_name) === serviceName;
6298
+ });
6299
+ console.log('MODEL FOUND VIA FOLDER: ', model);
6300
+ if (!model) throw new WasmRunnerErrors.MissingModelError(serviceName);
6301
+ return model;
6302
+ }
6213
6303
 
6214
6304
  /**
6215
6305
  * Execute model
@@ -6221,40 +6311,41 @@ var Spark = /*#__PURE__*/function () {
6221
6311
  }, {
6222
6312
  key: "execute",
6223
6313
  value: function () {
6224
- var _execute = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(input, version_id) {
6225
- var versionID, model, modelUrl, token, header, options, response, result;
6226
- return _regenerator["default"].wrap(function _callee$(_context) {
6314
+ var _execute = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(input, version_id) {
6315
+ var _this = this;
6316
+ var versionID, model, modelUrl, token, header, options, response, callback, _model2$folder_name, _model2$service_name, _model2, result;
6317
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
6227
6318
  while (1) {
6228
- switch (_context.prev = _context.next) {
6319
+ switch (_context2.prev = _context2.next) {
6229
6320
  case 0:
6230
6321
  versionID = version_id || input.request_meta.version_id || input.request_meta.version_uuid;
6231
6322
  model = this._getModel(versionID);
6232
6323
  if (!input) {
6233
- _context.next = 46;
6324
+ _context2.next = 47;
6234
6325
  break;
6235
6326
  }
6236
6327
  modelUrl = "";
6237
6328
  if (!(!this.isCompatible && !utils.isEmpty(this.url) && this.fallbackEnabled)) {
6238
- _context.next = 34;
6329
+ _context2.next = 34;
6239
6330
  break;
6240
6331
  }
6241
6332
  token = "";
6242
6333
  if (!(this.token.constructor.name === "AsyncFunction")) {
6243
- _context.next = 12;
6334
+ _context2.next = 12;
6244
6335
  break;
6245
6336
  }
6246
- _context.next = 9;
6337
+ _context2.next = 9;
6247
6338
  return this.token();
6248
6339
  case 9:
6249
- token = _context.sent;
6250
- _context.next = 13;
6340
+ token = _context2.sent;
6341
+ _context2.next = 13;
6251
6342
  break;
6252
6343
  case 12:
6253
6344
  token = this.token;
6254
6345
  case 13:
6255
6346
  console.log("TOKEN", token);
6256
6347
  if (!utils.isEmpty(this.token)) {
6257
- _context.next = 16;
6348
+ _context2.next = 16;
6258
6349
  break;
6259
6350
  }
6260
6351
  throw new WasmRunnerErrors.UnauthorizedError();
@@ -6271,55 +6362,114 @@ var Spark = /*#__PURE__*/function () {
6271
6362
  }), header)
6272
6363
  };
6273
6364
  modelUrl = new URL("/".concat(this.tenant, "/api/v3/version/").concat(input.request_meta.version_id), this.url);
6274
- _context.prev = 19;
6365
+ _context2.prev = 19;
6275
6366
  console.log("URL", modelUrl);
6276
- _context.next = 23;
6367
+ _context2.next = 23;
6277
6368
  return fetch(modelUrl, options);
6278
6369
  case 23:
6279
- response = _context.sent;
6280
- _context.next = 26;
6370
+ response = _context2.sent;
6371
+ _context2.next = 26;
6281
6372
  return response.json();
6282
6373
  case 26:
6283
- return _context.abrupt("return", _context.sent);
6374
+ return _context2.abrupt("return", _context2.sent);
6284
6375
  case 29:
6285
- _context.prev = 29;
6286
- _context.t0 = _context["catch"](19);
6376
+ _context2.prev = 29;
6377
+ _context2.t0 = _context2["catch"](19);
6287
6378
  throw new WasmRunnerErrors.UnauthorizedError();
6288
6379
  case 32:
6289
- _context.next = 46;
6380
+ _context2.next = 47;
6290
6381
  break;
6291
6382
  case 34:
6292
6383
  if (!this.isCompatible) {
6293
- _context.next = 45;
6384
+ _context2.next = 46;
6294
6385
  break;
6295
6386
  }
6296
6387
  if (!(registry instanceof WasmRunner)) {
6297
- registry = new WasmRunner();
6388
+ // callback
6389
+ callback = /*#__PURE__*/function () {
6390
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(options) {
6391
+ var hasFolderName, hasServiceName, hasVersionId, _model$folder_name, _model$service_name, _model$folder_name2, _model$service_name2, _model;
6392
+ return _regenerator["default"].wrap(function _callee$(_context) {
6393
+ while (1) {
6394
+ switch (_context.prev = _context.next) {
6395
+ case 0:
6396
+ hasFolderName = options === null || options === void 0 ? void 0 : options.folder_name;
6397
+ hasServiceName = options === null || options === void 0 ? void 0 : options.service_name;
6398
+ hasVersionId = options === null || options === void 0 ? void 0 : options.version_id;
6399
+ if (!(hasVersionId && !registry.isExist(hasVersionId))) {
6400
+ _context.next = 8;
6401
+ break;
6402
+ }
6403
+ _context.next = 6;
6404
+ return registry.append({
6405
+ id: hasVersionId,
6406
+ url: model.binary,
6407
+ meta: {
6408
+ folder_name: (_model$folder_name = model === null || model === void 0 ? void 0 : model.folder_name) !== null && _model$folder_name !== void 0 ? _model$folder_name : "",
6409
+ service_name: (_model$service_name = model === null || model === void 0 ? void 0 : model.service_name) !== null && _model$service_name !== void 0 ? _model$service_name : ""
6410
+ }
6411
+ });
6412
+ case 6:
6413
+ _context.next = 15;
6414
+ break;
6415
+ case 8:
6416
+ console.log('NEW MODEL FINDER FOLDER: ', hasFolderName);
6417
+ console.log('NEW MODEL FINDER hasServiceName: ', hasServiceName);
6418
+ _model = _this._getModelByMeta(hasFolderName, hasServiceName);
6419
+ console.log('NEW MODEL FINDER: ', _model.versionId);
6420
+ hasVersionId = _model === null || _model === void 0 ? void 0 : _model.versionId;
6421
+ _context.next = 15;
6422
+ return registry.append({
6423
+ id: hasVersionId,
6424
+ url: _model.binary,
6425
+ meta: {
6426
+ folder_name: (_model$folder_name2 = _model === null || _model === void 0 ? void 0 : _model.folder_name) !== null && _model$folder_name2 !== void 0 ? _model$folder_name2 : "",
6427
+ service_name: (_model$service_name2 = _model === null || _model === void 0 ? void 0 : _model.service_name) !== null && _model$service_name2 !== void 0 ? _model$service_name2 : ""
6428
+ }
6429
+ });
6430
+ case 15:
6431
+ return _context.abrupt("return", hasVersionId);
6432
+ case 16:
6433
+ case "end":
6434
+ return _context.stop();
6435
+ }
6436
+ }
6437
+ }, _callee);
6438
+ }));
6439
+ return function callback(_x3) {
6440
+ return _ref.apply(this, arguments);
6441
+ };
6442
+ }();
6443
+ registry = new WasmRunner("", callback);
6298
6444
  }
6299
6445
  if (registry.isExist(versionID)) {
6300
- _context.next = 39;
6446
+ _context2.next = 40;
6301
6447
  break;
6302
6448
  }
6303
- _context.next = 39;
6449
+ _model2 = this._getModel(versionID);
6450
+ _context2.next = 40;
6304
6451
  return registry.append({
6305
6452
  id: versionID,
6306
- url: model.binary
6307
- // size,
6453
+ url: _model2.binary,
6454
+ meta: {
6455
+ folder_name: (_model2$folder_name = _model2 === null || _model2 === void 0 ? void 0 : _model2.folder_name) !== null && _model2$folder_name !== void 0 ? _model2$folder_name : "",
6456
+ service_name: (_model2$service_name = _model2 === null || _model2 === void 0 ? void 0 : _model2.service_name) !== null && _model2$service_name !== void 0 ? _model2$service_name : ""
6457
+ }
6308
6458
  });
6309
- case 39:
6310
- _context.next = 41;
6311
- return registry.execute(input, versionID);
6312
- case 41:
6313
- result = _context.sent;
6314
- return _context.abrupt("return", result);
6315
- case 45:
6316
- throw new WasmRunnerErrors.NotSupportedError("WebAssembly is not supported");
6459
+ case 40:
6460
+ _context2.next = 42;
6461
+ return registry.execute(input, versionID, function () {});
6462
+ case 42:
6463
+ result = _context2.sent;
6464
+ return _context2.abrupt("return", result);
6317
6465
  case 46:
6466
+ throw new WasmRunnerErrors.NotSupportedError("WebAssembly is not supported");
6467
+ case 47:
6318
6468
  case "end":
6319
- return _context.stop();
6469
+ return _context2.stop();
6320
6470
  }
6321
6471
  }
6322
- }, _callee, this, [[19, 29]]);
6472
+ }, _callee2, this, [[19, 29]]);
6323
6473
  }));
6324
6474
  function execute(_x, _x2) {
6325
6475
  return _execute.apply(this, arguments);
@@ -6629,7 +6779,9 @@ var rules = Joi.object({
6629
6779
  js: Joi.string(),
6630
6780
  data: Joi.string()
6631
6781
  })],
6632
- metaData: Joi.object()
6782
+ metaData: Joi.object(),
6783
+ folder_name: Joi.string().allow(""),
6784
+ service_name: Joi.string().allow("")
6633
6785
  })).required()
6634
6786
  // xParameters: Joi.array().items(),
6635
6787
  // xCallCallback: Joi.func()