electron-html-to
Version:
Convert html to html/image using electron
291 lines (206 loc) • 8.75 kB
JavaScript
;
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _child_process = require('child_process');
var _child_process2 = _interopRequireDefault(_child_process);
var _debug = require('debug');
var _debug2 = _interopRequireDefault(_debug);
var _which = require('which');
var _which2 = _interopRequireDefault(_which);
var _sliced = require('sliced');
var _sliced2 = _interopRequireDefault(_sliced);
var _ipc = require('./ipc');
var _ipc2 = _interopRequireDefault(_ipc);
var _saveFile = require('./saveFile');
var _saveFile2 = _interopRequireDefault(_saveFile);
var _packageJson = require('../package.json');
var debugStrategy = _debug2['default'](_packageJson.name + ':dedicated-process-strategy'),
debugElectronLog = _debug2['default'](_packageJson.name + ':electron-log'),
debugPage = _debug2['default'](_packageJson.name + ':page');
var ELECTRON_PATH = undefined;
function getElectronPath() {
var electron = undefined;
if (ELECTRON_PATH) {
debugStrategy('getting electron path from cache');
return ELECTRON_PATH;
}
// first try to find the electron executable if it is installed from `electron`..
electron = getElectronPathFromPackage('electron');
if (electron == null) {
// second try to find the electron executable if it is installed from `electron-prebuilt`..
electron = getElectronPathFromPackage('electron-prebuilt');
}
if (electron == null) {
// last try to find the electron executable, trying using which module
debugStrategy('trying to get electron path from $PATH..');
try {
electron = _which2['default'].sync('electron');
} catch (whichErr) {
throw new Error('Couldn\'t find the path to the electron executable automatically, ' + 'try installing the `electron` or `electron-prebuilt` package, ' + 'or set the `pathToElectron` option to specify the path manually');
}
}
ELECTRON_PATH = electron;
return electron;
}
function getElectronPathFromPackage(moduleName) {
var electronPath = undefined;
try {
debugStrategy('trying to get electron path from "' + moduleName + '" module..');
// eslint-disable-next-line global-require
electronPath = require(moduleName);
return electronPath;
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
return electronPath;
}
throw err;
}
}
exports['default'] = function (options, requestOptions, converterPath, id, cb) {
var tmpDir = options.tmpDir;
var timeout = options.timeout;
var pathToElectron = options.pathToElectron;
var electronArgs = options.electronArgs;
var allowLocalFilesAccess = options.allowLocalFilesAccess;
var maxLogEntrySize = options.maxLogEntrySize;
var settingsFilePath = _path2['default'].resolve(_path2['default'].join(tmpDir, id + 'settings.html'));
var settingsContent = JSON.stringify(_extends({}, requestOptions, { converterPath: converterPath, allowLocalFilesAccess: allowLocalFilesAccess, maxLogEntrySize: maxLogEntrySize }));
debugStrategy('saving settings in temporal file..');
_saveFile2['default'](tmpDir, settingsFilePath, settingsContent, function (saveFileErr) {
var childArgs = [];
var debugMode = false,
isDone = false,
electronPath = undefined,
childOpts = undefined,
child = undefined,
childIpc = undefined,
timeoutId = undefined;
if (saveFileErr) {
return cb(saveFileErr);
}
if (Array.isArray(electronArgs)) {
childArgs = electronArgs.slice();
}
childArgs.unshift(_path2['default'].join(__dirname, 'scripts', 'standaloneScript.js'));
childOpts = {
env: {
ELECTRON_WORKER_ID: id,
ELECTRON_HTML_TO_SETTINGS_FILE_PATH: settingsFilePath,
// propagate the DISPLAY env var to make it work on LINUX
DISPLAY: process.env.DISPLAY
},
stdio: [null, null, null, 'ipc']
};
debugStrategy('searching electron executable path..');
if (pathToElectron) {
debugStrategy('using electron executable path from custom location: %s', pathToElectron);
}
electronPath = pathToElectron || getElectronPath();
if (process.env.ELECTRON_HTML_TO_DEBUGGING !== undefined) {
debugStrategy('electron process debugging mode activated');
debugMode = true;
childOpts.env.ELECTRON_HTML_TO_DEBUGGING = process.env.ELECTRON_HTML_TO_DEBUGGING;
}
if (process.env.ELECTRON_ENABLE_LOGGING !== undefined) {
childOpts.env.ELECTRON_ENABLE_LOGGING = process.env.ELECTRON_ENABLE_LOGGING;
}
if (process.env.IISNODE_VERSION !== undefined) {
debugStrategy('running in IISNODE..');
childOpts.env.IISNODE_VERSION = process.env.IISNODE_VERSION;
}
if (debugMode || process.env.ELECTRON_ENABLE_LOGGING !== undefined || process.env.ELECTRON_HTML_TO_STDSTREAMS !== undefined) {
childOpts.stdio = [null, process.stdout, process.stderr, 'ipc'];
}
debugStrategy('spawning new electron process with args:', childArgs, 'and options:', childOpts);
child = _child_process2['default'].spawn(electronPath, childArgs, childOpts);
debugStrategy('electron process pid:', child.pid);
childIpc = _ipc2['default'](child);
debugStrategy('processing conversion..');
child.on('exit', function (code, signal) {
debugStrategy('electron process exit with code: ' + code + ' and signal: ' + signal);
});
child.on('error', function (err) {
if (isDone) {
return;
}
isDone = true;
debugStrategy('electron process has an error: %s', err.message);
cb(err);
clearTimeout(timeoutId);
if (child.connected) {
child.disconnect();
}
child.kill();
});
childIpc.on('page-error', function (windowId, errMsg, errStack) {
debugPage('An error has ocurred in browser window [%s]: message: %s stack: %s', windowId, errMsg, errStack);
});
childIpc.on('page-log', function () {
// eslint-disable-next-line prefer-rest-params
var newArgs = _sliced2['default'](arguments),
windowId = newArgs.splice(0, 1);
newArgs.unshift('console log from browser window [' + windowId + ']:');
debugPage.apply(debugPage, newArgs);
});
childIpc.on('log', function () {
// eslint-disable-next-line prefer-rest-params
debugElectronLog.apply(debugElectronLog, _sliced2['default'](arguments));
});
childIpc.once('finish', function (err, childData) {
if (isDone) {
return;
}
isDone = true;
clearTimeout(timeoutId);
if (err) {
debugStrategy('conversion ended with error..');
cb(new Error(err));
} else {
// disabling no-undef rule because eslint don't detect object rest spread correctly
/* eslint-disable no-undef */
var output = childData.output;
var restData = _objectWithoutProperties(childData, ['output']);
debugStrategy('conversion ended successfully..');
if (Array.isArray(restData.logs)) {
restData.logs.forEach(function (m) {
// eslint-disable-next-line no-param-reassign
m.timestamp = new Date(m.timestamp);
});
}
cb(null, _extends({}, restData, {
stream: _fs2['default'].createReadStream(output)
}));
/* eslint-enable no-undef */
}
// in debug mode, don't close the electron process
if (!debugMode) {
if (child.connected) {
child.disconnect();
}
child.kill();
}
});
timeoutId = setTimeout(function () {
var timeoutErr = undefined;
if (isDone) {
return;
}
debugStrategy('conversion timeout..');
isDone = true;
timeoutErr = new Error('Timeout when executing in electron');
timeoutErr.electronTimeout = true;
cb(timeoutErr);
if (child.connected) {
child.disconnect();
}
child.kill();
}, requestOptions.timeout || timeout);
});
};
module.exports = exports['default'];