barejs 0.1.47 → 0.1.49
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 +20 -20
- package/README.md +221 -184
- package/dist/index.js +122 -187
- package/dist/types/client/index.d.ts +48 -0
- package/dist/types/core/bare.d.ts +14 -1
- package/dist/types/core/context.d.ts +9 -1
- package/dist/types/core/router.d.ts +1 -0
- package/dist/types/index.d.ts +6 -2
- package/dist/types/security/auth.d.ts +2 -2
- package/dist/types/security/headers.d.ts +9 -0
- package/dist/types/security/rateLimit.d.ts +9 -0
- package/dist/types/utils/openapi.d.ts +15 -0
- package/package.json +68 -68
- package/src/client/index.ts +121 -0
- package/src/core/bare.ts +463 -318
- package/src/core/config.ts +31 -31
- package/src/core/context.ts +205 -111
- package/src/core/loader.ts +12 -12
- package/src/core/radix.ts +184 -171
- package/src/core/router.ts +61 -62
- package/src/index.ts +73 -61
- package/src/security/auth.ts +174 -153
- package/src/security/cors.ts +45 -26
- package/src/security/hash.ts +22 -19
- package/src/security/headers.ts +59 -0
- package/src/security/rateLimit.ts +76 -0
- package/src/security/validators.ts +137 -133
- package/src/utils/openapi.ts +116 -0
- package/src/utils/static.ts +42 -25
package/src/core/config.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
// src/core/config.ts
|
|
2
|
-
|
|
3
|
-
export interface BareConfig {
|
|
4
|
-
hash: {
|
|
5
|
-
algorithm: "argon2id" | "bcrypt";
|
|
6
|
-
memoryCost: number;
|
|
7
|
-
timeCost: number;
|
|
8
|
-
};
|
|
9
|
-
port: number;
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const DEFAULT_CONFIG: BareConfig = {
|
|
14
|
-
hash: {
|
|
15
|
-
algorithm: "argon2id",
|
|
16
|
-
memoryCost: 65536, // 64MB
|
|
17
|
-
timeCost: 2
|
|
18
|
-
},
|
|
19
|
-
port: 3000
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export let BARE_CONFIG = DEFAULT_CONFIG;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export const initConfig = (userConfig: Partial<BareConfig> = {}) => {
|
|
26
|
-
BARE_CONFIG = {
|
|
27
|
-
...DEFAULT_CONFIG,
|
|
28
|
-
...userConfig,
|
|
29
|
-
hash: { ...DEFAULT_CONFIG.hash, ...userConfig.hash }
|
|
30
|
-
};
|
|
31
|
-
return BARE_CONFIG;
|
|
1
|
+
// src/core/config.ts
|
|
2
|
+
|
|
3
|
+
export interface BareConfig {
|
|
4
|
+
hash: {
|
|
5
|
+
algorithm: "argon2id" | "bcrypt";
|
|
6
|
+
memoryCost: number;
|
|
7
|
+
timeCost: number;
|
|
8
|
+
};
|
|
9
|
+
port: number;
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_CONFIG: BareConfig = {
|
|
14
|
+
hash: {
|
|
15
|
+
algorithm: "argon2id",
|
|
16
|
+
memoryCost: 65536, // 64MB
|
|
17
|
+
timeCost: 2
|
|
18
|
+
},
|
|
19
|
+
port: 3000
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export let BARE_CONFIG = DEFAULT_CONFIG;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export const initConfig = (userConfig: Partial<BareConfig> = {}) => {
|
|
26
|
+
BARE_CONFIG = {
|
|
27
|
+
...DEFAULT_CONFIG,
|
|
28
|
+
...userConfig,
|
|
29
|
+
hash: { ...DEFAULT_CONFIG.hash, ...userConfig.hash }
|
|
30
|
+
};
|
|
31
|
+
return BARE_CONFIG;
|
|
32
32
|
};
|
package/src/core/context.ts
CHANGED
|
@@ -1,112 +1,206 @@
|
|
|
1
|
-
export type Params = Record<string, string>;
|
|
2
|
-
export type Next = () => Promise<any> | any;
|
|
3
|
-
export type Middleware = (ctx: Context, next: Next) => Promise<any> | any;
|
|
4
|
-
export type Handler = (ctx: Context) => Promise<any> | any;
|
|
5
|
-
export type GroupCallback = (router: any) => void;
|
|
6
|
-
export type AuthUser = Record<string, any>;
|
|
7
|
-
|
|
8
|
-
export class Context {
|
|
9
|
-
public req!: Request;
|
|
10
|
-
public params: Params = {};
|
|
11
|
-
public _status = 200;
|
|
12
|
-
public _headers?: Record<string, string> | undefined = undefined;
|
|
13
|
-
public store = new Map<string, any>();
|
|
14
|
-
public body: any = undefined;
|
|
15
|
-
public
|
|
16
|
-
constructor() { }
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
this
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
1
|
+
export type Params = Record<string, string>;
|
|
2
|
+
export type Next = () => Promise<any> | any;
|
|
3
|
+
export type Middleware = (ctx: Context, next: Next) => Promise<any> | any;
|
|
4
|
+
export type Handler = (ctx: Context) => Promise<any> | any;
|
|
5
|
+
export type GroupCallback = (router: any) => void;
|
|
6
|
+
export type AuthUser = Record<string, any>;
|
|
7
|
+
|
|
8
|
+
export class Context {
|
|
9
|
+
public req!: Request;
|
|
10
|
+
public params: Params = {};
|
|
11
|
+
public _status = 200;
|
|
12
|
+
public _headers?: Record<string, string> | undefined = undefined;
|
|
13
|
+
public store = new Map<string, any>();
|
|
14
|
+
public body: any = undefined;
|
|
15
|
+
public _query: Record<string, string> | undefined = undefined;
|
|
16
|
+
constructor() { }
|
|
17
|
+
|
|
18
|
+
public get query(): Record<string, string> {
|
|
19
|
+
if (this._query !== undefined) return this._query;
|
|
20
|
+
const result: Record<string, string> = {};
|
|
21
|
+
const url = this.req?.url;
|
|
22
|
+
if (url) {
|
|
23
|
+
const qIdx = url.indexOf('?');
|
|
24
|
+
if (qIdx !== -1) {
|
|
25
|
+
let start = qIdx + 1;
|
|
26
|
+
const len = url.length;
|
|
27
|
+
while (start < len) {
|
|
28
|
+
let amp = url.indexOf('&', start);
|
|
29
|
+
if (amp === -1) amp = len;
|
|
30
|
+
if (amp > start) {
|
|
31
|
+
const eq = url.indexOf('=', start);
|
|
32
|
+
let k: string;
|
|
33
|
+
let v: string;
|
|
34
|
+
if (eq !== -1 && eq < amp) {
|
|
35
|
+
k = url.slice(start, eq);
|
|
36
|
+
v = url.slice(eq + 1, amp);
|
|
37
|
+
} else {
|
|
38
|
+
k = url.slice(start, amp);
|
|
39
|
+
v = '';
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
k = decodeURIComponent(k.replace(/\+/g, ' '));
|
|
43
|
+
v = decodeURIComponent(v.replace(/\+/g, ' '));
|
|
44
|
+
} catch { /* keep raw on malformed URI */ }
|
|
45
|
+
if (k !== '__proto__' && k !== 'constructor' && k !== 'prototype') {
|
|
46
|
+
result[k] = v;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
start = amp + 1;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
this._query = result;
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public getQuery(key: string): string | undefined {
|
|
58
|
+
return this.query[key];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async jsonBody(): Promise<any> {
|
|
62
|
+
if (this.body !== undefined) return this.body;
|
|
63
|
+
|
|
64
|
+
const contentType = this.req.headers.get("content-type");
|
|
65
|
+
if (contentType && contentType.includes("application/json")) {
|
|
66
|
+
try {
|
|
67
|
+
return this.body = await this.req.json();
|
|
68
|
+
} catch {
|
|
69
|
+
return this.body = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return this.body = null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public reset(req: Request): this {
|
|
76
|
+
this.req = req;
|
|
77
|
+
this._status = 200;
|
|
78
|
+
this._headers = undefined;
|
|
79
|
+
if (this.store.size > 0) this.store.clear();
|
|
80
|
+
this.params = {};
|
|
81
|
+
this._query = undefined;
|
|
82
|
+
this.body = undefined;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public status(code: number): this {
|
|
87
|
+
this._status = code;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// public json(data: any): Response {
|
|
92
|
+
// if (this._headers !== undefined) {
|
|
93
|
+
// if (this._headers["content-type"] === undefined) {
|
|
94
|
+
// this._headers["content-type"] = "application/json";
|
|
95
|
+
// }
|
|
96
|
+
// return Response.json(data, {
|
|
97
|
+
// status: this._status,
|
|
98
|
+
// headers: this._headers
|
|
99
|
+
// });
|
|
100
|
+
// }
|
|
101
|
+
// if (this._status === 200) {
|
|
102
|
+
// return Response.json(data);
|
|
103
|
+
// }
|
|
104
|
+
// return Response.json(data, { status: this._status });
|
|
105
|
+
// }
|
|
106
|
+
public json(data: any): Response {
|
|
107
|
+
const status = this._status || 200;
|
|
108
|
+
|
|
109
|
+
// No custom headers
|
|
110
|
+
if (!this._headers) {
|
|
111
|
+
return Response.json(data, { status });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Ensure Content-Type is set to application/json
|
|
115
|
+
if (this._headers["content-type"] === undefined) {
|
|
116
|
+
this._headers["content-type"] = "application/json";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return Response.json(data, {
|
|
120
|
+
status,
|
|
121
|
+
headers: this._headers as any // ⚠️ FIX TS18046: Cast to 'any' to allow assignment to 'unknown' headers
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public set(key: string, value: any): this {
|
|
126
|
+
this.store.set(key, value);
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public get(key: string): any {
|
|
131
|
+
return this.store.get(key);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public setHeader(name: string, value: string): this {
|
|
135
|
+
if (!this._headers) this._headers = {};
|
|
136
|
+
this._headers[name.toLowerCase()] = value;
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
public send(message: string, extra: Record<string, any> = {}): Response {
|
|
142
|
+
const isError = this._status >= 400;
|
|
143
|
+
|
|
144
|
+
return this.json({
|
|
145
|
+
status: isError ? "error" : "success",
|
|
146
|
+
msg: message,
|
|
147
|
+
...extra
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public sse(
|
|
152
|
+
source:
|
|
153
|
+
| AsyncIterable<string | { event?: string; data: any; id?: string; retry?: number }>
|
|
154
|
+
| ((send: (data: any, event?: string, id?: string) => void, close: () => void) => Promise<void> | void)
|
|
155
|
+
): Response {
|
|
156
|
+
const encoder = new TextEncoder();
|
|
157
|
+
const formatChunk = (chunk: string | { event?: string; data: any; id?: string; retry?: number }): Uint8Array => {
|
|
158
|
+
if (typeof chunk === 'string') {
|
|
159
|
+
return encoder.encode(`data: ${chunk}\n\n`);
|
|
160
|
+
}
|
|
161
|
+
let out = '';
|
|
162
|
+
if (chunk.id !== undefined) out += `id: ${chunk.id}\n`;
|
|
163
|
+
if (chunk.event !== undefined) out += `event: ${chunk.event}\n`;
|
|
164
|
+
if (chunk.retry !== undefined) out += `retry: ${chunk.retry}\n`;
|
|
165
|
+
const payload = typeof chunk.data === 'string' ? chunk.data : JSON.stringify(chunk.data);
|
|
166
|
+
out += `data: ${payload}\n\n`;
|
|
167
|
+
return encoder.encode(out);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
171
|
+
async start(controller) {
|
|
172
|
+
try {
|
|
173
|
+
if (typeof source === 'function') {
|
|
174
|
+
let closed = false;
|
|
175
|
+
const send = (data: any, event?: string, id?: string) => {
|
|
176
|
+
if (!closed) controller.enqueue(formatChunk({ data, event, id }));
|
|
177
|
+
};
|
|
178
|
+
const close = () => {
|
|
179
|
+
if (!closed) {
|
|
180
|
+
closed = true;
|
|
181
|
+
controller.close();
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
await source(send, close);
|
|
185
|
+
close();
|
|
186
|
+
} else {
|
|
187
|
+
for await (const item of source) {
|
|
188
|
+
controller.enqueue(formatChunk(item));
|
|
189
|
+
}
|
|
190
|
+
controller.close();
|
|
191
|
+
}
|
|
192
|
+
} catch (err) {
|
|
193
|
+
controller.error(err);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const headers: Record<string, string> = {
|
|
199
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
200
|
+
'cache-control': 'no-cache, no-transform',
|
|
201
|
+
'connection': 'keep-alive',
|
|
202
|
+
...(this._headers || {})
|
|
203
|
+
};
|
|
204
|
+
return new Response(stream, { status: this._status || 200, headers });
|
|
205
|
+
}
|
|
112
206
|
}
|
package/src/core/loader.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
// src/core/loader.ts
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { initConfig } from "./config";
|
|
4
|
-
|
|
5
|
-
export async function bootstrapConfig() {
|
|
6
|
-
const configPath = join(process.cwd(), "bare.config.ts");
|
|
7
|
-
try {
|
|
8
|
-
const userModule = await import(configPath);
|
|
9
|
-
return initConfig(userModule.default);
|
|
10
|
-
} catch {
|
|
11
|
-
return initConfig();
|
|
12
|
-
}
|
|
1
|
+
// src/core/loader.ts
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { initConfig } from "./config";
|
|
4
|
+
|
|
5
|
+
export async function bootstrapConfig() {
|
|
6
|
+
const configPath = join(process.cwd(), "bare.config.ts");
|
|
7
|
+
try {
|
|
8
|
+
const userModule = await import(configPath);
|
|
9
|
+
return initConfig(userModule.default);
|
|
10
|
+
} catch {
|
|
11
|
+
return initConfig();
|
|
12
|
+
}
|
|
13
13
|
}
|