microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
df4bce4041caa61af1460ef87f2380820508a455

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

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