(jsrt) opt.: move req from global to local

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-07-16 13:24:59 +08:00
parent 9d9070c899
commit 99a2fc5852
5 changed files with 14 additions and 23 deletions

View File

@@ -319,7 +319,6 @@ func (p *JSRuntimePool) PreProcessRequest(c *gin.Context) error {
}
result, err := p.executeWithTimeout(vm, func() (goja.Value, error) {
vm.Set("req", jsReq)
fn, ok := goja.AssertFunction(preProcessFunc)
if !ok {
return nil, fmt.Errorf("preProcessRequest is not a function")
@@ -417,7 +416,7 @@ func (p *JSRuntimePool) PostProcessResponse(c *gin.Context, statusCode int, body
return statusCode, body, fmt.Errorf("failed to create JS context: %v", err)
}
jsResponse := &JSResponse{
jsResp := &JSResponse{
StatusCode: statusCode,
Headers: make(map[string]string),
Body: string(body),
@@ -427,13 +426,16 @@ func (p *JSRuntimePool) PostProcessResponse(c *gin.Context, statusCode int, body
if c.Writer != nil {
for key, values := range c.Writer.Header() {
if len(values) > 0 {
jsResponse.Headers[key] = values[0]
jsResp.Headers[key] = values[0]
}
}
}
jsResponse, err := common.StructToMap(jsResp)
if err != nil {
return statusCode, body, fmt.Errorf("failed to create JS response context: %v", err)
}
result, err := p.executeWithTimeout(vm, func() (goja.Value, error) {
vm.Set("req", jsReq)
fn, ok := goja.AssertFunction(postProcessFunc)
if !ok {
return nil, fmt.Errorf("postProcessResponse is not a function")
@@ -537,22 +539,11 @@ func JSRuntimeMiddleware() *gin.HandlerFunc {
c.Writer.Header().Del("Content-Length")
c.Writer.Write(body)
}
common.SysLog(fmt.Sprintf("JS Runtime PostProcessing Completed with status %d", statusCode))
} else {
// 出错时回复原响应
c.Writer = writer.ResponseWriter
c.Status(writer.statusCode)
originalBody := writer.body.Bytes()
if len(originalBody) >= 0 {
c.Writer.Header().Set("Content-Length", fmt.Sprintf("%d", len(originalBody)))
c.Writer.Write(originalBody)
} else {
c.Writer.Header().Del("Content-Length")
c.Writer.Write(originalBody)
}
common.SysError(fmt.Sprintf("JS Runtime PostProcess Error: %v", err))
}

View File

@@ -1,6 +1,6 @@
// Utility functions for JavaScript runtime
function logWithReq(message) {
function logWithReq(req, message) {
let reqPath = req.url || 'unknown path';
console.log(`[${req.method} ${reqPath}] ${message}`);
}

View File

@@ -1,5 +1,5 @@
// Pre-processing function for incoming requests
function preProcessRequest() {
logWithReq('Pre-processing request');
function preProcessRequest(req) {
logWithReq(req, 'Pre-processing request');
}

View File

@@ -1,5 +1,5 @@
// Post-processing function for outgoing responses
function postProcessResponse() {
logWithReq('Post-processing response with status: ' + req.statusCode);
function postProcessResponse(req, resp) {
logWithReq(req, 'Post-processing response with: ' + resp.statusCode);
}

View File

@@ -42,14 +42,14 @@
- `headers`: object - 要修改或添加的请求头
- `body`: any - 修改后的请求体
### 2. postProcessResponse(req, res)
### 2. postProcessResponse(req, resp)
在响应返回给客户端之前调用。
**参数:**
- `req`: 原始请求对象
- `res`: 响应对象,包含 `statusCode`, `headers`, `body` 等属性
- `resp`: 响应对象,包含 `statusCode`, `headers`, `body` 等属性
**返回值:**
返回一个对象,可包含以下属性:
@@ -182,7 +182,7 @@ if (req.url.includes("/api/uptime/status")) {
method: "GET",
timeout: 5, // 5秒超时
headers: {
"User-Agent": "OneAPI-JSRT/1.0"
"User-Agent": "JSRT/1.0"
}
});
if (response.Error.length === 0) {