@airtop/sdk 1.0.0-alpha2.40 → 1.0.0-alpha2.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.cjs CHANGED
@@ -9,7 +9,7 @@ var require_package = __commonJS({
9
9
  module.exports = {
10
10
  name: "@airtop/sdk",
11
11
  description: "Airtop SDK for TypeScript",
12
- version: "1.0.0-alpha2.40",
12
+ version: "1.0.0-alpha2.41",
13
13
  type: "module",
14
14
  main: "./dist/index.cjs",
15
15
  module: "./dist/index.js",
@@ -48,7 +48,7 @@ var require_package = __commonJS({
48
48
  "verify-types": "tsc --noEmit && tsc --noEmit --project tsconfig.e2e.json"
49
49
  },
50
50
  dependencies: {
51
- "@airtop/core": "0.1.0-alpha.49",
51
+ "@airtop/core": "0.1.0-alpha.50",
52
52
  "@airtop/json-schema-adapter": "workspace:*",
53
53
  "date-fns": "4.1.0",
54
54
  loglayer: "8.1.0",
@@ -1408,8 +1408,12 @@ var AirtopSessionClient = class extends AirtopBase {
1408
1408
  * Defaults to looking back 5 seconds in the event stream for the file to be available.
1409
1409
  * Use `lookbackSeconds` to control this behavior.
1410
1410
  *
1411
+ * This method uses a two-phase approach:
1412
+ * - Phase 1 (if lookbackSeconds > 0): Check historical events within the lookback window
1413
+ * - Phase 2: Wait for events from startTime forward until timeout (ensures no gap between phases)
1414
+ *
1411
1415
  * @param configuration - The optional configuration parameters for the function
1412
- * @param configuration.lookbackSeconds - The number of seconds to look back for prior events. Default `5`. 0 means no lookback.
1416
+ * @param configuration.lookbackSeconds - The number of seconds to look back for prior events. Default `5`. 0 means no lookback (only wait for new events).
1413
1417
  * @param configuration.fileType - The file type to wait for, such as "browser_download". If provided, only files of this type will be considered.
1414
1418
  * @param requestOptions - Optional request configuration including timeout
1415
1419
  * @returns Object containing file's id and downloadUrl, or null if timed out
@@ -1425,35 +1429,62 @@ var AirtopSessionClient = class extends AirtopBase {
1425
1429
  resolve(null);
1426
1430
  }, timeoutSeconds * 1e3);
1427
1431
  });
1432
+ const isQualifyingEvent = (e) => {
1433
+ if (e.event !== "file_status" || e.status !== "available") {
1434
+ return false;
1435
+ }
1436
+ if (fileType && e.type !== fileType) {
1437
+ this.log.info(`skipping file of type ${e.type} (looking for ${fileType})`);
1438
+ return false;
1439
+ }
1440
+ return true;
1441
+ };
1442
+ const createResult = (e) => ({
1443
+ id: e.fileId,
1444
+ downloadUrl: e.downloadUrl,
1445
+ fileName: e.name,
1446
+ fileType: e.type
1447
+ });
1428
1448
  const processEventsPromise = (async () => {
1429
- const sessionEvents = await this.client.sessions.getEvents(
1449
+ const thresholdTime = startTime.getTime() - lookbackSeconds * 1e3;
1450
+ if (lookbackSeconds > 0) {
1451
+ this.log.info(`Phase 1: checking historical events from last ${lookbackSeconds} seconds`);
1452
+ const historicalEvents = await this.client.sessions.getEvents(
1453
+ this.sessionId,
1454
+ { all: true, reverse: true },
1455
+ { timeoutInSeconds: timeoutSeconds, ...requestOptions || {} }
1456
+ );
1457
+ for await (const event of historicalEvents) {
1458
+ const e = event;
1459
+ const eventTime = Date.parse(e.eventTime);
1460
+ if (eventTime < thresholdTime) {
1461
+ this.log.info(`Phase 1: reached lookback threshold, no matching file found in history`);
1462
+ break;
1463
+ }
1464
+ if (isQualifyingEvent(e)) {
1465
+ this.log.info(`Phase 1: found file in history:
1466
+ ${JSON.stringify(event, null, 2)}`);
1467
+ return createResult(e);
1468
+ }
1469
+ }
1470
+ }
1471
+ this.log.info(`Phase 2: waiting for download events from ${startTime.toISOString()} forward`);
1472
+ const newEvents = await this.client.sessions.getEvents(
1430
1473
  this.sessionId,
1431
- { all: lookbackSeconds >= 0, reverse: true },
1474
+ { all: true, reverse: false },
1432
1475
  { timeoutInSeconds: timeoutSeconds, ...requestOptions || {} }
1433
1476
  );
1434
- for await (const event of sessionEvents) {
1477
+ const startTimeMs = startTime.getTime();
1478
+ for await (const event of newEvents) {
1435
1479
  const e = event;
1436
- if (e.event === "file_status") {
1437
- if (e.status === "available") {
1438
- const eventTime = Date.parse(e.eventTime);
1439
- this.log.info(`file_status message received:
1480
+ const eventTime = Date.parse(e.eventTime);
1481
+ if (eventTime < startTimeMs) {
1482
+ continue;
1483
+ }
1484
+ if (isQualifyingEvent(e)) {
1485
+ this.log.info(`Phase 2: found download event:
1440
1486
  ${JSON.stringify(event, null, 2)}`);
1441
- if (fileType && e.type !== fileType) {
1442
- this.log.info(`skipping file of type ${e.type} (looking for ${fileType})`);
1443
- continue;
1444
- }
1445
- const thresholdTime = startTime.getTime() - lookbackSeconds * 1e3;
1446
- if (eventTime < thresholdTime) {
1447
- this.log.info(`stopping event processing - encountered event older than lookbackSeconds threshold`);
1448
- break;
1449
- }
1450
- return {
1451
- id: e.fileId,
1452
- downloadUrl: e.downloadUrl,
1453
- fileName: e.name,
1454
- fileType: e.type
1455
- };
1456
- }
1487
+ return createResult(e);
1457
1488
  }
1458
1489
  }
1459
1490
  return null;