1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getIdentVendorPath = exports.prettyWorkspace = exports.sortDescriptors = exports.prettyLocatorNoColors = exports.prettyLocator = exports.prettyReference = exports.prettyDescriptor = exports.prettyRange = exports.prettyIdent = exports.slugifyLocator = exports.slugifyIdent = exports.stringifyLocator = exports.stringifyDescriptor = exports.stringifyIdent = exports.requirableIdent = exports.convertToManifestRange = exports.makeRange = exports.parseFileStyleRange = exports.parseRange = exports.tryParseLocator = exports.parseLocator = exports.tryParseDescriptor = exports.parseDescriptor = exports.tryParseIdent = exports.parseIdent = exports.areVirtualPackagesEquivalent = exports.areLocatorsEqual = exports.areDescriptorsEqual = exports.areIdentsEqual = exports.bindLocator = exports.bindDescriptor = exports.devirtualizeLocator = exports.devirtualizeDescriptor = exports.isVirtualLocator = exports.isVirtualDescriptor = exports.virtualizePackage = exports.virtualizeDescriptor = exports.copyPackage = exports.renamePackage = exports.convertPackageToLocator = exports.convertLocatorToDescriptor = exports.convertDescriptorToLocator = exports.convertToIdent = exports.makeLocator = exports.makeDescriptor = exports.makeIdent = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const fslib_1 = require("@yarnpkg/fslib");
|
6 | const querystring_1 = tslib_1.__importDefault(require("querystring"));
|
7 | const semver_1 = tslib_1.__importDefault(require("semver"));
|
8 | const Configuration_1 = require("./Configuration");
|
9 | const hashUtils = tslib_1.__importStar(require("./hashUtils"));
|
10 | const miscUtils = tslib_1.__importStar(require("./miscUtils"));
|
11 | const VIRTUAL_PROTOCOL = `virtual:`;
|
12 | const VIRTUAL_ABBREVIATE = 5;
|
13 | function makeIdent(scope, name) {
|
14 | if (scope === null || scope === void 0 ? void 0 : scope.startsWith(`@`))
|
15 | throw new Error(`Invalid scope: don't prefix it with '@'`);
|
16 | return { identHash: hashUtils.makeHash(scope, name), scope, name };
|
17 | }
|
18 | exports.makeIdent = makeIdent;
|
19 | function makeDescriptor(ident, range) {
|
20 | return { identHash: ident.identHash, scope: ident.scope, name: ident.name, descriptorHash: hashUtils.makeHash(ident.identHash, range), range };
|
21 | }
|
22 | exports.makeDescriptor = makeDescriptor;
|
23 | function makeLocator(ident, reference) {
|
24 | return { identHash: ident.identHash, scope: ident.scope, name: ident.name, locatorHash: hashUtils.makeHash(ident.identHash, reference), reference };
|
25 | }
|
26 | exports.makeLocator = makeLocator;
|
27 | function convertToIdent(source) {
|
28 | return { identHash: source.identHash, scope: source.scope, name: source.name };
|
29 | }
|
30 | exports.convertToIdent = convertToIdent;
|
31 | function convertDescriptorToLocator(descriptor) {
|
32 | return { identHash: descriptor.identHash, scope: descriptor.scope, name: descriptor.name, locatorHash: descriptor.descriptorHash, reference: descriptor.range };
|
33 | }
|
34 | exports.convertDescriptorToLocator = convertDescriptorToLocator;
|
35 | function convertLocatorToDescriptor(locator) {
|
36 | return { identHash: locator.identHash, scope: locator.scope, name: locator.name, descriptorHash: locator.locatorHash, range: locator.reference };
|
37 | }
|
38 | exports.convertLocatorToDescriptor = convertLocatorToDescriptor;
|
39 | function convertPackageToLocator(pkg) {
|
40 | return { identHash: pkg.identHash, scope: pkg.scope, name: pkg.name, locatorHash: pkg.locatorHash, reference: pkg.reference };
|
41 | }
|
42 | exports.convertPackageToLocator = convertPackageToLocator;
|
43 | function renamePackage(pkg, locator) {
|
44 | return {
|
45 | identHash: locator.identHash,
|
46 | scope: locator.scope,
|
47 | name: locator.name,
|
48 | locatorHash: locator.locatorHash,
|
49 | reference: locator.reference,
|
50 | version: pkg.version,
|
51 | languageName: pkg.languageName,
|
52 | linkType: pkg.linkType,
|
53 | dependencies: new Map(pkg.dependencies),
|
54 | peerDependencies: new Map(pkg.peerDependencies),
|
55 | dependenciesMeta: new Map(pkg.dependenciesMeta),
|
56 | peerDependenciesMeta: new Map(pkg.peerDependenciesMeta),
|
57 | bin: new Map(pkg.bin),
|
58 | };
|
59 | }
|
60 | exports.renamePackage = renamePackage;
|
61 | function copyPackage(pkg) {
|
62 | return renamePackage(pkg, pkg);
|
63 | }
|
64 | exports.copyPackage = copyPackage;
|
65 | function virtualizeDescriptor(descriptor, entropy) {
|
66 | if (entropy.includes(`#`))
|
67 | throw new Error(`Invalid entropy`);
|
68 | return makeDescriptor(descriptor, `virtual:${entropy}#${descriptor.range}`);
|
69 | }
|
70 | exports.virtualizeDescriptor = virtualizeDescriptor;
|
71 | function virtualizePackage(pkg, entropy) {
|
72 | if (entropy.includes(`#`))
|
73 | throw new Error(`Invalid entropy`);
|
74 | return renamePackage(pkg, makeLocator(pkg, `virtual:${entropy}#${pkg.reference}`));
|
75 | }
|
76 | exports.virtualizePackage = virtualizePackage;
|
77 | function isVirtualDescriptor(descriptor) {
|
78 | return descriptor.range.startsWith(VIRTUAL_PROTOCOL);
|
79 | }
|
80 | exports.isVirtualDescriptor = isVirtualDescriptor;
|
81 | function isVirtualLocator(locator) {
|
82 | return locator.reference.startsWith(VIRTUAL_PROTOCOL);
|
83 | }
|
84 | exports.isVirtualLocator = isVirtualLocator;
|
85 | function devirtualizeDescriptor(descriptor) {
|
86 | if (!isVirtualDescriptor(descriptor))
|
87 | throw new Error(`Not a virtual descriptor`);
|
88 | return makeDescriptor(descriptor, descriptor.range.replace(/^[^#]*#/, ``));
|
89 | }
|
90 | exports.devirtualizeDescriptor = devirtualizeDescriptor;
|
91 | function devirtualizeLocator(locator) {
|
92 | if (!isVirtualLocator(locator))
|
93 | throw new Error(`Not a virtual descriptor`);
|
94 | return makeLocator(locator, locator.reference.replace(/^[^#]*#/, ``));
|
95 | }
|
96 | exports.devirtualizeLocator = devirtualizeLocator;
|
97 | function bindDescriptor(descriptor, params) {
|
98 | if (descriptor.range.includes(`::`))
|
99 | return descriptor;
|
100 | return makeDescriptor(descriptor, `${descriptor.range}::${querystring_1.default.stringify(params)}`);
|
101 | }
|
102 | exports.bindDescriptor = bindDescriptor;
|
103 | function bindLocator(locator, params) {
|
104 | if (locator.reference.includes(`::`))
|
105 | return locator;
|
106 | return makeLocator(locator, `${locator.reference}::${querystring_1.default.stringify(params)}`);
|
107 | }
|
108 | exports.bindLocator = bindLocator;
|
109 | function areIdentsEqual(a, b) {
|
110 | return a.identHash === b.identHash;
|
111 | }
|
112 | exports.areIdentsEqual = areIdentsEqual;
|
113 | function areDescriptorsEqual(a, b) {
|
114 | return a.descriptorHash === b.descriptorHash;
|
115 | }
|
116 | exports.areDescriptorsEqual = areDescriptorsEqual;
|
117 | function areLocatorsEqual(a, b) {
|
118 | return a.locatorHash === b.locatorHash;
|
119 | }
|
120 | exports.areLocatorsEqual = areLocatorsEqual;
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function areVirtualPackagesEquivalent(a, b) {
|
127 | if (!isVirtualLocator(a))
|
128 | throw new Error(`Invalid package type`);
|
129 | if (!isVirtualLocator(b))
|
130 | throw new Error(`Invalid package type`);
|
131 | if (!areIdentsEqual(a, b))
|
132 | return false;
|
133 | if (a.dependencies.size !== b.dependencies.size)
|
134 | return false;
|
135 | for (const dependencyDescriptorA of a.dependencies.values()) {
|
136 | const dependencyDescriptorB = b.dependencies.get(dependencyDescriptorA.identHash);
|
137 | if (!dependencyDescriptorB)
|
138 | return false;
|
139 | if (!areDescriptorsEqual(dependencyDescriptorA, dependencyDescriptorB)) {
|
140 | return false;
|
141 | }
|
142 | }
|
143 | return true;
|
144 | }
|
145 | exports.areVirtualPackagesEquivalent = areVirtualPackagesEquivalent;
|
146 | function parseIdent(string) {
|
147 | const ident = tryParseIdent(string);
|
148 | if (!ident)
|
149 | throw new Error(`Invalid ident (${string})`);
|
150 | return ident;
|
151 | }
|
152 | exports.parseIdent = parseIdent;
|
153 | function tryParseIdent(string) {
|
154 | const match = string.match(/^(?:@([^/]+?)\/)?([^/]+)$/);
|
155 | if (!match)
|
156 | return null;
|
157 | const [, scope, name] = match;
|
158 | const realScope = typeof scope !== `undefined`
|
159 | ? scope
|
160 | : null;
|
161 | return makeIdent(realScope, name);
|
162 | }
|
163 | exports.tryParseIdent = tryParseIdent;
|
164 | function parseDescriptor(string, strict = false) {
|
165 | const descriptor = tryParseDescriptor(string, strict);
|
166 | if (!descriptor)
|
167 | throw new Error(`Invalid descriptor (${string})`);
|
168 | return descriptor;
|
169 | }
|
170 | exports.parseDescriptor = parseDescriptor;
|
171 | function tryParseDescriptor(string, strict = false) {
|
172 | const match = strict
|
173 | ? string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))$/)
|
174 | : string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))?$/);
|
175 | if (!match)
|
176 | return null;
|
177 | const [, scope, name, range] = match;
|
178 | if (range === `unknown`)
|
179 | throw new Error(`Invalid range (${string})`);
|
180 | const realScope = typeof scope !== `undefined`
|
181 | ? scope
|
182 | : null;
|
183 | const realRange = typeof range !== `undefined`
|
184 | ? range
|
185 | : `unknown`;
|
186 | return makeDescriptor(makeIdent(realScope, name), realRange);
|
187 | }
|
188 | exports.tryParseDescriptor = tryParseDescriptor;
|
189 | function parseLocator(string, strict = false) {
|
190 | const locator = tryParseLocator(string, strict);
|
191 | if (!locator)
|
192 | throw new Error(`Invalid locator (${string})`);
|
193 | return locator;
|
194 | }
|
195 | exports.parseLocator = parseLocator;
|
196 | function tryParseLocator(string, strict = false) {
|
197 | const match = strict
|
198 | ? string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))$/)
|
199 | : string.match(/^(?:@([^/]+?)\/)?([^/]+?)(?:@(.+))?$/);
|
200 | if (!match)
|
201 | return null;
|
202 | const [, scope, name, reference] = match;
|
203 | if (reference === `unknown`)
|
204 | throw new Error(`Invalid reference (${string})`);
|
205 | const realScope = typeof scope !== `undefined`
|
206 | ? scope
|
207 | : null;
|
208 | const realReference = typeof reference !== `undefined`
|
209 | ? reference
|
210 | : `unknown`;
|
211 | return makeLocator(makeIdent(realScope, name), realReference);
|
212 | }
|
213 | exports.tryParseLocator = tryParseLocator;
|
214 | function parseRange(range, opts) {
|
215 | const match = range.match(/^([^#:]*:)?((?:(?!::)[^#])*)(?:#((?:(?!::).)*))?(?:::(.*))?$/);
|
216 | if (match === null)
|
217 | throw new Error(`Invalid range (${range})`);
|
218 | const protocol = typeof match[1] !== `undefined`
|
219 | ? match[1]
|
220 | : null;
|
221 | if (typeof (opts === null || opts === void 0 ? void 0 : opts.requireProtocol) === `string` && protocol !== opts.requireProtocol)
|
222 | throw new Error(`Invalid protocol (${protocol})`);
|
223 | else if ((opts === null || opts === void 0 ? void 0 : opts.requireProtocol) && protocol === null)
|
224 | throw new Error(`Missing protocol (${protocol})`);
|
225 | const source = typeof match[3] !== `undefined`
|
226 | ? decodeURIComponent(match[2])
|
227 | : null;
|
228 | if ((opts === null || opts === void 0 ? void 0 : opts.requireSource) && source === null)
|
229 | throw new Error(`Missing source (${range})`);
|
230 | const rawSelector = typeof match[3] !== `undefined`
|
231 | ? decodeURIComponent(match[3])
|
232 | : decodeURIComponent(match[2]);
|
233 | const selector = (opts === null || opts === void 0 ? void 0 : opts.parseSelector)
|
234 | ? querystring_1.default.parse(rawSelector)
|
235 | : rawSelector;
|
236 | const params = typeof match[4] !== `undefined`
|
237 | ? querystring_1.default.parse(match[4])
|
238 | : null;
|
239 | return {
|
240 |
|
241 | protocol,
|
242 |
|
243 | source,
|
244 |
|
245 | selector,
|
246 |
|
247 | params,
|
248 | };
|
249 | }
|
250 | exports.parseRange = parseRange;
|
251 | function parseFileStyleRange(range, { protocol }) {
|
252 | const { selector, params } = parseRange(range, {
|
253 | requireProtocol: protocol,
|
254 | requireBindings: true,
|
255 | });
|
256 | if (typeof params.locator !== `string`)
|
257 | throw new Error(`Assertion failed: Invalid bindings for ${range}`);
|
258 | const parentLocator = parseLocator(params.locator, true);
|
259 | const path = selector;
|
260 | return { parentLocator, path };
|
261 | }
|
262 | exports.parseFileStyleRange = parseFileStyleRange;
|
263 | function encodeUnsafeCharacters(str) {
|
264 | str = str.replace(/%/g, `%25`);
|
265 | str = str.replace(/:/g, `%3A`);
|
266 | str = str.replace(/#/g, `%23`);
|
267 | return str;
|
268 | }
|
269 | function hasParams(params) {
|
270 | if (params === null)
|
271 | return false;
|
272 | return Object.entries(params).length > 0;
|
273 | }
|
274 | function makeRange({ protocol, source, selector, params }) {
|
275 | let range = ``;
|
276 | if (protocol !== null)
|
277 | range += `${protocol}`;
|
278 | if (source !== null)
|
279 | range += `${encodeUnsafeCharacters(source)}#`;
|
280 | range += encodeUnsafeCharacters(selector);
|
281 | if (hasParams(params))
|
282 | range += `::${querystring_1.default.stringify(params)}`;
|
283 | return range;
|
284 | }
|
285 | exports.makeRange = makeRange;
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 | function convertToManifestRange(range) {
|
293 | const { params, protocol, source, selector } = parseRange(range);
|
294 | for (const name in params)
|
295 | if (name.startsWith(`__`))
|
296 | delete params[name];
|
297 | return makeRange({ protocol, source, params, selector });
|
298 | }
|
299 | exports.convertToManifestRange = convertToManifestRange;
|
300 | function requirableIdent(ident) {
|
301 | if (ident.scope) {
|
302 | return `@${ident.scope}/${ident.name}`;
|
303 | }
|
304 | else {
|
305 | return `${ident.name}`;
|
306 | }
|
307 | }
|
308 | exports.requirableIdent = requirableIdent;
|
309 | function stringifyIdent(ident) {
|
310 | if (ident.scope) {
|
311 | return `@${ident.scope}/${ident.name}`;
|
312 | }
|
313 | else {
|
314 | return `${ident.name}`;
|
315 | }
|
316 | }
|
317 | exports.stringifyIdent = stringifyIdent;
|
318 | function stringifyDescriptor(descriptor) {
|
319 | if (descriptor.scope) {
|
320 | return `@${descriptor.scope}/${descriptor.name}@${descriptor.range}`;
|
321 | }
|
322 | else {
|
323 | return `${descriptor.name}@${descriptor.range}`;
|
324 | }
|
325 | }
|
326 | exports.stringifyDescriptor = stringifyDescriptor;
|
327 | function stringifyLocator(locator) {
|
328 | if (locator.scope) {
|
329 | return `@${locator.scope}/${locator.name}@${locator.reference}`;
|
330 | }
|
331 | else {
|
332 | return `${locator.name}@${locator.reference}`;
|
333 | }
|
334 | }
|
335 | exports.stringifyLocator = stringifyLocator;
|
336 | function slugifyIdent(ident) {
|
337 | if (ident.scope !== null) {
|
338 | return `@${ident.scope}-${ident.name}`;
|
339 | }
|
340 | else {
|
341 | return ident.name;
|
342 | }
|
343 | }
|
344 | exports.slugifyIdent = slugifyIdent;
|
345 | function slugifyLocator(locator) {
|
346 | const { protocol, selector } = parseRange(locator.reference);
|
347 | const humanProtocol = protocol !== null
|
348 | ? protocol.replace(/:$/, ``)
|
349 | : `exotic`;
|
350 | const humanVersion = semver_1.default.valid(selector);
|
351 | const humanReference = humanVersion !== null
|
352 | ? `${humanProtocol}-${humanVersion}`
|
353 | : `${humanProtocol}`;
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 | const hashTruncate = 10;
|
363 | const slug = locator.scope
|
364 | ? `${slugifyIdent(locator)}-${humanReference}-${locator.locatorHash.slice(0, hashTruncate)}`
|
365 | : `${slugifyIdent(locator)}-${humanReference}-${locator.locatorHash.slice(0, hashTruncate)}`;
|
366 | return fslib_1.toFilename(slug);
|
367 | }
|
368 | exports.slugifyLocator = slugifyLocator;
|
369 | function prettyIdent(configuration, ident) {
|
370 | if (ident.scope) {
|
371 | return `${configuration.format(`@${ident.scope}/`, Configuration_1.FormatType.SCOPE)}${configuration.format(ident.name, Configuration_1.FormatType.NAME)}`;
|
372 | }
|
373 | else {
|
374 | return `${configuration.format(ident.name, Configuration_1.FormatType.NAME)}`;
|
375 | }
|
376 | }
|
377 | exports.prettyIdent = prettyIdent;
|
378 | function prettyRangeNoColors(range) {
|
379 | if (range.startsWith(VIRTUAL_PROTOCOL)) {
|
380 | const nested = prettyRangeNoColors(range.substr(range.indexOf(`#`) + 1));
|
381 | const abbrev = range.substr(VIRTUAL_PROTOCOL.length, VIRTUAL_ABBREVIATE);
|
382 |
|
383 |
|
384 | return false ? `${nested} (virtual:${abbrev})` : `${nested} [${abbrev}]`;
|
385 | }
|
386 | else {
|
387 | return range.replace(/\?.*/, `?[...]`);
|
388 | }
|
389 | }
|
390 | function prettyRange(configuration, range) {
|
391 | return `${configuration.format(prettyRangeNoColors(range), Configuration_1.FormatType.RANGE)}`;
|
392 | }
|
393 | exports.prettyRange = prettyRange;
|
394 | function prettyDescriptor(configuration, descriptor) {
|
395 | return `${prettyIdent(configuration, descriptor)}${configuration.format(`@`, Configuration_1.FormatType.RANGE)}${prettyRange(configuration, descriptor.range)}`;
|
396 | }
|
397 | exports.prettyDescriptor = prettyDescriptor;
|
398 | function prettyReference(configuration, reference) {
|
399 | return `${configuration.format(prettyRangeNoColors(reference), Configuration_1.FormatType.REFERENCE)}`;
|
400 | }
|
401 | exports.prettyReference = prettyReference;
|
402 | function prettyLocator(configuration, locator) {
|
403 | return `${prettyIdent(configuration, locator)}${configuration.format(`@`, Configuration_1.FormatType.REFERENCE)}${prettyReference(configuration, locator.reference)}`;
|
404 | }
|
405 | exports.prettyLocator = prettyLocator;
|
406 | function prettyLocatorNoColors(locator) {
|
407 | return `${stringifyIdent(locator)}@${prettyRangeNoColors(locator.reference)}`;
|
408 | }
|
409 | exports.prettyLocatorNoColors = prettyLocatorNoColors;
|
410 | function sortDescriptors(descriptors) {
|
411 | return miscUtils.sortMap(descriptors, [
|
412 | descriptor => stringifyIdent(descriptor),
|
413 | descriptor => descriptor.range,
|
414 | ]);
|
415 | }
|
416 | exports.sortDescriptors = sortDescriptors;
|
417 | function prettyWorkspace(configuration, workspace) {
|
418 | return prettyIdent(configuration, workspace.locator);
|
419 | }
|
420 | exports.prettyWorkspace = prettyWorkspace;
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 | function getIdentVendorPath(ident) {
|
433 | return `node_modules/${requirableIdent(ident)}`;
|
434 | }
|
435 | exports.getIdentVendorPath = getIdentVendorPath;
|