Adds RequireJS to worker.

This commit is contained in:
Yury Delendik 2015-12-16 18:37:43 -06:00
parent 85e95d34ed
commit fc3282db56
8 changed files with 143 additions and 152 deletions

View File

@ -303,36 +303,6 @@ function build(setup) {
} }
exports.build = build; exports.build = build;
function getWorkerSrcFiles(filePath) {
var src = fs.readFileSync(filePath).toString();
var reSrcFiles = /var\s+otherFiles\s*=\s*(\[[^\]]*\])/;
var match = reSrcFiles.exec(src);
if (!match) {
throw new Error('Cannot find otherFiles array in ' + filePath);
}
var files = match[1].replace(/'/g, '"').replace(/^\s*\/\/.*/gm, '')
.replace(/,\s*]$/, ']');
try {
files = JSON.parse(files);
} catch (e) {
throw new Error('Failed to parse otherFiles in ' + filePath + ' as JSON, ' +
e);
}
var srcFiles = files.filter(function(name) {
return name.indexOf('external') === -1;
});
var externalSrcFiles = files.filter(function(name) {
return name.indexOf('external') > -1;
});
return {
srcFiles: srcFiles,
externalSrcFiles: externalSrcFiles
};
}
exports.getWorkerSrcFiles = getWorkerSrcFiles;
/** /**
* Merge two defines arrays. Values in the second param will override values in * Merge two defines arrays. Values in the second param will override values in
* the first. * the first.

View File

@ -59,7 +59,7 @@ function parseUmd(filePath) {
var umdStart = '\\(function\\s\\(root,\\sfactory\\)\\s\\{'; var umdStart = '\\(function\\s\\(root,\\sfactory\\)\\s\\{';
var umdImports = '\\}\\(this,\\sfunction\\s\\(exports\\b'; var umdImports = '\\}\\(this,\\sfunction\\s\\(exports\\b';
var umdBody = '\\)\\s\\{'; var umdBody = '\\)\\s\\{';
var umdEnd = '\\}\\)\\);\\s*$'; var umdEnd = '\\}\\)\\);\\s*(//#endif\\s*)?$';
var m, re; var m, re;
m = new RegExp(umdStart + '([\\s\\S]*?)' + umdImports + '([\\s\\S]*?)' + m = new RegExp(umdStart + '([\\s\\S]*?)' + umdImports + '([\\s\\S]*?)' +
umdBody + '([\\s\\S]*?)' + umdEnd).exec(jscode); umdBody + '([\\s\\S]*?)' + umdEnd).exec(jscode);

35
make.js
View File

@ -542,8 +542,37 @@ target.bundle = function(args) {
'display/svg.js' 'display/svg.js'
]); ]);
var srcFiles = builder.getWorkerSrcFiles('src/worker_loader.js'); var WORKER_SRC_FILES = [
var WORKER_SRC_FILES = srcFiles.srcFiles; 'core/network.js',
'core/arithmetic_decoder.js',
'core/charsets.js',
'core/glyphlist.js',
'core/jpg.js',
'core/metrics.js',
'core/bidi.js',
'core/chunked_stream.js',
'core/jbig2.js',
'core/jpx.js',
'core/murmurhash3.js',
'core/primitives.js',
'core/stream.js',
'core/crypto.js',
'core/font_renderer.js',
'core/parser.js',
'core/cmap.js',
'core/obj.js',
'core/ps_parser.js',
'core/fonts.js',
'core/function.js',
'core/colorspace.js',
'core/image.js',
'core/pattern.js',
'core/evaluator.js',
'core/annotation.js',
'core/document.js',
'core/pdf_manager.js',
'core/worker.js'
];
if (!defines.SINGLE_FILE) { if (!defines.SINGLE_FILE) {
// We want shared_src_files in both pdf.js and pdf.worker.js // We want shared_src_files in both pdf.js and pdf.worker.js
@ -555,7 +584,7 @@ target.bundle = function(args) {
MAIN_SRC_FILES = MAIN_SRC_FILES.concat(WORKER_SRC_FILES); MAIN_SRC_FILES = MAIN_SRC_FILES.concat(WORKER_SRC_FILES);
} }
var EXT_SRC_FILES = srcFiles.externalSrcFiles; var EXT_SRC_FILES = [];
cd(SRC_DIR); cd(SRC_DIR);

View File

@ -291,3 +291,17 @@ var NetworkManager = (function NetworkManagerClosure() {
return NetworkManager; return NetworkManager;
})(); })();
//#if !(FIREFOX || MOZCENTRAL)
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs/core/network', ['exports'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports);
} else {
factory((root.pdfjsCoreNetwork = {}));
}
}(this, function (exports) {
exports.NetworkManager = NetworkManager;
}));
//#endif

View File

@ -84,7 +84,13 @@ var WorkerTask = (function WorkerTaskClosure() {
var WorkerMessageHandler = PDFJS.WorkerMessageHandler = { var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
setup: function wphSetup(handler, port) { setup: function wphSetup(handler, port) {
var testMessageProcessed = false;
handler.on('test', function wphSetupTest(data) { handler.on('test', function wphSetupTest(data) {
if (testMessageProcessed) {
return; // we already processed 'test' message once
}
testMessageProcessed = true;
// check if Uint8Array can be sent to worker // check if Uint8Array can be sent to worker
if (!(data instanceof Uint8Array)) { if (!(data instanceof Uint8Array)) {
handler.send('test', 'main', false); handler.send('test', 'main', false);
@ -611,9 +617,12 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
} }
}; };
var consoleTimer = {}; function initializeWorker() {
//#if !MOZCENTRAL
if (!('console' in globalScope)) {
var consoleTimer = {};
var workerConsole = { var workerConsole = {
log: function log() { log: function log() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
globalScope.postMessage({ globalScope.postMessage({
@ -644,18 +653,21 @@ var workerConsole = {
} }
this.log('Timer:', name, Date.now() - time); this.log('Timer:', name, Date.now() - time);
} }
}; };
// Worker thread?
if (typeof window === 'undefined' &&
!(typeof module !== 'undefined' && module.require)) {
if (!('console' in globalScope)) {
globalScope.console = workerConsole; globalScope.console = workerConsole;
} }
//#endif
var handler = new MessageHandler('worker', 'main', self); var handler = new MessageHandler('worker', 'main', self);
WorkerMessageHandler.setup(handler, self); WorkerMessageHandler.setup(handler, self);
handler.send('ready', null);
}
// Worker thread (and not node.js)?
if (typeof window === 'undefined' &&
!(typeof module !== 'undefined' && module.require)) {
initializeWorker();
} }
exports.WorkerTask = WorkerTask; exports.WorkerTask = WorkerTask;

View File

@ -414,7 +414,7 @@ PDFJS.getDocument = function getDocument(src,
var transport = new WorkerTransport(messageHandler, task, rangeTransport); var transport = new WorkerTransport(messageHandler, task, rangeTransport);
task._transport = transport; task._transport = transport;
}); });
}, task._capability.reject); }).catch(task._capability.reject);
return task; return task;
}; };
@ -1186,14 +1186,14 @@ var PDFWorker = (function PDFWorkerClosure() {
// pdf.worker.js file is needed. // pdf.worker.js file is needed.
//#if !PRODUCTION //#if !PRODUCTION
if (typeof amdRequire === 'function') { if (typeof amdRequire === 'function') {
amdRequire(['pdfjs/core/worker'], function () { amdRequire(['pdfjs/core/network', 'pdfjs/core/worker'], function () {
PDFJS.fakeWorkerFilesLoadedCapability.resolve(); PDFJS.fakeWorkerFilesLoadedCapability.resolve();
}); });
} else if (typeof require === 'function') { } else if (typeof require === 'function') {
require('../core/worker.js'); require('../core/worker.js');
PDFJS.fakeWorkerFilesLoadedCapability.resolve(); PDFJS.fakeWorkerFilesLoadedCapability.resolve();
} else { } else {
Util.loadScript(PDFJS.workerSrc); throw new Error('AMD or CommonJS must be used to load fake worker.');
} }
//#endif //#endif
//#if PRODUCTION && SINGLE_FILE //#if PRODUCTION && SINGLE_FILE
@ -1253,7 +1253,6 @@ var PDFWorker = (function PDFWorkerClosure() {
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280 // https://bugzilla.mozilla.org/show_bug.cgi?id=683280
var worker = new Worker(workerSrc); var worker = new Worker(workerSrc);
var messageHandler = new MessageHandler('main', 'worker', worker); var messageHandler = new MessageHandler('main', 'worker', worker);
messageHandler.on('test', function PDFWorker_test(data) { messageHandler.on('test', function PDFWorker_test(data) {
if (this.destroyed) { if (this.destroyed) {
this._readyCapability.reject(new Error('Worker was destroyed')); this._readyCapability.reject(new Error('Worker was destroyed'));
@ -1284,7 +1283,24 @@ var PDFWorker = (function PDFWorkerClosure() {
console.error.apply(console, data); console.error.apply(console, data);
}); });
var testObj = new Uint8Array([PDFJS.postMessageTransfers ? 255 : 0]); messageHandler.on('ready', function (data) {
if (this.destroyed) {
this._readyCapability.reject(new Error('Worker was destroyed'));
messageHandler.destroy();
worker.terminate();
return; // worker was destroyed
}
try {
sendTest();
} catch (e) {
// We need fallback to a faked worker.
this._setupFakeWorker();
}
}.bind(this));
var sendTest = function () {
var testObj = new Uint8Array(
[PDFJS.postMessageTransfers ? 255 : 0]);
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the // Some versions of Opera throw a DATA_CLONE_ERR on serializing the
// typed array. Also, checking if we can use transfers. // typed array. Also, checking if we can use transfers.
try { try {
@ -1294,6 +1310,13 @@ var PDFWorker = (function PDFWorkerClosure() {
testObj[0] = 0; testObj[0] = 0;
messageHandler.send('test', testObj); messageHandler.send('test', testObj);
} }
};
// It might take time for worker to initialize (especially when AMD
// loader is used). We will try to send test immediately, and then
// when 'ready' message will arrive. The worker shall process only
// first received 'test'.
sendTest();
return; return;
} catch (e) { } catch (e) {
info('The worker has been disabled.'); info('The worker has been disabled.');

View File

@ -16,6 +16,7 @@
'use strict'; 'use strict';
//#if (GENERIC || SINGLE_FILE)
(function (root, factory) { (function (root, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
define('pdfjs/display/svg', ['exports', 'pdfjs/shared/util'], factory); define('pdfjs/display/svg', ['exports', 'pdfjs/shared/util'], factory);
@ -25,7 +26,6 @@
factory((root.pdfjsDisplaySVG = {}), root.pdfjsSharedUtil); factory((root.pdfjsDisplaySVG = {}), root.pdfjsSharedUtil);
} }
}(this, function (exports, sharedUtil) { }(this, function (exports, sharedUtil) {
//#if (GENERIC || SINGLE_FILE)
var FONT_IDENTITY_MATRIX = sharedUtil.FONT_IDENTITY_MATRIX; var FONT_IDENTITY_MATRIX = sharedUtil.FONT_IDENTITY_MATRIX;
var IDENTITY_MATRIX = sharedUtil.IDENTITY_MATRIX; var IDENTITY_MATRIX = sharedUtil.IDENTITY_MATRIX;
@ -1209,5 +1209,5 @@ var SVGGraphics = (function SVGGraphicsClosure() {
PDFJS.SVGGraphics = SVGGraphics; PDFJS.SVGGraphics = SVGGraphics;
exports.SVGGraphics = SVGGraphics; exports.SVGGraphics = SVGGraphics;
//#endif
})); }));
//#endif

View File

@ -12,70 +12,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFJS, Util */
'use strict'; 'use strict';
// List of shared files to include; importScripts('../node_modules/requirejs/require.js');
var sharedFiles = [
'shared/global.js',
'shared/util.js'
];
// List of other files to include; require.config({paths: {'pdfjs': '.'}});
var otherFiles = [ require(['pdfjs/core/network', 'pdfjs/core/worker'],
'core/network.js', function (network, worker) {
'core/arithmetic_decoder.js', // Worker is loaded at this point.
'core/charsets.js', });
'core/glyphlist.js',
'core/jpg.js',
'core/metrics.js',
'core/bidi.js',
'core/chunked_stream.js',
'core/jbig2.js',
'core/jpx.js',
'core/murmurhash3.js',
'core/primitives.js',
'core/stream.js',
'core/crypto.js',
'core/font_renderer.js',
'core/parser.js',
'core/cmap.js',
'core/obj.js',
'core/ps_parser.js',
'core/fonts.js',
'core/function.js',
'core/colorspace.js',
'core/image.js',
'core/pattern.js',
'core/evaluator.js',
'core/annotation.js',
'core/document.js',
'core/pdf_manager.js',
'core/worker.js'
];
function loadInOrder(index, path, files) {
if (index >= files.length) {
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
return;
}
PDFJS.Util.loadScript(path + files[index],
loadInOrder.bind(null, ++index, path, files));
}
// Load all the files.
if (typeof PDFJS === 'undefined' || !PDFJS.fakeWorkerFilesLoadedCapability) {
var files = sharedFiles.concat(otherFiles);
for (var i = 0; i < files.length; i++) {
importScripts(files[i]);
}
} else {
var src = PDFJS.workerSrc;
var path = src.substr(0, src.indexOf('worker_loader.js'));
// If Util is available, we assume that shared files are already loaded. Can
// happen that they are not if PDF.js is bundled inside a special namespace.
var skipShared = typeof Util !== 'undefined';
var files = skipShared ? otherFiles : sharedFiles.concat(otherFiles);
loadInOrder(0, path, files);
}