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