|
@ -1,16 +1,25 @@ |
|
|
const axios = require('../axios.js') |
|
|
const axios = require('../axios.js') |
|
|
const millify = require('millify') |
|
|
const millify = require('millify') |
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/npm/registry/blob/master/docs/REGISTRY-API.md
|
|
|
|
|
|
// https://unpkg.com/
|
|
|
|
|
|
|
|
|
module.exports = async function (method, ...args) { |
|
|
module.exports = async function (method, ...args) { |
|
|
switch (method) { |
|
|
switch (method) { |
|
|
case 'v': |
|
|
case 'v': |
|
|
return v(args) |
|
|
return version(args) |
|
|
case 'dd': |
|
|
case 'dd': |
|
|
return d('last-day', args) |
|
|
return download('last-day', args) |
|
|
case 'dw': |
|
|
case 'dw': |
|
|
return d('last-week', args) |
|
|
return download('last-week', args) |
|
|
case 'dm': |
|
|
case 'dm': |
|
|
return d('last-month', args) |
|
|
return download('last-month', args) |
|
|
|
|
|
case 'dy': |
|
|
|
|
|
return download('last-year', args) |
|
|
|
|
|
case 'license': |
|
|
|
|
|
return pkg('license', args) |
|
|
|
|
|
case 'node': |
|
|
|
|
|
return pkg('node', args) |
|
|
default: |
|
|
default: |
|
|
return { |
|
|
return { |
|
|
subject: 'npm', |
|
|
subject: 'npm', |
|
@ -20,8 +29,27 @@ module.exports = async function (method, ...args) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// npm download
|
|
|
async function pkg (topic, args) { |
|
|
async function d (period, args) { |
|
|
const endpoint = `https://unpkg.com/${args.join('/')}/package.json` |
|
|
|
|
|
const meta = await axios.get(endpoint).then(res => res.data) |
|
|
|
|
|
|
|
|
|
|
|
switch (topic) { |
|
|
|
|
|
case 'license': |
|
|
|
|
|
return { |
|
|
|
|
|
subject: 'license', |
|
|
|
|
|
status: meta.license || 'unknown', |
|
|
|
|
|
color: 'blue' |
|
|
|
|
|
} |
|
|
|
|
|
case 'node': |
|
|
|
|
|
return { |
|
|
|
|
|
subject: 'node', |
|
|
|
|
|
status: (meta.engines && meta.engines.node) || '*', |
|
|
|
|
|
color: 'green' |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function download (period, args) { |
|
|
const endpoint = `https://api.npmjs.org/downloads/point/${period}/${args.join('/')}` |
|
|
const endpoint = `https://api.npmjs.org/downloads/point/${period}/${args.join('/')}` |
|
|
const stats = await axios.get(endpoint).then(res => res.data) |
|
|
const stats = await axios.get(endpoint).then(res => res.data) |
|
|
return { |
|
|
return { |
|
@ -31,9 +59,16 @@ async function d (period, args) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// npm version
|
|
|
async function version (args) { |
|
|
async function v (args) { |
|
|
// Due to an bug of npm registry api, scoped package need to be handled
|
|
|
const version = await fetchLatestVersion(args) |
|
|
// separately: https://github.com/npm/registry/issues/34
|
|
|
|
|
|
// A workaround is using version range("*" for "latest") by Andrew Goode:
|
|
|
|
|
|
// https://github.com/npm/registry/issues/34#issuecomment-228349870
|
|
|
|
|
|
const scoped = args.length === 2 && args[0][0] === '@' |
|
|
|
|
|
const endpoint = scoped |
|
|
|
|
|
? `https://registry.npmjs.org/${args.join('%2F')}/*` |
|
|
|
|
|
: `https://registry.npmjs.org/${args}/latest` |
|
|
|
|
|
const { version } = await axios.get(endpoint).then(res => res.data) |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
subject: 'npm', |
|
|
subject: 'npm', |
|
@ -41,18 +76,3 @@ async function v (args) { |
|
|
color: version.split('.')[0] === '0' ? 'orange' : 'blue' |
|
|
color: version.split('.')[0] === '0' ? 'orange' : 'blue' |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function fetchLatestVersion (args) { |
|
|
|
|
|
const scoped = args.length === 2 && args[0][0] === '@' |
|
|
|
|
|
let endpoint |
|
|
|
|
|
// Due to an bug of npm registry api, scoped package need to be handled
|
|
|
|
|
|
// separately: https://github.com/npm/registry/issues/34
|
|
|
|
|
|
// A workaround is using version range("*" for "latest") by Andrew Goode:
|
|
|
|
|
|
// https://github.com/npm/registry/issues/34#issuecomment-228349870
|
|
|
|
|
|
if (scoped) { |
|
|
|
|
|
endpoint = `https://registry.npmjs.org/${args.join('%2F')}/*` |
|
|
|
|
|
} else { |
|
|
|
|
|
endpoint = `https://registry.npmjs.org/${args}/latest` |
|
|
|
|
|
} |
|
|
|
|
|
return (await axios.get(endpoint).then(res => res.data)).version |
|
|
|
|
|
} |
|
|
|
|
|