Skip to content

Commit f9bbcad

Browse files
authored
Merge pull request #740 from podverse/develop
Release v4.16.10
2 parents f4bbe21 + 46450f1 commit f9bbcad

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "podverse-api",
3-
"version": "4.16.9",
3+
"version": "4.16.10",
44
"description": "Data API, database migration scripts, and backend services for all Podverse models.",
55
"contributors": [
66
"Mitch Downey"
@@ -175,6 +175,7 @@
175175
"eslint-plugin-jsx-a11y": "6.x",
176176
"eslint-plugin-react": "7.x",
177177
"eslint-plugin-react-hooks": "1.x",
178+
"follow-redirects": "^1.15.6",
178179
"googleapis": "45.0.0",
179180
"http-errors": "1.7.3",
180181
"husky": "3.1.0",

src/lib/request.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as requestPromiseNative from 'request-promise-native'
22
import { config } from '~/config'
33
const { userAgent } = config
4+
const { http, https } = require('follow-redirects')
45

56
export const request = async (url: string, options?: any) => {
67
const headers = (options && options.headers) || {}
@@ -15,3 +16,40 @@ export const request = async (url: string, options?: any) => {
1516

1617
return response
1718
}
19+
20+
export const getFinalRedirectedUrl = (url: string) => {
21+
return new Promise<string>((resolve) => {
22+
const parsedOrginalUrl = new URL(url)
23+
if (url.startsWith('https://')) {
24+
const frRequest = https.request(
25+
{
26+
method: 'HEAD',
27+
hostname: parsedOrginalUrl.hostname,
28+
path: parsedOrginalUrl.pathname + parsedOrginalUrl.search,
29+
Headers: {
30+
'User-Agent': userAgent
31+
}
32+
},
33+
(response) => {
34+
resolve(response.responseUrl)
35+
}
36+
)
37+
frRequest.end()
38+
} else {
39+
const frRequest = http.request(
40+
{
41+
method: 'HEAD',
42+
hostname: parsedOrginalUrl.hostname,
43+
path: parsedOrginalUrl.pathname + parsedOrginalUrl.search,
44+
Headers: {
45+
'User-Agent': userAgent
46+
}
47+
},
48+
(response) => {
49+
resolve(response.responseUrl)
50+
}
51+
)
52+
frRequest.end()
53+
}
54+
})
55+
}

src/routes/tools.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as Router from 'koa-router'
22
import { config } from '~/config'
33
import { emitRouterError } from '~/lib/errors'
4-
import { request } from '~/lib/request'
5-
const createError = require('http-errors')
4+
import { getFinalRedirectedUrl } from '~/lib/request'
65

76
const router = new Router({ prefix: `${config.apiPrefix}${config.apiVersion}/tools` })
87

@@ -25,24 +24,11 @@ router.post('/findHTTPS', async (ctx) => {
2524

2625
const extractHTTPSFromURL = async (url, tries) => {
2726
try {
28-
const res = await request(url, { followRedirect: true, method: 'head' })
2927
if (tries > 5) {
30-
throw new createError.NotFound('Secure URL for ' + url + ' was not found. Too many redirects')
31-
} else if (!res.location) {
32-
if (url.startsWith('https://')) {
33-
return url
34-
} else {
35-
try {
36-
const attemptHttpsUrl = url.replace('http://', 'https://')
37-
// If no error is thrown,then assume it is a valid url.
38-
await request(attemptHttpsUrl, { followRedirect: true, method: 'head' })
39-
return attemptHttpsUrl
40-
} catch (error) {
41-
throw new createError.NotFound('Secure URL for ' + url + ' was not found.')
42-
}
43-
}
28+
return url
4429
} else {
45-
return extractHTTPSFromURL(res.location, tries + 1)
30+
const redirectedUrl = await getFinalRedirectedUrl(url)
31+
return redirectedUrl
4632
}
4733
} catch (error) {
4834
if (['301', '302', '303', '307', '308'].includes(String(error.statusCode))) {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,7 @@ flush-write-stream@^1.0.0:
43604360
inherits "^2.0.3"
43614361
readable-stream "^2.3.6"
43624362

4363-
follow-redirects@^1.15.0:
4363+
follow-redirects@^1.15.0, follow-redirects@^1.15.6:
43644364
version "1.15.6"
43654365
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
43664366
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

0 commit comments

Comments
 (0)