forked from angrytidy/ada-snapfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-worker-response.js
More file actions
72 lines (57 loc) · 2.04 KB
/
debug-worker-response.js
File metadata and controls
72 lines (57 loc) · 2.04 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
const fetch = require('node-fetch');
async function debugWorkerResponse() {
const workerUrl = 'http://192.70.246.109:9999/api/scan';
const testUrl = 'https://example.com';
console.log('Testing worker response...');
console.log('Worker URL:', workerUrl);
console.log('Test URL:', testUrl);
try {
const response = await fetch(workerUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({ url: testUrl })
});
console.log('Response status:', response.status);
console.log('Response headers:', Object.fromEntries(response.headers.entries()));
if (!response.ok) {
const errorText = await response.text();
console.error('Error response:', errorText);
return;
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
console.log('\n--- Raw Response Data ---');
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
buffer += chunk;
console.log('Raw chunk:', JSON.stringify(chunk));
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
const jsonStr = line.slice(6);
console.log('\n--- Parsing JSON ---');
console.log('JSON string:', jsonStr);
try {
const data = JSON.parse(jsonStr);
console.log('✅ Parsed successfully:', data);
} catch (parseError) {
console.error('❌ JSON parse error:', parseError.message);
console.error('Problematic string:', jsonStr);
console.error('String length:', jsonStr.length);
console.error('String bytes:', Buffer.from(jsonStr).toString('hex'));
}
}
}
}
} catch (error) {
console.error('Request failed:', error);
}
}
debugWorkerResponse();