mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-05 17:23:51 -05:00
This reverts commit d15a3fcc98.
This commit is contained in:
252
src/vs/loader.js
252
src/vs/loader.js
@@ -235,7 +235,6 @@ var AMDLoader;
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
var AMDLoader;
|
||||
(function (AMDLoader) {
|
||||
;
|
||||
var ConfigurationOptionsUtil = (function () {
|
||||
function ConfigurationOptionsUtil() {
|
||||
}
|
||||
@@ -306,8 +305,22 @@ var AMDLoader;
|
||||
if (typeof options.nodeCachedData.writeDelay !== 'number' || options.nodeCachedData.writeDelay < 0) {
|
||||
options.nodeCachedData.writeDelay = 1000 * 7;
|
||||
}
|
||||
if (typeof options.nodeCachedData.onData !== 'function') {
|
||||
options.nodeCachedData.onData = function (err) {
|
||||
if (err && err.errorCode === 'cachedDataRejected') {
|
||||
console.warn('Rejected cached data from file: ' + err.path);
|
||||
}
|
||||
else if (err && err.errorCode) {
|
||||
console.error('Problems handling cached data file: ' + err.path);
|
||||
console.error(err.detail);
|
||||
}
|
||||
else if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
}
|
||||
if (!options.nodeCachedData.path || typeof options.nodeCachedData.path !== 'string') {
|
||||
options.onError('INVALID cached data configuration, \'path\' MUST be set');
|
||||
options.nodeCachedData.onData('INVALID cached data configuration, \'path\' MUST be set');
|
||||
options.nodeCachedData = undefined;
|
||||
}
|
||||
}
|
||||
@@ -647,6 +660,7 @@ var AMDLoader;
|
||||
this._env = env;
|
||||
this._didInitialize = false;
|
||||
this._didPatchNodeRequire = false;
|
||||
this._hasCreateCachedData = false;
|
||||
}
|
||||
NodeScriptLoader.prototype._init = function (nodeRequire) {
|
||||
if (this._didInitialize) {
|
||||
@@ -658,17 +672,14 @@ var AMDLoader;
|
||||
this._vm = nodeRequire('vm');
|
||||
this._path = nodeRequire('path');
|
||||
this._crypto = nodeRequire('crypto');
|
||||
// check for `createCachedData`-api
|
||||
this._hasCreateCachedData = typeof (new this._vm.Script('').createCachedData) === 'function';
|
||||
};
|
||||
// patch require-function of nodejs such that we can manually create a script
|
||||
// from cached data. this is done by overriding the `Module._compile` function
|
||||
NodeScriptLoader.prototype._initNodeRequire = function (nodeRequire, moduleManager) {
|
||||
// It is important to check for `nodeCachedData` first and then set `_didPatchNodeRequire`.
|
||||
// That's because `nodeCachedData` is set _after_ calling this for the first time...
|
||||
var nodeCachedData = moduleManager.getConfig().getOptionsLiteral().nodeCachedData;
|
||||
if (!nodeCachedData) {
|
||||
return;
|
||||
}
|
||||
if (this._didPatchNodeRequire) {
|
||||
if (!nodeCachedData || this._didPatchNodeRequire) {
|
||||
return;
|
||||
}
|
||||
this._didPatchNodeRequire = true;
|
||||
@@ -693,28 +704,25 @@ var AMDLoader;
|
||||
return require;
|
||||
}
|
||||
Module.prototype._compile = function (content, filename) {
|
||||
// remove shebang and create wrapper function
|
||||
var scriptSource = Module.wrap(content.replace(/^#!.*/, ''));
|
||||
// create script
|
||||
var recorder = moduleManager.getRecorder();
|
||||
var cachedDataPath = that._getCachedDataPath(nodeCachedData, filename);
|
||||
// remove shebang
|
||||
content = content.replace(/^#!.*/, '');
|
||||
// create wrapper function
|
||||
var wrapper = Module.wrap(content);
|
||||
var cachedDataPath = that._getCachedDataPath(nodeCachedData.seed, nodeCachedData.path, filename);
|
||||
var options = { filename: filename };
|
||||
try {
|
||||
options.cachedData = that._fs.readFileSync(cachedDataPath);
|
||||
recorder.record(60 /* CachedDataFound */, cachedDataPath);
|
||||
}
|
||||
catch (_e) {
|
||||
recorder.record(61 /* CachedDataMissed */, cachedDataPath);
|
||||
catch (e) {
|
||||
options.produceCachedData = !that._hasCreateCachedData;
|
||||
}
|
||||
var script = new that._vm.Script(scriptSource, options);
|
||||
var script = new that._vm.Script(wrapper, options);
|
||||
var compileWrapper = script.runInThisContext(options);
|
||||
// run script
|
||||
var dirname = that._path.dirname(filename);
|
||||
var require = makeRequireFunction(this);
|
||||
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._processCachedData(moduleManager, script, wrapper, cachedDataPath, !options.cachedData);
|
||||
return result;
|
||||
};
|
||||
};
|
||||
@@ -741,34 +749,57 @@ var AMDLoader;
|
||||
}
|
||||
else {
|
||||
scriptSrc = AMDLoader.Utilities.fileUriToFilePath(this._env.isWindows, scriptSrc);
|
||||
var normalizedScriptSrc_1 = this._path.normalize(scriptSrc);
|
||||
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._fs.readFile(scriptSrc, { encoding: 'utf8' }, function (err, data) {
|
||||
if (err) {
|
||||
errorback(err);
|
||||
return;
|
||||
}
|
||||
var scriptSource;
|
||||
var normalizedScriptSrc = _this._path.normalize(scriptSrc);
|
||||
var vmScriptSrc = normalizedScriptSrc;
|
||||
// Make the script src friendly towards electron
|
||||
if (_this._env.isElectronRenderer) {
|
||||
var driveLetterMatch = vmScriptSrc.match(/^([a-z])\:(.*)/i);
|
||||
if (driveLetterMatch) {
|
||||
// windows
|
||||
vmScriptSrc = "file:///" + (driveLetterMatch[1].toUpperCase() + ':' + driveLetterMatch[2]).replace(/\\/g, '/');
|
||||
}
|
||||
else {
|
||||
// nix
|
||||
vmScriptSrc = "file://" + vmScriptSrc;
|
||||
}
|
||||
}
|
||||
var contents, prefix = '(function (require, define, __filename, __dirname) { ', suffix = '\n});';
|
||||
if (data.charCodeAt(0) === NodeScriptLoader._BOM) {
|
||||
scriptSource = NodeScriptLoader._PREFIX + data.substring(1) + NodeScriptLoader._SUFFIX;
|
||||
contents = prefix + data.substring(1) + suffix;
|
||||
}
|
||||
else {
|
||||
scriptSource = NodeScriptLoader._PREFIX + data + NodeScriptLoader._SUFFIX;
|
||||
contents = prefix + data + suffix;
|
||||
}
|
||||
contents = nodeInstrumenter(contents, normalizedScriptSrc);
|
||||
if (!opts.nodeCachedData) {
|
||||
_this._loadAndEvalScript(moduleManager, scriptSrc, vmScriptSrc, contents, { filename: vmScriptSrc }, recorder, callback, errorback);
|
||||
}
|
||||
else {
|
||||
var cachedDataPath_1 = _this._getCachedDataPath(opts.nodeCachedData.seed, opts.nodeCachedData.path, scriptSrc);
|
||||
_this._fs.readFile(cachedDataPath_1, function (_err, cachedData) {
|
||||
// create script options
|
||||
var options = {
|
||||
filename: vmScriptSrc,
|
||||
produceCachedData: !_this._hasCreateCachedData && typeof cachedData === 'undefined',
|
||||
cachedData: cachedData
|
||||
};
|
||||
var script = _this._loadAndEvalScript(moduleManager, scriptSrc, vmScriptSrc, contents, options, recorder, callback, errorback);
|
||||
_this._processCachedData(moduleManager, script, contents, cachedDataPath_1, !options.cachedData);
|
||||
});
|
||||
}
|
||||
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);
|
||||
});
|
||||
}
|
||||
};
|
||||
NodeScriptLoader.prototype._createAndEvalScript = function (moduleManager, contents, options, callback, errorback) {
|
||||
var recorder = moduleManager.getRecorder();
|
||||
recorder.record(31 /* NodeBeginEvaluatingScript */, options.filename);
|
||||
NodeScriptLoader.prototype._loadAndEvalScript = function (moduleManager, scriptSrc, vmScriptSrc, contents, options, recorder, callback, errorback) {
|
||||
// create script, run script
|
||||
recorder.record(31 /* NodeBeginEvaluatingScript */, scriptSrc);
|
||||
var script = new this._vm.Script(contents, options);
|
||||
var ret = script.runInThisContext(options);
|
||||
var r = script.runInThisContext(options);
|
||||
var globalDefineFunc = moduleManager.getGlobalAMDDefineFunc();
|
||||
var receivedDefineCall = false;
|
||||
var localDefineFunc = function () {
|
||||
@@ -776,112 +807,89 @@ var AMDLoader;
|
||||
return globalDefineFunc.apply(null, arguments);
|
||||
};
|
||||
localDefineFunc.amd = globalDefineFunc.amd;
|
||||
ret.call(AMDLoader.global, moduleManager.getGlobalAMDRequireFunc(), localDefineFunc, options.filename, this._path.dirname(options.filename));
|
||||
recorder.record(32 /* NodeEndEvaluatingScript */, options.filename);
|
||||
r.call(AMDLoader.global, moduleManager.getGlobalAMDRequireFunc(), localDefineFunc, vmScriptSrc, this._path.dirname(scriptSrc));
|
||||
// signal done
|
||||
recorder.record(32 /* NodeEndEvaluatingScript */, scriptSrc);
|
||||
if (receivedDefineCall) {
|
||||
callback();
|
||||
}
|
||||
else {
|
||||
errorback(new Error("Didn't receive define call in " + options.filename + "!"));
|
||||
errorback(new Error("Didn't receive define call in " + scriptSrc + "!"));
|
||||
}
|
||||
return script;
|
||||
};
|
||||
NodeScriptLoader.prototype._getElectronRendererScriptPathOrUri = function (path) {
|
||||
if (!this._env.isElectronRenderer) {
|
||||
return path;
|
||||
}
|
||||
var driveLetterMatch = path.match(/^([a-z])\:(.*)/i);
|
||||
if (driveLetterMatch) {
|
||||
// windows
|
||||
return "file:///" + (driveLetterMatch[1].toUpperCase() + ':' + driveLetterMatch[2]).replace(/\\/g, '/');
|
||||
}
|
||||
else {
|
||||
// nix
|
||||
return "file://" + path;
|
||||
}
|
||||
};
|
||||
NodeScriptLoader.prototype._getCachedDataPath = function (config, filename) {
|
||||
var hash = this._crypto.createHash('md5').update(filename, 'utf8').update(config.seed, 'utf8').digest('hex');
|
||||
NodeScriptLoader.prototype._getCachedDataPath = function (seed, basedir, filename) {
|
||||
var hash = this._crypto.createHash('md5').update(filename, 'utf8').update(seed, 'utf8').digest('hex');
|
||||
var basename = this._path.basename(filename).replace(/\.js$/, '');
|
||||
return this._path.join(config.path, basename + "-" + hash + ".code");
|
||||
return this._path.join(basedir, basename + "-" + hash + ".code");
|
||||
};
|
||||
NodeScriptLoader.prototype._handleCachedData = function (script, cachedDataPath, createCachedData, moduleManager) {
|
||||
NodeScriptLoader.prototype._processCachedData = function (moduleManager, script, contents, cachedDataPath, createCachedData) {
|
||||
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);
|
||||
if (err) {
|
||||
moduleManager.getConfig().onError(err);
|
||||
}
|
||||
// data rejected => delete cache file
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData({
|
||||
errorCode: 'cachedDataRejected',
|
||||
path: cachedDataPath
|
||||
});
|
||||
}
|
||||
else if (createCachedData) {
|
||||
// no cached data, but wanted
|
||||
this._createAndWriteCachedData(script, cachedDataPath, moduleManager);
|
||||
}
|
||||
};
|
||||
NodeScriptLoader.prototype._createAndWriteCachedData = function (script, cachedDataPath, moduleManager) {
|
||||
var _this = this;
|
||||
var timeout = Math.ceil(moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay * (1 + Math.random()));
|
||||
var lastSize = -1;
|
||||
var iteration = 0;
|
||||
var createLoop = function () {
|
||||
setTimeout(function () {
|
||||
var cachedData = script.createCachedData();
|
||||
if (cachedData.length === 0 || cachedData.length === lastSize || iteration >= 5) {
|
||||
return;
|
||||
}
|
||||
lastSize = cachedData.length;
|
||||
_this._fs.writeFile(cachedDataPath, cachedData, function (err) {
|
||||
NodeScriptLoader._runSoon(function () {
|
||||
return _this._fs.unlink(cachedDataPath, function (err) {
|
||||
if (err) {
|
||||
moduleManager.getConfig().onError(err);
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData({
|
||||
errorCode: 'unlink',
|
||||
path: cachedDataPath,
|
||||
detail: err
|
||||
});
|
||||
}
|
||||
moduleManager.getRecorder().record(63 /* CachedDataCreated */, cachedDataPath);
|
||||
createLoop();
|
||||
});
|
||||
}, timeout * (Math.pow(4, iteration++)));
|
||||
};
|
||||
// with some delay (`timeout`) create cached data
|
||||
// and repeat that (with backoff delay) until the
|
||||
// data seems to be not changing anymore
|
||||
createLoop();
|
||||
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay / 2);
|
||||
}
|
||||
else if (script.cachedDataProduced) {
|
||||
// data produced => tell outside world
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData(undefined, {
|
||||
path: cachedDataPath
|
||||
});
|
||||
// data produced => write cache file
|
||||
NodeScriptLoader._runSoon(function () {
|
||||
return _this._fs.writeFile(cachedDataPath, script.cachedData, function (err) {
|
||||
if (err) {
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData({
|
||||
errorCode: 'writeFile',
|
||||
path: cachedDataPath,
|
||||
detail: err
|
||||
});
|
||||
}
|
||||
});
|
||||
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay);
|
||||
}
|
||||
else if (this._hasCreateCachedData && createCachedData) {
|
||||
// NEW world
|
||||
// data produced => tell outside world
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData(undefined, {
|
||||
path: cachedDataPath
|
||||
});
|
||||
// soon'ish create and save cached data
|
||||
NodeScriptLoader._runSoon(function () {
|
||||
var data = script.createCachedData(contents);
|
||||
_this._fs.writeFile(cachedDataPath, data, function (err) {
|
||||
if (!err) {
|
||||
return;
|
||||
}
|
||||
moduleManager.getConfig().getOptionsLiteral().nodeCachedData.onData({
|
||||
errorCode: 'writeFile',
|
||||
path: cachedDataPath,
|
||||
detail: err
|
||||
});
|
||||
});
|
||||
}, moduleManager.getConfig().getOptionsLiteral().nodeCachedData.writeDelay);
|
||||
}
|
||||
};
|
||||
NodeScriptLoader.prototype._readSourceAndCachedData = function (sourcePath, cachedDataPath, recorder, callback) {
|
||||
if (!cachedDataPath) {
|
||||
// no cached data case
|
||||
this._fs.readFile(sourcePath, { encoding: 'utf8' }, callback);
|
||||
}
|
||||
else {
|
||||
// cached data case: read both files in parallel
|
||||
var source_1;
|
||||
var cachedData_1;
|
||||
var steps_1 = 2;
|
||||
var step_1 = function (err) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
}
|
||||
else if (--steps_1 === 0) {
|
||||
callback(undefined, source_1, cachedData_1);
|
||||
}
|
||||
};
|
||||
this._fs.readFile(sourcePath, { encoding: 'utf8' }, function (err, data) {
|
||||
source_1 = data;
|
||||
step_1(err);
|
||||
});
|
||||
this._fs.readFile(cachedDataPath, function (err, data) {
|
||||
cachedData_1 = data && data.length > 0 ? data : undefined;
|
||||
step_1(); // ignored: cached data is optional
|
||||
recorder.record(err ? 61 /* CachedDataMissed */ : 60 /* CachedDataFound */, cachedDataPath);
|
||||
});
|
||||
}
|
||||
NodeScriptLoader._runSoon = function (callback, minTimeout) {
|
||||
var timeout = minTimeout + Math.ceil(Math.random() * minTimeout);
|
||||
setTimeout(callback, timeout);
|
||||
};
|
||||
return NodeScriptLoader;
|
||||
}());
|
||||
NodeScriptLoader._BOM = 0xFEFF;
|
||||
NodeScriptLoader._PREFIX = '(function (require, define, __filename, __dirname) { ';
|
||||
NodeScriptLoader._SUFFIX = '\n});';
|
||||
function createScriptLoader(env) {
|
||||
return new OnlyOnceScriptLoader(env);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user