UNPKG

6.77 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("validate-npm-package-name"),e=require("make-error"),t=require("isomorphic-unfetch"),n=require("tiny-lru"),a=require("git-url-parse");function i(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var s=i(r),o=i(e),c=i(t),d=i(n),u=i(a);const p=["https://registry.npmjs.cf","https://registry.yarnpkg.com"];class y extends e.BaseError{constructor(r,e){super(`fetch: request to ${r} failed with status ${e.statusText}`),this.url=void 0,this.response=void 0,this.url=r,this.response=e}}const g=o.default("InvalidPackageNameError"),l=o.default("InvalidPackageVersionError");async function m(r,...e){}function f({name:r}){const{validForOldPackages:e,validForNewPackages:t}=s.default(r);if(!e&&!t)throw m(),new g(`invalid package name: '${r}'`)}const h=d.default(250,3e5);async function w({url:r,headers:e,cached:t=!0}){const n=`headers=${JSON.stringify(e)};url=${r}`,a=h.get(n);if(t&&a)return m(),a;const i=await c.default(r,{headers:e});if(!i.ok)throw m(),new y(r,i);const s=await i.json();return t&&h.set(n,s),m(),s}async function P({endpoint:r,headers:e,query:t,registry:n="https://registry.npmjs.org",mirrors:a=p,cached:i}){const s=[n,...a].map((e=>{const n=new URL(r,e);return n.search=null!=t?t:"",n.href}));let o;for(const r of s)try{return await w({url:r,headers:e,cached:i})}catch(r){o=r}throw m(),o}async function k({name:r,registry:e,mirrors:t,cached:n}){return f({name:r}),P({endpoint:`/${r}`,headers:{Accept:"application/vnd.npm.install-v1+json"},registry:e,mirrors:t,cached:n})}async function v({endpoint:r,registryDownloadsAPI:e="https://api.npmjs.org",cached:t}){return P({endpoint:r,registry:e,mirrors:[],cached:t})}function $({rawDownloadPeriod:r="last-week"}){if("string"==typeof r)return r;if(r instanceof Date)return b(r);const{start:e,end:t}=r;return`${b(e)}:${b(t)}`}function b(r){return r.toISOString().split("T")[0]}function x({rawPackument:r,version:e="latest"}){var t;const{name:n,"dist-tags":a,versions:i}=r,s=i[null!=(t=a[e])?t:e];if(!s)throw m(),new l(`invalid package version: '${n}@${e}'`);return s}async function D({name:r,registry:e,mirrors:t,cached:n}){return f({name:r}),P({endpoint:`/${r}`,registry:e,mirrors:t,cached:n})}async function R({name:r,version:e,registry:t,mirrors:n,cached:a}){return x({rawPackument:await D({name:r,registry:t,mirrors:n,cached:a}),version:e})}function A({rawLicense:r}){if(r&&"string"==typeof r)return r}function j({rawRepository:r}){return function(r){return r&&"object"==typeof r&&"string"==typeof r.url&&["string","undefined"].includes(typeof r.type)&&["string","undefined"].includes(typeof r.directory)}(r)?I({rawRepository:r}):"string"==typeof r?I({rawRepository:{url:r}}):void 0}function I({rawRepository:r}){const{url:e,directory:t}=r,n=function({url:r}){let e;try{e=u.default(r)}catch{}return e}({url:e});if(!n)return;const{source:a,full_name:i,filepath:s}=n;return{type:"git",url:`https://${a.replace(/^$/,"github.com").replace(/^github$/,"github.com").replace(/^gitlab$/,"gitlab.com").replace(/^bitbucket$/,"bitbucket.org")}/${i}`,directory:null!=t?t:""!==s?s:void 0}}exports.FetchError=y,exports.InvalidPackageNameError=g,exports.InvalidPackageVersionError=l,exports.cloudflareRegistry="https://registry.npmjs.cf",exports.getAbbreviatedPackument=async function({name:r,registry:e,mirrors:t,cached:n}){return function({rawAbbreviatedPackument:r}){const{"dist-tags":e,name:t,modified:n}=r;return{...r,id:t,distTags:e,modifiedAt:n}}({rawAbbreviatedPackument:await k({name:r,registry:e,mirrors:t,cached:n})})},exports.getDailyPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return f({name:r}),v({endpoint:`/downloads/range/${$({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getDailyRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return v({endpoint:`/downloads/range/${$({rawDownloadPeriod:r})}`,registryDownloadsAPI:e,cached:t})},exports.getPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return f({name:r}),v({endpoint:`/downloads/point/${$({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getPackageManifest=async function({name:r,version:e,registry:t,mirrors:n,cached:a}){const i=await D({name:r,registry:t,mirrors:n,cached:a}),s=x({rawPackument:i,version:e});return await async function({rawPackageManifest:r,rawPackument:e,registry:t,mirrors:n,cached:a}){const{_id:i,name:s,version:o,license:c,repository:d,_npmUser:u}=r,p=e.time[o],y=A({rawLicense:c}),g=j({rawRepository:d}),l=await async function({rawPackageManifest:r,registry:e,mirrors:t,cached:n}){const{name:a,types:i,typings:s}=r,o=function({name:r}){return r.startsWith("@types/")?r:`@types/${r.replace("@","").replace("/","__")}`}({name:a});if(a===o||i||s)return;let c=!1;try{const{deprecated:r}=await R({name:o,registry:e,mirrors:t,cached:n});c=void 0===r}catch{}return c?o:void 0}({rawPackageManifest:r,registry:t,mirrors:n,cached:a}),m=function({name:r}){if(!r.startsWith("@types/"))return;const[e,t]=r.replace("@types/","").split("__");return t?`@${e}/${t}`:e}({name:s});return{...r,id:i,createdAt:p,publisher:u,license:y,gitRepository:g,definitelyTypedName:l,untypedName:m}}({rawPackageManifest:s,rawPackument:i,registry:t,mirrors:n,cached:a})},exports.getPackument=async function({name:r,registry:e,mirrors:t,cached:n}){return function({rawPackument:r}){const{_id:e,"dist-tags":t,time:n,license:a,repository:i}=r,s=A({rawLicense:a}),o=j({rawRepository:i}),c=Object.fromEntries(Object.entries(n).filter((([r])=>!["created","modified"].includes(r))));return{...r,id:e,distTags:t,versionsToTimestamps:c,license:s,gitRepository:o}}({rawPackument:await D({name:r,registry:e,mirrors:t,cached:n})})},exports.getRawAbbreviatedPackument=k,exports.getRawPackageManifest=R,exports.getRawPackument=D,exports.getRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return v({endpoint:`/downloads/point/${$({rawDownloadPeriod:r})}`,registryDownloadsAPI:e,cached:t})},exports.getRegistryMetadata=async function({registry:r,cached:e}={}){return P({registry:r,mirrors:[],endpoint:"/",cached:e})},exports.npmRegistry="https://registry.npmjs.org",exports.npmRegistryDownloadsAPI="https://api.npmjs.org",exports.npmRegistryMirrors=p,exports.searchPackages=async function({query:r,registry:e,mirrors:t,cached:n}){const a=function({rawSearchCriteria:r}){return Object.entries(r).filter((([,r])=>["string","number"].includes(typeof r))).map((([r,e])=>`${r}=${e}`)).join("&")}({rawSearchCriteria:r});return P({endpoint:"/-/v1/search",query:a,registry:e,mirrors:t,cached:n})},exports.yarnRegistry="https://registry.yarnpkg.com";
2//# sourceMappingURL=query-registry.cjs.production.min.js.map