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