Merge pull request #1761 from hartman:cors_worker
Use shim blob with importScripts to use CORS for Worker
This commit is contained in:
commit
ff88d5aa8d
1 changed files with 36 additions and 10 deletions
|
|
@ -35,7 +35,7 @@ var oop = require("../lib/oop");
|
||||||
var EventEmitter = require("../lib/event_emitter").EventEmitter;
|
var EventEmitter = require("../lib/event_emitter").EventEmitter;
|
||||||
var config = require("../config");
|
var config = require("../config");
|
||||||
|
|
||||||
var WorkerClient = function(topLevelNamespaces, mod, classname) {
|
var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) {
|
||||||
this.$sendDeltaQueue = this.$sendDeltaQueue.bind(this);
|
this.$sendDeltaQueue = this.$sendDeltaQueue.bind(this);
|
||||||
this.changeListener = this.changeListener.bind(this);
|
this.changeListener = this.changeListener.bind(this);
|
||||||
this.onMessage = this.onMessage.bind(this);
|
this.onMessage = this.onMessage.bind(this);
|
||||||
|
|
@ -44,12 +44,11 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
|
||||||
if (require.nameToUrl && !require.toUrl)
|
if (require.nameToUrl && !require.toUrl)
|
||||||
require.toUrl = require.nameToUrl;
|
require.toUrl = require.nameToUrl;
|
||||||
|
|
||||||
var workerUrl;
|
|
||||||
if (config.get("packaged") || !require.toUrl) {
|
if (config.get("packaged") || !require.toUrl) {
|
||||||
workerUrl = config.moduleUrl(mod, "worker");
|
workerUrl = workerUrl || config.moduleUrl(mod, "worker");
|
||||||
} else {
|
} else {
|
||||||
var normalizePath = this.$normalizePath;
|
var normalizePath = this.$normalizePath;
|
||||||
workerUrl = normalizePath(require.toUrl("ace/worker/worker.js", null, "_"));
|
workerUrl = workerUrl || normalizePath(require.toUrl("ace/worker/worker.js", null, "_"));
|
||||||
|
|
||||||
var tlns = {};
|
var tlns = {};
|
||||||
topLevelNamespaces.forEach(function(ns) {
|
topLevelNamespaces.forEach(function(ns) {
|
||||||
|
|
@ -57,12 +56,26 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
this.$worker = new Worker(workerUrl);
|
this.$worker = new Worker(workerUrl);
|
||||||
|
} catch(e) {
|
||||||
|
if (e instanceof window.DOMException) {
|
||||||
|
// Likely same origin problem. Use importScripts from a shim Worker
|
||||||
|
var blob = this.$workerBlob(workerUrl);
|
||||||
|
var URL = window.URL || window.webkitURL;
|
||||||
|
var blobURL = URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
this.$worker = new Worker(blobURL);
|
||||||
|
URL.revokeObjectURL(blobURL);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
this.$worker.postMessage({
|
this.$worker.postMessage({
|
||||||
init : true,
|
init : true,
|
||||||
tlns: tlns,
|
tlns : tlns,
|
||||||
module: mod,
|
module : mod,
|
||||||
classname: classname
|
classname : classname
|
||||||
});
|
});
|
||||||
|
|
||||||
this.callbackId = 1;
|
this.callbackId = 1;
|
||||||
|
|
@ -163,7 +176,20 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
|
||||||
this.call("setValue", [this.$doc.getValue()]);
|
this.call("setValue", [this.$doc.getValue()]);
|
||||||
} else
|
} else
|
||||||
this.emit("change", {data: q});
|
this.emit("change", {data: q});
|
||||||
|
};
|
||||||
|
|
||||||
|
this.$workerBlob = function(workerUrl) {
|
||||||
|
var script = 'importScripts("' + workerUrl + '");';
|
||||||
|
try {
|
||||||
|
var blob = new Blob([script], {'type': 'application/javascript'});
|
||||||
|
} catch (e) { // Backwards-compatibility
|
||||||
|
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
|
||||||
|
var blobBuilder = new BlobBuilder();
|
||||||
|
blobBuilder.append(script);
|
||||||
|
blob = blobBuilder.getBlob('application/javascript');
|
||||||
}
|
}
|
||||||
|
return blob;
|
||||||
|
};
|
||||||
|
|
||||||
}).call(WorkerClient.prototype);
|
}).call(WorkerClient.prototype);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue