@esmx/router 3.0.0-rc.13 → 3.0.0-rc.14

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.
@@ -1,18 +1,6 @@
1
1
  import { describe, it } from 'vitest';
2
2
 
3
- import {
4
- isPathWithProtocolOrDomain,
5
- normalizeLocation,
6
- normalizePath
7
- } from './path';
8
-
9
- // console.log(normalizeLocation({ path: '/' }));
10
-
11
- // console.log(isPathWithProtocolOrDomain('/'));
12
- // console.log(isPathWithProtocolOrDomain('www.a.b'));
13
- // console.log(isPathWithProtocolOrDomain('a.b.cm'));
14
- // console.log(isPathWithProtocolOrDomain('http://a.cn'));
15
- // console.log(isPathWithProtocolOrDomain('a.b/path'));
3
+ import { normalizeLocation, normalizePath } from './path';
16
4
 
17
5
  describe('testing normalizeLocation', () => {
18
6
  it('testing normal domain', ({ expect }) => {
package/src/utils/path.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import normalizeUrl from 'normalize-url';
1
2
  import URLParse from 'url-parse';
2
3
 
3
4
  import type {
@@ -15,7 +16,7 @@ import {
15
16
  encodeQueryValue
16
17
  } from './encoding';
17
18
  import { isValidValue } from './utils';
18
- import { warn } from './warn';
19
+ import { assert, warn } from './warn';
19
20
 
20
21
  /**
21
22
  * 判断路径是否以 http 或 https 开头 或者直接是域名开头
@@ -270,7 +271,10 @@ export function normalizeLocation(
270
271
  /**
271
272
  * 判断路径是否以协议或域名开头
272
273
  */
273
- export function isPathWithProtocolOrDomain(location: RouterRawLocation): {
274
+ export function isPathWithProtocolOrDomain(
275
+ location: RouterRawLocation,
276
+ base: RouterBase = ''
277
+ ): {
274
278
  /**
275
279
  * 是否以协议或域名开头
276
280
  */
@@ -282,6 +286,17 @@ export function isPathWithProtocolOrDomain(location: RouterRawLocation): {
282
286
  } {
283
287
  let url = '';
284
288
  let state = {};
289
+ let baseString = '';
290
+ if (typeof base === 'string') {
291
+ baseString = base;
292
+ } else {
293
+ baseString = base({
294
+ fullPath: '',
295
+ query: {},
296
+ queryArray: {},
297
+ hash: ''
298
+ });
299
+ }
285
300
 
286
301
  if (typeof location === 'string') {
287
302
  url = location;
@@ -303,51 +318,56 @@ export function isPathWithProtocolOrDomain(location: RouterRawLocation): {
303
318
  });
304
319
  }
305
320
 
306
- // 如果以 scheme 协议开头 并且不是 http(s) 协议开头 则认为是外站跳转
307
- if (regexScheme.test(url) && !regexHttpScheme.test(url)) {
321
+ try {
322
+ url = normalizeUrl(url, {
323
+ stripWWW: false,
324
+ removeQueryParameters: false
325
+ });
326
+ } catch (error) {
308
327
  try {
309
- const {
310
- hash,
311
- host,
312
- hostname,
313
- href,
314
- origin,
315
- pathname,
316
- port,
317
- protocol,
318
- search
319
- } = new URL(url);
320
- const route: Route = {
321
- hash,
322
- host,
323
- hostname,
324
- href,
325
- origin,
326
- pathname,
327
- port,
328
- protocol,
329
- search,
330
- params: {},
331
- query: {},
332
- queryArray: {},
333
- state,
334
- meta: {},
335
- path: pathname,
336
- fullPath: url,
337
- base: '',
338
- matched: []
339
- };
340
- return {
341
- flag: true,
342
- route
343
- };
328
+ url = new URL(url, baseString).href;
344
329
  } catch (error) {
345
- warn(error);
330
+ assert(false, `Invalid URL: ${url}`);
346
331
  }
347
332
  }
348
333
 
349
- if (!/^https?:\/\//i.test(url)) {
350
- url = `http://${url}`;
334
+ // 如果以 scheme 协议开头 并且不是 http(s) 协议开头 则认为是外站跳转
335
+ if (regexScheme.test(url) && !regexHttpScheme.test(url)) {
336
+ const {
337
+ hash,
338
+ host,
339
+ hostname,
340
+ href,
341
+ origin,
342
+ pathname,
343
+ port,
344
+ protocol,
345
+ search
346
+ } = new URL(url);
347
+ const route: Route = {
348
+ hash,
349
+ host,
350
+ hostname,
351
+ href,
352
+ origin,
353
+ pathname,
354
+ port,
355
+ protocol,
356
+ search,
357
+ params: {},
358
+ query: {},
359
+ queryArray: {},
360
+ state,
361
+ meta: {},
362
+ path: pathname,
363
+ fullPath: url,
364
+ base: '',
365
+ matched: []
366
+ };
367
+ return {
368
+ flag: true,
369
+ route
370
+ };
351
371
  }
352
372
 
353
373
  const {