@azure-net/kit 5.0.0-4 → 5.0.0

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,4 +1,4 @@
1
1
  export { createApp, type AppClientErrorCallback, type AppClientErrorContext, type AppClientInitLifecycleCallback, type AppClientInitLifecycleContext, type AppClientLifecycleCallback, type AppClientLifecycleContext, type AppContainer, type AppRegistrar, type AppServerErrorCallback, type AppServerErrorContext, type AppServerInitLifecycleCallback, type AppServerInitLifecycleContext, type AppServerLifecycleCallback, type AppServerLifecycleContext, type AppUniversalLifecycleCallback, type AppUniversalLifecycleContext, type CreateAppInstance } from './App.js';
2
2
  export type { IClientMiddleware } from './middleware/ClientMiddleware.js';
3
3
  export type { IServerMiddleware } from './middleware/ServerMiddleware.js';
4
- export type { RedirectStatus } from './middleware/Shared.js';
4
+ export type { EnsureRoute, EnsureRouteSource, EnsureRouteTarget, RedirectStatus } from './middleware/Shared.js';
@@ -1,8 +1,9 @@
1
1
  import { type BeforeNavigate } from '@sveltejs/kit';
2
- import type { RedirectStatus } from './Shared.js';
2
+ import { type EnsureRoute, type RedirectStatus } from './Shared.js';
3
3
  export type IClientMiddleware = (middlewareData: {
4
4
  to: URL;
5
5
  from?: URL;
6
6
  next: (location?: string | URL, status?: RedirectStatus) => void;
7
+ ensureRoute: EnsureRoute;
7
8
  }) => Promise<void> | void;
8
9
  export declare const executeClientMiddlewares: (middlewares: IClientMiddleware[], navigation?: BeforeNavigate) => Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import {} from '@sveltejs/kit';
2
+ import { ensureRoute } from './Shared.js';
2
3
  import { page } from '$app/state';
3
4
  import { goto } from '$app/navigation';
4
5
  export const executeClientMiddlewares = async (middlewares, navigation) => {
@@ -18,7 +19,8 @@ export const executeClientMiddlewares = async (middlewares, navigation) => {
18
19
  await middleware({
19
20
  to,
20
21
  from,
21
- next
22
+ next,
23
+ ensureRoute
22
24
  });
23
25
  if (!shouldContinue) {
24
26
  navigation?.cancel();
@@ -1,9 +1,10 @@
1
1
  import { type RequestEvent } from '@sveltejs/kit';
2
- import type { RedirectStatus } from './Shared.js';
2
+ import { type EnsureRoute, type RedirectStatus } from './Shared.js';
3
3
  export type IServerMiddleware = (middlewareData: {
4
4
  to: RequestEvent['url'];
5
5
  from?: RequestEvent['url'];
6
6
  next: (location?: string | URL, status?: RedirectStatus) => void;
7
7
  event?: RequestEvent;
8
+ ensureRoute: EnsureRoute;
8
9
  }) => Promise<void> | void;
9
10
  export declare const executeServerMiddlewares: (middlewares: IServerMiddleware[]) => Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import { error, redirect } from '@sveltejs/kit';
2
+ import { ensureRoute } from './Shared.js';
2
3
  import { BROWSER } from '../../../external/tools/index.js';
3
4
  import { RequestContext } from '../../../external/edges/ServerContext.js';
4
5
  export const executeServerMiddlewares = async (middlewares) => {
@@ -20,7 +21,8 @@ export const executeServerMiddlewares = async (middlewares) => {
20
21
  event,
21
22
  to,
22
23
  from,
23
- next
24
+ next,
25
+ ensureRoute
24
26
  });
25
27
  if (!shouldContinue) {
26
28
  error(403, 'Navigation blocked: middleware chain stopped (next() not called).');
@@ -1 +1,5 @@
1
1
  export type RedirectStatus = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
2
+ export type EnsureRouteSource = string | readonly string[];
3
+ export type EnsureRouteTarget = string | URL;
4
+ export type EnsureRoute = (route: EnsureRouteSource, target: EnsureRouteTarget) => boolean;
5
+ export declare const ensureRoute: EnsureRoute;
@@ -1 +1,31 @@
1
- export {};
1
+ const trimSlashes = (value) => value.replace(/^\/+|\/+$/g, '');
2
+ const normalizePathname = (value) => {
3
+ const pathname = value instanceof URL ? value.pathname : value;
4
+ const normalized = `/${trimSlashes(pathname.split('?')[0] ?? '/')}`;
5
+ return normalized === '//' ? '/' : normalized;
6
+ };
7
+ const escapeRegex = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
8
+ const routeToRegex = (route) => {
9
+ const normalizedRoute = normalizePathname(route);
10
+ if (normalizedRoute === '/')
11
+ return /^\/$/;
12
+ const segments = trimSlashes(normalizedRoute).split('/');
13
+ const pattern = segments
14
+ .map((segment) => {
15
+ if (/^\{[^/{}]+\}$/.test(segment)) {
16
+ return '[^/]+';
17
+ }
18
+ return escapeRegex(segment).replace(/\\\{[^/{}]+\\\}/g, '[^/]+');
19
+ })
20
+ .join('\\/');
21
+ return new RegExp(`^\\/${pattern}$`);
22
+ };
23
+ export const ensureRoute = (route, target) => {
24
+ const pathname = normalizePathname(target);
25
+ const routes = Array.isArray(route) ? route : [route];
26
+ for (const item of routes) {
27
+ if (routeToRegex(item).test(pathname))
28
+ return true;
29
+ }
30
+ return false;
31
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azure-net/kit",
3
- "version": "5.0.0-4",
3
+ "version": "5.0.0",
4
4
  "license": "MIT",
5
5
  "author": "Pixel1917",
6
6
  "description": "Universal toolkit and library collection, developed as part of the Azure-net ecosystem",
@@ -88,6 +88,7 @@
88
88
  "@testing-library/jest-dom": "^6.9.1",
89
89
  "@testing-library/svelte": "^5.3.1",
90
90
  "commitizen": "^4.3.1",
91
+ "env-cmd": "^11.0.0",
91
92
  "eslint": "^10.2.1",
92
93
  "eslint-config-prettier": "^10.1.8",
93
94
  "eslint-plugin-svelte": "^3.17.0",