Compare commits
7 Commits
be2e45c4fc
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 82ab65fa02 | |||
| b78ca6a793 | |||
| 12b64737a8 | |||
| 0f9407dc31 | |||
| 47eb5f1c41 | |||
| b26d0abc93 | |||
| 8b20764404 |
@@ -1 +1,2 @@
|
||||
path: string
|
||||
path: string
|
||||
deleteAfter: number
|
||||
28
index.ts
28
index.ts
@@ -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)
|
||||
})
|
||||
|
||||
15
types.ts
15
types.ts
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user