acrylblends 1.0.3 → 1.0.5
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/docs/framework/agents/index.md +6 -4
- package/docs/framework/workshop/README.md +3 -3
- package/docs/framework/workshop/facilitator.md +1 -0
- package/lib/accept-CNNHu4Cf.js +193 -0
- package/lib/{accepts-XBuZPVRI.js → accepts-Btq83xPd.js} +1 -1
- package/lib/accepts.js +1 -1
- package/lib/acryl-base.js +1 -331
- package/lib/adapter-CaZ6FZUU.js +37 -0
- package/lib/adapter.js +1 -36
- package/lib/{app-BM0rTCp7.js → app-CQcGWjrk.js} +4 -4
- package/lib/app-base-CpRMZL4v.js +332 -0
- package/lib/{audit-BXnYsLge.d.ts → audit-D5z9uwOP.d.ts} +14 -1
- package/lib/{base-NSN_SNv-.js → base-D_6kfOr-.js} +1 -1
- package/lib/basic-auth-C0HTRpXh.js +20 -0
- package/lib/basic-auth-DFLflXNe.js +95 -0
- package/lib/basic-auth.js +1 -94
- package/lib/bearer-auth-Cwxt-vgS.js +94 -0
- package/lib/bearer-auth.js +1 -93
- package/lib/body-D7f4e7w_.js +94 -0
- package/lib/body-limit-WmIjWRLt.js +66 -0
- package/lib/body-limit.js +1 -65
- package/lib/buffer-CkEOJCcY.js +51 -0
- package/lib/bun.js +3 -3
- package/lib/cache-B0bHH7s5.js +169 -0
- package/lib/cache.js +1 -168
- package/lib/{client-DDLXwhBo.js → client-BrMFnMEd.js} +1 -1
- package/lib/{client-modules-CLvWOjbd.js → client-modules-drj4mjhj.js} +4 -4
- package/lib/client.js +1 -1
- package/lib/color-COa3DplO.js +37 -0
- package/lib/combine-TWuGdcbC.js +137 -0
- package/lib/combine.js +1 -136
- package/lib/{components-cEW3ovK0.js → components-B2ka9Ax1.js} +4 -4
- package/lib/{components-JXVm45kK.js → components-PVXwaOaC.js} +2 -2
- package/lib/compress-BzWqsHF1.js +79 -0
- package/lib/compress-C-9vK4B2.d.ts +11 -0
- package/lib/compress-DCyrrQ5D.js +11 -0
- package/lib/compress.d.ts +1 -1
- package/lib/compress.js +2 -78
- package/lib/concurrent-DrF3d2c-.js +30 -0
- package/lib/conninfo-BCROmdnf.d.ts +11 -0
- package/lib/conninfo-CWJwieh4.js +19 -0
- package/lib/constants-CBSEudrb.js +7 -0
- package/lib/{context-DDO0cfuP.js → context-CkOh7Ygv.js} +2 -2
- package/lib/context-storage-iuPoI_CR.js +52 -0
- package/lib/context-storage.js +1 -51
- package/lib/cookie-B1LlkzOe.js +125 -0
- package/lib/cookie-BHIduy2T.js +82 -0
- package/lib/cookie.js +1 -81
- package/lib/cordis/db-sqlite.d.ts +1 -1
- package/lib/cordis/door.d.ts +17 -3
- package/lib/cordis/door.js +123 -16
- package/lib/cordis/inspect.js +4 -4
- package/lib/cordis/kv-db.d.ts +1 -1
- package/lib/cordis/kv-db.js +1 -1
- package/lib/cordis/kv-json.d.ts +1 -1
- package/lib/cordis/kv-json.js +1 -1
- package/lib/cordis/node-server.d.ts +1 -1
- package/lib/cordis/node-server.js +3 -116
- package/lib/cordis/router-http.d.ts +1 -1
- package/lib/cordis/router-http.js +2 -2
- package/lib/cordis/router.d.ts +1 -1
- package/lib/cordis/router.js +4 -4
- package/lib/cordis/state.js +7 -7
- package/lib/{core-CaY8XDZ6.js → core-BOu1jYua.js} +1 -1
- package/lib/cors-CbAQ-8qU.js +106 -0
- package/lib/cors.js +1 -105
- package/lib/crypto-iVP-N-tS.js +34 -0
- package/lib/csrf-DqqOZtgy.js +101 -0
- package/lib/csrf.js +1 -100
- package/lib/css-C5CPVMvM.js +134 -0
- package/lib/css.js +2 -133
- package/lib/db/sqlite.d.ts +1 -25
- package/lib/dev-CLgu-JRe.js +50 -0
- package/lib/dev.js +1 -49
- package/lib/dom-YjND9Jt3.js +74 -0
- package/lib/door/host.d.ts +2 -2
- package/lib/door/host.js +1 -1
- package/lib/door/page.d.ts +12 -1
- package/lib/door/page.js +22 -1
- package/lib/door/protocol.d.ts +2 -2
- package/lib/door/protocol.js +2 -197
- package/lib/encode-BhbNLrjp.js +33 -0
- package/lib/factory-CLdSf-xS.js +30 -0
- package/lib/factory.js +1 -29
- package/lib/handler-C0wZ0KwO.js +12 -0
- package/lib/helpers-BEjcdbgw.js +5 -0
- package/lib/{hooks-w72iLeFR.js → hooks-CfC07ri0.js} +1 -1
- package/lib/html-CgNa89xw.js +31 -0
- package/lib/html-DGExBFNg.js +103 -0
- package/lib/html.d.ts +2 -5
- package/lib/html.js +2 -30
- package/lib/http-exception-MjWkT89V.js +54 -0
- package/lib/http-exception.js +1 -53
- package/lib/{index-D4nVR3wu.d.ts → index-DMOtA-NJ.d.ts} +7 -1
- package/lib/index-dAzQsJG9.d.ts +6 -0
- package/lib/index.js +3 -3
- package/lib/ip-restriction-jKtKCRM_.js +166 -0
- package/lib/ip-restriction.js +1 -165
- package/lib/ipaddr-DCoIo528.js +238 -0
- package/lib/jsx/dom/client.js +1 -1
- package/lib/jsx/dom/jsx-dev-runtime.js +1 -1
- package/lib/jsx/dom/jsx-runtime.js +2 -1
- package/lib/jsx/dom/server.js +3 -3
- package/lib/jsx/dom.js +5 -73
- package/lib/jsx/jsx-dev-runtime.d.ts +2 -5
- package/lib/jsx/jsx-dev-runtime.js +2 -13
- package/lib/jsx/jsx-runtime.d.ts +2 -2
- package/lib/jsx/jsx-runtime.js +4 -4
- package/lib/jsx/streaming.js +1 -1
- package/lib/jsx-CUQgttxU.js +47 -0
- package/lib/jsx-dev-runtime-B4WTyq8r.js +14 -0
- package/lib/{jsx-dev-runtime-CWLRA2F3.js → jsx-dev-runtime-BLfA-EhI.js} +2 -2
- package/lib/jsx-dev-runtime-CGt4kNYr.d.ts +6 -0
- package/lib/jsx-renderer-CfifM4hT.js +99 -0
- package/lib/jsx-renderer.js +1 -98
- package/lib/jsx-runtime-COCRWBJ9.js +2 -0
- package/lib/jsx.js +5 -46
- package/lib/{jwk-LkzeoaU8.js → jwk-CDPpYQpD.js} +3 -3
- package/lib/jwk.js +1 -1
- package/lib/{jwt-BGJBw5AF.js → jwt-BXoIA2fQ.js} +2 -2
- package/lib/{jwt-CskdhEW9.js → jwt-lyS4Q3mT.js} +3 -3
- package/lib/jwt.js +2 -2
- package/lib/kv/db.d.ts +1 -10
- package/lib/kv/db.js +1 -56
- package/lib/kv/json.d.ts +1 -9
- package/lib/kv/json.js +1 -223
- package/lib/{language-D9a4zqvn.js → language-DeGRtLJt.js} +2 -2
- package/lib/language.js +1 -1
- package/lib/{linear-router-C3FPib6h.js → linear-router-CmZn_JLV.js} +2 -2
- package/lib/logger-Ckxu8diz.js +54 -0
- package/lib/logger.js +1 -53
- package/lib/loopback-only-BbasoF7R.js +90 -0
- package/lib/loopback-only.js +1 -89
- package/lib/method-not-allowed-TO2_hnfl.js +82 -0
- package/lib/method-not-allowed.js +1 -81
- package/lib/method-override-DnZKfJbV.js +103 -0
- package/lib/method-override.js +1 -102
- package/lib/middleware-BfG_fp8P.js +12 -0
- package/lib/mime-BnScXkQn.js +77 -0
- package/lib/mount-QkK_3NtJ.js +67 -0
- package/lib/mount.js +1 -66
- package/lib/node-server/conninfo.d.ts +1 -10
- package/lib/node-server/conninfo.js +1 -18
- package/lib/node-server/register.js +25 -2
- package/lib/node-server/serve-static.d.ts +1 -17
- package/lib/node-server/serve-static.js +1 -1
- package/lib/node-server.d.ts +2 -2
- package/lib/node-server.js +4 -4
- package/lib/open-BqK4D2Wn.d.ts +11 -0
- package/lib/open-But9E2rL.js +57 -0
- package/lib/open-CULVJgOc.d.ts +12 -0
- package/lib/open-Dp4sxspc.js +224 -0
- package/lib/open-T7kgbO2O.d.ts +27 -0
- package/lib/{pattern-router-HZxjDGEQ.js → pattern-router-CJBecJBd.js} +1 -1
- package/lib/powered-by-CXQNjNIX.js +24 -0
- package/lib/powered-by.js +1 -23
- package/lib/pretty-json-CeC1yvYt.js +39 -0
- package/lib/pretty-json.js +1 -38
- package/lib/{protocol-BizVt4J6.d.ts → protocol-C37gPePD.d.ts} +9 -2
- package/lib/protocol-Dt0AZBOz.js +213 -0
- package/lib/proxy-C2w_mH7k.js +121 -0
- package/lib/proxy.js +1 -120
- package/lib/quick.js +4 -4
- package/lib/{reg-exp-router-DC1R3qoY.js → reg-exp-router-C_WV84eV.js} +2 -2
- package/lib/request-DvMgGtlW.js +323 -0
- package/lib/request.js +1 -322
- package/lib/route-D8m-a2I1.js +126 -0
- package/lib/route.js +1 -125
- package/lib/router/linear-router.js +1 -1
- package/lib/router/pattern-router.js +1 -1
- package/lib/router/reg-exp-router.js +1 -1
- package/lib/router/smart-router.js +1 -1
- package/lib/router/trie-router.js +1 -1
- package/lib/router-Be_o6nFu.js +35 -0
- package/lib/router.js +1 -34
- package/lib/schema-validator-EK3cUUCt.js +22 -0
- package/lib/schema-validator.js +1 -21
- package/lib/{secure-headers-mP3j6Hm6.js → secure-headers-C5ZOcXqk.js} +1 -1
- package/lib/secure-headers.js +1 -1
- package/lib/serve-static-B7sqnftW.d.ts +18 -0
- package/lib/{serve-static-3uo_9rjF.js → serve-static-C1Nkj_xB.js} +2 -2
- package/lib/{serve-static-Cp5adzGo.js → serve-static-EiJf1zkO.js} +4 -4
- package/lib/serve-static.js +1 -1
- package/lib/{smart-router-C5CZ5XSM.js → smart-router-BjxkDQCE.js} +1 -1
- package/lib/{snapshot-text-DmE0YfeZ.js → snapshot-text-Bbx43mN0.js} +57 -9
- package/lib/{src-2Z4mJg0x.js → src-BTBm4OPD.js} +22 -6
- package/lib/src-LX0E4ec4.js +117 -0
- package/lib/{ssg-BwjcA-Ge.js → ssg-BEl7MHqg.js} +5 -5
- package/lib/ssg.js +1 -1
- package/lib/stream-CzM_SHkq.js +88 -0
- package/lib/{streaming-DHST3Scg.js → streaming-BlDaph8U.js} +3 -3
- package/lib/streaming.js +1 -1
- package/lib/{test-websocket-D8WWqE5G.js → test-websocket-BLeEvybj.js} +2 -2
- package/lib/testing-BQx5T_UX.js +17 -0
- package/lib/testing.js +1 -16
- package/lib/timeout-FeqZ9A-0.js +42 -0
- package/lib/timeout.js +1 -41
- package/lib/tiny.js +2 -2
- package/lib/trailing-slash-DwTx-1sk.js +85 -0
- package/lib/trailing-slash.js +1 -84
- package/lib/{trie-router-CfFbapzL.js → trie-router-JydLKUa2.js} +2 -2
- package/lib/url-BPh4d6j-.js +197 -0
- package/lib/utils/accept.js +1 -192
- package/lib/utils/basic-auth.js +1 -19
- package/lib/utils/body.js +1 -93
- package/lib/utils/buffer.js +1 -50
- package/lib/utils/color.js +1 -36
- package/lib/utils/compress.d.ts +1 -10
- package/lib/utils/compress.js +1 -10
- package/lib/utils/concurrent.js +1 -29
- package/lib/utils/constants.js +1 -6
- package/lib/utils/cookie.js +1 -124
- package/lib/utils/crypto.js +1 -33
- package/lib/utils/encode.js +1 -32
- package/lib/utils/handler.js +1 -11
- package/lib/utils/html.js +1 -102
- package/lib/utils/ipaddr.js +1 -237
- package/lib/utils/jwt.js +1 -1
- package/lib/utils/mime.js +1 -76
- package/lib/utils/stream.js +1 -87
- package/lib/utils/url.js +1 -196
- package/lib/{validator-D84mq-dO.js → validator-Btiyt5VJ.js} +3 -3
- package/lib/validator.js +1 -1
- package/lib/websocket-DsyBIKVA.js +56 -0
- package/lib/ws.js +1 -55
- package/package.json +1 -1
- package/plugins/acryl-door/src/cli/acryl.ts +5 -3
- package/plugins/acryl-door/src/host/channel.ts +36 -0
- package/plugins/acryl-door/src/host/chat-tools.ts +2 -0
- package/plugins/acryl-door/src/host/door.ts +44 -5
- package/plugins/acryl-door/src/host/shim.ts +23 -0
- package/plugins/acryl-door/src/host/verbs.ts +4 -3
- package/plugins/acryl-door/src/index.ts +39 -3
- package/plugins/acryl-door/src/page/driver/driver.ts +22 -1
- package/plugins/acryl-door/src/page/protocol.ts +13 -2
- package/plugins/acryl-node-server/src/register.ts +29 -2
- package/plugins/acryl-router/src/service.ts +19 -1
- package/plugins/agent/model.ts +6 -1
- package/plugins/agent/tools.ts +12 -2
- package/plugins/agent-panel/client/describe.ts +44 -0
- package/plugins/agent-panel/client/main.ts +67 -35
- package/plugins/agent-panel/index.ts +137 -35
- package/plugins/desktop/attach.ts +72 -0
- package/plugins/desktop/main.ts +17 -4
- package/plugins/extensions/index.ts +3 -4
- package/plugins/extensions/stage.ts +33 -5
- package/plugins/openai/index.ts +1 -1
- package/shared/DEFAULTS.md +4 -0
- package/templates/agent/AGENTS.md +2 -0
- package/templates/agent/blend.yaml +4 -1
- package/templates/agent/src/app.ts +2 -0
- package/templates/agent/src/routes.ts +1 -1
- package/templates/agent/template.json +1 -0
- package/templates/agent/test/builder.test.ts +26 -21
- package/lib/helpers-CvTauQT5.js +0 -5
- package/lib/middleware-CPQ8himN.js +0 -12
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { t as HTTPException } from "./http-exception-MjWkT89V.js";
|
|
2
|
+
import { t as GET_MATCH_RESULT } from "./constants-Dy7ZvN1H.js";
|
|
3
|
+
import { t as parseBody } from "./body-D7f4e7w_.js";
|
|
4
|
+
import { m as tryDecodeURIComponent, o as getQueryParam, s as getQueryParams } from "./url-BPh4d6j-.js";
|
|
5
|
+
//#region ../../plugins/acryl-router/src/core/request.ts
|
|
6
|
+
var AcrylRequest = class {
|
|
7
|
+
/**
|
|
8
|
+
* `.raw` can get the raw Request object.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // For Cloudflare Workers
|
|
14
|
+
* app.post('/', async (c) => {
|
|
15
|
+
* const metadata = c.req.raw.cf?.hostMetadata?
|
|
16
|
+
* ...
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
raw;
|
|
21
|
+
#validatedData;
|
|
22
|
+
#matchResult;
|
|
23
|
+
routeIndex = 0;
|
|
24
|
+
/**
|
|
25
|
+
* `.path` can get the pathname of the request.
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* app.get('/about/me', (c) => {
|
|
31
|
+
* const pathname = c.req.path // `/about/me`
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
path;
|
|
36
|
+
bodyCache = {};
|
|
37
|
+
constructor(request, path = "/", matchResult = [[]]) {
|
|
38
|
+
this.raw = request;
|
|
39
|
+
this.path = path;
|
|
40
|
+
this.#matchResult = matchResult;
|
|
41
|
+
}
|
|
42
|
+
param(key) {
|
|
43
|
+
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
44
|
+
}
|
|
45
|
+
#getDecodedParam(key) {
|
|
46
|
+
const paramKey = this.#matchResult[0][this.routeIndex]?.[1][key];
|
|
47
|
+
const param = this.#getParamValue(paramKey);
|
|
48
|
+
return param && tryDecodeURIComponent(param);
|
|
49
|
+
}
|
|
50
|
+
#getAllDecodedParams() {
|
|
51
|
+
const decoded = {};
|
|
52
|
+
const keys = Object.keys(this.#matchResult[0][this.routeIndex]?.[1] ?? {});
|
|
53
|
+
for (const key of keys) {
|
|
54
|
+
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
55
|
+
if (value !== void 0) decoded[key] = tryDecodeURIComponent(value);
|
|
56
|
+
}
|
|
57
|
+
return decoded;
|
|
58
|
+
}
|
|
59
|
+
#getParamValue(paramKey) {
|
|
60
|
+
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
61
|
+
}
|
|
62
|
+
query(key) {
|
|
63
|
+
return getQueryParam(this.url, key);
|
|
64
|
+
}
|
|
65
|
+
queries(key) {
|
|
66
|
+
return getQueryParams(this.url, key);
|
|
67
|
+
}
|
|
68
|
+
header(name) {
|
|
69
|
+
if (name) return this.raw.headers.get(name) ?? void 0;
|
|
70
|
+
const headerData = Object.create(null);
|
|
71
|
+
this.raw.headers.forEach((value, key) => {
|
|
72
|
+
headerData[key] = value;
|
|
73
|
+
});
|
|
74
|
+
return headerData;
|
|
75
|
+
}
|
|
76
|
+
async parseBody(options) {
|
|
77
|
+
return parseBody(this, options);
|
|
78
|
+
}
|
|
79
|
+
#cachedBody = (key) => {
|
|
80
|
+
const { bodyCache, raw } = this;
|
|
81
|
+
const cachedBody = bodyCache[key];
|
|
82
|
+
if (cachedBody) return cachedBody;
|
|
83
|
+
for (const anyCachedKey in bodyCache) return bodyCache[anyCachedKey].then((body) => {
|
|
84
|
+
if (anyCachedKey === "json") body = JSON.stringify(body);
|
|
85
|
+
const contentType = anyCachedKey === "formData" ? void 0 : raw.headers.get("content-type");
|
|
86
|
+
return new Response(body, { headers: contentType ? { "Content-Type": contentType } : void 0 })[key]();
|
|
87
|
+
});
|
|
88
|
+
return bodyCache[key] = raw[key]();
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* `.json()` can parse Request body of type `application/json`
|
|
92
|
+
*
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* app.post('/entry', async (c) => {
|
|
97
|
+
* const body = await c.req.json()
|
|
98
|
+
* })
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
json() {
|
|
102
|
+
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* `.text()` can parse Request body of type `text/plain`
|
|
106
|
+
*
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* app.post('/entry', async (c) => {
|
|
111
|
+
* const body = await c.req.text()
|
|
112
|
+
* })
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
text() {
|
|
116
|
+
return this.#cachedBody("text");
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* `.arrayBuffer()` parse Request body as an `ArrayBuffer`
|
|
120
|
+
*
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* app.post('/entry', async (c) => {
|
|
125
|
+
* const body = await c.req.arrayBuffer()
|
|
126
|
+
* })
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
arrayBuffer() {
|
|
130
|
+
return this.#cachedBody("arrayBuffer");
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* `.bytes()` parses the request body as a `Uint8Array`.
|
|
134
|
+
*
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* app.post('/entry', async (c) => {
|
|
139
|
+
* const body = await c.req.bytes()
|
|
140
|
+
* })
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
bytes() {
|
|
144
|
+
return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Parses the request body as a `Blob`.
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* app.post('/entry', async (c) => {
|
|
151
|
+
* const body = await c.req.blob();
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
blob() {
|
|
156
|
+
return this.#cachedBody("blob");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Parses the request body as `FormData`.
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* app.post('/entry', async (c) => {
|
|
163
|
+
* const body = await c.req.formData();
|
|
164
|
+
* });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
formData() {
|
|
168
|
+
return this.#cachedBody("formData");
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Adds validated data to the request.
|
|
172
|
+
*
|
|
173
|
+
* @param target - The target of the validation.
|
|
174
|
+
* @param data - The validated data to add.
|
|
175
|
+
*/
|
|
176
|
+
addValidatedData(target, data) {
|
|
177
|
+
(this.#validatedData ??= {})[target] = data;
|
|
178
|
+
}
|
|
179
|
+
valid(target) {
|
|
180
|
+
return this.#validatedData?.[target];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* `.url` can get the request url strings.
|
|
184
|
+
*
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* app.get('/about/me', (c) => {
|
|
189
|
+
* const url = c.req.url // `http://localhost:8787/about/me`
|
|
190
|
+
* ...
|
|
191
|
+
* })
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
get url() {
|
|
195
|
+
return this.raw.url;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* `.method` can get the method name of the request.
|
|
199
|
+
*
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* app.get('/about/me', (c) => {
|
|
204
|
+
* const method = c.req.method // `GET`
|
|
205
|
+
* })
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
get method() {
|
|
209
|
+
return this.raw.method;
|
|
210
|
+
}
|
|
211
|
+
get [GET_MATCH_RESULT]() {
|
|
212
|
+
return this.#matchResult;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* `.matchedRoutes` can return a matched route in the handler
|
|
216
|
+
*
|
|
217
|
+
* @deprecated
|
|
218
|
+
*
|
|
219
|
+
* Use matchedRoutes helper defined in "acryl-router/route" instead.
|
|
220
|
+
*
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* app.use('*', async function logger(c, next) {
|
|
225
|
+
* await next()
|
|
226
|
+
* c.req.matchedRoutes.forEach(({ handler, method, path }, i) => {
|
|
227
|
+
* const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]')
|
|
228
|
+
* console.log(
|
|
229
|
+
* method,
|
|
230
|
+
* ' ',
|
|
231
|
+
* path,
|
|
232
|
+
* ' '.repeat(Math.max(10 - path.length, 0)),
|
|
233
|
+
* name,
|
|
234
|
+
* i === c.req.routeIndex ? '<- respond from here' : ''
|
|
235
|
+
* )
|
|
236
|
+
* })
|
|
237
|
+
* })
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
get matchedRoutes() {
|
|
241
|
+
return this.#matchResult[0].map(([[, route]]) => route);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* `.routePath` can retrieve the path registered within the handler
|
|
245
|
+
*
|
|
246
|
+
* @deprecated
|
|
247
|
+
*
|
|
248
|
+
* Use routePath helper defined in "acryl-router/route" instead.
|
|
249
|
+
*
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* app.get('/posts/:id', (c) => {
|
|
254
|
+
* return c.json({ path: c.req.routePath })
|
|
255
|
+
* })
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
get routePath() {
|
|
259
|
+
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* Clones a AcrylRequest's underlying raw Request object.
|
|
264
|
+
*
|
|
265
|
+
* This utility handles both consumed and unconsumed request bodies:
|
|
266
|
+
* - If the request body hasn't been consumed, it uses the native `clone()` method
|
|
267
|
+
* - If the request body has been consumed, it reconstructs a new Request using cached body data
|
|
268
|
+
*
|
|
269
|
+
* This is particularly useful when you need to:
|
|
270
|
+
* - Process the same request body multiple times
|
|
271
|
+
* - Pass requests to external services after validation
|
|
272
|
+
*
|
|
273
|
+
* @param req - The AcrylRequest object to clone
|
|
274
|
+
* @returns A Promise that resolves to a new Request object with the same properties
|
|
275
|
+
* @throws {HTTPException} If the request body was consumed directly via `req.raw`
|
|
276
|
+
* without using AcrylRequest methods (e.g., `req.json()`, `req.text()`), making it
|
|
277
|
+
* impossible to reconstruct the body from cache
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```ts
|
|
281
|
+
* // Clone after consuming the body (e.g., after validation)
|
|
282
|
+
* app.post('/forward',
|
|
283
|
+
* validator('json', (data) => data),
|
|
284
|
+
* async (c) => {
|
|
285
|
+
* const validated = c.req.valid('json')
|
|
286
|
+
* // Body has been consumed, but cloneRawRequest still works
|
|
287
|
+
* const clonedReq = await cloneRawRequest(c.req)
|
|
288
|
+
* return fetch('http://backend-service.com', clonedReq)
|
|
289
|
+
* }
|
|
290
|
+
* )
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
const cloneRawRequest = async (req) => {
|
|
294
|
+
if (!req.raw.bodyUsed) return req.raw.clone();
|
|
295
|
+
const cacheKey = Object.keys(req.bodyCache)[0];
|
|
296
|
+
if (!cacheKey) throw new HTTPException(500, { message: "Cannot clone request: body was already consumed and not cached. Please use AcrylRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly." });
|
|
297
|
+
let body = await req[cacheKey]();
|
|
298
|
+
const headers = req.header();
|
|
299
|
+
if (cacheKey === "json") {
|
|
300
|
+
body = JSON.stringify(body);
|
|
301
|
+
delete headers["content-length"];
|
|
302
|
+
} else if (body instanceof FormData) {
|
|
303
|
+
delete headers["content-type"];
|
|
304
|
+
delete headers["content-length"];
|
|
305
|
+
}
|
|
306
|
+
const requestInit = {
|
|
307
|
+
body,
|
|
308
|
+
cache: req.raw.cache,
|
|
309
|
+
credentials: req.raw.credentials,
|
|
310
|
+
headers,
|
|
311
|
+
integrity: req.raw.integrity,
|
|
312
|
+
keepalive: req.raw.keepalive,
|
|
313
|
+
method: req.method,
|
|
314
|
+
mode: req.raw.mode,
|
|
315
|
+
redirect: req.raw.redirect,
|
|
316
|
+
referrer: req.raw.referrer,
|
|
317
|
+
referrerPolicy: req.raw.referrerPolicy,
|
|
318
|
+
signal: req.raw.signal
|
|
319
|
+
};
|
|
320
|
+
return new Request(req.url, requestInit);
|
|
321
|
+
};
|
|
322
|
+
//#endregion
|
|
323
|
+
export { cloneRawRequest as n, AcrylRequest as t };
|
package/lib/request.js
CHANGED
|
@@ -1,323 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as GET_MATCH_RESULT } from "./constants-Dy7ZvN1H.js";
|
|
3
|
-
import { parseBody } from "./utils/body.js";
|
|
4
|
-
import { getQueryParam, getQueryParams, tryDecodeURIComponent } from "./utils/url.js";
|
|
5
|
-
//#region ../../plugins/acryl-router/src/core/request.ts
|
|
6
|
-
var AcrylRequest = class {
|
|
7
|
-
/**
|
|
8
|
-
* `.raw` can get the raw Request object.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* // For Cloudflare Workers
|
|
14
|
-
* app.post('/', async (c) => {
|
|
15
|
-
* const metadata = c.req.raw.cf?.hostMetadata?
|
|
16
|
-
* ...
|
|
17
|
-
* })
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
raw;
|
|
21
|
-
#validatedData;
|
|
22
|
-
#matchResult;
|
|
23
|
-
routeIndex = 0;
|
|
24
|
-
/**
|
|
25
|
-
* `.path` can get the pathname of the request.
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```ts
|
|
30
|
-
* app.get('/about/me', (c) => {
|
|
31
|
-
* const pathname = c.req.path // `/about/me`
|
|
32
|
-
* })
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
path;
|
|
36
|
-
bodyCache = {};
|
|
37
|
-
constructor(request, path = "/", matchResult = [[]]) {
|
|
38
|
-
this.raw = request;
|
|
39
|
-
this.path = path;
|
|
40
|
-
this.#matchResult = matchResult;
|
|
41
|
-
}
|
|
42
|
-
param(key) {
|
|
43
|
-
return key ? this.#getDecodedParam(key) : this.#getAllDecodedParams();
|
|
44
|
-
}
|
|
45
|
-
#getDecodedParam(key) {
|
|
46
|
-
const paramKey = this.#matchResult[0][this.routeIndex]?.[1][key];
|
|
47
|
-
const param = this.#getParamValue(paramKey);
|
|
48
|
-
return param && tryDecodeURIComponent(param);
|
|
49
|
-
}
|
|
50
|
-
#getAllDecodedParams() {
|
|
51
|
-
const decoded = {};
|
|
52
|
-
const keys = Object.keys(this.#matchResult[0][this.routeIndex]?.[1] ?? {});
|
|
53
|
-
for (const key of keys) {
|
|
54
|
-
const value = this.#getParamValue(this.#matchResult[0][this.routeIndex][1][key]);
|
|
55
|
-
if (value !== void 0) decoded[key] = tryDecodeURIComponent(value);
|
|
56
|
-
}
|
|
57
|
-
return decoded;
|
|
58
|
-
}
|
|
59
|
-
#getParamValue(paramKey) {
|
|
60
|
-
return this.#matchResult[1] ? this.#matchResult[1][paramKey] : paramKey;
|
|
61
|
-
}
|
|
62
|
-
query(key) {
|
|
63
|
-
return getQueryParam(this.url, key);
|
|
64
|
-
}
|
|
65
|
-
queries(key) {
|
|
66
|
-
return getQueryParams(this.url, key);
|
|
67
|
-
}
|
|
68
|
-
header(name) {
|
|
69
|
-
if (name) return this.raw.headers.get(name) ?? void 0;
|
|
70
|
-
const headerData = Object.create(null);
|
|
71
|
-
this.raw.headers.forEach((value, key) => {
|
|
72
|
-
headerData[key] = value;
|
|
73
|
-
});
|
|
74
|
-
return headerData;
|
|
75
|
-
}
|
|
76
|
-
async parseBody(options) {
|
|
77
|
-
return parseBody(this, options);
|
|
78
|
-
}
|
|
79
|
-
#cachedBody = (key) => {
|
|
80
|
-
const { bodyCache, raw } = this;
|
|
81
|
-
const cachedBody = bodyCache[key];
|
|
82
|
-
if (cachedBody) return cachedBody;
|
|
83
|
-
for (const anyCachedKey in bodyCache) return bodyCache[anyCachedKey].then((body) => {
|
|
84
|
-
if (anyCachedKey === "json") body = JSON.stringify(body);
|
|
85
|
-
const contentType = anyCachedKey === "formData" ? void 0 : raw.headers.get("content-type");
|
|
86
|
-
return new Response(body, { headers: contentType ? { "Content-Type": contentType } : void 0 })[key]();
|
|
87
|
-
});
|
|
88
|
-
return bodyCache[key] = raw[key]();
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* `.json()` can parse Request body of type `application/json`
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```ts
|
|
96
|
-
* app.post('/entry', async (c) => {
|
|
97
|
-
* const body = await c.req.json()
|
|
98
|
-
* })
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
json() {
|
|
102
|
-
return this.#cachedBody("text").then((text) => JSON.parse(text));
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* `.text()` can parse Request body of type `text/plain`
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* @example
|
|
109
|
-
* ```ts
|
|
110
|
-
* app.post('/entry', async (c) => {
|
|
111
|
-
* const body = await c.req.text()
|
|
112
|
-
* })
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
text() {
|
|
116
|
-
return this.#cachedBody("text");
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* `.arrayBuffer()` parse Request body as an `ArrayBuffer`
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```ts
|
|
124
|
-
* app.post('/entry', async (c) => {
|
|
125
|
-
* const body = await c.req.arrayBuffer()
|
|
126
|
-
* })
|
|
127
|
-
* ```
|
|
128
|
-
*/
|
|
129
|
-
arrayBuffer() {
|
|
130
|
-
return this.#cachedBody("arrayBuffer");
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* `.bytes()` parses the request body as a `Uint8Array`.
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```ts
|
|
138
|
-
* app.post('/entry', async (c) => {
|
|
139
|
-
* const body = await c.req.bytes()
|
|
140
|
-
* })
|
|
141
|
-
* ```
|
|
142
|
-
*/
|
|
143
|
-
bytes() {
|
|
144
|
-
return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer));
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Parses the request body as a `Blob`.
|
|
148
|
-
* @example
|
|
149
|
-
* ```ts
|
|
150
|
-
* app.post('/entry', async (c) => {
|
|
151
|
-
* const body = await c.req.blob();
|
|
152
|
-
* });
|
|
153
|
-
* ```
|
|
154
|
-
*/
|
|
155
|
-
blob() {
|
|
156
|
-
return this.#cachedBody("blob");
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Parses the request body as `FormData`.
|
|
160
|
-
* @example
|
|
161
|
-
* ```ts
|
|
162
|
-
* app.post('/entry', async (c) => {
|
|
163
|
-
* const body = await c.req.formData();
|
|
164
|
-
* });
|
|
165
|
-
* ```
|
|
166
|
-
*/
|
|
167
|
-
formData() {
|
|
168
|
-
return this.#cachedBody("formData");
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Adds validated data to the request.
|
|
172
|
-
*
|
|
173
|
-
* @param target - The target of the validation.
|
|
174
|
-
* @param data - The validated data to add.
|
|
175
|
-
*/
|
|
176
|
-
addValidatedData(target, data) {
|
|
177
|
-
(this.#validatedData ??= {})[target] = data;
|
|
178
|
-
}
|
|
179
|
-
valid(target) {
|
|
180
|
-
return this.#validatedData?.[target];
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* `.url` can get the request url strings.
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* ```ts
|
|
188
|
-
* app.get('/about/me', (c) => {
|
|
189
|
-
* const url = c.req.url // `http://localhost:8787/about/me`
|
|
190
|
-
* ...
|
|
191
|
-
* })
|
|
192
|
-
* ```
|
|
193
|
-
*/
|
|
194
|
-
get url() {
|
|
195
|
-
return this.raw.url;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* `.method` can get the method name of the request.
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* ```ts
|
|
203
|
-
* app.get('/about/me', (c) => {
|
|
204
|
-
* const method = c.req.method // `GET`
|
|
205
|
-
* })
|
|
206
|
-
* ```
|
|
207
|
-
*/
|
|
208
|
-
get method() {
|
|
209
|
-
return this.raw.method;
|
|
210
|
-
}
|
|
211
|
-
get [GET_MATCH_RESULT]() {
|
|
212
|
-
return this.#matchResult;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* `.matchedRoutes` can return a matched route in the handler
|
|
216
|
-
*
|
|
217
|
-
* @deprecated
|
|
218
|
-
*
|
|
219
|
-
* Use matchedRoutes helper defined in "acryl-router/route" instead.
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```ts
|
|
224
|
-
* app.use('*', async function logger(c, next) {
|
|
225
|
-
* await next()
|
|
226
|
-
* c.req.matchedRoutes.forEach(({ handler, method, path }, i) => {
|
|
227
|
-
* const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]')
|
|
228
|
-
* console.log(
|
|
229
|
-
* method,
|
|
230
|
-
* ' ',
|
|
231
|
-
* path,
|
|
232
|
-
* ' '.repeat(Math.max(10 - path.length, 0)),
|
|
233
|
-
* name,
|
|
234
|
-
* i === c.req.routeIndex ? '<- respond from here' : ''
|
|
235
|
-
* )
|
|
236
|
-
* })
|
|
237
|
-
* })
|
|
238
|
-
* ```
|
|
239
|
-
*/
|
|
240
|
-
get matchedRoutes() {
|
|
241
|
-
return this.#matchResult[0].map(([[, route]]) => route);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* `.routePath` can retrieve the path registered within the handler
|
|
245
|
-
*
|
|
246
|
-
* @deprecated
|
|
247
|
-
*
|
|
248
|
-
* Use routePath helper defined in "acryl-router/route" instead.
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```ts
|
|
253
|
-
* app.get('/posts/:id', (c) => {
|
|
254
|
-
* return c.json({ path: c.req.routePath })
|
|
255
|
-
* })
|
|
256
|
-
* ```
|
|
257
|
-
*/
|
|
258
|
-
get routePath() {
|
|
259
|
-
return this.#matchResult[0].map(([[, route]]) => route)[this.routeIndex].path;
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
/**
|
|
263
|
-
* Clones a AcrylRequest's underlying raw Request object.
|
|
264
|
-
*
|
|
265
|
-
* This utility handles both consumed and unconsumed request bodies:
|
|
266
|
-
* - If the request body hasn't been consumed, it uses the native `clone()` method
|
|
267
|
-
* - If the request body has been consumed, it reconstructs a new Request using cached body data
|
|
268
|
-
*
|
|
269
|
-
* This is particularly useful when you need to:
|
|
270
|
-
* - Process the same request body multiple times
|
|
271
|
-
* - Pass requests to external services after validation
|
|
272
|
-
*
|
|
273
|
-
* @param req - The AcrylRequest object to clone
|
|
274
|
-
* @returns A Promise that resolves to a new Request object with the same properties
|
|
275
|
-
* @throws {HTTPException} If the request body was consumed directly via `req.raw`
|
|
276
|
-
* without using AcrylRequest methods (e.g., `req.json()`, `req.text()`), making it
|
|
277
|
-
* impossible to reconstruct the body from cache
|
|
278
|
-
*
|
|
279
|
-
* @example
|
|
280
|
-
* ```ts
|
|
281
|
-
* // Clone after consuming the body (e.g., after validation)
|
|
282
|
-
* app.post('/forward',
|
|
283
|
-
* validator('json', (data) => data),
|
|
284
|
-
* async (c) => {
|
|
285
|
-
* const validated = c.req.valid('json')
|
|
286
|
-
* // Body has been consumed, but cloneRawRequest still works
|
|
287
|
-
* const clonedReq = await cloneRawRequest(c.req)
|
|
288
|
-
* return fetch('http://backend-service.com', clonedReq)
|
|
289
|
-
* }
|
|
290
|
-
* )
|
|
291
|
-
* ```
|
|
292
|
-
*/
|
|
293
|
-
const cloneRawRequest = async (req) => {
|
|
294
|
-
if (!req.raw.bodyUsed) return req.raw.clone();
|
|
295
|
-
const cacheKey = Object.keys(req.bodyCache)[0];
|
|
296
|
-
if (!cacheKey) throw new HTTPException(500, { message: "Cannot clone request: body was already consumed and not cached. Please use AcrylRequest methods (e.g., req.json(), req.text()) instead of consuming req.raw directly." });
|
|
297
|
-
let body = await req[cacheKey]();
|
|
298
|
-
const headers = req.header();
|
|
299
|
-
if (cacheKey === "json") {
|
|
300
|
-
body = JSON.stringify(body);
|
|
301
|
-
delete headers["content-length"];
|
|
302
|
-
} else if (body instanceof FormData) {
|
|
303
|
-
delete headers["content-type"];
|
|
304
|
-
delete headers["content-length"];
|
|
305
|
-
}
|
|
306
|
-
const requestInit = {
|
|
307
|
-
body,
|
|
308
|
-
cache: req.raw.cache,
|
|
309
|
-
credentials: req.raw.credentials,
|
|
310
|
-
headers,
|
|
311
|
-
integrity: req.raw.integrity,
|
|
312
|
-
keepalive: req.raw.keepalive,
|
|
313
|
-
method: req.method,
|
|
314
|
-
mode: req.raw.mode,
|
|
315
|
-
redirect: req.raw.redirect,
|
|
316
|
-
referrer: req.raw.referrer,
|
|
317
|
-
referrerPolicy: req.raw.referrerPolicy,
|
|
318
|
-
signal: req.raw.signal
|
|
319
|
-
};
|
|
320
|
-
return new Request(req.url, requestInit);
|
|
321
|
-
};
|
|
322
|
-
//#endregion
|
|
1
|
+
import { n as cloneRawRequest, t as AcrylRequest } from "./request-DvMgGtlW.js";
|
|
323
2
|
export { AcrylRequest, cloneRawRequest };
|