Merge pull request #191 from lbryio/urlQueryStringsRB

parses querystrings from urls
This commit is contained in:
Sean Yesmunt 2019-08-28 23:28:26 -04:00 committed by GitHub
commit 314e878b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

15
dist/bundle.es.js vendored
View file

@ -907,6 +907,8 @@ const regexAddress = /^(b|r)(?=[^0OIl]{32,33})[0-9A-Za-z]{32,33}$/;
const regexPartProtocol = '^((?:lbry://)?)';
const regexPartStreamOrChannelName = '([^:$#/]*)';
const regexPartModifierSeparator = '([:$#]?)([^/]*)';
const queryStringBreaker = '^([\\S]+)([?][\\S]*)';
const separateQuerystring = new RegExp(queryStringBreaker);
/**
* Parses a LBRY name into its component parts. Throws errors with user-friendly
@ -927,13 +929,21 @@ const regexPartModifierSeparator = '([:$#]?)([^/]*)';
function parseURI(URL, requireProto = false) {
// Break into components. Empty sub-matches are converted to null
const componentsRegex = new RegExp(regexPartProtocol + // protocol
regexPartStreamOrChannelName + // stream or channel name (stops at the first separator or end)
regexPartModifierSeparator + // modifier separator, modifier (stops at the first path separator or end)
'(/?)' + // path separator, there should only be one (optional) slash to separate the stream and channel parts
regexPartStreamOrChannelName + regexPartModifierSeparator);
// chop off the querystring first
let QSStrippedURL, qs;
const qsRegexResult = separateQuerystring.exec(URL);
if (qsRegexResult) {
[QSStrippedURL, qs] = qsRegexResult.slice(1).map(match => match || null);
}
const regexMatch = componentsRegex.exec(URL) || [];
const cleanURL = QSStrippedURL || URL;
const regexMatch = componentsRegex.exec(cleanURL) || [];
const [proto, ...rest] = regexMatch.slice(1).map(match => match || null);
const path = rest.join('');
const [streamNameOrChannelName, primaryModSeparator, primaryModValue, pathSep, possibleStreamName, secondaryModSeparator, secondaryModValue] = rest;
@ -978,13 +988,14 @@ function parseURI(URL, requireProto = false) {
// They will not work properly with canonical_urls
claimName: streamNameOrChannelName,
claimId: primaryClaimId
}, streamName ? { contentName: streamName } : {});
}, streamName ? { contentName: streamName } : {}, qs ? { queryString: qs } : {});
}
function parseURIModifier(modSeperator, modValue) {
let claimId;
let claimSequence;
let bidPosition;
if (modSeperator) {
if (!modValue) {
throw new Error(__(`No modifier provided after separator %s.`, modSeperator));

View file

@ -9,6 +9,10 @@ export const regexAddress = /^(b|r)(?=[^0OIl]{32,33})[0-9A-Za-z]{32,33}$/;
const regexPartProtocol = '^((?:lbry://)?)';
const regexPartStreamOrChannelName = '([^:$#/]*)';
const regexPartModifierSeparator = '([:$#]?)([^/]*)';
const queryStringBreaker = '^([\\S]+)([?][\\S]*)';
const separateQuerystring = new RegExp(
queryStringBreaker
);
/**
* Parses a LBRY name into its component parts. Throws errors with user-friendly
@ -29,6 +33,7 @@ const regexPartModifierSeparator = '([:$#]?)([^/]*)';
export function parseURI(URL: string, requireProto: boolean = false): LbryUrlObj {
// Break into components. Empty sub-matches are converted to null
const componentsRegex = new RegExp(
regexPartProtocol + // protocol
regexPartStreamOrChannelName + // stream or channel name (stops at the first separator or end)
@ -37,8 +42,15 @@ export function parseURI(URL: string, requireProto: boolean = false): LbryUrlObj
regexPartStreamOrChannelName +
regexPartModifierSeparator
);
// chop off the querystring first
let QSStrippedURL, qs;
const qsRegexResult = separateQuerystring.exec(URL)
if (qsRegexResult) {
[QSStrippedURL, qs] = qsRegexResult.slice(1).map(match => match || null);
}
const regexMatch = componentsRegex.exec(URL) || [];
const cleanURL = QSStrippedURL || URL;
const regexMatch = componentsRegex.exec(cleanURL) || [];
const [proto, ...rest] = regexMatch.slice(1).map(match => match || null);
const path = rest.join('');
const [
@ -107,6 +119,7 @@ export function parseURI(URL: string, requireProto: boolean = false): LbryUrlObj
claimName: streamNameOrChannelName,
claimId: primaryClaimId,
...(streamName ? { contentName: streamName } : {}),
...(qs ? { queryString: qs} : {}),
};
}
@ -114,6 +127,7 @@ function parseURIModifier(modSeperator: ?string, modValue: ?string) {
let claimId;
let claimSequence;
let bidPosition;
if (modSeperator) {
if (!modValue) {
throw new Error(__(`No modifier provided after separator %s.`, modSeperator));