@stubber/virtual-worker 1.5.1 → 1.5.2

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.
@@ -21,7 +21,7 @@ export const browser_manage_sessions = async (params, stubber_context) => {
21
21
  return await clear_chromium_sessions(session_ids);
22
22
  } else if (operation === "delete_all") {
23
23
  const sessions_result = await list_chromium_sessions();
24
- const all_session_ids = sessions_result.payload.sessions;
24
+ const all_session_ids = sessions_result.payload.session_ids;
25
25
  return await clear_chromium_sessions(all_session_ids);
26
26
  } else {
27
27
  return create_error_conceptual({ message: "Invalid operation. Must be 'list', 'delete', or 'delete_all'." });
@@ -5,18 +5,11 @@ import { all_commands } from "./index.js";
5
5
  import { create_success } from "#app/functions/create_success.js";
6
6
  import { create_error_technical } from "#app/functions/create_error_technical.js";
7
7
  import { create_error_conceptual } from "#app/functions/create_error_conceptual.js";
8
+ import * as z from "zod";
8
9
 
9
10
  /**
10
- * @typedef {object} Command
11
- * @property {string} commandtype - The type of command to run.
12
- * @property {object} [params] - The parameters for the command.
13
- * @property {bool} [continue_on_error] - Whether to continue on error.
14
- * @property {number} [__order] - The order in which to run the command.
15
- * @property {string[]} [conditions] - Array of jsonata conditions to evaluate.
16
- */
17
-
18
- /**
19
- * @param {{[key: string]: Command}} commands
11
+ * @param {object} task
12
+ * @param {z.infer<typeof import("#app/controllers/virtual_worker_task.js").params_schema>} task.params
20
13
  * @param {object} _stubber - The context this task is being ran in.
21
14
  */
22
15
  export const run_commands = async (task, _stubber) => {
@@ -41,6 +34,10 @@ export const run_commands = async (task, _stubber) => {
41
34
  for (const [command_name, command] of command_entries) {
42
35
  console.log(`Starting command: ${command_name}`, command);
43
36
 
37
+ if (task.params.browser_session_id) {
38
+ command.params.session_id = task.params.browser_session_id;
39
+ }
40
+
44
41
  // Check if the command has conditions and evaluate them
45
42
  const conditions_met = await evaluate_command_conditions(command, cloned_context);
46
43
  if (!conditions_met) {
@@ -2,6 +2,7 @@ import { run_commands } from "#app/commands/run_commands.js";
2
2
  import { task_schema } from "#lib/interfaces/http/routers/stubber_task_schema.js";
3
3
  import * as z from "zod";
4
4
  import { worker_status } from "./virtual_worker_status.js";
5
+ import { config } from "#root/config/main.js";
5
6
 
6
7
  const command_schema = z.looseObject({
7
8
  commandtype: z.string().min(1),
@@ -11,8 +12,9 @@ const command_schema = z.looseObject({
11
12
  conditions: z.array(z.string().or(z.boolean())).optional(),
12
13
  });
13
14
 
14
- const params_schema = z.looseObject({
15
+ export const params_schema = z.looseObject({
15
16
  commands: z.record(z.string(), command_schema),
17
+ browser_session_id: z.string().min(1).optional(),
16
18
  });
17
19
 
18
20
  /**
@@ -27,5 +29,22 @@ export const virtual_worker_task = async (task) => {
27
29
 
28
30
  const task_result = await run_commands(task, stubber);
29
31
 
32
+ let vnc_url = `${config.vnc_origin}/vnc?worker_name=${encodeURIComponent(config.worker_name)}&orguuid=${
33
+ task._stubber.orguuid
34
+ }`;
35
+
36
+ if (config.api_key) {
37
+ vnc_url += `&api_key=${config.api_key}`;
38
+ }
39
+
40
+ const worker_name = config.worker_name;
41
+
42
+ task_result.details = {
43
+ worker_info: {
44
+ vnc_url: config.vnc_origin ? vnc_url : null,
45
+ worker_name,
46
+ },
47
+ };
48
+
30
49
  return task_result;
31
50
  };
@@ -22,6 +22,8 @@ export const get_chromium_page = async (params, stubber_context) => {
22
22
  return create_error_conceptual({ message: "session_id is required to get a Chromium page." });
23
23
  }
24
24
 
25
+ console.log("get_chromium_page called with session_id:", session_id);
26
+
25
27
  let slow_mo = params?.slow_mo || process.env.SLOW_MO || 0;
26
28
 
27
29
  slow_mo = Number(slow_mo);
@@ -40,6 +42,10 @@ export const get_chromium_page = async (params, stubber_context) => {
40
42
  // eslint-disable-next-line id-match
41
43
  slowMo: slow_mo,
42
44
  });
45
+ browser.on("disconnected", () => {
46
+ console.log("Chromium browser disconnected. Cleaning up browser instance.");
47
+ browser = null;
48
+ });
43
49
  }
44
50
 
45
51
  // Create a new context per stubref if not already created
@@ -52,6 +58,10 @@ export const get_chromium_page = async (params, stubber_context) => {
52
58
  contexts[session_id] = {
53
59
  context,
54
60
  };
61
+ context.on("close", () => {
62
+ console.log(`Chromium context for session_id=${session_id} closed. Cleaning up.`);
63
+ delete contexts[session_id];
64
+ });
55
65
  }
56
66
 
57
67
  const context = contexts[session_id].context;
@@ -71,27 +81,27 @@ export const list_chromium_sessions = () => {
71
81
  return create_success({
72
82
  message: "Successfully listed Chromium sessions.",
73
83
  payload: {
74
- sessions: Object.keys(contexts),
84
+ session_ids: Object.keys(contexts),
75
85
  },
76
86
  });
77
87
  };
78
88
 
79
89
  export const clear_chromium_sessions = async (session_ids) => {
80
90
  try {
81
- let cleared_sessions = [];
91
+ let cleared_session_ids = [];
82
92
  for (const session_id of session_ids) {
83
93
  const context_entry = contexts[session_id];
84
94
  if (context_entry) {
85
95
  await context_entry.context.close();
86
96
  delete contexts[session_id];
87
- cleared_sessions.push(session_id);
97
+ cleared_session_ids.push(session_id);
88
98
  }
89
99
  }
90
100
  return create_success({
91
101
  message: "Successfully cleared specified Chromium sessions.",
92
102
  payload: {
93
- cleared_sessions,
94
- sessions: Object.keys(contexts),
103
+ cleared_session_ids,
104
+ session_ids: Object.keys(contexts),
95
105
  },
96
106
  });
97
107
  } catch (error) {
package/devel/tests.sh ADDED
@@ -0,0 +1,24 @@
1
+ curl -X POST "http://localhost:3000/api/v1/task-gateway/virtual_worker_send_commands" \
2
+ -H "Content-Type: application/json" \
3
+ -H "stubber-virtual-worker-apikey: 123-456-789" \
4
+ -d '{
5
+ "task": {
6
+ "tasktype": "virtual_worker_send_commands",
7
+ "task_name": "virtual_worker_send_commands",
8
+ "params": {
9
+ "commands": {
10
+ "command_1": {
11
+ "commandtype": "browser_navigate",
12
+ "params": {
13
+ "url": "https://fast.com",
14
+ "session_id": "test-session-002"
15
+ }
16
+ }
17
+ }
18
+ },
19
+ "_stubber": {
20
+ "orguuid": "c8cfd7f1-8015-43ff-8878-d22c136a2325",
21
+ "stubref": "my-stub-ref"
22
+ }
23
+ }
24
+ }'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stubber/virtual-worker",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Template to easily create a node app and keep development standards",
5
5
  "main": "app.js",
6
6
  "directories": {
package/app_config.js DELETED
@@ -1,6 +0,0 @@
1
- export const config = {
2
- WORKER_CONNECT_URL: process.env.WORKER_CONNECT_URL,
3
- WORKER_SIGNATURE: process.env.WORKER_SIGNATURE,
4
- WORKER_WORKERUUID: process.env.WORKER_WORKERUUID,
5
- WORKER_GROUPS: process.env.WORKER_GROUPS?.split(",") || [],
6
- };