URL parsing for app redirects

URL parsing logic is basically a straight port of how lbryio/lbry-sdk
parses lbry URLs. While there's a lot more going on, it should be
easier to follow what they're doing this way.

Also, added some light unit tests for the URL parsing using jest.
Which, of course, needed a babel config.

With this, tabOnUpdated was able to be rewritten more elegantly to
handle redirects as it delegates a lot of the work to utilities now.
This commit is contained in:
Kevin Raoofi 2020-10-13 04:37:13 -04:00
parent da5cc12a7b
commit 9f8e521fa6
7 changed files with 5353 additions and 46 deletions

6
babel.config.js Normal file
View file

@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
]
}

192
jest.config.js Normal file
View file

@ -0,0 +1,192 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/rb/jcbfhkxn5svdbmr4vtqdj_100000gn/T/jest_dx",
// Automatically clear mock calls and instances between every test
clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
// coverageDirectory: undefined,
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "json",
// "jsx",
// "ts",
// "tsx",
// "node"
// ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
// preset: undefined,
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state between every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: undefined,
// Automatically restore mock state between every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
// testEnvironment: "jest-environment-jsdom",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jasmine2",
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",
// A map from regular expressions to paths to transformers
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
};

4949
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -10,7 +10,8 @@
"build": "npm-run-all -l -p build:parcel build:assets", "build": "npm-run-all -l -p build:parcel build:assets",
"watch": "npm-run-all -l -p watch:parcel watch:assets", "watch": "npm-run-all -l -p watch:parcel watch:assets",
"start:chrome": "web-ext run -t chromium --source-dir ./dist", "start:chrome": "web-ext run -t chromium --source-dir ./dist",
"start:firefox": "web-ext run --source-dir ./dist" "start:firefox": "web-ext run --source-dir ./dist",
"test": "jest"
}, },
"browserslist": [ "browserslist": [
">1%", ">1%",
@ -18,10 +19,13 @@
], ],
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@babel/preset-typescript": "^7.10.4",
"@types/chrome": "0.0.124", "@types/chrome": "0.0.124",
"@types/lodash": "^4.14.162", "@types/lodash": "^4.14.162",
"cpx": "^1.5.0", "cpx": "^1.5.0",
"cross-env": "^7.0.2", "cross-env": "^7.0.2",
"@types/jest": "^26.0.14",
"jest": "^26.5.3",
"lodash": "^4.17.20", "lodash": "^4.17.20",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"parcel-bundler": "^1.12.4", "parcel-bundler": "^1.12.4",

View file

@ -0,0 +1,29 @@
import { appRedirectUrl, parseProtocolUrl } from './lbry-url';
describe('web url parsing', () => {
const testCases: [string, string | undefined][] = [
['https://lbry.tv/@test:7/foo-123:7', 'lbry://@test:7/foo-123:7'],
['https://lbry.tv/@test1:c/foo:8', 'lbry://@test1:c/foo:8'],
['https://lbry.tv/@test1:0/foo-bar-2-baz-7:e#adasasddasdas123', 'lbry://@test1:0/foo-bar-2-baz-7:e'],
['https://lbry.tv/@test:7', 'lbry://@test:7'],
['https://lbry.tv/@test:c', 'lbry://@test:c'],
['https://lbry.tv/$/discover?t=foo%20bar', undefined],
['https://lbry.tv/$/signup?redirect=/@test1:0/foo-bar-2-baz-7:e#adasasddasdas123', undefined],
];
test.each(testCases)('redirect %s', (url, expected) => {
expect(appRedirectUrl(url)).toEqual(expected);
});
});
describe('app url parsing', () => {
const testCases: Array<[string, string[]]> = [
['test', ['test']],
['@test', ['@test']],
['lbry://@test$1/stuff', ['@test$1', 'stuff']],
];
test.each(testCases)('redirect %s', (url, expected) => {
expect(parseProtocolUrl(url)).toEqual(expected);
});
});

142
src/common/lbry-url.ts Normal file
View file

@ -0,0 +1,142 @@
// Port of https://github.com/lbryio/lbry-sdk/blob/master/lbry/schema/url.py
interface UrlOptions {
/**
* Whether or not to encodeURIComponent the path segments.
* Doing so is a workaround such that browsers interpret it as a valid URL in a way that the desktop app understands.
*/
encode?: boolean
}
const invalidNamesRegex = /[^=&#:$@%*?;\"/\\<>%{}|^~`\[\]\u0000-\u0020\uD800-\uDFFF\uFFFE-\uFFFF]+/.source;
/** Creates a named regex group */
const named = (name: string, regex: string) => `(?<${name}>${regex})`;
/** Creates a non-capturing group */
const group = (regex: string) => `(?:${regex})`;
/** Allows for one of the patterns */
const oneOf = (...choices: string[]) => group(choices.join('|'));
/** Create an lbry url claim */
const claim = (name: string, prefix = '') => group(
named(`${name}_name`, prefix + invalidNamesRegex)
+ oneOf(
group(':' + named(`${name}_claim_id`, '[0-9a-f]{1,40}')),
group('\\*' + named(`${name}_sequence`, '[1-9][0-9]*')),
group('\\$' + named(`${name}_amount_order`, '[1-9][0-9]*'))
) + '?'
);
/** Create an lbry url claim, but use the old pattern for claims */
const legacyClaim = (name: string, prefix = '') => group(
named(`${name}_name`, prefix + invalidNamesRegex)
+ oneOf(
group('#' + named(`${name}_claim_id`, '[0-9a-f]{1,40}')),
group(':' + named(`${name}_sequence`, '[1-9][0-9]*')),
group('\\$' + named(`${name}_amount_order`, '[1-9][0-9]*'))
) + '?');
export const builder = { named, group, oneOf, claim, legacyClaim, invalidNamesRegex };
/** Creates a pattern to parse lbry protocol URLs. Unused, but I left it here. */
function createProtocolUrlRegex(legacy = false) {
const claim = legacy ? builder.legacyClaim : builder.claim;
return new RegExp('^' + named('scheme', 'lbry://') + '?' + oneOf(
group(claim('channel_with_stream', '@') + '/' + claim('stream_in_channel')),
claim('channel', '@'),
claim('stream'),
) + '$');
}
/** Creates a pattern to match lbry.tv style sites by their pathname */
function createWebUrlRegex(legacy = false) {
const claim = legacy ? builder.legacyClaim : builder.claim;
return new RegExp('^/' + oneOf(
group(claim('channel_with_stream', '@') + '/' + claim('stream_in_channel')),
claim('channel', '@'),
claim('stream'),
) + '$');
}
/** Pattern for lbry.tv style sites */
export const URL_REGEX = createWebUrlRegex();
export const PROTOCOL_URL_REGEX = createProtocolUrlRegex();
const PROTOCOL_URL_REGEX_LEGACY = createProtocolUrlRegex(true);
/**
* Encapsulates a lbry url path segment.
* Handles `StreamClaimNameAndModifier' and `ChannelClaimNameAndModifier`
*/
export class PathSegment {
constructor(public name: string,
public claimID?: string,
public sequence?: number,
public amountOrder?: number) { }
static fromMatchGroup(segment: string, groups: Record<string, string>) {
return new PathSegment(
groups[`${segment}_name`],
groups[`${segment}_claim_id`],
parseInt(groups[`${segment}_sequence`]),
parseInt(groups[`${segment}_amount_order`])
);
}
/** Prints the segment */
toString() {
if (this.claimID) return `${this.name}:${this.claimID}`;
if (this.sequence) return `${this.name}*${this.sequence}`;
if (this.amountOrder) return `${this.name}$${this.amountOrder}`;
return this.name;
}
}
/**
* Utility function
*
* @param ptn pattern to use; specific to the patterns defined in this file
* @param url the url to try to parse
* @returns an array of path segments; if invalid, will return an empty array
*/
function patternSegmenter(ptn: RegExp, url: string, options: UrlOptions = { encode: false }): string[] {
const match = url.match(ptn)?.groups;
if (!match) return [];
const segments = match['channel_name'] ? ['channel']
: match['channel_with_stream_name'] ? ['channel_with_stream', 'stream_in_channel']
: match['stream_name'] ? ['stream']
: null;
if (!segments) throw new Error(`${url} matched the overall pattern, but could not determine type`);
return segments.map(s => PathSegment.fromMatchGroup(s, match).toString())
.map(s => options.encode ? encodeURIComponent(s) : s);
}
/**
* Produces the lbry protocl URL from the frontend URL
*
* @param url lbry frontend URL
* @param options options for the redirect
*/
export function appRedirectUrl(url: string, options?: UrlOptions): string | undefined {
const segments = patternSegmenter(URL_REGEX, new URL(url).pathname, options);
if (segments.length === 0) return;
const path = segments.join('/');
return `lbry://${path}`;
}
/**
* Parses a lbry protocol and returns its constituent path segments. Attempts the spec compliant and then the old URL schemes.
*
* @param url the lbry url
* @returns an array of path segments; if invalid, will return an empty array
*/
export function parseProtocolUrl(url: string, options: UrlOptions = { encode: false }): string[] {
for (const ptn of [PROTOCOL_URL_REGEX, PROTOCOL_URL_REGEX_LEGACY]) {
const segments = patternSegmenter(ptn, url, options);
if (segments.length === 0) continue;
return segments;
}
return [];
}

View file

@ -1,54 +1,43 @@
import { getSettingsAsync, redirectDomains } from "../common/settings"; import { appRedirectUrl, parseProtocolUrl } from '../common/lbry-url';
import { ytService } from "../common/yt"; import { getSettingsAsync, redirectDomains } from '../common/settings';
import { ytService } from '../common/yt';
function openApp(tabId: number, url: string) {
chrome.tabs.update(tabId, { url });
alert('Opened link in LBRY App!'); // Better for UX since sometimes LBRY App doesn't take focus, if that is fixed, this can be removed
// Close tab if it lacks history and go back if it does
chrome.tabs.executeScript(tabId, {
code: `if (window.history.length === 1) {
window.close();
} else {
window.history.back();
}`
});
}
async function resolveYT(ytUrl: string) {
const descriptor = ytService.getId(ytUrl);
if (!descriptor) return; // can't parse YT url; may not be one
const lbryProtocolUrl: string | null = await ytService.resolveById(descriptor).then(a => a[0]);
const segments = parseProtocolUrl(lbryProtocolUrl || '', { encode: true });
if (segments.length === 0) return;
return segments.join('/');
}
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, { url: tabUrl }) => { chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, { url: tabUrl }) => {
const { enabled, redirect } = await getSettingsAsync('enabled', 'redirect'); const { enabled, redirect } = await getSettingsAsync('enabled', 'redirect');
const urlPrefix = redirectDomains[redirect].prefix; const urlPrefix = redirectDomains[redirect].prefix;
if (!enabled || !changeInfo.url || !tabUrl) return; if (!enabled || !changeInfo.url || !tabUrl) return;
if (tabUrl.match(/\b(https:\/\/lbry.tv|lbry:\/\/)/g)) {
var redirectTo; const url = tabUrl.match(/\b(https:\/\/lbry.tv|lbry:\/\/)/g) ? appRedirectUrl(tabUrl, { encode: true })
if (redirect === "app") { : await resolveYT(tabUrl);
let isChannel = tabUrl.match(/^(https|http):\/\/lbry.tv\/@([^?:$#@;/"<>%{}|^~[\]`])+?:[a-z0-9]{1,40}($|(?=\?))/g);
let isClaim = tabUrl.match(/^(https|http):\/\/lbry.tv\/@([^?:$#@;/"<>%{}|^~[\]`])+?:[a-z0-9]{1,40}\/([^?:$#@;/"<>%{}|^~[\]`])+?:[a-z0-9]{1,40}($|(?=\?))/g);
if (isChannel) { if (!url) return;
redirectTo = `lbry://${tabUrl.match(/@([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)![0]}#${tabUrl.match(/#([a-z0-9]{40})|:[a-z0-9]($|(?=\?))/g)![0].substr(1)}`; if (redirect === 'app') {
} else if (isClaim) { openApp(tabId, urlPrefix + url);
redirectTo = `lbry://${tabUrl.match(/@([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)}#${tabUrl.match(/(#([a-z0-9]{40})|:[a-z0-9])(?=\/([^$#@;/"<>%{}|^~[\]`])+?(#([a-z0-9]{40})|:[a-z0-9])($|(?=\?)))/g)![0].substr(1)}${tabUrl.match(/\/([^$#@;/"<>%{}|^~[\]`])+?(?=[#:])/g)![0]}#${tabUrl.match(/(#([a-z0-9]{40})|:[a-z0-9])($|(?=\?))/g)![0].substr(1)}`;
}
}
if (redirectTo) {
chrome.tabs.update(tabId, { url: redirectTo + "?src=watch-on-lbry" });
if (redirect === "app") {
alert("Opened link in LBRY App!"); // Better for UX since sometimes LBRY App doesn't take focus, if that is fixed, this can be removed
// Close tab if it lacks history and go back if it does
chrome.tabs.executeScript(tabId, {
code: `
if (window.history.length === 1) {
window.close();
} else {
window.history.back();
}`
});
}
}
return; return;
} }
const descriptor = ytService.getId(tabUrl); chrome.tabs.executeScript(tabId, { code: `location.replace("${urlPrefix + url}")` });
if (!descriptor) return;
const title = (await ytService.resolveById(descriptor))[0]
if (!title) return;
console.log(title);
let newUrl;
if (redirect === "lbry.tv") {
newUrl = `${urlPrefix}${title.replace(/^lbry:\/\//, "").replace(/#/g, ":")}?src=watch-on-lbry`;
} else if (redirect === "app") {
newUrl = `lbry://${title.replace(/^lbry:\/\//, "")}`;
}
chrome.tabs.update(tabId, { url: newUrl });
}); });