Yurik has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/191883

Change subject: Use github's nyurik/vega, handle relative URLs
......................................................................

Use github's nyurik/vega, handle relative URLs

* Handle relative URLs (without hostname)
* Added config.timeout and config.defaultProtocol settings

Change-Id: Icdc5377059b112f6fe447cc1cff9476eb80d720d
---
M graphoid-worker.js
M graphoid.config.json
M package.json
3 files changed, 39 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/graphoid 
refs/changes/83/191883/1

diff --git a/graphoid-worker.js b/graphoid-worker.js
index 2f8f7b0..1883866 100644
--- a/graphoid-worker.js
+++ b/graphoid-worker.js
@@ -15,7 +15,7 @@
        request = require('request-promise'),
        Promise = require('promise'), // https://www.npmjs.com/package/promise
        http = require('http'),
-       url = require('url'),
+       urllib = require('url'),
        querystring = require('querystring'),
        vega = null; // Visualization grammar - https://github.com/trifacta/vega
 
@@ -23,7 +23,7 @@
 try{
        vega = require('vega');
 } catch(err) {
-       console.log(err);
+       console.error(err);
 }
 
 var config;
@@ -31,8 +31,12 @@
 // Get the config
 try {
        config = require('./graphoid.config.json');
-} catch ( e ) {
-       console.error('Please set up your graphoid.config.json');
+       if (!config.hasOwnProperty('domains') || !(config.domains instanceof 
Array) || config.domains.length == 0) {
+               throw 'The config "domains" value must be a non-empty list of 
domains';
+       }
+} catch (err) {
+       console.error('Error loading graphoid.config.json');
+       console.error(err);
        process.exit(1);
 }
 
@@ -40,6 +44,7 @@
 
 if (vega) {
        vega.config.domainWhiteList = config.domains;
+       vega.config.defaultProtocol = config.defaultProtocol || 'http:';
        vega.config.safeMode = true;
 }
 
@@ -47,7 +52,6 @@
 function merge() {
        var result = {},
                args = Array.prototype.slice.apply(arguments);
-       console.log(args);
 
        args.forEach(function (arg) {
                Object.getOwnPropertyNames(arg).forEach(function (prop) {
@@ -67,10 +71,10 @@
        processResult = function (response) {
                var body = JSON.parse(response);
                if (body.hasOwnProperty('error')) {
-                       throw 'API result error: ' + body.error;
+                       throw 'API result error: ' + JSON.stringify(body.error);
                }
                if (body.hasOwnProperty('warnings')) {
-                       console.log('API warning: ' + 
JSON.stringify(body.warnings) + ' while getting ' + JSON.stringify({
+                       console.error('API warning: ' + 
JSON.stringify(body.warnings) + ' while getting ' + JSON.stringify({
                                url: url,
                                opts: qs
                        }));
@@ -110,7 +114,7 @@
                return request(reqOpts)
                        .then(processResult)
                        .catch(function (reason) {
-                               console.log(JSON.stringify(reqOpts));
+                               console.error(JSON.stringify(reqOpts));
                                throw reason; // re-throw
                        });
        };
@@ -121,7 +125,7 @@
 }
 
 function validateRequest(req) {
-       var query = url.parse(req.url, true).query;
+       var query = urllib.parse(req.url, true).query;
 
        if (!query.hasOwnProperty('revid')) {
                throw 'no revid';
@@ -158,16 +162,14 @@
        };
 }
 
-function renderOnCanvas(spec, response) {
+function renderOnCanvas(spec, server, response) {
        return new Promise(function (fulfill, reject){
                if (!vega) {
                        throw 'Unable to load Vega npm module';
                }
 
-               // TODO: hack for local testing
-               // TODO: weird, but this only works some of the time
-               spec.data[1].url = 'http://www.mediawiki.org' + 
spec.data[1].url;
-               console.log(spec);
+               // In case of non-absolute URLs, use requesting server as 
"local"
+               vega.config.baseURL = vega.config.defaultProtocol + '//' + 
server;
 
                vega.headless.render({spec: spec, renderer: 'canvas'}, function 
(err, result) {
                        if (err) {
@@ -222,13 +224,13 @@
                }).then(function (spec) {
                        // render graph on canvas
                        // bug: timeout might happen right in the middle of 
streaming
-                       return renderOnCanvas(spec, response);
+                       return renderOnCanvas(spec, params.server, response);
                });
 
-       // Limit request to 10 seconds, handle all errors
-       timeout(render, 10000)
+       // Limit request to 10 seconds by default, handle all errors
+       timeout(render, config.timeout || 10000)
                .catch(function (reason) {
-                       console.log(reason + (params ? '\n' + 
JSON.stringify(params) : ''));
+                       console.error(reason + (params ? '\n' + 
JSON.stringify(params) : ''));
                        response.writeHead(400);
                        response.end(JSON.stringify(reason));
                });
diff --git a/graphoid.config.json b/graphoid.config.json
index 7f1d795..5d2799d 100644
--- a/graphoid.config.json
+++ b/graphoid.config.json
@@ -1,5 +1,7 @@
 {
   "domains": [
+    "localhost",
+    "127.0.0.1",
     "mediawiki.org",
     "wikibooks.org",
     "wikidata.org",
diff --git a/package.json b/package.json
index 07aec3a..959cf97 100644
--- a/package.json
+++ b/package.json
@@ -1,21 +1,30 @@
 {
-  "name": "graphoid",
+  "name": "Graphoid",
   "version": "0.1.0",
   "description": "renders vega graphs from mediawiki pages",
+  "homepage": "https://www.mediawiki.org/wiki/Extension:Graph";,
   "keywords": [
     "vega",
     "graph",
     "mediawiki"
   ],
-  "author": "Yuri Astrakhan",
+  "author": "Yuri Astrakhan <yurik at wikimedia org>",
   "contributors": [
     "Dan Andreescu <[email protected]>"
   ],
-  "dependencies": {
-    "express": "== 4.11.2",
-    "request-promise": "== 0.4.0",
-    "promise": "== 6.1.0",
-    "vega": "== 1.4.3"
+  "repository": {
+    "type": "git",
+    "url": "https://gerrit.wikimedia.org/r/mediawiki/services/graphoid";
   },
-  "license": "MIT"
+  "bugs": {
+    "url": "https://phabricator.wikimedia.org/tag/services/";
+  },
+  "dependencies": {
+    "express": "^4.11.2",
+    "request-promise": "^0.4.0",
+    "promise": "^6.1.0",
+    "vega": "git+http://[email protected]/nyurik/vega";
+  },
+  "license": "MIT",
+  "private": true
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/191883
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icdc5377059b112f6fe447cc1cff9476eb80d720d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/graphoid
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to