microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c4a42a327d73c81a8ed065dfe1a0963f35bed44c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

107lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
b0061ac6Meena Kunnathur Balakrishnan10 years ago4import {FileSystem} from "../common/node/fileSystem";
190e393cMeena Kunnathur Balakrishnan10 years ago5import {Log} from "../common/log/log";
6import {LogLevel} from "../common/log/logHelper";
87323739dlebu10 years ago7import {Packager} from "../common/packager";
3fb37ad5unknown10 years ago8import path = require("path");
9import Q = require("q");
b0061ac6Meena Kunnathur Balakrishnan10 years ago10import {Request} from "../common/node/request";
11import {SourceMapUtil} from "./sourceMap";
3fb37ad5unknown10 years ago12import url = require("url");
13
4677921cdigeff10 years ago14interface DownloadedScript {
15contents: string;
16filepath: string;
17}
18
3fb37ad5unknown10 years ago19export class ScriptImporter {
2743f19cdlebu10 years ago20public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
21public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
e3ae4227digeff10 years ago22private packagerPort: number;
4677921cdigeff10 years ago23private sourcesStoragePath: string;
4bd0c669dlebu10 years ago24private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago25
e3ae4227digeff10 years ago26constructor(packagerPort: number, sourcesStoragePath: string) {
27this.packagerPort = packagerPort;
4677921cdigeff10 years ago28this.sourcesStoragePath = sourcesStoragePath;
4bd0c669dlebu10 years ago29this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago30}
31
2743f19cdlebu10 years ago32public downloadAppScript(scriptUrlString: string, debugAdapterPort: number): Q.Promise<DownloadedScript> {
5db8b6deMark Oswald10 years ago33const overriddenScriptUrlString = this.overridePackagerPort(scriptUrlString);
e00f7325unknown10 years ago34// We'll get the source code, and store it locally to have a better debugging experience
5db8b6deMark Oswald10 years ago35return new Request().request(overriddenScriptUrlString, true).then(scriptBody => {
e00f7325unknown10 years ago36// Extract sourceMappingURL from body
5db8b6deMark Oswald10 years ago37let scriptUrl = url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago38let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago39
4677921cdigeff10 years ago40let waitForSourceMapping = Q<void>(null);
e00f7325unknown10 years ago41if (sourceMappingUrl) {
42/* handle source map - request it and store it locally */
2743f19cdlebu10 years ago43waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago44.then(() => {
4bd0c669dlebu10 years ago45scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, sourceMappingUrl);
4677921cdigeff10 years ago46});
e00f7325unknown10 years ago47}
4677921cdigeff10 years ago48
49return waitForSourceMapping
2743f19cdlebu10 years ago50.then(() => this.writeAppScript(scriptBody, scriptUrl))
4677921cdigeff10 years ago51.then((scriptFilePath: string) => {
5db8b6deMark Oswald10 years ago52Log.logInternalMessage(LogLevel.Info, `Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago53return { contents: scriptBody, filepath: scriptFilePath };
87e1f776Jimmy Thomson10 years ago54}).finally(() => {
55// Request that the debug adapter update breakpoints and sourcemaps now that we have written them
2743f19cdlebu10 years ago56return new Request().request(`http://localhost:${debugAdapterPort}/refreshBreakpoints`);
876df2a0dlebu10 years ago57});
e00f7325unknown10 years ago58});
59}
60
2743f19cdlebu10 years ago61public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
299b0557Patricio Beltran10 years ago62return Packager.isPackagerRunning(Packager.getHostForPort(this.packagerPort))
63.then(running => {
64if (running) {
65let debuggerWorkerURL = `http://${Packager.getHostForPort(this.packagerPort)}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
66let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
67Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
68return new Request().request(debuggerWorkerURL, true).then((body: string) => {
69return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
70});
71}
72throw 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.`);
73});
2743f19cdlebu10 years ago74}
75
e00f7325unknown10 years ago76/**
77* Writes the script file to the project temporary location.
78*/
2743f19cdlebu10 years ago79private writeAppScript(scriptBody: string, scriptUrl: url.Url): Q.Promise<String> {
bb77358cMark Oswald10 years ago80let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle"
f218397cdlebu10 years ago81return new FileSystem().writeFile(scriptFilePath, scriptBody)
82.then(() => scriptFilePath);
3fb37ad5unknown10 years ago83}
84
e00f7325unknown10 years ago85/**
86* Writes the source map file to the project temporary location.
87*/
2743f19cdlebu10 years ago88private writeAppSourceMap(sourceMapUrl: url.Url, scriptUrl: url.Url): Q.Promise<void> {
e00f7325unknown10 years ago89return new Request().request(sourceMapUrl.href, true)
90.then((sourceMapBody: string) => {
bb77358cMark Oswald10 years ago91let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
92let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago93let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
94return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago95});
3fb37ad5unknown10 years ago96}
5e651f3edigeff10 years ago97
98/**
99* Changes the port of the url to be the one configured as this.packagerPort
100*/
101private overridePackagerPort(urlToOverride: string): string {
102let components = url.parse(urlToOverride);
103components.port = this.packagerPort.toString();
104delete components.host; // We delete the host, if not the port change will be ignored
105return url.format(components);
106}
3fb37ad5unknown10 years ago107}