UNPKG

1.37 kBJavaScriptView Raw
1'use strict';
2
3// Node.js built-ins
4
5var url = require('url');
6
7// our modules
8
9var urlVars = require('./url_variations');
10
11// this module
12
13function FetcherIndex (opts) {
14 this.remoteUrl = opts.remoteUrl || '';
15 this.index = opts.index || {};
16}
17
18FetcherIndex.prototype.toJSON = function () {
19 return this.index;
20};
21
22FetcherIndex.prototype.set = function (remoteUrl, localUrl) {
23 this.index[remoteUrl] = localUrl;
24};
25
26FetcherIndex.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
37FetcherIndex.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
56FetcherIndex.prototype.ensureRemoteUrl = function () {
57 if (this.remoteUrl) {
58 return; // nothing to do
59 }
60 this.remoteUrl = this.resolveRemoteUrl('index.html');
61};
62
63module.exports = FetcherIndex;