@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,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.14",
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.14",
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": "e0349256d64a50d86be8a585a498bc4278014324"
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