@nordcraft/core 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/url.d.ts +4 -1
- package/dist/utils/url.js +3 -3
- package/dist/utils/url.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/url.test.ts +22 -3
- package/src/utils/url.ts +9 -4
package/dist/utils/url.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export declare const isLocalhostUrl: (hrefOrOrigin: string) => boolean;
|
|
2
2
|
export declare const isLocalhostHostname: (hostname: string) => hostname is "localhost" | "127.0.0.1";
|
|
3
|
-
export declare const validateUrl: (
|
|
3
|
+
export declare const validateUrl: ({ path, origin, }: {
|
|
4
|
+
path: string | null | undefined;
|
|
5
|
+
origin: string | undefined;
|
|
6
|
+
}) => false | URL;
|
|
4
7
|
export declare const PROXY_URL_HEADER = "x-toddle-url";
|
|
5
8
|
export declare const REWRITE_HEADER = "x-toddle-rewrite";
|
package/dist/utils/url.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const LOCALHOSTS = ['http://localhost:54404', 'http://preview.localhost:54404'];
|
|
2
2
|
export const isLocalhostUrl = (hrefOrOrigin) => LOCALHOSTS.some((host) => hrefOrOrigin.startsWith(host));
|
|
3
3
|
export const isLocalhostHostname = (hostname) => hostname === 'localhost' || hostname === '127.0.0.1';
|
|
4
|
-
export const validateUrl = (
|
|
5
|
-
if (typeof
|
|
4
|
+
export const validateUrl = ({ path, origin, }) => {
|
|
5
|
+
if (typeof path !== 'string') {
|
|
6
6
|
return false;
|
|
7
7
|
}
|
|
8
8
|
try {
|
|
9
|
-
const urlObject = new URL(
|
|
9
|
+
const urlObject = new URL(path, origin);
|
|
10
10
|
// Creating a new URL object will not correctly encode the search params
|
|
11
11
|
// So we need to iterate over them to make sure they are encoded as that happens when setting them explicitly
|
|
12
12
|
const searchCopy = new URLSearchParams(urlObject.searchParams);
|
package/dist/utils/url.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.js","sourceRoot":"","sources":["../../src/utils/url.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,CAAC,wBAAwB,EAAE,gCAAgC,CAAC,CAAA;AAE/E,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,YAAoB,EAAE,EAAE,CACrD,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAA;AAE1D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAgB,EAAE,EAAE,CACtD,QAAQ,KAAK,WAAW,IAAI,QAAQ,KAAK,WAAW,CAAA;AAEtD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"url.js","sourceRoot":"","sources":["../../src/utils/url.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,CAAC,wBAAwB,EAAE,gCAAgC,CAAC,CAAA;AAE/E,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,YAAoB,EAAE,EAAE,CACrD,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAA;AAE1D,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAgB,EAAE,EAAE,CACtD,QAAQ,KAAK,WAAW,IAAI,QAAQ,KAAK,WAAW,CAAA;AAEtD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,EAC1B,IAAI,EACJ,MAAM,GAIP,EAAE,EAAE;IACH,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAA;IACd,CAAC;IACD,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QACvC,wEAAwE;QACxE,6GAA6G;QAC7G,MAAM,UAAU,GAAG,IAAI,eAAe,CAAC,SAAS,CAAC,YAAY,CAAC,CAAA;QAC9D,UAAU,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAChC,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QAC3C,CAAC,CAAC,CAAA;QACF,UAAU,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAChC,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QAC3C,CAAC,CAAC,CAAA;QACF,OAAO,SAAS,CAAA;IAClB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,cAAc,CAAA;AAE9C,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAA"}
|
package/package.json
CHANGED
package/src/utils/url.test.ts
CHANGED
|
@@ -3,15 +3,34 @@ import { validateUrl } from './url'
|
|
|
3
3
|
|
|
4
4
|
describe('validateUrl()', () => {
|
|
5
5
|
test('it validates urls correctly', () => {
|
|
6
|
-
expect(
|
|
7
|
-
|
|
6
|
+
expect(
|
|
7
|
+
validateUrl({ path: 'https://nordcraft.com', origin: undefined }),
|
|
8
|
+
).toBeInstanceOf(URL)
|
|
9
|
+
expect(validateUrl({ path: 'not-a-url', origin: undefined })).toBe(false)
|
|
8
10
|
})
|
|
9
11
|
|
|
10
12
|
test('it validates urls arrays in query params correctly', async () => {
|
|
11
|
-
const url = validateUrl(
|
|
13
|
+
const url = validateUrl({
|
|
14
|
+
path: 'https://nordcraft.com?test=1&test=2',
|
|
15
|
+
origin: undefined,
|
|
16
|
+
})
|
|
12
17
|
expect(url).toBeInstanceOf(URL)
|
|
13
18
|
if (url instanceof URL) {
|
|
14
19
|
expect(url.searchParams.getAll('test')).toEqual(['1', '2'])
|
|
15
20
|
}
|
|
16
21
|
})
|
|
22
|
+
|
|
23
|
+
test('it validates relative urls when a base is provided', () => {
|
|
24
|
+
const url = validateUrl({
|
|
25
|
+
path: '/my-path',
|
|
26
|
+
origin: 'https://nordcraft.com',
|
|
27
|
+
})
|
|
28
|
+
expect(url).toBeInstanceOf(URL)
|
|
29
|
+
expect((url as URL).href).toBe('https://nordcraft.com/my-path')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test("it doesn't accept relative urls when no base is provided", () => {
|
|
33
|
+
const url = validateUrl({ path: '/my-path', origin: undefined })
|
|
34
|
+
expect(url).toBe(false)
|
|
35
|
+
})
|
|
17
36
|
})
|
package/src/utils/url.ts
CHANGED
|
@@ -6,13 +6,18 @@ export const isLocalhostUrl = (hrefOrOrigin: string) =>
|
|
|
6
6
|
export const isLocalhostHostname = (hostname: string) =>
|
|
7
7
|
hostname === 'localhost' || hostname === '127.0.0.1'
|
|
8
8
|
|
|
9
|
-
export const validateUrl = (
|
|
10
|
-
|
|
9
|
+
export const validateUrl = ({
|
|
10
|
+
path,
|
|
11
|
+
origin,
|
|
12
|
+
}: {
|
|
13
|
+
path: string | null | undefined
|
|
14
|
+
origin: string | undefined
|
|
15
|
+
}) => {
|
|
16
|
+
if (typeof path !== 'string') {
|
|
11
17
|
return false
|
|
12
18
|
}
|
|
13
|
-
|
|
14
19
|
try {
|
|
15
|
-
const urlObject = new URL(
|
|
20
|
+
const urlObject = new URL(path, origin)
|
|
16
21
|
// Creating a new URL object will not correctly encode the search params
|
|
17
22
|
// So we need to iterate over them to make sure they are encoded as that happens when setting them explicitly
|
|
18
23
|
const searchCopy = new URLSearchParams(urlObject.searchParams)
|