1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-05 10:49:28 +02:00

Use got instead of request

This commit is contained in:
Chocobozzz 2021-03-08 14:24:11 +01:00
parent 71926aae07
commit db4b15f21f
No known key found for this signature in database
GPG key ID: 583A612D890159BE
32 changed files with 346 additions and 328 deletions

View file

@ -1,27 +1,26 @@
import { ACTIVITY_PUB, REQUEST_TIMEOUT, WEBSERVER } from '../../initializers/constants'
import { doRequest } from '../../helpers/requests'
import { logger } from '../../helpers/logger'
import * as Bluebird from 'bluebird'
import { ActivityPubOrderedCollection } from '../../../shared/models/activitypub'
import { URL } from 'url'
import { ActivityPubOrderedCollection } from '../../../shared/models/activitypub'
import { logger } from '../../helpers/logger'
import { doJSONRequest } from '../../helpers/requests'
import { ACTIVITY_PUB, REQUEST_TIMEOUT, WEBSERVER } from '../../initializers/constants'
type HandlerFunction<T> = (items: T[]) => (Promise<any> | Bluebird<any>)
type CleanerFunction = (startedDate: Date) => (Promise<any> | Bluebird<any>)
async function crawlCollectionPage <T> (uri: string, handler: HandlerFunction<T>, cleaner?: CleanerFunction) {
logger.info('Crawling ActivityPub data on %s.', uri)
async function crawlCollectionPage <T> (argUrl: string, handler: HandlerFunction<T>, cleaner?: CleanerFunction) {
let url = argUrl
logger.info('Crawling ActivityPub data on %s.', url)
const options = {
method: 'GET',
uri,
json: true,
activityPub: true,
timeout: REQUEST_TIMEOUT
}
const startDate = new Date()
const response = await doRequest<ActivityPubOrderedCollection<T>>(options)
const response = await doJSONRequest<ActivityPubOrderedCollection<T>>(url, options)
const firstBody = response.body
const limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT
@ -35,9 +34,9 @@ async function crawlCollectionPage <T> (uri: string, handler: HandlerFunction<T>
const remoteHost = new URL(nextLink).host
if (remoteHost === WEBSERVER.HOST) continue
options.uri = nextLink
url = nextLink
const res = await doRequest<ActivityPubOrderedCollection<T>>(options)
const res = await doJSONRequest<ActivityPubOrderedCollection<T>>(url, options)
body = res.body
} else {
// nextLink is already the object we want
@ -49,7 +48,7 @@ async function crawlCollectionPage <T> (uri: string, handler: HandlerFunction<T>
if (Array.isArray(body.orderedItems)) {
const items = body.orderedItems
logger.info('Processing %i ActivityPub items for %s.', items.length, options.uri)
logger.info('Processing %i ActivityPub items for %s.', items.length, url)
await handler(items)
}