project-foundry 1.0.0 → 1.0.1
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.
- package/bin/foundry.js +3 -0
- package/package.json +1 -1
- package/src/api-client.js +52 -14
- package/src/tunnel-client.js +1 -0
package/bin/foundry.js
CHANGED
|
@@ -126,6 +126,9 @@ program
|
|
|
126
126
|
} catch (err) {
|
|
127
127
|
s.stop(pc.red('Erro durante o pareamento'));
|
|
128
128
|
p.log.error(err.message || 'Falha ao registrar nó');
|
|
129
|
+
if (err.cause) {
|
|
130
|
+
p.log.error(pc.dim(`Detalhes técnicos: ${err.cause?.message || err.cause?.code || err.cause}`));
|
|
131
|
+
}
|
|
129
132
|
process.exit(1);
|
|
130
133
|
}
|
|
131
134
|
});
|
package/package.json
CHANGED
package/src/api-client.js
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
|
+
import https from 'node:https';
|
|
2
|
+
import http from 'node:http';
|
|
1
3
|
import os from 'node:os';
|
|
2
4
|
|
|
5
|
+
async function postJson(targetUrl, payload) {
|
|
6
|
+
const parsed = new URL(targetUrl);
|
|
7
|
+
const isHttps = parsed.protocol === 'https:';
|
|
8
|
+
const transport = isHttps ? https : http;
|
|
9
|
+
const postData = JSON.stringify(payload);
|
|
10
|
+
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const req = transport.request(parsed, {
|
|
13
|
+
method: 'POST',
|
|
14
|
+
family: 4,
|
|
15
|
+
rejectUnauthorized: false,
|
|
16
|
+
headers: {
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
'Content-Length': Buffer.byteLength(postData),
|
|
19
|
+
'User-Agent': 'Project-Foundry-CLI/1.0',
|
|
20
|
+
},
|
|
21
|
+
}, (res) => {
|
|
22
|
+
let body = '';
|
|
23
|
+
res.on('data', (chunk) => { body += chunk; });
|
|
24
|
+
res.on('end', () => {
|
|
25
|
+
try {
|
|
26
|
+
const parsedBody = body ? JSON.parse(body) : {};
|
|
27
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
|
|
28
|
+
resolve(parsedBody.data);
|
|
29
|
+
} else {
|
|
30
|
+
const err = new Error(parsedBody?.error?.message || `HTTP ${res.statusCode}: ${body.slice(0, 100)}`);
|
|
31
|
+
err.code = parsedBody?.error?.code;
|
|
32
|
+
reject(err);
|
|
33
|
+
}
|
|
34
|
+
} catch {
|
|
35
|
+
reject(new Error(`Resposta inválida do servidor: HTTP ${res.statusCode}`));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
req.on('error', (err) => {
|
|
41
|
+
reject(new Error(`Falha de conexão com ${parsed.host} (${err.code || err.message})`));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
req.setTimeout(15000, () => {
|
|
45
|
+
req.destroy();
|
|
46
|
+
reject(new Error(`Tempo limite esgotado ao contatar ${parsed.host}`));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
req.write(postData);
|
|
50
|
+
req.end();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
3
54
|
export async function registerNodeWithCloud({ serverUrl, token, name }) {
|
|
4
55
|
const url = `${serverUrl.replace(/\/$/, '')}/api/v1/nodes/register`;
|
|
5
56
|
|
|
@@ -11,20 +62,7 @@ export async function registerNodeWithCloud({ serverUrl, token, name }) {
|
|
|
11
62
|
name: name || `Servidor ${os.hostname()}`,
|
|
12
63
|
};
|
|
13
64
|
|
|
14
|
-
|
|
15
|
-
method: 'POST',
|
|
16
|
-
headers: { 'Content-Type': 'application/json' },
|
|
17
|
-
body: JSON.stringify(payload),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const body = await response.json().catch(() => ({}));
|
|
21
|
-
|
|
22
|
-
if (!response.ok) {
|
|
23
|
-
const errorMsg = body?.error?.message || 'Falha ao registrar nó na nuvem';
|
|
24
|
-
throw new Error(errorMsg);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return body.data;
|
|
65
|
+
return await postJson(url, payload);
|
|
28
66
|
}
|
|
29
67
|
|
|
30
68
|
export async function fetchServerTelemetry(port = 3010) {
|