Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #550 from d00rman/mathoid/texvcinfo
Mathoid: store texvcinfo check results and reuse them
- Loading branch information
Showing
9 changed files
with
293 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
'use strict'; | ||
|
||
|
||
var P = require('bluebird'); | ||
var HyperSwitch = require('hyperswitch'); | ||
var URI = HyperSwitch.URI; | ||
var HTTPError = HyperSwitch.HTTPError; | ||
|
||
|
||
function MathoidService(options) { | ||
|
||
this.options = options; | ||
|
||
} | ||
|
||
|
||
MathoidService.prototype.checkInput = function(hyper, req) { | ||
|
||
var self = this; | ||
var rp = req.params; | ||
var hash; | ||
var origHash; | ||
var checkRes; | ||
|
||
// start by calculating the hash | ||
return hyper.post({ | ||
uri: new URI([rp.domain, 'sys', 'post_data', 'mathoid.input', 'hash']), | ||
body: { q: req.body.q, type: rp.type } | ||
}).then(function(res) { | ||
hash = origHash = res.body; | ||
// short-circuit if it's a no-cache request | ||
if (req.headers && /no-cache/.test(req.headers['cache-control'])) { | ||
return P.reject(new HTTPError({ status: 404 })); | ||
} | ||
// check the post storage | ||
return hyper.get({ | ||
uri: new URI([rp.domain, 'sys', 'key_value', 'mathoid.check', hash]) | ||
}).catch({ status: 404 }, function(err) { | ||
// let's try to find an indirection | ||
return hyper.get({ | ||
uri: new URI([rp.domain, 'sys', 'key_value', 'mathoid.hash_table', hash]) | ||
}).then(function(hashRes) { | ||
// we have a normalised version of the formula | ||
hash = hashRes.body; | ||
// grab that version from storage | ||
return hyper.get({ | ||
uri: new URI([rp.domain, 'sys', 'key_value', 'mathoid.check', hash]) | ||
}); | ||
}); | ||
}); | ||
}).catch({ status: 404 }, function() { | ||
// if we are here, it means this is a new input formula | ||
// so call mathoid | ||
return hyper.post({ | ||
uri: self.options.host + '/texvcinfo', | ||
headers: { 'content-type': 'application/json' }, | ||
body: { | ||
q: req.body.q, | ||
type: rp.type | ||
} | ||
}).then(function(res) { | ||
checkRes = res; | ||
// store the normalised version | ||
return hyper.put({ | ||
uri: new URI([rp.domain, 'sys', 'post_data', 'mathoid.input', '']), | ||
headers: { 'content-type': 'application/json' }, | ||
body: { | ||
q: res.body.checked, | ||
type: rp.type | ||
} | ||
}); | ||
}).then(function(res) { | ||
var indirectionP = P.resolve(); | ||
hash = res.body; | ||
// add the indirection to the hash table if the hashes don't match | ||
if (hash !== origHash) { | ||
indirectionP = hyper.put({ | ||
uri: new URI([rp.domain, 'sys', 'key_value', 'mathoid.hash_table', origHash]), | ||
headers: { 'content-type': 'text/plain' }, | ||
body: hash | ||
}); | ||
} | ||
// store the result | ||
checkRes.headers = { | ||
'content-type': 'application/json', | ||
'cache-control': 'no-cache', | ||
'x-resource-location': hash | ||
}; | ||
return P.join( | ||
hyper.put({ | ||
uri: new URI([rp.domain, 'sys', 'key_value', 'mathoid.check', hash]), | ||
headers: checkRes.headers, | ||
body: checkRes.body | ||
}), | ||
indirectionP, | ||
function() { | ||
return checkRes; | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
}; | ||
|
||
|
||
module.exports = function(options) { | ||
|
||
var mathoidSrv = new MathoidService(options); | ||
|
||
return { | ||
spec: { | ||
paths: { | ||
'/check/{type}': { | ||
post: { | ||
operationId: 'checkInput' | ||
} | ||
} | ||
} | ||
}, | ||
operations: { | ||
checkInput: mathoidSrv.checkInput.bind(mathoidSrv) | ||
}, | ||
resources: [ | ||
{ | ||
uri: '/{domain}/sys/post_data/mathoid.input' | ||
}, { | ||
uri: '/{domain}/sys/key_value/mathoid.hash_table', | ||
body: { valueType: 'string' } | ||
}, { | ||
uri: '/{domain}/sys/key_value/mathoid.check', | ||
body: { valueType: 'json' } | ||
} | ||
] | ||
}; | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
|
||
var assert = require('../utils/assert.js'); | ||
var server = require('../utils/server.js'); | ||
var preq = require('preq'); | ||
var P = require('bluebird'); | ||
|
||
|
||
describe('Mathoid', function() { | ||
|
||
var f = 'c^2 = a^2 + b^2'; | ||
var nf = 'c^{2}=a^{2}+b^{2}'; | ||
var uri = server.config.hostPort + '/wikimedia.org/v1/media/math'; | ||
|
||
this.timeout(20000); | ||
|
||
before(function () { return server.start(); }); | ||
|
||
it('checks the formula with Mathoid', function() { | ||
var slice = server.config.logStream.slice(); | ||
return preq.post({ | ||
uri: uri + '/check/tex', | ||
headers: { 'content-type': 'application/json' }, | ||
body: { q: f } | ||
}).then(function(res) { | ||
slice.halt(); | ||
assert.localRequests(slice, false); | ||
assert.remoteRequests(slice, true); | ||
assert.checkString(res.headers['cache-control'], 'no-cache'); | ||
}); | ||
}); | ||
|
||
it('retrieves the check output from storage', function() { | ||
var slice = server.config.logStream.slice(); | ||
return preq.post({ | ||
uri: uri + '/check/tex', | ||
headers: { 'content-type': 'application/json' }, | ||
body: { q: f } | ||
}).then(function(res) { | ||
slice.halt(); | ||
assert.localRequests(slice, true); | ||
assert.remoteRequests(slice, false); | ||
assert.checkString(res.headers['cache-control'], 'no-cache'); | ||
}); | ||
}); | ||
|
||
it('retrieves the check output of the normalised version', function() { | ||
var slice = server.config.logStream.slice(); | ||
return preq.post({ | ||
uri: uri + '/check/tex', | ||
headers: { 'content-type': 'application/json' }, | ||
body: { q: nf } | ||
}).then(function(res) { | ||
slice.halt(); | ||
assert.localRequests(slice, true); | ||
assert.remoteRequests(slice, false); | ||
assert.checkString(res.headers['cache-control'], 'no-cache'); | ||
}); | ||
}); | ||
|
||
it('ignores stored version for no-cache', function() { | ||
var slice = server.config.logStream.slice(); | ||
return preq.post({ | ||
uri: uri + '/check/tex', | ||
headers: { | ||
'content-type': 'application/json', | ||
'cache-control': 'no-cache' | ||
}, | ||
body: { q: f } | ||
}).then(function(res) { | ||
slice.halt(); | ||
assert.localRequests(slice, false); | ||
assert.remoteRequests(slice, true); | ||
assert.checkString(res.headers['cache-control'], 'no-cache'); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.