UNPKG

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