Merge from vscode 52dcb723a39ae75bee1bd56b3312d7fcdc87aeed (#6719)

This commit is contained in:
Anthony Dresser
2019-08-12 21:31:51 -07:00
committed by GitHub
parent 00250839fc
commit 7eba8c4c03
616 changed files with 9472 additions and 7087 deletions

View File

@@ -709,8 +709,11 @@ var AMDLoader;
var recorder = moduleManager.getRecorder();
var cachedDataPath = that._getCachedDataPath(nodeCachedData, filename);
var options = { filename: filename };
var hashData;
try {
options.cachedData = that._fs.readFileSync(cachedDataPath);
var data = that._fs.readFileSync(cachedDataPath);
hashData = data.slice(0, 16);
options.cachedData = data.slice(16);
recorder.record(60 /* CachedDataFound */, cachedDataPath);
}
catch (_e) {
@@ -724,7 +727,8 @@ var AMDLoader;
var args = [this.exports, require, this, filename, dirname, process, _commonjsGlobal, Buffer];
var result = compileWrapper.apply(this.exports, args);
// cached data aftermath
setTimeout(function () { return that._handleCachedData(script, cachedDataPath, !options.cachedData, moduleManager); }, Math.ceil(moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay * Math.random()));
that._handleCachedData(script, scriptSource, cachedDataPath, !options.cachedData, moduleManager);
that._verifyCachedData(script, scriptSource, cachedDataPath, hashData);
return result;
};
};
@@ -755,7 +759,7 @@ var AMDLoader;
var vmScriptPathOrUri_1 = this._getElectronRendererScriptPathOrUri(normalizedScriptSrc_1);
var wantsCachedData_1 = Boolean(opts.nodeCachedData);
var cachedDataPath_1 = wantsCachedData_1 ? this._getCachedDataPath(opts.nodeCachedData, scriptSrc) : undefined;
this._readSourceAndCachedData(normalizedScriptSrc_1, cachedDataPath_1, recorder, function (err, data, cachedData) {
this._readSourceAndCachedData(normalizedScriptSrc_1, cachedDataPath_1, recorder, function (err, data, cachedData, hashData) {
if (err) {
errorback(err);
return;
@@ -770,7 +774,8 @@ var AMDLoader;
scriptSource = nodeInstrumenter(scriptSource, normalizedScriptSrc_1);
var scriptOpts = { filename: vmScriptPathOrUri_1, cachedData: cachedData };
var script = _this._createAndEvalScript(moduleManager, scriptSource, scriptOpts, callback, errorback);
_this._handleCachedData(script, cachedDataPath_1, wantsCachedData_1 && !cachedData, moduleManager);
_this._handleCachedData(script, scriptSource, cachedDataPath_1, wantsCachedData_1 && !cachedData, moduleManager);
_this._verifyCachedData(script, scriptSource, cachedDataPath_1, hashData);
});
}
};
@@ -815,13 +820,13 @@ var AMDLoader;
var basename = this._path.basename(filename).replace(/\.js$/, '');
return this._path.join(config.path, basename + "-" + hash + ".code");
};
NodeScriptLoader.prototype._handleCachedData = function (script, cachedDataPath, createCachedData, moduleManager) {
NodeScriptLoader.prototype._handleCachedData = function (script, scriptSource, cachedDataPath, createCachedData, moduleManager) {
var _this = this;
if (script.cachedDataRejected) {
// cached data got rejected -> delete and re-create
this._fs.unlink(cachedDataPath, function (err) {
moduleManager.getRecorder().record(62 /* CachedDataRejected */, cachedDataPath);
_this._createAndWriteCachedData(script, cachedDataPath, moduleManager);
_this._createAndWriteCachedData(script, scriptSource, cachedDataPath, moduleManager);
if (err) {
moduleManager.getConfig().onError(err);
}
@@ -829,22 +834,29 @@ var AMDLoader;
}
else if (createCachedData) {
// no cached data, but wanted
this._createAndWriteCachedData(script, cachedDataPath, moduleManager);
this._createAndWriteCachedData(script, scriptSource, cachedDataPath, moduleManager);
}
};
NodeScriptLoader.prototype._createAndWriteCachedData = function (script, cachedDataPath, moduleManager) {
// Cached data format: | SOURCE_HASH | V8_CACHED_DATA |
// -SOURCE_HASH is the md5 hash of the JS source (always 16 bytes)
// -V8_CACHED_DATA is what v8 produces
NodeScriptLoader.prototype._createAndWriteCachedData = function (script, scriptSource, cachedDataPath, moduleManager) {
var _this = this;
var timeout = Math.ceil(moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay * (1 + Math.random()));
var lastSize = -1;
var iteration = 0;
var hashData = undefined;
var createLoop = function () {
setTimeout(function () {
if (!hashData) {
hashData = _this._crypto.createHash('md5').update(scriptSource, 'utf8').digest();
}
var cachedData = script.createCachedData();
if (cachedData.length === 0 || cachedData.length === lastSize || iteration >= 5) {
return;
}
lastSize = cachedData.length;
_this._fs.writeFile(cachedDataPath, cachedData, function (err) {
_this._fs.writeFile(cachedDataPath, Buffer.concat([hashData, cachedData]), function (err) {
if (err) {
moduleManager.getConfig().onError(err);
}
@@ -865,15 +877,16 @@ var AMDLoader;
}
else {
// cached data case: read both files in parallel
var source_1;
var cachedData_1;
var source_1 = undefined;
var cachedData_1 = undefined;
var hashData_1 = undefined;
var steps_1 = 2;
var step_1 = function (err) {
if (err) {
callback(err);
}
else if (--steps_1 === 0) {
callback(undefined, source_1, cachedData_1);
callback(undefined, source_1, cachedData_1, hashData_1);
}
};
this._fs.readFile(sourcePath, { encoding: 'utf8' }, function (err, data) {
@@ -881,12 +894,39 @@ var AMDLoader;
step_1(err);
});
this._fs.readFile(cachedDataPath, function (err, data) {
cachedData_1 = data && data.length > 0 ? data : undefined;
if (!err && data && data.length > 0) {
hashData_1 = data.slice(0, 16);
cachedData_1 = data.slice(16);
recorder.record(60 /* CachedDataFound */, cachedDataPath);
}
else {
recorder.record(61 /* CachedDataMissed */, cachedDataPath);
}
step_1(); // ignored: cached data is optional
recorder.record(err ? 61 /* CachedDataMissed */ : 60 /* CachedDataFound */, cachedDataPath);
});
}
};
NodeScriptLoader.prototype._verifyCachedData = function (script, scriptSource, cachedDataPath, hashData) {
var _this = this;
if (!hashData) {
// nothing to do
return;
}
if (script.cachedDataRejected) {
// invalid anyways
return;
}
setTimeout(function () {
// check source hash - the contract is that file paths change when file content
// change (e.g use the commit or version id as cache path). this check is
// for violations of this contract.
var hashDataNow = _this._crypto.createHash('md5').update(scriptSource, 'utf8').digest();
if (!hashData.equals(hashDataNow)) {
console.warn("FAILED TO VERIFY CACHED DATA. Deleting '" + cachedDataPath + "' now, but a RESTART IS REQUIRED");
_this._fs.unlink(cachedDataPath, function (err) { return console.error("FAILED to unlink: '" + cachedDataPath + "'", err); });
}
}, Math.ceil(5000 * (1 + Math.random())));
};
NodeScriptLoader._BOM = 0xFEFF;
NodeScriptLoader._PREFIX = '(function (require, define, __filename, __dirname) { ';
NodeScriptLoader._SUFFIX = '\n});';