@azure-net/kit 0.8.5 → 0.8.7
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/core/delivery/schema/Schema.d.ts +33 -0
- package/dist/core/delivery/schema/Schema.js +78 -0
- package/dist/core/delivery/schema/index.d.ts +1 -0
- package/dist/core/delivery/schema/index.js +1 -0
- package/dist/core/delivery/schema/rules/Rules.d.ts +59 -0
- package/dist/core/delivery/schema/rules/Rules.js +285 -0
- package/dist/core/delivery/schema/rules/messages/En.d.ts +2 -0
- package/dist/core/delivery/schema/rules/messages/En.js +40 -0
- package/dist/core/delivery/schema/rules/messages/Ru.d.ts +2 -0
- package/dist/core/delivery/schema/rules/messages/Ru.js +40 -0
- package/dist/core/delivery/schema/rules/messages/WithI18n.d.ts +2 -0
- package/dist/core/delivery/schema/rules/messages/WithI18n.js +40 -0
- package/dist/core/delivery/schema/rules/messages/index.d.ts +3 -0
- package/dist/core/delivery/schema/rules/messages/index.js +3 -0
- package/dist/core/delivery/schema/rules/messages/types.d.ts +41 -0
- package/dist/core/delivery/schema/rules/messages/types.js +1 -0
- package/dist/core/delivery/serverAction/CreateServerAction.d.ts +1 -1
- package/dist/core/delivery/serverAction/CreateServerAction.js +3 -3
- package/dist/core/index.d.ts +3 -5
- package/dist/core/index.js +3 -5
- package/dist/core/infra/index.d.ts +5 -0
- package/dist/core/infra/index.js +5 -0
- package/dist/core/infra/response/BaseResponse.d.ts +1 -1
- package/dist/core/infra/response/BaseResponse.js +0 -1
- package/dist/core/shared/boundaryProvider/Provider.js +6 -6
- package/dist/core/shared/cookie/UniversalCookie.d.ts +69 -0
- package/dist/core/shared/cookie/UniversalCookie.js +157 -0
- package/dist/core/shared/cookie/index.d.ts +1 -0
- package/dist/core/shared/cookie/index.js +1 -0
- package/dist/core/shared/eventBus/EventBus.d.ts +6 -10
- package/dist/core/shared/eventBus/EventBus.js +12 -18
- package/dist/core/shared/middleware/Middleware.d.ts +16 -0
- package/dist/core/shared/middleware/Middleware.js +68 -0
- package/dist/core/shared/middleware/index.d.ts +1 -0
- package/dist/core/shared/middleware/index.js +1 -0
- package/dist/{lib → core/ui}/onClickOutside/OnClickOutside.js +2 -2
- package/dist/svelte/AsyncSignal/AsyncSignal.svelte.js +4 -4
- package/package.json +5 -5
- /package/dist/{lib → core/ui}/eventModifiers/EventModifiers.d.ts +0 -0
- /package/dist/{lib → core/ui}/eventModifiers/EventModifiers.js +0 -0
- /package/dist/{lib → core/ui}/eventModifiers/index.d.ts +0 -0
- /package/dist/{lib → core/ui}/eventModifiers/index.js +0 -0
- /package/dist/{lib → core/ui}/index.d.ts +0 -0
- /package/dist/{lib → core/ui}/index.js +0 -0
- /package/dist/{lib → core/ui}/onClickOutside/OnClickOutside.d.ts +0 -0
- /package/dist/{lib → core/ui}/onClickOutside/index.d.ts +0 -0
- /package/dist/{lib → core/ui}/onClickOutside/index.js +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { RequestContext } from '../../../edges/context/index.js';
|
|
2
|
-
import { browser } from '$app/environment';
|
|
3
2
|
import { error, fail, redirect } from '@sveltejs/kit';
|
|
3
|
+
import { EnvironmentUtil } from 'azure-net-tools';
|
|
4
4
|
export const createServerAction = (factory) => {
|
|
5
5
|
return () => {
|
|
6
|
-
if (
|
|
6
|
+
if (EnvironmentUtil.isBrowser) {
|
|
7
7
|
throw Error('Do not use actions on client side');
|
|
8
8
|
}
|
|
9
|
-
const context = RequestContext.current();
|
|
9
|
+
const context = RequestContext.current().event;
|
|
10
10
|
return factory({ context, utils: { fail, redirect, error } });
|
|
11
11
|
};
|
|
12
12
|
};
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export * from './infra/response/index.js';
|
|
2
|
-
export * from './infra/resource/index.js';
|
|
3
|
-
export * from './infra/httpService/index.js';
|
|
4
|
-
export * from './infra/datasource/index.js';
|
|
5
|
-
export * from './infra/query/index.js';
|
|
6
1
|
export * from './delivery/index.js';
|
|
7
2
|
export * from './shared/request/index.js';
|
|
3
|
+
export * from './shared/middleware/index.js';
|
|
8
4
|
export * from './shared/classMirror/index.js';
|
|
5
|
+
export * from './shared/cookie/index.js';
|
|
9
6
|
export * from './shared/boundaryProvider/index.js';
|
|
7
|
+
export * from './ui/index.js';
|
package/dist/core/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export * from './infra/response/index.js';
|
|
2
|
-
export * from './infra/resource/index.js';
|
|
3
|
-
export * from './infra/httpService/index.js';
|
|
4
|
-
export * from './infra/datasource/index.js';
|
|
5
|
-
export * from './infra/query/index.js';
|
|
6
1
|
export * from './delivery/index.js';
|
|
7
2
|
export * from './shared/request/index.js';
|
|
3
|
+
export * from './shared/middleware/index.js';
|
|
8
4
|
export * from './shared/classMirror/index.js';
|
|
5
|
+
export * from './shared/cookie/index.js';
|
|
9
6
|
export * from './shared/boundaryProvider/index.js';
|
|
7
|
+
export * from './ui/index.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpServiceResponse } from '
|
|
1
|
+
import type { HttpServiceResponse } from '../httpService/index.js';
|
|
2
2
|
type DeepKeys<T> = T extends object ? {
|
|
3
3
|
[K in keyof T & string]: T[K] extends object ? K | `${K}.${DeepKeys<T[K]>}` : K;
|
|
4
4
|
}[keyof T & string] : never;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RequestContext } from '../../../edges/context/index.js';
|
|
2
|
-
import {
|
|
2
|
+
import { EnvironmentUtil } from 'azure-net-tools';
|
|
3
3
|
const clientCache = new Map();
|
|
4
4
|
const factoriesCache = new WeakMap();
|
|
5
5
|
const providerProxyCache = new Map();
|
|
6
6
|
const getProviderCache = (providerName) => {
|
|
7
|
-
if (
|
|
7
|
+
if (EnvironmentUtil.isBrowser) {
|
|
8
8
|
if (!clientCache.has(providerName)) {
|
|
9
9
|
clientCache.set(providerName, new Map());
|
|
10
10
|
}
|
|
@@ -29,7 +29,7 @@ const getProviderCache = (providerName) => {
|
|
|
29
29
|
export const createBoundaryProvider = (name, services, options) => {
|
|
30
30
|
const { dependsOn = {} } = options ?? {};
|
|
31
31
|
const providerFn = () => {
|
|
32
|
-
if (
|
|
32
|
+
if (EnvironmentUtil.isBrowser && providerProxyCache.has(name)) {
|
|
33
33
|
return providerProxyCache.get(name);
|
|
34
34
|
}
|
|
35
35
|
const cache = getProviderCache(name);
|
|
@@ -55,7 +55,7 @@ export const createBoundaryProvider = (name, services, options) => {
|
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
factories = services(deps);
|
|
58
|
-
if (
|
|
58
|
+
if (EnvironmentUtil.isBrowser) {
|
|
59
59
|
factoriesCache.set(services, factories);
|
|
60
60
|
}
|
|
61
61
|
return factories;
|
|
@@ -87,7 +87,7 @@ export const createBoundaryProvider = (name, services, options) => {
|
|
|
87
87
|
return [...new Set([...cachedKeys, ...factoryKeys])];
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
|
-
if (
|
|
90
|
+
if (EnvironmentUtil.isBrowser) {
|
|
91
91
|
providerProxyCache.set(name, providerProxy);
|
|
92
92
|
}
|
|
93
93
|
return providerProxy;
|
|
@@ -108,7 +108,7 @@ export function cleanupProvider(name) {
|
|
|
108
108
|
await Promise.all(promises);
|
|
109
109
|
cache.clear();
|
|
110
110
|
};
|
|
111
|
-
if (
|
|
111
|
+
if (EnvironmentUtil.isBrowser) {
|
|
112
112
|
const cache = clientCache.get(name);
|
|
113
113
|
if (cache) {
|
|
114
114
|
void cleanupCache(cache);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export type CookieOptions = {
|
|
2
|
+
expires?: Date | number;
|
|
3
|
+
maxAge?: number;
|
|
4
|
+
path?: string;
|
|
5
|
+
domain?: string;
|
|
6
|
+
secure?: boolean;
|
|
7
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
8
|
+
httpOnly?: boolean;
|
|
9
|
+
priority?: 'low' | 'medium' | 'high' | undefined;
|
|
10
|
+
partitioned?: boolean | undefined;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Utility class for managing cookies in svelte ssr (browser and server).
|
|
14
|
+
* Supports setting, getting, deleting, checking, and clearing cookies.
|
|
15
|
+
* All methods are static and operate without creating instances.
|
|
16
|
+
*/
|
|
17
|
+
export declare class UniversalCookie {
|
|
18
|
+
/**
|
|
19
|
+
* Sets a cookie with the specified key, value, and options.
|
|
20
|
+
* Serializes non-string values as JSON.
|
|
21
|
+
*
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {string} name - The cookie key (alphanumeric, dash, underscore only).
|
|
24
|
+
* @param {T} value - The value to store in the cookie.
|
|
25
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
static set<T>(name: string, value: T, options?: CookieOptions): void;
|
|
29
|
+
/**
|
|
30
|
+
* Retrieves the value of a cookie by key.
|
|
31
|
+
* Attempts to parse JSON if possible.
|
|
32
|
+
*
|
|
33
|
+
* @template T
|
|
34
|
+
* @param {string} name - The cookie key to retrieve.
|
|
35
|
+
* @returns {T | undefined} The cookie value, parsed as type T or null if not found.
|
|
36
|
+
*/
|
|
37
|
+
static get<T = string>(name: string): T | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves all cookies as a key-value record.
|
|
40
|
+
* Attempts to parse JSON values.
|
|
41
|
+
*
|
|
42
|
+
* @template T
|
|
43
|
+
* @returns {T | undefined} An object with all cookie keys and their values.
|
|
44
|
+
*/
|
|
45
|
+
static getAll<T = Record<string, unknown>>(): T | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a cookie with the given key exists.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} name - The cookie key to check.
|
|
50
|
+
* @returns {boolean} True if cookie exists, false otherwise.
|
|
51
|
+
*/
|
|
52
|
+
static has(name: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Deletes a cookie by key.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} name - The cookie key to delete.
|
|
57
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
58
|
+
* @returns {void}
|
|
59
|
+
*/
|
|
60
|
+
static delete(name: string, options?: CookieOptions): void;
|
|
61
|
+
/**
|
|
62
|
+
* Deletes all cookies with default domain and path.
|
|
63
|
+
* It's recommended to specify path and domain in options for full cleanup.
|
|
64
|
+
*
|
|
65
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
66
|
+
* @returns {void}
|
|
67
|
+
*/
|
|
68
|
+
static clear(options?: CookieOptions): void;
|
|
69
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { Cookies, EnvironmentUtil } from 'azure-net-tools';
|
|
2
|
+
import { RequestContext } from 'edges-svelte/context';
|
|
3
|
+
/**
|
|
4
|
+
* Utility class for managing cookies in svelte ssr (browser and server).
|
|
5
|
+
* Supports setting, getting, deleting, checking, and clearing cookies.
|
|
6
|
+
* All methods are static and operate without creating instances.
|
|
7
|
+
*/
|
|
8
|
+
export class UniversalCookie {
|
|
9
|
+
/**
|
|
10
|
+
* Sets a cookie with the specified key, value, and options.
|
|
11
|
+
* Serializes non-string values as JSON.
|
|
12
|
+
*
|
|
13
|
+
* @template T
|
|
14
|
+
* @param {string} name - The cookie key (alphanumeric, dash, underscore only).
|
|
15
|
+
* @param {T} value - The value to store in the cookie.
|
|
16
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
17
|
+
* @returns {void}
|
|
18
|
+
*/
|
|
19
|
+
static set(name, value, options) {
|
|
20
|
+
if (EnvironmentUtil.isBrowser) {
|
|
21
|
+
Cookies.set(name, value, options);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (EnvironmentUtil.isServer) {
|
|
25
|
+
const event = RequestContext.current().event;
|
|
26
|
+
if (event) {
|
|
27
|
+
const encodedKey = encodeURIComponent(name);
|
|
28
|
+
let serializedValue;
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
serializedValue = encodeURIComponent(value);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
serializedValue = encodeURIComponent(JSON.stringify(value));
|
|
34
|
+
}
|
|
35
|
+
let expires;
|
|
36
|
+
if (options?.expires !== undefined) {
|
|
37
|
+
if (typeof options.expires === 'number') {
|
|
38
|
+
const date = new Date();
|
|
39
|
+
date.setTime(date.getTime() + options.expires * 86400 * 1000);
|
|
40
|
+
expires = date;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
expires = options.expires;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
event.cookies.set(encodedKey, serializedValue, {
|
|
47
|
+
...options,
|
|
48
|
+
path: options?.path ?? '/',
|
|
49
|
+
httpOnly: options?.httpOnly ?? false,
|
|
50
|
+
sameSite: options?.sameSite?.toLowerCase(),
|
|
51
|
+
expires
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new Error('Could not detect current environment');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Retrieves the value of a cookie by key.
|
|
60
|
+
* Attempts to parse JSON if possible.
|
|
61
|
+
*
|
|
62
|
+
* @template T
|
|
63
|
+
* @param {string} name - The cookie key to retrieve.
|
|
64
|
+
* @returns {T | undefined} The cookie value, parsed as type T or null if not found.
|
|
65
|
+
*/
|
|
66
|
+
static get(name) {
|
|
67
|
+
if (EnvironmentUtil.isBrowser) {
|
|
68
|
+
return Cookies.get(name) ?? undefined;
|
|
69
|
+
}
|
|
70
|
+
if (EnvironmentUtil.isServer) {
|
|
71
|
+
const event = RequestContext.current().event;
|
|
72
|
+
if (event) {
|
|
73
|
+
const encodedKey = encodeURIComponent(name);
|
|
74
|
+
const cookieValue = event.cookies.get(encodedKey);
|
|
75
|
+
if (cookieValue) {
|
|
76
|
+
const decodedValue = decodeURIComponent(cookieValue);
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(decodedValue);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return decodedValue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Retrieves all cookies as a key-value record.
|
|
92
|
+
* Attempts to parse JSON values.
|
|
93
|
+
*
|
|
94
|
+
* @template T
|
|
95
|
+
* @returns {T | undefined} An object with all cookie keys and their values.
|
|
96
|
+
*/
|
|
97
|
+
static getAll() {
|
|
98
|
+
if (EnvironmentUtil.isBrowser) {
|
|
99
|
+
return Cookies.getAll();
|
|
100
|
+
}
|
|
101
|
+
if (EnvironmentUtil.isServer) {
|
|
102
|
+
const event = RequestContext.current().event;
|
|
103
|
+
const result = {};
|
|
104
|
+
if (event) {
|
|
105
|
+
const allCookies = event.cookies.getAll();
|
|
106
|
+
for (const singleCookie of allCookies) {
|
|
107
|
+
try {
|
|
108
|
+
result[singleCookie.name] = JSON.parse(singleCookie.value);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
result[singleCookie.name] = singleCookie.value;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Checks if a cookie with the given key exists.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} name - The cookie key to check.
|
|
123
|
+
* @returns {boolean} True if cookie exists, false otherwise.
|
|
124
|
+
*/
|
|
125
|
+
static has(name) {
|
|
126
|
+
return this.get(name) !== undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Deletes a cookie by key.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} name - The cookie key to delete.
|
|
132
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
133
|
+
* @returns {void}
|
|
134
|
+
*/
|
|
135
|
+
static delete(name, options) {
|
|
136
|
+
this.set(name, '', {
|
|
137
|
+
...options,
|
|
138
|
+
expires: new Date(0),
|
|
139
|
+
maxAge: 0
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Deletes all cookies with default domain and path.
|
|
144
|
+
* It's recommended to specify path and domain in options for full cleanup.
|
|
145
|
+
*
|
|
146
|
+
* @param {CookieOptions} [options] - Optional cookie attributes.
|
|
147
|
+
* @returns {void}
|
|
148
|
+
*/
|
|
149
|
+
static clear(options) {
|
|
150
|
+
const allCookies = this.getAll();
|
|
151
|
+
if (allCookies) {
|
|
152
|
+
for (const key in allCookies) {
|
|
153
|
+
this.delete(key, options);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './UniversalCookie.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './UniversalCookie.js';
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
export declare enum EventBusLayers {
|
|
2
|
-
DOMAIN = "domain",
|
|
3
|
-
APPLICATION = "application",
|
|
4
|
-
PRESENTATION = "presentation"
|
|
5
|
-
}
|
|
6
1
|
export interface IEvent<T = unknown> {
|
|
7
2
|
aggregateId: string;
|
|
8
3
|
eventType: string;
|
|
9
4
|
eventVersion: number;
|
|
10
5
|
occurredAt: Date;
|
|
11
6
|
metadata: IEventMetadata;
|
|
12
|
-
|
|
7
|
+
busName?: string;
|
|
13
8
|
payload: T;
|
|
14
9
|
}
|
|
15
10
|
export interface IEventMetadata {
|
|
@@ -34,9 +29,9 @@ export declare class EventBus<TEventMap extends object> {
|
|
|
34
29
|
private priorities;
|
|
35
30
|
private readonly debug;
|
|
36
31
|
private readonly maxHistorySize;
|
|
37
|
-
private readonly
|
|
32
|
+
private readonly busName?;
|
|
38
33
|
constructor(options?: {
|
|
39
|
-
|
|
34
|
+
busName?: string;
|
|
40
35
|
enableHistory?: boolean;
|
|
41
36
|
maxHistorySize?: number;
|
|
42
37
|
enableAsyncProcessing?: boolean;
|
|
@@ -70,12 +65,13 @@ export declare class EventBus<TEventMap extends object> {
|
|
|
70
65
|
private generateCorrelationId;
|
|
71
66
|
}
|
|
72
67
|
export declare const loggingMiddleware: (event: IEvent, next: () => Promise<void>) => Promise<void>;
|
|
73
|
-
export declare const createEventBus: <TEventMap extends object>(
|
|
68
|
+
export declare const createEventBus: <TEventMap extends object>(opts: {
|
|
74
69
|
history?: boolean;
|
|
75
70
|
asyncProcessing?: boolean;
|
|
76
71
|
historySize?: number;
|
|
77
72
|
middlewares?: (<T = unknown>(event: IEvent<T>, next: () => Promise<void>) => Promise<void>)[];
|
|
78
73
|
debug?: boolean;
|
|
74
|
+
busName?: string;
|
|
79
75
|
}) => {
|
|
80
76
|
eventBus: EventBus<TEventMap>;
|
|
81
77
|
CreateEvent: <K extends keyof TEventMap>(eventName: K, data: TEventMap[K]) => void;
|
|
@@ -87,5 +83,5 @@ export declare const createEventBus: <TEventMap extends object>(layer: EventBusL
|
|
|
87
83
|
CreateSubscriber: <K extends keyof TEventMap>(eventType: K, handler: DomainEventHandler<TEventMap[K]>, options?: {
|
|
88
84
|
priority?: number;
|
|
89
85
|
}) => IEventSubscription;
|
|
90
|
-
|
|
86
|
+
CreateSubscribers: <K extends keyof TEventMap>(eventTypes: K[], handler: DomainEventHandler<TEventMap[K]>) => IEventSubscription;
|
|
91
87
|
};
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
export var EventBusLayers;
|
|
2
|
-
(function (EventBusLayers) {
|
|
3
|
-
EventBusLayers["DOMAIN"] = "domain";
|
|
4
|
-
EventBusLayers["APPLICATION"] = "application";
|
|
5
|
-
EventBusLayers["PRESENTATION"] = "presentation";
|
|
6
|
-
})(EventBusLayers || (EventBusLayers = {}));
|
|
7
1
|
export class EventBus {
|
|
8
2
|
options;
|
|
9
3
|
handlers = new Map();
|
|
@@ -15,10 +9,10 @@ export class EventBus {
|
|
|
15
9
|
priorities = new Map();
|
|
16
10
|
debug = false;
|
|
17
11
|
maxHistorySize = 1000;
|
|
18
|
-
|
|
19
|
-
constructor(options = {
|
|
12
|
+
busName;
|
|
13
|
+
constructor(options = { debug: false }) {
|
|
20
14
|
this.options = options;
|
|
21
|
-
this.
|
|
15
|
+
this.busName = options.busName ?? undefined;
|
|
22
16
|
this.debug = options.debug ?? false;
|
|
23
17
|
if (options.maxHistorySize) {
|
|
24
18
|
this.maxHistorySize = options.maxHistorySize;
|
|
@@ -34,7 +28,7 @@ export class EventBus {
|
|
|
34
28
|
correlationId: this.generateCorrelationId(),
|
|
35
29
|
...metadata
|
|
36
30
|
},
|
|
37
|
-
|
|
31
|
+
busName: this.busName,
|
|
38
32
|
payload
|
|
39
33
|
};
|
|
40
34
|
void this.processEvent(event);
|
|
@@ -45,7 +39,7 @@ export class EventBus {
|
|
|
45
39
|
eventType: type,
|
|
46
40
|
eventVersion: 1,
|
|
47
41
|
occurredAt: new Date(),
|
|
48
|
-
|
|
42
|
+
busName: this.busName,
|
|
49
43
|
metadata: {
|
|
50
44
|
correlationId: this.generateCorrelationId(),
|
|
51
45
|
...metadata
|
|
@@ -198,17 +192,17 @@ export class EventBus {
|
|
|
198
192
|
}
|
|
199
193
|
}
|
|
200
194
|
export const loggingMiddleware = async (event, next) => {
|
|
201
|
-
console.log(`[${event.
|
|
202
|
-
console.log(`[${event.
|
|
195
|
+
console.log(`[${event.busName ?? ''}Event] ${event.eventType} at ${event.occurredAt.toISOString()}`);
|
|
196
|
+
console.log(`[${event.busName ?? ''}Event] data:`, event);
|
|
203
197
|
const start = performance.now();
|
|
204
198
|
await next();
|
|
205
199
|
const duration = performance.now() - start;
|
|
206
|
-
console.log(`[${event.
|
|
200
|
+
console.log(`[${event.busName ?? ''}Event] ${event.eventType} processed in ${duration.toFixed(2)}ms`);
|
|
207
201
|
};
|
|
208
|
-
export const createEventBus = (
|
|
202
|
+
export const createEventBus = (opts) => {
|
|
209
203
|
const { history = true, asyncProcessing = true, historySize = 1000, middlewares = [], debug = false } = opts;
|
|
210
204
|
const eventBus = new EventBus({
|
|
211
|
-
|
|
205
|
+
busName: opts.busName,
|
|
212
206
|
enableHistory: history,
|
|
213
207
|
enableAsyncProcessing: asyncProcessing,
|
|
214
208
|
maxHistorySize: historySize,
|
|
@@ -226,8 +220,8 @@ export const createEventBus = (layer, opts) => {
|
|
|
226
220
|
const CreateSubscriber = (eventType, handler, options) => {
|
|
227
221
|
return eventBus.subscribe(eventType, handler, options);
|
|
228
222
|
};
|
|
229
|
-
const
|
|
223
|
+
const CreateSubscribers = (eventTypes, handler) => {
|
|
230
224
|
return eventBus.subscribeMany(eventTypes, handler);
|
|
231
225
|
};
|
|
232
|
-
return { eventBus, CreateEvent, CreateBatch, CreateSubscriber,
|
|
226
|
+
return { eventBus, CreateEvent, CreateBatch, CreateSubscriber, CreateSubscribers };
|
|
233
227
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { error, type Page, type RequestEvent } from '@sveltejs/kit';
|
|
2
|
+
import { UniversalCookie } from '../cookie/index.js';
|
|
3
|
+
export type IMiddleware = (middlewareData: {
|
|
4
|
+
to: RequestEvent['url'] | Page['url'];
|
|
5
|
+
from?: RequestEvent['url'] | Page['url'];
|
|
6
|
+
error: typeof error;
|
|
7
|
+
next: (location?: string | URL, status?: 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | number) => void;
|
|
8
|
+
isServer: boolean;
|
|
9
|
+
cookies: UniversalCookie;
|
|
10
|
+
event?: RequestEvent;
|
|
11
|
+
page: Page;
|
|
12
|
+
}) => Promise<void> | void;
|
|
13
|
+
export declare const createMiddlewareManager: (middlewares: IMiddleware[]) => {
|
|
14
|
+
serverMiddleware: () => Promise<void>;
|
|
15
|
+
clientMiddleware: () => void;
|
|
16
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { error, redirect } from '@sveltejs/kit';
|
|
2
|
+
import { RequestContext } from 'edges-svelte/context';
|
|
3
|
+
import { beforeNavigate, goto } from '$app/navigation';
|
|
4
|
+
import { page } from '$app/state';
|
|
5
|
+
import { EnvironmentUtil } from 'azure-net-tools';
|
|
6
|
+
import { UniversalCookie } from '../cookie/index.js';
|
|
7
|
+
const universalRedirect = (location, status = 301, navigation) => {
|
|
8
|
+
if (EnvironmentUtil.isBrowser && navigation) {
|
|
9
|
+
navigation?.cancel();
|
|
10
|
+
return goto(location);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return redirect(status, location);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export const createMiddlewareManager = (middlewares) => {
|
|
17
|
+
const executeMiddlewares = async (navigation) => {
|
|
18
|
+
let event;
|
|
19
|
+
let from = undefined;
|
|
20
|
+
if (EnvironmentUtil.isServer) {
|
|
21
|
+
event = RequestContext.current().event;
|
|
22
|
+
const referer = event?.request.headers.get('referer');
|
|
23
|
+
from = referer ? new URL(referer) : undefined;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
from = navigation?.from?.url ?? undefined;
|
|
27
|
+
}
|
|
28
|
+
const to = (EnvironmentUtil.isBrowser ? (navigation?.to?.url ?? page?.url) : event?.url);
|
|
29
|
+
for (const middleware of middlewares) {
|
|
30
|
+
let shouldContinue = false;
|
|
31
|
+
const next = (location, status = 301) => {
|
|
32
|
+
shouldContinue = true;
|
|
33
|
+
if (location) {
|
|
34
|
+
return universalRedirect(location, status, navigation);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
await middleware({
|
|
38
|
+
event,
|
|
39
|
+
page,
|
|
40
|
+
cookies: UniversalCookie,
|
|
41
|
+
isServer: EnvironmentUtil.isServer,
|
|
42
|
+
to,
|
|
43
|
+
from,
|
|
44
|
+
error,
|
|
45
|
+
next
|
|
46
|
+
});
|
|
47
|
+
if (!shouldContinue) {
|
|
48
|
+
if (EnvironmentUtil.isBrowser) {
|
|
49
|
+
navigation?.cancel();
|
|
50
|
+
if (from) {
|
|
51
|
+
void goto(from.pathname);
|
|
52
|
+
console.warn('Navigation blocked: middleware chain stopped (next() not called).');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
error(403, 'Navigation blocked: middleware chain stopped (next() not called).');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const serverMiddleware = async () => await executeMiddlewares();
|
|
62
|
+
const clientMiddleware = () => {
|
|
63
|
+
beforeNavigate(async (navigation) => {
|
|
64
|
+
await executeMiddlewares(navigation);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
return { serverMiddleware, clientMiddleware };
|
|
68
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Middleware.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Middleware.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EnvironmentUtil } from 'azure-net-tools';
|
|
2
2
|
export const onClickOutside = (node, initiator) => {
|
|
3
|
-
if (
|
|
3
|
+
if (EnvironmentUtil.isServer)
|
|
4
4
|
return { destroy: () => { } };
|
|
5
5
|
const handleClick = (event) => {
|
|
6
6
|
if (event.target instanceof HTMLElement) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { untrack } from 'svelte';
|
|
2
|
-
import {
|
|
2
|
+
import { EnvironmentUtil } from 'azure-net-tools';
|
|
3
3
|
export const createAsyncSignal = (handler, options = {}) => {
|
|
4
4
|
const { server = false, immediate = true, watch = [], initialData = null } = options;
|
|
5
5
|
let data = $state(initialData);
|
|
@@ -30,7 +30,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
30
30
|
status = 'error';
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
if (
|
|
33
|
+
if (EnvironmentUtil.isBrowser && watch.length > 0) {
|
|
34
34
|
let isFirst = true;
|
|
35
35
|
$effect(() => {
|
|
36
36
|
watch.forEach((dep) => dep());
|
|
@@ -45,12 +45,12 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
if (immediate) {
|
|
48
|
-
if (
|
|
48
|
+
if (EnvironmentUtil.isServer && server) {
|
|
49
49
|
untrack(() => {
|
|
50
50
|
void execute();
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
|
-
else if (
|
|
53
|
+
else if (EnvironmentUtil.isBrowser) {
|
|
54
54
|
void execute();
|
|
55
55
|
}
|
|
56
56
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-net/kit",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"!dist/**/*.test.*",
|
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"svelte": "./dist/index.js"
|
|
19
19
|
},
|
|
20
|
+
"./infra": {
|
|
21
|
+
"types": "./dist/core/infra/index.d.ts",
|
|
22
|
+
"svelte": "./dist/core/infra/index.js"
|
|
23
|
+
},
|
|
20
24
|
"./svelte": {
|
|
21
25
|
"types": "./dist/svelte/index.d.ts",
|
|
22
26
|
"svelte": "./dist/svelte/index.js"
|
|
23
27
|
},
|
|
24
|
-
"./lib": {
|
|
25
|
-
"types": "./dist/lib/index.d.ts",
|
|
26
|
-
"svelte": "./dist/lib/index.js"
|
|
27
|
-
},
|
|
28
28
|
"./request/rules": {
|
|
29
29
|
"types": "./dist/core/shared/request/rules/index.d.ts",
|
|
30
30
|
"svelte": "./dist/core/shared/request/rules/index.js"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|