princejs 2.0.0 → 2.1.4
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 → Readme.md} +517 -414
- package/dist/middleware.js +230 -256
- package/dist/prince.d.ts +14 -4
- package/dist/prince.d.ts.map +1 -1
- package/dist/prince.js +64 -29
- package/package.json +18 -17
package/dist/prince.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface PrinceRequest extends Request {
|
|
|
12
12
|
session?: any;
|
|
13
13
|
apiKey?: string;
|
|
14
14
|
sseSend?: (data: any, event?: string, id?: string) => void;
|
|
15
|
+
cookies?: Record<string, string>;
|
|
16
|
+
ip?: string;
|
|
15
17
|
[key: string]: any;
|
|
16
18
|
}
|
|
17
19
|
export type OnRequest = (req: PrinceRequest) => void | Promise<void>;
|
|
@@ -32,10 +34,18 @@ declare class ResponseBuilder {
|
|
|
32
34
|
private _body;
|
|
33
35
|
status(code: number): this;
|
|
34
36
|
header(key: string, value: string): this;
|
|
35
|
-
json(data: any):
|
|
36
|
-
text(data: string):
|
|
37
|
-
html(data: string):
|
|
38
|
-
redirect(url: string, status?: number):
|
|
37
|
+
json(data: any): this;
|
|
38
|
+
text(data: string): this;
|
|
39
|
+
html(data: string): this;
|
|
40
|
+
redirect(url: string, status?: number): this;
|
|
41
|
+
cookie(name: string, value: string, options?: {
|
|
42
|
+
maxAge?: number;
|
|
43
|
+
path?: string;
|
|
44
|
+
domain?: string;
|
|
45
|
+
secure?: boolean;
|
|
46
|
+
httpOnly?: boolean;
|
|
47
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
48
|
+
}): this;
|
|
39
49
|
build(): Response;
|
|
40
50
|
}
|
|
41
51
|
export interface RouteSchema {
|
package/dist/prince.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prince.d.ts","sourceRoot":"","sources":["../src/prince.ts"],"names":[],"mappings":"AAGA,OAAO,KAA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C,IAAI,SAAO;CA8CnB;AAiCD,eAAO,MAAM,MAAM,GAAI,aAAW,WAAoB,CAAC"}
|
package/dist/prince.js
CHANGED
|
@@ -134,27 +134,71 @@ class ResponseBuilder {
|
|
|
134
134
|
json(data) {
|
|
135
135
|
this._headers["Content-Type"] = "application/json";
|
|
136
136
|
this._body = JSON.stringify(data);
|
|
137
|
-
return this
|
|
137
|
+
return this;
|
|
138
138
|
}
|
|
139
139
|
text(data) {
|
|
140
140
|
this._headers["Content-Type"] = "text/plain";
|
|
141
141
|
this._body = data;
|
|
142
|
-
return this
|
|
142
|
+
return this;
|
|
143
143
|
}
|
|
144
144
|
html(data) {
|
|
145
145
|
this._headers["Content-Type"] = "text/html";
|
|
146
146
|
this._body = data;
|
|
147
|
-
return this
|
|
147
|
+
return this;
|
|
148
148
|
}
|
|
149
149
|
redirect(url, status = 302) {
|
|
150
150
|
this._status = status;
|
|
151
151
|
this._headers["Location"] = url;
|
|
152
|
-
return this
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
cookie(name, value, options) {
|
|
155
|
+
let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
156
|
+
if (options?.maxAge)
|
|
157
|
+
cookieStr += `; Max-Age=${options.maxAge}`;
|
|
158
|
+
if (options?.path)
|
|
159
|
+
cookieStr += `; Path=${options.path}`;
|
|
160
|
+
if (options?.domain)
|
|
161
|
+
cookieStr += `; Domain=${options.domain}`;
|
|
162
|
+
if (options?.secure)
|
|
163
|
+
cookieStr += "; Secure";
|
|
164
|
+
if (options?.httpOnly)
|
|
165
|
+
cookieStr += "; HttpOnly";
|
|
166
|
+
if (options?.sameSite)
|
|
167
|
+
cookieStr += `; SameSite=${options.sameSite}`;
|
|
168
|
+
const existing = this._headers["Set-Cookie"];
|
|
169
|
+
this._headers["Set-Cookie"] = existing ? `${existing}, ${cookieStr}` : cookieStr;
|
|
170
|
+
return this;
|
|
153
171
|
}
|
|
154
172
|
build() {
|
|
155
173
|
return new Response(this._body, { status: this._status, headers: this._headers });
|
|
156
174
|
}
|
|
157
175
|
}
|
|
176
|
+
function parseCookies(cookieHeader) {
|
|
177
|
+
const cookies = {};
|
|
178
|
+
if (!cookieHeader)
|
|
179
|
+
return cookies;
|
|
180
|
+
cookieHeader.split(";").forEach((pair) => {
|
|
181
|
+
const [name, ...value] = pair.split("=");
|
|
182
|
+
if (name)
|
|
183
|
+
cookies[decodeURIComponent(name.trim())] = decodeURIComponent((value.join("=") || "").trim());
|
|
184
|
+
});
|
|
185
|
+
return cookies;
|
|
186
|
+
}
|
|
187
|
+
function detectIP(req) {
|
|
188
|
+
const forwarded = req.headers.get("x-forwarded-for");
|
|
189
|
+
if (forwarded)
|
|
190
|
+
return forwarded.split(",")[0].trim();
|
|
191
|
+
const realIp = req.headers.get("x-real-ip");
|
|
192
|
+
if (realIp)
|
|
193
|
+
return realIp;
|
|
194
|
+
const cfIp = req.headers.get("cf-connecting-ip");
|
|
195
|
+
if (cfIp)
|
|
196
|
+
return cfIp;
|
|
197
|
+
const clientIp = req.headers.get("x-client-ip");
|
|
198
|
+
if (clientIp)
|
|
199
|
+
return clientIp;
|
|
200
|
+
return "127.0.0.1";
|
|
201
|
+
}
|
|
158
202
|
function zodToJsonSchema(schema) {
|
|
159
203
|
const d = schema._def;
|
|
160
204
|
const typeName = d?.typeName ?? d?.type ?? "";
|
|
@@ -466,25 +510,19 @@ class Prince {
|
|
|
466
510
|
this.routeCache.set(cacheKey, result2);
|
|
467
511
|
return result2;
|
|
468
512
|
}
|
|
469
|
-
const allowedMethods = new Set;
|
|
470
|
-
let pathExists = false;
|
|
471
|
-
for (const route of this.rawRoutes) {
|
|
472
|
-
if (this.matchPath(route.path, pathname)) {
|
|
473
|
-
pathExists = true;
|
|
474
|
-
allowedMethods.add(route.method);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
if (!pathExists) {
|
|
478
|
-
this.routeCache.set(cacheKey, null);
|
|
479
|
-
return null;
|
|
480
|
-
}
|
|
481
513
|
const segments = pathname === "/" ? [""] : pathname.split("/").slice(1);
|
|
482
514
|
const result = this.matchRoute(this.buildRouter(), segments, method);
|
|
483
515
|
if (result) {
|
|
484
516
|
this.routeCache.set(cacheKey, result);
|
|
485
517
|
return result;
|
|
486
518
|
}
|
|
487
|
-
|
|
519
|
+
const allowedMethods = new Set;
|
|
520
|
+
for (const route of this.rawRoutes) {
|
|
521
|
+
if (this.matchPath(route.path, pathname)) {
|
|
522
|
+
allowedMethods.add(route.method);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (allowedMethods.size > 0) {
|
|
488
526
|
const methodNotAllowed = {
|
|
489
527
|
handler: null,
|
|
490
528
|
params: {},
|
|
@@ -593,26 +631,21 @@ class Prince {
|
|
|
593
631
|
return null;
|
|
594
632
|
}
|
|
595
633
|
async executeHandler(req, handler, params, query, routeMiddlewares, method, pathname) {
|
|
596
|
-
|
|
597
|
-
|
|
634
|
+
req.params = params;
|
|
635
|
+
req.query = query;
|
|
636
|
+
req.cookies = parseCookies(req.headers.get("cookie") || "");
|
|
637
|
+
req.ip = detectIP(req);
|
|
598
638
|
if (["POST", "PUT", "PATCH"].includes(req.method) && !req.parsedBody) {
|
|
599
639
|
const parsed = await this.parseBody(req);
|
|
600
640
|
if (parsed) {
|
|
601
641
|
if (typeof parsed === "object" && "files" in parsed && "fields" in parsed) {
|
|
602
|
-
|
|
603
|
-
|
|
642
|
+
req.parsedBody = parsed.fields;
|
|
643
|
+
req.files = parsed.files;
|
|
604
644
|
} else {
|
|
605
|
-
|
|
645
|
+
req.parsedBody = parsed;
|
|
606
646
|
}
|
|
607
647
|
}
|
|
608
648
|
}
|
|
609
|
-
Object.defineProperty(req, "body", {
|
|
610
|
-
get: () => req.parsedBody,
|
|
611
|
-
set: (value) => {
|
|
612
|
-
req.parsedBody = value;
|
|
613
|
-
},
|
|
614
|
-
configurable: true
|
|
615
|
-
});
|
|
616
649
|
for (const hook of this.onBeforeHandleHooks) {
|
|
617
650
|
await hook(req, pathname, method);
|
|
618
651
|
}
|
|
@@ -627,6 +660,8 @@ class Prince {
|
|
|
627
660
|
const res = await handler(req);
|
|
628
661
|
if (res instanceof Response)
|
|
629
662
|
return res;
|
|
663
|
+
if (res instanceof ResponseBuilder)
|
|
664
|
+
return res.build();
|
|
630
665
|
if (typeof res === "string")
|
|
631
666
|
return new Response(res);
|
|
632
667
|
if (res instanceof Uint8Array)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "princejs",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"description": "An easy and fast backend framework that is among the top three — by a 13yo developer, for developers.",
|
|
5
5
|
"main": "dist/prince.js",
|
|
6
6
|
"types": "dist/prince.d.ts",
|
|
@@ -59,6 +59,14 @@
|
|
|
59
59
|
"README.md",
|
|
60
60
|
"LICENSE"
|
|
61
61
|
],
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build:js": "bun build src/prince.ts --outdir dist --target bun --external zod --external jose && bun build src/middleware.ts --outdir dist --target bun && bun build src/helpers.ts --outdir dist --target bun && bun build src/scheduler.ts --outdir dist --target bun && bun build bin/create.ts --outdir dist --target bun && bun build src/jsx.ts --outdir dist --target bun && bun build src/db.ts --outdir dist --target bun --format esm && bun build src/client.ts --outdir dist --format esm && bun build src/adapters/vercel.ts --outdir dist/adapters --format esm && bun build src/adapters/cloudflare.ts --outdir dist/adapters --format esm && bun build src/adapters/deno.ts --outdir dist/adapters --format esm",
|
|
64
|
+
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
|
|
65
|
+
"build": "bun run build:js && bun run build:types",
|
|
66
|
+
"prepublishOnly": "bun run build",
|
|
67
|
+
"test": "bun test",
|
|
68
|
+
"test:watch": "bun test --watch"
|
|
69
|
+
},
|
|
62
70
|
"keywords": [
|
|
63
71
|
"backend",
|
|
64
72
|
"api",
|
|
@@ -96,29 +104,22 @@
|
|
|
96
104
|
},
|
|
97
105
|
"devDependencies": {
|
|
98
106
|
"@types/bun": "^1.3.2",
|
|
99
|
-
"@types/jsonwebtoken": "^9.0.10",
|
|
100
107
|
"bun-types": "latest",
|
|
101
|
-
"fast-jwt": "^5.0.0",
|
|
102
|
-
"jsonwebtoken": "^9.0.2",
|
|
103
108
|
"typescript": "^5.9.3",
|
|
104
|
-
"zod": "^4.1.12"
|
|
109
|
+
"zod": "^4.1.12",
|
|
110
|
+
"jose": "^6.1.2"
|
|
105
111
|
},
|
|
106
112
|
"peerDependencies": {
|
|
107
|
-
"zod": "^4.1.12"
|
|
113
|
+
"zod": "^4.1.12",
|
|
114
|
+
"jose": "^6.1.2"
|
|
108
115
|
},
|
|
109
116
|
"peerDependenciesMeta": {
|
|
110
117
|
"zod": {
|
|
111
118
|
"optional": true
|
|
119
|
+
},
|
|
120
|
+
"jose": {
|
|
121
|
+
"optional": true
|
|
112
122
|
}
|
|
113
123
|
},
|
|
114
|
-
"dependencies": {
|
|
115
|
-
|
|
116
|
-
},
|
|
117
|
-
"scripts": {
|
|
118
|
-
"build:js": "bun build src/prince.ts --outdir dist --target bun --external zod --external jose && bun build src/middleware.ts --outdir dist --target bun && bun build src/helpers.ts --outdir dist --target bun && bun build src/scheduler.ts --outdir dist --target bun && bun build bin/create.ts --outdir dist --target bun && bun build src/jsx.ts --outdir dist --target bun && bun build src/db.ts --outdir dist --target bun --format esm && bun build src/client.ts --outdir dist --format esm && bun build src/adapters/vercel.ts --outdir dist/adapters --format esm && bun build src/adapters/cloudflare.ts --outdir dist/adapters --format esm && bun build src/adapters/deno.ts --outdir dist/adapters --format esm",
|
|
119
|
-
"build:types": "tsc --emitDeclarationOnly --skipLibCheck",
|
|
120
|
-
"build": "bun run build:js && bun run build:types",
|
|
121
|
-
"test": "bun test",
|
|
122
|
-
"test:watch": "bun test --watch"
|
|
123
|
-
}
|
|
124
|
-
}
|
|
124
|
+
"dependencies": {}
|
|
125
|
+
}
|