import { IProjectData } from "../../definitions/project"; /** * Identifies a debug manifest whose cleartext permission the CLI wrote, so a * later prepare that doesn't need it can take it out again. */ export const DEV_CLEARTEXT_MARKER = "nativescript-cli: cleartext HTTP to the Vite dev server (debug HMR runs only)"; const TOOLS_NS = 'xmlns:tools="http://schemas.android.com/tools"'; const CLEARTEXT_ATTRIBUTE = "android:usesCleartextTraffic"; /** * A Vite HMR app loads its code from the dev server over plain HTTP, which * Android blocks by default from API 28. */ export function needsDevCleartext( projectData: Pick, prepareData: { hmr?: boolean; release?: boolean }, ): boolean { return ( projectData.bundler === "vite" && !!prepareData.hmr && !prepareData.release ); } export function hasDevCleartext(manifest: string | null): boolean { return !!manifest && manifest.includes(DEV_CLEARTEXT_MARKER); } /** The debug source set's manifest when the app ships none of its own. */ export function devCleartextManifest(): string { return [ '', ``, ``, `\t`, "", "", ].join("\n"); } interface Tag { index: number; text: string; } /** The first match of `pattern` outside an XML comment. */ function findTag(source: string, pattern: RegExp): Tag | null { const comments: Array<[number, number]> = []; const comment = //g; for (let m = comment.exec(source); m; m = comment.exec(source)) { comments.push([m.index, m.index + m[0].length]); } const search = new RegExp(pattern.source, "g"); for (let m = search.exec(source); m; m = search.exec(source)) { const at = m.index; if (!comments.some(([from, to]) => at >= from && at < to)) { return { index: at, text: m[0] }; } } return null; } function replaceTag(source: string, tag: Tag, replacement: string): string { return ( source.slice(0, tag.index) + replacement + source.slice(tag.index + tag.text.length) ); } /** * The app's own debug manifest with the permission added. `tools:replace` * lets it win over a main manifest that sets the attribute itself. */ export function withDevCleartext(manifest: string): string { let result = manifest; const manifestTag = findTag(result, /]*>/); if (!manifestTag) { return result; } if (!manifestTag.text.includes("xmlns:tools=")) { result = replaceTag( result, manifestTag, manifestTag.text.replace(/^]*?\/?>/); if (!applicationTag) { const application = `\t`; const root = findTag(result, /]*>/); const closing = findTag(result, /<\/manifest>/); if (root.text.endsWith("/>")) { result = replaceTag( result, root, `${root.text.slice(0, -2).trimEnd()}>\n${application}\n`, ); } else if (closing) { result = replaceTag(result, closing, `${application}\n`); } else { // Without the marker, the next prepare tries again. return manifest; } } else { let tag = applicationTag.text; const value = /android:usesCleartextTraffic\s*=\s*(?:"[^"]*"|'[^']*')/; tag = value.test(tag) ? tag.replace(value, `${CLEARTEXT_ATTRIBUTE}="true"`) : tag.replace( /^ name.trim()) .includes(CLEARTEXT_ATTRIBUTE) ) { const [, quote, names] = replace; tag = tag.replace( replace[0], `tools:replace=${quote}${names.trimEnd()},${CLEARTEXT_ATTRIBUTE}${quote}`, ); } result = replaceTag(result, applicationTag, tag); } const root = findTag(result, /\n