bxo 0.0.5-dev.81 → 0.0.5-dev.83
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/package.json +1 -1
- package/src/index.ts +36 -35
- package/test-bun-file.ts +0 -0
- package/test-url-encoding.ts +1 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BunFile } from "bun";
|
|
1
2
|
import { z } from "zod";
|
|
2
3
|
|
|
3
4
|
type Method =
|
|
@@ -75,7 +76,7 @@ export type Context<P extends string = string, S extends RouteSchema | undefined
|
|
|
75
76
|
headers: S extends RouteSchema ? InferOr<S["headers"], HeaderObject> : HeaderObject;
|
|
76
77
|
cookies: S extends RouteSchema ? InferOr<S["cookies"], CookieObject> : CookieObject;
|
|
77
78
|
body: S extends RouteSchema ? InferOr<S["body"], unknown> : unknown;
|
|
78
|
-
set: {
|
|
79
|
+
set: {
|
|
79
80
|
headers: Record<string, string | string[]>;
|
|
80
81
|
cookie: (name: string, value: string, options?: CookieOptions) => void;
|
|
81
82
|
};
|
|
@@ -88,7 +89,7 @@ type AnyHandler = (ctx: Context<any, any>, app: BXO) => Response | string | Prom
|
|
|
88
89
|
type Handler<P extends string, S extends RouteSchema | undefined = undefined> = (
|
|
89
90
|
ctx: Context<P, S>,
|
|
90
91
|
app: BXO
|
|
91
|
-
) => Response | string | Promise<Response | string
|
|
92
|
+
) => Response | string | BunFile | Promise<Response | string | BunFile>
|
|
92
93
|
|
|
93
94
|
// WebSocket handler types
|
|
94
95
|
export type WebSocketHandler<T = any> = {
|
|
@@ -134,37 +135,37 @@ function parseCookies(cookieHeader: string | null): CookieObject {
|
|
|
134
135
|
|
|
135
136
|
function serializeCookie(name: string, value: string, options: CookieOptions = {}): string {
|
|
136
137
|
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
137
|
-
|
|
138
|
+
|
|
138
139
|
if (options.domain) {
|
|
139
140
|
cookie += `; Domain=${options.domain}`;
|
|
140
141
|
}
|
|
141
|
-
|
|
142
|
+
|
|
142
143
|
if (options.path) {
|
|
143
144
|
cookie += `; Path=${options.path}`;
|
|
144
145
|
} else {
|
|
145
146
|
cookie += `; Path=/`;
|
|
146
147
|
}
|
|
147
|
-
|
|
148
|
+
|
|
148
149
|
if (options.expires) {
|
|
149
150
|
cookie += `; Expires=${options.expires.toUTCString()}`;
|
|
150
151
|
}
|
|
151
|
-
|
|
152
|
+
|
|
152
153
|
if (options.maxAge !== undefined) {
|
|
153
154
|
cookie += `; Max-Age=${options.maxAge}`;
|
|
154
155
|
}
|
|
155
|
-
|
|
156
|
+
|
|
156
157
|
if (options.httpOnly) {
|
|
157
158
|
cookie += `; HttpOnly`;
|
|
158
159
|
}
|
|
159
|
-
|
|
160
|
+
|
|
160
161
|
if (options.secure) {
|
|
161
162
|
cookie += `; Secure`;
|
|
162
163
|
}
|
|
163
|
-
|
|
164
|
+
|
|
164
165
|
if (options.sameSite) {
|
|
165
166
|
cookie += `; SameSite=${options.sameSite}`;
|
|
166
167
|
}
|
|
167
|
-
|
|
168
|
+
|
|
168
169
|
return cookie;
|
|
169
170
|
}
|
|
170
171
|
|
|
@@ -187,11 +188,11 @@ function parseQuery(searchParams: URLSearchParams, schema?: z.ZodTypeAny): any {
|
|
|
187
188
|
|
|
188
189
|
function formDataToObject(fd: FormData): Record<string, any> {
|
|
189
190
|
const obj: Record<string, any> = {};
|
|
190
|
-
|
|
191
|
+
|
|
191
192
|
for (const [key, value] of fd.entries()) {
|
|
192
193
|
setNestedValue(obj, key, value);
|
|
193
194
|
}
|
|
194
|
-
|
|
195
|
+
|
|
195
196
|
return obj;
|
|
196
197
|
}
|
|
197
198
|
|
|
@@ -201,68 +202,68 @@ function setNestedValue(obj: Record<string, any>, key: string, value: any): void
|
|
|
201
202
|
if (deepArrayMatch) {
|
|
202
203
|
const [, arrayKey, index, propertyKey] = deepArrayMatch;
|
|
203
204
|
const arrayIndex = parseInt(index, 10);
|
|
204
|
-
|
|
205
|
+
|
|
205
206
|
if (!obj[arrayKey]) {
|
|
206
207
|
obj[arrayKey] = [];
|
|
207
208
|
}
|
|
208
|
-
|
|
209
|
+
|
|
209
210
|
// Ensure it's an array
|
|
210
211
|
if (!Array.isArray(obj[arrayKey])) {
|
|
211
212
|
obj[arrayKey] = [];
|
|
212
213
|
}
|
|
213
|
-
|
|
214
|
+
|
|
214
215
|
// Ensure the object at the index exists
|
|
215
216
|
if (!obj[arrayKey][arrayIndex]) {
|
|
216
217
|
obj[arrayKey][arrayIndex] = {};
|
|
217
218
|
}
|
|
218
|
-
|
|
219
|
+
|
|
219
220
|
// Ensure it's an object
|
|
220
221
|
if (typeof obj[arrayKey][arrayIndex] !== 'object' || Array.isArray(obj[arrayKey][arrayIndex])) {
|
|
221
222
|
obj[arrayKey][arrayIndex] = {};
|
|
222
223
|
}
|
|
223
|
-
|
|
224
|
+
|
|
224
225
|
obj[arrayKey][arrayIndex][propertyKey] = value;
|
|
225
226
|
return;
|
|
226
227
|
}
|
|
227
|
-
|
|
228
|
+
|
|
228
229
|
// Handle array notation like items[0], items[1]
|
|
229
230
|
const arrayMatch = key.match(/^(.+)\[(\d+)\]$/);
|
|
230
231
|
if (arrayMatch) {
|
|
231
232
|
const [, arrayKey, index] = arrayMatch;
|
|
232
233
|
const arrayIndex = parseInt(index, 10);
|
|
233
|
-
|
|
234
|
+
|
|
234
235
|
if (!obj[arrayKey]) {
|
|
235
236
|
obj[arrayKey] = [];
|
|
236
237
|
}
|
|
237
|
-
|
|
238
|
+
|
|
238
239
|
// Ensure it's an array
|
|
239
240
|
if (!Array.isArray(obj[arrayKey])) {
|
|
240
241
|
obj[arrayKey] = [];
|
|
241
242
|
}
|
|
242
|
-
|
|
243
|
+
|
|
243
244
|
// Set the value at the specific index
|
|
244
245
|
obj[arrayKey][arrayIndex] = value;
|
|
245
246
|
return;
|
|
246
247
|
}
|
|
247
|
-
|
|
248
|
+
|
|
248
249
|
// Handle nested object notation like profile[name], profile[age]
|
|
249
250
|
const nestedMatch = key.match(/^(.+)\[([^\]]+)\]$/);
|
|
250
251
|
if (nestedMatch) {
|
|
251
252
|
const [, parentKey, nestedKey] = nestedMatch;
|
|
252
|
-
|
|
253
|
+
|
|
253
254
|
if (!obj[parentKey]) {
|
|
254
255
|
obj[parentKey] = {};
|
|
255
256
|
}
|
|
256
|
-
|
|
257
|
+
|
|
257
258
|
// Ensure it's an object
|
|
258
259
|
if (typeof obj[parentKey] !== 'object' || Array.isArray(obj[parentKey])) {
|
|
259
260
|
obj[parentKey] = {};
|
|
260
261
|
}
|
|
261
|
-
|
|
262
|
+
|
|
262
263
|
obj[parentKey][nestedKey] = value;
|
|
263
264
|
return;
|
|
264
265
|
}
|
|
265
|
-
|
|
266
|
+
|
|
266
267
|
// Handle simple keys - check for duplicates to convert to arrays
|
|
267
268
|
if (key in obj) {
|
|
268
269
|
const existing = obj[key];
|
|
@@ -411,7 +412,7 @@ export default class BXO {
|
|
|
411
412
|
start(): void {
|
|
412
413
|
// Check if we have any WebSocket routes
|
|
413
414
|
const hasWebSocketRoutes = this.routes.some(r => r.method === "WS");
|
|
414
|
-
|
|
415
|
+
|
|
415
416
|
// Build a basic routes map for Bun's native routes (exact paths only)
|
|
416
417
|
const nativeRoutes: Record<string, Record<string, (req: Request) => Promise<Response> | Response>> = {};
|
|
417
418
|
|
|
@@ -473,7 +474,7 @@ export default class BXO {
|
|
|
473
474
|
}
|
|
474
475
|
}
|
|
475
476
|
}
|
|
476
|
-
|
|
477
|
+
|
|
477
478
|
// Handle regular HTTP requests
|
|
478
479
|
return this.dispatchAny(req, nativeRoutes);
|
|
479
480
|
}
|
|
@@ -702,7 +703,7 @@ export default class BXO {
|
|
|
702
703
|
headers: headerObj,
|
|
703
704
|
cookies: cookieObj,
|
|
704
705
|
body: bodyObj,
|
|
705
|
-
set: {
|
|
706
|
+
set: {
|
|
706
707
|
headers: {},
|
|
707
708
|
cookie: (name: string, value: string, options: CookieOptions = {}) => {
|
|
708
709
|
const cookieString = serializeCookie(name, value, options);
|
|
@@ -727,9 +728,9 @@ export default class BXO {
|
|
|
727
728
|
return new Response(JSON.stringify({ error: "Invalid response", issues: res.error?.issues ?? [] }), { status: 500, headers: { "Content-Type": "application/json" } });
|
|
728
729
|
}
|
|
729
730
|
}
|
|
730
|
-
return new Response(JSON.stringify(data), {
|
|
731
|
-
status,
|
|
732
|
-
headers: { "Content-Type": "application/json" }
|
|
731
|
+
return new Response(JSON.stringify(data), {
|
|
732
|
+
status,
|
|
733
|
+
headers: { "Content-Type": "application/json" }
|
|
733
734
|
});
|
|
734
735
|
},
|
|
735
736
|
text: (data, status = 200) => {
|
|
@@ -740,9 +741,9 @@ export default class BXO {
|
|
|
740
741
|
return new Response(JSON.stringify({ error: "Invalid response", issues: res.error?.issues ?? [] }), { status: 500, headers: { "Content-Type": "application/json" } });
|
|
741
742
|
}
|
|
742
743
|
}
|
|
743
|
-
return new Response(String(data), {
|
|
744
|
-
status,
|
|
745
|
-
headers: { "Content-Type": "text/plain" }
|
|
744
|
+
return new Response(String(data), {
|
|
745
|
+
status,
|
|
746
|
+
headers: { "Content-Type": "text/plain" }
|
|
746
747
|
});
|
|
747
748
|
},
|
|
748
749
|
status: (status, data) => {
|
package/test-bun-file.ts
ADDED
|
File without changes
|
package/test-url-encoding.ts
CHANGED