@dominevo/series-scraper-core 0.0.91 → 0.0.92
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/controllers/notifications.controller.d.ts +4 -11
- package/dist/controllers/notifications.controller.js +7 -5
- package/dist/controllers/settings.controller.d.ts +3 -3
- package/dist/controllers/settings.controller.js +6 -4
- package/dist/controllers/sites.controller.d.ts +5 -53
- package/dist/controllers/sites.controller.js +47 -14
- package/dist/controllers/vpn.controller.js +2 -0
- package/dist/docker-layout/secret-layout.js +19 -7
- package/dist/ports/secret-store.d.ts +2 -0
- package/dist/ports/secret-store.js +2 -0
- package/dist/repositories/settings.repository.d.ts +5 -0
- package/dist/repositories/settings.repository.js +15 -1
- package/dist/repositories/sites.repository.d.ts +6 -1
- package/dist/repositories/sites.repository.js +34 -5
- package/dist/repositories/webhook.repository.d.ts +3 -0
- package/dist/repositories/webhook.repository.js +13 -4
- package/dist/scraper.module.js +2 -0
- package/dist/services/plugin-settings.service.d.ts +2 -0
- package/dist/services/plugin-settings.service.js +16 -3
- package/dist/services/secret-migration.service.d.ts +22 -0
- package/dist/services/secret-migration.service.js +48 -0
- package/dist/services/secret-views.d.ts +56 -0
- package/dist/services/secret-views.js +120 -0
- package/package.json +1 -1
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
2
|
import { NotificationService } from '../services/notification.service';
|
|
3
|
+
import { WebhookView } from '../services/secret-views';
|
|
3
4
|
import { WebhookRepository, WebhookConfig } from '../repositories/webhook.repository';
|
|
4
5
|
export declare class NotificationsController {
|
|
5
6
|
private readonly notifications;
|
|
6
7
|
private readonly webhookRepo;
|
|
7
8
|
constructor(notifications: NotificationService, webhookRepo: WebhookRepository);
|
|
8
9
|
stream(): Observable<MessageEvent>;
|
|
9
|
-
getWebhooks():
|
|
10
|
+
getWebhooks(): WebhookView[];
|
|
10
11
|
createWebhook(body: {
|
|
11
12
|
name: string;
|
|
12
13
|
url: string;
|
|
13
14
|
events?: string[];
|
|
14
15
|
headers?: Record<string, string>;
|
|
15
16
|
payloadTemplate?: any;
|
|
16
|
-
}):
|
|
17
|
-
updateWebhook(id: string, body: Partial<WebhookConfig>):
|
|
18
|
-
id: string;
|
|
19
|
-
name: string;
|
|
20
|
-
url: string;
|
|
21
|
-
enabled: boolean;
|
|
22
|
-
events: string[];
|
|
23
|
-
headers: Record<string, string>;
|
|
24
|
-
payloadTemplate?: any;
|
|
25
|
-
};
|
|
17
|
+
}): WebhookView;
|
|
18
|
+
updateWebhook(id: string, body: Partial<WebhookConfig>): WebhookView;
|
|
26
19
|
deleteWebhook(id: string): {
|
|
27
20
|
ok: boolean;
|
|
28
21
|
};
|
|
@@ -17,6 +17,7 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const rxjs_1 = require("rxjs");
|
|
18
18
|
const uuid_1 = require("uuid");
|
|
19
19
|
const notification_service_1 = require("../services/notification.service");
|
|
20
|
+
const secret_views_1 = require("../services/secret-views");
|
|
20
21
|
const webhook_repository_1 = require("../repositories/webhook.repository");
|
|
21
22
|
let NotificationsController = class NotificationsController {
|
|
22
23
|
constructor(notifications, webhookRepo) {
|
|
@@ -30,7 +31,7 @@ let NotificationsController = class NotificationsController {
|
|
|
30
31
|
}
|
|
31
32
|
// --- Webhook CRUD ---
|
|
32
33
|
getWebhooks() {
|
|
33
|
-
return this.webhookRepo.getAll();
|
|
34
|
+
return this.webhookRepo.getAll().map(secret_views_1.toWebhookView);
|
|
34
35
|
}
|
|
35
36
|
createWebhook(body) {
|
|
36
37
|
const webhook = {
|
|
@@ -39,19 +40,20 @@ let NotificationsController = class NotificationsController {
|
|
|
39
40
|
url: body.url,
|
|
40
41
|
enabled: true,
|
|
41
42
|
events: body.events || [],
|
|
42
|
-
headers: body.headers
|
|
43
|
+
headers: (0, secret_views_1.mergeHeaders)({}, body.headers ?? {}),
|
|
43
44
|
payloadTemplate: body.payloadTemplate,
|
|
44
45
|
};
|
|
45
46
|
this.webhookRepo.save(webhook);
|
|
46
|
-
return webhook;
|
|
47
|
+
return (0, secret_views_1.toWebhookView)(webhook);
|
|
47
48
|
}
|
|
48
49
|
updateWebhook(id, body) {
|
|
49
50
|
const existing = this.webhookRepo.getById(id);
|
|
50
51
|
if (!existing)
|
|
51
52
|
throw new common_1.HttpException('Webhook not found', common_1.HttpStatus.NOT_FOUND);
|
|
52
|
-
const
|
|
53
|
+
const { headers, secretHeaders: _view, urlSecretSet: _set, ...rest } = body;
|
|
54
|
+
const updated = { ...existing, ...rest, id: existing.id, url: (0, secret_views_1.mergeWebhookUrl)(existing.url, rest.url), headers: headers === undefined ? existing.headers : (0, secret_views_1.mergeHeaders)(existing.headers, headers) };
|
|
53
55
|
this.webhookRepo.save(updated);
|
|
54
|
-
return updated;
|
|
56
|
+
return (0, secret_views_1.toWebhookView)(updated);
|
|
55
57
|
}
|
|
56
58
|
deleteWebhook(id) {
|
|
57
59
|
const ok = this.webhookRepo.delete(id);
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { AppSettings } from '../types';
|
|
2
1
|
import { SettingsService } from '../services/settings.service';
|
|
3
2
|
import { DownloadService } from '../services/download.service';
|
|
3
|
+
import { SettingsView } from '../services/secret-views';
|
|
4
4
|
import { SUPPORTED_LANGUAGES, LANGUAGE_TYPES } from '../constants/languages';
|
|
5
5
|
export declare class SettingsController {
|
|
6
6
|
private readonly settingsService;
|
|
7
7
|
private readonly downloadService;
|
|
8
8
|
constructor(settingsService: SettingsService, downloadService: DownloadService);
|
|
9
|
-
get():
|
|
9
|
+
get(): SettingsView;
|
|
10
10
|
/** Predefined choices exposed to the UI for the preferred-language dropdowns. */
|
|
11
11
|
getLanguages(): {
|
|
12
12
|
languages: typeof SUPPORTED_LANGUAGES;
|
|
13
13
|
types: typeof LANGUAGE_TYPES;
|
|
14
14
|
};
|
|
15
|
-
update(body:
|
|
15
|
+
update(body: unknown): SettingsView;
|
|
16
16
|
}
|
|
@@ -16,6 +16,7 @@ exports.SettingsController = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const settings_service_1 = require("../services/settings.service");
|
|
18
18
|
const download_service_1 = require("../services/download.service");
|
|
19
|
+
const secret_views_1 = require("../services/secret-views");
|
|
19
20
|
const languages_1 = require("../constants/languages");
|
|
20
21
|
let SettingsController = class SettingsController {
|
|
21
22
|
constructor(settingsService, downloadService) {
|
|
@@ -23,18 +24,19 @@ let SettingsController = class SettingsController {
|
|
|
23
24
|
this.downloadService = downloadService;
|
|
24
25
|
}
|
|
25
26
|
get() {
|
|
26
|
-
return this.settingsService.get();
|
|
27
|
+
return (0, secret_views_1.toSettingsView)(this.settingsService.get());
|
|
27
28
|
}
|
|
28
29
|
/** Predefined choices exposed to the UI for the preferred-language dropdowns. */
|
|
29
30
|
getLanguages() {
|
|
30
31
|
return { languages: languages_1.SUPPORTED_LANGUAGES, types: languages_1.LANGUAGE_TYPES };
|
|
31
32
|
}
|
|
32
33
|
update(body) {
|
|
33
|
-
const
|
|
34
|
-
|
|
34
|
+
const patch = (0, secret_views_1.parseSettingsBody)(body);
|
|
35
|
+
const updated = this.settingsService.update(patch);
|
|
36
|
+
if (patch.concurrentDownloads !== undefined) {
|
|
35
37
|
this.downloadService.syncConcurrency();
|
|
36
38
|
}
|
|
37
|
-
return updated;
|
|
39
|
+
return (0, secret_views_1.toSettingsView)(updated);
|
|
38
40
|
}
|
|
39
41
|
};
|
|
40
42
|
exports.SettingsController = SettingsController;
|
|
@@ -7,70 +7,22 @@ export declare class SitesController {
|
|
|
7
7
|
private readonly adapterRegistry;
|
|
8
8
|
private readonly analyzer;
|
|
9
9
|
constructor(sitesRepo: SitesRepository, adapterRegistry: AdapterRegistryService, analyzer: SiteAnalyzerService);
|
|
10
|
-
getAll():
|
|
11
|
-
getById(id: string):
|
|
10
|
+
getAll(): import("../services/secret-views").SiteView[];
|
|
11
|
+
getById(id: string): import("../services/secret-views").SiteView;
|
|
12
12
|
analyze(body: {
|
|
13
13
|
url: string;
|
|
14
14
|
}): Promise<import("../services/site-analyzer.service").AnalysisResult>;
|
|
15
15
|
analyzeSearch(body: {
|
|
16
16
|
url: string;
|
|
17
17
|
}): Promise<Partial<import("../services/site-analyzer.service").AnalysisResult>>;
|
|
18
|
-
create(body: Omit<GenericSiteConfig, 'id'>):
|
|
19
|
-
update(id: string, body: Partial<GenericSiteConfig>):
|
|
20
|
-
id: string;
|
|
21
|
-
name: string;
|
|
22
|
-
baseUrl: string;
|
|
23
|
-
hosts: string[];
|
|
24
|
-
languages?: import("../types").AdapterLanguageInfo[];
|
|
25
|
-
search: {
|
|
26
|
-
urlPattern: string;
|
|
27
|
-
resultSelector: string;
|
|
28
|
-
titleSelector: string;
|
|
29
|
-
urlSelector: string;
|
|
30
|
-
imageSelector?: string;
|
|
31
|
-
imageAttr?: string;
|
|
32
|
-
};
|
|
33
|
-
seasons?: {
|
|
34
|
-
listSelector: string;
|
|
35
|
-
numberAttr?: string;
|
|
36
|
-
numberSelector?: string;
|
|
37
|
-
urlSelector?: string;
|
|
38
|
-
singlePage?: boolean;
|
|
39
|
-
};
|
|
40
|
-
episodes: {
|
|
41
|
-
listSelector: string;
|
|
42
|
-
numberAttr?: string;
|
|
43
|
-
numberSelector?: string;
|
|
44
|
-
titleSelector?: string;
|
|
45
|
-
urlSelector?: string;
|
|
46
|
-
seasonFilterAttr?: string;
|
|
47
|
-
seasonFilterSelector?: string;
|
|
48
|
-
episodeIdAttr?: string;
|
|
49
|
-
};
|
|
50
|
-
downloads: {
|
|
51
|
-
linkSelector?: string;
|
|
52
|
-
linkAttr?: string;
|
|
53
|
-
videoUrlPattern?: boolean;
|
|
54
|
-
iframeSelector?: string;
|
|
55
|
-
mirrorSelector?: string;
|
|
56
|
-
mirrorFilterSelector?: string;
|
|
57
|
-
mirrorLinkAttr?: string;
|
|
58
|
-
languageFromQuery?: string;
|
|
59
|
-
languageMap?: Record<string, string>;
|
|
60
|
-
defaultLanguage?: string;
|
|
61
|
-
grabberUrlPattern?: string;
|
|
62
|
-
playerIdSelector?: string;
|
|
63
|
-
playerIdAttr?: string;
|
|
64
|
-
};
|
|
65
|
-
headers?: Record<string, string>;
|
|
66
|
-
useHeadless?: boolean;
|
|
67
|
-
};
|
|
18
|
+
create(body: Omit<GenericSiteConfig, 'id'>): import("../services/secret-views").SiteView;
|
|
19
|
+
update(id: string, body: Partial<GenericSiteConfig>): import("../services/secret-views").SiteView;
|
|
68
20
|
delete(id: string): {
|
|
69
21
|
ok: boolean;
|
|
70
22
|
};
|
|
71
23
|
/** Export every site as a single JSON blob — suitable for backup / cross-instance copy. */
|
|
72
24
|
exportAll(): {
|
|
73
|
-
sites:
|
|
25
|
+
sites: import("../services/secret-views").SiteView[];
|
|
74
26
|
};
|
|
75
27
|
/**
|
|
76
28
|
* Bulk import site configs. Matches by `name`: existing sites are updated,
|
|
@@ -15,9 +15,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.SitesController = void 0;
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const uuid_1 = require("uuid");
|
|
18
|
+
const secret_views_1 = require("../services/secret-views");
|
|
18
19
|
const sites_repository_1 = require("../repositories/sites.repository");
|
|
19
20
|
const adapter_registry_service_1 = require("../services/adapter-registry.service");
|
|
20
21
|
const site_analyzer_service_1 = require("../services/site-analyzer.service");
|
|
22
|
+
function hostOf(url) {
|
|
23
|
+
if (typeof url !== 'string')
|
|
24
|
+
return undefined;
|
|
25
|
+
try {
|
|
26
|
+
return new URL(url).host.toLowerCase();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Whether an imported site stays on a host the stored one already served (its baseUrl host, or one of its hosts). */
|
|
33
|
+
function sameSiteHost(stored, incoming) {
|
|
34
|
+
const host = hostOf(incoming.baseUrl);
|
|
35
|
+
if (host === undefined)
|
|
36
|
+
return false;
|
|
37
|
+
return host === hostOf(stored.baseUrl) || stored.hosts.some((h) => h.toLowerCase() === host);
|
|
38
|
+
}
|
|
21
39
|
let SitesController = class SitesController {
|
|
22
40
|
constructor(sitesRepo, adapterRegistry, analyzer) {
|
|
23
41
|
this.sitesRepo = sitesRepo;
|
|
@@ -25,13 +43,13 @@ let SitesController = class SitesController {
|
|
|
25
43
|
this.analyzer = analyzer;
|
|
26
44
|
}
|
|
27
45
|
getAll() {
|
|
28
|
-
return this.sitesRepo.getAll();
|
|
46
|
+
return this.sitesRepo.getAll().map(secret_views_1.toSiteView);
|
|
29
47
|
}
|
|
30
48
|
getById(id) {
|
|
31
49
|
const site = this.sitesRepo.getById(id);
|
|
32
50
|
if (!site)
|
|
33
51
|
throw new common_1.HttpException('Site not found', common_1.HttpStatus.NOT_FOUND);
|
|
34
|
-
return site;
|
|
52
|
+
return (0, secret_views_1.toSiteView)(site);
|
|
35
53
|
}
|
|
36
54
|
async analyze(body) {
|
|
37
55
|
if (!body.url)
|
|
@@ -54,19 +72,19 @@ let SitesController = class SitesController {
|
|
|
54
72
|
}
|
|
55
73
|
}
|
|
56
74
|
create(body) {
|
|
57
|
-
const site = { ...body, id: (0, uuid_1.v4)() };
|
|
75
|
+
const site = { ...(0, secret_views_1.siteFromWrite)(body), id: (0, uuid_1.v4)() };
|
|
58
76
|
this.sitesRepo.save(site);
|
|
59
77
|
this.adapterRegistry.reloadGenericAdapters();
|
|
60
|
-
return site;
|
|
78
|
+
return (0, secret_views_1.toSiteView)(site);
|
|
61
79
|
}
|
|
62
80
|
update(id, body) {
|
|
63
81
|
const existing = this.sitesRepo.getById(id);
|
|
64
82
|
if (!existing)
|
|
65
83
|
throw new common_1.HttpException('Site not found', common_1.HttpStatus.NOT_FOUND);
|
|
66
|
-
const updated = { ...existing, ...body, id: existing.id };
|
|
84
|
+
const updated = { ...existing, ...(0, secret_views_1.siteFromWrite)(body, existing), id: existing.id };
|
|
67
85
|
this.sitesRepo.save(updated);
|
|
68
86
|
this.adapterRegistry.reloadGenericAdapters();
|
|
69
|
-
return updated;
|
|
87
|
+
return (0, secret_views_1.toSiteView)(updated);
|
|
70
88
|
}
|
|
71
89
|
delete(id) {
|
|
72
90
|
const ok = this.sitesRepo.delete(id);
|
|
@@ -77,7 +95,7 @@ let SitesController = class SitesController {
|
|
|
77
95
|
}
|
|
78
96
|
/** Export every site as a single JSON blob — suitable for backup / cross-instance copy. */
|
|
79
97
|
exportAll() {
|
|
80
|
-
return { sites: this.sitesRepo.getAll() };
|
|
98
|
+
return { sites: this.sitesRepo.getAll().map(secret_views_1.toSiteView) };
|
|
81
99
|
}
|
|
82
100
|
/**
|
|
83
101
|
* Bulk import site configs. Matches by `name`: existing sites are updated,
|
|
@@ -108,25 +126,40 @@ let SitesController = class SitesController {
|
|
|
108
126
|
}
|
|
109
127
|
let created = 0;
|
|
110
128
|
let updated = 0;
|
|
129
|
+
// Validate every entry before anything is deleted: a bad one must leave the stored sites untouched.
|
|
130
|
+
for (const raw of incoming) {
|
|
131
|
+
if (!raw || typeof raw !== 'object' || !raw.name) {
|
|
132
|
+
throw new common_1.HttpException('Each site must include a "name"', common_1.HttpStatus.BAD_REQUEST);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// A replace wipes the sites, and their stored credentials with them: remember what a site that comes back held,
|
|
136
|
+
// so its credential headers (which an export never carries) survive the round trip.
|
|
137
|
+
const before = this.sitesRepo.getAll();
|
|
138
|
+
const priorById = new Map(before.map((s) => [s.id, s]));
|
|
139
|
+
const priorByName = new Map(before.map((s) => [s.name, s]));
|
|
111
140
|
if (mode === 'replace') {
|
|
112
|
-
for (const s of
|
|
141
|
+
for (const s of before)
|
|
113
142
|
this.sitesRepo.delete(s.id);
|
|
114
143
|
}
|
|
115
144
|
const existing = this.sitesRepo.getAll();
|
|
116
145
|
const byName = new Map(existing.map((s) => [s.name, s]));
|
|
117
146
|
for (const raw of incoming) {
|
|
118
|
-
|
|
119
|
-
throw new common_1.HttpException('Each site must include a "name"', common_1.HttpStatus.BAD_REQUEST);
|
|
120
|
-
}
|
|
121
|
-
const match = byName.get(raw.name);
|
|
147
|
+
const match = byName.get(String(raw.name));
|
|
122
148
|
if (match) {
|
|
123
|
-
|
|
149
|
+
// The stored headers (a cookie) follow the name only while the site stays on the same host.
|
|
150
|
+
const { headers: _stored, ...bare } = match;
|
|
151
|
+
const merged = sameSiteHost(match, raw)
|
|
152
|
+
? { ...match, ...(0, secret_views_1.siteFromWrite)(raw, match), id: match.id }
|
|
153
|
+
: { ...bare, ...(0, secret_views_1.siteFromWrite)(raw), id: match.id };
|
|
124
154
|
this.sitesRepo.save(merged);
|
|
125
155
|
updated++;
|
|
126
156
|
}
|
|
127
157
|
else {
|
|
128
158
|
const id = ('id' in raw && typeof raw.id === 'string') ? raw.id : (0, uuid_1.v4)();
|
|
129
|
-
|
|
159
|
+
const byId = priorById.get(id);
|
|
160
|
+
const byNameMatch = priorByName.get(String(raw.name));
|
|
161
|
+
const prior = byId ?? (byNameMatch && sameSiteHost(byNameMatch, raw) ? byNameMatch : undefined);
|
|
162
|
+
this.sitesRepo.save({ ...(0, secret_views_1.siteFromWrite)(raw, prior), id });
|
|
130
163
|
created++;
|
|
131
164
|
}
|
|
132
165
|
}
|
|
@@ -120,6 +120,8 @@ let VpnController = class VpnController {
|
|
|
120
120
|
const known = new Set(this.providersRepo.getAll().map((e) => e.id));
|
|
121
121
|
const homeOnly = new Set(this.providersRepo.getAll().filter(vpn_service_2.isHomeByDesign).map((e) => e.id));
|
|
122
122
|
const next = validated(() => (0, egress_input_1.parseSettingsPatch)(body, this.settingsRepo.get(), known, homeOnly));
|
|
123
|
+
// Deliberate order: the flag goes first. If the settings write then fails, the old exit has lost its exemption
|
|
124
|
+
// (it fails closed, back under the VPN leak check); the reverse order could leave an exempt exit with no role.
|
|
123
125
|
this.clearHomeFlagLeaving(this.settingsRepo.get().captchaFallbackEgressId, next.captchaFallbackEgressId);
|
|
124
126
|
this.settingsRepo.update(next);
|
|
125
127
|
await this.vpn.reload();
|
|
@@ -100,12 +100,8 @@ const egressLayout = recordListLayout({
|
|
|
100
100
|
prefix: 'egress.',
|
|
101
101
|
fields: ['password', 'privateKey'],
|
|
102
102
|
});
|
|
103
|
-
/**
|
|
104
|
-
const
|
|
105
|
-
collection: 'webhooks',
|
|
106
|
-
prefix: 'webhook.',
|
|
107
|
-
fields: ['headers'],
|
|
108
|
-
listKey: 'webhooks',
|
|
103
|
+
/** An object in the file, one JSON text through the port. */
|
|
104
|
+
const headersCodec = {
|
|
109
105
|
encode: (_field, value) => {
|
|
110
106
|
try {
|
|
111
107
|
const parsed = JSON.parse(value);
|
|
@@ -116,5 +112,21 @@ const webhookLayout = recordListLayout({
|
|
|
116
112
|
}
|
|
117
113
|
},
|
|
118
114
|
decode: (_field, value) => (isObject(value) ? JSON.stringify(value) : undefined),
|
|
115
|
+
};
|
|
116
|
+
/** Webhook headers are an object in the file and one JSON text through the port. */
|
|
117
|
+
const webhookLayout = recordListLayout({
|
|
118
|
+
collection: 'webhooks',
|
|
119
|
+
prefix: 'webhook.',
|
|
120
|
+
fields: ['headers'],
|
|
121
|
+
listKey: 'webhooks',
|
|
122
|
+
...headersCodec,
|
|
123
|
+
});
|
|
124
|
+
/** A site's HTTP headers can carry a session cookie: same shape as a webhook's. */
|
|
125
|
+
const siteLayout = recordListLayout({
|
|
126
|
+
collection: 'sites',
|
|
127
|
+
prefix: 'site.',
|
|
128
|
+
fields: ['headers'],
|
|
129
|
+
listKey: 'sites',
|
|
130
|
+
...headersCodec,
|
|
119
131
|
});
|
|
120
|
-
exports.DOCKER_SECRET_LAYOUT = [settingsLayout, egressLayout, webhookLayout];
|
|
132
|
+
exports.DOCKER_SECRET_LAYOUT = [settingsLayout, egressLayout, webhookLayout, siteLayout];
|
|
@@ -22,4 +22,6 @@ export declare const secretNames: {
|
|
|
22
22
|
readonly egressPrivateKey: (egressId: string) => string;
|
|
23
23
|
/** The webhook's headers as one JSON object text. */
|
|
24
24
|
readonly webhookHeaders: (webhookId: string) => string;
|
|
25
|
+
/** A generic site's HTTP headers as one JSON object text (they can carry a session cookie). */
|
|
26
|
+
readonly siteHeaders: (siteId: string) => string;
|
|
25
27
|
};
|
|
@@ -10,4 +10,6 @@ exports.secretNames = {
|
|
|
10
10
|
egressPrivateKey: (egressId) => `egress.${egressId}.privateKey`,
|
|
11
11
|
/** The webhook's headers as one JSON object text. */
|
|
12
12
|
webhookHeaders: (webhookId) => `webhook.${webhookId}.headers`,
|
|
13
|
+
/** A generic site's HTTP headers as one JSON object text (they can carry a session cookie). */
|
|
14
|
+
siteHeaders: (siteId) => `site.${siteId}.headers`,
|
|
13
15
|
};
|
|
@@ -12,5 +12,10 @@ export declare class SettingsRepository {
|
|
|
12
12
|
get(): AppSettings;
|
|
13
13
|
save(settings: AppSettings): void;
|
|
14
14
|
update(partial: Partial<AppSettings>): AppSettings;
|
|
15
|
+
/**
|
|
16
|
+
* Moves a credential a state file from an older version still carries into the secret store, leaving none behind.
|
|
17
|
+
* Returns how many it moved; a second run finds none.
|
|
18
|
+
*/
|
|
19
|
+
migratePlaintext(): number;
|
|
15
20
|
private keepSecret;
|
|
16
21
|
}
|
|
@@ -70,10 +70,12 @@ let SettingsRepository = class SettingsRepository {
|
|
|
70
70
|
}
|
|
71
71
|
save(settings) {
|
|
72
72
|
const { sonarrApiKey, radarrApiKey, plexToken, ...rest } = settings;
|
|
73
|
-
this
|
|
73
|
+
// Secrets first, state after: a host that writes behind persists them in this order, so a crash between the two
|
|
74
|
+
// leaves a credential in both places (harmless) and never in neither.
|
|
74
75
|
this.keepSecret(secret_store_1.secretNames.sonarrApiKey, sonarrApiKey);
|
|
75
76
|
this.keepSecret(secret_store_1.secretNames.radarrApiKey, radarrApiKey);
|
|
76
77
|
this.keepSecret(secret_store_1.secretNames.plexToken, plexToken);
|
|
78
|
+
this.repo.write(rest);
|
|
77
79
|
}
|
|
78
80
|
update(partial) {
|
|
79
81
|
const current = this.get();
|
|
@@ -81,6 +83,18 @@ let SettingsRepository = class SettingsRepository {
|
|
|
81
83
|
this.save(updated);
|
|
82
84
|
return updated;
|
|
83
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Moves a credential a state file from an older version still carries into the secret store, leaving none behind.
|
|
88
|
+
* Returns how many it moved; a second run finds none.
|
|
89
|
+
*/
|
|
90
|
+
migratePlaintext() {
|
|
91
|
+
const raw = { ...this.repo.read() };
|
|
92
|
+
const keys = ['sonarrApiKey', 'radarrApiKey', 'plexToken'];
|
|
93
|
+
const moved = keys.filter((k) => typeof raw[k] === 'string' && raw[k] !== '').length;
|
|
94
|
+
if (moved > 0)
|
|
95
|
+
this.save(this.get());
|
|
96
|
+
return moved;
|
|
97
|
+
}
|
|
84
98
|
keepSecret(name, value) {
|
|
85
99
|
if (this.secrets.get(name) !== value)
|
|
86
100
|
this.secrets.set(name, value);
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { GenericSiteConfig } from '../types';
|
|
2
|
+
import { SecretStore } from '../ports/secret-store';
|
|
2
3
|
import { StateStore } from '../ports/state-store';
|
|
3
4
|
export declare class SitesRepository {
|
|
5
|
+
private readonly secrets;
|
|
4
6
|
private readonly repo;
|
|
5
|
-
constructor(state: StateStore);
|
|
7
|
+
constructor(state: StateStore, secrets: SecretStore);
|
|
6
8
|
getAll(): GenericSiteConfig[];
|
|
7
9
|
getById(id: string): GenericSiteConfig | undefined;
|
|
8
10
|
save(site: GenericSiteConfig): void;
|
|
9
11
|
delete(id: string): boolean;
|
|
12
|
+
/** Moves headers an older state file still holds into the secret store. Returns how many sites it moved; a second run finds none. */
|
|
13
|
+
migratePlaintext(): number;
|
|
14
|
+
private hydrate;
|
|
10
15
|
}
|
|
@@ -15,25 +15,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.SitesRepository = void 0;
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const tokens_1 = require("../ports/tokens");
|
|
18
|
+
const secret_store_1 = require("../ports/secret-store");
|
|
19
|
+
const webhook_repository_1 = require("./webhook.repository");
|
|
18
20
|
const state_store_1 = require("../ports/state-store");
|
|
19
21
|
let SitesRepository = class SitesRepository {
|
|
20
|
-
constructor(state) {
|
|
22
|
+
constructor(state, secrets) {
|
|
23
|
+
this.secrets = secrets;
|
|
21
24
|
this.repo = new state_store_1.StoredCollection(state, 'sites', { sites: [] });
|
|
22
25
|
}
|
|
23
26
|
getAll() {
|
|
24
|
-
return this.repo.read().sites;
|
|
27
|
+
return this.repo.read().sites.map((s) => this.hydrate(s));
|
|
25
28
|
}
|
|
26
29
|
getById(id) {
|
|
27
30
|
return this.getAll().find((s) => s.id === id);
|
|
28
31
|
}
|
|
29
32
|
save(site) {
|
|
33
|
+
const { headers, ...stored } = site;
|
|
34
|
+
// Secret first, state after (see SettingsRepository.save): a crash between the two never loses the headers.
|
|
35
|
+
const name = secret_store_1.secretNames.siteHeaders(site.id);
|
|
36
|
+
if (headers && Object.keys(headers).length > 0) {
|
|
37
|
+
const text = JSON.stringify(headers);
|
|
38
|
+
if (this.secrets.get(name) !== text)
|
|
39
|
+
this.secrets.set(name, text);
|
|
40
|
+
}
|
|
41
|
+
else if (this.secrets.get(name) !== undefined) {
|
|
42
|
+
this.secrets.delete(name);
|
|
43
|
+
}
|
|
30
44
|
const store = this.repo.read();
|
|
31
45
|
const idx = store.sites.findIndex((s) => s.id === site.id);
|
|
32
46
|
if (idx >= 0) {
|
|
33
|
-
store.sites[idx] =
|
|
47
|
+
store.sites[idx] = stored;
|
|
34
48
|
}
|
|
35
49
|
else {
|
|
36
|
-
store.sites.push(
|
|
50
|
+
store.sites.push(stored);
|
|
37
51
|
}
|
|
38
52
|
this.repo.write(store);
|
|
39
53
|
}
|
|
@@ -43,14 +57,29 @@ let SitesRepository = class SitesRepository {
|
|
|
43
57
|
store.sites = store.sites.filter((s) => s.id !== id);
|
|
44
58
|
if (store.sites.length < before) {
|
|
45
59
|
this.repo.write(store);
|
|
60
|
+
this.secrets.delete(secret_store_1.secretNames.siteHeaders(id));
|
|
46
61
|
return true;
|
|
47
62
|
}
|
|
48
63
|
return false;
|
|
49
64
|
}
|
|
65
|
+
/** Moves headers an older state file still holds into the secret store. Returns how many sites it moved; a second run finds none. */
|
|
66
|
+
migratePlaintext() {
|
|
67
|
+
const legacy = this.repo.read().sites.filter((s) => s.headers !== undefined);
|
|
68
|
+
for (const stored of legacy)
|
|
69
|
+
this.save(this.hydrate(stored));
|
|
70
|
+
return legacy.filter((s) => Object.keys(s.headers ?? {}).length > 0).length;
|
|
71
|
+
}
|
|
72
|
+
hydrate(stored) {
|
|
73
|
+
const text = this.secrets.get(secret_store_1.secretNames.siteHeaders(stored.id));
|
|
74
|
+
const headers = text === undefined ? stored.headers : (0, webhook_repository_1.parseHeaders)(text);
|
|
75
|
+
const { headers: _own, ...rest } = stored;
|
|
76
|
+
return headers && Object.keys(headers).length > 0 ? { ...rest, headers } : rest;
|
|
77
|
+
}
|
|
50
78
|
};
|
|
51
79
|
exports.SitesRepository = SitesRepository;
|
|
52
80
|
exports.SitesRepository = SitesRepository = __decorate([
|
|
53
81
|
(0, common_1.Injectable)(),
|
|
54
82
|
__param(0, (0, common_1.Inject)(tokens_1.STATE_STORE)),
|
|
55
|
-
|
|
83
|
+
__param(1, (0, common_1.Inject)(tokens_1.SECRET_STORE)),
|
|
84
|
+
__metadata("design:paramtypes", [Object, Object])
|
|
56
85
|
], SitesRepository);
|
|
@@ -17,5 +17,8 @@ export declare class WebhookRepository {
|
|
|
17
17
|
getById(id: string): WebhookConfig | undefined;
|
|
18
18
|
save(webhook: WebhookConfig): void;
|
|
19
19
|
delete(id: string): boolean;
|
|
20
|
+
/** Moves headers an older state file still holds into the secret store. Returns how many webhooks it moved; a second run finds none. */
|
|
21
|
+
migratePlaintext(): number;
|
|
20
22
|
private hydrate;
|
|
21
23
|
}
|
|
24
|
+
export declare function parseHeaders(text: string): Record<string, string>;
|
|
@@ -13,6 +13,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.WebhookRepository = void 0;
|
|
16
|
+
exports.parseHeaders = parseHeaders;
|
|
16
17
|
const common_1 = require("@nestjs/common");
|
|
17
18
|
const tokens_1 = require("../ports/tokens");
|
|
18
19
|
const secret_store_1 = require("../ports/secret-store");
|
|
@@ -30,6 +31,11 @@ let WebhookRepository = class WebhookRepository {
|
|
|
30
31
|
}
|
|
31
32
|
save(webhook) {
|
|
32
33
|
const { headers, ...stored } = webhook;
|
|
34
|
+
// Secret first, state after (see SettingsRepository.save): a crash between the two never loses the headers.
|
|
35
|
+
const text = JSON.stringify(headers ?? {});
|
|
36
|
+
const name = secret_store_1.secretNames.webhookHeaders(webhook.id);
|
|
37
|
+
if (this.secrets.get(name) !== text)
|
|
38
|
+
this.secrets.set(name, text);
|
|
33
39
|
const store = this.repo.read();
|
|
34
40
|
const idx = store.webhooks.findIndex((w) => w.id === webhook.id);
|
|
35
41
|
if (idx >= 0) {
|
|
@@ -39,10 +45,6 @@ let WebhookRepository = class WebhookRepository {
|
|
|
39
45
|
store.webhooks.push(stored);
|
|
40
46
|
}
|
|
41
47
|
this.repo.write(store);
|
|
42
|
-
const text = JSON.stringify(headers ?? {});
|
|
43
|
-
const name = secret_store_1.secretNames.webhookHeaders(webhook.id);
|
|
44
|
-
if (this.secrets.get(name) !== text)
|
|
45
|
-
this.secrets.set(name, text);
|
|
46
48
|
}
|
|
47
49
|
delete(id) {
|
|
48
50
|
const store = this.repo.read();
|
|
@@ -55,6 +57,13 @@ let WebhookRepository = class WebhookRepository {
|
|
|
55
57
|
}
|
|
56
58
|
return false;
|
|
57
59
|
}
|
|
60
|
+
/** Moves headers an older state file still holds into the secret store. Returns how many webhooks it moved; a second run finds none. */
|
|
61
|
+
migratePlaintext() {
|
|
62
|
+
const legacy = this.repo.read().webhooks.filter((w) => w.headers !== undefined);
|
|
63
|
+
for (const stored of legacy)
|
|
64
|
+
this.save(this.hydrate(stored));
|
|
65
|
+
return legacy.filter((w) => Object.keys(w.headers ?? {}).length > 0).length;
|
|
66
|
+
}
|
|
58
67
|
hydrate(stored) {
|
|
59
68
|
const text = this.secrets.get(secret_store_1.secretNames.webhookHeaders(stored.id));
|
|
60
69
|
return { ...stored, headers: text === undefined ? stored.headers ?? {} : parseHeaders(text) };
|
package/dist/scraper.module.js
CHANGED
|
@@ -65,6 +65,7 @@ const captcha_controller_1 = require("./controllers/captcha.controller");
|
|
|
65
65
|
const plex_controller_1 = require("./controllers/plex.controller");
|
|
66
66
|
const plex_service_1 = require("./services/plex.service");
|
|
67
67
|
const captcha_service_1 = require("./services/captcha.service");
|
|
68
|
+
const secret_migration_service_1 = require("./services/secret-migration.service");
|
|
68
69
|
const captcha_challenges_repository_1 = require("./repositories/captcha-challenges.repository");
|
|
69
70
|
const CONTROLLERS = [
|
|
70
71
|
core_controller_1.CoreController,
|
|
@@ -121,6 +122,7 @@ const PROVIDERS = [
|
|
|
121
122
|
vpn_service_1.VpnService,
|
|
122
123
|
captcha_service_1.CaptchaService,
|
|
123
124
|
book_adapter_registry_service_1.BookAdapterRegistryService,
|
|
125
|
+
secret_migration_service_1.SecretMigrationService,
|
|
124
126
|
];
|
|
125
127
|
let ScraperModule = class ScraperModule {
|
|
126
128
|
};
|
|
@@ -6,6 +6,8 @@ export { PluginSettingsError };
|
|
|
6
6
|
export interface PluginSettingsView {
|
|
7
7
|
fields: PluginSettingField[];
|
|
8
8
|
values: PluginSettingValues;
|
|
9
|
+
/** Text settings whose name reads like a credential: their value is never shown (`''`), and an empty one on a write keeps the stored value. */
|
|
10
|
+
secretKeys: string[];
|
|
9
11
|
}
|
|
10
12
|
export declare class PluginSettingsService {
|
|
11
13
|
private readonly loader;
|
|
@@ -11,10 +11,19 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.PluginSettingsService = exports.PluginSettingsError = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
|
+
const secret_views_1 = require("./secret-views");
|
|
14
15
|
const plugin_settings_repository_1 = require("../repositories/plugin-settings.repository");
|
|
15
16
|
const plugin_loader_service_1 = require("./plugin-loader.service");
|
|
16
17
|
const plugin_settings_1 = require("./plugin-settings");
|
|
17
18
|
Object.defineProperty(exports, "PluginSettingsError", { enumerable: true, get: function () { return plugin_settings_1.PluginSettingsError; } });
|
|
19
|
+
/** The text settings of a plugin that hold a credential, by the name heuristic (a schema flag may replace it). */
|
|
20
|
+
function secretKeysOf(fields) {
|
|
21
|
+
return fields.filter((f) => f.type === 'string' && (0, secret_views_1.isCredentialName)(f.key)).map((f) => f.key);
|
|
22
|
+
}
|
|
23
|
+
function viewOf(fields, values) {
|
|
24
|
+
const secretKeys = secretKeysOf(fields);
|
|
25
|
+
return { fields, values: { ...values, ...Object.fromEntries(secretKeys.map((k) => [k, ''])) }, secretKeys };
|
|
26
|
+
}
|
|
18
27
|
let PluginSettingsService = class PluginSettingsService {
|
|
19
28
|
constructor(loader, repo) {
|
|
20
29
|
this.loader = loader;
|
|
@@ -25,7 +34,7 @@ let PluginSettingsService = class PluginSettingsService {
|
|
|
25
34
|
const handle = this.loader.getSettingsHandle(pluginName);
|
|
26
35
|
if (!handle)
|
|
27
36
|
return null;
|
|
28
|
-
return
|
|
37
|
+
return viewOf(handle.fields, (0, plugin_settings_1.resolveSettingValues)(handle.fields, this.repo.getFor(pluginName)));
|
|
29
38
|
}
|
|
30
39
|
/**
|
|
31
40
|
* Validates, applies live through the plugin's `configure`, and persists only
|
|
@@ -35,7 +44,11 @@ let PluginSettingsService = class PluginSettingsService {
|
|
|
35
44
|
const handle = this.loader.getSettingsHandle(pluginName);
|
|
36
45
|
if (!handle)
|
|
37
46
|
throw new plugin_settings_1.PluginSettingsError(`Plugin "${pluginName}" has no settings`);
|
|
38
|
-
const
|
|
47
|
+
const secretKeys = secretKeysOf(handle.fields);
|
|
48
|
+
const typed = typeof input === 'object' && input !== null && !Array.isArray(input)
|
|
49
|
+
? Object.fromEntries(Object.entries(input).filter(([k, v]) => !(secretKeys.includes(k) && (v === '' || v === undefined || v === null))))
|
|
50
|
+
: input;
|
|
51
|
+
const changes = (0, plugin_settings_1.validateSettingValues)(handle.fields, typed);
|
|
39
52
|
const merged = (0, plugin_settings_1.resolveSettingValues)(handle.fields, { ...this.repo.getFor(pluginName), ...changes });
|
|
40
53
|
try {
|
|
41
54
|
handle.configure?.(merged);
|
|
@@ -44,7 +57,7 @@ let PluginSettingsService = class PluginSettingsService {
|
|
|
44
57
|
throw new plugin_settings_1.PluginSettingsError(err instanceof Error ? err.message : String(err));
|
|
45
58
|
}
|
|
46
59
|
this.repo.setFor(pluginName, merged);
|
|
47
|
-
return
|
|
60
|
+
return viewOf(handle.fields, merged);
|
|
48
61
|
}
|
|
49
62
|
};
|
|
50
63
|
exports.PluginSettingsService = PluginSettingsService;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { SettingsRepository } from '../repositories/settings.repository';
|
|
3
|
+
import { SitesRepository } from '../repositories/sites.repository';
|
|
4
|
+
import { WebhookRepository } from '../repositories/webhook.repository';
|
|
5
|
+
/**
|
|
6
|
+
* At boot, moves every credential an older state file still holds in plain text (API keys, the Plex token,
|
|
7
|
+
* webhook and site headers) into the secret store and leaves none behind. Idempotent: a second boot finds
|
|
8
|
+
* nothing. Logs counts only, never a value.
|
|
9
|
+
*/
|
|
10
|
+
export declare class SecretMigrationService implements OnModuleInit {
|
|
11
|
+
private readonly settings;
|
|
12
|
+
private readonly webhooks;
|
|
13
|
+
private readonly sites;
|
|
14
|
+
private readonly logger;
|
|
15
|
+
constructor(settings: SettingsRepository, webhooks: WebhookRepository, sites: SitesRepository);
|
|
16
|
+
onModuleInit(): void;
|
|
17
|
+
run(): {
|
|
18
|
+
settings: number;
|
|
19
|
+
webhooks: number;
|
|
20
|
+
sites: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var SecretMigrationService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.SecretMigrationService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const logger_1 = require("../ports/logger");
|
|
16
|
+
const settings_repository_1 = require("../repositories/settings.repository");
|
|
17
|
+
const sites_repository_1 = require("../repositories/sites.repository");
|
|
18
|
+
const webhook_repository_1 = require("../repositories/webhook.repository");
|
|
19
|
+
/**
|
|
20
|
+
* At boot, moves every credential an older state file still holds in plain text (API keys, the Plex token,
|
|
21
|
+
* webhook and site headers) into the secret store and leaves none behind. Idempotent: a second boot finds
|
|
22
|
+
* nothing. Logs counts only, never a value.
|
|
23
|
+
*/
|
|
24
|
+
let SecretMigrationService = SecretMigrationService_1 = class SecretMigrationService {
|
|
25
|
+
constructor(settings, webhooks, sites) {
|
|
26
|
+
this.settings = settings;
|
|
27
|
+
this.webhooks = webhooks;
|
|
28
|
+
this.sites = sites;
|
|
29
|
+
this.logger = new logger_1.ScopedLogger(SecretMigrationService_1.name);
|
|
30
|
+
}
|
|
31
|
+
onModuleInit() {
|
|
32
|
+
this.run();
|
|
33
|
+
}
|
|
34
|
+
run() {
|
|
35
|
+
const moved = { settings: this.settings.migratePlaintext(), webhooks: this.webhooks.migratePlaintext(), sites: this.sites.migratePlaintext() };
|
|
36
|
+
if (moved.settings + moved.webhooks + moved.sites > 0) {
|
|
37
|
+
this.logger.log(`Moved plaintext credentials to the secret store: ${moved.settings} setting(s), ${moved.webhooks} webhook(s), ${moved.sites} site(s)`);
|
|
38
|
+
}
|
|
39
|
+
return moved;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.SecretMigrationService = SecretMigrationService;
|
|
43
|
+
exports.SecretMigrationService = SecretMigrationService = SecretMigrationService_1 = __decorate([
|
|
44
|
+
(0, common_1.Injectable)(),
|
|
45
|
+
__metadata("design:paramtypes", [settings_repository_1.SettingsRepository,
|
|
46
|
+
webhook_repository_1.WebhookRepository,
|
|
47
|
+
sites_repository_1.SitesRepository])
|
|
48
|
+
], SecretMigrationService);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AppSettings } from '../types';
|
|
2
|
+
import type { WebhookConfig } from '../repositories/webhook.repository';
|
|
3
|
+
import type { GenericSiteConfig } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* What the HTTP API shows of a credential: whether it is set, never the value. A credential is
|
|
6
|
+
* written only when typed (an empty field keeps the stored one) and removed by an explicit clear.
|
|
7
|
+
*/
|
|
8
|
+
export declare const SETTINGS_SECRET_KEYS: readonly ["sonarrApiKey", "radarrApiKey", "plexToken"];
|
|
9
|
+
export type SettingsSecretKey = (typeof SETTINGS_SECRET_KEYS)[number];
|
|
10
|
+
/** The settings as the API answers them: every credential is `''` and its `<name>Set` flag says whether one is stored. */
|
|
11
|
+
export type SettingsView = Omit<AppSettings, SettingsSecretKey> & Record<SettingsSecretKey, ''> & Record<`${SettingsSecretKey}Set`, boolean>;
|
|
12
|
+
export declare function toSettingsView(settings: AppSettings): SettingsView;
|
|
13
|
+
/**
|
|
14
|
+
* The partial settings a PATCH body asks for. The view-only `<name>Set` flags are dropped, an empty
|
|
15
|
+
* credential keeps the stored one, and `clear<Name>: true` removes it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseSettingsBody(body: unknown): Partial<AppSettings>;
|
|
18
|
+
/** A name that reads like a credential: Authorization, X-Auth-User, X-Api-Key, Cookie, X-Session-Id, a plugin's `apiToken`. Over-matching is safe: such a value is only hidden, and a typed one still goes through. */
|
|
19
|
+
export declare const isCredentialName: (name: string) => boolean;
|
|
20
|
+
export declare const isCredentialHeader: (name: string) => boolean;
|
|
21
|
+
/** A webhook as the API answers it: a credential header keeps its name and answers `''`, and is listed in `secretHeaders`. */
|
|
22
|
+
export type WebhookView = Omit<WebhookConfig, 'headers'> & {
|
|
23
|
+
headers: Record<string, string>;
|
|
24
|
+
secretHeaders: string[];
|
|
25
|
+
urlSecretSet: boolean;
|
|
26
|
+
};
|
|
27
|
+
export declare function toWebhookView(webhook: WebhookConfig): WebhookView;
|
|
28
|
+
/**
|
|
29
|
+
* A webhook address in two halves: the origin (scheme://host:port), which is shown and editable, and the rest
|
|
30
|
+
* (path and query, where a token usually sits), which is never shown. An address that does not parse is all secret.
|
|
31
|
+
*/
|
|
32
|
+
export declare function splitUrl(url: string): {
|
|
33
|
+
origin: string;
|
|
34
|
+
secret: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* The address a webhook write leaves. A bare origin on the SAME origin keeps the stored path and query (an echoed
|
|
38
|
+
* view); a bare origin on another host means the address is that origin, so a token never follows to a new host. A
|
|
39
|
+
* full address replaces both. An absent or empty one keeps what is stored.
|
|
40
|
+
*/
|
|
41
|
+
export declare function mergeWebhookUrl(stored: string, incoming: unknown): string;
|
|
42
|
+
/**
|
|
43
|
+
* The headers a save leaves: the ones in `incoming`, where a credential header sent empty keeps its stored
|
|
44
|
+
* value. A name absent from `incoming` is removed, which is the explicit clear.
|
|
45
|
+
*/
|
|
46
|
+
export declare function mergeHeaders(stored: Record<string, string>, incoming: unknown): Record<string, string>;
|
|
47
|
+
/** A generic site as the API answers it: header names and plain values are shown, a credential header's value never. */
|
|
48
|
+
export type SiteView = Omit<GenericSiteConfig, 'headers'> & {
|
|
49
|
+
headers?: Record<string, string>;
|
|
50
|
+
secretHeaders?: string[];
|
|
51
|
+
};
|
|
52
|
+
export declare function toSiteView(site: GenericSiteConfig): SiteView;
|
|
53
|
+
/** A site as a write carries it: the view-only field dropped, the headers merged over the stored ones. */
|
|
54
|
+
export declare function siteFromWrite(body: Partial<GenericSiteConfig> & {
|
|
55
|
+
secretHeaders?: unknown;
|
|
56
|
+
}, stored?: GenericSiteConfig): Partial<GenericSiteConfig>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isCredentialHeader = exports.isCredentialName = exports.SETTINGS_SECRET_KEYS = void 0;
|
|
4
|
+
exports.toSettingsView = toSettingsView;
|
|
5
|
+
exports.parseSettingsBody = parseSettingsBody;
|
|
6
|
+
exports.toWebhookView = toWebhookView;
|
|
7
|
+
exports.splitUrl = splitUrl;
|
|
8
|
+
exports.mergeWebhookUrl = mergeWebhookUrl;
|
|
9
|
+
exports.mergeHeaders = mergeHeaders;
|
|
10
|
+
exports.toSiteView = toSiteView;
|
|
11
|
+
exports.siteFromWrite = siteFromWrite;
|
|
12
|
+
/**
|
|
13
|
+
* What the HTTP API shows of a credential: whether it is set, never the value. A credential is
|
|
14
|
+
* written only when typed (an empty field keeps the stored one) and removed by an explicit clear.
|
|
15
|
+
*/
|
|
16
|
+
exports.SETTINGS_SECRET_KEYS = ['sonarrApiKey', 'radarrApiKey', 'plexToken'];
|
|
17
|
+
function toSettingsView(settings) {
|
|
18
|
+
return {
|
|
19
|
+
...settings,
|
|
20
|
+
sonarrApiKey: '',
|
|
21
|
+
radarrApiKey: '',
|
|
22
|
+
plexToken: '',
|
|
23
|
+
sonarrApiKeySet: settings.sonarrApiKey !== '',
|
|
24
|
+
radarrApiKeySet: settings.radarrApiKey !== '',
|
|
25
|
+
plexTokenSet: settings.plexToken !== '',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The partial settings a PATCH body asks for. The view-only `<name>Set` flags are dropped, an empty
|
|
30
|
+
* credential keeps the stored one, and `clear<Name>: true` removes it.
|
|
31
|
+
*/
|
|
32
|
+
function parseSettingsBody(body) {
|
|
33
|
+
const raw = typeof body === 'object' && body !== null && !Array.isArray(body) ? { ...body } : {};
|
|
34
|
+
for (const key of exports.SETTINGS_SECRET_KEYS) {
|
|
35
|
+
const clear = `clear${key[0].toUpperCase()}${key.slice(1)}`;
|
|
36
|
+
const cleared = raw[clear] === true;
|
|
37
|
+
delete raw[`${key}Set`];
|
|
38
|
+
delete raw[clear];
|
|
39
|
+
if (cleared)
|
|
40
|
+
raw[key] = '';
|
|
41
|
+
else if (raw[key] === '' || raw[key] === undefined || raw[key] === null)
|
|
42
|
+
delete raw[key];
|
|
43
|
+
}
|
|
44
|
+
return raw;
|
|
45
|
+
}
|
|
46
|
+
/** A header whose value carries a credential: it is never shown, only its name. */
|
|
47
|
+
const CREDENTIAL_NAME = /auth|key|token|secret|pass|cookie|sign|cred|session/i;
|
|
48
|
+
/** A name that reads like a credential: Authorization, X-Auth-User, X-Api-Key, Cookie, X-Session-Id, a plugin's `apiToken`. Over-matching is safe: such a value is only hidden, and a typed one still goes through. */
|
|
49
|
+
const isCredentialName = (name) => CREDENTIAL_NAME.test(name);
|
|
50
|
+
exports.isCredentialName = isCredentialName;
|
|
51
|
+
exports.isCredentialHeader = exports.isCredentialName;
|
|
52
|
+
function toWebhookView(webhook) {
|
|
53
|
+
const secretHeaders = Object.keys(webhook.headers).filter(exports.isCredentialHeader);
|
|
54
|
+
const headers = Object.fromEntries(Object.entries(webhook.headers).map(([name, value]) => [name, (0, exports.isCredentialHeader)(name) ? '' : value]));
|
|
55
|
+
const { origin, secret } = splitUrl(webhook.url);
|
|
56
|
+
return { ...webhook, url: origin, urlSecretSet: secret !== '', headers, secretHeaders };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A webhook address in two halves: the origin (scheme://host:port), which is shown and editable, and the rest
|
|
60
|
+
* (path and query, where a token usually sits), which is never shown. An address that does not parse is all secret.
|
|
61
|
+
*/
|
|
62
|
+
function splitUrl(url) {
|
|
63
|
+
try {
|
|
64
|
+
const parsed = new URL(url);
|
|
65
|
+
const rest = `${parsed.pathname === '/' ? '' : parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
66
|
+
return { origin: parsed.origin, secret: rest };
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return { origin: '', secret: url };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The address a webhook write leaves. A bare origin on the SAME origin keeps the stored path and query (an echoed
|
|
74
|
+
* view); a bare origin on another host means the address is that origin, so a token never follows to a new host. A
|
|
75
|
+
* full address replaces both. An absent or empty one keeps what is stored.
|
|
76
|
+
*/
|
|
77
|
+
function mergeWebhookUrl(stored, incoming) {
|
|
78
|
+
if (typeof incoming !== 'string' || incoming === '')
|
|
79
|
+
return stored;
|
|
80
|
+
const next = splitUrl(incoming);
|
|
81
|
+
if (next.origin === '' || next.secret !== '')
|
|
82
|
+
return incoming;
|
|
83
|
+
const prior = splitUrl(stored);
|
|
84
|
+
return prior.origin === next.origin ? `${next.origin}${prior.secret}` : next.origin;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The headers a save leaves: the ones in `incoming`, where a credential header sent empty keeps its stored
|
|
88
|
+
* value. A name absent from `incoming` is removed, which is the explicit clear.
|
|
89
|
+
*/
|
|
90
|
+
function mergeHeaders(stored, incoming) {
|
|
91
|
+
if (typeof incoming !== 'object' || incoming === null || Array.isArray(incoming))
|
|
92
|
+
return stored;
|
|
93
|
+
const out = {};
|
|
94
|
+
for (const [name, value] of Object.entries(incoming)) {
|
|
95
|
+
if (typeof value !== 'string')
|
|
96
|
+
continue;
|
|
97
|
+
if (value === '' && (0, exports.isCredentialHeader)(name)) {
|
|
98
|
+
// an empty credential keeps the stored value; with none stored there is nothing to keep, so it is not saved as a literal empty header
|
|
99
|
+
if (name in stored)
|
|
100
|
+
out[name] = stored[name];
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
out[name] = value;
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
function toSiteView(site) {
|
|
108
|
+
if (!site.headers || Object.keys(site.headers).length === 0)
|
|
109
|
+
return site;
|
|
110
|
+
const secretHeaders = Object.keys(site.headers).filter(exports.isCredentialHeader);
|
|
111
|
+
const headers = Object.fromEntries(Object.entries(site.headers).map(([name, value]) => [name, (0, exports.isCredentialHeader)(name) ? '' : value]));
|
|
112
|
+
return { ...site, headers, secretHeaders };
|
|
113
|
+
}
|
|
114
|
+
/** A site as a write carries it: the view-only field dropped, the headers merged over the stored ones. */
|
|
115
|
+
function siteFromWrite(body, stored) {
|
|
116
|
+
const { secretHeaders: _view, headers, ...rest } = body;
|
|
117
|
+
if (headers === undefined)
|
|
118
|
+
return rest;
|
|
119
|
+
return { ...rest, headers: mergeHeaders(stored?.headers ?? {}, headers) };
|
|
120
|
+
}
|
package/package.json
CHANGED