1 | 'use strict'
|
2 |
|
3 | const { FetchError, Request, isRedirect } = require('minipass-fetch')
|
4 | const url = require('url')
|
5 |
|
6 | const CachePolicy = require('./cache/policy.js')
|
7 | const cache = require('./cache/index.js')
|
8 | const remote = require('./remote.js')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const canFollowRedirect = (request, response, options) => {
|
16 | if (!isRedirect(response.status)) {
|
17 | return false
|
18 | }
|
19 |
|
20 | if (options.redirect === 'manual') {
|
21 | return false
|
22 | }
|
23 |
|
24 | if (options.redirect === 'error') {
|
25 | throw new FetchError(`redirect mode is set to error: ${request.url}`,
|
26 | 'no-redirect', { code: 'ENOREDIRECT' })
|
27 | }
|
28 |
|
29 | if (!response.headers.has('location')) {
|
30 | throw new FetchError(`redirect location header missing for: ${request.url}`,
|
31 | 'no-location', { code: 'EINVALIDREDIRECT' })
|
32 | }
|
33 |
|
34 | if (request.counter >= request.follow) {
|
35 | throw new FetchError(`maximum redirect reached at: ${request.url}`,
|
36 | 'max-redirect', { code: 'EMAXREDIRECT' })
|
37 | }
|
38 |
|
39 | return true
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const getRedirect = (request, response, options) => {
|
46 | const _opts = { ...options }
|
47 | const location = response.headers.get('location')
|
48 | const redirectUrl = new url.URL(location, /^https?:/.test(location) ? undefined : request.url)
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | if (new url.URL(request.url).hostname !== redirectUrl.hostname) {
|
68 | request.headers.delete('authorization')
|
69 | request.headers.delete('cookie')
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | if (
|
75 | response.status === 303 ||
|
76 | (request.method === 'POST' && [301, 302].includes(response.status))
|
77 | ) {
|
78 | _opts.method = 'GET'
|
79 | _opts.body = null
|
80 | request.headers.delete('content-length')
|
81 | }
|
82 |
|
83 | _opts.headers = {}
|
84 | request.headers.forEach((value, key) => {
|
85 | _opts.headers[key] = value
|
86 | })
|
87 |
|
88 | _opts.counter = ++request.counter
|
89 | const redirectReq = new Request(url.format(redirectUrl), _opts)
|
90 | return {
|
91 | request: redirectReq,
|
92 | options: _opts,
|
93 | }
|
94 | }
|
95 |
|
96 | const fetch = async (request, options) => {
|
97 | const response = CachePolicy.storable(request, options)
|
98 | ? await cache(request, options)
|
99 | : await remote(request, options)
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | if (!['GET', 'HEAD'].includes(request.method) &&
|
105 | response.status >= 200 &&
|
106 | response.status <= 399) {
|
107 | await cache.invalidate(request, options)
|
108 | }
|
109 |
|
110 | if (!canFollowRedirect(request, response, options)) {
|
111 | return response
|
112 | }
|
113 |
|
114 | const redirect = getRedirect(request, response, options)
|
115 | return fetch(redirect.request, redirect.options)
|
116 | }
|
117 |
|
118 | module.exports = fetch
|