@balancy/wasm 1.0.39 → 1.0.41

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/index.umd.js CHANGED
@@ -1197,17 +1197,18 @@
1197
1197
  });
1198
1198
  }
1199
1199
  }
1200
- function js_fetch(url, bLoadFile, method, headers, headersCount, body, successCallbackPtr, errorCallbackPtr, callbackPtr, binaryCallbackPtr) {
1200
+ function js_fetch(url, bLoadFile, method, headers, headersCount, body, successCallbackPtr, errorCallbackPtr, callbackPtr, binaryCallbackPtr, requestContextPtr) {
1201
+ var requestId = Date.now() + Math.random();
1201
1202
  var urlString = "";
1202
1203
  var methodString = "";
1203
1204
  var bodyString = null;
1204
1205
  try {
1205
1206
  if (!url) {
1206
- console.error(" URL pointer is null");
1207
+ console.error("[ERROR] [Request ".concat(requestId, "] URL pointer is null"));
1207
1208
  return;
1208
1209
  }
1209
1210
  if (!method) {
1210
- console.error(" Method pointer is null");
1211
+ console.error("[ERROR] [Request ".concat(requestId, "] Method pointer is null"));
1211
1212
  return;
1212
1213
  }
1213
1214
  urlString = UTF8ToString(url);
@@ -1216,51 +1217,80 @@
1216
1217
  bodyString = UTF8ToString(body);
1217
1218
  }
1218
1219
  if (!urlString || urlString.length === 0) {
1219
- console.error(" URL is empty or invalid");
1220
+ console.error("[ERROR] [Request ".concat(requestId, "] URL is empty or invalid"));
1220
1221
  return;
1221
1222
  }
1222
1223
  if (urlString.includes("\0") || urlString.includes("€")) {
1223
- console.error(" URL contains invalid characters:", urlString);
1224
+ console.error("[ERROR] [Request ".concat(requestId, "] URL contains invalid characters:"), urlString);
1224
1225
  return;
1225
1226
  }
1226
1227
  } catch (error) {
1227
- console.error(" Error reading strings from WASM memory:", error);
1228
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading strings from WASM memory:"), error);
1228
1229
  return;
1229
1230
  }
1230
- var invokeErrorCallback = function(status, message, callbackPtr) {
1231
- console.log("❌ invokeErrorCallback:", status, message);
1231
+ var invokeErrorCallback = function(status, message) {
1232
+ var details = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "";
1233
+ var fullMessage = "[Request ".concat(requestId, "] ").concat(bLoadFile ? "FILE LOAD" : "API REQUEST", " FAILED: ").concat(methodString, " ").concat(urlString).concat(details ? " - " + details : "");
1234
+ console.error("[ERROR] ".concat(fullMessage));
1235
+ console.error(" Status: ".concat(status));
1236
+ console.error(" Error: ".concat(message || "Unknown error"));
1237
+ if (bodyString) {
1238
+ console.error(" Request Body: ".concat(bodyString.length > 500 ? bodyString.substring(0, 500) + "..." : bodyString));
1239
+ }
1232
1240
  try {
1233
- var errorMessage = message || "Unknown error";
1234
- var errorLength = lengthBytesUTF8(errorMessage) + 1;
1235
- var errorPtr = Module._malloc(errorLength);
1236
- stringToUTF8(errorMessage, errorPtr, errorLength);
1241
+ if (requestContextPtr) {
1242
+ Module._free(requestContextPtr);
1243
+ }
1244
+ if (!errorCallbackPtr || !callbackPtr) {
1245
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid callback pointers: errorCallback=").concat(errorCallbackPtr, ", callback=").concat(callbackPtr));
1246
+ return;
1247
+ }
1248
+ var errorMessage = fullMessage + (message ? " - ".concat(message) : "");
1249
+ var errorLength = lengthBytesUTF8(errorMessage);
1250
+ if (errorLength <= 0 || errorLength > 1048576) {
1251
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid error message length: ").concat(errorLength));
1252
+ return;
1253
+ }
1254
+ var errorPtr = Module._malloc(errorLength + 1);
1255
+ if (!errorPtr) {
1256
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate memory for error message"));
1257
+ return;
1258
+ }
1259
+ stringToUTF8(errorMessage, errorPtr, errorLength + 1);
1260
+ if (typeof Module["dynCall_viii"] !== "function") {
1261
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available"));
1262
+ Module._free(errorPtr);
1263
+ return;
1264
+ }
1237
1265
  Module["dynCall_viii"](errorCallbackPtr, status, errorPtr, callbackPtr);
1238
1266
  Module._free(errorPtr);
1239
1267
  } catch (error) {
1240
- console.error(" Error in error callback:", error);
1268
+ console.error("[ERROR] [Request ".concat(requestId, "] Error in error callback:"), error);
1241
1269
  }
1242
1270
  };
1243
1271
  var headersObj = new Headers;
1244
1272
  var ptrSize = Module.HEAPU32.BYTES_PER_ELEMENT;
1273
+ var headersLogged = [];
1245
1274
  try {
1246
1275
  for(var i = 0; i < headersCount; i += 2){
1247
1276
  var keyPtr = getValue(headers + i * ptrSize, "*");
1248
1277
  var valuePtr = getValue(headers + (i + 1) * ptrSize, "*");
1249
1278
  if (!keyPtr || !valuePtr) {
1250
- console.warn("⚠️ Skipping invalid header pointer at index", i);
1279
+ console.warn("[WARN] [Request ".concat(requestId, "] Skipping invalid header pointer at index"), i);
1251
1280
  continue;
1252
1281
  }
1253
1282
  var key = UTF8ToString(keyPtr);
1254
1283
  var value = UTF8ToString(valuePtr);
1255
1284
  if (!key || !value) {
1256
- console.warn("⚠️ Skipping empty header at index", i);
1285
+ console.warn("[WARN] [Request ".concat(requestId, "] Skipping empty header at index"), i);
1257
1286
  continue;
1258
1287
  }
1259
1288
  headersObj.append(key, value);
1289
+ headersLogged.push("".concat(key, ": ").concat(value));
1260
1290
  }
1261
1291
  } catch (error) {
1262
- console.error(" Error processing headers:", error);
1263
- invokeErrorCallback(-1, "Header processing error: " + error.message, callbackPtr);
1292
+ console.error("[ERROR] [Request ".concat(requestId, "] Error processing headers:"), error);
1293
+ invokeErrorCallback(-1, "Header processing error: " + error.message, "");
1264
1294
  return;
1265
1295
  }
1266
1296
  var fetchOptions = {
@@ -1268,15 +1298,17 @@
1268
1298
  headers: headersObj,
1269
1299
  body: bodyString
1270
1300
  };
1301
+ var startTime = performance.now();
1271
1302
  fetch(urlString, fetchOptions).then(function(response) {
1303
+ performance.now() - startTime;
1272
1304
  if (!response.ok) {
1273
- console.log(" Response not OK, reading error message");
1305
+ console.error("[ERROR] [Request ".concat(requestId, "] Response not OK, status: ").concat(response.status, " ").concat(response.statusText));
1274
1306
  response.text().then(function(errorMessage) {
1275
- console.log(" Error message:", errorMessage);
1276
- invokeErrorCallback(response.status, errorMessage, callbackPtr);
1307
+ console.error(" Response Body: ".concat(errorMessage));
1308
+ invokeErrorCallback(response.status, errorMessage, "HTTP ".concat(response.status, " ").concat(response.statusText));
1277
1309
  }).catch(function() {
1278
- console.log(" Failed to read error message");
1279
- invokeErrorCallback(response.status, null, callbackPtr);
1310
+ console.error(" Failed to read error response body");
1311
+ invokeErrorCallback(response.status, "HTTP ".concat(response.status, " ").concat(response.statusText), "Could not read response body");
1280
1312
  });
1281
1313
  return;
1282
1314
  }
@@ -1324,30 +1356,87 @@
1324
1356
  if (isBinary) {
1325
1357
  response.arrayBuffer().then(function(arrayBuffer) {
1326
1358
  var dataSize = arrayBuffer.byteLength;
1359
+ if (dataSize <= 0 || dataSize > 104857600) {
1360
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid binary data size: ").concat(dataSize));
1361
+ invokeErrorCallback(-1, "Invalid binary data size: ".concat(dataSize), "");
1362
+ return;
1363
+ }
1327
1364
  var dataPtr = Module._malloc(dataSize);
1365
+ if (!dataPtr) {
1366
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate ").concat(dataSize, " bytes for binary data"));
1367
+ invokeErrorCallback(-1, "Memory allocation failed for binary data", "");
1368
+ return;
1369
+ }
1328
1370
  var dataView = new Uint8Array(arrayBuffer);
1329
1371
  Module.HEAPU8.set(dataView, dataPtr);
1330
- Module["dynCall_viii"](binaryCallbackPtr, dataPtr, dataSize, callbackPtr);
1372
+ try {
1373
+ if (!binaryCallbackPtr || !callbackPtr) {
1374
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid binary callback pointers: binary=").concat(binaryCallbackPtr, ", callback=").concat(callbackPtr));
1375
+ Module._free(dataPtr);
1376
+ return;
1377
+ }
1378
+ if (typeof Module["dynCall_viii"] !== "function") {
1379
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available for binary callback"));
1380
+ Module._free(dataPtr);
1381
+ return;
1382
+ }
1383
+ Module["dynCall_viii"](binaryCallbackPtr, dataPtr, dataSize, callbackPtr);
1384
+ if (requestContextPtr) {
1385
+ Module._free(requestContextPtr);
1386
+ }
1387
+ } catch (error) {
1388
+ console.error("[ERROR] [Request ".concat(requestId, "] Error calling binary callback:"), error);
1389
+ invokeErrorCallback(-1, "Binary callback error: " + error.message, "");
1390
+ }
1331
1391
  Module._free(dataPtr);
1332
1392
  }).catch(function(error) {
1333
- console.log(" Error reading binary data:", error);
1334
- invokeErrorCallback(-1, error.message, callbackPtr);
1393
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading binary data:"), error);
1394
+ invokeErrorCallback(-1, error.message, "Failed to read binary response");
1335
1395
  });
1336
1396
  } else {
1337
1397
  response.text().then(function(data) {
1338
1398
  var dataLength = lengthBytesUTF8(data);
1399
+ if (dataLength <= 0 || dataLength > 104857600) {
1400
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid text data size: ").concat(dataLength));
1401
+ invokeErrorCallback(-1, "Invalid text data size: ".concat(dataLength), "");
1402
+ return;
1403
+ }
1339
1404
  var dataPtr = Module._malloc(dataLength + 1);
1340
- stringToUTF8(data, dataPtr, dataLength + 1);
1341
- Module["dynCall_viii"](successCallbackPtr, dataPtr, dataLength, callbackPtr);
1405
+ if (!dataPtr) {
1406
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate ").concat(dataLength + 1, " bytes for text data"));
1407
+ invokeErrorCallback(-1, "Memory allocation failed for text data", "");
1408
+ return;
1409
+ }
1410
+ try {
1411
+ stringToUTF8(data, dataPtr, dataLength + 1);
1412
+ if (!successCallbackPtr || !callbackPtr) {
1413
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid text callback pointers: success=").concat(successCallbackPtr, ", callback=").concat(callbackPtr));
1414
+ Module._free(dataPtr);
1415
+ return;
1416
+ }
1417
+ if (typeof Module["dynCall_viii"] !== "function") {
1418
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available for text callback"));
1419
+ Module._free(dataPtr);
1420
+ return;
1421
+ }
1422
+ Module["dynCall_viii"](successCallbackPtr, dataPtr, dataLength, callbackPtr);
1423
+ if (requestContextPtr) {
1424
+ Module._free(requestContextPtr);
1425
+ }
1426
+ } catch (error) {
1427
+ console.error("[ERROR] [Request ".concat(requestId, "] Error calling text callback:"), error);
1428
+ invokeErrorCallback(-1, "Text callback error: " + error.message, "");
1429
+ }
1342
1430
  Module._free(dataPtr);
1343
1431
  }).catch(function(error) {
1344
- console.log(" Error reading text data:", error);
1345
- invokeErrorCallback(-1, error.message, callbackPtr);
1432
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading text data:"), error);
1433
+ invokeErrorCallback(-1, error.message, "Failed to read text response");
1346
1434
  });
1347
1435
  }
1348
1436
  }).catch(function(error) {
1349
- console.log("❌ Fetch error:", error);
1350
- invokeErrorCallback(-1, error.message, callbackPtr);
1437
+ var duration = performance.now() - startTime;
1438
+ console.error("[ERROR] [Request ".concat(requestId, "] Fetch error after ").concat(duration.toFixed(2), "ms:"), error);
1439
+ invokeErrorCallback(-1, error.message, "Network fetch failed");
1351
1440
  });
1352
1441
  }
1353
1442
  function js_call_typescript_bridge_send_ack(connectionId, ackId, responseData) {
@@ -1203,17 +1203,18 @@ var Balancy = function() {
1203
1203
  });
1204
1204
  }
1205
1205
  }
1206
- function js_fetch(url, bLoadFile, method, headers, headersCount, body, successCallbackPtr, errorCallbackPtr, callbackPtr, binaryCallbackPtr) {
1206
+ function js_fetch(url, bLoadFile, method, headers, headersCount, body, successCallbackPtr, errorCallbackPtr, callbackPtr, binaryCallbackPtr, requestContextPtr) {
1207
+ var requestId = Date.now() + Math.random();
1207
1208
  var urlString = "";
1208
1209
  var methodString = "";
1209
1210
  var bodyString = null;
1210
1211
  try {
1211
1212
  if (!url) {
1212
- console.error(" URL pointer is null");
1213
+ console.error("[ERROR] [Request ".concat(requestId, "] URL pointer is null"));
1213
1214
  return;
1214
1215
  }
1215
1216
  if (!method) {
1216
- console.error(" Method pointer is null");
1217
+ console.error("[ERROR] [Request ".concat(requestId, "] Method pointer is null"));
1217
1218
  return;
1218
1219
  }
1219
1220
  urlString = UTF8ToString(url);
@@ -1222,51 +1223,80 @@ var Balancy = function() {
1222
1223
  bodyString = UTF8ToString(body);
1223
1224
  }
1224
1225
  if (!urlString || urlString.length === 0) {
1225
- console.error(" URL is empty or invalid");
1226
+ console.error("[ERROR] [Request ".concat(requestId, "] URL is empty or invalid"));
1226
1227
  return;
1227
1228
  }
1228
1229
  if (urlString.includes("\0") || urlString.includes("€")) {
1229
- console.error(" URL contains invalid characters:", urlString);
1230
+ console.error("[ERROR] [Request ".concat(requestId, "] URL contains invalid characters:"), urlString);
1230
1231
  return;
1231
1232
  }
1232
1233
  } catch (error) {
1233
- console.error(" Error reading strings from WASM memory:", error);
1234
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading strings from WASM memory:"), error);
1234
1235
  return;
1235
1236
  }
1236
- var invokeErrorCallback = function(status, message, callbackPtr) {
1237
- console.log("❌ invokeErrorCallback:", status, message);
1237
+ var invokeErrorCallback = function(status, message) {
1238
+ var details = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "";
1239
+ var fullMessage = "[Request ".concat(requestId, "] ").concat(bLoadFile ? "FILE LOAD" : "API REQUEST", " FAILED: ").concat(methodString, " ").concat(urlString).concat(details ? " - " + details : "");
1240
+ console.error("[ERROR] ".concat(fullMessage));
1241
+ console.error(" Status: ".concat(status));
1242
+ console.error(" Error: ".concat(message || "Unknown error"));
1243
+ if (bodyString) {
1244
+ console.error(" Request Body: ".concat(bodyString.length > 500 ? bodyString.substring(0, 500) + "..." : bodyString));
1245
+ }
1238
1246
  try {
1239
- var errorMessage = message || "Unknown error";
1240
- var errorLength = lengthBytesUTF8(errorMessage) + 1;
1241
- var errorPtr = Module._malloc(errorLength);
1242
- stringToUTF8(errorMessage, errorPtr, errorLength);
1247
+ if (requestContextPtr) {
1248
+ Module._free(requestContextPtr);
1249
+ }
1250
+ if (!errorCallbackPtr || !callbackPtr) {
1251
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid callback pointers: errorCallback=").concat(errorCallbackPtr, ", callback=").concat(callbackPtr));
1252
+ return;
1253
+ }
1254
+ var errorMessage = fullMessage + (message ? " - ".concat(message) : "");
1255
+ var errorLength = lengthBytesUTF8(errorMessage);
1256
+ if (errorLength <= 0 || errorLength > 1048576) {
1257
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid error message length: ").concat(errorLength));
1258
+ return;
1259
+ }
1260
+ var errorPtr = Module._malloc(errorLength + 1);
1261
+ if (!errorPtr) {
1262
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate memory for error message"));
1263
+ return;
1264
+ }
1265
+ stringToUTF8(errorMessage, errorPtr, errorLength + 1);
1266
+ if (typeof Module["dynCall_viii"] !== "function") {
1267
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available"));
1268
+ Module._free(errorPtr);
1269
+ return;
1270
+ }
1243
1271
  Module["dynCall_viii"](errorCallbackPtr, status, errorPtr, callbackPtr);
1244
1272
  Module._free(errorPtr);
1245
1273
  } catch (error) {
1246
- console.error(" Error in error callback:", error);
1274
+ console.error("[ERROR] [Request ".concat(requestId, "] Error in error callback:"), error);
1247
1275
  }
1248
1276
  };
1249
1277
  var headersObj = new Headers;
1250
1278
  var ptrSize = Module.HEAPU32.BYTES_PER_ELEMENT;
1279
+ var headersLogged = [];
1251
1280
  try {
1252
1281
  for(var i = 0; i < headersCount; i += 2){
1253
1282
  var keyPtr = getValue(headers + i * ptrSize, "*");
1254
1283
  var valuePtr = getValue(headers + (i + 1) * ptrSize, "*");
1255
1284
  if (!keyPtr || !valuePtr) {
1256
- console.warn("⚠️ Skipping invalid header pointer at index", i);
1285
+ console.warn("[WARN] [Request ".concat(requestId, "] Skipping invalid header pointer at index"), i);
1257
1286
  continue;
1258
1287
  }
1259
1288
  var key = UTF8ToString(keyPtr);
1260
1289
  var value = UTF8ToString(valuePtr);
1261
1290
  if (!key || !value) {
1262
- console.warn("⚠️ Skipping empty header at index", i);
1291
+ console.warn("[WARN] [Request ".concat(requestId, "] Skipping empty header at index"), i);
1263
1292
  continue;
1264
1293
  }
1265
1294
  headersObj.append(key, value);
1295
+ headersLogged.push("".concat(key, ": ").concat(value));
1266
1296
  }
1267
1297
  } catch (error) {
1268
- console.error(" Error processing headers:", error);
1269
- invokeErrorCallback(-1, "Header processing error: " + error.message, callbackPtr);
1298
+ console.error("[ERROR] [Request ".concat(requestId, "] Error processing headers:"), error);
1299
+ invokeErrorCallback(-1, "Header processing error: " + error.message, "");
1270
1300
  return;
1271
1301
  }
1272
1302
  var fetchOptions = {
@@ -1274,15 +1304,17 @@ var Balancy = function() {
1274
1304
  headers: headersObj,
1275
1305
  body: bodyString
1276
1306
  };
1307
+ var startTime = performance.now();
1277
1308
  fetch(urlString, fetchOptions).then(function(response) {
1309
+ performance.now() - startTime;
1278
1310
  if (!response.ok) {
1279
- console.log(" Response not OK, reading error message");
1311
+ console.error("[ERROR] [Request ".concat(requestId, "] Response not OK, status: ").concat(response.status, " ").concat(response.statusText));
1280
1312
  response.text().then(function(errorMessage) {
1281
- console.log(" Error message:", errorMessage);
1282
- invokeErrorCallback(response.status, errorMessage, callbackPtr);
1313
+ console.error(" Response Body: ".concat(errorMessage));
1314
+ invokeErrorCallback(response.status, errorMessage, "HTTP ".concat(response.status, " ").concat(response.statusText));
1283
1315
  }).catch(function() {
1284
- console.log(" Failed to read error message");
1285
- invokeErrorCallback(response.status, null, callbackPtr);
1316
+ console.error(" Failed to read error response body");
1317
+ invokeErrorCallback(response.status, "HTTP ".concat(response.status, " ").concat(response.statusText), "Could not read response body");
1286
1318
  });
1287
1319
  return;
1288
1320
  }
@@ -1330,30 +1362,87 @@ var Balancy = function() {
1330
1362
  if (isBinary) {
1331
1363
  response.arrayBuffer().then(function(arrayBuffer) {
1332
1364
  var dataSize = arrayBuffer.byteLength;
1365
+ if (dataSize <= 0 || dataSize > 104857600) {
1366
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid binary data size: ").concat(dataSize));
1367
+ invokeErrorCallback(-1, "Invalid binary data size: ".concat(dataSize), "");
1368
+ return;
1369
+ }
1333
1370
  var dataPtr = Module._malloc(dataSize);
1371
+ if (!dataPtr) {
1372
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate ").concat(dataSize, " bytes for binary data"));
1373
+ invokeErrorCallback(-1, "Memory allocation failed for binary data", "");
1374
+ return;
1375
+ }
1334
1376
  var dataView = new Uint8Array(arrayBuffer);
1335
1377
  Module.HEAPU8.set(dataView, dataPtr);
1336
- Module["dynCall_viii"](binaryCallbackPtr, dataPtr, dataSize, callbackPtr);
1378
+ try {
1379
+ if (!binaryCallbackPtr || !callbackPtr) {
1380
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid binary callback pointers: binary=").concat(binaryCallbackPtr, ", callback=").concat(callbackPtr));
1381
+ Module._free(dataPtr);
1382
+ return;
1383
+ }
1384
+ if (typeof Module["dynCall_viii"] !== "function") {
1385
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available for binary callback"));
1386
+ Module._free(dataPtr);
1387
+ return;
1388
+ }
1389
+ Module["dynCall_viii"](binaryCallbackPtr, dataPtr, dataSize, callbackPtr);
1390
+ if (requestContextPtr) {
1391
+ Module._free(requestContextPtr);
1392
+ }
1393
+ } catch (error) {
1394
+ console.error("[ERROR] [Request ".concat(requestId, "] Error calling binary callback:"), error);
1395
+ invokeErrorCallback(-1, "Binary callback error: " + error.message, "");
1396
+ }
1337
1397
  Module._free(dataPtr);
1338
1398
  }).catch(function(error) {
1339
- console.log(" Error reading binary data:", error);
1340
- invokeErrorCallback(-1, error.message, callbackPtr);
1399
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading binary data:"), error);
1400
+ invokeErrorCallback(-1, error.message, "Failed to read binary response");
1341
1401
  });
1342
1402
  } else {
1343
1403
  response.text().then(function(data) {
1344
1404
  var dataLength = lengthBytesUTF8(data);
1405
+ if (dataLength <= 0 || dataLength > 104857600) {
1406
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid text data size: ").concat(dataLength));
1407
+ invokeErrorCallback(-1, "Invalid text data size: ".concat(dataLength), "");
1408
+ return;
1409
+ }
1345
1410
  var dataPtr = Module._malloc(dataLength + 1);
1346
- stringToUTF8(data, dataPtr, dataLength + 1);
1347
- Module["dynCall_viii"](successCallbackPtr, dataPtr, dataLength, callbackPtr);
1411
+ if (!dataPtr) {
1412
+ console.error("[ERROR] [Request ".concat(requestId, "] Failed to allocate ").concat(dataLength + 1, " bytes for text data"));
1413
+ invokeErrorCallback(-1, "Memory allocation failed for text data", "");
1414
+ return;
1415
+ }
1416
+ try {
1417
+ stringToUTF8(data, dataPtr, dataLength + 1);
1418
+ if (!successCallbackPtr || !callbackPtr) {
1419
+ console.error("[ERROR] [Request ".concat(requestId, "] Invalid text callback pointers: success=").concat(successCallbackPtr, ", callback=").concat(callbackPtr));
1420
+ Module._free(dataPtr);
1421
+ return;
1422
+ }
1423
+ if (typeof Module["dynCall_viii"] !== "function") {
1424
+ console.error("[ERROR] [Request ".concat(requestId, "] dynCall_viii function not available for text callback"));
1425
+ Module._free(dataPtr);
1426
+ return;
1427
+ }
1428
+ Module["dynCall_viii"](successCallbackPtr, dataPtr, dataLength, callbackPtr);
1429
+ if (requestContextPtr) {
1430
+ Module._free(requestContextPtr);
1431
+ }
1432
+ } catch (error) {
1433
+ console.error("[ERROR] [Request ".concat(requestId, "] Error calling text callback:"), error);
1434
+ invokeErrorCallback(-1, "Text callback error: " + error.message, "");
1435
+ }
1348
1436
  Module._free(dataPtr);
1349
1437
  }).catch(function(error) {
1350
- console.log(" Error reading text data:", error);
1351
- invokeErrorCallback(-1, error.message, callbackPtr);
1438
+ console.error("[ERROR] [Request ".concat(requestId, "] Error reading text data:"), error);
1439
+ invokeErrorCallback(-1, error.message, "Failed to read text response");
1352
1440
  });
1353
1441
  }
1354
1442
  }).catch(function(error) {
1355
- console.log("❌ Fetch error:", error);
1356
- invokeErrorCallback(-1, error.message, callbackPtr);
1443
+ var duration = performance.now() - startTime;
1444
+ console.error("[ERROR] [Request ".concat(requestId, "] Fetch error after ").concat(duration.toFixed(2), "ms:"), error);
1445
+ invokeErrorCallback(-1, error.message, "Network fetch failed");
1357
1446
  });
1358
1447
  }
1359
1448
  function js_call_typescript_bridge_send_ack(connectionId, ackId, responseData) {