Skip to content

Commit 8d56793

Browse files
bamerfBamdad Erfanian
andauthored
fix(webplayer): CORE-13314 - update example repo (#15)
* fix(webplayer): CORE-13314 - fix handling requests * fix(webplayer): CORE-13314 - added http agent * fix(webplayer): CORE-13314 - clean up --------- Co-authored-by: Bamdad Erfanian <[email protected]>
1 parent 10c31c3 commit 8d56793

File tree

2 files changed

+104
-31
lines changed

2 files changed

+104
-31
lines changed

webplayer-example-vanilla-js/public/app.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { apiToken } from './token.js';
2-
32
const CorelliumWebplayer = window.CorelliumWebplayer.default;
43

5-
const instanceId = 'Your device ID';
4+
const instanceId = 'The device ID goes here';
65
const corelliumDomain = 'https://app.corellium.co';
7-
const projectId = 'Your project ID';
6+
const projectId = 'the project ID goes here';
87
const features = {
98
apps: true,
109
console: true,
@@ -35,21 +34,40 @@ const containerId = 'container';
3534
document.addEventListener('DOMContentLoaded', async function (event) {
3635
try {
3736
// get JWT using token
38-
const res = await fetch('http://localhost:8000/api/auth', {
37+
console.log('Starting fetch request...');
38+
39+
const response = await fetch('http://localhost:8000/api/auth', {
3940
method: 'POST',
4041
headers: {
4142
'Content-Type': 'application/json',
43+
Accept: 'application/json',
4244
},
4345
body: JSON.stringify({
4446
token: apiToken,
4547
instanceId,
4648
projectId,
4749
features,
4850
}),
51+
signal: AbortSignal.timeout(10000), // 10 second timeout
4952
});
5053

51-
const { token, ...data } = await res.json();
52-
console.log('token', token, data);
54+
console.log('Fetch completed, status:', response.status);
55+
56+
if (!response.ok) {
57+
const errorText = await response.text();
58+
console.error('Server responded with error:', {
59+
status: response.status,
60+
statusText: response.statusText,
61+
body: errorText,
62+
});
63+
throw new Error(`HTTP error! status: ${response.status}`);
64+
}
65+
66+
const responseData = await response.json();
67+
console.log('Response data received:', responseData);
68+
69+
const { token, ...data } = responseData;
70+
console.log('Token extracted:', token ? 'Token exists' : 'No token');
5371

5472
// now that we have a JWT, set up the webplayer
5573
// pass the id for the div that will hold the iframe as `containerId`
@@ -61,14 +79,18 @@ const containerId = 'container';
6179
});
6280

6381
webplayer.on('success', (data) => {
64-
console.log('data', data);
82+
console.log('Webplayer success:', data);
6583
});
6684

6785
webplayer.on('error', (data) => {
68-
console.error('err', data);
86+
console.error('Webplayer error:', data);
6987
});
70-
} catch (err) {
71-
console.log('server err :>> ', err);
88+
} catch (error) {
89+
console.error('Request failed:', error);
90+
if (error.name === 'AbortError') {
91+
console.error('Request timed out');
92+
}
93+
throw error;
7294
}
7395
});
7496
})();

webplayer-example-vanilla-js/server.js

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require('express');
2-
const fetch = require('node-fetch');
2+
const fetch = (...args) =>
3+
import('node-fetch').then(({ default: fetch }) => fetch(...args));
34
const app = express();
45
const https = require('https');
56
const cors = require('cors');
@@ -14,42 +15,92 @@ app.use(
1415
cors({
1516
origin: '*',
1617
methods: ['GET', 'POST'],
18+
allowedHeaders: ['Content-Type', 'Authorization', 'Accept'],
19+
credentials: true,
20+
exposedHeaders: ['Content-Type', 'Authorization'],
1721
})
1822
);
1923

2024
app.use(express.static('public'));
2125

2226
app.post('/api/auth', jsonParser, async (req, res) => {
23-
const loginUrl = 'https://ci-1.corellium.co/api/v1/webplayer'; // this domain would be https://app.corellium.co/api/v1/webplayer in production
27+
console.log('Received auth request at:', new Date().toISOString());
28+
console.log('Request headers:', req.headers);
2429

25-
const httpsAgent = new https.Agent({
26-
rejectUnauthorized: false,
30+
// Change this URL to your domain URL
31+
const baseUrl = ''; // example - https://app.corellium.co in production
32+
33+
const loginUrl = `${baseUrl}/api/v1/webplayer`;
34+
35+
console.log('Incoming request data:', {
36+
projectId: req.body?.projectId,
37+
instanceId: req.body?.instanceId,
38+
features: req.body?.features,
39+
hasToken: !!req.body?.token,
2740
});
2841

42+
const options = {
43+
method: 'POST',
44+
headers: {
45+
'Content-Type': 'application/json',
46+
Authorization: req.body.token,
47+
Accept: 'application/json',
48+
},
49+
body: JSON.stringify({
50+
projectId: req.body.projectId,
51+
instanceId: req.body.instanceId,
52+
expiresIn: 60 * 60 * 5,
53+
features: req.body.features,
54+
}),
55+
agent: new https.Agent({
56+
rejectUnauthorized: false,
57+
}),
58+
};
59+
2960
try {
30-
const response = await fetch(loginUrl, {
31-
method: 'POST',
32-
headers: {
33-
'Content-Type': 'application/json',
34-
Authorization: req.body.token, // your API token
35-
},
36-
body: JSON.stringify({
37-
projectId: req.body.projectId,
38-
instanceId: req.body.instanceId,
39-
expiresIn: 60 * 60 * 5, // value is in seconds
40-
features: req.body.features,
41-
}),
42-
agent: httpsAgent,
61+
console.log('Making request to:', loginUrl);
62+
console.log('Request options:', {
63+
method: options.method,
64+
headers: options.headers,
65+
body: JSON.parse(options.body),
4366
});
4467

68+
const response = await fetch(loginUrl, options);
69+
70+
console.log('Response status:', response.status);
71+
console.log('Response headers:', response.headers.raw());
72+
73+
if (!response.ok) {
74+
const errorText = await response.text();
75+
console.error('Error response body:', errorText);
76+
throw new Error(
77+
`HTTP error! status: ${response.status}, body: ${errorText}`
78+
);
79+
}
80+
4581
const data = await response.json();
82+
console.log('Success response:', data);
4683

47-
res.send(data);
84+
// Set explicit headers for the response
85+
res.setHeader('Content-Type', 'application/json');
86+
res.setHeader('Access-Control-Allow-Origin', '*');
87+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
88+
res.setHeader(
89+
'Access-Control-Allow-Headers',
90+
'Content-Type, Authorization, Accept'
91+
);
4892

49-
return;
93+
console.log('Sending response to client');
94+
res.send(data);
95+
console.log('Response sent successfully');
5096
} catch (err) {
51-
console.log('webplayer ERROR: ', err);
52-
throw new Error(err);
97+
console.error('Webplayer Error:', err.message);
98+
console.error('Error stack:', err.stack);
99+
res.status(500).json({
100+
error: 'Failed to connect to webplayer service',
101+
details: err.message,
102+
status: err.status || 500,
103+
});
53104
}
54105
});
55106

0 commit comments

Comments
 (0)