microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0a68f8db56aae8352c5b9ca8062abd78cfdaa44a

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

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