-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04-cli-tools.ts
More file actions
291 lines (248 loc) · 10.7 KB
/
04-cli-tools.ts
File metadata and controls
291 lines (248 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* 04 - CLI Tools Example
*
* This example demonstrates how to use the CLI tools for
* webhook development, testing, and monitoring.
*/
import { spawn } from 'child_process';
import path from 'path';
// CLI tool paths
const CLI_PATH = path.join(__dirname, '../dist/cli');
/**
* Example: Using CLI tools programmatically
*/
export class CLIToolsDemo {
/**
* Test webhook endpoint using CLI
*/
static async testWebhookEndpoint(url: string, provider: string = 'stripe') {
console.log(`🧪 Testing webhook endpoint: ${url}`);
return new Promise((resolve, reject) => {
const testProcess = spawn('node', [
`${CLI_PATH}/test-webhook.js`,
'--url', url,
'--provider', provider,
'--payload', JSON.stringify({ test: 'data', timestamp: Date.now() })
]);
let output = '';
let errorOutput = '';
testProcess.stdout.on('data', (data) => {
output += data.toString();
console.log(`📤 ${data.toString().trim()}`);
});
testProcess.stderr.on('data', (data) => {
errorOutput += data.toString();
console.error(`❌ ${data.toString().trim()}`);
});
testProcess.on('close', (code) => {
if (code === 0) {
console.log('✅ Webhook test completed successfully');
resolve(output);
} else {
console.error(`❌ Webhook test failed with code ${code}`);
reject(new Error(errorOutput || `Process exited with code ${code}`));
}
});
});
}
/**
* Generate webhook configuration using CLI
*/
static async generateConfig(provider: string, outputPath: string) {
console.log(`⚙️ Generating configuration for ${provider}`);
return new Promise((resolve, reject) => {
const configProcess = spawn('node', [
`${CLI_PATH}/generate-config.js`,
'--provider', provider,
'--output', outputPath,
'--format', 'json'
]);
let output = '';
configProcess.stdout.on('data', (data) => {
output += data.toString();
console.log(`📝 ${data.toString().trim()}`);
});
configProcess.stderr.on('data', (data) => {
console.error(`❌ ${data.toString().trim()}`);
});
configProcess.on('close', (code) => {
if (code === 0) {
console.log(`✅ Configuration generated: ${outputPath}`);
resolve(output);
} else {
reject(new Error(`Config generation failed with code ${code}`));
}
});
});
}
/**
* Monitor webhook performance using CLI
*/
static async monitorWebhooks(duration: number = 30) {
console.log(`📊 Monitoring webhooks for ${duration} seconds`);
return new Promise((resolve, reject) => {
const monitorProcess = spawn('node', [
`${CLI_PATH}/monitor.js`,
'--duration', duration.toString(),
'--format', 'json',
'--output', './logs/webhook-monitor.log'
]);
let output = '';
monitorProcess.stdout.on('data', (data) => {
const line = data.toString().trim();
output += line + '\n';
// Parse monitoring output
try {
const monitorData = JSON.parse(line);
console.log(`📈 ${monitorData.timestamp}: ${monitorData.metric} = ${monitorData.value}`);
} catch {
console.log(`📊 ${line}`);
}
});
monitorProcess.stderr.on('data', (data) => {
console.error(`❌ ${data.toString().trim()}`);
});
monitorProcess.on('close', (code) => {
if (code === 0) {
console.log('✅ Monitoring completed');
resolve(output);
} else {
reject(new Error(`Monitoring failed with code ${code}`));
}
});
});
}
/**
* Validate webhook signatures using CLI
*/
static async validateSignature(payload: string, signature: string, secret: string, provider: string = 'stripe') {
console.log(`🔐 Validating webhook signature for ${provider}`);
return new Promise((resolve, reject) => {
const validateProcess = spawn('node', [
`${CLI_PATH}/validate-signature.js`,
'--provider', provider,
'--payload', payload,
'--signature', signature,
'--secret', secret
]);
let output = '';
validateProcess.stdout.on('data', (data) => {
output += data.toString();
console.log(`🔍 ${data.toString().trim()}`);
});
validateProcess.stderr.on('data', (data) => {
console.error(`❌ ${data.toString().trim()}`);
});
validateProcess.on('close', (code) => {
if (code === 0) {
console.log('✅ Signature validation completed');
resolve(output);
} else {
reject(new Error(`Signature validation failed with code ${code}`));
}
});
});
}
/**
* Run comprehensive CLI demo
*/
static async runDemo() {
console.log('🚀 Starting CLI Tools Demo\n');
try {
// 1. Generate configuration
console.log('=== Step 1: Generate Configuration ===');
await this.generateConfig('stripe', './examples/generated-stripe-config.json');
console.log('');
// 2. Test webhook endpoint (assuming a server is running)
console.log('=== Step 2: Test Webhook Endpoint ===');
try {
await this.testWebhookEndpoint('http://localhost:3000/webhooks/stripe', 'stripe');
} catch (error) {
console.log('⚠️ Webhook endpoint test failed (server may not be running)');
console.log(' Start a webhook server first with: npm run example:basic');
}
console.log('');
// 3. Validate signature
console.log('=== Step 3: Validate Signature ===');
const testPayload = JSON.stringify({ test: 'data', timestamp: Date.now() });
const testSignature = 'test_signature';
const testSecret = 'whsec_test_secret';
try {
await this.validateSignature(testPayload, testSignature, testSecret, 'stripe');
} catch (error) {
console.log('⚠️ Signature validation failed (expected for demo data)');
}
console.log('');
// 4. Monitor webhooks (short duration for demo)
console.log('=== Step 4: Monitor Webhooks ===');
try {
await this.monitorWebhooks(5); // 5 seconds for demo
} catch (error) {
console.log('⚠️ Monitoring failed (may need active webhook traffic)');
}
console.log('\n✅ CLI Tools Demo completed!');
console.log('\n📚 Available CLI commands:');
console.log(' hook-engine test --url <url> --provider <provider>');
console.log(' hook-engine generate --provider <provider> --output <file>');
console.log(' hook-engine monitor --duration <seconds>');
console.log(' hook-engine validate --provider <provider> --payload <data> --signature <sig> --secret <secret>');
} catch (error) {
console.error('❌ CLI Demo failed:', (error as Error).message);
}
}
}
/**
* CLI Usage Examples
*/
export const CLI_EXAMPLES = {
// Test webhook endpoints
testWebhook: {
description: 'Test a webhook endpoint with sample data',
commands: [
'hook-engine test --url http://localhost:3000/webhooks/stripe --provider stripe',
'hook-engine test --url http://localhost:3001/webhooks/github --provider github --payload \'{"action":"opened","pull_request":{"id":1}}\'',
'hook-engine test --url http://localhost:3002/webhooks/shopify --provider shopify --headers \'{"X-Shopify-Topic":"orders/create"}\''
]
},
// Generate configurations
generateConfig: {
description: 'Generate webhook configurations for different providers',
commands: [
'hook-engine generate --provider stripe --output ./config/stripe.json',
'hook-engine generate --provider github --output ./config/github.json --format yaml',
'hook-engine generate --provider all --output ./config/ --format json'
]
},
// Monitor webhooks
monitor: {
description: 'Monitor webhook performance and metrics',
commands: [
'hook-engine monitor --duration 60 --format json',
'hook-engine monitor --output ./logs/metrics.log --interval 5',
'hook-engine monitor --providers stripe,github --format table'
]
},
// Validate signatures
validateSignature: {
description: 'Validate webhook signatures',
commands: [
'hook-engine validate --provider stripe --payload \'{"data":"test"}\' --signature "t=123,v1=abc" --secret "whsec_secret"',
'hook-engine validate --provider github --payload \'{"action":"push"}\' --signature "sha256=hash" --secret "github_secret"',
'hook-engine validate --provider shopify --payload \'{"id":123}\' --signature "hmac_hash" --secret "shopify_secret"'
]
},
// Development helpers
development: {
description: 'Development and debugging tools',
commands: [
'hook-engine dev --port 3000 --provider stripe --auto-reload',
'hook-engine debug --log-level debug --output ./logs/debug.log',
'hook-engine benchmark --provider stripe --requests 100 --concurrent 10'
]
}
};
// Run demo if this file is executed directly
if (require.main === module) {
CLIToolsDemo.runDemo().catch(console.error);
}
export default CLIToolsDemo;