2020-08-17 14:04:44 +00:00
|
|
|
# MIT licensed
|
|
|
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
|
|
|
|
2020-09-23 12:04:02 +00:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
from nvchecker.api import session
|
|
|
|
|
2020-08-17 14:04:44 +00:00
|
|
|
NPM_URL = 'https://registry.npmjs.org/%s'
|
|
|
|
|
2020-09-24 10:27:30 +00:00
|
|
|
def configure(config):
|
|
|
|
global NPM_URL
|
2020-09-24 10:56:14 +00:00
|
|
|
url = config.get('registry')
|
|
|
|
if url:
|
2020-09-24 10:27:30 +00:00
|
|
|
NPM_URL = f'{url.rstrip("/")}/%s'
|
|
|
|
|
2020-09-23 12:04:02 +00:00
|
|
|
async def get_first_1k(url):
|
|
|
|
headers = {
|
|
|
|
"Accept": "application/vnd.npm.install-v1+json",
|
|
|
|
"Range": "bytes=0-1023",
|
|
|
|
}
|
|
|
|
res = await session.get(url, headers=headers)
|
|
|
|
return res.body
|
|
|
|
|
2020-08-17 14:04:44 +00:00
|
|
|
async def get_version(name, conf, *, cache, **kwargs):
|
|
|
|
key = conf.get('npm', name)
|
2020-09-23 12:04:02 +00:00
|
|
|
data = await cache.get(NPM_URL % key, get_first_1k)
|
|
|
|
|
|
|
|
dist_tags = json.loads(re.search(b'"dist-tags":({.*?})', data).group(1))
|
|
|
|
return dist_tags['latest']
|