microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/scriptImporter.ts
116lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {FileSystem} from "../common/node/fileSystem"; |
| 5 | import { logger } from "vscode-chrome-debug-core"; |
| 6 | import { ensurePackagerRunning } from "../common/packagerStatus"; |
| 7 | import path = require("path"); |
| 8 | import Q = require("q"); |
| 9 | import {Request} from "../common/node/request"; |
| 10 | import {SourceMapUtil} from "./sourceMap"; |
| 11 | import url = require("url"); |
| 12 | |
| 13 | export interface DownloadedScript { |
| 14 | contents: string; |
| 15 | filepath: string; |
| 16 | } |
| 17 | |
| 18 | interface IStrictUrl extends url.Url { |
| 19 | pathname: string; |
| 20 | href: string; |
| 21 | } |
| 22 | |
| 23 | export class ScriptImporter { |
| 24 | public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker"; |
| 25 | public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js"; |
| 26 | private packagerAddress: string; |
| 27 | private packagerPort: number; |
| 28 | private sourcesStoragePath: string; |
| 29 | private packagerRemoteRoot?: string; |
| 30 | private packagerLocalRoot?: string; |
| 31 | private sourceMapUtil: SourceMapUtil; |
| 32 | |
| 33 | constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) { |
| 34 | this.packagerAddress = packagerAddress; |
| 35 | this.packagerPort = packagerPort; |
| 36 | this.sourcesStoragePath = sourcesStoragePath; |
| 37 | this.packagerRemoteRoot = packagerRemoteRoot; |
| 38 | this.packagerLocalRoot = packagerLocalRoot; |
| 39 | this.sourceMapUtil = new SourceMapUtil(); |
| 40 | } |
| 41 | |
| 42 | public downloadAppScript(scriptUrlString: string): Q.Promise<DownloadedScript> { |
| 43 | const parsedScriptUrl = url.parse(scriptUrlString); |
| 44 | const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString; |
| 45 | // We'll get the source code, and store it locally to have a better debugging experience |
| 46 | return Request.request(overriddenScriptUrlString, true).then(scriptBody => { |
| 47 | // Extract sourceMappingURL from body |
| 48 | let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true" |
| 49 | let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true" |
| 50 | |
| 51 | let waitForSourceMapping = Q<void>(void 0); |
| 52 | if (sourceMappingUrl) { |
| 53 | /* handle source map - request it and store it locally */ |
| 54 | waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl) |
| 55 | .then(() => { |
| 56 | scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | return waitForSourceMapping |
| 61 | .then(() => this.writeAppScript(scriptBody, scriptUrl)) |
| 62 | .then((scriptFilePath: string) => { |
| 63 | logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`); |
| 64 | return { contents: scriptBody, filepath: scriptFilePath }; |
| 65 | }); |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> { |
| 70 | const errPackagerNotRunning = new RangeError(`Cannot attach to packager. Are you sure there is a packager and it is running in the port ${this.packagerPort}? If your packager is configured to run in another port make sure to add that to the setting.json.`); |
| 71 | |
| 72 | return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning) |
| 73 | .then(() => { |
| 74 | let debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`; |
| 75 | let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME); |
| 76 | logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath); |
| 77 | |
| 78 | return Request.request(debuggerWorkerURL, true) |
| 79 | .then((body: string) => { |
| 80 | return new FileSystem().writeFile(debuggerWorkerLocalPath, body); |
| 81 | }); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Writes the script file to the project temporary location. |
| 87 | */ |
| 88 | private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<String> { |
| 89 | let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle" |
| 90 | return new FileSystem().writeFile(scriptFilePath, scriptBody) |
| 91 | .then(() => scriptFilePath); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Writes the source map file to the project temporary location. |
| 96 | */ |
| 97 | private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> { |
| 98 | return Request.request(sourceMapUrl.href, true) |
| 99 | .then((sourceMapBody: string) => { |
| 100 | let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map" |
| 101 | let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle" |
| 102 | let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot); |
| 103 | return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent); |
| 104 | }); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Changes the port of the url to be the one configured as this.packagerPort |
| 109 | */ |
| 110 | private overridePackagerPort(urlToOverride: string): string { |
| 111 | let components = url.parse(urlToOverride); |
| 112 | components.port = this.packagerPort.toString(); |
| 113 | delete components.host; // We delete the host, if not the port change will be ignored |
| 114 | return url.format(components); |
| 115 | } |
| 116 | } |
| 117 | |