microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
194lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
a6562589RedMickey6 years ago | 4 | import { logger } from "vscode-debugadapter"; |
0a68f8dbArtem Egorov8 years ago | 5 | import { ensurePackagerRunning } from "../common/packagerStatus"; |
3fb37ad5unknown10 years ago | 6 | import path = require("path"); |
34472878RedMickey5 years ago | 7 | import { Request } from "../common/node/request"; |
| 8 | import { SourceMapUtil } from "./sourceMap"; | |
3fb37ad5unknown10 years ago | 9 | import url = require("url"); |
a1324704Artem Egorov8 years ago | 10 | import * as semver from "semver"; |
0d77292aJiglioNero4 years ago | 11 | import { ProjectVersionHelper } from "../common/projectVersionHelper"; |
d124bf0eYuri Skorokhodov7 years ago | 12 | import { ErrorHelper } from "../common/error/errorHelper"; |
| 13 | import { InternalErrorCode } from "../common/error/internalErrorCode"; | |
ce5e88eeYuri Skorokhodov5 years ago | 14 | import { FileSystem } from "../common/node/fileSystem"; |
3fb37ad5unknown10 years ago | 15 | |
5c8365a6Artem Egorov8 years ago | 16 | export interface DownloadedScript { |
4677921cdigeff10 years ago | 17 | contents: string; |
| 18 | filepath: string; | |
| 19 | } | |
| 20 | | |
5c8365a6Artem Egorov8 years ago | 21 | interface IStrictUrl extends url.Url { |
| 22 | pathname: string; | |
| 23 | href: string; | |
| 24 | } | |
| 25 | | |
3fb37ad5unknown10 years ago | 26 | export class ScriptImporter { |
2743f19cdlebu10 years ago | 27 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 28 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; | |
6eeec3c0Serge Svekolnikov8 years ago | 29 | private packagerAddress: string; |
e3ae4227digeff10 years ago | 30 | private packagerPort: number; |
4677921cdigeff10 years ago | 31 | private sourcesStoragePath: string; |
6eeec3c0Serge Svekolnikov8 years ago | 32 | private packagerRemoteRoot?: string; |
| 33 | private packagerLocalRoot?: string; | |
4bd0c669dlebu10 years ago | 34 | private sourceMapUtil: SourceMapUtil; |
3fb37ad5unknown10 years ago | 35 | |
34472878RedMickey5 years ago | 36 | constructor( |
| 37 | packagerAddress: string, | |
| 38 | packagerPort: number, | |
| 39 | sourcesStoragePath: string, | |
| 40 | packagerRemoteRoot?: string, | |
| 41 | packagerLocalRoot?: string, | |
| 42 | ) { | |
6eeec3c0Serge Svekolnikov8 years ago | 43 | this.packagerAddress = packagerAddress; |
e3ae4227digeff10 years ago | 44 | this.packagerPort = packagerPort; |
4677921cdigeff10 years ago | 45 | this.sourcesStoragePath = sourcesStoragePath; |
6eeec3c0Serge Svekolnikov8 years ago | 46 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 47 | this.packagerLocalRoot = packagerLocalRoot; | |
4bd0c669dlebu10 years ago | 48 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 49 | } |
| 50 | | |
0d77292aJiglioNero4 years ago | 51 | public async downloadAppScript( |
34472878RedMickey5 years ago | 52 | scriptUrlString: string, |
| 53 | projectRootPath: string, | |
| 54 | ): Promise<DownloadedScript> { | |
1c32fe84Patricio Beltran9 years ago | 55 | const parsedScriptUrl = url.parse(scriptUrlString); |
34472878RedMickey5 years ago | 56 | const overriddenScriptUrlString = |
| 57 | parsedScriptUrl.hostname === "localhost" | |
| 58 | ? this.overridePackagerPort(scriptUrlString) | |
| 59 | : scriptUrlString; | |
0d77292aJiglioNero4 years ago | 60 | |
e00f7325unknown10 years ago | 61 | // We'll get the source code, and store it locally to have a better debugging experience |
0d77292aJiglioNero4 years ago | 62 | let scriptBody = await Request.request(overriddenScriptUrlString, true); |
e00f7325unknown10 years ago | 63 | |
0d77292aJiglioNero4 years ago | 64 | const rnVersions = await ProjectVersionHelper.getReactNativeVersions(projectRootPath); |
| 65 | // unfortunatelly Metro Bundler is broken in RN 0.54.x versions, so use this workaround unless it will be fixed | |
| 66 | // https://github.com/facebook/metro/issues/147 | |
| 67 | // https://github.com/microsoft/vscode-react-native/issues/660 | |
| 68 | if ( | |
| 69 | ProjectVersionHelper.getRNVersionsWithBrokenMetroBundler().indexOf( | |
| 70 | rnVersions.reactNativeVersion, | |
| 71 | ) >= 0 | |
| 72 | ) { | |
| 73 | let noSourceMappingUrlGenerated = scriptBody.match(/sourceMappingURL=/g) === null; | |
| 74 | if (noSourceMappingUrlGenerated) { | |
| 75 | let sourceMapPathUrl = overriddenScriptUrlString.replace("bundle", "map"); | |
| 76 | scriptBody = this.sourceMapUtil.appendSourceMapPaths(scriptBody, sourceMapPathUrl); | |
| 77 | } | |
| 78 | } | |
| 79 | | |
| 80 | // Extract sourceMappingURL from body | |
| 81 | let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
| 82 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" | |
979d7bfemax-mironov8 years ago | 83 | |
0d77292aJiglioNero4 years ago | 84 | let waitForSourceMapping: Promise<void> = Promise.resolve(); |
| 85 | if (sourceMappingUrl) { | |
| 86 | /* handle source map - request it and store it locally */ | |
| 87 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl).then(() => { | |
| 88 | scriptBody = this.sourceMapUtil.updateScriptPaths( | |
| 89 | scriptBody, | |
| 90 | <IStrictUrl>sourceMappingUrl, | |
| 91 | ); | |
| 92 | if (semver.gte(rnVersions.reactNativeVersion, "0.61.0")) { | |
| 93 | scriptBody = this.sourceMapUtil.removeSourceURL(scriptBody); | |
| 94 | } | |
979d7bfemax-mironov8 years ago | 95 | }); |
0d77292aJiglioNero4 years ago | 96 | } |
| 97 | await waitForSourceMapping; | |
| 98 | const scriptFilePath = await this.writeAppScript(scriptBody, scriptUrl); | |
| 99 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); | |
| 100 | return { contents: scriptBody, filepath: scriptFilePath }; | |
e00f7325unknown10 years ago | 101 | } |
| 102 | | |
0d77292aJiglioNero4 years ago | 103 | public async downloadDebuggerWorker( |
34472878RedMickey5 years ago | 104 | sourcesStoragePath: string, |
| 105 | projectRootPath: string, | |
| 106 | debuggerWorkerUrlPath?: string, | |
| 107 | ): Promise<void> { | |
| 108 | const errPackagerNotRunning = ErrorHelper.getInternalError( | |
| 109 | InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort, | |
| 110 | this.packagerPort, | |
| 111 | ); | |
0a68f8dbArtem Egorov8 years ago | 112 | |
0d77292aJiglioNero4 years ago | 113 | await ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning); |
| 114 | | |
| 115 | const rnVersions = await ProjectVersionHelper.getReactNativeVersions(projectRootPath); | |
| 116 | let debuggerWorkerURL = this.prepareDebuggerWorkerURL( | |
| 117 | rnVersions.reactNativeVersion, | |
| 118 | debuggerWorkerUrlPath, | |
| 119 | ); | |
| 120 | let debuggerWorkerLocalPath = path.join( | |
| 121 | sourcesStoragePath, | |
| 122 | ScriptImporter.DEBUGGER_WORKER_FILENAME, | |
| 123 | ); | |
| 124 | logger.verbose( | |
| 125 | "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath, | |
| 126 | ); | |
| 127 | | |
| 128 | const body = await Request.request(debuggerWorkerURL, true); | |
| 129 | | |
| 130 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); | |
2743f19cdlebu10 years ago | 131 | } |
| 132 | | |
cf911877Yuri Skorokhodov7 years ago | 133 | public prepareDebuggerWorkerURL(rnVersion: string, debuggerWorkerUrlPath?: string): string { |
| 134 | let debuggerWorkerURL: string; | |
| 135 | // It can be empty string | |
| 136 | if (debuggerWorkerUrlPath !== undefined) { | |
| 137 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${debuggerWorkerUrlPath}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 138 | } else { | |
| 139 | let newPackager = ""; | |
34472878RedMickey5 years ago | 140 | if ( |
| 141 | !semver.valid( | |
| 142 | rnVersion, | |
| 143 | ) /*Custom RN implementations should support new packager*/ || | |
| 144 | semver.gte(rnVersion, "0.50.0") | |
| 145 | ) { | |
cf911877Yuri Skorokhodov7 years ago | 146 | newPackager = "debugger-ui/"; |
| 147 | } | |
| 148 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${newPackager}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 149 | } | |
| 150 | return debuggerWorkerURL; | |
| 151 | } | |
| 152 | | |
e00f7325unknown10 years ago | 153 | /** |
| 154 | * Writes the script file to the project temporary location. | |
| 155 | */ | |
0d77292aJiglioNero4 years ago | 156 | private async writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Promise<string> { |
bb77358cMark Oswald10 years ago | 157 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
0d77292aJiglioNero4 years ago | 158 | await new FileSystem().writeFile(scriptFilePath, scriptBody); |
| 159 | return scriptFilePath; | |
3fb37ad5unknown10 years ago | 160 | } |
| 161 | | |
e00f7325unknown10 years ago | 162 | /** |
| 163 | * Writes the source map file to the project temporary location. | |
| 164 | */ | |
0d77292aJiglioNero4 years ago | 165 | private async writeAppSourceMap( |
| 166 | sourceMapUrl: IStrictUrl, | |
| 167 | scriptUrl: IStrictUrl, | |
| 168 | ): Promise<void> { | |
| 169 | const sourceMapBody = await Request.request(sourceMapUrl.href, true); | |
| 170 | let sourceMappingLocalPath = path.join( | |
| 171 | this.sourcesStoragePath, | |
| 172 | path.basename(sourceMapUrl.pathname), | |
| 173 | ); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" | |
| 174 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" | |
| 175 | let updatedContent = this.sourceMapUtil.updateSourceMapFile( | |
| 176 | sourceMapBody, | |
| 177 | scriptFileRelativePath, | |
| 178 | this.sourcesStoragePath, | |
| 179 | this.packagerRemoteRoot, | |
| 180 | this.packagerLocalRoot, | |
| 181 | ); | |
| 182 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); | |
3fb37ad5unknown10 years ago | 183 | } |
5e651f3edigeff10 years ago | 184 | |
| 185 | /** | |
| 186 | * Changes the port of the url to be the one configured as this.packagerPort | |
| 187 | */ | |
| 188 | private overridePackagerPort(urlToOverride: string): string { | |
| 189 | let components = url.parse(urlToOverride); | |
| 190 | components.port = this.packagerPort.toString(); | |
| 191 | delete components.host; // We delete the host, if not the port change will be ignored | |
| 192 | return url.format(components); | |
| 193 | } | |
3fb37ad5unknown10 years ago | 194 | } |