hono-agents 0.0.24 → 0.0.25
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 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +8 -1
package/dist/index.js
CHANGED
|
@@ -40,7 +40,12 @@ async function handleWebSocketUpgrade(c, options) {
|
|
|
40
40
|
}
|
|
41
41
|
async function handleHttpRequest(c, options) {
|
|
42
42
|
const req = createRequestFromContext(c);
|
|
43
|
-
return routeAgentRequest(
|
|
43
|
+
return routeAgentRequest(
|
|
44
|
+
req,
|
|
45
|
+
env(c),
|
|
46
|
+
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
47
|
+
options
|
|
48
|
+
);
|
|
44
49
|
}
|
|
45
50
|
export {
|
|
46
51
|
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-sdk\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents-sdk\";\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 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), 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 return routeAgentRequest(req
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { env } from \"hono/adapter\";\nimport { createMiddleware } from \"hono/factory\";\nimport { routeAgentRequest } from \"agents-sdk\";\n\nimport type { Context, Env } from \"hono\";\nimport type { AgentOptions } from \"agents-sdk\";\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 // @ts-expect-error - TODO: fix this, I'm just bad at TS\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 // @ts-expect-error - TODO: fix this, I'm just bad at TS\n const response = await routeAgentRequest(req, env(c), 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 return routeAgentRequest(\n req,\n env(c),\n // @ts-expect-error - TODO: fix this, I'm just bad at TS\n options\n );\n}\n"],"mappings":";AAAA,SAAS,WAAW;AACpB,SAAS,wBAAwB;AACjC,SAAS,yBAAyB;AAmB3B,SAAS,iBACd,KACA;AACA,SAAO,iBAAiB,OAAO,GAAG,SAAS;AACzC,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;AAEtC,QAAM,WAAW,MAAM,kBAAkB,KAAK,IAAI,CAAC,GAAG,OAAO;AAE7D,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;AACtC,SAAO;AAAA,IACL;AAAA,IACA,IAAI,CAAC;AAAA;AAAA,IAEL;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-agents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.25",
|
|
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-sdk": "^0.0.
|
|
38
|
+
"agents-sdk": "^0.0.35",
|
|
39
39
|
"hono": "^4.6.17"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"agents-sdk": "^0.0.
|
|
42
|
+
"agents-sdk": "^0.0.35",
|
|
43
43
|
"hono": "^4.7.4"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -27,6 +27,7 @@ export function agentsMiddleware<E extends Env = Env>(
|
|
|
27
27
|
const handler = isWebSocketUpgrade(c)
|
|
28
28
|
? handleWebSocketUpgrade
|
|
29
29
|
: handleHttpRequest;
|
|
30
|
+
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
30
31
|
const response = await handler(c, ctx?.options);
|
|
31
32
|
|
|
32
33
|
return response === null ? await next() : response;
|
|
@@ -69,6 +70,7 @@ async function handleWebSocketUpgrade<E extends Env>(
|
|
|
69
70
|
options?: AgentOptions<E>
|
|
70
71
|
) {
|
|
71
72
|
const req = createRequestFromContext(c);
|
|
73
|
+
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
72
74
|
const response = await routeAgentRequest(req, env(c), options);
|
|
73
75
|
|
|
74
76
|
if (!response?.webSocket) {
|
|
@@ -90,5 +92,10 @@ async function handleHttpRequest<E extends Env>(
|
|
|
90
92
|
options?: AgentOptions<E>
|
|
91
93
|
) {
|
|
92
94
|
const req = createRequestFromContext(c);
|
|
93
|
-
return routeAgentRequest(
|
|
95
|
+
return routeAgentRequest(
|
|
96
|
+
req,
|
|
97
|
+
env(c),
|
|
98
|
+
// @ts-expect-error - TODO: fix this, I'm just bad at TS
|
|
99
|
+
options
|
|
100
|
+
);
|
|
94
101
|
}
|