MCPcopy Index your code
hub / github.com/nodejs/node / #parse

Method #parse

lib/internal/modules/package_map.js:66–135  ·  view source on GitHub ↗

* @param {object} data

(data)

Source from the content-addressed store, hash-verified

64 * @param {object} data
65 */
66 #parse(data) {
67 if (!data.packages || typeof data.packages !== 'object') {
68 throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, 'missing "packages" object');
69 }
70
71 const entries = ObjectEntries(data.packages);
72 for (let i = 0; i < entries.length; i++) {
73 const { 0: key, 1: entry } = entries[i];
74
75 const { url } = entry;
76 switch (typeof url) {
77 case 'string':
78 if (url === '') {
79 throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, `package "${key}" has an empty "url" field`);
80 }
81 break;
82 case 'undefined':
83 throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, `package "${key}" is missing "url" field`);
84 default:
85 throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, `package "${key}" has invalid "url" field: expected string, got ${typeof url}`);
86 }
87 let packageURL;
88 try {
89 packageURL = new URL(entry.url, this.#baseURL);
90 } catch (err) {
91 const error = new ERR_PACKAGE_MAP_INVALID(
92 this.#configPath,
93 `package "${key}" has invalid url "${entry.url}"`,
94 );
95 setOwnProperty(error, 'cause', err);
96 throw error;
97 }
98
99 if (packageURL.protocol !== 'file:') {
100 throw new ERR_PACKAGE_MAP_INVALID(
101 this.#configPath,
102 `package "${key}" has unsupported URL scheme in url "${entry.url}"; only file: URLs and relative URLs are currently supported`,
103 );
104 }
105
106 let absolutePath;
107 try {
108 absolutePath = fileURLToPath(packageURL);
109 } catch (err) {
110 const error = new ERR_PACKAGE_MAP_INVALID(
111 this.#configPath,
112 `package "${key}" has invalid file url "${entry.url}"`,
113 );
114 setOwnProperty(error, 'cause', err);
115 throw error;
116 }
117
118 this.#packages.set(key, {
119 path: absolutePath,
120 dependencies: new SafeMap(ObjectEntries(entry.dependencies ?? {})),
121 });
122
123 // TODO(arcanis): Duplicates should be tolerated, but it requires changing module IDs

Callers 1

constructorMethod · 0.95

Calls 4

setOwnPropertyFunction · 0.85
fileURLToPathFunction · 0.85
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected