Compare commits
4 Commits
47eb5f1c41
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 82ab65fa02 | |||
| b78ca6a793 | |||
| 12b64737a8 | |||
| 0f9407dc31 |
@@ -1 +1,2 @@
|
|||||||
path: string
|
path: string
|
||||||
|
deleteAfter: number
|
||||||
22
index.ts
22
index.ts
@@ -2,21 +2,24 @@ import fs from 'fs'
|
|||||||
import got, {HTTPError, Options} from 'got'
|
import got, {HTTPError, Options} from 'got'
|
||||||
import YAML from 'yaml'
|
import YAML from 'yaml'
|
||||||
|
|
||||||
import {timeout} from './util.js'
|
import {Iconfig, IrequestData} from './types.js'
|
||||||
import {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) {
|
fs.watch(config.path, async function (event, filename) {
|
||||||
if (!(filename && event === 'change')) return
|
if (!(filename && event === 'change') || lock.includes(filename)) return
|
||||||
const jpath: string = `${path}/${filename}`
|
lock.push(filename)
|
||||||
|
const jpath: string = `${config.path}/${filename}`
|
||||||
const requestData: IrequestData = JSON.parse(fs.readFileSync(jpath).toString())
|
const requestData: IrequestData = JSON.parse(fs.readFileSync(jpath).toString())
|
||||||
if (requestData.Processed === true) return
|
if (requestData.Processed === true) return
|
||||||
const method: any = requestData.Request.Mode.toUpperCase()
|
const method: any = requestData.Request.Mode.toUpperCase()
|
||||||
if (!['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'].includes(method)) {
|
if (!['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'].includes(method)) {
|
||||||
|
console.error('無效的請求方式')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
requestData.Response = {Headers: {}}
|
requestData.Response = {Headers: {}}
|
||||||
|
console.log(`接收到請求:${filename} => ${method} ${requestData.Request.URL}`)
|
||||||
try {
|
try {
|
||||||
const res = await got({
|
const res = await got({
|
||||||
url: requestData.Request.URL,
|
url: requestData.Request.URL,
|
||||||
@@ -35,8 +38,15 @@ fs.watch(path, async function (event, filename) {
|
|||||||
requestData.Response.StatusCode = error.response.statusCode
|
requestData.Response.StatusCode = error.response.statusCode
|
||||||
} else {
|
} else {
|
||||||
requestData.Response.StatusCode = -1
|
requestData.Response.StatusCode = -1
|
||||||
|
console.error(`處理失敗:${filename}\n`, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestData.Processed = true
|
requestData.Processed = true
|
||||||
fs.writeFileSync(jpath, JSON.stringify(requestData))
|
fs.writeFileSync(jpath, JSON.stringify(requestData))
|
||||||
|
setTimeout(() => {
|
||||||
|
fs.unlinkSync(jpath)
|
||||||
|
setTimeout(() => {
|
||||||
|
lock.splice(lock.indexOf(filename), 1)
|
||||||
|
}, 300)
|
||||||
|
}, config.deleteAfter * 1000)
|
||||||
})
|
})
|
||||||
|
|||||||
5
types.ts
5
types.ts
@@ -12,3 +12,8 @@ export interface IrequestData {
|
|||||||
}
|
}
|
||||||
Processed?: boolean
|
Processed?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Iconfig {
|
||||||
|
path: string
|
||||||
|
deleteAfter: number
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user