microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
152lines · 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"); |
b0061ac6Meena Kunnathur Balakrishnan10 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"; |
e3706a1cRedMickey6 years ago | 11 | import {ProjectVersionHelper, RNPackageVersions} 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 | |
6eeec3c0Serge Svekolnikov8 years ago | 36 | constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 37 | this.packagerAddress = packagerAddress; | |
e3ae4227digeff10 years ago | 38 | this.packagerPort = packagerPort; |
4677921cdigeff10 years ago | 39 | this.sourcesStoragePath = sourcesStoragePath; |
6eeec3c0Serge Svekolnikov8 years ago | 40 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 41 | this.packagerLocalRoot = packagerLocalRoot; | |
4bd0c669dlebu10 years ago | 42 | this.sourceMapUtil = new SourceMapUtil(); |
3fb37ad5unknown10 years ago | 43 | } |
| 44 | | |
ce5e88eeYuri Skorokhodov5 years ago | 45 | public downloadAppScript(scriptUrlString: string, projectRootPath: string): Promise<DownloadedScript> { |
1c32fe84Patricio Beltran9 years ago | 46 | const parsedScriptUrl = url.parse(scriptUrlString); |
| 47 | const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString; | |
e00f7325unknown10 years ago | 48 | // We'll get the source code, and store it locally to have a better debugging experience |
833e37c7Vladimir Kotikov8 years ago | 49 | return Request.request(overriddenScriptUrlString, true).then(scriptBody => { |
e3706a1cRedMickey6 years ago | 50 | return ProjectVersionHelper.getReactNativeVersions(projectRootPath).then(rnVersions => { |
979d7bfemax-mironov8 years ago | 51 | // unfortunatelly Metro Bundler is broken in RN 0.54.x versions, so use this workaround unless it will be fixed |
| 52 | // https://github.com/facebook/metro/issues/147 | |
af0c5c30Yuri Skorokhodov5 years ago | 53 | // https://github.com/microsoft/vscode-react-native/issues/660 |
e3706a1cRedMickey6 years ago | 54 | if (ProjectVersionHelper.getRNVersionsWithBrokenMetroBundler().indexOf(rnVersions.reactNativeVersion) >= 0) { |
979d7bfemax-mironov8 years ago | 55 | let noSourceMappingUrlGenerated = scriptBody.match(/sourceMappingURL=/g) === null; |
| 56 | if (noSourceMappingUrlGenerated) { | |
| 57 | let sourceMapPathUrl = overriddenScriptUrlString.replace("bundle", "map"); | |
| 58 | scriptBody = this.sourceMapUtil.appendSourceMapPaths(scriptBody, sourceMapPathUrl); | |
| 59 | } | |
| 60 | } | |
e00f7325unknown10 years ago | 61 | |
979d7bfemax-mironov8 years ago | 62 | // Extract sourceMappingURL from body |
| 63 | let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" | |
| 64 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" | |
4677921cdigeff10 years ago | 65 | |
ce5e88eeYuri Skorokhodov5 years ago | 66 | let waitForSourceMapping: Promise<void> = Promise.resolve(); |
979d7bfemax-mironov8 years ago | 67 | if (sourceMappingUrl) { |
| 68 | /* handle source map - request it and store it locally */ | |
| 69 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) | |
| 70 | .then(() => { | |
| 71 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl); | |
7fa90b3bRedMickey6 years ago | 72 | if (semver.gte(rnVersions.reactNativeVersion, "0.61.0")) { |
2f5c780bYuri Skorokhodov6 years ago | 73 | scriptBody = this.sourceMapUtil.removeSourceURL(scriptBody); |
| 74 | } | |
979d7bfemax-mironov8 years ago | 75 | }); |
| 76 | } | |
| 77 | | |
| 78 | return waitForSourceMapping | |
2743f19cdlebu10 years ago | 79 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
4677921cdigeff10 years ago | 80 | .then((scriptFilePath: string) => { |
0a68f8dbArtem Egorov8 years ago | 81 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); |
4677921cdigeff10 years ago | 82 | return { contents: scriptBody, filepath: scriptFilePath }; |
876df2a0dlebu10 years ago | 83 | }); |
979d7bfemax-mironov8 years ago | 84 | }); |
e00f7325unknown10 years ago | 85 | }); |
| 86 | } | |
| 87 | | |
ce5e88eeYuri Skorokhodov5 years ago | 88 | public downloadDebuggerWorker(sourcesStoragePath: string, projectRootPath: string, debuggerWorkerUrlPath?: string): Promise<void> { |
d124bf0eYuri Skorokhodov7 years ago | 89 | const errPackagerNotRunning = ErrorHelper.getInternalError(InternalErrorCode.CannotAttachToPackagerCheckPackagerRunningOnPort, this.packagerPort); |
6eeec3c0Serge Svekolnikov8 years ago | 90 | return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning) |
0a68f8dbArtem Egorov8 years ago | 91 | .then(() => { |
e3706a1cRedMickey6 years ago | 92 | return ProjectVersionHelper.getReactNativeVersions(projectRootPath); |
a1324704Artem Egorov8 years ago | 93 | }) |
7fa90b3bRedMickey6 years ago | 94 | .then((rnVersions: RNPackageVersions) => { |
| 95 | let debuggerWorkerURL = this.prepareDebuggerWorkerURL(rnVersions.reactNativeVersion, debuggerWorkerUrlPath); | |
0a68f8dbArtem Egorov8 years ago | 96 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 97 | logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); | |
| 98 | | |
| 99 | return Request.request(debuggerWorkerURL, true) | |
| 100 | .then((body: string) => { | |
299b0557Patricio Beltran10 years ago | 101 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 102 | }); | |
| 103 | }); | |
2743f19cdlebu10 years ago | 104 | } |
| 105 | | |
cf911877Yuri Skorokhodov7 years ago | 106 | public prepareDebuggerWorkerURL(rnVersion: string, debuggerWorkerUrlPath?: string): string { |
| 107 | let debuggerWorkerURL: string; | |
| 108 | // It can be empty string | |
| 109 | if (debuggerWorkerUrlPath !== undefined) { | |
| 110 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${debuggerWorkerUrlPath}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 111 | } else { | |
| 112 | let newPackager = ""; | |
| 113 | if (!semver.valid(rnVersion) /*Custom RN implementations should support new packager*/ || (semver.gte(rnVersion, "0.50.0"))) { | |
| 114 | newPackager = "debugger-ui/"; | |
| 115 | } | |
| 116 | debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${newPackager}${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; | |
| 117 | } | |
| 118 | return debuggerWorkerURL; | |
| 119 | } | |
| 120 | | |
e00f7325unknown10 years ago | 121 | /** |
| 122 | * Writes the script file to the project temporary location. | |
| 123 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 124 | private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Promise<string> { |
bb77358cMark Oswald10 years ago | 125 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
f218397cdlebu10 years ago | 126 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 127 | .then(() => scriptFilePath); | |
3fb37ad5unknown10 years ago | 128 | } |
| 129 | | |
e00f7325unknown10 years ago | 130 | /** |
| 131 | * Writes the source map file to the project temporary location. | |
| 132 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 133 | private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Promise<void> { |
833e37c7Vladimir Kotikov8 years ago | 134 | return Request.request(sourceMapUrl.href, true) |
e00f7325unknown10 years ago | 135 | .then((sourceMapBody: string) => { |
bb77358cMark Oswald10 years ago | 136 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 137 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" | |
6eeec3c0Serge Svekolnikov8 years ago | 138 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
f218397cdlebu10 years ago | 139 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
e00f7325unknown10 years ago | 140 | }); |
3fb37ad5unknown10 years ago | 141 | } |
5e651f3edigeff10 years ago | 142 | |
| 143 | /** | |
| 144 | * Changes the port of the url to be the one configured as this.packagerPort | |
| 145 | */ | |
| 146 | private overridePackagerPort(urlToOverride: string): string { | |
| 147 | let components = url.parse(urlToOverride); | |
| 148 | components.port = this.packagerPort.toString(); | |
| 149 | delete components.host; // We delete the host, if not the port change will be ignored | |
| 150 | return url.format(components); | |
| 151 | } | |
3fb37ad5unknown10 years ago | 152 | } |