Move JavaScript to root lib/ folder
This commit is contained in:
parent
a9c40de393
commit
70aa9b06ee
56 changed files with 0 additions and 0 deletions
66
lib/common/api/callbacks-registry.js
Normal file
66
lib/common/api/callbacks-registry.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
|
||||
var slice = [].slice;
|
||||
|
||||
const v8Util = process.atomBinding('v8_util');
|
||||
|
||||
class CallbacksRegistry {
|
||||
constructor() {
|
||||
this.nextId = 0;
|
||||
this.callbacks = {};
|
||||
}
|
||||
|
||||
add(callback) {
|
||||
// The callback is already added.
|
||||
var filenameAndLine, id, location, match, ref, regexp, stackString;
|
||||
id = v8Util.getHiddenValue(callback, 'callbackId');
|
||||
if (id != null) {
|
||||
return id;
|
||||
}
|
||||
id = ++this.nextId;
|
||||
|
||||
// Capture the location of the function and put it in the ID string,
|
||||
// so that release errors can be tracked down easily.
|
||||
regexp = /at (.*)/gi;
|
||||
stackString = (new Error).stack;
|
||||
while ((match = regexp.exec(stackString)) !== null) {
|
||||
location = match[1];
|
||||
if (location.indexOf('(native)') !== -1) {
|
||||
continue;
|
||||
}
|
||||
if (location.indexOf('atom.asar') !== -1) {
|
||||
continue;
|
||||
}
|
||||
ref = /([^\/^\)]*)\)?$/gi.exec(location);
|
||||
filenameAndLine = ref[1];
|
||||
break;
|
||||
}
|
||||
this.callbacks[id] = callback;
|
||||
v8Util.setHiddenValue(callback, 'callbackId', id);
|
||||
v8Util.setHiddenValue(callback, 'location', filenameAndLine);
|
||||
return id;
|
||||
}
|
||||
|
||||
get(id) {
|
||||
var ref;
|
||||
return (ref = this.callbacks[id]) != null ? ref : function() {};
|
||||
}
|
||||
|
||||
call() {
|
||||
var args, id, ref;
|
||||
id = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
||||
return (ref = this.get(id)).call.apply(ref, [global].concat(slice.call(args)));
|
||||
}
|
||||
|
||||
apply() {
|
||||
var args, id, ref;
|
||||
id = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
|
||||
return (ref = this.get(id)).apply.apply(ref, [global].concat(slice.call(args)));
|
||||
}
|
||||
|
||||
remove(id) {
|
||||
return delete this.callbacks[id];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = CallbacksRegistry;
|
6
lib/common/api/clipboard.js
Normal file
6
lib/common/api/clipboard.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
if (process.platform === 'linux' && process.type === 'renderer') {
|
||||
// On Linux we could not access clipboard in renderer process.
|
||||
module.exports = require('electron').remote.clipboard;
|
||||
} else {
|
||||
module.exports = process.atomBinding('clipboard');
|
||||
}
|
94
lib/common/api/crash-reporter.js
Normal file
94
lib/common/api/crash-reporter.js
Normal file
|
@ -0,0 +1,94 @@
|
|||
const os = require('os');
|
||||
const path = require('path');
|
||||
const spawn = require('child_process').spawn;
|
||||
const electron = require('electron');
|
||||
const binding = process.atomBinding('crash_reporter');
|
||||
|
||||
var CrashReporter = (function() {
|
||||
function CrashReporter() {}
|
||||
|
||||
CrashReporter.prototype.start = function(options) {
|
||||
var app, args, autoSubmit, companyName, deprecate, env, extra, ignoreSystemCrashHandler, start, submitURL;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
this.productName = options.productName, companyName = options.companyName, submitURL = options.submitURL, autoSubmit = options.autoSubmit, ignoreSystemCrashHandler = options.ignoreSystemCrashHandler, extra = options.extra;
|
||||
|
||||
// Deprecated.
|
||||
deprecate = electron.deprecate;
|
||||
if (options.submitUrl) {
|
||||
if (submitURL == null) {
|
||||
submitURL = options.submitUrl;
|
||||
}
|
||||
deprecate.warn('submitUrl', 'submitURL');
|
||||
}
|
||||
app = (process.type === 'browser' ? electron : electron.remote).app;
|
||||
if (this.productName == null) {
|
||||
this.productName = app.getName();
|
||||
}
|
||||
if (autoSubmit == null) {
|
||||
autoSubmit = true;
|
||||
}
|
||||
if (ignoreSystemCrashHandler == null) {
|
||||
ignoreSystemCrashHandler = false;
|
||||
}
|
||||
if (extra == null) {
|
||||
extra = {};
|
||||
}
|
||||
if (extra._productName == null) {
|
||||
extra._productName = this.productName;
|
||||
}
|
||||
if (extra._companyName == null) {
|
||||
extra._companyName = companyName;
|
||||
}
|
||||
if (extra._version == null) {
|
||||
extra._version = app.getVersion();
|
||||
}
|
||||
if (companyName == null) {
|
||||
deprecate.log('companyName is now a required option to crashReporter.start');
|
||||
return;
|
||||
}
|
||||
if (submitURL == null) {
|
||||
deprecate.log('submitURL is now a required option to crashReporter.start');
|
||||
return;
|
||||
}
|
||||
start = (function(_this) {
|
||||
return function() {
|
||||
return binding.start(_this.productName, companyName, submitURL, autoSubmit, ignoreSystemCrashHandler, extra);
|
||||
};
|
||||
})(this);
|
||||
if (process.platform === 'win32') {
|
||||
args = ["--reporter-url=" + submitURL, "--application-name=" + this.productName, "--v=1"];
|
||||
env = {
|
||||
ATOM_SHELL_INTERNAL_CRASH_SERVICE: 1
|
||||
};
|
||||
spawn(process.execPath, args, {
|
||||
env: env,
|
||||
detached: true
|
||||
});
|
||||
}
|
||||
return start();
|
||||
};
|
||||
|
||||
CrashReporter.prototype.getLastCrashReport = function() {
|
||||
var reports;
|
||||
reports = this.getUploadedReports();
|
||||
if (reports.length > 0) {
|
||||
return reports[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
CrashReporter.prototype.getUploadedReports = function() {
|
||||
var log, tmpdir;
|
||||
tmpdir = process.platform === 'win32' ? os.tmpdir() : '/tmp';
|
||||
log = process.platform === 'darwin' ? path.join(tmpdir, this.productName + " Crashes") : path.join(tmpdir, this.productName + " Crashes", 'uploads.log');
|
||||
return binding._getUploadedReports(log);
|
||||
};
|
||||
|
||||
return CrashReporter;
|
||||
|
||||
})();
|
||||
|
||||
module.exports = new CrashReporter;
|
114
lib/common/api/deprecate.js
Normal file
114
lib/common/api/deprecate.js
Normal file
|
@ -0,0 +1,114 @@
|
|||
// Deprecate a method.
|
||||
var deprecate,
|
||||
slice = [].slice;
|
||||
|
||||
deprecate = function(oldName, newName, fn) {
|
||||
var warned;
|
||||
warned = false;
|
||||
return function() {
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true;
|
||||
deprecate.warn(oldName, newName);
|
||||
}
|
||||
return fn.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
// The method is renamed.
|
||||
deprecate.rename = function(object, oldName, newName) {
|
||||
var newMethod, warned;
|
||||
warned = false;
|
||||
newMethod = function() {
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true;
|
||||
deprecate.warn(oldName, newName);
|
||||
}
|
||||
return this[newName].apply(this, arguments);
|
||||
};
|
||||
if (typeof object === 'function') {
|
||||
return object.prototype[oldName] = newMethod;
|
||||
} else {
|
||||
return object[oldName] = newMethod;
|
||||
}
|
||||
};
|
||||
|
||||
// Forward the method to member.
|
||||
deprecate.member = function(object, method, member) {
|
||||
var warned;
|
||||
warned = false;
|
||||
return object.prototype[method] = function() {
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true;
|
||||
deprecate.warn(method, member + "." + method);
|
||||
}
|
||||
return this[member][method].apply(this[member], arguments);
|
||||
};
|
||||
};
|
||||
|
||||
// Deprecate a property.
|
||||
deprecate.property = function(object, property, method) {
|
||||
return Object.defineProperty(object, property, {
|
||||
get: function() {
|
||||
var warned;
|
||||
warned = false;
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true;
|
||||
deprecate.warn(property + " property", method + " method");
|
||||
}
|
||||
return this[method]();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Deprecate an event.
|
||||
deprecate.event = function(emitter, oldName, newName, fn) {
|
||||
var warned;
|
||||
warned = false;
|
||||
return emitter.on(newName, function() {
|
||||
var args;
|
||||
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
||||
|
||||
// there is listeners for old API.
|
||||
if (this.listenerCount(oldName) > 0) {
|
||||
if (!(warned || process.noDeprecation)) {
|
||||
warned = true;
|
||||
deprecate.warn("'" + oldName + "' event", "'" + newName + "' event");
|
||||
}
|
||||
if (fn != null) {
|
||||
return fn.apply(this, arguments);
|
||||
} else {
|
||||
return this.emit.apply(this, [oldName].concat(slice.call(args)));
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Print deprecation warning.
|
||||
deprecate.warn = function(oldName, newName) {
|
||||
return deprecate.log(oldName + " is deprecated. Use " + newName + " instead.");
|
||||
};
|
||||
|
||||
var deprecationHandler = null;
|
||||
|
||||
// Print deprecation message.
|
||||
deprecate.log = function(message) {
|
||||
if (typeof deprecationHandler === 'function') {
|
||||
deprecationHandler(message);
|
||||
} else if (process.throwDeprecation) {
|
||||
throw new Error(message);
|
||||
} else if (process.traceDeprecation) {
|
||||
return console.trace(message);
|
||||
} else {
|
||||
return console.warn("(electron) " + message);
|
||||
}
|
||||
};
|
||||
|
||||
deprecate.setHandler = function(handler) {
|
||||
deprecationHandler = handler;
|
||||
};
|
||||
|
||||
deprecate.getHandler = function() {
|
||||
return deprecationHandler;
|
||||
};
|
||||
|
||||
module.exports = deprecate;
|
11
lib/common/api/deprecations.js
Normal file
11
lib/common/api/deprecations.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const deprecate = require('electron').deprecate;
|
||||
|
||||
exports.setHandler = function (deprecationHandler) {
|
||||
deprecate.setHandler(deprecationHandler);
|
||||
};
|
||||
|
||||
exports.getHandler = function () {
|
||||
return deprecate.getHandler();
|
||||
};
|
65
lib/common/api/exports/electron.js
Normal file
65
lib/common/api/exports/electron.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Do not expose the internal modules to `require`.
|
||||
const hideInternalModules = function() {
|
||||
var globalPaths = require('module').globalPaths;
|
||||
if (globalPaths.length === 3) {
|
||||
|
||||
// Remove the "common/api/lib" and "browser-or-renderer/api/lib".
|
||||
return globalPaths.splice(0, 2);
|
||||
}
|
||||
};
|
||||
|
||||
// Attaches properties to |exports|.
|
||||
exports.defineProperties = function(exports) {
|
||||
return Object.defineProperties(exports, {
|
||||
hideInternalModules: {
|
||||
enumerable: true,
|
||||
value: hideInternalModules
|
||||
},
|
||||
|
||||
// Common modules, please sort with alphabet order.
|
||||
clipboard: {
|
||||
|
||||
// Must be enumerable, otherwise it woulde be invisible to remote module.
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return require('../clipboard');
|
||||
}
|
||||
},
|
||||
crashReporter: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return require('../crash-reporter');
|
||||
}
|
||||
},
|
||||
deprecations: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return require('../deprecations');
|
||||
}
|
||||
},
|
||||
nativeImage: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return require('../native-image');
|
||||
}
|
||||
},
|
||||
shell: {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return require('../shell');
|
||||
}
|
||||
},
|
||||
|
||||
// The internal modules, invisible unless you know their names.
|
||||
CallbacksRegistry: {
|
||||
get: function() {
|
||||
return require('../callbacks-registry');
|
||||
}
|
||||
},
|
||||
deprecate: {
|
||||
get: function() {
|
||||
return require('../deprecate');
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
7
lib/common/api/native-image.js
Normal file
7
lib/common/api/native-image.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
const deprecate = require('electron').deprecate;
|
||||
const nativeImage = process.atomBinding('native_image');
|
||||
|
||||
// Deprecated.
|
||||
deprecate.rename(nativeImage, 'createFromDataUrl', 'createFromDataURL');
|
||||
|
||||
module.exports = nativeImage;
|
1
lib/common/api/shell.js
Normal file
1
lib/common/api/shell.js
Normal file
|
@ -0,0 +1 @@
|
|||
module.exports = process.atomBinding('shell');
|
602
lib/common/asar.js
Normal file
602
lib/common/asar.js
Normal file
|
@ -0,0 +1,602 @@
|
|||
(function () {
|
||||
const asar = process.binding('atom_common_asar');
|
||||
const child_process = require('child_process');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
|
||||
var hasProp = {}.hasOwnProperty;
|
||||
|
||||
// Cache asar archive objects.
|
||||
var cachedArchives = {};
|
||||
|
||||
var getOrCreateArchive = function(p) {
|
||||
var archive;
|
||||
archive = cachedArchives[p];
|
||||
if (archive != null) {
|
||||
return archive;
|
||||
}
|
||||
archive = asar.createArchive(p);
|
||||
if (!archive) {
|
||||
return false;
|
||||
}
|
||||
return cachedArchives[p] = archive;
|
||||
};
|
||||
|
||||
// Clean cache on quit.
|
||||
process.on('exit', function() {
|
||||
var archive, p, results;
|
||||
results = [];
|
||||
for (p in cachedArchives) {
|
||||
if (!hasProp.call(cachedArchives, p)) continue;
|
||||
archive = cachedArchives[p];
|
||||
results.push(archive.destroy());
|
||||
}
|
||||
return results;
|
||||
});
|
||||
|
||||
// Separate asar package's path from full path.
|
||||
var splitPath = function(p) {
|
||||
var index;
|
||||
|
||||
// shortcut to disable asar.
|
||||
if (process.noAsar) {
|
||||
return [false];
|
||||
}
|
||||
|
||||
if (typeof p !== 'string') {
|
||||
return [false];
|
||||
}
|
||||
if (p.substr(-5) === '.asar') {
|
||||
return [true, p, ''];
|
||||
}
|
||||
p = path.normalize(p);
|
||||
index = p.lastIndexOf(".asar" + path.sep);
|
||||
if (index === -1) {
|
||||
return [false];
|
||||
}
|
||||
return [true, p.substr(0, index + 5), p.substr(index + 6)];
|
||||
};
|
||||
|
||||
// Convert asar archive's Stats object to fs's Stats object.
|
||||
var nextInode = 0;
|
||||
|
||||
var uid = process.getuid != null ? process.getuid() : 0;
|
||||
|
||||
var gid = process.getgid != null ? process.getgid() : 0;
|
||||
|
||||
var fakeTime = new Date();
|
||||
|
||||
var asarStatsToFsStats = function(stats) {
|
||||
return {
|
||||
dev: 1,
|
||||
ino: ++nextInode,
|
||||
mode: 33188,
|
||||
nlink: 1,
|
||||
uid: uid,
|
||||
gid: gid,
|
||||
rdev: 0,
|
||||
atime: stats.atime || fakeTime,
|
||||
birthtime: stats.birthtime || fakeTime,
|
||||
mtime: stats.mtime || fakeTime,
|
||||
ctime: stats.ctime || fakeTime,
|
||||
size: stats.size,
|
||||
isFile: function() {
|
||||
return stats.isFile;
|
||||
},
|
||||
isDirectory: function() {
|
||||
return stats.isDirectory;
|
||||
},
|
||||
isSymbolicLink: function() {
|
||||
return stats.isLink;
|
||||
},
|
||||
isBlockDevice: function() {
|
||||
return false;
|
||||
},
|
||||
isCharacterDevice: function() {
|
||||
return false;
|
||||
},
|
||||
isFIFO: function() {
|
||||
return false;
|
||||
},
|
||||
isSocket: function() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Create a ENOENT error.
|
||||
var notFoundError = function(asarPath, filePath, callback) {
|
||||
var error;
|
||||
error = new Error("ENOENT, " + filePath + " not found in " + asarPath);
|
||||
error.code = "ENOENT";
|
||||
error.errno = -2;
|
||||
if (typeof callback !== 'function') {
|
||||
throw error;
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(error);
|
||||
});
|
||||
};
|
||||
|
||||
// Create a ENOTDIR error.
|
||||
var notDirError = function(callback) {
|
||||
var error;
|
||||
error = new Error('ENOTDIR, not a directory');
|
||||
error.code = 'ENOTDIR';
|
||||
error.errno = -20;
|
||||
if (typeof callback !== 'function') {
|
||||
throw error;
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(error);
|
||||
});
|
||||
};
|
||||
|
||||
// Create invalid archive error.
|
||||
var invalidArchiveError = function(asarPath, callback) {
|
||||
var error;
|
||||
error = new Error("Invalid package " + asarPath);
|
||||
if (typeof callback !== 'function') {
|
||||
throw error;
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(error);
|
||||
});
|
||||
};
|
||||
|
||||
// Override APIs that rely on passing file path instead of content to C++.
|
||||
var overrideAPISync = function(module, name, arg) {
|
||||
var old;
|
||||
if (arg == null) {
|
||||
arg = 0;
|
||||
}
|
||||
old = module[name];
|
||||
return module[name] = function() {
|
||||
var archive, asarPath, filePath, isAsar, newPath, p, ref;
|
||||
p = arguments[arg];
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return old.apply(this, arguments);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
invalidArchiveError(asarPath);
|
||||
}
|
||||
newPath = archive.copyFileOut(filePath);
|
||||
if (!newPath) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
arguments[arg] = newPath;
|
||||
return old.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
var overrideAPI = function(module, name, arg) {
|
||||
var old;
|
||||
if (arg == null) {
|
||||
arg = 0;
|
||||
}
|
||||
old = module[name];
|
||||
return module[name] = function() {
|
||||
var archive, asarPath, callback, filePath, isAsar, newPath, p, ref;
|
||||
p = arguments[arg];
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return old.apply(this, arguments);
|
||||
}
|
||||
callback = arguments[arguments.length - 1];
|
||||
if (typeof callback !== 'function') {
|
||||
return overrideAPISync(module, name, arg);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
newPath = archive.copyFileOut(filePath);
|
||||
if (!newPath) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
arguments[arg] = newPath;
|
||||
return old.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
// Override fs APIs.
|
||||
exports.wrapFsWithAsar = function(fs) {
|
||||
var exists, existsSync, internalModuleReadFile, internalModuleStat, lstat, lstatSync, mkdir, mkdirSync, readFile, readFileSync, readdir, readdirSync, realpath, realpathSync, stat, statSync, statSyncNoException, logFDs, logASARAccess;
|
||||
|
||||
logFDs = {};
|
||||
logASARAccess = function(asarPath, filePath, offset) {
|
||||
if (!process.env.ELECTRON_LOG_ASAR_READS) {
|
||||
return;
|
||||
}
|
||||
if (!logFDs[asarPath]) {
|
||||
var logFilename, logPath;
|
||||
const path = require('path');
|
||||
logFilename = path.basename(asarPath, '.asar') + '-access-log.txt';
|
||||
logPath = path.join(require('os').tmpdir(), logFilename);
|
||||
logFDs[asarPath] = fs.openSync(logPath, 'a');
|
||||
console.log('Logging ' + asarPath + ' access to ' + logPath);
|
||||
}
|
||||
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n');
|
||||
};
|
||||
|
||||
lstatSync = fs.lstatSync;
|
||||
fs.lstatSync = function(p) {
|
||||
var archive, asarPath, filePath, isAsar, ref, stats;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return lstatSync(p);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
invalidArchiveError(asarPath);
|
||||
}
|
||||
stats = archive.stat(filePath);
|
||||
if (!stats) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
return asarStatsToFsStats(stats);
|
||||
};
|
||||
lstat = fs.lstat;
|
||||
fs.lstat = function(p, callback) {
|
||||
var archive, asarPath, filePath, isAsar, ref, stats;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return lstat(p, callback);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
stats = getOrCreateArchive(asarPath).stat(filePath);
|
||||
if (!stats) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(null, asarStatsToFsStats(stats));
|
||||
});
|
||||
};
|
||||
statSync = fs.statSync;
|
||||
fs.statSync = function(p) {
|
||||
var isAsar = splitPath(p)[0];
|
||||
if (!isAsar) {
|
||||
return statSync(p);
|
||||
}
|
||||
|
||||
// Do not distinguish links for now.
|
||||
return fs.lstatSync(p);
|
||||
};
|
||||
stat = fs.stat;
|
||||
fs.stat = function(p, callback) {
|
||||
var isAsar = splitPath(p)[0];
|
||||
if (!isAsar) {
|
||||
return stat(p, callback);
|
||||
}
|
||||
|
||||
// Do not distinguish links for now.
|
||||
return process.nextTick(function() {
|
||||
return fs.lstat(p, callback);
|
||||
});
|
||||
};
|
||||
statSyncNoException = fs.statSyncNoException;
|
||||
fs.statSyncNoException = function(p) {
|
||||
var archive, asarPath, filePath, isAsar, ref, stats;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return statSyncNoException(p);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return false;
|
||||
}
|
||||
stats = archive.stat(filePath);
|
||||
if (!stats) {
|
||||
return false;
|
||||
}
|
||||
return asarStatsToFsStats(stats);
|
||||
};
|
||||
realpathSync = fs.realpathSync;
|
||||
fs.realpathSync = function(p) {
|
||||
var archive, asarPath, filePath, isAsar, real, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return realpathSync.apply(this, arguments);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
invalidArchiveError(asarPath);
|
||||
}
|
||||
real = archive.realpath(filePath);
|
||||
if (real === false) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
return path.join(realpathSync(asarPath), real);
|
||||
};
|
||||
realpath = fs.realpath;
|
||||
fs.realpath = function(p, cache, callback) {
|
||||
var archive, asarPath, filePath, isAsar, real, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return realpath.apply(this, arguments);
|
||||
}
|
||||
if (typeof cache === 'function') {
|
||||
callback = cache;
|
||||
cache = void 0;
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
real = archive.realpath(filePath);
|
||||
if (real === false) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
return realpath(asarPath, function(err, p) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(null, path.join(p, real));
|
||||
});
|
||||
};
|
||||
exists = fs.exists;
|
||||
fs.exists = function(p, callback) {
|
||||
var archive, asarPath, filePath, isAsar, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return exists(p, callback);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(archive.stat(filePath) !== false);
|
||||
});
|
||||
};
|
||||
existsSync = fs.existsSync;
|
||||
fs.existsSync = function(p) {
|
||||
var archive, asarPath, filePath, isAsar, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return existsSync(p);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return false;
|
||||
}
|
||||
return archive.stat(filePath) !== false;
|
||||
};
|
||||
readFile = fs.readFile;
|
||||
fs.readFile = function(p, options, callback) {
|
||||
var archive, asarPath, buffer, encoding, fd, filePath, info, isAsar, realPath, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return readFile.apply(this, arguments);
|
||||
}
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = void 0;
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
info = archive.getFileInfo(filePath);
|
||||
if (!info) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
if (info.size === 0) {
|
||||
return process.nextTick(function() {
|
||||
return callback(null, new Buffer(0));
|
||||
});
|
||||
}
|
||||
if (info.unpacked) {
|
||||
realPath = archive.copyFileOut(filePath);
|
||||
return fs.readFile(realPath, options, callback);
|
||||
}
|
||||
if (!options) {
|
||||
options = {
|
||||
encoding: null
|
||||
};
|
||||
} else if (util.isString(options)) {
|
||||
options = {
|
||||
encoding: options
|
||||
};
|
||||
} else if (!util.isObject(options)) {
|
||||
throw new TypeError('Bad arguments');
|
||||
}
|
||||
encoding = options.encoding;
|
||||
buffer = new Buffer(info.size);
|
||||
fd = archive.getFd();
|
||||
if (!(fd >= 0)) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
logASARAccess(asarPath, filePath, info.offset);
|
||||
return fs.read(fd, buffer, 0, info.size, info.offset, function(error) {
|
||||
return callback(error, encoding ? buffer.toString(encoding) : buffer);
|
||||
});
|
||||
};
|
||||
readFileSync = fs.readFileSync;
|
||||
fs.readFileSync = function(p, opts) {
|
||||
// this allows v8 to optimize this function
|
||||
var archive, asarPath, buffer, encoding, fd, filePath, info, isAsar, options, realPath, ref;
|
||||
options = opts;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return readFileSync.apply(this, arguments);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
invalidArchiveError(asarPath);
|
||||
}
|
||||
info = archive.getFileInfo(filePath);
|
||||
if (!info) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
if (info.size === 0) {
|
||||
if (options) {
|
||||
return '';
|
||||
} else {
|
||||
return new Buffer(0);
|
||||
}
|
||||
}
|
||||
if (info.unpacked) {
|
||||
realPath = archive.copyFileOut(filePath);
|
||||
return fs.readFileSync(realPath, options);
|
||||
}
|
||||
if (!options) {
|
||||
options = {
|
||||
encoding: null
|
||||
};
|
||||
} else if (util.isString(options)) {
|
||||
options = {
|
||||
encoding: options
|
||||
};
|
||||
} else if (!util.isObject(options)) {
|
||||
throw new TypeError('Bad arguments');
|
||||
}
|
||||
encoding = options.encoding;
|
||||
buffer = new Buffer(info.size);
|
||||
fd = archive.getFd();
|
||||
if (!(fd >= 0)) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
logASARAccess(asarPath, filePath, info.offset);
|
||||
fs.readSync(fd, buffer, 0, info.size, info.offset);
|
||||
if (encoding) {
|
||||
return buffer.toString(encoding);
|
||||
} else {
|
||||
return buffer;
|
||||
}
|
||||
};
|
||||
readdir = fs.readdir;
|
||||
fs.readdir = function(p, callback) {
|
||||
var archive, asarPath, filePath, files, isAsar, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return readdir.apply(this, arguments);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return invalidArchiveError(asarPath, callback);
|
||||
}
|
||||
files = archive.readdir(filePath);
|
||||
if (!files) {
|
||||
return notFoundError(asarPath, filePath, callback);
|
||||
}
|
||||
return process.nextTick(function() {
|
||||
return callback(null, files);
|
||||
});
|
||||
};
|
||||
readdirSync = fs.readdirSync;
|
||||
fs.readdirSync = function(p) {
|
||||
var archive, asarPath, filePath, files, isAsar, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return readdirSync.apply(this, arguments);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
invalidArchiveError(asarPath);
|
||||
}
|
||||
files = archive.readdir(filePath);
|
||||
if (!files) {
|
||||
notFoundError(asarPath, filePath);
|
||||
}
|
||||
return files;
|
||||
};
|
||||
internalModuleReadFile = process.binding('fs').internalModuleReadFile;
|
||||
process.binding('fs').internalModuleReadFile = function(p) {
|
||||
var archive, asarPath, buffer, fd, filePath, info, isAsar, realPath, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return internalModuleReadFile(p);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
if (!archive) {
|
||||
return void 0;
|
||||
}
|
||||
info = archive.getFileInfo(filePath);
|
||||
if (!info) {
|
||||
return void 0;
|
||||
}
|
||||
if (info.size === 0) {
|
||||
return '';
|
||||
}
|
||||
if (info.unpacked) {
|
||||
realPath = archive.copyFileOut(filePath);
|
||||
return fs.readFileSync(realPath, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
}
|
||||
buffer = new Buffer(info.size);
|
||||
fd = archive.getFd();
|
||||
if (!(fd >= 0)) {
|
||||
return void 0;
|
||||
}
|
||||
logASARAccess(asarPath, filePath, info.offset);
|
||||
fs.readSync(fd, buffer, 0, info.size, info.offset);
|
||||
return buffer.toString('utf8');
|
||||
};
|
||||
internalModuleStat = process.binding('fs').internalModuleStat;
|
||||
process.binding('fs').internalModuleStat = function(p) {
|
||||
var archive, asarPath, filePath, isAsar, ref, stats;
|
||||
ref = splitPath(p), isAsar = ref[0], asarPath = ref[1], filePath = ref[2];
|
||||
if (!isAsar) {
|
||||
return internalModuleStat(p);
|
||||
}
|
||||
archive = getOrCreateArchive(asarPath);
|
||||
|
||||
// -ENOENT
|
||||
if (!archive) {
|
||||
return -34;
|
||||
}
|
||||
stats = archive.stat(filePath);
|
||||
|
||||
// -ENOENT
|
||||
if (!stats) {
|
||||
return -34;
|
||||
}
|
||||
if (stats.isDirectory) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Calling mkdir for directory inside asar archive should throw ENOTDIR
|
||||
// error, but on Windows it throws ENOENT.
|
||||
// This is to work around the recursive looping bug of mkdirp since it is
|
||||
// widely used.
|
||||
if (process.platform === 'win32') {
|
||||
mkdir = fs.mkdir;
|
||||
fs.mkdir = function(p, mode, callback) {
|
||||
var filePath, isAsar, ref;
|
||||
if (typeof mode === 'function') {
|
||||
callback = mode;
|
||||
}
|
||||
ref = splitPath(p), isAsar = ref[0], filePath = ref[2];
|
||||
if (isAsar && filePath.length) {
|
||||
return notDirError(callback);
|
||||
}
|
||||
return mkdir(p, mode, callback);
|
||||
};
|
||||
mkdirSync = fs.mkdirSync;
|
||||
fs.mkdirSync = function(p, mode) {
|
||||
var filePath, isAsar, ref;
|
||||
ref = splitPath(p), isAsar = ref[0], filePath = ref[2];
|
||||
if (isAsar && filePath.length) {
|
||||
notDirError();
|
||||
}
|
||||
return mkdirSync(p, mode);
|
||||
};
|
||||
}
|
||||
overrideAPI(fs, 'open');
|
||||
overrideAPI(child_process, 'execFile');
|
||||
overrideAPISync(process, 'dlopen', 1);
|
||||
overrideAPISync(require('module')._extensions, '.node', 1);
|
||||
overrideAPISync(fs, 'openSync');
|
||||
return overrideAPISync(child_process, 'execFileSync');
|
||||
};
|
||||
})();
|
14
lib/common/asar_init.js
Normal file
14
lib/common/asar_init.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
(function () {
|
||||
return function(process, require, asarSource) {
|
||||
// Make asar.coffee accessible via "require".
|
||||
process.binding('natives').ATOM_SHELL_ASAR = asarSource;
|
||||
|
||||
// Monkey-patch the fs module.
|
||||
require('ATOM_SHELL_ASAR').wrapFsWithAsar(require('fs'));
|
||||
|
||||
// Make graceful-fs work with asar.
|
||||
var source = process.binding('natives');
|
||||
source['original-fs'] = source.fs;
|
||||
return source['fs'] = "var src = '(function (exports, require, module, __filename, __dirname) { ' +\n process.binding('natives')['original-fs'] +\n ' });';\nvar vm = require('vm');\nvar fn = vm.runInThisContext(src, { filename: 'fs.js' });\nfn(exports, require, module);\nvar asar = require('ATOM_SHELL_ASAR');\nasar.wrapFsWithAsar(exports);";
|
||||
};
|
||||
})();
|
47
lib/common/init.js
Normal file
47
lib/common/init.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
const path = require('path');
|
||||
const timers = require('timers');
|
||||
const Module = require('module');
|
||||
|
||||
process.atomBinding = function(name) {
|
||||
try {
|
||||
return process.binding("atom_" + process.type + "_" + name);
|
||||
} catch (error) {
|
||||
if (/No such module/.test(error.message)) {
|
||||
return process.binding("atom_common_" + name);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (!process.env.ELECTRON_HIDE_INTERNAL_MODULES) {
|
||||
// Add common/api/lib to module search paths.
|
||||
Module.globalPaths.push(path.resolve(__dirname, '..', 'api', 'lib'));
|
||||
}
|
||||
|
||||
|
||||
// setImmediate and process.nextTick makes use of uv_check and uv_prepare to
|
||||
// run the callbacks, however since we only run uv loop on requests, the
|
||||
// callbacks wouldn't be called until something else activated the uv loop,
|
||||
// which would delay the callbacks for arbitrary long time. So we should
|
||||
// initiatively activate the uv loop once setImmediate and process.nextTick is
|
||||
// called.
|
||||
var wrapWithActivateUvLoop = function(func) {
|
||||
return function() {
|
||||
process.activateUvLoop();
|
||||
return func.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
process.nextTick = wrapWithActivateUvLoop(process.nextTick);
|
||||
|
||||
global.setImmediate = wrapWithActivateUvLoop(timers.setImmediate);
|
||||
|
||||
global.clearImmediate = timers.clearImmediate;
|
||||
|
||||
if (process.type === 'browser') {
|
||||
// setTimeout needs to update the polling timeout of the event loop, when
|
||||
// called under Chromium's event loop the node's event loop won't get a chance
|
||||
// to update the timeout, so we have to force the node's event loop to
|
||||
// recalculate the timeout in browser process.
|
||||
global.setTimeout = wrapWithActivateUvLoop(timers.setTimeout);
|
||||
global.setInterval = wrapWithActivateUvLoop(timers.setInterval);
|
||||
}
|
36
lib/common/reset-search-paths.js
Normal file
36
lib/common/reset-search-paths.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
const path = require('path');
|
||||
const Module = require('module');
|
||||
|
||||
// Clear Node's global search paths.
|
||||
Module.globalPaths.length = 0;
|
||||
|
||||
// Clear current and parent(init.coffee)'s search paths.
|
||||
module.paths = [];
|
||||
|
||||
module.parent.paths = [];
|
||||
|
||||
// Prevent Node from adding paths outside this app to search paths.
|
||||
Module._nodeModulePaths = function(from) {
|
||||
var dir, i, part, parts, paths, skipOutsidePaths, splitRe, tip;
|
||||
from = path.resolve(from);
|
||||
|
||||
// If "from" is outside the app then we do nothing.
|
||||
skipOutsidePaths = from.startsWith(process.resourcesPath);
|
||||
|
||||
// Following logoic is copied from module.js.
|
||||
splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
|
||||
paths = [];
|
||||
parts = from.split(splitRe);
|
||||
for (tip = i = parts.length - 1; i >= 0; tip = i += -1) {
|
||||
part = parts[tip];
|
||||
if (part === 'node_modules') {
|
||||
continue;
|
||||
}
|
||||
dir = parts.slice(0, tip + 1).join(path.sep);
|
||||
if (skipOutsidePaths && !dir.startsWith(process.resourcesPath)) {
|
||||
break;
|
||||
}
|
||||
paths.push(path.join(dir, 'node_modules'));
|
||||
}
|
||||
return paths;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue