This repository has been archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iwdp debugger #4 -- implement file cache for managing files served fr…
…om iwdp Summary: This provides an abstraction for translating between URLs and files on disk. URLs are provided by the IWDP server, and map to js bundles or source maps. Reviewed By: jgebhardt Differential Revision: D3989142 fbshipit-source-id: 6c64090a32f4866c5e2e66eca450793a7d3bb7e6
- Loading branch information
1 parent
f39d82f
commit 43318c6
Showing
2 changed files
with
143 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
'use babel'; | ||
/* @flow */ | ||
|
||
/* | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the LICENSE file in | ||
* the root directory of this source tree. | ||
*/ | ||
|
||
import UniversalDisposable from '../../commons-node/UniversalDisposable'; | ||
import xfetch from '../../commons-node/xfetch'; | ||
import fsPromise from '../../commons-node/fsPromise'; | ||
import nuclideUri from '../../commons-node/nuclideUri'; | ||
import {logger} from './logger'; | ||
|
||
const {log} = logger; | ||
|
||
type FileData = { | ||
filePath: string, // Path to file on disk. | ||
url: string, // Url bundle is served from. | ||
sourceMapUrl?: string, // Url that chrome devtools understands and can decode to get source maps. | ||
}; | ||
|
||
type Url = string; | ||
|
||
const SOURCE_MAP_REGEX = /\/\/# sourceMappingURL=(.+)$/; | ||
const SOURCE_MAP_PREFIX = 'data:application/json;base64,'; | ||
|
||
export class FileCache { | ||
_disposables: UniversalDisposable; | ||
_filePathToFileData: Map<Url, FileData>; | ||
_urlToFileData: Map<Url, FileData>; | ||
|
||
constructor() { | ||
this._filePathToFileData = new Map(); | ||
this._urlToFileData = new Map(); | ||
this._disposables = new UniversalDisposable( | ||
() => this._filePathToFileData.clear(), | ||
() => this._urlToFileData.clear(), | ||
); | ||
} | ||
|
||
async handleScriptParsed(obj: Object): Promise<Object> { | ||
const {params} = obj; | ||
if (params == null) { | ||
return obj; | ||
} | ||
const {url: urlString} = params; | ||
if (urlString == null || urlString === '') { | ||
return obj; | ||
} | ||
const url = new URL(urlString); | ||
if (url.protocol !== 'http:') { | ||
return obj; | ||
} | ||
const fileData = this._urlToFileData.get(urlString); | ||
if (fileData != null) { | ||
updateMessageObjWithFileData(obj, fileData); | ||
return obj; | ||
} | ||
const newFileData = await createFileData(url); | ||
this._urlToFileData.set(newFileData.url, newFileData); | ||
this._filePathToFileData.set(newFileData.filePath, newFileData); | ||
updateMessageObjWithFileData(obj, newFileData); | ||
return obj; | ||
} | ||
|
||
handleSetBreakpointByUrl(obj: Object): Object { | ||
const filePath = obj.params.url; | ||
const fileData = this._filePathToFileData.get(filePath); | ||
if (fileData == null) { | ||
return obj; | ||
} | ||
obj.params.url = fileData.url; | ||
return obj; | ||
} | ||
|
||
async dispose(): Promise<void> { | ||
this._disposables.dispose(); | ||
} | ||
} | ||
|
||
async function createFileData(url: URL): Promise<FileData> { | ||
// Handle the bundle file. | ||
log(`FileCache got url: ${url.toString()}`); | ||
const fileResponse = await xfetch(url.toString(), {}); | ||
const basename = nuclideUri.basename(url.pathname); | ||
const [fileText, filePath] = await Promise.all([ | ||
fileResponse.text(), | ||
fsPromise.tempfile({prefix: basename, suffix: '.js'}), | ||
]); | ||
await fsPromise.writeFile(filePath, fileText); | ||
const fileSystemUrl = `file://${filePath}`; | ||
|
||
const matches = SOURCE_MAP_REGEX.exec(fileText); | ||
if (matches == null) { | ||
return { | ||
filePath: fileSystemUrl, | ||
url: url.toString(), | ||
}; | ||
} | ||
|
||
// Handle source maps for the bundle. | ||
const sourceMapUrl = `${url.origin}${matches[1]}`; | ||
const sourceMapResponse = await xfetch(sourceMapUrl, {}); | ||
const sourceMap = await sourceMapResponse.text(); | ||
const base64SourceMap = new Buffer(sourceMap).toString('base64'); | ||
return { | ||
filePath: fileSystemUrl, | ||
url: url.toString(), | ||
sourceMapUrl: `${SOURCE_MAP_PREFIX}${base64SourceMap}`, | ||
}; | ||
} | ||
|
||
function updateMessageObjWithFileData( | ||
obj: {params: {url: string, sourceMapURL?: string}}, | ||
fileData: FileData, | ||
): void { | ||
obj.params.url = fileData.filePath; | ||
obj.params.sourceMapURL = fileData.sourceMapUrl; | ||
} |