barejs 0.1.63 → 0.2.3
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/README.md +8 -8
- package/dist/index.js +145 -143
- package/dist/node.js +145 -143
- package/dist/types/core/bare.d.ts +34 -4
- package/dist/types/core/context.d.ts +3 -2
- package/dist/types/runtime/types.d.ts +2 -0
- package/dist/types/runtime/uws.d.ts +13 -0
- package/dist/types/utils/openapi.d.ts +3 -1
- package/dist/types/utils/static.d.ts +4 -0
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/src/core/bare.ts +232 -13
- package/src/core/context.ts +36 -3
- package/src/runtime/bun.ts +39 -31
- package/src/runtime/node.ts +27 -3
- package/src/runtime/types.ts +2 -0
- package/src/runtime/uws.ts +655 -0
- package/src/security/auth.ts +11 -6
- package/src/utils/openapi.ts +19 -2
- package/src/utils/static.ts +37 -3
package/src/utils/openapi.ts
CHANGED
|
@@ -69,6 +69,8 @@ const extractSchema = (schema: any): Record<string, any> => {
|
|
|
69
69
|
export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {}) => {
|
|
70
70
|
const paths: Record<string, Record<string, any>> = {};
|
|
71
71
|
const excluded = new Set<string>(['/docs', '/docs/json', ...(options.excludePaths || [])]);
|
|
72
|
+
let usesBearer = false;
|
|
73
|
+
let usesBasic = false;
|
|
72
74
|
|
|
73
75
|
for (const route of routes) {
|
|
74
76
|
if (excluded.has(route.path)) continue;
|
|
@@ -86,9 +88,18 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
86
88
|
let responseSchema: any = undefined;
|
|
87
89
|
let detail: any = undefined;
|
|
88
90
|
let paramSchemaObj: Record<string, any> | null = null;
|
|
91
|
+
const routeSecurity: any[] = [];
|
|
89
92
|
|
|
90
93
|
for (const h of route.handlers) {
|
|
91
94
|
if (!h) continue;
|
|
95
|
+
if ((h as any).__isBareAuth) {
|
|
96
|
+
usesBearer = true;
|
|
97
|
+
routeSecurity.push({ bearerAuth: [] });
|
|
98
|
+
}
|
|
99
|
+
if ((h as any).__isBasicAuth) {
|
|
100
|
+
usesBasic = true;
|
|
101
|
+
routeSecurity.push({ basicAuth: [] });
|
|
102
|
+
}
|
|
92
103
|
const rs = (h as any).__routeSchemas;
|
|
93
104
|
if (rs) {
|
|
94
105
|
if (rs.detail) detail = rs.detail;
|
|
@@ -144,6 +155,7 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
144
155
|
summary: detail?.summary || `${route.method} ${openApiPath}`,
|
|
145
156
|
...(detail?.description ? { description: detail.description } : {}),
|
|
146
157
|
...(detail?.tags ? { tags: detail.tags } : {}),
|
|
158
|
+
...(routeSecurity.length ? { security: routeSecurity } : {}),
|
|
147
159
|
...(allParams.length ? { parameters: allParams } : {}),
|
|
148
160
|
...(requestBody ? { requestBody } : {}),
|
|
149
161
|
responses: {
|
|
@@ -157,6 +169,11 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
157
169
|
};
|
|
158
170
|
}
|
|
159
171
|
|
|
172
|
+
const autoSchemes: Record<string, any> = {};
|
|
173
|
+
if (usesBearer) autoSchemes.bearerAuth = { type: 'http', scheme: 'bearer', bearerFormat: 'JWT' };
|
|
174
|
+
if (usesBasic) autoSchemes.basicAuth = { type: 'http', scheme: 'basic' };
|
|
175
|
+
const mergedSchemes = { ...autoSchemes, ...(options.securitySchemes || {}) };
|
|
176
|
+
|
|
160
177
|
return {
|
|
161
178
|
openapi: '3.1.0',
|
|
162
179
|
info: {
|
|
@@ -166,8 +183,8 @@ export const generateOpenAPISpec = (routes: any[], options: OpenAPIOptions = {})
|
|
|
166
183
|
},
|
|
167
184
|
...(options.servers?.length ? { servers: options.servers } : {}),
|
|
168
185
|
...(options.tags?.length ? { tags: options.tags } : {}),
|
|
169
|
-
...(
|
|
170
|
-
? { components: { securitySchemes:
|
|
186
|
+
...(Object.keys(mergedSchemes).length > 0
|
|
187
|
+
? { components: { securitySchemes: mergedSchemes } }
|
|
171
188
|
: {}),
|
|
172
189
|
...(options.security?.length ? { security: options.security } : {}),
|
|
173
190
|
paths
|
package/src/utils/static.ts
CHANGED
|
@@ -14,6 +14,10 @@ export interface StaticOptions {
|
|
|
14
14
|
etag?: boolean;
|
|
15
15
|
/** Optional LRU metadata cache for realpath, stat, and ETag to avoid repeated syscalls */
|
|
16
16
|
cacheMetadata?: boolean | StaticMetadataCacheOptions;
|
|
17
|
+
/** Enable Single Page Application fallback to index.html for unmatched GET/HEAD routes */
|
|
18
|
+
spa?: boolean;
|
|
19
|
+
/** Custom fallback file relative to root when spa is true (default: 'index.html') */
|
|
20
|
+
spaFallback?: string;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
interface StaticMetaEntry {
|
|
@@ -31,12 +35,42 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
|
|
|
31
35
|
const maxAge = options.maxAge ?? 3600;
|
|
32
36
|
const enableEtag = options.etag !== false;
|
|
33
37
|
|
|
34
|
-
const cacheOpt = options.cacheMetadata
|
|
38
|
+
const cacheOpt = options.cacheMetadata !== undefined
|
|
39
|
+
? options.cacheMetadata
|
|
40
|
+
: process.env.NODE_ENV === 'production';
|
|
35
41
|
const useCache = Boolean(cacheOpt);
|
|
36
42
|
const cacheMax = typeof cacheOpt === 'object' ? (cacheOpt.maxEntries ?? 1000) : 1000;
|
|
37
43
|
const cacheTtl = typeof cacheOpt === 'object' ? (cacheOpt.ttlMs ?? 30_000) : 30_000;
|
|
38
44
|
const metaCache = useCache ? new Map<string, StaticMetaEntry>() : null;
|
|
39
45
|
|
|
46
|
+
const serveSpaFallback = async (next: any): Promise<Response | undefined> => {
|
|
47
|
+
const downstream = await next?.();
|
|
48
|
+
if (downstream instanceof Response && downstream.status !== 404) return downstream;
|
|
49
|
+
if (!options.spa) return downstream;
|
|
50
|
+
const fallbackName = (options.spaFallback ?? 'index.html').replace(/^\/+/, '');
|
|
51
|
+
const fallbackPath = resolve(rootDir, fallbackName);
|
|
52
|
+
if (fallbackPath !== rootDir && !fallbackPath.startsWith(rootPrefix)) return downstream;
|
|
53
|
+
try {
|
|
54
|
+
const [realRoot, resolvedFallback] = await Promise.all([
|
|
55
|
+
(realRootPromise ??= realpath(rootDir)),
|
|
56
|
+
realpath(fallbackPath)
|
|
57
|
+
]);
|
|
58
|
+
const realPrefix = realRoot.endsWith(sep) ? realRoot : realRoot + sep;
|
|
59
|
+
if (resolvedFallback !== realRoot && !resolvedFallback.startsWith(realPrefix)) return downstream;
|
|
60
|
+
const st = await stat(resolvedFallback);
|
|
61
|
+
if (st.isDirectory()) return downstream;
|
|
62
|
+
const file = runtime.openFile(resolvedFallback);
|
|
63
|
+
return new Response(file.full(), {
|
|
64
|
+
headers: {
|
|
65
|
+
'Content-Type': file.type || 'text/html; charset=utf-8',
|
|
66
|
+
'Cache-Control': 'no-cache'
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
} catch {
|
|
70
|
+
return downstream;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
40
74
|
const mw = async (ctx: any, next: any) => {
|
|
41
75
|
const method = ctx.req.method;
|
|
42
76
|
if (method !== 'GET' && method !== 'HEAD') return next?.();
|
|
@@ -91,7 +125,7 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
|
|
|
91
125
|
return ctx.status(403).send('Forbidden');
|
|
92
126
|
}
|
|
93
127
|
const st = await stat(resolvedFile);
|
|
94
|
-
if (st.isDirectory()) return next?.();
|
|
128
|
+
if (st.isDirectory()) return (options.spa && ctx._isStaticFallback) ? serveSpaFallback(next) : next?.();
|
|
95
129
|
realFile = resolvedFile;
|
|
96
130
|
fileSize = st.size;
|
|
97
131
|
mtimeMs = Math.floor(st.mtimeMs);
|
|
@@ -112,7 +146,7 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
|
|
|
112
146
|
}
|
|
113
147
|
} catch {
|
|
114
148
|
realRootPromise = null;
|
|
115
|
-
return next?.();
|
|
149
|
+
return (options.spa && ctx._isStaticFallback) ? serveSpaFallback(next) : next?.();
|
|
116
150
|
}
|
|
117
151
|
}
|
|
118
152
|
|