-
Notifications
You must be signed in to change notification settings - Fork 2
/
maven-client.js
243 lines (223 loc) · 9.47 KB
/
maven-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const fetch = require("node-fetch");
const fs = require("fs")
const unzip = require("unzip-stream");
const tar = require("tar");
const {pipeline} = require('stream/promises');
const {
MavenRepository,
MavenArtifact
} = require('./maven-types')
const semver = require('semver')
const {MavenSettingsFile, MavenMetaDataFile} = require("./maven-files");
const Process = require("process");
const META_DATA_FILE_NAME = 'maven-metadata.xml';
class MavenClient {
logger;
constructor(logger) {
this.logger = logger
}
/**
*
* @param {MavenRepository} repository
* @param {String }targetFolder
* @param {MavenArtifact} mavenArtifact
* @returns {Promise<void>}
*/
async downloadAndExtract(repository, targetFolder, mavenArtifact) {
const downloadUrl = await MavenClient.#buildUrl(repository, mavenArtifact, this.logger)
this.logger.debug('Fetching ' + downloadUrl)
const response = await fetch(downloadUrl, repository.fetchOptions);
if (!response.ok) {
throw new MavenClientError("Failure downloading " + downloadUrl + ' (' + response.status + ')');
}
switch (mavenArtifact.extension) {
case 'zip':
case 'jar':
await pipeline(
response.body,
unzip.Extract({path: targetFolder})
);
break;
case 'tgz':
await pipeline(
response.body,
tar.extract({cwd: targetFolder})
);
break;
default:
throw new MavenClientError('Unsupported extension: ' + mavenArtifact.extension);
}
}
/**
*
* @param {MavenRepository }repository
* @param {MavenArtifact} mavenArtifact
* @param logger
* @returns {Promise<string>}
*/
static async #buildUrl(repository, mavenArtifact, logger) {
if (mavenArtifact.version.endsWith("-SNAPSHOT")) {
return await MavenClient.#buildSnapshotUrl(repository, mavenArtifact, logger);
} else {
return MavenClient.#buildReleaseUrl(repository.baseUrl, mavenArtifact);
}
}
/**
*
* @param {MavenRepository[]} repositories
* @param {string} groupId
* @param {string} artifactId
* @param {string} version optional, try to find this if metadata discovery fails for a reason
* @param {string} classifier optional, try to find this if metadata discovery fails for a reason
* @param {string} extension optional, try to find this if metadata discovery fails for a reason
*
* @returns {Promise<{version, repository}[]>}
*/
async retrieveAvailableVersions(repositories, {groupId, artifactId, fallback: {version, classifier, extension}}) {
const versionToRepoMap = new Map();
await Promise.all(repositories.map(async (repository) => {
const metaDataUrl = MavenClient.#buildMetaDataUrlForArtifact(repository.baseUrl, {groupId, artifactId});
this.logger.debug('Downloading metadata to extract version from ' + metaDataUrl);
try {
const metaDataFile = new MavenMetaDataFile(await MavenClient.#downloadXml(metaDataUrl, repository.fetchOptions), this.logger);
metaDataFile.getVersions().forEach(discoveredVersion => {
if (!semver.valid(discoveredVersion)) {
this.logger.info('Found invalid SemVer version ' + discoveredVersion + ' for ' + groupId + ':' + artifactId + ', skipping.');
} else if (!versionToRepoMap.has(discoveredVersion)) {
versionToRepoMap.set(discoveredVersion, repository);
}
});
} catch (error) {
this.logger.warn('Error when trying to fetch meta data for ' + groupId + ':' + artifactId + ' from ' + repository + ': ' + error.message);
const fixedVersionUrl = await MavenClient.#buildUrl(
repository,
new MavenArtifact({groupId, artifactId, version, classifier, extension}))
this.logger.debug('Probing "' + fixedVersionUrl + '"...')
if (await MavenClient.#existsRemotely(fixedVersionUrl, repository.fetchOptions)) {
this.logger.debug('Using fallback version for ' + groupId + ':' + artifactId + ':' + version);
versionToRepoMap.set(version, repository)
} else {
this.logger.warn('Skipping ' + groupId + ':' + artifactId);
}
}
}));
if (!versionToRepoMap.size) {
throw new MavenClientError('Unable to find any version for ' + groupId + ':' + artifactId);
}
const versionList = semver.sort([...versionToRepoMap.keys()]);
versionList.reverse();
this.logger.info('Found ' + versionList.length + ' versions for ' + groupId + ':' + artifactId);
return versionList.map(version => {
return {
version: version,
repository: versionToRepoMap.get(version)
};
});
}
/**
*
* @param {MavenRepository }repository
* @param {MavenArtifact} mavenArtifact
* @param logger
* @returns {Promise<string>}
*/
static async #buildSnapshotUrl(repository, mavenArtifact, logger) {
const snaphotMetadataUrl =
[repository.baseUrl]
.concat(...MavenClient.#groupIdAsPathSegments(mavenArtifact.groupId))
.concat(mavenArtifact.artifactId)
.concat(mavenArtifact.version)
.concat(META_DATA_FILE_NAME)
.join('/');
const snapshotMetaData = new MavenMetaDataFile(await MavenClient.#downloadXml(snaphotMetadataUrl, repository.fetchOptions), logger);
const latestVersion = snapshotMetaData.getLatestSnapshotVersion(mavenArtifact.classifier, mavenArtifact.extension);
if (!latestVersion) {
throw new MavenClientError('Cannot find latest snapshot version info for ' + mavenArtifact.extension + ' in maven metadata: ' + snapshotMetaData)
}
return [repository.baseUrl]
.concat(...MavenClient.#groupIdAsPathSegments(mavenArtifact.groupId))
.concat(mavenArtifact.artifactId)
.concat(mavenArtifact.version)
.concat(
mavenArtifact.artifactId
+ '-'
+ latestVersion
+ (mavenArtifact.classifier ? '-' + mavenArtifact.classifier : '')
+ '.'
+ mavenArtifact.extension)
.join('/')
}
static #buildMetaDataUrlForArtifact(baseUrl, mavenArtifact) {
return [baseUrl]
.concat(...MavenClient.#groupIdAsPathSegments(mavenArtifact.groupId))
.concat(mavenArtifact.artifactId)
.concat(META_DATA_FILE_NAME)
.join('/');
}
/**
*
* @param {string} baseUrl
* @param {MavenArtifact} mavenArtifact
* @returns {string}
*/
static #buildReleaseUrl(baseUrl, mavenArtifact) {
return [baseUrl]
.concat(...MavenClient.#groupIdAsPathSegments(mavenArtifact.groupId))
.concat(mavenArtifact.artifactId)
.concat(mavenArtifact.version)
.concat(
mavenArtifact.artifactId
+ '-'
+ mavenArtifact.version
+ (mavenArtifact.classifier ? '-' + mavenArtifact.classifier : '')
+ '.'
+ mavenArtifact.extension)
.join('/');
}
static async #downloadXml(url, fetchOptions) {
let metaDataResponse = await fetch(url, fetchOptions)
let blob = await metaDataResponse.blob()
let text = await blob.text();
if (!metaDataResponse.ok) {
throw new MavenClientError("Failure while downloading xml file: " + metaDataResponse.status + " : " + text);
}
return text;
}
static async #existsRemotely(url, fetchOptions) {
const options = Object.assign({}, fetchOptions);
options.method = 'HEAD';
let probeResponse = await fetch(url, options);
return probeResponse.ok
}
static #groupIdAsPathSegments(groupId) {
return groupId.split('\.')
}
async extractRepositoriesFromSettingsFile(settingsFilePath) {
this.logger.debug('Loading repositories from ' + settingsFilePath + '...');
if (!fs.existsSync(settingsFilePath)) {
this.logger.warn('Skip loading repos from settings, ' + settingsFilePath + ' does not exist.');
return [];
}
const mavenSettingsXml = await fs.promises.readFile(settingsFilePath, "utf-8")
const settingsFile = new MavenSettingsFile(mavenSettingsXml, this.logger);
return settingsFile.getActiveProfileRepositories();
}
findMavenSettingsFile() {
const USER_SETTINGS = Process.env.HOME + '/.m2/settings.xml';
if (fs.existsSync(USER_SETTINGS)) {
return USER_SETTINGS;
}
const INSTALL_SETTINGS = Process.env.M2_HOME + '/conf/settings.xml';
if (fs.existsSync(INSTALL_SETTINGS)) {
return INSTALL_SETTINGS;
}
throw new MavenClientError("Unable to find maven settings. Looked for: " + [USER_SETTINGS, INSTALL_SETTINGS]);
}
}
class MavenClientError extends Error {
constructor(message) {
super(message);
this.name = 'MavenContentSourceError';
}
}
module.exports = MavenClient