Files
CJCIM_SYS/core.js
❀ » Cato Sweeney. ❀ » Console@the.bb e9e409d958 *
2025-12-03 12:53:19 +08:00

109 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// core.js 的 main 函数是一切脚本函数执行的预处理入口
function main() {
// 拒绝任何请求访问和操作以下文件名的路径
if (
payload.get().url.includes('function.js') ||
payload.get().url.includes('core.js') ||
payload.get().url.includes('global.js')
) {
//当请求的路径包含以上文件名时,返回 404
response.contentType.html()
response.status.notFound()
return `<h1>404!</h1>`
}
// 声明路径
const filePath = rootPath + payload.get().path
switch (payload.get().method) {
case 'OPTIONS':
// 这样对于所有的 OPTIONS 请求都会返回 200 (OK) 状态码
response.status.ok()
return
// 当接收的请求为 GET 时
case 'GET':
// 声明缓存
response.headers.set(
'Cache-Control',
'no-transform,max-age=31536000,immutable'
)
// 拒绝任何接口通过GET访问和操作以下文件名的路径
if (
payload.get().url.includes('/api') &&
payload.get().url.includes('function.js')
) {
//当请求的路径包含以上文件名时,返回 404
response.contentType.html()
response.status.notFound()
return `<h1>404!</h1>`
}
// 判断路径是否为目录
if (fs.isDir(filePath)) {
// 设置 Content-Type
response.headers.set('Content-Type', 'text/html; charset=utf-8;')
// 如果是目录,则尝试返回目录下的 index.htm
var indexFile = filePath + '/index.htm'
if (fs.exists(filePath)) {
// 如果 index.htm 存在,则返回 index.htm
response.file(filePath)
return ``
}
// 如果是index.htm不存在则尝试返回目录下的 index.html
indexFile = indexFile + 'l'
if (fs.exists(filePath)) {
// 如果 index.html 存在,则返回 index.html
response.file(filePath)
return ``
}
// 则将请求移交给对应path下的 function.js 进行处理。
runtime.call(filePath) // call会将 return 的内容写到 response.body 里面。
return `` // 如果不需要在call的结果后面追加内容这里请设置为空
}
// 如果不是目录,则尝试返回文件
if (fs.exists(filePath)) {
if (payload.get().query.download) {
// 设置 Content-Disposition
response.headers.set('Content-Disposition', 'attachment;')
}
if (
filePath.includes('.html') ||
filePath.includes('.htm') ||
filePath[filePath.length - 1] == '/'
) {
response.headers.set('Content-Type', 'text/html; charset=utf-8;')
}
// response.headers.set("Content-Type", "text/html; charset=utf-8;")
// 设置 Content-Type虽然会根据URL的文件后缀自动设置但是为了安全起见你也可以手动设置一下。
// response.headers.set("Content-Type", "application/octet-stream; charset=utf-8")
// 如果文件存在,则返回文件
response.file(filePath)
return `` // 如果不需要在call的结果后面追加内容这里请设置为空
}
// 则将请求移交给对应path下的 function.js 进行处理。
runtime.call(filePath) // call会将 return 的内容写到 response.body 里面。
return `` // 如果不需要在call的结果后面追加内容这里请设置为空
// 如果客户端发起 POST
case 'POST':
// 则将请求移交给对应path下的 function.js 进行处理。
runtime.call(filePath) // call会将 return 的内容写到 response.body 里面。
return `` // 如果不需要在call的结果后面追加内容这里请设置为空
// 如果客户端发起 PUT
case 'PUT':
// 将请求交给 response 库的的 upload 函数处理
// console.log(payload.get().body);
// return encoding.from(payload.get().body).toString();
response.upload(filePath)
return ``
}
// 当路由处理逻辑不存在
response.contentType.html()
response.status.methodNotAllowed()
return `<h1>405!</h1>`
}