MCPcopy Index your code
hub / github.com/coderoad/coderoad-vscode / render

Function render

src/services/webview/render.ts:35–149  ·  view source on GitHub ↗

* render * configures the index.html into a webview panel * * React + webpack generate a number of files that are injected on build time * and must be accommodated for the webview using a vscode:// path. * * Modified paths include * - /static/js/x.chunk.js * - /static/js/main.chunk.js * - r

(panel: vscode.WebviewPanel, rootPath: string)

Source from the content-addressed store, hash-verified

33 * - manages the CSP policy for all the loaded files
34 */
35async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<void> {
36 // generate vscode-resource build path uri
37 const createUri = (_filePath: string): any => {
38 const filePath = (_filePath.startsWith('vscode') ? _filePath.substr(16) : _filePath).replace('///', '\\')
39
40 // @ts-ignore
41 return panel.webview.asWebviewUri(vscode.Uri.file(path.join(rootPath, filePath)))
42 }
43
44 try {
45 // load copied index.html from web app build
46 let html = await asyncReadFile(path.join(rootPath, 'index.html'), 'utf8')
47
48 // set base href at top of <head>
49 const baseHref = `${vscode.Uri.file(path.join(rootPath, 'build')).with({ scheme: 'vscode-resource' })}`
50 html = html.replace('<head>', `<head><base href="${baseHref}" />`)
51
52 // used for CSP
53 const nonces: string[] = []
54 const hashes: string[] = []
55
56 // fix paths for react static scripts to use vscode-resource paths
57 var jsBundleChunkRegex = /\/static\/js\/[\d].[^"]*\.js/g
58 var jsBundleChunk: RegExpExecArray | null = jsBundleChunkRegex.exec(html)
59 if (jsBundleChunk) {
60 const nonce: string = getNonce()
61 nonces.push(nonce)
62 const src = createUri(jsBundleChunk[0])
63 // replace script src, add nonce
64 html = html.replace(jsBundleChunk[0], `${src}" nonce="${nonce}`)
65 }
66
67 var mainBundleChunkRegex = /\/static\/js\/main.[^"]*\.js/g
68 var mainBundleChunk: RegExpExecArray | null = mainBundleChunkRegex.exec(html)
69 if (mainBundleChunk) {
70 const nonce: string = getNonce()
71 nonces.push(nonce)
72 const src = createUri(mainBundleChunk[0])
73 // replace script src, add nonce
74 html = html.replace(mainBundleChunk[0], `${src}" nonce="${nonce}`)
75 }
76
77 // support additional CSP exemptions when CodeRoad is embedded
78 if (CONTENT_SECURITY_POLICY_EXEMPTIONS && CONTENT_SECURITY_POLICY_EXEMPTIONS.length) {
79 for (const exemption of CONTENT_SECURITY_POLICY_EXEMPTIONS.split(' ')) {
80 // sha hashes should not be prefixed with 'nonce-'
81 if (exemption.match(/^sha/)) {
82 hashes.push(exemption)
83 } else {
84 nonces.push(exemption)
85 }
86 }
87 }
88
89 // note: file cannot be imported or results in esbuild error. Easier to read it.
90 let manifest
91 try {
92 const manifestPath = path.join(rootPath, 'asset-manifest.json')

Callers 1

createReactWebViewFunction · 0.85

Calls 5

onErrorFunction · 0.90
getNonceFunction · 0.85
createUriFunction · 0.85
wrapInQuotesFunction · 0.85
loggerFunction · 0.50

Tested by

no test coverage detected