MCPcopy Index your code
hub / github.com/nodejs/node / parsePackageName

Function parsePackageName

lib/internal/modules/package_json_reader.js:233–265  ·  view source on GitHub ↗

* Parse a package name from a specifier. * @param {string} specifier - The import specifier. * @param {string | URL | undefined} base - The parent URL. * @returns {object}

(specifier, base)

Source from the content-addressed store, hash-verified

231 * @returns {object}
232 */
233function parsePackageName(specifier, base) {
234 let separatorIndex = StringPrototypeIndexOf(specifier, '/');
235 let validPackageName = true;
236 let isScoped = false;
237 if (specifier[0] === '@') {
238 isScoped = true;
239 if (separatorIndex === -1 || specifier.length === 0) {
240 validPackageName = false;
241 } else {
242 separatorIndex = StringPrototypeIndexOf(
243 specifier, '/', separatorIndex + 1);
244 }
245 }
246
247 const packageName = separatorIndex === -1 ?
248 specifier : StringPrototypeSlice(specifier, 0, separatorIndex);
249
250 // Package name cannot have leading . and cannot have percent-encoding or
251 // \\ separators.
252 if (RegExpPrototypeExec(invalidPackageNameRegEx, packageName) !== null) {
253 validPackageName = false;
254 }
255
256 if (!validPackageName) {
257 throw new ERR_INVALID_MODULE_SPECIFIER(
258 specifier, 'is not a valid package name', fileURLToPath(base));
259 }
260
261 const packageSubpath = '.' + (separatorIndex === -1 ? '' :
262 StringPrototypeSlice(specifier, separatorIndex));
263
264 return { packageName, packageSubpath, isScoped };
265}
266
267function getPackageJSONURL(specifier, base) {
268 const { packageName, packageSubpath, isScoped } = parsePackageName(specifier, base);

Callers 1

getPackageJSONURLFunction · 0.70

Calls 1

fileURLToPathFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…