This repository was archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDownloader.js
More file actions
282 lines (252 loc) · 8.37 KB
/
Downloader.js
File metadata and controls
282 lines (252 loc) · 8.37 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const { inspect } = require('util');
const URI = require('uri-js');
const { setdefault } = require('ferrum');
const fetchAPI = require('@adobe/helix-fetch');
const DEFAULT_FORWARD_HEADERS = [
'x-request-id',
'x-cdn-request-id',
'x-cdn-url',
'x-ow-version-lock',
];
class Downloader {
constructor(context, action, options = {}) {
this._context = context;
this._action = action;
this._queue = [];
this._forceNoCache = options.forceNoCache || false;
const { logger, secrets } = this._action;
if (!secrets || !secrets.HTTP_TIMEOUT) {
logger.warn('No HTTP timeout set, risk of denial-of-service');
}
if (options.forceHttp1 || process.env.HELIX_PIPELINE_FORCE_HTTP1) {
this._fetchContext = fetchAPI.context({
userAgent: 'helix-fetch', // static user-agent for recorded tests
alpnProtocols: [fetchAPI.ALPN_HTTP1_1],
});
} else {
/* istanbul ignore next */
this._fetchContext = fetchAPI;
}
this._client = this._fetchContext.fetch;
}
get tasks() {
return this._queue;
}
get githubRootPath() {
return this._action.secrets.REPO_RAW_ROOT;
}
get timeout() {
return this._action.secrets.HTTP_TIMEOUT;
}
get githubToken() {
const { request = {}, secrets } = this._action;
return (secrets && secrets.GITHUB_TOKEN) || (request.headers ? request.headers['x-github-token'] : '');
}
computeGithubURI(owner, repo, ref, path) {
if (ref === 'gh-pages') {
const url = new URL(`https://${owner}.github.io`);
// remove double slashes
url.pathname = `/${repo}/${path}`.replace(/\/+/g, '/');
return url.href;
}
const rootURI = URI.parse(this.githubRootPath);
const rootPath = rootURI.path;
// remove double slashes
const fullPath = `${rootPath}/${owner}/${repo}/${ref}/${path}`.replace(
/\/+/g,
'/',
);
return URI.serialize({
scheme: rootURI.scheme,
host: rootURI.host,
port: rootURI.port,
path: fullPath,
});
}
/**
* Schedules a task that fetches a web resource.
* @param {object} opts options.
* @param {object} opts.uri URI to download
* @param {object} opts.options Fetch options passed to the underlying helix-fetch.
* @param {string} opts.id Some id to later identify the task.
* @param {number} opts.timeout Override global timeout
* @param {boolean} opts.errorOn404 Treat 404 as error.
* @param {string[]} opts.forwardHeaders Names of headers that are forwarded.
* Default `['x-request-id', 'x-cdn-request-id', 'x-cdn-url']`
* @return {Promise} Promise that resolves with the response.
*/
fetch(opts) {
const { request = {}, logger } = this._action;
const context = this._context;
const { uri, options = {} } = opts;
if (!uri) {
throw new Error('Unknown uri, cannot fetch content');
}
if (!('timeout' in options)) {
options.timeout = this.timeout;
}
if (!('cache' in options)) {
options.cache = request.headers ? request.headers['Cache-Control'] : '';
}
if (this._forceNoCache) {
options.cache = 'no-store';
}
options.headers = options.headers || {};
if (request.headers) {
const forwardHeaders = opts.forwardHeaders || DEFAULT_FORWARD_HEADERS;
if (forwardHeaders.length > 0) {
forwardHeaders.forEach((header) => {
header = header.toLowerCase();
if (request.headers[header]) {
options.headers[header] = request.headers[header];
}
});
}
}
// include transaction id if not already present
if (!options.headers['x-request-id']
&& process.env.__OW_TRANSACTION_ID) {
options.headers['x-request-id'] = process.env.__OW_TRANSACTION_ID;
}
const download = async () => {
logger.debug(`fetching file from ${uri}`);
let res = {};
const { AbortError, timeoutSignal } = this._fetchContext;
const { timeout } = options;
if (timeout) {
delete options.timeout;
options.signal = timeoutSignal(timeout);
}
try {
res = await this._client(uri, options);
const body = await res.text();
if (res.ok) {
return {
status: res.status,
body,
headers: res.headers,
};
}
} catch (e) {
res = e;
res.ok = false;
} finally {
if (options.signal) {
options.signal.clear();
}
}
if (res.status === 404) {
if (!opts.errorOn404) {
logger.info(`Could not find file at ${uri}`);
// res.body = 'not found';
return new fetchAPI.Response('not found', { status: res.status, headers: res.headers });
}
logger.info(`Could not find file at ${uri}`);
setdefault(context, 'response', {}).status = 404;
} else if (res.status === 502 || res instanceof AbortError) {
logger.error(`Gateway timeout of ${timeout} milliseconds exceeded for ${uri}`);
setdefault(context, 'response', {}).status = 504;
} else {
logger.error(`Error while fetching file from ${uri} with the following options:\n${inspect(options, { depth: null })}`);
setdefault(context, 'response', {}).status = 502;
}
let err = res;
if (!(err instanceof Error)) {
err = Error(`Error while fetching file from ${uri}: ${res.status}`);
}
err.status = res.status || 500;
context.error = {
message: err.message,
status: err.status,
stack: err.stack,
};
throw err;
};
const task = download();
task.uri = uri;
task.id = opts.id || '';
task.options = opts;
// ignore rejections in case the task is never `awaited`. this avoids unhandled promise
// rejection warnings
task.catch(() => {});
this._queue.push(task);
return task;
}
/**
* Schedules a task that fetches a file from github.
*
* @param {object} opts Options.
* @param {string} opts.owner Repository owner
* @param {string} opts.repo Repository name
* @param {string} opts.ref Repository ref (e.g. master or sha)
* @param {string} opts.path Path of the file to fetch
* @param {number} opts.timeout Override global timeout
* @param {string} opts.id Some id to later identify the task.
* @param {boolean} opts.errorOn404 Treat 404 as error.
*
* @return {Promise} Promise that resolves with the fetch result.
*/
fetchGithub(opts) {
// prepare request options
const { logger } = this._action;
const options = {};
// if there is a github token, send it in the Authorization header
const token = this.githubToken;
if (token) {
setdefault(options, 'headers', {
Authorization: `token ${token}`,
});
}
// get required request parameters
const {
owner, repo, ref, path,
} = opts;
// bail if a required parameter cannot be found
if (!owner) {
throw new Error('Unknown owner, cannot fetch content');
}
if (!repo) {
throw new Error('Unknown repo, cannot fetch content');
}
if (!path) {
throw new Error('Unknown path, cannot fetch content');
}
if (!ref) {
logger.warn(`Recoverable error: no ref given for ${repo}/${owner}.git${path}, falling back to master`);
}
const uri = this.computeGithubURI(owner, repo, ref || 'master', path);
return this.fetch({
uri,
options,
id: opts.id || '',
errorOn404: opts.errorOn404 || false,
forwardHeaders: [],
});
}
/**
* Returns the task with the specified id.
* @param {string} id task id.
* @returns {object} Task or null, if not found.
*/
getTaskById(id) {
return this._queue.find((task) => (task.id === id));
}
/**
* disconnect clients
*/
destroy() {
this._fetchContext.reset();
}
}
module.exports = Downloader;