migrate regularUpdate() from request-->got (#7215)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,93 +1,62 @@
|
||||
import requestModule from 'request'
|
||||
import { Inaccessible, InvalidResponse } from '../base-service/errors.js'
|
||||
/**
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { InvalidResponse } from '../base-service/errors.js'
|
||||
import { fetchFactory } from '../../core/base-service/got.js'
|
||||
import checkErrorResponse from '../../core/base-service/check-error-response.js'
|
||||
const fetcher = fetchFactory()
|
||||
|
||||
// Map from URL to { timestamp: last fetch time, data: data }.
|
||||
let regularUpdateCache = Object.create(null)
|
||||
|
||||
// url: a string, scraper: a function that takes string data at that URL.
|
||||
// interval: number in milliseconds.
|
||||
// cb: a callback function that takes an error and data returned by the scraper.
|
||||
//
|
||||
// To use this from a service:
|
||||
//
|
||||
// import { promisify } from 'util'
|
||||
// import { regularUpdate } from '../../core/legacy/regular-update.js'
|
||||
//
|
||||
// function getThing() {
|
||||
// return promisify(regularUpdate)({
|
||||
// url: ...,
|
||||
// ...
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// in handle():
|
||||
//
|
||||
// const thing = await getThing()
|
||||
|
||||
function regularUpdate(
|
||||
{
|
||||
url,
|
||||
intervalMillis,
|
||||
json = true,
|
||||
scraper = buffer => buffer,
|
||||
options = {},
|
||||
request = requestModule,
|
||||
},
|
||||
cb
|
||||
) {
|
||||
/**
|
||||
* Make a HTTP request using an in-memory cache
|
||||
*
|
||||
* @param {object} attrs Refer to individual attrs
|
||||
* @param {string} attrs.url URL to request
|
||||
* @param {number} attrs.intervalMillis Number of milliseconds to keep cached value for
|
||||
* @param {boolean} [attrs.json=true] True if we expect to parse the response as JSON
|
||||
* @param {Function} [attrs.scraper=buffer => buffer] Function to extract value from the response
|
||||
* @param {object} [attrs.options={}] Options to pass to got
|
||||
* @param {Function} [attrs.requestFetcher=fetcher] Custom fetch function
|
||||
* @returns {*} Parsed response
|
||||
*/
|
||||
async function regularUpdate({
|
||||
url,
|
||||
intervalMillis,
|
||||
json = true,
|
||||
scraper = buffer => buffer,
|
||||
options = {},
|
||||
requestFetcher = fetcher,
|
||||
}) {
|
||||
const timestamp = Date.now()
|
||||
const cached = regularUpdateCache[url]
|
||||
if (cached != null && timestamp - cached.timestamp < intervalMillis) {
|
||||
cb(null, cached.data)
|
||||
return
|
||||
return cached.data
|
||||
}
|
||||
request(url, options, (err, res, buffer) => {
|
||||
if (err != null) {
|
||||
cb(
|
||||
new Inaccessible({
|
||||
prettyMessage: 'intermediate resource inaccessible',
|
||||
underlyingError: err,
|
||||
})
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
cb(
|
||||
new InvalidResponse({
|
||||
prettyMessage: 'intermediate resource inaccessible',
|
||||
})
|
||||
)
|
||||
}
|
||||
const { buffer } = await checkErrorResponse({})(
|
||||
await requestFetcher(url, options)
|
||||
)
|
||||
|
||||
let reqData
|
||||
if (json) {
|
||||
try {
|
||||
reqData = JSON.parse(buffer)
|
||||
} catch (e) {
|
||||
cb(
|
||||
new InvalidResponse({
|
||||
prettyMessage: 'unparseable intermediate json response',
|
||||
underlyingError: e,
|
||||
})
|
||||
)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
reqData = buffer
|
||||
}
|
||||
|
||||
let data
|
||||
let reqData
|
||||
if (json) {
|
||||
try {
|
||||
data = scraper(reqData)
|
||||
reqData = JSON.parse(buffer)
|
||||
} catch (e) {
|
||||
cb(e)
|
||||
return
|
||||
throw new InvalidResponse({
|
||||
prettyMessage: 'unparseable intermediate json response',
|
||||
underlyingError: e,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
reqData = buffer
|
||||
}
|
||||
|
||||
regularUpdateCache[url] = { timestamp, data }
|
||||
cb(null, data)
|
||||
})
|
||||
const data = scraper(reqData)
|
||||
regularUpdateCache[url] = { timestamp, data }
|
||||
return data
|
||||
}
|
||||
|
||||
function clearRegularUpdateCache() {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { promisify } from 'util'
|
||||
import { regularUpdate } from '../../core/legacy/regular-update.js'
|
||||
import { renderVersionBadge } from '../version.js'
|
||||
import { BaseService, NotFound } from '../index.js'
|
||||
@@ -32,7 +31,7 @@ export default class JenkinsPluginVersion extends BaseService {
|
||||
}
|
||||
|
||||
async fetch() {
|
||||
return promisify(regularUpdate)({
|
||||
return regularUpdate({
|
||||
url: 'https://updates.jenkins-ci.org/current/update-center.actual.json',
|
||||
intervalMillis: 4 * 3600 * 1000,
|
||||
scraper: json =>
|
||||
|
||||
@@ -43,7 +43,7 @@ t.create('total downloads (connection error)')
|
||||
.networkOff()
|
||||
.expectBadge({
|
||||
label: 'downloads',
|
||||
message: 'intermediate resource inaccessible',
|
||||
message: 'inaccessible',
|
||||
})
|
||||
|
||||
// This tests the erroring behavior in regular-update.
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { promisify } from 'util'
|
||||
import moment from 'moment'
|
||||
import semver from 'semver'
|
||||
import { regularUpdate } from '../../core/legacy/regular-update.js'
|
||||
|
||||
const dateFormat = 'YYYY-MM-DD'
|
||||
|
||||
function getVersion(version) {
|
||||
async function getVersion(version) {
|
||||
let semver = ``
|
||||
if (version) {
|
||||
semver = `-${version}.x`
|
||||
}
|
||||
return promisify(regularUpdate)({
|
||||
return regularUpdate({
|
||||
url: `https://nodejs.org/dist/latest${semver}/SHASUMS256.txt`,
|
||||
intervalMillis: 24 * 3600 * 1000,
|
||||
json: false,
|
||||
@@ -37,7 +36,7 @@ async function getCurrentVersion() {
|
||||
}
|
||||
|
||||
async function getLtsVersions() {
|
||||
const versions = await promisify(regularUpdate)({
|
||||
const versions = await regularUpdate({
|
||||
url: 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json',
|
||||
intervalMillis: 24 * 3600 * 1000,
|
||||
json: true,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { promisify } from 'util'
|
||||
import semver from 'semver'
|
||||
import { metric, addv } from '../text-formatters.js'
|
||||
import { downloadCount as downloadCountColor } from '../color-formatters.js'
|
||||
@@ -52,7 +51,7 @@ function randomElementFrom(items) {
|
||||
*/
|
||||
async function searchServiceUrl(baseUrl, serviceType = 'SearchQueryService') {
|
||||
// Should we really be caching all these NuGet feeds in memory?
|
||||
const searchQueryServices = await promisify(regularUpdate)({
|
||||
const searchQueryServices = await regularUpdate({
|
||||
url: `${baseUrl}/index.json`,
|
||||
// The endpoint changes once per year (ie, a period of n = 1 year).
|
||||
// We minimize the users' waiting time for information.
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
* using the algorithm followed by Composer (see
|
||||
* https://getcomposer.org/doc/04-schema.md#version).
|
||||
*/
|
||||
import { promisify } from 'util'
|
||||
import request from 'request'
|
||||
import { fetchFactory } from '../core/base-service/got.js'
|
||||
import { regularUpdate } from '../core/legacy/regular-update.js'
|
||||
import { listCompare } from './version.js'
|
||||
import { omitv } from './text-formatters.js'
|
||||
@@ -218,7 +217,7 @@ function versionReduction(versions, phpReleases) {
|
||||
}
|
||||
|
||||
async function getPhpReleases(githubApiProvider) {
|
||||
return promisify(regularUpdate)({
|
||||
return regularUpdate({
|
||||
url: '/repos/php/php-src/git/refs/tags',
|
||||
intervalMillis: 24 * 3600 * 1000, // 1 day
|
||||
scraper: tags =>
|
||||
@@ -233,8 +232,10 @@ async function getPhpReleases(githubApiProvider) {
|
||||
.map(tag => tag.ref.match(/^refs\/tags\/php-(\d+\.\d+)\.\d+$/)[1])
|
||||
)
|
||||
),
|
||||
request: (url, options, cb) =>
|
||||
githubApiProvider.request(request, url, {}, cb),
|
||||
requestFetcher: githubApiProvider.fetch.bind(
|
||||
githubApiProvider,
|
||||
fetchFactory()
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { promisify } from 'util'
|
||||
import semver from 'semver'
|
||||
import { regularUpdate } from '../../core/legacy/regular-update.js'
|
||||
|
||||
@@ -19,8 +18,8 @@ import { regularUpdate } from '../../core/legacy/regular-update.js'
|
||||
// })
|
||||
// .required()
|
||||
|
||||
function getOfferedVersions() {
|
||||
return promisify(regularUpdate)({
|
||||
async function getOfferedVersions() {
|
||||
return regularUpdate({
|
||||
url: 'https://api.wordpress.org/core/version-check/1.7/',
|
||||
intervalMillis: 24 * 3600 * 1000,
|
||||
json: true,
|
||||
|
||||
Reference in New Issue
Block a user