diff --git a/middleware/jsrt/cfg.go b/middleware/jsrt/cfg.go index fc333f94c..8f2a0488a 100644 --- a/middleware/jsrt/cfg.go +++ b/middleware/jsrt/cfg.go @@ -6,7 +6,7 @@ import ( "time" ) -// / Runtime 配置 +// Runtime 配置 type JSRuntimeConfig struct { Enabled bool `json:"enabled"` MaxVMCount int `json:"max_vm_count"` diff --git a/middleware/jsrt/jsrt.go b/middleware/jsrt/jsrt.go index b14d14dbe..95e61dd55 100644 --- a/middleware/jsrt/jsrt.go +++ b/middleware/jsrt/jsrt.go @@ -18,7 +18,7 @@ import ( "github.com/gin-gonic/gin" ) -// / 池化 +// 池化 type JSRuntimePool struct { pool chan *goja.Runtime maxSize int @@ -267,18 +267,18 @@ func (p *JSRuntimePool) PreProcessRequest(c *gin.Context) error { return nil } - jsCtx := createJSContext(c) - if jsCtx == nil { + jsReq := createJSReq(c) + if jsReq == nil { return fmt.Errorf("failed to create JS context") } result, err := p.executeWithTimeout(vm, func() (goja.Value, error) { - vm.Set("ctx", jsCtx) + vm.Set("req", jsReq) fn, ok := goja.AssertFunction(preProcessFunc) if !ok { return nil, fmt.Errorf("preProcessRequest is not a function") } - return fn(goja.Undefined(), vm.ToValue(jsCtx)) + return fn(goja.Undefined(), vm.ToValue(jsReq)) }) if err != nil { @@ -366,8 +366,8 @@ func (p *JSRuntimePool) PostProcessResponse(c *gin.Context, statusCode int, body return statusCode, body, nil } - jsCtx := createJSContext(c) - if jsCtx == nil { + jsReq := createJSReq(c) + if jsReq == nil { return statusCode, body, fmt.Errorf("failed to create JS context") } @@ -387,12 +387,12 @@ func (p *JSRuntimePool) PostProcessResponse(c *gin.Context, statusCode int, body } result, err := p.executeWithTimeout(vm, func() (goja.Value, error) { - vm.Set("ctx", jsCtx) + vm.Set("req", jsReq) fn, ok := goja.AssertFunction(postProcessFunc) if !ok { return nil, fmt.Errorf("postProcessResponse is not a function") } - return fn(goja.Undefined(), vm.ToValue(jsCtx), vm.ToValue(jsResponse)) + return fn(goja.Undefined(), vm.ToValue(jsReq), vm.ToValue(jsResponse)) }) if err != nil { diff --git a/middleware/jsrt/ctx.go b/middleware/jsrt/req.go similarity index 96% rename from middleware/jsrt/ctx.go rename to middleware/jsrt/req.go index c5cee0389..d9e29d8ea 100644 --- a/middleware/jsrt/ctx.go +++ b/middleware/jsrt/req.go @@ -10,8 +10,8 @@ import ( "github.com/gin-gonic/gin" ) -// / 上下文 -type JSContext struct { +// 请求 +type JSReq struct { Method string `json:"method"` URL string `json:"url"` Headers map[string]string `json:"headers"` @@ -36,7 +36,7 @@ type responseWriter struct { mu sync.RWMutex } -func createJSContext(c *gin.Context) *JSContext { +func createJSReq(c *gin.Context) *JSReq { var bodyBytes []byte if c.Request != nil && c.Request.Body != nil { bodyBytes, _ = io.ReadAll(c.Request.Body) @@ -74,7 +74,7 @@ func createJSContext(c *gin.Context) *JSContext { parsedBody := parseBodyByType(bodyBytes, contentType) - return &JSContext{ + return &JSReq{ Method: method, URL: url, Headers: headers, diff --git a/scripts/pre_process.js b/scripts/pre_process.js index 72047f9b0..1b365456e 100644 --- a/scripts/pre_process.js +++ b/scripts/pre_process.js @@ -1,23 +1,23 @@ // 请求预处理 // 在请求被处理之前执行的函数 // -// @param {Object} ctx - 请求上下文对象 +// @param {Object} req - 请求对象 // @returns {Object|undefined} - 返回修改后的请求对象或 undefined // -// 参考: [JS Rt](./middleware/jsrt/ctx.go) 里的 `JSContext` -function preProcessRequest(ctx) { +// 参考: [JS Rt](./middleware/jsrt/req.go) 里的 `JSReq` +function preProcessRequest(req) { // 例子:基于数据库的速率限制 - // if (ctx.url.includes("/v1/chat/completions")) { + // if (req.url.includes("/v1/chat/completions")) { // try { // // Check recent requests from this IP // var recentRequests = db.Query( // "SELECT COUNT(*) as count FROM logs WHERE created_at > ? AND ip = ?", // Math.floor(Date.now() / 1000) - 60, // last minute - // ctx.remoteIP + // req.remoteIP // ); // if (recentRequests && recentRequests.length > 0 && recentRequests[0].count > 10) { - // console.log("速率限制 IP:", ctx.RemoteIP); + // console.log("速率限制 IP:", req.RemoteIP); // return { // block: true, // statusCode: 429, @@ -30,9 +30,9 @@ function preProcessRequest(ctx) { // } // 例子:修改请求 - // if (ctx.URL.includes("/v1/chat/completions")) { + // if (req.URL.includes("/v1/chat/completions")) { // try { - // var bodyObj = ctx.Body; + // var bodyObj = req.Body; // let firstMsg = { // 需要新建一个对象,不能修改原有对象 // role: "user", @@ -45,7 +45,7 @@ function preProcessRequest(ctx) { // return { // body: bodyObj, // headers: { - // ...ctx.Headers, + // ...req.Headers, // "X-Modified-Body": "true" // } // }; @@ -53,14 +53,14 @@ function preProcessRequest(ctx) { // console.error("Failed to modify request body:", { // message: e.message, // stack: e.stack, - // bodyType: typeof ctx.Body, - // url: ctx.URL + // bodyType: typeof req.Body, + // url: req.URL // }); // } // } // // 例子:读取最近一条日志,新增 jsrt 日志,并输出日志总数 - // if (ctx.URL) { + // if (req.URL) { // try { // // 1. 读取最近一条日志 // var recentLogs = logdb.Query( @@ -75,12 +75,12 @@ function preProcessRequest(ctx) { // // 2. 新增一条 jsrt 日志 // var currentTimestamp = Math.floor(Date.now() / 1000); - // var jsrtLogContent = "JSRT 预处理中间件执行 - " + ctx.URL + " - " + new Date().toISOString(); + // var jsrtLogContent = "JSRT 预处理中间件执行 - " + req.URL + " - " + new Date().toISOString(); // var insertResult = logdb.Exec( // "INSERT INTO logs (user_id, username, created_at, type, content) VALUES (?, ?, ?, ?, ?)", - // ctx.UserID || 0, - // ctx.Username || "jsrt-system", + // req.UserID || 0, + // req.Username || "jsrt-system", // currentTimestamp, // 4, // LogTypeSystem // jsrtLogContent @@ -106,13 +106,13 @@ function preProcessRequest(ctx) { // console.error("JSRT 日志管理示例执行失败:", { // message: e.message, // stack: e.stack, - // url: ctx.URL + // url: req.URL // }); // } // } // // 例子:使用 fetch 调用外部 API - // if (ctx.URL.includes("/api/uptime/status")) { + // if (req.URL.includes("/api/uptime/status")) { // try { // // 使用 httpbin.org/ip 测试 fetch 功能 // var response = fetch("https://httpbin.org/ip", { @@ -137,8 +137,8 @@ function preProcessRequest(ctx) { // var insertResult = logdb.Exec( // "INSERT INTO logs (user_id, username, created_at, type, content) VALUES (?, ?, ?, ?, ?)", - // ctx.UserID || 0, - // ctx.Username || "jsrt-fetch", + // req.UserID || 0, + // req.Username || "jsrt-fetch", // currentTimestamp, // 4, // LogTypeSystem // logContent @@ -157,7 +157,7 @@ function preProcessRequest(ctx) { // console.error("Fetch 失败:", { // message: e.message, // stack: e.stack, - // url: ctx.URL + // url: req.URL // }); // } // }