hono-agents 0.0.32 → 0.0.34
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +5 -10
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,6 @@ type AgentMiddlewareContext<E extends Env> = {
|
|
|
17
17
|
*/
|
|
18
18
|
declare function agentsMiddleware<E extends Env = Env>(
|
|
19
19
|
ctx?: AgentMiddlewareContext<E>
|
|
20
|
-
): hono.MiddlewareHandler<
|
|
20
|
+
): hono.MiddlewareHandler<Env, string, {}>;
|
|
21
21
|
|
|
22
22
|
export { agentsMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -40,12 +40,7 @@ async function handleWebSocketUpgrade(c, options) {
|
|
|
40
40
|
}
|
|
41
41
|
async function handleHttpRequest(c, options) {
|
|
42
42
|
const req = createRequestFromContext(c);
|
|
43
|
-
return routeAgentRequest(
|
|
44
|
-
req,
|
|
45
|
-
env(c),
|
|
46
|
-
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
47
|
-
options
|
|
48
|
-
);
|
|
43
|
+
return routeAgentRequest(req, env(c), options);
|
|
49
44
|
}
|
|
50
45
|
export {
|
|
51
46
|
agentsMiddleware
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { env } from \"hono/adapter\";\nimport { createMiddleware } from \"hono/factory\";\nimport { routeAgentRequest } from \"agents\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents\";\n\n/**\n * Configuration options for the Cloudflare Agents middleware\n */\ntype AgentMiddlewareContext<E extends Env> = {\n /** Cloudflare Agents-specific configuration options */\n options?: AgentOptions<E>;\n /** Optional error handler for caught errors */\n onError?: (error: Error) => void;\n};\n\n/**\n * Creates a middleware for handling Cloudflare Agents WebSocket and HTTP requests\n * Processes both WebSocket upgrades and standard HTTP requests, delegating them to Cloudflare Agents\n */\nexport function agentsMiddleware<E extends Env = Env>(\n ctx?: AgentMiddlewareContext<E>\n) {\n return createMiddleware(async (c, next) => {\n try {\n const handler = isWebSocketUpgrade(c)\n ? handleWebSocketUpgrade\n : handleHttpRequest;\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { env } from \"hono/adapter\";\nimport { createMiddleware } from \"hono/factory\";\nimport { routeAgentRequest } from \"agents\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents\";\n\n/**\n * Configuration options for the Cloudflare Agents middleware\n */\ntype AgentMiddlewareContext<E extends Env> = {\n /** Cloudflare Agents-specific configuration options */\n options?: AgentOptions<E>;\n /** Optional error handler for caught errors */\n onError?: (error: Error) => void;\n};\n\n/**\n * Creates a middleware for handling Cloudflare Agents WebSocket and HTTP requests\n * Processes both WebSocket upgrades and standard HTTP requests, delegating them to Cloudflare Agents\n */\nexport function agentsMiddleware<E extends Env = Env>(\n ctx?: AgentMiddlewareContext<E>\n) {\n return createMiddleware<Env>(async (c, next) => {\n try {\n const handler = isWebSocketUpgrade(c)\n ? handleWebSocketUpgrade\n : handleHttpRequest;\n\n const response = await handler(c, ctx?.options);\n\n return response === null ? await next() : response;\n } catch (error) {\n if (ctx?.onError) {\n ctx.onError(error as Error);\n return next();\n }\n throw error;\n }\n });\n}\n\n/**\n * Checks if the incoming request is a WebSocket upgrade request\n * Looks for the 'upgrade' header with a value of 'websocket' (case-insensitive)\n */\nfunction isWebSocketUpgrade(c: Context): boolean {\n return c.req.header(\"upgrade\")?.toLowerCase() === \"websocket\";\n}\n\n/**\n * Creates a new Request object from the Hono context\n * Preserves the original request's URL, method, headers, and body\n */\nfunction createRequestFromContext(c: Context) {\n return new Request(c.req.url, {\n method: c.req.method,\n headers: c.req.header(),\n body: c.req.raw.body,\n });\n}\n\n/**\n * Handles WebSocket upgrade requests\n * Returns a WebSocket upgrade response if successful, null otherwise\n */\nasync function handleWebSocketUpgrade<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n const req = createRequestFromContext(c);\n const response = await routeAgentRequest(req, env(c) satisfies Env, options);\n\n if (!response?.webSocket) {\n return null;\n }\n\n return new Response(null, {\n status: 101,\n webSocket: response.webSocket,\n });\n}\n\n/**\n * Handles standard HTTP requests\n * Forwards the request to Cloudflare Agents and returns the response\n */\nasync function handleHttpRequest<E extends Env>(\n c: Context<E>,\n options?: AgentOptions<E>\n) {\n const req = createRequestFromContext(c);\n\n return routeAgentRequest(req, env(c) satisfies Env, options);\n}\n"],"mappings":";AAAA,SAAS,WAAW;AACpB,SAAS,wBAAwB;AACjC,SAAS,yBAAyB;AAmB3B,SAAS,iBACd,KACA;AACA,SAAO,iBAAsB,OAAO,GAAG,SAAS;AAC9C,QAAI;AACF,YAAM,UAAU,mBAAmB,CAAC,IAChC,yBACA;AAEJ,YAAM,WAAW,MAAM,QAAQ,GAAG,KAAK,OAAO;AAE9C,aAAO,aAAa,OAAO,MAAM,KAAK,IAAI;AAAA,IAC5C,SAAS,OAAO;AACd,UAAI,KAAK,SAAS;AAChB,YAAI,QAAQ,KAAc;AAC1B,eAAO,KAAK;AAAA,MACd;AACA,YAAM;AAAA,IACR;AAAA,EACF,CAAC;AACH;AAMA,SAAS,mBAAmB,GAAqB;AAC/C,SAAO,EAAE,IAAI,OAAO,SAAS,GAAG,YAAY,MAAM;AACpD;AAMA,SAAS,yBAAyB,GAAY;AAC5C,SAAO,IAAI,QAAQ,EAAE,IAAI,KAAK;AAAA,IAC5B,QAAQ,EAAE,IAAI;AAAA,IACd,SAAS,EAAE,IAAI,OAAO;AAAA,IACtB,MAAM,EAAE,IAAI,IAAI;AAAA,EAClB,CAAC;AACH;AAMA,eAAe,uBACb,GACA,SACA;AACA,QAAM,MAAM,yBAAyB,CAAC;AACtC,QAAM,WAAW,MAAM,kBAAkB,KAAK,IAAI,CAAC,GAAiB,OAAO;AAE3E,MAAI,CAAC,UAAU,WAAW;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,SAAS,MAAM;AAAA,IACxB,QAAQ;AAAA,IACR,WAAW,SAAS;AAAA,EACtB,CAAC;AACH;AAMA,eAAe,kBACb,GACA,SACA;AACA,QAAM,MAAM,yBAAyB,CAAC;AAEtC,SAAO,kBAAkB,KAAK,IAAI,CAAC,GAAiB,OAAO;AAC7D;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-agents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.34",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"license": "MIT",
|
|
36
36
|
"description": "Add Cloudflare Agents to your Hono app",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"agents": "^0.0.
|
|
38
|
+
"agents": "^0.0.44",
|
|
39
39
|
"hono": "^4.6.17"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"agents": "^0.0.
|
|
42
|
+
"agents": "^0.0.44",
|
|
43
43
|
"hono": "^4.7.5"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -22,12 +22,12 @@ type AgentMiddlewareContext<E extends Env> = {
|
|
|
22
22
|
export function agentsMiddleware<E extends Env = Env>(
|
|
23
23
|
ctx?: AgentMiddlewareContext<E>
|
|
24
24
|
) {
|
|
25
|
-
return createMiddleware(async (c, next) => {
|
|
25
|
+
return createMiddleware<Env>(async (c, next) => {
|
|
26
26
|
try {
|
|
27
27
|
const handler = isWebSocketUpgrade(c)
|
|
28
28
|
? handleWebSocketUpgrade
|
|
29
29
|
: handleHttpRequest;
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
const response = await handler(c, ctx?.options);
|
|
32
32
|
|
|
33
33
|
return response === null ? await next() : response;
|
|
@@ -70,8 +70,7 @@ async function handleWebSocketUpgrade<E extends Env>(
|
|
|
70
70
|
options?: AgentOptions<E>
|
|
71
71
|
) {
|
|
72
72
|
const req = createRequestFromContext(c);
|
|
73
|
-
|
|
74
|
-
const response = await routeAgentRequest(req, env(c), options);
|
|
73
|
+
const response = await routeAgentRequest(req, env(c) satisfies Env, options);
|
|
75
74
|
|
|
76
75
|
if (!response?.webSocket) {
|
|
77
76
|
return null;
|
|
@@ -92,10 +91,6 @@ async function handleHttpRequest<E extends Env>(
|
|
|
92
91
|
options?: AgentOptions<E>
|
|
93
92
|
) {
|
|
94
93
|
const req = createRequestFromContext(c);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
env(c),
|
|
98
|
-
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
99
|
-
options
|
|
100
|
-
);
|
|
94
|
+
|
|
95
|
+
return routeAgentRequest(req, env(c) satisfies Env, options);
|
|
101
96
|
}
|