@serve.zone/appstore 0.2.0
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/.smartconfig.json +44 -0
- package/apps/adminer/app.json +10 -0
- package/apps/adminer/versions/1.0.0/config.json +4 -0
- package/apps/cloudly/app.json +14 -0
- package/apps/cloudly/versions/1.0.0/config.json +101 -0
- package/apps/ghost/app.json +10 -0
- package/apps/ghost/versions/1.0.0/config.json +16 -0
- package/apps/gitea/app.json +10 -0
- package/apps/gitea/versions/1.0.0/config.json +14 -0
- package/apps/gitops/app.json +14 -0
- package/apps/gitops/versions/1.0.0/config.json +26 -0
- package/apps/grafana/app.json +10 -0
- package/apps/grafana/versions/1.0.0/config.json +7 -0
- package/apps/mariadb/app.json +10 -0
- package/apps/mariadb/versions/1.0.0/config.json +7 -0
- package/apps/mattermost/app.json +10 -0
- package/apps/mattermost/versions/1.0.0/config.json +4 -0
- package/apps/n8n/app.json +10 -0
- package/apps/n8n/versions/1.0.0/config.json +4 -0
- package/apps/nextcloud/app.json +10 -0
- package/apps/nextcloud/versions/1.0.0/config.json +14 -0
- package/apps/nginx/app.json +10 -0
- package/apps/nginx/versions/1.0.0/config.json +4 -0
- package/apps/plausible/app.json +10 -0
- package/apps/plausible/versions/1.0.0/config.json +7 -0
- package/apps/portainer/app.json +10 -0
- package/apps/portainer/versions/1.0.0/config.json +4 -0
- package/apps/postgres/app.json +10 -0
- package/apps/postgres/versions/1.0.0/config.json +9 -0
- package/apps/redis/app.json +10 -0
- package/apps/redis/versions/1.0.0/config.json +4 -0
- package/apps/rustdesk-server/app.json +14 -0
- package/apps/rustdesk-server/versions/1.0.0/config.json +17 -0
- package/apps/siprouter/app.json +14 -0
- package/apps/siprouter/versions/1.0.0/config.json +78 -0
- package/apps/uptime-kuma/app.json +10 -0
- package/apps/uptime-kuma/versions/1.0.0/config.json +4 -0
- package/apps/vaultwarden/app.json +10 -0
- package/apps/vaultwarden/versions/1.0.0/config.json +4 -0
- package/apps/wordpress/app.json +10 -0
- package/apps/wordpress/versions/1.0.0/config.json +13 -0
- package/appstore.json +191 -0
- package/changelog.md +14 -0
- package/dist_ts_client/classes.appstoreresolver.d.ts +39 -0
- package/dist_ts_client/classes.appstoreresolver.js +513 -0
- package/dist_ts_client/index.d.ts +2 -0
- package/dist_ts_client/index.js +3 -0
- package/dist_ts_client/types.d.ts +27 -0
- package/dist_ts_client/types.js +2 -0
- package/license +21 -0
- package/package.json +52 -0
- package/readme.md +180 -0
- package/ts_client/classes.appstoreresolver.ts +623 -0
- package/ts_client/index.ts +2 -0
- package/ts_client/tspublish.json +3 -0
- package/ts_client/types.ts +31 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
import type * as interfaces from '@serve.zone/interfaces';
|
|
2
|
+
import type {
|
|
3
|
+
IAppStoreResolverOptions,
|
|
4
|
+
IParsedDockerImageReference,
|
|
5
|
+
IResolvedAppStoreApp,
|
|
6
|
+
TAppStoreApp,
|
|
7
|
+
TAppStoreDockerImageSource,
|
|
8
|
+
TAppStoreIndex,
|
|
9
|
+
TAppStoreRepoManifestSource,
|
|
10
|
+
TAppStoreResolvedSource,
|
|
11
|
+
TAppStoreVersionConfig,
|
|
12
|
+
TServezoneAppStoreManifest,
|
|
13
|
+
} from './types.js';
|
|
14
|
+
|
|
15
|
+
export class AppStoreResolver {
|
|
16
|
+
public readonly baseUrl: string;
|
|
17
|
+
private readonly fetchRef: typeof fetch;
|
|
18
|
+
private readonly resolveDockerDigests: boolean;
|
|
19
|
+
private readonly now: () => Date;
|
|
20
|
+
private appStoreCache: TAppStoreIndex | null = null;
|
|
21
|
+
private sourceAppCache = new Map<string, IResolvedAppStoreApp>();
|
|
22
|
+
|
|
23
|
+
constructor(optionsArg: IAppStoreResolverOptions = {}) {
|
|
24
|
+
this.baseUrl = optionsArg.baseUrl || 'https://code.foss.global/serve.zone/appstore/raw/branch/main';
|
|
25
|
+
this.fetchRef = optionsArg.fetch || fetch;
|
|
26
|
+
this.resolveDockerDigests = optionsArg.resolveDockerDigests ?? true;
|
|
27
|
+
this.now = optionsArg.now || (() => new Date());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public async getAppStoreIndex(): Promise<TAppStoreIndex> {
|
|
31
|
+
if (this.appStoreCache) {
|
|
32
|
+
return this.appStoreCache;
|
|
33
|
+
}
|
|
34
|
+
const appStore = await this.fetchAppStoreIndex();
|
|
35
|
+
this.appStoreCache = await this.resolveAppStore(appStore);
|
|
36
|
+
return this.appStoreCache;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public async getApps(): Promise<TAppStoreApp[]> {
|
|
40
|
+
return (await this.getAppStoreIndex()).apps;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async getAppMeta(appIdArg: string): Promise<interfaces.appstore.IAppStoreAppMeta> {
|
|
44
|
+
const app = await this.getAppStoreApp(appIdArg);
|
|
45
|
+
if (app?.source?.type === 'repoManifest') {
|
|
46
|
+
return (await this.resolveRepoManifestSource(app.source, app)).appMeta;
|
|
47
|
+
}
|
|
48
|
+
if (app?.source?.type === 'dockerImage') {
|
|
49
|
+
return this.createAppMetaFromAppStoreApp(app);
|
|
50
|
+
}
|
|
51
|
+
return await this.fetchJson(`apps/${appIdArg}/app.json`) as interfaces.appstore.IAppStoreAppMeta;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public async getAppVersionConfig(
|
|
55
|
+
appIdArg: string,
|
|
56
|
+
versionArg: string,
|
|
57
|
+
): Promise<TAppStoreVersionConfig> {
|
|
58
|
+
const app = await this.getAppStoreApp(appIdArg);
|
|
59
|
+
if (app?.source?.type === 'repoManifest') {
|
|
60
|
+
const resolvedApp = await this.resolveRepoManifestSource(app.source, app);
|
|
61
|
+
const config = resolvedApp.configsByVersion.get(versionArg);
|
|
62
|
+
if (!config) {
|
|
63
|
+
throw new Error(`Version '${versionArg}' is not defined by the linked appstore manifest`);
|
|
64
|
+
}
|
|
65
|
+
this.validateAppStoreVersionConfig(config, `${appIdArg}@${versionArg}`);
|
|
66
|
+
return config;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (app?.source?.type === 'dockerImage' && app.runtime) {
|
|
70
|
+
const config: TAppStoreVersionConfig = { ...app.runtime };
|
|
71
|
+
await this.applyDockerImageSourceToConfig(app.source, config, versionArg);
|
|
72
|
+
this.validateAppStoreVersionConfig(config, `${appIdArg}@${versionArg}`);
|
|
73
|
+
return config;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let config: TAppStoreVersionConfig;
|
|
77
|
+
try {
|
|
78
|
+
config = await this.fetchJson(`apps/${appIdArg}/versions/${versionArg}/config.json`) as TAppStoreVersionConfig;
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (app?.source?.type !== 'dockerImage') {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
const appMeta = await this.fetchJson(`apps/${appIdArg}/app.json`) as interfaces.appstore.IAppStoreAppMeta;
|
|
84
|
+
config = await this.fetchJson(`apps/${appIdArg}/versions/${appMeta.latestVersion}/config.json`) as TAppStoreVersionConfig;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (app?.source?.type === 'dockerImage') {
|
|
88
|
+
await this.applyDockerImageSourceToConfig(app.source, config, versionArg);
|
|
89
|
+
}
|
|
90
|
+
this.validateAppStoreVersionConfig(config, `${appIdArg}@${versionArg}`);
|
|
91
|
+
return config;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public async resolveAppStore(appStoreArg: unknown): Promise<TAppStoreIndex> {
|
|
95
|
+
const appStore = parseAppStoreIndex(appStoreArg);
|
|
96
|
+
this.sourceAppCache.clear();
|
|
97
|
+
const apps: TAppStoreApp[] = [];
|
|
98
|
+
|
|
99
|
+
for (const app of appStore.apps) {
|
|
100
|
+
apps.push(await this.resolveAppStoreApp(app));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...appStore,
|
|
105
|
+
apps,
|
|
106
|
+
resolvedAt: this.now().toISOString(),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public async resolveRepoManifestSource(
|
|
111
|
+
sourceArg: TAppStoreRepoManifestSource,
|
|
112
|
+
appArg?: TAppStoreApp,
|
|
113
|
+
): Promise<IResolvedAppStoreApp> {
|
|
114
|
+
const cacheKey = `${sourceArg.url}#${sourceArg.ref || ''}`;
|
|
115
|
+
const cachedApp = this.sourceAppCache.get(cacheKey);
|
|
116
|
+
if (cachedApp) {
|
|
117
|
+
return cachedApp;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const manifestText = await this.fetchTextFromUrl(sourceArg.url);
|
|
121
|
+
const manifestHash = await createSha256Hex(manifestText);
|
|
122
|
+
const manifest = parseServezoneAppStoreManifest(JSON.parse(manifestText));
|
|
123
|
+
const resolvedApp = await this.resolveServezoneAppStoreManifest(manifest, {
|
|
124
|
+
type: 'repoManifest',
|
|
125
|
+
url: sourceArg.url,
|
|
126
|
+
ref: sourceArg.ref,
|
|
127
|
+
manifestHash,
|
|
128
|
+
resolvedAt: this.now().toISOString(),
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (appArg) {
|
|
132
|
+
resolvedApp.appStoreApp = {
|
|
133
|
+
...resolvedApp.appStoreApp,
|
|
134
|
+
...withoutUndefined(appArg),
|
|
135
|
+
latestVersion: resolvedApp.appStoreApp.latestVersion,
|
|
136
|
+
versions: resolvedApp.appStoreApp.versions,
|
|
137
|
+
source: appArg.source,
|
|
138
|
+
tags: appArg.tags || resolvedApp.appStoreApp.tags,
|
|
139
|
+
resolvedSource: resolvedApp.appStoreApp.resolvedSource,
|
|
140
|
+
};
|
|
141
|
+
resolvedApp.appMeta = {
|
|
142
|
+
...resolvedApp.appMeta,
|
|
143
|
+
id: resolvedApp.appStoreApp.id,
|
|
144
|
+
name: resolvedApp.appStoreApp.name,
|
|
145
|
+
description: resolvedApp.appStoreApp.description,
|
|
146
|
+
category: resolvedApp.appStoreApp.category,
|
|
147
|
+
iconName: resolvedApp.appStoreApp.iconName,
|
|
148
|
+
latestVersion: resolvedApp.appStoreApp.latestVersion,
|
|
149
|
+
versions: resolvedApp.appStoreApp.versions || resolvedApp.appMeta.versions,
|
|
150
|
+
tags: resolvedApp.appStoreApp.tags,
|
|
151
|
+
source: appArg.source,
|
|
152
|
+
resolvedSource: resolvedApp.appStoreApp.resolvedSource,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.sourceAppCache.set(cacheKey, resolvedApp);
|
|
157
|
+
return resolvedApp;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public async resolveDockerImageSource(
|
|
161
|
+
sourceArg: TAppStoreDockerImageSource,
|
|
162
|
+
): Promise<TAppStoreResolvedSource> {
|
|
163
|
+
let imageDigest: string | undefined;
|
|
164
|
+
if (sourceArg.tracking === 'digest' && this.resolveDockerDigests) {
|
|
165
|
+
imageDigest = await this.resolveDockerImageDigest(sourceArg.image) || undefined;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
type: 'dockerImage',
|
|
170
|
+
image: sourceArg.image,
|
|
171
|
+
imageDigest,
|
|
172
|
+
resolvedAt: this.now().toISOString(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public async resolveDockerImageDigest(imageArg: string): Promise<string | null> {
|
|
177
|
+
const parsedImage = parseDockerImageReference(imageArg);
|
|
178
|
+
if (parsedImage.digest) {
|
|
179
|
+
return parsedImage.digest;
|
|
180
|
+
}
|
|
181
|
+
return await this.fetchDockerManifestDigest(parsedImage);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public validateAppStoreVersionConfig(
|
|
185
|
+
configArg: TAppStoreVersionConfig,
|
|
186
|
+
labelArg = 'appstore config',
|
|
187
|
+
): void {
|
|
188
|
+
if (!configArg || typeof configArg !== 'object') {
|
|
189
|
+
throw new Error(`Invalid ${labelArg}: config must be an object`);
|
|
190
|
+
}
|
|
191
|
+
if (!configArg.image || typeof configArg.image !== 'string') {
|
|
192
|
+
throw new Error(`Invalid ${labelArg}: image is required`);
|
|
193
|
+
}
|
|
194
|
+
this.assertValidPort(configArg.port, `${labelArg} port`);
|
|
195
|
+
|
|
196
|
+
for (const envVar of configArg.envVars || []) {
|
|
197
|
+
if (!envVar.key || !/^[A-Z_][A-Z0-9_]*$/.test(envVar.key)) {
|
|
198
|
+
throw new Error(`Invalid ${labelArg}: env var key '${envVar.key}' is not valid`);
|
|
199
|
+
}
|
|
200
|
+
if (envVar.value !== undefined && typeof envVar.value !== 'string') {
|
|
201
|
+
throw new Error(`Invalid ${labelArg}: env var '${envVar.key}' value must be a string`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.normalizeVolumes(configArg.volumes);
|
|
206
|
+
this.validatePublishedPorts(configArg.publishedPorts || [], labelArg);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public normalizeVolumes(
|
|
210
|
+
volumesArg: TAppStoreVersionConfig['volumes'] = [],
|
|
211
|
+
): interfaces.appstore.IAppStoreVolume[] {
|
|
212
|
+
return volumesArg.map((volumeArg): interfaces.appstore.IAppStoreVolume => {
|
|
213
|
+
if (typeof volumeArg === 'string') {
|
|
214
|
+
return { mountPath: volumeArg };
|
|
215
|
+
}
|
|
216
|
+
return volumeArg;
|
|
217
|
+
}).map((volumeArg, indexArg) => {
|
|
218
|
+
this.validateVolume(volumeArg, `volume ${indexArg + 1}`);
|
|
219
|
+
return volumeArg;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private async fetchAppStoreIndex(): Promise<TAppStoreIndex> {
|
|
224
|
+
try {
|
|
225
|
+
return await this.fetchJson('appstore.resolved.json') as TAppStoreIndex;
|
|
226
|
+
} catch {
|
|
227
|
+
return await this.fetchJson('appstore.json') as TAppStoreIndex;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private async resolveAppStoreApp(appArg: TAppStoreApp): Promise<TAppStoreApp> {
|
|
232
|
+
if (appArg.source?.type === 'repoManifest') {
|
|
233
|
+
const resolvedApp = await this.resolveRepoManifestSource(appArg.source, appArg);
|
|
234
|
+
return {
|
|
235
|
+
...resolvedApp.appStoreApp,
|
|
236
|
+
...withoutUndefined(appArg),
|
|
237
|
+
latestVersion: resolvedApp.appStoreApp.latestVersion,
|
|
238
|
+
versions: resolvedApp.appStoreApp.versions,
|
|
239
|
+
source: appArg.source,
|
|
240
|
+
tags: appArg.tags || resolvedApp.appStoreApp.tags,
|
|
241
|
+
resolvedSource: resolvedApp.appStoreApp.resolvedSource,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (appArg.source?.type === 'dockerImage') {
|
|
246
|
+
const config = appArg.runtime ? { ...appArg.runtime } : undefined;
|
|
247
|
+
const resolvedSource = config
|
|
248
|
+
? (await this.applyDockerImageSourceToConfig(appArg.source, config, appArg.latestVersion)).resolvedSource
|
|
249
|
+
: await this.resolveDockerImageSource(appArg.source);
|
|
250
|
+
const latestVersion = createAppStoreVersionForDockerSource(
|
|
251
|
+
appArg.source,
|
|
252
|
+
appArg.latestVersion,
|
|
253
|
+
resolvedSource?.imageDigest,
|
|
254
|
+
);
|
|
255
|
+
return {
|
|
256
|
+
...appArg,
|
|
257
|
+
runtime: config,
|
|
258
|
+
latestVersion,
|
|
259
|
+
versions: uniqueStrings([...(appArg.versions || []), latestVersion]),
|
|
260
|
+
upgradeStrategy: appArg.source.tracking === 'digest' ? 'dockerDigest' : appArg.upgradeStrategy,
|
|
261
|
+
resolvedSource,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return appArg;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private async resolveServezoneAppStoreManifest(
|
|
269
|
+
manifestArg: TServezoneAppStoreManifest,
|
|
270
|
+
resolvedSourceArg: TAppStoreResolvedSource,
|
|
271
|
+
): Promise<IResolvedAppStoreApp> {
|
|
272
|
+
const configsByVersion = new Map<string, TAppStoreVersionConfig>();
|
|
273
|
+
const versions: string[] = [];
|
|
274
|
+
const sourceVersionToResolvedVersion = new Map<string, string>();
|
|
275
|
+
|
|
276
|
+
for (const versionArg of manifestArg.versions || []) {
|
|
277
|
+
const sourceVersion = versionArg.version;
|
|
278
|
+
const { version: _version, ...versionConfig } = versionArg;
|
|
279
|
+
const config: TAppStoreVersionConfig = {
|
|
280
|
+
...versionConfig,
|
|
281
|
+
source: versionConfig.source || manifestArg.source,
|
|
282
|
+
resolvedSource: resolvedSourceArg,
|
|
283
|
+
};
|
|
284
|
+
await this.resolveConfigSource(config, sourceVersion);
|
|
285
|
+
const resolvedVersion = config.appStoreVersion || sourceVersion;
|
|
286
|
+
config.appStoreVersion = resolvedVersion;
|
|
287
|
+
this.validateAppStoreVersionConfig(config, `${manifestArg.app.id}@${resolvedVersion}`);
|
|
288
|
+
configsByVersion.set(resolvedVersion, config);
|
|
289
|
+
configsByVersion.set(sourceVersion, config);
|
|
290
|
+
versions.push(resolvedVersion);
|
|
291
|
+
sourceVersionToResolvedVersion.set(sourceVersion, resolvedVersion);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (manifestArg.runtime) {
|
|
295
|
+
const sourceVersion = manifestArg.latestVersion || manifestArg.channel || 'latest';
|
|
296
|
+
const config: TAppStoreVersionConfig = {
|
|
297
|
+
...manifestArg.runtime,
|
|
298
|
+
source: manifestArg.runtime.source || manifestArg.source,
|
|
299
|
+
resolvedSource: resolvedSourceArg,
|
|
300
|
+
};
|
|
301
|
+
await this.resolveConfigSource(config, sourceVersion);
|
|
302
|
+
const resolvedVersion = config.appStoreVersion || sourceVersion;
|
|
303
|
+
config.appStoreVersion = resolvedVersion;
|
|
304
|
+
this.validateAppStoreVersionConfig(config, `${manifestArg.app.id}@${resolvedVersion}`);
|
|
305
|
+
configsByVersion.set(resolvedVersion, config);
|
|
306
|
+
configsByVersion.set(sourceVersion, config);
|
|
307
|
+
versions.push(resolvedVersion);
|
|
308
|
+
sourceVersionToResolvedVersion.set(sourceVersion, resolvedVersion);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (configsByVersion.size === 0) {
|
|
312
|
+
throw new Error('Appstore manifest must define at least one runtime config or version');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const selectedChannel = manifestArg.policy?.defaultChannel || manifestArg.channel || 'stable';
|
|
316
|
+
const channelVersion = manifestArg.channels?.[selectedChannel];
|
|
317
|
+
const declaredLatestVersion = manifestArg.latestVersion || channelVersion || versions[versions.length - 1];
|
|
318
|
+
const latestVersion = sourceVersionToResolvedVersion.get(declaredLatestVersion) || declaredLatestVersion;
|
|
319
|
+
const uniqueVersions = uniqueStrings(versions);
|
|
320
|
+
|
|
321
|
+
const appStoreApp: TAppStoreApp = {
|
|
322
|
+
id: manifestArg.app.id,
|
|
323
|
+
name: manifestArg.app.name,
|
|
324
|
+
description: manifestArg.app.description,
|
|
325
|
+
category: manifestArg.app.category,
|
|
326
|
+
iconName: manifestArg.app.iconName,
|
|
327
|
+
iconUrl: manifestArg.app.iconUrl,
|
|
328
|
+
latestVersion,
|
|
329
|
+
versions: uniqueVersions,
|
|
330
|
+
tags: manifestArg.app.tags,
|
|
331
|
+
channel: selectedChannel,
|
|
332
|
+
source: manifestArg.source,
|
|
333
|
+
upgradeStrategy: getUpgradeStrategyForConfig(configsByVersion.get(latestVersion)),
|
|
334
|
+
resolvedSource: resolvedSourceArg,
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const appMeta: interfaces.appstore.IAppStoreAppMeta = {
|
|
338
|
+
id: manifestArg.app.id,
|
|
339
|
+
name: manifestArg.app.name,
|
|
340
|
+
description: manifestArg.app.description,
|
|
341
|
+
category: manifestArg.app.category,
|
|
342
|
+
iconName: manifestArg.app.iconName,
|
|
343
|
+
latestVersion,
|
|
344
|
+
versions: uniqueVersions,
|
|
345
|
+
maintainer: manifestArg.app.maintainer,
|
|
346
|
+
links: manifestArg.app.links,
|
|
347
|
+
tags: manifestArg.app.tags,
|
|
348
|
+
source: manifestArg.source,
|
|
349
|
+
resolvedSource: resolvedSourceArg,
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
return { appStoreApp, appMeta, configsByVersion };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private async resolveConfigSource(configArg: TAppStoreVersionConfig, versionArg: string): Promise<void> {
|
|
356
|
+
if (configArg.source?.type === 'dockerImage') {
|
|
357
|
+
await this.applyDockerImageSourceToConfig(configArg.source, configArg, versionArg);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private async applyDockerImageSourceToConfig(
|
|
362
|
+
sourceArg: TAppStoreDockerImageSource,
|
|
363
|
+
configArg: TAppStoreVersionConfig,
|
|
364
|
+
versionArg: string,
|
|
365
|
+
): Promise<TAppStoreVersionConfig> {
|
|
366
|
+
configArg.image = sourceArg.image;
|
|
367
|
+
configArg.source = sourceArg;
|
|
368
|
+
|
|
369
|
+
const resolvedSource = await this.resolveDockerImageSource(sourceArg);
|
|
370
|
+
configArg.resolvedSource = resolvedSource;
|
|
371
|
+
configArg.resolvedImageDigest = resolvedSource.imageDigest;
|
|
372
|
+
configArg.upgradeStrategy = sourceArg.tracking === 'digest' ? 'dockerDigest' : configArg.upgradeStrategy;
|
|
373
|
+
configArg.appStoreVersion = createAppStoreVersionForDockerSource(
|
|
374
|
+
sourceArg,
|
|
375
|
+
versionArg,
|
|
376
|
+
resolvedSource.imageDigest,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
return configArg;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
private createAppMetaFromAppStoreApp(appArg: TAppStoreApp): interfaces.appstore.IAppStoreAppMeta {
|
|
383
|
+
return {
|
|
384
|
+
id: appArg.id,
|
|
385
|
+
name: appArg.name,
|
|
386
|
+
description: appArg.description,
|
|
387
|
+
category: appArg.category,
|
|
388
|
+
iconName: appArg.iconName,
|
|
389
|
+
latestVersion: appArg.latestVersion,
|
|
390
|
+
versions: appArg.versions || [appArg.latestVersion],
|
|
391
|
+
tags: appArg.tags,
|
|
392
|
+
source: appArg.source,
|
|
393
|
+
resolvedSource: appArg.resolvedSource,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private async getAppStoreApp(appIdArg: string): Promise<TAppStoreApp | undefined> {
|
|
398
|
+
const appStore = await this.getAppStoreIndex();
|
|
399
|
+
return appStore.apps.find((appArg) => appArg.id === appIdArg);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private async fetchJson(pathArg: string): Promise<unknown> {
|
|
403
|
+
const url = `${this.baseUrl}/${pathArg}`;
|
|
404
|
+
const response = await this.fetchRef(url);
|
|
405
|
+
if (!response.ok) {
|
|
406
|
+
throw new Error(`HTTP ${response.status} for ${url}`);
|
|
407
|
+
}
|
|
408
|
+
return response.json();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
private async fetchTextFromUrl(urlArg: string): Promise<string> {
|
|
412
|
+
const response = await this.fetchRef(urlArg);
|
|
413
|
+
if (!response.ok) {
|
|
414
|
+
throw new Error(`HTTP ${response.status} for ${urlArg}`);
|
|
415
|
+
}
|
|
416
|
+
return response.text();
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
private async fetchDockerManifestDigest(imageArg: IParsedDockerImageReference): Promise<string | null> {
|
|
420
|
+
const manifestUrl = `https://${imageArg.registry}/v2/${imageArg.repository}/manifests/${imageArg.tag}`;
|
|
421
|
+
const headers = new Headers({
|
|
422
|
+
Accept: [
|
|
423
|
+
'application/vnd.docker.distribution.manifest.v2+json',
|
|
424
|
+
'application/vnd.oci.image.manifest.v1+json',
|
|
425
|
+
'application/vnd.docker.distribution.manifest.list.v2+json',
|
|
426
|
+
'application/vnd.oci.image.index.v1+json',
|
|
427
|
+
].join(', '),
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
let response = await this.fetchRef(manifestUrl, { method: 'HEAD', headers });
|
|
431
|
+
if (response.status === 401) {
|
|
432
|
+
const authHeader = response.headers.get('www-authenticate');
|
|
433
|
+
const token = authHeader ? await this.fetchDockerRegistryToken(authHeader, imageArg.repository) : null;
|
|
434
|
+
if (token) {
|
|
435
|
+
headers.set('Authorization', `Bearer ${token}`);
|
|
436
|
+
response = await this.fetchRef(manifestUrl, { method: 'HEAD', headers });
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (!response.ok || !response.headers.get('docker-content-digest')) {
|
|
441
|
+
response = await this.fetchRef(manifestUrl, { method: 'GET', headers });
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (!response.ok) {
|
|
445
|
+
throw new Error(`HTTP ${response.status} while resolving ${imageArg.repository}:${imageArg.tag}`);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return response.headers.get('docker-content-digest');
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
private async fetchDockerRegistryToken(authHeaderArg: string, repositoryArg: string): Promise<string | null> {
|
|
452
|
+
const match = authHeaderArg.match(/^Bearer\s+(.+)$/i);
|
|
453
|
+
if (!match) return null;
|
|
454
|
+
|
|
455
|
+
const authParams = new Map<string, string>();
|
|
456
|
+
for (const partArg of match[1].match(/(?:[^,\"]+|\"[^\"]*\")+/g) || []) {
|
|
457
|
+
const [key, rawValue] = partArg.split('=');
|
|
458
|
+
if (!key || rawValue === undefined) continue;
|
|
459
|
+
authParams.set(key.trim(), rawValue.trim().replace(/^\"|\"$/g, ''));
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const realm = authParams.get('realm');
|
|
463
|
+
if (!realm) return null;
|
|
464
|
+
const tokenUrl = new URL(realm);
|
|
465
|
+
const service = authParams.get('service');
|
|
466
|
+
const scope = authParams.get('scope') || `repository:${repositoryArg}:pull`;
|
|
467
|
+
if (service) tokenUrl.searchParams.set('service', service);
|
|
468
|
+
tokenUrl.searchParams.set('scope', scope);
|
|
469
|
+
|
|
470
|
+
const response = await this.fetchRef(tokenUrl.toString());
|
|
471
|
+
if (!response.ok) return null;
|
|
472
|
+
const tokenResponse = await response.json() as { token?: string; access_token?: string };
|
|
473
|
+
return tokenResponse.token || tokenResponse.access_token || null;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
private validateVolume(volumeArg: interfaces.appstore.IAppStoreVolume, labelArg: string): void {
|
|
477
|
+
if (!volumeArg.mountPath || !volumeArg.mountPath.startsWith('/')) {
|
|
478
|
+
throw new Error(`Invalid ${labelArg}: mountPath must be an absolute path`);
|
|
479
|
+
}
|
|
480
|
+
if (volumeArg.mountPath.includes(':')) {
|
|
481
|
+
throw new Error(`Invalid ${labelArg}: mountPath must not contain ':'`);
|
|
482
|
+
}
|
|
483
|
+
if ((volumeArg.source || volumeArg.name)?.includes(':')) {
|
|
484
|
+
throw new Error(`Invalid ${labelArg}: source/name must not contain ':'`);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
private validatePublishedPorts(
|
|
489
|
+
publishedPortsArg: TAppStoreVersionConfig['publishedPorts'] = [],
|
|
490
|
+
labelArg: string,
|
|
491
|
+
): void {
|
|
492
|
+
const seenPublishedPorts = new Set<string>();
|
|
493
|
+
for (const portArg of publishedPortsArg) {
|
|
494
|
+
const protocol = portArg.protocol || 'tcp';
|
|
495
|
+
const targetStart = portArg.targetPort;
|
|
496
|
+
const targetEnd = portArg.targetPortEnd || targetStart;
|
|
497
|
+
const publishedStart = portArg.publishedPort || targetStart;
|
|
498
|
+
const publishedEnd = portArg.publishedPortEnd || (publishedStart + (targetEnd - targetStart));
|
|
499
|
+
const hostIp = portArg.hostIp || '0.0.0.0';
|
|
500
|
+
|
|
501
|
+
if (!['tcp', 'udp'].includes(protocol)) {
|
|
502
|
+
throw new Error(`Invalid ${labelArg}: published port protocol '${protocol}' is not supported`);
|
|
503
|
+
}
|
|
504
|
+
this.assertValidPort(targetStart, `${labelArg} targetPort`);
|
|
505
|
+
this.assertValidPort(targetEnd, `${labelArg} targetPortEnd`);
|
|
506
|
+
this.assertValidPort(publishedStart, `${labelArg} publishedPort`);
|
|
507
|
+
this.assertValidPort(publishedEnd, `${labelArg} publishedPortEnd`);
|
|
508
|
+
if (targetEnd < targetStart || publishedEnd < publishedStart) {
|
|
509
|
+
throw new Error(`Invalid ${labelArg}: published port ranges must be ascending`);
|
|
510
|
+
}
|
|
511
|
+
if ((targetEnd - targetStart) !== (publishedEnd - publishedStart)) {
|
|
512
|
+
throw new Error(`Invalid ${labelArg}: target and published port ranges must have the same size`);
|
|
513
|
+
}
|
|
514
|
+
if ((targetEnd - targetStart) > 1000) {
|
|
515
|
+
throw new Error(`Invalid ${labelArg}: published port ranges may not exceed 1001 ports`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
for (let offset = 0; offset <= targetEnd - targetStart; offset++) {
|
|
519
|
+
const publishedPort = publishedStart + offset;
|
|
520
|
+
const publishedKey = `${hostIp}/${protocol}/${publishedPort}`;
|
|
521
|
+
const wildcardKey = `0.0.0.0/${protocol}/${publishedPort}`;
|
|
522
|
+
const conflictsWithWildcard = hostIp === '0.0.0.0'
|
|
523
|
+
? Array.from(seenPublishedPorts).some((keyArg) => keyArg.endsWith(`/${protocol}/${publishedPort}`))
|
|
524
|
+
: seenPublishedPorts.has(wildcardKey);
|
|
525
|
+
if (seenPublishedPorts.has(publishedKey) || conflictsWithWildcard) {
|
|
526
|
+
throw new Error(`Invalid ${labelArg}: duplicate published port ${hostIp}:${publishedPort}/${protocol}`);
|
|
527
|
+
}
|
|
528
|
+
seenPublishedPorts.add(publishedKey);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
private assertValidPort(portArg: number, labelArg: string): void {
|
|
534
|
+
if (!Number.isInteger(portArg) || portArg < 1 || portArg > 65535) {
|
|
535
|
+
throw new Error(`Invalid ${labelArg}: ${portArg}. Expected an integer port between 1 and 65535.`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export function parseAppStoreIndex(inputArg: unknown): TAppStoreIndex {
|
|
541
|
+
const appStore = inputArg as TAppStoreIndex;
|
|
542
|
+
if (!appStore || typeof appStore !== 'object' || !Array.isArray(appStore.apps)) {
|
|
543
|
+
throw new Error('Invalid appstore format');
|
|
544
|
+
}
|
|
545
|
+
return appStore;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export function parseServezoneAppStoreManifest(inputArg: unknown): TServezoneAppStoreManifest {
|
|
549
|
+
const manifest = inputArg as TServezoneAppStoreManifest;
|
|
550
|
+
if (!manifest || typeof manifest !== 'object') {
|
|
551
|
+
throw new Error('Appstore manifest must be an object');
|
|
552
|
+
}
|
|
553
|
+
if (manifest.schemaVersion !== 1) {
|
|
554
|
+
throw new Error(`Unsupported appstore manifest schemaVersion '${manifest.schemaVersion}'`);
|
|
555
|
+
}
|
|
556
|
+
if (!manifest.app?.id || !manifest.app?.name) {
|
|
557
|
+
throw new Error('Appstore manifest app.id and app.name are required');
|
|
558
|
+
}
|
|
559
|
+
return manifest;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export function parseDockerImageReference(imageArg: string): IParsedDockerImageReference {
|
|
563
|
+
const [imageWithoutDigest, digest] = imageArg.split('@');
|
|
564
|
+
const imageParts = imageWithoutDigest.split('/');
|
|
565
|
+
const firstPart = imageParts[0];
|
|
566
|
+
const hasExplicitRegistry = imageParts.length > 1
|
|
567
|
+
&& (firstPart.includes('.') || firstPart.includes(':') || firstPart === 'localhost');
|
|
568
|
+
const registry = hasExplicitRegistry ? firstPart : 'registry-1.docker.io';
|
|
569
|
+
const repositoryParts = hasExplicitRegistry ? imageParts.slice(1) : imageParts;
|
|
570
|
+
let repositoryWithTag = repositoryParts.join('/');
|
|
571
|
+
if (!repositoryWithTag) {
|
|
572
|
+
throw new Error(`Invalid Docker image reference '${imageArg}'`);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!hasExplicitRegistry && !repositoryWithTag.includes('/')) {
|
|
576
|
+
repositoryWithTag = `library/${repositoryWithTag}`;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const lastSlashIndex = repositoryWithTag.lastIndexOf('/');
|
|
580
|
+
const lastColonIndex = repositoryWithTag.lastIndexOf(':');
|
|
581
|
+
const hasTag = lastColonIndex > lastSlashIndex;
|
|
582
|
+
const repository = hasTag ? repositoryWithTag.slice(0, lastColonIndex) : repositoryWithTag;
|
|
583
|
+
const tag = hasTag ? repositoryWithTag.slice(lastColonIndex + 1) : 'latest';
|
|
584
|
+
|
|
585
|
+
return { registry, repository, tag, digest };
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export function createAppStoreVersionForDockerSource(
|
|
589
|
+
sourceArg: TAppStoreDockerImageSource,
|
|
590
|
+
fallbackVersionArg: string,
|
|
591
|
+
digestArg?: string,
|
|
592
|
+
): string {
|
|
593
|
+
if (sourceArg.tracking !== 'digest' || !digestArg) {
|
|
594
|
+
return fallbackVersionArg;
|
|
595
|
+
}
|
|
596
|
+
const parsedImage = parseDockerImageReference(sourceArg.image);
|
|
597
|
+
return `${parsedImage.tag}@${digestArg}`;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
async function createSha256Hex(inputArg: string): Promise<string> {
|
|
601
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(inputArg));
|
|
602
|
+
return Array.from(new Uint8Array(digest))
|
|
603
|
+
.map((byteArg) => byteArg.toString(16).padStart(2, '0'))
|
|
604
|
+
.join('');
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function uniqueStrings(valuesArg: string[]): string[] {
|
|
608
|
+
return Array.from(new Set(valuesArg.filter(Boolean)));
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function withoutUndefined<T extends object>(objectArg: T): Partial<T> {
|
|
612
|
+
return Object.fromEntries(
|
|
613
|
+
Object.entries(objectArg).filter(([, valueArg]) => valueArg !== undefined),
|
|
614
|
+
) as Partial<T>;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function getUpgradeStrategyForConfig(
|
|
618
|
+
configArg?: TAppStoreVersionConfig,
|
|
619
|
+
): interfaces.appstore.IAppStoreApp['upgradeStrategy'] {
|
|
620
|
+
if (configArg?.upgradeStrategy) return configArg.upgradeStrategy;
|
|
621
|
+
if (configArg?.source?.type === 'dockerImage' && configArg.source.tracking === 'digest') return 'dockerDigest';
|
|
622
|
+
return undefined;
|
|
623
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type * as interfaces from '@serve.zone/interfaces';
|
|
2
|
+
|
|
3
|
+
export type TAppStoreFetch = typeof fetch;
|
|
4
|
+
export type TAppStoreIndex = interfaces.appstore.IAppStoreIndex;
|
|
5
|
+
export type TAppStoreApp = interfaces.appstore.IAppStoreApp;
|
|
6
|
+
export type TAppStoreAppMeta = interfaces.appstore.IAppStoreAppMeta;
|
|
7
|
+
export type TAppStoreVersionConfig = interfaces.appstore.IAppStoreVersionConfig;
|
|
8
|
+
export type TAppStoreDockerImageSource = interfaces.appstore.IAppStoreDockerImageSource;
|
|
9
|
+
export type TAppStoreRepoManifestSource = interfaces.appstore.IAppStoreRepoManifestSource;
|
|
10
|
+
export type TAppStoreResolvedSource = interfaces.appstore.IAppStoreResolvedSource;
|
|
11
|
+
export type TServezoneAppStoreManifest = interfaces.appstore.IServezoneAppStoreManifest;
|
|
12
|
+
|
|
13
|
+
export interface IAppStoreResolverOptions {
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
fetch?: TAppStoreFetch;
|
|
16
|
+
resolveDockerDigests?: boolean;
|
|
17
|
+
now?: () => Date;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IResolvedAppStoreApp {
|
|
21
|
+
appStoreApp: TAppStoreApp;
|
|
22
|
+
appMeta: TAppStoreAppMeta;
|
|
23
|
+
configsByVersion: Map<string, TAppStoreVersionConfig>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface IParsedDockerImageReference {
|
|
27
|
+
registry: string;
|
|
28
|
+
repository: string;
|
|
29
|
+
tag: string;
|
|
30
|
+
digest?: string;
|
|
31
|
+
}
|