Compare commits

..

7 Commits

Author SHA1 Message Date
82ab65fa02 feat: 添加自動刪除功能 2025-01-27 17:46:41 +08:00
b78ca6a793 fix: 修復文件監聽重復問題 2025-01-27 17:37:25 +08:00
12b64737a8 perf: 去掉沒使用過的timeout 2025-01-27 17:10:40 +08:00
0f9407dc31 feat: 加幾條日志 2025-01-27 17:04:21 +08:00
47eb5f1c41 fix: 修復GET模式下攜帶Body報錯 2025-01-27 16:51:06 +08:00
b26d0abc93 fix: 修復未自動添加Headers 2025-01-27 16:48:15 +08:00
8b20764404 fix: 修復路徑錯誤問題 2025-01-27 16:37:02 +08:00
4 changed files with 31 additions and 21 deletions

View File

@@ -1 +1,2 @@
path: string
path: string
deleteAfter: number

View File

@@ -2,32 +2,35 @@ import fs from 'fs'
import got, {HTTPError, Options} from 'got'
import YAML from 'yaml'
import {timeout} from './util.js'
import {IrequestData} from './types.js'
import {Iconfig, IrequestData} from './types.js'
const path: string = YAML.parse(fs.readFileSync('../config.yml', 'utf-8')).path
const config: Iconfig = YAML.parse(fs.readFileSync('../config.yml', 'utf-8'))
const lock: string[] = []
fs.watch(path, async function (event, filename) {
if (!(filename && event === 'change')) return
const jpath: string = `./${filename}`
fs.watch(config.path, async function (event, filename) {
if (!(filename && event === 'change') || lock.includes(filename)) return
lock.push(filename)
const jpath: string = `${config.path}/${filename}`
const requestData: IrequestData = JSON.parse(fs.readFileSync(jpath).toString())
if (requestData.Processed === true) return
const method: any = requestData.Request.Mode.toUpperCase()
if (!['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'].includes(method)) {
console.error('無效的請求方式')
return
}
requestData.Response = {Headers: {}}
console.log(`接收到請求:${filename} => ${method} ${requestData.Request.URL}`)
try {
const res = await got({
url: requestData.Request.URL,
method: method,
headers: requestData.Request.Headers,
body: requestData.Request.Body,
body: method !== 'GET' ? requestData.Request.Body : undefined,
})
requestData.Response.StatusCode = res.statusCode
Object.keys(res.headers).forEach((e) => {
requestData.Response.Headers[e] = res.headers[e]
if (requestData.Response) requestData.Response.Headers[e] = res.headers[e]
})
requestData.Response.Body = res.body
} catch (error) {
@@ -35,8 +38,15 @@ fs.watch(path, async function (event, filename) {
requestData.Response.StatusCode = error.response.statusCode
} else {
requestData.Response.StatusCode = -1
console.error(`處理失敗:${filename}\n`, error)
}
}
requestData.Processed = true
fs.writeFileSync(jpath, JSON.stringify(requestData))
setTimeout(() => {
fs.unlinkSync(jpath)
setTimeout(() => {
lock.splice(lock.indexOf(filename), 1)
}, 300)
}, config.deleteAfter * 1000)
})

View File

@@ -2,13 +2,18 @@ export interface IrequestData {
Request: {
Mode: string
URL: string
Headers: any
Body: string
Headers?: any
Body?: string
}
Response: {
StatusCode: number
Response?: {
StatusCode?: number
Headers: any
Body: string
Body?: string
}
Processed?: boolean
}
export interface Iconfig {
path: string
deleteAfter: number
}

View File

@@ -1,6 +0,0 @@
export function timeout(ms: number): Promise<unknown> {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms)
})
}