microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/scriptImporter.ts

116lines · 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";
0a68f8dbArtem Egorov8 years ago5import { logger } from "vscode-chrome-debug-core";
6import { ensurePackagerRunning } from "../common/packagerStatus";
3fb37ad5unknown10 years ago7import path = require("path");
8import Q = require("q");
b0061ac6Meena Kunnathur Balakrishnan10 years ago9import {Request} from "../common/node/request";
10import {SourceMapUtil} from "./sourceMap";
3fb37ad5unknown10 years ago11import url = require("url");
12
5c8365a6Artem Egorov8 years ago13export interface DownloadedScript {
4677921cdigeff10 years ago14contents: string;
15filepath: string;
16}
17
5c8365a6Artem Egorov8 years ago18interface IStrictUrl extends url.Url {
19pathname: string;
20href: string;
21}
22
3fb37ad5unknown10 years ago23export class ScriptImporter {
2743f19cdlebu10 years ago24public static DEBUGGER_WORKER_FILE_BASENAME = "debuggerWorker";
25public static DEBUGGER_WORKER_FILENAME = ScriptImporter.DEBUGGER_WORKER_FILE_BASENAME + ".js";
6eeec3c0Serge Svekolnikov8 years ago26private packagerAddress: string;
e3ae4227digeff10 years ago27private packagerPort: number;
4677921cdigeff10 years ago28private sourcesStoragePath: string;
6eeec3c0Serge Svekolnikov8 years ago29private packagerRemoteRoot?: string;
30private packagerLocalRoot?: string;
4bd0c669dlebu10 years ago31private sourceMapUtil: SourceMapUtil;
3fb37ad5unknown10 years ago32
6eeec3c0Serge Svekolnikov8 years ago33constructor(packagerAddress: string, packagerPort: number, sourcesStoragePath: string, packagerRemoteRoot?: string, packagerLocalRoot?: string) {
34this.packagerAddress = packagerAddress;
e3ae4227digeff10 years ago35this.packagerPort = packagerPort;
4677921cdigeff10 years ago36this.sourcesStoragePath = sourcesStoragePath;
6eeec3c0Serge Svekolnikov8 years ago37this.packagerRemoteRoot = packagerRemoteRoot;
38this.packagerLocalRoot = packagerLocalRoot;
4bd0c669dlebu10 years ago39this.sourceMapUtil = new SourceMapUtil();
3fb37ad5unknown10 years ago40}
41
e45838cbVladimir Kotikov9 years ago42public downloadAppScript(scriptUrlString: string): Q.Promise<DownloadedScript> {
1c32fe84Patricio Beltran9 years ago43const parsedScriptUrl = url.parse(scriptUrlString);
44const overriddenScriptUrlString = (parsedScriptUrl.hostname === "localhost") ? this.overridePackagerPort(scriptUrlString) : scriptUrlString;
e00f7325unknown10 years ago45// We'll get the source code, and store it locally to have a better debugging experience
833e37c7Vladimir Kotikov8 years ago46return Request.request(overriddenScriptUrlString, true).then(scriptBody => {
e00f7325unknown10 years ago47// Extract sourceMappingURL from body
5c8365a6Artem Egorov8 years ago48let scriptUrl = <IStrictUrl>url.parse(overriddenScriptUrlString); // scriptUrl = "http://localhost:8081/index.ios.bundle?platform=ios&dev=true"
4bd0c669dlebu10 years ago49let sourceMappingUrl = this.sourceMapUtil.getSourceMapURL(scriptUrl, scriptBody); // sourceMappingUrl = "http://localhost:8081/index.ios.map?platform=ios&dev=true"
e00f7325unknown10 years ago50
5c8365a6Artem Egorov8 years ago51let waitForSourceMapping = Q<void>(void 0);
e00f7325unknown10 years ago52if (sourceMappingUrl) {
53/* handle source map - request it and store it locally */
2743f19cdlebu10 years ago54waitForSourceMapping = this.writeAppSourceMap(sourceMappingUrl, scriptUrl)
3af9a124unknown10 years ago55.then(() => {
5c8365a6Artem Egorov8 years ago56scriptBody = this.sourceMapUtil.updateScriptPaths(scriptBody, <IStrictUrl>sourceMappingUrl);
4677921cdigeff10 years ago57});
e00f7325unknown10 years ago58}
4677921cdigeff10 years ago59
60return waitForSourceMapping
2743f19cdlebu10 years ago61.then(() => this.writeAppScript(scriptBody, scriptUrl))
4677921cdigeff10 years ago62.then((scriptFilePath: string) => {
0a68f8dbArtem Egorov8 years ago63logger.verbose(`Script ${overriddenScriptUrlString} downloaded to ${scriptFilePath}`);
4677921cdigeff10 years ago64return { contents: scriptBody, filepath: scriptFilePath };
876df2a0dlebu10 years ago65});
e00f7325unknown10 years ago66});
67}
68
2743f19cdlebu10 years ago69public downloadDebuggerWorker(sourcesStoragePath: string): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago70const 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
6eeec3c0Serge Svekolnikov8 years ago72return ensurePackagerRunning(this.packagerAddress, this.packagerPort, errPackagerNotRunning)
0a68f8dbArtem Egorov8 years ago73.then(() => {
6eeec3c0Serge Svekolnikov8 years ago74let debuggerWorkerURL = `http://${this.packagerAddress}:${this.packagerPort}/${ScriptImporter.DEBUGGER_WORKER_FILENAME}`;
0a68f8dbArtem Egorov8 years ago75let debuggerWorkerLocalPath = path.join(sourcesStoragePath, ScriptImporter.DEBUGGER_WORKER_FILENAME);
76logger.verbose("About to download: " + debuggerWorkerURL + " to: " + debuggerWorkerLocalPath);
77
78return Request.request(debuggerWorkerURL, true)
79.then((body: string) => {
299b0557Patricio Beltran10 years ago80return new FileSystem().writeFile(debuggerWorkerLocalPath, body);
81});
82});
2743f19cdlebu10 years ago83}
84
e00f7325unknown10 years ago85/**
86* Writes the script file to the project temporary location.
87*/
5c8365a6Artem Egorov8 years ago88private writeAppScript(scriptBody: string, scriptUrl: IStrictUrl): Q.Promise<String> {
bb77358cMark Oswald10 years ago89let scriptFilePath = path.join(this.sourcesStoragePath, path.basename(scriptUrl.pathname)); // scriptFilePath = "$TMPDIR/index.ios.bundle"
f218397cdlebu10 years ago90return new FileSystem().writeFile(scriptFilePath, scriptBody)
91.then(() => scriptFilePath);
3fb37ad5unknown10 years ago92}
93
e00f7325unknown10 years ago94/**
95* Writes the source map file to the project temporary location.
96*/
5c8365a6Artem Egorov8 years ago97private writeAppSourceMap(sourceMapUrl: IStrictUrl, scriptUrl: IStrictUrl): Q.Promise<void> {
833e37c7Vladimir Kotikov8 years ago98return Request.request(sourceMapUrl.href, true)
e00f7325unknown10 years ago99.then((sourceMapBody: string) => {
bb77358cMark Oswald10 years ago100let sourceMappingLocalPath = path.join(this.sourcesStoragePath, path.basename(sourceMapUrl.pathname)); // sourceMappingLocalPath = "$TMPDIR/index.ios.map"
101let scriptFileRelativePath = path.basename(scriptUrl.pathname); // scriptFileRelativePath = "index.ios.bundle"
6eeec3c0Serge Svekolnikov8 years ago102let updatedContent = this.sourceMapUtil.updateSourceMapFile(sourceMapBody, scriptFileRelativePath, this.sourcesStoragePath, this.packagerRemoteRoot, this.packagerLocalRoot);
f218397cdlebu10 years ago103return new FileSystem().writeFile(sourceMappingLocalPath, updatedContent);
e00f7325unknown10 years ago104});
3fb37ad5unknown10 years ago105}
5e651f3edigeff10 years ago106
107/**
108* Changes the port of the url to be the one configured as this.packagerPort
109*/
110private overridePackagerPort(urlToOverride: string): string {
111let components = url.parse(urlToOverride);
112components.port = this.packagerPort.toString();
113delete components.host; // We delete the host, if not the port change will be ignored
114return url.format(components);
115}
3fb37ad5unknown10 years ago116}