1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | var url = require('url');
|
6 |
|
7 |
|
8 |
|
9 | var urlVars = require('./url_variations');
|
10 |
|
11 |
|
12 |
|
13 | function FetcherIndex (opts) {
|
14 | this.remoteUrl = opts.remoteUrl || '';
|
15 | this.index = opts.index || {};
|
16 | }
|
17 |
|
18 | FetcherIndex.prototype.toJSON = function () {
|
19 | return this.index;
|
20 | };
|
21 |
|
22 | FetcherIndex.prototype.set = function (remoteUrl, localUrl) {
|
23 | this.index[remoteUrl] = localUrl;
|
24 | };
|
25 |
|
26 | FetcherIndex.prototype.resolveRemoteUrl = function (localUrl) {
|
27 | var me = this;
|
28 | var remoteUrl;
|
29 | Object.keys(this.index).forEach(function (key) {
|
30 | if (me.index[key] === localUrl) {
|
31 | remoteUrl = key;
|
32 | }
|
33 | });
|
34 | return remoteUrl || null;
|
35 | };
|
36 |
|
37 | FetcherIndex.prototype.resolveLocalUrl = function (remoteUrl) {
|
38 | var me = this;
|
39 | var absUrl;
|
40 | var localHref;
|
41 | var variations, v, vLength, variation;
|
42 | this.ensureRemoteUrl();
|
43 | absUrl = url.resolve(this.remoteUrl, remoteUrl);
|
44 | variations = urlVars.getURLVariations(absUrl);
|
45 | vLength = variations.length;
|
46 | for (v = 0; v < vLength; v++) {
|
47 | variation = variations[v];
|
48 | localHref = me.index[variation];
|
49 | if (localHref) {
|
50 | return localHref;
|
51 | }
|
52 | }
|
53 | return absUrl;
|
54 | };
|
55 |
|
56 | FetcherIndex.prototype.ensureRemoteUrl = function () {
|
57 | if (this.remoteUrl) {
|
58 | return;
|
59 | }
|
60 | this.remoteUrl = this.resolveRemoteUrl('index.html');
|
61 | };
|
62 |
|
63 | module.exports = FetcherIndex;
|