chrome-cdp-cli 1.8.1 → 2.0.0

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.
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.demonstrateConfigurationSystem = demonstrateConfigurationSystem;
4
+ const ConfigurationManager_1 = require("./ConfigurationManager");
5
+ const utils_1 = require("./utils");
6
+ async function demonstrateConfigurationSystem() {
7
+ console.log('=== Configuration Management System Demo ===\n');
8
+ const configManager = new ConfigurationManager_1.ConfigurationManager();
9
+ console.log('1. Creating configuration sources...');
10
+ const cliOptions = {
11
+ verbose: true,
12
+ port: 8080,
13
+ outputFormat: 'json'
14
+ };
15
+ const sources = await configManager.createConfigurationSources(cliOptions);
16
+ console.log(`Created ${sources.length} configuration sources:`);
17
+ sources.forEach(source => {
18
+ console.log(` - ${source.type} (priority: ${source.priority}) from ${source.source}`);
19
+ });
20
+ console.log('\n2. Loading and merging configuration...');
21
+ const config = await configManager.loadConfiguration(sources);
22
+ console.log('Final configuration:', JSON.stringify(config, null, 2));
23
+ console.log('\n3. Validating configuration...');
24
+ const validation = configManager.validateConfiguration(config);
25
+ console.log(`Validation result: ${validation.valid ? 'VALID' : 'INVALID'}`);
26
+ if (validation.errors.length > 0) {
27
+ console.log('Errors:', validation.errors);
28
+ }
29
+ if (validation.warnings.length > 0) {
30
+ console.log('Warnings:', validation.warnings);
31
+ }
32
+ console.log('\n4. Resolving specific configuration values...');
33
+ try {
34
+ const host = configManager.resolveConfigValue('host', 'string');
35
+ const port = configManager.resolveConfigValue('port', 'number');
36
+ const verbose = configManager.resolveConfigValue('verbose', 'boolean');
37
+ console.log(`Host: ${host}`);
38
+ console.log(`Port: ${port}`);
39
+ console.log(`Verbose: ${verbose}`);
40
+ }
41
+ catch (error) {
42
+ console.error('Error resolving config values:', error);
43
+ }
44
+ console.log('\n5. Environment configuration example...');
45
+ const envSource = configManager.loadEnvironmentConfig();
46
+ console.log('Environment variables found:', envSource.data);
47
+ console.log('\n6. Configuration file template (YAML):');
48
+ console.log('---');
49
+ console.log((0, utils_1.getConfigTemplate)('yaml'));
50
+ console.log('---');
51
+ }
52
+ if (require.main === module) {
53
+ demonstrateConfigurationSystem().catch(console.error);
54
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.ConfigurationManager = void 0;
18
+ __exportStar(require("./interfaces"), exports);
19
+ var ConfigurationManager_1 = require("./ConfigurationManager");
20
+ Object.defineProperty(exports, "ConfigurationManager", { enumerable: true, get: function () { return ConfigurationManager_1.ConfigurationManager; } });
21
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_CONFIG_PATHS = exports.DEFAULT_CONFIG_FILES = exports.ENV_PREFIX = exports.CONFIG_PRIORITIES = exports.DEFAULT_CLI_CONFIG = void 0;
4
+ exports.DEFAULT_CLI_CONFIG = {
5
+ host: 'localhost',
6
+ port: 9222,
7
+ timeout: 30000,
8
+ outputFormat: 'text',
9
+ verbose: false,
10
+ quiet: false,
11
+ debug: false,
12
+ pluginDirs: [],
13
+ aliases: {},
14
+ commands: {}
15
+ };
16
+ exports.CONFIG_PRIORITIES = {
17
+ default: 0,
18
+ file: 10,
19
+ env: 20,
20
+ cli: 30
21
+ };
22
+ exports.ENV_PREFIX = 'CHROME_CDP_CLI_';
23
+ exports.DEFAULT_CONFIG_FILES = [
24
+ '.chrome-cdp-cli.yaml',
25
+ '.chrome-cdp-cli.yml',
26
+ '.chrome-cdp-cli.json',
27
+ 'chrome-cdp-cli.config.yaml',
28
+ 'chrome-cdp-cli.config.yml',
29
+ 'chrome-cdp-cli.config.json'
30
+ ];
31
+ exports.DEFAULT_CONFIG_PATHS = [
32
+ process.cwd(),
33
+ process.env.HOME || process.env.USERPROFILE || '',
34
+ '/etc'
35
+ ];
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createDefaultConfigSources = createDefaultConfigSources;
37
+ exports.validateConfigFile = validateConfigFile;
38
+ exports.getConfigTemplate = getConfigTemplate;
39
+ exports.normalizeConfigPath = normalizeConfigPath;
40
+ exports.configFileExists = configFileExists;
41
+ exports.createConfigFile = createConfigFile;
42
+ exports.getEnvironmentVariables = getEnvironmentVariables;
43
+ const fs = __importStar(require("fs"));
44
+ const path = __importStar(require("path"));
45
+ const interfaces_1 = require("./interfaces");
46
+ const ConfigurationManager_1 = require("./ConfigurationManager");
47
+ async function createDefaultConfigSources(cliOptions = {}) {
48
+ const configManager = new ConfigurationManager_1.ConfigurationManager();
49
+ return await configManager.createConfigurationSources(cliOptions);
50
+ }
51
+ async function validateConfigFile(filePath) {
52
+ const configManager = new ConfigurationManager_1.ConfigurationManager();
53
+ try {
54
+ await fs.promises.access(filePath, fs.constants.R_OK);
55
+ const config = await configManager.loadYAMLConfig(filePath);
56
+ const sources = [
57
+ {
58
+ type: 'default',
59
+ priority: interfaces_1.CONFIG_PRIORITIES.default,
60
+ data: { ...interfaces_1.DEFAULT_CLI_CONFIG },
61
+ source: 'defaults'
62
+ },
63
+ {
64
+ type: 'file',
65
+ priority: interfaces_1.CONFIG_PRIORITIES.file,
66
+ data: config,
67
+ source: filePath
68
+ }
69
+ ];
70
+ const mergedConfig = await configManager.loadConfiguration(sources);
71
+ return configManager.validateConfiguration(mergedConfig);
72
+ }
73
+ catch (error) {
74
+ return {
75
+ valid: false,
76
+ errors: [{
77
+ field: 'file',
78
+ message: error instanceof Error ? error.message : String(error),
79
+ code: 'FILE_VALIDATION_ERROR',
80
+ suggestion: 'Check file syntax and permissions'
81
+ }],
82
+ warnings: []
83
+ };
84
+ }
85
+ }
86
+ function getConfigTemplate(format = 'yaml') {
87
+ const template = {
88
+ profile: 'development',
89
+ host: 'localhost',
90
+ port: 9222,
91
+ timeout: 30000,
92
+ outputFormat: 'text',
93
+ verbose: false,
94
+ quiet: false,
95
+ debug: false,
96
+ plugins: {
97
+ directories: [
98
+ '~/.chrome-cdp-cli/plugins',
99
+ './plugins'
100
+ ]
101
+ },
102
+ aliases: {
103
+ ss: 'screenshot',
104
+ snap: 'snapshot',
105
+ js: 'eval'
106
+ },
107
+ profiles: {
108
+ development: {
109
+ host: 'localhost',
110
+ port: 9222,
111
+ debug: true,
112
+ verbose: true
113
+ },
114
+ production: {
115
+ host: 'chrome-server',
116
+ port: 9222,
117
+ quiet: true,
118
+ timeout: 60000
119
+ }
120
+ },
121
+ commands: {
122
+ screenshot: {
123
+ timeout: 10000,
124
+ defaults: {
125
+ format: 'png',
126
+ quality: 90,
127
+ fullPage: false
128
+ }
129
+ },
130
+ eval: {
131
+ timeout: 5000,
132
+ defaults: {
133
+ awaitPromise: true,
134
+ returnByValue: true
135
+ }
136
+ }
137
+ }
138
+ };
139
+ if (format === 'json') {
140
+ return JSON.stringify(template, null, 2);
141
+ }
142
+ return `# Chrome CDP CLI Configuration File
143
+ # This file supports YAML syntax for better readability
144
+
145
+ # Active profile (can be overridden with --profile)
146
+ profile: development
147
+
148
+ # Global defaults
149
+ host: localhost
150
+ port: 9222
151
+ timeout: 30000
152
+ outputFormat: text
153
+ verbose: false
154
+ quiet: false
155
+ debug: false
156
+
157
+ # Plugin configuration
158
+ plugins:
159
+ directories:
160
+ - ~/.chrome-cdp-cli/plugins
161
+ - ./plugins
162
+
163
+ # Command aliases
164
+ aliases:
165
+ ss: screenshot
166
+ snap: snapshot
167
+ js: eval
168
+
169
+ # Profile-specific configurations
170
+ profiles:
171
+ development:
172
+ host: localhost
173
+ port: 9222
174
+ debug: true
175
+ verbose: true
176
+
177
+ production:
178
+ host: chrome-server
179
+ port: 9222
180
+ quiet: true
181
+ timeout: 60000
182
+
183
+ # Command-specific defaults
184
+ commands:
185
+ screenshot:
186
+ timeout: 10000
187
+ defaults:
188
+ format: png
189
+ quality: 90
190
+ fullPage: false
191
+
192
+ eval:
193
+ timeout: 5000
194
+ defaults:
195
+ awaitPromise: true
196
+ returnByValue: true
197
+ `;
198
+ }
199
+ function normalizeConfigPath(configPath) {
200
+ if (configPath.startsWith('~/')) {
201
+ const homeDir = process.env.HOME || process.env.USERPROFILE || '';
202
+ return path.join(homeDir, configPath.slice(2));
203
+ }
204
+ if (!path.isAbsolute(configPath)) {
205
+ return path.resolve(process.cwd(), configPath);
206
+ }
207
+ return configPath;
208
+ }
209
+ async function configFileExists(filePath) {
210
+ try {
211
+ await fs.promises.access(normalizeConfigPath(filePath), fs.constants.R_OK);
212
+ return true;
213
+ }
214
+ catch {
215
+ return false;
216
+ }
217
+ }
218
+ async function createConfigFile(filePath, format = 'yaml') {
219
+ const normalizedPath = normalizeConfigPath(filePath);
220
+ const content = getConfigTemplate(format);
221
+ const dir = path.dirname(normalizedPath);
222
+ await fs.promises.mkdir(dir, { recursive: true });
223
+ await fs.promises.writeFile(normalizedPath, content, 'utf-8');
224
+ }
225
+ function getEnvironmentVariables() {
226
+ return [
227
+ 'CHROME_CDP_CLI_HOST',
228
+ 'CHROME_CDP_CLI_PORT',
229
+ 'CHROME_CDP_CLI_TIMEOUT',
230
+ 'CHROME_CDP_CLI_OUTPUT_FORMAT',
231
+ 'CHROME_CDP_CLI_VERBOSE',
232
+ 'CHROME_CDP_CLI_QUIET',
233
+ 'CHROME_CDP_CLI_DEBUG',
234
+ 'CHROME_CDP_CLI_PROFILE',
235
+ 'CHROME_CDP_CLI_CONFIG_FILE',
236
+ 'CHROME_CDP_CLI_PLUGIN_DIRS'
237
+ ];
238
+ }
@@ -159,7 +159,7 @@ class EvaluateScriptHandler {
159
159
  }
160
160
  let value = commandResult.result?.value;
161
161
  if (commandResult.result?.type === 'undefined') {
162
- value = undefined;
162
+ value = '';
163
163
  }
164
164
  else if (commandResult.result?.unserializableValue) {
165
165
  value = commandResult.result.unserializableValue;
@@ -227,6 +227,10 @@ class EvaluateScriptHandler {
227
227
  }
228
228
  }
229
229
  async executeScript(client, expression, awaitPromise, returnByValue) {
230
+ const consoleHandler = (params) => {
231
+ this.handleConsoleOutput(params);
232
+ };
233
+ client.on('Runtime.consoleAPICalled', consoleHandler);
230
234
  try {
231
235
  const response = (await client.send('Runtime.evaluate', {
232
236
  expression,
@@ -261,6 +265,52 @@ class EvaluateScriptHandler {
261
265
  error: `CDP command failed: ${error instanceof Error ? error.message : String(error)}`
262
266
  };
263
267
  }
268
+ finally {
269
+ client.off('Runtime.consoleAPICalled', consoleHandler);
270
+ }
271
+ }
272
+ handleConsoleOutput(params) {
273
+ try {
274
+ const consoleParams = params;
275
+ const messageText = this.formatConsoleArgs(consoleParams.args || []);
276
+ const messageType = this.mapConsoleType(consoleParams.type);
277
+ if (messageType === 'log' || messageType === 'info' || messageType === 'debug') {
278
+ process.stdout.write(messageText + '\n');
279
+ }
280
+ else {
281
+ process.stderr.write(messageText + '\n');
282
+ }
283
+ }
284
+ catch (error) {
285
+ this.logger.debug('Error handling console output:', error);
286
+ }
287
+ }
288
+ formatConsoleArgs(args) {
289
+ return args.map(arg => {
290
+ if (arg.value !== undefined) {
291
+ if (typeof arg.value === 'string') {
292
+ return arg.value;
293
+ }
294
+ return JSON.stringify(arg.value);
295
+ }
296
+ return arg.description || '';
297
+ }).join(' ');
298
+ }
299
+ mapConsoleType(cdpType) {
300
+ switch (cdpType) {
301
+ case 'log':
302
+ return 'log';
303
+ case 'info':
304
+ return 'info';
305
+ case 'warning':
306
+ return 'warn';
307
+ case 'error':
308
+ return 'error';
309
+ case 'debug':
310
+ return 'debug';
311
+ default:
312
+ return 'log';
313
+ }
264
314
  }
265
315
  formatException(exceptionDetails) {
266
316
  if (!exceptionDetails) {
@@ -285,7 +335,10 @@ class EvaluateScriptHandler {
285
335
  }
286
336
  formatResult(result) {
287
337
  if (!result) {
288
- return 'undefined';
338
+ return '';
339
+ }
340
+ if (result.type === 'undefined') {
341
+ return '';
289
342
  }
290
343
  if (result.value !== undefined) {
291
344
  return result.value;