Skip to content

Commit

Permalink
加载脚本文件的插件,响应内容优化。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Apr 2, 2024
1 parent 00d52f1 commit 23a57f7
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const through = require('through2')
const zlib = require('zlib')
// eslint-disable-next-line no-unused-vars
const url = require('url')

var httpUtil = {}
const httpUtil = {}
httpUtil.getCharset = function (res) {
const contentType = res.getHeader('content-type')
const reg = /charset=(.*)/
Expand All @@ -14,11 +12,11 @@ httpUtil.getCharset = function (res) {
return 'utf-8'
}
httpUtil.isGzip = function (res) {
var contentEncoding = res.headers['content-encoding']
const contentEncoding = res.headers['content-encoding']
return !!(contentEncoding && contentEncoding.toLowerCase() === 'gzip')
}
httpUtil.isHtml = function (res) {
var contentType = res.headers['content-type']
const contentType = res.headers['content-type']
return (typeof contentType !== 'undefined') && /text\/html|application\/xhtml\+xml/.test(contentType)
}
const HEAD = Buffer.from('</head>')
Expand All @@ -42,6 +40,7 @@ function chunkByteReplace (_this, chunk, enc, callback, append) {
_this.push(chunk)
callback()
}

function injectScriptIntoHtml (tags, chunk, script) {
for (const tag of tags) {
const index = chunk.indexOf(tag)
Expand Down Expand Up @@ -71,9 +70,18 @@ module.exports = {
const filename = urlPath.replace(contextPath, '')

const script = monkey.get(setting.script.defaultDir)[filename]
// log.info(`urlPath: ${urlPath}, fileName: ${filename}, script: ${script}`)

log.info('ds_script', filename, script != null)
res.writeHead(200)
log.info('ds_script, filename:', filename, ', `script != null` =', script != null)
const now = new Date()
res.writeHead(200, {
'DS-Middleware': 'ds_script',
'Content-Type': 'application/javascript; charset=utf-8',
'Cache-Control': 'public, max-age=86401, immutable', // 缓存1天
'Last-Modified': now.toUTCString(),
Expires: new Date(now.getTime() + 86400000).toUTCString(), // 缓存1天
Date: new Date().toUTCString()
})
res.write(script.script)
res.end()
return true
Expand Down

0 comments on commit 23a57f7

Please sign in to comment.