utils.js
8.4 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { join, dirname, readJson } = require("../util/fs");
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
// Extreme shorthand only for github. eg: foo/bar
const RE_URL_GITHUB_EXTREME_SHORT = /^[^/@:.\s][^/@:\s]*\/[^@:\s]*[^/@:\s]#\S+/;
// Short url with specific protocol. eg: github:foo/bar
const RE_GIT_URL_SHORT = /^(github|gitlab|bitbucket|gist):\/?[^/.]+\/?/i;
// Currently supported protocols
const RE_PROTOCOL =
/^((git\+)?(ssh|https?|file)|git|github|gitlab|bitbucket|gist):$/i;
// Has custom protocol
const RE_CUSTOM_PROTOCOL = /^((git\+)?(ssh|https?|file)|git):\/\//i;
// Valid hash format for npm / yarn ...
const RE_URL_HASH_VERSION = /#(?:semver:)?(.+)/;
// Simple hostname validate
const RE_HOSTNAME = /^(?:[^/.]+(\.[^/]+)+|localhost)$/;
// For hostname with colon. eg: ssh://user@github.com:foo/bar
const RE_HOSTNAME_WITH_COLON =
/([^/@#:.]+(?:\.[^/@#:.]+)+|localhost):([^#/0-9]+)/;
// Reg for url without protocol
const RE_NO_PROTOCOL = /^([^/@#:.]+(?:\.[^/@#:.]+)+)/;
// RegExp for version string
const VERSION_PATTERN_REGEXP = /^([\d^=v<>~]|[*xX]$)/;
// Specific protocol for short url without normal hostname
const PROTOCOLS_FOR_SHORT = [
"github:",
"gitlab:",
"bitbucket:",
"gist:",
"file:"
];
// Default protocol for git url
const DEF_GIT_PROTOCOL = "git+ssh://";
// thanks to https://github.com/npm/hosted-git-info/blob/latest/git-host-info.js
const extractCommithashByDomain = {
"github.com": (pathname, hash) => {
let [, user, project, type, commithash] = pathname.split("/", 5);
if (type && type !== "tree") {
return;
}
if (!type) {
commithash = hash;
} else {
commithash = "#" + commithash;
}
if (project && project.endsWith(".git")) {
project = project.slice(0, -4);
}
if (!user || !project) {
return;
}
return commithash;
},
"gitlab.com": (pathname, hash) => {
const path = pathname.slice(1);
if (path.includes("/-/") || path.includes("/archive.tar.gz")) {
return;
}
const segments = path.split("/");
let project = segments.pop();
if (project.endsWith(".git")) {
project = project.slice(0, -4);
}
const user = segments.join("/");
if (!user || !project) {
return;
}
return hash;
},
"bitbucket.org": (pathname, hash) => {
let [, user, project, aux] = pathname.split("/", 4);
if (["get"].includes(aux)) {
return;
}
if (project && project.endsWith(".git")) {
project = project.slice(0, -4);
}
if (!user || !project) {
return;
}
return hash;
},
"gist.github.com": (pathname, hash) => {
let [, user, project, aux] = pathname.split("/", 4);
if (aux === "raw") {
return;
}
if (!project) {
if (!user) {
return;
}
project = user;
user = null;
}
if (project.endsWith(".git")) {
project = project.slice(0, -4);
}
return hash;
}
};
/**
* extract commit hash from parsed url
*
* @inner
* @param {Object} urlParsed parsed url
* @returns {string} commithash
*/
function getCommithash(urlParsed) {
let { hostname, pathname, hash } = urlParsed;
hostname = hostname.replace(/^www\./, "");
try {
hash = decodeURIComponent(hash);
// eslint-disable-next-line no-empty
} catch (e) {}
if (extractCommithashByDomain[hostname]) {
return extractCommithashByDomain[hostname](pathname, hash) || "";
}
return hash;
}
/**
* make url right for URL parse
*
* @inner
* @param {string} gitUrl git url
* @returns {string} fixed url
*/
function correctUrl(gitUrl) {
// like:
// proto://hostname.com:user/repo -> proto://hostname.com/user/repo
return gitUrl.replace(RE_HOSTNAME_WITH_COLON, "$1/$2");
}
/**
* make url protocol right for URL parse
*
* @inner
* @param {string} gitUrl git url
* @returns {string} fixed url
*/
function correctProtocol(gitUrl) {
// eg: github:foo/bar#v1.0. Should not add double slash, in case of error parsed `pathname`
if (RE_GIT_URL_SHORT.test(gitUrl)) {
return gitUrl;
}
// eg: user@github.com:foo/bar
if (!RE_CUSTOM_PROTOCOL.test(gitUrl)) {
return `${DEF_GIT_PROTOCOL}${gitUrl}`;
}
return gitUrl;
}
/**
* extract git dep version from hash
*
* @inner
* @param {string} hash hash
* @returns {string} git dep version
*/
function getVersionFromHash(hash) {
const matched = hash.match(RE_URL_HASH_VERSION);
return (matched && matched[1]) || "";
}
/**
* if string can be decoded
*
* @inner
* @param {string} str str to be checked
* @returns {boolean} if can be decoded
*/
function canBeDecoded(str) {
try {
decodeURIComponent(str);
} catch (e) {
return false;
}
return true;
}
/**
* get right dep version from git url
*
* @inner
* @param {string} gitUrl git url
* @returns {string} dep version
*/
function getGitUrlVersion(gitUrl) {
let oriGitUrl = gitUrl;
// github extreme shorthand
if (RE_URL_GITHUB_EXTREME_SHORT.test(gitUrl)) {
gitUrl = "github:" + gitUrl;
} else {
gitUrl = correctProtocol(gitUrl);
}
gitUrl = correctUrl(gitUrl);
let parsed;
try {
parsed = new URL(gitUrl);
// eslint-disable-next-line no-empty
} catch (e) {}
if (!parsed) {
return "";
}
const { protocol, hostname, pathname, username, password } = parsed;
if (!RE_PROTOCOL.test(protocol)) {
return "";
}
// pathname shouldn't be empty or URL malformed
if (!pathname || !canBeDecoded(pathname)) {
return "";
}
// without protocol, there should have auth info
if (RE_NO_PROTOCOL.test(oriGitUrl) && !username && !password) {
return "";
}
if (!PROTOCOLS_FOR_SHORT.includes(protocol.toLowerCase())) {
if (!RE_HOSTNAME.test(hostname)) {
return "";
}
const commithash = getCommithash(parsed);
return getVersionFromHash(commithash) || commithash;
}
// for protocol short
return getVersionFromHash(gitUrl);
}
/**
* @param {string} str maybe required version
* @returns {boolean} true, if it looks like a version
*/
function isRequiredVersion(str) {
return VERSION_PATTERN_REGEXP.test(str);
}
exports.isRequiredVersion = isRequiredVersion;
/**
* @see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#urls-as-dependencies
* @param {string} versionDesc version to be normalized
* @returns {string} normalized version
*/
function normalizeVersion(versionDesc) {
versionDesc = (versionDesc && versionDesc.trim()) || "";
if (isRequiredVersion(versionDesc)) {
return versionDesc;
}
// add handle for URL Dependencies
return getGitUrlVersion(versionDesc.toLowerCase());
}
exports.normalizeVersion = normalizeVersion;
/**
*
* @param {InputFileSystem} fs file system
* @param {string} directory directory to start looking into
* @param {string[]} descriptionFiles possible description filenames
* @param {function((Error | null)=, {data: object, path: string}=): void} callback callback
*/
const getDescriptionFile = (fs, directory, descriptionFiles, callback) => {
let i = 0;
const tryLoadCurrent = () => {
if (i >= descriptionFiles.length) {
const parentDirectory = dirname(fs, directory);
if (!parentDirectory || parentDirectory === directory) return callback();
return getDescriptionFile(
fs,
parentDirectory,
descriptionFiles,
callback
);
}
const filePath = join(fs, directory, descriptionFiles[i]);
readJson(fs, filePath, (err, data) => {
if (err) {
if ("code" in err && err.code === "ENOENT") {
i++;
return tryLoadCurrent();
}
return callback(err);
}
if (!data || typeof data !== "object" || Array.isArray(data)) {
return callback(
new Error(`Description file ${filePath} is not an object`)
);
}
callback(null, { data, path: filePath });
});
};
tryLoadCurrent();
};
exports.getDescriptionFile = getDescriptionFile;
exports.getRequiredVersionFromDescriptionFile = (data, packageName) => {
if (
data.optionalDependencies &&
typeof data.optionalDependencies === "object" &&
packageName in data.optionalDependencies
) {
return normalizeVersion(data.optionalDependencies[packageName]);
}
if (
data.dependencies &&
typeof data.dependencies === "object" &&
packageName in data.dependencies
) {
return normalizeVersion(data.dependencies[packageName]);
}
if (
data.peerDependencies &&
typeof data.peerDependencies === "object" &&
packageName in data.peerDependencies
) {
return normalizeVersion(data.peerDependencies[packageName]);
}
if (
data.devDependencies &&
typeof data.devDependencies === "object" &&
packageName in data.devDependencies
) {
return normalizeVersion(data.devDependencies[packageName]);
}
};