@realtimex/sdk 1.0.4 → 1.0.6
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 +73 -1
- package/dist/index.d.ts +73 -1
- package/dist/index.js +127 -0
- package/dist/index.mjs +115 -0
- package/package.json +1 -1
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;
|
|
@@ -143,6 +144,75 @@ declare class ApiModule {
|
|
|
143
144
|
getTask(taskUuid: string): Promise<Task>;
|
|
144
145
|
}
|
|
145
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Task Module - Report task status to RealtimeX
|
|
149
|
+
* Used by external agents/processors to update task status
|
|
150
|
+
*/
|
|
151
|
+
interface TaskStatusResponse {
|
|
152
|
+
success: boolean;
|
|
153
|
+
task_uuid: string;
|
|
154
|
+
status: string;
|
|
155
|
+
message?: string;
|
|
156
|
+
}
|
|
157
|
+
declare class TaskModule {
|
|
158
|
+
private realtimexUrl;
|
|
159
|
+
private appName?;
|
|
160
|
+
private appId?;
|
|
161
|
+
constructor(realtimexUrl: string, appName?: string, appId?: string);
|
|
162
|
+
/**
|
|
163
|
+
* Mark task as processing
|
|
164
|
+
*/
|
|
165
|
+
start(taskUuid: string, machineId?: string): Promise<TaskStatusResponse>;
|
|
166
|
+
/**
|
|
167
|
+
* Mark task as completed with result
|
|
168
|
+
*/
|
|
169
|
+
complete(taskUuid: string, result?: object, machineId?: string): Promise<TaskStatusResponse>;
|
|
170
|
+
/**
|
|
171
|
+
* Mark task as failed with error
|
|
172
|
+
*/
|
|
173
|
+
fail(taskUuid: string, error: string, machineId?: string): Promise<TaskStatusResponse>;
|
|
174
|
+
private _sendEvent;
|
|
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
|
|
190
|
+
* @param port - Port number to check
|
|
191
|
+
* @returns Promise resolving to true if port is available
|
|
192
|
+
*/
|
|
193
|
+
isPortAvailable(port: number): Promise<boolean>;
|
|
194
|
+
/**
|
|
195
|
+
* Find an available port starting from the suggested port
|
|
196
|
+
* @param startPort - Starting port number (default: RTX_PORT or defaultPort)
|
|
197
|
+
* @param maxAttempts - Maximum ports to try (default: 100)
|
|
198
|
+
* @returns Promise resolving to an available port number
|
|
199
|
+
* @throws Error if no available port found in range
|
|
200
|
+
*/
|
|
201
|
+
findAvailablePort(startPort?: number, maxAttempts?: number): Promise<number>;
|
|
202
|
+
/**
|
|
203
|
+
* Get a ready-to-use port
|
|
204
|
+
* Returns the suggested port if available, otherwise finds the next available port
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const sdk = new RealtimeXSDK();
|
|
209
|
+
* const port = await sdk.port.getPort();
|
|
210
|
+
* app.listen(port);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
getPort(): Promise<number>;
|
|
214
|
+
}
|
|
215
|
+
|
|
146
216
|
/**
|
|
147
217
|
* RealtimeX Local App SDK
|
|
148
218
|
*
|
|
@@ -154,6 +224,8 @@ declare class RealtimeXSDK {
|
|
|
154
224
|
activities: ActivitiesModule;
|
|
155
225
|
webhook: WebhookModule;
|
|
156
226
|
api: ApiModule;
|
|
227
|
+
task: TaskModule;
|
|
228
|
+
port: PortModule;
|
|
157
229
|
readonly appId: string;
|
|
158
230
|
readonly appName: string | undefined;
|
|
159
231
|
private static DEFAULT_REALTIMEX_URL;
|
|
@@ -164,4 +236,4 @@ declare class RealtimeXSDK {
|
|
|
164
236
|
private getEnvVar;
|
|
165
237
|
}
|
|
166
238
|
|
|
167
|
-
export { ActivitiesModule, type Activity, type Agent, ApiModule, RealtimeXSDK, type SDKConfig, type Task, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
|
|
239
|
+
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;
|
|
@@ -143,6 +144,75 @@ declare class ApiModule {
|
|
|
143
144
|
getTask(taskUuid: string): Promise<Task>;
|
|
144
145
|
}
|
|
145
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Task Module - Report task status to RealtimeX
|
|
149
|
+
* Used by external agents/processors to update task status
|
|
150
|
+
*/
|
|
151
|
+
interface TaskStatusResponse {
|
|
152
|
+
success: boolean;
|
|
153
|
+
task_uuid: string;
|
|
154
|
+
status: string;
|
|
155
|
+
message?: string;
|
|
156
|
+
}
|
|
157
|
+
declare class TaskModule {
|
|
158
|
+
private realtimexUrl;
|
|
159
|
+
private appName?;
|
|
160
|
+
private appId?;
|
|
161
|
+
constructor(realtimexUrl: string, appName?: string, appId?: string);
|
|
162
|
+
/**
|
|
163
|
+
* Mark task as processing
|
|
164
|
+
*/
|
|
165
|
+
start(taskUuid: string, machineId?: string): Promise<TaskStatusResponse>;
|
|
166
|
+
/**
|
|
167
|
+
* Mark task as completed with result
|
|
168
|
+
*/
|
|
169
|
+
complete(taskUuid: string, result?: object, machineId?: string): Promise<TaskStatusResponse>;
|
|
170
|
+
/**
|
|
171
|
+
* Mark task as failed with error
|
|
172
|
+
*/
|
|
173
|
+
fail(taskUuid: string, error: string, machineId?: string): Promise<TaskStatusResponse>;
|
|
174
|
+
private _sendEvent;
|
|
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
|
|
190
|
+
* @param port - Port number to check
|
|
191
|
+
* @returns Promise resolving to true if port is available
|
|
192
|
+
*/
|
|
193
|
+
isPortAvailable(port: number): Promise<boolean>;
|
|
194
|
+
/**
|
|
195
|
+
* Find an available port starting from the suggested port
|
|
196
|
+
* @param startPort - Starting port number (default: RTX_PORT or defaultPort)
|
|
197
|
+
* @param maxAttempts - Maximum ports to try (default: 100)
|
|
198
|
+
* @returns Promise resolving to an available port number
|
|
199
|
+
* @throws Error if no available port found in range
|
|
200
|
+
*/
|
|
201
|
+
findAvailablePort(startPort?: number, maxAttempts?: number): Promise<number>;
|
|
202
|
+
/**
|
|
203
|
+
* Get a ready-to-use port
|
|
204
|
+
* Returns the suggested port if available, otherwise finds the next available port
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const sdk = new RealtimeXSDK();
|
|
209
|
+
* const port = await sdk.port.getPort();
|
|
210
|
+
* app.listen(port);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
getPort(): Promise<number>;
|
|
214
|
+
}
|
|
215
|
+
|
|
146
216
|
/**
|
|
147
217
|
* RealtimeX Local App SDK
|
|
148
218
|
*
|
|
@@ -154,6 +224,8 @@ declare class RealtimeXSDK {
|
|
|
154
224
|
activities: ActivitiesModule;
|
|
155
225
|
webhook: WebhookModule;
|
|
156
226
|
api: ApiModule;
|
|
227
|
+
task: TaskModule;
|
|
228
|
+
port: PortModule;
|
|
157
229
|
readonly appId: string;
|
|
158
230
|
readonly appName: string | undefined;
|
|
159
231
|
private static DEFAULT_REALTIMEX_URL;
|
|
@@ -164,4 +236,4 @@ declare class RealtimeXSDK {
|
|
|
164
236
|
private getEnvVar;
|
|
165
237
|
}
|
|
166
238
|
|
|
167
|
-
export { ActivitiesModule, type Activity, type Agent, ApiModule, RealtimeXSDK, type SDKConfig, type Task, type TaskRun, type Thread, type TriggerAgentPayload, type TriggerAgentResponse, WebhookModule, type Workspace };
|
|
239
|
+
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,7 +32,9 @@ var index_exports = {};
|
|
|
22
32
|
__export(index_exports, {
|
|
23
33
|
ActivitiesModule: () => ActivitiesModule,
|
|
24
34
|
ApiModule: () => ApiModule,
|
|
35
|
+
PortModule: () => PortModule,
|
|
25
36
|
RealtimeXSDK: () => RealtimeXSDK,
|
|
37
|
+
TaskModule: () => TaskModule,
|
|
26
38
|
WebhookModule: () => WebhookModule
|
|
27
39
|
});
|
|
28
40
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -187,6 +199,117 @@ var ApiModule = class {
|
|
|
187
199
|
}
|
|
188
200
|
};
|
|
189
201
|
|
|
202
|
+
// src/modules/task.ts
|
|
203
|
+
var TaskModule = class {
|
|
204
|
+
constructor(realtimexUrl, appName, appId) {
|
|
205
|
+
this.realtimexUrl = realtimexUrl.replace(/\/$/, "");
|
|
206
|
+
this.appName = appName;
|
|
207
|
+
this.appId = appId;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Mark task as processing
|
|
211
|
+
*/
|
|
212
|
+
async start(taskUuid, machineId) {
|
|
213
|
+
return this._sendEvent("task-start", taskUuid, { machine_id: machineId });
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Mark task as completed with result
|
|
217
|
+
*/
|
|
218
|
+
async complete(taskUuid, result, machineId) {
|
|
219
|
+
return this._sendEvent("task-complete", taskUuid, { result, machine_id: machineId });
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Mark task as failed with error
|
|
223
|
+
*/
|
|
224
|
+
async fail(taskUuid, error, machineId) {
|
|
225
|
+
return this._sendEvent("task-fail", taskUuid, { error, machine_id: machineId });
|
|
226
|
+
}
|
|
227
|
+
async _sendEvent(event, taskUuid, extra) {
|
|
228
|
+
const response = await fetch(`${this.realtimexUrl}/webhooks/realtimex`, {
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers: { "Content-Type": "application/json" },
|
|
231
|
+
body: JSON.stringify({
|
|
232
|
+
app_name: this.appName,
|
|
233
|
+
app_id: this.appId,
|
|
234
|
+
event,
|
|
235
|
+
payload: {
|
|
236
|
+
task_uuid: taskUuid,
|
|
237
|
+
...extra
|
|
238
|
+
}
|
|
239
|
+
})
|
|
240
|
+
});
|
|
241
|
+
const data = await response.json();
|
|
242
|
+
if (!response.ok) throw new Error(data.error || `Failed to ${event}`);
|
|
243
|
+
return data;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
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
|
|
262
|
+
* @param port - Port number to check
|
|
263
|
+
* @returns Promise resolving to true if port is available
|
|
264
|
+
*/
|
|
265
|
+
async isPortAvailable(port) {
|
|
266
|
+
return new Promise((resolve) => {
|
|
267
|
+
const server = net.createServer();
|
|
268
|
+
server.once("error", () => resolve(false));
|
|
269
|
+
server.once("listening", () => {
|
|
270
|
+
server.close();
|
|
271
|
+
resolve(true);
|
|
272
|
+
});
|
|
273
|
+
server.listen(port, "127.0.0.1");
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Find an available port starting from the suggested port
|
|
278
|
+
* @param startPort - Starting port number (default: RTX_PORT or defaultPort)
|
|
279
|
+
* @param maxAttempts - Maximum ports to try (default: 100)
|
|
280
|
+
* @returns Promise resolving to an available port number
|
|
281
|
+
* @throws Error if no available port found in range
|
|
282
|
+
*/
|
|
283
|
+
async findAvailablePort(startPort, maxAttempts = 100) {
|
|
284
|
+
const port = startPort ?? this.getSuggestedPort();
|
|
285
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
286
|
+
const currentPort = port + i;
|
|
287
|
+
if (await this.isPortAvailable(currentPort)) {
|
|
288
|
+
return currentPort;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
throw new Error(`No available port found in range ${port}-${port + maxAttempts - 1}`);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Get a ready-to-use port
|
|
295
|
+
* Returns the suggested port if available, otherwise finds the next available port
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* const sdk = new RealtimeXSDK();
|
|
300
|
+
* const port = await sdk.port.getPort();
|
|
301
|
+
* app.listen(port);
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
async getPort() {
|
|
305
|
+
const suggested = this.getSuggestedPort();
|
|
306
|
+
if (await this.isPortAvailable(suggested)) {
|
|
307
|
+
return suggested;
|
|
308
|
+
}
|
|
309
|
+
return this.findAvailablePort(suggested + 1);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
190
313
|
// src/index.ts
|
|
191
314
|
var _RealtimeXSDK = class _RealtimeXSDK {
|
|
192
315
|
constructor(config = {}) {
|
|
@@ -198,6 +321,8 @@ var _RealtimeXSDK = class _RealtimeXSDK {
|
|
|
198
321
|
this.activities = new ActivitiesModule(realtimexUrl, this.appId);
|
|
199
322
|
this.webhook = new WebhookModule(realtimexUrl, this.appName, this.appId);
|
|
200
323
|
this.api = new ApiModule(realtimexUrl);
|
|
324
|
+
this.task = new TaskModule(realtimexUrl, this.appName, this.appId);
|
|
325
|
+
this.port = new PortModule(config.defaultPort);
|
|
201
326
|
}
|
|
202
327
|
/**
|
|
203
328
|
* Get environment variable (works in Node.js and browser)
|
|
@@ -218,6 +343,8 @@ var RealtimeXSDK = _RealtimeXSDK;
|
|
|
218
343
|
0 && (module.exports = {
|
|
219
344
|
ActivitiesModule,
|
|
220
345
|
ApiModule,
|
|
346
|
+
PortModule,
|
|
221
347
|
RealtimeXSDK,
|
|
348
|
+
TaskModule,
|
|
222
349
|
WebhookModule
|
|
223
350
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -158,6 +158,117 @@ var ApiModule = class {
|
|
|
158
158
|
}
|
|
159
159
|
};
|
|
160
160
|
|
|
161
|
+
// src/modules/task.ts
|
|
162
|
+
var TaskModule = class {
|
|
163
|
+
constructor(realtimexUrl, appName, appId) {
|
|
164
|
+
this.realtimexUrl = realtimexUrl.replace(/\/$/, "");
|
|
165
|
+
this.appName = appName;
|
|
166
|
+
this.appId = appId;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Mark task as processing
|
|
170
|
+
*/
|
|
171
|
+
async start(taskUuid, machineId) {
|
|
172
|
+
return this._sendEvent("task-start", taskUuid, { machine_id: machineId });
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Mark task as completed with result
|
|
176
|
+
*/
|
|
177
|
+
async complete(taskUuid, result, machineId) {
|
|
178
|
+
return this._sendEvent("task-complete", taskUuid, { result, machine_id: machineId });
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Mark task as failed with error
|
|
182
|
+
*/
|
|
183
|
+
async fail(taskUuid, error, machineId) {
|
|
184
|
+
return this._sendEvent("task-fail", taskUuid, { error, machine_id: machineId });
|
|
185
|
+
}
|
|
186
|
+
async _sendEvent(event, taskUuid, extra) {
|
|
187
|
+
const response = await fetch(`${this.realtimexUrl}/webhooks/realtimex`, {
|
|
188
|
+
method: "POST",
|
|
189
|
+
headers: { "Content-Type": "application/json" },
|
|
190
|
+
body: JSON.stringify({
|
|
191
|
+
app_name: this.appName,
|
|
192
|
+
app_id: this.appId,
|
|
193
|
+
event,
|
|
194
|
+
payload: {
|
|
195
|
+
task_uuid: taskUuid,
|
|
196
|
+
...extra
|
|
197
|
+
}
|
|
198
|
+
})
|
|
199
|
+
});
|
|
200
|
+
const data = await response.json();
|
|
201
|
+
if (!response.ok) throw new Error(data.error || `Failed to ${event}`);
|
|
202
|
+
return data;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
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
|
|
221
|
+
* @param port - Port number to check
|
|
222
|
+
* @returns Promise resolving to true if port is available
|
|
223
|
+
*/
|
|
224
|
+
async isPortAvailable(port) {
|
|
225
|
+
return new Promise((resolve) => {
|
|
226
|
+
const server = net.createServer();
|
|
227
|
+
server.once("error", () => resolve(false));
|
|
228
|
+
server.once("listening", () => {
|
|
229
|
+
server.close();
|
|
230
|
+
resolve(true);
|
|
231
|
+
});
|
|
232
|
+
server.listen(port, "127.0.0.1");
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Find an available port starting from the suggested port
|
|
237
|
+
* @param startPort - Starting port number (default: RTX_PORT or defaultPort)
|
|
238
|
+
* @param maxAttempts - Maximum ports to try (default: 100)
|
|
239
|
+
* @returns Promise resolving to an available port number
|
|
240
|
+
* @throws Error if no available port found in range
|
|
241
|
+
*/
|
|
242
|
+
async findAvailablePort(startPort, maxAttempts = 100) {
|
|
243
|
+
const port = startPort ?? this.getSuggestedPort();
|
|
244
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
245
|
+
const currentPort = port + i;
|
|
246
|
+
if (await this.isPortAvailable(currentPort)) {
|
|
247
|
+
return currentPort;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
throw new Error(`No available port found in range ${port}-${port + maxAttempts - 1}`);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get a ready-to-use port
|
|
254
|
+
* Returns the suggested port if available, otherwise finds the next available port
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* const sdk = new RealtimeXSDK();
|
|
259
|
+
* const port = await sdk.port.getPort();
|
|
260
|
+
* app.listen(port);
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
async getPort() {
|
|
264
|
+
const suggested = this.getSuggestedPort();
|
|
265
|
+
if (await this.isPortAvailable(suggested)) {
|
|
266
|
+
return suggested;
|
|
267
|
+
}
|
|
268
|
+
return this.findAvailablePort(suggested + 1);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
|
|
161
272
|
// src/index.ts
|
|
162
273
|
var _RealtimeXSDK = class _RealtimeXSDK {
|
|
163
274
|
constructor(config = {}) {
|
|
@@ -169,6 +280,8 @@ var _RealtimeXSDK = class _RealtimeXSDK {
|
|
|
169
280
|
this.activities = new ActivitiesModule(realtimexUrl, this.appId);
|
|
170
281
|
this.webhook = new WebhookModule(realtimexUrl, this.appName, this.appId);
|
|
171
282
|
this.api = new ApiModule(realtimexUrl);
|
|
283
|
+
this.task = new TaskModule(realtimexUrl, this.appName, this.appId);
|
|
284
|
+
this.port = new PortModule(config.defaultPort);
|
|
172
285
|
}
|
|
173
286
|
/**
|
|
174
287
|
* Get environment variable (works in Node.js and browser)
|
|
@@ -188,6 +301,8 @@ var RealtimeXSDK = _RealtimeXSDK;
|
|
|
188
301
|
export {
|
|
189
302
|
ActivitiesModule,
|
|
190
303
|
ApiModule,
|
|
304
|
+
PortModule,
|
|
191
305
|
RealtimeXSDK,
|
|
306
|
+
TaskModule,
|
|
192
307
|
WebhookModule
|
|
193
308
|
};
|