hono-agents 0.0.47 → 0.0.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.
- package/dist/index.js +6 -11
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +6 -17
package/dist/index.js
CHANGED
|
@@ -20,16 +20,12 @@ function agentsMiddleware(ctx) {
|
|
|
20
20
|
function isWebSocketUpgrade(c) {
|
|
21
21
|
return c.req.header("upgrade")?.toLowerCase() === "websocket";
|
|
22
22
|
}
|
|
23
|
-
function createRequestFromContext(c) {
|
|
24
|
-
return new Request(c.req.url, {
|
|
25
|
-
method: c.req.method,
|
|
26
|
-
headers: c.req.header(),
|
|
27
|
-
body: c.req.raw.body
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
23
|
async function handleWebSocketUpgrade(c, options) {
|
|
31
|
-
const
|
|
32
|
-
|
|
24
|
+
const response = await routeAgentRequest(
|
|
25
|
+
c.req.raw,
|
|
26
|
+
env(c),
|
|
27
|
+
options
|
|
28
|
+
);
|
|
33
29
|
if (!response?.webSocket) {
|
|
34
30
|
return null;
|
|
35
31
|
}
|
|
@@ -39,8 +35,7 @@ async function handleWebSocketUpgrade(c, options) {
|
|
|
39
35
|
});
|
|
40
36
|
}
|
|
41
37
|
async function handleHttpRequest(c, options) {
|
|
42
|
-
|
|
43
|
-
return routeAgentRequest(req, env(c), options);
|
|
38
|
+
return routeAgentRequest(c.req.raw, env(c), options);
|
|
44
39
|
}
|
|
45
40
|
export {
|
|
46
41
|
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<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 *
|
|
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 * 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 response = await routeAgentRequest(\n c.req.raw,\n env(c) satisfies Env,\n options\n );\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 return routeAgentRequest(c.req.raw, 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,eAAe,uBACb,GACA,SACA;AACA,QAAM,WAAW,MAAM;AAAA,IACrB,EAAE,IAAI;AAAA,IACN,IAAI,CAAC;AAAA,IACL;AAAA,EACF;AAEA,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,SAAO,kBAAkB,EAAE,IAAI,KAAK,IAAI,CAAC,GAAiB,OAAO;AACnE;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-agents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.49",
|
|
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.59",
|
|
39
39
|
"hono": "^4.6.17"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"agents": "^0.0.
|
|
43
|
-
"hono": "^4.7.
|
|
42
|
+
"agents": "^0.0.59",
|
|
43
|
+
"hono": "^4.7.6"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -49,18 +49,6 @@ function isWebSocketUpgrade(c: Context): boolean {
|
|
|
49
49
|
return c.req.header("upgrade")?.toLowerCase() === "websocket";
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
/**
|
|
53
|
-
* Creates a new Request object from the Hono context
|
|
54
|
-
* Preserves the original request's URL, method, headers, and body
|
|
55
|
-
*/
|
|
56
|
-
function createRequestFromContext(c: Context) {
|
|
57
|
-
return new Request(c.req.url, {
|
|
58
|
-
method: c.req.method,
|
|
59
|
-
headers: c.req.header(),
|
|
60
|
-
body: c.req.raw.body,
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
52
|
/**
|
|
65
53
|
* Handles WebSocket upgrade requests
|
|
66
54
|
* Returns a WebSocket upgrade response if successful, null otherwise
|
|
@@ -69,8 +57,11 @@ async function handleWebSocketUpgrade<E extends Env>(
|
|
|
69
57
|
c: Context<E>,
|
|
70
58
|
options?: AgentOptions<E>
|
|
71
59
|
) {
|
|
72
|
-
const
|
|
73
|
-
|
|
60
|
+
const response = await routeAgentRequest(
|
|
61
|
+
c.req.raw,
|
|
62
|
+
env(c) satisfies Env,
|
|
63
|
+
options
|
|
64
|
+
);
|
|
74
65
|
|
|
75
66
|
if (!response?.webSocket) {
|
|
76
67
|
return null;
|
|
@@ -90,7 +81,5 @@ async function handleHttpRequest<E extends Env>(
|
|
|
90
81
|
c: Context<E>,
|
|
91
82
|
options?: AgentOptions<E>
|
|
92
83
|
) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return routeAgentRequest(req, env(c) satisfies Env, options);
|
|
84
|
+
return routeAgentRequest(c.req.raw, env(c) satisfies Env, options);
|
|
96
85
|
}
|