Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
fix backpost http header
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoyane committed Mar 17, 2024
1 parent d801473 commit e60bd0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ function getMineType(url, currentContentType) {
return currentContentType;
}

function isContent(originalUrl) {
if (originalUrl.startsWith('/image') || originalUrl.startsWith('/icons') || originalUrl.endsWith('.wasm')) {
return true
}
return false;
}

module.exports = {
generateSitemap: generateSitemap,
generateNotionUrl: generateNotionUrl,
getMineType: getMineType,
isContent: isContent,
};
3 changes: 2 additions & 1 deletion src/proxy/contentCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ContentCache {
return;
}

this.cache[originUrl] = new CacheData(this.toSec(Date.now()) + this.expiresSec, content);
const now = this.toSec(Date.now());
this.cache[originUrl] = new CacheData(now + this.expiresSec, content);
}

getData(originUrl) {
Expand Down
20 changes: 10 additions & 10 deletions src/proxy/notionProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class NotionProxy {
let url;
try {
url = utility.generateNotionUrl(req, res, this.PERMA_TO_PAGE, this.SLUG_TO_PAGE);
//console.log('PROXY_TO ' + url)
// console.log('[DEBUG] PROXY_TO ' + url)
} catch (e) {
if (e instanceof Redirect) {
//console.log('REDIRECT_TO ' + e.message);
// console.log('[DEBUG] REDIRECT_TO ' + e.message);
return res.redirect(301, '/' + e.message);
} else {
console.error(e)
Expand All @@ -77,16 +77,15 @@ class NotionProxy {
}

const requestHeader = req.headers
requestHeader['If-Modified-Since'] = new Date().toString();
delete requestHeader['host']
delete requestHeader['referer']
res.headers = requestHeader

let contentType = mime.lookup(req.originalUrl)
if (!contentType) {
contentType = 'text/html'
}
contentType = utility.getMineType(req.originalUrl, contentType);

res.set('Content-Type', contentType)
res.removeHeader('Content-Security-Policy')
res.removeHeader('X-Content-Security-Policy')
Expand All @@ -96,30 +95,31 @@ class NotionProxy {
return res.send(cachedData);
}

if (utility.isContent(req.originalUrl)) {
// Set it to If-Modified-Since now to accommodate 304
requestHeader['If-Modified-Since'] = new Date().toString();
}

return fetch(url, {
headers: requestHeader,
method: 'GET',
}, (error, header, body) => {
let isObjectData = false;

// See https://github.com/stephenou/fruitionsite
if (req.originalUrl.startsWith('/app') && req.originalUrl.endsWith('js')) {
res.set('Content-Type', 'application/x-javascript')
body = body.toString().replace(/www.notion.so/g, this.DOMAIN).replace(/notion.so/g, this.DOMAIN)
} else if (req.originalUrl.endsWith('css') || req.originalUrl.endsWith('js')) {
body = body.toString()
} else if (req.originalUrl.startsWith('/image') || req.originalUrl.startsWith('/icons') || req.originalUrl.endsWith('.wasm')) {
} else if (utility.isContent(req.originalUrl)) {
// Nothing
isObjectData = true;
} else if (header !== undefined) {
const dom = new JSDOM(body.toString(), { includeNodeLocations: true })
this.PARSER.parse(dom.window.document);
body = dom.serialize();
}

if (!isObjectData) {
this.CACHE_STORE.setData(req.originalUrl, body);
}
this.CACHE_STORE.setData(req.originalUrl, body);
return res.send(body);
})
}
Expand Down

0 comments on commit e60bd0b

Please sign in to comment.