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;
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
* the first.

View File

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

35
make.js
View File

@ -542,8 +542,37 @@ target.bundle = function(args) {
'display/svg.js'
]);
var srcFiles = builder.getWorkerSrcFiles('src/worker_loader.js');
var WORKER_SRC_FILES = srcFiles.srcFiles;
var WORKER_SRC_FILES = [
'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) {
// 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);
}
var EXT_SRC_FILES = srcFiles.externalSrcFiles;
var EXT_SRC_FILES = [];
cd(SRC_DIR);

View File

@ -291,3 +291,17 @@ var NetworkManager = (function NetworkManagerClosure() {
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 = {
setup: function wphSetup(handler, port) {
var testMessageProcessed = false;
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
if (!(data instanceof Uint8Array)) {
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() {
var args = Array.prototype.slice.call(arguments);
globalScope.postMessage({
@ -644,18 +653,21 @@ var workerConsole = {
}
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;
}
//#endif
var handler = new MessageHandler('worker', 'main', 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;

View File

@ -414,7 +414,7 @@ PDFJS.getDocument = function getDocument(src,
var transport = new WorkerTransport(messageHandler, task, rangeTransport);
task._transport = transport;
});
}, task._capability.reject);
}).catch(task._capability.reject);
return task;
};
@ -1186,14 +1186,14 @@ var PDFWorker = (function PDFWorkerClosure() {
// pdf.worker.js file is needed.
//#if !PRODUCTION
if (typeof amdRequire === 'function') {
amdRequire(['pdfjs/core/worker'], function () {
amdRequire(['pdfjs/core/network', 'pdfjs/core/worker'], function () {
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
});
} else if (typeof require === 'function') {
require('../core/worker.js');
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
} else {
Util.loadScript(PDFJS.workerSrc);
throw new Error('AMD or CommonJS must be used to load fake worker.');
}
//#endif
//#if PRODUCTION && SINGLE_FILE
@ -1253,7 +1253,6 @@ var PDFWorker = (function PDFWorkerClosure() {
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
var worker = new Worker(workerSrc);
var messageHandler = new MessageHandler('main', 'worker', worker);
messageHandler.on('test', function PDFWorker_test(data) {
if (this.destroyed) {
this._readyCapability.reject(new Error('Worker was destroyed'));
@ -1284,7 +1283,24 @@ var PDFWorker = (function PDFWorkerClosure() {
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
// typed array. Also, checking if we can use transfers.
try {
@ -1294,6 +1310,13 @@ var PDFWorker = (function PDFWorkerClosure() {
testObj[0] = 0;
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;
} catch (e) {
info('The worker has been disabled.');

View File

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

View File

@ -12,70 +12,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFJS, Util */
'use strict';
// List of shared files to include;
var sharedFiles = [
'shared/global.js',
'shared/util.js'
];
importScripts('../node_modules/requirejs/require.js');
// List of other files to include;
var otherFiles = [
'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'
];
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);
}
require.config({paths: {'pdfjs': '.'}});
require(['pdfjs/core/network', 'pdfjs/core/worker'],
function (network, worker) {
// Worker is loaded at this point.
});