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("url-join"),i=require("git-url-parse");function s(r){return r&&"object"==typeof r&&"default"in r?r:{default:r}}var o=s(r),c=s(e),d=s(t),u=s(n),p=s(a),y=s(i);const g=["https://registry.npmjs.cf","https://registry.yarnpkg.com"];class l 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 m=c.default("InvalidPackageNameError"),f=c.default("InvalidPackageVersionError");async function h(r,...e){}function w({name:r}){const{validForOldPackages:e,validForNewPackages:t}=o.default(r);if(!e&&!t)throw h(),new m(`invalid package name: '${r}'`)}const P=u.default(250,3e5);async function k({url:r,headers:e,cached:t=!0}){const n=`headers=${JSON.stringify(e)};url=${r}`,a=P.get(n);if(t&&a)return h(),a;const i=await d.default(r,{headers:e});if(!i.ok)throw h(),new l(r,i);const s=await i.json();return t&&P.set(n,s),h(),s}async function v({endpoint:r,headers:e,query:t,registry:n="https://registry.npmjs.org",mirrors:a=g,cached:i}){const s=[n,...a].map((e=>p.default(e,r,t?`?${t}`:"")));let o;for(const r of s)try{return await k({url:r,headers:e,cached:i})}catch(r){o=r}throw h(),o}async function $({name:r,registry:e,mirrors:t,cached:n}){return w({name:r}),v({endpoint:`/${r}`,headers:{Accept:"application/vnd.npm.install-v1+json"},registry:e,mirrors:t,cached:n})}async function b({endpoint:r,registryDownloadsAPI:e="https://api.npmjs.org",cached:t}){return v({endpoint:r,registry:e,mirrors:[],cached:t})}function x({rawDownloadPeriod:r="last-week"}){if("string"==typeof r)return r;if(r instanceof Date)return D(r);const{start:e,end:t}=r;return`${D(e)}:${D(t)}`}function D(r){return r.toISOString().split("T")[0]}function R({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 h(),new f(`invalid package version: '${n}@${e}'`);return s}async function A({name:r,registry:e,mirrors:t,cached:n}){return w({name:r}),v({endpoint:`/${r}`,registry:e,mirrors:t,cached:n})}async function j({name:r,version:e,registry:t,mirrors:n,cached:a}){return R({rawPackument:await A({name:r,registry:t,mirrors:n,cached:a}),version:e})}function I({rawLicense:r}){if(r&&"string"==typeof r)return r}function q({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)?_({rawRepository:r}):"string"==typeof r?_({rawRepository:{url:r}}):void 0}function _({rawRepository:r}){const{url:e,directory:t}=r,n=function({url:r}){let e;try{e=y.default(r)}catch{}return e}({url:e});if(!n)return;const{resource: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=l,exports.InvalidPackageNameError=m,exports.InvalidPackageVersionError=f,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 $({name:r,registry:e,mirrors:t,cached:n})})},exports.getDailyPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return w({name:r}),b({endpoint:`/downloads/range/${x({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getDailyRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return b({endpoint:`/downloads/range/${x({rawDownloadPeriod:r})}`,registryDownloadsAPI:e,cached:t})},exports.getPackageDownloads=async function({name:r,period:e,registryDownloadsAPI:t,cached:n}){return w({name:r}),b({endpoint:`/downloads/point/${x({rawDownloadPeriod:e})}/${r}`,registryDownloadsAPI:t,cached:n})},exports.getPackageManifest=async function({name:r,version:e,registry:t,mirrors:n,cached:a}){const i=await A({name:r,registry:t,mirrors:n,cached:a}),s=R({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=I({rawLicense:c}),g=q({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 j({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=I({rawLicense:a}),o=q({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 A({name:r,registry:e,mirrors:t,cached:n})})},exports.getRawAbbreviatedPackument=$,exports.getRawPackageManifest=j,exports.getRawPackument=A,exports.getRegistryDownloads=async function({period:r,registryDownloadsAPI:e,cached:t}={}){return b({endpoint:`/downloads/point/${x({rawDownloadPeriod:r})}`,registryDownloadsAPI:e,cached:t})},exports.getRegistryMetadata=async function({registry:r,cached:e}={}){return v({registry:r,mirrors:[],endpoint:"/",cached:e})},exports.npmRegistry="https://registry.npmjs.org",exports.npmRegistryDownloadsAPI="https://api.npmjs.org",exports.npmRegistryMirrors=g,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 v({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