@realtimex/sdk 1.0.5 → 1.0.7

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.d.mts CHANGED
@@ -7,6 +7,7 @@ interface SDKConfig {
7
7
  appId?: string;
8
8
  appName?: string;
9
9
  };
10
+ defaultPort?: number;
10
11
  }
11
12
  interface Activity {
12
13
  id: string;
@@ -173,6 +174,49 @@ declare class TaskModule {
173
174
  private _sendEvent;
174
175
  }
175
176
 
177
+ /**
178
+ * Port utilities for Local Apps
179
+ * Helps find available ports to avoid conflicts when multiple apps run simultaneously
180
+ */
181
+ declare class PortModule {
182
+ private defaultPort;
183
+ constructor(defaultPort?: number);
184
+ /**
185
+ * Get suggested port from environment (RTX_PORT) or default
186
+ */
187
+ getSuggestedPort(): number;
188
+ /**
189
+ * Check if a port is available on a specific host
190
+ */
191
+ private isPortAvailableOn;
192
+ /**
193
+ * Check if a port is available (checks both IPv4 and IPv6)
194
+ * @param port - Port number to check
195
+ * @returns Promise resolving to true if port is available on ALL interfaces
196
+ */
197
+ isPortAvailable(port: number): Promise<boolean>;
198
+ /**
199
+ * Find an available port starting from the suggested port
200
+ * @param startPort - Starting port number (default: RTX_PORT or defaultPort)
201
+ * @param maxAttempts - Maximum ports to try (default: 100)
202
+ * @returns Promise resolving to an available port number
203
+ * @throws Error if no available port found in range
204
+ */
205
+ findAvailablePort(startPort?: number, maxAttempts?: number): Promise<number>;
206
+ /**
207
+ * Get a ready-to-use port
208
+ * Returns the suggested port if available, otherwise finds the next available port
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * const sdk = new RealtimeXSDK();
213
+ * const port = await sdk.port.getPort();
214
+ * app.listen(port);
215
+ * ```
216
+ */
217
+ getPort(): Promise<number>;
218
+ }
219
+
176
220
  /**
177
221
  * RealtimeX Local App SDK
178
222
  *
@@ -185,6 +229,7 @@ declare class RealtimeXSDK {
185
229
  webhook: WebhookModule;
186
230
  api: ApiModule;
187
231
  task: TaskModule;
232
+ port: PortModule;
188
233
  readonly appId: string;
189
234
  readonly appName: string | undefined;
190
235
  private static DEFAULT_REALTIMEX_URL;
@@ -195,4 +240,4 @@ declare class RealtimeXSDK {
195
240
  private getEnvVar;
196
241
  }
197
242
 
198
- export { ActivitiesModule, type Activity, type Agent, ApiModule, RealtimeXSDK, type SDKConfig, type Task, TaskModule, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
243
+ export { ActivitiesModule, type Activity, type Agent, ApiModule, PortModule, RealtimeXSDK, type SDKConfig, type Task, TaskModule, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ interface SDKConfig {
7
7
  appId?: string;
8
8
  appName?: string;
9
9
  };
10
+ defaultPort?: number;
10
11
  }
11
12
  interface Activity {
12
13
  id: string;
@@ -173,6 +174,49 @@ declare class TaskModule {
173
174
  private _sendEvent;
174
175
  }
175
176
 
177
+ /**
178
+ * Port utilities for Local Apps
179
+ * Helps find available ports to avoid conflicts when multiple apps run simultaneously
180
+ */
181
+ declare class PortModule {
182
+ private defaultPort;
183
+ constructor(defaultPort?: number);
184
+ /**
185
+ * Get suggested port from environment (RTX_PORT) or default
186
+ */
187
+ getSuggestedPort(): number;
188
+ /**
189
+ * Check if a port is available on a specific host
190
+ */
191
+ private isPortAvailableOn;
192
+ /**
193
+ * Check if a port is available (checks both IPv4 and IPv6)
194
+ * @param port - Port number to check
195
+ * @returns Promise resolving to true if port is available on ALL interfaces
196
+ */
197
+ isPortAvailable(port: number): Promise<boolean>;
198
+ /**
199
+ * Find an available port starting from the suggested port
200
+ * @param startPort - Starting port number (default: RTX_PORT or defaultPort)
201
+ * @param maxAttempts - Maximum ports to try (default: 100)
202
+ * @returns Promise resolving to an available port number
203
+ * @throws Error if no available port found in range
204
+ */
205
+ findAvailablePort(startPort?: number, maxAttempts?: number): Promise<number>;
206
+ /**
207
+ * Get a ready-to-use port
208
+ * Returns the suggested port if available, otherwise finds the next available port
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * const sdk = new RealtimeXSDK();
213
+ * const port = await sdk.port.getPort();
214
+ * app.listen(port);
215
+ * ```
216
+ */
217
+ getPort(): Promise<number>;
218
+ }
219
+
176
220
  /**
177
221
  * RealtimeX Local App SDK
178
222
  *
@@ -185,6 +229,7 @@ declare class RealtimeXSDK {
185
229
  webhook: WebhookModule;
186
230
  api: ApiModule;
187
231
  task: TaskModule;
232
+ port: PortModule;
188
233
  readonly appId: string;
189
234
  readonly appName: string | undefined;
190
235
  private static DEFAULT_REALTIMEX_URL;
@@ -195,4 +240,4 @@ declare class RealtimeXSDK {
195
240
  private getEnvVar;
196
241
  }
197
242
 
198
- export { ActivitiesModule, type Activity, type Agent, ApiModule, RealtimeXSDK, type SDKConfig, type Task, TaskModule, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
243
+ export { ActivitiesModule, type Activity, type Agent, ApiModule, PortModule, RealtimeXSDK, type SDKConfig, type Task, TaskModule, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -22,6 +32,7 @@ var index_exports = {};
22
32
  __export(index_exports, {
23
33
  ActivitiesModule: () => ActivitiesModule,
24
34
  ApiModule: () => ApiModule,
35
+ PortModule: () => PortModule,
25
36
  RealtimeXSDK: () => RealtimeXSDK,
26
37
  TaskModule: () => TaskModule,
27
38
  WebhookModule: () => WebhookModule
@@ -233,6 +244,83 @@ var TaskModule = class {
233
244
  }
234
245
  };
235
246
 
247
+ // src/modules/port.ts
248
+ var net = __toESM(require("net"));
249
+ var PortModule = class {
250
+ constructor(defaultPort = 8080) {
251
+ this.defaultPort = defaultPort;
252
+ }
253
+ /**
254
+ * Get suggested port from environment (RTX_PORT) or default
255
+ */
256
+ getSuggestedPort() {
257
+ const envPort = process.env.RTX_PORT;
258
+ return envPort ? parseInt(envPort, 10) : this.defaultPort;
259
+ }
260
+ /**
261
+ * Check if a port is available on a specific host
262
+ */
263
+ async isPortAvailableOn(port, host) {
264
+ return new Promise((resolve) => {
265
+ const server = net.createServer();
266
+ server.once("error", () => resolve(false));
267
+ server.once("listening", () => {
268
+ server.close();
269
+ resolve(true);
270
+ });
271
+ server.listen(port, host);
272
+ });
273
+ }
274
+ /**
275
+ * Check if a port is available (checks both IPv4 and IPv6)
276
+ * @param port - Port number to check
277
+ * @returns Promise resolving to true if port is available on ALL interfaces
278
+ */
279
+ async isPortAvailable(port) {
280
+ const ipv4Available = await this.isPortAvailableOn(port, "127.0.0.1");
281
+ if (!ipv4Available) return false;
282
+ const ipv6Available = await this.isPortAvailableOn(port, "::1");
283
+ if (!ipv6Available) return false;
284
+ const allInterfacesAvailable = await this.isPortAvailableOn(port, "0.0.0.0");
285
+ return allInterfacesAvailable;
286
+ }
287
+ /**
288
+ * Find an available port starting from the suggested port
289
+ * @param startPort - Starting port number (default: RTX_PORT or defaultPort)
290
+ * @param maxAttempts - Maximum ports to try (default: 100)
291
+ * @returns Promise resolving to an available port number
292
+ * @throws Error if no available port found in range
293
+ */
294
+ async findAvailablePort(startPort, maxAttempts = 100) {
295
+ const port = startPort ?? this.getSuggestedPort();
296
+ for (let i = 0; i < maxAttempts; i++) {
297
+ const currentPort = port + i;
298
+ if (await this.isPortAvailable(currentPort)) {
299
+ return currentPort;
300
+ }
301
+ }
302
+ throw new Error(`No available port found in range ${port}-${port + maxAttempts - 1}`);
303
+ }
304
+ /**
305
+ * Get a ready-to-use port
306
+ * Returns the suggested port if available, otherwise finds the next available port
307
+ *
308
+ * @example
309
+ * ```typescript
310
+ * const sdk = new RealtimeXSDK();
311
+ * const port = await sdk.port.getPort();
312
+ * app.listen(port);
313
+ * ```
314
+ */
315
+ async getPort() {
316
+ const suggested = this.getSuggestedPort();
317
+ if (await this.isPortAvailable(suggested)) {
318
+ return suggested;
319
+ }
320
+ return this.findAvailablePort(suggested + 1);
321
+ }
322
+ };
323
+
236
324
  // src/index.ts
237
325
  var _RealtimeXSDK = class _RealtimeXSDK {
238
326
  constructor(config = {}) {
@@ -245,6 +333,7 @@ var _RealtimeXSDK = class _RealtimeXSDK {
245
333
  this.webhook = new WebhookModule(realtimexUrl, this.appName, this.appId);
246
334
  this.api = new ApiModule(realtimexUrl);
247
335
  this.task = new TaskModule(realtimexUrl, this.appName, this.appId);
336
+ this.port = new PortModule(config.defaultPort);
248
337
  }
249
338
  /**
250
339
  * Get environment variable (works in Node.js and browser)
@@ -265,6 +354,7 @@ var RealtimeXSDK = _RealtimeXSDK;
265
354
  0 && (module.exports = {
266
355
  ActivitiesModule,
267
356
  ApiModule,
357
+ PortModule,
268
358
  RealtimeXSDK,
269
359
  TaskModule,
270
360
  WebhookModule
package/dist/index.mjs CHANGED
@@ -203,6 +203,83 @@ var TaskModule = class {
203
203
  }
204
204
  };
205
205
 
206
+ // src/modules/port.ts
207
+ import * as net from "net";
208
+ var PortModule = class {
209
+ constructor(defaultPort = 8080) {
210
+ this.defaultPort = defaultPort;
211
+ }
212
+ /**
213
+ * Get suggested port from environment (RTX_PORT) or default
214
+ */
215
+ getSuggestedPort() {
216
+ const envPort = process.env.RTX_PORT;
217
+ return envPort ? parseInt(envPort, 10) : this.defaultPort;
218
+ }
219
+ /**
220
+ * Check if a port is available on a specific host
221
+ */
222
+ async isPortAvailableOn(port, host) {
223
+ return new Promise((resolve) => {
224
+ const server = net.createServer();
225
+ server.once("error", () => resolve(false));
226
+ server.once("listening", () => {
227
+ server.close();
228
+ resolve(true);
229
+ });
230
+ server.listen(port, host);
231
+ });
232
+ }
233
+ /**
234
+ * Check if a port is available (checks both IPv4 and IPv6)
235
+ * @param port - Port number to check
236
+ * @returns Promise resolving to true if port is available on ALL interfaces
237
+ */
238
+ async isPortAvailable(port) {
239
+ const ipv4Available = await this.isPortAvailableOn(port, "127.0.0.1");
240
+ if (!ipv4Available) return false;
241
+ const ipv6Available = await this.isPortAvailableOn(port, "::1");
242
+ if (!ipv6Available) return false;
243
+ const allInterfacesAvailable = await this.isPortAvailableOn(port, "0.0.0.0");
244
+ return allInterfacesAvailable;
245
+ }
246
+ /**
247
+ * Find an available port starting from the suggested port
248
+ * @param startPort - Starting port number (default: RTX_PORT or defaultPort)
249
+ * @param maxAttempts - Maximum ports to try (default: 100)
250
+ * @returns Promise resolving to an available port number
251
+ * @throws Error if no available port found in range
252
+ */
253
+ async findAvailablePort(startPort, maxAttempts = 100) {
254
+ const port = startPort ?? this.getSuggestedPort();
255
+ for (let i = 0; i < maxAttempts; i++) {
256
+ const currentPort = port + i;
257
+ if (await this.isPortAvailable(currentPort)) {
258
+ return currentPort;
259
+ }
260
+ }
261
+ throw new Error(`No available port found in range ${port}-${port + maxAttempts - 1}`);
262
+ }
263
+ /**
264
+ * Get a ready-to-use port
265
+ * Returns the suggested port if available, otherwise finds the next available port
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * const sdk = new RealtimeXSDK();
270
+ * const port = await sdk.port.getPort();
271
+ * app.listen(port);
272
+ * ```
273
+ */
274
+ async getPort() {
275
+ const suggested = this.getSuggestedPort();
276
+ if (await this.isPortAvailable(suggested)) {
277
+ return suggested;
278
+ }
279
+ return this.findAvailablePort(suggested + 1);
280
+ }
281
+ };
282
+
206
283
  // src/index.ts
207
284
  var _RealtimeXSDK = class _RealtimeXSDK {
208
285
  constructor(config = {}) {
@@ -215,6 +292,7 @@ var _RealtimeXSDK = class _RealtimeXSDK {
215
292
  this.webhook = new WebhookModule(realtimexUrl, this.appName, this.appId);
216
293
  this.api = new ApiModule(realtimexUrl);
217
294
  this.task = new TaskModule(realtimexUrl, this.appName, this.appId);
295
+ this.port = new PortModule(config.defaultPort);
218
296
  }
219
297
  /**
220
298
  * Get environment variable (works in Node.js and browser)
@@ -234,6 +312,7 @@ var RealtimeXSDK = _RealtimeXSDK;
234
312
  export {
235
313
  ActivitiesModule,
236
314
  ApiModule,
315
+ PortModule,
237
316
  RealtimeXSDK,
238
317
  TaskModule,
239
318
  WebhookModule
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realtimex/sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "SDK for building Local Apps that integrate with RealtimeX",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -40,4 +40,4 @@
40
40
  "engines": {
41
41
  "node": ">=18.0.0"
42
42
  }
43
- }
43
+ }