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

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.
@@ -22,7 +22,7 @@ export declare class AbstractHistory extends BaseRouterHistory {
22
22
  */
23
23
  replaceWindow(location: RouterRawLocation): Promise<void>;
24
24
  jump(location: RouterRawLocation, replace?: boolean): Promise<void>;
25
- private _jump;
25
+ _jump(location: RouterRawLocation, replace?: boolean, isTriggerWithWindow?: boolean): Promise<void>;
26
26
  go(delta: number): void;
27
27
  forward(): void;
28
28
  back(): void;
@@ -7,7 +7,6 @@ export class AbstractHistory extends BaseRouterHistory {
7
7
  super(router);
8
8
  this.index = -1;
9
9
  this.stack = [];
10
- this.init();
11
10
  }
12
11
  async init({ replace } = { replace: true }) {
13
12
  const { initUrl } = this.router.options;
@@ -35,7 +34,8 @@ export class AbstractHistory extends BaseRouterHistory {
35
34
  }
36
35
  // 处理外站跳转逻辑
37
36
  handleOutside(location, replace = false, isTriggerWithWindow = false) {
38
- const { flag, route } = isPathWithProtocolOrDomain(location);
37
+ const base = this.router.base;
38
+ const { flag, route } = isPathWithProtocolOrDomain(location, base);
39
39
  if (!flag) {
40
40
  return false;
41
41
  }
@@ -1,4 +1,4 @@
1
- import { type RouterInstance, type RouterRawLocation } from '../types';
1
+ import type { RouterInstance, RouterRawLocation } from '../types';
2
2
  import { BaseRouterHistory } from './base';
3
3
  export declare class HtmlHistory extends BaseRouterHistory {
4
4
  constructor(router: RouterInstance);
@@ -94,7 +94,8 @@ export class HtmlHistory extends BaseRouterHistory {
94
94
  }
95
95
  // 处理外站跳转逻辑
96
96
  handleOutside(location, replace = false, isTriggerWithWindow = false) {
97
- const { flag, route } = isPathWithProtocolOrDomain(location);
97
+ const base = this.router.base;
98
+ const { flag, route } = isPathWithProtocolOrDomain(location, base);
98
99
  const router = this.router;
99
100
  const { handleOutside, validateOutside } = router.options;
100
101
  const isSameHost = !flag || window.location.hostname === route.hostname;
@@ -103,13 +104,14 @@ export class HtmlHistory extends BaseRouterHistory {
103
104
  return false;
104
105
  }
105
106
  }
106
- if (handleOutside == null ? void 0 : handleOutside({
107
+ const res = handleOutside == null ? void 0 : handleOutside({
107
108
  router,
108
109
  route,
109
110
  replace,
110
111
  isTriggerWithWindow,
111
112
  isSameHost
112
- })) {
113
+ });
114
+ if (res === false) {
113
115
  return true;
114
116
  }
115
117
  if (replace) {
@@ -48,7 +48,7 @@ export declare function normalizeLocation(rawLocation: RouterRawLocation, base?:
48
48
  /**
49
49
  * 判断路径是否以协议或域名开头
50
50
  */
51
- export declare function isPathWithProtocolOrDomain(location: RouterRawLocation): {
51
+ export declare function isPathWithProtocolOrDomain(location: RouterRawLocation, base?: RouterBase): {
52
52
  /**
53
53
  * 是否以协议或域名开头
54
54
  */
@@ -1,3 +1,4 @@
1
+ import normalizeUrl from "normalize-url";
1
2
  import URLParse from "url-parse";
2
3
  import {
3
4
  decode,
@@ -7,7 +8,7 @@ import {
7
8
  encodeQueryValue
8
9
  } from "./encoding.mjs";
9
10
  import { isValidValue } from "./utils.mjs";
10
- import { warn } from "./warn.mjs";
11
+ import { assert } from "./warn.mjs";
11
12
  export const regexDomain = /^(?:https?:\/\/|[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9](\/.*)?/i;
12
13
  export const regexScheme = /^(?:[a-z][a-z\d+.-]*:.+)/i;
13
14
  export const regexHttpScheme = /^(http(s)?:\/\/)/;
@@ -153,9 +154,20 @@ export function normalizeLocation(rawLocation, base = "") {
153
154
  if (params) res.params = params;
154
155
  return res;
155
156
  }
156
- export function isPathWithProtocolOrDomain(location) {
157
+ export function isPathWithProtocolOrDomain(location, base = "") {
157
158
  let url = "";
158
159
  let state = {};
160
+ let baseString = "";
161
+ if (typeof base === "string") {
162
+ baseString = base;
163
+ } else {
164
+ baseString = base({
165
+ fullPath: "",
166
+ query: {},
167
+ queryArray: {},
168
+ hash: ""
169
+ });
170
+ }
159
171
  if (typeof location === "string") {
160
172
  url = location;
161
173
  } else {
@@ -175,49 +187,54 @@ export function isPathWithProtocolOrDomain(location) {
175
187
  hash: hash2
176
188
  });
177
189
  }
178
- if (regexScheme.test(url) && !regexHttpScheme.test(url)) {
190
+ try {
191
+ url = normalizeUrl(url, {
192
+ stripWWW: false,
193
+ removeQueryParameters: false
194
+ });
195
+ } catch (error) {
179
196
  try {
180
- const {
181
- hash: hash2,
182
- host: host2,
183
- hostname: hostname2,
184
- href: href2,
185
- origin: origin2,
186
- pathname: pathname2,
187
- port: port2,
188
- protocol: protocol2,
189
- search: search2
190
- } = new URL(url);
191
- const route2 = {
192
- hash: hash2,
193
- host: host2,
194
- hostname: hostname2,
195
- href: href2,
196
- origin: origin2,
197
- pathname: pathname2,
198
- port: port2,
199
- protocol: protocol2,
200
- search: search2,
201
- params: {},
202
- query: {},
203
- queryArray: {},
204
- state,
205
- meta: {},
206
- path: pathname2,
207
- fullPath: url,
208
- base: "",
209
- matched: []
210
- };
211
- return {
212
- flag: true,
213
- route: route2
214
- };
215
- } catch (error) {
216
- warn(error);
197
+ url = new URL(url, baseString).href;
198
+ } catch (error2) {
199
+ assert(false, `Invalid URL: ${url}`);
217
200
  }
218
201
  }
219
- if (!/^https?:\/\//i.test(url)) {
220
- url = `http://${url}`;
202
+ if (regexScheme.test(url) && !regexHttpScheme.test(url)) {
203
+ const {
204
+ hash: hash2,
205
+ host: host2,
206
+ hostname: hostname2,
207
+ href: href2,
208
+ origin: origin2,
209
+ pathname: pathname2,
210
+ port: port2,
211
+ protocol: protocol2,
212
+ search: search2
213
+ } = new URL(url);
214
+ const route2 = {
215
+ hash: hash2,
216
+ host: host2,
217
+ hostname: hostname2,
218
+ href: href2,
219
+ origin: origin2,
220
+ pathname: pathname2,
221
+ port: port2,
222
+ protocol: protocol2,
223
+ search: search2,
224
+ params: {},
225
+ query: {},
226
+ queryArray: {},
227
+ state,
228
+ meta: {},
229
+ path: pathname2,
230
+ fullPath: url,
231
+ base: "",
232
+ matched: []
233
+ };
234
+ return {
235
+ flag: true,
236
+ route: route2
237
+ };
221
238
  }
222
239
  const {
223
240
  hash,
@@ -1,8 +1,5 @@
1
1
  import { describe, it } from "vitest";
2
- import {
3
- normalizeLocation,
4
- normalizePath
5
- } from "./path.mjs";
2
+ import { normalizeLocation, normalizePath } from "./path.mjs";
6
3
  describe("testing normalizeLocation", () => {
7
4
  it("testing normal domain", ({ expect }) => {
8
5
  expect(
package/package.json CHANGED
@@ -28,12 +28,13 @@
28
28
  }
29
29
  ],
30
30
  "dependencies": {
31
+ "normalize-url": "^8.0.1",
31
32
  "path-to-regexp": "^6.2.2",
32
33
  "url-parse": "^1.5.10"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@biomejs/biome": "1.9.4",
36
- "@esmx/lint": "3.0.0-rc.13",
37
+ "@esmx/lint": "3.0.0-rc.16",
37
38
  "@gez/lint": "3.0.0-rc.9",
38
39
  "@types/node": "22.13.10",
39
40
  "@types/url-parse": "^1.4.11",
@@ -43,7 +44,7 @@
43
44
  "unbuild": "2.0.0",
44
45
  "vitest": "3.0.8"
45
46
  },
46
- "version": "3.0.0-rc.13",
47
+ "version": "3.0.0-rc.16",
47
48
  "type": "module",
48
49
  "private": false,
49
50
  "exports": {
@@ -62,5 +63,5 @@
62
63
  "template",
63
64
  "public"
64
65
  ],
65
- "gitHead": "801082f89364db5e1137431e3cc1121f689fa7ca"
66
+ "gitHead": "4d3c88e6751289b4045bffd496859afeb52a5901"
66
67
  }
@@ -15,7 +15,6 @@ export class AbstractHistory extends BaseRouterHistory {
15
15
  super(router);
16
16
  this.index = -1;
17
17
  this.stack = [];
18
- this.init();
19
18
  }
20
19
 
21
20
  async init({ replace }: { replace?: boolean } = { replace: true }) {
@@ -55,7 +54,8 @@ export class AbstractHistory extends BaseRouterHistory {
55
54
  replace = false,
56
55
  isTriggerWithWindow = false
57
56
  ) {
58
- const { flag, route } = isPathWithProtocolOrDomain(location);
57
+ const base = this.router.base;
58
+ const { flag, route } = isPathWithProtocolOrDomain(location, base);
59
59
  if (!flag) {
60
60
  // 如果不以域名开头则跳出
61
61
  return false;
@@ -111,7 +111,7 @@ export class AbstractHistory extends BaseRouterHistory {
111
111
  await this._jump(location, replace);
112
112
  }
113
113
 
114
- private async _jump(
114
+ async _jump(
115
115
  location: RouterRawLocation,
116
116
  replace = false,
117
117
  isTriggerWithWindow = false
@@ -1,8 +1,4 @@
1
- import {
2
- type RouterInstance,
3
- type RouterRawLocation,
4
- StateLayerConfigKey
5
- } from '../types';
1
+ import type { RouterInstance, RouterRawLocation } from '../types';
6
2
  import {
7
3
  computeScrollPosition,
8
4
  getKeepScrollPosition,
@@ -119,7 +115,8 @@ export class HtmlHistory extends BaseRouterHistory {
119
115
  // 是否是 pushWindow/replaceWindow 触发的
120
116
  isTriggerWithWindow = false
121
117
  ) {
122
- const { flag, route } = isPathWithProtocolOrDomain(location);
118
+ const base = this.router.base;
119
+ const { flag, route } = isPathWithProtocolOrDomain(location, base);
123
120
  const router = this.router;
124
121
  const { handleOutside, validateOutside } = router.options;
125
122
 
@@ -136,15 +133,15 @@ export class HtmlHistory extends BaseRouterHistory {
136
133
 
137
134
  // 如果有配置跳转外站函数,则执行配置函数
138
135
  // 如果配置函数返回 true 则认为其处理了打开逻辑,跳出
139
- if (
140
- handleOutside?.({
141
- router,
142
- route,
143
- replace,
144
- isTriggerWithWindow,
145
- isSameHost
146
- })
147
- ) {
136
+ const res = handleOutside?.({
137
+ router,
138
+ route,
139
+ replace,
140
+ isTriggerWithWindow,
141
+ isSameHost
142
+ });
143
+ if (res === false) {
144
+ // 如果配置函数返回 false 则跳出
148
145
  return true;
149
146
  }
150
147
 
@@ -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 {