mirror of
https://github.com/lilydjwg/nvchecker.git
synced 2025-03-10 06:14:02 +00:00
npm: Fetch only first 1KB
The result is still large even after specifying "Accept: application/vnd.npm.install-v1+json". Since the content we need are always at the beginning of the response, let's make it more violent.
This commit is contained in:
parent
07951bd745
commit
71a63f90f6
1 changed files with 16 additions and 2 deletions
|
@ -1,9 +1,23 @@
|
||||||
# MIT licensed
|
# MIT licensed
|
||||||
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from nvchecker.api import session
|
||||||
|
|
||||||
NPM_URL = 'https://registry.npmjs.org/%s'
|
NPM_URL = 'https://registry.npmjs.org/%s'
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
async def get_version(name, conf, *, cache, **kwargs):
|
async def get_version(name, conf, *, cache, **kwargs):
|
||||||
key = conf.get('npm', name)
|
key = conf.get('npm', name)
|
||||||
data = await cache.get_json(NPM_URL % key, headers={"Accept": "application/vnd.npm.install-v1+json"})
|
data = await cache.get(NPM_URL % key, get_first_1k)
|
||||||
return data['dist-tags']['latest']
|
|
||||||
|
dist_tags = json.loads(re.search(b'"dist-tags":({.*?})', data).group(1))
|
||||||
|
return dist_tags['latest']
|
||||||
|
|
Loading…
Add table
Reference in a new issue