forked from wikipathways/cget
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCache.ts
More file actions
619 lines (502 loc) · 16.3 KB
/
Cache.ts
File metadata and controls
619 lines (502 loc) · 16.3 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// This file is part of cget, copyright (c) 2015-2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.
import * as fs from "fs";
import * as path from "path";
import * as http from "http";
import * as stream from "stream";
import got from "got";
import { PromisyClass, TaskQueue } from "cwait";
import { fsa, isDir } from "./mkdirp";
import { mkdirp } from "mkdirp";
import { Address } from "./Address";
// TODO: continue interrupted downloads.
// TODO: handle redirect loops.
export interface FetchOptions {
allowLocal?: boolean;
forceHost?: string;
forcePort?: number;
cwd?: string;
}
export interface CacheOptions extends FetchOptions {
indexName?: string;
concurrency?: number;
}
export interface FilledCacheOptions extends CacheOptions {
allowLocal: boolean;
cwd: string;
indexName: string;
concurrency: number;
forceHost?: string;
forcePort?: number;
}
type InternalHeaders = { [key: string]: number | string };
export type Headers = { [key: string]: string } | http.IncomingHttpHeaders;
interface RedirectSpec {
address: Address;
status: number;
message: string;
headers: Headers;
}
export class CacheResult {
constructor(
streamOut: stream.Readable,
address: Address,
status: number,
message: string,
headers: Headers
) {
this.stream = streamOut;
this.address = address;
this.status = status;
this.message = message;
this.headers = headers;
}
stream: stream.Readable;
address: Address;
status: number;
message: string;
headers: Headers;
}
export class CacheError extends Error {
status: number;
message: string;
headers: Headers;
}
const DefaultOptions = {
indexName: "index.html",
concurrency: 2,
allowLocal: false,
cwd: "."
};
/* Note that in the test from serve.ts, the second param for the Cache
* constructor is a string: "index.html".
* Since the original creator of this library wrote it that way, I updated
* this constructor to handle that case. When that parameter is a string,
* it is now clearly specifying indexName.
*/
export class Cache {
constructor(
basePath: string = "cache",
rawOptions: CacheOptions | string = DefaultOptions
) {
let options: FilledCacheOptions;
if (typeof rawOptions === "string") {
options = { ...DefaultOptions, indexName: rawOptions };
} else {
options = { ...DefaultOptions, ...rawOptions };
}
this.basePath = path.resolve(basePath);
this.indexName = options.indexName;
this.fetchQueue = new TaskQueue(
Promise as PromisyClass,
options.concurrency
);
this.allowLocal = options.allowLocal;
this.forceHost = options.forceHost;
this.forcePort = options.forcePort;
this.cwd = options.cwd;
}
/** Store HTTP redirect headers with the final target address. */
private addLinks(redirectList: RedirectSpec[], target: Address) {
return Promise.all(
redirectList.map(
({
address: address,
status: status,
message: message,
headers: headers
}) =>
this.createCachePath(address).then((cachePath: string) =>
this.storeHeaders(cachePath, headers, {
"cget-status": status,
"cget-message": message,
"cget-target": target.uri
})
)
)
);
}
/** Try to synchronously guess the cache path for an address.
* May be incorrect if it's a directory. */
getCachePathSync(address: Address) {
var cachePath = path.join(this.basePath, address.path);
return cachePath;
}
/** Get local cache file path where a remote URL should be downloaded. */
getCachePath(address: Address) {
var cachePath = this.getCachePathSync(address);
var makeValidPath = (isDir: boolean) => {
if (isDir) cachePath = path.join(cachePath, this.indexName);
return cachePath;
};
if (cachePath.charAt(cachePath.length - 1) == "/") {
return Promise.resolve(makeValidPath(true));
}
return isDir(cachePath).then(makeValidPath);
}
/** Get path to headers for a locally cached file. */
static getHeaderPath(cachePath: string) {
return cachePath + ".header.json";
}
/** Test if an address is cached. */
isCached(uri: string) {
return this.getCachePath(new Address(uri)).then((cachePath: string) =>
fsa
.stat(cachePath)
.then((stats: fs.Stats) => !stats.isDirectory())
.catch((err: NodeJS.ErrnoException) => false)
);
}
/** Like getCachePath, but create its parent directory if nonexistent. */
private createCachePath(address: Address) {
return this.getCachePath(address).then((cachePath: string) =>
mkdirp(path.dirname(cachePath)).then(() => cachePath)
);
}
/** Check if there are cached headers with errors or redirecting the URL. */
private static getRedirect(cachePath: string) {
return fsa
.readFile(Cache.getHeaderPath(cachePath), { encoding: "utf8" })
.then(JSON.parse)
.catch((err: any) => ({}))
.then((headers: InternalHeaders) => {
const status = headers["cget-status"] as number;
if (!status) return null;
if (status >= 300 && status <= 308 && headers["location"]) {
return (headers["cget-target"] || headers["location"]) as string;
}
if (status != 200 && (status < 500 || status >= 600)) {
var err = new CacheError(status + " " + headers["cget-message"]);
err.headers = Cache.removeInternalHeaders(headers);
err.status = status;
throw err;
}
return null;
});
}
/** Store custom data related to a URL-like address,
* for example an XML namespace.
* @return Promise resolving to true after all data is written. */
store(uri: string, data: string) {
return this.createCachePath(new Address(uri))
.then((cachePath: string) =>
fsa.writeFile(cachePath, data, { encoding: "utf8" })
)
.then(() => true);
}
/** Fetch URL from cache or download it if not available yet.
* Returns the file's URL after redirections
* and a readable stream of its contents. */
fetch(uri: string, options?: FetchOptions): Promise<CacheResult> {
if (!options) options = {};
const address = new Address(uri, this.cwd || options.cwd);
if (address.isLocal) {
if (
!(
options.allowLocal ||
(options.allowLocal !== false && this.allowLocal)
)
) {
return Promise.reject(new Error("Access denied to url " + address.url));
}
return new Promise((resolve, reject) =>
this.fetchQueue.add(
() =>
new Promise((resolveTask, rejectTask) =>
this.fetchLocal(address, options!, resolveTask, rejectTask).then(
resolve,
reject
)
)
)
);
}
return new Promise((resolve, reject) =>
this.fetchQueue.add(
() =>
new Promise((resolveTask, rejectTask) =>
this.fetchCached(address, options!, resolveTask)
.catch((err: CacheError | NodeJS.ErrnoException) => {
// Re-throw HTTP and unexpected errors.
if (err instanceof CacheError || err.code != "ENOENT") {
rejectTask(err);
throw err;
}
if (address.url && !address.isLocal) {
return this.fetchRemote(
address,
options!,
resolveTask,
rejectTask
);
} else {
rejectTask(err);
throw err;
}
})
.then(resolve, reject)
)
)
);
}
private async fetchLocal(
address: Address,
options: FetchOptions,
resolveTask: (value: unknown) => void,
rejectTask: (err?: NodeJS.ErrnoException) => void
) {
var streamIn = fs.createReadStream(address.path);
const headers: InternalHeaders = await new Promise((resolve, reject) => {
// Resolve promise with headers if stream opens successfully.
streamIn.on("open", () => resolve(Cache.defaultHeaders));
// Cached file doesn't exist or IO error.
streamIn.on("error", (err_1: NodeJS.ErrnoException) => {
reject(err_1);
rejectTask(err_1);
throw err_1;
});
streamIn.on("end", () => resolveTask(undefined));
});
return new CacheResult(
streamIn,
address,
headers["cget-status"] as number,
headers["cget-message"] as string,
Cache.removeInternalHeaders(headers)
);
}
private fetchCached(
address: Address,
options: FetchOptions,
resolveTask: (value: unknown) => void
) {
var streamIn: fs.ReadStream;
// Any errors shouldn't be handled here, but instead in the caller.
return this.getCachePath(address)
.then((cachePath: string) =>
Cache.getRedirect(cachePath).then((urlRemote: string) =>
urlRemote ? this.getCachePath(new Address(urlRemote)) : cachePath
)
)
.then(
(cachePath: string) =>
new Promise((resolve, reject) => {
streamIn = fs.createReadStream(cachePath);
// Resolve promise with headers if stream opens successfully.
streamIn.on("open", () =>
resolve(
fsa
.readFile(Cache.getHeaderPath(cachePath), {
encoding: "utf8"
})
.then(
/** Parse headers stored as JSON. */
(data: string) => JSON.parse(data)
)
.catch(
/** If headers are not found, invent some. */
(err: NodeJS.ErrnoException) => Cache.defaultHeaders
)
)
);
// Cached file doesn't exist.
streamIn.on("error", reject);
streamIn.on("end", () => resolveTask(undefined));
})
)
.then(
(headers: InternalHeaders) =>
new CacheResult(
streamIn,
address,
headers["cget-status"] as number,
headers["cget-message"] as string,
Cache.removeInternalHeaders(headers)
)
);
}
private storeHeaders(
cachePath: string,
headers: Headers,
extra: InternalHeaders
) {
for (let key of Object.keys(headers)) {
if (!extra.hasOwnProperty(key)) extra[key] = headers[key] as string;
}
return fsa.writeFile(
Cache.getHeaderPath(cachePath),
JSON.stringify(extra),
{ encoding: "utf8" }
);
}
private fetchRemote(
address: Address,
options: FetchOptions,
resolveTask: (value: unknown) => void,
rejectTask: (err?: NodeJS.ErrnoException) => void
) {
var urlRemote = address.url!;
var redirectList: RedirectSpec[] = [];
var found = false;
var resolve: (result: any) => void;
var reject: (err: any) => void;
var promise = new Promise<CacheResult>((res, rej) => {
resolve = res;
reject = rej;
});
function die(err: NodeJS.ErrnoException) {
// Abort and report.
if (streamRequest) streamRequest.destroy();
console.error("Got error:");
console.error(err);
console.error("Downloading URL:");
console.error(urlRemote);
reject(err);
rejectTask(err);
throw err;
}
var streamBuffer = new stream.PassThrough();
const streamRequest = got.stream(Cache.forceRedirect(urlRemote, options), {
method: "get",
responseType: "buffer",
isStream: true,
followRedirect: true
});
streamRequest.on("error", (err: NodeJS.ErrnoException) => {
// Check if retrying makes sense for this error.
if (
(
"EAI_AGAIN ECONNREFUSED ECONNRESET EHOSTUNREACH " +
"ENOTFOUND EPIPE ESOCKETTIMEDOUT ETIMEDOUT "
).indexOf(err.code || "") < 0
) {
die(err);
}
console.error("SHOULD RETRY");
throw err;
});
streamRequest.on("response", (res: http.IncomingMessage) => {
if (found) return;
found = true;
const status = res.statusCode!;
if (status != 200) {
if (status < 500 || status >= 600) {
var err = new CacheError(status + " " + res.statusMessage);
this.createCachePath(address).then((cachePath: string) =>
this.storeHeaders(cachePath, res.headers, {
"cget-status": status,
"cget-message": res.statusMessage!
})
);
err.headers = res.headers;
err.status = status;
reject(err);
rejectTask(err);
return;
}
// TODO
console.error("SHOULD RETRY");
throw new Error("RETRY");
}
streamRequest.pause();
this.createCachePath(address)
.then((cachePath: string) => {
var streamOut = fs.createWriteStream(cachePath);
streamOut.on("finish", () => {
// Output stream file handle stays open after piping unless manually closed.
streamOut.close();
});
streamRequest.pipe(streamOut, { end: true });
streamRequest.pipe(streamBuffer, { end: true });
streamRequest.resume();
return Promise.all([
this.addLinks(redirectList, address),
this.storeHeaders(cachePath, res.headers, {
"cget-status": res.statusCode!,
"cget-message": res.statusMessage!
})
]).finally(() =>
resolve(
new CacheResult(
streamBuffer as any as stream.Readable,
address,
res.statusCode!,
res.statusMessage!,
res.headers
)
)
);
})
.catch(die);
});
streamRequest.on("end", resolveTask);
if (
options.forceHost ||
options.forcePort ||
this.forceHost ||
this.forcePort
) {
// Monkey-patch request to support forceHost when running tests.
(streamRequest as any).cgetOptions = {
forceHost: options.forceHost || this.forceHost,
forcePort: options.forcePort || this.forcePort
};
}
return promise;
}
private static defaultHeaders = {
"cget-status": 200,
"cget-message": "OK"
};
private static internalHeaderTbl: { [key: string]: boolean } = {
"cget-status": true,
"cget-message": true,
"cget-target": true
};
private static removeInternalHeaders(headers: InternalHeaders) {
const output: Headers = {};
for (let key of Object.keys(headers)) {
if (!Cache.internalHeaderTbl[key]) output[key] = headers[key] as string;
}
return output;
}
private static forceRedirect(urlRemote: string, options: FetchOptions) {
if (!options.forceHost && !options.forcePort) return urlRemote;
const urlObj = new URL(urlRemote);
var changed = false;
if (!urlObj.hostname) return urlRemote;
if (options.forceHost && urlObj.hostname != options.forceHost) {
urlObj.hostname = options.forceHost;
changed = true;
}
if (options.forcePort && urlObj.port != "" + options.forcePort) {
urlObj.port = "" + options.forcePort;
changed = true;
}
if (!changed) return urlRemote;
const originalHost = urlObj.host;
urlObj.search = "?host=" + encodeURIComponent(originalHost || "");
// Remove the host to use the modified hostname/port
urlObj.host = urlObj.hostname + (urlObj.port ? ":" + urlObj.port : "");
return urlObj.href;
}
/** Queue for limiting parallel downloads. */
private fetchQueue: TaskQueue<PromisyClass>;
private basePath: string;
private indexName: string;
private allowLocal: boolean;
private forceHost?: string;
private forcePort?: number;
private cwd: string;
/** Monkey-patch request to support forceHost when running tests. */
static patchRequest() {
var proto = require("request/lib/redirect.js").Redirect.prototype;
var func = proto.redirectTo;
proto.redirectTo = function (this: any) {
var urlRemote = func.apply(this, Array.prototype.slice.apply(arguments));
var options: FetchOptions = this.request.cgetOptions;
if (urlRemote && options) return Cache.forceRedirect(urlRemote, options);
return urlRemote;
};
}
}