-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use strict"; | ||
|
||
const HyperSwitch = require('hyperswitch'); | ||
const URI = HyperSwitch.URI; | ||
const mwUtil = require('./mwUtil'); | ||
|
||
module.exports = (hyper, req, next, options, specInfo) => { | ||
const rp = req.params; | ||
const acceptLanguage = req.headers && req.headers['accept-language']; | ||
if (!acceptLanguage || mwUtil.isNoCacheRequest(req)) { | ||
delete req.headers['accept-language']; | ||
return next(hyper, req); | ||
} | ||
|
||
const revTableURI = [rp.domain, 'sys', 'page_revisions', 'page', rp.title]; | ||
if (rp.revision) { | ||
revTableURI.push(`${rp.revision}`); | ||
} | ||
return hyper.get({ uri: new URI(revTableURI) }) | ||
.then((res) => { | ||
const revision = res.body.items[0]; | ||
return mwUtil.shouldConvertLangVariant(hyper, req, revision.page_language, acceptLanguage) | ||
.then((shouldConvert) => { | ||
if (!shouldConvert) { | ||
delete req.headers['accept-language']; | ||
return next(hyper, req); | ||
} | ||
|
||
if (/\/page\/html\//.test(specInfo.path)) { | ||
// It's HTML, hit Parsoid for conversion | ||
return next(hyper, req) | ||
.then(html => hyper.post({ | ||
uri: new URI([rp.domain, 'sys', 'parsoid', 'transform', 'html', 'to', 'html']), | ||
headers: { | ||
'content-type': 'application/json', | ||
'content-language': revision.page_language | ||
}, | ||
body: { | ||
original: { | ||
html: { | ||
headers: html.headers, | ||
body: html.body.toString() | ||
} | ||
}, | ||
updates: { | ||
variant: { | ||
source: null, | ||
target: acceptLanguage | ||
} | ||
} | ||
} | ||
})); | ||
// We can skip setting Vary: accept-language as Parsoid sets it. | ||
} else { | ||
// TODO: It's something else, so just forward to MCS | ||
return next(hyper, req); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters