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.
@@ -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
  };
@@ -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 query: Record<string, string> = {};
16
- constructor() { }
17
-
18
-
19
- public async jsonBody(): Promise<any> {
20
-
21
- if (this.body !== undefined) return this.body;
22
-
23
-
24
- const contentType = this.req.headers.get("content-type");
25
- if (contentType && contentType.includes("application/json")) {
26
- try {
27
-
28
- return this.body = await this.req.json();
29
- } catch {
30
- return this.body = null;
31
- }
32
- }
33
- return this.body = null;
34
- }
35
-
36
-
37
- public reset(req: Request): this {
38
- this.req = req;
39
- this._status = 200;
40
- this._headers = undefined;
41
- if (this.store.size > 0) this.store.clear();
42
- this.params = {};
43
- this.query = {};
44
- this.body = undefined;
45
- return this;
46
- }
47
-
48
- public status(code: number): this {
49
- this._status = code;
50
- return this;
51
- }
52
-
53
- // public json(data: any): Response {
54
- // if (this._headers !== undefined) {
55
- // if (this._headers["content-type"] === undefined) {
56
- // this._headers["content-type"] = "application/json";
57
- // }
58
- // return Response.json(data, {
59
- // status: this._status,
60
- // headers: this._headers
61
- // });
62
- // }
63
- // if (this._status === 200) {
64
- // return Response.json(data);
65
- // }
66
- // return Response.json(data, { status: this._status });
67
- // }
68
- public json(data: any): Response {
69
- const status = this._status || 200;
70
-
71
- // No custom headers
72
- if (!this._headers) {
73
- return Response.json(data, { status });
74
- }
75
-
76
- // Ensure Content-Type is set to application/json
77
- if (this._headers["content-type"] === undefined) {
78
- this._headers["content-type"] = "application/json";
79
- }
80
-
81
- return Response.json(data, {
82
- status,
83
- headers: this._headers as any // ⚠️ FIX TS18046: Cast to 'any' to allow assignment to 'unknown' headers
84
- });
85
- }
86
-
87
- public set(key: string, value: any): this {
88
- this.store.set(key, value);
89
- return this;
90
- }
91
-
92
- public get(key: string): any {
93
- return this.store.get(key);
94
- }
95
-
96
- public setHeader(name: string, value: string): this {
97
- if (!this._headers) this._headers = {};
98
- this._headers[name.toLowerCase()] = value;
99
- return this;
100
- }
101
-
102
-
103
- public send(message: string, extra: Record<string, any> = {}): Response {
104
- const isError = this._status >= 400;
105
-
106
- return this.json({
107
- status: isError ? "error" : "success",
108
- msg: message,
109
- ...extra
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
  }
@@ -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
  }