Updated a few more things

This commit is contained in:
QuiteAFancyEmerald 2022-02-08 00:43:17 -08:00
parent c827e9f7e1
commit 73cdcb3186
47 changed files with 35805 additions and 1102 deletions

View file

@ -9,30 +9,29 @@ const defaultConfig = {
};
class URLWrapper {
constructor(config = defaultConfig) {
constructor(config = defaultConfig, ctx) {
this.prefix = config.prefix || defaultConfig.prefix;
this.codec = codec[config.codec || 'plain'] || codec['plain'];
this.regex = /^(#|about:|data:|blob:|mailto:|javascript:)/;
this.regex = /^(#|about:|data:|blob:|mailto:)/;
this.javascript = /^javascript:/;
this.protocols = ['http:', 'https:', 'ws:', 'wss:'];
this.ctx = ctx;
};
wrap(val, config = {}) {
if (!val || this.regex.test(val)) return val;
let flags = '';
(config.flags || []).forEach(flag => flags += `${flag}_/`);
if (config.base) try {
if (!['http:', 'https:', 'ws:', 'wss:'].includes(new URL(val, config.base).protocol)) return val;
} catch(e) {
return val;
if (!val && val != null || this.regex.test(val)) return val;
if (this.javascript.test(val)) {
return 'javascript:' + this.ctx.js.process(val.slice('javascript:'.length));
};
return (config.origin || '') + this.prefix + flags + this.codec.encode(config.base ? new URL(val, config.base) : val) + '/';
const url = new URL(val, config.base);
if (!this.protocols.includes(url.protocol)) return val;
return (config.origin || '') + this.prefix.slice(0, -1) + [ '', ...(config.flags || []) ].join('/_') + '/' + this.codec.encode(url.href);
};
unwrap(val, config = {}) {
if (!val || this.regex.test(val)) return val;
let processed = val.slice((config.origin || '').length + this.prefix.length);
const flags = ('/' + processed).match(/(?<=\/)(.*?)(?=_\/)/g) || [];
flags.forEach(flag => processed = processed.slice(`${flag}_/`.length));
let [ url, leftovers ] = processed.split(/\/(.+)?/);
return config.flags ? { value: this.codec.decode((url || '')) + (config.leftovers && leftovers ? leftovers : ''), flags } : this.codec.decode((url || '')) + (config.leftovers && leftovers ? leftovers : '');
if (!val && val != null || this.regex.test(val) || this.javascript.test(val)) return val;
const flags = (val.match(/\/_(.*)\//) || [ null, ''])[1].split('/_');
const url = this.codec.decode(val.slice(((config.origin || '') + this.prefix + (flags[0] ? '_' + flags.join('/_') + '/' : '')).length));
return config.flags ? { flags, value: url } : url;
};
};
}
module.exports = URLWrapper;