fix: normalize prefix when parsing. fixes #9081

This commit is contained in:
Satyajit Sahoo
2021-01-22 13:38:51 +01:00
parent 35747a6066
commit 4ca2d2d22b
3 changed files with 352 additions and 24 deletions

View File

@@ -0,0 +1,318 @@
import extractPathFromURL from '../extractPathFromURL';
it('extracts path from URL with protocol', () => {
expect(extractPathFromURL(['scheme://'], 'scheme://some/path')).toBe(
'some/path'
);
expect(extractPathFromURL(['scheme://'], 'scheme:some/path')).toBe(
'some/path'
);
expect(extractPathFromURL(['scheme://'], 'scheme:///some/path')).toBe(
'some/path'
);
expect(extractPathFromURL(['scheme:///'], 'scheme:some/path')).toBe(
'some/path'
);
expect(extractPathFromURL(['scheme:'], 'scheme:some/path')).toBe('some/path');
expect(extractPathFromURL(['scheme:'], 'scheme://some/path')).toBe(
'some/path'
);
expect(extractPathFromURL(['scheme:'], 'scheme:///some/path')).toBe(
'some/path'
);
});
it('extracts path from URL with protocol and host', () => {
expect(
extractPathFromURL(
['scheme://example.com'],
'scheme://example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(['scheme://example.com'], 'scheme:example.com/some/path')
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme://example.com'],
'scheme:///example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:///example.com'],
'scheme:example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(['scheme:example.com'], 'scheme:example.com/some/path')
).toBe('/some/path');
expect(
extractPathFromURL(['scheme:example.com'], 'scheme://example.com/some/path')
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com'],
'scheme:///example.com/some/path'
)
).toBe('/some/path');
});
it('extracts path from URL with protocol and host with wildcard', () => {
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme://test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme:test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme:///test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:///*.example.com'],
'scheme:test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme:test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme://test.example.com/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme:///test.example.com/some/path'
)
).toBe('/some/path');
});
it('extracts path from URL with protocol, host and path', () => {
expect(
extractPathFromURL(
['scheme://example.com/test'],
'scheme://example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(['scheme://example.com'], 'scheme:example.com/some/path')
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme://example.com/test'],
'scheme:///example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:///example.com/test'],
'scheme:example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme:example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme://example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme:///example.com/test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme:///example.com//test/some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme:///example.com/test//some/path'
)
).toBe('/some/path');
expect(
extractPathFromURL(
['scheme:example.com/test'],
'scheme:///example.com/test/some//path'
)
).toBe('/some/path');
});
it('returns undefined for non-matching protocol', () => {
expect(extractPathFromURL(['scheme://'], 'foo://some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme://'], 'foo:some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme://'], 'foo:///some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme:///'], 'foo:some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme:'], 'foo:some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme:'], 'foo://some/path')).toBe(undefined);
expect(extractPathFromURL(['scheme:'], 'foo:///some/path')).toBe(undefined);
});
it('returns undefined for non-matching path', () => {
expect(extractPathFromURL(['scheme://foo'], 'scheme://some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme://foo'], 'scheme:some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme://foo'], 'scheme:///some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme:///foo'], 'scheme:some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme:foo'], 'scheme:some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme:foo'], 'scheme://some/path')).toBe(
undefined
);
expect(extractPathFromURL(['scheme:foo'], 'scheme:///some/path')).toBe(
undefined
);
});
it('returns undefined for non-matching host', () => {
expect(
extractPathFromURL(['scheme://example.com'], 'scheme://foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme://example.com'], 'scheme:foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme://example.com'], 'scheme:///foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme:///example.com'], 'scheme:foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme:example.com'], 'scheme:foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme:example.com'], 'scheme://foo.com/some/path')
).toBe(undefined);
expect(
extractPathFromURL(['scheme:example.com'], 'scheme:///foo.com/some/path')
).toBe(undefined);
});
it('returns undefined for non-matching host with wildcard', () => {
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme://test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme:test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme://*.example.com'],
'scheme:///test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme:///*.example.com'],
'scheme:test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme:test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme://test.foo.com/some/path'
)
).toBe(undefined);
expect(
extractPathFromURL(
['scheme:*.example.com'],
'scheme:///test.foo.com/some/path'
)
).toBe(undefined);
});

View File

@@ -0,0 +1,26 @@
import escapeStringRegexp from 'escape-string-regexp';
export default function extractPathFromURL(prefixes: string[], url: string) {
for (const prefix of prefixes) {
const protocol = prefix.match(/^[^:]+:/)?.[0] ?? '';
const host = prefix
.replace(new RegExp(`^${escapeStringRegexp(protocol)}`), '')
.replace(/\/+/g, '/') // Replace multiple slash (//) with single ones
.replace(/^\//, ''); // Remove extra leading slash
const prefixRegex = new RegExp(
`^${escapeStringRegexp(protocol)}(/)*${host
.split('.')
.map((it) => (it === '*' ? '[^/]+' : escapeStringRegexp(it)))
.join('\\.')}`
);
const normalizedURL = url.replace(/\/+/g, '/');
if (prefixRegex.test(normalizedURL)) {
return normalizedURL.replace(prefixRegex, '');
}
}
return undefined;
}

View File

@@ -5,7 +5,7 @@ import {
getStateFromPath as getStateFromPathDefault,
NavigationContainerRef,
} from '@react-navigation/core';
import escapeStringRegexp from 'escape-string-regexp';
import extractPathFromURL from './extractPathFromURL';
import type { LinkingOptions } from './types';
type ResultState = ReturnType<typeof getStateFromPathDefault>;
@@ -80,24 +80,6 @@ export default function useLinking(
getActionFromStateRef.current = getActionFromState;
});
const extractPathFromURL = React.useCallback((url: string) => {
for (const prefix of prefixesRef.current) {
const protocol = prefix.match(/^[^:]+:\/\//)?.[0] ?? '';
const host = prefix.replace(protocol, '');
const prefixRegex = new RegExp(
`^${escapeStringRegexp(protocol)}${host
.split('.')
.map((it) => (it === '*' ? '[^/]+' : escapeStringRegexp(it)))
.join('\\.')}`
);
if (prefixRegex.test(url)) {
return url.replace(prefixRegex, '');
}
}
return undefined;
}, []);
const getInitialState = React.useCallback(() => {
let state: ResultState | undefined;
@@ -106,7 +88,9 @@ export default function useLinking(
if (url != null && typeof url !== 'string') {
return url.then((url) => {
const path = url ? extractPathFromURL(url) : null;
const path = url
? extractPathFromURL(prefixesRef.current, url)
: null;
return path
? getStateFromPathRef.current(path, configRef.current)
@@ -114,7 +98,7 @@ export default function useLinking(
});
}
const path = url ? extractPathFromURL(url) : null;
const path = url ? extractPathFromURL(prefixesRef.current, url) : null;
state = path
? getStateFromPathRef.current(path, configRef.current)
@@ -131,7 +115,7 @@ export default function useLinking(
};
return thenable as PromiseLike<ResultState | undefined>;
}, [extractPathFromURL]);
}, []);
React.useEffect(() => {
const listener = (url: string) => {
@@ -139,7 +123,7 @@ export default function useLinking(
return;
}
const path = extractPathFromURL(url);
const path = extractPathFromURL(prefixesRef.current, url);
const navigation = ref.current;
if (navigation && path) {
@@ -182,7 +166,7 @@ export default function useLinking(
};
return subscribe(listener);
}, [enabled, ref, subscribe, extractPathFromURL]);
}, [enabled, ref, subscribe]);
return {
getInitialState,