Permalink
Browse files

Implement WorkerLocation API.

- Implement WorkerLocation API and use it to populate
  workerCtx.location.
- Lay some superficial groundwork for loading workers from arbitrary
  URLs.
  • Loading branch information...
1 parent c11a059 commit 1bef0d9765349c9f9a2054bdebaf970f3d2c0fc5 @pgriess pgriess committed Jun 1, 2010
Showing with 61 additions and 10 deletions.
  1. +38 −0 lib/webworker-utils.js
  2. +1 −1 lib/webworker.js
  3. +21 −8 libexec/worker.js
  4. +1 −1 test/test-basic-worker.js
View
@@ -3,7 +3,9 @@
var events = require('events');
var msgpack = require('msgpack');
+var path = require('path');
var sys = require('sys');
+var urllib = require('url');
// Symbolic names for our messages types
exports.MSGTYPE_NOOP = 0;
@@ -90,3 +92,39 @@ var MsgStream = function(s) {
sys.inherits(MsgStream, events.EventEmitter);
exports.MsgStream = MsgStream;
+
+// Implement the WorkerLocation interface described in
+// http://www.whatwg.org/specs/web-workers/current-work/#dom-workerlocation-href
+var WorkerLocation = function(url) {
+ var u = urllib.parse(url);
+
+ var portForProto = function(proto) {
+ switch (proto) {
+ case 'http':
+ return 80;
+
+ case 'https':
+ return 443;
+
+ case 'file':
+ return undefined;
+
+ default:
+ sys.debug(
+ 'Unknown protocol \'' + proto + '\'; returning undefined'
+ );
+ return undefined;
+ };
+ };
+
+ this.href = u.href;
+ this.protocol = u.protocol.substring(0, u.protocol.length - 1);
+ this.host = u.host;
+ this.hostname = u.hostname;
+ this.port = (u.port) ? u.port : portForProto(this.protocol);
+ this.pathname = (u.pathname) ? path.normalize(u.pathname) : '/';
+ this.search = (u.search) ? u.search : '';
+ this.hash = (u.hash) ? u.hash : '';
+};
+
+exports.WorkerLocation = WorkerLocation;
View
@@ -174,7 +174,7 @@ var Worker = function(src) {
var cp = child_process.spawn(process.argv[0], [
path.join(__dirname, '..', 'libexec', 'worker.js'),
SOCK_PATH,
- src
+ 'file://' + src
]);
// Save this off because cp.pid will get set to 'null' once the process
View
@@ -19,17 +19,30 @@ var script = process.binding('evals');
var sys = require('sys');
var wwutil = require('webworker-utils');
+var writeError = process.binding('stdio').writeError;
+
if (process.argv.length < 4) {
throw new Error('usage: node worker.js <sock> <script>');
}
var sockPath = process.argv[2];
-var scriptPath = process.argv[3];
+var scriptLoc = new wwutil.WorkerLocation(process.argv[3]);
+
+var scriptObj = undefined;
-var scriptObj = new script.Script(
- fs.readFileSync(scriptPath),
- scriptPath
-);
+switch (scriptLoc.protocol) {
+case 'file':
+ scriptObj = new script.Script(
+ fs.readFileSync(scriptLoc.pathname),
+ scriptLoc.href
+ );
+ break;
+
+default:
+ writeError('Cannot load script from unknown protocol \'' +
+ scriptLoc.protocol);
+ process.exit(1);
+}
var s = net.createConnection(sockPath);
var ms = new wwutil.MsgStream(s);
@@ -71,15 +84,15 @@ var workerCtx = {};
workerCtx.global = workerCtx;
workerCtx.process = process;
workerCtx.require = require;
-workerCtx.__filename = scriptPath;
-workerCtx.__dirname = path.dirname(scriptPath);
+workerCtx.__filename = scriptLoc.pathname;
+workerCtx.__dirname = path.dirname(scriptLoc.pathname);
// Context elements required by the WebWorkers API spec
workerCtx.postMessage = function(msg) {
ms.send([wwutil.MSGTYPE_USER, msg]);
};
workerCtx.self = workerCtx;
-workerCtx.location = scriptPath;
+workerCtx.location = scriptLoc;
workerCtx.close = function() {
process.exit(0);
};
@@ -7,4 +7,4 @@ onmessage = function(msg) {
close();
};
-sys.debug('Running with location=' + self.location);
+sys.debug('Running with location=' + sys.inspect(self.location));

0 comments on commit 1bef0d9

Please sign in to comment.