(base: string, path = '/')
| 678 | } |
| 679 | |
| 680 | export async function fetchStyles(base: string, path = '/'): Promise<string> { |
| 681 | while (base.endsWith('/')) { |
| 682 | base = base.slice(0, -1) |
| 683 | } |
| 684 | |
| 685 | let index = await fetch(`${base}${path}`) |
| 686 | let html = await index.text() |
| 687 | |
| 688 | let linkRegex = /<link rel="stylesheet" href="([a-zA-Z0-9/_.?=%-]+)"/gi |
| 689 | let styleRegex = /<style\b[^>]*>([\s\S]*?)<\/style>/gi |
| 690 | |
| 691 | let stylesheets: string[] = [] |
| 692 | |
| 693 | let paths: string[] = [] |
| 694 | for (let match of html.matchAll(linkRegex)) { |
| 695 | let path: string = match[1] |
| 696 | if (path.startsWith('./')) { |
| 697 | path = path.slice(1) |
| 698 | } |
| 699 | paths.push(path) |
| 700 | } |
| 701 | stylesheets.push( |
| 702 | ...(await Promise.all( |
| 703 | paths.map(async (path) => { |
| 704 | let css = await fetch(`${base}${path}`, { |
| 705 | headers: { |
| 706 | Accept: 'text/css', |
| 707 | }, |
| 708 | }) |
| 709 | return await css.text() |
| 710 | }), |
| 711 | )), |
| 712 | ) |
| 713 | |
| 714 | for (let match of html.matchAll(styleRegex)) { |
| 715 | stylesheets.push(match[1]) |
| 716 | } |
| 717 | |
| 718 | return stylesheets.reduce((acc, css) => { |
| 719 | if (acc.length > 0) acc += '\n' |
| 720 | acc += css |
| 721 | return acc |
| 722 | }, '') |
| 723 | } |
| 724 | |
| 725 | export async function getRandomPort() { |
| 726 | return new Promise<number>((resolve, reject) => { |
no outgoing calls
no test coverage detected