url-resolver-fs
Version:
resolves urls and provides fs like access
71 lines (62 loc) • 1.68 kB
JavaScript
import { URLScheme } from "./url-scheme";
function generate(name) {
return function(context, url, ...args) {
return this.baseScheme[name](context, this.remap(url), ...args);
};
}
/**
* Remap url
* special:some/path -> https://myserver.com/repo/some/path
* name: special
* baseScheme: https
* prefix: https://myserver.com/repo/
* @param {URLScheme} baseScheme
* @param {string} name of the newly created scheme
* @param {string} prefix urls will be prefixed by this value
*
* @property {URLScheme} baseScheme
* @property {string} name of the newly created scheme
* @property {string} prefix urls will be prefixed by this value
*/
export class URLMapperScheme extends URLScheme {
constructor(baseScheme, name, prefix, options) {
super(options);
const properties = {
baseScheme: {
value: baseScheme
},
name: {
value: name
},
prefix: {
value: prefix
}
};
this.constructor.methods.forEach(name => {
properties[name] = {
value: generate(name)
};
});
Object.defineProperties(this, properties);
}
toJSON() {
return {
name: this.name,
prefix: this.prefix.toString(),
baseScheme: this.baseScheme.name
};
}
/**
* Remapps url by separating scheme (and direct following '/') from suffix
* and appending the suffix (in front)
* @param {URL} url to be remapped
* @return {URL} remapped url
*/
remap(url) {
const m = url.href.match(/^[^:]+:(\/\/)?(.*)/);
return m ? new URL(this.prefix + m[2]) : url;
}
async provideCredentials(...args) {
return this.baseScheme.provideCredentials(...args);
}
}