@nextclaw/server 0.10.20 → 0.10.21
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 +65 -3
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// src/ui/server.ts
|
|
2
2
|
import { Hono as Hono2 } from "hono";
|
|
3
3
|
import { compress } from "hono/compress";
|
|
4
|
-
import { cors } from "hono/cors";
|
|
5
4
|
import { serve } from "@hono/node-server";
|
|
6
5
|
import { WebSocketServer, WebSocket } from "ws";
|
|
7
6
|
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
@@ -5162,12 +5161,75 @@ var DEFAULT_CORS_ORIGINS = (origin) => {
|
|
|
5162
5161
|
}
|
|
5163
5162
|
return void 0;
|
|
5164
5163
|
};
|
|
5164
|
+
var DEFAULT_ALLOWED_CORS_HEADERS = "Content-Type, Authorization";
|
|
5165
|
+
var DEFAULT_ALLOWED_CORS_METHODS = "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS";
|
|
5166
|
+
function readRequestHeader(request, name) {
|
|
5167
|
+
return request.headers.get(name)?.trim() ?? null;
|
|
5168
|
+
}
|
|
5169
|
+
function appendVaryHeader(headers, value) {
|
|
5170
|
+
const current = headers.get("Vary");
|
|
5171
|
+
if (!current) {
|
|
5172
|
+
headers.set("Vary", value);
|
|
5173
|
+
return;
|
|
5174
|
+
}
|
|
5175
|
+
const values = current.split(",").map((item) => item.trim()).filter(Boolean);
|
|
5176
|
+
if (!values.includes(value)) {
|
|
5177
|
+
values.push(value);
|
|
5178
|
+
}
|
|
5179
|
+
headers.set("Vary", values.join(", "));
|
|
5180
|
+
}
|
|
5181
|
+
function resolveAllowedCorsOrigin(requestOrigin, policy) {
|
|
5182
|
+
if (!requestOrigin) {
|
|
5183
|
+
return null;
|
|
5184
|
+
}
|
|
5185
|
+
if (policy === "*") {
|
|
5186
|
+
return requestOrigin;
|
|
5187
|
+
}
|
|
5188
|
+
if (Array.isArray(policy)) {
|
|
5189
|
+
return policy.includes(requestOrigin) ? requestOrigin : null;
|
|
5190
|
+
}
|
|
5191
|
+
return policy(requestOrigin) ?? null;
|
|
5192
|
+
}
|
|
5193
|
+
function applyCorsHeaders(params) {
|
|
5194
|
+
params.headers.set("Access-Control-Allow-Origin", params.allowOrigin);
|
|
5195
|
+
params.headers.set("Access-Control-Allow-Credentials", "true");
|
|
5196
|
+
params.headers.set("Access-Control-Allow-Methods", DEFAULT_ALLOWED_CORS_METHODS);
|
|
5197
|
+
params.headers.set(
|
|
5198
|
+
"Access-Control-Allow-Headers",
|
|
5199
|
+
params.allowHeaders?.trim() || DEFAULT_ALLOWED_CORS_HEADERS
|
|
5200
|
+
);
|
|
5201
|
+
appendVaryHeader(params.headers, "Origin");
|
|
5202
|
+
appendVaryHeader(params.headers, "Access-Control-Request-Headers");
|
|
5203
|
+
}
|
|
5165
5204
|
function startUiServer(options) {
|
|
5166
5205
|
const app = new Hono2();
|
|
5167
5206
|
app.use("/*", compress());
|
|
5168
|
-
const
|
|
5207
|
+
const corsPolicy = options.corsOrigins ?? DEFAULT_CORS_ORIGINS;
|
|
5169
5208
|
const authService = new UiAuthService(options.configPath);
|
|
5170
|
-
app.use("/api/*",
|
|
5209
|
+
app.use("/api/*", async (c, next) => {
|
|
5210
|
+
const allowOrigin = resolveAllowedCorsOrigin(readRequestHeader(c.req.raw, "origin"), corsPolicy);
|
|
5211
|
+
const allowHeaders = readRequestHeader(c.req.raw, "access-control-request-headers");
|
|
5212
|
+
if (c.req.method === "OPTIONS") {
|
|
5213
|
+
if (allowOrigin) {
|
|
5214
|
+
const headers = new Headers();
|
|
5215
|
+
applyCorsHeaders({
|
|
5216
|
+
headers,
|
|
5217
|
+
allowOrigin,
|
|
5218
|
+
allowHeaders
|
|
5219
|
+
});
|
|
5220
|
+
return new Response(null, { status: 204, headers });
|
|
5221
|
+
}
|
|
5222
|
+
return new Response(null, { status: 204 });
|
|
5223
|
+
}
|
|
5224
|
+
await next();
|
|
5225
|
+
if (allowOrigin) {
|
|
5226
|
+
applyCorsHeaders({
|
|
5227
|
+
headers: c.res.headers,
|
|
5228
|
+
allowOrigin,
|
|
5229
|
+
allowHeaders
|
|
5230
|
+
});
|
|
5231
|
+
}
|
|
5232
|
+
});
|
|
5171
5233
|
const clients = /* @__PURE__ */ new Set();
|
|
5172
5234
|
const publish = (event) => {
|
|
5173
5235
|
const payload = JSON.stringify(event);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextclaw/server",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.21",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Nextclaw UI/API server.",
|
|
6
6
|
"type": "module",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"@hono/node-server": "^1.13.3",
|
|
19
19
|
"hono": "^4.6.2",
|
|
20
20
|
"ws": "^8.18.0",
|
|
21
|
-
"@nextclaw/mcp": "0.1.
|
|
22
|
-
"@nextclaw/ncp": "0.3.1",
|
|
23
|
-
"@nextclaw/ncp-http-agent-server": "0.3.1",
|
|
21
|
+
"@nextclaw/mcp": "0.1.21",
|
|
24
22
|
"@nextclaw/runtime": "0.2.8",
|
|
23
|
+
"@nextclaw/core": "0.9.8",
|
|
24
|
+
"@nextclaw/ncp-http-agent-server": "0.3.1",
|
|
25
25
|
"@nextclaw/openclaw-compat": "0.3.11",
|
|
26
|
-
"@nextclaw/
|
|
26
|
+
"@nextclaw/ncp": "0.3.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^20.17.6",
|