certops 1.0.4 → 1.0.6
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/dist/api.js +25 -10
- package/dist/client.js +2 -1
- package/dist/commands/service/renew.js +2 -1
- package/dist/version.js +18 -0
- package/package.json +1 -1
package/dist/api.js
CHANGED
|
@@ -1,32 +1,47 @@
|
|
|
1
1
|
export function createApiClient(options) {
|
|
2
2
|
const base = (options.apiUrl ?? 'https://ssl-manager.dcom.at').replace(/\/$/, '');
|
|
3
|
+
const baseHeaders = {
|
|
4
|
+
'Authorization': `Bearer ${options.apiKey}`,
|
|
5
|
+
'X-CLI-Version': options.cliVersion,
|
|
6
|
+
};
|
|
7
|
+
function handleErrorResponse(status, errors) {
|
|
8
|
+
const code = errors?.[0]?.code;
|
|
9
|
+
const msg = errors?.[0]?.message ?? `HTTP ${status}`;
|
|
10
|
+
if (status === 426 || code === 'CLI_UPDATE_REQUIRED') {
|
|
11
|
+
throw new Error(`Your certops CLI is out of date (current: v${options.cliVersion}).\n\n` +
|
|
12
|
+
` Update now:\n` +
|
|
13
|
+
` npm install -g certops@latest\n`);
|
|
14
|
+
}
|
|
15
|
+
if (status === 400 && code === 'INVALID_CLI_VERSION') {
|
|
16
|
+
throw new Error(`Server rejected CLI version header (sent: v${options.cliVersion}).\n\n` +
|
|
17
|
+
` Update now:\n` +
|
|
18
|
+
` npm install -g certops@latest\n`);
|
|
19
|
+
}
|
|
20
|
+
throw new Error(msg);
|
|
21
|
+
}
|
|
3
22
|
async function request(path) {
|
|
4
23
|
const res = await fetch(`${base}/api/cli${path}`, {
|
|
5
|
-
headers: {
|
|
6
|
-
'Authorization': `Bearer ${options.apiKey}`,
|
|
7
|
-
'Content-Type': 'application/json',
|
|
8
|
-
},
|
|
24
|
+
headers: { ...baseHeaders, 'Content-Type': 'application/json' },
|
|
9
25
|
});
|
|
10
26
|
const body = (await res.json());
|
|
11
27
|
if (!res.ok || 'errors' in body) {
|
|
12
28
|
const errBody = body;
|
|
13
|
-
|
|
14
|
-
throw new Error(msg);
|
|
29
|
+
handleErrorResponse(res.status, errBody.errors);
|
|
15
30
|
}
|
|
16
31
|
return body.data;
|
|
17
32
|
}
|
|
18
33
|
async function requestBinary(path) {
|
|
19
34
|
const res = await fetch(`${base}/api/cli${path}`, {
|
|
20
|
-
headers:
|
|
35
|
+
headers: baseHeaders,
|
|
21
36
|
});
|
|
22
37
|
if (!res.ok) {
|
|
23
|
-
let
|
|
38
|
+
let errors = [];
|
|
24
39
|
try {
|
|
25
40
|
const body = (await res.json());
|
|
26
|
-
|
|
41
|
+
errors = body.errors ?? [];
|
|
27
42
|
}
|
|
28
43
|
catch { /* ignore */ }
|
|
29
|
-
|
|
44
|
+
handleErrorResponse(res.status, errors);
|
|
30
45
|
}
|
|
31
46
|
return Buffer.from(await res.arrayBuffer());
|
|
32
47
|
}
|
package/dist/client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import input from '@inquirer/input';
|
|
2
2
|
import { readConfig } from './config.js';
|
|
3
3
|
import { createApiClient } from './api.js';
|
|
4
|
+
import { CLI_VERSION } from './version.js';
|
|
4
5
|
export async function getConfiguredClient() {
|
|
5
6
|
let apiKey = process.env['CERTOPS_API_KEY'];
|
|
6
7
|
if (!apiKey) {
|
|
@@ -17,5 +18,5 @@ export async function getConfiguredClient() {
|
|
|
17
18
|
});
|
|
18
19
|
}
|
|
19
20
|
const config = await readConfig();
|
|
20
|
-
return createApiClient({ apiKey, apiUrl: config.apiUrl });
|
|
21
|
+
return createApiClient({ apiKey, apiUrl: config.apiUrl, cliVersion: CLI_VERSION });
|
|
21
22
|
}
|
|
@@ -3,6 +3,7 @@ import { saveZip } from '../../files.js';
|
|
|
3
3
|
import { readConfig } from '../../config.js';
|
|
4
4
|
import { readState, updateCertState } from '../../state.js';
|
|
5
5
|
import { runHooks } from '../../hooks.js';
|
|
6
|
+
import { CLI_VERSION } from '../../version.js';
|
|
6
7
|
const BASE_RETRY_DELAY_MS = 2_000; // 2 s → 4 s → 8 s …
|
|
7
8
|
function sleep(ms) {
|
|
8
9
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
@@ -22,7 +23,7 @@ export async function resolveApiKey() {
|
|
|
22
23
|
}
|
|
23
24
|
export async function runRenewalCycle(apiKey, log) {
|
|
24
25
|
const config = await readConfig();
|
|
25
|
-
const client = createApiClient({ apiKey, apiUrl: config.apiUrl });
|
|
26
|
+
const client = createApiClient({ apiKey, apiUrl: config.apiUrl, cliVersion: CLI_VERSION });
|
|
26
27
|
const allCerts = await client.listCerts();
|
|
27
28
|
const DOWNLOADABLE = new Set(['active', 'renewing']);
|
|
28
29
|
const certs = (config.watchDomains.length > 0
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
function isPackageJson(value) {
|
|
5
|
+
return (typeof value === 'object' &&
|
|
6
|
+
value !== null &&
|
|
7
|
+
'version' in value &&
|
|
8
|
+
typeof value.version === 'string');
|
|
9
|
+
}
|
|
10
|
+
function readCliVersion() {
|
|
11
|
+
const dir = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const path = join(dir, '..', 'package.json');
|
|
13
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
14
|
+
if (!isPackageJson(raw))
|
|
15
|
+
throw new Error('package.json is missing a valid version field');
|
|
16
|
+
return raw.version;
|
|
17
|
+
}
|
|
18
|
+
export const CLI_VERSION = readCliVersion();
|