microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1ca47c7c2d73a604d8f110cdbad1aad4bbdddce6

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

110lines · 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
5c8365a6Artem Egorov8 years ago14export interface DownloadedScript {
4677921cdigeff10 years ago15contents: string;
16filepath: string;
17}
18
5c8365a6Artem Egorov8 years ago19interface IStrictUrl extends url.Url {
20pathname: string;
21href: string;
22}
23
3fb37ad5unknown10 years ago24export class ScriptImporter {
2743f19cdlebu10 years ago25public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
26public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
e3ae4227digeff10 years ago27private packagerPort: number;
4677921cdigeff10 years ago28private sourcesStoragePath: string;
4bd0c669dlebu10 years ago29private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago30
e3ae4227digeff10 years ago31constructor(packagerPort: number, sourcesStoragePath: string) {
32this.packagerPort = packagerPort;
4677921cdigeff10 years ago33this.sourcesStoragePath = sourcesStoragePath;
4bd0c669dlebu10 years ago34this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago35}
36
e45838cbVladimir Kotikov9 years ago37public downloadAppScript(scriptUrlString: string): Q.Promise<DownloadedScript> {
1c32fe84Patricio Beltran9 years ago38const parsedScriptUrl = url.parse(scriptUrlString);
39const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString;
e00f7325unknown10 years ago40// We'll get the source code, and store it locally to have a better debugging experience
833e37c7Vladimir Kotikov8 years ago41return Request.request(overriddenScriptUrlString, true).then(scriptBody => {
e00f7325unknown10 years ago42// Extract sourceMappingURL from body
5c8365a6Artem Egorov8 years ago43let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago44let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago45
5c8365a6Artem Egorov8 years ago46let waitForSourceMapping = Q<void>(void 0);
e00f7325unknown10 years ago47if (sourceMappingUrl) {
48/* handle source map - request it and store it locally */
2743f19cdlebu10 years ago49waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago50.then(() => {
5c8365a6Artem Egorov8 years ago51scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl);
4677921cdigeff10 years ago52});
e00f7325unknown10 years ago53}
4677921cdigeff10 years ago54
55return waitForSourceMapping
2743f19cdlebu10 years ago56.then(() => this.writeAppScript(scriptBody, scriptUrl))
4677921cdigeff10 years ago57.then((scriptFilePath: string) => {
5db8b6deMark Oswald10 years ago58Log.logInternalMessage(LogLevel.Info, `Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago59return { contents: scriptBody, filepath: scriptFilePath };
876df2a0dlebu10 years ago60});
e00f7325unknown10 years ago61});
62}
63
2743f19cdlebu10 years ago64public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
299b0557Patricio Beltran10 years ago65return Packager.isPackagerRunning(Packager.getHostForPort(this.packagerPort))
66.then(running => {
67if (running) {
68let debuggerWorkerURL = `http://${Packager.getHostForPort(this.packagerPort)}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
69let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
70Log.logInternalMessage(LogLevel.Info, "About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
833e37c7Vladimir Kotikov8 years ago71return Request.request(debuggerWorkerURL, true).then((body: string) => {
299b0557Patricio Beltran10 years ago72return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
73});
74}
75throw 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.`);
76});
2743f19cdlebu10 years ago77}
78
e00f7325unknown10 years ago79/**
80* Writes the script file to the project temporary location.
81*/
5c8365a6Artem Egorov8 years ago82private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<String> {
bb77358cMark Oswald10 years ago83let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle"
f218397cdlebu10 years ago84return new FileSystem().writeFile(scriptFilePath, scriptBody)
85.then(() => scriptFilePath);
3fb37ad5unknown10 years ago86}
87
e00f7325unknown10 years ago88/**
89* Writes the source map file to the project temporary location.
90*/
5c8365a6Artem Egorov8 years ago91private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> {
833e37c7Vladimir Kotikov8 years ago92return Request.request(sourceMapUrl.href, true)
e00f7325unknown10 years ago93.then((sourceMapBody: string) => {
bb77358cMark Oswald10 years ago94let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
95let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
f218397cdlebu10 years ago96let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath);
97return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago98});
3fb37ad5unknown10 years ago99}
5e651f3edigeff10 years ago100
101/**
102* Changes the port of the url to be the one configured as this.packagerPort
103*/
104private overridePackagerPort(urlToOverride: string): string {
105let components = url.parse(urlToOverride);
106components.port = this.packagerPort.toString();
107delete components.host; // We delete the host, if not the port change will be ignored
108return url.format(components);
109}
3fb37ad5unknown10 years ago110}