(url)
| 491 | * @return {string} |
| 492 | */ |
| 493 | export function getSourceUrl(url) { |
| 494 | url = urlAsLocation(url); |
| 495 | |
| 496 | // Not a proxy URL - return the URL itself. |
| 497 | if (!isProxyOrigin(url)) { |
| 498 | return url.href; |
| 499 | } |
| 500 | |
| 501 | // A proxy URL. |
| 502 | // Example path that is being matched here. |
| 503 | // https://cdn.ampproject.org/c/s/www.origin.com/foo/ |
| 504 | // The /s/ is optional and signals a secure origin. |
| 505 | const path = url.pathname.split('/'); |
| 506 | const prefix = path[1]; |
| 507 | userAssert( |
| 508 | SERVING_TYPE_PREFIX.has(prefix), |
| 509 | 'Unknown path prefix in url %s', |
| 510 | url.href |
| 511 | ); |
| 512 | const domainOrHttpsSignal = path[2]; |
| 513 | const origin = |
| 514 | domainOrHttpsSignal == 's' |
| 515 | ? 'https://' + decodeURIComponent(path[3]) |
| 516 | : 'http://' + decodeURIComponent(domainOrHttpsSignal); |
| 517 | // Sanity test that what we found looks like a domain. |
| 518 | userAssert(origin.indexOf('.') > 0, 'Expected a . in origin %s', origin); |
| 519 | path.splice(1, domainOrHttpsSignal == 's' ? 3 : 2); |
| 520 | return ( |
| 521 | origin + |
| 522 | path.join('/') + |
| 523 | removeAmpJsParamsFromSearch(url.search) + |
| 524 | (url.hash || '') |
| 525 | ); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Returns the source origin of an AMP document for documents served |
no test coverage detected