MCPcopy Create free account
hub / github.com/alibaba/open-code-review / fetchLatestVersion

Function fetchLatestVersion

scripts/update.js:72–110  ·  view source on GitHub ↗
(pkg)

Source from the content-addressed store, hash-verified

70}
71
72function fetchLatestVersion(pkg) {
73 const registry = (pkg.publishConfig && pkg.publishConfig.registry) || DEFAULT_REGISTRY;
74 const pkgName = pkg.name;
75 if (!pkgName) return Promise.resolve(null);
76 const encodedName = pkgName.replace(/\//g, "%2F");
77 const url = `${registry.replace(/\/$/, "")}/${encodedName}/latest`;
78 if (!url.startsWith("https://")) return Promise.resolve(null);
79
80 return new Promise((resolve) => {
81 const options = {
82 headers: { "User-Agent": "ocr-updater", Accept: "application/json" },
83 timeout: 15000,
84 };
85 const req = https
86 .get(url, options, (res) => {
87 if (res.statusCode !== 200) {
88 res.resume();
89 resolve(null);
90 return;
91 }
92 let data = "";
93 res.on("data", (chunk) => (data += chunk));
94 res.on("end", () => {
95 try {
96 const json = JSON.parse(data);
97 resolve(json.version || null);
98 } catch (_) {
99 resolve(null);
100 }
101 });
102 res.on("error", () => resolve(null));
103 })
104 .on("error", () => resolve(null));
105 req.on("timeout", () => {
106 req.destroy();
107 resolve(null);
108 });
109 });
110}
111
112const SEMVER_RE = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/;
113

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected