seacloud-sdk 0.12.1 → 0.12.3

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.js CHANGED
@@ -288,7 +288,10 @@ async function initSeacloud(apiKeyOrConfig, options) {
288
288
  try {
289
289
  const parentHost = await getHostFromParent(3e3);
290
290
  if (parentHost) {
291
- const isDevelopment = parentHost.includes("localhost") || parentHost.includes("127.0.0.1") || parentHost.includes(":3000") || parentHost.includes(":8080") || parentHost.includes("seaverse.dev");
291
+ const devHostPatterns = ["localhost", "127.0.0.1", ":3000", ":8080", "seaverse.dev"];
292
+ const currentHost = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" ? globalThis.window.location.host : "";
293
+ const checkIsDev = (host) => devHostPatterns.some((pattern) => host.includes(pattern));
294
+ const isDevelopment = checkIsDev(currentHost) || checkIsDev(parentHost);
292
295
  if (isDevelopment) {
293
296
  config.baseUrl = "https://proxy-rs.sg.seaverse.dev";
294
297
  console.log("[SeaCloud SDK] \u68C0\u6D4B\u5230\u5F00\u53D1\u73AF\u5883\uFF0C\u4F7F\u7528\u5F00\u53D1 baseUrl:", config.baseUrl);
@@ -352,24 +355,80 @@ function isTaskComplete(status) {
352
355
  function sleep(ms) {
353
356
  return new Promise((resolve) => setTimeout(resolve, ms));
354
357
  }
358
+ function isNetworkError(error) {
359
+ if (error instanceof SeacloudError) {
360
+ const statusCode = error.statusCode;
361
+ if (!statusCode) return false;
362
+ const networkStatusCodes = [
363
+ 408,
364
+ // Request Timeout
365
+ 500,
366
+ // Internal Server Error
367
+ 502,
368
+ // Bad Gateway
369
+ 503,
370
+ // Service Unavailable
371
+ 504
372
+ // Gateway Timeout
373
+ ];
374
+ return networkStatusCodes.includes(statusCode);
375
+ }
376
+ if (error instanceof Error) {
377
+ const errorCode = error.code;
378
+ const networkErrorCodes = [
379
+ "ETIMEDOUT",
380
+ "ECONNRESET",
381
+ "ECONNREFUSED",
382
+ "ENOTFOUND",
383
+ "EAI_AGAIN",
384
+ "ENETUNREACH",
385
+ "EHOSTUNREACH"
386
+ ];
387
+ if (errorCode && networkErrorCodes.includes(errorCode)) {
388
+ return true;
389
+ }
390
+ }
391
+ return false;
392
+ }
355
393
  async function pollTaskUntilComplete(client, endpoint, taskId, options = {}) {
356
394
  const opts = { ...DEFAULT_POLLING_OPTIONS, ...options };
395
+ let consecutiveNetworkErrors = 0;
396
+ const MAX_CONSECUTIVE_NETWORK_ERRORS = 3;
357
397
  for (let attempt = 1; attempt <= opts.maxAttempts; attempt++) {
358
- const result = await client.getTaskStatus(endpoint, taskId);
359
- opts.onProgress(attempt, result.status);
360
- if (isTaskComplete(result.status)) {
361
- if (result.status === "failed") {
362
- const errorMsg = result.error?.message || "Task failed without error message";
363
- throw new SeacloudError(
364
- `Task failed: ${errorMsg}`,
365
- void 0,
366
- result.error
367
- );
398
+ try {
399
+ const result = await client.getTaskStatus(endpoint, taskId);
400
+ consecutiveNetworkErrors = 0;
401
+ opts.onProgress(attempt, result.status);
402
+ if (isTaskComplete(result.status)) {
403
+ if (result.status === "failed") {
404
+ const errorMsg = result.error?.message || "Task failed without error message";
405
+ throw new SeacloudError(
406
+ `Task failed: ${errorMsg}`,
407
+ void 0,
408
+ result.error
409
+ );
410
+ }
411
+ return result;
412
+ }
413
+ if (attempt < opts.maxAttempts) {
414
+ await sleep(opts.intervalMs);
415
+ }
416
+ } catch (error) {
417
+ if (isNetworkError(error)) {
418
+ consecutiveNetworkErrors++;
419
+ if (consecutiveNetworkErrors >= MAX_CONSECUTIVE_NETWORK_ERRORS) {
420
+ throw new SeacloudError(
421
+ `Task polling failed: encountered ${MAX_CONSECUTIVE_NETWORK_ERRORS} consecutive network errors. Last error: ${error.message}`,
422
+ error.statusCode,
423
+ error
424
+ );
425
+ }
426
+ if (attempt < opts.maxAttempts) {
427
+ await sleep(opts.intervalMs);
428
+ }
429
+ } else {
430
+ throw error;
368
431
  }
369
- return result;
370
- }
371
- if (attempt < opts.maxAttempts) {
372
- await sleep(opts.intervalMs);
373
432
  }
374
433
  }
375
434
  throw new SeacloudError(