* Fetches an AMP document from the AMP proxy and replaces JS * URLs, so that they point to localhost. * * @param {express.Request} req * @param {express.Response} res * @param {string} mode * @return {Promise }
(req, res, mode)
| 506 | * @return {Promise<void>} |
| 507 | */ |
| 508 | async function proxyToAmpProxy(req, res, mode) { |
| 509 | const url = |
| 510 | 'https://cdn.ampproject.org/' + |
| 511 | (req.query['amp_js_v'] ? 'v' : 'c') + |
| 512 | req.url; |
| 513 | logWithoutTimestamp('Fetching URL: ' + url); |
| 514 | const urlResponse = await fetch(url); |
| 515 | let body = await urlResponse.text(); |
| 516 | body = body |
| 517 | // Unversion URLs. |
| 518 | .replace( |
| 519 | /https\:\/\/cdn\.ampproject\.org\/rtv\/\d+\//g, |
| 520 | 'https://cdn.ampproject.org/' |
| 521 | ) |
| 522 | // <base> href pointing to the proxy, so that images, etc. still work. |
| 523 | .replace('<head>', '<head><base href="https://cdn.ampproject.org/">'); |
| 524 | const inabox = req.query['inabox']; |
| 525 | const urlPrefix = getUrlPrefix(req); |
| 526 | if (req.query['mraid']) { |
| 527 | body = body |
| 528 | .replace( |
| 529 | '</head>', |
| 530 | '<script async host-service="amp-mraid" src="https://cdn.ampproject.org/v0/amp-mraid-0.1.js">' + |
| 531 | '</script>' + |
| 532 | '</head>' |
| 533 | ) |
| 534 | // Change cdnUrl from the default so amp-mraid requests the (mock) |
| 535 | // mraid.js from the local server. In a real environment this doesn't |
| 536 | // matter as the local environment would intercept this request. |
| 537 | .replace( |
| 538 | '<head>', |
| 539 | ' <head>' + |
| 540 | ' <script>' + |
| 541 | ' window.AMP_CONFIG = {' + |
| 542 | ` cdnUrl: "${urlPrefix}",` + |
| 543 | ' };' + |
| 544 | ' </script>' |
| 545 | ); |
| 546 | } |
| 547 | if (inabox) { |
| 548 | body = toInaboxDocument(body); |
| 549 | // Allow CORS requests for A4A. |
| 550 | const origin = req.headers.origin || urlPrefix; |
| 551 | cors.enableCors(req, res, origin); |
| 552 | } |
| 553 | body = replaceUrls(mode, body, urlPrefix); |
| 554 | res.status(urlResponse.status).send(body); |
| 555 | } |
| 556 | |
| 557 | let itemCtr = 2; |
| 558 | const doctype = '<!doctype html>\n'; |
no test coverage detected