vector-framework 0.8.1
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/LICENSE +21 -0
- package/README.md +508 -0
- package/dist/auth/protected.d.ts +9 -0
- package/dist/auth/protected.d.ts.map +1 -0
- package/dist/auth/protected.js +26 -0
- package/dist/auth/protected.js.map +1 -0
- package/dist/cache/manager.d.ts +21 -0
- package/dist/cache/manager.d.ts.map +1 -0
- package/dist/cache/manager.js +92 -0
- package/dist/cache/manager.js.map +1 -0
- package/dist/cli/index.d.ts +3 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +142 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/constants/index.d.ts +84 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +88 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/core/router.d.ts +26 -0
- package/dist/core/router.d.ts.map +1 -0
- package/dist/core/router.js +208 -0
- package/dist/core/router.js.map +1 -0
- package/dist/core/server.d.ts +18 -0
- package/dist/core/server.d.ts.map +1 -0
- package/dist/core/server.js +89 -0
- package/dist/core/server.js.map +1 -0
- package/dist/core/vector.d.ts +43 -0
- package/dist/core/vector.d.ts.map +1 -0
- package/dist/core/vector.js +179 -0
- package/dist/core/vector.js.map +1 -0
- package/dist/dev/route-generator.d.ts +8 -0
- package/dist/dev/route-generator.d.ts.map +1 -0
- package/dist/dev/route-generator.js +77 -0
- package/dist/dev/route-generator.js.map +1 -0
- package/dist/dev/route-scanner.d.ts +9 -0
- package/dist/dev/route-scanner.d.ts.map +1 -0
- package/dist/dev/route-scanner.js +85 -0
- package/dist/dev/route-scanner.js.map +1 -0
- package/dist/errors/index.d.ts +24 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +73 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/http.d.ts +73 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +143 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +21 -0
- package/dist/middleware/manager.d.ts +11 -0
- package/dist/middleware/manager.d.ts.map +1 -0
- package/dist/middleware/manager.js +35 -0
- package/dist/middleware/manager.js.map +1 -0
- package/dist/types/index.d.ts +85 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/logger.d.ts +25 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +68 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/validation.d.ts +5 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +48 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +110 -0
- package/src/auth/protected.ts +41 -0
- package/src/cache/manager.ts +133 -0
- package/src/cli/index.ts +157 -0
- package/src/constants/index.ts +93 -0
- package/src/core/router.ts +258 -0
- package/src/core/server.ts +107 -0
- package/src/core/vector.ts +228 -0
- package/src/dev/route-generator.ts +93 -0
- package/src/dev/route-scanner.ts +97 -0
- package/src/errors/index.ts +91 -0
- package/src/http.ts +331 -0
- package/src/index.ts +19 -0
- package/src/middleware/manager.ts +53 -0
- package/src/types/index.ts +126 -0
- package/src/utils/logger.ts +87 -0
- package/src/utils/validation.ts +58 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { cors } from 'itty-router';
|
|
2
|
+
export class VectorServer {
|
|
3
|
+
server = null;
|
|
4
|
+
router;
|
|
5
|
+
config;
|
|
6
|
+
corsHandler;
|
|
7
|
+
constructor(router, config) {
|
|
8
|
+
this.router = router;
|
|
9
|
+
this.config = config;
|
|
10
|
+
if (config.cors) {
|
|
11
|
+
const { preflight, corsify } = cors(this.normalizeCorsOptions(config.cors));
|
|
12
|
+
this.corsHandler = { preflight, corsify };
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
normalizeCorsOptions(options) {
|
|
16
|
+
return {
|
|
17
|
+
origin: options.origin || '*',
|
|
18
|
+
credentials: options.credentials !== false,
|
|
19
|
+
allowHeaders: Array.isArray(options.allowHeaders)
|
|
20
|
+
? options.allowHeaders.join(', ')
|
|
21
|
+
: options.allowHeaders || 'Content-Type, Authorization',
|
|
22
|
+
allowMethods: Array.isArray(options.allowMethods)
|
|
23
|
+
? options.allowMethods.join(', ')
|
|
24
|
+
: options.allowMethods || 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
|
|
25
|
+
exposeHeaders: Array.isArray(options.exposeHeaders)
|
|
26
|
+
? options.exposeHeaders.join(', ')
|
|
27
|
+
: options.exposeHeaders || 'Authorization',
|
|
28
|
+
maxAge: options.maxAge || 86400,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async start() {
|
|
32
|
+
const port = this.config.port || 3000;
|
|
33
|
+
const hostname = this.config.hostname || 'localhost';
|
|
34
|
+
const fetch = async (request) => {
|
|
35
|
+
try {
|
|
36
|
+
// Handle CORS preflight
|
|
37
|
+
if (this.corsHandler && request.method === 'OPTIONS') {
|
|
38
|
+
return this.corsHandler.preflight(request);
|
|
39
|
+
}
|
|
40
|
+
// Try to handle the request with our router
|
|
41
|
+
let response = await this.router.handle(request);
|
|
42
|
+
// Apply CORS headers if configured
|
|
43
|
+
if (this.corsHandler) {
|
|
44
|
+
response = this.corsHandler.corsify(response, request);
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.error('Server error:', error);
|
|
50
|
+
return new Response('Internal Server Error', { status: 500 });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
this.server = Bun.serve({
|
|
54
|
+
port,
|
|
55
|
+
hostname,
|
|
56
|
+
reusePort: this.config.reusePort !== false,
|
|
57
|
+
fetch,
|
|
58
|
+
error: (error) => {
|
|
59
|
+
console.error('[ERROR] Server error:', error);
|
|
60
|
+
return new Response('Internal Server Error', { status: 500 });
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
// Server logs are handled by CLI, keep this minimal
|
|
64
|
+
console.log(`→ Vector server running at http://${hostname}:${port}`);
|
|
65
|
+
return this.server;
|
|
66
|
+
}
|
|
67
|
+
stop() {
|
|
68
|
+
if (this.server) {
|
|
69
|
+
this.server.stop();
|
|
70
|
+
this.server = null;
|
|
71
|
+
console.log('Server stopped');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getServer() {
|
|
75
|
+
return this.server;
|
|
76
|
+
}
|
|
77
|
+
getPort() {
|
|
78
|
+
return this.server?.port || this.config.port || 3000;
|
|
79
|
+
}
|
|
80
|
+
getHostname() {
|
|
81
|
+
return this.server?.hostname || this.config.hostname || 'localhost';
|
|
82
|
+
}
|
|
83
|
+
getUrl() {
|
|
84
|
+
const port = this.getPort();
|
|
85
|
+
const hostname = this.getHostname();
|
|
86
|
+
return `http://${hostname}:${port}`;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/core/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAInC,MAAM,OAAO,YAAY;IACf,MAAM,GAAkB,IAAI,CAAC;IAC7B,MAAM,CAAuB;IAC7B,MAAM,CAAuB;IAC7B,WAAW,CAAM;IAEzB,YAAY,MAA4B,EAAE,MAA4B;QACpE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QAErB,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;YAChB,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;YAC5E,IAAI,CAAC,WAAW,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AC;QAChC,CAAC;IACH,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,MAAM,EAAE,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,IAAI,IAAI,CAAC;IACvD,CAAC;IAED,WAAW;QACT,OAAO,IAAI,CAAC,MAAM,EAAE,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,IAAI,WAAW,CAAC;IACtE,CAAC;IAED,MAAM;QACJ,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5B,MAAM,QAAQ,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QACpC,OAAO,UAAU,QAAQ,IAAI,IAAI,EAAE,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Server } from 'bun';
|
|
2
|
+
import type { RouteEntry } from 'itty-router';
|
|
3
|
+
import { AuthManager } from '../auth/protected';
|
|
4
|
+
import { CacheManager } from '../cache/manager';
|
|
5
|
+
import type { AfterMiddlewareHandler, BeforeMiddlewareHandler, CacheHandler, DefaultVectorTypes, ProtectedHandler, RouteHandler, RouteOptions, VectorConfig, VectorTypes } from '../types';
|
|
6
|
+
import { VectorRouter } from './router';
|
|
7
|
+
import { VectorServer } from './server';
|
|
8
|
+
export declare class Vector<TTypes extends VectorTypes = DefaultVectorTypes> {
|
|
9
|
+
private static instance;
|
|
10
|
+
private router;
|
|
11
|
+
private server;
|
|
12
|
+
private middlewareManager;
|
|
13
|
+
private authManager;
|
|
14
|
+
private cacheManager;
|
|
15
|
+
private config;
|
|
16
|
+
private routeScanner;
|
|
17
|
+
private routeGenerator;
|
|
18
|
+
private _protectedHandler;
|
|
19
|
+
private _cacheHandler;
|
|
20
|
+
private constructor();
|
|
21
|
+
static getInstance<T extends VectorTypes = DefaultVectorTypes>(): Vector<T>;
|
|
22
|
+
set protected(handler: ProtectedHandler<TTypes>);
|
|
23
|
+
get protected(): ProtectedHandler<TTypes> | null;
|
|
24
|
+
set cache(handler: CacheHandler);
|
|
25
|
+
get cache(): CacheHandler | null;
|
|
26
|
+
route(options: RouteOptions<TTypes>, handler: RouteHandler<TTypes>): RouteEntry;
|
|
27
|
+
use(...middleware: BeforeMiddlewareHandler<TTypes>[]): this;
|
|
28
|
+
before(...middleware: BeforeMiddlewareHandler<TTypes>[]): this;
|
|
29
|
+
finally(...middleware: AfterMiddlewareHandler<TTypes>[]): this;
|
|
30
|
+
serve(config?: VectorConfig<TTypes>): Promise<Server>;
|
|
31
|
+
private discoverRoutes;
|
|
32
|
+
loadRoute(routeModule: any): Promise<void>;
|
|
33
|
+
private isRouteEntry;
|
|
34
|
+
private logRouteLoaded;
|
|
35
|
+
stop(): void;
|
|
36
|
+
getServer(): VectorServer<TTypes> | null;
|
|
37
|
+
getRouter(): VectorRouter<TTypes>;
|
|
38
|
+
getCacheManager(): CacheManager<TTypes>;
|
|
39
|
+
getAuthManager(): AuthManager<TTypes>;
|
|
40
|
+
}
|
|
41
|
+
declare const vector: Vector<DefaultVectorTypes>;
|
|
42
|
+
export default vector;
|
|
43
|
+
//# sourceMappingURL=vector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.d.ts","sourceRoot":"","sources":["../../src/core/vector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAClC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAIhD,OAAO,KAAK,EACV,sBAAsB,EACtB,uBAAuB,EACvB,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,WAAW,EACZ,MAAM,UAAU,CAAC;AA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,CAAC,EAAE,YAAY,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;YA2B7C,cAAc;IAuDtB,SAAS,CAAC,WAAW,EAAE,GAAG;IAkBhC,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,cAAc;IAQtB,IAAI,IAAI,IAAI;IAOZ,SAAS,IAAI,YAAY,CAAC,MAAM,CAAC,GAAG,IAAI;IAIxC,SAAS,IAAI,YAAY,CAAC,MAAM,CAAC;IAIjC,eAAe,IAAI,YAAY,CAAC,MAAM,CAAC;IAIvC,cAAc,IAAI,WAAW,CAAC,MAAM,CAAC;CAGtC;AAED,QAAA,MAAM,MAAM,4BAAuB,CAAC;AAEpC,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { AuthManager } from '../auth/protected';
|
|
2
|
+
import { CacheManager } from '../cache/manager';
|
|
3
|
+
import { RouteGenerator } from '../dev/route-generator';
|
|
4
|
+
import { RouteScanner } from '../dev/route-scanner';
|
|
5
|
+
import { MiddlewareManager } from '../middleware/manager';
|
|
6
|
+
import { VectorRouter } from './router';
|
|
7
|
+
import { VectorServer } from './server';
|
|
8
|
+
export class Vector {
|
|
9
|
+
static instance;
|
|
10
|
+
router;
|
|
11
|
+
server = null;
|
|
12
|
+
middlewareManager;
|
|
13
|
+
authManager;
|
|
14
|
+
cacheManager;
|
|
15
|
+
config = {};
|
|
16
|
+
routeScanner = null;
|
|
17
|
+
routeGenerator = null;
|
|
18
|
+
_protectedHandler = null;
|
|
19
|
+
_cacheHandler = null;
|
|
20
|
+
constructor() {
|
|
21
|
+
this.middlewareManager = new MiddlewareManager();
|
|
22
|
+
this.authManager = new AuthManager();
|
|
23
|
+
this.cacheManager = new CacheManager();
|
|
24
|
+
this.router = new VectorRouter(this.middlewareManager, this.authManager, this.cacheManager);
|
|
25
|
+
}
|
|
26
|
+
static getInstance() {
|
|
27
|
+
if (!Vector.instance) {
|
|
28
|
+
Vector.instance = new Vector();
|
|
29
|
+
}
|
|
30
|
+
return Vector.instance;
|
|
31
|
+
}
|
|
32
|
+
set protected(handler) {
|
|
33
|
+
this._protectedHandler = handler;
|
|
34
|
+
this.authManager.setProtectedHandler(handler);
|
|
35
|
+
}
|
|
36
|
+
get protected() {
|
|
37
|
+
return this._protectedHandler;
|
|
38
|
+
}
|
|
39
|
+
set cache(handler) {
|
|
40
|
+
this._cacheHandler = handler;
|
|
41
|
+
this.cacheManager.setCacheHandler(handler);
|
|
42
|
+
}
|
|
43
|
+
get cache() {
|
|
44
|
+
return this._cacheHandler;
|
|
45
|
+
}
|
|
46
|
+
route(options, handler) {
|
|
47
|
+
return this.router.route(options, handler);
|
|
48
|
+
}
|
|
49
|
+
use(...middleware) {
|
|
50
|
+
this.middlewareManager.addBefore(...middleware);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
before(...middleware) {
|
|
54
|
+
this.middlewareManager.addBefore(...middleware);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
finally(...middleware) {
|
|
58
|
+
this.middlewareManager.addFinally(...middleware);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
async serve(config) {
|
|
62
|
+
this.config = { ...this.config, ...config };
|
|
63
|
+
if (config?.before) {
|
|
64
|
+
this.middlewareManager.addBefore(...config.before);
|
|
65
|
+
}
|
|
66
|
+
if (config?.finally) {
|
|
67
|
+
this.middlewareManager.addFinally(...config.finally);
|
|
68
|
+
}
|
|
69
|
+
if (this.config.autoDiscover !== false) {
|
|
70
|
+
await this.discoverRoutes();
|
|
71
|
+
}
|
|
72
|
+
this.server = new VectorServer(this.router, this.config);
|
|
73
|
+
const bunServer = await this.server.start();
|
|
74
|
+
if (this.config.development && this.routeScanner) {
|
|
75
|
+
this.routeScanner.enableWatch(async () => {
|
|
76
|
+
await this.discoverRoutes();
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return bunServer;
|
|
80
|
+
}
|
|
81
|
+
async discoverRoutes() {
|
|
82
|
+
const routesDir = this.config.routesDir || './routes';
|
|
83
|
+
if (!this.routeScanner) {
|
|
84
|
+
this.routeScanner = new RouteScanner(routesDir);
|
|
85
|
+
}
|
|
86
|
+
if (!this.routeGenerator) {
|
|
87
|
+
this.routeGenerator = new RouteGenerator();
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const routes = await this.routeScanner.scan();
|
|
91
|
+
if (routes.length > 0) {
|
|
92
|
+
if (this.config.development) {
|
|
93
|
+
await this.routeGenerator.generate(routes);
|
|
94
|
+
}
|
|
95
|
+
for (const route of routes) {
|
|
96
|
+
try {
|
|
97
|
+
// Convert Windows paths to URLs for import
|
|
98
|
+
const importPath = process.platform === 'win32'
|
|
99
|
+
? `file:///${route.path.replace(/\\/g, '/')}`
|
|
100
|
+
: route.path;
|
|
101
|
+
const module = await import(importPath);
|
|
102
|
+
const exported = route.name === 'default' ? module.default : module[route.name];
|
|
103
|
+
if (exported) {
|
|
104
|
+
if (this.isRouteEntry(exported)) {
|
|
105
|
+
this.router.addRoute(exported);
|
|
106
|
+
this.logRouteLoaded(exported);
|
|
107
|
+
}
|
|
108
|
+
else if (typeof exported === 'function') {
|
|
109
|
+
this.router.route(route.options, exported);
|
|
110
|
+
this.logRouteLoaded(route.options);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error(`Failed to load route ${route.name} from ${route.path}:`, error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Ensure routes are properly sorted after loading all
|
|
119
|
+
this.router.sortRoutes();
|
|
120
|
+
console.log(`✅ Loaded ${routes.length} routes from ${routesDir}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error.code !== 'ENOENT') {
|
|
125
|
+
console.error('Failed to discover routes:', error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async loadRoute(routeModule) {
|
|
130
|
+
if (typeof routeModule === 'function') {
|
|
131
|
+
const routeEntry = routeModule();
|
|
132
|
+
if (Array.isArray(routeEntry)) {
|
|
133
|
+
this.router.addRoute(routeEntry);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (routeModule && typeof routeModule === 'object') {
|
|
137
|
+
for (const [, value] of Object.entries(routeModule)) {
|
|
138
|
+
if (typeof value === 'function') {
|
|
139
|
+
const routeEntry = value();
|
|
140
|
+
if (Array.isArray(routeEntry)) {
|
|
141
|
+
this.router.addRoute(routeEntry);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
isRouteEntry(value) {
|
|
148
|
+
return Array.isArray(value) && value.length >= 3;
|
|
149
|
+
}
|
|
150
|
+
logRouteLoaded(route) {
|
|
151
|
+
if (Array.isArray(route)) {
|
|
152
|
+
console.log(` ✓ Loaded route: ${route[0]} ${route[3] || route[1]}`);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
console.log(` ✓ Loaded route: ${route.method} ${route.path}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
stop() {
|
|
159
|
+
if (this.server) {
|
|
160
|
+
this.server.stop();
|
|
161
|
+
this.server = null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
getServer() {
|
|
165
|
+
return this.server;
|
|
166
|
+
}
|
|
167
|
+
getRouter() {
|
|
168
|
+
return this.router;
|
|
169
|
+
}
|
|
170
|
+
getCacheManager() {
|
|
171
|
+
return this.cacheManager;
|
|
172
|
+
}
|
|
173
|
+
getAuthManager() {
|
|
174
|
+
return this.authManager;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const vector = Vector.getInstance();
|
|
178
|
+
export default vector;
|
|
179
|
+
//# sourceMappingURL=vector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.js","sourceRoot":"","sources":["../../src/core/vector.ts"],"names":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,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACvE,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,GAAG,CAAC,qBAAqB,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAED,IAAI;QACF,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;YACnB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACrB,CAAC;IACH,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,eAAe;QACb,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,cAAc;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;CACF;AAED,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;AAEpC,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { GeneratedRoute } from '../types';
|
|
2
|
+
export declare class RouteGenerator {
|
|
3
|
+
private outputPath;
|
|
4
|
+
constructor(outputPath?: string);
|
|
5
|
+
generate(routes: GeneratedRoute[]): Promise<void>;
|
|
6
|
+
generateDynamic(routes: GeneratedRoute[]): Promise<string>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=route-generator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-generator.d.ts","sourceRoot":"","sources":["../../src/dev/route-generator.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,qBAAa,cAAc;IACzB,OAAO,CAAC,UAAU,CAAS;gBAEf,UAAU,SAAkC;IAIlD,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA2DjD,eAAe,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;CAsBjE"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, relative } from 'node:path';
|
|
3
|
+
export class RouteGenerator {
|
|
4
|
+
outputPath;
|
|
5
|
+
constructor(outputPath = './.vector/routes.generated.ts') {
|
|
6
|
+
this.outputPath = outputPath;
|
|
7
|
+
}
|
|
8
|
+
async generate(routes) {
|
|
9
|
+
const outputDir = dirname(this.outputPath);
|
|
10
|
+
await mkdir(outputDir, { recursive: true });
|
|
11
|
+
const imports = [];
|
|
12
|
+
const groupedByFile = new Map();
|
|
13
|
+
for (const route of routes) {
|
|
14
|
+
if (!groupedByFile.has(route.path)) {
|
|
15
|
+
groupedByFile.set(route.path, []);
|
|
16
|
+
}
|
|
17
|
+
groupedByFile.get(route.path).push(route);
|
|
18
|
+
}
|
|
19
|
+
let importIndex = 0;
|
|
20
|
+
const routeEntries = [];
|
|
21
|
+
for (const [filePath, fileRoutes] of groupedByFile) {
|
|
22
|
+
const relativePath = relative(dirname(this.outputPath), filePath)
|
|
23
|
+
.replace(/\\/g, '/')
|
|
24
|
+
.replace(/\.(ts|js)$/, '');
|
|
25
|
+
const importName = `route_${importIndex++}`;
|
|
26
|
+
const namedImports = fileRoutes.filter((r) => r.name !== 'default').map((r) => r.name);
|
|
27
|
+
if (fileRoutes.some((r) => r.name === 'default')) {
|
|
28
|
+
if (namedImports.length > 0) {
|
|
29
|
+
imports.push(`import ${importName}, { ${namedImports.join(', ')} } from '${relativePath}';`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
imports.push(`import ${importName} from '${relativePath}';`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (namedImports.length > 0) {
|
|
36
|
+
imports.push(`import { ${namedImports.join(', ')} } from '${relativePath}';`);
|
|
37
|
+
}
|
|
38
|
+
for (const route of fileRoutes) {
|
|
39
|
+
const routeVar = route.name === 'default' ? importName : route.name;
|
|
40
|
+
routeEntries.push(` ${routeVar},`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const content = `// This file is auto-generated. Do not edit manually.
|
|
44
|
+
// Generated at: ${new Date().toISOString()}
|
|
45
|
+
|
|
46
|
+
${imports.join('\n')}
|
|
47
|
+
|
|
48
|
+
export const routes = [
|
|
49
|
+
${routeEntries.join('\n')}
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
export default routes;
|
|
53
|
+
`;
|
|
54
|
+
await writeFile(this.outputPath, content, 'utf-8');
|
|
55
|
+
console.log(`Generated routes file: ${this.outputPath}`);
|
|
56
|
+
}
|
|
57
|
+
async generateDynamic(routes) {
|
|
58
|
+
const routeEntries = [];
|
|
59
|
+
for (const route of routes) {
|
|
60
|
+
const routeObj = JSON.stringify({
|
|
61
|
+
method: route.method,
|
|
62
|
+
path: route.options.path,
|
|
63
|
+
options: route.options,
|
|
64
|
+
});
|
|
65
|
+
routeEntries.push(` await import('${route.path}').then(m => ({
|
|
66
|
+
...${routeObj},
|
|
67
|
+
handler: m.${route.name === 'default' ? 'default' : route.name}
|
|
68
|
+
}))`);
|
|
69
|
+
}
|
|
70
|
+
return `export const loadRoutes = async () => {
|
|
71
|
+
return Promise.all([
|
|
72
|
+
${routeEntries.join(',\n')}
|
|
73
|
+
]);
|
|
74
|
+
};`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=route-generator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-generator.js","sourceRoot":"","sources":["../../src/dev/route-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,OA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0B,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;IAC3D,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,MAAwB;QAC5C,MAAM,YAAY,GAAa,EAAE,CAAC;QAElC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC;gBAC9B,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI;gBACxB,OAAO,EAAE,KAAK,CAAC,OAAO;aACvB,CAAC,CAAC;YAEH,YAAY,CAAC,IAAI,CAAC,mBAAmB,KAAK,CAAC,IAAI;aACxC,QAAQ;qBACA,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI;UAC5D,CAAC,CAAC;QACR,CAAC;QAED,OAAO;;EAET,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC;;GAEvB,CAAC;IACF,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GeneratedRoute } from '../types';
|
|
2
|
+
export declare class RouteScanner {
|
|
3
|
+
private routesDir;
|
|
4
|
+
constructor(routesDir?: string);
|
|
5
|
+
scan(): Promise<GeneratedRoute[]>;
|
|
6
|
+
private scanDirectory;
|
|
7
|
+
enableWatch(callback: () => void): void;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=route-scanner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-scanner.d.ts","sourceRoot":"","sources":["../../src/dev/route-scanner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,qBAAa,YAAY;IACvB,OAAO,CAAC,SAAS,CAAS;gBAEd,SAAS,SAAa;IAI5B,IAAI,IAAI,OAAO,CAAC,cAAc,EAAE,CAAC;YAgBzB,aAAa;IA4D3B,WAAW,CAAC,QAAQ,EAAE,MAAM,IAAI;CASjC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
3
|
+
export class RouteScanner {
|
|
4
|
+
routesDir;
|
|
5
|
+
constructor(routesDir = './routes') {
|
|
6
|
+
this.routesDir = resolve(process.cwd(), routesDir);
|
|
7
|
+
}
|
|
8
|
+
async scan() {
|
|
9
|
+
const routes = [];
|
|
10
|
+
try {
|
|
11
|
+
await this.scanDirectory(this.routesDir, routes);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error.code === 'ENOENT') {
|
|
15
|
+
console.warn(`Routes directory not found: ${this.routesDir}`);
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
return routes;
|
|
21
|
+
}
|
|
22
|
+
async scanDirectory(dir, routes, basePath = '') {
|
|
23
|
+
const entries = await readdir(dir);
|
|
24
|
+
for (const entry of entries) {
|
|
25
|
+
const fullPath = join(dir, entry);
|
|
26
|
+
const stats = await stat(fullPath);
|
|
27
|
+
if (stats.isDirectory()) {
|
|
28
|
+
const newBasePath = basePath ? `${basePath}/${entry}` : entry;
|
|
29
|
+
await this.scanDirectory(fullPath, routes, newBasePath);
|
|
30
|
+
}
|
|
31
|
+
else if (entry.endsWith('.ts') || entry.endsWith('.js')) {
|
|
32
|
+
const routePath = relative(this.routesDir, fullPath)
|
|
33
|
+
.replace(/\.(ts|js)$/, '')
|
|
34
|
+
.split(sep)
|
|
35
|
+
.join('/');
|
|
36
|
+
try {
|
|
37
|
+
// Convert Windows paths to URLs for import
|
|
38
|
+
const importPath = process.platform === 'win32' ? `file:///${fullPath.replace(/\\/g, '/')}` : fullPath;
|
|
39
|
+
const module = await import(importPath);
|
|
40
|
+
if (module.default && typeof module.default === 'function') {
|
|
41
|
+
routes.push({
|
|
42
|
+
name: 'default',
|
|
43
|
+
path: fullPath,
|
|
44
|
+
method: 'GET',
|
|
45
|
+
options: {
|
|
46
|
+
method: 'GET',
|
|
47
|
+
path: `/${routePath}`,
|
|
48
|
+
expose: true,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
for (const [name, value] of Object.entries(module)) {
|
|
53
|
+
if (name === 'default')
|
|
54
|
+
continue;
|
|
55
|
+
if (Array.isArray(value) && value.length >= 4) {
|
|
56
|
+
const [method, , , path] = value;
|
|
57
|
+
routes.push({
|
|
58
|
+
name,
|
|
59
|
+
path: fullPath,
|
|
60
|
+
method: method,
|
|
61
|
+
options: {
|
|
62
|
+
method: method,
|
|
63
|
+
path: path,
|
|
64
|
+
expose: true,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error(`Failed to load route from ${fullPath}:`, error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
enableWatch(callback) {
|
|
77
|
+
if (typeof Bun !== 'undefined' && Bun.env.NODE_ENV === 'development') {
|
|
78
|
+
console.log(`Watching for route changes in ${this.routesDir}`);
|
|
79
|
+
setInterval(async () => {
|
|
80
|
+
await callback();
|
|
81
|
+
}, 1000);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=route-scanner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-scanner.js","sourceRoot":"","sources":["../../src/dev/route-scanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAGzD,MAAM,OAAO,YAAY;IACf,SAAS,CAAS;IAE1B,Y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;oCACP,MAAM,EAAE,MAAgB;oCACxB,IAAI,EAAE,IAAc;oCACpB,MAAM,EAAE,IAAI;iCACb;6BACF,CAAC,CAAC;wBACL,CAAC;oBACH,CAAC;gBACH,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,QAAQ,GAAG,EAAE,KAAK,CAAC,CAAC;gBACjE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,WAAW,CAAC,QAAoB;QAC9B,IAAI,OAAO,GAAG,KAAK,WAAW,IAAI,GAAG,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EAAE,CAAC;YACrE,OAAO,CAAC,GAAG,CAAC,iCAAiC,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;YAE/D,WAAW,CAAC,KAAK,IAAI,EAAE;gBACrB,MAAM,QAAQ,EAAE,CAAC;YACnB,CAAC,EAAE,IAAI,CAAC,CAAC;QACX,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare class VectorError extends Error {
|
|
2
|
+
readonly code: string;
|
|
3
|
+
readonly statusCode?: number | undefined;
|
|
4
|
+
constructor(message: string, code: string, statusCode?: number | undefined);
|
|
5
|
+
}
|
|
6
|
+
export declare class AuthenticationError extends VectorError {
|
|
7
|
+
constructor(message?: string);
|
|
8
|
+
}
|
|
9
|
+
export declare class ValidationError extends VectorError {
|
|
10
|
+
readonly field?: string | undefined;
|
|
11
|
+
constructor(message: string, field?: string | undefined);
|
|
12
|
+
}
|
|
13
|
+
export declare class RouteNotFoundError extends VectorError {
|
|
14
|
+
constructor(path: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class ConfigurationError extends VectorError {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
export declare class ServerError extends VectorError {
|
|
20
|
+
constructor(message?: string);
|
|
21
|
+
}
|
|
22
|
+
export declare function isVectorError(error: unknown): error is VectorError;
|
|
23
|
+
export declare function handleError(error: unknown): Response;
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;aAGlB,IAAI,EAAE,MAAM;aACZ,UAAU,CAAC,EAAE,MAAM;gBAFnC,OAAO,EAAE,MAAM,EACC,IAAI,EAAE,MAAM,EACZ,UAAU,CAAC,EAAE,MAAM,YAAA;CAKtC;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,SAA0B;CAI9C;AAED,qBAAa,eAAgB,SAAQ,WAAW;aAG5B,KAAK,CAAC,EAAE,MAAM;gBAD9B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,MAAM,YAAA;CAKjC;AAED,qBAAa,kBAAmB,SAAQ,WAAW;gBACrC,IAAI,EAAE,MAAM;CAIzB;AAED,qBAAa,kBAAmB,SAAQ,WAAW;gBACrC,OAAO,EAAE,MAAM;CAI5B;AAED,qBAAa,WAAY,SAAQ,WAAW;gBAC9B,OAAO,SAA0B;CAI9C;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,WAAW,CAElE;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,QAAQ,CAqCpD"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export class VectorError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
statusCode;
|
|
4
|
+
constructor(message, code, statusCode) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.name = 'VectorError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class AuthenticationError extends VectorError {
|
|
12
|
+
constructor(message = 'Authentication failed') {
|
|
13
|
+
super(message, 'AUTH_ERROR', 401);
|
|
14
|
+
this.name = 'AuthenticationError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class ValidationError extends VectorError {
|
|
18
|
+
field;
|
|
19
|
+
constructor(message, field) {
|
|
20
|
+
super(message, 'VALIDATION_ERROR', 400);
|
|
21
|
+
this.field = field;
|
|
22
|
+
this.name = 'ValidationError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class RouteNotFoundError extends VectorError {
|
|
26
|
+
constructor(path) {
|
|
27
|
+
super(`Route not found: ${path}`, 'ROUTE_NOT_FOUND', 404);
|
|
28
|
+
this.name = 'RouteNotFoundError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class ConfigurationError extends VectorError {
|
|
32
|
+
constructor(message) {
|
|
33
|
+
super(message, 'CONFIG_ERROR');
|
|
34
|
+
this.name = 'ConfigurationError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class ServerError extends VectorError {
|
|
38
|
+
constructor(message = 'Internal server error') {
|
|
39
|
+
super(message, 'SERVER_ERROR', 500);
|
|
40
|
+
this.name = 'ServerError';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function isVectorError(error) {
|
|
44
|
+
return error instanceof VectorError;
|
|
45
|
+
}
|
|
46
|
+
export function handleError(error) {
|
|
47
|
+
if (isVectorError(error)) {
|
|
48
|
+
return new Response(JSON.stringify({
|
|
49
|
+
error: error.message,
|
|
50
|
+
code: error.code,
|
|
51
|
+
}), {
|
|
52
|
+
status: error.statusCode || 500,
|
|
53
|
+
headers: { 'content-type': 'application/json' },
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
if (error instanceof Error) {
|
|
57
|
+
return new Response(JSON.stringify({
|
|
58
|
+
error: error.message,
|
|
59
|
+
code: 'UNKNOWN_ERROR',
|
|
60
|
+
}), {
|
|
61
|
+
status: 500,
|
|
62
|
+
headers: { 'content-type': 'application/json' },
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return new Response(JSON.stringify({
|
|
66
|
+
error: 'An unknown error occurred',
|
|
67
|
+
code: 'UNKNOWN_ERROR',
|
|
68
|
+
}), {
|
|
69
|
+
status: 500,
|
|
70
|
+
headers: { 'content-type': 'application/json' },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/errors/index.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,WAAY,SAAQ,KAAK;IAGlB;IACA;IAHlB,YACE,OAAe,EACC,IAAY,EACZ,UAAmB;QAEnC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,SAAI,GAAJ,IAAI,CAAQ;QACZ,eAAU,GAAV,UAAU,CAAS;QAGnC,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,WAAW;IAClD,YAAY,OAAO,GAAG,uBAAuB;QAC3C,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED,MAAM,OAAO,eAAgB,SAAQ,WAAW;IAG5B;IAFlB,YACE,OAAe,EACC,KAAc;QAE9B,KAAK,CAAC,OAAO,EAAE,kBAAkB,EAAE,GAAG,CAAC,CAAC;QAFxB,UAAK,GAAL,KAAK,CAAS;QAG9B,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,WAAW;IACjD,YAAY,IAAY;QACtB,KAAK,CAAC,oBAAoB,IAAI,EAAE,EAAE,iBAAiB,EAAE,GAAG,CAAC,CAAC;QAC1D,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;IACnC,CAAC;CACF;AAED,MAAM,OAAO,kBAAmB,SAAQ,WAAW;IACjD,YAAY,OAAe;QACzB,KAAK,CA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