-
Notifications
You must be signed in to change notification settings - Fork 43
/
http.js
147 lines (119 loc) · 3.79 KB
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const config = require('./../../../config')
const http = require('http')
const https = require('https')
const path = require('path')
const streamifier = require('streamifier')
const url = require('url')
const urljoin = require('url-join')
const Missing = require(path.join(__dirname, '/missing'))
const HTTPStorage = function ({assetType = 'assets', domain, url}) {
let isExternalURL = url.indexOf('http:') === 0 ||
url.indexOf('https:') === 0
let remoteAddress = config.get(`${assetType}.remote.path`, domain)
if (!isExternalURL) {
if (!remoteAddress) {
throw new Error('Remote address not specified')
}
this.baseUrl = remoteAddress
}
this.domain = domain
this.url = url
}
HTTPStorage.prototype.getFullUrl = function () {
if (this.baseUrl) {
return urljoin(this.baseUrl, this.url)
} else {
return this.url
}
}
HTTPStorage.prototype.get = function ({
redirects = 0,
requestUrl = this.getFullUrl()
} = {}) {
return new Promise((resolve, reject) => {
let parsedUrl = url.parse(requestUrl)
let requestFn = parsedUrl.protocol === 'https:'
? https
: http
requestFn.get({
protocol: parsedUrl.protocol,
hostname: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port,
headers: {
'User-Agent': 'DADI CDN'
}
}, res => {
let statusCode = res.statusCode
if (statusCode === 200) {
let buffers = []
res.on('data', chunk => {
buffers.push(chunk)
})
res.on('end', () => {
return resolve(streamifier.createReadStream(Buffer.concat(buffers)))
})
} else {
if (
[301, 302, 307].includes(statusCode) &&
typeof res.headers.location === 'string'
) {
let parsedRedirectUrl = url.parse(res.headers.location)
parsedRedirectUrl.host = parsedRedirectUrl.host || parsedUrl.host
parsedRedirectUrl.port = parsedRedirectUrl.port || parsedUrl.port
parsedRedirectUrl.protocol = parsedRedirectUrl.protocol || parsedUrl.protocol
if (redirects < config.get('http.followRedirects', this.domain)) {
return resolve(
this.get({
redirects: redirects + 1,
requestUrl: url.format(parsedRedirectUrl)
})
)
}
// We've hit the maximum number of redirects allowed, so we'll
// treat this as a 404.
statusCode = 404
}
let httpError
switch (statusCode) {
case 404:
httpError = new Error(`Not Found: ${this.getFullUrl()}`)
break
case 403:
httpError = new Error(`Forbidden: ${this.getFullUrl()}`)
break
default:
httpError = new Error(`Remote server responded with error code ${statusCode} for URL: ${this.getFullUrl()}`)
break
}
httpError.statusCode = statusCode
if (statusCode === 404) {
new Missing().get({
domain: this.domain,
isDirectory: path.parse(this.getFullUrl()).ext === ''
}).then(stream => {
this.notFound = true
this.lastModified = new Date()
resolve(stream)
}).catch(() => {
reject(httpError)
})
} else {
reject(httpError)
}
}
}).on('error', (err) => {
let httpError
if (err.code && err.code === 'ENOTFOUND') {
httpError = new Error(`Remote server not found for URL: ${this.getFullUrl()} ${err.message}`)
} else {
httpError = new Error(`ERROR: ${err.message} CODE: ${err.code}`)
}
httpError.statusCode = 500
reject(httpError)
})
})
}
module.exports = HTTPStorage
module.exports.HTTPStorage = HTTPStorage